OSDN Git Service

PR middle-end/18164
[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, bool, 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, bool, 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_committed_to_tentative_parse
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 *, int);
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_parsing_tentatively (parser)
1842       && !cp_parser_committed_to_tentative_parse (parser))
1843     {
1844       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1845       return true;
1846     }
1847   return false;
1848 }
1849
1850 /* This function is called when a type is defined.  If type
1851    definitions are forbidden at this point, an error message is
1852    issued.  */
1853
1854 static void
1855 cp_parser_check_type_definition (cp_parser* parser)
1856 {
1857   /* If types are forbidden here, issue a message.  */
1858   if (parser->type_definition_forbidden_message)
1859     /* Use `%s' to print the string in case there are any escape
1860        characters in the message.  */
1861     error ("%s", parser->type_definition_forbidden_message);
1862 }
1863
1864 /* This function is called when a declaration is parsed.  If
1865    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1866    indicates that a type was defined in the decl-specifiers for DECL,
1867    then an error is issued.  */
1868
1869 static void
1870 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1871                                                int declares_class_or_enum)
1872 {
1873   /* [dcl.fct] forbids type definitions in return types.
1874      Unfortunately, it's not easy to know whether or not we are
1875      processing a return type until after the fact.  */
1876   while (declarator
1877          && (declarator->kind == cdk_pointer
1878              || declarator->kind == cdk_reference
1879              || declarator->kind == cdk_ptrmem))
1880     declarator = declarator->declarator;
1881   if (declarator
1882       && declarator->kind == cdk_function
1883       && declares_class_or_enum & 2)
1884     error ("new types may not be defined in a return type");
1885 }
1886
1887 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1888    "<" in any valid C++ program.  If the next token is indeed "<",
1889    issue a message warning the user about what appears to be an
1890    invalid attempt to form a template-id.  */
1891
1892 static void
1893 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1894                                          tree type)
1895 {
1896   cp_token_position start = 0;
1897
1898   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1899     {
1900       if (TYPE_P (type))
1901         error ("%qT is not a template", type);
1902       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1903         error ("%qE is not a template", type);
1904       else
1905         error ("invalid template-id");
1906       /* Remember the location of the invalid "<".  */
1907       if (cp_parser_parsing_tentatively (parser)
1908           && !cp_parser_committed_to_tentative_parse (parser))
1909         start = cp_lexer_token_position (parser->lexer, true);
1910       /* Consume the "<".  */
1911       cp_lexer_consume_token (parser->lexer);
1912       /* Parse the template arguments.  */
1913       cp_parser_enclosed_template_argument_list (parser);
1914       /* Permanently remove the invalid template arguments so that
1915          this error message is not issued again.  */
1916       if (start)
1917         cp_lexer_purge_tokens_after (parser->lexer, start);
1918     }
1919 }
1920
1921 /* If parsing an integral constant-expression, issue an error message
1922    about the fact that THING appeared and return true.  Otherwise,
1923    return false, marking the current expression as non-constant.  */
1924
1925 static bool
1926 cp_parser_non_integral_constant_expression (cp_parser  *parser,
1927                                             const char *thing)
1928 {
1929   if (parser->integral_constant_expression_p)
1930     {
1931       if (!parser->allow_non_integral_constant_expression_p)
1932         {
1933           error ("%s cannot appear in a constant-expression", thing);
1934           return true;
1935         }
1936       parser->non_integral_constant_expression_p = true;
1937     }
1938   return false;
1939 }
1940
1941 /* Emit a diagnostic for an invalid type name. Consider also if it is
1942    qualified or not and the result of a lookup, to provide a better
1943    message.  */
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 && cp_parser_parsing_tentatively (parser)
2077       && !cp_parser_committed_to_tentative_parse (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, /*complain=*/0);
2299       if (result == error_mark_node)
2300         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2301       return result;
2302     }
2303   return make_typename_type (scope, id, tf_error);
2304 }
2305
2306
2307 /* Create a new C++ parser.  */
2308
2309 static cp_parser *
2310 cp_parser_new (void)
2311 {
2312   cp_parser *parser;
2313   cp_lexer *lexer;
2314   unsigned i;
2315
2316   /* cp_lexer_new_main is called before calling ggc_alloc because
2317      cp_lexer_new_main might load a PCH file.  */
2318   lexer = cp_lexer_new_main ();
2319
2320   /* Initialize the binops_by_token so that we can get the tree
2321      directly from the token.  */
2322   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2323     binops_by_token[binops[i].token_type] = binops[i];
2324
2325   parser = GGC_CNEW (cp_parser);
2326   parser->lexer = lexer;
2327   parser->context = cp_parser_context_new (NULL);
2328
2329   /* For now, we always accept GNU extensions.  */
2330   parser->allow_gnu_extensions_p = 1;
2331
2332   /* The `>' token is a greater-than operator, not the end of a
2333      template-id.  */
2334   parser->greater_than_is_operator_p = true;
2335
2336   parser->default_arg_ok_p = true;
2337
2338   /* We are not parsing a constant-expression.  */
2339   parser->integral_constant_expression_p = false;
2340   parser->allow_non_integral_constant_expression_p = false;
2341   parser->non_integral_constant_expression_p = false;
2342
2343   /* Local variable names are not forbidden.  */
2344   parser->local_variables_forbidden_p = false;
2345
2346   /* We are not processing an `extern "C"' declaration.  */
2347   parser->in_unbraced_linkage_specification_p = false;
2348
2349   /* We are not processing a declarator.  */
2350   parser->in_declarator_p = false;
2351
2352   /* We are not processing a template-argument-list.  */
2353   parser->in_template_argument_list_p = false;
2354
2355   /* We are not in an iteration statement.  */
2356   parser->in_iteration_statement_p = false;
2357
2358   /* We are not in a switch statement.  */
2359   parser->in_switch_statement_p = false;
2360
2361   /* We are not parsing a type-id inside an expression.  */
2362   parser->in_type_id_in_expr_p = false;
2363
2364   /* Declarations aren't implicitly extern "C".  */
2365   parser->implicit_extern_c = false;
2366
2367   /* String literals should be translated to the execution character set.  */
2368   parser->translate_strings_p = true;
2369
2370   /* The unparsed function queue is empty.  */
2371   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2372
2373   /* There are no classes being defined.  */
2374   parser->num_classes_being_defined = 0;
2375
2376   /* No template parameters apply.  */
2377   parser->num_template_parameter_lists = 0;
2378
2379   return parser;
2380 }
2381
2382 /* Create a cp_lexer structure which will emit the tokens in CACHE
2383    and push it onto the parser's lexer stack.  This is used for delayed
2384    parsing of in-class method bodies and default arguments, and should
2385    not be confused with tentative parsing.  */
2386 static void
2387 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2388 {
2389   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2390   lexer->next = parser->lexer;
2391   parser->lexer = lexer;
2392
2393   /* Move the current source position to that of the first token in the
2394      new lexer.  */
2395   cp_lexer_set_source_position_from_token (lexer->next_token);
2396 }
2397
2398 /* Pop the top lexer off the parser stack.  This is never used for the
2399    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2400 static void
2401 cp_parser_pop_lexer (cp_parser *parser)
2402 {
2403   cp_lexer *lexer = parser->lexer;
2404   parser->lexer = lexer->next;
2405   cp_lexer_destroy (lexer);
2406
2407   /* Put the current source position back where it was before this
2408      lexer was pushed.  */
2409   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2410 }
2411
2412 /* Lexical conventions [gram.lex]  */
2413
2414 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2415    identifier.  */
2416
2417 static tree
2418 cp_parser_identifier (cp_parser* parser)
2419 {
2420   cp_token *token;
2421
2422   /* Look for the identifier.  */
2423   token = cp_parser_require (parser, CPP_NAME, "identifier");
2424   /* Return the value.  */
2425   return token ? token->value : error_mark_node;
2426 }
2427
2428 /* Parse a sequence of adjacent string constants.  Returns a
2429    TREE_STRING representing the combined, nul-terminated string
2430    constant.  If TRANSLATE is true, translate the string to the
2431    execution character set.  If WIDE_OK is true, a wide string is
2432    invalid here.
2433
2434    C++98 [lex.string] says that if a narrow string literal token is
2435    adjacent to a wide string literal token, the behavior is undefined.
2436    However, C99 6.4.5p4 says that this results in a wide string literal.
2437    We follow C99 here, for consistency with the C front end.
2438
2439    This code is largely lifted from lex_string() in c-lex.c.
2440
2441    FUTURE: ObjC++ will need to handle @-strings here.  */
2442 static tree
2443 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2444 {
2445   tree value;
2446   bool wide = false;
2447   size_t count;
2448   struct obstack str_ob;
2449   cpp_string str, istr, *strs;
2450   cp_token *tok;
2451
2452   tok = cp_lexer_peek_token (parser->lexer);
2453   if (!cp_parser_is_string_literal (tok))
2454     {
2455       cp_parser_error (parser, "expected string-literal");
2456       return error_mark_node;
2457     }
2458
2459   /* Try to avoid the overhead of creating and destroying an obstack
2460      for the common case of just one string.  */
2461   if (!cp_parser_is_string_literal
2462       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2463     {
2464       cp_lexer_consume_token (parser->lexer);
2465
2466       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2467       str.len = TREE_STRING_LENGTH (tok->value);
2468       count = 1;
2469       if (tok->type == CPP_WSTRING)
2470         wide = true;
2471
2472       strs = &str;
2473     }
2474   else
2475     {
2476       gcc_obstack_init (&str_ob);
2477       count = 0;
2478
2479       do
2480         {
2481           cp_lexer_consume_token (parser->lexer);
2482           count++;
2483           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2484           str.len = TREE_STRING_LENGTH (tok->value);
2485           if (tok->type == CPP_WSTRING)
2486             wide = true;
2487
2488           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2489
2490           tok = cp_lexer_peek_token (parser->lexer);
2491         }
2492       while (cp_parser_is_string_literal (tok));
2493
2494       strs = (cpp_string *) obstack_finish (&str_ob);
2495     }
2496
2497   if (wide && !wide_ok)
2498     {
2499       cp_parser_error (parser, "a wide string is invalid in this context");
2500       wide = false;
2501     }
2502
2503   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2504       (parse_in, strs, count, &istr, wide))
2505     {
2506       value = build_string (istr.len, (char *)istr.text);
2507       free ((void *)istr.text);
2508
2509       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2510       value = fix_string_type (value);
2511     }
2512   else
2513     /* cpp_interpret_string has issued an error.  */
2514     value = error_mark_node;
2515
2516   if (count > 1)
2517     obstack_free (&str_ob, 0);
2518
2519   return value;
2520 }
2521
2522
2523 /* Basic concepts [gram.basic]  */
2524
2525 /* Parse a translation-unit.
2526
2527    translation-unit:
2528      declaration-seq [opt]
2529
2530    Returns TRUE if all went well.  */
2531
2532 static bool
2533 cp_parser_translation_unit (cp_parser* parser)
2534 {
2535   /* The address of the first non-permanent object on the declarator
2536      obstack.  */
2537   static void *declarator_obstack_base;
2538
2539   bool success;
2540
2541   /* Create the declarator obstack, if necessary.  */
2542   if (!cp_error_declarator)
2543     {
2544       gcc_obstack_init (&declarator_obstack);
2545       /* Create the error declarator.  */
2546       cp_error_declarator = make_declarator (cdk_error);
2547       /* Create the empty parameter list.  */
2548       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2549       /* Remember where the base of the declarator obstack lies.  */
2550       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2551     }
2552
2553   while (true)
2554     {
2555       cp_parser_declaration_seq_opt (parser);
2556
2557       /* If there are no tokens left then all went well.  */
2558       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2559         {
2560           /* Get rid of the token array; we don't need it any more.  */
2561           cp_lexer_destroy (parser->lexer);
2562           parser->lexer = NULL;
2563
2564           /* This file might have been a context that's implicitly extern
2565              "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2566           if (parser->implicit_extern_c)
2567             {
2568               pop_lang_context ();
2569               parser->implicit_extern_c = false;
2570             }
2571
2572           /* Finish up.  */
2573           finish_translation_unit ();
2574
2575           success = true;
2576           break;
2577         }
2578       else
2579         {
2580           cp_parser_error (parser, "expected declaration");
2581           success = false;
2582           break;
2583         }
2584     }
2585
2586   /* Make sure the declarator obstack was fully cleaned up.  */
2587   gcc_assert (obstack_next_free (&declarator_obstack)
2588               == declarator_obstack_base);
2589
2590   /* All went well.  */
2591   return success;
2592 }
2593
2594 /* Expressions [gram.expr] */
2595
2596 /* Parse a primary-expression.
2597
2598    primary-expression:
2599      literal
2600      this
2601      ( expression )
2602      id-expression
2603
2604    GNU Extensions:
2605
2606    primary-expression:
2607      ( compound-statement )
2608      __builtin_va_arg ( assignment-expression , type-id )
2609
2610    literal:
2611      __null
2612
2613    Returns a representation of the expression.
2614
2615    *IDK indicates what kind of id-expression (if any) was present.
2616
2617    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2618    used as the operand of a pointer-to-member.  In that case,
2619    *QUALIFYING_CLASS gives the class that is used as the qualifying
2620    class in the pointer-to-member.  */
2621
2622 static tree
2623 cp_parser_primary_expression (cp_parser *parser,
2624                               cp_id_kind *idk,
2625                               tree *qualifying_class)
2626 {
2627   cp_token *token;
2628
2629   /* Assume the primary expression is not an id-expression.  */
2630   *idk = CP_ID_KIND_NONE;
2631   /* And that it cannot be used as pointer-to-member.  */
2632   *qualifying_class = NULL_TREE;
2633
2634   /* Peek at the next token.  */
2635   token = cp_lexer_peek_token (parser->lexer);
2636   switch (token->type)
2637     {
2638       /* literal:
2639            integer-literal
2640            character-literal
2641            floating-literal
2642            string-literal
2643            boolean-literal  */
2644     case CPP_CHAR:
2645     case CPP_WCHAR:
2646     case CPP_NUMBER:
2647       token = cp_lexer_consume_token (parser->lexer);
2648       return token->value;
2649
2650     case CPP_STRING:
2651     case CPP_WSTRING:
2652       /* ??? Should wide strings be allowed when parser->translate_strings_p
2653          is false (i.e. in attributes)?  If not, we can kill the third
2654          argument to cp_parser_string_literal.  */
2655       return cp_parser_string_literal (parser,
2656                                        parser->translate_strings_p,
2657                                        true);
2658
2659     case CPP_OPEN_PAREN:
2660       {
2661         tree expr;
2662         bool saved_greater_than_is_operator_p;
2663
2664         /* Consume the `('.  */
2665         cp_lexer_consume_token (parser->lexer);
2666         /* Within a parenthesized expression, a `>' token is always
2667            the greater-than operator.  */
2668         saved_greater_than_is_operator_p
2669           = parser->greater_than_is_operator_p;
2670         parser->greater_than_is_operator_p = true;
2671         /* If we see `( { ' then we are looking at the beginning of
2672            a GNU statement-expression.  */
2673         if (cp_parser_allow_gnu_extensions_p (parser)
2674             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2675           {
2676             /* Statement-expressions are not allowed by the standard.  */
2677             if (pedantic)
2678               pedwarn ("ISO C++ forbids braced-groups within expressions");
2679
2680             /* And they're not allowed outside of a function-body; you
2681                cannot, for example, write:
2682
2683                  int i = ({ int j = 3; j + 1; });
2684
2685                at class or namespace scope.  */
2686             if (!at_function_scope_p ())
2687               error ("statement-expressions are allowed only inside functions");
2688             /* Start the statement-expression.  */
2689             expr = begin_stmt_expr ();
2690             /* Parse the compound-statement.  */
2691             cp_parser_compound_statement (parser, expr, false);
2692             /* Finish up.  */
2693             expr = finish_stmt_expr (expr, false);
2694           }
2695         else
2696           {
2697             /* Parse the parenthesized expression.  */
2698             expr = cp_parser_expression (parser);
2699             /* Let the front end know that this expression was
2700                enclosed in parentheses. This matters in case, for
2701                example, the expression is of the form `A::B', since
2702                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2703                not.  */
2704             finish_parenthesized_expr (expr);
2705           }
2706         /* The `>' token might be the end of a template-id or
2707            template-parameter-list now.  */
2708         parser->greater_than_is_operator_p
2709           = saved_greater_than_is_operator_p;
2710         /* Consume the `)'.  */
2711         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2712           cp_parser_skip_to_end_of_statement (parser);
2713
2714         return expr;
2715       }
2716
2717     case CPP_KEYWORD:
2718       switch (token->keyword)
2719         {
2720           /* These two are the boolean literals.  */
2721         case RID_TRUE:
2722           cp_lexer_consume_token (parser->lexer);
2723           return boolean_true_node;
2724         case RID_FALSE:
2725           cp_lexer_consume_token (parser->lexer);
2726           return boolean_false_node;
2727
2728           /* The `__null' literal.  */
2729         case RID_NULL:
2730           cp_lexer_consume_token (parser->lexer);
2731           return null_node;
2732
2733           /* Recognize the `this' keyword.  */
2734         case RID_THIS:
2735           cp_lexer_consume_token (parser->lexer);
2736           if (parser->local_variables_forbidden_p)
2737             {
2738               error ("%<this%> may not be used in this context");
2739               return error_mark_node;
2740             }
2741           /* Pointers cannot appear in constant-expressions.  */
2742           if (cp_parser_non_integral_constant_expression (parser,
2743                                                           "`this'"))
2744             return error_mark_node;
2745           return finish_this_expr ();
2746
2747           /* The `operator' keyword can be the beginning of an
2748              id-expression.  */
2749         case RID_OPERATOR:
2750           goto id_expression;
2751
2752         case RID_FUNCTION_NAME:
2753         case RID_PRETTY_FUNCTION_NAME:
2754         case RID_C99_FUNCTION_NAME:
2755           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2756              __func__ are the names of variables -- but they are
2757              treated specially.  Therefore, they are handled here,
2758              rather than relying on the generic id-expression logic
2759              below.  Grammatically, these names are id-expressions.
2760
2761              Consume the token.  */
2762           token = cp_lexer_consume_token (parser->lexer);
2763           /* Look up the name.  */
2764           return finish_fname (token->value);
2765
2766         case RID_VA_ARG:
2767           {
2768             tree expression;
2769             tree type;
2770
2771             /* The `__builtin_va_arg' construct is used to handle
2772                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2773             cp_lexer_consume_token (parser->lexer);
2774             /* Look for the opening `('.  */
2775             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2776             /* Now, parse the assignment-expression.  */
2777             expression = cp_parser_assignment_expression (parser);
2778             /* Look for the `,'.  */
2779             cp_parser_require (parser, CPP_COMMA, "`,'");
2780             /* Parse the type-id.  */
2781             type = cp_parser_type_id (parser);
2782             /* Look for the closing `)'.  */
2783             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2784             /* Using `va_arg' in a constant-expression is not
2785                allowed.  */
2786             if (cp_parser_non_integral_constant_expression (parser,
2787                                                             "`va_arg'"))
2788               return error_mark_node;
2789             return build_x_va_arg (expression, type);
2790           }
2791
2792         case RID_OFFSETOF:
2793           return cp_parser_builtin_offsetof (parser);
2794
2795         default:
2796           cp_parser_error (parser, "expected primary-expression");
2797           return error_mark_node;
2798         }
2799
2800       /* An id-expression can start with either an identifier, a
2801          `::' as the beginning of a qualified-id, or the "operator"
2802          keyword.  */
2803     case CPP_NAME:
2804     case CPP_SCOPE:
2805     case CPP_TEMPLATE_ID:
2806     case CPP_NESTED_NAME_SPECIFIER:
2807       {
2808         tree id_expression;
2809         tree decl;
2810         const char *error_msg;
2811
2812       id_expression:
2813         /* Parse the id-expression.  */
2814         id_expression
2815           = cp_parser_id_expression (parser,
2816                                      /*template_keyword_p=*/false,
2817                                      /*check_dependency_p=*/true,
2818                                      /*template_p=*/NULL,
2819                                      /*declarator_p=*/false);
2820         if (id_expression == error_mark_node)
2821           return error_mark_node;
2822         /* If we have a template-id, then no further lookup is
2823            required.  If the template-id was for a template-class, we
2824            will sometimes have a TYPE_DECL at this point.  */
2825         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2826             || TREE_CODE (id_expression) == TYPE_DECL)
2827           decl = id_expression;
2828         /* Look up the name.  */
2829         else
2830           {
2831             bool ambiguous_p;
2832
2833             decl = cp_parser_lookup_name (parser, id_expression,
2834                                           /*is_type=*/false,
2835                                           /*is_template=*/false,
2836                                           /*is_namespace=*/false,
2837                                           /*check_dependency=*/true,
2838                                           &ambiguous_p);
2839             /* If the lookup was ambiguous, an error will already have
2840                been issued.  */
2841             if (ambiguous_p)
2842               return error_mark_node;
2843             /* If name lookup gives us a SCOPE_REF, then the
2844                qualifying scope was dependent.  Just propagate the
2845                name.  */
2846             if (TREE_CODE (decl) == SCOPE_REF)
2847               {
2848                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2849                   *qualifying_class = TREE_OPERAND (decl, 0);
2850                 return decl;
2851               }
2852             /* Check to see if DECL is a local variable in a context
2853                where that is forbidden.  */
2854             if (parser->local_variables_forbidden_p
2855                 && local_variable_p (decl))
2856               {
2857                 /* It might be that we only found DECL because we are
2858                    trying to be generous with pre-ISO scoping rules.
2859                    For example, consider:
2860
2861                      int i;
2862                      void g() {
2863                        for (int i = 0; i < 10; ++i) {}
2864                        extern void f(int j = i);
2865                      }
2866
2867                    Here, name look up will originally find the out
2868                    of scope `i'.  We need to issue a warning message,
2869                    but then use the global `i'.  */
2870                 decl = check_for_out_of_scope_variable (decl);
2871                 if (local_variable_p (decl))
2872                   {
2873                     error ("local variable %qD may not appear in this context",
2874                            decl);
2875                     return error_mark_node;
2876                   }
2877               }
2878           }
2879
2880         decl = finish_id_expression (id_expression, decl, parser->scope,
2881                                      idk, qualifying_class,
2882                                      parser->integral_constant_expression_p,
2883                                      parser->allow_non_integral_constant_expression_p,
2884                                      &parser->non_integral_constant_expression_p,
2885                                      &error_msg);
2886         if (error_msg)
2887           cp_parser_error (parser, error_msg);
2888         return decl;
2889       }
2890
2891       /* Anything else is an error.  */
2892     default:
2893       cp_parser_error (parser, "expected primary-expression");
2894       return error_mark_node;
2895     }
2896 }
2897
2898 /* Parse an id-expression.
2899
2900    id-expression:
2901      unqualified-id
2902      qualified-id
2903
2904    qualified-id:
2905      :: [opt] nested-name-specifier template [opt] unqualified-id
2906      :: identifier
2907      :: operator-function-id
2908      :: template-id
2909
2910    Return a representation of the unqualified portion of the
2911    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2912    a `::' or nested-name-specifier.
2913
2914    Often, if the id-expression was a qualified-id, the caller will
2915    want to make a SCOPE_REF to represent the qualified-id.  This
2916    function does not do this in order to avoid wastefully creating
2917    SCOPE_REFs when they are not required.
2918
2919    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2920    `template' keyword.
2921
2922    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2923    uninstantiated templates.
2924
2925    If *TEMPLATE_P is non-NULL, it is set to true iff the
2926    `template' keyword is used to explicitly indicate that the entity
2927    named is a template.
2928
2929    If DECLARATOR_P is true, the id-expression is appearing as part of
2930    a declarator, rather than as part of an expression.  */
2931
2932 static tree
2933 cp_parser_id_expression (cp_parser *parser,
2934                          bool template_keyword_p,
2935                          bool check_dependency_p,
2936                          bool *template_p,
2937                          bool declarator_p)
2938 {
2939   bool global_scope_p;
2940   bool nested_name_specifier_p;
2941
2942   /* Assume the `template' keyword was not used.  */
2943   if (template_p)
2944     *template_p = false;
2945
2946   /* Look for the optional `::' operator.  */
2947   global_scope_p
2948     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2949        != NULL_TREE);
2950   /* Look for the optional nested-name-specifier.  */
2951   nested_name_specifier_p
2952     = (cp_parser_nested_name_specifier_opt (parser,
2953                                             /*typename_keyword_p=*/false,
2954                                             check_dependency_p,
2955                                             /*type_p=*/false,
2956                                             declarator_p)
2957        != NULL_TREE);
2958   /* If there is a nested-name-specifier, then we are looking at
2959      the first qualified-id production.  */
2960   if (nested_name_specifier_p)
2961     {
2962       tree saved_scope;
2963       tree saved_object_scope;
2964       tree saved_qualifying_scope;
2965       tree unqualified_id;
2966       bool is_template;
2967
2968       /* See if the next token is the `template' keyword.  */
2969       if (!template_p)
2970         template_p = &is_template;
2971       *template_p = cp_parser_optional_template_keyword (parser);
2972       /* Name lookup we do during the processing of the
2973          unqualified-id might obliterate SCOPE.  */
2974       saved_scope = parser->scope;
2975       saved_object_scope = parser->object_scope;
2976       saved_qualifying_scope = parser->qualifying_scope;
2977       /* Process the final unqualified-id.  */
2978       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2979                                                  check_dependency_p,
2980                                                  declarator_p);
2981       /* Restore the SAVED_SCOPE for our caller.  */
2982       parser->scope = saved_scope;
2983       parser->object_scope = saved_object_scope;
2984       parser->qualifying_scope = saved_qualifying_scope;
2985
2986       return unqualified_id;
2987     }
2988   /* Otherwise, if we are in global scope, then we are looking at one
2989      of the other qualified-id productions.  */
2990   else if (global_scope_p)
2991     {
2992       cp_token *token;
2993       tree id;
2994
2995       /* Peek at the next token.  */
2996       token = cp_lexer_peek_token (parser->lexer);
2997
2998       /* If it's an identifier, and the next token is not a "<", then
2999          we can avoid the template-id case.  This is an optimization
3000          for this common case.  */
3001       if (token->type == CPP_NAME
3002           && !cp_parser_nth_token_starts_template_argument_list_p
3003                (parser, 2))
3004         return cp_parser_identifier (parser);
3005
3006       cp_parser_parse_tentatively (parser);
3007       /* Try a template-id.  */
3008       id = cp_parser_template_id (parser,
3009                                   /*template_keyword_p=*/false,
3010                                   /*check_dependency_p=*/true,
3011                                   declarator_p);
3012       /* If that worked, we're done.  */
3013       if (cp_parser_parse_definitely (parser))
3014         return id;
3015
3016       /* Peek at the next token.  (Changes in the token buffer may
3017          have invalidated the pointer obtained above.)  */
3018       token = cp_lexer_peek_token (parser->lexer);
3019
3020       switch (token->type)
3021         {
3022         case CPP_NAME:
3023           return cp_parser_identifier (parser);
3024
3025         case CPP_KEYWORD:
3026           if (token->keyword == RID_OPERATOR)
3027             return cp_parser_operator_function_id (parser);
3028           /* Fall through.  */
3029
3030         default:
3031           cp_parser_error (parser, "expected id-expression");
3032           return error_mark_node;
3033         }
3034     }
3035   else
3036     return cp_parser_unqualified_id (parser, template_keyword_p,
3037                                      /*check_dependency_p=*/true,
3038                                      declarator_p);
3039 }
3040
3041 /* Parse an unqualified-id.
3042
3043    unqualified-id:
3044      identifier
3045      operator-function-id
3046      conversion-function-id
3047      ~ class-name
3048      template-id
3049
3050    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3051    keyword, in a construct like `A::template ...'.
3052
3053    Returns a representation of unqualified-id.  For the `identifier'
3054    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3055    production a BIT_NOT_EXPR is returned; the operand of the
3056    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3057    other productions, see the documentation accompanying the
3058    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3059    names are looked up in uninstantiated templates.  If DECLARATOR_P
3060    is true, the unqualified-id is appearing as part of a declarator,
3061    rather than as part of an expression.  */
3062
3063 static tree
3064 cp_parser_unqualified_id (cp_parser* parser,
3065                           bool template_keyword_p,
3066                           bool check_dependency_p,
3067                           bool declarator_p)
3068 {
3069   cp_token *token;
3070
3071   /* Peek at the next token.  */
3072   token = cp_lexer_peek_token (parser->lexer);
3073
3074   switch (token->type)
3075     {
3076     case CPP_NAME:
3077       {
3078         tree id;
3079
3080         /* We don't know yet whether or not this will be a
3081            template-id.  */
3082         cp_parser_parse_tentatively (parser);
3083         /* Try a template-id.  */
3084         id = cp_parser_template_id (parser, template_keyword_p,
3085                                     check_dependency_p,
3086                                     declarator_p);
3087         /* If it worked, we're done.  */
3088         if (cp_parser_parse_definitely (parser))
3089           return id;
3090         /* Otherwise, it's an ordinary identifier.  */
3091         return cp_parser_identifier (parser);
3092       }
3093
3094     case CPP_TEMPLATE_ID:
3095       return cp_parser_template_id (parser, template_keyword_p,
3096                                     check_dependency_p,
3097                                     declarator_p);
3098
3099     case CPP_COMPL:
3100       {
3101         tree type_decl;
3102         tree qualifying_scope;
3103         tree object_scope;
3104         tree scope;
3105
3106         /* Consume the `~' token.  */
3107         cp_lexer_consume_token (parser->lexer);
3108         /* Parse the class-name.  The standard, as written, seems to
3109            say that:
3110
3111              template <typename T> struct S { ~S (); };
3112              template <typename T> S<T>::~S() {}
3113
3114            is invalid, since `~' must be followed by a class-name, but
3115            `S<T>' is dependent, and so not known to be a class.
3116            That's not right; we need to look in uninstantiated
3117            templates.  A further complication arises from:
3118
3119              template <typename T> void f(T t) {
3120                t.T::~T();
3121              }
3122
3123            Here, it is not possible to look up `T' in the scope of `T'
3124            itself.  We must look in both the current scope, and the
3125            scope of the containing complete expression.
3126
3127            Yet another issue is:
3128
3129              struct S {
3130                int S;
3131                ~S();
3132              };
3133
3134              S::~S() {}
3135
3136            The standard does not seem to say that the `S' in `~S'
3137            should refer to the type `S' and not the data member
3138            `S::S'.  */
3139
3140         /* DR 244 says that we look up the name after the "~" in the
3141            same scope as we looked up the qualifying name.  That idea
3142            isn't fully worked out; it's more complicated than that.  */
3143         scope = parser->scope;
3144         object_scope = parser->object_scope;
3145         qualifying_scope = parser->qualifying_scope;
3146
3147         /* If the name is of the form "X::~X" it's OK.  */
3148         if (scope && TYPE_P (scope)
3149             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3150             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3151                 == CPP_OPEN_PAREN)
3152             && (cp_lexer_peek_token (parser->lexer)->value
3153                 == TYPE_IDENTIFIER (scope)))
3154           {
3155             cp_lexer_consume_token (parser->lexer);
3156             return build_nt (BIT_NOT_EXPR, scope);
3157           }
3158
3159         /* If there was an explicit qualification (S::~T), first look
3160            in the scope given by the qualification (i.e., S).  */
3161         if (scope)
3162           {
3163             cp_parser_parse_tentatively (parser);
3164             type_decl = cp_parser_class_name (parser,
3165                                               /*typename_keyword_p=*/false,
3166                                               /*template_keyword_p=*/false,
3167                                               /*type_p=*/false,
3168                                               /*check_dependency=*/false,
3169                                               /*class_head_p=*/false,
3170                                               declarator_p);
3171             if (cp_parser_parse_definitely (parser))
3172               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3173           }
3174         /* In "N::S::~S", look in "N" as well.  */
3175         if (scope && qualifying_scope)
3176           {
3177             cp_parser_parse_tentatively (parser);
3178             parser->scope = qualifying_scope;
3179             parser->object_scope = NULL_TREE;
3180             parser->qualifying_scope = NULL_TREE;
3181             type_decl
3182               = cp_parser_class_name (parser,
3183                                       /*typename_keyword_p=*/false,
3184                                       /*template_keyword_p=*/false,
3185                                       /*type_p=*/false,
3186                                       /*check_dependency=*/false,
3187                                       /*class_head_p=*/false,
3188                                       declarator_p);
3189             if (cp_parser_parse_definitely (parser))
3190               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3191           }
3192         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3193         else if (object_scope)
3194           {
3195             cp_parser_parse_tentatively (parser);
3196             parser->scope = object_scope;
3197             parser->object_scope = NULL_TREE;
3198             parser->qualifying_scope = NULL_TREE;
3199             type_decl
3200               = cp_parser_class_name (parser,
3201                                       /*typename_keyword_p=*/false,
3202                                       /*template_keyword_p=*/false,
3203                                       /*type_p=*/false,
3204                                       /*check_dependency=*/false,
3205                                       /*class_head_p=*/false,
3206                                       declarator_p);
3207             if (cp_parser_parse_definitely (parser))
3208               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3209           }
3210         /* Look in the surrounding context.  */
3211         parser->scope = NULL_TREE;
3212         parser->object_scope = NULL_TREE;
3213         parser->qualifying_scope = NULL_TREE;
3214         type_decl
3215           = cp_parser_class_name (parser,
3216                                   /*typename_keyword_p=*/false,
3217                                   /*template_keyword_p=*/false,
3218                                   /*type_p=*/false,
3219                                   /*check_dependency=*/false,
3220                                   /*class_head_p=*/false,
3221                                   declarator_p);
3222         /* If an error occurred, assume that the name of the
3223            destructor is the same as the name of the qualifying
3224            class.  That allows us to keep parsing after running
3225            into ill-formed destructor names.  */
3226         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3227           return build_nt (BIT_NOT_EXPR, scope);
3228         else if (type_decl == error_mark_node)
3229           return error_mark_node;
3230
3231         /* [class.dtor]
3232
3233            A typedef-name that names a class shall not be used as the
3234            identifier in the declarator for a destructor declaration.  */
3235         if (declarator_p
3236             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3237             && !DECL_SELF_REFERENCE_P (type_decl))
3238           error ("typedef-name %qD used as destructor declarator",
3239                  type_decl);
3240
3241         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3242       }
3243
3244     case CPP_KEYWORD:
3245       if (token->keyword == RID_OPERATOR)
3246         {
3247           tree id;
3248
3249           /* This could be a template-id, so we try that first.  */
3250           cp_parser_parse_tentatively (parser);
3251           /* Try a template-id.  */
3252           id = cp_parser_template_id (parser, template_keyword_p,
3253                                       /*check_dependency_p=*/true,
3254                                       declarator_p);
3255           /* If that worked, we're done.  */
3256           if (cp_parser_parse_definitely (parser))
3257             return id;
3258           /* We still don't know whether we're looking at an
3259              operator-function-id or a conversion-function-id.  */
3260           cp_parser_parse_tentatively (parser);
3261           /* Try an operator-function-id.  */
3262           id = cp_parser_operator_function_id (parser);
3263           /* If that didn't work, try a conversion-function-id.  */
3264           if (!cp_parser_parse_definitely (parser))
3265             id = cp_parser_conversion_function_id (parser);
3266
3267           return id;
3268         }
3269       /* Fall through.  */
3270
3271     default:
3272       cp_parser_error (parser, "expected unqualified-id");
3273       return error_mark_node;
3274     }
3275 }
3276
3277 /* Parse an (optional) nested-name-specifier.
3278
3279    nested-name-specifier:
3280      class-or-namespace-name :: nested-name-specifier [opt]
3281      class-or-namespace-name :: template nested-name-specifier [opt]
3282
3283    PARSER->SCOPE should be set appropriately before this function is
3284    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3285    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3286    in name lookups.
3287
3288    Sets PARSER->SCOPE to the class (TYPE) or namespace
3289    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3290    it unchanged if there is no nested-name-specifier.  Returns the new
3291    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3292
3293    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3294    part of a declaration and/or decl-specifier.  */
3295
3296 static tree
3297 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3298                                      bool typename_keyword_p,
3299                                      bool check_dependency_p,
3300                                      bool type_p,
3301                                      bool is_declaration)
3302 {
3303   bool success = false;
3304   tree access_check = NULL_TREE;
3305   cp_token_position start = 0;
3306   cp_token *token;
3307
3308   /* If the next token corresponds to a nested name specifier, there
3309      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3310      false, it may have been true before, in which case something
3311      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3312      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3313      CHECK_DEPENDENCY_P is false, we have to fall through into the
3314      main loop.  */
3315   if (check_dependency_p
3316       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3317     {
3318       cp_parser_pre_parsed_nested_name_specifier (parser);
3319       return parser->scope;
3320     }
3321
3322   /* Remember where the nested-name-specifier starts.  */
3323   if (cp_parser_parsing_tentatively (parser)
3324       && !cp_parser_committed_to_tentative_parse (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,
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                                      /*complain=*/1);
3819
3820         postfix_expression = cp_parser_functional_cast (parser, type);
3821       }
3822       break;
3823
3824     default:
3825       {
3826         tree type;
3827
3828         /* If the next thing is a simple-type-specifier, we may be
3829            looking at a functional cast.  We could also be looking at
3830            an id-expression.  So, we try the functional cast, and if
3831            that doesn't work we fall back to the primary-expression.  */
3832         cp_parser_parse_tentatively (parser);
3833         /* Look for the simple-type-specifier.  */
3834         type = cp_parser_simple_type_specifier (parser,
3835                                                 /*decl_specs=*/NULL,
3836                                                 CP_PARSER_FLAGS_NONE);
3837         /* Parse the cast itself.  */
3838         if (!cp_parser_error_occurred (parser))
3839           postfix_expression
3840             = cp_parser_functional_cast (parser, type);
3841         /* If that worked, we're done.  */
3842         if (cp_parser_parse_definitely (parser))
3843           break;
3844
3845         /* If the functional-cast didn't work out, try a
3846            compound-literal.  */
3847         if (cp_parser_allow_gnu_extensions_p (parser)
3848             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3849           {
3850             tree initializer_list = NULL_TREE;
3851             bool saved_in_type_id_in_expr_p;
3852
3853             cp_parser_parse_tentatively (parser);
3854             /* Consume the `('.  */
3855             cp_lexer_consume_token (parser->lexer);
3856             /* Parse the type.  */
3857             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3858             parser->in_type_id_in_expr_p = true;
3859             type = cp_parser_type_id (parser);
3860             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3861             /* Look for the `)'.  */
3862             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3863             /* Look for the `{'.  */
3864             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3865             /* If things aren't going well, there's no need to
3866                keep going.  */
3867             if (!cp_parser_error_occurred (parser))
3868               {
3869                 bool non_constant_p;
3870                 /* Parse the initializer-list.  */
3871                 initializer_list
3872                   = cp_parser_initializer_list (parser, &non_constant_p);
3873                 /* Allow a trailing `,'.  */
3874                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3875                   cp_lexer_consume_token (parser->lexer);
3876                 /* Look for the final `}'.  */
3877                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3878               }
3879             /* If that worked, we're definitely looking at a
3880                compound-literal expression.  */
3881             if (cp_parser_parse_definitely (parser))
3882               {
3883                 /* Warn the user that a compound literal is not
3884                    allowed in standard C++.  */
3885                 if (pedantic)
3886                   pedwarn ("ISO C++ forbids compound-literals");
3887                 /* Form the representation of the compound-literal.  */
3888                 postfix_expression
3889                   = finish_compound_literal (type, initializer_list);
3890                 break;
3891               }
3892           }
3893
3894         /* It must be a primary-expression.  */
3895         postfix_expression = cp_parser_primary_expression (parser,
3896                                                            &idk,
3897                                                            &qualifying_class);
3898       }
3899       break;
3900     }
3901
3902   /* If we were avoiding committing to the processing of a
3903      qualified-id until we knew whether or not we had a
3904      pointer-to-member, we now know.  */
3905   if (qualifying_class)
3906     {
3907       bool done;
3908
3909       /* Peek at the next token.  */
3910       token = cp_lexer_peek_token (parser->lexer);
3911       done = (token->type != CPP_OPEN_SQUARE
3912               && token->type != CPP_OPEN_PAREN
3913               && token->type != CPP_DOT
3914               && token->type != CPP_DEREF
3915               && token->type != CPP_PLUS_PLUS
3916               && token->type != CPP_MINUS_MINUS);
3917
3918       postfix_expression = finish_qualified_id_expr (qualifying_class,
3919                                                      postfix_expression,
3920                                                      done,
3921                                                      address_p);
3922       if (done)
3923         return postfix_expression;
3924     }
3925
3926   /* Keep looping until the postfix-expression is complete.  */
3927   while (true)
3928     {
3929       if (idk == CP_ID_KIND_UNQUALIFIED
3930           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3931           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3932         /* It is not a Koenig lookup function call.  */
3933         postfix_expression
3934           = unqualified_name_lookup_error (postfix_expression);
3935
3936       /* Peek at the next token.  */
3937       token = cp_lexer_peek_token (parser->lexer);
3938
3939       switch (token->type)
3940         {
3941         case CPP_OPEN_SQUARE:
3942           postfix_expression
3943             = cp_parser_postfix_open_square_expression (parser,
3944                                                         postfix_expression,
3945                                                         false);
3946           idk = CP_ID_KIND_NONE;
3947           break;
3948
3949         case CPP_OPEN_PAREN:
3950           /* postfix-expression ( expression-list [opt] ) */
3951           {
3952             bool koenig_p;
3953             tree args = (cp_parser_parenthesized_expression_list
3954                          (parser, false, /*non_constant_p=*/NULL));
3955
3956             if (args == error_mark_node)
3957               {
3958                 postfix_expression = error_mark_node;
3959                 break;
3960               }
3961
3962             /* Function calls are not permitted in
3963                constant-expressions.  */
3964             if (cp_parser_non_integral_constant_expression (parser,
3965                                                             "a function call"))
3966               {
3967                 postfix_expression = error_mark_node;
3968                 break;
3969               }
3970
3971             koenig_p = false;
3972             if (idk == CP_ID_KIND_UNQUALIFIED)
3973               {
3974                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3975                   {
3976                     if (args)
3977                       {
3978                         koenig_p = true;
3979                         postfix_expression
3980                           = perform_koenig_lookup (postfix_expression, args);
3981                       }
3982                     else
3983                       postfix_expression
3984                         = unqualified_fn_lookup_error (postfix_expression);
3985                   }
3986                 /* We do not perform argument-dependent lookup if
3987                    normal lookup finds a non-function, in accordance
3988                    with the expected resolution of DR 218.  */
3989                 else if (args && is_overloaded_fn (postfix_expression))
3990                   {
3991                     tree fn = get_first_fn (postfix_expression);
3992
3993                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3994                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3995
3996                     /* Only do argument dependent lookup if regular
3997                        lookup does not find a set of member functions.
3998                        [basic.lookup.koenig]/2a  */
3999                     if (!DECL_FUNCTION_MEMBER_P (fn))
4000                       {
4001                         koenig_p = true;
4002                         postfix_expression
4003                           = perform_koenig_lookup (postfix_expression, args);
4004                       }
4005                   }
4006               }
4007
4008             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4009               {
4010                 tree instance = TREE_OPERAND (postfix_expression, 0);
4011                 tree fn = TREE_OPERAND (postfix_expression, 1);
4012
4013                 if (processing_template_decl
4014                     && (type_dependent_expression_p (instance)
4015                         || (!BASELINK_P (fn)
4016                             && TREE_CODE (fn) != FIELD_DECL)
4017                         || type_dependent_expression_p (fn)
4018                         || any_type_dependent_arguments_p (args)))
4019                   {
4020                     postfix_expression
4021                       = build_min_nt (CALL_EXPR, postfix_expression,
4022                                       args, NULL_TREE);
4023                     break;
4024                   }
4025
4026                 if (BASELINK_P (fn))
4027                   postfix_expression
4028                     = (build_new_method_call
4029                        (instance, fn, args, NULL_TREE,
4030                         (idk == CP_ID_KIND_QUALIFIED
4031                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4032                 else
4033                   postfix_expression
4034                     = finish_call_expr (postfix_expression, args,
4035                                         /*disallow_virtual=*/false,
4036                                         /*koenig_p=*/false);
4037               }
4038             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4039                      || TREE_CODE (postfix_expression) == MEMBER_REF
4040                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4041               postfix_expression = (build_offset_ref_call_from_tree
4042                                     (postfix_expression, args));
4043             else if (idk == CP_ID_KIND_QUALIFIED)
4044               /* A call to a static class member, or a namespace-scope
4045                  function.  */
4046               postfix_expression
4047                 = finish_call_expr (postfix_expression, args,
4048                                     /*disallow_virtual=*/true,
4049                                     koenig_p);
4050             else
4051               /* All other function calls.  */
4052               postfix_expression
4053                 = finish_call_expr (postfix_expression, args,
4054                                     /*disallow_virtual=*/false,
4055                                     koenig_p);
4056
4057             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4058             idk = CP_ID_KIND_NONE;
4059           }
4060           break;
4061
4062         case CPP_DOT:
4063         case CPP_DEREF:
4064           /* postfix-expression . template [opt] id-expression
4065              postfix-expression . pseudo-destructor-name
4066              postfix-expression -> template [opt] id-expression
4067              postfix-expression -> pseudo-destructor-name */
4068
4069           /* Consume the `.' or `->' operator.  */
4070           cp_lexer_consume_token (parser->lexer);
4071
4072           postfix_expression
4073             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4074                                                       postfix_expression,
4075                                                       false, &idk);
4076           break;
4077
4078         case CPP_PLUS_PLUS:
4079           /* postfix-expression ++  */
4080           /* Consume the `++' token.  */
4081           cp_lexer_consume_token (parser->lexer);
4082           /* Generate a representation for the complete expression.  */
4083           postfix_expression
4084             = finish_increment_expr (postfix_expression,
4085                                      POSTINCREMENT_EXPR);
4086           /* Increments may not appear in constant-expressions.  */
4087           if (cp_parser_non_integral_constant_expression (parser,
4088                                                           "an increment"))
4089             postfix_expression = error_mark_node;
4090           idk = CP_ID_KIND_NONE;
4091           break;
4092
4093         case CPP_MINUS_MINUS:
4094           /* postfix-expression -- */
4095           /* Consume the `--' token.  */
4096           cp_lexer_consume_token (parser->lexer);
4097           /* Generate a representation for the complete expression.  */
4098           postfix_expression
4099             = finish_increment_expr (postfix_expression,
4100                                      POSTDECREMENT_EXPR);
4101           /* Decrements may not appear in constant-expressions.  */
4102           if (cp_parser_non_integral_constant_expression (parser,
4103                                                           "a decrement"))
4104             postfix_expression = error_mark_node;
4105           idk = CP_ID_KIND_NONE;
4106           break;
4107
4108         default:
4109           return postfix_expression;
4110         }
4111     }
4112
4113   /* We should never get here.  */
4114   gcc_unreachable ();
4115   return error_mark_node;
4116 }
4117
4118 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4119    by cp_parser_builtin_offsetof.  We're looking for
4120
4121      postfix-expression [ expression ]
4122
4123    FOR_OFFSETOF is set if we're being called in that context, which
4124    changes how we deal with integer constant expressions.  */
4125
4126 static tree
4127 cp_parser_postfix_open_square_expression (cp_parser *parser,
4128                                           tree postfix_expression,
4129                                           bool for_offsetof)
4130 {
4131   tree index;
4132
4133   /* Consume the `[' token.  */
4134   cp_lexer_consume_token (parser->lexer);
4135
4136   /* Parse the index expression.  */
4137   /* ??? For offsetof, there is a question of what to allow here.  If
4138      offsetof is not being used in an integral constant expression context,
4139      then we *could* get the right answer by computing the value at runtime.
4140      If we are in an integral constant expression context, then we might
4141      could accept any constant expression; hard to say without analysis.
4142      Rather than open the barn door too wide right away, allow only integer
4143      constant expressions here.  */
4144   if (for_offsetof)
4145     index = cp_parser_constant_expression (parser, false, NULL);
4146   else
4147     index = cp_parser_expression (parser);
4148
4149   /* Look for the closing `]'.  */
4150   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4151
4152   /* Build the ARRAY_REF.  */
4153   postfix_expression = grok_array_decl (postfix_expression, index);
4154
4155   /* When not doing offsetof, array references are not permitted in
4156      constant-expressions.  */
4157   if (!for_offsetof
4158       && (cp_parser_non_integral_constant_expression
4159           (parser, "an array reference")))
4160     postfix_expression = error_mark_node;
4161
4162   return postfix_expression;
4163 }
4164
4165 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4166    by cp_parser_builtin_offsetof.  We're looking for
4167
4168      postfix-expression . template [opt] id-expression
4169      postfix-expression . pseudo-destructor-name
4170      postfix-expression -> template [opt] id-expression
4171      postfix-expression -> pseudo-destructor-name
4172
4173    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4174    limits what of the above we'll actually accept, but nevermind.
4175    TOKEN_TYPE is the "." or "->" token, which will already have been
4176    removed from the stream.  */
4177
4178 static tree
4179 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4180                                         enum cpp_ttype token_type,
4181                                         tree postfix_expression,
4182                                         bool for_offsetof, cp_id_kind *idk)
4183 {
4184   tree name;
4185   bool dependent_p;
4186   bool template_p;
4187   bool pseudo_destructor_p;
4188   tree scope = NULL_TREE;
4189
4190   /* If this is a `->' operator, dereference the pointer.  */
4191   if (token_type == CPP_DEREF)
4192     postfix_expression = build_x_arrow (postfix_expression);
4193   /* Check to see whether or not the expression is type-dependent.  */
4194   dependent_p = type_dependent_expression_p (postfix_expression);
4195   /* The identifier following the `->' or `.' is not qualified.  */
4196   parser->scope = NULL_TREE;
4197   parser->qualifying_scope = NULL_TREE;
4198   parser->object_scope = NULL_TREE;
4199   *idk = CP_ID_KIND_NONE;
4200   /* Enter the scope corresponding to the type of the object
4201      given by the POSTFIX_EXPRESSION.  */
4202   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4203     {
4204       scope = TREE_TYPE (postfix_expression);
4205       /* According to the standard, no expression should ever have
4206          reference type.  Unfortunately, we do not currently match
4207          the standard in this respect in that our internal representation
4208          of an expression may have reference type even when the standard
4209          says it does not.  Therefore, we have to manually obtain the
4210          underlying type here.  */
4211       scope = non_reference (scope);
4212       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4213       scope = complete_type_or_else (scope, NULL_TREE);
4214       /* Let the name lookup machinery know that we are processing a
4215          class member access expression.  */
4216       parser->context->object_type = scope;
4217       /* If something went wrong, we want to be able to discern that case,
4218          as opposed to the case where there was no SCOPE due to the type
4219          of expression being dependent.  */
4220       if (!scope)
4221         scope = error_mark_node;
4222       /* If the SCOPE was erroneous, make the various semantic analysis
4223          functions exit quickly -- and without issuing additional error
4224          messages.  */
4225       if (scope == error_mark_node)
4226         postfix_expression = error_mark_node;
4227     }
4228
4229   /* Assume this expression is not a pseudo-destructor access.  */
4230   pseudo_destructor_p = false;
4231
4232   /* If the SCOPE is a scalar type, then, if this is a valid program,
4233      we must be looking at a pseudo-destructor-name.  */
4234   if (scope && SCALAR_TYPE_P (scope))
4235     {
4236       tree s;
4237       tree type;
4238
4239       cp_parser_parse_tentatively (parser);
4240       /* Parse the pseudo-destructor-name.  */
4241       s = NULL_TREE;
4242       cp_parser_pseudo_destructor_name (parser, &s, &type);
4243       if (cp_parser_parse_definitely (parser))
4244         {
4245           pseudo_destructor_p = true;
4246           postfix_expression
4247             = finish_pseudo_destructor_expr (postfix_expression,
4248                                              s, TREE_TYPE (type));
4249         }
4250     }
4251
4252   if (!pseudo_destructor_p)
4253     {
4254       /* If the SCOPE is not a scalar type, we are looking at an
4255          ordinary class member access expression, rather than a
4256          pseudo-destructor-name.  */
4257       template_p = cp_parser_optional_template_keyword (parser);
4258       /* Parse the id-expression.  */
4259       name = cp_parser_id_expression (parser, template_p,
4260                                       /*check_dependency_p=*/true,
4261                                       /*template_p=*/NULL,
4262                                       /*declarator_p=*/false);
4263       /* In general, build a SCOPE_REF if the member name is qualified.
4264          However, if the name was not dependent and has already been
4265          resolved; there is no need to build the SCOPE_REF.  For example;
4266
4267              struct X { void f(); };
4268              template <typename T> void f(T* t) { t->X::f(); }
4269
4270          Even though "t" is dependent, "X::f" is not and has been resolved
4271          to a BASELINK; there is no need to include scope information.  */
4272
4273       /* But we do need to remember that there was an explicit scope for
4274          virtual function calls.  */
4275       if (parser->scope)
4276         *idk = CP_ID_KIND_QUALIFIED;
4277
4278       if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4279         {
4280           name = build_nt (SCOPE_REF, parser->scope, name);
4281           parser->scope = NULL_TREE;
4282           parser->qualifying_scope = NULL_TREE;
4283           parser->object_scope = NULL_TREE;
4284         }
4285       if (scope && name && BASELINK_P (name))
4286         adjust_result_of_qualified_name_lookup
4287           (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4288       postfix_expression
4289         = finish_class_member_access_expr (postfix_expression, name);
4290     }
4291
4292   /* We no longer need to look up names in the scope of the object on
4293      the left-hand side of the `.' or `->' operator.  */
4294   parser->context->object_type = NULL_TREE;
4295
4296   /* Outside of offsetof, these operators may not appear in
4297      constant-expressions.  */
4298   if (!for_offsetof
4299       && (cp_parser_non_integral_constant_expression
4300           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4301     postfix_expression = error_mark_node;
4302
4303   return postfix_expression;
4304 }
4305
4306 /* Parse a parenthesized expression-list.
4307
4308    expression-list:
4309      assignment-expression
4310      expression-list, assignment-expression
4311
4312    attribute-list:
4313      expression-list
4314      identifier
4315      identifier, expression-list
4316
4317    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4318    representation of an assignment-expression.  Note that a TREE_LIST
4319    is returned even if there is only a single expression in the list.
4320    error_mark_node is returned if the ( and or ) are
4321    missing. NULL_TREE is returned on no expressions. The parentheses
4322    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4323    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4324    indicates whether or not all of the expressions in the list were
4325    constant.  */
4326
4327 static tree
4328 cp_parser_parenthesized_expression_list (cp_parser* parser,
4329                                          bool is_attribute_list,
4330                                          bool *non_constant_p)
4331 {
4332   tree expression_list = NULL_TREE;
4333   bool fold_expr_p = is_attribute_list;
4334   tree identifier = NULL_TREE;
4335
4336   /* Assume all the expressions will be constant.  */
4337   if (non_constant_p)
4338     *non_constant_p = false;
4339
4340   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4341     return error_mark_node;
4342
4343   /* Consume expressions until there are no more.  */
4344   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4345     while (true)
4346       {
4347         tree expr;
4348
4349         /* At the beginning of attribute lists, check to see if the
4350            next token is an identifier.  */
4351         if (is_attribute_list
4352             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4353           {
4354             cp_token *token;
4355
4356             /* Consume the identifier.  */
4357             token = cp_lexer_consume_token (parser->lexer);
4358             /* Save the identifier.  */
4359             identifier = token->value;
4360           }
4361         else
4362           {
4363             /* Parse the next assignment-expression.  */
4364             if (non_constant_p)
4365               {
4366                 bool expr_non_constant_p;
4367                 expr = (cp_parser_constant_expression
4368                         (parser, /*allow_non_constant_p=*/true,
4369                          &expr_non_constant_p));
4370                 if (expr_non_constant_p)
4371                   *non_constant_p = true;
4372               }
4373             else
4374               expr = cp_parser_assignment_expression (parser);
4375
4376             if (fold_expr_p)
4377               expr = fold_non_dependent_expr (expr);
4378
4379              /* Add it to the list.  We add error_mark_node
4380                 expressions to the list, so that we can still tell if
4381                 the correct form for a parenthesized expression-list
4382                 is found. That gives better errors.  */
4383             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4384
4385             if (expr == error_mark_node)
4386               goto skip_comma;
4387           }
4388
4389         /* After the first item, attribute lists look the same as
4390            expression lists.  */
4391         is_attribute_list = false;
4392
4393       get_comma:;
4394         /* If the next token isn't a `,', then we are done.  */
4395         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4396           break;
4397
4398         /* Otherwise, consume the `,' and keep going.  */
4399         cp_lexer_consume_token (parser->lexer);
4400       }
4401
4402   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4403     {
4404       int ending;
4405
4406     skip_comma:;
4407       /* We try and resync to an unnested comma, as that will give the
4408          user better diagnostics.  */
4409       ending = cp_parser_skip_to_closing_parenthesis (parser,
4410                                                       /*recovering=*/true,
4411                                                       /*or_comma=*/true,
4412                                                       /*consume_paren=*/true);
4413       if (ending < 0)
4414         goto get_comma;
4415       if (!ending)
4416         return error_mark_node;
4417     }
4418
4419   /* We built up the list in reverse order so we must reverse it now.  */
4420   expression_list = nreverse (expression_list);
4421   if (identifier)
4422     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4423
4424   return expression_list;
4425 }
4426
4427 /* Parse a pseudo-destructor-name.
4428
4429    pseudo-destructor-name:
4430      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4431      :: [opt] nested-name-specifier template template-id :: ~ type-name
4432      :: [opt] nested-name-specifier [opt] ~ type-name
4433
4434    If either of the first two productions is used, sets *SCOPE to the
4435    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4436    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4437    or ERROR_MARK_NODE if the parse fails.  */
4438
4439 static void
4440 cp_parser_pseudo_destructor_name (cp_parser* parser,
4441                                   tree* scope,
4442                                   tree* type)
4443 {
4444   bool nested_name_specifier_p;
4445
4446   /* Assume that things will not work out.  */
4447   *type = error_mark_node;
4448
4449   /* Look for the optional `::' operator.  */
4450   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4451   /* Look for the optional nested-name-specifier.  */
4452   nested_name_specifier_p
4453     = (cp_parser_nested_name_specifier_opt (parser,
4454                                             /*typename_keyword_p=*/false,
4455                                             /*check_dependency_p=*/true,
4456                                             /*type_p=*/false,
4457                                             /*is_declaration=*/true)
4458        != NULL_TREE);
4459   /* Now, if we saw a nested-name-specifier, we might be doing the
4460      second production.  */
4461   if (nested_name_specifier_p
4462       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4463     {
4464       /* Consume the `template' keyword.  */
4465       cp_lexer_consume_token (parser->lexer);
4466       /* Parse the template-id.  */
4467       cp_parser_template_id (parser,
4468                              /*template_keyword_p=*/true,
4469                              /*check_dependency_p=*/false,
4470                              /*is_declaration=*/true);
4471       /* Look for the `::' token.  */
4472       cp_parser_require (parser, CPP_SCOPE, "`::'");
4473     }
4474   /* If the next token is not a `~', then there might be some
4475      additional qualification.  */
4476   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4477     {
4478       /* Look for the type-name.  */
4479       *scope = TREE_TYPE (cp_parser_type_name (parser));
4480
4481       if (*scope == error_mark_node)
4482         return;
4483
4484       /* If we don't have ::~, then something has gone wrong.  Since
4485          the only caller of this function is looking for something
4486          after `.' or `->' after a scalar type, most likely the
4487          program is trying to get a member of a non-aggregate
4488          type.  */
4489       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4490           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4491         {
4492           cp_parser_error (parser, "request for member of non-aggregate type");
4493           return;
4494         }
4495
4496       /* Look for the `::' token.  */
4497       cp_parser_require (parser, CPP_SCOPE, "`::'");
4498     }
4499   else
4500     *scope = NULL_TREE;
4501
4502   /* Look for the `~'.  */
4503   cp_parser_require (parser, CPP_COMPL, "`~'");
4504   /* Look for the type-name again.  We are not responsible for
4505      checking that it matches the first type-name.  */
4506   *type = cp_parser_type_name (parser);
4507 }
4508
4509 /* Parse a unary-expression.
4510
4511    unary-expression:
4512      postfix-expression
4513      ++ cast-expression
4514      -- cast-expression
4515      unary-operator cast-expression
4516      sizeof unary-expression
4517      sizeof ( type-id )
4518      new-expression
4519      delete-expression
4520
4521    GNU Extensions:
4522
4523    unary-expression:
4524      __extension__ cast-expression
4525      __alignof__ unary-expression
4526      __alignof__ ( type-id )
4527      __real__ cast-expression
4528      __imag__ cast-expression
4529      && identifier
4530
4531    ADDRESS_P is true iff the unary-expression is appearing as the
4532    operand of the `&' operator.
4533
4534    Returns a representation of the expression.  */
4535
4536 static tree
4537 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4538 {
4539   cp_token *token;
4540   enum tree_code unary_operator;
4541
4542   /* Peek at the next token.  */
4543   token = cp_lexer_peek_token (parser->lexer);
4544   /* Some keywords give away the kind of expression.  */
4545   if (token->type == CPP_KEYWORD)
4546     {
4547       enum rid keyword = token->keyword;
4548
4549       switch (keyword)
4550         {
4551         case RID_ALIGNOF:
4552         case RID_SIZEOF:
4553           {
4554             tree operand;
4555             enum tree_code op;
4556
4557             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4558             /* Consume the token.  */
4559             cp_lexer_consume_token (parser->lexer);
4560             /* Parse the operand.  */
4561             operand = cp_parser_sizeof_operand (parser, keyword);
4562
4563             if (TYPE_P (operand))
4564               return cxx_sizeof_or_alignof_type (operand, op, true);
4565             else
4566               return cxx_sizeof_or_alignof_expr (operand, op);
4567           }
4568
4569         case RID_NEW:
4570           return cp_parser_new_expression (parser);
4571
4572         case RID_DELETE:
4573           return cp_parser_delete_expression (parser);
4574
4575         case RID_EXTENSION:
4576           {
4577             /* The saved value of the PEDANTIC flag.  */
4578             int saved_pedantic;
4579             tree expr;
4580
4581             /* Save away the PEDANTIC flag.  */
4582             cp_parser_extension_opt (parser, &saved_pedantic);
4583             /* Parse the cast-expression.  */
4584             expr = cp_parser_simple_cast_expression (parser);
4585             /* Restore the PEDANTIC flag.  */
4586             pedantic = saved_pedantic;
4587
4588             return expr;
4589           }
4590
4591         case RID_REALPART:
4592         case RID_IMAGPART:
4593           {
4594             tree expression;
4595
4596             /* Consume the `__real__' or `__imag__' token.  */
4597             cp_lexer_consume_token (parser->lexer);
4598             /* Parse the cast-expression.  */
4599             expression = cp_parser_simple_cast_expression (parser);
4600             /* Create the complete representation.  */
4601             return build_x_unary_op ((keyword == RID_REALPART
4602                                       ? REALPART_EXPR : IMAGPART_EXPR),
4603                                      expression);
4604           }
4605           break;
4606
4607         default:
4608           break;
4609         }
4610     }
4611
4612   /* Look for the `:: new' and `:: delete', which also signal the
4613      beginning of a new-expression, or delete-expression,
4614      respectively.  If the next token is `::', then it might be one of
4615      these.  */
4616   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4617     {
4618       enum rid keyword;
4619
4620       /* See if the token after the `::' is one of the keywords in
4621          which we're interested.  */
4622       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4623       /* If it's `new', we have a new-expression.  */
4624       if (keyword == RID_NEW)
4625         return cp_parser_new_expression (parser);
4626       /* Similarly, for `delete'.  */
4627       else if (keyword == RID_DELETE)
4628         return cp_parser_delete_expression (parser);
4629     }
4630
4631   /* Look for a unary operator.  */
4632   unary_operator = cp_parser_unary_operator (token);
4633   /* The `++' and `--' operators can be handled similarly, even though
4634      they are not technically unary-operators in the grammar.  */
4635   if (unary_operator == ERROR_MARK)
4636     {
4637       if (token->type == CPP_PLUS_PLUS)
4638         unary_operator = PREINCREMENT_EXPR;
4639       else if (token->type == CPP_MINUS_MINUS)
4640         unary_operator = PREDECREMENT_EXPR;
4641       /* Handle the GNU address-of-label extension.  */
4642       else if (cp_parser_allow_gnu_extensions_p (parser)
4643                && token->type == CPP_AND_AND)
4644         {
4645           tree identifier;
4646
4647           /* Consume the '&&' token.  */
4648           cp_lexer_consume_token (parser->lexer);
4649           /* Look for the identifier.  */
4650           identifier = cp_parser_identifier (parser);
4651           /* Create an expression representing the address.  */
4652           return finish_label_address_expr (identifier);
4653         }
4654     }
4655   if (unary_operator != ERROR_MARK)
4656     {
4657       tree cast_expression;
4658       tree expression = error_mark_node;
4659       const char *non_constant_p = NULL;
4660
4661       /* Consume the operator token.  */
4662       token = cp_lexer_consume_token (parser->lexer);
4663       /* Parse the cast-expression.  */
4664       cast_expression
4665         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4666       /* Now, build an appropriate representation.  */
4667       switch (unary_operator)
4668         {
4669         case INDIRECT_REF:
4670           non_constant_p = "`*'";
4671           expression = build_x_indirect_ref (cast_expression, "unary *");
4672           break;
4673
4674         case ADDR_EXPR:
4675           non_constant_p = "`&'";
4676           /* Fall through.  */
4677         case BIT_NOT_EXPR:
4678           expression = build_x_unary_op (unary_operator, cast_expression);
4679           break;
4680
4681         case PREINCREMENT_EXPR:
4682         case PREDECREMENT_EXPR:
4683           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4684                             ? "`++'" : "`--'");
4685           /* Fall through.  */
4686         case CONVERT_EXPR:
4687         case NEGATE_EXPR:
4688         case TRUTH_NOT_EXPR:
4689           expression = finish_unary_op_expr (unary_operator, cast_expression);
4690           break;
4691
4692         default:
4693           gcc_unreachable ();
4694         }
4695
4696       if (non_constant_p
4697           && cp_parser_non_integral_constant_expression (parser,
4698                                                          non_constant_p))
4699         expression = error_mark_node;
4700
4701       return expression;
4702     }
4703
4704   return cp_parser_postfix_expression (parser, address_p);
4705 }
4706
4707 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4708    unary-operator, the corresponding tree code is returned.  */
4709
4710 static enum tree_code
4711 cp_parser_unary_operator (cp_token* token)
4712 {
4713   switch (token->type)
4714     {
4715     case CPP_MULT:
4716       return INDIRECT_REF;
4717
4718     case CPP_AND:
4719       return ADDR_EXPR;
4720
4721     case CPP_PLUS:
4722       return CONVERT_EXPR;
4723
4724     case CPP_MINUS:
4725       return NEGATE_EXPR;
4726
4727     case CPP_NOT:
4728       return TRUTH_NOT_EXPR;
4729
4730     case CPP_COMPL:
4731       return BIT_NOT_EXPR;
4732
4733     default:
4734       return ERROR_MARK;
4735     }
4736 }
4737
4738 /* Parse a new-expression.
4739
4740    new-expression:
4741      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4742      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4743
4744    Returns a representation of the expression.  */
4745
4746 static tree
4747 cp_parser_new_expression (cp_parser* parser)
4748 {
4749   bool global_scope_p;
4750   tree placement;
4751   tree type;
4752   tree initializer;
4753   tree nelts;
4754
4755   /* Look for the optional `::' operator.  */
4756   global_scope_p
4757     = (cp_parser_global_scope_opt (parser,
4758                                    /*current_scope_valid_p=*/false)
4759        != NULL_TREE);
4760   /* Look for the `new' operator.  */
4761   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4762   /* There's no easy way to tell a new-placement from the
4763      `( type-id )' construct.  */
4764   cp_parser_parse_tentatively (parser);
4765   /* Look for a new-placement.  */
4766   placement = cp_parser_new_placement (parser);
4767   /* If that didn't work out, there's no new-placement.  */
4768   if (!cp_parser_parse_definitely (parser))
4769     placement = NULL_TREE;
4770
4771   /* If the next token is a `(', then we have a parenthesized
4772      type-id.  */
4773   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4774     {
4775       /* Consume the `('.  */
4776       cp_lexer_consume_token (parser->lexer);
4777       /* Parse the type-id.  */
4778       type = cp_parser_type_id (parser);
4779       /* Look for the closing `)'.  */
4780       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4781       /* There should not be a direct-new-declarator in this production,
4782          but GCC used to allowed this, so we check and emit a sensible error
4783          message for this case.  */
4784       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4785         {
4786           error ("array bound forbidden after parenthesized type-id");
4787           inform ("try removing the parentheses around the type-id");
4788           cp_parser_direct_new_declarator (parser);
4789         }
4790       nelts = NULL_TREE;
4791     }
4792   /* Otherwise, there must be a new-type-id.  */
4793   else
4794     type = cp_parser_new_type_id (parser, &nelts);
4795
4796   /* If the next token is a `(', then we have a new-initializer.  */
4797   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4798     initializer = cp_parser_new_initializer (parser);
4799   else
4800     initializer = NULL_TREE;
4801
4802   /* A new-expression may not appear in an integral constant
4803      expression.  */
4804   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4805     return error_mark_node;
4806
4807   /* Create a representation of the new-expression.  */
4808   return build_new (placement, type, nelts, initializer, global_scope_p);
4809 }
4810
4811 /* Parse a new-placement.
4812
4813    new-placement:
4814      ( expression-list )
4815
4816    Returns the same representation as for an expression-list.  */
4817
4818 static tree
4819 cp_parser_new_placement (cp_parser* parser)
4820 {
4821   tree expression_list;
4822
4823   /* Parse the expression-list.  */
4824   expression_list = (cp_parser_parenthesized_expression_list
4825                      (parser, false, /*non_constant_p=*/NULL));
4826
4827   return expression_list;
4828 }
4829
4830 /* Parse a new-type-id.
4831
4832    new-type-id:
4833      type-specifier-seq new-declarator [opt]
4834
4835    Returns the TYPE allocated.  If the new-type-id indicates an array
4836    type, *NELTS is set to the number of elements in the last array
4837    bound; the TYPE will not include the last array bound.  */
4838
4839 static tree
4840 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4841 {
4842   cp_decl_specifier_seq type_specifier_seq;
4843   cp_declarator *new_declarator;
4844   cp_declarator *declarator;
4845   cp_declarator *outer_declarator;
4846   const char *saved_message;
4847   tree type;
4848
4849   /* The type-specifier sequence must not contain type definitions.
4850      (It cannot contain declarations of new types either, but if they
4851      are not definitions we will catch that because they are not
4852      complete.)  */
4853   saved_message = parser->type_definition_forbidden_message;
4854   parser->type_definition_forbidden_message
4855     = "types may not be defined in a new-type-id";
4856   /* Parse the type-specifier-seq.  */
4857   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4858   /* Restore the old message.  */
4859   parser->type_definition_forbidden_message = saved_message;
4860   /* Parse the new-declarator.  */
4861   new_declarator = cp_parser_new_declarator_opt (parser);
4862
4863   /* Determine the number of elements in the last array dimension, if
4864      any.  */
4865   *nelts = NULL_TREE;
4866   /* Skip down to the last array dimension.  */
4867   declarator = new_declarator;
4868   outer_declarator = NULL;
4869   while (declarator && (declarator->kind == cdk_pointer
4870                         || declarator->kind == cdk_ptrmem))
4871     {
4872       outer_declarator = declarator;
4873       declarator = declarator->declarator;
4874     }
4875   while (declarator
4876          && declarator->kind == cdk_array
4877          && declarator->declarator
4878          && declarator->declarator->kind == cdk_array)
4879     {
4880       outer_declarator = declarator;
4881       declarator = declarator->declarator;
4882     }
4883
4884   if (declarator && declarator->kind == cdk_array)
4885     {
4886       *nelts = declarator->u.array.bounds;
4887       if (*nelts == error_mark_node)
4888         *nelts = integer_one_node;
4889       else if (!processing_template_decl)
4890         {
4891           if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4892                                            false))
4893             pedwarn ("size in array new must have integral type");
4894           *nelts = save_expr (cp_convert (sizetype, *nelts));
4895           if (*nelts == integer_zero_node)
4896             warning ("zero size array reserves no space");
4897         }
4898       if (outer_declarator)
4899         outer_declarator->declarator = declarator->declarator;
4900       else
4901         new_declarator = NULL;
4902     }
4903
4904   type = groktypename (&type_specifier_seq, new_declarator);
4905   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4906     {
4907       *nelts = array_type_nelts_top (type);
4908       type = TREE_TYPE (type);
4909     }
4910   return type;
4911 }
4912
4913 /* Parse an (optional) new-declarator.
4914
4915    new-declarator:
4916      ptr-operator new-declarator [opt]
4917      direct-new-declarator
4918
4919    Returns the declarator.  */
4920
4921 static cp_declarator *
4922 cp_parser_new_declarator_opt (cp_parser* parser)
4923 {
4924   enum tree_code code;
4925   tree type;
4926   cp_cv_quals cv_quals;
4927
4928   /* We don't know if there's a ptr-operator next, or not.  */
4929   cp_parser_parse_tentatively (parser);
4930   /* Look for a ptr-operator.  */
4931   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4932   /* If that worked, look for more new-declarators.  */
4933   if (cp_parser_parse_definitely (parser))
4934     {
4935       cp_declarator *declarator;
4936
4937       /* Parse another optional declarator.  */
4938       declarator = cp_parser_new_declarator_opt (parser);
4939
4940       /* Create the representation of the declarator.  */
4941       if (type)
4942         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4943       else if (code == INDIRECT_REF)
4944         declarator = make_pointer_declarator (cv_quals, declarator);
4945       else
4946         declarator = make_reference_declarator (cv_quals, declarator);
4947
4948       return declarator;
4949     }
4950
4951   /* If the next token is a `[', there is a direct-new-declarator.  */
4952   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4953     return cp_parser_direct_new_declarator (parser);
4954
4955   return NULL;
4956 }
4957
4958 /* Parse a direct-new-declarator.
4959
4960    direct-new-declarator:
4961      [ expression ]
4962      direct-new-declarator [constant-expression]
4963
4964    */
4965
4966 static cp_declarator *
4967 cp_parser_direct_new_declarator (cp_parser* parser)
4968 {
4969   cp_declarator *declarator = NULL;
4970
4971   while (true)
4972     {
4973       tree expression;
4974
4975       /* Look for the opening `['.  */
4976       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4977       /* The first expression is not required to be constant.  */
4978       if (!declarator)
4979         {
4980           expression = cp_parser_expression (parser);
4981           /* The standard requires that the expression have integral
4982              type.  DR 74 adds enumeration types.  We believe that the
4983              real intent is that these expressions be handled like the
4984              expression in a `switch' condition, which also allows
4985              classes with a single conversion to integral or
4986              enumeration type.  */
4987           if (!processing_template_decl)
4988             {
4989               expression
4990                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4991                                               expression,
4992                                               /*complain=*/true);
4993               if (!expression)
4994                 {
4995                   error ("expression in new-declarator must have integral "
4996                          "or enumeration type");
4997                   expression = error_mark_node;
4998                 }
4999             }
5000         }
5001       /* But all the other expressions must be.  */
5002       else
5003         expression
5004           = cp_parser_constant_expression (parser,
5005                                            /*allow_non_constant=*/false,
5006                                            NULL);
5007       /* Look for the closing `]'.  */
5008       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5009
5010       /* Add this bound to the declarator.  */
5011       declarator = make_array_declarator (declarator, expression);
5012
5013       /* If the next token is not a `[', then there are no more
5014          bounds.  */
5015       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5016         break;
5017     }
5018
5019   return declarator;
5020 }
5021
5022 /* Parse a new-initializer.
5023
5024    new-initializer:
5025      ( expression-list [opt] )
5026
5027    Returns a representation of the expression-list.  If there is no
5028    expression-list, VOID_ZERO_NODE is returned.  */
5029
5030 static tree
5031 cp_parser_new_initializer (cp_parser* parser)
5032 {
5033   tree expression_list;
5034
5035   expression_list = (cp_parser_parenthesized_expression_list
5036                      (parser, false, /*non_constant_p=*/NULL));
5037   if (!expression_list)
5038     expression_list = void_zero_node;
5039
5040   return expression_list;
5041 }
5042
5043 /* Parse a delete-expression.
5044
5045    delete-expression:
5046      :: [opt] delete cast-expression
5047      :: [opt] delete [ ] cast-expression
5048
5049    Returns a representation of the expression.  */
5050
5051 static tree
5052 cp_parser_delete_expression (cp_parser* parser)
5053 {
5054   bool global_scope_p;
5055   bool array_p;
5056   tree expression;
5057
5058   /* Look for the optional `::' operator.  */
5059   global_scope_p
5060     = (cp_parser_global_scope_opt (parser,
5061                                    /*current_scope_valid_p=*/false)
5062        != NULL_TREE);
5063   /* Look for the `delete' keyword.  */
5064   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5065   /* See if the array syntax is in use.  */
5066   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5067     {
5068       /* Consume the `[' token.  */
5069       cp_lexer_consume_token (parser->lexer);
5070       /* Look for the `]' token.  */
5071       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5072       /* Remember that this is the `[]' construct.  */
5073       array_p = true;
5074     }
5075   else
5076     array_p = false;
5077
5078   /* Parse the cast-expression.  */
5079   expression = cp_parser_simple_cast_expression (parser);
5080
5081   /* A delete-expression may not appear in an integral constant
5082      expression.  */
5083   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5084     return error_mark_node;
5085
5086   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5087 }
5088
5089 /* Parse a cast-expression.
5090
5091    cast-expression:
5092      unary-expression
5093      ( type-id ) cast-expression
5094
5095    Returns a representation of the expression.  */
5096
5097 static tree
5098 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5099 {
5100   /* If it's a `(', then we might be looking at a cast.  */
5101   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5102     {
5103       tree type = NULL_TREE;
5104       tree expr = NULL_TREE;
5105       bool compound_literal_p;
5106       const char *saved_message;
5107
5108       /* There's no way to know yet whether or not this is a cast.
5109          For example, `(int (3))' is a unary-expression, while `(int)
5110          3' is a cast.  So, we resort to parsing tentatively.  */
5111       cp_parser_parse_tentatively (parser);
5112       /* Types may not be defined in a cast.  */
5113       saved_message = parser->type_definition_forbidden_message;
5114       parser->type_definition_forbidden_message
5115         = "types may not be defined in casts";
5116       /* Consume the `('.  */
5117       cp_lexer_consume_token (parser->lexer);
5118       /* A very tricky bit is that `(struct S) { 3 }' is a
5119          compound-literal (which we permit in C++ as an extension).
5120          But, that construct is not a cast-expression -- it is a
5121          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5122          is legal; if the compound-literal were a cast-expression,
5123          you'd need an extra set of parentheses.)  But, if we parse
5124          the type-id, and it happens to be a class-specifier, then we
5125          will commit to the parse at that point, because we cannot
5126          undo the action that is done when creating a new class.  So,
5127          then we cannot back up and do a postfix-expression.
5128
5129          Therefore, we scan ahead to the closing `)', and check to see
5130          if the token after the `)' is a `{'.  If so, we are not
5131          looking at a cast-expression.
5132
5133          Save tokens so that we can put them back.  */
5134       cp_lexer_save_tokens (parser->lexer);
5135       /* Skip tokens until the next token is a closing parenthesis.
5136          If we find the closing `)', and the next token is a `{', then
5137          we are looking at a compound-literal.  */
5138       compound_literal_p
5139         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5140                                                   /*consume_paren=*/true)
5141            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5142       /* Roll back the tokens we skipped.  */
5143       cp_lexer_rollback_tokens (parser->lexer);
5144       /* If we were looking at a compound-literal, simulate an error
5145          so that the call to cp_parser_parse_definitely below will
5146          fail.  */
5147       if (compound_literal_p)
5148         cp_parser_simulate_error (parser);
5149       else
5150         {
5151           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5152           parser->in_type_id_in_expr_p = true;
5153           /* Look for the type-id.  */
5154           type = cp_parser_type_id (parser);
5155           /* Look for the closing `)'.  */
5156           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5157           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5158         }
5159
5160       /* Restore the saved message.  */
5161       parser->type_definition_forbidden_message = saved_message;
5162
5163       /* If ok so far, parse the dependent expression. We cannot be
5164          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5165          ctor of T, but looks like a cast to function returning T
5166          without a dependent expression.  */
5167       if (!cp_parser_error_occurred (parser))
5168         expr = cp_parser_simple_cast_expression (parser);
5169
5170       if (cp_parser_parse_definitely (parser))
5171         {
5172           /* Warn about old-style casts, if so requested.  */
5173           if (warn_old_style_cast
5174               && !in_system_header
5175               && !VOID_TYPE_P (type)
5176               && current_lang_name != lang_name_c)
5177             warning ("use of old-style cast");
5178
5179           /* Only type conversions to integral or enumeration types
5180              can be used in constant-expressions.  */
5181           if (parser->integral_constant_expression_p
5182               && !dependent_type_p (type)
5183               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5184               && (cp_parser_non_integral_constant_expression
5185                   (parser,
5186                    "a cast to a type other than an integral or "
5187                    "enumeration type")))
5188             return error_mark_node;
5189
5190           /* Perform the cast.  */
5191           expr = build_c_cast (type, expr);
5192           return expr;
5193         }
5194     }
5195
5196   /* If we get here, then it's not a cast, so it must be a
5197      unary-expression.  */
5198   return cp_parser_unary_expression (parser, address_p);
5199 }
5200
5201 /* Parse a binary expression of the general form:
5202
5203    pm-expression:
5204      cast-expression
5205      pm-expression .* cast-expression
5206      pm-expression ->* cast-expression
5207
5208    multiplicative-expression:
5209      pm-expression
5210      multiplicative-expression * pm-expression
5211      multiplicative-expression / pm-expression
5212      multiplicative-expression % pm-expression
5213
5214    additive-expression:
5215      multiplicative-expression
5216      additive-expression + multiplicative-expression
5217      additive-expression - multiplicative-expression
5218
5219    shift-expression:
5220      additive-expression
5221      shift-expression << additive-expression
5222      shift-expression >> additive-expression
5223
5224    relational-expression:
5225      shift-expression
5226      relational-expression < shift-expression
5227      relational-expression > shift-expression
5228      relational-expression <= shift-expression
5229      relational-expression >= shift-expression
5230
5231   GNU Extension:
5232   
5233    relational-expression:
5234      relational-expression <? shift-expression
5235      relational-expression >? shift-expression
5236
5237    equality-expression:
5238      relational-expression
5239      equality-expression == relational-expression
5240      equality-expression != relational-expression
5241
5242    and-expression:
5243      equality-expression
5244      and-expression & equality-expression
5245
5246    exclusive-or-expression:
5247      and-expression
5248      exclusive-or-expression ^ and-expression
5249
5250    inclusive-or-expression:
5251      exclusive-or-expression
5252      inclusive-or-expression | exclusive-or-expression
5253
5254    logical-and-expression:
5255      inclusive-or-expression
5256      logical-and-expression && inclusive-or-expression
5257
5258    logical-or-expression:
5259      logical-and-expression
5260      logical-or-expression || logical-and-expression
5261
5262    All these are implemented with a single function like:
5263
5264    binary-expression:
5265      simple-cast-expression
5266      binary-expression <token> binary-expression
5267
5268    The binops_by_token map is used to get the tree codes for each <token> type.
5269    binary-expressions are associated according to a precedence table.  */
5270
5271 #define TOKEN_PRECEDENCE(token) \
5272   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5273    ? PREC_NOT_OPERATOR \
5274    : binops_by_token[token->type].prec)
5275
5276 static tree
5277 cp_parser_binary_expression (cp_parser* parser)
5278 {
5279   cp_parser_expression_stack stack;
5280   cp_parser_expression_stack_entry *sp = &stack[0];
5281   tree lhs, rhs;
5282   cp_token *token;
5283   enum tree_code tree_type;
5284   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5285   bool overloaded_p;
5286
5287   /* Parse the first expression.  */
5288   lhs = cp_parser_simple_cast_expression (parser);
5289
5290   for (;;)
5291     {
5292       /* Get an operator token.  */
5293       token = cp_lexer_peek_token (parser->lexer);
5294       new_prec = TOKEN_PRECEDENCE (token);
5295
5296       /* Popping an entry off the stack means we completed a subexpression:
5297          - either we found a token which is not an operator (`>' where it is not
5298            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5299            will happen repeatedly;
5300          - or, we found an operator which has lower priority.  This is the case 
5301            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5302            parsing `3 * 4'.  */
5303       if (new_prec <= prec)
5304         {
5305           if (sp == stack)
5306             break;
5307           else
5308             goto pop;
5309         }
5310
5311      get_rhs:
5312       tree_type = binops_by_token[token->type].tree_type;
5313
5314       /* We used the operator token.  */
5315       cp_lexer_consume_token (parser->lexer);
5316
5317       /* Extract another operand.  It may be the RHS of this expression
5318          or the LHS of a new, higher priority expression.  */
5319       rhs = cp_parser_simple_cast_expression (parser);
5320
5321       /* Get another operator token.  Look up its precedence to avoid
5322          building a useless (immediately popped) stack entry for common
5323          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5324       token = cp_lexer_peek_token (parser->lexer);
5325       lookahead_prec = TOKEN_PRECEDENCE (token);
5326       if (lookahead_prec > new_prec)
5327         {
5328           /* ... and prepare to parse the RHS of the new, higher priority
5329              expression.  Since precedence levels on the stack are
5330              monotonically increasing, we do not have to care about
5331              stack overflows.  */
5332           sp->prec = prec;
5333           sp->tree_type = tree_type;
5334           sp->lhs = lhs;
5335           sp++;
5336           lhs = rhs;
5337           prec = new_prec;
5338           new_prec = lookahead_prec;
5339           goto get_rhs;
5340
5341          pop:
5342           /* If the stack is not empty, we have parsed into LHS the right side
5343              (`4' in the example above) of an expression we had suspended.
5344              We can use the information on the stack to recover the LHS (`3') 
5345              from the stack together with the tree code (`MULT_EXPR'), and
5346              the precedence of the higher level subexpression
5347              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5348              which will be used to actually build the additive expression.  */
5349           --sp;
5350           prec = sp->prec;
5351           tree_type = sp->tree_type;
5352           rhs = lhs;
5353           lhs = sp->lhs;
5354         }
5355
5356       overloaded_p = false;
5357       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5358
5359       /* If the binary operator required the use of an overloaded operator,
5360          then this expression cannot be an integral constant-expression.
5361          An overloaded operator can be used even if both operands are
5362          otherwise permissible in an integral constant-expression if at
5363          least one of the operands is of enumeration type.  */
5364
5365       if (overloaded_p
5366           && (cp_parser_non_integral_constant_expression 
5367               (parser, "calls to overloaded operators")))
5368         return error_mark_node;
5369     }
5370
5371   return lhs;
5372 }
5373
5374
5375 /* Parse the `? expression : assignment-expression' part of a
5376    conditional-expression.  The LOGICAL_OR_EXPR is the
5377    logical-or-expression that started the conditional-expression.
5378    Returns a representation of the entire conditional-expression.
5379
5380    This routine is used by cp_parser_assignment_expression.
5381
5382      ? expression : assignment-expression
5383
5384    GNU Extensions:
5385
5386      ? : assignment-expression */
5387
5388 static tree
5389 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5390 {
5391   tree expr;
5392   tree assignment_expr;
5393
5394   /* Consume the `?' token.  */
5395   cp_lexer_consume_token (parser->lexer);
5396   if (cp_parser_allow_gnu_extensions_p (parser)
5397       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5398     /* Implicit true clause.  */
5399     expr = NULL_TREE;
5400   else
5401     /* Parse the expression.  */
5402     expr = cp_parser_expression (parser);
5403
5404   /* The next token should be a `:'.  */
5405   cp_parser_require (parser, CPP_COLON, "`:'");
5406   /* Parse the assignment-expression.  */
5407   assignment_expr = cp_parser_assignment_expression (parser);
5408
5409   /* Build the conditional-expression.  */
5410   return build_x_conditional_expr (logical_or_expr,
5411                                    expr,
5412                                    assignment_expr);
5413 }
5414
5415 /* Parse an assignment-expression.
5416
5417    assignment-expression:
5418      conditional-expression
5419      logical-or-expression assignment-operator assignment_expression
5420      throw-expression
5421
5422    Returns a representation for the expression.  */
5423
5424 static tree
5425 cp_parser_assignment_expression (cp_parser* parser)
5426 {
5427   tree expr;
5428
5429   /* If the next token is the `throw' keyword, then we're looking at
5430      a throw-expression.  */
5431   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5432     expr = cp_parser_throw_expression (parser);
5433   /* Otherwise, it must be that we are looking at a
5434      logical-or-expression.  */
5435   else
5436     {
5437       /* Parse the binary expressions (logical-or-expression).  */
5438       expr = cp_parser_binary_expression (parser);
5439       /* If the next token is a `?' then we're actually looking at a
5440          conditional-expression.  */
5441       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5442         return cp_parser_question_colon_clause (parser, expr);
5443       else
5444         {
5445           enum tree_code assignment_operator;
5446
5447           /* If it's an assignment-operator, we're using the second
5448              production.  */
5449           assignment_operator
5450             = cp_parser_assignment_operator_opt (parser);
5451           if (assignment_operator != ERROR_MARK)
5452             {
5453               tree rhs;
5454
5455               /* Parse the right-hand side of the assignment.  */
5456               rhs = cp_parser_assignment_expression (parser);
5457               /* An assignment may not appear in a
5458                  constant-expression.  */
5459               if (cp_parser_non_integral_constant_expression (parser,
5460                                                               "an assignment"))
5461                 return error_mark_node;
5462               /* Build the assignment expression.  */
5463               expr = build_x_modify_expr (expr,
5464                                           assignment_operator,
5465                                           rhs);
5466             }
5467         }
5468     }
5469
5470   return expr;
5471 }
5472
5473 /* Parse an (optional) assignment-operator.
5474
5475    assignment-operator: one of
5476      = *= /= %= += -= >>= <<= &= ^= |=
5477
5478    GNU Extension:
5479
5480    assignment-operator: one of
5481      <?= >?=
5482
5483    If the next token is an assignment operator, the corresponding tree
5484    code is returned, and the token is consumed.  For example, for
5485    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5486    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5487    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5488    operator, ERROR_MARK is returned.  */
5489
5490 static enum tree_code
5491 cp_parser_assignment_operator_opt (cp_parser* parser)
5492 {
5493   enum tree_code op;
5494   cp_token *token;
5495
5496   /* Peek at the next toen.  */
5497   token = cp_lexer_peek_token (parser->lexer);
5498
5499   switch (token->type)
5500     {
5501     case CPP_EQ:
5502       op = NOP_EXPR;
5503       break;
5504
5505     case CPP_MULT_EQ:
5506       op = MULT_EXPR;
5507       break;
5508
5509     case CPP_DIV_EQ:
5510       op = TRUNC_DIV_EXPR;
5511       break;
5512
5513     case CPP_MOD_EQ:
5514       op = TRUNC_MOD_EXPR;
5515       break;
5516
5517     case CPP_PLUS_EQ:
5518       op = PLUS_EXPR;
5519       break;
5520
5521     case CPP_MINUS_EQ:
5522       op = MINUS_EXPR;
5523       break;
5524
5525     case CPP_RSHIFT_EQ:
5526       op = RSHIFT_EXPR;
5527       break;
5528
5529     case CPP_LSHIFT_EQ:
5530       op = LSHIFT_EXPR;
5531       break;
5532
5533     case CPP_AND_EQ:
5534       op = BIT_AND_EXPR;
5535       break;
5536
5537     case CPP_XOR_EQ:
5538       op = BIT_XOR_EXPR;
5539       break;
5540
5541     case CPP_OR_EQ:
5542       op = BIT_IOR_EXPR;
5543       break;
5544
5545     case CPP_MIN_EQ:
5546       op = MIN_EXPR;
5547       break;
5548
5549     case CPP_MAX_EQ:
5550       op = MAX_EXPR;
5551       break;
5552
5553     default:
5554       /* Nothing else is an assignment operator.  */
5555       op = ERROR_MARK;
5556     }
5557
5558   /* If it was an assignment operator, consume it.  */
5559   if (op != ERROR_MARK)
5560     cp_lexer_consume_token (parser->lexer);
5561
5562   return op;
5563 }
5564
5565 /* Parse an expression.
5566
5567    expression:
5568      assignment-expression
5569      expression , assignment-expression
5570
5571    Returns a representation of the expression.  */
5572
5573 static tree
5574 cp_parser_expression (cp_parser* parser)
5575 {
5576   tree expression = NULL_TREE;
5577
5578   while (true)
5579     {
5580       tree assignment_expression;
5581
5582       /* Parse the next assignment-expression.  */
5583       assignment_expression
5584         = cp_parser_assignment_expression (parser);
5585       /* If this is the first assignment-expression, we can just
5586          save it away.  */
5587       if (!expression)
5588         expression = assignment_expression;
5589       else
5590         expression = build_x_compound_expr (expression,
5591                                             assignment_expression);
5592       /* If the next token is not a comma, then we are done with the
5593          expression.  */
5594       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5595         break;
5596       /* Consume the `,'.  */
5597       cp_lexer_consume_token (parser->lexer);
5598       /* A comma operator cannot appear in a constant-expression.  */
5599       if (cp_parser_non_integral_constant_expression (parser,
5600                                                       "a comma operator"))
5601         expression = error_mark_node;
5602     }
5603
5604   return expression;
5605 }
5606
5607 /* Parse a constant-expression.
5608
5609    constant-expression:
5610      conditional-expression
5611
5612   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5613   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5614   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5615   is false, NON_CONSTANT_P should be NULL.  */
5616
5617 static tree
5618 cp_parser_constant_expression (cp_parser* parser,
5619                                bool allow_non_constant_p,
5620                                bool *non_constant_p)
5621 {
5622   bool saved_integral_constant_expression_p;
5623   bool saved_allow_non_integral_constant_expression_p;
5624   bool saved_non_integral_constant_expression_p;
5625   tree expression;
5626
5627   /* It might seem that we could simply parse the
5628      conditional-expression, and then check to see if it were
5629      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5630      one that the compiler can figure out is constant, possibly after
5631      doing some simplifications or optimizations.  The standard has a
5632      precise definition of constant-expression, and we must honor
5633      that, even though it is somewhat more restrictive.
5634
5635      For example:
5636
5637        int i[(2, 3)];
5638
5639      is not a legal declaration, because `(2, 3)' is not a
5640      constant-expression.  The `,' operator is forbidden in a
5641      constant-expression.  However, GCC's constant-folding machinery
5642      will fold this operation to an INTEGER_CST for `3'.  */
5643
5644   /* Save the old settings.  */
5645   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5646   saved_allow_non_integral_constant_expression_p
5647     = parser->allow_non_integral_constant_expression_p;
5648   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5649   /* We are now parsing a constant-expression.  */
5650   parser->integral_constant_expression_p = true;
5651   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5652   parser->non_integral_constant_expression_p = false;
5653   /* Although the grammar says "conditional-expression", we parse an
5654      "assignment-expression", which also permits "throw-expression"
5655      and the use of assignment operators.  In the case that
5656      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5657      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5658      actually essential that we look for an assignment-expression.
5659      For example, cp_parser_initializer_clauses uses this function to
5660      determine whether a particular assignment-expression is in fact
5661      constant.  */
5662   expression = cp_parser_assignment_expression (parser);
5663   /* Restore the old settings.  */
5664   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5665   parser->allow_non_integral_constant_expression_p
5666     = saved_allow_non_integral_constant_expression_p;
5667   if (allow_non_constant_p)
5668     *non_constant_p = parser->non_integral_constant_expression_p;
5669   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5670
5671   return expression;
5672 }
5673
5674 /* Parse __builtin_offsetof.
5675
5676    offsetof-expression:
5677      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5678
5679    offsetof-member-designator:
5680      id-expression
5681      | offsetof-member-designator "." id-expression
5682      | offsetof-member-designator "[" expression "]"
5683 */
5684
5685 static tree
5686 cp_parser_builtin_offsetof (cp_parser *parser)
5687 {
5688   int save_ice_p, save_non_ice_p;
5689   tree type, expr;
5690   cp_id_kind dummy;
5691
5692   /* We're about to accept non-integral-constant things, but will
5693      definitely yield an integral constant expression.  Save and
5694      restore these values around our local parsing.  */
5695   save_ice_p = parser->integral_constant_expression_p;
5696   save_non_ice_p = parser->non_integral_constant_expression_p;
5697
5698   /* Consume the "__builtin_offsetof" token.  */
5699   cp_lexer_consume_token (parser->lexer);
5700   /* Consume the opening `('.  */
5701   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5702   /* Parse the type-id.  */
5703   type = cp_parser_type_id (parser);
5704   /* Look for the `,'.  */
5705   cp_parser_require (parser, CPP_COMMA, "`,'");
5706
5707   /* Build the (type *)null that begins the traditional offsetof macro.  */
5708   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5709
5710   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5711   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5712                                                  true, &dummy);
5713   while (true)
5714     {
5715       cp_token *token = cp_lexer_peek_token (parser->lexer);
5716       switch (token->type)
5717         {
5718         case CPP_OPEN_SQUARE:
5719           /* offsetof-member-designator "[" expression "]" */
5720           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5721           break;
5722
5723         case CPP_DOT:
5724           /* offsetof-member-designator "." identifier */
5725           cp_lexer_consume_token (parser->lexer);
5726           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5727                                                          true, &dummy);
5728           break;
5729
5730         case CPP_CLOSE_PAREN:
5731           /* Consume the ")" token.  */
5732           cp_lexer_consume_token (parser->lexer);
5733           goto success;
5734
5735         default:
5736           /* Error.  We know the following require will fail, but
5737              that gives the proper error message.  */
5738           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5739           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5740           expr = error_mark_node;
5741           goto failure;
5742         }
5743     }
5744
5745  success:
5746   /* If we're processing a template, we can't finish the semantics yet.
5747      Otherwise we can fold the entire expression now.  */
5748   if (processing_template_decl)
5749     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5750   else
5751     expr = fold_offsetof (expr);
5752
5753  failure:
5754   parser->integral_constant_expression_p = save_ice_p;
5755   parser->non_integral_constant_expression_p = save_non_ice_p;
5756
5757   return expr;
5758 }
5759
5760 /* Statements [gram.stmt.stmt]  */
5761
5762 /* Parse a statement.
5763
5764    statement:
5765      labeled-statement
5766      expression-statement
5767      compound-statement
5768      selection-statement
5769      iteration-statement
5770      jump-statement
5771      declaration-statement
5772      try-block  */
5773
5774 static void
5775 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5776 {
5777   tree statement;
5778   cp_token *token;
5779   location_t statement_location;
5780
5781   /* There is no statement yet.  */
5782   statement = NULL_TREE;
5783   /* Peek at the next token.  */
5784   token = cp_lexer_peek_token (parser->lexer);
5785   /* Remember the location of the first token in the statement.  */
5786   statement_location = token->location;
5787   /* If this is a keyword, then that will often determine what kind of
5788      statement we have.  */
5789   if (token->type == CPP_KEYWORD)
5790     {
5791       enum rid keyword = token->keyword;
5792
5793       switch (keyword)
5794         {
5795         case RID_CASE:
5796         case RID_DEFAULT:
5797           statement = cp_parser_labeled_statement (parser,
5798                                                    in_statement_expr);
5799           break;
5800
5801         case RID_IF:
5802         case RID_SWITCH:
5803           statement = cp_parser_selection_statement (parser);
5804           break;
5805
5806         case RID_WHILE:
5807         case RID_DO:
5808         case RID_FOR:
5809           statement = cp_parser_iteration_statement (parser);
5810           break;
5811
5812         case RID_BREAK:
5813         case RID_CONTINUE:
5814         case RID_RETURN:
5815         case RID_GOTO:
5816           statement = cp_parser_jump_statement (parser);
5817           break;
5818
5819         case RID_TRY:
5820           statement = cp_parser_try_block (parser);
5821           break;
5822
5823         default:
5824           /* It might be a keyword like `int' that can start a
5825              declaration-statement.  */
5826           break;
5827         }
5828     }
5829   else if (token->type == CPP_NAME)
5830     {
5831       /* If the next token is a `:', then we are looking at a
5832          labeled-statement.  */
5833       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5834       if (token->type == CPP_COLON)
5835         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5836     }
5837   /* Anything that starts with a `{' must be a compound-statement.  */
5838   else if (token->type == CPP_OPEN_BRACE)
5839     statement = cp_parser_compound_statement (parser, NULL, false);
5840   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5841      a statement all its own.  */
5842   else if (token->type == CPP_PRAGMA)
5843     {
5844       cp_lexer_handle_pragma (parser->lexer);
5845       return;
5846     }
5847
5848   /* Everything else must be a declaration-statement or an
5849      expression-statement.  Try for the declaration-statement
5850      first, unless we are looking at a `;', in which case we know that
5851      we have an expression-statement.  */
5852   if (!statement)
5853     {
5854       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5855         {
5856           cp_parser_parse_tentatively (parser);
5857           /* Try to parse the declaration-statement.  */
5858           cp_parser_declaration_statement (parser);
5859           /* If that worked, we're done.  */
5860           if (cp_parser_parse_definitely (parser))
5861             return;
5862         }
5863       /* Look for an expression-statement instead.  */
5864       statement = cp_parser_expression_statement (parser, in_statement_expr);
5865     }
5866
5867   /* Set the line number for the statement.  */
5868   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5869     SET_EXPR_LOCATION (statement, statement_location);
5870 }
5871
5872 /* Parse a labeled-statement.
5873
5874    labeled-statement:
5875      identifier : statement
5876      case constant-expression : statement
5877      default : statement
5878
5879    GNU Extension:
5880
5881    labeled-statement:
5882      case constant-expression ... constant-expression : statement
5883
5884    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5885    For an ordinary label, returns a LABEL_EXPR.  */
5886
5887 static tree
5888 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5889 {
5890   cp_token *token;
5891   tree statement = error_mark_node;
5892
5893   /* The next token should be an identifier.  */
5894   token = cp_lexer_peek_token (parser->lexer);
5895   if (token->type != CPP_NAME
5896       && token->type != CPP_KEYWORD)
5897     {
5898       cp_parser_error (parser, "expected labeled-statement");
5899       return error_mark_node;
5900     }
5901
5902   switch (token->keyword)
5903     {
5904     case RID_CASE:
5905       {
5906         tree expr, expr_hi;
5907         cp_token *ellipsis;
5908
5909         /* Consume the `case' token.  */
5910         cp_lexer_consume_token (parser->lexer);
5911         /* Parse the constant-expression.  */
5912         expr = cp_parser_constant_expression (parser,
5913                                               /*allow_non_constant_p=*/false,
5914                                               NULL);
5915
5916         ellipsis = cp_lexer_peek_token (parser->lexer);
5917         if (ellipsis->type == CPP_ELLIPSIS)
5918           {
5919             /* Consume the `...' token.  */
5920             cp_lexer_consume_token (parser->lexer);
5921             expr_hi =
5922               cp_parser_constant_expression (parser,
5923                                              /*allow_non_constant_p=*/false,
5924                                              NULL);
5925             /* We don't need to emit warnings here, as the common code
5926                will do this for us.  */
5927           }
5928         else
5929           expr_hi = NULL_TREE;
5930
5931         if (!parser->in_switch_statement_p)
5932           error ("case label %qE not within a switch statement", expr);
5933         else
5934           statement = finish_case_label (expr, expr_hi);
5935       }
5936       break;
5937
5938     case RID_DEFAULT:
5939       /* Consume the `default' token.  */
5940       cp_lexer_consume_token (parser->lexer);
5941       if (!parser->in_switch_statement_p)
5942         error ("case label not within a switch statement");
5943       else
5944         statement = finish_case_label (NULL_TREE, NULL_TREE);
5945       break;
5946
5947     default:
5948       /* Anything else must be an ordinary label.  */
5949       statement = finish_label_stmt (cp_parser_identifier (parser));
5950       break;
5951     }
5952
5953   /* Require the `:' token.  */
5954   cp_parser_require (parser, CPP_COLON, "`:'");
5955   /* Parse the labeled statement.  */
5956   cp_parser_statement (parser, in_statement_expr);
5957
5958   /* Return the label, in the case of a `case' or `default' label.  */
5959   return statement;
5960 }
5961
5962 /* Parse an expression-statement.
5963
5964    expression-statement:
5965      expression [opt] ;
5966
5967    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5968    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5969    indicates whether this expression-statement is part of an
5970    expression statement.  */
5971
5972 static tree
5973 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
5974 {
5975   tree statement = NULL_TREE;
5976
5977   /* If the next token is a ';', then there is no expression
5978      statement.  */
5979   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5980     statement = cp_parser_expression (parser);
5981
5982   /* Consume the final `;'.  */
5983   cp_parser_consume_semicolon_at_end_of_statement (parser);
5984
5985   if (in_statement_expr
5986       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5987     {
5988       /* This is the final expression statement of a statement
5989          expression.  */
5990       statement = finish_stmt_expr_expr (statement, in_statement_expr);
5991     }
5992   else if (statement)
5993     statement = finish_expr_stmt (statement);
5994   else
5995     finish_stmt ();
5996
5997   return statement;
5998 }
5999
6000 /* Parse a compound-statement.
6001
6002    compound-statement:
6003      { statement-seq [opt] }
6004
6005    Returns a tree representing the statement.  */
6006
6007 static tree
6008 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6009                               bool in_try)
6010 {
6011   tree compound_stmt;
6012
6013   /* Consume the `{'.  */
6014   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6015     return error_mark_node;
6016   /* Begin the compound-statement.  */
6017   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6018   /* Parse an (optional) statement-seq.  */
6019   cp_parser_statement_seq_opt (parser, in_statement_expr);
6020   /* Finish the compound-statement.  */
6021   finish_compound_stmt (compound_stmt);
6022   /* Consume the `}'.  */
6023   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6024
6025   return compound_stmt;
6026 }
6027
6028 /* Parse an (optional) statement-seq.
6029
6030    statement-seq:
6031      statement
6032      statement-seq [opt] statement  */
6033
6034 static void
6035 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6036 {
6037   /* Scan statements until there aren't any more.  */
6038   while (true)
6039     {
6040       /* If we're looking at a `}', then we've run out of statements.  */
6041       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6042           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6043         break;
6044
6045       /* Parse the statement.  */
6046       cp_parser_statement (parser, in_statement_expr);
6047     }
6048 }
6049
6050 /* Parse a selection-statement.
6051
6052    selection-statement:
6053      if ( condition ) statement
6054      if ( condition ) statement else statement
6055      switch ( condition ) statement
6056
6057    Returns the new IF_STMT or SWITCH_STMT.  */
6058
6059 static tree
6060 cp_parser_selection_statement (cp_parser* parser)
6061 {
6062   cp_token *token;
6063   enum rid keyword;
6064
6065   /* Peek at the next token.  */
6066   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6067
6068   /* See what kind of keyword it is.  */
6069   keyword = token->keyword;
6070   switch (keyword)
6071     {
6072     case RID_IF:
6073     case RID_SWITCH:
6074       {
6075         tree statement;
6076         tree condition;
6077
6078         /* Look for the `('.  */
6079         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6080           {
6081             cp_parser_skip_to_end_of_statement (parser);
6082             return error_mark_node;
6083           }
6084
6085         /* Begin the selection-statement.  */
6086         if (keyword == RID_IF)
6087           statement = begin_if_stmt ();
6088         else
6089           statement = begin_switch_stmt ();
6090
6091         /* Parse the condition.  */
6092         condition = cp_parser_condition (parser);
6093         /* Look for the `)'.  */
6094         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6095           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6096                                                  /*consume_paren=*/true);
6097
6098         if (keyword == RID_IF)
6099           {
6100             /* Add the condition.  */
6101             finish_if_stmt_cond (condition, statement);
6102
6103             /* Parse the then-clause.  */
6104             cp_parser_implicitly_scoped_statement (parser);
6105             finish_then_clause (statement);
6106
6107             /* If the next token is `else', parse the else-clause.  */
6108             if (cp_lexer_next_token_is_keyword (parser->lexer,
6109                                                 RID_ELSE))
6110               {
6111                 /* Consume the `else' keyword.  */
6112                 cp_lexer_consume_token (parser->lexer);
6113                 begin_else_clause (statement);
6114                 /* Parse the else-clause.  */
6115                 cp_parser_implicitly_scoped_statement (parser);
6116                 finish_else_clause (statement);
6117               }
6118
6119             /* Now we're all done with the if-statement.  */
6120             finish_if_stmt (statement);
6121           }
6122         else
6123           {
6124             bool in_switch_statement_p;
6125
6126             /* Add the condition.  */
6127             finish_switch_cond (condition, statement);
6128
6129             /* Parse the body of the switch-statement.  */
6130             in_switch_statement_p = parser->in_switch_statement_p;
6131             parser->in_switch_statement_p = true;
6132             cp_parser_implicitly_scoped_statement (parser);
6133             parser->in_switch_statement_p = in_switch_statement_p;
6134
6135             /* Now we're all done with the switch-statement.  */
6136             finish_switch_stmt (statement);
6137           }
6138
6139         return statement;
6140       }
6141       break;
6142
6143     default:
6144       cp_parser_error (parser, "expected selection-statement");
6145       return error_mark_node;
6146     }
6147 }
6148
6149 /* Parse a condition.
6150
6151    condition:
6152      expression
6153      type-specifier-seq declarator = assignment-expression
6154
6155    GNU Extension:
6156
6157    condition:
6158      type-specifier-seq declarator asm-specification [opt]
6159        attributes [opt] = assignment-expression
6160
6161    Returns the expression that should be tested.  */
6162
6163 static tree
6164 cp_parser_condition (cp_parser* parser)
6165 {
6166   cp_decl_specifier_seq type_specifiers;
6167   const char *saved_message;
6168
6169   /* Try the declaration first.  */
6170   cp_parser_parse_tentatively (parser);
6171   /* New types are not allowed in the type-specifier-seq for a
6172      condition.  */
6173   saved_message = parser->type_definition_forbidden_message;
6174   parser->type_definition_forbidden_message
6175     = "types may not be defined in conditions";
6176   /* Parse the type-specifier-seq.  */
6177   cp_parser_type_specifier_seq (parser, &type_specifiers);
6178   /* Restore the saved message.  */
6179   parser->type_definition_forbidden_message = saved_message;
6180   /* If all is well, we might be looking at a declaration.  */
6181   if (!cp_parser_error_occurred (parser))
6182     {
6183       tree decl;
6184       tree asm_specification;
6185       tree attributes;
6186       cp_declarator *declarator;
6187       tree initializer = NULL_TREE;
6188
6189       /* Parse the declarator.  */
6190       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6191                                          /*ctor_dtor_or_conv_p=*/NULL,
6192                                          /*parenthesized_p=*/NULL,
6193                                          /*member_p=*/false);
6194       /* Parse the attributes.  */
6195       attributes = cp_parser_attributes_opt (parser);
6196       /* Parse the asm-specification.  */
6197       asm_specification = cp_parser_asm_specification_opt (parser);
6198       /* If the next token is not an `=', then we might still be
6199          looking at an expression.  For example:
6200
6201            if (A(a).x)
6202
6203          looks like a decl-specifier-seq and a declarator -- but then
6204          there is no `=', so this is an expression.  */
6205       cp_parser_require (parser, CPP_EQ, "`='");
6206       /* If we did see an `=', then we are looking at a declaration
6207          for sure.  */
6208       if (cp_parser_parse_definitely (parser))
6209         {
6210           bool pop_p;   
6211
6212           /* Create the declaration.  */
6213           decl = start_decl (declarator, &type_specifiers,
6214                              /*initialized_p=*/true,
6215                              attributes, /*prefix_attributes=*/NULL_TREE,
6216                              &pop_p);
6217           /* Parse the assignment-expression.  */
6218           initializer = cp_parser_assignment_expression (parser);
6219
6220           /* Process the initializer.  */
6221           cp_finish_decl (decl,
6222                           initializer,
6223                           asm_specification,
6224                           LOOKUP_ONLYCONVERTING);
6225
6226           if (pop_p)
6227             pop_scope (DECL_CONTEXT (decl));
6228
6229           return convert_from_reference (decl);
6230         }
6231     }
6232   /* If we didn't even get past the declarator successfully, we are
6233      definitely not looking at a declaration.  */
6234   else
6235     cp_parser_abort_tentative_parse (parser);
6236
6237   /* Otherwise, we are looking at an expression.  */
6238   return cp_parser_expression (parser);
6239 }
6240
6241 /* Parse an iteration-statement.
6242
6243    iteration-statement:
6244      while ( condition ) statement
6245      do statement while ( expression ) ;
6246      for ( for-init-statement condition [opt] ; expression [opt] )
6247        statement
6248
6249    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6250
6251 static tree
6252 cp_parser_iteration_statement (cp_parser* parser)
6253 {
6254   cp_token *token;
6255   enum rid keyword;
6256   tree statement;
6257   bool in_iteration_statement_p;
6258
6259
6260   /* Peek at the next token.  */
6261   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6262   if (!token)
6263     return error_mark_node;
6264
6265   /* Remember whether or not we are already within an iteration
6266      statement.  */
6267   in_iteration_statement_p = parser->in_iteration_statement_p;
6268
6269   /* See what kind of keyword it is.  */
6270   keyword = token->keyword;
6271   switch (keyword)
6272     {
6273     case RID_WHILE:
6274       {
6275         tree condition;
6276
6277         /* Begin the while-statement.  */
6278         statement = begin_while_stmt ();
6279         /* Look for the `('.  */
6280         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6281         /* Parse the condition.  */
6282         condition = cp_parser_condition (parser);
6283         finish_while_stmt_cond (condition, statement);
6284         /* Look for the `)'.  */
6285         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6286         /* Parse the dependent statement.  */
6287         parser->in_iteration_statement_p = true;
6288         cp_parser_already_scoped_statement (parser);
6289         parser->in_iteration_statement_p = in_iteration_statement_p;
6290         /* We're done with the while-statement.  */
6291         finish_while_stmt (statement);
6292       }
6293       break;
6294
6295     case RID_DO:
6296       {
6297         tree expression;
6298
6299         /* Begin the do-statement.  */
6300         statement = begin_do_stmt ();
6301         /* Parse the body of the do-statement.  */
6302         parser->in_iteration_statement_p = true;
6303         cp_parser_implicitly_scoped_statement (parser);
6304         parser->in_iteration_statement_p = in_iteration_statement_p;
6305         finish_do_body (statement);
6306         /* Look for the `while' keyword.  */
6307         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6308         /* Look for the `('.  */
6309         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6310         /* Parse the expression.  */
6311         expression = cp_parser_expression (parser);
6312         /* We're done with the do-statement.  */
6313         finish_do_stmt (expression, statement);
6314         /* Look for the `)'.  */
6315         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6316         /* Look for the `;'.  */
6317         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6318       }
6319       break;
6320
6321     case RID_FOR:
6322       {
6323         tree condition = NULL_TREE;
6324         tree expression = NULL_TREE;
6325
6326         /* Begin the for-statement.  */
6327         statement = begin_for_stmt ();
6328         /* Look for the `('.  */
6329         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6330         /* Parse the initialization.  */
6331         cp_parser_for_init_statement (parser);
6332         finish_for_init_stmt (statement);
6333
6334         /* If there's a condition, process it.  */
6335         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6336           condition = cp_parser_condition (parser);
6337         finish_for_cond (condition, statement);
6338         /* Look for the `;'.  */
6339         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6340
6341         /* If there's an expression, process it.  */
6342         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6343           expression = cp_parser_expression (parser);
6344         finish_for_expr (expression, statement);
6345         /* Look for the `)'.  */
6346         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6347
6348         /* Parse the body of the for-statement.  */
6349         parser->in_iteration_statement_p = true;
6350         cp_parser_already_scoped_statement (parser);
6351         parser->in_iteration_statement_p = in_iteration_statement_p;
6352
6353         /* We're done with the for-statement.  */
6354         finish_for_stmt (statement);
6355       }
6356       break;
6357
6358     default:
6359       cp_parser_error (parser, "expected iteration-statement");
6360       statement = error_mark_node;
6361       break;
6362     }
6363
6364   return statement;
6365 }
6366
6367 /* Parse a for-init-statement.
6368
6369    for-init-statement:
6370      expression-statement
6371      simple-declaration  */
6372
6373 static void
6374 cp_parser_for_init_statement (cp_parser* parser)
6375 {
6376   /* If the next token is a `;', then we have an empty
6377      expression-statement.  Grammatically, this is also a
6378      simple-declaration, but an invalid one, because it does not
6379      declare anything.  Therefore, if we did not handle this case
6380      specially, we would issue an error message about an invalid
6381      declaration.  */
6382   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6383     {
6384       /* We're going to speculatively look for a declaration, falling back
6385          to an expression, if necessary.  */
6386       cp_parser_parse_tentatively (parser);
6387       /* Parse the declaration.  */
6388       cp_parser_simple_declaration (parser,
6389                                     /*function_definition_allowed_p=*/false);
6390       /* If the tentative parse failed, then we shall need to look for an
6391          expression-statement.  */
6392       if (cp_parser_parse_definitely (parser))
6393         return;
6394     }
6395
6396   cp_parser_expression_statement (parser, false);
6397 }
6398
6399 /* Parse a jump-statement.
6400
6401    jump-statement:
6402      break ;
6403      continue ;
6404      return expression [opt] ;
6405      goto identifier ;
6406
6407    GNU extension:
6408
6409    jump-statement:
6410      goto * expression ;
6411
6412    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6413
6414 static tree
6415 cp_parser_jump_statement (cp_parser* parser)
6416 {
6417   tree statement = error_mark_node;
6418   cp_token *token;
6419   enum rid keyword;
6420
6421   /* Peek at the next token.  */
6422   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6423   if (!token)
6424     return error_mark_node;
6425
6426   /* See what kind of keyword it is.  */
6427   keyword = token->keyword;
6428   switch (keyword)
6429     {
6430     case RID_BREAK:
6431       if (!parser->in_switch_statement_p
6432           && !parser->in_iteration_statement_p)
6433         {
6434           error ("break statement not within loop or switch");
6435           statement = error_mark_node;
6436         }
6437       else
6438         statement = finish_break_stmt ();
6439       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6440       break;
6441
6442     case RID_CONTINUE:
6443       if (!parser->in_iteration_statement_p)
6444         {
6445           error ("continue statement not within a loop");
6446           statement = error_mark_node;
6447         }
6448       else
6449         statement = finish_continue_stmt ();
6450       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6451       break;
6452
6453     case RID_RETURN:
6454       {
6455         tree expr;
6456
6457         /* If the next token is a `;', then there is no
6458            expression.  */
6459         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6460           expr = cp_parser_expression (parser);
6461         else
6462           expr = NULL_TREE;
6463         /* Build the return-statement.  */
6464         statement = finish_return_stmt (expr);
6465         /* Look for the final `;'.  */
6466         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6467       }
6468       break;
6469
6470     case RID_GOTO:
6471       /* Create the goto-statement.  */
6472       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6473         {
6474           /* Issue a warning about this use of a GNU extension.  */
6475           if (pedantic)
6476             pedwarn ("ISO C++ forbids computed gotos");
6477           /* Consume the '*' token.  */
6478           cp_lexer_consume_token (parser->lexer);
6479           /* Parse the dependent expression.  */
6480           finish_goto_stmt (cp_parser_expression (parser));
6481         }
6482       else
6483         finish_goto_stmt (cp_parser_identifier (parser));
6484       /* Look for the final `;'.  */
6485       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6486       break;
6487
6488     default:
6489       cp_parser_error (parser, "expected jump-statement");
6490       break;
6491     }
6492
6493   return statement;
6494 }
6495
6496 /* Parse a declaration-statement.
6497
6498    declaration-statement:
6499      block-declaration  */
6500
6501 static void
6502 cp_parser_declaration_statement (cp_parser* parser)
6503 {
6504   void *p;
6505
6506   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6507   p = obstack_alloc (&declarator_obstack, 0);
6508
6509  /* Parse the block-declaration.  */
6510   cp_parser_block_declaration (parser, /*statement_p=*/true);
6511
6512   /* Free any declarators allocated.  */
6513   obstack_free (&declarator_obstack, p);
6514
6515   /* Finish off the statement.  */
6516   finish_stmt ();
6517 }
6518
6519 /* Some dependent statements (like `if (cond) statement'), are
6520    implicitly in their own scope.  In other words, if the statement is
6521    a single statement (as opposed to a compound-statement), it is
6522    none-the-less treated as if it were enclosed in braces.  Any
6523    declarations appearing in the dependent statement are out of scope
6524    after control passes that point.  This function parses a statement,
6525    but ensures that is in its own scope, even if it is not a
6526    compound-statement.
6527
6528    Returns the new statement.  */
6529
6530 static tree
6531 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6532 {
6533   tree statement;
6534
6535   /* If the token is not a `{', then we must take special action.  */
6536   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6537     {
6538       /* Create a compound-statement.  */
6539       statement = begin_compound_stmt (0);
6540       /* Parse the dependent-statement.  */
6541       cp_parser_statement (parser, false);
6542       /* Finish the dummy compound-statement.  */
6543       finish_compound_stmt (statement);
6544     }
6545   /* Otherwise, we simply parse the statement directly.  */
6546   else
6547     statement = cp_parser_compound_statement (parser, NULL, false);
6548
6549   /* Return the statement.  */
6550   return statement;
6551 }
6552
6553 /* For some dependent statements (like `while (cond) statement'), we
6554    have already created a scope.  Therefore, even if the dependent
6555    statement is a compound-statement, we do not want to create another
6556    scope.  */
6557
6558 static void
6559 cp_parser_already_scoped_statement (cp_parser* parser)
6560 {
6561   /* If the token is a `{', then we must take special action.  */
6562   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6563     cp_parser_statement (parser, false);
6564   else
6565     {
6566       /* Avoid calling cp_parser_compound_statement, so that we
6567          don't create a new scope.  Do everything else by hand.  */
6568       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6569       cp_parser_statement_seq_opt (parser, false);
6570       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6571     }
6572 }
6573
6574 /* Declarations [gram.dcl.dcl] */
6575
6576 /* Parse an optional declaration-sequence.
6577
6578    declaration-seq:
6579      declaration
6580      declaration-seq declaration  */
6581
6582 static void
6583 cp_parser_declaration_seq_opt (cp_parser* parser)
6584 {
6585   while (true)
6586     {
6587       cp_token *token;
6588
6589       token = cp_lexer_peek_token (parser->lexer);
6590
6591       if (token->type == CPP_CLOSE_BRACE
6592           || token->type == CPP_EOF)
6593         break;
6594
6595       if (token->type == CPP_SEMICOLON)
6596         {
6597           /* A declaration consisting of a single semicolon is
6598              invalid.  Allow it unless we're being pedantic.  */
6599           cp_lexer_consume_token (parser->lexer);
6600           if (pedantic && !in_system_header)
6601             pedwarn ("extra %<;%>");
6602           continue;
6603         }
6604
6605       /* If we're entering or exiting a region that's implicitly
6606          extern "C", modify the lang context appropriately.  */
6607       if (!parser->implicit_extern_c && token->implicit_extern_c)
6608         {
6609           push_lang_context (lang_name_c);
6610           parser->implicit_extern_c = true;
6611         }
6612       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6613         {
6614           pop_lang_context ();
6615           parser->implicit_extern_c = false;
6616         }
6617
6618       if (token->type == CPP_PRAGMA)
6619         {
6620           /* A top-level declaration can consist solely of a #pragma.
6621              A nested declaration cannot, so this is done here and not
6622              in cp_parser_declaration.  (A #pragma at block scope is
6623              handled in cp_parser_statement.)  */
6624           cp_lexer_handle_pragma (parser->lexer);
6625           continue;
6626         }
6627
6628       /* Parse the declaration itself.  */
6629       cp_parser_declaration (parser);
6630     }
6631 }
6632
6633 /* Parse a declaration.
6634
6635    declaration:
6636      block-declaration
6637      function-definition
6638      template-declaration
6639      explicit-instantiation
6640      explicit-specialization
6641      linkage-specification
6642      namespace-definition
6643
6644    GNU extension:
6645
6646    declaration:
6647       __extension__ declaration */
6648
6649 static void
6650 cp_parser_declaration (cp_parser* parser)
6651 {
6652   cp_token token1;
6653   cp_token token2;
6654   int saved_pedantic;
6655   void *p;
6656
6657   /* Check for the `__extension__' keyword.  */
6658   if (cp_parser_extension_opt (parser, &saved_pedantic))
6659     {
6660       /* Parse the qualified declaration.  */
6661       cp_parser_declaration (parser);
6662       /* Restore the PEDANTIC flag.  */
6663       pedantic = saved_pedantic;
6664
6665       return;
6666     }
6667
6668   /* Try to figure out what kind of declaration is present.  */
6669   token1 = *cp_lexer_peek_token (parser->lexer);
6670
6671   if (token1.type != CPP_EOF)
6672     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6673
6674   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6675   p = obstack_alloc (&declarator_obstack, 0);
6676
6677   /* If the next token is `extern' and the following token is a string
6678      literal, then we have a linkage specification.  */
6679   if (token1.keyword == RID_EXTERN
6680       && cp_parser_is_string_literal (&token2))
6681     cp_parser_linkage_specification (parser);
6682   /* If the next token is `template', then we have either a template
6683      declaration, an explicit instantiation, or an explicit
6684      specialization.  */
6685   else if (token1.keyword == RID_TEMPLATE)
6686     {
6687       /* `template <>' indicates a template specialization.  */
6688       if (token2.type == CPP_LESS
6689           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6690         cp_parser_explicit_specialization (parser);
6691       /* `template <' indicates a template declaration.  */
6692       else if (token2.type == CPP_LESS)
6693         cp_parser_template_declaration (parser, /*member_p=*/false);
6694       /* Anything else must be an explicit instantiation.  */
6695       else
6696         cp_parser_explicit_instantiation (parser);
6697     }
6698   /* If the next token is `export', then we have a template
6699      declaration.  */
6700   else if (token1.keyword == RID_EXPORT)
6701     cp_parser_template_declaration (parser, /*member_p=*/false);
6702   /* If the next token is `extern', 'static' or 'inline' and the one
6703      after that is `template', we have a GNU extended explicit
6704      instantiation directive.  */
6705   else if (cp_parser_allow_gnu_extensions_p (parser)
6706            && (token1.keyword == RID_EXTERN
6707                || token1.keyword == RID_STATIC
6708                || token1.keyword == RID_INLINE)
6709            && token2.keyword == RID_TEMPLATE)
6710     cp_parser_explicit_instantiation (parser);
6711   /* If the next token is `namespace', check for a named or unnamed
6712      namespace definition.  */
6713   else if (token1.keyword == RID_NAMESPACE
6714            && (/* A named namespace definition.  */
6715                (token2.type == CPP_NAME
6716                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6717                     == CPP_OPEN_BRACE))
6718                /* An unnamed namespace definition.  */
6719                || token2.type == CPP_OPEN_BRACE))
6720     cp_parser_namespace_definition (parser);
6721   /* We must have either a block declaration or a function
6722      definition.  */
6723   else
6724     /* Try to parse a block-declaration, or a function-definition.  */
6725     cp_parser_block_declaration (parser, /*statement_p=*/false);
6726
6727   /* Free any declarators allocated.  */
6728   obstack_free (&declarator_obstack, p);
6729 }
6730
6731 /* Parse a block-declaration.
6732
6733    block-declaration:
6734      simple-declaration
6735      asm-definition
6736      namespace-alias-definition
6737      using-declaration
6738      using-directive
6739
6740    GNU Extension:
6741
6742    block-declaration:
6743      __extension__ block-declaration
6744      label-declaration
6745
6746    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6747    part of a declaration-statement.  */
6748
6749 static void
6750 cp_parser_block_declaration (cp_parser *parser,
6751                              bool      statement_p)
6752 {
6753   cp_token *token1;
6754   int saved_pedantic;
6755
6756   /* Check for the `__extension__' keyword.  */
6757   if (cp_parser_extension_opt (parser, &saved_pedantic))
6758     {
6759       /* Parse the qualified declaration.  */
6760       cp_parser_block_declaration (parser, statement_p);
6761       /* Restore the PEDANTIC flag.  */
6762       pedantic = saved_pedantic;
6763
6764       return;
6765     }
6766
6767   /* Peek at the next token to figure out which kind of declaration is
6768      present.  */
6769   token1 = cp_lexer_peek_token (parser->lexer);
6770
6771   /* If the next keyword is `asm', we have an asm-definition.  */
6772   if (token1->keyword == RID_ASM)
6773     {
6774       if (statement_p)
6775         cp_parser_commit_to_tentative_parse (parser);
6776       cp_parser_asm_definition (parser);
6777     }
6778   /* If the next keyword is `namespace', we have a
6779      namespace-alias-definition.  */
6780   else if (token1->keyword == RID_NAMESPACE)
6781     cp_parser_namespace_alias_definition (parser);
6782   /* If the next keyword is `using', we have either a
6783      using-declaration or a using-directive.  */
6784   else if (token1->keyword == RID_USING)
6785     {
6786       cp_token *token2;
6787
6788       if (statement_p)
6789         cp_parser_commit_to_tentative_parse (parser);
6790       /* If the token after `using' is `namespace', then we have a
6791          using-directive.  */
6792       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6793       if (token2->keyword == RID_NAMESPACE)
6794         cp_parser_using_directive (parser);
6795       /* Otherwise, it's a using-declaration.  */
6796       else
6797         cp_parser_using_declaration (parser);
6798     }
6799   /* If the next keyword is `__label__' we have a label declaration.  */
6800   else if (token1->keyword == RID_LABEL)
6801     {
6802       if (statement_p)
6803         cp_parser_commit_to_tentative_parse (parser);
6804       cp_parser_label_declaration (parser);
6805     }
6806   /* Anything else must be a simple-declaration.  */
6807   else
6808     cp_parser_simple_declaration (parser, !statement_p);
6809 }
6810
6811 /* Parse a simple-declaration.
6812
6813    simple-declaration:
6814      decl-specifier-seq [opt] init-declarator-list [opt] ;
6815
6816    init-declarator-list:
6817      init-declarator
6818      init-declarator-list , init-declarator
6819
6820    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6821    function-definition as a simple-declaration.  */
6822
6823 static void
6824 cp_parser_simple_declaration (cp_parser* parser,
6825                               bool function_definition_allowed_p)
6826 {
6827   cp_decl_specifier_seq decl_specifiers;
6828   int declares_class_or_enum;
6829   bool saw_declarator;
6830
6831   /* Defer access checks until we know what is being declared; the
6832      checks for names appearing in the decl-specifier-seq should be
6833      done as if we were in the scope of the thing being declared.  */
6834   push_deferring_access_checks (dk_deferred);
6835
6836   /* Parse the decl-specifier-seq.  We have to keep track of whether
6837      or not the decl-specifier-seq declares a named class or
6838      enumeration type, since that is the only case in which the
6839      init-declarator-list is allowed to be empty.
6840
6841      [dcl.dcl]
6842
6843      In a simple-declaration, the optional init-declarator-list can be
6844      omitted only when declaring a class or enumeration, that is when
6845      the decl-specifier-seq contains either a class-specifier, an
6846      elaborated-type-specifier, or an enum-specifier.  */
6847   cp_parser_decl_specifier_seq (parser,
6848                                 CP_PARSER_FLAGS_OPTIONAL,
6849                                 &decl_specifiers,
6850                                 &declares_class_or_enum);
6851   /* We no longer need to defer access checks.  */
6852   stop_deferring_access_checks ();
6853
6854   /* In a block scope, a valid declaration must always have a
6855      decl-specifier-seq.  By not trying to parse declarators, we can
6856      resolve the declaration/expression ambiguity more quickly.  */
6857   if (!function_definition_allowed_p
6858       && !decl_specifiers.any_specifiers_p)
6859     {
6860       cp_parser_error (parser, "expected declaration");
6861       goto done;
6862     }
6863
6864   /* If the next two tokens are both identifiers, the code is
6865      erroneous. The usual cause of this situation is code like:
6866
6867        T t;
6868
6869      where "T" should name a type -- but does not.  */
6870   if (!decl_specifiers.type
6871       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6872     {
6873       /* If parsing tentatively, we should commit; we really are
6874          looking at a declaration.  */
6875       cp_parser_commit_to_tentative_parse (parser);
6876       /* Give up.  */
6877       goto done;
6878     }
6879   
6880   /* If we have seen at least one decl-specifier, and the next token
6881      is not a parenthesis, then we must be looking at a declaration.
6882      (After "int (" we might be looking at a functional cast.)  */
6883   if (decl_specifiers.any_specifiers_p 
6884       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6885     cp_parser_commit_to_tentative_parse (parser);
6886
6887   /* Keep going until we hit the `;' at the end of the simple
6888      declaration.  */
6889   saw_declarator = false;
6890   while (cp_lexer_next_token_is_not (parser->lexer,
6891                                      CPP_SEMICOLON))
6892     {
6893       cp_token *token;
6894       bool function_definition_p;
6895       tree decl;
6896
6897       saw_declarator = true;
6898       /* Parse the init-declarator.  */
6899       decl = cp_parser_init_declarator (parser, &decl_specifiers,
6900                                         function_definition_allowed_p,
6901                                         /*member_p=*/false,
6902                                         declares_class_or_enum,
6903                                         &function_definition_p);
6904       /* If an error occurred while parsing tentatively, exit quickly.
6905          (That usually happens when in the body of a function; each
6906          statement is treated as a declaration-statement until proven
6907          otherwise.)  */
6908       if (cp_parser_error_occurred (parser))
6909         goto done;
6910       /* Handle function definitions specially.  */
6911       if (function_definition_p)
6912         {
6913           /* If the next token is a `,', then we are probably
6914              processing something like:
6915
6916                void f() {}, *p;
6917
6918              which is erroneous.  */
6919           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6920             error ("mixing declarations and function-definitions is forbidden");
6921           /* Otherwise, we're done with the list of declarators.  */
6922           else
6923             {
6924               pop_deferring_access_checks ();
6925               return;
6926             }
6927         }
6928       /* The next token should be either a `,' or a `;'.  */
6929       token = cp_lexer_peek_token (parser->lexer);
6930       /* If it's a `,', there are more declarators to come.  */
6931       if (token->type == CPP_COMMA)
6932         cp_lexer_consume_token (parser->lexer);
6933       /* If it's a `;', we are done.  */
6934       else if (token->type == CPP_SEMICOLON)
6935         break;
6936       /* Anything else is an error.  */
6937       else
6938         {
6939           /* If we have already issued an error message we don't need
6940              to issue another one.  */
6941           if (decl != error_mark_node
6942               || (cp_parser_parsing_tentatively (parser)
6943                   && !cp_parser_committed_to_tentative_parse (parser)))
6944             cp_parser_error (parser, "expected %<,%> or %<;%>");
6945           /* Skip tokens until we reach the end of the statement.  */
6946           cp_parser_skip_to_end_of_statement (parser);
6947           /* If the next token is now a `;', consume it.  */
6948           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6949             cp_lexer_consume_token (parser->lexer);
6950           goto done;
6951         }
6952       /* After the first time around, a function-definition is not
6953          allowed -- even if it was OK at first.  For example:
6954
6955            int i, f() {}
6956
6957          is not valid.  */
6958       function_definition_allowed_p = false;
6959     }
6960
6961   /* Issue an error message if no declarators are present, and the
6962      decl-specifier-seq does not itself declare a class or
6963      enumeration.  */
6964   if (!saw_declarator)
6965     {
6966       if (cp_parser_declares_only_class_p (parser))
6967         shadow_tag (&decl_specifiers);
6968       /* Perform any deferred access checks.  */
6969       perform_deferred_access_checks ();
6970     }
6971
6972   /* Consume the `;'.  */
6973   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6974
6975  done:
6976   pop_deferring_access_checks ();
6977 }
6978
6979 /* Parse a decl-specifier-seq.
6980
6981    decl-specifier-seq:
6982      decl-specifier-seq [opt] decl-specifier
6983
6984    decl-specifier:
6985      storage-class-specifier
6986      type-specifier
6987      function-specifier
6988      friend
6989      typedef
6990
6991    GNU Extension:
6992
6993    decl-specifier:
6994      attributes
6995
6996    Set *DECL_SPECS to a representation of the decl-specifier-seq.
6997
6998    The parser flags FLAGS is used to control type-specifier parsing.
6999
7000    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7001    flags:
7002
7003      1: one of the decl-specifiers is an elaborated-type-specifier
7004         (i.e., a type declaration)
7005      2: one of the decl-specifiers is an enum-specifier or a
7006         class-specifier (i.e., a type definition)
7007
7008    */
7009
7010 static void
7011 cp_parser_decl_specifier_seq (cp_parser* parser,
7012                               cp_parser_flags flags,
7013                               cp_decl_specifier_seq *decl_specs,
7014                               int* declares_class_or_enum)
7015 {
7016   bool constructor_possible_p = !parser->in_declarator_p;
7017
7018   /* Clear DECL_SPECS.  */
7019   clear_decl_specs (decl_specs);
7020
7021   /* Assume no class or enumeration type is declared.  */
7022   *declares_class_or_enum = 0;
7023
7024   /* Keep reading specifiers until there are no more to read.  */
7025   while (true)
7026     {
7027       bool constructor_p;
7028       bool found_decl_spec;
7029       cp_token *token;
7030
7031       /* Peek at the next token.  */
7032       token = cp_lexer_peek_token (parser->lexer);
7033       /* Handle attributes.  */
7034       if (token->keyword == RID_ATTRIBUTE)
7035         {
7036           /* Parse the attributes.  */
7037           decl_specs->attributes
7038             = chainon (decl_specs->attributes,
7039                        cp_parser_attributes_opt (parser));
7040           continue;
7041         }
7042       /* Assume we will find a decl-specifier keyword.  */
7043       found_decl_spec = true;
7044       /* If the next token is an appropriate keyword, we can simply
7045          add it to the list.  */
7046       switch (token->keyword)
7047         {
7048           /* decl-specifier:
7049                friend  */
7050         case RID_FRIEND:
7051           if (decl_specs->specs[(int) ds_friend]++)
7052             error ("duplicate %<friend%>");
7053           /* Consume the token.  */
7054           cp_lexer_consume_token (parser->lexer);
7055           break;
7056
7057           /* function-specifier:
7058                inline
7059                virtual
7060                explicit  */
7061         case RID_INLINE:
7062         case RID_VIRTUAL:
7063         case RID_EXPLICIT:
7064           cp_parser_function_specifier_opt (parser, decl_specs);
7065           break;
7066
7067           /* decl-specifier:
7068                typedef  */
7069         case RID_TYPEDEF:
7070           ++decl_specs->specs[(int) ds_typedef];
7071           /* Consume the token.  */
7072           cp_lexer_consume_token (parser->lexer);
7073           /* A constructor declarator cannot appear in a typedef.  */
7074           constructor_possible_p = false;
7075           /* The "typedef" keyword can only occur in a declaration; we
7076              may as well commit at this point.  */
7077           cp_parser_commit_to_tentative_parse (parser);
7078           break;
7079
7080           /* storage-class-specifier:
7081                auto
7082                register
7083                static
7084                extern
7085                mutable
7086
7087              GNU Extension:
7088                thread  */
7089         case RID_AUTO:
7090           /* Consume the token.  */
7091           cp_lexer_consume_token (parser->lexer);
7092           cp_parser_set_storage_class (decl_specs, sc_auto);
7093           break;
7094         case RID_REGISTER:
7095           /* Consume the token.  */
7096           cp_lexer_consume_token (parser->lexer);
7097           cp_parser_set_storage_class (decl_specs, sc_register);
7098           break;
7099         case RID_STATIC:
7100           /* Consume the token.  */
7101           cp_lexer_consume_token (parser->lexer);
7102           if (decl_specs->specs[(int) ds_thread])
7103             {
7104               error ("%<__thread%> before %<static%>");
7105               decl_specs->specs[(int) ds_thread] = 0;
7106             }
7107           cp_parser_set_storage_class (decl_specs, sc_static);
7108           break;
7109         case RID_EXTERN:
7110           /* Consume the token.  */
7111           cp_lexer_consume_token (parser->lexer);
7112           if (decl_specs->specs[(int) ds_thread])
7113             {
7114               error ("%<__thread%> before %<extern%>");
7115               decl_specs->specs[(int) ds_thread] = 0;
7116             }
7117           cp_parser_set_storage_class (decl_specs, sc_extern);
7118           break;
7119         case RID_MUTABLE:
7120           /* Consume the token.  */
7121           cp_lexer_consume_token (parser->lexer);
7122           cp_parser_set_storage_class (decl_specs, sc_mutable);
7123           break;
7124         case RID_THREAD:
7125           /* Consume the token.  */
7126           cp_lexer_consume_token (parser->lexer);
7127           ++decl_specs->specs[(int) ds_thread];
7128           break;
7129
7130         default:
7131           /* We did not yet find a decl-specifier yet.  */
7132           found_decl_spec = false;
7133           break;
7134         }
7135
7136       /* Constructors are a special case.  The `S' in `S()' is not a
7137          decl-specifier; it is the beginning of the declarator.  */
7138       constructor_p
7139         = (!found_decl_spec
7140            && constructor_possible_p
7141            && (cp_parser_constructor_declarator_p
7142                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7143
7144       /* If we don't have a DECL_SPEC yet, then we must be looking at
7145          a type-specifier.  */
7146       if (!found_decl_spec && !constructor_p)
7147         {
7148           int decl_spec_declares_class_or_enum;
7149           bool is_cv_qualifier;
7150           tree type_spec;
7151
7152           type_spec
7153             = cp_parser_type_specifier (parser, flags,
7154                                         decl_specs,
7155                                         /*is_declaration=*/true,
7156                                         &decl_spec_declares_class_or_enum,
7157                                         &is_cv_qualifier);
7158
7159           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7160
7161           /* If this type-specifier referenced a user-defined type
7162              (a typedef, class-name, etc.), then we can't allow any
7163              more such type-specifiers henceforth.
7164
7165              [dcl.spec]
7166
7167              The longest sequence of decl-specifiers that could
7168              possibly be a type name is taken as the
7169              decl-specifier-seq of a declaration.  The sequence shall
7170              be self-consistent as described below.
7171
7172              [dcl.type]
7173
7174              As a general rule, at most one type-specifier is allowed
7175              in the complete decl-specifier-seq of a declaration.  The
7176              only exceptions are the following:
7177
7178              -- const or volatile can be combined with any other
7179                 type-specifier.
7180
7181              -- signed or unsigned can be combined with char, long,
7182                 short, or int.
7183
7184              -- ..
7185
7186              Example:
7187
7188                typedef char* Pc;
7189                void g (const int Pc);
7190
7191              Here, Pc is *not* part of the decl-specifier seq; it's
7192              the declarator.  Therefore, once we see a type-specifier
7193              (other than a cv-qualifier), we forbid any additional
7194              user-defined types.  We *do* still allow things like `int
7195              int' to be considered a decl-specifier-seq, and issue the
7196              error message later.  */
7197           if (type_spec && !is_cv_qualifier)
7198             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7199           /* A constructor declarator cannot follow a type-specifier.  */
7200           if (type_spec)
7201             {
7202               constructor_possible_p = false;
7203               found_decl_spec = true;
7204             }
7205         }
7206
7207       /* If we still do not have a DECL_SPEC, then there are no more
7208          decl-specifiers.  */
7209       if (!found_decl_spec)
7210         break;
7211
7212       decl_specs->any_specifiers_p = true;
7213       /* After we see one decl-specifier, further decl-specifiers are
7214          always optional.  */
7215       flags |= CP_PARSER_FLAGS_OPTIONAL;
7216     }
7217
7218   /* Don't allow a friend specifier with a class definition.  */
7219   if (decl_specs->specs[(int) ds_friend] != 0
7220       && (*declares_class_or_enum & 2))
7221     error ("class definition may not be declared a friend");
7222 }
7223
7224 /* Parse an (optional) storage-class-specifier.
7225
7226    storage-class-specifier:
7227      auto
7228      register
7229      static
7230      extern
7231      mutable
7232
7233    GNU Extension:
7234
7235    storage-class-specifier:
7236      thread
7237
7238    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7239
7240 static tree
7241 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7242 {
7243   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7244     {
7245     case RID_AUTO:
7246     case RID_REGISTER:
7247     case RID_STATIC:
7248     case RID_EXTERN:
7249     case RID_MUTABLE:
7250     case RID_THREAD:
7251       /* Consume the token.  */
7252       return cp_lexer_consume_token (parser->lexer)->value;
7253
7254     default:
7255       return NULL_TREE;
7256     }
7257 }
7258
7259 /* Parse an (optional) function-specifier.
7260
7261    function-specifier:
7262      inline
7263      virtual
7264      explicit
7265
7266    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7267    Updates DECL_SPECS, if it is non-NULL.  */
7268
7269 static tree
7270 cp_parser_function_specifier_opt (cp_parser* parser,
7271                                   cp_decl_specifier_seq *decl_specs)
7272 {
7273   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7274     {
7275     case RID_INLINE:
7276       if (decl_specs)
7277         ++decl_specs->specs[(int) ds_inline];
7278       break;
7279
7280     case RID_VIRTUAL:
7281       if (decl_specs)
7282         ++decl_specs->specs[(int) ds_virtual];
7283       break;
7284
7285     case RID_EXPLICIT:
7286       if (decl_specs)
7287         ++decl_specs->specs[(int) ds_explicit];
7288       break;
7289
7290     default:
7291       return NULL_TREE;
7292     }
7293
7294   /* Consume the token.  */
7295   return cp_lexer_consume_token (parser->lexer)->value;
7296 }
7297
7298 /* Parse a linkage-specification.
7299
7300    linkage-specification:
7301      extern string-literal { declaration-seq [opt] }
7302      extern string-literal declaration  */
7303
7304 static void
7305 cp_parser_linkage_specification (cp_parser* parser)
7306 {
7307   tree linkage;
7308
7309   /* Look for the `extern' keyword.  */
7310   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7311
7312   /* Look for the string-literal.  */
7313   linkage = cp_parser_string_literal (parser, false, false);
7314
7315   /* Transform the literal into an identifier.  If the literal is a
7316      wide-character string, or contains embedded NULs, then we can't
7317      handle it as the user wants.  */
7318   if (strlen (TREE_STRING_POINTER (linkage))
7319       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7320     {
7321       cp_parser_error (parser, "invalid linkage-specification");
7322       /* Assume C++ linkage.  */
7323       linkage = lang_name_cplusplus;
7324     }
7325   else
7326     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7327
7328   /* We're now using the new linkage.  */
7329   push_lang_context (linkage);
7330
7331   /* If the next token is a `{', then we're using the first
7332      production.  */
7333   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7334     {
7335       /* Consume the `{' token.  */
7336       cp_lexer_consume_token (parser->lexer);
7337       /* Parse the declarations.  */
7338       cp_parser_declaration_seq_opt (parser);
7339       /* Look for the closing `}'.  */
7340       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7341     }
7342   /* Otherwise, there's just one declaration.  */
7343   else
7344     {
7345       bool saved_in_unbraced_linkage_specification_p;
7346
7347       saved_in_unbraced_linkage_specification_p
7348         = parser->in_unbraced_linkage_specification_p;
7349       parser->in_unbraced_linkage_specification_p = true;
7350       have_extern_spec = true;
7351       cp_parser_declaration (parser);
7352       have_extern_spec = false;
7353       parser->in_unbraced_linkage_specification_p
7354         = saved_in_unbraced_linkage_specification_p;
7355     }
7356
7357   /* We're done with the linkage-specification.  */
7358   pop_lang_context ();
7359 }
7360
7361 /* Special member functions [gram.special] */
7362
7363 /* Parse a conversion-function-id.
7364
7365    conversion-function-id:
7366      operator conversion-type-id
7367
7368    Returns an IDENTIFIER_NODE representing the operator.  */
7369
7370 static tree
7371 cp_parser_conversion_function_id (cp_parser* parser)
7372 {
7373   tree type;
7374   tree saved_scope;
7375   tree saved_qualifying_scope;
7376   tree saved_object_scope;
7377   bool pop_p = false;
7378
7379   /* Look for the `operator' token.  */
7380   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7381     return error_mark_node;
7382   /* When we parse the conversion-type-id, the current scope will be
7383      reset.  However, we need that information in able to look up the
7384      conversion function later, so we save it here.  */
7385   saved_scope = parser->scope;
7386   saved_qualifying_scope = parser->qualifying_scope;
7387   saved_object_scope = parser->object_scope;
7388   /* We must enter the scope of the class so that the names of
7389      entities declared within the class are available in the
7390      conversion-type-id.  For example, consider:
7391
7392        struct S {
7393          typedef int I;
7394          operator I();
7395        };
7396
7397        S::operator I() { ... }
7398
7399      In order to see that `I' is a type-name in the definition, we
7400      must be in the scope of `S'.  */
7401   if (saved_scope)
7402     pop_p = push_scope (saved_scope);
7403   /* Parse the conversion-type-id.  */
7404   type = cp_parser_conversion_type_id (parser);
7405   /* Leave the scope of the class, if any.  */
7406   if (pop_p)
7407     pop_scope (saved_scope);
7408   /* Restore the saved scope.  */
7409   parser->scope = saved_scope;
7410   parser->qualifying_scope = saved_qualifying_scope;
7411   parser->object_scope = saved_object_scope;
7412   /* If the TYPE is invalid, indicate failure.  */
7413   if (type == error_mark_node)
7414     return error_mark_node;
7415   return mangle_conv_op_name_for_type (type);
7416 }
7417
7418 /* Parse a conversion-type-id:
7419
7420    conversion-type-id:
7421      type-specifier-seq conversion-declarator [opt]
7422
7423    Returns the TYPE specified.  */
7424
7425 static tree
7426 cp_parser_conversion_type_id (cp_parser* parser)
7427 {
7428   tree attributes;
7429   cp_decl_specifier_seq type_specifiers;
7430   cp_declarator *declarator;
7431   tree type_specified;
7432
7433   /* Parse the attributes.  */
7434   attributes = cp_parser_attributes_opt (parser);
7435   /* Parse the type-specifiers.  */
7436   cp_parser_type_specifier_seq (parser, &type_specifiers);
7437   /* If that didn't work, stop.  */
7438   if (type_specifiers.type == error_mark_node)
7439     return error_mark_node;
7440   /* Parse the conversion-declarator.  */
7441   declarator = cp_parser_conversion_declarator_opt (parser);
7442
7443   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7444                                     /*initialized=*/0, &attributes);
7445   if (attributes)
7446     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7447   return type_specified;
7448 }
7449
7450 /* Parse an (optional) conversion-declarator.
7451
7452    conversion-declarator:
7453      ptr-operator conversion-declarator [opt]
7454
7455    */
7456
7457 static cp_declarator *
7458 cp_parser_conversion_declarator_opt (cp_parser* parser)
7459 {
7460   enum tree_code code;
7461   tree class_type;
7462   cp_cv_quals cv_quals;
7463
7464   /* We don't know if there's a ptr-operator next, or not.  */
7465   cp_parser_parse_tentatively (parser);
7466   /* Try the ptr-operator.  */
7467   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7468   /* If it worked, look for more conversion-declarators.  */
7469   if (cp_parser_parse_definitely (parser))
7470     {
7471       cp_declarator *declarator;
7472
7473       /* Parse another optional declarator.  */
7474       declarator = cp_parser_conversion_declarator_opt (parser);
7475
7476       /* Create the representation of the declarator.  */
7477       if (class_type)
7478         declarator = make_ptrmem_declarator (cv_quals, class_type,
7479                                              declarator);
7480       else if (code == INDIRECT_REF)
7481         declarator = make_pointer_declarator (cv_quals, declarator);
7482       else
7483         declarator = make_reference_declarator (cv_quals, declarator);
7484
7485       return declarator;
7486    }
7487
7488   return NULL;
7489 }
7490
7491 /* Parse an (optional) ctor-initializer.
7492
7493    ctor-initializer:
7494      : mem-initializer-list
7495
7496    Returns TRUE iff the ctor-initializer was actually present.  */
7497
7498 static bool
7499 cp_parser_ctor_initializer_opt (cp_parser* parser)
7500 {
7501   /* If the next token is not a `:', then there is no
7502      ctor-initializer.  */
7503   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7504     {
7505       /* Do default initialization of any bases and members.  */
7506       if (DECL_CONSTRUCTOR_P (current_function_decl))
7507         finish_mem_initializers (NULL_TREE);
7508
7509       return false;
7510     }
7511
7512   /* Consume the `:' token.  */
7513   cp_lexer_consume_token (parser->lexer);
7514   /* And the mem-initializer-list.  */
7515   cp_parser_mem_initializer_list (parser);
7516
7517   return true;
7518 }
7519
7520 /* Parse a mem-initializer-list.
7521
7522    mem-initializer-list:
7523      mem-initializer
7524      mem-initializer , mem-initializer-list  */
7525
7526 static void
7527 cp_parser_mem_initializer_list (cp_parser* parser)
7528 {
7529   tree mem_initializer_list = NULL_TREE;
7530
7531   /* Let the semantic analysis code know that we are starting the
7532      mem-initializer-list.  */
7533   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7534     error ("only constructors take base initializers");
7535
7536   /* Loop through the list.  */
7537   while (true)
7538     {
7539       tree mem_initializer;
7540
7541       /* Parse the mem-initializer.  */
7542       mem_initializer = cp_parser_mem_initializer (parser);
7543       /* Add it to the list, unless it was erroneous.  */
7544       if (mem_initializer)
7545         {
7546           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7547           mem_initializer_list = mem_initializer;
7548         }
7549       /* If the next token is not a `,', we're done.  */
7550       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7551         break;
7552       /* Consume the `,' token.  */
7553       cp_lexer_consume_token (parser->lexer);
7554     }
7555
7556   /* Perform semantic analysis.  */
7557   if (DECL_CONSTRUCTOR_P (current_function_decl))
7558     finish_mem_initializers (mem_initializer_list);
7559 }
7560
7561 /* Parse a mem-initializer.
7562
7563    mem-initializer:
7564      mem-initializer-id ( expression-list [opt] )
7565
7566    GNU extension:
7567
7568    mem-initializer:
7569      ( expression-list [opt] )
7570
7571    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7572    class) or FIELD_DECL (for a non-static data member) to initialize;
7573    the TREE_VALUE is the expression-list.  */
7574
7575 static tree
7576 cp_parser_mem_initializer (cp_parser* parser)
7577 {
7578   tree mem_initializer_id;
7579   tree expression_list;
7580   tree member;
7581
7582   /* Find out what is being initialized.  */
7583   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7584     {
7585       pedwarn ("anachronistic old-style base class initializer");
7586       mem_initializer_id = NULL_TREE;
7587     }
7588   else
7589     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7590   member = expand_member_init (mem_initializer_id);
7591   if (member && !DECL_P (member))
7592     in_base_initializer = 1;
7593
7594   expression_list
7595     = cp_parser_parenthesized_expression_list (parser, false,
7596                                                /*non_constant_p=*/NULL);
7597   if (!expression_list)
7598     expression_list = void_type_node;
7599
7600   in_base_initializer = 0;
7601
7602   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7603 }
7604
7605 /* Parse a mem-initializer-id.
7606
7607    mem-initializer-id:
7608      :: [opt] nested-name-specifier [opt] class-name
7609      identifier
7610
7611    Returns a TYPE indicating the class to be initializer for the first
7612    production.  Returns an IDENTIFIER_NODE indicating the data member
7613    to be initialized for the second production.  */
7614
7615 static tree
7616 cp_parser_mem_initializer_id (cp_parser* parser)
7617 {
7618   bool global_scope_p;
7619   bool nested_name_specifier_p;
7620   bool template_p = false;
7621   tree id;
7622
7623   /* `typename' is not allowed in this context ([temp.res]).  */
7624   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7625     {
7626       error ("keyword %<typename%> not allowed in this context (a qualified "
7627              "member initializer is implicitly a type)");
7628       cp_lexer_consume_token (parser->lexer);
7629     }
7630   /* Look for the optional `::' operator.  */
7631   global_scope_p
7632     = (cp_parser_global_scope_opt (parser,
7633                                    /*current_scope_valid_p=*/false)
7634        != NULL_TREE);
7635   /* Look for the optional nested-name-specifier.  The simplest way to
7636      implement:
7637
7638        [temp.res]
7639
7640        The keyword `typename' is not permitted in a base-specifier or
7641        mem-initializer; in these contexts a qualified name that
7642        depends on a template-parameter is implicitly assumed to be a
7643        type name.
7644
7645      is to assume that we have seen the `typename' keyword at this
7646      point.  */
7647   nested_name_specifier_p
7648     = (cp_parser_nested_name_specifier_opt (parser,
7649                                             /*typename_keyword_p=*/true,
7650                                             /*check_dependency_p=*/true,
7651                                             /*type_p=*/true,
7652                                             /*is_declaration=*/true)
7653        != NULL_TREE);
7654   if (nested_name_specifier_p)
7655     template_p = cp_parser_optional_template_keyword (parser);
7656   /* If there is a `::' operator or a nested-name-specifier, then we
7657      are definitely looking for a class-name.  */
7658   if (global_scope_p || nested_name_specifier_p)
7659     return cp_parser_class_name (parser,
7660                                  /*typename_keyword_p=*/true,
7661                                  /*template_keyword_p=*/template_p,
7662                                  /*type_p=*/false,
7663                                  /*check_dependency_p=*/true,
7664                                  /*class_head_p=*/false,
7665                                  /*is_declaration=*/true);
7666   /* Otherwise, we could also be looking for an ordinary identifier.  */
7667   cp_parser_parse_tentatively (parser);
7668   /* Try a class-name.  */
7669   id = cp_parser_class_name (parser,
7670                              /*typename_keyword_p=*/true,
7671                              /*template_keyword_p=*/false,
7672                              /*type_p=*/false,
7673                              /*check_dependency_p=*/true,
7674                              /*class_head_p=*/false,
7675                              /*is_declaration=*/true);
7676   /* If we found one, we're done.  */
7677   if (cp_parser_parse_definitely (parser))
7678     return id;
7679   /* Otherwise, look for an ordinary identifier.  */
7680   return cp_parser_identifier (parser);
7681 }
7682
7683 /* Overloading [gram.over] */
7684
7685 /* Parse an operator-function-id.
7686
7687    operator-function-id:
7688      operator operator
7689
7690    Returns an IDENTIFIER_NODE for the operator which is a
7691    human-readable spelling of the identifier, e.g., `operator +'.  */
7692
7693 static tree
7694 cp_parser_operator_function_id (cp_parser* parser)
7695 {
7696   /* Look for the `operator' keyword.  */
7697   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7698     return error_mark_node;
7699   /* And then the name of the operator itself.  */
7700   return cp_parser_operator (parser);
7701 }
7702
7703 /* Parse an operator.
7704
7705    operator:
7706      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7707      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7708      || ++ -- , ->* -> () []
7709
7710    GNU Extensions:
7711
7712    operator:
7713      <? >? <?= >?=
7714
7715    Returns an IDENTIFIER_NODE for the operator which is a
7716    human-readable spelling of the identifier, e.g., `operator +'.  */
7717
7718 static tree
7719 cp_parser_operator (cp_parser* parser)
7720 {
7721   tree id = NULL_TREE;
7722   cp_token *token;
7723
7724   /* Peek at the next token.  */
7725   token = cp_lexer_peek_token (parser->lexer);
7726   /* Figure out which operator we have.  */
7727   switch (token->type)
7728     {
7729     case CPP_KEYWORD:
7730       {
7731         enum tree_code op;
7732
7733         /* The keyword should be either `new' or `delete'.  */
7734         if (token->keyword == RID_NEW)
7735           op = NEW_EXPR;
7736         else if (token->keyword == RID_DELETE)
7737           op = DELETE_EXPR;
7738         else
7739           break;
7740
7741         /* Consume the `new' or `delete' token.  */
7742         cp_lexer_consume_token (parser->lexer);
7743
7744         /* Peek at the next token.  */
7745         token = cp_lexer_peek_token (parser->lexer);
7746         /* If it's a `[' token then this is the array variant of the
7747            operator.  */
7748         if (token->type == CPP_OPEN_SQUARE)
7749           {
7750             /* Consume the `[' token.  */
7751             cp_lexer_consume_token (parser->lexer);
7752             /* Look for the `]' token.  */
7753             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7754             id = ansi_opname (op == NEW_EXPR
7755                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7756           }
7757         /* Otherwise, we have the non-array variant.  */
7758         else
7759           id = ansi_opname (op);
7760
7761         return id;
7762       }
7763
7764     case CPP_PLUS:
7765       id = ansi_opname (PLUS_EXPR);
7766       break;
7767
7768     case CPP_MINUS:
7769       id = ansi_opname (MINUS_EXPR);
7770       break;
7771
7772     case CPP_MULT:
7773       id = ansi_opname (MULT_EXPR);
7774       break;
7775
7776     case CPP_DIV:
7777       id = ansi_opname (TRUNC_DIV_EXPR);
7778       break;
7779
7780     case CPP_MOD:
7781       id = ansi_opname (TRUNC_MOD_EXPR);
7782       break;
7783
7784     case CPP_XOR:
7785       id = ansi_opname (BIT_XOR_EXPR);
7786       break;
7787
7788     case CPP_AND:
7789       id = ansi_opname (BIT_AND_EXPR);
7790       break;
7791
7792     case CPP_OR:
7793       id = ansi_opname (BIT_IOR_EXPR);
7794       break;
7795
7796     case CPP_COMPL:
7797       id = ansi_opname (BIT_NOT_EXPR);
7798       break;
7799
7800     case CPP_NOT:
7801       id = ansi_opname (TRUTH_NOT_EXPR);
7802       break;
7803
7804     case CPP_EQ:
7805       id = ansi_assopname (NOP_EXPR);
7806       break;
7807
7808     case CPP_LESS:
7809       id = ansi_opname (LT_EXPR);
7810       break;
7811
7812     case CPP_GREATER:
7813       id = ansi_opname (GT_EXPR);
7814       break;
7815
7816     case CPP_PLUS_EQ:
7817       id = ansi_assopname (PLUS_EXPR);
7818       break;
7819
7820     case CPP_MINUS_EQ:
7821       id = ansi_assopname (MINUS_EXPR);
7822       break;
7823
7824     case CPP_MULT_EQ:
7825       id = ansi_assopname (MULT_EXPR);
7826       break;
7827
7828     case CPP_DIV_EQ:
7829       id = ansi_assopname (TRUNC_DIV_EXPR);
7830       break;
7831
7832     case CPP_MOD_EQ:
7833       id = ansi_assopname (TRUNC_MOD_EXPR);
7834       break;
7835
7836     case CPP_XOR_EQ:
7837       id = ansi_assopname (BIT_XOR_EXPR);
7838       break;
7839
7840     case CPP_AND_EQ:
7841       id = ansi_assopname (BIT_AND_EXPR);
7842       break;
7843
7844     case CPP_OR_EQ:
7845       id = ansi_assopname (BIT_IOR_EXPR);
7846       break;
7847
7848     case CPP_LSHIFT:
7849       id = ansi_opname (LSHIFT_EXPR);
7850       break;
7851
7852     case CPP_RSHIFT:
7853       id = ansi_opname (RSHIFT_EXPR);
7854       break;
7855
7856     case CPP_LSHIFT_EQ:
7857       id = ansi_assopname (LSHIFT_EXPR);
7858       break;
7859
7860     case CPP_RSHIFT_EQ:
7861       id = ansi_assopname (RSHIFT_EXPR);
7862       break;
7863
7864     case CPP_EQ_EQ:
7865       id = ansi_opname (EQ_EXPR);
7866       break;
7867
7868     case CPP_NOT_EQ:
7869       id = ansi_opname (NE_EXPR);
7870       break;
7871
7872     case CPP_LESS_EQ:
7873       id = ansi_opname (LE_EXPR);
7874       break;
7875
7876     case CPP_GREATER_EQ:
7877       id = ansi_opname (GE_EXPR);
7878       break;
7879
7880     case CPP_AND_AND:
7881       id = ansi_opname (TRUTH_ANDIF_EXPR);
7882       break;
7883
7884     case CPP_OR_OR:
7885       id = ansi_opname (TRUTH_ORIF_EXPR);
7886       break;
7887
7888     case CPP_PLUS_PLUS:
7889       id = ansi_opname (POSTINCREMENT_EXPR);
7890       break;
7891
7892     case CPP_MINUS_MINUS:
7893       id = ansi_opname (PREDECREMENT_EXPR);
7894       break;
7895
7896     case CPP_COMMA:
7897       id = ansi_opname (COMPOUND_EXPR);
7898       break;
7899
7900     case CPP_DEREF_STAR:
7901       id = ansi_opname (MEMBER_REF);
7902       break;
7903
7904     case CPP_DEREF:
7905       id = ansi_opname (COMPONENT_REF);
7906       break;
7907
7908     case CPP_OPEN_PAREN:
7909       /* Consume the `('.  */
7910       cp_lexer_consume_token (parser->lexer);
7911       /* Look for the matching `)'.  */
7912       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7913       return ansi_opname (CALL_EXPR);
7914
7915     case CPP_OPEN_SQUARE:
7916       /* Consume the `['.  */
7917       cp_lexer_consume_token (parser->lexer);
7918       /* Look for the matching `]'.  */
7919       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7920       return ansi_opname (ARRAY_REF);
7921
7922       /* Extensions.  */
7923     case CPP_MIN:
7924       id = ansi_opname (MIN_EXPR);
7925       break;
7926
7927     case CPP_MAX:
7928       id = ansi_opname (MAX_EXPR);
7929       break;
7930
7931     case CPP_MIN_EQ:
7932       id = ansi_assopname (MIN_EXPR);
7933       break;
7934
7935     case CPP_MAX_EQ:
7936       id = ansi_assopname (MAX_EXPR);
7937       break;
7938
7939     default:
7940       /* Anything else is an error.  */
7941       break;
7942     }
7943
7944   /* If we have selected an identifier, we need to consume the
7945      operator token.  */
7946   if (id)
7947     cp_lexer_consume_token (parser->lexer);
7948   /* Otherwise, no valid operator name was present.  */
7949   else
7950     {
7951       cp_parser_error (parser, "expected operator");
7952       id = error_mark_node;
7953     }
7954
7955   return id;
7956 }
7957
7958 /* Parse a template-declaration.
7959
7960    template-declaration:
7961      export [opt] template < template-parameter-list > declaration
7962
7963    If MEMBER_P is TRUE, this template-declaration occurs within a
7964    class-specifier.
7965
7966    The grammar rule given by the standard isn't correct.  What
7967    is really meant is:
7968
7969    template-declaration:
7970      export [opt] template-parameter-list-seq
7971        decl-specifier-seq [opt] init-declarator [opt] ;
7972      export [opt] template-parameter-list-seq
7973        function-definition
7974
7975    template-parameter-list-seq:
7976      template-parameter-list-seq [opt]
7977      template < template-parameter-list >  */
7978
7979 static void
7980 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7981 {
7982   /* Check for `export'.  */
7983   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7984     {
7985       /* Consume the `export' token.  */
7986       cp_lexer_consume_token (parser->lexer);
7987       /* Warn that we do not support `export'.  */
7988       warning ("keyword %<export%> not implemented, and will be ignored");
7989     }
7990
7991   cp_parser_template_declaration_after_export (parser, member_p);
7992 }
7993
7994 /* Parse a template-parameter-list.
7995
7996    template-parameter-list:
7997      template-parameter
7998      template-parameter-list , template-parameter
7999
8000    Returns a TREE_LIST.  Each node represents a template parameter.
8001    The nodes are connected via their TREE_CHAINs.  */
8002
8003 static tree
8004 cp_parser_template_parameter_list (cp_parser* parser)
8005 {
8006   tree parameter_list = NULL_TREE;
8007
8008   while (true)
8009     {
8010       tree parameter;
8011       cp_token *token;
8012       bool is_non_type;
8013
8014       /* Parse the template-parameter.  */
8015       parameter = cp_parser_template_parameter (parser, &is_non_type);
8016       /* Add it to the list.  */
8017       parameter_list = process_template_parm (parameter_list,
8018                                               parameter,
8019                                               is_non_type);
8020       /* Peek at the next token.  */
8021       token = cp_lexer_peek_token (parser->lexer);
8022       /* If it's not a `,', we're done.  */
8023       if (token->type != CPP_COMMA)
8024         break;
8025       /* Otherwise, consume the `,' token.  */
8026       cp_lexer_consume_token (parser->lexer);
8027     }
8028
8029   return parameter_list;
8030 }
8031
8032 /* Parse a template-parameter.
8033
8034    template-parameter:
8035      type-parameter
8036      parameter-declaration
8037
8038    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
8039    TREE_PURPOSE is the default value, if any.  *IS_NON_TYPE is set to
8040    true iff this parameter is a non-type parameter.  */
8041
8042 static tree
8043 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8044 {
8045   cp_token *token;
8046   cp_parameter_declarator *parameter_declarator;
8047
8048   /* Assume it is a type parameter or a template parameter.  */
8049   *is_non_type = false;
8050   /* Peek at the next token.  */
8051   token = cp_lexer_peek_token (parser->lexer);
8052   /* If it is `class' or `template', we have a type-parameter.  */
8053   if (token->keyword == RID_TEMPLATE)
8054     return cp_parser_type_parameter (parser);
8055   /* If it is `class' or `typename' we do not know yet whether it is a
8056      type parameter or a non-type parameter.  Consider:
8057
8058        template <typename T, typename T::X X> ...
8059
8060      or:
8061
8062        template <class C, class D*> ...
8063
8064      Here, the first parameter is a type parameter, and the second is
8065      a non-type parameter.  We can tell by looking at the token after
8066      the identifier -- if it is a `,', `=', or `>' then we have a type
8067      parameter.  */
8068   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8069     {
8070       /* Peek at the token after `class' or `typename'.  */
8071       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8072       /* If it's an identifier, skip it.  */
8073       if (token->type == CPP_NAME)
8074         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8075       /* Now, see if the token looks like the end of a template
8076          parameter.  */
8077       if (token->type == CPP_COMMA
8078           || token->type == CPP_EQ
8079           || token->type == CPP_GREATER)
8080         return cp_parser_type_parameter (parser);
8081     }
8082
8083   /* Otherwise, it is a non-type parameter.
8084
8085      [temp.param]
8086
8087      When parsing a default template-argument for a non-type
8088      template-parameter, the first non-nested `>' is taken as the end
8089      of the template parameter-list rather than a greater-than
8090      operator.  */
8091   *is_non_type = true;
8092   parameter_declarator
8093      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8094                                         /*parenthesized_p=*/NULL);
8095   return (build_tree_list
8096           (parameter_declarator->default_argument,
8097            grokdeclarator (parameter_declarator->declarator,
8098                            &parameter_declarator->decl_specifiers,
8099                            PARM, /*initialized=*/0,
8100                            /*attrlist=*/NULL)));
8101 }
8102
8103 /* Parse a type-parameter.
8104
8105    type-parameter:
8106      class identifier [opt]
8107      class identifier [opt] = type-id
8108      typename identifier [opt]
8109      typename identifier [opt] = type-id
8110      template < template-parameter-list > class identifier [opt]
8111      template < template-parameter-list > class identifier [opt]
8112        = id-expression
8113
8114    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8115    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8116    the declaration of the parameter.  */
8117
8118 static tree
8119 cp_parser_type_parameter (cp_parser* parser)
8120 {
8121   cp_token *token;
8122   tree parameter;
8123
8124   /* Look for a keyword to tell us what kind of parameter this is.  */
8125   token = cp_parser_require (parser, CPP_KEYWORD,
8126                              "`class', `typename', or `template'");
8127   if (!token)
8128     return error_mark_node;
8129
8130   switch (token->keyword)
8131     {
8132     case RID_CLASS:
8133     case RID_TYPENAME:
8134       {
8135         tree identifier;
8136         tree default_argument;
8137
8138         /* If the next token is an identifier, then it names the
8139            parameter.  */
8140         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8141           identifier = cp_parser_identifier (parser);
8142         else
8143           identifier = NULL_TREE;
8144
8145         /* Create the parameter.  */
8146         parameter = finish_template_type_parm (class_type_node, identifier);
8147
8148         /* If the next token is an `=', we have a default argument.  */
8149         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8150           {
8151             /* Consume the `=' token.  */
8152             cp_lexer_consume_token (parser->lexer);
8153             /* Parse the default-argument.  */
8154             default_argument = cp_parser_type_id (parser);
8155           }
8156         else
8157           default_argument = NULL_TREE;
8158
8159         /* Create the combined representation of the parameter and the
8160            default argument.  */
8161         parameter = build_tree_list (default_argument, parameter);
8162       }
8163       break;
8164
8165     case RID_TEMPLATE:
8166       {
8167         tree parameter_list;
8168         tree identifier;
8169         tree default_argument;
8170
8171         /* Look for the `<'.  */
8172         cp_parser_require (parser, CPP_LESS, "`<'");
8173         /* Parse the template-parameter-list.  */
8174         begin_template_parm_list ();
8175         parameter_list
8176           = cp_parser_template_parameter_list (parser);
8177         parameter_list = end_template_parm_list (parameter_list);
8178         /* Look for the `>'.  */
8179         cp_parser_require (parser, CPP_GREATER, "`>'");
8180         /* Look for the `class' keyword.  */
8181         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8182         /* If the next token is an `=', then there is a
8183            default-argument.  If the next token is a `>', we are at
8184            the end of the parameter-list.  If the next token is a `,',
8185            then we are at the end of this parameter.  */
8186         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8187             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8188             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8189           {
8190             identifier = cp_parser_identifier (parser);
8191             /* Treat invalid names as if the parameter were nameless.  */
8192             if (identifier == error_mark_node)
8193               identifier = NULL_TREE;
8194           }
8195         else
8196           identifier = NULL_TREE;
8197
8198         /* Create the template parameter.  */
8199         parameter = finish_template_template_parm (class_type_node,
8200                                                    identifier);
8201
8202         /* If the next token is an `=', then there is a
8203            default-argument.  */
8204         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8205           {
8206             bool is_template;
8207
8208             /* Consume the `='.  */
8209             cp_lexer_consume_token (parser->lexer);
8210             /* Parse the id-expression.  */
8211             default_argument
8212               = cp_parser_id_expression (parser,
8213                                          /*template_keyword_p=*/false,
8214                                          /*check_dependency_p=*/true,
8215                                          /*template_p=*/&is_template,
8216                                          /*declarator_p=*/false);
8217             if (TREE_CODE (default_argument) == TYPE_DECL)
8218               /* If the id-expression was a template-id that refers to
8219                  a template-class, we already have the declaration here,
8220                  so no further lookup is needed.  */
8221                  ;
8222             else
8223               /* Look up the name.  */
8224               default_argument
8225                 = cp_parser_lookup_name (parser, default_argument,
8226                                         /*is_type=*/false,
8227                                         /*is_template=*/is_template,
8228                                         /*is_namespace=*/false,
8229                                         /*check_dependency=*/true,
8230                                         /*ambiguous_p=*/NULL);
8231             /* See if the default argument is valid.  */
8232             default_argument
8233               = check_template_template_default_arg (default_argument);
8234           }
8235         else
8236           default_argument = NULL_TREE;
8237
8238         /* Create the combined representation of the parameter and the
8239            default argument.  */
8240         parameter = build_tree_list (default_argument, parameter);
8241       }
8242       break;
8243
8244     default:
8245       gcc_unreachable ();
8246       break;
8247     }
8248
8249   return parameter;
8250 }
8251
8252 /* Parse a template-id.
8253
8254    template-id:
8255      template-name < template-argument-list [opt] >
8256
8257    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8258    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8259    returned.  Otherwise, if the template-name names a function, or set
8260    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8261    names a class, returns a TYPE_DECL for the specialization.
8262
8263    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8264    uninstantiated templates.  */
8265
8266 static tree
8267 cp_parser_template_id (cp_parser *parser,
8268                        bool template_keyword_p,
8269                        bool check_dependency_p,
8270                        bool is_declaration)
8271 {
8272   tree template;
8273   tree arguments;
8274   tree template_id;
8275   cp_token_position start_of_id = 0;
8276   tree access_check = NULL_TREE;
8277   cp_token *next_token, *next_token_2;
8278   bool is_identifier;
8279
8280   /* If the next token corresponds to a template-id, there is no need
8281      to reparse it.  */
8282   next_token = cp_lexer_peek_token (parser->lexer);
8283   if (next_token->type == CPP_TEMPLATE_ID)
8284     {
8285       tree value;
8286       tree check;
8287
8288       /* Get the stored value.  */
8289       value = cp_lexer_consume_token (parser->lexer)->value;
8290       /* Perform any access checks that were deferred.  */
8291       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8292         perform_or_defer_access_check (TREE_PURPOSE (check),
8293                                        TREE_VALUE (check));
8294       /* Return the stored value.  */
8295       return TREE_VALUE (value);
8296     }
8297
8298   /* Avoid performing name lookup if there is no possibility of
8299      finding a template-id.  */
8300   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8301       || (next_token->type == CPP_NAME
8302           && !cp_parser_nth_token_starts_template_argument_list_p
8303                (parser, 2)))
8304     {
8305       cp_parser_error (parser, "expected template-id");
8306       return error_mark_node;
8307     }
8308
8309   /* Remember where the template-id starts.  */
8310   if (cp_parser_parsing_tentatively (parser)
8311       && !cp_parser_committed_to_tentative_parse (parser))
8312     start_of_id = cp_lexer_token_position (parser->lexer, false);
8313
8314   push_deferring_access_checks (dk_deferred);
8315
8316   /* Parse the template-name.  */
8317   is_identifier = false;
8318   template = cp_parser_template_name (parser, template_keyword_p,
8319                                       check_dependency_p,
8320                                       is_declaration,
8321                                       &is_identifier);
8322   if (template == error_mark_node || is_identifier)
8323     {
8324       pop_deferring_access_checks ();
8325       return template;
8326     }
8327
8328   /* If we find the sequence `[:' after a template-name, it's probably
8329      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8330      parse correctly the argument list.  */
8331   next_token = cp_lexer_peek_token (parser->lexer);
8332   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8333   if (next_token->type == CPP_OPEN_SQUARE
8334       && next_token->flags & DIGRAPH
8335       && next_token_2->type == CPP_COLON
8336       && !(next_token_2->flags & PREV_WHITE))
8337     {
8338       cp_parser_parse_tentatively (parser);
8339       /* Change `:' into `::'.  */
8340       next_token_2->type = CPP_SCOPE;
8341       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8342          CPP_LESS.  */
8343       cp_lexer_consume_token (parser->lexer);
8344       /* Parse the arguments.  */
8345       arguments = cp_parser_enclosed_template_argument_list (parser);
8346       if (!cp_parser_parse_definitely (parser))
8347         {
8348           /* If we couldn't parse an argument list, then we revert our changes
8349              and return simply an error. Maybe this is not a template-id
8350              after all.  */
8351           next_token_2->type = CPP_COLON;
8352           cp_parser_error (parser, "expected %<<%>");
8353           pop_deferring_access_checks ();
8354           return error_mark_node;
8355         }
8356       /* Otherwise, emit an error about the invalid digraph, but continue
8357          parsing because we got our argument list.  */
8358       pedwarn ("%<<::%> cannot begin a template-argument list");
8359       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8360               "between %<<%> and %<::%>");
8361       if (!flag_permissive)
8362         {
8363           static bool hint;
8364           if (!hint)
8365             {
8366               inform ("(if you use -fpermissive G++ will accept your code)");
8367               hint = true;
8368             }
8369         }
8370     }
8371   else
8372     {
8373       /* Look for the `<' that starts the template-argument-list.  */
8374       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8375         {
8376           pop_deferring_access_checks ();
8377           return error_mark_node;
8378         }
8379       /* Parse the arguments.  */
8380       arguments = cp_parser_enclosed_template_argument_list (parser);
8381     }
8382
8383   /* Build a representation of the specialization.  */
8384   if (TREE_CODE (template) == IDENTIFIER_NODE)
8385     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8386   else if (DECL_CLASS_TEMPLATE_P (template)
8387            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8388     template_id
8389       = finish_template_type (template, arguments,
8390                               cp_lexer_next_token_is (parser->lexer,
8391                                                       CPP_SCOPE));
8392   else
8393     {
8394       /* If it's not a class-template or a template-template, it should be
8395          a function-template.  */
8396       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8397                    || TREE_CODE (template) == OVERLOAD
8398                    || BASELINK_P (template)));
8399
8400       template_id = lookup_template_function (template, arguments);
8401     }
8402
8403   /* Retrieve any deferred checks.  Do not pop this access checks yet
8404      so the memory will not be reclaimed during token replacing below.  */
8405   access_check = get_deferred_access_checks ();
8406
8407   /* If parsing tentatively, replace the sequence of tokens that makes
8408      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8409      should we re-parse the token stream, we will not have to repeat
8410      the effort required to do the parse, nor will we issue duplicate
8411      error messages about problems during instantiation of the
8412      template.  */
8413   if (start_of_id)
8414     {
8415       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8416       
8417       /* Reset the contents of the START_OF_ID token.  */
8418       token->type = CPP_TEMPLATE_ID;
8419       token->value = build_tree_list (access_check, template_id);
8420       token->keyword = RID_MAX;
8421       
8422       /* Purge all subsequent tokens.  */
8423       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8424     }
8425
8426   pop_deferring_access_checks ();
8427   return template_id;
8428 }
8429
8430 /* Parse a template-name.
8431
8432    template-name:
8433      identifier
8434
8435    The standard should actually say:
8436
8437    template-name:
8438      identifier
8439      operator-function-id
8440
8441    A defect report has been filed about this issue.
8442
8443    A conversion-function-id cannot be a template name because they cannot
8444    be part of a template-id. In fact, looking at this code:
8445
8446    a.operator K<int>()
8447
8448    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8449    It is impossible to call a templated conversion-function-id with an
8450    explicit argument list, since the only allowed template parameter is
8451    the type to which it is converting.
8452
8453    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8454    `template' keyword, in a construction like:
8455
8456      T::template f<3>()
8457
8458    In that case `f' is taken to be a template-name, even though there
8459    is no way of knowing for sure.
8460
8461    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8462    name refers to a set of overloaded functions, at least one of which
8463    is a template, or an IDENTIFIER_NODE with the name of the template,
8464    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8465    names are looked up inside uninstantiated templates.  */
8466
8467 static tree
8468 cp_parser_template_name (cp_parser* parser,
8469                          bool template_keyword_p,
8470                          bool check_dependency_p,
8471                          bool is_declaration,
8472                          bool *is_identifier)
8473 {
8474   tree identifier;
8475   tree decl;
8476   tree fns;
8477
8478   /* If the next token is `operator', then we have either an
8479      operator-function-id or a conversion-function-id.  */
8480   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8481     {
8482       /* We don't know whether we're looking at an
8483          operator-function-id or a conversion-function-id.  */
8484       cp_parser_parse_tentatively (parser);
8485       /* Try an operator-function-id.  */
8486       identifier = cp_parser_operator_function_id (parser);
8487       /* If that didn't work, try a conversion-function-id.  */
8488       if (!cp_parser_parse_definitely (parser))
8489         {
8490           cp_parser_error (parser, "expected template-name");
8491           return error_mark_node;
8492         }
8493     }
8494   /* Look for the identifier.  */
8495   else
8496     identifier = cp_parser_identifier (parser);
8497
8498   /* If we didn't find an identifier, we don't have a template-id.  */
8499   if (identifier == error_mark_node)
8500     return error_mark_node;
8501
8502   /* If the name immediately followed the `template' keyword, then it
8503      is a template-name.  However, if the next token is not `<', then
8504      we do not treat it as a template-name, since it is not being used
8505      as part of a template-id.  This enables us to handle constructs
8506      like:
8507
8508        template <typename T> struct S { S(); };
8509        template <typename T> S<T>::S();
8510
8511      correctly.  We would treat `S' as a template -- if it were `S<T>'
8512      -- but we do not if there is no `<'.  */
8513
8514   if (processing_template_decl
8515       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8516     {
8517       /* In a declaration, in a dependent context, we pretend that the
8518          "template" keyword was present in order to improve error
8519          recovery.  For example, given:
8520
8521            template <typename T> void f(T::X<int>);
8522
8523          we want to treat "X<int>" as a template-id.  */
8524       if (is_declaration
8525           && !template_keyword_p
8526           && parser->scope && TYPE_P (parser->scope)
8527           && check_dependency_p
8528           && dependent_type_p (parser->scope)
8529           /* Do not do this for dtors (or ctors), since they never
8530              need the template keyword before their name.  */
8531           && !constructor_name_p (identifier, parser->scope))
8532         {
8533           cp_token_position start = 0;
8534           
8535           /* Explain what went wrong.  */
8536           error ("non-template %qD used as template", identifier);
8537           inform ("use %<%T::template %D%> to indicate that it is a template",
8538                   parser->scope, identifier);
8539           /* If parsing tentatively, find the location of the "<"
8540              token.  */
8541           if (cp_parser_parsing_tentatively (parser)
8542               && !cp_parser_committed_to_tentative_parse (parser))
8543             {
8544               cp_parser_simulate_error (parser);
8545               start = cp_lexer_token_position (parser->lexer, true);
8546             }
8547           /* Parse the template arguments so that we can issue error
8548              messages about them.  */
8549           cp_lexer_consume_token (parser->lexer);
8550           cp_parser_enclosed_template_argument_list (parser);
8551           /* Skip tokens until we find a good place from which to
8552              continue parsing.  */
8553           cp_parser_skip_to_closing_parenthesis (parser,
8554                                                  /*recovering=*/true,
8555                                                  /*or_comma=*/true,
8556                                                  /*consume_paren=*/false);
8557           /* If parsing tentatively, permanently remove the
8558              template argument list.  That will prevent duplicate
8559              error messages from being issued about the missing
8560              "template" keyword.  */
8561           if (start)
8562             cp_lexer_purge_tokens_after (parser->lexer, start);
8563           if (is_identifier)
8564             *is_identifier = true;
8565           return identifier;
8566         }
8567
8568       /* If the "template" keyword is present, then there is generally
8569          no point in doing name-lookup, so we just return IDENTIFIER.
8570          But, if the qualifying scope is non-dependent then we can
8571          (and must) do name-lookup normally.  */
8572       if (template_keyword_p
8573           && (!parser->scope
8574               || (TYPE_P (parser->scope)
8575                   && dependent_type_p (parser->scope))))
8576         return identifier;
8577     }
8578
8579   /* Look up the name.  */
8580   decl = cp_parser_lookup_name (parser, identifier,
8581                                 /*is_type=*/false,
8582                                 /*is_template=*/false,
8583                                 /*is_namespace=*/false,
8584                                 check_dependency_p,
8585                                 /*ambiguous_p=*/NULL);
8586   decl = maybe_get_template_decl_from_type_decl (decl);
8587
8588   /* If DECL is a template, then the name was a template-name.  */
8589   if (TREE_CODE (decl) == TEMPLATE_DECL)
8590     ;
8591   else
8592     {
8593       /* The standard does not explicitly indicate whether a name that
8594          names a set of overloaded declarations, some of which are
8595          templates, is a template-name.  However, such a name should
8596          be a template-name; otherwise, there is no way to form a
8597          template-id for the overloaded templates.  */
8598       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8599       if (TREE_CODE (fns) == OVERLOAD)
8600         {
8601           tree fn;
8602
8603           for (fn = fns; fn; fn = OVL_NEXT (fn))
8604             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8605               break;
8606         }
8607       else
8608         {
8609           /* Otherwise, the name does not name a template.  */
8610           cp_parser_error (parser, "expected template-name");
8611           return error_mark_node;
8612         }
8613     }
8614
8615   /* If DECL is dependent, and refers to a function, then just return
8616      its name; we will look it up again during template instantiation.  */
8617   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8618     {
8619       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8620       if (TYPE_P (scope) && dependent_type_p (scope))
8621         return identifier;
8622     }
8623
8624   return decl;
8625 }
8626
8627 /* Parse a template-argument-list.
8628
8629    template-argument-list:
8630      template-argument
8631      template-argument-list , template-argument
8632
8633    Returns a TREE_VEC containing the arguments.  */
8634
8635 static tree
8636 cp_parser_template_argument_list (cp_parser* parser)
8637 {
8638   tree fixed_args[10];
8639   unsigned n_args = 0;
8640   unsigned alloced = 10;
8641   tree *arg_ary = fixed_args;
8642   tree vec;
8643   bool saved_in_template_argument_list_p;
8644
8645   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8646   parser->in_template_argument_list_p = true;
8647   do
8648     {
8649       tree argument;
8650
8651       if (n_args)
8652         /* Consume the comma.  */
8653         cp_lexer_consume_token (parser->lexer);
8654
8655       /* Parse the template-argument.  */
8656       argument = cp_parser_template_argument (parser);
8657       if (n_args == alloced)
8658         {
8659           alloced *= 2;
8660
8661           if (arg_ary == fixed_args)
8662             {
8663               arg_ary = xmalloc (sizeof (tree) * alloced);
8664               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8665             }
8666           else
8667             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8668         }
8669       arg_ary[n_args++] = argument;
8670     }
8671   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8672
8673   vec = make_tree_vec (n_args);
8674
8675   while (n_args--)
8676     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8677
8678   if (arg_ary != fixed_args)
8679     free (arg_ary);
8680   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8681   return vec;
8682 }
8683
8684 /* Parse a template-argument.
8685
8686    template-argument:
8687      assignment-expression
8688      type-id
8689      id-expression
8690
8691    The representation is that of an assignment-expression, type-id, or
8692    id-expression -- except that the qualified id-expression is
8693    evaluated, so that the value returned is either a DECL or an
8694    OVERLOAD.
8695
8696    Although the standard says "assignment-expression", it forbids
8697    throw-expressions or assignments in the template argument.
8698    Therefore, we use "conditional-expression" instead.  */
8699
8700 static tree
8701 cp_parser_template_argument (cp_parser* parser)
8702 {
8703   tree argument;
8704   bool template_p;
8705   bool address_p;
8706   bool maybe_type_id = false;
8707   cp_token *token;
8708   cp_id_kind idk;
8709   tree qualifying_class;
8710
8711   /* There's really no way to know what we're looking at, so we just
8712      try each alternative in order.
8713
8714        [temp.arg]
8715
8716        In a template-argument, an ambiguity between a type-id and an
8717        expression is resolved to a type-id, regardless of the form of
8718        the corresponding template-parameter.
8719
8720      Therefore, we try a type-id first.  */
8721   cp_parser_parse_tentatively (parser);
8722   argument = cp_parser_type_id (parser);
8723   /* If there was no error parsing the type-id but the next token is a '>>',
8724      we probably found a typo for '> >'. But there are type-id which are
8725      also valid expressions. For instance:
8726
8727      struct X { int operator >> (int); };
8728      template <int V> struct Foo {};
8729      Foo<X () >> 5> r;
8730
8731      Here 'X()' is a valid type-id of a function type, but the user just
8732      wanted to write the expression "X() >> 5". Thus, we remember that we
8733      found a valid type-id, but we still try to parse the argument as an
8734      expression to see what happens.  */
8735   if (!cp_parser_error_occurred (parser)
8736       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8737     {
8738       maybe_type_id = true;
8739       cp_parser_abort_tentative_parse (parser);
8740     }
8741   else
8742     {
8743       /* If the next token isn't a `,' or a `>', then this argument wasn't
8744       really finished. This means that the argument is not a valid
8745       type-id.  */
8746       if (!cp_parser_next_token_ends_template_argument_p (parser))
8747         cp_parser_error (parser, "expected template-argument");
8748       /* If that worked, we're done.  */
8749       if (cp_parser_parse_definitely (parser))
8750         return argument;
8751     }
8752   /* We're still not sure what the argument will be.  */
8753   cp_parser_parse_tentatively (parser);
8754   /* Try a template.  */
8755   argument = cp_parser_id_expression (parser,
8756                                       /*template_keyword_p=*/false,
8757                                       /*check_dependency_p=*/true,
8758                                       &template_p,
8759                                       /*declarator_p=*/false);
8760   /* If the next token isn't a `,' or a `>', then this argument wasn't
8761      really finished.  */
8762   if (!cp_parser_next_token_ends_template_argument_p (parser))
8763     cp_parser_error (parser, "expected template-argument");
8764   if (!cp_parser_error_occurred (parser))
8765     {
8766       /* Figure out what is being referred to.  If the id-expression
8767          was for a class template specialization, then we will have a
8768          TYPE_DECL at this point.  There is no need to do name lookup
8769          at this point in that case.  */
8770       if (TREE_CODE (argument) != TYPE_DECL)
8771         argument = cp_parser_lookup_name (parser, argument,
8772                                           /*is_type=*/false,
8773                                           /*is_template=*/template_p,
8774                                           /*is_namespace=*/false,
8775                                           /*check_dependency=*/true,
8776                                           /*ambiguous_p=*/NULL);
8777       if (TREE_CODE (argument) != TEMPLATE_DECL
8778           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8779         cp_parser_error (parser, "expected template-name");
8780     }
8781   if (cp_parser_parse_definitely (parser))
8782     return argument;
8783   /* It must be a non-type argument.  There permitted cases are given
8784      in [temp.arg.nontype]:
8785
8786      -- an integral constant-expression of integral or enumeration
8787         type; or
8788
8789      -- the name of a non-type template-parameter; or
8790
8791      -- the name of an object or function with external linkage...
8792
8793      -- the address of an object or function with external linkage...
8794
8795      -- a pointer to member...  */
8796   /* Look for a non-type template parameter.  */
8797   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8798     {
8799       cp_parser_parse_tentatively (parser);
8800       argument = cp_parser_primary_expression (parser,
8801                                                &idk,
8802                                                &qualifying_class);
8803       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8804           || !cp_parser_next_token_ends_template_argument_p (parser))
8805         cp_parser_simulate_error (parser);
8806       if (cp_parser_parse_definitely (parser))
8807         return argument;
8808     }
8809   /* If the next token is "&", the argument must be the address of an
8810      object or function with external linkage.  */
8811   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8812   if (address_p)
8813     cp_lexer_consume_token (parser->lexer);
8814   /* See if we might have an id-expression.  */
8815   token = cp_lexer_peek_token (parser->lexer);
8816   if (token->type == CPP_NAME
8817       || token->keyword == RID_OPERATOR
8818       || token->type == CPP_SCOPE
8819       || token->type == CPP_TEMPLATE_ID
8820       || token->type == CPP_NESTED_NAME_SPECIFIER)
8821     {
8822       cp_parser_parse_tentatively (parser);
8823       argument = cp_parser_primary_expression (parser,
8824                                                &idk,
8825                                                &qualifying_class);
8826       if (cp_parser_error_occurred (parser)
8827           || !cp_parser_next_token_ends_template_argument_p (parser))
8828         cp_parser_abort_tentative_parse (parser);
8829       else
8830         {
8831           if (qualifying_class)
8832             argument = finish_qualified_id_expr (qualifying_class,
8833                                                  argument,
8834                                                  /*done=*/true,
8835                                                  address_p);
8836           if (TREE_CODE (argument) == VAR_DECL)
8837             {
8838               /* A variable without external linkage might still be a
8839                  valid constant-expression, so no error is issued here
8840                  if the external-linkage check fails.  */
8841               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8842                 cp_parser_simulate_error (parser);
8843             }
8844           else if (is_overloaded_fn (argument))
8845             /* All overloaded functions are allowed; if the external
8846                linkage test does not pass, an error will be issued
8847                later.  */
8848             ;
8849           else if (address_p
8850                    && (TREE_CODE (argument) == OFFSET_REF
8851                        || TREE_CODE (argument) == SCOPE_REF))
8852             /* A pointer-to-member.  */
8853             ;
8854           else
8855             cp_parser_simulate_error (parser);
8856
8857           if (cp_parser_parse_definitely (parser))
8858             {
8859               if (address_p)
8860                 argument = build_x_unary_op (ADDR_EXPR, argument);
8861               return argument;
8862             }
8863         }
8864     }
8865   /* If the argument started with "&", there are no other valid
8866      alternatives at this point.  */
8867   if (address_p)
8868     {
8869       cp_parser_error (parser, "invalid non-type template argument");
8870       return error_mark_node;
8871     }
8872   /* If the argument wasn't successfully parsed as a type-id followed
8873      by '>>', the argument can only be a constant expression now.
8874      Otherwise, we try parsing the constant-expression tentatively,
8875      because the argument could really be a type-id.  */
8876   if (maybe_type_id)
8877     cp_parser_parse_tentatively (parser);
8878   argument = cp_parser_constant_expression (parser,
8879                                             /*allow_non_constant_p=*/false,
8880                                             /*non_constant_p=*/NULL);
8881   argument = fold_non_dependent_expr (argument);
8882   if (!maybe_type_id)
8883     return argument;
8884   if (!cp_parser_next_token_ends_template_argument_p (parser))
8885     cp_parser_error (parser, "expected template-argument");
8886   if (cp_parser_parse_definitely (parser))
8887     return argument;
8888   /* We did our best to parse the argument as a non type-id, but that
8889      was the only alternative that matched (albeit with a '>' after
8890      it). We can assume it's just a typo from the user, and a
8891      diagnostic will then be issued.  */
8892   return cp_parser_type_id (parser);
8893 }
8894
8895 /* Parse an explicit-instantiation.
8896
8897    explicit-instantiation:
8898      template declaration
8899
8900    Although the standard says `declaration', what it really means is:
8901
8902    explicit-instantiation:
8903      template decl-specifier-seq [opt] declarator [opt] ;
8904
8905    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8906    supposed to be allowed.  A defect report has been filed about this
8907    issue.
8908
8909    GNU Extension:
8910
8911    explicit-instantiation:
8912      storage-class-specifier template
8913        decl-specifier-seq [opt] declarator [opt] ;
8914      function-specifier template
8915        decl-specifier-seq [opt] declarator [opt] ;  */
8916
8917 static void
8918 cp_parser_explicit_instantiation (cp_parser* parser)
8919 {
8920   int declares_class_or_enum;
8921   cp_decl_specifier_seq decl_specifiers;
8922   tree extension_specifier = NULL_TREE;
8923
8924   /* Look for an (optional) storage-class-specifier or
8925      function-specifier.  */
8926   if (cp_parser_allow_gnu_extensions_p (parser))
8927     {
8928       extension_specifier
8929         = cp_parser_storage_class_specifier_opt (parser);
8930       if (!extension_specifier)
8931         extension_specifier
8932           = cp_parser_function_specifier_opt (parser,
8933                                               /*decl_specs=*/NULL);
8934     }
8935
8936   /* Look for the `template' keyword.  */
8937   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8938   /* Let the front end know that we are processing an explicit
8939      instantiation.  */
8940   begin_explicit_instantiation ();
8941   /* [temp.explicit] says that we are supposed to ignore access
8942      control while processing explicit instantiation directives.  */
8943   push_deferring_access_checks (dk_no_check);
8944   /* Parse a decl-specifier-seq.  */
8945   cp_parser_decl_specifier_seq (parser,
8946                                 CP_PARSER_FLAGS_OPTIONAL,
8947                                 &decl_specifiers,
8948                                 &declares_class_or_enum);
8949   /* If there was exactly one decl-specifier, and it declared a class,
8950      and there's no declarator, then we have an explicit type
8951      instantiation.  */
8952   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8953     {
8954       tree type;
8955
8956       type = check_tag_decl (&decl_specifiers);
8957       /* Turn access control back on for names used during
8958          template instantiation.  */
8959       pop_deferring_access_checks ();
8960       if (type)
8961         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8962     }
8963   else
8964     {
8965       cp_declarator *declarator;
8966       tree decl;
8967
8968       /* Parse the declarator.  */
8969       declarator
8970         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8971                                 /*ctor_dtor_or_conv_p=*/NULL,
8972                                 /*parenthesized_p=*/NULL,
8973                                 /*member_p=*/false);
8974       cp_parser_check_for_definition_in_return_type (declarator,
8975                                                      declares_class_or_enum);
8976       if (declarator != cp_error_declarator)
8977         {
8978           decl = grokdeclarator (declarator, &decl_specifiers,
8979                                  NORMAL, 0, NULL);
8980           /* Turn access control back on for names used during
8981              template instantiation.  */
8982           pop_deferring_access_checks ();
8983           /* Do the explicit instantiation.  */
8984           do_decl_instantiation (decl, extension_specifier);
8985         }
8986       else
8987         {
8988           pop_deferring_access_checks ();
8989           /* Skip the body of the explicit instantiation.  */
8990           cp_parser_skip_to_end_of_statement (parser);
8991         }
8992     }
8993   /* We're done with the instantiation.  */
8994   end_explicit_instantiation ();
8995
8996   cp_parser_consume_semicolon_at_end_of_statement (parser);
8997 }
8998
8999 /* Parse an explicit-specialization.
9000
9001    explicit-specialization:
9002      template < > declaration
9003
9004    Although the standard says `declaration', what it really means is:
9005
9006    explicit-specialization:
9007      template <> decl-specifier [opt] init-declarator [opt] ;
9008      template <> function-definition
9009      template <> explicit-specialization
9010      template <> template-declaration  */
9011
9012 static void
9013 cp_parser_explicit_specialization (cp_parser* parser)
9014 {
9015   /* Look for the `template' keyword.  */
9016   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9017   /* Look for the `<'.  */
9018   cp_parser_require (parser, CPP_LESS, "`<'");
9019   /* Look for the `>'.  */
9020   cp_parser_require (parser, CPP_GREATER, "`>'");
9021   /* We have processed another parameter list.  */
9022   ++parser->num_template_parameter_lists;
9023   /* Let the front end know that we are beginning a specialization.  */
9024   begin_specialization ();
9025
9026   /* If the next keyword is `template', we need to figure out whether
9027      or not we're looking a template-declaration.  */
9028   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9029     {
9030       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9031           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9032         cp_parser_template_declaration_after_export (parser,
9033                                                      /*member_p=*/false);
9034       else
9035         cp_parser_explicit_specialization (parser);
9036     }
9037   else
9038     /* Parse the dependent declaration.  */
9039     cp_parser_single_declaration (parser,
9040                                   /*member_p=*/false,
9041                                   /*friend_p=*/NULL);
9042
9043   /* We're done with the specialization.  */
9044   end_specialization ();
9045   /* We're done with this parameter list.  */
9046   --parser->num_template_parameter_lists;
9047 }
9048
9049 /* Parse a type-specifier.
9050
9051    type-specifier:
9052      simple-type-specifier
9053      class-specifier
9054      enum-specifier
9055      elaborated-type-specifier
9056      cv-qualifier
9057
9058    GNU Extension:
9059
9060    type-specifier:
9061      __complex__
9062
9063    Returns a representation of the type-specifier.  For a
9064    class-specifier, enum-specifier, or elaborated-type-specifier, a
9065    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9066
9067    The parser flags FLAGS is used to control type-specifier parsing.
9068
9069    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9070    in a decl-specifier-seq.
9071
9072    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9073    class-specifier, enum-specifier, or elaborated-type-specifier, then
9074    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9075    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9076    zero.
9077
9078    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9079    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9080    is set to FALSE.  */
9081
9082 static tree
9083 cp_parser_type_specifier (cp_parser* parser,
9084                           cp_parser_flags flags,
9085                           cp_decl_specifier_seq *decl_specs,
9086                           bool is_declaration,
9087                           int* declares_class_or_enum,
9088                           bool* is_cv_qualifier)
9089 {
9090   tree type_spec = NULL_TREE;
9091   cp_token *token;
9092   enum rid keyword;
9093   cp_decl_spec ds = ds_last;
9094
9095   /* Assume this type-specifier does not declare a new type.  */
9096   if (declares_class_or_enum)
9097     *declares_class_or_enum = 0;
9098   /* And that it does not specify a cv-qualifier.  */
9099   if (is_cv_qualifier)
9100     *is_cv_qualifier = false;
9101   /* Peek at the next token.  */
9102   token = cp_lexer_peek_token (parser->lexer);
9103
9104   /* If we're looking at a keyword, we can use that to guide the
9105      production we choose.  */
9106   keyword = token->keyword;
9107   switch (keyword)
9108     {
9109     case RID_ENUM:
9110       /* 'enum' [identifier] '{' introduces an enum-specifier;
9111          'enum' <anything else> introduces an elaborated-type-specifier.  */
9112       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9113           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9114               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9115                  == CPP_OPEN_BRACE))
9116         {
9117           type_spec = cp_parser_enum_specifier (parser);
9118           if (declares_class_or_enum)
9119             *declares_class_or_enum = 2;
9120           if (decl_specs)
9121             cp_parser_set_decl_spec_type (decl_specs,
9122                                           type_spec,
9123                                           /*user_defined_p=*/true);
9124           return type_spec;
9125         }
9126       else
9127         goto elaborated_type_specifier;
9128
9129       /* Any of these indicate either a class-specifier, or an
9130          elaborated-type-specifier.  */
9131     case RID_CLASS:
9132     case RID_STRUCT:
9133     case RID_UNION:
9134       /* Parse tentatively so that we can back up if we don't find a
9135          class-specifier.  */
9136       cp_parser_parse_tentatively (parser);
9137       /* Look for the class-specifier.  */
9138       type_spec = cp_parser_class_specifier (parser);
9139       /* If that worked, we're done.  */
9140       if (cp_parser_parse_definitely (parser))
9141         {
9142           if (declares_class_or_enum)
9143             *declares_class_or_enum = 2;
9144           if (decl_specs)
9145             cp_parser_set_decl_spec_type (decl_specs,
9146                                           type_spec,
9147                                           /*user_defined_p=*/true);
9148           return type_spec;
9149         }
9150
9151       /* Fall through.  */
9152     elaborated_type_specifier:
9153       /* We're declaring (not defining) a class or enum.  */
9154       if (declares_class_or_enum)
9155         *declares_class_or_enum = 1;
9156
9157       /* Fall through.  */
9158     case RID_TYPENAME:
9159       /* Look for an elaborated-type-specifier.  */
9160       type_spec
9161         = (cp_parser_elaborated_type_specifier
9162            (parser,
9163             decl_specs && decl_specs->specs[(int) ds_friend],
9164             is_declaration));
9165       if (decl_specs)
9166         cp_parser_set_decl_spec_type (decl_specs,
9167                                       type_spec,
9168                                       /*user_defined_p=*/true);
9169       return type_spec;
9170
9171     case RID_CONST:
9172       ds = ds_const;
9173       if (is_cv_qualifier)
9174         *is_cv_qualifier = true;
9175       break;
9176
9177     case RID_VOLATILE:
9178       ds = ds_volatile;
9179       if (is_cv_qualifier)
9180         *is_cv_qualifier = true;
9181       break;
9182
9183     case RID_RESTRICT:
9184       ds = ds_restrict;
9185       if (is_cv_qualifier)
9186         *is_cv_qualifier = true;
9187       break;
9188
9189     case RID_COMPLEX:
9190       /* The `__complex__' keyword is a GNU extension.  */
9191       ds = ds_complex;
9192       break;
9193
9194     default:
9195       break;
9196     }
9197
9198   /* Handle simple keywords.  */
9199   if (ds != ds_last)
9200     {
9201       if (decl_specs)
9202         {
9203           ++decl_specs->specs[(int)ds];
9204           decl_specs->any_specifiers_p = true;
9205         }
9206       return cp_lexer_consume_token (parser->lexer)->value;
9207     }
9208
9209   /* If we do not already have a type-specifier, assume we are looking
9210      at a simple-type-specifier.  */
9211   type_spec = cp_parser_simple_type_specifier (parser,
9212                                                decl_specs,
9213                                                flags);
9214
9215   /* If we didn't find a type-specifier, and a type-specifier was not
9216      optional in this context, issue an error message.  */
9217   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9218     {
9219       cp_parser_error (parser, "expected type specifier");
9220       return error_mark_node;
9221     }
9222
9223   return type_spec;
9224 }
9225
9226 /* Parse a simple-type-specifier.
9227
9228    simple-type-specifier:
9229      :: [opt] nested-name-specifier [opt] type-name
9230      :: [opt] nested-name-specifier template template-id
9231      char
9232      wchar_t
9233      bool
9234      short
9235      int
9236      long
9237      signed
9238      unsigned
9239      float
9240      double
9241      void
9242
9243    GNU Extension:
9244
9245    simple-type-specifier:
9246      __typeof__ unary-expression
9247      __typeof__ ( type-id )
9248
9249    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9250    appropriately updated.  */
9251
9252 static tree
9253 cp_parser_simple_type_specifier (cp_parser* parser,
9254                                  cp_decl_specifier_seq *decl_specs,
9255                                  cp_parser_flags flags)
9256 {
9257   tree type = NULL_TREE;
9258   cp_token *token;
9259
9260   /* Peek at the next token.  */
9261   token = cp_lexer_peek_token (parser->lexer);
9262
9263   /* If we're looking at a keyword, things are easy.  */
9264   switch (token->keyword)
9265     {
9266     case RID_CHAR:
9267       if (decl_specs)
9268         decl_specs->explicit_char_p = true;
9269       type = char_type_node;
9270       break;
9271     case RID_WCHAR:
9272       type = wchar_type_node;
9273       break;
9274     case RID_BOOL:
9275       type = boolean_type_node;
9276       break;
9277     case RID_SHORT:
9278       if (decl_specs)
9279         ++decl_specs->specs[(int) ds_short];
9280       type = short_integer_type_node;
9281       break;
9282     case RID_INT:
9283       if (decl_specs)
9284         decl_specs->explicit_int_p = true;
9285       type = integer_type_node;
9286       break;
9287     case RID_LONG:
9288       if (decl_specs)
9289         ++decl_specs->specs[(int) ds_long];
9290       type = long_integer_type_node;
9291       break;
9292     case RID_SIGNED:
9293       if (decl_specs)
9294         ++decl_specs->specs[(int) ds_signed];
9295       type = integer_type_node;
9296       break;
9297     case RID_UNSIGNED:
9298       if (decl_specs)
9299         ++decl_specs->specs[(int) ds_unsigned];
9300       type = unsigned_type_node;
9301       break;
9302     case RID_FLOAT:
9303       type = float_type_node;
9304       break;
9305     case RID_DOUBLE:
9306       type = double_type_node;
9307       break;
9308     case RID_VOID:
9309       type = void_type_node;
9310       break;
9311
9312     case RID_TYPEOF:
9313       /* Consume the `typeof' token.  */
9314       cp_lexer_consume_token (parser->lexer);
9315       /* Parse the operand to `typeof'.  */
9316       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9317       /* If it is not already a TYPE, take its type.  */
9318       if (!TYPE_P (type))
9319         type = finish_typeof (type);
9320
9321       if (decl_specs)
9322         cp_parser_set_decl_spec_type (decl_specs, type,
9323                                       /*user_defined_p=*/true);
9324
9325       return type;
9326
9327     default:
9328       break;
9329     }
9330
9331   /* If the type-specifier was for a built-in type, we're done.  */
9332   if (type)
9333     {
9334       tree id;
9335
9336       /* Record the type.  */
9337       if (decl_specs
9338           && (token->keyword != RID_SIGNED
9339               && token->keyword != RID_UNSIGNED
9340               && token->keyword != RID_SHORT
9341               && token->keyword != RID_LONG))
9342         cp_parser_set_decl_spec_type (decl_specs,
9343                                       type,
9344                                       /*user_defined=*/false);
9345       if (decl_specs)
9346         decl_specs->any_specifiers_p = true;
9347
9348       /* Consume the token.  */
9349       id = cp_lexer_consume_token (parser->lexer)->value;
9350
9351       /* There is no valid C++ program where a non-template type is
9352          followed by a "<".  That usually indicates that the user thought
9353          that the type was a template.  */
9354       cp_parser_check_for_invalid_template_id (parser, type);
9355
9356       return TYPE_NAME (type);
9357     }
9358
9359   /* The type-specifier must be a user-defined type.  */
9360   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9361     {
9362       bool qualified_p;
9363       bool global_p;
9364
9365       /* Don't gobble tokens or issue error messages if this is an
9366          optional type-specifier.  */
9367       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9368         cp_parser_parse_tentatively (parser);
9369
9370       /* Look for the optional `::' operator.  */
9371       global_p
9372         = (cp_parser_global_scope_opt (parser,
9373                                        /*current_scope_valid_p=*/false)
9374            != NULL_TREE);
9375       /* Look for the nested-name specifier.  */
9376       qualified_p
9377         = (cp_parser_nested_name_specifier_opt (parser,
9378                                                 /*typename_keyword_p=*/false,
9379                                                 /*check_dependency_p=*/true,
9380                                                 /*type_p=*/false,
9381                                                 /*is_declaration=*/false)
9382            != NULL_TREE);
9383       /* If we have seen a nested-name-specifier, and the next token
9384          is `template', then we are using the template-id production.  */
9385       if (parser->scope
9386           && cp_parser_optional_template_keyword (parser))
9387         {
9388           /* Look for the template-id.  */
9389           type = cp_parser_template_id (parser,
9390                                         /*template_keyword_p=*/true,
9391                                         /*check_dependency_p=*/true,
9392                                         /*is_declaration=*/false);
9393           /* If the template-id did not name a type, we are out of
9394              luck.  */
9395           if (TREE_CODE (type) != TYPE_DECL)
9396             {
9397               cp_parser_error (parser, "expected template-id for type");
9398               type = NULL_TREE;
9399             }
9400         }
9401       /* Otherwise, look for a type-name.  */
9402       else
9403         type = cp_parser_type_name (parser);
9404       /* Keep track of all name-lookups performed in class scopes.  */
9405       if (type
9406           && !global_p
9407           && !qualified_p
9408           && TREE_CODE (type) == TYPE_DECL
9409           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9410         maybe_note_name_used_in_class (DECL_NAME (type), type);
9411       /* If it didn't work out, we don't have a TYPE.  */
9412       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9413           && !cp_parser_parse_definitely (parser))
9414         type = NULL_TREE;
9415       if (type && decl_specs)
9416         cp_parser_set_decl_spec_type (decl_specs, type,
9417                                       /*user_defined=*/true);
9418     }
9419
9420   /* If we didn't get a type-name, issue an error message.  */
9421   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9422     {
9423       cp_parser_error (parser, "expected type-name");
9424       return error_mark_node;
9425     }
9426
9427   /* There is no valid C++ program where a non-template type is
9428      followed by a "<".  That usually indicates that the user thought
9429      that the type was a template.  */
9430   if (type && type != error_mark_node)
9431     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9432
9433   return type;
9434 }
9435
9436 /* Parse a type-name.
9437
9438    type-name:
9439      class-name
9440      enum-name
9441      typedef-name
9442
9443    enum-name:
9444      identifier
9445
9446    typedef-name:
9447      identifier
9448
9449    Returns a TYPE_DECL for the the type.  */
9450
9451 static tree
9452 cp_parser_type_name (cp_parser* parser)
9453 {
9454   tree type_decl;
9455   tree identifier;
9456
9457   /* We can't know yet whether it is a class-name or not.  */
9458   cp_parser_parse_tentatively (parser);
9459   /* Try a class-name.  */
9460   type_decl = cp_parser_class_name (parser,
9461                                     /*typename_keyword_p=*/false,
9462                                     /*template_keyword_p=*/false,
9463                                     /*type_p=*/false,
9464                                     /*check_dependency_p=*/true,
9465                                     /*class_head_p=*/false,
9466                                     /*is_declaration=*/false);
9467   /* If it's not a class-name, keep looking.  */
9468   if (!cp_parser_parse_definitely (parser))
9469     {
9470       /* It must be a typedef-name or an enum-name.  */
9471       identifier = cp_parser_identifier (parser);
9472       if (identifier == error_mark_node)
9473         return error_mark_node;
9474
9475       /* Look up the type-name.  */
9476       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9477       /* Issue an error if we did not find a type-name.  */
9478       if (TREE_CODE (type_decl) != TYPE_DECL)
9479         {
9480           if (!cp_parser_simulate_error (parser))
9481             cp_parser_name_lookup_error (parser, identifier, type_decl,
9482                                          "is not a type");
9483           type_decl = error_mark_node;
9484         }
9485       /* Remember that the name was used in the definition of the
9486          current class so that we can check later to see if the
9487          meaning would have been different after the class was
9488          entirely defined.  */
9489       else if (type_decl != error_mark_node
9490                && !parser->scope)
9491         maybe_note_name_used_in_class (identifier, type_decl);
9492     }
9493
9494   return type_decl;
9495 }
9496
9497
9498 /* Parse an elaborated-type-specifier.  Note that the grammar given
9499    here incorporates the resolution to DR68.
9500
9501    elaborated-type-specifier:
9502      class-key :: [opt] nested-name-specifier [opt] identifier
9503      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9504      enum :: [opt] nested-name-specifier [opt] identifier
9505      typename :: [opt] nested-name-specifier identifier
9506      typename :: [opt] nested-name-specifier template [opt]
9507        template-id
9508
9509    GNU extension:
9510
9511    elaborated-type-specifier:
9512      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9513      class-key attributes :: [opt] nested-name-specifier [opt]
9514                template [opt] template-id
9515      enum attributes :: [opt] nested-name-specifier [opt] identifier
9516
9517    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9518    declared `friend'.  If IS_DECLARATION is TRUE, then this
9519    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9520    something is being declared.
9521
9522    Returns the TYPE specified.  */
9523
9524 static tree
9525 cp_parser_elaborated_type_specifier (cp_parser* parser,
9526                                      bool is_friend,
9527                                      bool is_declaration)
9528 {
9529   enum tag_types tag_type;
9530   tree identifier;
9531   tree type = NULL_TREE;
9532   tree attributes = NULL_TREE;
9533
9534   /* See if we're looking at the `enum' keyword.  */
9535   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9536     {
9537       /* Consume the `enum' token.  */
9538       cp_lexer_consume_token (parser->lexer);
9539       /* Remember that it's an enumeration type.  */
9540       tag_type = enum_type;
9541       /* Parse the attributes.  */
9542       attributes = cp_parser_attributes_opt (parser);
9543     }
9544   /* Or, it might be `typename'.  */
9545   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9546                                            RID_TYPENAME))
9547     {
9548       /* Consume the `typename' token.  */
9549       cp_lexer_consume_token (parser->lexer);
9550       /* Remember that it's a `typename' type.  */
9551       tag_type = typename_type;
9552       /* The `typename' keyword is only allowed in templates.  */
9553       if (!processing_template_decl)
9554         pedwarn ("using %<typename%> outside of template");
9555     }
9556   /* Otherwise it must be a class-key.  */
9557   else
9558     {
9559       tag_type = cp_parser_class_key (parser);
9560       if (tag_type == none_type)
9561         return error_mark_node;
9562       /* Parse the attributes.  */
9563       attributes = cp_parser_attributes_opt (parser);
9564     }
9565
9566   /* Look for the `::' operator.  */
9567   cp_parser_global_scope_opt (parser,
9568                               /*current_scope_valid_p=*/false);
9569   /* Look for the nested-name-specifier.  */
9570   if (tag_type == typename_type)
9571     {
9572       if (cp_parser_nested_name_specifier (parser,
9573                                            /*typename_keyword_p=*/true,
9574                                            /*check_dependency_p=*/true,
9575                                            /*type_p=*/true,
9576                                            is_declaration)
9577           == error_mark_node)
9578         return error_mark_node;
9579     }
9580   else
9581     /* Even though `typename' is not present, the proposed resolution
9582        to Core Issue 180 says that in `class A<T>::B', `B' should be
9583        considered a type-name, even if `A<T>' is dependent.  */
9584     cp_parser_nested_name_specifier_opt (parser,
9585                                          /*typename_keyword_p=*/true,
9586                                          /*check_dependency_p=*/true,
9587                                          /*type_p=*/true,
9588                                          is_declaration);
9589   /* For everything but enumeration types, consider a template-id.  */
9590   if (tag_type != enum_type)
9591     {
9592       bool template_p = false;
9593       tree decl;
9594
9595       /* Allow the `template' keyword.  */
9596       template_p = cp_parser_optional_template_keyword (parser);
9597       /* If we didn't see `template', we don't know if there's a
9598          template-id or not.  */
9599       if (!template_p)
9600         cp_parser_parse_tentatively (parser);
9601       /* Parse the template-id.  */
9602       decl = cp_parser_template_id (parser, template_p,
9603                                     /*check_dependency_p=*/true,
9604                                     is_declaration);
9605       /* If we didn't find a template-id, look for an ordinary
9606          identifier.  */
9607       if (!template_p && !cp_parser_parse_definitely (parser))
9608         ;
9609       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9610          in effect, then we must assume that, upon instantiation, the
9611          template will correspond to a class.  */
9612       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9613                && tag_type == typename_type)
9614         type = make_typename_type (parser->scope, decl,
9615                                    /*complain=*/1);
9616       else
9617         type = TREE_TYPE (decl);
9618     }
9619
9620   /* For an enumeration type, consider only a plain identifier.  */
9621   if (!type)
9622     {
9623       identifier = cp_parser_identifier (parser);
9624
9625       if (identifier == error_mark_node)
9626         {
9627           parser->scope = NULL_TREE;
9628           return error_mark_node;
9629         }
9630
9631       /* For a `typename', we needn't call xref_tag.  */
9632       if (tag_type == typename_type)
9633         return cp_parser_make_typename_type (parser, parser->scope,
9634                                              identifier);
9635       /* Look up a qualified name in the usual way.  */
9636       if (parser->scope)
9637         {
9638           tree decl;
9639
9640           /* In an elaborated-type-specifier, names are assumed to name
9641              types, so we set IS_TYPE to TRUE when calling
9642              cp_parser_lookup_name.  */
9643           decl = cp_parser_lookup_name (parser, identifier,
9644                                         /*is_type=*/true,
9645                                         /*is_template=*/false,
9646                                         /*is_namespace=*/false,
9647                                         /*check_dependency=*/true,
9648                                         /*ambiguous_p=*/NULL);
9649
9650           /* If we are parsing friend declaration, DECL may be a
9651              TEMPLATE_DECL tree node here.  However, we need to check
9652              whether this TEMPLATE_DECL results in valid code.  Consider
9653              the following example:
9654
9655                namespace N {
9656                  template <class T> class C {};
9657                }
9658                class X {
9659                  template <class T> friend class N::C; // #1, valid code
9660                };
9661                template <class T> class Y {
9662                  friend class N::C;                    // #2, invalid code
9663                };
9664
9665              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9666              name lookup of `N::C'.  We see that friend declaration must
9667              be template for the code to be valid.  Note that
9668              processing_template_decl does not work here since it is
9669              always 1 for the above two cases.  */
9670
9671           decl = (cp_parser_maybe_treat_template_as_class
9672                   (decl, /*tag_name_p=*/is_friend
9673                          && parser->num_template_parameter_lists));
9674
9675           if (TREE_CODE (decl) != TYPE_DECL)
9676             {
9677               error ("expected type-name");
9678               return error_mark_node;
9679             }
9680
9681           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9682             check_elaborated_type_specifier
9683               (tag_type, decl,
9684                (parser->num_template_parameter_lists
9685                 || DECL_SELF_REFERENCE_P (decl)));
9686
9687           type = TREE_TYPE (decl);
9688         }
9689       else
9690         {
9691           /* An elaborated-type-specifier sometimes introduces a new type and
9692              sometimes names an existing type.  Normally, the rule is that it
9693              introduces a new type only if there is not an existing type of
9694              the same name already in scope.  For example, given:
9695
9696                struct S {};
9697                void f() { struct S s; }
9698
9699              the `struct S' in the body of `f' is the same `struct S' as in
9700              the global scope; the existing definition is used.  However, if
9701              there were no global declaration, this would introduce a new
9702              local class named `S'.
9703
9704              An exception to this rule applies to the following code:
9705
9706                namespace N { struct S; }
9707
9708              Here, the elaborated-type-specifier names a new type
9709              unconditionally; even if there is already an `S' in the
9710              containing scope this declaration names a new type.
9711              This exception only applies if the elaborated-type-specifier
9712              forms the complete declaration:
9713
9714                [class.name]
9715
9716                A declaration consisting solely of `class-key identifier ;' is
9717                either a redeclaration of the name in the current scope or a
9718                forward declaration of the identifier as a class name.  It
9719                introduces the name into the current scope.
9720
9721              We are in this situation precisely when the next token is a `;'.
9722
9723              An exception to the exception is that a `friend' declaration does
9724              *not* name a new type; i.e., given:
9725
9726                struct S { friend struct T; };
9727
9728              `T' is not a new type in the scope of `S'.
9729
9730              Also, `new struct S' or `sizeof (struct S)' never results in the
9731              definition of a new type; a new type can only be declared in a
9732              declaration context.  */
9733
9734           tag_scope ts;
9735           if (is_friend)
9736             /* Friends have special name lookup rules.  */
9737             ts = ts_within_enclosing_non_class;
9738           else if (is_declaration
9739                    && cp_lexer_next_token_is (parser->lexer,
9740                                               CPP_SEMICOLON))
9741             /* This is a `class-key identifier ;' */
9742             ts = ts_current;
9743           else
9744             ts = ts_global;
9745
9746           /* Warn about attributes. They are ignored.  */
9747           if (attributes)
9748             warning ("type attributes are honored only at type definition");
9749
9750           type = xref_tag (tag_type, identifier, ts,
9751                            parser->num_template_parameter_lists);
9752         }
9753     }
9754   if (tag_type != enum_type)
9755     cp_parser_check_class_key (tag_type, type);
9756
9757   /* A "<" cannot follow an elaborated type specifier.  If that
9758      happens, the user was probably trying to form a template-id.  */
9759   cp_parser_check_for_invalid_template_id (parser, type);
9760
9761   return type;
9762 }
9763
9764 /* Parse an enum-specifier.
9765
9766    enum-specifier:
9767      enum identifier [opt] { enumerator-list [opt] }
9768
9769    GNU Extensions:
9770      enum identifier [opt] { enumerator-list [opt] } attributes
9771
9772    Returns an ENUM_TYPE representing the enumeration.  */
9773
9774 static tree
9775 cp_parser_enum_specifier (cp_parser* parser)
9776 {
9777   tree identifier;
9778   tree type;
9779
9780   /* Caller guarantees that the current token is 'enum', an identifier
9781      possibly follows, and the token after that is an opening brace.
9782      If we don't have an identifier, fabricate an anonymous name for
9783      the enumeration being defined.  */
9784   cp_lexer_consume_token (parser->lexer);
9785
9786   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9787     identifier = cp_parser_identifier (parser);
9788   else
9789     identifier = make_anon_name ();
9790
9791   /* Issue an error message if type-definitions are forbidden here.  */
9792   cp_parser_check_type_definition (parser);
9793
9794   /* Create the new type.  We do this before consuming the opening brace
9795      so the enum will be recorded as being on the line of its tag (or the
9796      'enum' keyword, if there is no tag).  */
9797   type = start_enum (identifier);
9798
9799   /* Consume the opening brace.  */
9800   cp_lexer_consume_token (parser->lexer);
9801
9802   /* If the next token is not '}', then there are some enumerators.  */
9803   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9804     cp_parser_enumerator_list (parser, type);
9805
9806   /* Consume the final '}'.  */
9807   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9808
9809   /* Look for trailing attributes to apply to this enumeration, and
9810      apply them if appropriate.  */
9811   if (cp_parser_allow_gnu_extensions_p (parser))
9812     {
9813       tree trailing_attr = cp_parser_attributes_opt (parser);
9814       cplus_decl_attributes (&type,
9815                              trailing_attr,
9816                              (int) ATTR_FLAG_TYPE_IN_PLACE);
9817     }
9818
9819   /* Finish up the enumeration.  */
9820   finish_enum (type);
9821
9822   return type;
9823 }
9824
9825 /* Parse an enumerator-list.  The enumerators all have the indicated
9826    TYPE.
9827
9828    enumerator-list:
9829      enumerator-definition
9830      enumerator-list , enumerator-definition  */
9831
9832 static void
9833 cp_parser_enumerator_list (cp_parser* parser, tree type)
9834 {
9835   while (true)
9836     {
9837       /* Parse an enumerator-definition.  */
9838       cp_parser_enumerator_definition (parser, type);
9839
9840       /* If the next token is not a ',', we've reached the end of
9841          the list.  */
9842       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9843         break;
9844       /* Otherwise, consume the `,' and keep going.  */
9845       cp_lexer_consume_token (parser->lexer);
9846       /* If the next token is a `}', there is a trailing comma.  */
9847       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9848         {
9849           if (pedantic && !in_system_header)
9850             pedwarn ("comma at end of enumerator list");
9851           break;
9852         }
9853     }
9854 }
9855
9856 /* Parse an enumerator-definition.  The enumerator has the indicated
9857    TYPE.
9858
9859    enumerator-definition:
9860      enumerator
9861      enumerator = constant-expression
9862
9863    enumerator:
9864      identifier  */
9865
9866 static void
9867 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9868 {
9869   tree identifier;
9870   tree value;
9871
9872   /* Look for the identifier.  */
9873   identifier = cp_parser_identifier (parser);
9874   if (identifier == error_mark_node)
9875     return;
9876
9877   /* If the next token is an '=', then there is an explicit value.  */
9878   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9879     {
9880       /* Consume the `=' token.  */
9881       cp_lexer_consume_token (parser->lexer);
9882       /* Parse the value.  */
9883       value = cp_parser_constant_expression (parser,
9884                                              /*allow_non_constant_p=*/false,
9885                                              NULL);
9886     }
9887   else
9888     value = NULL_TREE;
9889
9890   /* Create the enumerator.  */
9891   build_enumerator (identifier, value, type);
9892 }
9893
9894 /* Parse a namespace-name.
9895
9896    namespace-name:
9897      original-namespace-name
9898      namespace-alias
9899
9900    Returns the NAMESPACE_DECL for the namespace.  */
9901
9902 static tree
9903 cp_parser_namespace_name (cp_parser* parser)
9904 {
9905   tree identifier;
9906   tree namespace_decl;
9907
9908   /* Get the name of the namespace.  */
9909   identifier = cp_parser_identifier (parser);
9910   if (identifier == error_mark_node)
9911     return error_mark_node;
9912
9913   /* Look up the identifier in the currently active scope.  Look only
9914      for namespaces, due to:
9915
9916        [basic.lookup.udir]
9917
9918        When looking up a namespace-name in a using-directive or alias
9919        definition, only namespace names are considered.
9920
9921      And:
9922
9923        [basic.lookup.qual]
9924
9925        During the lookup of a name preceding the :: scope resolution
9926        operator, object, function, and enumerator names are ignored.
9927
9928      (Note that cp_parser_class_or_namespace_name only calls this
9929      function if the token after the name is the scope resolution
9930      operator.)  */
9931   namespace_decl = cp_parser_lookup_name (parser, identifier,
9932                                           /*is_type=*/false,
9933                                           /*is_template=*/false,
9934                                           /*is_namespace=*/true,
9935                                           /*check_dependency=*/true,
9936                                           /*ambiguous_p=*/NULL);
9937   /* If it's not a namespace, issue an error.  */
9938   if (namespace_decl == error_mark_node
9939       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9940     {
9941       cp_parser_error (parser, "expected namespace-name");
9942       namespace_decl = error_mark_node;
9943     }
9944
9945   return namespace_decl;
9946 }
9947
9948 /* Parse a namespace-definition.
9949
9950    namespace-definition:
9951      named-namespace-definition
9952      unnamed-namespace-definition
9953
9954    named-namespace-definition:
9955      original-namespace-definition
9956      extension-namespace-definition
9957
9958    original-namespace-definition:
9959      namespace identifier { namespace-body }
9960
9961    extension-namespace-definition:
9962      namespace original-namespace-name { namespace-body }
9963
9964    unnamed-namespace-definition:
9965      namespace { namespace-body } */
9966
9967 static void
9968 cp_parser_namespace_definition (cp_parser* parser)
9969 {
9970   tree identifier;
9971
9972   /* Look for the `namespace' keyword.  */
9973   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9974
9975   /* Get the name of the namespace.  We do not attempt to distinguish
9976      between an original-namespace-definition and an
9977      extension-namespace-definition at this point.  The semantic
9978      analysis routines are responsible for that.  */
9979   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9980     identifier = cp_parser_identifier (parser);
9981   else
9982     identifier = NULL_TREE;
9983
9984   /* Look for the `{' to start the namespace.  */
9985   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9986   /* Start the namespace.  */
9987   push_namespace (identifier);
9988   /* Parse the body of the namespace.  */
9989   cp_parser_namespace_body (parser);
9990   /* Finish the namespace.  */
9991   pop_namespace ();
9992   /* Look for the final `}'.  */
9993   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9994 }
9995
9996 /* Parse a namespace-body.
9997
9998    namespace-body:
9999      declaration-seq [opt]  */
10000
10001 static void
10002 cp_parser_namespace_body (cp_parser* parser)
10003 {
10004   cp_parser_declaration_seq_opt (parser);
10005 }
10006
10007 /* Parse a namespace-alias-definition.
10008
10009    namespace-alias-definition:
10010      namespace identifier = qualified-namespace-specifier ;  */
10011
10012 static void
10013 cp_parser_namespace_alias_definition (cp_parser* parser)
10014 {
10015   tree identifier;
10016   tree namespace_specifier;
10017
10018   /* Look for the `namespace' keyword.  */
10019   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10020   /* Look for the identifier.  */
10021   identifier = cp_parser_identifier (parser);
10022   if (identifier == error_mark_node)
10023     return;
10024   /* Look for the `=' token.  */
10025   cp_parser_require (parser, CPP_EQ, "`='");
10026   /* Look for the qualified-namespace-specifier.  */
10027   namespace_specifier
10028     = cp_parser_qualified_namespace_specifier (parser);
10029   /* Look for the `;' token.  */
10030   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10031
10032   /* Register the alias in the symbol table.  */
10033   do_namespace_alias (identifier, namespace_specifier);
10034 }
10035
10036 /* Parse a qualified-namespace-specifier.
10037
10038    qualified-namespace-specifier:
10039      :: [opt] nested-name-specifier [opt] namespace-name
10040
10041    Returns a NAMESPACE_DECL corresponding to the specified
10042    namespace.  */
10043
10044 static tree
10045 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10046 {
10047   /* Look for the optional `::'.  */
10048   cp_parser_global_scope_opt (parser,
10049                               /*current_scope_valid_p=*/false);
10050
10051   /* Look for the optional nested-name-specifier.  */
10052   cp_parser_nested_name_specifier_opt (parser,
10053                                        /*typename_keyword_p=*/false,
10054                                        /*check_dependency_p=*/true,
10055                                        /*type_p=*/false,
10056                                        /*is_declaration=*/true);
10057
10058   return cp_parser_namespace_name (parser);
10059 }
10060
10061 /* Parse a using-declaration.
10062
10063    using-declaration:
10064      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10065      using :: unqualified-id ;  */
10066
10067 static void
10068 cp_parser_using_declaration (cp_parser* parser)
10069 {
10070   cp_token *token;
10071   bool typename_p = false;
10072   bool global_scope_p;
10073   tree decl;
10074   tree identifier;
10075   tree qscope;
10076
10077   /* Look for the `using' keyword.  */
10078   cp_parser_require_keyword (parser, RID_USING, "`using'");
10079
10080   /* Peek at the next token.  */
10081   token = cp_lexer_peek_token (parser->lexer);
10082   /* See if it's `typename'.  */
10083   if (token->keyword == RID_TYPENAME)
10084     {
10085       /* Remember that we've seen it.  */
10086       typename_p = true;
10087       /* Consume the `typename' token.  */
10088       cp_lexer_consume_token (parser->lexer);
10089     }
10090
10091   /* Look for the optional global scope qualification.  */
10092   global_scope_p
10093     = (cp_parser_global_scope_opt (parser,
10094                                    /*current_scope_valid_p=*/false)
10095        != NULL_TREE);
10096
10097   /* If we saw `typename', or didn't see `::', then there must be a
10098      nested-name-specifier present.  */
10099   if (typename_p || !global_scope_p)
10100     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10101                                               /*check_dependency_p=*/true,
10102                                               /*type_p=*/false,
10103                                               /*is_declaration=*/true);
10104   /* Otherwise, we could be in either of the two productions.  In that
10105      case, treat the nested-name-specifier as optional.  */
10106   else
10107     qscope = cp_parser_nested_name_specifier_opt (parser,
10108                                                   /*typename_keyword_p=*/false,
10109                                                   /*check_dependency_p=*/true,
10110                                                   /*type_p=*/false,
10111                                                   /*is_declaration=*/true);
10112   if (!qscope)
10113     qscope = global_namespace;
10114
10115   /* Parse the unqualified-id.  */
10116   identifier = cp_parser_unqualified_id (parser,
10117                                          /*template_keyword_p=*/false,
10118                                          /*check_dependency_p=*/true,
10119                                          /*declarator_p=*/true);
10120
10121   /* The function we call to handle a using-declaration is different
10122      depending on what scope we are in.  */
10123   if (identifier == error_mark_node)
10124     ;
10125   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10126            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10127     /* [namespace.udecl]
10128
10129        A using declaration shall not name a template-id.  */
10130     error ("a template-id may not appear in a using-declaration");
10131   else
10132     {
10133       if (at_class_scope_p ())
10134         {
10135           /* Create the USING_DECL.  */
10136           decl = do_class_using_decl (build_nt (SCOPE_REF,
10137                                                 parser->scope,
10138                                                 identifier));
10139           /* Add it to the list of members in this class.  */
10140           finish_member_declaration (decl);
10141         }
10142       else
10143         {
10144           decl = cp_parser_lookup_name_simple (parser, identifier);
10145           if (decl == error_mark_node)
10146             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10147           else if (!at_namespace_scope_p ())
10148             do_local_using_decl (decl, qscope, identifier);
10149           else
10150             do_toplevel_using_decl (decl, qscope, identifier);
10151         }
10152     }
10153
10154   /* Look for the final `;'.  */
10155   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10156 }
10157
10158 /* Parse a using-directive.
10159
10160    using-directive:
10161      using namespace :: [opt] nested-name-specifier [opt]
10162        namespace-name ;  */
10163
10164 static void
10165 cp_parser_using_directive (cp_parser* parser)
10166 {
10167   tree namespace_decl;
10168   tree attribs;
10169
10170   /* Look for the `using' keyword.  */
10171   cp_parser_require_keyword (parser, RID_USING, "`using'");
10172   /* And the `namespace' keyword.  */
10173   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10174   /* Look for the optional `::' operator.  */
10175   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10176   /* And the optional nested-name-specifier.  */
10177   cp_parser_nested_name_specifier_opt (parser,
10178                                        /*typename_keyword_p=*/false,
10179                                        /*check_dependency_p=*/true,
10180                                        /*type_p=*/false,
10181                                        /*is_declaration=*/true);
10182   /* Get the namespace being used.  */
10183   namespace_decl = cp_parser_namespace_name (parser);
10184   /* And any specified attributes.  */
10185   attribs = cp_parser_attributes_opt (parser);
10186   /* Update the symbol table.  */
10187   parse_using_directive (namespace_decl, attribs);
10188   /* Look for the final `;'.  */
10189   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10190 }
10191
10192 /* Parse an asm-definition.
10193
10194    asm-definition:
10195      asm ( string-literal ) ;
10196
10197    GNU Extension:
10198
10199    asm-definition:
10200      asm volatile [opt] ( string-literal ) ;
10201      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10202      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10203                           : asm-operand-list [opt] ) ;
10204      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10205                           : asm-operand-list [opt]
10206                           : asm-operand-list [opt] ) ;  */
10207
10208 static void
10209 cp_parser_asm_definition (cp_parser* parser)
10210 {
10211   tree string;
10212   tree outputs = NULL_TREE;
10213   tree inputs = NULL_TREE;
10214   tree clobbers = NULL_TREE;
10215   tree asm_stmt;
10216   bool volatile_p = false;
10217   bool extended_p = false;
10218
10219   /* Look for the `asm' keyword.  */
10220   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10221   /* See if the next token is `volatile'.  */
10222   if (cp_parser_allow_gnu_extensions_p (parser)
10223       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10224     {
10225       /* Remember that we saw the `volatile' keyword.  */
10226       volatile_p = true;
10227       /* Consume the token.  */
10228       cp_lexer_consume_token (parser->lexer);
10229     }
10230   /* Look for the opening `('.  */
10231   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10232     return;
10233   /* Look for the string.  */
10234   string = cp_parser_string_literal (parser, false, false);
10235   if (string == error_mark_node)
10236     {
10237       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10238                                              /*consume_paren=*/true);
10239       return;
10240     }
10241
10242   /* If we're allowing GNU extensions, check for the extended assembly
10243      syntax.  Unfortunately, the `:' tokens need not be separated by
10244      a space in C, and so, for compatibility, we tolerate that here
10245      too.  Doing that means that we have to treat the `::' operator as
10246      two `:' tokens.  */
10247   if (cp_parser_allow_gnu_extensions_p (parser)
10248       && at_function_scope_p ()
10249       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10250           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10251     {
10252       bool inputs_p = false;
10253       bool clobbers_p = false;
10254
10255       /* The extended syntax was used.  */
10256       extended_p = true;
10257
10258       /* Look for outputs.  */
10259       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10260         {
10261           /* Consume the `:'.  */
10262           cp_lexer_consume_token (parser->lexer);
10263           /* Parse the output-operands.  */
10264           if (cp_lexer_next_token_is_not (parser->lexer,
10265                                           CPP_COLON)
10266               && cp_lexer_next_token_is_not (parser->lexer,
10267                                              CPP_SCOPE)
10268               && cp_lexer_next_token_is_not (parser->lexer,
10269                                              CPP_CLOSE_PAREN))
10270             outputs = cp_parser_asm_operand_list (parser);
10271         }
10272       /* If the next token is `::', there are no outputs, and the
10273          next token is the beginning of the inputs.  */
10274       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10275         /* The inputs are coming next.  */
10276         inputs_p = true;
10277
10278       /* Look for inputs.  */
10279       if (inputs_p
10280           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10281         {
10282           /* Consume the `:' or `::'.  */
10283           cp_lexer_consume_token (parser->lexer);
10284           /* Parse the output-operands.  */
10285           if (cp_lexer_next_token_is_not (parser->lexer,
10286                                           CPP_COLON)
10287               && cp_lexer_next_token_is_not (parser->lexer,
10288                                              CPP_CLOSE_PAREN))
10289             inputs = cp_parser_asm_operand_list (parser);
10290         }
10291       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10292         /* The clobbers are coming next.  */
10293         clobbers_p = true;
10294
10295       /* Look for clobbers.  */
10296       if (clobbers_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 clobbers.  */
10302           if (cp_lexer_next_token_is_not (parser->lexer,
10303                                           CPP_CLOSE_PAREN))
10304             clobbers = cp_parser_asm_clobber_list (parser);
10305         }
10306     }
10307   /* Look for the closing `)'.  */
10308   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10309     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10310                                            /*consume_paren=*/true);
10311   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10312
10313   /* Create the ASM_EXPR.  */
10314   if (at_function_scope_p ())
10315     {
10316       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10317                                   inputs, clobbers);
10318       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10319       if (!extended_p)
10320         {
10321           tree temp = asm_stmt;
10322           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10323             temp = TREE_OPERAND (temp, 0);
10324           
10325           ASM_INPUT_P (temp) = 1;
10326         }
10327     }
10328   else
10329     assemble_asm (string);
10330 }
10331
10332 /* Declarators [gram.dcl.decl] */
10333
10334 /* Parse an init-declarator.
10335
10336    init-declarator:
10337      declarator initializer [opt]
10338
10339    GNU Extension:
10340
10341    init-declarator:
10342      declarator asm-specification [opt] attributes [opt] initializer [opt]
10343
10344    function-definition:
10345      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10346        function-body
10347      decl-specifier-seq [opt] declarator function-try-block
10348
10349    GNU Extension:
10350
10351    function-definition:
10352      __extension__ function-definition
10353
10354    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10355    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10356    then this declarator appears in a class scope.  The new DECL created
10357    by this declarator is returned.
10358
10359    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10360    for a function-definition here as well.  If the declarator is a
10361    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10362    be TRUE upon return.  By that point, the function-definition will
10363    have been completely parsed.
10364
10365    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10366    is FALSE.  */
10367
10368 static tree
10369 cp_parser_init_declarator (cp_parser* parser,
10370                            cp_decl_specifier_seq *decl_specifiers,
10371                            bool function_definition_allowed_p,
10372                            bool member_p,
10373                            int declares_class_or_enum,
10374                            bool* function_definition_p)
10375 {
10376   cp_token *token;
10377   cp_declarator *declarator;
10378   tree prefix_attributes;
10379   tree attributes;
10380   tree asm_specification;
10381   tree initializer;
10382   tree decl = NULL_TREE;
10383   tree scope;
10384   bool is_initialized;
10385   bool is_parenthesized_init;
10386   bool is_non_constant_init;
10387   int ctor_dtor_or_conv_p;
10388   bool friend_p;
10389   bool pop_p = false;
10390
10391   /* Gather the attributes that were provided with the
10392      decl-specifiers.  */
10393   prefix_attributes = decl_specifiers->attributes;
10394
10395   /* Assume that this is not the declarator for a function
10396      definition.  */
10397   if (function_definition_p)
10398     *function_definition_p = false;
10399
10400   /* Defer access checks while parsing the declarator; we cannot know
10401      what names are accessible until we know what is being
10402      declared.  */
10403   resume_deferring_access_checks ();
10404
10405   /* Parse the declarator.  */
10406   declarator
10407     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10408                             &ctor_dtor_or_conv_p,
10409                             /*parenthesized_p=*/NULL,
10410                             /*member_p=*/false);
10411   /* Gather up the deferred checks.  */
10412   stop_deferring_access_checks ();
10413
10414   /* If the DECLARATOR was erroneous, there's no need to go
10415      further.  */
10416   if (declarator == cp_error_declarator)
10417     return error_mark_node;
10418
10419   cp_parser_check_for_definition_in_return_type (declarator,
10420                                                  declares_class_or_enum);
10421
10422   /* Figure out what scope the entity declared by the DECLARATOR is
10423      located in.  `grokdeclarator' sometimes changes the scope, so
10424      we compute it now.  */
10425   scope = get_scope_of_declarator (declarator);
10426
10427   /* If we're allowing GNU extensions, look for an asm-specification
10428      and attributes.  */
10429   if (cp_parser_allow_gnu_extensions_p (parser))
10430     {
10431       /* Look for an asm-specification.  */
10432       asm_specification = cp_parser_asm_specification_opt (parser);
10433       /* And attributes.  */
10434       attributes = cp_parser_attributes_opt (parser);
10435     }
10436   else
10437     {
10438       asm_specification = NULL_TREE;
10439       attributes = NULL_TREE;
10440     }
10441
10442   /* Peek at the next token.  */
10443   token = cp_lexer_peek_token (parser->lexer);
10444   /* Check to see if the token indicates the start of a
10445      function-definition.  */
10446   if (cp_parser_token_starts_function_definition_p (token))
10447     {
10448       if (!function_definition_allowed_p)
10449         {
10450           /* If a function-definition should not appear here, issue an
10451              error message.  */
10452           cp_parser_error (parser,
10453                            "a function-definition is not allowed here");
10454           return error_mark_node;
10455         }
10456       else
10457         {
10458           /* Neither attributes nor an asm-specification are allowed
10459              on a function-definition.  */
10460           if (asm_specification)
10461             error ("an asm-specification is not allowed on a function-definition");
10462           if (attributes)
10463             error ("attributes are not allowed on a function-definition");
10464           /* This is a function-definition.  */
10465           *function_definition_p = true;
10466
10467           /* Parse the function definition.  */
10468           if (member_p)
10469             decl = cp_parser_save_member_function_body (parser,
10470                                                         decl_specifiers,
10471                                                         declarator,
10472                                                         prefix_attributes);
10473           else
10474             decl
10475               = (cp_parser_function_definition_from_specifiers_and_declarator
10476                  (parser, decl_specifiers, prefix_attributes, declarator));
10477
10478           return decl;
10479         }
10480     }
10481
10482   /* [dcl.dcl]
10483
10484      Only in function declarations for constructors, destructors, and
10485      type conversions can the decl-specifier-seq be omitted.
10486
10487      We explicitly postpone this check past the point where we handle
10488      function-definitions because we tolerate function-definitions
10489      that are missing their return types in some modes.  */
10490   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10491     {
10492       cp_parser_error (parser,
10493                        "expected constructor, destructor, or type conversion");
10494       return error_mark_node;
10495     }
10496
10497   /* An `=' or an `(' indicates an initializer.  */
10498   is_initialized = (token->type == CPP_EQ
10499                      || token->type == CPP_OPEN_PAREN);
10500   /* If the init-declarator isn't initialized and isn't followed by a
10501      `,' or `;', it's not a valid init-declarator.  */
10502   if (!is_initialized
10503       && token->type != CPP_COMMA
10504       && token->type != CPP_SEMICOLON)
10505     {
10506       cp_parser_error (parser, "expected initializer");
10507       return error_mark_node;
10508     }
10509
10510   /* Because start_decl has side-effects, we should only call it if we
10511      know we're going ahead.  By this point, we know that we cannot
10512      possibly be looking at any other construct.  */
10513   cp_parser_commit_to_tentative_parse (parser);
10514
10515   /* If the decl specifiers were bad, issue an error now that we're
10516      sure this was intended to be a declarator.  Then continue
10517      declaring the variable(s), as int, to try to cut down on further
10518      errors.  */
10519   if (decl_specifiers->any_specifiers_p
10520       && decl_specifiers->type == error_mark_node)
10521     {
10522       cp_parser_error (parser, "invalid type in declaration");
10523       decl_specifiers->type = integer_type_node;
10524     }
10525
10526   /* Check to see whether or not this declaration is a friend.  */
10527   friend_p = cp_parser_friend_p (decl_specifiers);
10528
10529   /* Check that the number of template-parameter-lists is OK.  */
10530   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10531     return error_mark_node;
10532
10533   /* Enter the newly declared entry in the symbol table.  If we're
10534      processing a declaration in a class-specifier, we wait until
10535      after processing the initializer.  */
10536   if (!member_p)
10537     {
10538       if (parser->in_unbraced_linkage_specification_p)
10539         {
10540           decl_specifiers->storage_class = sc_extern;
10541           have_extern_spec = false;
10542         }
10543       decl = start_decl (declarator, decl_specifiers,
10544                          is_initialized, attributes, prefix_attributes,
10545                          &pop_p);
10546     }
10547   else if (scope)
10548     /* Enter the SCOPE.  That way unqualified names appearing in the
10549        initializer will be looked up in SCOPE.  */
10550     pop_p = push_scope (scope);
10551
10552   /* Perform deferred access control checks, now that we know in which
10553      SCOPE the declared entity resides.  */
10554   if (!member_p && decl)
10555     {
10556       tree saved_current_function_decl = NULL_TREE;
10557
10558       /* If the entity being declared is a function, pretend that we
10559          are in its scope.  If it is a `friend', it may have access to
10560          things that would not otherwise be accessible.  */
10561       if (TREE_CODE (decl) == FUNCTION_DECL)
10562         {
10563           saved_current_function_decl = current_function_decl;
10564           current_function_decl = decl;
10565         }
10566
10567       /* Perform the access control checks for the declarator and the
10568          the decl-specifiers.  */
10569       perform_deferred_access_checks ();
10570
10571       /* Restore the saved value.  */
10572       if (TREE_CODE (decl) == FUNCTION_DECL)
10573         current_function_decl = saved_current_function_decl;
10574     }
10575
10576   /* Parse the initializer.  */
10577   if (is_initialized)
10578     initializer = cp_parser_initializer (parser,
10579                                          &is_parenthesized_init,
10580                                          &is_non_constant_init);
10581   else
10582     {
10583       initializer = NULL_TREE;
10584       is_parenthesized_init = false;
10585       is_non_constant_init = true;
10586     }
10587
10588   /* The old parser allows attributes to appear after a parenthesized
10589      initializer.  Mark Mitchell proposed removing this functionality
10590      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10591      attributes -- but ignores them.  */
10592   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10593     if (cp_parser_attributes_opt (parser))
10594       warning ("attributes after parenthesized initializer ignored");
10595
10596   /* For an in-class declaration, use `grokfield' to create the
10597      declaration.  */
10598   if (member_p)
10599     {
10600       if (pop_p)
10601         {
10602           pop_scope (scope);
10603           pop_p = false;
10604         }
10605       decl = grokfield (declarator, decl_specifiers,
10606                         initializer, /*asmspec=*/NULL_TREE,
10607                         /*attributes=*/NULL_TREE);
10608       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10609         cp_parser_save_default_args (parser, decl);
10610     }
10611
10612   /* Finish processing the declaration.  But, skip friend
10613      declarations.  */
10614   if (!friend_p && decl && decl != error_mark_node)
10615     {
10616       cp_finish_decl (decl,
10617                       initializer,
10618                       asm_specification,
10619                       /* If the initializer is in parentheses, then this is
10620                          a direct-initialization, which means that an
10621                          `explicit' constructor is OK.  Otherwise, an
10622                          `explicit' constructor cannot be used.  */
10623                       ((is_parenthesized_init || !is_initialized)
10624                      ? 0 : LOOKUP_ONLYCONVERTING));
10625       if (pop_p)
10626         pop_scope (DECL_CONTEXT (decl));
10627     }
10628
10629   /* Remember whether or not variables were initialized by
10630      constant-expressions.  */
10631   if (decl && TREE_CODE (decl) == VAR_DECL
10632       && is_initialized && !is_non_constant_init)
10633     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10634
10635   return decl;
10636 }
10637
10638 /* Parse a declarator.
10639
10640    declarator:
10641      direct-declarator
10642      ptr-operator declarator
10643
10644    abstract-declarator:
10645      ptr-operator abstract-declarator [opt]
10646      direct-abstract-declarator
10647
10648    GNU Extensions:
10649
10650    declarator:
10651      attributes [opt] direct-declarator
10652      attributes [opt] ptr-operator declarator
10653
10654    abstract-declarator:
10655      attributes [opt] ptr-operator abstract-declarator [opt]
10656      attributes [opt] direct-abstract-declarator
10657
10658    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10659    detect constructor, destructor or conversion operators. It is set
10660    to -1 if the declarator is a name, and +1 if it is a
10661    function. Otherwise it is set to zero. Usually you just want to
10662    test for >0, but internally the negative value is used.
10663
10664    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10665    a decl-specifier-seq unless it declares a constructor, destructor,
10666    or conversion.  It might seem that we could check this condition in
10667    semantic analysis, rather than parsing, but that makes it difficult
10668    to handle something like `f()'.  We want to notice that there are
10669    no decl-specifiers, and therefore realize that this is an
10670    expression, not a declaration.)
10671
10672    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10673    the declarator is a direct-declarator of the form "(...)".  
10674
10675    MEMBER_P is true iff this declarator is a member-declarator.  */
10676
10677 static cp_declarator *
10678 cp_parser_declarator (cp_parser* parser,
10679                       cp_parser_declarator_kind dcl_kind,
10680                       int* ctor_dtor_or_conv_p,
10681                       bool* parenthesized_p,
10682                       bool member_p)
10683 {
10684   cp_token *token;
10685   cp_declarator *declarator;
10686   enum tree_code code;
10687   cp_cv_quals cv_quals;
10688   tree class_type;
10689   tree attributes = NULL_TREE;
10690
10691   /* Assume this is not a constructor, destructor, or type-conversion
10692      operator.  */
10693   if (ctor_dtor_or_conv_p)
10694     *ctor_dtor_or_conv_p = 0;
10695
10696   if (cp_parser_allow_gnu_extensions_p (parser))
10697     attributes = cp_parser_attributes_opt (parser);
10698
10699   /* Peek at the next token.  */
10700   token = cp_lexer_peek_token (parser->lexer);
10701
10702   /* Check for the ptr-operator production.  */
10703   cp_parser_parse_tentatively (parser);
10704   /* Parse the ptr-operator.  */
10705   code = cp_parser_ptr_operator (parser,
10706                                  &class_type,
10707                                  &cv_quals);
10708   /* If that worked, then we have a ptr-operator.  */
10709   if (cp_parser_parse_definitely (parser))
10710     {
10711       /* If a ptr-operator was found, then this declarator was not
10712          parenthesized.  */
10713       if (parenthesized_p)
10714         *parenthesized_p = true;
10715       /* The dependent declarator is optional if we are parsing an
10716          abstract-declarator.  */
10717       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10718         cp_parser_parse_tentatively (parser);
10719
10720       /* Parse the dependent declarator.  */
10721       declarator = cp_parser_declarator (parser, dcl_kind,
10722                                          /*ctor_dtor_or_conv_p=*/NULL,
10723                                          /*parenthesized_p=*/NULL,
10724                                          /*member_p=*/false);
10725
10726       /* If we are parsing an abstract-declarator, we must handle the
10727          case where the dependent declarator is absent.  */
10728       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10729           && !cp_parser_parse_definitely (parser))
10730         declarator = NULL;
10731
10732       /* Build the representation of the ptr-operator.  */
10733       if (class_type)
10734         declarator = make_ptrmem_declarator (cv_quals,
10735                                              class_type,
10736                                              declarator);
10737       else if (code == INDIRECT_REF)
10738         declarator = make_pointer_declarator (cv_quals, declarator);
10739       else
10740         declarator = make_reference_declarator (cv_quals, declarator);
10741     }
10742   /* Everything else is a direct-declarator.  */
10743   else
10744     {
10745       if (parenthesized_p)
10746         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10747                                                    CPP_OPEN_PAREN);
10748       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10749                                                 ctor_dtor_or_conv_p,
10750                                                 member_p);
10751     }
10752
10753   if (attributes && declarator != cp_error_declarator)
10754     declarator->attributes = attributes;
10755
10756   return declarator;
10757 }
10758
10759 /* Parse a direct-declarator or direct-abstract-declarator.
10760
10761    direct-declarator:
10762      declarator-id
10763      direct-declarator ( parameter-declaration-clause )
10764        cv-qualifier-seq [opt]
10765        exception-specification [opt]
10766      direct-declarator [ constant-expression [opt] ]
10767      ( declarator )
10768
10769    direct-abstract-declarator:
10770      direct-abstract-declarator [opt]
10771        ( parameter-declaration-clause )
10772        cv-qualifier-seq [opt]
10773        exception-specification [opt]
10774      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10775      ( abstract-declarator )
10776
10777    Returns a representation of the declarator.  DCL_KIND is
10778    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10779    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10780    we are parsing a direct-declarator.  It is
10781    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10782    of ambiguity we prefer an abstract declarator, as per
10783    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10784    cp_parser_declarator.  */
10785
10786 static cp_declarator *
10787 cp_parser_direct_declarator (cp_parser* parser,
10788                              cp_parser_declarator_kind dcl_kind,
10789                              int* ctor_dtor_or_conv_p,
10790                              bool member_p)
10791 {
10792   cp_token *token;
10793   cp_declarator *declarator = NULL;
10794   tree scope = NULL_TREE;
10795   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10796   bool saved_in_declarator_p = parser->in_declarator_p;
10797   bool first = true;
10798   bool pop_p = false;
10799
10800   while (true)
10801     {
10802       /* Peek at the next token.  */
10803       token = cp_lexer_peek_token (parser->lexer);
10804       if (token->type == CPP_OPEN_PAREN)
10805         {
10806           /* This is either a parameter-declaration-clause, or a
10807              parenthesized declarator. When we know we are parsing a
10808              named declarator, it must be a parenthesized declarator
10809              if FIRST is true. For instance, `(int)' is a
10810              parameter-declaration-clause, with an omitted
10811              direct-abstract-declarator. But `((*))', is a
10812              parenthesized abstract declarator. Finally, when T is a
10813              template parameter `(T)' is a
10814              parameter-declaration-clause, and not a parenthesized
10815              named declarator.
10816
10817              We first try and parse a parameter-declaration-clause,
10818              and then try a nested declarator (if FIRST is true).
10819
10820              It is not an error for it not to be a
10821              parameter-declaration-clause, even when FIRST is
10822              false. Consider,
10823
10824                int i (int);
10825                int i (3);
10826
10827              The first is the declaration of a function while the
10828              second is a the definition of a variable, including its
10829              initializer.
10830
10831              Having seen only the parenthesis, we cannot know which of
10832              these two alternatives should be selected.  Even more
10833              complex are examples like:
10834
10835                int i (int (a));
10836                int i (int (3));
10837
10838              The former is a function-declaration; the latter is a
10839              variable initialization.
10840
10841              Thus again, we try a parameter-declaration-clause, and if
10842              that fails, we back out and return.  */
10843
10844           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10845             {
10846               cp_parameter_declarator *params;
10847               unsigned saved_num_template_parameter_lists;
10848
10849               /* In a member-declarator, the only valid interpretation
10850                  of a parenthesis is the start of a
10851                  parameter-declaration-clause.  (It is invalid to
10852                  initialize a static data member with a parenthesized
10853                  initializer; only the "=" form of initialization is
10854                  permitted.)  */
10855               if (!member_p)
10856                 cp_parser_parse_tentatively (parser);
10857
10858               /* Consume the `('.  */
10859               cp_lexer_consume_token (parser->lexer);
10860               if (first)
10861                 {
10862                   /* If this is going to be an abstract declarator, we're
10863                      in a declarator and we can't have default args.  */
10864                   parser->default_arg_ok_p = false;
10865                   parser->in_declarator_p = true;
10866                 }
10867
10868               /* Inside the function parameter list, surrounding
10869                  template-parameter-lists do not apply.  */
10870               saved_num_template_parameter_lists
10871                 = parser->num_template_parameter_lists;
10872               parser->num_template_parameter_lists = 0;
10873
10874               /* Parse the parameter-declaration-clause.  */
10875               params = cp_parser_parameter_declaration_clause (parser);
10876
10877               parser->num_template_parameter_lists
10878                 = saved_num_template_parameter_lists;
10879
10880               /* If all went well, parse the cv-qualifier-seq and the
10881                  exception-specification.  */
10882               if (member_p || cp_parser_parse_definitely (parser))
10883                 {
10884                   cp_cv_quals cv_quals;
10885                   tree exception_specification;
10886
10887                   if (ctor_dtor_or_conv_p)
10888                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10889                   first = false;
10890                   /* Consume the `)'.  */
10891                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10892
10893                   /* Parse the cv-qualifier-seq.  */
10894                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10895                   /* And the exception-specification.  */
10896                   exception_specification
10897                     = cp_parser_exception_specification_opt (parser);
10898
10899                   /* Create the function-declarator.  */
10900                   declarator = make_call_declarator (declarator,
10901                                                      params,
10902                                                      cv_quals,
10903                                                      exception_specification);
10904                   /* Any subsequent parameter lists are to do with
10905                      return type, so are not those of the declared
10906                      function.  */
10907                   parser->default_arg_ok_p = false;
10908
10909                   /* Repeat the main loop.  */
10910                   continue;
10911                 }
10912             }
10913
10914           /* If this is the first, we can try a parenthesized
10915              declarator.  */
10916           if (first)
10917             {
10918               bool saved_in_type_id_in_expr_p;
10919
10920               parser->default_arg_ok_p = saved_default_arg_ok_p;
10921               parser->in_declarator_p = saved_in_declarator_p;
10922
10923               /* Consume the `('.  */
10924               cp_lexer_consume_token (parser->lexer);
10925               /* Parse the nested declarator.  */
10926               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10927               parser->in_type_id_in_expr_p = true;
10928               declarator
10929                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10930                                         /*parenthesized_p=*/NULL,
10931                                         member_p);
10932               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10933               first = false;
10934               /* Expect a `)'.  */
10935               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10936                 declarator = cp_error_declarator;
10937               if (declarator == cp_error_declarator)
10938                 break;
10939
10940               goto handle_declarator;
10941             }
10942           /* Otherwise, we must be done.  */
10943           else
10944             break;
10945         }
10946       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10947                && token->type == CPP_OPEN_SQUARE)
10948         {
10949           /* Parse an array-declarator.  */
10950           tree bounds;
10951
10952           if (ctor_dtor_or_conv_p)
10953             *ctor_dtor_or_conv_p = 0;
10954
10955           first = false;
10956           parser->default_arg_ok_p = false;
10957           parser->in_declarator_p = true;
10958           /* Consume the `['.  */
10959           cp_lexer_consume_token (parser->lexer);
10960           /* Peek at the next token.  */
10961           token = cp_lexer_peek_token (parser->lexer);
10962           /* If the next token is `]', then there is no
10963              constant-expression.  */
10964           if (token->type != CPP_CLOSE_SQUARE)
10965             {
10966               bool non_constant_p;
10967
10968               bounds
10969                 = cp_parser_constant_expression (parser,
10970                                                  /*allow_non_constant=*/true,
10971                                                  &non_constant_p);
10972               if (!non_constant_p)
10973                 bounds = fold_non_dependent_expr (bounds);
10974               else if (!at_function_scope_p ())
10975                 {
10976                   error ("array bound is not an integer constant");
10977                   bounds = error_mark_node;
10978                 }
10979             }
10980           else
10981             bounds = NULL_TREE;
10982           /* Look for the closing `]'.  */
10983           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10984             {
10985               declarator = cp_error_declarator;
10986               break;
10987             }
10988
10989           declarator = make_array_declarator (declarator, bounds);
10990         }
10991       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10992         {
10993           tree id;
10994
10995           /* Parse a declarator-id */
10996           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10997             cp_parser_parse_tentatively (parser);
10998           id = cp_parser_declarator_id (parser);
10999           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11000             {
11001               if (!cp_parser_parse_definitely (parser))
11002                 id = error_mark_node;
11003               else if (TREE_CODE (id) != IDENTIFIER_NODE)
11004                 {
11005                   cp_parser_error (parser, "expected unqualified-id");
11006                   id = error_mark_node;
11007                 }
11008             }
11009
11010           if (id == error_mark_node)
11011             {
11012               declarator = cp_error_declarator;
11013               break;
11014             }
11015
11016           if (TREE_CODE (id) == SCOPE_REF && at_namespace_scope_p ())
11017             {
11018               tree scope = TREE_OPERAND (id, 0);
11019
11020               /* In the declaration of a member of a template class
11021                  outside of the class itself, the SCOPE will sometimes
11022                  be a TYPENAME_TYPE.  For example, given:
11023
11024                  template <typename T>
11025                  int S<T>::R::i = 3;
11026
11027                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11028                  this context, we must resolve S<T>::R to an ordinary
11029                  type, rather than a typename type.
11030
11031                  The reason we normally avoid resolving TYPENAME_TYPEs
11032                  is that a specialization of `S' might render
11033                  `S<T>::R' not a type.  However, if `S' is
11034                  specialized, then this `i' will not be used, so there
11035                  is no harm in resolving the types here.  */
11036               if (TREE_CODE (scope) == TYPENAME_TYPE)
11037                 {
11038                   tree type;
11039
11040                   /* Resolve the TYPENAME_TYPE.  */
11041                   type = resolve_typename_type (scope,
11042                                                  /*only_current_p=*/false);
11043                   /* If that failed, the declarator is invalid.  */
11044                   if (type == error_mark_node)
11045                     error ("%<%T::%D%> is not a type",
11046                            TYPE_CONTEXT (scope),
11047                            TYPE_IDENTIFIER (scope));
11048                   /* Build a new DECLARATOR.  */
11049                   id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11050                 }
11051             }
11052
11053           declarator = make_id_declarator (id);
11054           if (id)
11055             {
11056               tree class_type;
11057               tree unqualified_name;
11058
11059               if (TREE_CODE (id) == SCOPE_REF
11060                   && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11061                 {
11062                   class_type = TREE_OPERAND (id, 0);
11063                   unqualified_name = TREE_OPERAND (id, 1);
11064                 }
11065               else
11066                 {
11067                   class_type = current_class_type;
11068                   unqualified_name = id;
11069                 }
11070
11071               if (class_type)
11072                 {
11073                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11074                     declarator->u.id.sfk = sfk_destructor;
11075                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11076                     declarator->u.id.sfk = sfk_conversion;
11077                   else if (constructor_name_p (unqualified_name,
11078                                                class_type)
11079                            || (TREE_CODE (unqualified_name) == TYPE_DECL
11080                                && same_type_p (TREE_TYPE (unqualified_name),
11081                                                class_type)))
11082                     declarator->u.id.sfk = sfk_constructor;
11083
11084                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11085                     *ctor_dtor_or_conv_p = -1;
11086                   if (TREE_CODE (id) == SCOPE_REF
11087                       && TREE_CODE (unqualified_name) == TYPE_DECL
11088                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11089                     {
11090                       error ("invalid use of constructor as a template");
11091                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11092                               "the constructor in a qualified name",
11093                               class_type,
11094                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11095                               class_type, class_type);
11096                     }
11097                 }
11098             }
11099
11100         handle_declarator:;
11101           scope = get_scope_of_declarator (declarator);
11102           if (scope)
11103             /* Any names that appear after the declarator-id for a
11104                member are looked up in the containing scope.  */
11105             pop_p = push_scope (scope);
11106           parser->in_declarator_p = true;
11107           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11108               || (declarator && declarator->kind == cdk_id))
11109             /* Default args are only allowed on function
11110                declarations.  */
11111             parser->default_arg_ok_p = saved_default_arg_ok_p;
11112           else
11113             parser->default_arg_ok_p = false;
11114
11115           first = false;
11116         }
11117       /* We're done.  */
11118       else
11119         break;
11120     }
11121
11122   /* For an abstract declarator, we might wind up with nothing at this
11123      point.  That's an error; the declarator is not optional.  */
11124   if (!declarator)
11125     cp_parser_error (parser, "expected declarator");
11126
11127   /* If we entered a scope, we must exit it now.  */
11128   if (pop_p)
11129     pop_scope (scope);
11130
11131   parser->default_arg_ok_p = saved_default_arg_ok_p;
11132   parser->in_declarator_p = saved_in_declarator_p;
11133
11134   return declarator;
11135 }
11136
11137 /* Parse a ptr-operator.
11138
11139    ptr-operator:
11140      * cv-qualifier-seq [opt]
11141      &
11142      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11143
11144    GNU Extension:
11145
11146    ptr-operator:
11147      & cv-qualifier-seq [opt]
11148
11149    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11150    Returns ADDR_EXPR if a reference was used.  In the case of a
11151    pointer-to-member, *TYPE is filled in with the TYPE containing the
11152    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11153    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11154    ERROR_MARK if an error occurred.  */
11155
11156 static enum tree_code
11157 cp_parser_ptr_operator (cp_parser* parser,
11158                         tree* type,
11159                         cp_cv_quals *cv_quals)
11160 {
11161   enum tree_code code = ERROR_MARK;
11162   cp_token *token;
11163
11164   /* Assume that it's not a pointer-to-member.  */
11165   *type = NULL_TREE;
11166   /* And that there are no cv-qualifiers.  */
11167   *cv_quals = TYPE_UNQUALIFIED;
11168
11169   /* Peek at the next token.  */
11170   token = cp_lexer_peek_token (parser->lexer);
11171   /* If it's a `*' or `&' we have a pointer or reference.  */
11172   if (token->type == CPP_MULT || token->type == CPP_AND)
11173     {
11174       /* Remember which ptr-operator we were processing.  */
11175       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11176
11177       /* Consume the `*' or `&'.  */
11178       cp_lexer_consume_token (parser->lexer);
11179
11180       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11181          `&', if we are allowing GNU extensions.  (The only qualifier
11182          that can legally appear after `&' is `restrict', but that is
11183          enforced during semantic analysis.  */
11184       if (code == INDIRECT_REF
11185           || cp_parser_allow_gnu_extensions_p (parser))
11186         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11187     }
11188   else
11189     {
11190       /* Try the pointer-to-member case.  */
11191       cp_parser_parse_tentatively (parser);
11192       /* Look for the optional `::' operator.  */
11193       cp_parser_global_scope_opt (parser,
11194                                   /*current_scope_valid_p=*/false);
11195       /* Look for the nested-name specifier.  */
11196       cp_parser_nested_name_specifier (parser,
11197                                        /*typename_keyword_p=*/false,
11198                                        /*check_dependency_p=*/true,
11199                                        /*type_p=*/false,
11200                                        /*is_declaration=*/false);
11201       /* If we found it, and the next token is a `*', then we are
11202          indeed looking at a pointer-to-member operator.  */
11203       if (!cp_parser_error_occurred (parser)
11204           && cp_parser_require (parser, CPP_MULT, "`*'"))
11205         {
11206           /* The type of which the member is a member is given by the
11207              current SCOPE.  */
11208           *type = parser->scope;
11209           /* The next name will not be qualified.  */
11210           parser->scope = NULL_TREE;
11211           parser->qualifying_scope = NULL_TREE;
11212           parser->object_scope = NULL_TREE;
11213           /* Indicate that the `*' operator was used.  */
11214           code = INDIRECT_REF;
11215           /* Look for the optional cv-qualifier-seq.  */
11216           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11217         }
11218       /* If that didn't work we don't have a ptr-operator.  */
11219       if (!cp_parser_parse_definitely (parser))
11220         cp_parser_error (parser, "expected ptr-operator");
11221     }
11222
11223   return code;
11224 }
11225
11226 /* Parse an (optional) cv-qualifier-seq.
11227
11228    cv-qualifier-seq:
11229      cv-qualifier cv-qualifier-seq [opt]
11230
11231    cv-qualifier:
11232      const
11233      volatile
11234
11235    GNU Extension:
11236
11237    cv-qualifier:
11238      __restrict__
11239
11240    Returns a bitmask representing the cv-qualifiers.  */
11241
11242 static cp_cv_quals
11243 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11244 {
11245   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11246
11247   while (true)
11248     {
11249       cp_token *token;
11250       cp_cv_quals cv_qualifier;
11251
11252       /* Peek at the next token.  */
11253       token = cp_lexer_peek_token (parser->lexer);
11254       /* See if it's a cv-qualifier.  */
11255       switch (token->keyword)
11256         {
11257         case RID_CONST:
11258           cv_qualifier = TYPE_QUAL_CONST;
11259           break;
11260
11261         case RID_VOLATILE:
11262           cv_qualifier = TYPE_QUAL_VOLATILE;
11263           break;
11264
11265         case RID_RESTRICT:
11266           cv_qualifier = TYPE_QUAL_RESTRICT;
11267           break;
11268
11269         default:
11270           cv_qualifier = TYPE_UNQUALIFIED;
11271           break;
11272         }
11273
11274       if (!cv_qualifier)
11275         break;
11276
11277       if (cv_quals & cv_qualifier)
11278         {
11279           error ("duplicate cv-qualifier");
11280           cp_lexer_purge_token (parser->lexer);
11281         }
11282       else
11283         {
11284           cp_lexer_consume_token (parser->lexer);
11285           cv_quals |= cv_qualifier;
11286         }
11287     }
11288
11289   return cv_quals;
11290 }
11291
11292 /* Parse a declarator-id.
11293
11294    declarator-id:
11295      id-expression
11296      :: [opt] nested-name-specifier [opt] type-name
11297
11298    In the `id-expression' case, the value returned is as for
11299    cp_parser_id_expression if the id-expression was an unqualified-id.
11300    If the id-expression was a qualified-id, then a SCOPE_REF is
11301    returned.  The first operand is the scope (either a NAMESPACE_DECL
11302    or TREE_TYPE), but the second is still just a representation of an
11303    unqualified-id.  */
11304
11305 static tree
11306 cp_parser_declarator_id (cp_parser* parser)
11307 {
11308   tree id_expression;
11309
11310   /* The expression must be an id-expression.  Assume that qualified
11311      names are the names of types so that:
11312
11313        template <class T>
11314        int S<T>::R::i = 3;
11315
11316      will work; we must treat `S<T>::R' as the name of a type.
11317      Similarly, assume that qualified names are templates, where
11318      required, so that:
11319
11320        template <class T>
11321        int S<T>::R<T>::i = 3;
11322
11323      will work, too.  */
11324   id_expression = cp_parser_id_expression (parser,
11325                                            /*template_keyword_p=*/false,
11326                                            /*check_dependency_p=*/false,
11327                                            /*template_p=*/NULL,
11328                                            /*declarator_p=*/true);
11329   /* If the name was qualified, create a SCOPE_REF to represent
11330      that.  */
11331   if (parser->scope)
11332     {
11333       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11334       parser->scope = NULL_TREE;
11335     }
11336
11337   return id_expression;
11338 }
11339
11340 /* Parse a type-id.
11341
11342    type-id:
11343      type-specifier-seq abstract-declarator [opt]
11344
11345    Returns the TYPE specified.  */
11346
11347 static tree
11348 cp_parser_type_id (cp_parser* parser)
11349 {
11350   cp_decl_specifier_seq type_specifier_seq;
11351   cp_declarator *abstract_declarator;
11352
11353   /* Parse the type-specifier-seq.  */
11354   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11355   if (type_specifier_seq.type == error_mark_node)
11356     return error_mark_node;
11357
11358   /* There might or might not be an abstract declarator.  */
11359   cp_parser_parse_tentatively (parser);
11360   /* Look for the declarator.  */
11361   abstract_declarator
11362     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11363                             /*parenthesized_p=*/NULL,
11364                             /*member_p=*/false);
11365   /* Check to see if there really was a declarator.  */
11366   if (!cp_parser_parse_definitely (parser))
11367     abstract_declarator = NULL;
11368
11369   return groktypename (&type_specifier_seq, abstract_declarator);
11370 }
11371
11372 /* Parse a type-specifier-seq.
11373
11374    type-specifier-seq:
11375      type-specifier type-specifier-seq [opt]
11376
11377    GNU extension:
11378
11379    type-specifier-seq:
11380      attributes type-specifier-seq [opt]
11381
11382    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11383
11384 static void
11385 cp_parser_type_specifier_seq (cp_parser* parser,
11386                               cp_decl_specifier_seq *type_specifier_seq)
11387 {
11388   bool seen_type_specifier = false;
11389
11390   /* Clear the TYPE_SPECIFIER_SEQ.  */
11391   clear_decl_specs (type_specifier_seq);
11392
11393   /* Parse the type-specifiers and attributes.  */
11394   while (true)
11395     {
11396       tree type_specifier;
11397
11398       /* Check for attributes first.  */
11399       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11400         {
11401           type_specifier_seq->attributes =
11402             chainon (type_specifier_seq->attributes,
11403                      cp_parser_attributes_opt (parser));
11404           continue;
11405         }
11406
11407       /* Look for the type-specifier.  */
11408       type_specifier = cp_parser_type_specifier (parser,
11409                                                  CP_PARSER_FLAGS_OPTIONAL,
11410                                                  type_specifier_seq,
11411                                                  /*is_declaration=*/false,
11412                                                  NULL,
11413                                                  NULL);
11414       /* If the first type-specifier could not be found, this is not a
11415          type-specifier-seq at all.  */
11416       if (!seen_type_specifier && !type_specifier)
11417         {
11418           cp_parser_error (parser, "expected type-specifier");
11419           type_specifier_seq->type = error_mark_node;
11420           return;
11421         }
11422       /* If subsequent type-specifiers could not be found, the
11423          type-specifier-seq is complete.  */
11424       else if (seen_type_specifier && !type_specifier)
11425         break;
11426
11427       seen_type_specifier = true;
11428     }
11429
11430   return;
11431 }
11432
11433 /* Parse a parameter-declaration-clause.
11434
11435    parameter-declaration-clause:
11436      parameter-declaration-list [opt] ... [opt]
11437      parameter-declaration-list , ...
11438
11439    Returns a representation for the parameter declarations.  A return
11440    value of NULL indicates a parameter-declaration-clause consisting
11441    only of an ellipsis.  */
11442
11443 static cp_parameter_declarator *
11444 cp_parser_parameter_declaration_clause (cp_parser* parser)
11445 {
11446   cp_parameter_declarator *parameters;
11447   cp_token *token;
11448   bool ellipsis_p;
11449   bool is_error;
11450
11451   /* Peek at the next token.  */
11452   token = cp_lexer_peek_token (parser->lexer);
11453   /* Check for trivial parameter-declaration-clauses.  */
11454   if (token->type == CPP_ELLIPSIS)
11455     {
11456       /* Consume the `...' token.  */
11457       cp_lexer_consume_token (parser->lexer);
11458       return NULL;
11459     }
11460   else if (token->type == CPP_CLOSE_PAREN)
11461     /* There are no parameters.  */
11462     {
11463 #ifndef NO_IMPLICIT_EXTERN_C
11464       if (in_system_header && current_class_type == NULL
11465           && current_lang_name == lang_name_c)
11466         return NULL;
11467       else
11468 #endif
11469         return no_parameters;
11470     }
11471   /* Check for `(void)', too, which is a special case.  */
11472   else if (token->keyword == RID_VOID
11473            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11474                == CPP_CLOSE_PAREN))
11475     {
11476       /* Consume the `void' token.  */
11477       cp_lexer_consume_token (parser->lexer);
11478       /* There are no parameters.  */
11479       return no_parameters;
11480     }
11481
11482   /* Parse the parameter-declaration-list.  */
11483   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11484   /* If a parse error occurred while parsing the
11485      parameter-declaration-list, then the entire
11486      parameter-declaration-clause is erroneous.  */
11487   if (is_error)
11488     return NULL;
11489
11490   /* Peek at the next token.  */
11491   token = cp_lexer_peek_token (parser->lexer);
11492   /* If it's a `,', the clause should terminate with an ellipsis.  */
11493   if (token->type == CPP_COMMA)
11494     {
11495       /* Consume the `,'.  */
11496       cp_lexer_consume_token (parser->lexer);
11497       /* Expect an ellipsis.  */
11498       ellipsis_p
11499         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11500     }
11501   /* It might also be `...' if the optional trailing `,' was
11502      omitted.  */
11503   else if (token->type == CPP_ELLIPSIS)
11504     {
11505       /* Consume the `...' token.  */
11506       cp_lexer_consume_token (parser->lexer);
11507       /* And remember that we saw it.  */
11508       ellipsis_p = true;
11509     }
11510   else
11511     ellipsis_p = false;
11512
11513   /* Finish the parameter list.  */
11514   if (parameters && ellipsis_p)
11515     parameters->ellipsis_p = true;
11516
11517   return parameters;
11518 }
11519
11520 /* Parse a parameter-declaration-list.
11521
11522    parameter-declaration-list:
11523      parameter-declaration
11524      parameter-declaration-list , parameter-declaration
11525
11526    Returns a representation of the parameter-declaration-list, as for
11527    cp_parser_parameter_declaration_clause.  However, the
11528    `void_list_node' is never appended to the list.  Upon return,
11529    *IS_ERROR will be true iff an error occurred.  */
11530
11531 static cp_parameter_declarator *
11532 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11533 {
11534   cp_parameter_declarator *parameters = NULL;
11535   cp_parameter_declarator **tail = &parameters;
11536
11537   /* Assume all will go well.  */
11538   *is_error = false;
11539
11540   /* Look for more parameters.  */
11541   while (true)
11542     {
11543       cp_parameter_declarator *parameter;
11544       bool parenthesized_p;
11545       /* Parse the parameter.  */
11546       parameter
11547         = cp_parser_parameter_declaration (parser,
11548                                            /*template_parm_p=*/false,
11549                                            &parenthesized_p);
11550
11551       /* If a parse error occurred parsing the parameter declaration,
11552          then the entire parameter-declaration-list is erroneous.  */
11553       if (!parameter)
11554         {
11555           *is_error = true;
11556           parameters = NULL;
11557           break;
11558         }
11559       /* Add the new parameter to the list.  */
11560       *tail = parameter;
11561       tail = &parameter->next;
11562
11563       /* Peek at the next token.  */
11564       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11565           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11566         /* The parameter-declaration-list is complete.  */
11567         break;
11568       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11569         {
11570           cp_token *token;
11571
11572           /* Peek at the next token.  */
11573           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11574           /* If it's an ellipsis, then the list is complete.  */
11575           if (token->type == CPP_ELLIPSIS)
11576             break;
11577           /* Otherwise, there must be more parameters.  Consume the
11578              `,'.  */
11579           cp_lexer_consume_token (parser->lexer);
11580           /* When parsing something like:
11581
11582                 int i(float f, double d)
11583
11584              we can tell after seeing the declaration for "f" that we
11585              are not looking at an initialization of a variable "i",
11586              but rather at the declaration of a function "i".
11587
11588              Due to the fact that the parsing of template arguments
11589              (as specified to a template-id) requires backtracking we
11590              cannot use this technique when inside a template argument
11591              list.  */
11592           if (!parser->in_template_argument_list_p
11593               && !parser->in_type_id_in_expr_p
11594               && cp_parser_parsing_tentatively (parser)
11595               && !cp_parser_committed_to_tentative_parse (parser)
11596               /* However, a parameter-declaration of the form
11597                  "foat(f)" (which is a valid declaration of a
11598                  parameter "f") can also be interpreted as an
11599                  expression (the conversion of "f" to "float").  */
11600               && !parenthesized_p)
11601             cp_parser_commit_to_tentative_parse (parser);
11602         }
11603       else
11604         {
11605           cp_parser_error (parser, "expected %<,%> or %<...%>");
11606           if (!cp_parser_parsing_tentatively (parser)
11607               || cp_parser_committed_to_tentative_parse (parser))
11608             cp_parser_skip_to_closing_parenthesis (parser,
11609                                                    /*recovering=*/true,
11610                                                    /*or_comma=*/false,
11611                                                    /*consume_paren=*/false);
11612           break;
11613         }
11614     }
11615
11616   return parameters;
11617 }
11618
11619 /* Parse a parameter declaration.
11620
11621    parameter-declaration:
11622      decl-specifier-seq declarator
11623      decl-specifier-seq declarator = assignment-expression
11624      decl-specifier-seq abstract-declarator [opt]
11625      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11626
11627    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11628    declares a template parameter.  (In that case, a non-nested `>'
11629    token encountered during the parsing of the assignment-expression
11630    is not interpreted as a greater-than operator.)
11631
11632    Returns a representation of the parameter, or NULL if an error
11633    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11634    true iff the declarator is of the form "(p)".  */
11635
11636 static cp_parameter_declarator *
11637 cp_parser_parameter_declaration (cp_parser *parser,
11638                                  bool template_parm_p,
11639                                  bool *parenthesized_p)
11640 {
11641   int declares_class_or_enum;
11642   bool greater_than_is_operator_p;
11643   cp_decl_specifier_seq decl_specifiers;
11644   cp_declarator *declarator;
11645   tree default_argument;
11646   cp_token *token;
11647   const char *saved_message;
11648
11649   /* In a template parameter, `>' is not an operator.
11650
11651      [temp.param]
11652
11653      When parsing a default template-argument for a non-type
11654      template-parameter, the first non-nested `>' is taken as the end
11655      of the template parameter-list rather than a greater-than
11656      operator.  */
11657   greater_than_is_operator_p = !template_parm_p;
11658
11659   /* Type definitions may not appear in parameter types.  */
11660   saved_message = parser->type_definition_forbidden_message;
11661   parser->type_definition_forbidden_message
11662     = "types may not be defined in parameter types";
11663
11664   /* Parse the declaration-specifiers.  */
11665   cp_parser_decl_specifier_seq (parser,
11666                                 CP_PARSER_FLAGS_NONE,
11667                                 &decl_specifiers,
11668                                 &declares_class_or_enum);
11669   /* If an error occurred, there's no reason to attempt to parse the
11670      rest of the declaration.  */
11671   if (cp_parser_error_occurred (parser))
11672     {
11673       parser->type_definition_forbidden_message = saved_message;
11674       return NULL;
11675     }
11676
11677   /* Peek at the next token.  */
11678   token = cp_lexer_peek_token (parser->lexer);
11679   /* If the next token is a `)', `,', `=', `>', or `...', then there
11680      is no declarator.  */
11681   if (token->type == CPP_CLOSE_PAREN
11682       || token->type == CPP_COMMA
11683       || token->type == CPP_EQ
11684       || token->type == CPP_ELLIPSIS
11685       || token->type == CPP_GREATER)
11686     {
11687       declarator = NULL;
11688       if (parenthesized_p)
11689         *parenthesized_p = false;
11690     }
11691   /* Otherwise, there should be a declarator.  */
11692   else
11693     {
11694       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11695       parser->default_arg_ok_p = false;
11696
11697       /* After seeing a decl-specifier-seq, if the next token is not a
11698          "(", there is no possibility that the code is a valid
11699          expression.  Therefore, if parsing tentatively, we commit at
11700          this point.  */
11701       if (!parser->in_template_argument_list_p
11702           /* In an expression context, having seen:
11703
11704                (int((char ...
11705
11706              we cannot be sure whether we are looking at a
11707              function-type (taking a "char" as a parameter) or a cast
11708              of some object of type "char" to "int".  */
11709           && !parser->in_type_id_in_expr_p
11710           && cp_parser_parsing_tentatively (parser)
11711           && !cp_parser_committed_to_tentative_parse (parser)
11712           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11713         cp_parser_commit_to_tentative_parse (parser);
11714       /* Parse the declarator.  */
11715       declarator = cp_parser_declarator (parser,
11716                                          CP_PARSER_DECLARATOR_EITHER,
11717                                          /*ctor_dtor_or_conv_p=*/NULL,
11718                                          parenthesized_p,
11719                                          /*member_p=*/false);
11720       parser->default_arg_ok_p = saved_default_arg_ok_p;
11721       /* After the declarator, allow more attributes.  */
11722       decl_specifiers.attributes
11723         = chainon (decl_specifiers.attributes,
11724                    cp_parser_attributes_opt (parser));
11725     }
11726
11727   /* The restriction on defining new types applies only to the type
11728      of the parameter, not to the default argument.  */
11729   parser->type_definition_forbidden_message = saved_message;
11730
11731   /* If the next token is `=', then process a default argument.  */
11732   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11733     {
11734       bool saved_greater_than_is_operator_p;
11735       /* Consume the `='.  */
11736       cp_lexer_consume_token (parser->lexer);
11737
11738       /* If we are defining a class, then the tokens that make up the
11739          default argument must be saved and processed later.  */
11740       if (!template_parm_p && at_class_scope_p ()
11741           && TYPE_BEING_DEFINED (current_class_type))
11742         {
11743           unsigned depth = 0;
11744           cp_token *first_token;
11745           cp_token *token;
11746
11747           /* Add tokens until we have processed the entire default
11748              argument.  We add the range [first_token, token).  */
11749           first_token = cp_lexer_peek_token (parser->lexer);
11750           while (true)
11751             {
11752               bool done = false;
11753
11754               /* Peek at the next token.  */
11755               token = cp_lexer_peek_token (parser->lexer);
11756               /* What we do depends on what token we have.  */
11757               switch (token->type)
11758                 {
11759                   /* In valid code, a default argument must be
11760                      immediately followed by a `,' `)', or `...'.  */
11761                 case CPP_COMMA:
11762                 case CPP_CLOSE_PAREN:
11763                 case CPP_ELLIPSIS:
11764                   /* If we run into a non-nested `;', `}', or `]',
11765                      then the code is invalid -- but the default
11766                      argument is certainly over.  */
11767                 case CPP_SEMICOLON:
11768                 case CPP_CLOSE_BRACE:
11769                 case CPP_CLOSE_SQUARE:
11770                   if (depth == 0)
11771                     done = true;
11772                   /* Update DEPTH, if necessary.  */
11773                   else if (token->type == CPP_CLOSE_PAREN
11774                            || token->type == CPP_CLOSE_BRACE
11775                            || token->type == CPP_CLOSE_SQUARE)
11776                     --depth;
11777                   break;
11778
11779                 case CPP_OPEN_PAREN:
11780                 case CPP_OPEN_SQUARE:
11781                 case CPP_OPEN_BRACE:
11782                   ++depth;
11783                   break;
11784
11785                 case CPP_GREATER:
11786                   /* If we see a non-nested `>', and `>' is not an
11787                      operator, then it marks the end of the default
11788                      argument.  */
11789                   if (!depth && !greater_than_is_operator_p)
11790                     done = true;
11791                   break;
11792
11793                   /* If we run out of tokens, issue an error message.  */
11794                 case CPP_EOF:
11795                   error ("file ends in default argument");
11796                   done = true;
11797                   break;
11798
11799                 case CPP_NAME:
11800                 case CPP_SCOPE:
11801                   /* In these cases, we should look for template-ids.
11802                      For example, if the default argument is
11803                      `X<int, double>()', we need to do name lookup to
11804                      figure out whether or not `X' is a template; if
11805                      so, the `,' does not end the default argument.
11806
11807                      That is not yet done.  */
11808                   break;
11809
11810                 default:
11811                   break;
11812                 }
11813
11814               /* If we've reached the end, stop.  */
11815               if (done)
11816                 break;
11817
11818               /* Add the token to the token block.  */
11819               token = cp_lexer_consume_token (parser->lexer);
11820             }
11821
11822           /* Create a DEFAULT_ARG to represented the unparsed default
11823              argument.  */
11824           default_argument = make_node (DEFAULT_ARG);
11825           DEFARG_TOKENS (default_argument)
11826             = cp_token_cache_new (first_token, token);  
11827         }
11828       /* Outside of a class definition, we can just parse the
11829          assignment-expression.  */
11830       else
11831         {
11832           bool saved_local_variables_forbidden_p;
11833
11834           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11835              set correctly.  */
11836           saved_greater_than_is_operator_p
11837             = parser->greater_than_is_operator_p;
11838           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11839           /* Local variable names (and the `this' keyword) may not
11840              appear in a default argument.  */
11841           saved_local_variables_forbidden_p
11842             = parser->local_variables_forbidden_p;
11843           parser->local_variables_forbidden_p = true;
11844           /* Parse the assignment-expression.  */
11845           default_argument = cp_parser_assignment_expression (parser);
11846           /* Restore saved state.  */
11847           parser->greater_than_is_operator_p
11848             = saved_greater_than_is_operator_p;
11849           parser->local_variables_forbidden_p
11850             = saved_local_variables_forbidden_p;
11851         }
11852       if (!parser->default_arg_ok_p)
11853         {
11854           if (!flag_pedantic_errors)
11855             warning ("deprecated use of default argument for parameter of non-function");
11856           else
11857             {
11858               error ("default arguments are only permitted for function parameters");
11859               default_argument = NULL_TREE;
11860             }
11861         }
11862     }
11863   else
11864     default_argument = NULL_TREE;
11865
11866   return make_parameter_declarator (&decl_specifiers,
11867                                     declarator,
11868                                     default_argument);
11869 }
11870
11871 /* Parse a function-body.
11872
11873    function-body:
11874      compound_statement  */
11875
11876 static void
11877 cp_parser_function_body (cp_parser *parser)
11878 {
11879   cp_parser_compound_statement (parser, NULL, false);
11880 }
11881
11882 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11883    true if a ctor-initializer was present.  */
11884
11885 static bool
11886 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11887 {
11888   tree body;
11889   bool ctor_initializer_p;
11890
11891   /* Begin the function body.  */
11892   body = begin_function_body ();
11893   /* Parse the optional ctor-initializer.  */
11894   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11895   /* Parse the function-body.  */
11896   cp_parser_function_body (parser);
11897   /* Finish the function body.  */
11898   finish_function_body (body);
11899
11900   return ctor_initializer_p;
11901 }
11902
11903 /* Parse an initializer.
11904
11905    initializer:
11906      = initializer-clause
11907      ( expression-list )
11908
11909    Returns a expression representing the initializer.  If no
11910    initializer is present, NULL_TREE is returned.
11911
11912    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11913    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11914    set to FALSE if there is no initializer present.  If there is an
11915    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11916    is set to true; otherwise it is set to false.  */
11917
11918 static tree
11919 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11920                        bool* non_constant_p)
11921 {
11922   cp_token *token;
11923   tree init;
11924
11925   /* Peek at the next token.  */
11926   token = cp_lexer_peek_token (parser->lexer);
11927
11928   /* Let our caller know whether or not this initializer was
11929      parenthesized.  */
11930   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11931   /* Assume that the initializer is constant.  */
11932   *non_constant_p = false;
11933
11934   if (token->type == CPP_EQ)
11935     {
11936       /* Consume the `='.  */
11937       cp_lexer_consume_token (parser->lexer);
11938       /* Parse the initializer-clause.  */
11939       init = cp_parser_initializer_clause (parser, non_constant_p);
11940     }
11941   else if (token->type == CPP_OPEN_PAREN)
11942     init = cp_parser_parenthesized_expression_list (parser, false,
11943                                                     non_constant_p);
11944   else
11945     {
11946       /* Anything else is an error.  */
11947       cp_parser_error (parser, "expected initializer");
11948       init = error_mark_node;
11949     }
11950
11951   return init;
11952 }
11953
11954 /* Parse an initializer-clause.
11955
11956    initializer-clause:
11957      assignment-expression
11958      { initializer-list , [opt] }
11959      { }
11960
11961    Returns an expression representing the initializer.
11962
11963    If the `assignment-expression' production is used the value
11964    returned is simply a representation for the expression.
11965
11966    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11967    the elements of the initializer-list (or NULL_TREE, if the last
11968    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11969    NULL_TREE.  There is no way to detect whether or not the optional
11970    trailing `,' was provided.  NON_CONSTANT_P is as for
11971    cp_parser_initializer.  */
11972
11973 static tree
11974 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11975 {
11976   tree initializer;
11977
11978   /* If it is not a `{', then we are looking at an
11979      assignment-expression.  */
11980   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11981     {
11982       initializer
11983         = cp_parser_constant_expression (parser,
11984                                         /*allow_non_constant_p=*/true,
11985                                         non_constant_p);
11986       if (!*non_constant_p)
11987         initializer = fold_non_dependent_expr (initializer);
11988     }
11989   else
11990     {
11991       /* Consume the `{' token.  */
11992       cp_lexer_consume_token (parser->lexer);
11993       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11994       initializer = make_node (CONSTRUCTOR);
11995       /* If it's not a `}', then there is a non-trivial initializer.  */
11996       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11997         {
11998           /* Parse the initializer list.  */
11999           CONSTRUCTOR_ELTS (initializer)
12000             = cp_parser_initializer_list (parser, non_constant_p);
12001           /* A trailing `,' token is allowed.  */
12002           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12003             cp_lexer_consume_token (parser->lexer);
12004         }
12005       /* Now, there should be a trailing `}'.  */
12006       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12007     }
12008
12009   return initializer;
12010 }
12011
12012 /* Parse an initializer-list.
12013
12014    initializer-list:
12015      initializer-clause
12016      initializer-list , initializer-clause
12017
12018    GNU Extension:
12019
12020    initializer-list:
12021      identifier : initializer-clause
12022      initializer-list, identifier : initializer-clause
12023
12024    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12025    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12026    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12027    as for cp_parser_initializer.  */
12028
12029 static tree
12030 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12031 {
12032   tree initializers = NULL_TREE;
12033
12034   /* Assume all of the expressions are constant.  */
12035   *non_constant_p = false;
12036
12037   /* Parse the rest of the list.  */
12038   while (true)
12039     {
12040       cp_token *token;
12041       tree identifier;
12042       tree initializer;
12043       bool clause_non_constant_p;
12044
12045       /* If the next token is an identifier and the following one is a
12046          colon, we are looking at the GNU designated-initializer
12047          syntax.  */
12048       if (cp_parser_allow_gnu_extensions_p (parser)
12049           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12050           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12051         {
12052           /* Consume the identifier.  */
12053           identifier = cp_lexer_consume_token (parser->lexer)->value;
12054           /* Consume the `:'.  */
12055           cp_lexer_consume_token (parser->lexer);
12056         }
12057       else
12058         identifier = NULL_TREE;
12059
12060       /* Parse the initializer.  */
12061       initializer = cp_parser_initializer_clause (parser,
12062                                                   &clause_non_constant_p);
12063       /* If any clause is non-constant, so is the entire initializer.  */
12064       if (clause_non_constant_p)
12065         *non_constant_p = true;
12066       /* Add it to the list.  */
12067       initializers = tree_cons (identifier, initializer, initializers);
12068
12069       /* If the next token is not a comma, we have reached the end of
12070          the list.  */
12071       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12072         break;
12073
12074       /* Peek at the next token.  */
12075       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12076       /* If the next token is a `}', then we're still done.  An
12077          initializer-clause can have a trailing `,' after the
12078          initializer-list and before the closing `}'.  */
12079       if (token->type == CPP_CLOSE_BRACE)
12080         break;
12081
12082       /* Consume the `,' token.  */
12083       cp_lexer_consume_token (parser->lexer);
12084     }
12085
12086   /* The initializers were built up in reverse order, so we need to
12087      reverse them now.  */
12088   return nreverse (initializers);
12089 }
12090
12091 /* Classes [gram.class] */
12092
12093 /* Parse a class-name.
12094
12095    class-name:
12096      identifier
12097      template-id
12098
12099    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12100    to indicate that names looked up in dependent types should be
12101    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12102    keyword has been used to indicate that the name that appears next
12103    is a template.  TYPE_P is true iff the next name should be treated
12104    as class-name, even if it is declared to be some other kind of name
12105    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12106    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
12107    being defined in a class-head.
12108
12109    Returns the TYPE_DECL representing the class.  */
12110
12111 static tree
12112 cp_parser_class_name (cp_parser *parser,
12113                       bool typename_keyword_p,
12114                       bool template_keyword_p,
12115                       bool type_p,
12116                       bool check_dependency_p,
12117                       bool class_head_p,
12118                       bool is_declaration)
12119 {
12120   tree decl;
12121   tree scope;
12122   bool typename_p;
12123   cp_token *token;
12124
12125   /* All class-names start with an identifier.  */
12126   token = cp_lexer_peek_token (parser->lexer);
12127   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12128     {
12129       cp_parser_error (parser, "expected class-name");
12130       return error_mark_node;
12131     }
12132
12133   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12134      to a template-id, so we save it here.  */
12135   scope = parser->scope;
12136   if (scope == error_mark_node)
12137     return error_mark_node;
12138
12139   /* Any name names a type if we're following the `typename' keyword
12140      in a qualified name where the enclosing scope is type-dependent.  */
12141   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12142                 && dependent_type_p (scope));
12143   /* Handle the common case (an identifier, but not a template-id)
12144      efficiently.  */
12145   if (token->type == CPP_NAME
12146       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12147     {
12148       tree identifier;
12149
12150       /* Look for the identifier.  */
12151       identifier = cp_parser_identifier (parser);
12152       /* If the next token isn't an identifier, we are certainly not
12153          looking at a class-name.  */
12154       if (identifier == error_mark_node)
12155         decl = error_mark_node;
12156       /* If we know this is a type-name, there's no need to look it
12157          up.  */
12158       else if (typename_p)
12159         decl = identifier;
12160       else
12161         {
12162           /* If the next token is a `::', then the name must be a type
12163              name.
12164
12165              [basic.lookup.qual]
12166
12167              During the lookup for a name preceding the :: scope
12168              resolution operator, object, function, and enumerator
12169              names are ignored.  */
12170           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12171             type_p = true;
12172           /* Look up the name.  */
12173           decl = cp_parser_lookup_name (parser, identifier,
12174                                         type_p,
12175                                         /*is_template=*/false,
12176                                         /*is_namespace=*/false,
12177                                         check_dependency_p,
12178                                         /*ambiguous_p=*/NULL);
12179         }
12180     }
12181   else
12182     {
12183       /* Try a template-id.  */
12184       decl = cp_parser_template_id (parser, template_keyword_p,
12185                                     check_dependency_p,
12186                                     is_declaration);
12187       if (decl == error_mark_node)
12188         return error_mark_node;
12189     }
12190
12191   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12192
12193   /* If this is a typename, create a TYPENAME_TYPE.  */
12194   if (typename_p && decl != error_mark_node)
12195     {
12196       decl = make_typename_type (scope, decl, /*complain=*/1);
12197       if (decl != error_mark_node)
12198         decl = TYPE_NAME (decl);
12199     }
12200
12201   /* Check to see that it is really the name of a class.  */
12202   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12203       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12204       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12205     /* Situations like this:
12206
12207          template <typename T> struct A {
12208            typename T::template X<int>::I i;
12209          };
12210
12211        are problematic.  Is `T::template X<int>' a class-name?  The
12212        standard does not seem to be definitive, but there is no other
12213        valid interpretation of the following `::'.  Therefore, those
12214        names are considered class-names.  */
12215     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12216   else if (decl == error_mark_node
12217            || TREE_CODE (decl) != TYPE_DECL
12218            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12219     {
12220       cp_parser_error (parser, "expected class-name");
12221       return error_mark_node;
12222     }
12223
12224   return decl;
12225 }
12226
12227 /* Parse a class-specifier.
12228
12229    class-specifier:
12230      class-head { member-specification [opt] }
12231
12232    Returns the TREE_TYPE representing the class.  */
12233
12234 static tree
12235 cp_parser_class_specifier (cp_parser* parser)
12236 {
12237   cp_token *token;
12238   tree type;
12239   tree attributes = NULL_TREE;
12240   int has_trailing_semicolon;
12241   bool nested_name_specifier_p;
12242   unsigned saved_num_template_parameter_lists;
12243   tree old_scope = NULL_TREE;
12244   tree scope = NULL_TREE;
12245
12246   push_deferring_access_checks (dk_no_deferred);
12247
12248   /* Parse the class-head.  */
12249   type = cp_parser_class_head (parser,
12250                                &nested_name_specifier_p,
12251                                &attributes);
12252   /* If the class-head was a semantic disaster, skip the entire body
12253      of the class.  */
12254   if (!type)
12255     {
12256       cp_parser_skip_to_end_of_block_or_statement (parser);
12257       pop_deferring_access_checks ();
12258       return error_mark_node;
12259     }
12260
12261   /* Look for the `{'.  */
12262   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12263     {
12264       pop_deferring_access_checks ();
12265       return error_mark_node;
12266     }
12267
12268   /* Issue an error message if type-definitions are forbidden here.  */
12269   cp_parser_check_type_definition (parser);
12270   /* Remember that we are defining one more class.  */
12271   ++parser->num_classes_being_defined;
12272   /* Inside the class, surrounding template-parameter-lists do not
12273      apply.  */
12274   saved_num_template_parameter_lists
12275     = parser->num_template_parameter_lists;
12276   parser->num_template_parameter_lists = 0;
12277
12278   /* Start the class.  */
12279   if (nested_name_specifier_p)
12280     {
12281       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12282       old_scope = push_inner_scope (scope);
12283     }
12284   type = begin_class_definition (type);
12285
12286   if (type == error_mark_node)
12287     /* If the type is erroneous, skip the entire body of the class.  */
12288     cp_parser_skip_to_closing_brace (parser);
12289   else
12290     /* Parse the member-specification.  */
12291     cp_parser_member_specification_opt (parser);
12292
12293   /* Look for the trailing `}'.  */
12294   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12295   /* We get better error messages by noticing a common problem: a
12296      missing trailing `;'.  */
12297   token = cp_lexer_peek_token (parser->lexer);
12298   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12299   /* Look for trailing attributes to apply to this class.  */
12300   if (cp_parser_allow_gnu_extensions_p (parser))
12301     {
12302       tree sub_attr = cp_parser_attributes_opt (parser);
12303       attributes = chainon (attributes, sub_attr);
12304     }
12305   if (type != error_mark_node)
12306     type = finish_struct (type, attributes);
12307   if (nested_name_specifier_p)
12308     pop_inner_scope (old_scope, scope);
12309   /* If this class is not itself within the scope of another class,
12310      then we need to parse the bodies of all of the queued function
12311      definitions.  Note that the queued functions defined in a class
12312      are not always processed immediately following the
12313      class-specifier for that class.  Consider:
12314
12315        struct A {
12316          struct B { void f() { sizeof (A); } };
12317        };
12318
12319      If `f' were processed before the processing of `A' were
12320      completed, there would be no way to compute the size of `A'.
12321      Note that the nesting we are interested in here is lexical --
12322      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12323      for:
12324
12325        struct A { struct B; };
12326        struct A::B { void f() { } };
12327
12328      there is no need to delay the parsing of `A::B::f'.  */
12329   if (--parser->num_classes_being_defined == 0)
12330     {
12331       tree queue_entry;
12332       tree fn;
12333       tree class_type;
12334       bool pop_p;
12335
12336       /* In a first pass, parse default arguments to the functions.
12337          Then, in a second pass, parse the bodies of the functions.
12338          This two-phased approach handles cases like:
12339
12340             struct S {
12341               void f() { g(); }
12342               void g(int i = 3);
12343             };
12344
12345          */
12346       class_type = NULL_TREE;
12347       pop_p = false;
12348       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12349              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12350            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12351            TREE_PURPOSE (parser->unparsed_functions_queues)
12352              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12353         {
12354           fn = TREE_VALUE (queue_entry);
12355           /* If there are default arguments that have not yet been processed,
12356              take care of them now.  */
12357           if (class_type != TREE_PURPOSE (queue_entry))
12358             {
12359               if (pop_p)
12360                 pop_scope (class_type);
12361               class_type = TREE_PURPOSE (queue_entry);
12362               pop_p = push_scope (class_type);
12363             }
12364           /* Make sure that any template parameters are in scope.  */
12365           maybe_begin_member_template_processing (fn);
12366           /* Parse the default argument expressions.  */
12367           cp_parser_late_parsing_default_args (parser, fn);
12368           /* Remove any template parameters from the symbol table.  */
12369           maybe_end_member_template_processing ();
12370         }
12371       if (pop_p)
12372         pop_scope (class_type);
12373       /* Now parse the body of the functions.  */
12374       for (TREE_VALUE (parser->unparsed_functions_queues)
12375              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12376            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12377            TREE_VALUE (parser->unparsed_functions_queues)
12378              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12379         {
12380           /* Figure out which function we need to process.  */
12381           fn = TREE_VALUE (queue_entry);
12382
12383           /* A hack to prevent garbage collection.  */
12384           function_depth++;
12385
12386           /* Parse the function.  */
12387           cp_parser_late_parsing_for_member (parser, fn);
12388           function_depth--;
12389         }
12390     }
12391
12392   /* Put back any saved access checks.  */
12393   pop_deferring_access_checks ();
12394
12395   /* Restore the count of active template-parameter-lists.  */
12396   parser->num_template_parameter_lists
12397     = saved_num_template_parameter_lists;
12398
12399   return type;
12400 }
12401
12402 /* Parse a class-head.
12403
12404    class-head:
12405      class-key identifier [opt] base-clause [opt]
12406      class-key nested-name-specifier identifier base-clause [opt]
12407      class-key nested-name-specifier [opt] template-id
12408        base-clause [opt]
12409
12410    GNU Extensions:
12411      class-key attributes identifier [opt] base-clause [opt]
12412      class-key attributes nested-name-specifier identifier base-clause [opt]
12413      class-key attributes nested-name-specifier [opt] template-id
12414        base-clause [opt]
12415
12416    Returns the TYPE of the indicated class.  Sets
12417    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12418    involving a nested-name-specifier was used, and FALSE otherwise.
12419
12420    Returns NULL_TREE if the class-head is syntactically valid, but
12421    semantically invalid in a way that means we should skip the entire
12422    body of the class.  */
12423
12424 static tree
12425 cp_parser_class_head (cp_parser* parser,
12426                       bool* nested_name_specifier_p,
12427                       tree *attributes_p)
12428 {
12429   tree nested_name_specifier;
12430   enum tag_types class_key;
12431   tree id = NULL_TREE;
12432   tree type = NULL_TREE;
12433   tree attributes;
12434   bool template_id_p = false;
12435   bool qualified_p = false;
12436   bool invalid_nested_name_p = false;
12437   bool invalid_explicit_specialization_p = false;
12438   bool pop_p = false;
12439   unsigned num_templates;
12440   tree bases;
12441
12442   /* Assume no nested-name-specifier will be present.  */
12443   *nested_name_specifier_p = false;
12444   /* Assume no template parameter lists will be used in defining the
12445      type.  */
12446   num_templates = 0;
12447
12448   /* Look for the class-key.  */
12449   class_key = cp_parser_class_key (parser);
12450   if (class_key == none_type)
12451     return error_mark_node;
12452
12453   /* Parse the attributes.  */
12454   attributes = cp_parser_attributes_opt (parser);
12455
12456   /* If the next token is `::', that is invalid -- but sometimes
12457      people do try to write:
12458
12459        struct ::S {};
12460
12461      Handle this gracefully by accepting the extra qualifier, and then
12462      issuing an error about it later if this really is a
12463      class-head.  If it turns out just to be an elaborated type
12464      specifier, remain silent.  */
12465   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12466     qualified_p = true;
12467
12468   push_deferring_access_checks (dk_no_check);
12469
12470   /* Determine the name of the class.  Begin by looking for an
12471      optional nested-name-specifier.  */
12472   nested_name_specifier
12473     = cp_parser_nested_name_specifier_opt (parser,
12474                                            /*typename_keyword_p=*/false,
12475                                            /*check_dependency_p=*/false,
12476                                            /*type_p=*/false,
12477                                            /*is_declaration=*/false);
12478   /* If there was a nested-name-specifier, then there *must* be an
12479      identifier.  */
12480   if (nested_name_specifier)
12481     {
12482       /* Although the grammar says `identifier', it really means
12483          `class-name' or `template-name'.  You are only allowed to
12484          define a class that has already been declared with this
12485          syntax.
12486
12487          The proposed resolution for Core Issue 180 says that whever
12488          you see `class T::X' you should treat `X' as a type-name.
12489
12490          It is OK to define an inaccessible class; for example:
12491
12492            class A { class B; };
12493            class A::B {};
12494
12495          We do not know if we will see a class-name, or a
12496          template-name.  We look for a class-name first, in case the
12497          class-name is a template-id; if we looked for the
12498          template-name first we would stop after the template-name.  */
12499       cp_parser_parse_tentatively (parser);
12500       type = cp_parser_class_name (parser,
12501                                    /*typename_keyword_p=*/false,
12502                                    /*template_keyword_p=*/false,
12503                                    /*type_p=*/true,
12504                                    /*check_dependency_p=*/false,
12505                                    /*class_head_p=*/true,
12506                                    /*is_declaration=*/false);
12507       /* If that didn't work, ignore the nested-name-specifier.  */
12508       if (!cp_parser_parse_definitely (parser))
12509         {
12510           invalid_nested_name_p = true;
12511           id = cp_parser_identifier (parser);
12512           if (id == error_mark_node)
12513             id = NULL_TREE;
12514         }
12515       /* If we could not find a corresponding TYPE, treat this
12516          declaration like an unqualified declaration.  */
12517       if (type == error_mark_node)
12518         nested_name_specifier = NULL_TREE;
12519       /* Otherwise, count the number of templates used in TYPE and its
12520          containing scopes.  */
12521       else
12522         {
12523           tree scope;
12524
12525           for (scope = TREE_TYPE (type);
12526                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12527                scope = (TYPE_P (scope)
12528                         ? TYPE_CONTEXT (scope)
12529                         : DECL_CONTEXT (scope)))
12530             if (TYPE_P (scope)
12531                 && CLASS_TYPE_P (scope)
12532                 && CLASSTYPE_TEMPLATE_INFO (scope)
12533                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12534                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12535               ++num_templates;
12536         }
12537     }
12538   /* Otherwise, the identifier is optional.  */
12539   else
12540     {
12541       /* We don't know whether what comes next is a template-id,
12542          an identifier, or nothing at all.  */
12543       cp_parser_parse_tentatively (parser);
12544       /* Check for a template-id.  */
12545       id = cp_parser_template_id (parser,
12546                                   /*template_keyword_p=*/false,
12547                                   /*check_dependency_p=*/true,
12548                                   /*is_declaration=*/true);
12549       /* If that didn't work, it could still be an identifier.  */
12550       if (!cp_parser_parse_definitely (parser))
12551         {
12552           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12553             id = cp_parser_identifier (parser);
12554           else
12555             id = NULL_TREE;
12556         }
12557       else
12558         {
12559           template_id_p = true;
12560           ++num_templates;
12561         }
12562     }
12563
12564   pop_deferring_access_checks ();
12565
12566   if (id)
12567     cp_parser_check_for_invalid_template_id (parser, id);
12568
12569   /* If it's not a `:' or a `{' then we can't really be looking at a
12570      class-head, since a class-head only appears as part of a
12571      class-specifier.  We have to detect this situation before calling
12572      xref_tag, since that has irreversible side-effects.  */
12573   if (!cp_parser_next_token_starts_class_definition_p (parser))
12574     {
12575       cp_parser_error (parser, "expected %<{%> or %<:%>");
12576       return error_mark_node;
12577     }
12578
12579   /* At this point, we're going ahead with the class-specifier, even
12580      if some other problem occurs.  */
12581   cp_parser_commit_to_tentative_parse (parser);
12582   /* Issue the error about the overly-qualified name now.  */
12583   if (qualified_p)
12584     cp_parser_error (parser,
12585                      "global qualification of class name is invalid");
12586   else if (invalid_nested_name_p)
12587     cp_parser_error (parser,
12588                      "qualified name does not name a class");
12589   else if (nested_name_specifier)
12590     {
12591       tree scope;
12592       /* Figure out in what scope the declaration is being placed.  */
12593       scope = current_scope ();
12594       /* If that scope does not contain the scope in which the
12595          class was originally declared, the program is invalid.  */
12596       if (scope && !is_ancestor (scope, nested_name_specifier))
12597         {
12598           error ("declaration of %qD in %qD which does not enclose %qD",
12599                  type, scope, nested_name_specifier);
12600           type = NULL_TREE;
12601           goto done;
12602         }
12603       /* [dcl.meaning]
12604
12605          A declarator-id shall not be qualified exception of the
12606          definition of a ... nested class outside of its class
12607          ... [or] a the definition or explicit instantiation of a
12608          class member of a namespace outside of its namespace.  */
12609       if (scope == nested_name_specifier)
12610         {
12611           pedwarn ("extra qualification ignored");
12612           nested_name_specifier = NULL_TREE;
12613           num_templates = 0;
12614         }
12615     }
12616   /* An explicit-specialization must be preceded by "template <>".  If
12617      it is not, try to recover gracefully.  */
12618   if (at_namespace_scope_p ()
12619       && parser->num_template_parameter_lists == 0
12620       && template_id_p)
12621     {
12622       error ("an explicit specialization must be preceded by %<template <>%>");
12623       invalid_explicit_specialization_p = true;
12624       /* Take the same action that would have been taken by
12625          cp_parser_explicit_specialization.  */
12626       ++parser->num_template_parameter_lists;
12627       begin_specialization ();
12628     }
12629   /* There must be no "return" statements between this point and the
12630      end of this function; set "type "to the correct return value and
12631      use "goto done;" to return.  */
12632   /* Make sure that the right number of template parameters were
12633      present.  */
12634   if (!cp_parser_check_template_parameters (parser, num_templates))
12635     {
12636       /* If something went wrong, there is no point in even trying to
12637          process the class-definition.  */
12638       type = NULL_TREE;
12639       goto done;
12640     }
12641
12642   /* Look up the type.  */
12643   if (template_id_p)
12644     {
12645       type = TREE_TYPE (id);
12646       maybe_process_partial_specialization (type);
12647     }
12648   else if (!nested_name_specifier)
12649     {
12650       /* If the class was unnamed, create a dummy name.  */
12651       if (!id)
12652         id = make_anon_name ();
12653       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12654                        parser->num_template_parameter_lists);
12655     }
12656   else
12657     {
12658       tree class_type;
12659       bool pop_p = false;
12660
12661       /* Given:
12662
12663             template <typename T> struct S { struct T };
12664             template <typename T> struct S<T>::T { };
12665
12666          we will get a TYPENAME_TYPE when processing the definition of
12667          `S::T'.  We need to resolve it to the actual type before we
12668          try to define it.  */
12669       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12670         {
12671           class_type = resolve_typename_type (TREE_TYPE (type),
12672                                               /*only_current_p=*/false);
12673           if (class_type != error_mark_node)
12674             type = TYPE_NAME (class_type);
12675           else
12676             {
12677               cp_parser_error (parser, "could not resolve typename type");
12678               type = error_mark_node;
12679             }
12680         }
12681
12682       maybe_process_partial_specialization (TREE_TYPE (type));
12683       class_type = current_class_type;
12684       /* Enter the scope indicated by the nested-name-specifier.  */
12685       if (nested_name_specifier)
12686         pop_p = push_scope (nested_name_specifier);
12687       /* Get the canonical version of this type.  */
12688       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12689       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12690           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12691         type = push_template_decl (type);
12692       type = TREE_TYPE (type);
12693       if (nested_name_specifier)
12694         {
12695           *nested_name_specifier_p = true;
12696           if (pop_p)
12697             pop_scope (nested_name_specifier);
12698         }
12699     }
12700   /* Indicate whether this class was declared as a `class' or as a
12701      `struct'.  */
12702   if (TREE_CODE (type) == RECORD_TYPE)
12703     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12704   cp_parser_check_class_key (class_key, type);
12705
12706   /* Enter the scope containing the class; the names of base classes
12707      should be looked up in that context.  For example, given:
12708
12709        struct A { struct B {}; struct C; };
12710        struct A::C : B {};
12711
12712      is valid.  */
12713   if (nested_name_specifier)
12714     pop_p = push_scope (nested_name_specifier);
12715
12716   bases = NULL_TREE;
12717
12718   /* Get the list of base-classes, if there is one.  */
12719   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12720     bases = cp_parser_base_clause (parser);
12721
12722   /* Process the base classes.  */
12723   xref_basetypes (type, bases);
12724
12725   /* Leave the scope given by the nested-name-specifier.  We will
12726      enter the class scope itself while processing the members.  */
12727   if (pop_p)
12728     pop_scope (nested_name_specifier);
12729
12730  done:
12731   if (invalid_explicit_specialization_p)
12732     {
12733       end_specialization ();
12734       --parser->num_template_parameter_lists;
12735     }
12736   *attributes_p = attributes;
12737   return type;
12738 }
12739
12740 /* Parse a class-key.
12741
12742    class-key:
12743      class
12744      struct
12745      union
12746
12747    Returns the kind of class-key specified, or none_type to indicate
12748    error.  */
12749
12750 static enum tag_types
12751 cp_parser_class_key (cp_parser* parser)
12752 {
12753   cp_token *token;
12754   enum tag_types tag_type;
12755
12756   /* Look for the class-key.  */
12757   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12758   if (!token)
12759     return none_type;
12760
12761   /* Check to see if the TOKEN is a class-key.  */
12762   tag_type = cp_parser_token_is_class_key (token);
12763   if (!tag_type)
12764     cp_parser_error (parser, "expected class-key");
12765   return tag_type;
12766 }
12767
12768 /* Parse an (optional) member-specification.
12769
12770    member-specification:
12771      member-declaration member-specification [opt]
12772      access-specifier : member-specification [opt]  */
12773
12774 static void
12775 cp_parser_member_specification_opt (cp_parser* parser)
12776 {
12777   while (true)
12778     {
12779       cp_token *token;
12780       enum rid keyword;
12781
12782       /* Peek at the next token.  */
12783       token = cp_lexer_peek_token (parser->lexer);
12784       /* If it's a `}', or EOF then we've seen all the members.  */
12785       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12786         break;
12787
12788       /* See if this token is a keyword.  */
12789       keyword = token->keyword;
12790       switch (keyword)
12791         {
12792         case RID_PUBLIC:
12793         case RID_PROTECTED:
12794         case RID_PRIVATE:
12795           /* Consume the access-specifier.  */
12796           cp_lexer_consume_token (parser->lexer);
12797           /* Remember which access-specifier is active.  */
12798           current_access_specifier = token->value;
12799           /* Look for the `:'.  */
12800           cp_parser_require (parser, CPP_COLON, "`:'");
12801           break;
12802
12803         default:
12804           /* Accept #pragmas at class scope.  */
12805           if (token->type == CPP_PRAGMA)
12806             {
12807               cp_lexer_handle_pragma (parser->lexer);
12808               break;
12809             }
12810
12811           /* Otherwise, the next construction must be a
12812              member-declaration.  */
12813           cp_parser_member_declaration (parser);
12814         }
12815     }
12816 }
12817
12818 /* Parse a member-declaration.
12819
12820    member-declaration:
12821      decl-specifier-seq [opt] member-declarator-list [opt] ;
12822      function-definition ; [opt]
12823      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12824      using-declaration
12825      template-declaration
12826
12827    member-declarator-list:
12828      member-declarator
12829      member-declarator-list , member-declarator
12830
12831    member-declarator:
12832      declarator pure-specifier [opt]
12833      declarator constant-initializer [opt]
12834      identifier [opt] : constant-expression
12835
12836    GNU Extensions:
12837
12838    member-declaration:
12839      __extension__ member-declaration
12840
12841    member-declarator:
12842      declarator attributes [opt] pure-specifier [opt]
12843      declarator attributes [opt] constant-initializer [opt]
12844      identifier [opt] attributes [opt] : constant-expression  */
12845
12846 static void
12847 cp_parser_member_declaration (cp_parser* parser)
12848 {
12849   cp_decl_specifier_seq decl_specifiers;
12850   tree prefix_attributes;
12851   tree decl;
12852   int declares_class_or_enum;
12853   bool friend_p;
12854   cp_token *token;
12855   int saved_pedantic;
12856
12857   /* Check for the `__extension__' keyword.  */
12858   if (cp_parser_extension_opt (parser, &saved_pedantic))
12859     {
12860       /* Recurse.  */
12861       cp_parser_member_declaration (parser);
12862       /* Restore the old value of the PEDANTIC flag.  */
12863       pedantic = saved_pedantic;
12864
12865       return;
12866     }
12867
12868   /* Check for a template-declaration.  */
12869   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12870     {
12871       /* Parse the template-declaration.  */
12872       cp_parser_template_declaration (parser, /*member_p=*/true);
12873
12874       return;
12875     }
12876
12877   /* Check for a using-declaration.  */
12878   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12879     {
12880       /* Parse the using-declaration.  */
12881       cp_parser_using_declaration (parser);
12882
12883       return;
12884     }
12885
12886   /* Parse the decl-specifier-seq.  */
12887   cp_parser_decl_specifier_seq (parser,
12888                                 CP_PARSER_FLAGS_OPTIONAL,
12889                                 &decl_specifiers,
12890                                 &declares_class_or_enum);
12891   prefix_attributes = decl_specifiers.attributes;
12892   decl_specifiers.attributes = NULL_TREE;
12893   /* Check for an invalid type-name.  */
12894   if (!decl_specifiers.type
12895       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12896     return;
12897   /* If there is no declarator, then the decl-specifier-seq should
12898      specify a type.  */
12899   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12900     {
12901       /* If there was no decl-specifier-seq, and the next token is a
12902          `;', then we have something like:
12903
12904            struct S { ; };
12905
12906          [class.mem]
12907
12908          Each member-declaration shall declare at least one member
12909          name of the class.  */
12910       if (!decl_specifiers.any_specifiers_p)
12911         {
12912           cp_token *token = cp_lexer_peek_token (parser->lexer);
12913           if (pedantic && !token->in_system_header)
12914             pedwarn ("%Hextra %<;%>", &token->location);
12915         }
12916       else
12917         {
12918           tree type;
12919
12920           /* See if this declaration is a friend.  */
12921           friend_p = cp_parser_friend_p (&decl_specifiers);
12922           /* If there were decl-specifiers, check to see if there was
12923              a class-declaration.  */
12924           type = check_tag_decl (&decl_specifiers);
12925           /* Nested classes have already been added to the class, but
12926              a `friend' needs to be explicitly registered.  */
12927           if (friend_p)
12928             {
12929               /* If the `friend' keyword was present, the friend must
12930                  be introduced with a class-key.  */
12931                if (!declares_class_or_enum)
12932                  error ("a class-key must be used when declaring a friend");
12933                /* In this case:
12934
12935                     template <typename T> struct A {
12936                       friend struct A<T>::B;
12937                     };
12938
12939                   A<T>::B will be represented by a TYPENAME_TYPE, and
12940                   therefore not recognized by check_tag_decl.  */
12941                if (!type
12942                    && decl_specifiers.type
12943                    && TYPE_P (decl_specifiers.type))
12944                  type = decl_specifiers.type;
12945                if (!type || !TYPE_P (type))
12946                  error ("friend declaration does not name a class or "
12947                         "function");
12948                else
12949                  make_friend_class (current_class_type, type,
12950                                     /*complain=*/true);
12951             }
12952           /* If there is no TYPE, an error message will already have
12953              been issued.  */
12954           else if (!type || type == error_mark_node)
12955             ;
12956           /* An anonymous aggregate has to be handled specially; such
12957              a declaration really declares a data member (with a
12958              particular type), as opposed to a nested class.  */
12959           else if (ANON_AGGR_TYPE_P (type))
12960             {
12961               /* Remove constructors and such from TYPE, now that we
12962                  know it is an anonymous aggregate.  */
12963               fixup_anonymous_aggr (type);
12964               /* And make the corresponding data member.  */
12965               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12966               /* Add it to the class.  */
12967               finish_member_declaration (decl);
12968             }
12969           else
12970             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12971         }
12972     }
12973   else
12974     {
12975       /* See if these declarations will be friends.  */
12976       friend_p = cp_parser_friend_p (&decl_specifiers);
12977
12978       /* Keep going until we hit the `;' at the end of the
12979          declaration.  */
12980       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12981         {
12982           tree attributes = NULL_TREE;
12983           tree first_attribute;
12984
12985           /* Peek at the next token.  */
12986           token = cp_lexer_peek_token (parser->lexer);
12987
12988           /* Check for a bitfield declaration.  */
12989           if (token->type == CPP_COLON
12990               || (token->type == CPP_NAME
12991                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12992                   == CPP_COLON))
12993             {
12994               tree identifier;
12995               tree width;
12996
12997               /* Get the name of the bitfield.  Note that we cannot just
12998                  check TOKEN here because it may have been invalidated by
12999                  the call to cp_lexer_peek_nth_token above.  */
13000               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13001                 identifier = cp_parser_identifier (parser);
13002               else
13003                 identifier = NULL_TREE;
13004
13005               /* Consume the `:' token.  */
13006               cp_lexer_consume_token (parser->lexer);
13007               /* Get the width of the bitfield.  */
13008               width
13009                 = cp_parser_constant_expression (parser,
13010                                                  /*allow_non_constant=*/false,
13011                                                  NULL);
13012
13013               /* Look for attributes that apply to the bitfield.  */
13014               attributes = cp_parser_attributes_opt (parser);
13015               /* Remember which attributes are prefix attributes and
13016                  which are not.  */
13017               first_attribute = attributes;
13018               /* Combine the attributes.  */
13019               attributes = chainon (prefix_attributes, attributes);
13020
13021               /* Create the bitfield declaration.  */
13022               decl = grokbitfield (identifier
13023                                    ? make_id_declarator (identifier)
13024                                    : NULL,
13025                                    &decl_specifiers,
13026                                    width);
13027               /* Apply the attributes.  */
13028               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13029             }
13030           else
13031             {
13032               cp_declarator *declarator;
13033               tree initializer;
13034               tree asm_specification;
13035               int ctor_dtor_or_conv_p;
13036
13037               /* Parse the declarator.  */
13038               declarator
13039                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13040                                         &ctor_dtor_or_conv_p,
13041                                         /*parenthesized_p=*/NULL,
13042                                         /*member_p=*/true);
13043
13044               /* If something went wrong parsing the declarator, make sure
13045                  that we at least consume some tokens.  */
13046               if (declarator == cp_error_declarator)
13047                 {
13048                   /* Skip to the end of the statement.  */
13049                   cp_parser_skip_to_end_of_statement (parser);
13050                   /* If the next token is not a semicolon, that is
13051                      probably because we just skipped over the body of
13052                      a function.  So, we consume a semicolon if
13053                      present, but do not issue an error message if it
13054                      is not present.  */
13055                   if (cp_lexer_next_token_is (parser->lexer,
13056                                               CPP_SEMICOLON))
13057                     cp_lexer_consume_token (parser->lexer);
13058                   return;
13059                 }
13060
13061               cp_parser_check_for_definition_in_return_type
13062                 (declarator, declares_class_or_enum);
13063
13064               /* Look for an asm-specification.  */
13065               asm_specification = cp_parser_asm_specification_opt (parser);
13066               /* Look for attributes that apply to the declaration.  */
13067               attributes = cp_parser_attributes_opt (parser);
13068               /* Remember which attributes are prefix attributes and
13069                  which are not.  */
13070               first_attribute = attributes;
13071               /* Combine the attributes.  */
13072               attributes = chainon (prefix_attributes, attributes);
13073
13074               /* If it's an `=', then we have a constant-initializer or a
13075                  pure-specifier.  It is not correct to parse the
13076                  initializer before registering the member declaration
13077                  since the member declaration should be in scope while
13078                  its initializer is processed.  However, the rest of the
13079                  front end does not yet provide an interface that allows
13080                  us to handle this correctly.  */
13081               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13082                 {
13083                   /* In [class.mem]:
13084
13085                      A pure-specifier shall be used only in the declaration of
13086                      a virtual function.
13087
13088                      A member-declarator can contain a constant-initializer
13089                      only if it declares a static member of integral or
13090                      enumeration type.
13091
13092                      Therefore, if the DECLARATOR is for a function, we look
13093                      for a pure-specifier; otherwise, we look for a
13094                      constant-initializer.  When we call `grokfield', it will
13095                      perform more stringent semantics checks.  */
13096                   if (declarator->kind == cdk_function)
13097                     initializer = cp_parser_pure_specifier (parser);
13098                   else
13099                     /* Parse the initializer.  */
13100                     initializer = cp_parser_constant_initializer (parser);
13101                 }
13102               /* Otherwise, there is no initializer.  */
13103               else
13104                 initializer = NULL_TREE;
13105
13106               /* See if we are probably looking at a function
13107                  definition.  We are certainly not looking at at a
13108                  member-declarator.  Calling `grokfield' has
13109                  side-effects, so we must not do it unless we are sure
13110                  that we are looking at a member-declarator.  */
13111               if (cp_parser_token_starts_function_definition_p
13112                   (cp_lexer_peek_token (parser->lexer)))
13113                 {
13114                   /* The grammar does not allow a pure-specifier to be
13115                      used when a member function is defined.  (It is
13116                      possible that this fact is an oversight in the
13117                      standard, since a pure function may be defined
13118                      outside of the class-specifier.  */
13119                   if (initializer)
13120                     error ("pure-specifier on function-definition");
13121                   decl = cp_parser_save_member_function_body (parser,
13122                                                               &decl_specifiers,
13123                                                               declarator,
13124                                                               attributes);
13125                   /* If the member was not a friend, declare it here.  */
13126                   if (!friend_p)
13127                     finish_member_declaration (decl);
13128                   /* Peek at the next token.  */
13129                   token = cp_lexer_peek_token (parser->lexer);
13130                   /* If the next token is a semicolon, consume it.  */
13131                   if (token->type == CPP_SEMICOLON)
13132                     cp_lexer_consume_token (parser->lexer);
13133                   return;
13134                 }
13135               else
13136                 {
13137                   /* Create the declaration.  */
13138                   decl = grokfield (declarator, &decl_specifiers,
13139                                     initializer, asm_specification,
13140                                     attributes);
13141                   /* Any initialization must have been from a
13142                      constant-expression.  */
13143                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13144                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13145                 }
13146             }
13147
13148           /* Reset PREFIX_ATTRIBUTES.  */
13149           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13150             attributes = TREE_CHAIN (attributes);
13151           if (attributes)
13152             TREE_CHAIN (attributes) = NULL_TREE;
13153
13154           /* If there is any qualification still in effect, clear it
13155              now; we will be starting fresh with the next declarator.  */
13156           parser->scope = NULL_TREE;
13157           parser->qualifying_scope = NULL_TREE;
13158           parser->object_scope = NULL_TREE;
13159           /* If it's a `,', then there are more declarators.  */
13160           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13161             cp_lexer_consume_token (parser->lexer);
13162           /* If the next token isn't a `;', then we have a parse error.  */
13163           else if (cp_lexer_next_token_is_not (parser->lexer,
13164                                                CPP_SEMICOLON))
13165             {
13166               cp_parser_error (parser, "expected %<;%>");
13167               /* Skip tokens until we find a `;'.  */
13168               cp_parser_skip_to_end_of_statement (parser);
13169
13170               break;
13171             }
13172
13173           if (decl)
13174             {
13175               /* Add DECL to the list of members.  */
13176               if (!friend_p)
13177                 finish_member_declaration (decl);
13178
13179               if (TREE_CODE (decl) == FUNCTION_DECL)
13180                 cp_parser_save_default_args (parser, decl);
13181             }
13182         }
13183     }
13184
13185   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13186 }
13187
13188 /* Parse a pure-specifier.
13189
13190    pure-specifier:
13191      = 0
13192
13193    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13194    Otherwise, ERROR_MARK_NODE is returned.  */
13195
13196 static tree
13197 cp_parser_pure_specifier (cp_parser* parser)
13198 {
13199   cp_token *token;
13200
13201   /* Look for the `=' token.  */
13202   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13203     return error_mark_node;
13204   /* Look for the `0' token.  */
13205   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13206   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13207      to get information from the lexer about how the number was
13208      spelled in order to fix this problem.  */
13209   if (!token || !integer_zerop (token->value))
13210     return error_mark_node;
13211
13212   return integer_zero_node;
13213 }
13214
13215 /* Parse a constant-initializer.
13216
13217    constant-initializer:
13218      = constant-expression
13219
13220    Returns a representation of the constant-expression.  */
13221
13222 static tree
13223 cp_parser_constant_initializer (cp_parser* parser)
13224 {
13225   /* Look for the `=' token.  */
13226   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13227     return error_mark_node;
13228
13229   /* It is invalid to write:
13230
13231        struct S { static const int i = { 7 }; };
13232
13233      */
13234   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13235     {
13236       cp_parser_error (parser,
13237                        "a brace-enclosed initializer is not allowed here");
13238       /* Consume the opening brace.  */
13239       cp_lexer_consume_token (parser->lexer);
13240       /* Skip the initializer.  */
13241       cp_parser_skip_to_closing_brace (parser);
13242       /* Look for the trailing `}'.  */
13243       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13244
13245       return error_mark_node;
13246     }
13247
13248   return cp_parser_constant_expression (parser,
13249                                         /*allow_non_constant=*/false,
13250                                         NULL);
13251 }
13252
13253 /* Derived classes [gram.class.derived] */
13254
13255 /* Parse a base-clause.
13256
13257    base-clause:
13258      : base-specifier-list
13259
13260    base-specifier-list:
13261      base-specifier
13262      base-specifier-list , base-specifier
13263
13264    Returns a TREE_LIST representing the base-classes, in the order in
13265    which they were declared.  The representation of each node is as
13266    described by cp_parser_base_specifier.
13267
13268    In the case that no bases are specified, this function will return
13269    NULL_TREE, not ERROR_MARK_NODE.  */
13270
13271 static tree
13272 cp_parser_base_clause (cp_parser* parser)
13273 {
13274   tree bases = NULL_TREE;
13275
13276   /* Look for the `:' that begins the list.  */
13277   cp_parser_require (parser, CPP_COLON, "`:'");
13278
13279   /* Scan the base-specifier-list.  */
13280   while (true)
13281     {
13282       cp_token *token;
13283       tree base;
13284
13285       /* Look for the base-specifier.  */
13286       base = cp_parser_base_specifier (parser);
13287       /* Add BASE to the front of the list.  */
13288       if (base != error_mark_node)
13289         {
13290           TREE_CHAIN (base) = bases;
13291           bases = base;
13292         }
13293       /* Peek at the next token.  */
13294       token = cp_lexer_peek_token (parser->lexer);
13295       /* If it's not a comma, then the list is complete.  */
13296       if (token->type != CPP_COMMA)
13297         break;
13298       /* Consume the `,'.  */
13299       cp_lexer_consume_token (parser->lexer);
13300     }
13301
13302   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13303      base class had a qualified name.  However, the next name that
13304      appears is certainly not qualified.  */
13305   parser->scope = NULL_TREE;
13306   parser->qualifying_scope = NULL_TREE;
13307   parser->object_scope = NULL_TREE;
13308
13309   return nreverse (bases);
13310 }
13311
13312 /* Parse a base-specifier.
13313
13314    base-specifier:
13315      :: [opt] nested-name-specifier [opt] class-name
13316      virtual access-specifier [opt] :: [opt] nested-name-specifier
13317        [opt] class-name
13318      access-specifier virtual [opt] :: [opt] nested-name-specifier
13319        [opt] class-name
13320
13321    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13322    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13323    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13324    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13325
13326 static tree
13327 cp_parser_base_specifier (cp_parser* parser)
13328 {
13329   cp_token *token;
13330   bool done = false;
13331   bool virtual_p = false;
13332   bool duplicate_virtual_error_issued_p = false;
13333   bool duplicate_access_error_issued_p = false;
13334   bool class_scope_p, template_p;
13335   tree access = access_default_node;
13336   tree type;
13337
13338   /* Process the optional `virtual' and `access-specifier'.  */
13339   while (!done)
13340     {
13341       /* Peek at the next token.  */
13342       token = cp_lexer_peek_token (parser->lexer);
13343       /* Process `virtual'.  */
13344       switch (token->keyword)
13345         {
13346         case RID_VIRTUAL:
13347           /* If `virtual' appears more than once, issue an error.  */
13348           if (virtual_p && !duplicate_virtual_error_issued_p)
13349             {
13350               cp_parser_error (parser,
13351                                "%<virtual%> specified more than once in base-specified");
13352               duplicate_virtual_error_issued_p = true;
13353             }
13354
13355           virtual_p = true;
13356
13357           /* Consume the `virtual' token.  */
13358           cp_lexer_consume_token (parser->lexer);
13359
13360           break;
13361
13362         case RID_PUBLIC:
13363         case RID_PROTECTED:
13364         case RID_PRIVATE:
13365           /* If more than one access specifier appears, issue an
13366              error.  */
13367           if (access != access_default_node
13368               && !duplicate_access_error_issued_p)
13369             {
13370               cp_parser_error (parser,
13371                                "more than one access specifier in base-specified");
13372               duplicate_access_error_issued_p = true;
13373             }
13374
13375           access = ridpointers[(int) token->keyword];
13376
13377           /* Consume the access-specifier.  */
13378           cp_lexer_consume_token (parser->lexer);
13379
13380           break;
13381
13382         default:
13383           done = true;
13384           break;
13385         }
13386     }
13387   /* It is not uncommon to see programs mechanically, erroneously, use
13388      the 'typename' keyword to denote (dependent) qualified types
13389      as base classes.  */
13390   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13391     {
13392       if (!processing_template_decl)
13393         error ("keyword %<typename%> not allowed outside of templates");
13394       else
13395         error ("keyword %<typename%> not allowed in this context "
13396                "(the base class is implicitly a type)");
13397       cp_lexer_consume_token (parser->lexer);
13398     }
13399
13400   /* Look for the optional `::' operator.  */
13401   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13402   /* Look for the nested-name-specifier.  The simplest way to
13403      implement:
13404
13405        [temp.res]
13406
13407        The keyword `typename' is not permitted in a base-specifier or
13408        mem-initializer; in these contexts a qualified name that
13409        depends on a template-parameter is implicitly assumed to be a
13410        type name.
13411
13412      is to pretend that we have seen the `typename' keyword at this
13413      point.  */
13414   cp_parser_nested_name_specifier_opt (parser,
13415                                        /*typename_keyword_p=*/true,
13416                                        /*check_dependency_p=*/true,
13417                                        /*type_p=*/true,
13418                                        /*is_declaration=*/true);
13419   /* If the base class is given by a qualified name, assume that names
13420      we see are type names or templates, as appropriate.  */
13421   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13422   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13423
13424   /* Finally, look for the class-name.  */
13425   type = cp_parser_class_name (parser,
13426                                class_scope_p,
13427                                template_p,
13428                                /*type_p=*/true,
13429                                /*check_dependency_p=*/true,
13430                                /*class_head_p=*/false,
13431                                /*is_declaration=*/true);
13432
13433   if (type == error_mark_node)
13434     return error_mark_node;
13435
13436   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13437 }
13438
13439 /* Exception handling [gram.exception] */
13440
13441 /* Parse an (optional) exception-specification.
13442
13443    exception-specification:
13444      throw ( type-id-list [opt] )
13445
13446    Returns a TREE_LIST representing the exception-specification.  The
13447    TREE_VALUE of each node is a type.  */
13448
13449 static tree
13450 cp_parser_exception_specification_opt (cp_parser* parser)
13451 {
13452   cp_token *token;
13453   tree type_id_list;
13454
13455   /* Peek at the next token.  */
13456   token = cp_lexer_peek_token (parser->lexer);
13457   /* If it's not `throw', then there's no exception-specification.  */
13458   if (!cp_parser_is_keyword (token, RID_THROW))
13459     return NULL_TREE;
13460
13461   /* Consume the `throw'.  */
13462   cp_lexer_consume_token (parser->lexer);
13463
13464   /* Look for the `('.  */
13465   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13466
13467   /* Peek at the next token.  */
13468   token = cp_lexer_peek_token (parser->lexer);
13469   /* If it's not a `)', then there is a type-id-list.  */
13470   if (token->type != CPP_CLOSE_PAREN)
13471     {
13472       const char *saved_message;
13473
13474       /* Types may not be defined in an exception-specification.  */
13475       saved_message = parser->type_definition_forbidden_message;
13476       parser->type_definition_forbidden_message
13477         = "types may not be defined in an exception-specification";
13478       /* Parse the type-id-list.  */
13479       type_id_list = cp_parser_type_id_list (parser);
13480       /* Restore the saved message.  */
13481       parser->type_definition_forbidden_message = saved_message;
13482     }
13483   else
13484     type_id_list = empty_except_spec;
13485
13486   /* Look for the `)'.  */
13487   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13488
13489   return type_id_list;
13490 }
13491
13492 /* Parse an (optional) type-id-list.
13493
13494    type-id-list:
13495      type-id
13496      type-id-list , type-id
13497
13498    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13499    in the order that the types were presented.  */
13500
13501 static tree
13502 cp_parser_type_id_list (cp_parser* parser)
13503 {
13504   tree types = NULL_TREE;
13505
13506   while (true)
13507     {
13508       cp_token *token;
13509       tree type;
13510
13511       /* Get the next type-id.  */
13512       type = cp_parser_type_id (parser);
13513       /* Add it to the list.  */
13514       types = add_exception_specifier (types, type, /*complain=*/1);
13515       /* Peek at the next token.  */
13516       token = cp_lexer_peek_token (parser->lexer);
13517       /* If it is not a `,', we are done.  */
13518       if (token->type != CPP_COMMA)
13519         break;
13520       /* Consume the `,'.  */
13521       cp_lexer_consume_token (parser->lexer);
13522     }
13523
13524   return nreverse (types);
13525 }
13526
13527 /* Parse a try-block.
13528
13529    try-block:
13530      try compound-statement handler-seq  */
13531
13532 static tree
13533 cp_parser_try_block (cp_parser* parser)
13534 {
13535   tree try_block;
13536
13537   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13538   try_block = begin_try_block ();
13539   cp_parser_compound_statement (parser, NULL, true);
13540   finish_try_block (try_block);
13541   cp_parser_handler_seq (parser);
13542   finish_handler_sequence (try_block);
13543
13544   return try_block;
13545 }
13546
13547 /* Parse a function-try-block.
13548
13549    function-try-block:
13550      try ctor-initializer [opt] function-body handler-seq  */
13551
13552 static bool
13553 cp_parser_function_try_block (cp_parser* parser)
13554 {
13555   tree try_block;
13556   bool ctor_initializer_p;
13557
13558   /* Look for the `try' keyword.  */
13559   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13560     return false;
13561   /* Let the rest of the front-end know where we are.  */
13562   try_block = begin_function_try_block ();
13563   /* Parse the function-body.  */
13564   ctor_initializer_p
13565     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13566   /* We're done with the `try' part.  */
13567   finish_function_try_block (try_block);
13568   /* Parse the handlers.  */
13569   cp_parser_handler_seq (parser);
13570   /* We're done with the handlers.  */
13571   finish_function_handler_sequence (try_block);
13572
13573   return ctor_initializer_p;
13574 }
13575
13576 /* Parse a handler-seq.
13577
13578    handler-seq:
13579      handler handler-seq [opt]  */
13580
13581 static void
13582 cp_parser_handler_seq (cp_parser* parser)
13583 {
13584   while (true)
13585     {
13586       cp_token *token;
13587
13588       /* Parse the handler.  */
13589       cp_parser_handler (parser);
13590       /* Peek at the next token.  */
13591       token = cp_lexer_peek_token (parser->lexer);
13592       /* If it's not `catch' then there are no more handlers.  */
13593       if (!cp_parser_is_keyword (token, RID_CATCH))
13594         break;
13595     }
13596 }
13597
13598 /* Parse a handler.
13599
13600    handler:
13601      catch ( exception-declaration ) compound-statement  */
13602
13603 static void
13604 cp_parser_handler (cp_parser* parser)
13605 {
13606   tree handler;
13607   tree declaration;
13608
13609   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13610   handler = begin_handler ();
13611   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13612   declaration = cp_parser_exception_declaration (parser);
13613   finish_handler_parms (declaration, handler);
13614   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13615   cp_parser_compound_statement (parser, NULL, false);
13616   finish_handler (handler);
13617 }
13618
13619 /* Parse an exception-declaration.
13620
13621    exception-declaration:
13622      type-specifier-seq declarator
13623      type-specifier-seq abstract-declarator
13624      type-specifier-seq
13625      ...
13626
13627    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13628    ellipsis variant is used.  */
13629
13630 static tree
13631 cp_parser_exception_declaration (cp_parser* parser)
13632 {
13633   tree decl;
13634   cp_decl_specifier_seq type_specifiers;
13635   cp_declarator *declarator;
13636   const char *saved_message;
13637
13638   /* If it's an ellipsis, it's easy to handle.  */
13639   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13640     {
13641       /* Consume the `...' token.  */
13642       cp_lexer_consume_token (parser->lexer);
13643       return NULL_TREE;
13644     }
13645
13646   /* Types may not be defined in exception-declarations.  */
13647   saved_message = parser->type_definition_forbidden_message;
13648   parser->type_definition_forbidden_message
13649     = "types may not be defined in exception-declarations";
13650
13651   /* Parse the type-specifier-seq.  */
13652   cp_parser_type_specifier_seq (parser, &type_specifiers);
13653   /* If it's a `)', then there is no declarator.  */
13654   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13655     declarator = NULL;
13656   else
13657     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13658                                        /*ctor_dtor_or_conv_p=*/NULL,
13659                                        /*parenthesized_p=*/NULL,
13660                                        /*member_p=*/false);
13661
13662   /* Restore the saved message.  */
13663   parser->type_definition_forbidden_message = saved_message;
13664
13665   if (type_specifiers.any_specifiers_p)
13666     {
13667       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13668       if (decl == NULL_TREE)
13669         error ("invalid catch parameter");
13670     }
13671   else
13672     decl = NULL_TREE;
13673
13674   return decl;
13675 }
13676
13677 /* Parse a throw-expression.
13678
13679    throw-expression:
13680      throw assignment-expression [opt]
13681
13682    Returns a THROW_EXPR representing the throw-expression.  */
13683
13684 static tree
13685 cp_parser_throw_expression (cp_parser* parser)
13686 {
13687   tree expression;
13688   cp_token* token;
13689
13690   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13691   token = cp_lexer_peek_token (parser->lexer);
13692   /* Figure out whether or not there is an assignment-expression
13693      following the "throw" keyword.  */
13694   if (token->type == CPP_COMMA
13695       || token->type == CPP_SEMICOLON
13696       || token->type == CPP_CLOSE_PAREN
13697       || token->type == CPP_CLOSE_SQUARE
13698       || token->type == CPP_CLOSE_BRACE
13699       || token->type == CPP_COLON)
13700     expression = NULL_TREE;
13701   else
13702     expression = cp_parser_assignment_expression (parser);
13703
13704   return build_throw (expression);
13705 }
13706
13707 /* GNU Extensions */
13708
13709 /* Parse an (optional) asm-specification.
13710
13711    asm-specification:
13712      asm ( string-literal )
13713
13714    If the asm-specification is present, returns a STRING_CST
13715    corresponding to the string-literal.  Otherwise, returns
13716    NULL_TREE.  */
13717
13718 static tree
13719 cp_parser_asm_specification_opt (cp_parser* parser)
13720 {
13721   cp_token *token;
13722   tree asm_specification;
13723
13724   /* Peek at the next token.  */
13725   token = cp_lexer_peek_token (parser->lexer);
13726   /* If the next token isn't the `asm' keyword, then there's no
13727      asm-specification.  */
13728   if (!cp_parser_is_keyword (token, RID_ASM))
13729     return NULL_TREE;
13730
13731   /* Consume the `asm' token.  */
13732   cp_lexer_consume_token (parser->lexer);
13733   /* Look for the `('.  */
13734   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13735
13736   /* Look for the string-literal.  */
13737   asm_specification = cp_parser_string_literal (parser, false, false);
13738
13739   /* Look for the `)'.  */
13740   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13741
13742   return asm_specification;
13743 }
13744
13745 /* Parse an asm-operand-list.
13746
13747    asm-operand-list:
13748      asm-operand
13749      asm-operand-list , asm-operand
13750
13751    asm-operand:
13752      string-literal ( expression )
13753      [ string-literal ] string-literal ( expression )
13754
13755    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13756    each node is the expression.  The TREE_PURPOSE is itself a
13757    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13758    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13759    is a STRING_CST for the string literal before the parenthesis.  */
13760
13761 static tree
13762 cp_parser_asm_operand_list (cp_parser* parser)
13763 {
13764   tree asm_operands = NULL_TREE;
13765
13766   while (true)
13767     {
13768       tree string_literal;
13769       tree expression;
13770       tree name;
13771
13772       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13773         {
13774           /* Consume the `[' token.  */
13775           cp_lexer_consume_token (parser->lexer);
13776           /* Read the operand name.  */
13777           name = cp_parser_identifier (parser);
13778           if (name != error_mark_node)
13779             name = build_string (IDENTIFIER_LENGTH (name),
13780                                  IDENTIFIER_POINTER (name));
13781           /* Look for the closing `]'.  */
13782           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13783         }
13784       else
13785         name = NULL_TREE;
13786       /* Look for the string-literal.  */
13787       string_literal = cp_parser_string_literal (parser, false, false);
13788
13789       /* Look for the `('.  */
13790       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13791       /* Parse the expression.  */
13792       expression = cp_parser_expression (parser);
13793       /* Look for the `)'.  */
13794       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13795
13796       /* Add this operand to the list.  */
13797       asm_operands = tree_cons (build_tree_list (name, string_literal),
13798                                 expression,
13799                                 asm_operands);
13800       /* If the next token is not a `,', there are no more
13801          operands.  */
13802       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13803         break;
13804       /* Consume the `,'.  */
13805       cp_lexer_consume_token (parser->lexer);
13806     }
13807
13808   return nreverse (asm_operands);
13809 }
13810
13811 /* Parse an asm-clobber-list.
13812
13813    asm-clobber-list:
13814      string-literal
13815      asm-clobber-list , string-literal
13816
13817    Returns a TREE_LIST, indicating the clobbers in the order that they
13818    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13819
13820 static tree
13821 cp_parser_asm_clobber_list (cp_parser* parser)
13822 {
13823   tree clobbers = NULL_TREE;
13824
13825   while (true)
13826     {
13827       tree string_literal;
13828
13829       /* Look for the string literal.  */
13830       string_literal = cp_parser_string_literal (parser, false, false);
13831       /* Add it to the list.  */
13832       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13833       /* If the next token is not a `,', then the list is
13834          complete.  */
13835       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13836         break;
13837       /* Consume the `,' token.  */
13838       cp_lexer_consume_token (parser->lexer);
13839     }
13840
13841   return clobbers;
13842 }
13843
13844 /* Parse an (optional) series of attributes.
13845
13846    attributes:
13847      attributes attribute
13848
13849    attribute:
13850      __attribute__ (( attribute-list [opt] ))
13851
13852    The return value is as for cp_parser_attribute_list.  */
13853
13854 static tree
13855 cp_parser_attributes_opt (cp_parser* parser)
13856 {
13857   tree attributes = NULL_TREE;
13858
13859   while (true)
13860     {
13861       cp_token *token;
13862       tree attribute_list;
13863
13864       /* Peek at the next token.  */
13865       token = cp_lexer_peek_token (parser->lexer);
13866       /* If it's not `__attribute__', then we're done.  */
13867       if (token->keyword != RID_ATTRIBUTE)
13868         break;
13869
13870       /* Consume the `__attribute__' keyword.  */
13871       cp_lexer_consume_token (parser->lexer);
13872       /* Look for the two `(' tokens.  */
13873       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13874       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13875
13876       /* Peek at the next token.  */
13877       token = cp_lexer_peek_token (parser->lexer);
13878       if (token->type != CPP_CLOSE_PAREN)
13879         /* Parse the attribute-list.  */
13880         attribute_list = cp_parser_attribute_list (parser);
13881       else
13882         /* If the next token is a `)', then there is no attribute
13883            list.  */
13884         attribute_list = NULL;
13885
13886       /* Look for the two `)' tokens.  */
13887       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13888       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13889
13890       /* Add these new attributes to the list.  */
13891       attributes = chainon (attributes, attribute_list);
13892     }
13893
13894   return attributes;
13895 }
13896
13897 /* Parse an attribute-list.
13898
13899    attribute-list:
13900      attribute
13901      attribute-list , attribute
13902
13903    attribute:
13904      identifier
13905      identifier ( identifier )
13906      identifier ( identifier , expression-list )
13907      identifier ( expression-list )
13908
13909    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13910    TREE_PURPOSE of each node is the identifier indicating which
13911    attribute is in use.  The TREE_VALUE represents the arguments, if
13912    any.  */
13913
13914 static tree
13915 cp_parser_attribute_list (cp_parser* parser)
13916 {
13917   tree attribute_list = NULL_TREE;
13918   bool save_translate_strings_p = parser->translate_strings_p;
13919
13920   parser->translate_strings_p = false;
13921   while (true)
13922     {
13923       cp_token *token;
13924       tree identifier;
13925       tree attribute;
13926
13927       /* Look for the identifier.  We also allow keywords here; for
13928          example `__attribute__ ((const))' is legal.  */
13929       token = cp_lexer_peek_token (parser->lexer);
13930       if (token->type != CPP_NAME
13931           && token->type != CPP_KEYWORD)
13932         return error_mark_node;
13933       /* Consume the token.  */
13934       token = cp_lexer_consume_token (parser->lexer);
13935
13936       /* Save away the identifier that indicates which attribute this is.  */
13937       identifier = token->value;
13938       attribute = build_tree_list (identifier, NULL_TREE);
13939
13940       /* Peek at the next token.  */
13941       token = cp_lexer_peek_token (parser->lexer);
13942       /* If it's an `(', then parse the attribute arguments.  */
13943       if (token->type == CPP_OPEN_PAREN)
13944         {
13945           tree arguments;
13946
13947           arguments = (cp_parser_parenthesized_expression_list
13948                        (parser, true, /*non_constant_p=*/NULL));
13949           /* Save the identifier and arguments away.  */
13950           TREE_VALUE (attribute) = arguments;
13951         }
13952
13953       /* Add this attribute to the list.  */
13954       TREE_CHAIN (attribute) = attribute_list;
13955       attribute_list = attribute;
13956
13957       /* Now, look for more attributes.  */
13958       token = cp_lexer_peek_token (parser->lexer);
13959       /* If the next token isn't a `,', we're done.  */
13960       if (token->type != CPP_COMMA)
13961         break;
13962
13963       /* Consume the comma and keep going.  */
13964       cp_lexer_consume_token (parser->lexer);
13965     }
13966   parser->translate_strings_p = save_translate_strings_p;
13967
13968   /* We built up the list in reverse order.  */
13969   return nreverse (attribute_list);
13970 }
13971
13972 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13973    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13974    current value of the PEDANTIC flag, regardless of whether or not
13975    the `__extension__' keyword is present.  The caller is responsible
13976    for restoring the value of the PEDANTIC flag.  */
13977
13978 static bool
13979 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13980 {
13981   /* Save the old value of the PEDANTIC flag.  */
13982   *saved_pedantic = pedantic;
13983
13984   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13985     {
13986       /* Consume the `__extension__' token.  */
13987       cp_lexer_consume_token (parser->lexer);
13988       /* We're not being pedantic while the `__extension__' keyword is
13989          in effect.  */
13990       pedantic = 0;
13991
13992       return true;
13993     }
13994
13995   return false;
13996 }
13997
13998 /* Parse a label declaration.
13999
14000    label-declaration:
14001      __label__ label-declarator-seq ;
14002
14003    label-declarator-seq:
14004      identifier , label-declarator-seq
14005      identifier  */
14006
14007 static void
14008 cp_parser_label_declaration (cp_parser* parser)
14009 {
14010   /* Look for the `__label__' keyword.  */
14011   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14012
14013   while (true)
14014     {
14015       tree identifier;
14016
14017       /* Look for an identifier.  */
14018       identifier = cp_parser_identifier (parser);
14019       /* Declare it as a lobel.  */
14020       finish_label_decl (identifier);
14021       /* If the next token is a `;', stop.  */
14022       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14023         break;
14024       /* Look for the `,' separating the label declarations.  */
14025       cp_parser_require (parser, CPP_COMMA, "`,'");
14026     }
14027
14028   /* Look for the final `;'.  */
14029   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14030 }
14031
14032 /* Support Functions */
14033
14034 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14035    NAME should have one of the representations used for an
14036    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14037    is returned.  If PARSER->SCOPE is a dependent type, then a
14038    SCOPE_REF is returned.
14039
14040    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14041    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14042    was formed.  Abstractly, such entities should not be passed to this
14043    function, because they do not need to be looked up, but it is
14044    simpler to check for this special case here, rather than at the
14045    call-sites.
14046
14047    In cases not explicitly covered above, this function returns a
14048    DECL, OVERLOAD, or baselink representing the result of the lookup.
14049    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14050    is returned.
14051
14052    If IS_TYPE is TRUE, bindings that do not refer to types are
14053    ignored.
14054
14055    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14056    ignored.
14057
14058    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14059    are ignored.
14060
14061    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14062    types.  
14063
14064    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14065    results in an ambiguity, and false otherwise.  */
14066
14067 static tree
14068 cp_parser_lookup_name (cp_parser *parser, tree name,
14069                        bool is_type, bool is_template, bool is_namespace,
14070                        bool check_dependency,
14071                        bool *ambiguous_p)
14072 {
14073   tree decl;
14074   tree object_type = parser->context->object_type;
14075
14076   /* Assume that the lookup will be unambiguous.  */
14077   if (ambiguous_p)
14078     *ambiguous_p = false;
14079
14080   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14081      no longer valid.  Note that if we are parsing tentatively, and
14082      the parse fails, OBJECT_TYPE will be automatically restored.  */
14083   parser->context->object_type = NULL_TREE;
14084
14085   if (name == error_mark_node)
14086     return error_mark_node;
14087
14088   /* A template-id has already been resolved; there is no lookup to
14089      do.  */
14090   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14091     return name;
14092   if (BASELINK_P (name))
14093     {
14094       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14095                   == TEMPLATE_ID_EXPR);
14096       return name;
14097     }
14098
14099   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14100      it should already have been checked to make sure that the name
14101      used matches the type being destroyed.  */
14102   if (TREE_CODE (name) == BIT_NOT_EXPR)
14103     {
14104       tree type;
14105
14106       /* Figure out to which type this destructor applies.  */
14107       if (parser->scope)
14108         type = parser->scope;
14109       else if (object_type)
14110         type = object_type;
14111       else
14112         type = current_class_type;
14113       /* If that's not a class type, there is no destructor.  */
14114       if (!type || !CLASS_TYPE_P (type))
14115         return error_mark_node;
14116       if (!CLASSTYPE_DESTRUCTORS (type))
14117           return error_mark_node;
14118       /* If it was a class type, return the destructor.  */
14119       return CLASSTYPE_DESTRUCTORS (type);
14120     }
14121
14122   /* By this point, the NAME should be an ordinary identifier.  If
14123      the id-expression was a qualified name, the qualifying scope is
14124      stored in PARSER->SCOPE at this point.  */
14125   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14126
14127   /* Perform the lookup.  */
14128   if (parser->scope)
14129     {
14130       bool dependent_p;
14131
14132       if (parser->scope == error_mark_node)
14133         return error_mark_node;
14134
14135       /* If the SCOPE is dependent, the lookup must be deferred until
14136          the template is instantiated -- unless we are explicitly
14137          looking up names in uninstantiated templates.  Even then, we
14138          cannot look up the name if the scope is not a class type; it
14139          might, for example, be a template type parameter.  */
14140       dependent_p = (TYPE_P (parser->scope)
14141                      && !(parser->in_declarator_p
14142                           && currently_open_class (parser->scope))
14143                      && dependent_type_p (parser->scope));
14144       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14145            && dependent_p)
14146         {
14147           if (is_type)
14148             /* The resolution to Core Issue 180 says that `struct A::B'
14149                should be considered a type-name, even if `A' is
14150                dependent.  */
14151             decl = TYPE_NAME (make_typename_type (parser->scope,
14152                                                   name,
14153                                                   /*complain=*/1));
14154           else if (is_template)
14155             decl = make_unbound_class_template (parser->scope,
14156                                                 name, NULL_TREE,
14157                                                 /*complain=*/1);
14158           else
14159             decl = build_nt (SCOPE_REF, parser->scope, name);
14160         }
14161       else
14162         {
14163           bool pop_p = false;
14164
14165           /* If PARSER->SCOPE is a dependent type, then it must be a
14166              class type, and we must not be checking dependencies;
14167              otherwise, we would have processed this lookup above.  So
14168              that PARSER->SCOPE is not considered a dependent base by
14169              lookup_member, we must enter the scope here.  */
14170           if (dependent_p)
14171             pop_p = push_scope (parser->scope);
14172           /* If the PARSER->SCOPE is a a template specialization, it
14173              may be instantiated during name lookup.  In that case,
14174              errors may be issued.  Even if we rollback the current
14175              tentative parse, those errors are valid.  */
14176           decl = lookup_qualified_name (parser->scope, name, is_type,
14177                                         /*complain=*/true);
14178           if (pop_p)
14179             pop_scope (parser->scope);
14180         }
14181       parser->qualifying_scope = parser->scope;
14182       parser->object_scope = NULL_TREE;
14183     }
14184   else if (object_type)
14185     {
14186       tree object_decl = NULL_TREE;
14187       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14188          OBJECT_TYPE is not a class.  */
14189       if (CLASS_TYPE_P (object_type))
14190         /* If the OBJECT_TYPE is a template specialization, it may
14191            be instantiated during name lookup.  In that case, errors
14192            may be issued.  Even if we rollback the current tentative
14193            parse, those errors are valid.  */
14194         object_decl = lookup_member (object_type,
14195                                      name,
14196                                      /*protect=*/0, is_type);
14197       /* Look it up in the enclosing context, too.  */
14198       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14199                                /*block_p=*/true, is_namespace,
14200                                /*flags=*/0);
14201       parser->object_scope = object_type;
14202       parser->qualifying_scope = NULL_TREE;
14203       if (object_decl)
14204         decl = object_decl;
14205     }
14206   else
14207     {
14208       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14209                                /*block_p=*/true, is_namespace,
14210                                /*flags=*/0);
14211       parser->qualifying_scope = NULL_TREE;
14212       parser->object_scope = NULL_TREE;
14213     }
14214
14215   /* If the lookup failed, let our caller know.  */
14216   if (!decl
14217       || decl == error_mark_node
14218       || (TREE_CODE (decl) == FUNCTION_DECL
14219           && DECL_ANTICIPATED (decl)))
14220     return error_mark_node;
14221
14222   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14223   if (TREE_CODE (decl) == TREE_LIST)
14224     {
14225       if (ambiguous_p)
14226         *ambiguous_p = true;
14227       /* The error message we have to print is too complicated for
14228          cp_parser_error, so we incorporate its actions directly.  */
14229       if (!cp_parser_simulate_error (parser))
14230         {
14231           error ("reference to %qD is ambiguous", name);
14232           print_candidates (decl);
14233         }
14234       return error_mark_node;
14235     }
14236
14237   gcc_assert (DECL_P (decl)
14238               || TREE_CODE (decl) == OVERLOAD
14239               || TREE_CODE (decl) == SCOPE_REF
14240               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14241               || BASELINK_P (decl));
14242
14243   /* If we have resolved the name of a member declaration, check to
14244      see if the declaration is accessible.  When the name resolves to
14245      set of overloaded functions, accessibility is checked when
14246      overload resolution is done.
14247
14248      During an explicit instantiation, access is not checked at all,
14249      as per [temp.explicit].  */
14250   if (DECL_P (decl))
14251     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14252
14253   return decl;
14254 }
14255
14256 /* Like cp_parser_lookup_name, but for use in the typical case where
14257    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14258    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14259
14260 static tree
14261 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14262 {
14263   return cp_parser_lookup_name (parser, name,
14264                                 /*is_type=*/false,
14265                                 /*is_template=*/false,
14266                                 /*is_namespace=*/false,
14267                                 /*check_dependency=*/true,
14268                                 /*ambiguous_p=*/NULL);
14269 }
14270
14271 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14272    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14273    true, the DECL indicates the class being defined in a class-head,
14274    or declared in an elaborated-type-specifier.
14275
14276    Otherwise, return DECL.  */
14277
14278 static tree
14279 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14280 {
14281   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14282      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14283
14284        struct A {
14285          template <typename T> struct B;
14286        };
14287
14288        template <typename T> struct A::B {};
14289
14290      Similarly, in a elaborated-type-specifier:
14291
14292        namespace N { struct X{}; }
14293
14294        struct A {
14295          template <typename T> friend struct N::X;
14296        };
14297
14298      However, if the DECL refers to a class type, and we are in
14299      the scope of the class, then the name lookup automatically
14300      finds the TYPE_DECL created by build_self_reference rather
14301      than a TEMPLATE_DECL.  For example, in:
14302
14303        template <class T> struct S {
14304          S s;
14305        };
14306
14307      there is no need to handle such case.  */
14308
14309   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14310     return DECL_TEMPLATE_RESULT (decl);
14311
14312   return decl;
14313 }
14314
14315 /* If too many, or too few, template-parameter lists apply to the
14316    declarator, issue an error message.  Returns TRUE if all went well,
14317    and FALSE otherwise.  */
14318
14319 static bool
14320 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14321                                                 cp_declarator *declarator)
14322 {
14323   unsigned num_templates;
14324
14325   /* We haven't seen any classes that involve template parameters yet.  */
14326   num_templates = 0;
14327
14328   switch (declarator->kind)
14329     {
14330     case cdk_id:
14331       if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14332         {
14333           tree scope;
14334           tree member;
14335
14336           scope = TREE_OPERAND (declarator->u.id.name, 0);
14337           member = TREE_OPERAND (declarator->u.id.name, 1);
14338
14339           while (scope && CLASS_TYPE_P (scope))
14340             {
14341               /* You're supposed to have one `template <...>'
14342                  for every template class, but you don't need one
14343                  for a full specialization.  For example:
14344
14345                  template <class T> struct S{};
14346                  template <> struct S<int> { void f(); };
14347                  void S<int>::f () {}
14348
14349                  is correct; there shouldn't be a `template <>' for
14350                  the definition of `S<int>::f'.  */
14351               if (CLASSTYPE_TEMPLATE_INFO (scope)
14352                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14353                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14354                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14355                 ++num_templates;
14356
14357               scope = TYPE_CONTEXT (scope);
14358             }
14359         }
14360
14361       /* If the DECLARATOR has the form `X<y>' then it uses one
14362          additional level of template parameters.  */
14363       if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14364         ++num_templates;
14365
14366       return cp_parser_check_template_parameters (parser,
14367                                                   num_templates);
14368
14369     case cdk_function:
14370     case cdk_array:
14371     case cdk_pointer:
14372     case cdk_reference:
14373     case cdk_ptrmem:
14374       return (cp_parser_check_declarator_template_parameters
14375               (parser, declarator->declarator));
14376
14377     case cdk_error:
14378       return true;
14379
14380     default:
14381       gcc_unreachable ();
14382     }
14383   return false;
14384 }
14385
14386 /* NUM_TEMPLATES were used in the current declaration.  If that is
14387    invalid, return FALSE and issue an error messages.  Otherwise,
14388    return TRUE.  */
14389
14390 static bool
14391 cp_parser_check_template_parameters (cp_parser* parser,
14392                                      unsigned num_templates)
14393 {
14394   /* If there are more template classes than parameter lists, we have
14395      something like:
14396
14397        template <class T> void S<T>::R<T>::f ();  */
14398   if (parser->num_template_parameter_lists < num_templates)
14399     {
14400       error ("too few template-parameter-lists");
14401       return false;
14402     }
14403   /* If there are the same number of template classes and parameter
14404      lists, that's OK.  */
14405   if (parser->num_template_parameter_lists == num_templates)
14406     return true;
14407   /* If there are more, but only one more, then we are referring to a
14408      member template.  That's OK too.  */
14409   if (parser->num_template_parameter_lists == num_templates + 1)
14410       return true;
14411   /* Otherwise, there are too many template parameter lists.  We have
14412      something like:
14413
14414      template <class T> template <class U> void S::f();  */
14415   error ("too many template-parameter-lists");
14416   return false;
14417 }
14418
14419 /* Parse an optional `::' token indicating that the following name is
14420    from the global namespace.  If so, PARSER->SCOPE is set to the
14421    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14422    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14423    Returns the new value of PARSER->SCOPE, if the `::' token is
14424    present, and NULL_TREE otherwise.  */
14425
14426 static tree
14427 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14428 {
14429   cp_token *token;
14430
14431   /* Peek at the next token.  */
14432   token = cp_lexer_peek_token (parser->lexer);
14433   /* If we're looking at a `::' token then we're starting from the
14434      global namespace, not our current location.  */
14435   if (token->type == CPP_SCOPE)
14436     {
14437       /* Consume the `::' token.  */
14438       cp_lexer_consume_token (parser->lexer);
14439       /* Set the SCOPE so that we know where to start the lookup.  */
14440       parser->scope = global_namespace;
14441       parser->qualifying_scope = global_namespace;
14442       parser->object_scope = NULL_TREE;
14443
14444       return parser->scope;
14445     }
14446   else if (!current_scope_valid_p)
14447     {
14448       parser->scope = NULL_TREE;
14449       parser->qualifying_scope = NULL_TREE;
14450       parser->object_scope = NULL_TREE;
14451     }
14452
14453   return NULL_TREE;
14454 }
14455
14456 /* Returns TRUE if the upcoming token sequence is the start of a
14457    constructor declarator.  If FRIEND_P is true, the declarator is
14458    preceded by the `friend' specifier.  */
14459
14460 static bool
14461 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14462 {
14463   bool constructor_p;
14464   tree type_decl = NULL_TREE;
14465   bool nested_name_p;
14466   cp_token *next_token;
14467
14468   /* The common case is that this is not a constructor declarator, so
14469      try to avoid doing lots of work if at all possible.  It's not
14470      valid declare a constructor at function scope.  */
14471   if (at_function_scope_p ())
14472     return false;
14473   /* And only certain tokens can begin a constructor declarator.  */
14474   next_token = cp_lexer_peek_token (parser->lexer);
14475   if (next_token->type != CPP_NAME
14476       && next_token->type != CPP_SCOPE
14477       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14478       && next_token->type != CPP_TEMPLATE_ID)
14479     return false;
14480
14481   /* Parse tentatively; we are going to roll back all of the tokens
14482      consumed here.  */
14483   cp_parser_parse_tentatively (parser);
14484   /* Assume that we are looking at a constructor declarator.  */
14485   constructor_p = true;
14486
14487   /* Look for the optional `::' operator.  */
14488   cp_parser_global_scope_opt (parser,
14489                               /*current_scope_valid_p=*/false);
14490   /* Look for the nested-name-specifier.  */
14491   nested_name_p
14492     = (cp_parser_nested_name_specifier_opt (parser,
14493                                             /*typename_keyword_p=*/false,
14494                                             /*check_dependency_p=*/false,
14495                                             /*type_p=*/false,
14496                                             /*is_declaration=*/false)
14497        != NULL_TREE);
14498   /* Outside of a class-specifier, there must be a
14499      nested-name-specifier.  */
14500   if (!nested_name_p &&
14501       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14502        || friend_p))
14503     constructor_p = false;
14504   /* If we still think that this might be a constructor-declarator,
14505      look for a class-name.  */
14506   if (constructor_p)
14507     {
14508       /* If we have:
14509
14510            template <typename T> struct S { S(); };
14511            template <typename T> S<T>::S ();
14512
14513          we must recognize that the nested `S' names a class.
14514          Similarly, for:
14515
14516            template <typename T> S<T>::S<T> ();
14517
14518          we must recognize that the nested `S' names a template.  */
14519       type_decl = cp_parser_class_name (parser,
14520                                         /*typename_keyword_p=*/false,
14521                                         /*template_keyword_p=*/false,
14522                                         /*type_p=*/false,
14523                                         /*check_dependency_p=*/false,
14524                                         /*class_head_p=*/false,
14525                                         /*is_declaration=*/false);
14526       /* If there was no class-name, then this is not a constructor.  */
14527       constructor_p = !cp_parser_error_occurred (parser);
14528     }
14529
14530   /* If we're still considering a constructor, we have to see a `(',
14531      to begin the parameter-declaration-clause, followed by either a
14532      `)', an `...', or a decl-specifier.  We need to check for a
14533      type-specifier to avoid being fooled into thinking that:
14534
14535        S::S (f) (int);
14536
14537      is a constructor.  (It is actually a function named `f' that
14538      takes one parameter (of type `int') and returns a value of type
14539      `S::S'.  */
14540   if (constructor_p
14541       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14542     {
14543       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14544           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14545           /* A parameter declaration begins with a decl-specifier,
14546              which is either the "attribute" keyword, a storage class
14547              specifier, or (usually) a type-specifier.  */
14548           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14549           && !cp_parser_storage_class_specifier_opt (parser))
14550         {
14551           tree type;
14552           bool pop_p = false;
14553           unsigned saved_num_template_parameter_lists;
14554
14555           /* Names appearing in the type-specifier should be looked up
14556              in the scope of the class.  */
14557           if (current_class_type)
14558             type = NULL_TREE;
14559           else
14560             {
14561               type = TREE_TYPE (type_decl);
14562               if (TREE_CODE (type) == TYPENAME_TYPE)
14563                 {
14564                   type = resolve_typename_type (type,
14565                                                 /*only_current_p=*/false);
14566                   if (type == error_mark_node)
14567                     {
14568                       cp_parser_abort_tentative_parse (parser);
14569                       return false;
14570                     }
14571                 }
14572               pop_p = push_scope (type);
14573             }
14574
14575           /* Inside the constructor parameter list, surrounding
14576              template-parameter-lists do not apply.  */
14577           saved_num_template_parameter_lists
14578             = parser->num_template_parameter_lists;
14579           parser->num_template_parameter_lists = 0;
14580
14581           /* Look for the type-specifier.  */
14582           cp_parser_type_specifier (parser,
14583                                     CP_PARSER_FLAGS_NONE,
14584                                     /*decl_specs=*/NULL,
14585                                     /*is_declarator=*/true,
14586                                     /*declares_class_or_enum=*/NULL,
14587                                     /*is_cv_qualifier=*/NULL);
14588
14589           parser->num_template_parameter_lists
14590             = saved_num_template_parameter_lists;
14591
14592           /* Leave the scope of the class.  */
14593           if (pop_p)
14594             pop_scope (type);
14595
14596           constructor_p = !cp_parser_error_occurred (parser);
14597         }
14598     }
14599   else
14600     constructor_p = false;
14601   /* We did not really want to consume any tokens.  */
14602   cp_parser_abort_tentative_parse (parser);
14603
14604   return constructor_p;
14605 }
14606
14607 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14608    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14609    they must be performed once we are in the scope of the function.
14610
14611    Returns the function defined.  */
14612
14613 static tree
14614 cp_parser_function_definition_from_specifiers_and_declarator
14615   (cp_parser* parser,
14616    cp_decl_specifier_seq *decl_specifiers,
14617    tree attributes,
14618    const cp_declarator *declarator)
14619 {
14620   tree fn;
14621   bool success_p;
14622
14623   /* Begin the function-definition.  */
14624   success_p = start_function (decl_specifiers, declarator, attributes);
14625
14626   /* The things we're about to see are not directly qualified by any
14627      template headers we've seen thus far.  */
14628   reset_specialization ();
14629
14630   /* If there were names looked up in the decl-specifier-seq that we
14631      did not check, check them now.  We must wait until we are in the
14632      scope of the function to perform the checks, since the function
14633      might be a friend.  */
14634   perform_deferred_access_checks ();
14635
14636   if (!success_p)
14637     {
14638       /* Skip the entire function.  */
14639       error ("invalid function declaration");
14640       cp_parser_skip_to_end_of_block_or_statement (parser);
14641       fn = error_mark_node;
14642     }
14643   else
14644     fn = cp_parser_function_definition_after_declarator (parser,
14645                                                          /*inline_p=*/false);
14646
14647   return fn;
14648 }
14649
14650 /* Parse the part of a function-definition that follows the
14651    declarator.  INLINE_P is TRUE iff this function is an inline
14652    function defined with a class-specifier.
14653
14654    Returns the function defined.  */
14655
14656 static tree
14657 cp_parser_function_definition_after_declarator (cp_parser* parser,
14658                                                 bool inline_p)
14659 {
14660   tree fn;
14661   bool ctor_initializer_p = false;
14662   bool saved_in_unbraced_linkage_specification_p;
14663   unsigned saved_num_template_parameter_lists;
14664
14665   /* If the next token is `return', then the code may be trying to
14666      make use of the "named return value" extension that G++ used to
14667      support.  */
14668   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14669     {
14670       /* Consume the `return' keyword.  */
14671       cp_lexer_consume_token (parser->lexer);
14672       /* Look for the identifier that indicates what value is to be
14673          returned.  */
14674       cp_parser_identifier (parser);
14675       /* Issue an error message.  */
14676       error ("named return values are no longer supported");
14677       /* Skip tokens until we reach the start of the function body.  */
14678       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14679              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14680         cp_lexer_consume_token (parser->lexer);
14681     }
14682   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14683      anything declared inside `f'.  */
14684   saved_in_unbraced_linkage_specification_p
14685     = parser->in_unbraced_linkage_specification_p;
14686   parser->in_unbraced_linkage_specification_p = false;
14687   /* Inside the function, surrounding template-parameter-lists do not
14688      apply.  */
14689   saved_num_template_parameter_lists
14690     = parser->num_template_parameter_lists;
14691   parser->num_template_parameter_lists = 0;
14692   /* If the next token is `try', then we are looking at a
14693      function-try-block.  */
14694   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14695     ctor_initializer_p = cp_parser_function_try_block (parser);
14696   /* A function-try-block includes the function-body, so we only do
14697      this next part if we're not processing a function-try-block.  */
14698   else
14699     ctor_initializer_p
14700       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14701
14702   /* Finish the function.  */
14703   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14704                         (inline_p ? 2 : 0));
14705   /* Generate code for it, if necessary.  */
14706   expand_or_defer_fn (fn);
14707   /* Restore the saved values.  */
14708   parser->in_unbraced_linkage_specification_p
14709     = saved_in_unbraced_linkage_specification_p;
14710   parser->num_template_parameter_lists
14711     = saved_num_template_parameter_lists;
14712
14713   return fn;
14714 }
14715
14716 /* Parse a template-declaration, assuming that the `export' (and
14717    `extern') keywords, if present, has already been scanned.  MEMBER_P
14718    is as for cp_parser_template_declaration.  */
14719
14720 static void
14721 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14722 {
14723   tree decl = NULL_TREE;
14724   tree parameter_list;
14725   bool friend_p = false;
14726
14727   /* Look for the `template' keyword.  */
14728   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14729     return;
14730
14731   /* And the `<'.  */
14732   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14733     return;
14734
14735   /* If the next token is `>', then we have an invalid
14736      specialization.  Rather than complain about an invalid template
14737      parameter, issue an error message here.  */
14738   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14739     {
14740       cp_parser_error (parser, "invalid explicit specialization");
14741       begin_specialization ();
14742       parameter_list = NULL_TREE;
14743     }
14744   else
14745     {
14746       /* Parse the template parameters.  */
14747       begin_template_parm_list ();
14748       parameter_list = cp_parser_template_parameter_list (parser);
14749       parameter_list = end_template_parm_list (parameter_list);
14750     }
14751
14752   /* Look for the `>'.  */
14753   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14754   /* We just processed one more parameter list.  */
14755   ++parser->num_template_parameter_lists;
14756   /* If the next token is `template', there are more template
14757      parameters.  */
14758   if (cp_lexer_next_token_is_keyword (parser->lexer,
14759                                       RID_TEMPLATE))
14760     cp_parser_template_declaration_after_export (parser, member_p);
14761   else
14762     {
14763       /* There are no access checks when parsing a template, as we do not
14764          know if a specialization will be a friend.  */
14765       push_deferring_access_checks (dk_no_check);
14766
14767       decl = cp_parser_single_declaration (parser,
14768                                            member_p,
14769                                            &friend_p);
14770
14771       pop_deferring_access_checks ();
14772
14773       /* If this is a member template declaration, let the front
14774          end know.  */
14775       if (member_p && !friend_p && decl)
14776         {
14777           if (TREE_CODE (decl) == TYPE_DECL)
14778             cp_parser_check_access_in_redeclaration (decl);
14779
14780           decl = finish_member_template_decl (decl);
14781         }
14782       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14783         make_friend_class (current_class_type, TREE_TYPE (decl),
14784                            /*complain=*/true);
14785     }
14786   /* We are done with the current parameter list.  */
14787   --parser->num_template_parameter_lists;
14788
14789   /* Finish up.  */
14790   finish_template_decl (parameter_list);
14791
14792   /* Register member declarations.  */
14793   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14794     finish_member_declaration (decl);
14795
14796   /* If DECL is a function template, we must return to parse it later.
14797      (Even though there is no definition, there might be default
14798      arguments that need handling.)  */
14799   if (member_p && decl
14800       && (TREE_CODE (decl) == FUNCTION_DECL
14801           || DECL_FUNCTION_TEMPLATE_P (decl)))
14802     TREE_VALUE (parser->unparsed_functions_queues)
14803       = tree_cons (NULL_TREE, decl,
14804                    TREE_VALUE (parser->unparsed_functions_queues));
14805 }
14806
14807 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14808    `function-definition' sequence.  MEMBER_P is true, this declaration
14809    appears in a class scope.
14810
14811    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14812    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14813
14814 static tree
14815 cp_parser_single_declaration (cp_parser* parser,
14816                               bool member_p,
14817                               bool* friend_p)
14818 {
14819   int declares_class_or_enum;
14820   tree decl = NULL_TREE;
14821   cp_decl_specifier_seq decl_specifiers;
14822   bool function_definition_p = false;
14823
14824   /* This function is only used when processing a template
14825      declaration.  */
14826   gcc_assert (innermost_scope_kind () == sk_template_parms
14827               || innermost_scope_kind () == sk_template_spec);
14828
14829   /* Defer access checks until we know what is being declared.  */
14830   push_deferring_access_checks (dk_deferred);
14831
14832   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14833      alternative.  */
14834   cp_parser_decl_specifier_seq (parser,
14835                                 CP_PARSER_FLAGS_OPTIONAL,
14836                                 &decl_specifiers,
14837                                 &declares_class_or_enum);
14838   if (friend_p)
14839     *friend_p = cp_parser_friend_p (&decl_specifiers);
14840
14841   /* There are no template typedefs.  */
14842   if (decl_specifiers.specs[(int) ds_typedef])
14843     {
14844       error ("template declaration of %qs", "typedef");
14845       decl = error_mark_node;
14846     }
14847
14848   /* Gather up the access checks that occurred the
14849      decl-specifier-seq.  */
14850   stop_deferring_access_checks ();
14851
14852   /* Check for the declaration of a template class.  */
14853   if (declares_class_or_enum)
14854     {
14855       if (cp_parser_declares_only_class_p (parser))
14856         {
14857           decl = shadow_tag (&decl_specifiers);
14858
14859           /* In this case:
14860
14861                struct C {
14862                  friend template <typename T> struct A<T>::B;
14863                };
14864
14865              A<T>::B will be represented by a TYPENAME_TYPE, and
14866              therefore not recognized by shadow_tag.  */
14867           if (friend_p && *friend_p
14868               && !decl
14869               && decl_specifiers.type
14870               && TYPE_P (decl_specifiers.type))
14871             decl = decl_specifiers.type;
14872
14873           if (decl && decl != error_mark_node)
14874             decl = TYPE_NAME (decl);
14875           else
14876             decl = error_mark_node;
14877         }
14878     }
14879   /* If it's not a template class, try for a template function.  If
14880      the next token is a `;', then this declaration does not declare
14881      anything.  But, if there were errors in the decl-specifiers, then
14882      the error might well have come from an attempted class-specifier.
14883      In that case, there's no need to warn about a missing declarator.  */
14884   if (!decl
14885       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14886           || decl_specifiers.type != error_mark_node))
14887     decl = cp_parser_init_declarator (parser,
14888                                       &decl_specifiers,
14889                                       /*function_definition_allowed_p=*/true,
14890                                       member_p,
14891                                       declares_class_or_enum,
14892                                       &function_definition_p);
14893
14894   pop_deferring_access_checks ();
14895
14896   /* Clear any current qualification; whatever comes next is the start
14897      of something new.  */
14898   parser->scope = NULL_TREE;
14899   parser->qualifying_scope = NULL_TREE;
14900   parser->object_scope = NULL_TREE;
14901   /* Look for a trailing `;' after the declaration.  */
14902   if (!function_definition_p
14903       && (decl == error_mark_node
14904           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
14905     cp_parser_skip_to_end_of_block_or_statement (parser);
14906
14907   return decl;
14908 }
14909
14910 /* Parse a cast-expression that is not the operand of a unary "&".  */
14911
14912 static tree
14913 cp_parser_simple_cast_expression (cp_parser *parser)
14914 {
14915   return cp_parser_cast_expression (parser, /*address_p=*/false);
14916 }
14917
14918 /* Parse a functional cast to TYPE.  Returns an expression
14919    representing the cast.  */
14920
14921 static tree
14922 cp_parser_functional_cast (cp_parser* parser, tree type)
14923 {
14924   tree expression_list;
14925   tree cast;
14926
14927   expression_list
14928     = cp_parser_parenthesized_expression_list (parser, false,
14929                                                /*non_constant_p=*/NULL);
14930
14931   cast = build_functional_cast (type, expression_list);
14932   /* [expr.const]/1: In an integral constant expression "only type
14933      conversions to integral or enumeration type can be used".  */
14934   if (cast != error_mark_node && !type_dependent_expression_p (type)
14935       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14936     {
14937       if (cp_parser_non_integral_constant_expression
14938           (parser, "a call to a constructor"))
14939         return error_mark_node;
14940     }
14941   return cast;
14942 }
14943
14944 /* Save the tokens that make up the body of a member function defined
14945    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
14946    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
14947    specifiers applied to the declaration.  Returns the FUNCTION_DECL
14948    for the member function.  */
14949
14950 static tree
14951 cp_parser_save_member_function_body (cp_parser* parser,
14952                                      cp_decl_specifier_seq *decl_specifiers,
14953                                      cp_declarator *declarator,
14954                                      tree attributes)
14955 {
14956   cp_token *first;
14957   cp_token *last;
14958   tree fn;
14959
14960   /* Create the function-declaration.  */
14961   fn = start_method (decl_specifiers, declarator, attributes);
14962   /* If something went badly wrong, bail out now.  */
14963   if (fn == error_mark_node)
14964     {
14965       /* If there's a function-body, skip it.  */
14966       if (cp_parser_token_starts_function_definition_p
14967           (cp_lexer_peek_token (parser->lexer)))
14968         cp_parser_skip_to_end_of_block_or_statement (parser);
14969       return error_mark_node;
14970     }
14971
14972   /* Remember it, if there default args to post process.  */
14973   cp_parser_save_default_args (parser, fn);
14974
14975   /* Save away the tokens that make up the body of the
14976      function.  */
14977   first = parser->lexer->next_token;
14978   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14979   /* Handle function try blocks.  */
14980   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14981     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14982   last = parser->lexer->next_token;
14983
14984   /* Save away the inline definition; we will process it when the
14985      class is complete.  */
14986   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
14987   DECL_PENDING_INLINE_P (fn) = 1;
14988
14989   /* We need to know that this was defined in the class, so that
14990      friend templates are handled correctly.  */
14991   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14992
14993   /* We're done with the inline definition.  */
14994   finish_method (fn);
14995
14996   /* Add FN to the queue of functions to be parsed later.  */
14997   TREE_VALUE (parser->unparsed_functions_queues)
14998     = tree_cons (NULL_TREE, fn,
14999                  TREE_VALUE (parser->unparsed_functions_queues));
15000
15001   return fn;
15002 }
15003
15004 /* Parse a template-argument-list, as well as the trailing ">" (but
15005    not the opening ">").  See cp_parser_template_argument_list for the
15006    return value.  */
15007
15008 static tree
15009 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15010 {
15011   tree arguments;
15012   tree saved_scope;
15013   tree saved_qualifying_scope;
15014   tree saved_object_scope;
15015   bool saved_greater_than_is_operator_p;
15016
15017   /* [temp.names]
15018
15019      When parsing a template-id, the first non-nested `>' is taken as
15020      the end of the template-argument-list rather than a greater-than
15021      operator.  */
15022   saved_greater_than_is_operator_p
15023     = parser->greater_than_is_operator_p;
15024   parser->greater_than_is_operator_p = false;
15025   /* Parsing the argument list may modify SCOPE, so we save it
15026      here.  */
15027   saved_scope = parser->scope;
15028   saved_qualifying_scope = parser->qualifying_scope;
15029   saved_object_scope = parser->object_scope;
15030   /* Parse the template-argument-list itself.  */
15031   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15032     arguments = NULL_TREE;
15033   else
15034     arguments = cp_parser_template_argument_list (parser);
15035   /* Look for the `>' that ends the template-argument-list. If we find
15036      a '>>' instead, it's probably just a typo.  */
15037   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15038     {
15039       if (!saved_greater_than_is_operator_p)
15040         {
15041           /* If we're in a nested template argument list, the '>>' has
15042             to be a typo for '> >'. We emit the error message, but we
15043             continue parsing and we push a '>' as next token, so that
15044             the argument list will be parsed correctly.  Note that the
15045             global source location is still on the token before the
15046             '>>', so we need to say explicitly where we want it.  */
15047           cp_token *token = cp_lexer_peek_token (parser->lexer);
15048           error ("%H%<>>%> should be %<> >%> "
15049                  "within a nested template argument list",
15050                  &token->location);
15051
15052           /* ??? Proper recovery should terminate two levels of
15053              template argument list here.  */
15054           token->type = CPP_GREATER;
15055         }
15056       else
15057         {
15058           /* If this is not a nested template argument list, the '>>'
15059             is a typo for '>'. Emit an error message and continue.
15060             Same deal about the token location, but here we can get it
15061             right by consuming the '>>' before issuing the diagnostic.  */
15062           cp_lexer_consume_token (parser->lexer);
15063           error ("spurious %<>>%>, use %<>%> to terminate "
15064                  "a template argument list");
15065         }
15066     }
15067   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15068     error ("missing %<>%> to terminate the template argument list");
15069   else
15070     /* It's what we want, a '>'; consume it.  */
15071     cp_lexer_consume_token (parser->lexer);
15072   /* The `>' token might be a greater-than operator again now.  */
15073   parser->greater_than_is_operator_p
15074     = saved_greater_than_is_operator_p;
15075   /* Restore the SAVED_SCOPE.  */
15076   parser->scope = saved_scope;
15077   parser->qualifying_scope = saved_qualifying_scope;
15078   parser->object_scope = saved_object_scope;
15079
15080   return arguments;
15081 }
15082
15083 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15084    arguments, or the body of the function have not yet been parsed,
15085    parse them now.  */
15086
15087 static void
15088 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15089 {
15090   /* If this member is a template, get the underlying
15091      FUNCTION_DECL.  */
15092   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15093     member_function = DECL_TEMPLATE_RESULT (member_function);
15094
15095   /* There should not be any class definitions in progress at this
15096      point; the bodies of members are only parsed outside of all class
15097      definitions.  */
15098   gcc_assert (parser->num_classes_being_defined == 0);
15099   /* While we're parsing the member functions we might encounter more
15100      classes.  We want to handle them right away, but we don't want
15101      them getting mixed up with functions that are currently in the
15102      queue.  */
15103   parser->unparsed_functions_queues
15104     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15105
15106   /* Make sure that any template parameters are in scope.  */
15107   maybe_begin_member_template_processing (member_function);
15108
15109   /* If the body of the function has not yet been parsed, parse it
15110      now.  */
15111   if (DECL_PENDING_INLINE_P (member_function))
15112     {
15113       tree function_scope;
15114       cp_token_cache *tokens;
15115
15116       /* The function is no longer pending; we are processing it.  */
15117       tokens = DECL_PENDING_INLINE_INFO (member_function);
15118       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15119       DECL_PENDING_INLINE_P (member_function) = 0;
15120       /* If this was an inline function in a local class, enter the scope
15121          of the containing function.  */
15122       function_scope = decl_function_context (member_function);
15123       if (function_scope)
15124         push_function_context_to (function_scope);
15125
15126       /* Push the body of the function onto the lexer stack.  */
15127       cp_parser_push_lexer_for_tokens (parser, tokens);
15128
15129       /* Let the front end know that we going to be defining this
15130          function.  */
15131       start_preparsed_function (member_function, NULL_TREE,
15132                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15133
15134       /* Now, parse the body of the function.  */
15135       cp_parser_function_definition_after_declarator (parser,
15136                                                       /*inline_p=*/true);
15137
15138       /* Leave the scope of the containing function.  */
15139       if (function_scope)
15140         pop_function_context_from (function_scope);
15141       cp_parser_pop_lexer (parser);
15142     }
15143
15144   /* Remove any template parameters from the symbol table.  */
15145   maybe_end_member_template_processing ();
15146
15147   /* Restore the queue.  */
15148   parser->unparsed_functions_queues
15149     = TREE_CHAIN (parser->unparsed_functions_queues);
15150 }
15151
15152 /* If DECL contains any default args, remember it on the unparsed
15153    functions queue.  */
15154
15155 static void
15156 cp_parser_save_default_args (cp_parser* parser, tree decl)
15157 {
15158   tree probe;
15159
15160   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15161        probe;
15162        probe = TREE_CHAIN (probe))
15163     if (TREE_PURPOSE (probe))
15164       {
15165         TREE_PURPOSE (parser->unparsed_functions_queues)
15166           = tree_cons (current_class_type, decl,
15167                        TREE_PURPOSE (parser->unparsed_functions_queues));
15168         break;
15169       }
15170   return;
15171 }
15172
15173 /* FN is a FUNCTION_DECL which may contains a parameter with an
15174    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15175    assumes that the current scope is the scope in which the default
15176    argument should be processed.  */
15177
15178 static void
15179 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15180 {
15181   bool saved_local_variables_forbidden_p;
15182   tree parm;
15183
15184   /* While we're parsing the default args, we might (due to the
15185      statement expression extension) encounter more classes.  We want
15186      to handle them right away, but we don't want them getting mixed
15187      up with default args that are currently in the queue.  */
15188   parser->unparsed_functions_queues
15189     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15190
15191   /* Local variable names (and the `this' keyword) may not appear
15192      in a default argument.  */
15193   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15194   parser->local_variables_forbidden_p = true;
15195
15196   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15197        parm;
15198        parm = TREE_CHAIN (parm))
15199     {
15200       cp_token_cache *tokens;
15201
15202       if (!TREE_PURPOSE (parm)
15203           || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15204         continue;
15205
15206        /* Push the saved tokens for the default argument onto the parser's
15207           lexer stack.  */
15208       tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15209       cp_parser_push_lexer_for_tokens (parser, tokens);
15210
15211       /* Parse the assignment-expression.  */
15212       TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15213
15214       /* If the token stream has not been completely used up, then
15215          there was extra junk after the end of the default
15216          argument.  */
15217       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15218         cp_parser_error (parser, "expected %<,%>");
15219
15220       /* Revert to the main lexer.  */
15221       cp_parser_pop_lexer (parser);
15222     }
15223
15224   /* Restore the state of local_variables_forbidden_p.  */
15225   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15226
15227   /* Restore the queue.  */
15228   parser->unparsed_functions_queues
15229     = TREE_CHAIN (parser->unparsed_functions_queues);
15230 }
15231
15232 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15233    either a TYPE or an expression, depending on the form of the
15234    input.  The KEYWORD indicates which kind of expression we have
15235    encountered.  */
15236
15237 static tree
15238 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15239 {
15240   static const char *format;
15241   tree expr = NULL_TREE;
15242   const char *saved_message;
15243   bool saved_integral_constant_expression_p;
15244
15245   /* Initialize FORMAT the first time we get here.  */
15246   if (!format)
15247     format = "types may not be defined in '%s' expressions";
15248
15249   /* Types cannot be defined in a `sizeof' expression.  Save away the
15250      old message.  */
15251   saved_message = parser->type_definition_forbidden_message;
15252   /* And create the new one.  */
15253   parser->type_definition_forbidden_message
15254     = xmalloc (strlen (format)
15255                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15256                + 1 /* `\0' */);
15257   sprintf ((char *) parser->type_definition_forbidden_message,
15258            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15259
15260   /* The restrictions on constant-expressions do not apply inside
15261      sizeof expressions.  */
15262   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15263   parser->integral_constant_expression_p = false;
15264
15265   /* Do not actually evaluate the expression.  */
15266   ++skip_evaluation;
15267   /* If it's a `(', then we might be looking at the type-id
15268      construction.  */
15269   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15270     {
15271       tree type;
15272       bool saved_in_type_id_in_expr_p;
15273
15274       /* We can't be sure yet whether we're looking at a type-id or an
15275          expression.  */
15276       cp_parser_parse_tentatively (parser);
15277       /* Consume the `('.  */
15278       cp_lexer_consume_token (parser->lexer);
15279       /* Parse the type-id.  */
15280       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15281       parser->in_type_id_in_expr_p = true;
15282       type = cp_parser_type_id (parser);
15283       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15284       /* Now, look for the trailing `)'.  */
15285       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15286       /* If all went well, then we're done.  */
15287       if (cp_parser_parse_definitely (parser))
15288         {
15289           cp_decl_specifier_seq decl_specs;
15290
15291           /* Build a trivial decl-specifier-seq.  */
15292           clear_decl_specs (&decl_specs);
15293           decl_specs.type = type;
15294
15295           /* Call grokdeclarator to figure out what type this is.  */
15296           expr = grokdeclarator (NULL,
15297                                  &decl_specs,
15298                                  TYPENAME,
15299                                  /*initialized=*/0,
15300                                  /*attrlist=*/NULL);
15301         }
15302     }
15303
15304   /* If the type-id production did not work out, then we must be
15305      looking at the unary-expression production.  */
15306   if (!expr)
15307     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15308   /* Go back to evaluating expressions.  */
15309   --skip_evaluation;
15310
15311   /* Free the message we created.  */
15312   free ((char *) parser->type_definition_forbidden_message);
15313   /* And restore the old one.  */
15314   parser->type_definition_forbidden_message = saved_message;
15315   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15316
15317   return expr;
15318 }
15319
15320 /* If the current declaration has no declarator, return true.  */
15321
15322 static bool
15323 cp_parser_declares_only_class_p (cp_parser *parser)
15324 {
15325   /* If the next token is a `;' or a `,' then there is no
15326      declarator.  */
15327   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15328           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15329 }
15330
15331 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15332
15333 static void
15334 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15335                              cp_storage_class storage_class)
15336 {
15337   if (decl_specs->storage_class != sc_none)
15338     decl_specs->multiple_storage_classes_p = true;
15339   else
15340     decl_specs->storage_class = storage_class;
15341 }
15342
15343 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15344    is true, the type is a user-defined type; otherwise it is a
15345    built-in type specified by a keyword.  */
15346
15347 static void
15348 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15349                               tree type_spec,
15350                               bool user_defined_p)
15351 {
15352   decl_specs->any_specifiers_p = true;
15353
15354   /* If the user tries to redeclare bool or wchar_t (with, for
15355      example, in "typedef int wchar_t;") we remember that this is what
15356      happened.  In system headers, we ignore these declarations so
15357      that G++ can work with system headers that are not C++-safe.  */
15358   if (decl_specs->specs[(int) ds_typedef]
15359       && !user_defined_p
15360       && (type_spec == boolean_type_node
15361           || type_spec == wchar_type_node)
15362       && (decl_specs->type
15363           || decl_specs->specs[(int) ds_long]
15364           || decl_specs->specs[(int) ds_short]
15365           || decl_specs->specs[(int) ds_unsigned]
15366           || decl_specs->specs[(int) ds_signed]))
15367     {
15368       decl_specs->redefined_builtin_type = type_spec;
15369       if (!decl_specs->type)
15370         {
15371           decl_specs->type = type_spec;
15372           decl_specs->user_defined_type_p = false;
15373         }
15374     }
15375   else if (decl_specs->type)
15376     decl_specs->multiple_types_p = true;
15377   else
15378     {
15379       decl_specs->type = type_spec;
15380       decl_specs->user_defined_type_p = user_defined_p;
15381       decl_specs->redefined_builtin_type = NULL_TREE;
15382     }
15383 }
15384
15385 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15386    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15387
15388 static bool
15389 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15390 {
15391   return decl_specifiers->specs[(int) ds_friend] != 0;
15392 }
15393
15394 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15395    issue an error message indicating that TOKEN_DESC was expected.
15396
15397    Returns the token consumed, if the token had the appropriate type.
15398    Otherwise, returns NULL.  */
15399
15400 static cp_token *
15401 cp_parser_require (cp_parser* parser,
15402                    enum cpp_ttype type,
15403                    const char* token_desc)
15404 {
15405   if (cp_lexer_next_token_is (parser->lexer, type))
15406     return cp_lexer_consume_token (parser->lexer);
15407   else
15408     {
15409       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15410       if (!cp_parser_simulate_error (parser))
15411         {
15412           char *message = concat ("expected ", token_desc, NULL);
15413           cp_parser_error (parser, message);
15414           free (message);
15415         }
15416       return NULL;
15417     }
15418 }
15419
15420 /* Like cp_parser_require, except that tokens will be skipped until
15421    the desired token is found.  An error message is still produced if
15422    the next token is not as expected.  */
15423
15424 static void
15425 cp_parser_skip_until_found (cp_parser* parser,
15426                             enum cpp_ttype type,
15427                             const char* token_desc)
15428 {
15429   cp_token *token;
15430   unsigned nesting_depth = 0;
15431
15432   if (cp_parser_require (parser, type, token_desc))
15433     return;
15434
15435   /* Skip tokens until the desired token is found.  */
15436   while (true)
15437     {
15438       /* Peek at the next token.  */
15439       token = cp_lexer_peek_token (parser->lexer);
15440       /* If we've reached the token we want, consume it and
15441          stop.  */
15442       if (token->type == type && !nesting_depth)
15443         {
15444           cp_lexer_consume_token (parser->lexer);
15445           return;
15446         }
15447       /* If we've run out of tokens, stop.  */
15448       if (token->type == CPP_EOF)
15449         return;
15450       if (token->type == CPP_OPEN_BRACE
15451           || token->type == CPP_OPEN_PAREN
15452           || token->type == CPP_OPEN_SQUARE)
15453         ++nesting_depth;
15454       else if (token->type == CPP_CLOSE_BRACE
15455                || token->type == CPP_CLOSE_PAREN
15456                || token->type == CPP_CLOSE_SQUARE)
15457         {
15458           if (nesting_depth-- == 0)
15459             return;
15460         }
15461       /* Consume this token.  */
15462       cp_lexer_consume_token (parser->lexer);
15463     }
15464 }
15465
15466 /* If the next token is the indicated keyword, consume it.  Otherwise,
15467    issue an error message indicating that TOKEN_DESC was expected.
15468
15469    Returns the token consumed, if the token had the appropriate type.
15470    Otherwise, returns NULL.  */
15471
15472 static cp_token *
15473 cp_parser_require_keyword (cp_parser* parser,
15474                            enum rid keyword,
15475                            const char* token_desc)
15476 {
15477   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15478
15479   if (token && token->keyword != keyword)
15480     {
15481       dyn_string_t error_msg;
15482
15483       /* Format the error message.  */
15484       error_msg = dyn_string_new (0);
15485       dyn_string_append_cstr (error_msg, "expected ");
15486       dyn_string_append_cstr (error_msg, token_desc);
15487       cp_parser_error (parser, error_msg->s);
15488       dyn_string_delete (error_msg);
15489       return NULL;
15490     }
15491
15492   return token;
15493 }
15494
15495 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15496    function-definition.  */
15497
15498 static bool
15499 cp_parser_token_starts_function_definition_p (cp_token* token)
15500 {
15501   return (/* An ordinary function-body begins with an `{'.  */
15502           token->type == CPP_OPEN_BRACE
15503           /* A ctor-initializer begins with a `:'.  */
15504           || token->type == CPP_COLON
15505           /* A function-try-block begins with `try'.  */
15506           || token->keyword == RID_TRY
15507           /* The named return value extension begins with `return'.  */
15508           || token->keyword == RID_RETURN);
15509 }
15510
15511 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15512    definition.  */
15513
15514 static bool
15515 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15516 {
15517   cp_token *token;
15518
15519   token = cp_lexer_peek_token (parser->lexer);
15520   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15521 }
15522
15523 /* Returns TRUE iff the next token is the "," or ">" ending a
15524    template-argument.  */
15525
15526 static bool
15527 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15528 {
15529   cp_token *token;
15530
15531   token = cp_lexer_peek_token (parser->lexer);
15532   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15533 }
15534
15535 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15536    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15537
15538 static bool
15539 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15540                                                      size_t n)
15541 {
15542   cp_token *token;
15543
15544   token = cp_lexer_peek_nth_token (parser->lexer, n);
15545   if (token->type == CPP_LESS)
15546     return true;
15547   /* Check for the sequence `<::' in the original code. It would be lexed as
15548      `[:', where `[' is a digraph, and there is no whitespace before
15549      `:'.  */
15550   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15551     {
15552       cp_token *token2;
15553       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15554       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15555         return true;
15556     }
15557   return false;
15558 }
15559
15560 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15561    or none_type otherwise.  */
15562
15563 static enum tag_types
15564 cp_parser_token_is_class_key (cp_token* token)
15565 {
15566   switch (token->keyword)
15567     {
15568     case RID_CLASS:
15569       return class_type;
15570     case RID_STRUCT:
15571       return record_type;
15572     case RID_UNION:
15573       return union_type;
15574
15575     default:
15576       return none_type;
15577     }
15578 }
15579
15580 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15581
15582 static void
15583 cp_parser_check_class_key (enum tag_types class_key, tree type)
15584 {
15585   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15586     pedwarn ("%qs tag used in naming %q#T",
15587             class_key == union_type ? "union"
15588              : class_key == record_type ? "struct" : "class",
15589              type);
15590 }
15591
15592 /* Issue an error message if DECL is redeclared with different
15593    access than its original declaration [class.access.spec/3].
15594    This applies to nested classes and nested class templates.
15595    [class.mem/1].  */
15596
15597 static void
15598 cp_parser_check_access_in_redeclaration (tree decl)
15599 {
15600   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15601     return;
15602
15603   if ((TREE_PRIVATE (decl)
15604        != (current_access_specifier == access_private_node))
15605       || (TREE_PROTECTED (decl)
15606           != (current_access_specifier == access_protected_node)))
15607     error ("%qD redeclared with different access", decl);
15608 }
15609
15610 /* Look for the `template' keyword, as a syntactic disambiguator.
15611    Return TRUE iff it is present, in which case it will be
15612    consumed.  */
15613
15614 static bool
15615 cp_parser_optional_template_keyword (cp_parser *parser)
15616 {
15617   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15618     {
15619       /* The `template' keyword can only be used within templates;
15620          outside templates the parser can always figure out what is a
15621          template and what is not.  */
15622       if (!processing_template_decl)
15623         {
15624           error ("%<template%> (as a disambiguator) is only allowed "
15625                  "within templates");
15626           /* If this part of the token stream is rescanned, the same
15627              error message would be generated.  So, we purge the token
15628              from the stream.  */
15629           cp_lexer_purge_token (parser->lexer);
15630           return false;
15631         }
15632       else
15633         {
15634           /* Consume the `template' keyword.  */
15635           cp_lexer_consume_token (parser->lexer);
15636           return true;
15637         }
15638     }
15639
15640   return false;
15641 }
15642
15643 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15644    set PARSER->SCOPE, and perform other related actions.  */
15645
15646 static void
15647 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15648 {
15649   tree value;
15650   tree check;
15651
15652   /* Get the stored value.  */
15653   value = cp_lexer_consume_token (parser->lexer)->value;
15654   /* Perform any access checks that were deferred.  */
15655   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15656     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15657   /* Set the scope from the stored value.  */
15658   parser->scope = TREE_VALUE (value);
15659   parser->qualifying_scope = TREE_TYPE (value);
15660   parser->object_scope = NULL_TREE;
15661 }
15662
15663 /* Consume tokens up through a non-nested END token.  */
15664
15665 static void
15666 cp_parser_cache_group (cp_parser *parser,
15667                        enum cpp_ttype end,
15668                        unsigned depth)
15669 {
15670   while (true)
15671     {
15672       cp_token *token;
15673
15674       /* Abort a parenthesized expression if we encounter a brace.  */
15675       if ((end == CPP_CLOSE_PAREN || depth == 0)
15676           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15677         return;
15678       /* If we've reached the end of the file, stop.  */
15679       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15680         return;
15681       /* Consume the next token.  */
15682       token = cp_lexer_consume_token (parser->lexer);
15683       /* See if it starts a new group.  */
15684       if (token->type == CPP_OPEN_BRACE)
15685         {
15686           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15687           if (depth == 0)
15688             return;
15689         }
15690       else if (token->type == CPP_OPEN_PAREN)
15691         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15692       else if (token->type == end)
15693         return;
15694     }
15695 }
15696
15697 /* Begin parsing tentatively.  We always save tokens while parsing
15698    tentatively so that if the tentative parsing fails we can restore the
15699    tokens.  */
15700
15701 static void
15702 cp_parser_parse_tentatively (cp_parser* parser)
15703 {
15704   /* Enter a new parsing context.  */
15705   parser->context = cp_parser_context_new (parser->context);
15706   /* Begin saving tokens.  */
15707   cp_lexer_save_tokens (parser->lexer);
15708   /* In order to avoid repetitive access control error messages,
15709      access checks are queued up until we are no longer parsing
15710      tentatively.  */
15711   push_deferring_access_checks (dk_deferred);
15712 }
15713
15714 /* Commit to the currently active tentative parse.  */
15715
15716 static void
15717 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15718 {
15719   cp_parser_context *context;
15720   cp_lexer *lexer;
15721
15722   /* Mark all of the levels as committed.  */
15723   lexer = parser->lexer;
15724   for (context = parser->context; context->next; context = context->next)
15725     {
15726       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15727         break;
15728       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15729       while (!cp_lexer_saving_tokens (lexer))
15730         lexer = lexer->next;
15731       cp_lexer_commit_tokens (lexer);
15732     }
15733 }
15734
15735 /* Abort the currently active tentative parse.  All consumed tokens
15736    will be rolled back, and no diagnostics will be issued.  */
15737
15738 static void
15739 cp_parser_abort_tentative_parse (cp_parser* parser)
15740 {
15741   cp_parser_simulate_error (parser);
15742   /* Now, pretend that we want to see if the construct was
15743      successfully parsed.  */
15744   cp_parser_parse_definitely (parser);
15745 }
15746
15747 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15748    token stream.  Otherwise, commit to the tokens we have consumed.
15749    Returns true if no error occurred; false otherwise.  */
15750
15751 static bool
15752 cp_parser_parse_definitely (cp_parser* parser)
15753 {
15754   bool error_occurred;
15755   cp_parser_context *context;
15756
15757   /* Remember whether or not an error occurred, since we are about to
15758      destroy that information.  */
15759   error_occurred = cp_parser_error_occurred (parser);
15760   /* Remove the topmost context from the stack.  */
15761   context = parser->context;
15762   parser->context = context->next;
15763   /* If no parse errors occurred, commit to the tentative parse.  */
15764   if (!error_occurred)
15765     {
15766       /* Commit to the tokens read tentatively, unless that was
15767          already done.  */
15768       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15769         cp_lexer_commit_tokens (parser->lexer);
15770
15771       pop_to_parent_deferring_access_checks ();
15772     }
15773   /* Otherwise, if errors occurred, roll back our state so that things
15774      are just as they were before we began the tentative parse.  */
15775   else
15776     {
15777       cp_lexer_rollback_tokens (parser->lexer);
15778       pop_deferring_access_checks ();
15779     }
15780   /* Add the context to the front of the free list.  */
15781   context->next = cp_parser_context_free_list;
15782   cp_parser_context_free_list = context;
15783
15784   return !error_occurred;
15785 }
15786
15787 /* Returns true if we are parsing tentatively -- but have decided that
15788    we will stick with this tentative parse, even if errors occur.  */
15789
15790 static bool
15791 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15792 {
15793   return (cp_parser_parsing_tentatively (parser)
15794           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15795 }
15796
15797 /* Returns nonzero iff an error has occurred during the most recent
15798    tentative parse.  */
15799
15800 static bool
15801 cp_parser_error_occurred (cp_parser* parser)
15802 {
15803   return (cp_parser_parsing_tentatively (parser)
15804           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15805 }
15806
15807 /* Returns nonzero if GNU extensions are allowed.  */
15808
15809 static bool
15810 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15811 {
15812   return parser->allow_gnu_extensions_p;
15813 }
15814
15815 \f
15816 /* The parser.  */
15817
15818 static GTY (()) cp_parser *the_parser;
15819
15820 /* External interface.  */
15821
15822 /* Parse one entire translation unit.  */
15823
15824 void
15825 c_parse_file (void)
15826 {
15827   bool error_occurred;
15828   static bool already_called = false;
15829
15830   if (already_called)
15831     {
15832       sorry ("inter-module optimizations not implemented for C++");
15833       return;
15834     }
15835   already_called = true;
15836
15837   the_parser = cp_parser_new ();
15838   push_deferring_access_checks (flag_access_control
15839                                 ? dk_no_deferred : dk_no_check);
15840   error_occurred = cp_parser_translation_unit (the_parser);
15841   the_parser = NULL;
15842 }
15843
15844 /* This variable must be provided by every front end.  */
15845
15846 int yydebug;
15847
15848 #include "gt-cp-parser.h"