OSDN Git Service

* parser.c (cp_parser_template_id): Revert comment patch too.
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING.  If not, write to the Free
20    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39
40 \f
41 /* The lexer.  */
42
43 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
44    and c-lex.c) and the C++ parser.  */
45
46 /* A C++ token.  */
47
48 typedef struct cp_token GTY (())
49 {
50   /* The kind of token.  */
51   ENUM_BITFIELD (cpp_ttype) type : 8;
52   /* If this token is a keyword, this value indicates which keyword.
53      Otherwise, this value is RID_MAX.  */
54   ENUM_BITFIELD (rid) keyword : 8;
55   /* Token flags.  */
56   unsigned char flags;
57   /* True if this token is from a system header.  */
58   BOOL_BITFIELD in_system_header : 1;
59   /* True if this token is from a context where it is implicitly extern "C" */
60   BOOL_BITFIELD implicit_extern_c : 1;
61   /* The value associated with this token, if any.  */
62   tree value;
63   /* The location at which this token was found.  */
64   location_t location;
65 } cp_token;
66
67 /* We use a stack of token pointer for saving token sets.  */
68 typedef struct cp_token *cp_token_position;
69 DEF_VEC_MALLOC_P (cp_token_position);
70
71 static const cp_token eof_token =
72 {
73   CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
74 #if USE_MAPPED_LOCATION
75   0
76 #else
77   {0, 0}
78 #endif
79 };
80
81 /* The cp_lexer structure represents the C++ lexer.  It is responsible
82    for managing the token stream from the preprocessor and supplying
83    it to the parser.  Tokens are never added to the cp_lexer after
84    it is created.  */
85
86 typedef struct cp_lexer GTY (())
87 {
88   /* The memory allocated for the buffer.  NULL if this lexer does not
89      own the token buffer.  */
90   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
91   /* If the lexer owns the buffer, this is the number of tokens in the
92      buffer.  */
93   size_t buffer_length;
94   
95   /* A pointer just past the last available token.  The tokens
96      in this lexer are [buffer, last_token).  */
97   cp_token_position GTY ((skip)) last_token;
98
99   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
100      no more available tokens.  */
101   cp_token_position GTY ((skip)) next_token;
102
103   /* A stack indicating positions at which cp_lexer_save_tokens was
104      called.  The top entry is the most recent position at which we
105      began saving tokens.  If the stack is non-empty, we are saving
106      tokens.  */
107   VEC (cp_token_position) *GTY ((skip)) saved_tokens;
108
109   /* True if we should output debugging information.  */
110   bool debugging_p;
111
112   /* The next lexer in a linked list of lexers.  */
113   struct cp_lexer *next;
114 } cp_lexer;
115
116 /* cp_token_cache is a range of tokens.  There is no need to represent
117    allocate heap memory for it, since tokens are never removed from the
118    lexer's array.  There is also no need for the GC to walk through
119    a cp_token_cache, since everything in here is referenced through
120    a lexer.  */
121
122 typedef struct cp_token_cache GTY(())
123 {
124   /* The beginning of the token range.  */
125   cp_token * GTY((skip)) first;
126
127   /* Points immediately after the last token in the range.  */
128   cp_token * GTY ((skip)) last;
129 } cp_token_cache;
130
131 /* Prototypes.  */
132
133 static cp_lexer *cp_lexer_new_main
134   (void);
135 static cp_lexer *cp_lexer_new_from_tokens
136   (cp_token_cache *tokens);
137 static void cp_lexer_destroy
138   (cp_lexer *);
139 static int cp_lexer_saving_tokens
140   (const cp_lexer *);
141 static cp_token_position cp_lexer_token_position
142   (cp_lexer *, bool);
143 static cp_token *cp_lexer_token_at
144   (cp_lexer *, cp_token_position);
145 static void cp_lexer_get_preprocessor_token
146   (cp_lexer *, cp_token *);
147 static inline cp_token *cp_lexer_peek_token
148   (cp_lexer *);
149 static cp_token *cp_lexer_peek_nth_token
150   (cp_lexer *, size_t);
151 static inline bool cp_lexer_next_token_is
152   (cp_lexer *, enum cpp_ttype);
153 static bool cp_lexer_next_token_is_not
154   (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_keyword
156   (cp_lexer *, enum rid);
157 static cp_token *cp_lexer_consume_token
158   (cp_lexer *);
159 static void cp_lexer_purge_token
160   (cp_lexer *);
161 static void cp_lexer_purge_tokens_after
162   (cp_lexer *, cp_token_position);
163 static void cp_lexer_handle_pragma
164   (cp_lexer *);
165 static void cp_lexer_save_tokens
166   (cp_lexer *);
167 static void cp_lexer_commit_tokens
168   (cp_lexer *);
169 static void cp_lexer_rollback_tokens
170   (cp_lexer *);
171 #ifdef ENABLE_CHECKING
172 static void cp_lexer_print_token
173   (FILE *, cp_token *);
174 static inline bool cp_lexer_debugging_p
175   (cp_lexer *);
176 static void cp_lexer_start_debugging
177   (cp_lexer *) ATTRIBUTE_UNUSED;
178 static void cp_lexer_stop_debugging
179   (cp_lexer *) ATTRIBUTE_UNUSED;
180 #else
181 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
182    about passing NULL to functions that require non-NULL arguments
183    (fputs, fprintf).  It will never be used, so all we need is a value
184    of the right type that's guaranteed not to be NULL.  */
185 #define cp_lexer_debug_stream stdout
186 #define cp_lexer_print_token(str, tok) (void) 0
187 #define cp_lexer_debugging_p(lexer) 0
188 #endif /* ENABLE_CHECKING */
189
190 static cp_token_cache *cp_token_cache_new
191   (cp_token *, cp_token *);
192
193 /* Manifest constants.  */
194 #define CP_LEXER_BUFFER_SIZE 10000
195 #define CP_SAVED_TOKEN_STACK 5
196
197 /* A token type for keywords, as opposed to ordinary identifiers.  */
198 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
199
200 /* A token type for template-ids.  If a template-id is processed while
201    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
202    the value of the CPP_TEMPLATE_ID is whatever was returned by
203    cp_parser_template_id.  */
204 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
205
206 /* A token type for nested-name-specifiers.  If a
207    nested-name-specifier is processed while parsing tentatively, it is
208    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
209    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
210    cp_parser_nested_name_specifier_opt.  */
211 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
212
213 /* A token type for tokens that are not tokens at all; these are used
214    to represent slots in the array where there used to be a token
215    that has now been deleted.  */
216 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
217
218 /* The number of token types, including C++-specific ones.  */
219 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
220
221 /* Variables.  */
222
223 #ifdef ENABLE_CHECKING
224 /* The stream to which debugging output should be written.  */
225 static FILE *cp_lexer_debug_stream;
226 #endif /* ENABLE_CHECKING */
227
228 /* Create a new main C++ lexer, the lexer that gets tokens from the
229    preprocessor.  */
230
231 static cp_lexer *
232 cp_lexer_new_main (void)
233 {
234   cp_token first_token;
235   cp_lexer *lexer;
236   cp_token *pos;
237   size_t alloc;
238   size_t space;
239   cp_token *buffer;
240
241   /* It's possible that lexing the first token will load a PCH file,
242      which is a GC collection point.  So we have to grab the first
243      token before allocating any memory.  Pragmas must not be deferred
244      as -fpch-preprocess can generate a pragma to load the PCH file in
245      the preprocessed output used by -save-temps.  */
246   cp_lexer_get_preprocessor_token (NULL, &first_token);
247
248   /* Tell cpplib we want CPP_PRAGMA tokens.  */
249   cpp_get_options (parse_in)->defer_pragmas = true;
250
251   /* Tell c_lex not to merge string constants.  */
252   c_lex_return_raw_strings = true;
253
254   c_common_no_more_pch ();
255
256   /* Allocate the memory.  */
257   lexer = GGC_CNEW (cp_lexer);
258
259 #ifdef ENABLE_CHECKING  
260   /* Initially we are not debugging.  */
261   lexer->debugging_p = false;
262 #endif /* ENABLE_CHECKING */
263   lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
264          
265   /* Create the buffer.  */
266   alloc = CP_LEXER_BUFFER_SIZE;
267   buffer = ggc_alloc (alloc * sizeof (cp_token));
268
269   /* Put the first token in the buffer.  */
270   space = alloc;
271   pos = buffer;
272   *pos = first_token;
273   
274   /* Get the remaining tokens from the preprocessor.  */
275   while (pos->type != CPP_EOF)
276     {
277       pos++;
278       if (!--space)
279         {
280           space = alloc;
281           alloc *= 2;
282           buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
283           pos = buffer + space;
284         }
285       cp_lexer_get_preprocessor_token (lexer, pos);
286     }
287   lexer->buffer = buffer;
288   lexer->buffer_length = alloc - space;
289   lexer->last_token = pos;
290   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
291
292   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
293      direct calls to c_lex.  Those callers all expect c_lex to do
294      string constant concatenation.  */
295   c_lex_return_raw_strings = false;
296
297   gcc_assert (lexer->next_token->type != CPP_PURGED);
298   return lexer;
299 }
300
301 /* Create a new lexer whose token stream is primed with the tokens in
302    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
303
304 static cp_lexer *
305 cp_lexer_new_from_tokens (cp_token_cache *cache)
306 {
307   cp_token *first = cache->first;
308   cp_token *last = cache->last;
309   cp_lexer *lexer = GGC_CNEW (cp_lexer);
310
311   /* We do not own the buffer.  */
312   lexer->buffer = NULL;
313   lexer->buffer_length = 0;
314   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
315   lexer->last_token = last;
316   
317   lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
318
319 #ifdef ENABLE_CHECKING
320   /* Initially we are not debugging.  */
321   lexer->debugging_p = false;
322 #endif
323
324   gcc_assert (lexer->next_token->type != CPP_PURGED);
325   return lexer;
326 }
327
328 /* Frees all resources associated with LEXER.  */
329
330 static void
331 cp_lexer_destroy (cp_lexer *lexer)
332 {
333   if (lexer->buffer)
334     ggc_free (lexer->buffer);
335   VEC_free (cp_token_position, lexer->saved_tokens);
336   ggc_free (lexer);
337 }
338
339 /* Returns nonzero if debugging information should be output.  */
340
341 #ifdef ENABLE_CHECKING
342
343 static inline bool
344 cp_lexer_debugging_p (cp_lexer *lexer)
345 {
346   return lexer->debugging_p;
347 }
348
349 #endif /* ENABLE_CHECKING */
350
351 static inline cp_token_position
352 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
353 {
354   gcc_assert (!previous_p || lexer->next_token != &eof_token);
355   
356   return lexer->next_token - previous_p;
357 }
358
359 static inline cp_token *
360 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
361 {
362   return pos;
363 }
364
365 /* nonzero if we are presently saving tokens.  */
366
367 static inline int
368 cp_lexer_saving_tokens (const cp_lexer* lexer)
369 {
370   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
371 }
372
373 /* Store the next token from the preprocessor in *TOKEN.  Return true
374    if we reach EOF.  */
375
376 static void
377 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
378                                  cp_token *token)
379 {
380   static int is_extern_c = 0;
381
382    /* Get a new token from the preprocessor.  */
383   token->type = c_lex_with_flags (&token->value, &token->flags);
384   token->location = input_location;
385   token->in_system_header = in_system_header;
386
387   /* On some systems, some header files are surrounded by an 
388      implicit extern "C" block.  Set a flag in the token if it
389      comes from such a header.  */
390   is_extern_c += pending_lang_change;
391   pending_lang_change = 0;
392   token->implicit_extern_c = is_extern_c > 0;
393
394   /* Check to see if this token is a keyword.  */
395   if (token->type == CPP_NAME
396       && C_IS_RESERVED_WORD (token->value))
397     {
398       /* Mark this token as a keyword.  */
399       token->type = CPP_KEYWORD;
400       /* Record which keyword.  */
401       token->keyword = C_RID_CODE (token->value);
402       /* Update the value.  Some keywords are mapped to particular
403          entities, rather than simply having the value of the
404          corresponding IDENTIFIER_NODE.  For example, `__const' is
405          mapped to `const'.  */
406       token->value = ridpointers[token->keyword];
407     }
408   else
409     token->keyword = RID_MAX;
410 }
411
412 /* Update the globals input_location and in_system_header from TOKEN.  */
413 static inline void
414 cp_lexer_set_source_position_from_token (cp_token *token)
415 {
416   if (token->type != CPP_EOF)
417     {
418       input_location = token->location;
419       in_system_header = token->in_system_header;
420     }
421 }
422
423 /* Return a pointer to the next token in the token stream, but do not
424    consume it.  */
425
426 static inline cp_token *
427 cp_lexer_peek_token (cp_lexer *lexer)
428 {
429   if (cp_lexer_debugging_p (lexer))
430     {
431       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
432       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
433       putc ('\n', cp_lexer_debug_stream);
434     }
435   return lexer->next_token;
436 }
437
438 /* Return true if the next token has the indicated TYPE.  */
439
440 static inline bool
441 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
442 {
443   return cp_lexer_peek_token (lexer)->type == type;
444 }
445
446 /* Return true if the next token does not have the indicated TYPE.  */
447
448 static inline bool
449 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
450 {
451   return !cp_lexer_next_token_is (lexer, type);
452 }
453
454 /* Return true if the next token is the indicated KEYWORD.  */
455
456 static inline bool
457 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
458 {
459   cp_token *token;
460
461   /* Peek at the next token.  */
462   token = cp_lexer_peek_token (lexer);
463   /* Check to see if it is the indicated keyword.  */
464   return token->keyword == keyword;
465 }
466
467 /* Return a pointer to the Nth token in the token stream.  If N is 1,
468    then this is precisely equivalent to cp_lexer_peek_token (except
469    that it is not inline).  One would like to disallow that case, but
470    there is one case (cp_parser_nth_token_starts_template_id) where
471    the caller passes a variable for N and it might be 1.  */
472
473 static cp_token *
474 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
475 {
476   cp_token *token;
477
478   /* N is 1-based, not zero-based.  */
479   gcc_assert (n > 0 && lexer->next_token != &eof_token);
480
481   if (cp_lexer_debugging_p (lexer))
482     fprintf (cp_lexer_debug_stream,
483              "cp_lexer: peeking ahead %ld at token: ", (long)n);
484
485   --n;
486   token = lexer->next_token;
487   while (n != 0)
488     {
489       ++token;
490       if (token == lexer->last_token)
491         {
492           token = (cp_token *)&eof_token;
493           break;
494         }
495       
496       if (token->type != CPP_PURGED)
497         --n;
498     }
499
500   if (cp_lexer_debugging_p (lexer))
501     {
502       cp_lexer_print_token (cp_lexer_debug_stream, token);
503       putc ('\n', cp_lexer_debug_stream);
504     }
505
506   return token;
507 }
508
509 /* Return the next token, and advance the lexer's next_token pointer
510    to point to the next non-purged token.  */
511
512 static cp_token *
513 cp_lexer_consume_token (cp_lexer* lexer)
514 {
515   cp_token *token = lexer->next_token;
516
517   gcc_assert (token != &eof_token);
518   
519   do
520     {
521       lexer->next_token++;
522       if (lexer->next_token == lexer->last_token)
523         {
524           lexer->next_token = (cp_token *)&eof_token;
525           break;
526         }
527       
528     }
529   while (lexer->next_token->type == CPP_PURGED);
530   
531   cp_lexer_set_source_position_from_token (token);
532   
533   /* Provide debugging output.  */
534   if (cp_lexer_debugging_p (lexer))
535     {
536       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
537       cp_lexer_print_token (cp_lexer_debug_stream, token);
538       putc ('\n', cp_lexer_debug_stream);
539     }
540   
541   return token;
542 }
543
544 /* Permanently remove the next token from the token stream, and
545    advance the next_token pointer to refer to the next non-purged
546    token.  */
547
548 static void
549 cp_lexer_purge_token (cp_lexer *lexer)
550 {
551   cp_token *tok = lexer->next_token;
552   
553   gcc_assert (tok != &eof_token);
554   tok->type = CPP_PURGED;
555   tok->location = UNKNOWN_LOCATION;
556   tok->value = NULL_TREE;
557   tok->keyword = RID_MAX;
558
559   do
560     {
561       tok++;
562       if (tok == lexer->last_token)
563         {
564           tok = (cp_token *)&eof_token;
565           break;
566         }
567     }
568   while (tok->type == CPP_PURGED);
569   lexer->next_token = tok;
570 }
571
572 /* Permanently remove all tokens after TOK, up to, but not
573    including, the token that will be returned next by
574    cp_lexer_peek_token.  */
575
576 static void
577 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
578 {
579   cp_token *peek = lexer->next_token;
580
581   if (peek == &eof_token)
582     peek = lexer->last_token;
583   
584   gcc_assert (tok < peek);
585
586   for ( tok += 1; tok != peek; tok += 1)
587     {
588       tok->type = CPP_PURGED;
589       tok->location = UNKNOWN_LOCATION;
590       tok->value = NULL_TREE;
591       tok->keyword = RID_MAX;
592     }
593 }
594
595 /* Consume and handle a pragma token.  */
596 static void
597 cp_lexer_handle_pragma (cp_lexer *lexer)
598 {
599   cpp_string s;
600   cp_token *token = cp_lexer_consume_token (lexer);
601   gcc_assert (token->type == CPP_PRAGMA);
602   gcc_assert (token->value);
603
604   s.len = TREE_STRING_LENGTH (token->value);
605   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
606
607   cpp_handle_deferred_pragma (parse_in, &s);
608
609   /* Clearing token->value here means that we will get an ICE if we
610      try to process this #pragma again (which should be impossible).  */
611   token->value = NULL;
612 }
613
614 /* Begin saving tokens.  All tokens consumed after this point will be
615    preserved.  */
616
617 static void
618 cp_lexer_save_tokens (cp_lexer* lexer)
619 {
620   /* Provide debugging output.  */
621   if (cp_lexer_debugging_p (lexer))
622     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
623
624   VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
625 }
626
627 /* Commit to the portion of the token stream most recently saved.  */
628
629 static void
630 cp_lexer_commit_tokens (cp_lexer* lexer)
631 {
632   /* Provide debugging output.  */
633   if (cp_lexer_debugging_p (lexer))
634     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
635
636   VEC_pop (cp_token_position, lexer->saved_tokens);
637 }
638
639 /* Return all tokens saved since the last call to cp_lexer_save_tokens
640    to the token stream.  Stop saving tokens.  */
641
642 static void
643 cp_lexer_rollback_tokens (cp_lexer* lexer)
644 {
645   /* Provide debugging output.  */
646   if (cp_lexer_debugging_p (lexer))
647     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
648
649   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
650 }
651
652 /* Print a representation of the TOKEN on the STREAM.  */
653
654 #ifdef ENABLE_CHECKING
655
656 static void
657 cp_lexer_print_token (FILE * stream, cp_token *token)
658 {
659   /* We don't use cpp_type2name here because the parser defines
660      a few tokens of its own.  */
661   static const char *const token_names[] = {
662     /* cpplib-defined token types */
663 #define OP(e, s) #e,
664 #define TK(e, s) #e,
665     TTYPE_TABLE
666 #undef OP
667 #undef TK
668     /* C++ parser token types - see "Manifest constants", above.  */
669     "KEYWORD",
670     "TEMPLATE_ID",
671     "NESTED_NAME_SPECIFIER",
672     "PURGED"
673   };
674   
675   /* If we have a name for the token, print it out.  Otherwise, we
676      simply give the numeric code.  */
677   gcc_assert (token->type < ARRAY_SIZE(token_names));
678   fputs (token_names[token->type], stream);
679
680   /* For some tokens, print the associated data.  */
681   switch (token->type)
682     {
683     case CPP_KEYWORD:
684       /* Some keywords have a value that is not an IDENTIFIER_NODE.
685          For example, `struct' is mapped to an INTEGER_CST.  */
686       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
687         break;
688       /* else fall through */
689     case CPP_NAME:
690       fputs (IDENTIFIER_POINTER (token->value), stream);
691       break;
692
693     case CPP_STRING:
694     case CPP_WSTRING:
695     case CPP_PRAGMA:
696       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
697       break;
698
699     default:
700       break;
701     }
702 }
703
704 /* Start emitting debugging information.  */
705
706 static void
707 cp_lexer_start_debugging (cp_lexer* lexer)
708 {
709   ++lexer->debugging_p;
710 }
711
712 /* Stop emitting debugging information.  */
713
714 static void
715 cp_lexer_stop_debugging (cp_lexer* lexer)
716 {
717   --lexer->debugging_p;
718 }
719
720 #endif /* ENABLE_CHECKING */
721
722 /* Create a new cp_token_cache, representing a range of tokens.  */
723
724 static cp_token_cache *
725 cp_token_cache_new (cp_token *first, cp_token *last)
726 {
727   cp_token_cache *cache = GGC_NEW (cp_token_cache);
728   cache->first = first;
729   cache->last = last;
730   return cache;
731 }
732
733 \f
734 /* Decl-specifiers.  */
735
736 static void clear_decl_specs
737   (cp_decl_specifier_seq *);
738
739 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
740
741 static void
742 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
743 {
744   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
745 }
746
747 /* Declarators.  */
748
749 /* Nothing other than the parser should be creating declarators;
750    declarators are a semi-syntactic representation of C++ entities.
751    Other parts of the front end that need to create entities (like
752    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
753
754 static cp_declarator *make_call_declarator
755   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
756 static cp_declarator *make_array_declarator
757   (cp_declarator *, tree);
758 static cp_declarator *make_pointer_declarator
759   (cp_cv_quals, cp_declarator *);
760 static cp_declarator *make_reference_declarator
761   (cp_cv_quals, cp_declarator *);
762 static cp_parameter_declarator *make_parameter_declarator
763   (cp_decl_specifier_seq *, cp_declarator *, tree);
764 static cp_declarator *make_ptrmem_declarator
765   (cp_cv_quals, tree, cp_declarator *);
766
767 cp_declarator *cp_error_declarator;
768
769 /* The obstack on which declarators and related data structures are
770    allocated.  */
771 static struct obstack declarator_obstack;
772
773 /* Alloc BYTES from the declarator memory pool.  */
774
775 static inline void *
776 alloc_declarator (size_t bytes)
777 {
778   return obstack_alloc (&declarator_obstack, bytes);
779 }
780
781 /* Allocate a declarator of the indicated KIND.  Clear fields that are
782    common to all declarators.  */
783
784 static cp_declarator *
785 make_declarator (cp_declarator_kind kind)
786 {
787   cp_declarator *declarator;
788
789   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
790   declarator->kind = kind;
791   declarator->attributes = NULL_TREE;
792   declarator->declarator = NULL;
793
794   return declarator;
795 }
796
797 /* Make a declarator for a generalized identifier.  If non-NULL, the
798    identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
799    just UNQUALIFIED_NAME.  */
800
801 static cp_declarator *
802 make_id_declarator (tree qualifying_scope, tree unqualified_name)
803 {
804   cp_declarator *declarator;
805
806   /* It is valid to write:
807
808        class C { void f(); };
809        typedef C D;
810        void D::f();
811
812      The standard is not clear about whether `typedef const C D' is
813      legal; as of 2002-09-15 the committee is considering that
814      question.  EDG 3.0 allows that syntax.  Therefore, we do as
815      well.  */
816   if (qualifying_scope && TYPE_P (qualifying_scope))
817     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
818
819   declarator = make_declarator (cdk_id);
820   declarator->u.id.qualifying_scope = qualifying_scope;
821   declarator->u.id.unqualified_name = unqualified_name;
822   declarator->u.id.sfk = sfk_none;
823
824   return declarator;
825 }
826
827 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
828    of modifiers such as const or volatile to apply to the pointer
829    type, represented as identifiers.  */
830
831 cp_declarator *
832 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
833 {
834   cp_declarator *declarator;
835
836   declarator = make_declarator (cdk_pointer);
837   declarator->declarator = target;
838   declarator->u.pointer.qualifiers = cv_qualifiers;
839   declarator->u.pointer.class_type = NULL_TREE;
840
841   return declarator;
842 }
843
844 /* Like make_pointer_declarator -- but for references.  */
845
846 cp_declarator *
847 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
848 {
849   cp_declarator *declarator;
850
851   declarator = make_declarator (cdk_reference);
852   declarator->declarator = target;
853   declarator->u.pointer.qualifiers = cv_qualifiers;
854   declarator->u.pointer.class_type = NULL_TREE;
855
856   return declarator;
857 }
858
859 /* Like make_pointer_declarator -- but for a pointer to a non-static
860    member of CLASS_TYPE.  */
861
862 cp_declarator *
863 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
864                         cp_declarator *pointee)
865 {
866   cp_declarator *declarator;
867
868   declarator = make_declarator (cdk_ptrmem);
869   declarator->declarator = pointee;
870   declarator->u.pointer.qualifiers = cv_qualifiers;
871   declarator->u.pointer.class_type = class_type;
872
873   return declarator;
874 }
875
876 /* Make a declarator for the function given by TARGET, with the
877    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
878    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
879    indicates what exceptions can be thrown.  */
880
881 cp_declarator *
882 make_call_declarator (cp_declarator *target,
883                       cp_parameter_declarator *parms,
884                       cp_cv_quals cv_qualifiers,
885                       tree exception_specification)
886 {
887   cp_declarator *declarator;
888
889   declarator = make_declarator (cdk_function);
890   declarator->declarator = target;
891   declarator->u.function.parameters = parms;
892   declarator->u.function.qualifiers = cv_qualifiers;
893   declarator->u.function.exception_specification = exception_specification;
894
895   return declarator;
896 }
897
898 /* Make a declarator for an array of BOUNDS elements, each of which is
899    defined by ELEMENT.  */
900
901 cp_declarator *
902 make_array_declarator (cp_declarator *element, tree bounds)
903 {
904   cp_declarator *declarator;
905
906   declarator = make_declarator (cdk_array);
907   declarator->declarator = element;
908   declarator->u.array.bounds = bounds;
909
910   return declarator;
911 }
912
913 cp_parameter_declarator *no_parameters;
914
915 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
916    DECLARATOR and DEFAULT_ARGUMENT.  */
917
918 cp_parameter_declarator *
919 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
920                            cp_declarator *declarator,
921                            tree default_argument)
922 {
923   cp_parameter_declarator *parameter;
924
925   parameter = ((cp_parameter_declarator *)
926                alloc_declarator (sizeof (cp_parameter_declarator)));
927   parameter->next = NULL;
928   if (decl_specifiers)
929     parameter->decl_specifiers = *decl_specifiers;
930   else
931     clear_decl_specs (&parameter->decl_specifiers);
932   parameter->declarator = declarator;
933   parameter->default_argument = default_argument;
934   parameter->ellipsis_p = false;
935
936   return parameter;
937 }
938
939 /* The parser.  */
940
941 /* Overview
942    --------
943
944    A cp_parser parses the token stream as specified by the C++
945    grammar.  Its job is purely parsing, not semantic analysis.  For
946    example, the parser breaks the token stream into declarators,
947    expressions, statements, and other similar syntactic constructs.
948    It does not check that the types of the expressions on either side
949    of an assignment-statement are compatible, or that a function is
950    not declared with a parameter of type `void'.
951
952    The parser invokes routines elsewhere in the compiler to perform
953    semantic analysis and to build up the abstract syntax tree for the
954    code processed.
955
956    The parser (and the template instantiation code, which is, in a
957    way, a close relative of parsing) are the only parts of the
958    compiler that should be calling push_scope and pop_scope, or
959    related functions.  The parser (and template instantiation code)
960    keeps track of what scope is presently active; everything else
961    should simply honor that.  (The code that generates static
962    initializers may also need to set the scope, in order to check
963    access control correctly when emitting the initializers.)
964
965    Methodology
966    -----------
967
968    The parser is of the standard recursive-descent variety.  Upcoming
969    tokens in the token stream are examined in order to determine which
970    production to use when parsing a non-terminal.  Some C++ constructs
971    require arbitrary look ahead to disambiguate.  For example, it is
972    impossible, in the general case, to tell whether a statement is an
973    expression or declaration without scanning the entire statement.
974    Therefore, the parser is capable of "parsing tentatively."  When the
975    parser is not sure what construct comes next, it enters this mode.
976    Then, while we attempt to parse the construct, the parser queues up
977    error messages, rather than issuing them immediately, and saves the
978    tokens it consumes.  If the construct is parsed successfully, the
979    parser "commits", i.e., it issues any queued error messages and
980    the tokens that were being preserved are permanently discarded.
981    If, however, the construct is not parsed successfully, the parser
982    rolls back its state completely so that it can resume parsing using
983    a different alternative.
984
985    Future Improvements
986    -------------------
987
988    The performance of the parser could probably be improved substantially.
989    We could often eliminate the need to parse tentatively by looking ahead
990    a little bit.  In some places, this approach might not entirely eliminate
991    the need to parse tentatively, but it might still speed up the average
992    case.  */
993
994 /* Flags that are passed to some parsing functions.  These values can
995    be bitwise-ored together.  */
996
997 typedef enum cp_parser_flags
998 {
999   /* No flags.  */
1000   CP_PARSER_FLAGS_NONE = 0x0,
1001   /* The construct is optional.  If it is not present, then no error
1002      should be issued.  */
1003   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1004   /* When parsing a type-specifier, do not allow user-defined types.  */
1005   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1006 } cp_parser_flags;
1007
1008 /* The different kinds of declarators we want to parse.  */
1009
1010 typedef enum cp_parser_declarator_kind
1011 {
1012   /* We want an abstract declarator.  */
1013   CP_PARSER_DECLARATOR_ABSTRACT,
1014   /* We want a named declarator.  */
1015   CP_PARSER_DECLARATOR_NAMED,
1016   /* We don't mind, but the name must be an unqualified-id.  */
1017   CP_PARSER_DECLARATOR_EITHER
1018 } cp_parser_declarator_kind;
1019
1020 /* The precedence values used to parse binary expressions.  The minimum value
1021    of PREC must be 1, because zero is reserved to quickly discriminate
1022    binary operators from other tokens.  */
1023
1024 enum cp_parser_prec
1025 {
1026   PREC_NOT_OPERATOR,
1027   PREC_LOGICAL_OR_EXPRESSION,
1028   PREC_LOGICAL_AND_EXPRESSION,
1029   PREC_INCLUSIVE_OR_EXPRESSION,
1030   PREC_EXCLUSIVE_OR_EXPRESSION,
1031   PREC_AND_EXPRESSION,
1032   PREC_EQUALITY_EXPRESSION,
1033   PREC_RELATIONAL_EXPRESSION,
1034   PREC_SHIFT_EXPRESSION,
1035   PREC_ADDITIVE_EXPRESSION,
1036   PREC_MULTIPLICATIVE_EXPRESSION,
1037   PREC_PM_EXPRESSION,
1038   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1039 };
1040
1041 /* A mapping from a token type to a corresponding tree node type, with a
1042    precedence value.  */
1043
1044 typedef struct cp_parser_binary_operations_map_node
1045 {
1046   /* The token type.  */
1047   enum cpp_ttype token_type;
1048   /* The corresponding tree code.  */
1049   enum tree_code tree_type;
1050   /* The precedence of this operator.  */
1051   enum cp_parser_prec prec;
1052 } cp_parser_binary_operations_map_node;
1053
1054 /* The status of a tentative parse.  */
1055
1056 typedef enum cp_parser_status_kind
1057 {
1058   /* No errors have occurred.  */
1059   CP_PARSER_STATUS_KIND_NO_ERROR,
1060   /* An error has occurred.  */
1061   CP_PARSER_STATUS_KIND_ERROR,
1062   /* We are committed to this tentative parse, whether or not an error
1063      has occurred.  */
1064   CP_PARSER_STATUS_KIND_COMMITTED
1065 } cp_parser_status_kind;
1066
1067 typedef struct cp_parser_expression_stack_entry
1068 {
1069   tree lhs;
1070   enum tree_code tree_type;
1071   int prec;
1072 } cp_parser_expression_stack_entry;
1073
1074 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1075    entries because precedence levels on the stack are monotonically
1076    increasing.  */
1077 typedef struct cp_parser_expression_stack_entry
1078   cp_parser_expression_stack[NUM_PREC_VALUES];
1079
1080 /* Context that is saved and restored when parsing tentatively.  */
1081 typedef struct cp_parser_context GTY (())
1082 {
1083   /* If this is a tentative parsing context, the status of the
1084      tentative parse.  */
1085   enum cp_parser_status_kind status;
1086   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1087      that are looked up in this context must be looked up both in the
1088      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1089      the context of the containing expression.  */
1090   tree object_type;
1091
1092   /* The next parsing context in the stack.  */
1093   struct cp_parser_context *next;
1094 } cp_parser_context;
1095
1096 /* Prototypes.  */
1097
1098 /* Constructors and destructors.  */
1099
1100 static cp_parser_context *cp_parser_context_new
1101   (cp_parser_context *);
1102
1103 /* Class variables.  */
1104
1105 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1106
1107 /* The operator-precedence table used by cp_parser_binary_expression.
1108    Transformed into an associative array (binops_by_token) by
1109    cp_parser_new.  */
1110
1111 static const cp_parser_binary_operations_map_node binops[] = {
1112   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1113   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1114
1115   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1116   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1117   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1118
1119   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1120   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1121
1122   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1123   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1124
1125   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1126   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1127   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1128   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1129   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1130   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1131
1132   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1133   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1134
1135   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1136
1137   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1138
1139   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1140
1141   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1142
1143   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1144 };
1145
1146 /* The same as binops, but initialized by cp_parser_new so that
1147    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1148    for speed.  */
1149 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1150
1151 /* Constructors and destructors.  */
1152
1153 /* Construct a new context.  The context below this one on the stack
1154    is given by NEXT.  */
1155
1156 static cp_parser_context *
1157 cp_parser_context_new (cp_parser_context* next)
1158 {
1159   cp_parser_context *context;
1160
1161   /* Allocate the storage.  */
1162   if (cp_parser_context_free_list != NULL)
1163     {
1164       /* Pull the first entry from the free list.  */
1165       context = cp_parser_context_free_list;
1166       cp_parser_context_free_list = context->next;
1167       memset (context, 0, sizeof (*context));
1168     }
1169   else
1170     context = GGC_CNEW (cp_parser_context);
1171
1172   /* No errors have occurred yet in this context.  */
1173   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1174   /* If this is not the bottomost context, copy information that we
1175      need from the previous context.  */
1176   if (next)
1177     {
1178       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1179          expression, then we are parsing one in this context, too.  */
1180       context->object_type = next->object_type;
1181       /* Thread the stack.  */
1182       context->next = next;
1183     }
1184
1185   return context;
1186 }
1187
1188 /* The cp_parser structure represents the C++ parser.  */
1189
1190 typedef struct cp_parser GTY(())
1191 {
1192   /* The lexer from which we are obtaining tokens.  */
1193   cp_lexer *lexer;
1194
1195   /* The scope in which names should be looked up.  If NULL_TREE, then
1196      we look up names in the scope that is currently open in the
1197      source program.  If non-NULL, this is either a TYPE or
1198      NAMESPACE_DECL for the scope in which we should look.
1199
1200      This value is not cleared automatically after a name is looked
1201      up, so we must be careful to clear it before starting a new look
1202      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1203      will look up `Z' in the scope of `X', rather than the current
1204      scope.)  Unfortunately, it is difficult to tell when name lookup
1205      is complete, because we sometimes peek at a token, look it up,
1206      and then decide not to consume it.  */
1207   tree scope;
1208
1209   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1210      last lookup took place.  OBJECT_SCOPE is used if an expression
1211      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1212      respectively.  QUALIFYING_SCOPE is used for an expression of the
1213      form "X::Y"; it refers to X.  */
1214   tree object_scope;
1215   tree qualifying_scope;
1216
1217   /* A stack of parsing contexts.  All but the bottom entry on the
1218      stack will be tentative contexts.
1219
1220      We parse tentatively in order to determine which construct is in
1221      use in some situations.  For example, in order to determine
1222      whether a statement is an expression-statement or a
1223      declaration-statement we parse it tentatively as a
1224      declaration-statement.  If that fails, we then reparse the same
1225      token stream as an expression-statement.  */
1226   cp_parser_context *context;
1227
1228   /* True if we are parsing GNU C++.  If this flag is not set, then
1229      GNU extensions are not recognized.  */
1230   bool allow_gnu_extensions_p;
1231
1232   /* TRUE if the `>' token should be interpreted as the greater-than
1233      operator.  FALSE if it is the end of a template-id or
1234      template-parameter-list.  */
1235   bool greater_than_is_operator_p;
1236
1237   /* TRUE if default arguments are allowed within a parameter list
1238      that starts at this point. FALSE if only a gnu extension makes
1239      them permissible.  */
1240   bool default_arg_ok_p;
1241
1242   /* TRUE if we are parsing an integral constant-expression.  See
1243      [expr.const] for a precise definition.  */
1244   bool integral_constant_expression_p;
1245
1246   /* TRUE if we are parsing an integral constant-expression -- but a
1247      non-constant expression should be permitted as well.  This flag
1248      is used when parsing an array bound so that GNU variable-length
1249      arrays are tolerated.  */
1250   bool allow_non_integral_constant_expression_p;
1251
1252   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1253      been seen that makes the expression non-constant.  */
1254   bool non_integral_constant_expression_p;
1255
1256   /* TRUE if local variable names and `this' are forbidden in the
1257      current context.  */
1258   bool local_variables_forbidden_p;
1259
1260   /* TRUE if the declaration we are parsing is part of a
1261      linkage-specification of the form `extern string-literal
1262      declaration'.  */
1263   bool in_unbraced_linkage_specification_p;
1264
1265   /* TRUE if we are presently parsing a declarator, after the
1266      direct-declarator.  */
1267   bool in_declarator_p;
1268
1269   /* TRUE if we are presently parsing a template-argument-list.  */
1270   bool in_template_argument_list_p;
1271
1272   /* TRUE if we are presently parsing the body of an
1273      iteration-statement.  */
1274   bool in_iteration_statement_p;
1275
1276   /* TRUE if we are presently parsing the body of a switch
1277      statement.  */
1278   bool in_switch_statement_p;
1279
1280   /* TRUE if we are parsing a type-id in an expression context.  In
1281      such a situation, both "type (expr)" and "type (type)" are valid
1282      alternatives.  */
1283   bool in_type_id_in_expr_p;
1284
1285   /* TRUE if we are currently in a header file where declarations are
1286      implicitly extern "C".  */
1287   bool implicit_extern_c;
1288
1289   /* TRUE if strings in expressions should be translated to the execution
1290      character set.  */
1291   bool translate_strings_p;
1292
1293   /* If non-NULL, then we are parsing a construct where new type
1294      definitions are not permitted.  The string stored here will be
1295      issued as an error message if a type is defined.  */
1296   const char *type_definition_forbidden_message;
1297
1298   /* A list of lists. The outer list is a stack, used for member
1299      functions of local classes. At each level there are two sub-list,
1300      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1301      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1302      TREE_VALUE's. The functions are chained in reverse declaration
1303      order.
1304
1305      The TREE_PURPOSE sublist contains those functions with default
1306      arguments that need post processing, and the TREE_VALUE sublist
1307      contains those functions with definitions that need post
1308      processing.
1309
1310      These lists can only be processed once the outermost class being
1311      defined is complete.  */
1312   tree unparsed_functions_queues;
1313
1314   /* The number of classes whose definitions are currently in
1315      progress.  */
1316   unsigned num_classes_being_defined;
1317
1318   /* The number of template parameter lists that apply directly to the
1319      current declaration.  */
1320   unsigned num_template_parameter_lists;
1321 } cp_parser;
1322
1323 /* The type of a function that parses some kind of expression.  */
1324 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1325
1326 /* Prototypes.  */
1327
1328 /* Constructors and destructors.  */
1329
1330 static cp_parser *cp_parser_new
1331   (void);
1332
1333 /* Routines to parse various constructs.
1334
1335    Those that return `tree' will return the error_mark_node (rather
1336    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1337    Sometimes, they will return an ordinary node if error-recovery was
1338    attempted, even though a parse error occurred.  So, to check
1339    whether or not a parse error occurred, you should always use
1340    cp_parser_error_occurred.  If the construct is optional (indicated
1341    either by an `_opt' in the name of the function that does the
1342    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1343    the construct is not present.  */
1344
1345 /* Lexical conventions [gram.lex]  */
1346
1347 static tree cp_parser_identifier
1348   (cp_parser *);
1349 static tree cp_parser_string_literal
1350   (cp_parser *, bool, bool);
1351
1352 /* Basic concepts [gram.basic]  */
1353
1354 static bool cp_parser_translation_unit
1355   (cp_parser *);
1356
1357 /* Expressions [gram.expr]  */
1358
1359 static tree cp_parser_primary_expression
1360   (cp_parser *, bool, cp_id_kind *, tree *);
1361 static tree cp_parser_id_expression
1362   (cp_parser *, bool, bool, bool *, bool);
1363 static tree cp_parser_unqualified_id
1364   (cp_parser *, bool, bool, bool);
1365 static tree cp_parser_nested_name_specifier_opt
1366   (cp_parser *, bool, bool, bool, bool);
1367 static tree cp_parser_nested_name_specifier
1368   (cp_parser *, bool, bool, bool, bool);
1369 static tree cp_parser_class_or_namespace_name
1370   (cp_parser *, bool, bool, bool, bool, bool);
1371 static tree cp_parser_postfix_expression
1372   (cp_parser *, bool, bool);
1373 static tree cp_parser_postfix_open_square_expression
1374   (cp_parser *, tree, bool);
1375 static tree cp_parser_postfix_dot_deref_expression
1376   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1377 static tree cp_parser_parenthesized_expression_list
1378   (cp_parser *, bool, bool, bool *);
1379 static void cp_parser_pseudo_destructor_name
1380   (cp_parser *, tree *, tree *);
1381 static tree cp_parser_unary_expression
1382   (cp_parser *, bool, bool);
1383 static enum tree_code cp_parser_unary_operator
1384   (cp_token *);
1385 static tree cp_parser_new_expression
1386   (cp_parser *);
1387 static tree cp_parser_new_placement
1388   (cp_parser *);
1389 static tree cp_parser_new_type_id
1390   (cp_parser *, tree *);
1391 static cp_declarator *cp_parser_new_declarator_opt
1392   (cp_parser *);
1393 static cp_declarator *cp_parser_direct_new_declarator
1394   (cp_parser *);
1395 static tree cp_parser_new_initializer
1396   (cp_parser *);
1397 static tree cp_parser_delete_expression
1398   (cp_parser *);
1399 static tree cp_parser_cast_expression
1400   (cp_parser *, bool, bool);
1401 static tree cp_parser_binary_expression
1402   (cp_parser *, bool);
1403 static tree cp_parser_question_colon_clause
1404   (cp_parser *, tree);
1405 static tree cp_parser_assignment_expression
1406   (cp_parser *, bool);
1407 static enum tree_code cp_parser_assignment_operator_opt
1408   (cp_parser *);
1409 static tree cp_parser_expression
1410   (cp_parser *, bool);
1411 static tree cp_parser_constant_expression
1412   (cp_parser *, bool, bool *);
1413 static tree cp_parser_builtin_offsetof
1414   (cp_parser *);
1415
1416 /* Statements [gram.stmt.stmt]  */
1417
1418 static void cp_parser_statement
1419   (cp_parser *, tree);
1420 static tree cp_parser_labeled_statement
1421   (cp_parser *, tree);
1422 static tree cp_parser_expression_statement
1423   (cp_parser *, tree);
1424 static tree cp_parser_compound_statement
1425   (cp_parser *, tree, bool);
1426 static void cp_parser_statement_seq_opt
1427   (cp_parser *, tree);
1428 static tree cp_parser_selection_statement
1429   (cp_parser *);
1430 static tree cp_parser_condition
1431   (cp_parser *);
1432 static tree cp_parser_iteration_statement
1433   (cp_parser *);
1434 static void cp_parser_for_init_statement
1435   (cp_parser *);
1436 static tree cp_parser_jump_statement
1437   (cp_parser *);
1438 static void cp_parser_declaration_statement
1439   (cp_parser *);
1440
1441 static tree cp_parser_implicitly_scoped_statement
1442   (cp_parser *);
1443 static void cp_parser_already_scoped_statement
1444   (cp_parser *);
1445
1446 /* Declarations [gram.dcl.dcl] */
1447
1448 static void cp_parser_declaration_seq_opt
1449   (cp_parser *);
1450 static void cp_parser_declaration
1451   (cp_parser *);
1452 static void cp_parser_block_declaration
1453   (cp_parser *, bool);
1454 static void cp_parser_simple_declaration
1455   (cp_parser *, bool);
1456 static void cp_parser_decl_specifier_seq
1457   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1458 static tree cp_parser_storage_class_specifier_opt
1459   (cp_parser *);
1460 static tree cp_parser_function_specifier_opt
1461   (cp_parser *, cp_decl_specifier_seq *);
1462 static tree cp_parser_type_specifier
1463   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1464    int *, bool *);
1465 static tree cp_parser_simple_type_specifier
1466   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1467 static tree cp_parser_type_name
1468   (cp_parser *);
1469 static tree cp_parser_elaborated_type_specifier
1470   (cp_parser *, bool, bool);
1471 static tree cp_parser_enum_specifier
1472   (cp_parser *);
1473 static void cp_parser_enumerator_list
1474   (cp_parser *, tree);
1475 static void cp_parser_enumerator_definition
1476   (cp_parser *, tree);
1477 static tree cp_parser_namespace_name
1478   (cp_parser *);
1479 static void cp_parser_namespace_definition
1480   (cp_parser *);
1481 static void cp_parser_namespace_body
1482   (cp_parser *);
1483 static tree cp_parser_qualified_namespace_specifier
1484   (cp_parser *);
1485 static void cp_parser_namespace_alias_definition
1486   (cp_parser *);
1487 static void cp_parser_using_declaration
1488   (cp_parser *);
1489 static void cp_parser_using_directive
1490   (cp_parser *);
1491 static void cp_parser_asm_definition
1492   (cp_parser *);
1493 static void cp_parser_linkage_specification
1494   (cp_parser *);
1495
1496 /* Declarators [gram.dcl.decl] */
1497
1498 static tree cp_parser_init_declarator
1499   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1500 static cp_declarator *cp_parser_declarator
1501   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1502 static cp_declarator *cp_parser_direct_declarator
1503   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1504 static enum tree_code cp_parser_ptr_operator
1505   (cp_parser *, tree *, cp_cv_quals *);
1506 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1507   (cp_parser *);
1508 static tree cp_parser_declarator_id
1509   (cp_parser *);
1510 static tree cp_parser_type_id
1511   (cp_parser *);
1512 static void cp_parser_type_specifier_seq
1513   (cp_parser *, cp_decl_specifier_seq *);
1514 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1515   (cp_parser *);
1516 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1517   (cp_parser *, bool *);
1518 static cp_parameter_declarator *cp_parser_parameter_declaration
1519   (cp_parser *, bool, bool *);
1520 static void cp_parser_function_body
1521   (cp_parser *);
1522 static tree cp_parser_initializer
1523   (cp_parser *, bool *, bool *);
1524 static tree cp_parser_initializer_clause
1525   (cp_parser *, bool *);
1526 static tree cp_parser_initializer_list
1527   (cp_parser *, bool *);
1528
1529 static bool cp_parser_ctor_initializer_opt_and_function_body
1530   (cp_parser *);
1531
1532 /* Classes [gram.class] */
1533
1534 static tree cp_parser_class_name
1535   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1536 static tree cp_parser_class_specifier
1537   (cp_parser *);
1538 static tree cp_parser_class_head
1539   (cp_parser *, bool *, tree *);
1540 static enum tag_types cp_parser_class_key
1541   (cp_parser *);
1542 static void cp_parser_member_specification_opt
1543   (cp_parser *);
1544 static void cp_parser_member_declaration
1545   (cp_parser *);
1546 static tree cp_parser_pure_specifier
1547   (cp_parser *);
1548 static tree cp_parser_constant_initializer
1549   (cp_parser *);
1550
1551 /* Derived classes [gram.class.derived] */
1552
1553 static tree cp_parser_base_clause
1554   (cp_parser *);
1555 static tree cp_parser_base_specifier
1556   (cp_parser *);
1557
1558 /* Special member functions [gram.special] */
1559
1560 static tree cp_parser_conversion_function_id
1561   (cp_parser *);
1562 static tree cp_parser_conversion_type_id
1563   (cp_parser *);
1564 static cp_declarator *cp_parser_conversion_declarator_opt
1565   (cp_parser *);
1566 static bool cp_parser_ctor_initializer_opt
1567   (cp_parser *);
1568 static void cp_parser_mem_initializer_list
1569   (cp_parser *);
1570 static tree cp_parser_mem_initializer
1571   (cp_parser *);
1572 static tree cp_parser_mem_initializer_id
1573   (cp_parser *);
1574
1575 /* Overloading [gram.over] */
1576
1577 static tree cp_parser_operator_function_id
1578   (cp_parser *);
1579 static tree cp_parser_operator
1580   (cp_parser *);
1581
1582 /* Templates [gram.temp] */
1583
1584 static void cp_parser_template_declaration
1585   (cp_parser *, bool);
1586 static tree cp_parser_template_parameter_list
1587   (cp_parser *);
1588 static tree cp_parser_template_parameter
1589   (cp_parser *, bool *);
1590 static tree cp_parser_type_parameter
1591   (cp_parser *);
1592 static tree cp_parser_template_id
1593   (cp_parser *, bool, bool, bool);
1594 static tree cp_parser_template_name
1595   (cp_parser *, bool, bool, bool, bool *);
1596 static tree cp_parser_template_argument_list
1597   (cp_parser *);
1598 static tree cp_parser_template_argument
1599   (cp_parser *);
1600 static void cp_parser_explicit_instantiation
1601   (cp_parser *);
1602 static void cp_parser_explicit_specialization
1603   (cp_parser *);
1604
1605 /* Exception handling [gram.exception] */
1606
1607 static tree cp_parser_try_block
1608   (cp_parser *);
1609 static bool cp_parser_function_try_block
1610   (cp_parser *);
1611 static void cp_parser_handler_seq
1612   (cp_parser *);
1613 static void cp_parser_handler
1614   (cp_parser *);
1615 static tree cp_parser_exception_declaration
1616   (cp_parser *);
1617 static tree cp_parser_throw_expression
1618   (cp_parser *);
1619 static tree cp_parser_exception_specification_opt
1620   (cp_parser *);
1621 static tree cp_parser_type_id_list
1622   (cp_parser *);
1623
1624 /* GNU Extensions */
1625
1626 static tree cp_parser_asm_specification_opt
1627   (cp_parser *);
1628 static tree cp_parser_asm_operand_list
1629   (cp_parser *);
1630 static tree cp_parser_asm_clobber_list
1631   (cp_parser *);
1632 static tree cp_parser_attributes_opt
1633   (cp_parser *);
1634 static tree cp_parser_attribute_list
1635   (cp_parser *);
1636 static bool cp_parser_extension_opt
1637   (cp_parser *, int *);
1638 static void cp_parser_label_declaration
1639   (cp_parser *);
1640
1641 /* Utility Routines */
1642
1643 static tree cp_parser_lookup_name
1644   (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1645 static tree cp_parser_lookup_name_simple
1646   (cp_parser *, tree);
1647 static tree cp_parser_maybe_treat_template_as_class
1648   (tree, bool);
1649 static bool cp_parser_check_declarator_template_parameters
1650   (cp_parser *, cp_declarator *);
1651 static bool cp_parser_check_template_parameters
1652   (cp_parser *, unsigned);
1653 static tree cp_parser_simple_cast_expression
1654   (cp_parser *);
1655 static tree cp_parser_global_scope_opt
1656   (cp_parser *, bool);
1657 static bool cp_parser_constructor_declarator_p
1658   (cp_parser *, bool);
1659 static tree cp_parser_function_definition_from_specifiers_and_declarator
1660   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1661 static tree cp_parser_function_definition_after_declarator
1662   (cp_parser *, bool);
1663 static void cp_parser_template_declaration_after_export
1664   (cp_parser *, bool);
1665 static tree cp_parser_single_declaration
1666   (cp_parser *, bool, bool *);
1667 static tree cp_parser_functional_cast
1668   (cp_parser *, tree);
1669 static tree cp_parser_save_member_function_body
1670   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1671 static tree cp_parser_enclosed_template_argument_list
1672   (cp_parser *);
1673 static void cp_parser_save_default_args
1674   (cp_parser *, tree);
1675 static void cp_parser_late_parsing_for_member
1676   (cp_parser *, tree);
1677 static void cp_parser_late_parsing_default_args
1678   (cp_parser *, tree);
1679 static tree cp_parser_sizeof_operand
1680   (cp_parser *, enum rid);
1681 static bool cp_parser_declares_only_class_p
1682   (cp_parser *);
1683 static void cp_parser_set_storage_class
1684   (cp_decl_specifier_seq *, cp_storage_class);
1685 static void cp_parser_set_decl_spec_type
1686   (cp_decl_specifier_seq *, tree, bool);
1687 static bool cp_parser_friend_p
1688   (const cp_decl_specifier_seq *);
1689 static cp_token *cp_parser_require
1690   (cp_parser *, enum cpp_ttype, const char *);
1691 static cp_token *cp_parser_require_keyword
1692   (cp_parser *, enum rid, const char *);
1693 static bool cp_parser_token_starts_function_definition_p
1694   (cp_token *);
1695 static bool cp_parser_next_token_starts_class_definition_p
1696   (cp_parser *);
1697 static bool cp_parser_next_token_ends_template_argument_p
1698   (cp_parser *);
1699 static bool cp_parser_nth_token_starts_template_argument_list_p
1700   (cp_parser *, size_t);
1701 static enum tag_types cp_parser_token_is_class_key
1702   (cp_token *);
1703 static void cp_parser_check_class_key
1704   (enum tag_types, tree type);
1705 static void cp_parser_check_access_in_redeclaration
1706   (tree type);
1707 static bool cp_parser_optional_template_keyword
1708   (cp_parser *);
1709 static void cp_parser_pre_parsed_nested_name_specifier
1710   (cp_parser *);
1711 static void cp_parser_cache_group
1712   (cp_parser *, enum cpp_ttype, unsigned);
1713 static void cp_parser_parse_tentatively
1714   (cp_parser *);
1715 static void cp_parser_commit_to_tentative_parse
1716   (cp_parser *);
1717 static void cp_parser_abort_tentative_parse
1718   (cp_parser *);
1719 static bool cp_parser_parse_definitely
1720   (cp_parser *);
1721 static inline bool cp_parser_parsing_tentatively
1722   (cp_parser *);
1723 static bool cp_parser_uncommitted_to_tentative_parse_p
1724   (cp_parser *);
1725 static void cp_parser_error
1726   (cp_parser *, const char *);
1727 static void cp_parser_name_lookup_error
1728   (cp_parser *, tree, tree, const char *);
1729 static bool cp_parser_simulate_error
1730   (cp_parser *);
1731 static void cp_parser_check_type_definition
1732   (cp_parser *);
1733 static void cp_parser_check_for_definition_in_return_type
1734   (cp_declarator *, tree);
1735 static void cp_parser_check_for_invalid_template_id
1736   (cp_parser *, tree);
1737 static bool cp_parser_non_integral_constant_expression
1738   (cp_parser *, const char *);
1739 static void cp_parser_diagnose_invalid_type_name
1740   (cp_parser *, tree, tree);
1741 static bool cp_parser_parse_and_diagnose_invalid_type_name
1742   (cp_parser *);
1743 static int cp_parser_skip_to_closing_parenthesis
1744   (cp_parser *, bool, bool, bool);
1745 static void cp_parser_skip_to_end_of_statement
1746   (cp_parser *);
1747 static void cp_parser_consume_semicolon_at_end_of_statement
1748   (cp_parser *);
1749 static void cp_parser_skip_to_end_of_block_or_statement
1750   (cp_parser *);
1751 static void cp_parser_skip_to_closing_brace
1752   (cp_parser *);
1753 static void cp_parser_skip_until_found
1754   (cp_parser *, enum cpp_ttype, const char *);
1755 static bool cp_parser_error_occurred
1756   (cp_parser *);
1757 static bool cp_parser_allow_gnu_extensions_p
1758   (cp_parser *);
1759 static bool cp_parser_is_string_literal
1760   (cp_token *);
1761 static bool cp_parser_is_keyword
1762   (cp_token *, enum rid);
1763 static tree cp_parser_make_typename_type
1764   (cp_parser *, tree, tree);
1765
1766 /* Returns nonzero if we are parsing tentatively.  */
1767
1768 static inline bool
1769 cp_parser_parsing_tentatively (cp_parser* parser)
1770 {
1771   return parser->context->next != NULL;
1772 }
1773
1774 /* Returns nonzero if TOKEN is a string literal.  */
1775
1776 static bool
1777 cp_parser_is_string_literal (cp_token* token)
1778 {
1779   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1780 }
1781
1782 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1783
1784 static bool
1785 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1786 {
1787   return token->keyword == keyword;
1788 }
1789
1790 /* If not parsing tentatively, issue a diagnostic of the form
1791       FILE:LINE: MESSAGE before TOKEN
1792    where TOKEN is the next token in the input stream.  MESSAGE
1793    (specified by the caller) is usually of the form "expected
1794    OTHER-TOKEN".  */
1795
1796 static void
1797 cp_parser_error (cp_parser* parser, const char* message)
1798 {
1799   if (!cp_parser_simulate_error (parser))
1800     {
1801       cp_token *token = cp_lexer_peek_token (parser->lexer);
1802       /* This diagnostic makes more sense if it is tagged to the line
1803          of the token we just peeked at.  */
1804       cp_lexer_set_source_position_from_token (token);
1805       if (token->type == CPP_PRAGMA)
1806         {
1807           error ("%<#pragma%> is not allowed here"); 
1808           cp_lexer_purge_token (parser->lexer);
1809           return;
1810         }
1811       c_parse_error (message,
1812                      /* Because c_parser_error does not understand
1813                         CPP_KEYWORD, keywords are treated like
1814                         identifiers.  */
1815                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1816                      token->value);
1817     }
1818 }
1819
1820 /* Issue an error about name-lookup failing.  NAME is the
1821    IDENTIFIER_NODE DECL is the result of
1822    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1823    the thing that we hoped to find.  */
1824
1825 static void
1826 cp_parser_name_lookup_error (cp_parser* parser,
1827                              tree name,
1828                              tree decl,
1829                              const char* desired)
1830 {
1831   /* If name lookup completely failed, tell the user that NAME was not
1832      declared.  */
1833   if (decl == error_mark_node)
1834     {
1835       if (parser->scope && parser->scope != global_namespace)
1836         error ("%<%D::%D%> has not been declared",
1837                parser->scope, name);
1838       else if (parser->scope == global_namespace)
1839         error ("%<::%D%> has not been declared", name);
1840       else if (parser->object_scope 
1841                && !CLASS_TYPE_P (parser->object_scope))
1842         error ("request for member %qD in non-class type %qT",
1843                name, parser->object_scope);
1844       else if (parser->object_scope)
1845         error ("%<%T::%D%> has not been declared", 
1846                parser->object_scope, name);
1847       else
1848         error ("%qD has not been declared", name);
1849     }
1850   else if (parser->scope && parser->scope != global_namespace)
1851     error ("%<%D::%D%> %s", parser->scope, name, desired);
1852   else if (parser->scope == global_namespace)
1853     error ("%<::%D%> %s", name, desired);
1854   else
1855     error ("%qD %s", name, desired);
1856 }
1857
1858 /* If we are parsing tentatively, remember that an error has occurred
1859    during this tentative parse.  Returns true if the error was
1860    simulated; false if a message should be issued by the caller.  */
1861
1862 static bool
1863 cp_parser_simulate_error (cp_parser* parser)
1864 {
1865   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1866     {
1867       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1868       return true;
1869     }
1870   return false;
1871 }
1872
1873 /* This function is called when a type is defined.  If type
1874    definitions are forbidden at this point, an error message is
1875    issued.  */
1876
1877 static void
1878 cp_parser_check_type_definition (cp_parser* parser)
1879 {
1880   /* If types are forbidden here, issue a message.  */
1881   if (parser->type_definition_forbidden_message)
1882     /* Use `%s' to print the string in case there are any escape
1883        characters in the message.  */
1884     error ("%s", parser->type_definition_forbidden_message);
1885 }
1886
1887 /* This function is called when the DECLARATOR is processed.  The TYPE
1888    was a type defined in the decl-specifiers.  If it is invalid to
1889    define a type in the decl-specifiers for DECLARATOR, an error is
1890    issued.  */
1891
1892 static void
1893 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1894                                                tree type)
1895 {
1896   /* [dcl.fct] forbids type definitions in return types.
1897      Unfortunately, it's not easy to know whether or not we are
1898      processing a return type until after the fact.  */
1899   while (declarator
1900          && (declarator->kind == cdk_pointer
1901              || declarator->kind == cdk_reference
1902              || declarator->kind == cdk_ptrmem))
1903     declarator = declarator->declarator;
1904   if (declarator
1905       && declarator->kind == cdk_function)
1906     {
1907       error ("new types may not be defined in a return type");
1908       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1909               type);
1910     }
1911 }
1912
1913 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1914    "<" in any valid C++ program.  If the next token is indeed "<",
1915    issue a message warning the user about what appears to be an
1916    invalid attempt to form a template-id.  */
1917
1918 static void
1919 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1920                                          tree type)
1921 {
1922   cp_token_position start = 0;
1923
1924   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1925     {
1926       if (TYPE_P (type))
1927         error ("%qT is not a template", type);
1928       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1929         error ("%qE is not a template", type);
1930       else
1931         error ("invalid template-id");
1932       /* Remember the location of the invalid "<".  */
1933       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1934         start = cp_lexer_token_position (parser->lexer, true);
1935       /* Consume the "<".  */
1936       cp_lexer_consume_token (parser->lexer);
1937       /* Parse the template arguments.  */
1938       cp_parser_enclosed_template_argument_list (parser);
1939       /* Permanently remove the invalid template arguments so that
1940          this error message is not issued again.  */
1941       if (start)
1942         cp_lexer_purge_tokens_after (parser->lexer, start);
1943     }
1944 }
1945
1946 /* If parsing an integral constant-expression, issue an error message
1947    about the fact that THING appeared and return true.  Otherwise,
1948    return false.  In either case, set
1949    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */ 
1950
1951 static bool
1952 cp_parser_non_integral_constant_expression (cp_parser  *parser,
1953                                             const char *thing)
1954 {
1955   parser->non_integral_constant_expression_p = true;
1956   if (parser->integral_constant_expression_p)
1957     {
1958       if (!parser->allow_non_integral_constant_expression_p)
1959         {
1960           error ("%s cannot appear in a constant-expression", thing);
1961           return true;
1962         }
1963     }
1964   return false;
1965 }
1966
1967 /* Emit a diagnostic for an invalid type name.  SCOPE is the
1968    qualifying scope (or NULL, if none) for ID.  This function commits
1969    to the current active tentative parse, if any.  (Otherwise, the
1970    problematic construct might be encountered again later, resulting
1971    in duplicate error messages.)  */
1972
1973 static void
1974 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1975 {
1976   tree decl, old_scope;
1977   /* Try to lookup the identifier.  */
1978   old_scope = parser->scope;
1979   parser->scope = scope;
1980   decl = cp_parser_lookup_name_simple (parser, id);
1981   parser->scope = old_scope;
1982   /* If the lookup found a template-name, it means that the user forgot
1983   to specify an argument list. Emit an useful error message.  */
1984   if (TREE_CODE (decl) == TEMPLATE_DECL)
1985     error ("invalid use of template-name %qE without an argument list",
1986       decl);
1987   else if (!parser->scope)
1988     {
1989       /* Issue an error message.  */
1990       error ("%qE does not name a type", id);
1991       /* If we're in a template class, it's possible that the user was
1992          referring to a type from a base class.  For example:
1993
1994            template <typename T> struct A { typedef T X; };
1995            template <typename T> struct B : public A<T> { X x; };
1996
1997          The user should have said "typename A<T>::X".  */
1998       if (processing_template_decl && current_class_type)
1999         {
2000           tree b;
2001
2002           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2003                b;
2004                b = TREE_CHAIN (b))
2005             {
2006               tree base_type = BINFO_TYPE (b);
2007               if (CLASS_TYPE_P (base_type)
2008                   && dependent_type_p (base_type))
2009                 {
2010                   tree field;
2011                   /* Go from a particular instantiation of the
2012                      template (which will have an empty TYPE_FIELDs),
2013                      to the main version.  */
2014                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2015                   for (field = TYPE_FIELDS (base_type);
2016                        field;
2017                        field = TREE_CHAIN (field))
2018                     if (TREE_CODE (field) == TYPE_DECL
2019                         && DECL_NAME (field) == id)
2020                       {
2021                         inform ("(perhaps %<typename %T::%E%> was intended)",
2022                                 BINFO_TYPE (b), id);
2023                         break;
2024                       }
2025                   if (field)
2026                     break;
2027                 }
2028             }
2029         }
2030     }
2031   /* Here we diagnose qualified-ids where the scope is actually correct,
2032      but the identifier does not resolve to a valid type name.  */
2033   else
2034     {
2035       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2036         error ("%qE in namespace %qE does not name a type",
2037                id, parser->scope);
2038       else if (TYPE_P (parser->scope))
2039         error ("%qE in class %qT does not name a type", id, parser->scope);
2040       else
2041         gcc_unreachable ();
2042     }
2043   cp_parser_commit_to_tentative_parse (parser);
2044 }
2045
2046 /* Check for a common situation where a type-name should be present,
2047    but is not, and issue a sensible error message.  Returns true if an
2048    invalid type-name was detected.
2049
2050    The situation handled by this function are variable declarations of the
2051    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2052    Usually, `ID' should name a type, but if we got here it means that it
2053    does not. We try to emit the best possible error message depending on
2054    how exactly the id-expression looks like.
2055 */
2056
2057 static bool
2058 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2059 {
2060   tree id;
2061
2062   cp_parser_parse_tentatively (parser);
2063   id = cp_parser_id_expression (parser,
2064                                 /*template_keyword_p=*/false,
2065                                 /*check_dependency_p=*/true,
2066                                 /*template_p=*/NULL,
2067                                 /*declarator_p=*/true);
2068   /* After the id-expression, there should be a plain identifier,
2069      otherwise this is not a simple variable declaration. Also, if
2070      the scope is dependent, we cannot do much.  */
2071   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2072       || (parser->scope && TYPE_P (parser->scope)
2073           && dependent_type_p (parser->scope)))
2074     {
2075       cp_parser_abort_tentative_parse (parser);
2076       return false;
2077     }
2078   if (!cp_parser_parse_definitely (parser)
2079       || TREE_CODE (id) != IDENTIFIER_NODE)
2080     return false;
2081
2082   /* Emit a diagnostic for the invalid type.  */
2083   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2084   /* Skip to the end of the declaration; there's no point in
2085      trying to process it.  */
2086   cp_parser_skip_to_end_of_block_or_statement (parser);
2087   return true;
2088 }
2089
2090 /* Consume tokens up to, and including, the next non-nested closing `)'.
2091    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2092    are doing error recovery. Returns -1 if OR_COMMA is true and we
2093    found an unnested comma.  */
2094
2095 static int
2096 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2097                                        bool recovering,
2098                                        bool or_comma,
2099                                        bool consume_paren)
2100 {
2101   unsigned paren_depth = 0;
2102   unsigned brace_depth = 0;
2103   int result;
2104
2105   if (recovering && !or_comma
2106       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2107     return 0;
2108
2109   while (true)
2110     {
2111       cp_token *token;
2112
2113       /* If we've run out of tokens, then there is no closing `)'.  */
2114       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2115         {
2116           result = 0;
2117           break;
2118         }
2119
2120       token = cp_lexer_peek_token (parser->lexer);
2121
2122       /* This matches the processing in skip_to_end_of_statement.  */
2123       if (token->type == CPP_SEMICOLON && !brace_depth)
2124         {
2125           result = 0;
2126           break;
2127         }
2128       if (token->type == CPP_OPEN_BRACE)
2129         ++brace_depth;
2130       if (token->type == CPP_CLOSE_BRACE)
2131         {
2132           if (!brace_depth--)
2133             {
2134               result = 0;
2135               break;
2136             }
2137         }
2138       if (recovering && or_comma && token->type == CPP_COMMA
2139           && !brace_depth && !paren_depth)
2140         {
2141           result = -1;
2142           break;
2143         }
2144
2145       if (!brace_depth)
2146         {
2147           /* If it is an `(', we have entered another level of nesting.  */
2148           if (token->type == CPP_OPEN_PAREN)
2149             ++paren_depth;
2150           /* If it is a `)', then we might be done.  */
2151           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2152             {
2153               if (consume_paren)
2154                 cp_lexer_consume_token (parser->lexer);
2155               {
2156                 result = 1;
2157                 break;
2158               }
2159             }
2160         }
2161
2162       /* Consume the token.  */
2163       cp_lexer_consume_token (parser->lexer);
2164     }
2165
2166   return result;
2167 }
2168
2169 /* Consume tokens until we reach the end of the current statement.
2170    Normally, that will be just before consuming a `;'.  However, if a
2171    non-nested `}' comes first, then we stop before consuming that.  */
2172
2173 static void
2174 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2175 {
2176   unsigned nesting_depth = 0;
2177
2178   while (true)
2179     {
2180       cp_token *token;
2181
2182       /* Peek at the next token.  */
2183       token = cp_lexer_peek_token (parser->lexer);
2184       /* If we've run out of tokens, stop.  */
2185       if (token->type == CPP_EOF)
2186         break;
2187       /* If the next token is a `;', we have reached the end of the
2188          statement.  */
2189       if (token->type == CPP_SEMICOLON && !nesting_depth)
2190         break;
2191       /* If the next token is a non-nested `}', then we have reached
2192          the end of the current block.  */
2193       if (token->type == CPP_CLOSE_BRACE)
2194         {
2195           /* If this is a non-nested `}', stop before consuming it.
2196              That way, when confronted with something like:
2197
2198                { 3 + }
2199
2200              we stop before consuming the closing `}', even though we
2201              have not yet reached a `;'.  */
2202           if (nesting_depth == 0)
2203             break;
2204           /* If it is the closing `}' for a block that we have
2205              scanned, stop -- but only after consuming the token.
2206              That way given:
2207
2208                 void f g () { ... }
2209                 typedef int I;
2210
2211              we will stop after the body of the erroneously declared
2212              function, but before consuming the following `typedef'
2213              declaration.  */
2214           if (--nesting_depth == 0)
2215             {
2216               cp_lexer_consume_token (parser->lexer);
2217               break;
2218             }
2219         }
2220       /* If it the next token is a `{', then we are entering a new
2221          block.  Consume the entire block.  */
2222       else if (token->type == CPP_OPEN_BRACE)
2223         ++nesting_depth;
2224       /* Consume the token.  */
2225       cp_lexer_consume_token (parser->lexer);
2226     }
2227 }
2228
2229 /* This function is called at the end of a statement or declaration.
2230    If the next token is a semicolon, it is consumed; otherwise, error
2231    recovery is attempted.  */
2232
2233 static void
2234 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2235 {
2236   /* Look for the trailing `;'.  */
2237   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2238     {
2239       /* If there is additional (erroneous) input, skip to the end of
2240          the statement.  */
2241       cp_parser_skip_to_end_of_statement (parser);
2242       /* If the next token is now a `;', consume it.  */
2243       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2244         cp_lexer_consume_token (parser->lexer);
2245     }
2246 }
2247
2248 /* Skip tokens until we have consumed an entire block, or until we
2249    have consumed a non-nested `;'.  */
2250
2251 static void
2252 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2253 {
2254   unsigned nesting_depth = 0;
2255
2256   while (true)
2257     {
2258       cp_token *token;
2259
2260       /* Peek at the next token.  */
2261       token = cp_lexer_peek_token (parser->lexer);
2262       /* If we've run out of tokens, stop.  */
2263       if (token->type == CPP_EOF)
2264         break;
2265       /* If the next token is a `;', we have reached the end of the
2266          statement.  */
2267       if (token->type == CPP_SEMICOLON && !nesting_depth)
2268         {
2269           /* Consume the `;'.  */
2270           cp_lexer_consume_token (parser->lexer);
2271           break;
2272         }
2273       /* Consume the token.  */
2274       token = cp_lexer_consume_token (parser->lexer);
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
2278           && (nesting_depth == 0 || --nesting_depth == 0))
2279         break;
2280       /* If it the next token is a `{', then we are entering a new
2281          block.  Consume the entire block.  */
2282       if (token->type == CPP_OPEN_BRACE)
2283         ++nesting_depth;
2284     }
2285 }
2286
2287 /* Skip tokens until a non-nested closing curly brace is the next
2288    token.  */
2289
2290 static void
2291 cp_parser_skip_to_closing_brace (cp_parser *parser)
2292 {
2293   unsigned nesting_depth = 0;
2294
2295   while (true)
2296     {
2297       cp_token *token;
2298
2299       /* Peek at the next token.  */
2300       token = cp_lexer_peek_token (parser->lexer);
2301       /* If we've run out of tokens, stop.  */
2302       if (token->type == CPP_EOF)
2303         break;
2304       /* If the next token is a non-nested `}', then we have reached
2305          the end of the current block.  */
2306       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2307         break;
2308       /* If it the next token is a `{', then we are entering a new
2309          block.  Consume the entire block.  */
2310       else if (token->type == CPP_OPEN_BRACE)
2311         ++nesting_depth;
2312       /* Consume the token.  */
2313       cp_lexer_consume_token (parser->lexer);
2314     }
2315 }
2316
2317 /* This is a simple wrapper around make_typename_type. When the id is
2318    an unresolved identifier node, we can provide a superior diagnostic
2319    using cp_parser_diagnose_invalid_type_name.  */
2320
2321 static tree
2322 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2323 {
2324   tree result;
2325   if (TREE_CODE (id) == IDENTIFIER_NODE)
2326     {
2327       result = make_typename_type (scope, id, typename_type,
2328                                    /*complain=*/0);
2329       if (result == error_mark_node)
2330         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2331       return result;
2332     }
2333   return make_typename_type (scope, id, typename_type, tf_error);
2334 }
2335
2336
2337 /* Create a new C++ parser.  */
2338
2339 static cp_parser *
2340 cp_parser_new (void)
2341 {
2342   cp_parser *parser;
2343   cp_lexer *lexer;
2344   unsigned i;
2345
2346   /* cp_lexer_new_main is called before calling ggc_alloc because
2347      cp_lexer_new_main might load a PCH file.  */
2348   lexer = cp_lexer_new_main ();
2349
2350   /* Initialize the binops_by_token so that we can get the tree
2351      directly from the token.  */
2352   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2353     binops_by_token[binops[i].token_type] = binops[i];
2354
2355   parser = GGC_CNEW (cp_parser);
2356   parser->lexer = lexer;
2357   parser->context = cp_parser_context_new (NULL);
2358
2359   /* For now, we always accept GNU extensions.  */
2360   parser->allow_gnu_extensions_p = 1;
2361
2362   /* The `>' token is a greater-than operator, not the end of a
2363      template-id.  */
2364   parser->greater_than_is_operator_p = true;
2365
2366   parser->default_arg_ok_p = true;
2367
2368   /* We are not parsing a constant-expression.  */
2369   parser->integral_constant_expression_p = false;
2370   parser->allow_non_integral_constant_expression_p = false;
2371   parser->non_integral_constant_expression_p = false;
2372
2373   /* Local variable names are not forbidden.  */
2374   parser->local_variables_forbidden_p = false;
2375
2376   /* We are not processing an `extern "C"' declaration.  */
2377   parser->in_unbraced_linkage_specification_p = false;
2378
2379   /* We are not processing a declarator.  */
2380   parser->in_declarator_p = false;
2381
2382   /* We are not processing a template-argument-list.  */
2383   parser->in_template_argument_list_p = false;
2384
2385   /* We are not in an iteration statement.  */
2386   parser->in_iteration_statement_p = false;
2387
2388   /* We are not in a switch statement.  */
2389   parser->in_switch_statement_p = false;
2390
2391   /* We are not parsing a type-id inside an expression.  */
2392   parser->in_type_id_in_expr_p = false;
2393
2394   /* Declarations aren't implicitly extern "C".  */
2395   parser->implicit_extern_c = false;
2396
2397   /* String literals should be translated to the execution character set.  */
2398   parser->translate_strings_p = true;
2399
2400   /* The unparsed function queue is empty.  */
2401   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2402
2403   /* There are no classes being defined.  */
2404   parser->num_classes_being_defined = 0;
2405
2406   /* No template parameters apply.  */
2407   parser->num_template_parameter_lists = 0;
2408
2409   return parser;
2410 }
2411
2412 /* Create a cp_lexer structure which will emit the tokens in CACHE
2413    and push it onto the parser's lexer stack.  This is used for delayed
2414    parsing of in-class method bodies and default arguments, and should
2415    not be confused with tentative parsing.  */
2416 static void
2417 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2418 {
2419   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2420   lexer->next = parser->lexer;
2421   parser->lexer = lexer;
2422
2423   /* Move the current source position to that of the first token in the
2424      new lexer.  */
2425   cp_lexer_set_source_position_from_token (lexer->next_token);
2426 }
2427
2428 /* Pop the top lexer off the parser stack.  This is never used for the
2429    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2430 static void
2431 cp_parser_pop_lexer (cp_parser *parser)
2432 {
2433   cp_lexer *lexer = parser->lexer;
2434   parser->lexer = lexer->next;
2435   cp_lexer_destroy (lexer);
2436
2437   /* Put the current source position back where it was before this
2438      lexer was pushed.  */
2439   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2440 }
2441
2442 /* Lexical conventions [gram.lex]  */
2443
2444 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2445    identifier.  */
2446
2447 static tree
2448 cp_parser_identifier (cp_parser* parser)
2449 {
2450   cp_token *token;
2451
2452   /* Look for the identifier.  */
2453   token = cp_parser_require (parser, CPP_NAME, "identifier");
2454   /* Return the value.  */
2455   return token ? token->value : error_mark_node;
2456 }
2457
2458 /* Parse a sequence of adjacent string constants.  Returns a
2459    TREE_STRING representing the combined, nul-terminated string
2460    constant.  If TRANSLATE is true, translate the string to the
2461    execution character set.  If WIDE_OK is true, a wide string is
2462    invalid here.
2463
2464    C++98 [lex.string] says that if a narrow string literal token is
2465    adjacent to a wide string literal token, the behavior is undefined.
2466    However, C99 6.4.5p4 says that this results in a wide string literal.
2467    We follow C99 here, for consistency with the C front end.
2468
2469    This code is largely lifted from lex_string() in c-lex.c.
2470
2471    FUTURE: ObjC++ will need to handle @-strings here.  */
2472 static tree
2473 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2474 {
2475   tree value;
2476   bool wide = false;
2477   size_t count;
2478   struct obstack str_ob;
2479   cpp_string str, istr, *strs;
2480   cp_token *tok;
2481
2482   tok = cp_lexer_peek_token (parser->lexer);
2483   if (!cp_parser_is_string_literal (tok))
2484     {
2485       cp_parser_error (parser, "expected string-literal");
2486       return error_mark_node;
2487     }
2488
2489   /* Try to avoid the overhead of creating and destroying an obstack
2490      for the common case of just one string.  */
2491   if (!cp_parser_is_string_literal
2492       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2493     {
2494       cp_lexer_consume_token (parser->lexer);
2495
2496       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2497       str.len = TREE_STRING_LENGTH (tok->value);
2498       count = 1;
2499       if (tok->type == CPP_WSTRING)
2500         wide = true;
2501
2502       strs = &str;
2503     }
2504   else
2505     {
2506       gcc_obstack_init (&str_ob);
2507       count = 0;
2508
2509       do
2510         {
2511           cp_lexer_consume_token (parser->lexer);
2512           count++;
2513           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2514           str.len = TREE_STRING_LENGTH (tok->value);
2515           if (tok->type == CPP_WSTRING)
2516             wide = true;
2517
2518           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2519
2520           tok = cp_lexer_peek_token (parser->lexer);
2521         }
2522       while (cp_parser_is_string_literal (tok));
2523
2524       strs = (cpp_string *) obstack_finish (&str_ob);
2525     }
2526
2527   if (wide && !wide_ok)
2528     {
2529       cp_parser_error (parser, "a wide string is invalid in this context");
2530       wide = false;
2531     }
2532
2533   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2534       (parse_in, strs, count, &istr, wide))
2535     {
2536       value = build_string (istr.len, (char *)istr.text);
2537       free ((void *)istr.text);
2538
2539       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2540       value = fix_string_type (value);
2541     }
2542   else
2543     /* cpp_interpret_string has issued an error.  */
2544     value = error_mark_node;
2545
2546   if (count > 1)
2547     obstack_free (&str_ob, 0);
2548
2549   return value;
2550 }
2551
2552
2553 /* Basic concepts [gram.basic]  */
2554
2555 /* Parse a translation-unit.
2556
2557    translation-unit:
2558      declaration-seq [opt]
2559
2560    Returns TRUE if all went well.  */
2561
2562 static bool
2563 cp_parser_translation_unit (cp_parser* parser)
2564 {
2565   /* The address of the first non-permanent object on the declarator
2566      obstack.  */
2567   static void *declarator_obstack_base;
2568
2569   bool success;
2570
2571   /* Create the declarator obstack, if necessary.  */
2572   if (!cp_error_declarator)
2573     {
2574       gcc_obstack_init (&declarator_obstack);
2575       /* Create the error declarator.  */
2576       cp_error_declarator = make_declarator (cdk_error);
2577       /* Create the empty parameter list.  */
2578       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2579       /* Remember where the base of the declarator obstack lies.  */
2580       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2581     }
2582
2583   while (true)
2584     {
2585       cp_parser_declaration_seq_opt (parser);
2586
2587       /* If there are no tokens left then all went well.  */
2588       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2589         {
2590           /* Get rid of the token array; we don't need it any more.  */
2591           cp_lexer_destroy (parser->lexer);
2592           parser->lexer = NULL;
2593
2594           /* This file might have been a context that's implicitly extern
2595              "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2596           if (parser->implicit_extern_c)
2597             {
2598               pop_lang_context ();
2599               parser->implicit_extern_c = false;
2600             }
2601
2602           /* Finish up.  */
2603           finish_translation_unit ();
2604
2605           success = true;
2606           break;
2607         }
2608       else
2609         {
2610           cp_parser_error (parser, "expected declaration");
2611           success = false;
2612           break;
2613         }
2614     }
2615
2616   /* Make sure the declarator obstack was fully cleaned up.  */
2617   gcc_assert (obstack_next_free (&declarator_obstack)
2618               == declarator_obstack_base);
2619
2620   /* All went well.  */
2621   return success;
2622 }
2623
2624 /* Expressions [gram.expr] */
2625
2626 /* Parse a primary-expression.
2627
2628    primary-expression:
2629      literal
2630      this
2631      ( expression )
2632      id-expression
2633
2634    GNU Extensions:
2635
2636    primary-expression:
2637      ( compound-statement )
2638      __builtin_va_arg ( assignment-expression , type-id )
2639
2640    literal:
2641      __null
2642
2643    CAST_P is true if this primary expression is the target of a cast.
2644
2645    Returns a representation of the expression.
2646
2647    *IDK indicates what kind of id-expression (if any) was present.
2648
2649    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2650    used as the operand of a pointer-to-member.  In that case,
2651    *QUALIFYING_CLASS gives the class that is used as the qualifying
2652    class in the pointer-to-member.  */
2653
2654 static tree
2655 cp_parser_primary_expression (cp_parser *parser,
2656                               bool cast_p,
2657                               cp_id_kind *idk,
2658                               tree *qualifying_class)
2659 {
2660   cp_token *token;
2661
2662   /* Assume the primary expression is not an id-expression.  */
2663   *idk = CP_ID_KIND_NONE;
2664   /* And that it cannot be used as pointer-to-member.  */
2665   *qualifying_class = NULL_TREE;
2666
2667   /* Peek at the next token.  */
2668   token = cp_lexer_peek_token (parser->lexer);
2669   switch (token->type)
2670     {
2671       /* literal:
2672            integer-literal
2673            character-literal
2674            floating-literal
2675            string-literal
2676            boolean-literal  */
2677     case CPP_CHAR:
2678     case CPP_WCHAR:
2679     case CPP_NUMBER:
2680       token = cp_lexer_consume_token (parser->lexer);
2681       /* Floating-point literals are only allowed in an integral
2682          constant expression if they are cast to an integral or
2683          enumeration type.  */
2684       if (TREE_CODE (token->value) == REAL_CST
2685           && parser->integral_constant_expression_p)
2686         {
2687           /* CAST_P will be set even in invalid code like "int(2.7 +
2688              ...)".   Therefore, we have to check that the next token
2689              is sure to end the cast.  */
2690           if (cast_p)
2691             {
2692               cp_token *next_token;
2693
2694               next_token = cp_lexer_peek_token (parser->lexer);
2695               if (/* The comma at the end of an
2696                      enumerator-definition.  */
2697                   next_token->type != CPP_COMMA
2698                   /* The curly brace at the end of an enum-specifier.  */
2699                   && next_token->type != CPP_CLOSE_BRACE
2700                   /* The end of a statement.  */
2701                   && next_token->type != CPP_SEMICOLON
2702                   /* The end of the cast-expression.  */
2703                   && next_token->type != CPP_CLOSE_PAREN
2704                   /* The end of an array bound.  */
2705                   && next_token->type != CPP_CLOSE_SQUARE)
2706                 cast_p = false;
2707             }
2708
2709           /* If we are within a cast, then the constraint that the
2710              cast is to an integral or enumeration type will be
2711              checked at that point.  If we are not within a cast, then
2712              this code is invalid.  */
2713           if (!cast_p)
2714             cp_parser_non_integral_constant_expression 
2715               (parser, "floating-point literal");
2716         }
2717       return token->value;
2718
2719     case CPP_STRING:
2720     case CPP_WSTRING:
2721       /* ??? Should wide strings be allowed when parser->translate_strings_p
2722          is false (i.e. in attributes)?  If not, we can kill the third
2723          argument to cp_parser_string_literal.  */
2724       return cp_parser_string_literal (parser,
2725                                        parser->translate_strings_p,
2726                                        true);
2727
2728     case CPP_OPEN_PAREN:
2729       {
2730         tree expr;
2731         bool saved_greater_than_is_operator_p;
2732
2733         /* Consume the `('.  */
2734         cp_lexer_consume_token (parser->lexer);
2735         /* Within a parenthesized expression, a `>' token is always
2736            the greater-than operator.  */
2737         saved_greater_than_is_operator_p
2738           = parser->greater_than_is_operator_p;
2739         parser->greater_than_is_operator_p = true;
2740         /* If we see `( { ' then we are looking at the beginning of
2741            a GNU statement-expression.  */
2742         if (cp_parser_allow_gnu_extensions_p (parser)
2743             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2744           {
2745             /* Statement-expressions are not allowed by the standard.  */
2746             if (pedantic)
2747               pedwarn ("ISO C++ forbids braced-groups within expressions");
2748
2749             /* And they're not allowed outside of a function-body; you
2750                cannot, for example, write:
2751
2752                  int i = ({ int j = 3; j + 1; });
2753
2754                at class or namespace scope.  */
2755             if (!at_function_scope_p ())
2756               error ("statement-expressions are allowed only inside functions");
2757             /* Start the statement-expression.  */
2758             expr = begin_stmt_expr ();
2759             /* Parse the compound-statement.  */
2760             cp_parser_compound_statement (parser, expr, false);
2761             /* Finish up.  */
2762             expr = finish_stmt_expr (expr, false);
2763           }
2764         else
2765           {
2766             /* Parse the parenthesized expression.  */
2767             expr = cp_parser_expression (parser, cast_p);
2768             /* Let the front end know that this expression was
2769                enclosed in parentheses. This matters in case, for
2770                example, the expression is of the form `A::B', since
2771                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2772                not.  */
2773             finish_parenthesized_expr (expr);
2774           }
2775         /* The `>' token might be the end of a template-id or
2776            template-parameter-list now.  */
2777         parser->greater_than_is_operator_p
2778           = saved_greater_than_is_operator_p;
2779         /* Consume the `)'.  */
2780         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2781           cp_parser_skip_to_end_of_statement (parser);
2782
2783         return expr;
2784       }
2785
2786     case CPP_KEYWORD:
2787       switch (token->keyword)
2788         {
2789           /* These two are the boolean literals.  */
2790         case RID_TRUE:
2791           cp_lexer_consume_token (parser->lexer);
2792           return boolean_true_node;
2793         case RID_FALSE:
2794           cp_lexer_consume_token (parser->lexer);
2795           return boolean_false_node;
2796
2797           /* The `__null' literal.  */
2798         case RID_NULL:
2799           cp_lexer_consume_token (parser->lexer);
2800           return null_node;
2801
2802           /* Recognize the `this' keyword.  */
2803         case RID_THIS:
2804           cp_lexer_consume_token (parser->lexer);
2805           if (parser->local_variables_forbidden_p)
2806             {
2807               error ("%<this%> may not be used in this context");
2808               return error_mark_node;
2809             }
2810           /* Pointers cannot appear in constant-expressions.  */
2811           if (cp_parser_non_integral_constant_expression (parser,
2812                                                           "`this'"))
2813             return error_mark_node;
2814           return finish_this_expr ();
2815
2816           /* The `operator' keyword can be the beginning of an
2817              id-expression.  */
2818         case RID_OPERATOR:
2819           goto id_expression;
2820
2821         case RID_FUNCTION_NAME:
2822         case RID_PRETTY_FUNCTION_NAME:
2823         case RID_C99_FUNCTION_NAME:
2824           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2825              __func__ are the names of variables -- but they are
2826              treated specially.  Therefore, they are handled here,
2827              rather than relying on the generic id-expression logic
2828              below.  Grammatically, these names are id-expressions.
2829
2830              Consume the token.  */
2831           token = cp_lexer_consume_token (parser->lexer);
2832           /* Look up the name.  */
2833           return finish_fname (token->value);
2834
2835         case RID_VA_ARG:
2836           {
2837             tree expression;
2838             tree type;
2839
2840             /* The `__builtin_va_arg' construct is used to handle
2841                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2842             cp_lexer_consume_token (parser->lexer);
2843             /* Look for the opening `('.  */
2844             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2845             /* Now, parse the assignment-expression.  */
2846             expression = cp_parser_assignment_expression (parser,
2847                                                           /*cast_p=*/false);
2848             /* Look for the `,'.  */
2849             cp_parser_require (parser, CPP_COMMA, "`,'");
2850             /* Parse the type-id.  */
2851             type = cp_parser_type_id (parser);
2852             /* Look for the closing `)'.  */
2853             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2854             /* Using `va_arg' in a constant-expression is not
2855                allowed.  */
2856             if (cp_parser_non_integral_constant_expression (parser,
2857                                                             "`va_arg'"))
2858               return error_mark_node;
2859             return build_x_va_arg (expression, type);
2860           }
2861
2862         case RID_OFFSETOF:
2863           return cp_parser_builtin_offsetof (parser);
2864
2865         default:
2866           cp_parser_error (parser, "expected primary-expression");
2867           return error_mark_node;
2868         }
2869
2870       /* An id-expression can start with either an identifier, a
2871          `::' as the beginning of a qualified-id, or the "operator"
2872          keyword.  */
2873     case CPP_NAME:
2874     case CPP_SCOPE:
2875     case CPP_TEMPLATE_ID:
2876     case CPP_NESTED_NAME_SPECIFIER:
2877       {
2878         tree id_expression;
2879         tree decl;
2880         const char *error_msg;
2881
2882       id_expression:
2883         /* Parse the id-expression.  */
2884         id_expression
2885           = cp_parser_id_expression (parser,
2886                                      /*template_keyword_p=*/false,
2887                                      /*check_dependency_p=*/true,
2888                                      /*template_p=*/NULL,
2889                                      /*declarator_p=*/false);
2890         if (id_expression == error_mark_node)
2891           return error_mark_node;
2892         /* If we have a template-id, then no further lookup is
2893            required.  If the template-id was for a template-class, we
2894            will sometimes have a TYPE_DECL at this point.  */
2895         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2896             || TREE_CODE (id_expression) == TYPE_DECL)
2897           decl = id_expression;
2898         /* Look up the name.  */
2899         else
2900           {
2901             bool ambiguous_p;
2902
2903             decl = cp_parser_lookup_name (parser, id_expression,
2904                                           none_type,
2905                                           /*is_template=*/false,
2906                                           /*is_namespace=*/false,
2907                                           /*check_dependency=*/true,
2908                                           &ambiguous_p);
2909             /* If the lookup was ambiguous, an error will already have
2910                been issued.  */
2911             if (ambiguous_p)
2912               return error_mark_node;
2913             /* If name lookup gives us a SCOPE_REF, then the
2914                qualifying scope was dependent.  Just propagate the
2915                name.  */
2916             if (TREE_CODE (decl) == SCOPE_REF)
2917               {
2918                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2919                   *qualifying_class = TREE_OPERAND (decl, 0);
2920                 return decl;
2921               }
2922             /* Check to see if DECL is a local variable in a context
2923                where that is forbidden.  */
2924             if (parser->local_variables_forbidden_p
2925                 && local_variable_p (decl))
2926               {
2927                 /* It might be that we only found DECL because we are
2928                    trying to be generous with pre-ISO scoping rules.
2929                    For example, consider:
2930
2931                      int i;
2932                      void g() {
2933                        for (int i = 0; i < 10; ++i) {}
2934                        extern void f(int j = i);
2935                      }
2936
2937                    Here, name look up will originally find the out
2938                    of scope `i'.  We need to issue a warning message,
2939                    but then use the global `i'.  */
2940                 decl = check_for_out_of_scope_variable (decl);
2941                 if (local_variable_p (decl))
2942                   {
2943                     error ("local variable %qD may not appear in this context",
2944                            decl);
2945                     return error_mark_node;
2946                   }
2947               }
2948           }
2949
2950         decl = finish_id_expression (id_expression, decl, parser->scope,
2951                                      idk, qualifying_class,
2952                                      parser->integral_constant_expression_p,
2953                                      parser->allow_non_integral_constant_expression_p,
2954                                      &parser->non_integral_constant_expression_p,
2955                                      &error_msg);
2956         if (error_msg)
2957           cp_parser_error (parser, error_msg);
2958         return decl;
2959       }
2960
2961       /* Anything else is an error.  */
2962     default:
2963       cp_parser_error (parser, "expected primary-expression");
2964       return error_mark_node;
2965     }
2966 }
2967
2968 /* Parse an id-expression.
2969
2970    id-expression:
2971      unqualified-id
2972      qualified-id
2973
2974    qualified-id:
2975      :: [opt] nested-name-specifier template [opt] unqualified-id
2976      :: identifier
2977      :: operator-function-id
2978      :: template-id
2979
2980    Return a representation of the unqualified portion of the
2981    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2982    a `::' or nested-name-specifier.
2983
2984    Often, if the id-expression was a qualified-id, the caller will
2985    want to make a SCOPE_REF to represent the qualified-id.  This
2986    function does not do this in order to avoid wastefully creating
2987    SCOPE_REFs when they are not required.
2988
2989    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2990    `template' keyword.
2991
2992    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2993    uninstantiated templates.
2994
2995    If *TEMPLATE_P is non-NULL, it is set to true iff the
2996    `template' keyword is used to explicitly indicate that the entity
2997    named is a template.
2998
2999    If DECLARATOR_P is true, the id-expression is appearing as part of
3000    a declarator, rather than as part of an expression.  */
3001
3002 static tree
3003 cp_parser_id_expression (cp_parser *parser,
3004                          bool template_keyword_p,
3005                          bool check_dependency_p,
3006                          bool *template_p,
3007                          bool declarator_p)
3008 {
3009   bool global_scope_p;
3010   bool nested_name_specifier_p;
3011
3012   /* Assume the `template' keyword was not used.  */
3013   if (template_p)
3014     *template_p = false;
3015
3016   /* Look for the optional `::' operator.  */
3017   global_scope_p
3018     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3019        != NULL_TREE);
3020   /* Look for the optional nested-name-specifier.  */
3021   nested_name_specifier_p
3022     = (cp_parser_nested_name_specifier_opt (parser,
3023                                             /*typename_keyword_p=*/false,
3024                                             check_dependency_p,
3025                                             /*type_p=*/false,
3026                                             declarator_p)
3027        != NULL_TREE);
3028   /* If there is a nested-name-specifier, then we are looking at
3029      the first qualified-id production.  */
3030   if (nested_name_specifier_p)
3031     {
3032       tree saved_scope;
3033       tree saved_object_scope;
3034       tree saved_qualifying_scope;
3035       tree unqualified_id;
3036       bool is_template;
3037
3038       /* See if the next token is the `template' keyword.  */
3039       if (!template_p)
3040         template_p = &is_template;
3041       *template_p = cp_parser_optional_template_keyword (parser);
3042       /* Name lookup we do during the processing of the
3043          unqualified-id might obliterate SCOPE.  */
3044       saved_scope = parser->scope;
3045       saved_object_scope = parser->object_scope;
3046       saved_qualifying_scope = parser->qualifying_scope;
3047       /* Process the final unqualified-id.  */
3048       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3049                                                  check_dependency_p,
3050                                                  declarator_p);
3051       /* Restore the SAVED_SCOPE for our caller.  */
3052       parser->scope = saved_scope;
3053       parser->object_scope = saved_object_scope;
3054       parser->qualifying_scope = saved_qualifying_scope;
3055
3056       return unqualified_id;
3057     }
3058   /* Otherwise, if we are in global scope, then we are looking at one
3059      of the other qualified-id productions.  */
3060   else if (global_scope_p)
3061     {
3062       cp_token *token;
3063       tree id;
3064
3065       /* Peek at the next token.  */
3066       token = cp_lexer_peek_token (parser->lexer);
3067
3068       /* If it's an identifier, and the next token is not a "<", then
3069          we can avoid the template-id case.  This is an optimization
3070          for this common case.  */
3071       if (token->type == CPP_NAME
3072           && !cp_parser_nth_token_starts_template_argument_list_p
3073                (parser, 2))
3074         return cp_parser_identifier (parser);
3075
3076       cp_parser_parse_tentatively (parser);
3077       /* Try a template-id.  */
3078       id = cp_parser_template_id (parser,
3079                                   /*template_keyword_p=*/false,
3080                                   /*check_dependency_p=*/true,
3081                                   declarator_p);
3082       /* If that worked, we're done.  */
3083       if (cp_parser_parse_definitely (parser))
3084         return id;
3085
3086       /* Peek at the next token.  (Changes in the token buffer may
3087          have invalidated the pointer obtained above.)  */
3088       token = cp_lexer_peek_token (parser->lexer);
3089
3090       switch (token->type)
3091         {
3092         case CPP_NAME:
3093           return cp_parser_identifier (parser);
3094
3095         case CPP_KEYWORD:
3096           if (token->keyword == RID_OPERATOR)
3097             return cp_parser_operator_function_id (parser);
3098           /* Fall through.  */
3099
3100         default:
3101           cp_parser_error (parser, "expected id-expression");
3102           return error_mark_node;
3103         }
3104     }
3105   else
3106     return cp_parser_unqualified_id (parser, template_keyword_p,
3107                                      /*check_dependency_p=*/true,
3108                                      declarator_p);
3109 }
3110
3111 /* Parse an unqualified-id.
3112
3113    unqualified-id:
3114      identifier
3115      operator-function-id
3116      conversion-function-id
3117      ~ class-name
3118      template-id
3119
3120    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3121    keyword, in a construct like `A::template ...'.
3122
3123    Returns a representation of unqualified-id.  For the `identifier'
3124    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3125    production a BIT_NOT_EXPR is returned; the operand of the
3126    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3127    other productions, see the documentation accompanying the
3128    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3129    names are looked up in uninstantiated templates.  If DECLARATOR_P
3130    is true, the unqualified-id is appearing as part of a declarator,
3131    rather than as part of an expression.  */
3132
3133 static tree
3134 cp_parser_unqualified_id (cp_parser* parser,
3135                           bool template_keyword_p,
3136                           bool check_dependency_p,
3137                           bool declarator_p)
3138 {
3139   cp_token *token;
3140
3141   /* Peek at the next token.  */
3142   token = cp_lexer_peek_token (parser->lexer);
3143
3144   switch (token->type)
3145     {
3146     case CPP_NAME:
3147       {
3148         tree id;
3149
3150         /* We don't know yet whether or not this will be a
3151            template-id.  */
3152         cp_parser_parse_tentatively (parser);
3153         /* Try a template-id.  */
3154         id = cp_parser_template_id (parser, template_keyword_p,
3155                                     check_dependency_p,
3156                                     declarator_p);
3157         /* If it worked, we're done.  */
3158         if (cp_parser_parse_definitely (parser))
3159           return id;
3160         /* Otherwise, it's an ordinary identifier.  */
3161         return cp_parser_identifier (parser);
3162       }
3163
3164     case CPP_TEMPLATE_ID:
3165       return cp_parser_template_id (parser, template_keyword_p,
3166                                     check_dependency_p,
3167                                     declarator_p);
3168
3169     case CPP_COMPL:
3170       {
3171         tree type_decl;
3172         tree qualifying_scope;
3173         tree object_scope;
3174         tree scope;
3175
3176         /* Consume the `~' token.  */
3177         cp_lexer_consume_token (parser->lexer);
3178         /* Parse the class-name.  The standard, as written, seems to
3179            say that:
3180
3181              template <typename T> struct S { ~S (); };
3182              template <typename T> S<T>::~S() {}
3183
3184            is invalid, since `~' must be followed by a class-name, but
3185            `S<T>' is dependent, and so not known to be a class.
3186            That's not right; we need to look in uninstantiated
3187            templates.  A further complication arises from:
3188
3189              template <typename T> void f(T t) {
3190                t.T::~T();
3191              }
3192
3193            Here, it is not possible to look up `T' in the scope of `T'
3194            itself.  We must look in both the current scope, and the
3195            scope of the containing complete expression.
3196
3197            Yet another issue is:
3198
3199              struct S {
3200                int S;
3201                ~S();
3202              };
3203
3204              S::~S() {}
3205
3206            The standard does not seem to say that the `S' in `~S'
3207            should refer to the type `S' and not the data member
3208            `S::S'.  */
3209
3210         /* DR 244 says that we look up the name after the "~" in the
3211            same scope as we looked up the qualifying name.  That idea
3212            isn't fully worked out; it's more complicated than that.  */
3213         scope = parser->scope;
3214         object_scope = parser->object_scope;
3215         qualifying_scope = parser->qualifying_scope;
3216
3217         /* If the name is of the form "X::~X" it's OK.  */
3218         if (scope && TYPE_P (scope)
3219             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3220             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3221                 == CPP_OPEN_PAREN)
3222             && (cp_lexer_peek_token (parser->lexer)->value
3223                 == TYPE_IDENTIFIER (scope)))
3224           {
3225             cp_lexer_consume_token (parser->lexer);
3226             return build_nt (BIT_NOT_EXPR, scope);
3227           }
3228
3229         /* If there was an explicit qualification (S::~T), first look
3230            in the scope given by the qualification (i.e., S).  */
3231         if (scope)
3232           {
3233             cp_parser_parse_tentatively (parser);
3234             type_decl = cp_parser_class_name (parser,
3235                                               /*typename_keyword_p=*/false,
3236                                               /*template_keyword_p=*/false,
3237                                               none_type,
3238                                               /*check_dependency=*/false,
3239                                               /*class_head_p=*/false,
3240                                               declarator_p);
3241             if (cp_parser_parse_definitely (parser))
3242               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3243           }
3244         /* In "N::S::~S", look in "N" as well.  */
3245         if (scope && qualifying_scope)
3246           {
3247             cp_parser_parse_tentatively (parser);
3248             parser->scope = qualifying_scope;
3249             parser->object_scope = NULL_TREE;
3250             parser->qualifying_scope = NULL_TREE;
3251             type_decl
3252               = cp_parser_class_name (parser,
3253                                       /*typename_keyword_p=*/false,
3254                                       /*template_keyword_p=*/false,
3255                                       none_type,
3256                                       /*check_dependency=*/false,
3257                                       /*class_head_p=*/false,
3258                                       declarator_p);
3259             if (cp_parser_parse_definitely (parser))
3260               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3261           }
3262         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3263         else if (object_scope)
3264           {
3265             cp_parser_parse_tentatively (parser);
3266             parser->scope = object_scope;
3267             parser->object_scope = NULL_TREE;
3268             parser->qualifying_scope = NULL_TREE;
3269             type_decl
3270               = cp_parser_class_name (parser,
3271                                       /*typename_keyword_p=*/false,
3272                                       /*template_keyword_p=*/false,
3273                                       none_type,
3274                                       /*check_dependency=*/false,
3275                                       /*class_head_p=*/false,
3276                                       declarator_p);
3277             if (cp_parser_parse_definitely (parser))
3278               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3279           }
3280         /* Look in the surrounding context.  */
3281         parser->scope = NULL_TREE;
3282         parser->object_scope = NULL_TREE;
3283         parser->qualifying_scope = NULL_TREE;
3284         type_decl
3285           = cp_parser_class_name (parser,
3286                                   /*typename_keyword_p=*/false,
3287                                   /*template_keyword_p=*/false,
3288                                   none_type,
3289                                   /*check_dependency=*/false,
3290                                   /*class_head_p=*/false,
3291                                   declarator_p);
3292         /* If an error occurred, assume that the name of the
3293            destructor is the same as the name of the qualifying
3294            class.  That allows us to keep parsing after running
3295            into ill-formed destructor names.  */
3296         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3297           return build_nt (BIT_NOT_EXPR, scope);
3298         else if (type_decl == error_mark_node)
3299           return error_mark_node;
3300
3301         /* [class.dtor]
3302
3303            A typedef-name that names a class shall not be used as the
3304            identifier in the declarator for a destructor declaration.  */
3305         if (declarator_p
3306             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3307             && !DECL_SELF_REFERENCE_P (type_decl)
3308             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3309           error ("typedef-name %qD used as destructor declarator",
3310                  type_decl);
3311
3312         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3313       }
3314
3315     case CPP_KEYWORD:
3316       if (token->keyword == RID_OPERATOR)
3317         {
3318           tree id;
3319
3320           /* This could be a template-id, so we try that first.  */
3321           cp_parser_parse_tentatively (parser);
3322           /* Try a template-id.  */
3323           id = cp_parser_template_id (parser, template_keyword_p,
3324                                       /*check_dependency_p=*/true,
3325                                       declarator_p);
3326           /* If that worked, we're done.  */
3327           if (cp_parser_parse_definitely (parser))
3328             return id;
3329           /* We still don't know whether we're looking at an
3330              operator-function-id or a conversion-function-id.  */
3331           cp_parser_parse_tentatively (parser);
3332           /* Try an operator-function-id.  */
3333           id = cp_parser_operator_function_id (parser);
3334           /* If that didn't work, try a conversion-function-id.  */
3335           if (!cp_parser_parse_definitely (parser))
3336             id = cp_parser_conversion_function_id (parser);
3337
3338           return id;
3339         }
3340       /* Fall through.  */
3341
3342     default:
3343       cp_parser_error (parser, "expected unqualified-id");
3344       return error_mark_node;
3345     }
3346 }
3347
3348 /* Parse an (optional) nested-name-specifier.
3349
3350    nested-name-specifier:
3351      class-or-namespace-name :: nested-name-specifier [opt]
3352      class-or-namespace-name :: template nested-name-specifier [opt]
3353
3354    PARSER->SCOPE should be set appropriately before this function is
3355    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3356    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3357    in name lookups.
3358
3359    Sets PARSER->SCOPE to the class (TYPE) or namespace
3360    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3361    it unchanged if there is no nested-name-specifier.  Returns the new
3362    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3363
3364    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3365    part of a declaration and/or decl-specifier.  */
3366
3367 static tree
3368 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3369                                      bool typename_keyword_p,
3370                                      bool check_dependency_p,
3371                                      bool type_p,
3372                                      bool is_declaration)
3373 {
3374   bool success = false;
3375   tree access_check = NULL_TREE;
3376   cp_token_position start = 0;
3377   cp_token *token;
3378
3379   /* If the next token corresponds to a nested name specifier, there
3380      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3381      false, it may have been true before, in which case something
3382      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3383      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3384      CHECK_DEPENDENCY_P is false, we have to fall through into the
3385      main loop.  */
3386   if (check_dependency_p
3387       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3388     {
3389       cp_parser_pre_parsed_nested_name_specifier (parser);
3390       return parser->scope;
3391     }
3392
3393   /* Remember where the nested-name-specifier starts.  */
3394   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3395     start = cp_lexer_token_position (parser->lexer, false);
3396
3397   push_deferring_access_checks (dk_deferred);
3398
3399   while (true)
3400     {
3401       tree new_scope;
3402       tree old_scope;
3403       tree saved_qualifying_scope;
3404       bool template_keyword_p;
3405
3406       /* Spot cases that cannot be the beginning of a
3407          nested-name-specifier.  */
3408       token = cp_lexer_peek_token (parser->lexer);
3409
3410       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3411          the already parsed nested-name-specifier.  */
3412       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3413         {
3414           /* Grab the nested-name-specifier and continue the loop.  */
3415           cp_parser_pre_parsed_nested_name_specifier (parser);
3416           success = true;
3417           continue;
3418         }
3419
3420       /* Spot cases that cannot be the beginning of a
3421          nested-name-specifier.  On the second and subsequent times
3422          through the loop, we look for the `template' keyword.  */
3423       if (success && token->keyword == RID_TEMPLATE)
3424         ;
3425       /* A template-id can start a nested-name-specifier.  */
3426       else if (token->type == CPP_TEMPLATE_ID)
3427         ;
3428       else
3429         {
3430           /* If the next token is not an identifier, then it is
3431              definitely not a class-or-namespace-name.  */
3432           if (token->type != CPP_NAME)
3433             break;
3434           /* If the following token is neither a `<' (to begin a
3435              template-id), nor a `::', then we are not looking at a
3436              nested-name-specifier.  */
3437           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3438           if (token->type != CPP_SCOPE
3439               && !cp_parser_nth_token_starts_template_argument_list_p
3440                   (parser, 2))
3441             break;
3442         }
3443
3444       /* The nested-name-specifier is optional, so we parse
3445          tentatively.  */
3446       cp_parser_parse_tentatively (parser);
3447
3448       /* Look for the optional `template' keyword, if this isn't the
3449          first time through the loop.  */
3450       if (success)
3451         template_keyword_p = cp_parser_optional_template_keyword (parser);
3452       else
3453         template_keyword_p = false;
3454
3455       /* Save the old scope since the name lookup we are about to do
3456          might destroy it.  */
3457       old_scope = parser->scope;
3458       saved_qualifying_scope = parser->qualifying_scope;
3459       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3460          look up names in "X<T>::I" in order to determine that "Y" is
3461          a template.  So, if we have a typename at this point, we make
3462          an effort to look through it.  */
3463       if (is_declaration 
3464           && !typename_keyword_p
3465           && parser->scope 
3466           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3467         parser->scope = resolve_typename_type (parser->scope, 
3468                                                /*only_current_p=*/false);
3469       /* Parse the qualifying entity.  */
3470       new_scope
3471         = cp_parser_class_or_namespace_name (parser,
3472                                              typename_keyword_p,
3473                                              template_keyword_p,
3474                                              check_dependency_p,
3475                                              type_p,
3476                                              is_declaration);
3477       /* Look for the `::' token.  */
3478       cp_parser_require (parser, CPP_SCOPE, "`::'");
3479
3480       /* If we found what we wanted, we keep going; otherwise, we're
3481          done.  */
3482       if (!cp_parser_parse_definitely (parser))
3483         {
3484           bool error_p = false;
3485
3486           /* Restore the OLD_SCOPE since it was valid before the
3487              failed attempt at finding the last
3488              class-or-namespace-name.  */
3489           parser->scope = old_scope;
3490           parser->qualifying_scope = saved_qualifying_scope;
3491           /* If the next token is an identifier, and the one after
3492              that is a `::', then any valid interpretation would have
3493              found a class-or-namespace-name.  */
3494           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3495                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3496                      == CPP_SCOPE)
3497                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3498                      != CPP_COMPL))
3499             {
3500               token = cp_lexer_consume_token (parser->lexer);
3501               if (!error_p)
3502                 {
3503                   tree decl;
3504
3505                   decl = cp_parser_lookup_name_simple (parser, token->value);
3506                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3507                     error ("%qD used without template parameters", decl);
3508                   else
3509                     cp_parser_name_lookup_error
3510                       (parser, token->value, decl,
3511                        "is not a class or namespace");
3512                   parser->scope = NULL_TREE;
3513                   error_p = true;
3514                   /* Treat this as a successful nested-name-specifier
3515                      due to:
3516
3517                      [basic.lookup.qual]
3518
3519                      If the name found is not a class-name (clause
3520                      _class_) or namespace-name (_namespace.def_), the
3521                      program is ill-formed.  */
3522                   success = true;
3523                 }
3524               cp_lexer_consume_token (parser->lexer);
3525             }
3526           break;
3527         }
3528
3529       /* We've found one valid nested-name-specifier.  */
3530       success = true;
3531       /* Make sure we look in the right scope the next time through
3532          the loop.  */
3533       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3534                        ? TREE_TYPE (new_scope)
3535                        : new_scope);
3536       /* If it is a class scope, try to complete it; we are about to
3537          be looking up names inside the class.  */
3538       if (TYPE_P (parser->scope)
3539           /* Since checking types for dependency can be expensive,
3540              avoid doing it if the type is already complete.  */
3541           && !COMPLETE_TYPE_P (parser->scope)
3542           /* Do not try to complete dependent types.  */
3543           && !dependent_type_p (parser->scope))
3544         complete_type (parser->scope);
3545     }
3546
3547   /* Retrieve any deferred checks.  Do not pop this access checks yet
3548      so the memory will not be reclaimed during token replacing below.  */
3549   access_check = get_deferred_access_checks ();
3550
3551   /* If parsing tentatively, replace the sequence of tokens that makes
3552      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3553      token.  That way, should we re-parse the token stream, we will
3554      not have to repeat the effort required to do the parse, nor will
3555      we issue duplicate error messages.  */
3556   if (success && start)
3557     {
3558       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3559       
3560       /* Reset the contents of the START token.  */
3561       token->type = CPP_NESTED_NAME_SPECIFIER;
3562       token->value = build_tree_list (access_check, parser->scope);
3563       TREE_TYPE (token->value) = parser->qualifying_scope;
3564       token->keyword = RID_MAX;
3565       
3566       /* Purge all subsequent tokens.  */
3567       cp_lexer_purge_tokens_after (parser->lexer, start);
3568     }
3569
3570   pop_deferring_access_checks ();
3571   return success ? parser->scope : NULL_TREE;
3572 }
3573
3574 /* Parse a nested-name-specifier.  See
3575    cp_parser_nested_name_specifier_opt for details.  This function
3576    behaves identically, except that it will an issue an error if no
3577    nested-name-specifier is present, and it will return
3578    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3579    is present.  */
3580
3581 static tree
3582 cp_parser_nested_name_specifier (cp_parser *parser,
3583                                  bool typename_keyword_p,
3584                                  bool check_dependency_p,
3585                                  bool type_p,
3586                                  bool is_declaration)
3587 {
3588   tree scope;
3589
3590   /* Look for the nested-name-specifier.  */
3591   scope = cp_parser_nested_name_specifier_opt (parser,
3592                                                typename_keyword_p,
3593                                                check_dependency_p,
3594                                                type_p,
3595                                                is_declaration);
3596   /* If it was not present, issue an error message.  */
3597   if (!scope)
3598     {
3599       cp_parser_error (parser, "expected nested-name-specifier");
3600       parser->scope = NULL_TREE;
3601       return error_mark_node;
3602     }
3603
3604   return scope;
3605 }
3606
3607 /* Parse a class-or-namespace-name.
3608
3609    class-or-namespace-name:
3610      class-name
3611      namespace-name
3612
3613    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3614    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3615    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3616    TYPE_P is TRUE iff the next name should be taken as a class-name,
3617    even the same name is declared to be another entity in the same
3618    scope.
3619
3620    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3621    specified by the class-or-namespace-name.  If neither is found the
3622    ERROR_MARK_NODE is returned.  */
3623
3624 static tree
3625 cp_parser_class_or_namespace_name (cp_parser *parser,
3626                                    bool typename_keyword_p,
3627                                    bool template_keyword_p,
3628                                    bool check_dependency_p,
3629                                    bool type_p,
3630                                    bool is_declaration)
3631 {
3632   tree saved_scope;
3633   tree saved_qualifying_scope;
3634   tree saved_object_scope;
3635   tree scope;
3636   bool only_class_p;
3637
3638   /* Before we try to parse the class-name, we must save away the
3639      current PARSER->SCOPE since cp_parser_class_name will destroy
3640      it.  */
3641   saved_scope = parser->scope;
3642   saved_qualifying_scope = parser->qualifying_scope;
3643   saved_object_scope = parser->object_scope;
3644   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3645      there is no need to look for a namespace-name.  */
3646   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3647   if (!only_class_p)
3648     cp_parser_parse_tentatively (parser);
3649   scope = cp_parser_class_name (parser,
3650                                 typename_keyword_p,
3651                                 template_keyword_p,
3652                                 type_p ? class_type : none_type,
3653                                 check_dependency_p,
3654                                 /*class_head_p=*/false,
3655                                 is_declaration);
3656   /* If that didn't work, try for a namespace-name.  */
3657   if (!only_class_p && !cp_parser_parse_definitely (parser))
3658     {
3659       /* Restore the saved scope.  */
3660       parser->scope = saved_scope;
3661       parser->qualifying_scope = saved_qualifying_scope;
3662       parser->object_scope = saved_object_scope;
3663       /* If we are not looking at an identifier followed by the scope
3664          resolution operator, then this is not part of a
3665          nested-name-specifier.  (Note that this function is only used
3666          to parse the components of a nested-name-specifier.)  */
3667       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3668           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3669         return error_mark_node;
3670       scope = cp_parser_namespace_name (parser);
3671     }
3672
3673   return scope;
3674 }
3675
3676 /* Parse a postfix-expression.
3677
3678    postfix-expression:
3679      primary-expression
3680      postfix-expression [ expression ]
3681      postfix-expression ( expression-list [opt] )
3682      simple-type-specifier ( expression-list [opt] )
3683      typename :: [opt] nested-name-specifier identifier
3684        ( expression-list [opt] )
3685      typename :: [opt] nested-name-specifier template [opt] template-id
3686        ( expression-list [opt] )
3687      postfix-expression . template [opt] id-expression
3688      postfix-expression -> template [opt] id-expression
3689      postfix-expression . pseudo-destructor-name
3690      postfix-expression -> pseudo-destructor-name
3691      postfix-expression ++
3692      postfix-expression --
3693      dynamic_cast < type-id > ( expression )
3694      static_cast < type-id > ( expression )
3695      reinterpret_cast < type-id > ( expression )
3696      const_cast < type-id > ( expression )
3697      typeid ( expression )
3698      typeid ( type-id )
3699
3700    GNU Extension:
3701
3702    postfix-expression:
3703      ( type-id ) { initializer-list , [opt] }
3704
3705    This extension is a GNU version of the C99 compound-literal
3706    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3707    but they are essentially the same concept.)
3708
3709    If ADDRESS_P is true, the postfix expression is the operand of the
3710    `&' operator.  CAST_P is true if this expression is the target of a
3711    cast. 
3712
3713    Returns a representation of the expression.  */
3714
3715 static tree
3716 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3717 {
3718   cp_token *token;
3719   enum rid keyword;
3720   cp_id_kind idk = CP_ID_KIND_NONE;
3721   tree postfix_expression = NULL_TREE;
3722   /* Non-NULL only if the current postfix-expression can be used to
3723      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3724      class used to qualify the member.  */
3725   tree qualifying_class = NULL_TREE;
3726
3727   /* Peek at the next token.  */
3728   token = cp_lexer_peek_token (parser->lexer);
3729   /* Some of the productions are determined by keywords.  */
3730   keyword = token->keyword;
3731   switch (keyword)
3732     {
3733     case RID_DYNCAST:
3734     case RID_STATCAST:
3735     case RID_REINTCAST:
3736     case RID_CONSTCAST:
3737       {
3738         tree type;
3739         tree expression;
3740         const char *saved_message;
3741
3742         /* All of these can be handled in the same way from the point
3743            of view of parsing.  Begin by consuming the token
3744            identifying the cast.  */
3745         cp_lexer_consume_token (parser->lexer);
3746
3747         /* New types cannot be defined in the cast.  */
3748         saved_message = parser->type_definition_forbidden_message;
3749         parser->type_definition_forbidden_message
3750           = "types may not be defined in casts";
3751
3752         /* Look for the opening `<'.  */
3753         cp_parser_require (parser, CPP_LESS, "`<'");
3754         /* Parse the type to which we are casting.  */
3755         type = cp_parser_type_id (parser);
3756         /* Look for the closing `>'.  */
3757         cp_parser_require (parser, CPP_GREATER, "`>'");
3758         /* Restore the old message.  */
3759         parser->type_definition_forbidden_message = saved_message;
3760
3761         /* And the expression which is being cast.  */
3762         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3763         expression = cp_parser_expression (parser, /*cast_p=*/true);
3764         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3765
3766         /* Only type conversions to integral or enumeration types
3767            can be used in constant-expressions.  */
3768         if (parser->integral_constant_expression_p
3769             && !dependent_type_p (type)
3770             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3771             && (cp_parser_non_integral_constant_expression
3772                 (parser,
3773                  "a cast to a type other than an integral or "
3774                  "enumeration type")))
3775           return error_mark_node;
3776
3777         switch (keyword)
3778           {
3779           case RID_DYNCAST:
3780             postfix_expression
3781               = build_dynamic_cast (type, expression);
3782             break;
3783           case RID_STATCAST:
3784             postfix_expression
3785               = build_static_cast (type, expression);
3786             break;
3787           case RID_REINTCAST:
3788             postfix_expression
3789               = build_reinterpret_cast (type, expression);
3790             break;
3791           case RID_CONSTCAST:
3792             postfix_expression
3793               = build_const_cast (type, expression);
3794             break;
3795           default:
3796             gcc_unreachable ();
3797           }
3798       }
3799       break;
3800
3801     case RID_TYPEID:
3802       {
3803         tree type;
3804         const char *saved_message;
3805         bool saved_in_type_id_in_expr_p;
3806
3807         /* Consume the `typeid' token.  */
3808         cp_lexer_consume_token (parser->lexer);
3809         /* Look for the `(' token.  */
3810         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3811         /* Types cannot be defined in a `typeid' expression.  */
3812         saved_message = parser->type_definition_forbidden_message;
3813         parser->type_definition_forbidden_message
3814           = "types may not be defined in a `typeid\' expression";
3815         /* We can't be sure yet whether we're looking at a type-id or an
3816            expression.  */
3817         cp_parser_parse_tentatively (parser);
3818         /* Try a type-id first.  */
3819         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3820         parser->in_type_id_in_expr_p = true;
3821         type = cp_parser_type_id (parser);
3822         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3823         /* Look for the `)' token.  Otherwise, we can't be sure that
3824            we're not looking at an expression: consider `typeid (int
3825            (3))', for example.  */
3826         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3827         /* If all went well, simply lookup the type-id.  */
3828         if (cp_parser_parse_definitely (parser))
3829           postfix_expression = get_typeid (type);
3830         /* Otherwise, fall back to the expression variant.  */
3831         else
3832           {
3833             tree expression;
3834
3835             /* Look for an expression.  */
3836             expression = cp_parser_expression (parser, /*cast_p=*/false);
3837             /* Compute its typeid.  */
3838             postfix_expression = build_typeid (expression);
3839             /* Look for the `)' token.  */
3840             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3841           }
3842         /* `typeid' may not appear in an integral constant expression.  */
3843         if (cp_parser_non_integral_constant_expression(parser,
3844                                                        "`typeid' operator"))
3845           return error_mark_node;
3846         /* Restore the saved message.  */
3847         parser->type_definition_forbidden_message = saved_message;
3848       }
3849       break;
3850
3851     case RID_TYPENAME:
3852       {
3853         bool template_p = false;
3854         tree id;
3855         tree type;
3856
3857         /* Consume the `typename' token.  */
3858         cp_lexer_consume_token (parser->lexer);
3859         /* Look for the optional `::' operator.  */
3860         cp_parser_global_scope_opt (parser,
3861                                     /*current_scope_valid_p=*/false);
3862         /* Look for the nested-name-specifier.  */
3863         cp_parser_nested_name_specifier (parser,
3864                                          /*typename_keyword_p=*/true,
3865                                          /*check_dependency_p=*/true,
3866                                          /*type_p=*/true,
3867                                          /*is_declaration=*/true);
3868         /* Look for the optional `template' keyword.  */
3869         template_p = cp_parser_optional_template_keyword (parser);
3870         /* We don't know whether we're looking at a template-id or an
3871            identifier.  */
3872         cp_parser_parse_tentatively (parser);
3873         /* Try a template-id.  */
3874         id = cp_parser_template_id (parser, template_p,
3875                                     /*check_dependency_p=*/true,
3876                                     /*is_declaration=*/true);
3877         /* If that didn't work, try an identifier.  */
3878         if (!cp_parser_parse_definitely (parser))
3879           id = cp_parser_identifier (parser);
3880         /* If we look up a template-id in a non-dependent qualifying
3881            scope, there's no need to create a dependent type.  */
3882         if (TREE_CODE (id) == TYPE_DECL
3883             && !dependent_type_p (parser->scope))
3884           type = TREE_TYPE (id);
3885         /* Create a TYPENAME_TYPE to represent the type to which the
3886            functional cast is being performed.  */
3887         else
3888           type = make_typename_type (parser->scope, id,
3889                                      typename_type,
3890                                      /*complain=*/1);
3891
3892         postfix_expression = cp_parser_functional_cast (parser, type);
3893       }
3894       break;
3895
3896     default:
3897       {
3898         tree type;
3899
3900         /* If the next thing is a simple-type-specifier, we may be
3901            looking at a functional cast.  We could also be looking at
3902            an id-expression.  So, we try the functional cast, and if
3903            that doesn't work we fall back to the primary-expression.  */
3904         cp_parser_parse_tentatively (parser);
3905         /* Look for the simple-type-specifier.  */
3906         type = cp_parser_simple_type_specifier (parser,
3907                                                 /*decl_specs=*/NULL,
3908                                                 CP_PARSER_FLAGS_NONE);
3909         /* Parse the cast itself.  */
3910         if (!cp_parser_error_occurred (parser))
3911           postfix_expression
3912             = cp_parser_functional_cast (parser, type);
3913         /* If that worked, we're done.  */
3914         if (cp_parser_parse_definitely (parser))
3915           break;
3916
3917         /* If the functional-cast didn't work out, try a
3918            compound-literal.  */
3919         if (cp_parser_allow_gnu_extensions_p (parser)
3920             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3921           {
3922             tree initializer_list = NULL_TREE;
3923             bool saved_in_type_id_in_expr_p;
3924
3925             cp_parser_parse_tentatively (parser);
3926             /* Consume the `('.  */
3927             cp_lexer_consume_token (parser->lexer);
3928             /* Parse the type.  */
3929             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3930             parser->in_type_id_in_expr_p = true;
3931             type = cp_parser_type_id (parser);
3932             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3933             /* Look for the `)'.  */
3934             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3935             /* Look for the `{'.  */
3936             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3937             /* If things aren't going well, there's no need to
3938                keep going.  */
3939             if (!cp_parser_error_occurred (parser))
3940               {
3941                 bool non_constant_p;
3942                 /* Parse the initializer-list.  */
3943                 initializer_list
3944                   = cp_parser_initializer_list (parser, &non_constant_p);
3945                 /* Allow a trailing `,'.  */
3946                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3947                   cp_lexer_consume_token (parser->lexer);
3948                 /* Look for the final `}'.  */
3949                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3950               }
3951             /* If that worked, we're definitely looking at a
3952                compound-literal expression.  */
3953             if (cp_parser_parse_definitely (parser))
3954               {
3955                 /* Warn the user that a compound literal is not
3956                    allowed in standard C++.  */
3957                 if (pedantic)
3958                   pedwarn ("ISO C++ forbids compound-literals");
3959                 /* Form the representation of the compound-literal.  */
3960                 postfix_expression
3961                   = finish_compound_literal (type, initializer_list);
3962                 break;
3963               }
3964           }
3965
3966         /* It must be a primary-expression.  */
3967         postfix_expression = cp_parser_primary_expression (parser,
3968                                                            cast_p,
3969                                                            &idk,
3970                                                            &qualifying_class);
3971       }
3972       break;
3973     }
3974
3975   /* If we were avoiding committing to the processing of a
3976      qualified-id until we knew whether or not we had a
3977      pointer-to-member, we now know.  */
3978   if (qualifying_class)
3979     {
3980       bool done;
3981
3982       /* Peek at the next token.  */
3983       token = cp_lexer_peek_token (parser->lexer);
3984       done = (token->type != CPP_OPEN_SQUARE
3985               && token->type != CPP_OPEN_PAREN
3986               && token->type != CPP_DOT
3987               && token->type != CPP_DEREF
3988               && token->type != CPP_PLUS_PLUS
3989               && token->type != CPP_MINUS_MINUS);
3990
3991       postfix_expression = finish_qualified_id_expr (qualifying_class,
3992                                                      postfix_expression,
3993                                                      done,
3994                                                      address_p);
3995       if (done)
3996         return postfix_expression;
3997     }
3998
3999   /* Keep looping until the postfix-expression is complete.  */
4000   while (true)
4001     {
4002       if (idk == CP_ID_KIND_UNQUALIFIED
4003           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4004           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4005         /* It is not a Koenig lookup function call.  */
4006         postfix_expression
4007           = unqualified_name_lookup_error (postfix_expression);
4008
4009       /* Peek at the next token.  */
4010       token = cp_lexer_peek_token (parser->lexer);
4011
4012       switch (token->type)
4013         {
4014         case CPP_OPEN_SQUARE:
4015           postfix_expression
4016             = cp_parser_postfix_open_square_expression (parser,
4017                                                         postfix_expression,
4018                                                         false);
4019           idk = CP_ID_KIND_NONE;
4020           break;
4021
4022         case CPP_OPEN_PAREN:
4023           /* postfix-expression ( expression-list [opt] ) */
4024           {
4025             bool koenig_p;
4026             tree args = (cp_parser_parenthesized_expression_list
4027                          (parser, false, 
4028                           /*cast_p=*/false,
4029                           /*non_constant_p=*/NULL));
4030
4031             if (args == error_mark_node)
4032               {
4033                 postfix_expression = error_mark_node;
4034                 break;
4035               }
4036
4037             /* Function calls are not permitted in
4038                constant-expressions.  */
4039             if (cp_parser_non_integral_constant_expression (parser,
4040                                                             "a function call"))
4041               {
4042                 postfix_expression = error_mark_node;
4043                 break;
4044               }
4045
4046             koenig_p = false;
4047             if (idk == CP_ID_KIND_UNQUALIFIED)
4048               {
4049                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4050                   {
4051                     if (args)
4052                       {
4053                         koenig_p = true;
4054                         postfix_expression
4055                           = perform_koenig_lookup (postfix_expression, args);
4056                       }
4057                     else
4058                       postfix_expression
4059                         = unqualified_fn_lookup_error (postfix_expression);
4060                   }
4061                 /* We do not perform argument-dependent lookup if
4062                    normal lookup finds a non-function, in accordance
4063                    with the expected resolution of DR 218.  */
4064                 else if (args && is_overloaded_fn (postfix_expression))
4065                   {
4066                     tree fn = get_first_fn (postfix_expression);
4067
4068                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4069                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4070
4071                     /* Only do argument dependent lookup if regular
4072                        lookup does not find a set of member functions.
4073                        [basic.lookup.koenig]/2a  */
4074                     if (!DECL_FUNCTION_MEMBER_P (fn))
4075                       {
4076                         koenig_p = true;
4077                         postfix_expression
4078                           = perform_koenig_lookup (postfix_expression, args);
4079                       }
4080                   }
4081               }
4082
4083             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4084               {
4085                 tree instance = TREE_OPERAND (postfix_expression, 0);
4086                 tree fn = TREE_OPERAND (postfix_expression, 1);
4087
4088                 if (processing_template_decl
4089                     && (type_dependent_expression_p (instance)
4090                         || (!BASELINK_P (fn)
4091                             && TREE_CODE (fn) != FIELD_DECL)
4092                         || type_dependent_expression_p (fn)
4093                         || any_type_dependent_arguments_p (args)))
4094                   {
4095                     postfix_expression
4096                       = build_min_nt (CALL_EXPR, postfix_expression,
4097                                       args, NULL_TREE);
4098                     break;
4099                   }
4100
4101                 if (BASELINK_P (fn))
4102                   postfix_expression
4103                     = (build_new_method_call
4104                        (instance, fn, args, NULL_TREE,
4105                         (idk == CP_ID_KIND_QUALIFIED
4106                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4107                 else
4108                   postfix_expression
4109                     = finish_call_expr (postfix_expression, args,
4110                                         /*disallow_virtual=*/false,
4111                                         /*koenig_p=*/false);
4112               }
4113             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4114                      || TREE_CODE (postfix_expression) == MEMBER_REF
4115                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4116               postfix_expression = (build_offset_ref_call_from_tree
4117                                     (postfix_expression, args));
4118             else if (idk == CP_ID_KIND_QUALIFIED)
4119               /* A call to a static class member, or a namespace-scope
4120                  function.  */
4121               postfix_expression
4122                 = finish_call_expr (postfix_expression, args,
4123                                     /*disallow_virtual=*/true,
4124                                     koenig_p);
4125             else
4126               /* All other function calls.  */
4127               postfix_expression
4128                 = finish_call_expr (postfix_expression, args,
4129                                     /*disallow_virtual=*/false,
4130                                     koenig_p);
4131
4132             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4133             idk = CP_ID_KIND_NONE;
4134           }
4135           break;
4136
4137         case CPP_DOT:
4138         case CPP_DEREF:
4139           /* postfix-expression . template [opt] id-expression
4140              postfix-expression . pseudo-destructor-name
4141              postfix-expression -> template [opt] id-expression
4142              postfix-expression -> pseudo-destructor-name */
4143
4144           /* Consume the `.' or `->' operator.  */
4145           cp_lexer_consume_token (parser->lexer);
4146
4147           postfix_expression
4148             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4149                                                       postfix_expression,
4150                                                       false, &idk);
4151           break;
4152
4153         case CPP_PLUS_PLUS:
4154           /* postfix-expression ++  */
4155           /* Consume the `++' token.  */
4156           cp_lexer_consume_token (parser->lexer);
4157           /* Generate a representation for the complete expression.  */
4158           postfix_expression
4159             = finish_increment_expr (postfix_expression,
4160                                      POSTINCREMENT_EXPR);
4161           /* Increments may not appear in constant-expressions.  */
4162           if (cp_parser_non_integral_constant_expression (parser,
4163                                                           "an increment"))
4164             postfix_expression = error_mark_node;
4165           idk = CP_ID_KIND_NONE;
4166           break;
4167
4168         case CPP_MINUS_MINUS:
4169           /* postfix-expression -- */
4170           /* Consume the `--' token.  */
4171           cp_lexer_consume_token (parser->lexer);
4172           /* Generate a representation for the complete expression.  */
4173           postfix_expression
4174             = finish_increment_expr (postfix_expression,
4175                                      POSTDECREMENT_EXPR);
4176           /* Decrements may not appear in constant-expressions.  */
4177           if (cp_parser_non_integral_constant_expression (parser,
4178                                                           "a decrement"))
4179             postfix_expression = error_mark_node;
4180           idk = CP_ID_KIND_NONE;
4181           break;
4182
4183         default:
4184           return postfix_expression;
4185         }
4186     }
4187
4188   /* We should never get here.  */
4189   gcc_unreachable ();
4190   return error_mark_node;
4191 }
4192
4193 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4194    by cp_parser_builtin_offsetof.  We're looking for
4195
4196      postfix-expression [ expression ]
4197
4198    FOR_OFFSETOF is set if we're being called in that context, which
4199    changes how we deal with integer constant expressions.  */
4200
4201 static tree
4202 cp_parser_postfix_open_square_expression (cp_parser *parser,
4203                                           tree postfix_expression,
4204                                           bool for_offsetof)
4205 {
4206   tree index;
4207
4208   /* Consume the `[' token.  */
4209   cp_lexer_consume_token (parser->lexer);
4210
4211   /* Parse the index expression.  */
4212   /* ??? For offsetof, there is a question of what to allow here.  If
4213      offsetof is not being used in an integral constant expression context,
4214      then we *could* get the right answer by computing the value at runtime.
4215      If we are in an integral constant expression context, then we might
4216      could accept any constant expression; hard to say without analysis.
4217      Rather than open the barn door too wide right away, allow only integer
4218      constant expressions here.  */
4219   if (for_offsetof)
4220     index = cp_parser_constant_expression (parser, false, NULL);
4221   else
4222     index = cp_parser_expression (parser, /*cast_p=*/false);
4223
4224   /* Look for the closing `]'.  */
4225   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4226
4227   /* Build the ARRAY_REF.  */
4228   postfix_expression = grok_array_decl (postfix_expression, index);
4229
4230   /* When not doing offsetof, array references are not permitted in
4231      constant-expressions.  */
4232   if (!for_offsetof
4233       && (cp_parser_non_integral_constant_expression
4234           (parser, "an array reference")))
4235     postfix_expression = error_mark_node;
4236
4237   return postfix_expression;
4238 }
4239
4240 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4241    by cp_parser_builtin_offsetof.  We're looking for
4242
4243      postfix-expression . template [opt] id-expression
4244      postfix-expression . pseudo-destructor-name
4245      postfix-expression -> template [opt] id-expression
4246      postfix-expression -> pseudo-destructor-name
4247
4248    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4249    limits what of the above we'll actually accept, but nevermind.
4250    TOKEN_TYPE is the "." or "->" token, which will already have been
4251    removed from the stream.  */
4252
4253 static tree
4254 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4255                                         enum cpp_ttype token_type,
4256                                         tree postfix_expression,
4257                                         bool for_offsetof, cp_id_kind *idk)
4258 {
4259   tree name;
4260   bool dependent_p;
4261   bool template_p;
4262   bool pseudo_destructor_p;
4263   tree scope = NULL_TREE;
4264
4265   /* If this is a `->' operator, dereference the pointer.  */
4266   if (token_type == CPP_DEREF)
4267     postfix_expression = build_x_arrow (postfix_expression);
4268   /* Check to see whether or not the expression is type-dependent.  */
4269   dependent_p = type_dependent_expression_p (postfix_expression);
4270   /* The identifier following the `->' or `.' is not qualified.  */
4271   parser->scope = NULL_TREE;
4272   parser->qualifying_scope = NULL_TREE;
4273   parser->object_scope = NULL_TREE;
4274   *idk = CP_ID_KIND_NONE;
4275   /* Enter the scope corresponding to the type of the object
4276      given by the POSTFIX_EXPRESSION.  */
4277   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4278     {
4279       scope = TREE_TYPE (postfix_expression);
4280       /* According to the standard, no expression should ever have
4281          reference type.  Unfortunately, we do not currently match
4282          the standard in this respect in that our internal representation
4283          of an expression may have reference type even when the standard
4284          says it does not.  Therefore, we have to manually obtain the
4285          underlying type here.  */
4286       scope = non_reference (scope);
4287       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4288       scope = complete_type_or_else (scope, NULL_TREE);
4289       /* Let the name lookup machinery know that we are processing a
4290          class member access expression.  */
4291       parser->context->object_type = scope;
4292       /* If something went wrong, we want to be able to discern that case,
4293          as opposed to the case where there was no SCOPE due to the type
4294          of expression being dependent.  */
4295       if (!scope)
4296         scope = error_mark_node;
4297       /* If the SCOPE was erroneous, make the various semantic analysis
4298          functions exit quickly -- and without issuing additional error
4299          messages.  */
4300       if (scope == error_mark_node)
4301         postfix_expression = error_mark_node;
4302     }
4303
4304   /* Assume this expression is not a pseudo-destructor access.  */
4305   pseudo_destructor_p = false;
4306
4307   /* If the SCOPE is a scalar type, then, if this is a valid program,
4308      we must be looking at a pseudo-destructor-name.  */
4309   if (scope && SCALAR_TYPE_P (scope))
4310     {
4311       tree s;
4312       tree type;
4313
4314       cp_parser_parse_tentatively (parser);
4315       /* Parse the pseudo-destructor-name.  */
4316       s = NULL_TREE;
4317       cp_parser_pseudo_destructor_name (parser, &s, &type);
4318       if (cp_parser_parse_definitely (parser))
4319         {
4320           pseudo_destructor_p = true;
4321           postfix_expression
4322             = finish_pseudo_destructor_expr (postfix_expression,
4323                                              s, TREE_TYPE (type));
4324         }
4325     }
4326
4327   if (!pseudo_destructor_p)
4328     {
4329       /* If the SCOPE is not a scalar type, we are looking at an
4330          ordinary class member access expression, rather than a
4331          pseudo-destructor-name.  */
4332       template_p = cp_parser_optional_template_keyword (parser);
4333       /* Parse the id-expression.  */
4334       name = cp_parser_id_expression (parser, template_p,
4335                                       /*check_dependency_p=*/true,
4336                                       /*template_p=*/NULL,
4337                                       /*declarator_p=*/false);
4338       /* In general, build a SCOPE_REF if the member name is qualified.
4339          However, if the name was not dependent and has already been
4340          resolved; there is no need to build the SCOPE_REF.  For example;
4341
4342              struct X { void f(); };
4343              template <typename T> void f(T* t) { t->X::f(); }
4344
4345          Even though "t" is dependent, "X::f" is not and has been resolved
4346          to a BASELINK; there is no need to include scope information.  */
4347
4348       /* But we do need to remember that there was an explicit scope for
4349          virtual function calls.  */
4350       if (parser->scope)
4351         *idk = CP_ID_KIND_QUALIFIED;
4352
4353       /* If the name is a template-id that names a type, we will get a
4354          TYPE_DECL here.  That is invalid code.  */
4355       if (TREE_CODE (name) == TYPE_DECL)
4356         {
4357           error ("invalid use of %qD", name);
4358           postfix_expression = error_mark_node;
4359         }
4360       else
4361         {
4362           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4363             {
4364               name = build_nt (SCOPE_REF, parser->scope, name);
4365               parser->scope = NULL_TREE;
4366               parser->qualifying_scope = NULL_TREE;
4367               parser->object_scope = NULL_TREE;
4368             }
4369           if (scope && name && BASELINK_P (name))
4370             adjust_result_of_qualified_name_lookup
4371               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4372           postfix_expression
4373             = finish_class_member_access_expr (postfix_expression, name);
4374         }
4375     }
4376
4377   /* We no longer need to look up names in the scope of the object on
4378      the left-hand side of the `.' or `->' operator.  */
4379   parser->context->object_type = NULL_TREE;
4380
4381   /* Outside of offsetof, these operators may not appear in
4382      constant-expressions.  */
4383   if (!for_offsetof
4384       && (cp_parser_non_integral_constant_expression
4385           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4386     postfix_expression = error_mark_node;
4387
4388   return postfix_expression;
4389 }
4390
4391 /* Parse a parenthesized expression-list.
4392
4393    expression-list:
4394      assignment-expression
4395      expression-list, assignment-expression
4396
4397    attribute-list:
4398      expression-list
4399      identifier
4400      identifier, expression-list
4401
4402    CAST_P is true if this expression is the target of a cast.
4403
4404    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4405    representation of an assignment-expression.  Note that a TREE_LIST
4406    is returned even if there is only a single expression in the list.
4407    error_mark_node is returned if the ( and or ) are
4408    missing. NULL_TREE is returned on no expressions. The parentheses
4409    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4410    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4411    indicates whether or not all of the expressions in the list were
4412    constant.  */
4413
4414 static tree
4415 cp_parser_parenthesized_expression_list (cp_parser* parser,
4416                                          bool is_attribute_list,
4417                                          bool cast_p,
4418                                          bool *non_constant_p)
4419 {
4420   tree expression_list = NULL_TREE;
4421   bool fold_expr_p = is_attribute_list;
4422   tree identifier = NULL_TREE;
4423
4424   /* Assume all the expressions will be constant.  */
4425   if (non_constant_p)
4426     *non_constant_p = false;
4427
4428   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4429     return error_mark_node;
4430
4431   /* Consume expressions until there are no more.  */
4432   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4433     while (true)
4434       {
4435         tree expr;
4436
4437         /* At the beginning of attribute lists, check to see if the
4438            next token is an identifier.  */
4439         if (is_attribute_list
4440             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4441           {
4442             cp_token *token;
4443
4444             /* Consume the identifier.  */
4445             token = cp_lexer_consume_token (parser->lexer);
4446             /* Save the identifier.  */
4447             identifier = token->value;
4448           }
4449         else
4450           {
4451             /* Parse the next assignment-expression.  */
4452             if (non_constant_p)
4453               {
4454                 bool expr_non_constant_p;
4455                 expr = (cp_parser_constant_expression
4456                         (parser, /*allow_non_constant_p=*/true,
4457                          &expr_non_constant_p));
4458                 if (expr_non_constant_p)
4459                   *non_constant_p = true;
4460               }
4461             else
4462               expr = cp_parser_assignment_expression (parser, cast_p);
4463
4464             if (fold_expr_p)
4465               expr = fold_non_dependent_expr (expr);
4466
4467              /* Add it to the list.  We add error_mark_node
4468                 expressions to the list, so that we can still tell if
4469                 the correct form for a parenthesized expression-list
4470                 is found. That gives better errors.  */
4471             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4472
4473             if (expr == error_mark_node)
4474               goto skip_comma;
4475           }
4476
4477         /* After the first item, attribute lists look the same as
4478            expression lists.  */
4479         is_attribute_list = false;
4480
4481       get_comma:;
4482         /* If the next token isn't a `,', then we are done.  */
4483         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4484           break;
4485
4486         /* Otherwise, consume the `,' and keep going.  */
4487         cp_lexer_consume_token (parser->lexer);
4488       }
4489
4490   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4491     {
4492       int ending;
4493
4494     skip_comma:;
4495       /* We try and resync to an unnested comma, as that will give the
4496          user better diagnostics.  */
4497       ending = cp_parser_skip_to_closing_parenthesis (parser,
4498                                                       /*recovering=*/true,
4499                                                       /*or_comma=*/true,
4500                                                       /*consume_paren=*/true);
4501       if (ending < 0)
4502         goto get_comma;
4503       if (!ending)
4504         return error_mark_node;
4505     }
4506
4507   /* We built up the list in reverse order so we must reverse it now.  */
4508   expression_list = nreverse (expression_list);
4509   if (identifier)
4510     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4511
4512   return expression_list;
4513 }
4514
4515 /* Parse a pseudo-destructor-name.
4516
4517    pseudo-destructor-name:
4518      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4519      :: [opt] nested-name-specifier template template-id :: ~ type-name
4520      :: [opt] nested-name-specifier [opt] ~ type-name
4521
4522    If either of the first two productions is used, sets *SCOPE to the
4523    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4524    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4525    or ERROR_MARK_NODE if the parse fails.  */
4526
4527 static void
4528 cp_parser_pseudo_destructor_name (cp_parser* parser,
4529                                   tree* scope,
4530                                   tree* type)
4531 {
4532   bool nested_name_specifier_p;
4533
4534   /* Assume that things will not work out.  */
4535   *type = error_mark_node;
4536
4537   /* Look for the optional `::' operator.  */
4538   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4539   /* Look for the optional nested-name-specifier.  */
4540   nested_name_specifier_p
4541     = (cp_parser_nested_name_specifier_opt (parser,
4542                                             /*typename_keyword_p=*/false,
4543                                             /*check_dependency_p=*/true,
4544                                             /*type_p=*/false,
4545                                             /*is_declaration=*/true)
4546        != NULL_TREE);
4547   /* Now, if we saw a nested-name-specifier, we might be doing the
4548      second production.  */
4549   if (nested_name_specifier_p
4550       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4551     {
4552       /* Consume the `template' keyword.  */
4553       cp_lexer_consume_token (parser->lexer);
4554       /* Parse the template-id.  */
4555       cp_parser_template_id (parser,
4556                              /*template_keyword_p=*/true,
4557                              /*check_dependency_p=*/false,
4558                              /*is_declaration=*/true);
4559       /* Look for the `::' token.  */
4560       cp_parser_require (parser, CPP_SCOPE, "`::'");
4561     }
4562   /* If the next token is not a `~', then there might be some
4563      additional qualification.  */
4564   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4565     {
4566       /* Look for the type-name.  */
4567       *scope = TREE_TYPE (cp_parser_type_name (parser));
4568
4569       if (*scope == error_mark_node)
4570         return;
4571
4572       /* If we don't have ::~, then something has gone wrong.  Since
4573          the only caller of this function is looking for something
4574          after `.' or `->' after a scalar type, most likely the
4575          program is trying to get a member of a non-aggregate
4576          type.  */
4577       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4578           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4579         {
4580           cp_parser_error (parser, "request for member of non-aggregate type");
4581           return;
4582         }
4583
4584       /* Look for the `::' token.  */
4585       cp_parser_require (parser, CPP_SCOPE, "`::'");
4586     }
4587   else
4588     *scope = NULL_TREE;
4589
4590   /* Look for the `~'.  */
4591   cp_parser_require (parser, CPP_COMPL, "`~'");
4592   /* Look for the type-name again.  We are not responsible for
4593      checking that it matches the first type-name.  */
4594   *type = cp_parser_type_name (parser);
4595 }
4596
4597 /* Parse a unary-expression.
4598
4599    unary-expression:
4600      postfix-expression
4601      ++ cast-expression
4602      -- cast-expression
4603      unary-operator cast-expression
4604      sizeof unary-expression
4605      sizeof ( type-id )
4606      new-expression
4607      delete-expression
4608
4609    GNU Extensions:
4610
4611    unary-expression:
4612      __extension__ cast-expression
4613      __alignof__ unary-expression
4614      __alignof__ ( type-id )
4615      __real__ cast-expression
4616      __imag__ cast-expression
4617      && identifier
4618
4619    ADDRESS_P is true iff the unary-expression is appearing as the
4620    operand of the `&' operator.   CAST_P is true if this expression is
4621    the target of a cast.
4622
4623    Returns a representation of the expression.  */
4624
4625 static tree
4626 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4627 {
4628   cp_token *token;
4629   enum tree_code unary_operator;
4630
4631   /* Peek at the next token.  */
4632   token = cp_lexer_peek_token (parser->lexer);
4633   /* Some keywords give away the kind of expression.  */
4634   if (token->type == CPP_KEYWORD)
4635     {
4636       enum rid keyword = token->keyword;
4637
4638       switch (keyword)
4639         {
4640         case RID_ALIGNOF:
4641         case RID_SIZEOF:
4642           {
4643             tree operand;
4644             enum tree_code op;
4645
4646             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4647             /* Consume the token.  */
4648             cp_lexer_consume_token (parser->lexer);
4649             /* Parse the operand.  */
4650             operand = cp_parser_sizeof_operand (parser, keyword);
4651
4652             if (TYPE_P (operand))
4653               return cxx_sizeof_or_alignof_type (operand, op, true);
4654             else
4655               return cxx_sizeof_or_alignof_expr (operand, op);
4656           }
4657
4658         case RID_NEW:
4659           return cp_parser_new_expression (parser);
4660
4661         case RID_DELETE:
4662           return cp_parser_delete_expression (parser);
4663
4664         case RID_EXTENSION:
4665           {
4666             /* The saved value of the PEDANTIC flag.  */
4667             int saved_pedantic;
4668             tree expr;
4669
4670             /* Save away the PEDANTIC flag.  */
4671             cp_parser_extension_opt (parser, &saved_pedantic);
4672             /* Parse the cast-expression.  */
4673             expr = cp_parser_simple_cast_expression (parser);
4674             /* Restore the PEDANTIC flag.  */
4675             pedantic = saved_pedantic;
4676
4677             return expr;
4678           }
4679
4680         case RID_REALPART:
4681         case RID_IMAGPART:
4682           {
4683             tree expression;
4684
4685             /* Consume the `__real__' or `__imag__' token.  */
4686             cp_lexer_consume_token (parser->lexer);
4687             /* Parse the cast-expression.  */
4688             expression = cp_parser_simple_cast_expression (parser);
4689             /* Create the complete representation.  */
4690             return build_x_unary_op ((keyword == RID_REALPART
4691                                       ? REALPART_EXPR : IMAGPART_EXPR),
4692                                      expression);
4693           }
4694           break;
4695
4696         default:
4697           break;
4698         }
4699     }
4700
4701   /* Look for the `:: new' and `:: delete', which also signal the
4702      beginning of a new-expression, or delete-expression,
4703      respectively.  If the next token is `::', then it might be one of
4704      these.  */
4705   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4706     {
4707       enum rid keyword;
4708
4709       /* See if the token after the `::' is one of the keywords in
4710          which we're interested.  */
4711       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4712       /* If it's `new', we have a new-expression.  */
4713       if (keyword == RID_NEW)
4714         return cp_parser_new_expression (parser);
4715       /* Similarly, for `delete'.  */
4716       else if (keyword == RID_DELETE)
4717         return cp_parser_delete_expression (parser);
4718     }
4719
4720   /* Look for a unary operator.  */
4721   unary_operator = cp_parser_unary_operator (token);
4722   /* The `++' and `--' operators can be handled similarly, even though
4723      they are not technically unary-operators in the grammar.  */
4724   if (unary_operator == ERROR_MARK)
4725     {
4726       if (token->type == CPP_PLUS_PLUS)
4727         unary_operator = PREINCREMENT_EXPR;
4728       else if (token->type == CPP_MINUS_MINUS)
4729         unary_operator = PREDECREMENT_EXPR;
4730       /* Handle the GNU address-of-label extension.  */
4731       else if (cp_parser_allow_gnu_extensions_p (parser)
4732                && token->type == CPP_AND_AND)
4733         {
4734           tree identifier;
4735
4736           /* Consume the '&&' token.  */
4737           cp_lexer_consume_token (parser->lexer);
4738           /* Look for the identifier.  */
4739           identifier = cp_parser_identifier (parser);
4740           /* Create an expression representing the address.  */
4741           return finish_label_address_expr (identifier);
4742         }
4743     }
4744   if (unary_operator != ERROR_MARK)
4745     {
4746       tree cast_expression;
4747       tree expression = error_mark_node;
4748       const char *non_constant_p = NULL;
4749
4750       /* Consume the operator token.  */
4751       token = cp_lexer_consume_token (parser->lexer);
4752       /* Parse the cast-expression.  */
4753       cast_expression
4754         = cp_parser_cast_expression (parser, 
4755                                      unary_operator == ADDR_EXPR,
4756                                      /*cast_p=*/false);
4757       /* Now, build an appropriate representation.  */
4758       switch (unary_operator)
4759         {
4760         case INDIRECT_REF:
4761           non_constant_p = "`*'";
4762           expression = build_x_indirect_ref (cast_expression, "unary *");
4763           break;
4764
4765         case ADDR_EXPR:
4766           non_constant_p = "`&'";
4767           /* Fall through.  */
4768         case BIT_NOT_EXPR:
4769           expression = build_x_unary_op (unary_operator, cast_expression);
4770           break;
4771
4772         case PREINCREMENT_EXPR:
4773         case PREDECREMENT_EXPR:
4774           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4775                             ? "`++'" : "`--'");
4776           /* Fall through.  */
4777         case CONVERT_EXPR:
4778         case NEGATE_EXPR:
4779         case TRUTH_NOT_EXPR:
4780           expression = finish_unary_op_expr (unary_operator, cast_expression);
4781           break;
4782
4783         default:
4784           gcc_unreachable ();
4785         }
4786
4787       if (non_constant_p
4788           && cp_parser_non_integral_constant_expression (parser,
4789                                                          non_constant_p))
4790         expression = error_mark_node;
4791
4792       return expression;
4793     }
4794
4795   return cp_parser_postfix_expression (parser, address_p, cast_p);
4796 }
4797
4798 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4799    unary-operator, the corresponding tree code is returned.  */
4800
4801 static enum tree_code
4802 cp_parser_unary_operator (cp_token* token)
4803 {
4804   switch (token->type)
4805     {
4806     case CPP_MULT:
4807       return INDIRECT_REF;
4808
4809     case CPP_AND:
4810       return ADDR_EXPR;
4811
4812     case CPP_PLUS:
4813       return CONVERT_EXPR;
4814
4815     case CPP_MINUS:
4816       return NEGATE_EXPR;
4817
4818     case CPP_NOT:
4819       return TRUTH_NOT_EXPR;
4820
4821     case CPP_COMPL:
4822       return BIT_NOT_EXPR;
4823
4824     default:
4825       return ERROR_MARK;
4826     }
4827 }
4828
4829 /* Parse a new-expression.
4830
4831    new-expression:
4832      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4833      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4834
4835    Returns a representation of the expression.  */
4836
4837 static tree
4838 cp_parser_new_expression (cp_parser* parser)
4839 {
4840   bool global_scope_p;
4841   tree placement;
4842   tree type;
4843   tree initializer;
4844   tree nelts;
4845
4846   /* Look for the optional `::' operator.  */
4847   global_scope_p
4848     = (cp_parser_global_scope_opt (parser,
4849                                    /*current_scope_valid_p=*/false)
4850        != NULL_TREE);
4851   /* Look for the `new' operator.  */
4852   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4853   /* There's no easy way to tell a new-placement from the
4854      `( type-id )' construct.  */
4855   cp_parser_parse_tentatively (parser);
4856   /* Look for a new-placement.  */
4857   placement = cp_parser_new_placement (parser);
4858   /* If that didn't work out, there's no new-placement.  */
4859   if (!cp_parser_parse_definitely (parser))
4860     placement = NULL_TREE;
4861
4862   /* If the next token is a `(', then we have a parenthesized
4863      type-id.  */
4864   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4865     {
4866       /* Consume the `('.  */
4867       cp_lexer_consume_token (parser->lexer);
4868       /* Parse the type-id.  */
4869       type = cp_parser_type_id (parser);
4870       /* Look for the closing `)'.  */
4871       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4872       /* There should not be a direct-new-declarator in this production,
4873          but GCC used to allowed this, so we check and emit a sensible error
4874          message for this case.  */
4875       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4876         {
4877           error ("array bound forbidden after parenthesized type-id");
4878           inform ("try removing the parentheses around the type-id");
4879           cp_parser_direct_new_declarator (parser);
4880         }
4881       nelts = NULL_TREE;
4882     }
4883   /* Otherwise, there must be a new-type-id.  */
4884   else
4885     type = cp_parser_new_type_id (parser, &nelts);
4886
4887   /* If the next token is a `(', then we have a new-initializer.  */
4888   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4889     initializer = cp_parser_new_initializer (parser);
4890   else
4891     initializer = NULL_TREE;
4892
4893   /* A new-expression may not appear in an integral constant
4894      expression.  */
4895   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4896     return error_mark_node;
4897
4898   /* Create a representation of the new-expression.  */
4899   return build_new (placement, type, nelts, initializer, global_scope_p);
4900 }
4901
4902 /* Parse a new-placement.
4903
4904    new-placement:
4905      ( expression-list )
4906
4907    Returns the same representation as for an expression-list.  */
4908
4909 static tree
4910 cp_parser_new_placement (cp_parser* parser)
4911 {
4912   tree expression_list;
4913
4914   /* Parse the expression-list.  */
4915   expression_list = (cp_parser_parenthesized_expression_list
4916                      (parser, false, /*cast_p=*/false,
4917                       /*non_constant_p=*/NULL));
4918
4919   return expression_list;
4920 }
4921
4922 /* Parse a new-type-id.
4923
4924    new-type-id:
4925      type-specifier-seq new-declarator [opt]
4926
4927    Returns the TYPE allocated.  If the new-type-id indicates an array
4928    type, *NELTS is set to the number of elements in the last array
4929    bound; the TYPE will not include the last array bound.  */
4930
4931 static tree
4932 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4933 {
4934   cp_decl_specifier_seq type_specifier_seq;
4935   cp_declarator *new_declarator;
4936   cp_declarator *declarator;
4937   cp_declarator *outer_declarator;
4938   const char *saved_message;
4939   tree type;
4940
4941   /* The type-specifier sequence must not contain type definitions.
4942      (It cannot contain declarations of new types either, but if they
4943      are not definitions we will catch that because they are not
4944      complete.)  */
4945   saved_message = parser->type_definition_forbidden_message;
4946   parser->type_definition_forbidden_message
4947     = "types may not be defined in a new-type-id";
4948   /* Parse the type-specifier-seq.  */
4949   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4950   /* Restore the old message.  */
4951   parser->type_definition_forbidden_message = saved_message;
4952   /* Parse the new-declarator.  */
4953   new_declarator = cp_parser_new_declarator_opt (parser);
4954
4955   /* Determine the number of elements in the last array dimension, if
4956      any.  */
4957   *nelts = NULL_TREE;
4958   /* Skip down to the last array dimension.  */
4959   declarator = new_declarator;
4960   outer_declarator = NULL;
4961   while (declarator && (declarator->kind == cdk_pointer
4962                         || declarator->kind == cdk_ptrmem))
4963     {
4964       outer_declarator = declarator;
4965       declarator = declarator->declarator;
4966     }
4967   while (declarator
4968          && declarator->kind == cdk_array
4969          && declarator->declarator
4970          && declarator->declarator->kind == cdk_array)
4971     {
4972       outer_declarator = declarator;
4973       declarator = declarator->declarator;
4974     }
4975
4976   if (declarator && declarator->kind == cdk_array)
4977     {
4978       *nelts = declarator->u.array.bounds;
4979       if (*nelts == error_mark_node)
4980         *nelts = integer_one_node;
4981       
4982       if (outer_declarator)
4983         outer_declarator->declarator = declarator->declarator;
4984       else
4985         new_declarator = NULL;
4986     }
4987
4988   type = groktypename (&type_specifier_seq, new_declarator);
4989   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4990     {
4991       *nelts = array_type_nelts_top (type);
4992       type = TREE_TYPE (type);
4993     }
4994   return type;
4995 }
4996
4997 /* Parse an (optional) new-declarator.
4998
4999    new-declarator:
5000      ptr-operator new-declarator [opt]
5001      direct-new-declarator
5002
5003    Returns the declarator.  */
5004
5005 static cp_declarator *
5006 cp_parser_new_declarator_opt (cp_parser* parser)
5007 {
5008   enum tree_code code;
5009   tree type;
5010   cp_cv_quals cv_quals;
5011
5012   /* We don't know if there's a ptr-operator next, or not.  */
5013   cp_parser_parse_tentatively (parser);
5014   /* Look for a ptr-operator.  */
5015   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5016   /* If that worked, look for more new-declarators.  */
5017   if (cp_parser_parse_definitely (parser))
5018     {
5019       cp_declarator *declarator;
5020
5021       /* Parse another optional declarator.  */
5022       declarator = cp_parser_new_declarator_opt (parser);
5023
5024       /* Create the representation of the declarator.  */
5025       if (type)
5026         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5027       else if (code == INDIRECT_REF)
5028         declarator = make_pointer_declarator (cv_quals, declarator);
5029       else
5030         declarator = make_reference_declarator (cv_quals, declarator);
5031
5032       return declarator;
5033     }
5034
5035   /* If the next token is a `[', there is a direct-new-declarator.  */
5036   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5037     return cp_parser_direct_new_declarator (parser);
5038
5039   return NULL;
5040 }
5041
5042 /* Parse a direct-new-declarator.
5043
5044    direct-new-declarator:
5045      [ expression ]
5046      direct-new-declarator [constant-expression]
5047
5048    */
5049
5050 static cp_declarator *
5051 cp_parser_direct_new_declarator (cp_parser* parser)
5052 {
5053   cp_declarator *declarator = NULL;
5054
5055   while (true)
5056     {
5057       tree expression;
5058
5059       /* Look for the opening `['.  */
5060       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5061       /* The first expression is not required to be constant.  */
5062       if (!declarator)
5063         {
5064           expression = cp_parser_expression (parser, /*cast_p=*/false);
5065           /* The standard requires that the expression have integral
5066              type.  DR 74 adds enumeration types.  We believe that the
5067              real intent is that these expressions be handled like the
5068              expression in a `switch' condition, which also allows
5069              classes with a single conversion to integral or
5070              enumeration type.  */
5071           if (!processing_template_decl)
5072             {
5073               expression
5074                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5075                                               expression,
5076                                               /*complain=*/true);
5077               if (!expression)
5078                 {
5079                   error ("expression in new-declarator must have integral "
5080                          "or enumeration type");
5081                   expression = error_mark_node;
5082                 }
5083             }
5084         }
5085       /* But all the other expressions must be.  */
5086       else
5087         expression
5088           = cp_parser_constant_expression (parser,
5089                                            /*allow_non_constant=*/false,
5090                                            NULL);
5091       /* Look for the closing `]'.  */
5092       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5093
5094       /* Add this bound to the declarator.  */
5095       declarator = make_array_declarator (declarator, expression);
5096
5097       /* If the next token is not a `[', then there are no more
5098          bounds.  */
5099       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5100         break;
5101     }
5102
5103   return declarator;
5104 }
5105
5106 /* Parse a new-initializer.
5107
5108    new-initializer:
5109      ( expression-list [opt] )
5110
5111    Returns a representation of the expression-list.  If there is no
5112    expression-list, VOID_ZERO_NODE is returned.  */
5113
5114 static tree
5115 cp_parser_new_initializer (cp_parser* parser)
5116 {
5117   tree expression_list;
5118
5119   expression_list = (cp_parser_parenthesized_expression_list
5120                      (parser, false, /*cast_p=*/false,
5121                       /*non_constant_p=*/NULL));
5122   if (!expression_list)
5123     expression_list = void_zero_node;
5124
5125   return expression_list;
5126 }
5127
5128 /* Parse a delete-expression.
5129
5130    delete-expression:
5131      :: [opt] delete cast-expression
5132      :: [opt] delete [ ] cast-expression
5133
5134    Returns a representation of the expression.  */
5135
5136 static tree
5137 cp_parser_delete_expression (cp_parser* parser)
5138 {
5139   bool global_scope_p;
5140   bool array_p;
5141   tree expression;
5142
5143   /* Look for the optional `::' operator.  */
5144   global_scope_p
5145     = (cp_parser_global_scope_opt (parser,
5146                                    /*current_scope_valid_p=*/false)
5147        != NULL_TREE);
5148   /* Look for the `delete' keyword.  */
5149   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5150   /* See if the array syntax is in use.  */
5151   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5152     {
5153       /* Consume the `[' token.  */
5154       cp_lexer_consume_token (parser->lexer);
5155       /* Look for the `]' token.  */
5156       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5157       /* Remember that this is the `[]' construct.  */
5158       array_p = true;
5159     }
5160   else
5161     array_p = false;
5162
5163   /* Parse the cast-expression.  */
5164   expression = cp_parser_simple_cast_expression (parser);
5165
5166   /* A delete-expression may not appear in an integral constant
5167      expression.  */
5168   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5169     return error_mark_node;
5170
5171   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5172 }
5173
5174 /* Parse a cast-expression.
5175
5176    cast-expression:
5177      unary-expression
5178      ( type-id ) cast-expression
5179
5180    ADDRESS_P is true iff the unary-expression is appearing as the
5181    operand of the `&' operator.   CAST_P is true if this expression is
5182    the target of a cast.
5183
5184    Returns a representation of the expression.  */
5185
5186 static tree
5187 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5188 {
5189   /* If it's a `(', then we might be looking at a cast.  */
5190   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5191     {
5192       tree type = NULL_TREE;
5193       tree expr = NULL_TREE;
5194       bool compound_literal_p;
5195       const char *saved_message;
5196
5197       /* There's no way to know yet whether or not this is a cast.
5198          For example, `(int (3))' is a unary-expression, while `(int)
5199          3' is a cast.  So, we resort to parsing tentatively.  */
5200       cp_parser_parse_tentatively (parser);
5201       /* Types may not be defined in a cast.  */
5202       saved_message = parser->type_definition_forbidden_message;
5203       parser->type_definition_forbidden_message
5204         = "types may not be defined in casts";
5205       /* Consume the `('.  */
5206       cp_lexer_consume_token (parser->lexer);
5207       /* A very tricky bit is that `(struct S) { 3 }' is a
5208          compound-literal (which we permit in C++ as an extension).
5209          But, that construct is not a cast-expression -- it is a
5210          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5211          is legal; if the compound-literal were a cast-expression,
5212          you'd need an extra set of parentheses.)  But, if we parse
5213          the type-id, and it happens to be a class-specifier, then we
5214          will commit to the parse at that point, because we cannot
5215          undo the action that is done when creating a new class.  So,
5216          then we cannot back up and do a postfix-expression.
5217
5218          Therefore, we scan ahead to the closing `)', and check to see
5219          if the token after the `)' is a `{'.  If so, we are not
5220          looking at a cast-expression.
5221
5222          Save tokens so that we can put them back.  */
5223       cp_lexer_save_tokens (parser->lexer);
5224       /* Skip tokens until the next token is a closing parenthesis.
5225          If we find the closing `)', and the next token is a `{', then
5226          we are looking at a compound-literal.  */
5227       compound_literal_p
5228         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5229                                                   /*consume_paren=*/true)
5230            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5231       /* Roll back the tokens we skipped.  */
5232       cp_lexer_rollback_tokens (parser->lexer);
5233       /* If we were looking at a compound-literal, simulate an error
5234          so that the call to cp_parser_parse_definitely below will
5235          fail.  */
5236       if (compound_literal_p)
5237         cp_parser_simulate_error (parser);
5238       else
5239         {
5240           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5241           parser->in_type_id_in_expr_p = true;
5242           /* Look for the type-id.  */
5243           type = cp_parser_type_id (parser);
5244           /* Look for the closing `)'.  */
5245           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5246           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5247         }
5248
5249       /* Restore the saved message.  */
5250       parser->type_definition_forbidden_message = saved_message;
5251
5252       /* If ok so far, parse the dependent expression. We cannot be
5253          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5254          ctor of T, but looks like a cast to function returning T
5255          without a dependent expression.  */
5256       if (!cp_parser_error_occurred (parser))
5257         expr = cp_parser_cast_expression (parser, 
5258                                           /*address_p=*/false,
5259                                           /*cast_p=*/true);
5260
5261       if (cp_parser_parse_definitely (parser))
5262         {
5263           /* Warn about old-style casts, if so requested.  */
5264           if (warn_old_style_cast
5265               && !in_system_header
5266               && !VOID_TYPE_P (type)
5267               && current_lang_name != lang_name_c)
5268             warning ("use of old-style cast");
5269
5270           /* Only type conversions to integral or enumeration types
5271              can be used in constant-expressions.  */
5272           if (parser->integral_constant_expression_p
5273               && !dependent_type_p (type)
5274               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5275               && (cp_parser_non_integral_constant_expression
5276                   (parser,
5277                    "a cast to a type other than an integral or "
5278                    "enumeration type")))
5279             return error_mark_node;
5280
5281           /* Perform the cast.  */
5282           expr = build_c_cast (type, expr);
5283           return expr;
5284         }
5285     }
5286
5287   /* If we get here, then it's not a cast, so it must be a
5288      unary-expression.  */
5289   return cp_parser_unary_expression (parser, address_p, cast_p);
5290 }
5291
5292 /* Parse a binary expression of the general form:
5293
5294    pm-expression:
5295      cast-expression
5296      pm-expression .* cast-expression
5297      pm-expression ->* cast-expression
5298
5299    multiplicative-expression:
5300      pm-expression
5301      multiplicative-expression * pm-expression
5302      multiplicative-expression / pm-expression
5303      multiplicative-expression % pm-expression
5304
5305    additive-expression:
5306      multiplicative-expression
5307      additive-expression + multiplicative-expression
5308      additive-expression - multiplicative-expression
5309
5310    shift-expression:
5311      additive-expression
5312      shift-expression << additive-expression
5313      shift-expression >> additive-expression
5314
5315    relational-expression:
5316      shift-expression
5317      relational-expression < shift-expression
5318      relational-expression > shift-expression
5319      relational-expression <= shift-expression
5320      relational-expression >= shift-expression
5321
5322   GNU Extension:
5323   
5324    relational-expression:
5325      relational-expression <? shift-expression
5326      relational-expression >? shift-expression
5327
5328    equality-expression:
5329      relational-expression
5330      equality-expression == relational-expression
5331      equality-expression != relational-expression
5332
5333    and-expression:
5334      equality-expression
5335      and-expression & equality-expression
5336
5337    exclusive-or-expression:
5338      and-expression
5339      exclusive-or-expression ^ and-expression
5340
5341    inclusive-or-expression:
5342      exclusive-or-expression
5343      inclusive-or-expression | exclusive-or-expression
5344
5345    logical-and-expression:
5346      inclusive-or-expression
5347      logical-and-expression && inclusive-or-expression
5348
5349    logical-or-expression:
5350      logical-and-expression
5351      logical-or-expression || logical-and-expression
5352
5353    All these are implemented with a single function like:
5354
5355    binary-expression:
5356      simple-cast-expression
5357      binary-expression <token> binary-expression
5358
5359    CAST_P is true if this expression is the target of a cast.
5360
5361    The binops_by_token map is used to get the tree codes for each <token> type.
5362    binary-expressions are associated according to a precedence table.  */
5363
5364 #define TOKEN_PRECEDENCE(token) \
5365   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5366    ? PREC_NOT_OPERATOR \
5367    : binops_by_token[token->type].prec)
5368
5369 static tree
5370 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5371 {
5372   cp_parser_expression_stack stack;
5373   cp_parser_expression_stack_entry *sp = &stack[0];
5374   tree lhs, rhs;
5375   cp_token *token;
5376   enum tree_code tree_type;
5377   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5378   bool overloaded_p;
5379
5380   /* Parse the first expression.  */
5381   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5382
5383   for (;;)
5384     {
5385       /* Get an operator token.  */
5386       token = cp_lexer_peek_token (parser->lexer);
5387       new_prec = TOKEN_PRECEDENCE (token);
5388
5389       /* Popping an entry off the stack means we completed a subexpression:
5390          - either we found a token which is not an operator (`>' where it is not
5391            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5392            will happen repeatedly;
5393          - or, we found an operator which has lower priority.  This is the case 
5394            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5395            parsing `3 * 4'.  */
5396       if (new_prec <= prec)
5397         {
5398           if (sp == stack)
5399             break;
5400           else
5401             goto pop;
5402         }
5403
5404      get_rhs:
5405       tree_type = binops_by_token[token->type].tree_type;
5406
5407       /* We used the operator token.  */
5408       cp_lexer_consume_token (parser->lexer);
5409
5410       /* Extract another operand.  It may be the RHS of this expression
5411          or the LHS of a new, higher priority expression.  */
5412       rhs = cp_parser_simple_cast_expression (parser);
5413
5414       /* Get another operator token.  Look up its precedence to avoid
5415          building a useless (immediately popped) stack entry for common
5416          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5417       token = cp_lexer_peek_token (parser->lexer);
5418       lookahead_prec = TOKEN_PRECEDENCE (token);
5419       if (lookahead_prec > new_prec)
5420         {
5421           /* ... and prepare to parse the RHS of the new, higher priority
5422              expression.  Since precedence levels on the stack are
5423              monotonically increasing, we do not have to care about
5424              stack overflows.  */
5425           sp->prec = prec;
5426           sp->tree_type = tree_type;
5427           sp->lhs = lhs;
5428           sp++;
5429           lhs = rhs;
5430           prec = new_prec;
5431           new_prec = lookahead_prec;
5432           goto get_rhs;
5433
5434          pop:
5435           /* If the stack is not empty, we have parsed into LHS the right side
5436              (`4' in the example above) of an expression we had suspended.
5437              We can use the information on the stack to recover the LHS (`3') 
5438              from the stack together with the tree code (`MULT_EXPR'), and
5439              the precedence of the higher level subexpression
5440              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5441              which will be used to actually build the additive expression.  */
5442           --sp;
5443           prec = sp->prec;
5444           tree_type = sp->tree_type;
5445           rhs = lhs;
5446           lhs = sp->lhs;
5447         }
5448
5449       overloaded_p = false;
5450       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5451
5452       /* If the binary operator required the use of an overloaded operator,
5453          then this expression cannot be an integral constant-expression.
5454          An overloaded operator can be used even if both operands are
5455          otherwise permissible in an integral constant-expression if at
5456          least one of the operands is of enumeration type.  */
5457
5458       if (overloaded_p
5459           && (cp_parser_non_integral_constant_expression 
5460               (parser, "calls to overloaded operators")))
5461         return error_mark_node;
5462     }
5463
5464   return lhs;
5465 }
5466
5467
5468 /* Parse the `? expression : assignment-expression' part of a
5469    conditional-expression.  The LOGICAL_OR_EXPR is the
5470    logical-or-expression that started the conditional-expression.
5471    Returns a representation of the entire conditional-expression.
5472
5473    This routine is used by cp_parser_assignment_expression.
5474
5475      ? expression : assignment-expression
5476
5477    GNU Extensions:
5478
5479      ? : assignment-expression */
5480
5481 static tree
5482 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5483 {
5484   tree expr;
5485   tree assignment_expr;
5486
5487   /* Consume the `?' token.  */
5488   cp_lexer_consume_token (parser->lexer);
5489   if (cp_parser_allow_gnu_extensions_p (parser)
5490       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5491     /* Implicit true clause.  */
5492     expr = NULL_TREE;
5493   else
5494     /* Parse the expression.  */
5495     expr = cp_parser_expression (parser, /*cast_p=*/false);
5496
5497   /* The next token should be a `:'.  */
5498   cp_parser_require (parser, CPP_COLON, "`:'");
5499   /* Parse the assignment-expression.  */
5500   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5501
5502   /* Build the conditional-expression.  */
5503   return build_x_conditional_expr (logical_or_expr,
5504                                    expr,
5505                                    assignment_expr);
5506 }
5507
5508 /* Parse an assignment-expression.
5509
5510    assignment-expression:
5511      conditional-expression
5512      logical-or-expression assignment-operator assignment_expression
5513      throw-expression
5514
5515    CAST_P is true if this expression is the target of a cast.
5516
5517    Returns a representation for the expression.  */
5518
5519 static tree
5520 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5521 {
5522   tree expr;
5523
5524   /* If the next token is the `throw' keyword, then we're looking at
5525      a throw-expression.  */
5526   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5527     expr = cp_parser_throw_expression (parser);
5528   /* Otherwise, it must be that we are looking at a
5529      logical-or-expression.  */
5530   else
5531     {
5532       /* Parse the binary expressions (logical-or-expression).  */
5533       expr = cp_parser_binary_expression (parser, cast_p);
5534       /* If the next token is a `?' then we're actually looking at a
5535          conditional-expression.  */
5536       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5537         return cp_parser_question_colon_clause (parser, expr);
5538       else
5539         {
5540           enum tree_code assignment_operator;
5541
5542           /* If it's an assignment-operator, we're using the second
5543              production.  */
5544           assignment_operator
5545             = cp_parser_assignment_operator_opt (parser);
5546           if (assignment_operator != ERROR_MARK)
5547             {
5548               tree rhs;
5549
5550               /* Parse the right-hand side of the assignment.  */
5551               rhs = cp_parser_assignment_expression (parser, cast_p);
5552               /* An assignment may not appear in a
5553                  constant-expression.  */
5554               if (cp_parser_non_integral_constant_expression (parser,
5555                                                               "an assignment"))
5556                 return error_mark_node;
5557               /* Build the assignment expression.  */
5558               expr = build_x_modify_expr (expr,
5559                                           assignment_operator,
5560                                           rhs);
5561             }
5562         }
5563     }
5564
5565   return expr;
5566 }
5567
5568 /* Parse an (optional) assignment-operator.
5569
5570    assignment-operator: one of
5571      = *= /= %= += -= >>= <<= &= ^= |=
5572
5573    GNU Extension:
5574
5575    assignment-operator: one of
5576      <?= >?=
5577
5578    If the next token is an assignment operator, the corresponding tree
5579    code is returned, and the token is consumed.  For example, for
5580    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5581    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5582    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5583    operator, ERROR_MARK is returned.  */
5584
5585 static enum tree_code
5586 cp_parser_assignment_operator_opt (cp_parser* parser)
5587 {
5588   enum tree_code op;
5589   cp_token *token;
5590
5591   /* Peek at the next toen.  */
5592   token = cp_lexer_peek_token (parser->lexer);
5593
5594   switch (token->type)
5595     {
5596     case CPP_EQ:
5597       op = NOP_EXPR;
5598       break;
5599
5600     case CPP_MULT_EQ:
5601       op = MULT_EXPR;
5602       break;
5603
5604     case CPP_DIV_EQ:
5605       op = TRUNC_DIV_EXPR;
5606       break;
5607
5608     case CPP_MOD_EQ:
5609       op = TRUNC_MOD_EXPR;
5610       break;
5611
5612     case CPP_PLUS_EQ:
5613       op = PLUS_EXPR;
5614       break;
5615
5616     case CPP_MINUS_EQ:
5617       op = MINUS_EXPR;
5618       break;
5619
5620     case CPP_RSHIFT_EQ:
5621       op = RSHIFT_EXPR;
5622       break;
5623
5624     case CPP_LSHIFT_EQ:
5625       op = LSHIFT_EXPR;
5626       break;
5627
5628     case CPP_AND_EQ:
5629       op = BIT_AND_EXPR;
5630       break;
5631
5632     case CPP_XOR_EQ:
5633       op = BIT_XOR_EXPR;
5634       break;
5635
5636     case CPP_OR_EQ:
5637       op = BIT_IOR_EXPR;
5638       break;
5639
5640     case CPP_MIN_EQ:
5641       op = MIN_EXPR;
5642       break;
5643
5644     case CPP_MAX_EQ:
5645       op = MAX_EXPR;
5646       break;
5647
5648     default:
5649       /* Nothing else is an assignment operator.  */
5650       op = ERROR_MARK;
5651     }
5652
5653   /* If it was an assignment operator, consume it.  */
5654   if (op != ERROR_MARK)
5655     cp_lexer_consume_token (parser->lexer);
5656
5657   return op;
5658 }
5659
5660 /* Parse an expression.
5661
5662    expression:
5663      assignment-expression
5664      expression , assignment-expression
5665
5666    CAST_P is true if this expression is the target of a cast.
5667
5668    Returns a representation of the expression.  */
5669
5670 static tree
5671 cp_parser_expression (cp_parser* parser, bool cast_p)
5672 {
5673   tree expression = NULL_TREE;
5674
5675   while (true)
5676     {
5677       tree assignment_expression;
5678
5679       /* Parse the next assignment-expression.  */
5680       assignment_expression
5681         = cp_parser_assignment_expression (parser, cast_p);
5682       /* If this is the first assignment-expression, we can just
5683          save it away.  */
5684       if (!expression)
5685         expression = assignment_expression;
5686       else
5687         expression = build_x_compound_expr (expression,
5688                                             assignment_expression);
5689       /* If the next token is not a comma, then we are done with the
5690          expression.  */
5691       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5692         break;
5693       /* Consume the `,'.  */
5694       cp_lexer_consume_token (parser->lexer);
5695       /* A comma operator cannot appear in a constant-expression.  */
5696       if (cp_parser_non_integral_constant_expression (parser,
5697                                                       "a comma operator"))
5698         expression = error_mark_node;
5699     }
5700
5701   return expression;
5702 }
5703
5704 /* Parse a constant-expression.
5705
5706    constant-expression:
5707      conditional-expression
5708
5709   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5710   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5711   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5712   is false, NON_CONSTANT_P should be NULL.  */
5713
5714 static tree
5715 cp_parser_constant_expression (cp_parser* parser,
5716                                bool allow_non_constant_p,
5717                                bool *non_constant_p)
5718 {
5719   bool saved_integral_constant_expression_p;
5720   bool saved_allow_non_integral_constant_expression_p;
5721   bool saved_non_integral_constant_expression_p;
5722   tree expression;
5723
5724   /* It might seem that we could simply parse the
5725      conditional-expression, and then check to see if it were
5726      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5727      one that the compiler can figure out is constant, possibly after
5728      doing some simplifications or optimizations.  The standard has a
5729      precise definition of constant-expression, and we must honor
5730      that, even though it is somewhat more restrictive.
5731
5732      For example:
5733
5734        int i[(2, 3)];
5735
5736      is not a legal declaration, because `(2, 3)' is not a
5737      constant-expression.  The `,' operator is forbidden in a
5738      constant-expression.  However, GCC's constant-folding machinery
5739      will fold this operation to an INTEGER_CST for `3'.  */
5740
5741   /* Save the old settings.  */
5742   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5743   saved_allow_non_integral_constant_expression_p
5744     = parser->allow_non_integral_constant_expression_p;
5745   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5746   /* We are now parsing a constant-expression.  */
5747   parser->integral_constant_expression_p = true;
5748   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5749   parser->non_integral_constant_expression_p = false;
5750   /* Although the grammar says "conditional-expression", we parse an
5751      "assignment-expression", which also permits "throw-expression"
5752      and the use of assignment operators.  In the case that
5753      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5754      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5755      actually essential that we look for an assignment-expression.
5756      For example, cp_parser_initializer_clauses uses this function to
5757      determine whether a particular assignment-expression is in fact
5758      constant.  */
5759   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5760   /* Restore the old settings.  */
5761   parser->integral_constant_expression_p 
5762     = saved_integral_constant_expression_p;
5763   parser->allow_non_integral_constant_expression_p
5764     = saved_allow_non_integral_constant_expression_p;
5765   if (allow_non_constant_p)
5766     *non_constant_p = parser->non_integral_constant_expression_p;
5767   else if (parser->non_integral_constant_expression_p)
5768     expression = error_mark_node;
5769   parser->non_integral_constant_expression_p 
5770     = saved_non_integral_constant_expression_p;
5771
5772   return expression;
5773 }
5774
5775 /* Parse __builtin_offsetof.
5776
5777    offsetof-expression:
5778      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5779
5780    offsetof-member-designator:
5781      id-expression
5782      | offsetof-member-designator "." id-expression
5783      | offsetof-member-designator "[" expression "]"
5784 */
5785
5786 static tree
5787 cp_parser_builtin_offsetof (cp_parser *parser)
5788 {
5789   int save_ice_p, save_non_ice_p;
5790   tree type, expr;
5791   cp_id_kind dummy;
5792
5793   /* We're about to accept non-integral-constant things, but will
5794      definitely yield an integral constant expression.  Save and
5795      restore these values around our local parsing.  */
5796   save_ice_p = parser->integral_constant_expression_p;
5797   save_non_ice_p = parser->non_integral_constant_expression_p;
5798
5799   /* Consume the "__builtin_offsetof" token.  */
5800   cp_lexer_consume_token (parser->lexer);
5801   /* Consume the opening `('.  */
5802   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5803   /* Parse the type-id.  */
5804   type = cp_parser_type_id (parser);
5805   /* Look for the `,'.  */
5806   cp_parser_require (parser, CPP_COMMA, "`,'");
5807
5808   /* Build the (type *)null that begins the traditional offsetof macro.  */
5809   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5810
5811   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5812   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5813                                                  true, &dummy);
5814   while (true)
5815     {
5816       cp_token *token = cp_lexer_peek_token (parser->lexer);
5817       switch (token->type)
5818         {
5819         case CPP_OPEN_SQUARE:
5820           /* offsetof-member-designator "[" expression "]" */
5821           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5822           break;
5823
5824         case CPP_DOT:
5825           /* offsetof-member-designator "." identifier */
5826           cp_lexer_consume_token (parser->lexer);
5827           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5828                                                          true, &dummy);
5829           break;
5830
5831         case CPP_CLOSE_PAREN:
5832           /* Consume the ")" token.  */
5833           cp_lexer_consume_token (parser->lexer);
5834           goto success;
5835
5836         default:
5837           /* Error.  We know the following require will fail, but
5838              that gives the proper error message.  */
5839           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5840           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5841           expr = error_mark_node;
5842           goto failure;
5843         }
5844     }
5845
5846  success:
5847   /* If we're processing a template, we can't finish the semantics yet.
5848      Otherwise we can fold the entire expression now.  */
5849   if (processing_template_decl)
5850     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5851   else
5852     expr = fold_offsetof (expr);
5853
5854  failure:
5855   parser->integral_constant_expression_p = save_ice_p;
5856   parser->non_integral_constant_expression_p = save_non_ice_p;
5857
5858   return expr;
5859 }
5860
5861 /* Statements [gram.stmt.stmt]  */
5862
5863 /* Parse a statement.
5864
5865    statement:
5866      labeled-statement
5867      expression-statement
5868      compound-statement
5869      selection-statement
5870      iteration-statement
5871      jump-statement
5872      declaration-statement
5873      try-block  */
5874
5875 static void
5876 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5877 {
5878   tree statement;
5879   cp_token *token;
5880   location_t statement_location;
5881
5882   /* There is no statement yet.  */
5883   statement = NULL_TREE;
5884   /* Peek at the next token.  */
5885   token = cp_lexer_peek_token (parser->lexer);
5886   /* Remember the location of the first token in the statement.  */
5887   statement_location = token->location;
5888   /* If this is a keyword, then that will often determine what kind of
5889      statement we have.  */
5890   if (token->type == CPP_KEYWORD)
5891     {
5892       enum rid keyword = token->keyword;
5893
5894       switch (keyword)
5895         {
5896         case RID_CASE:
5897         case RID_DEFAULT:
5898           statement = cp_parser_labeled_statement (parser,
5899                                                    in_statement_expr);
5900           break;
5901
5902         case RID_IF:
5903         case RID_SWITCH:
5904           statement = cp_parser_selection_statement (parser);
5905           break;
5906
5907         case RID_WHILE:
5908         case RID_DO:
5909         case RID_FOR:
5910           statement = cp_parser_iteration_statement (parser);
5911           break;
5912
5913         case RID_BREAK:
5914         case RID_CONTINUE:
5915         case RID_RETURN:
5916         case RID_GOTO:
5917           statement = cp_parser_jump_statement (parser);
5918           break;
5919
5920         case RID_TRY:
5921           statement = cp_parser_try_block (parser);
5922           break;
5923
5924         default:
5925           /* It might be a keyword like `int' that can start a
5926              declaration-statement.  */
5927           break;
5928         }
5929     }
5930   else if (token->type == CPP_NAME)
5931     {
5932       /* If the next token is a `:', then we are looking at a
5933          labeled-statement.  */
5934       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5935       if (token->type == CPP_COLON)
5936         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5937     }
5938   /* Anything that starts with a `{' must be a compound-statement.  */
5939   else if (token->type == CPP_OPEN_BRACE)
5940     statement = cp_parser_compound_statement (parser, NULL, false);
5941   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5942      a statement all its own.  */
5943   else if (token->type == CPP_PRAGMA)
5944     {
5945       cp_lexer_handle_pragma (parser->lexer);
5946       return;
5947     }
5948
5949   /* Everything else must be a declaration-statement or an
5950      expression-statement.  Try for the declaration-statement
5951      first, unless we are looking at a `;', in which case we know that
5952      we have an expression-statement.  */
5953   if (!statement)
5954     {
5955       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5956         {
5957           cp_parser_parse_tentatively (parser);
5958           /* Try to parse the declaration-statement.  */
5959           cp_parser_declaration_statement (parser);
5960           /* If that worked, we're done.  */
5961           if (cp_parser_parse_definitely (parser))
5962             return;
5963         }
5964       /* Look for an expression-statement instead.  */
5965       statement = cp_parser_expression_statement (parser, in_statement_expr);
5966     }
5967
5968   /* Set the line number for the statement.  */
5969   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5970     SET_EXPR_LOCATION (statement, statement_location);
5971 }
5972
5973 /* Parse a labeled-statement.
5974
5975    labeled-statement:
5976      identifier : statement
5977      case constant-expression : statement
5978      default : statement
5979
5980    GNU Extension:
5981
5982    labeled-statement:
5983      case constant-expression ... constant-expression : statement
5984
5985    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5986    For an ordinary label, returns a LABEL_EXPR.  */
5987
5988 static tree
5989 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5990 {
5991   cp_token *token;
5992   tree statement = error_mark_node;
5993
5994   /* The next token should be an identifier.  */
5995   token = cp_lexer_peek_token (parser->lexer);
5996   if (token->type != CPP_NAME
5997       && token->type != CPP_KEYWORD)
5998     {
5999       cp_parser_error (parser, "expected labeled-statement");
6000       return error_mark_node;
6001     }
6002
6003   switch (token->keyword)
6004     {
6005     case RID_CASE:
6006       {
6007         tree expr, expr_hi;
6008         cp_token *ellipsis;
6009
6010         /* Consume the `case' token.  */
6011         cp_lexer_consume_token (parser->lexer);
6012         /* Parse the constant-expression.  */
6013         expr = cp_parser_constant_expression (parser,
6014                                               /*allow_non_constant_p=*/false,
6015                                               NULL);
6016
6017         ellipsis = cp_lexer_peek_token (parser->lexer);
6018         if (ellipsis->type == CPP_ELLIPSIS)
6019           {
6020             /* Consume the `...' token.  */
6021             cp_lexer_consume_token (parser->lexer);
6022             expr_hi =
6023               cp_parser_constant_expression (parser,
6024                                              /*allow_non_constant_p=*/false,
6025                                              NULL);
6026             /* We don't need to emit warnings here, as the common code
6027                will do this for us.  */
6028           }
6029         else
6030           expr_hi = NULL_TREE;
6031
6032         if (!parser->in_switch_statement_p)
6033           error ("case label %qE not within a switch statement", expr);
6034         else
6035           statement = finish_case_label (expr, expr_hi);
6036       }
6037       break;
6038
6039     case RID_DEFAULT:
6040       /* Consume the `default' token.  */
6041       cp_lexer_consume_token (parser->lexer);
6042       if (!parser->in_switch_statement_p)
6043         error ("case label not within a switch statement");
6044       else
6045         statement = finish_case_label (NULL_TREE, NULL_TREE);
6046       break;
6047
6048     default:
6049       /* Anything else must be an ordinary label.  */
6050       statement = finish_label_stmt (cp_parser_identifier (parser));
6051       break;
6052     }
6053
6054   /* Require the `:' token.  */
6055   cp_parser_require (parser, CPP_COLON, "`:'");
6056   /* Parse the labeled statement.  */
6057   cp_parser_statement (parser, in_statement_expr);
6058
6059   /* Return the label, in the case of a `case' or `default' label.  */
6060   return statement;
6061 }
6062
6063 /* Parse an expression-statement.
6064
6065    expression-statement:
6066      expression [opt] ;
6067
6068    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6069    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6070    indicates whether this expression-statement is part of an
6071    expression statement.  */
6072
6073 static tree
6074 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6075 {
6076   tree statement = NULL_TREE;
6077
6078   /* If the next token is a ';', then there is no expression
6079      statement.  */
6080   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6081     statement = cp_parser_expression (parser, /*cast_p=*/false);
6082
6083   /* Consume the final `;'.  */
6084   cp_parser_consume_semicolon_at_end_of_statement (parser);
6085
6086   if (in_statement_expr
6087       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6088     /* This is the final expression statement of a statement
6089        expression.  */
6090     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6091   else if (statement)
6092     statement = finish_expr_stmt (statement);
6093   else
6094     finish_stmt ();
6095
6096   return statement;
6097 }
6098
6099 /* Parse a compound-statement.
6100
6101    compound-statement:
6102      { statement-seq [opt] }
6103
6104    Returns a tree representing the statement.  */
6105
6106 static tree
6107 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6108                               bool in_try)
6109 {
6110   tree compound_stmt;
6111
6112   /* Consume the `{'.  */
6113   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6114     return error_mark_node;
6115   /* Begin the compound-statement.  */
6116   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6117   /* Parse an (optional) statement-seq.  */
6118   cp_parser_statement_seq_opt (parser, in_statement_expr);
6119   /* Finish the compound-statement.  */
6120   finish_compound_stmt (compound_stmt);
6121   /* Consume the `}'.  */
6122   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6123
6124   return compound_stmt;
6125 }
6126
6127 /* Parse an (optional) statement-seq.
6128
6129    statement-seq:
6130      statement
6131      statement-seq [opt] statement  */
6132
6133 static void
6134 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6135 {
6136   /* Scan statements until there aren't any more.  */
6137   while (true)
6138     {
6139       /* If we're looking at a `}', then we've run out of statements.  */
6140       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6141           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6142         break;
6143
6144       /* Parse the statement.  */
6145       cp_parser_statement (parser, in_statement_expr);
6146     }
6147 }
6148
6149 /* Parse a selection-statement.
6150
6151    selection-statement:
6152      if ( condition ) statement
6153      if ( condition ) statement else statement
6154      switch ( condition ) statement
6155
6156    Returns the new IF_STMT or SWITCH_STMT.  */
6157
6158 static tree
6159 cp_parser_selection_statement (cp_parser* parser)
6160 {
6161   cp_token *token;
6162   enum rid keyword;
6163
6164   /* Peek at the next token.  */
6165   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6166
6167   /* See what kind of keyword it is.  */
6168   keyword = token->keyword;
6169   switch (keyword)
6170     {
6171     case RID_IF:
6172     case RID_SWITCH:
6173       {
6174         tree statement;
6175         tree condition;
6176
6177         /* Look for the `('.  */
6178         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6179           {
6180             cp_parser_skip_to_end_of_statement (parser);
6181             return error_mark_node;
6182           }
6183
6184         /* Begin the selection-statement.  */
6185         if (keyword == RID_IF)
6186           statement = begin_if_stmt ();
6187         else
6188           statement = begin_switch_stmt ();
6189
6190         /* Parse the condition.  */
6191         condition = cp_parser_condition (parser);
6192         /* Look for the `)'.  */
6193         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6194           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6195                                                  /*consume_paren=*/true);
6196
6197         if (keyword == RID_IF)
6198           {
6199             /* Add the condition.  */
6200             finish_if_stmt_cond (condition, statement);
6201
6202             /* Parse the then-clause.  */
6203             cp_parser_implicitly_scoped_statement (parser);
6204             finish_then_clause (statement);
6205
6206             /* If the next token is `else', parse the else-clause.  */
6207             if (cp_lexer_next_token_is_keyword (parser->lexer,
6208                                                 RID_ELSE))
6209               {
6210                 /* Consume the `else' keyword.  */
6211                 cp_lexer_consume_token (parser->lexer);
6212                 begin_else_clause (statement);
6213                 /* Parse the else-clause.  */
6214                 cp_parser_implicitly_scoped_statement (parser);
6215                 finish_else_clause (statement);
6216               }
6217
6218             /* Now we're all done with the if-statement.  */
6219             finish_if_stmt (statement);
6220           }
6221         else
6222           {
6223             bool in_switch_statement_p;
6224
6225             /* Add the condition.  */
6226             finish_switch_cond (condition, statement);
6227
6228             /* Parse the body of the switch-statement.  */
6229             in_switch_statement_p = parser->in_switch_statement_p;
6230             parser->in_switch_statement_p = true;
6231             cp_parser_implicitly_scoped_statement (parser);
6232             parser->in_switch_statement_p = in_switch_statement_p;
6233
6234             /* Now we're all done with the switch-statement.  */
6235             finish_switch_stmt (statement);
6236           }
6237
6238         return statement;
6239       }
6240       break;
6241
6242     default:
6243       cp_parser_error (parser, "expected selection-statement");
6244       return error_mark_node;
6245     }
6246 }
6247
6248 /* Parse a condition.
6249
6250    condition:
6251      expression
6252      type-specifier-seq declarator = assignment-expression
6253
6254    GNU Extension:
6255
6256    condition:
6257      type-specifier-seq declarator asm-specification [opt]
6258        attributes [opt] = assignment-expression
6259
6260    Returns the expression that should be tested.  */
6261
6262 static tree
6263 cp_parser_condition (cp_parser* parser)
6264 {
6265   cp_decl_specifier_seq type_specifiers;
6266   const char *saved_message;
6267
6268   /* Try the declaration first.  */
6269   cp_parser_parse_tentatively (parser);
6270   /* New types are not allowed in the type-specifier-seq for a
6271      condition.  */
6272   saved_message = parser->type_definition_forbidden_message;
6273   parser->type_definition_forbidden_message
6274     = "types may not be defined in conditions";
6275   /* Parse the type-specifier-seq.  */
6276   cp_parser_type_specifier_seq (parser, &type_specifiers);
6277   /* Restore the saved message.  */
6278   parser->type_definition_forbidden_message = saved_message;
6279   /* If all is well, we might be looking at a declaration.  */
6280   if (!cp_parser_error_occurred (parser))
6281     {
6282       tree decl;
6283       tree asm_specification;
6284       tree attributes;
6285       cp_declarator *declarator;
6286       tree initializer = NULL_TREE;
6287
6288       /* Parse the declarator.  */
6289       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6290                                          /*ctor_dtor_or_conv_p=*/NULL,
6291                                          /*parenthesized_p=*/NULL,
6292                                          /*member_p=*/false);
6293       /* Parse the attributes.  */
6294       attributes = cp_parser_attributes_opt (parser);
6295       /* Parse the asm-specification.  */
6296       asm_specification = cp_parser_asm_specification_opt (parser);
6297       /* If the next token is not an `=', then we might still be
6298          looking at an expression.  For example:
6299
6300            if (A(a).x)
6301
6302          looks like a decl-specifier-seq and a declarator -- but then
6303          there is no `=', so this is an expression.  */
6304       cp_parser_require (parser, CPP_EQ, "`='");
6305       /* If we did see an `=', then we are looking at a declaration
6306          for sure.  */
6307       if (cp_parser_parse_definitely (parser))
6308         {
6309           tree pushed_scope;    
6310
6311           /* Create the declaration.  */
6312           decl = start_decl (declarator, &type_specifiers,
6313                              /*initialized_p=*/true,
6314                              attributes, /*prefix_attributes=*/NULL_TREE,
6315                              &pushed_scope);
6316           /* Parse the assignment-expression.  */
6317           initializer = cp_parser_assignment_expression (parser,
6318                                                          /*cast_p=*/false);
6319
6320           /* Process the initializer.  */
6321           cp_finish_decl (decl,
6322                           initializer,
6323                           asm_specification,
6324                           LOOKUP_ONLYCONVERTING);
6325
6326           if (pushed_scope)
6327             pop_scope (pushed_scope);
6328
6329           return convert_from_reference (decl);
6330         }
6331     }
6332   /* If we didn't even get past the declarator successfully, we are
6333      definitely not looking at a declaration.  */
6334   else
6335     cp_parser_abort_tentative_parse (parser);
6336
6337   /* Otherwise, we are looking at an expression.  */
6338   return cp_parser_expression (parser, /*cast_p=*/false);
6339 }
6340
6341 /* Parse an iteration-statement.
6342
6343    iteration-statement:
6344      while ( condition ) statement
6345      do statement while ( expression ) ;
6346      for ( for-init-statement condition [opt] ; expression [opt] )
6347        statement
6348
6349    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6350
6351 static tree
6352 cp_parser_iteration_statement (cp_parser* parser)
6353 {
6354   cp_token *token;
6355   enum rid keyword;
6356   tree statement;
6357   bool in_iteration_statement_p;
6358
6359
6360   /* Peek at the next token.  */
6361   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6362   if (!token)
6363     return error_mark_node;
6364
6365   /* Remember whether or not we are already within an iteration
6366      statement.  */
6367   in_iteration_statement_p = parser->in_iteration_statement_p;
6368
6369   /* See what kind of keyword it is.  */
6370   keyword = token->keyword;
6371   switch (keyword)
6372     {
6373     case RID_WHILE:
6374       {
6375         tree condition;
6376
6377         /* Begin the while-statement.  */
6378         statement = begin_while_stmt ();
6379         /* Look for the `('.  */
6380         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6381         /* Parse the condition.  */
6382         condition = cp_parser_condition (parser);
6383         finish_while_stmt_cond (condition, statement);
6384         /* Look for the `)'.  */
6385         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6386         /* Parse the dependent statement.  */
6387         parser->in_iteration_statement_p = true;
6388         cp_parser_already_scoped_statement (parser);
6389         parser->in_iteration_statement_p = in_iteration_statement_p;
6390         /* We're done with the while-statement.  */
6391         finish_while_stmt (statement);
6392       }
6393       break;
6394
6395     case RID_DO:
6396       {
6397         tree expression;
6398
6399         /* Begin the do-statement.  */
6400         statement = begin_do_stmt ();
6401         /* Parse the body of the do-statement.  */
6402         parser->in_iteration_statement_p = true;
6403         cp_parser_implicitly_scoped_statement (parser);
6404         parser->in_iteration_statement_p = in_iteration_statement_p;
6405         finish_do_body (statement);
6406         /* Look for the `while' keyword.  */
6407         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6408         /* Look for the `('.  */
6409         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6410         /* Parse the expression.  */
6411         expression = cp_parser_expression (parser, /*cast_p=*/false);
6412         /* We're done with the do-statement.  */
6413         finish_do_stmt (expression, statement);
6414         /* Look for the `)'.  */
6415         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6416         /* Look for the `;'.  */
6417         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6418       }
6419       break;
6420
6421     case RID_FOR:
6422       {
6423         tree condition = NULL_TREE;
6424         tree expression = NULL_TREE;
6425
6426         /* Begin the for-statement.  */
6427         statement = begin_for_stmt ();
6428         /* Look for the `('.  */
6429         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6430         /* Parse the initialization.  */
6431         cp_parser_for_init_statement (parser);
6432         finish_for_init_stmt (statement);
6433
6434         /* If there's a condition, process it.  */
6435         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6436           condition = cp_parser_condition (parser);
6437         finish_for_cond (condition, statement);
6438         /* Look for the `;'.  */
6439         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6440
6441         /* If there's an expression, process it.  */
6442         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6443           expression = cp_parser_expression (parser, /*cast_p=*/false);
6444         finish_for_expr (expression, statement);
6445         /* Look for the `)'.  */
6446         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6447
6448         /* Parse the body of the for-statement.  */
6449         parser->in_iteration_statement_p = true;
6450         cp_parser_already_scoped_statement (parser);
6451         parser->in_iteration_statement_p = in_iteration_statement_p;
6452
6453         /* We're done with the for-statement.  */
6454         finish_for_stmt (statement);
6455       }
6456       break;
6457
6458     default:
6459       cp_parser_error (parser, "expected iteration-statement");
6460       statement = error_mark_node;
6461       break;
6462     }
6463
6464   return statement;
6465 }
6466
6467 /* Parse a for-init-statement.
6468
6469    for-init-statement:
6470      expression-statement
6471      simple-declaration  */
6472
6473 static void
6474 cp_parser_for_init_statement (cp_parser* parser)
6475 {
6476   /* If the next token is a `;', then we have an empty
6477      expression-statement.  Grammatically, this is also a
6478      simple-declaration, but an invalid one, because it does not
6479      declare anything.  Therefore, if we did not handle this case
6480      specially, we would issue an error message about an invalid
6481      declaration.  */
6482   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6483     {
6484       /* We're going to speculatively look for a declaration, falling back
6485          to an expression, if necessary.  */
6486       cp_parser_parse_tentatively (parser);
6487       /* Parse the declaration.  */
6488       cp_parser_simple_declaration (parser,
6489                                     /*function_definition_allowed_p=*/false);
6490       /* If the tentative parse failed, then we shall need to look for an
6491          expression-statement.  */
6492       if (cp_parser_parse_definitely (parser))
6493         return;
6494     }
6495
6496   cp_parser_expression_statement (parser, false);
6497 }
6498
6499 /* Parse a jump-statement.
6500
6501    jump-statement:
6502      break ;
6503      continue ;
6504      return expression [opt] ;
6505      goto identifier ;
6506
6507    GNU extension:
6508
6509    jump-statement:
6510      goto * expression ;
6511
6512    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6513
6514 static tree
6515 cp_parser_jump_statement (cp_parser* parser)
6516 {
6517   tree statement = error_mark_node;
6518   cp_token *token;
6519   enum rid keyword;
6520
6521   /* Peek at the next token.  */
6522   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6523   if (!token)
6524     return error_mark_node;
6525
6526   /* See what kind of keyword it is.  */
6527   keyword = token->keyword;
6528   switch (keyword)
6529     {
6530     case RID_BREAK:
6531       if (!parser->in_switch_statement_p
6532           && !parser->in_iteration_statement_p)
6533         {
6534           error ("break statement not within loop or switch");
6535           statement = error_mark_node;
6536         }
6537       else
6538         statement = finish_break_stmt ();
6539       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6540       break;
6541
6542     case RID_CONTINUE:
6543       if (!parser->in_iteration_statement_p)
6544         {
6545           error ("continue statement not within a loop");
6546           statement = error_mark_node;
6547         }
6548       else
6549         statement = finish_continue_stmt ();
6550       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6551       break;
6552
6553     case RID_RETURN:
6554       {
6555         tree expr;
6556
6557         /* If the next token is a `;', then there is no
6558            expression.  */
6559         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6560           expr = cp_parser_expression (parser, /*cast_p=*/false);
6561         else
6562           expr = NULL_TREE;
6563         /* Build the return-statement.  */
6564         statement = finish_return_stmt (expr);
6565         /* Look for the final `;'.  */
6566         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6567       }
6568       break;
6569
6570     case RID_GOTO:
6571       /* Create the goto-statement.  */
6572       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6573         {
6574           /* Issue a warning about this use of a GNU extension.  */
6575           if (pedantic)
6576             pedwarn ("ISO C++ forbids computed gotos");
6577           /* Consume the '*' token.  */
6578           cp_lexer_consume_token (parser->lexer);
6579           /* Parse the dependent expression.  */
6580           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6581         }
6582       else
6583         finish_goto_stmt (cp_parser_identifier (parser));
6584       /* Look for the final `;'.  */
6585       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6586       break;
6587
6588     default:
6589       cp_parser_error (parser, "expected jump-statement");
6590       break;
6591     }
6592
6593   return statement;
6594 }
6595
6596 /* Parse a declaration-statement.
6597
6598    declaration-statement:
6599      block-declaration  */
6600
6601 static void
6602 cp_parser_declaration_statement (cp_parser* parser)
6603 {
6604   void *p;
6605
6606   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6607   p = obstack_alloc (&declarator_obstack, 0);
6608
6609  /* Parse the block-declaration.  */
6610   cp_parser_block_declaration (parser, /*statement_p=*/true);
6611
6612   /* Free any declarators allocated.  */
6613   obstack_free (&declarator_obstack, p);
6614
6615   /* Finish off the statement.  */
6616   finish_stmt ();
6617 }
6618
6619 /* Some dependent statements (like `if (cond) statement'), are
6620    implicitly in their own scope.  In other words, if the statement is
6621    a single statement (as opposed to a compound-statement), it is
6622    none-the-less treated as if it were enclosed in braces.  Any
6623    declarations appearing in the dependent statement are out of scope
6624    after control passes that point.  This function parses a statement,
6625    but ensures that is in its own scope, even if it is not a
6626    compound-statement.
6627
6628    Returns the new statement.  */
6629
6630 static tree
6631 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6632 {
6633   tree statement;
6634
6635   /* If the token is not a `{', then we must take special action.  */
6636   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6637     {
6638       /* Create a compound-statement.  */
6639       statement = begin_compound_stmt (0);
6640       /* Parse the dependent-statement.  */
6641       cp_parser_statement (parser, false);
6642       /* Finish the dummy compound-statement.  */
6643       finish_compound_stmt (statement);
6644     }
6645   /* Otherwise, we simply parse the statement directly.  */
6646   else
6647     statement = cp_parser_compound_statement (parser, NULL, false);
6648
6649   /* Return the statement.  */
6650   return statement;
6651 }
6652
6653 /* For some dependent statements (like `while (cond) statement'), we
6654    have already created a scope.  Therefore, even if the dependent
6655    statement is a compound-statement, we do not want to create another
6656    scope.  */
6657
6658 static void
6659 cp_parser_already_scoped_statement (cp_parser* parser)
6660 {
6661   /* If the token is a `{', then we must take special action.  */
6662   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6663     cp_parser_statement (parser, false);
6664   else
6665     {
6666       /* Avoid calling cp_parser_compound_statement, so that we
6667          don't create a new scope.  Do everything else by hand.  */
6668       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6669       cp_parser_statement_seq_opt (parser, false);
6670       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6671     }
6672 }
6673
6674 /* Declarations [gram.dcl.dcl] */
6675
6676 /* Parse an optional declaration-sequence.
6677
6678    declaration-seq:
6679      declaration
6680      declaration-seq declaration  */
6681
6682 static void
6683 cp_parser_declaration_seq_opt (cp_parser* parser)
6684 {
6685   while (true)
6686     {
6687       cp_token *token;
6688
6689       token = cp_lexer_peek_token (parser->lexer);
6690
6691       if (token->type == CPP_CLOSE_BRACE
6692           || token->type == CPP_EOF)
6693         break;
6694
6695       if (token->type == CPP_SEMICOLON)
6696         {
6697           /* A declaration consisting of a single semicolon is
6698              invalid.  Allow it unless we're being pedantic.  */
6699           cp_lexer_consume_token (parser->lexer);
6700           if (pedantic && !in_system_header)
6701             pedwarn ("extra %<;%>");
6702           continue;
6703         }
6704
6705       /* If we're entering or exiting a region that's implicitly
6706          extern "C", modify the lang context appropriately.  */
6707       if (!parser->implicit_extern_c && token->implicit_extern_c)
6708         {
6709           push_lang_context (lang_name_c);
6710           parser->implicit_extern_c = true;
6711         }
6712       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6713         {
6714           pop_lang_context ();
6715           parser->implicit_extern_c = false;
6716         }
6717
6718       if (token->type == CPP_PRAGMA)
6719         {
6720           /* A top-level declaration can consist solely of a #pragma.
6721              A nested declaration cannot, so this is done here and not
6722              in cp_parser_declaration.  (A #pragma at block scope is
6723              handled in cp_parser_statement.)  */
6724           cp_lexer_handle_pragma (parser->lexer);
6725           continue;
6726         }
6727
6728       /* Parse the declaration itself.  */
6729       cp_parser_declaration (parser);
6730     }
6731 }
6732
6733 /* Parse a declaration.
6734
6735    declaration:
6736      block-declaration
6737      function-definition
6738      template-declaration
6739      explicit-instantiation
6740      explicit-specialization
6741      linkage-specification
6742      namespace-definition
6743
6744    GNU extension:
6745
6746    declaration:
6747       __extension__ declaration */
6748
6749 static void
6750 cp_parser_declaration (cp_parser* parser)
6751 {
6752   cp_token token1;
6753   cp_token token2;
6754   int saved_pedantic;
6755   void *p;
6756
6757   /* Check for the `__extension__' keyword.  */
6758   if (cp_parser_extension_opt (parser, &saved_pedantic))
6759     {
6760       /* Parse the qualified declaration.  */
6761       cp_parser_declaration (parser);
6762       /* Restore the PEDANTIC flag.  */
6763       pedantic = saved_pedantic;
6764
6765       return;
6766     }
6767
6768   /* Try to figure out what kind of declaration is present.  */
6769   token1 = *cp_lexer_peek_token (parser->lexer);
6770
6771   if (token1.type != CPP_EOF)
6772     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6773
6774   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6775   p = obstack_alloc (&declarator_obstack, 0);
6776
6777   /* If the next token is `extern' and the following token is a string
6778      literal, then we have a linkage specification.  */
6779   if (token1.keyword == RID_EXTERN
6780       && cp_parser_is_string_literal (&token2))
6781     cp_parser_linkage_specification (parser);
6782   /* If the next token is `template', then we have either a template
6783      declaration, an explicit instantiation, or an explicit
6784      specialization.  */
6785   else if (token1.keyword == RID_TEMPLATE)
6786     {
6787       /* `template <>' indicates a template specialization.  */
6788       if (token2.type == CPP_LESS
6789           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6790         cp_parser_explicit_specialization (parser);
6791       /* `template <' indicates a template declaration.  */
6792       else if (token2.type == CPP_LESS)
6793         cp_parser_template_declaration (parser, /*member_p=*/false);
6794       /* Anything else must be an explicit instantiation.  */
6795       else
6796         cp_parser_explicit_instantiation (parser);
6797     }
6798   /* If the next token is `export', then we have a template
6799      declaration.  */
6800   else if (token1.keyword == RID_EXPORT)
6801     cp_parser_template_declaration (parser, /*member_p=*/false);
6802   /* If the next token is `extern', 'static' or 'inline' and the one
6803      after that is `template', we have a GNU extended explicit
6804      instantiation directive.  */
6805   else if (cp_parser_allow_gnu_extensions_p (parser)
6806            && (token1.keyword == RID_EXTERN
6807                || token1.keyword == RID_STATIC
6808                || token1.keyword == RID_INLINE)
6809            && token2.keyword == RID_TEMPLATE)
6810     cp_parser_explicit_instantiation (parser);
6811   /* If the next token is `namespace', check for a named or unnamed
6812      namespace definition.  */
6813   else if (token1.keyword == RID_NAMESPACE
6814            && (/* A named namespace definition.  */
6815                (token2.type == CPP_NAME
6816                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6817                     == CPP_OPEN_BRACE))
6818                /* An unnamed namespace definition.  */
6819                || token2.type == CPP_OPEN_BRACE))
6820     cp_parser_namespace_definition (parser);
6821   /* We must have either a block declaration or a function
6822      definition.  */
6823   else
6824     /* Try to parse a block-declaration, or a function-definition.  */
6825     cp_parser_block_declaration (parser, /*statement_p=*/false);
6826
6827   /* Free any declarators allocated.  */
6828   obstack_free (&declarator_obstack, p);
6829 }
6830
6831 /* Parse a block-declaration.
6832
6833    block-declaration:
6834      simple-declaration
6835      asm-definition
6836      namespace-alias-definition
6837      using-declaration
6838      using-directive
6839
6840    GNU Extension:
6841
6842    block-declaration:
6843      __extension__ block-declaration
6844      label-declaration
6845
6846    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6847    part of a declaration-statement.  */
6848
6849 static void
6850 cp_parser_block_declaration (cp_parser *parser,
6851                              bool      statement_p)
6852 {
6853   cp_token *token1;
6854   int saved_pedantic;
6855
6856   /* Check for the `__extension__' keyword.  */
6857   if (cp_parser_extension_opt (parser, &saved_pedantic))
6858     {
6859       /* Parse the qualified declaration.  */
6860       cp_parser_block_declaration (parser, statement_p);
6861       /* Restore the PEDANTIC flag.  */
6862       pedantic = saved_pedantic;
6863
6864       return;
6865     }
6866
6867   /* Peek at the next token to figure out which kind of declaration is
6868      present.  */
6869   token1 = cp_lexer_peek_token (parser->lexer);
6870
6871   /* If the next keyword is `asm', we have an asm-definition.  */
6872   if (token1->keyword == RID_ASM)
6873     {
6874       if (statement_p)
6875         cp_parser_commit_to_tentative_parse (parser);
6876       cp_parser_asm_definition (parser);
6877     }
6878   /* If the next keyword is `namespace', we have a
6879      namespace-alias-definition.  */
6880   else if (token1->keyword == RID_NAMESPACE)
6881     cp_parser_namespace_alias_definition (parser);
6882   /* If the next keyword is `using', we have either a
6883      using-declaration or a using-directive.  */
6884   else if (token1->keyword == RID_USING)
6885     {
6886       cp_token *token2;
6887
6888       if (statement_p)
6889         cp_parser_commit_to_tentative_parse (parser);
6890       /* If the token after `using' is `namespace', then we have a
6891          using-directive.  */
6892       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6893       if (token2->keyword == RID_NAMESPACE)
6894         cp_parser_using_directive (parser);
6895       /* Otherwise, it's a using-declaration.  */
6896       else
6897         cp_parser_using_declaration (parser);
6898     }
6899   /* If the next keyword is `__label__' we have a label declaration.  */
6900   else if (token1->keyword == RID_LABEL)
6901     {
6902       if (statement_p)
6903         cp_parser_commit_to_tentative_parse (parser);
6904       cp_parser_label_declaration (parser);
6905     }
6906   /* Anything else must be a simple-declaration.  */
6907   else
6908     cp_parser_simple_declaration (parser, !statement_p);
6909 }
6910
6911 /* Parse a simple-declaration.
6912
6913    simple-declaration:
6914      decl-specifier-seq [opt] init-declarator-list [opt] ;
6915
6916    init-declarator-list:
6917      init-declarator
6918      init-declarator-list , init-declarator
6919
6920    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6921    function-definition as a simple-declaration.  */
6922
6923 static void
6924 cp_parser_simple_declaration (cp_parser* parser,
6925                               bool function_definition_allowed_p)
6926 {
6927   cp_decl_specifier_seq decl_specifiers;
6928   int declares_class_or_enum;
6929   bool saw_declarator;
6930
6931   /* Defer access checks until we know what is being declared; the
6932      checks for names appearing in the decl-specifier-seq should be
6933      done as if we were in the scope of the thing being declared.  */
6934   push_deferring_access_checks (dk_deferred);
6935
6936   /* Parse the decl-specifier-seq.  We have to keep track of whether
6937      or not the decl-specifier-seq declares a named class or
6938      enumeration type, since that is the only case in which the
6939      init-declarator-list is allowed to be empty.
6940
6941      [dcl.dcl]
6942
6943      In a simple-declaration, the optional init-declarator-list can be
6944      omitted only when declaring a class or enumeration, that is when
6945      the decl-specifier-seq contains either a class-specifier, an
6946      elaborated-type-specifier, or an enum-specifier.  */
6947   cp_parser_decl_specifier_seq (parser,
6948                                 CP_PARSER_FLAGS_OPTIONAL,
6949                                 &decl_specifiers,
6950                                 &declares_class_or_enum);
6951   /* We no longer need to defer access checks.  */
6952   stop_deferring_access_checks ();
6953
6954   /* In a block scope, a valid declaration must always have a
6955      decl-specifier-seq.  By not trying to parse declarators, we can
6956      resolve the declaration/expression ambiguity more quickly.  */
6957   if (!function_definition_allowed_p
6958       && !decl_specifiers.any_specifiers_p)
6959     {
6960       cp_parser_error (parser, "expected declaration");
6961       goto done;
6962     }
6963
6964   /* If the next two tokens are both identifiers, the code is
6965      erroneous. The usual cause of this situation is code like:
6966
6967        T t;
6968
6969      where "T" should name a type -- but does not.  */
6970   if (!decl_specifiers.type
6971       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6972     {
6973       /* If parsing tentatively, we should commit; we really are
6974          looking at a declaration.  */
6975       cp_parser_commit_to_tentative_parse (parser);
6976       /* Give up.  */
6977       goto done;
6978     }
6979   
6980   /* If we have seen at least one decl-specifier, and the next token
6981      is not a parenthesis, then we must be looking at a declaration.
6982      (After "int (" we might be looking at a functional cast.)  */
6983   if (decl_specifiers.any_specifiers_p 
6984       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6985     cp_parser_commit_to_tentative_parse (parser);
6986
6987   /* Keep going until we hit the `;' at the end of the simple
6988      declaration.  */
6989   saw_declarator = false;
6990   while (cp_lexer_next_token_is_not (parser->lexer,
6991                                      CPP_SEMICOLON))
6992     {
6993       cp_token *token;
6994       bool function_definition_p;
6995       tree decl;
6996
6997       saw_declarator = true;
6998       /* Parse the init-declarator.  */
6999       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7000                                         function_definition_allowed_p,
7001                                         /*member_p=*/false,
7002                                         declares_class_or_enum,
7003                                         &function_definition_p);
7004       /* If an error occurred while parsing tentatively, exit quickly.
7005          (That usually happens when in the body of a function; each
7006          statement is treated as a declaration-statement until proven
7007          otherwise.)  */
7008       if (cp_parser_error_occurred (parser))
7009         goto done;
7010       /* Handle function definitions specially.  */
7011       if (function_definition_p)
7012         {
7013           /* If the next token is a `,', then we are probably
7014              processing something like:
7015
7016                void f() {}, *p;
7017
7018              which is erroneous.  */
7019           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7020             error ("mixing declarations and function-definitions is forbidden");
7021           /* Otherwise, we're done with the list of declarators.  */
7022           else
7023             {
7024               pop_deferring_access_checks ();
7025               return;
7026             }
7027         }
7028       /* The next token should be either a `,' or a `;'.  */
7029       token = cp_lexer_peek_token (parser->lexer);
7030       /* If it's a `,', there are more declarators to come.  */
7031       if (token->type == CPP_COMMA)
7032         cp_lexer_consume_token (parser->lexer);
7033       /* If it's a `;', we are done.  */
7034       else if (token->type == CPP_SEMICOLON)
7035         break;
7036       /* Anything else is an error.  */
7037       else
7038         {
7039           /* If we have already issued an error message we don't need
7040              to issue another one.  */
7041           if (decl != error_mark_node
7042               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7043             cp_parser_error (parser, "expected %<,%> or %<;%>");
7044           /* Skip tokens until we reach the end of the statement.  */
7045           cp_parser_skip_to_end_of_statement (parser);
7046           /* If the next token is now a `;', consume it.  */
7047           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7048             cp_lexer_consume_token (parser->lexer);
7049           goto done;
7050         }
7051       /* After the first time around, a function-definition is not
7052          allowed -- even if it was OK at first.  For example:
7053
7054            int i, f() {}
7055
7056          is not valid.  */
7057       function_definition_allowed_p = false;
7058     }
7059
7060   /* Issue an error message if no declarators are present, and the
7061      decl-specifier-seq does not itself declare a class or
7062      enumeration.  */
7063   if (!saw_declarator)
7064     {
7065       if (cp_parser_declares_only_class_p (parser))
7066         shadow_tag (&decl_specifiers);
7067       /* Perform any deferred access checks.  */
7068       perform_deferred_access_checks ();
7069     }
7070
7071   /* Consume the `;'.  */
7072   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7073
7074  done:
7075   pop_deferring_access_checks ();
7076 }
7077
7078 /* Parse a decl-specifier-seq.
7079
7080    decl-specifier-seq:
7081      decl-specifier-seq [opt] decl-specifier
7082
7083    decl-specifier:
7084      storage-class-specifier
7085      type-specifier
7086      function-specifier
7087      friend
7088      typedef
7089
7090    GNU Extension:
7091
7092    decl-specifier:
7093      attributes
7094
7095    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7096
7097    The parser flags FLAGS is used to control type-specifier parsing.
7098
7099    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7100    flags:
7101
7102      1: one of the decl-specifiers is an elaborated-type-specifier
7103         (i.e., a type declaration)
7104      2: one of the decl-specifiers is an enum-specifier or a
7105         class-specifier (i.e., a type definition)
7106
7107    */
7108
7109 static void
7110 cp_parser_decl_specifier_seq (cp_parser* parser,
7111                               cp_parser_flags flags,
7112                               cp_decl_specifier_seq *decl_specs,
7113                               int* declares_class_or_enum)
7114 {
7115   bool constructor_possible_p = !parser->in_declarator_p;
7116
7117   /* Clear DECL_SPECS.  */
7118   clear_decl_specs (decl_specs);
7119
7120   /* Assume no class or enumeration type is declared.  */
7121   *declares_class_or_enum = 0;
7122
7123   /* Keep reading specifiers until there are no more to read.  */
7124   while (true)
7125     {
7126       bool constructor_p;
7127       bool found_decl_spec;
7128       cp_token *token;
7129
7130       /* Peek at the next token.  */
7131       token = cp_lexer_peek_token (parser->lexer);
7132       /* Handle attributes.  */
7133       if (token->keyword == RID_ATTRIBUTE)
7134         {
7135           /* Parse the attributes.  */
7136           decl_specs->attributes
7137             = chainon (decl_specs->attributes,
7138                        cp_parser_attributes_opt (parser));
7139           continue;
7140         }
7141       /* Assume we will find a decl-specifier keyword.  */
7142       found_decl_spec = true;
7143       /* If the next token is an appropriate keyword, we can simply
7144          add it to the list.  */
7145       switch (token->keyword)
7146         {
7147           /* decl-specifier:
7148                friend  */
7149         case RID_FRIEND:
7150           if (decl_specs->specs[(int) ds_friend]++)
7151             error ("duplicate %<friend%>");
7152           /* Consume the token.  */
7153           cp_lexer_consume_token (parser->lexer);
7154           break;
7155
7156           /* function-specifier:
7157                inline
7158                virtual
7159                explicit  */
7160         case RID_INLINE:
7161         case RID_VIRTUAL:
7162         case RID_EXPLICIT:
7163           cp_parser_function_specifier_opt (parser, decl_specs);
7164           break;
7165
7166           /* decl-specifier:
7167                typedef  */
7168         case RID_TYPEDEF:
7169           ++decl_specs->specs[(int) ds_typedef];
7170           /* Consume the token.  */
7171           cp_lexer_consume_token (parser->lexer);
7172           /* A constructor declarator cannot appear in a typedef.  */
7173           constructor_possible_p = false;
7174           /* The "typedef" keyword can only occur in a declaration; we
7175              may as well commit at this point.  */
7176           cp_parser_commit_to_tentative_parse (parser);
7177           break;
7178
7179           /* storage-class-specifier:
7180                auto
7181                register
7182                static
7183                extern
7184                mutable
7185
7186              GNU Extension:
7187                thread  */
7188         case RID_AUTO:
7189           /* Consume the token.  */
7190           cp_lexer_consume_token (parser->lexer);
7191           cp_parser_set_storage_class (decl_specs, sc_auto);
7192           break;
7193         case RID_REGISTER:
7194           /* Consume the token.  */
7195           cp_lexer_consume_token (parser->lexer);
7196           cp_parser_set_storage_class (decl_specs, sc_register);
7197           break;
7198         case RID_STATIC:
7199           /* Consume the token.  */
7200           cp_lexer_consume_token (parser->lexer);
7201           if (decl_specs->specs[(int) ds_thread])
7202             {
7203               error ("%<__thread%> before %<static%>");
7204               decl_specs->specs[(int) ds_thread] = 0;
7205             }
7206           cp_parser_set_storage_class (decl_specs, sc_static);
7207           break;
7208         case RID_EXTERN:
7209           /* Consume the token.  */
7210           cp_lexer_consume_token (parser->lexer);
7211           if (decl_specs->specs[(int) ds_thread])
7212             {
7213               error ("%<__thread%> before %<extern%>");
7214               decl_specs->specs[(int) ds_thread] = 0;
7215             }
7216           cp_parser_set_storage_class (decl_specs, sc_extern);
7217           break;
7218         case RID_MUTABLE:
7219           /* Consume the token.  */
7220           cp_lexer_consume_token (parser->lexer);
7221           cp_parser_set_storage_class (decl_specs, sc_mutable);
7222           break;
7223         case RID_THREAD:
7224           /* Consume the token.  */
7225           cp_lexer_consume_token (parser->lexer);
7226           ++decl_specs->specs[(int) ds_thread];
7227           break;
7228
7229         default:
7230           /* We did not yet find a decl-specifier yet.  */
7231           found_decl_spec = false;
7232           break;
7233         }
7234
7235       /* Constructors are a special case.  The `S' in `S()' is not a
7236          decl-specifier; it is the beginning of the declarator.  */
7237       constructor_p
7238         = (!found_decl_spec
7239            && constructor_possible_p
7240            && (cp_parser_constructor_declarator_p
7241                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7242
7243       /* If we don't have a DECL_SPEC yet, then we must be looking at
7244          a type-specifier.  */
7245       if (!found_decl_spec && !constructor_p)
7246         {
7247           int decl_spec_declares_class_or_enum;
7248           bool is_cv_qualifier;
7249           tree type_spec;
7250
7251           type_spec
7252             = cp_parser_type_specifier (parser, flags,
7253                                         decl_specs,
7254                                         /*is_declaration=*/true,
7255                                         &decl_spec_declares_class_or_enum,
7256                                         &is_cv_qualifier);
7257
7258           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7259
7260           /* If this type-specifier referenced a user-defined type
7261              (a typedef, class-name, etc.), then we can't allow any
7262              more such type-specifiers henceforth.
7263
7264              [dcl.spec]
7265
7266              The longest sequence of decl-specifiers that could
7267              possibly be a type name is taken as the
7268              decl-specifier-seq of a declaration.  The sequence shall
7269              be self-consistent as described below.
7270
7271              [dcl.type]
7272
7273              As a general rule, at most one type-specifier is allowed
7274              in the complete decl-specifier-seq of a declaration.  The
7275              only exceptions are the following:
7276
7277              -- const or volatile can be combined with any other
7278                 type-specifier.
7279
7280              -- signed or unsigned can be combined with char, long,
7281                 short, or int.
7282
7283              -- ..
7284
7285              Example:
7286
7287                typedef char* Pc;
7288                void g (const int Pc);
7289
7290              Here, Pc is *not* part of the decl-specifier seq; it's
7291              the declarator.  Therefore, once we see a type-specifier
7292              (other than a cv-qualifier), we forbid any additional
7293              user-defined types.  We *do* still allow things like `int
7294              int' to be considered a decl-specifier-seq, and issue the
7295              error message later.  */
7296           if (type_spec && !is_cv_qualifier)
7297             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7298           /* A constructor declarator cannot follow a type-specifier.  */
7299           if (type_spec)
7300             {
7301               constructor_possible_p = false;
7302               found_decl_spec = true;
7303             }
7304         }
7305
7306       /* If we still do not have a DECL_SPEC, then there are no more
7307          decl-specifiers.  */
7308       if (!found_decl_spec)
7309         break;
7310
7311       decl_specs->any_specifiers_p = true;
7312       /* After we see one decl-specifier, further decl-specifiers are
7313          always optional.  */
7314       flags |= CP_PARSER_FLAGS_OPTIONAL;
7315     }
7316
7317   /* Don't allow a friend specifier with a class definition.  */
7318   if (decl_specs->specs[(int) ds_friend] != 0
7319       && (*declares_class_or_enum & 2))
7320     error ("class definition may not be declared a friend");
7321 }
7322
7323 /* Parse an (optional) storage-class-specifier.
7324
7325    storage-class-specifier:
7326      auto
7327      register
7328      static
7329      extern
7330      mutable
7331
7332    GNU Extension:
7333
7334    storage-class-specifier:
7335      thread
7336
7337    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7338
7339 static tree
7340 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7341 {
7342   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7343     {
7344     case RID_AUTO:
7345     case RID_REGISTER:
7346     case RID_STATIC:
7347     case RID_EXTERN:
7348     case RID_MUTABLE:
7349     case RID_THREAD:
7350       /* Consume the token.  */
7351       return cp_lexer_consume_token (parser->lexer)->value;
7352
7353     default:
7354       return NULL_TREE;
7355     }
7356 }
7357
7358 /* Parse an (optional) function-specifier.
7359
7360    function-specifier:
7361      inline
7362      virtual
7363      explicit
7364
7365    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7366    Updates DECL_SPECS, if it is non-NULL.  */
7367
7368 static tree
7369 cp_parser_function_specifier_opt (cp_parser* parser,
7370                                   cp_decl_specifier_seq *decl_specs)
7371 {
7372   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7373     {
7374     case RID_INLINE:
7375       if (decl_specs)
7376         ++decl_specs->specs[(int) ds_inline];
7377       break;
7378
7379     case RID_VIRTUAL:
7380       if (decl_specs)
7381         ++decl_specs->specs[(int) ds_virtual];
7382       break;
7383
7384     case RID_EXPLICIT:
7385       if (decl_specs)
7386         ++decl_specs->specs[(int) ds_explicit];
7387       break;
7388
7389     default:
7390       return NULL_TREE;
7391     }
7392
7393   /* Consume the token.  */
7394   return cp_lexer_consume_token (parser->lexer)->value;
7395 }
7396
7397 /* Parse a linkage-specification.
7398
7399    linkage-specification:
7400      extern string-literal { declaration-seq [opt] }
7401      extern string-literal declaration  */
7402
7403 static void
7404 cp_parser_linkage_specification (cp_parser* parser)
7405 {
7406   tree linkage;
7407
7408   /* Look for the `extern' keyword.  */
7409   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7410
7411   /* Look for the string-literal.  */
7412   linkage = cp_parser_string_literal (parser, false, false);
7413
7414   /* Transform the literal into an identifier.  If the literal is a
7415      wide-character string, or contains embedded NULs, then we can't
7416      handle it as the user wants.  */
7417   if (strlen (TREE_STRING_POINTER (linkage))
7418       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7419     {
7420       cp_parser_error (parser, "invalid linkage-specification");
7421       /* Assume C++ linkage.  */
7422       linkage = lang_name_cplusplus;
7423     }
7424   else
7425     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7426
7427   /* We're now using the new linkage.  */
7428   push_lang_context (linkage);
7429
7430   /* If the next token is a `{', then we're using the first
7431      production.  */
7432   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7433     {
7434       /* Consume the `{' token.  */
7435       cp_lexer_consume_token (parser->lexer);
7436       /* Parse the declarations.  */
7437       cp_parser_declaration_seq_opt (parser);
7438       /* Look for the closing `}'.  */
7439       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7440     }
7441   /* Otherwise, there's just one declaration.  */
7442   else
7443     {
7444       bool saved_in_unbraced_linkage_specification_p;
7445
7446       saved_in_unbraced_linkage_specification_p
7447         = parser->in_unbraced_linkage_specification_p;
7448       parser->in_unbraced_linkage_specification_p = true;
7449       have_extern_spec = true;
7450       cp_parser_declaration (parser);
7451       have_extern_spec = false;
7452       parser->in_unbraced_linkage_specification_p
7453         = saved_in_unbraced_linkage_specification_p;
7454     }
7455
7456   /* We're done with the linkage-specification.  */
7457   pop_lang_context ();
7458 }
7459
7460 /* Special member functions [gram.special] */
7461
7462 /* Parse a conversion-function-id.
7463
7464    conversion-function-id:
7465      operator conversion-type-id
7466
7467    Returns an IDENTIFIER_NODE representing the operator.  */
7468
7469 static tree
7470 cp_parser_conversion_function_id (cp_parser* parser)
7471 {
7472   tree type;
7473   tree saved_scope;
7474   tree saved_qualifying_scope;
7475   tree saved_object_scope;
7476   tree pushed_scope = NULL_TREE;
7477
7478   /* Look for the `operator' token.  */
7479   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7480     return error_mark_node;
7481   /* When we parse the conversion-type-id, the current scope will be
7482      reset.  However, we need that information in able to look up the
7483      conversion function later, so we save it here.  */
7484   saved_scope = parser->scope;
7485   saved_qualifying_scope = parser->qualifying_scope;
7486   saved_object_scope = parser->object_scope;
7487   /* We must enter the scope of the class so that the names of
7488      entities declared within the class are available in the
7489      conversion-type-id.  For example, consider:
7490
7491        struct S {
7492          typedef int I;
7493          operator I();
7494        };
7495
7496        S::operator I() { ... }
7497
7498      In order to see that `I' is a type-name in the definition, we
7499      must be in the scope of `S'.  */
7500   if (saved_scope)
7501     pushed_scope = push_scope (saved_scope);
7502   /* Parse the conversion-type-id.  */
7503   type = cp_parser_conversion_type_id (parser);
7504   /* Leave the scope of the class, if any.  */
7505   if (pushed_scope)
7506     pop_scope (pushed_scope);
7507   /* Restore the saved scope.  */
7508   parser->scope = saved_scope;
7509   parser->qualifying_scope = saved_qualifying_scope;
7510   parser->object_scope = saved_object_scope;
7511   /* If the TYPE is invalid, indicate failure.  */
7512   if (type == error_mark_node)
7513     return error_mark_node;
7514   return mangle_conv_op_name_for_type (type);
7515 }
7516
7517 /* Parse a conversion-type-id:
7518
7519    conversion-type-id:
7520      type-specifier-seq conversion-declarator [opt]
7521
7522    Returns the TYPE specified.  */
7523
7524 static tree
7525 cp_parser_conversion_type_id (cp_parser* parser)
7526 {
7527   tree attributes;
7528   cp_decl_specifier_seq type_specifiers;
7529   cp_declarator *declarator;
7530   tree type_specified;
7531
7532   /* Parse the attributes.  */
7533   attributes = cp_parser_attributes_opt (parser);
7534   /* Parse the type-specifiers.  */
7535   cp_parser_type_specifier_seq (parser, &type_specifiers);
7536   /* If that didn't work, stop.  */
7537   if (type_specifiers.type == error_mark_node)
7538     return error_mark_node;
7539   /* Parse the conversion-declarator.  */
7540   declarator = cp_parser_conversion_declarator_opt (parser);
7541
7542   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7543                                     /*initialized=*/0, &attributes);
7544   if (attributes)
7545     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7546   return type_specified;
7547 }
7548
7549 /* Parse an (optional) conversion-declarator.
7550
7551    conversion-declarator:
7552      ptr-operator conversion-declarator [opt]
7553
7554    */
7555
7556 static cp_declarator *
7557 cp_parser_conversion_declarator_opt (cp_parser* parser)
7558 {
7559   enum tree_code code;
7560   tree class_type;
7561   cp_cv_quals cv_quals;
7562
7563   /* We don't know if there's a ptr-operator next, or not.  */
7564   cp_parser_parse_tentatively (parser);
7565   /* Try the ptr-operator.  */
7566   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7567   /* If it worked, look for more conversion-declarators.  */
7568   if (cp_parser_parse_definitely (parser))
7569     {
7570       cp_declarator *declarator;
7571
7572       /* Parse another optional declarator.  */
7573       declarator = cp_parser_conversion_declarator_opt (parser);
7574
7575       /* Create the representation of the declarator.  */
7576       if (class_type)
7577         declarator = make_ptrmem_declarator (cv_quals, class_type,
7578                                              declarator);
7579       else if (code == INDIRECT_REF)
7580         declarator = make_pointer_declarator (cv_quals, declarator);
7581       else
7582         declarator = make_reference_declarator (cv_quals, declarator);
7583
7584       return declarator;
7585    }
7586
7587   return NULL;
7588 }
7589
7590 /* Parse an (optional) ctor-initializer.
7591
7592    ctor-initializer:
7593      : mem-initializer-list
7594
7595    Returns TRUE iff the ctor-initializer was actually present.  */
7596
7597 static bool
7598 cp_parser_ctor_initializer_opt (cp_parser* parser)
7599 {
7600   /* If the next token is not a `:', then there is no
7601      ctor-initializer.  */
7602   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7603     {
7604       /* Do default initialization of any bases and members.  */
7605       if (DECL_CONSTRUCTOR_P (current_function_decl))
7606         finish_mem_initializers (NULL_TREE);
7607
7608       return false;
7609     }
7610
7611   /* Consume the `:' token.  */
7612   cp_lexer_consume_token (parser->lexer);
7613   /* And the mem-initializer-list.  */
7614   cp_parser_mem_initializer_list (parser);
7615
7616   return true;
7617 }
7618
7619 /* Parse a mem-initializer-list.
7620
7621    mem-initializer-list:
7622      mem-initializer
7623      mem-initializer , mem-initializer-list  */
7624
7625 static void
7626 cp_parser_mem_initializer_list (cp_parser* parser)
7627 {
7628   tree mem_initializer_list = NULL_TREE;
7629
7630   /* Let the semantic analysis code know that we are starting the
7631      mem-initializer-list.  */
7632   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7633     error ("only constructors take base initializers");
7634
7635   /* Loop through the list.  */
7636   while (true)
7637     {
7638       tree mem_initializer;
7639
7640       /* Parse the mem-initializer.  */
7641       mem_initializer = cp_parser_mem_initializer (parser);
7642       /* Add it to the list, unless it was erroneous.  */
7643       if (mem_initializer)
7644         {
7645           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7646           mem_initializer_list = mem_initializer;
7647         }
7648       /* If the next token is not a `,', we're done.  */
7649       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7650         break;
7651       /* Consume the `,' token.  */
7652       cp_lexer_consume_token (parser->lexer);
7653     }
7654
7655   /* Perform semantic analysis.  */
7656   if (DECL_CONSTRUCTOR_P (current_function_decl))
7657     finish_mem_initializers (mem_initializer_list);
7658 }
7659
7660 /* Parse a mem-initializer.
7661
7662    mem-initializer:
7663      mem-initializer-id ( expression-list [opt] )
7664
7665    GNU extension:
7666
7667    mem-initializer:
7668      ( expression-list [opt] )
7669
7670    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7671    class) or FIELD_DECL (for a non-static data member) to initialize;
7672    the TREE_VALUE is the expression-list.  */
7673
7674 static tree
7675 cp_parser_mem_initializer (cp_parser* parser)
7676 {
7677   tree mem_initializer_id;
7678   tree expression_list;
7679   tree member;
7680
7681   /* Find out what is being initialized.  */
7682   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7683     {
7684       pedwarn ("anachronistic old-style base class initializer");
7685       mem_initializer_id = NULL_TREE;
7686     }
7687   else
7688     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7689   member = expand_member_init (mem_initializer_id);
7690   if (member && !DECL_P (member))
7691     in_base_initializer = 1;
7692
7693   expression_list
7694     = cp_parser_parenthesized_expression_list (parser, false,
7695                                                /*cast_p=*/false,
7696                                                /*non_constant_p=*/NULL);
7697   if (!expression_list)
7698     expression_list = void_type_node;
7699
7700   in_base_initializer = 0;
7701
7702   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7703 }
7704
7705 /* Parse a mem-initializer-id.
7706
7707    mem-initializer-id:
7708      :: [opt] nested-name-specifier [opt] class-name
7709      identifier
7710
7711    Returns a TYPE indicating the class to be initializer for the first
7712    production.  Returns an IDENTIFIER_NODE indicating the data member
7713    to be initialized for the second production.  */
7714
7715 static tree
7716 cp_parser_mem_initializer_id (cp_parser* parser)
7717 {
7718   bool global_scope_p;
7719   bool nested_name_specifier_p;
7720   bool template_p = false;
7721   tree id;
7722
7723   /* `typename' is not allowed in this context ([temp.res]).  */
7724   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7725     {
7726       error ("keyword %<typename%> not allowed in this context (a qualified "
7727              "member initializer is implicitly a type)");
7728       cp_lexer_consume_token (parser->lexer);
7729     }
7730   /* Look for the optional `::' operator.  */
7731   global_scope_p
7732     = (cp_parser_global_scope_opt (parser,
7733                                    /*current_scope_valid_p=*/false)
7734        != NULL_TREE);
7735   /* Look for the optional nested-name-specifier.  The simplest way to
7736      implement:
7737
7738        [temp.res]
7739
7740        The keyword `typename' is not permitted in a base-specifier or
7741        mem-initializer; in these contexts a qualified name that
7742        depends on a template-parameter is implicitly assumed to be a
7743        type name.
7744
7745      is to assume that we have seen the `typename' keyword at this
7746      point.  */
7747   nested_name_specifier_p
7748     = (cp_parser_nested_name_specifier_opt (parser,
7749                                             /*typename_keyword_p=*/true,
7750                                             /*check_dependency_p=*/true,
7751                                             /*type_p=*/true,
7752                                             /*is_declaration=*/true)
7753        != NULL_TREE);
7754   if (nested_name_specifier_p)
7755     template_p = cp_parser_optional_template_keyword (parser);
7756   /* If there is a `::' operator or a nested-name-specifier, then we
7757      are definitely looking for a class-name.  */
7758   if (global_scope_p || nested_name_specifier_p)
7759     return cp_parser_class_name (parser,
7760                                  /*typename_keyword_p=*/true,
7761                                  /*template_keyword_p=*/template_p,
7762                                  none_type,
7763                                  /*check_dependency_p=*/true,
7764                                  /*class_head_p=*/false,
7765                                  /*is_declaration=*/true);
7766   /* Otherwise, we could also be looking for an ordinary identifier.  */
7767   cp_parser_parse_tentatively (parser);
7768   /* Try a class-name.  */
7769   id = cp_parser_class_name (parser,
7770                              /*typename_keyword_p=*/true,
7771                              /*template_keyword_p=*/false,
7772                              none_type,
7773                              /*check_dependency_p=*/true,
7774                              /*class_head_p=*/false,
7775                              /*is_declaration=*/true);
7776   /* If we found one, we're done.  */
7777   if (cp_parser_parse_definitely (parser))
7778     return id;
7779   /* Otherwise, look for an ordinary identifier.  */
7780   return cp_parser_identifier (parser);
7781 }
7782
7783 /* Overloading [gram.over] */
7784
7785 /* Parse an operator-function-id.
7786
7787    operator-function-id:
7788      operator operator
7789
7790    Returns an IDENTIFIER_NODE for the operator which is a
7791    human-readable spelling of the identifier, e.g., `operator +'.  */
7792
7793 static tree
7794 cp_parser_operator_function_id (cp_parser* parser)
7795 {
7796   /* Look for the `operator' keyword.  */
7797   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7798     return error_mark_node;
7799   /* And then the name of the operator itself.  */
7800   return cp_parser_operator (parser);
7801 }
7802
7803 /* Parse an operator.
7804
7805    operator:
7806      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7807      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7808      || ++ -- , ->* -> () []
7809
7810    GNU Extensions:
7811
7812    operator:
7813      <? >? <?= >?=
7814
7815    Returns an IDENTIFIER_NODE for the operator which is a
7816    human-readable spelling of the identifier, e.g., `operator +'.  */
7817
7818 static tree
7819 cp_parser_operator (cp_parser* parser)
7820 {
7821   tree id = NULL_TREE;
7822   cp_token *token;
7823
7824   /* Peek at the next token.  */
7825   token = cp_lexer_peek_token (parser->lexer);
7826   /* Figure out which operator we have.  */
7827   switch (token->type)
7828     {
7829     case CPP_KEYWORD:
7830       {
7831         enum tree_code op;
7832
7833         /* The keyword should be either `new' or `delete'.  */
7834         if (token->keyword == RID_NEW)
7835           op = NEW_EXPR;
7836         else if (token->keyword == RID_DELETE)
7837           op = DELETE_EXPR;
7838         else
7839           break;
7840
7841         /* Consume the `new' or `delete' token.  */
7842         cp_lexer_consume_token (parser->lexer);
7843
7844         /* Peek at the next token.  */
7845         token = cp_lexer_peek_token (parser->lexer);
7846         /* If it's a `[' token then this is the array variant of the
7847            operator.  */
7848         if (token->type == CPP_OPEN_SQUARE)
7849           {
7850             /* Consume the `[' token.  */
7851             cp_lexer_consume_token (parser->lexer);
7852             /* Look for the `]' token.  */
7853             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7854             id = ansi_opname (op == NEW_EXPR
7855                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7856           }
7857         /* Otherwise, we have the non-array variant.  */
7858         else
7859           id = ansi_opname (op);
7860
7861         return id;
7862       }
7863
7864     case CPP_PLUS:
7865       id = ansi_opname (PLUS_EXPR);
7866       break;
7867
7868     case CPP_MINUS:
7869       id = ansi_opname (MINUS_EXPR);
7870       break;
7871
7872     case CPP_MULT:
7873       id = ansi_opname (MULT_EXPR);
7874       break;
7875
7876     case CPP_DIV:
7877       id = ansi_opname (TRUNC_DIV_EXPR);
7878       break;
7879
7880     case CPP_MOD:
7881       id = ansi_opname (TRUNC_MOD_EXPR);
7882       break;
7883
7884     case CPP_XOR:
7885       id = ansi_opname (BIT_XOR_EXPR);
7886       break;
7887
7888     case CPP_AND:
7889       id = ansi_opname (BIT_AND_EXPR);
7890       break;
7891
7892     case CPP_OR:
7893       id = ansi_opname (BIT_IOR_EXPR);
7894       break;
7895
7896     case CPP_COMPL:
7897       id = ansi_opname (BIT_NOT_EXPR);
7898       break;
7899
7900     case CPP_NOT:
7901       id = ansi_opname (TRUTH_NOT_EXPR);
7902       break;
7903
7904     case CPP_EQ:
7905       id = ansi_assopname (NOP_EXPR);
7906       break;
7907
7908     case CPP_LESS:
7909       id = ansi_opname (LT_EXPR);
7910       break;
7911
7912     case CPP_GREATER:
7913       id = ansi_opname (GT_EXPR);
7914       break;
7915
7916     case CPP_PLUS_EQ:
7917       id = ansi_assopname (PLUS_EXPR);
7918       break;
7919
7920     case CPP_MINUS_EQ:
7921       id = ansi_assopname (MINUS_EXPR);
7922       break;
7923
7924     case CPP_MULT_EQ:
7925       id = ansi_assopname (MULT_EXPR);
7926       break;
7927
7928     case CPP_DIV_EQ:
7929       id = ansi_assopname (TRUNC_DIV_EXPR);
7930       break;
7931
7932     case CPP_MOD_EQ:
7933       id = ansi_assopname (TRUNC_MOD_EXPR);
7934       break;
7935
7936     case CPP_XOR_EQ:
7937       id = ansi_assopname (BIT_XOR_EXPR);
7938       break;
7939
7940     case CPP_AND_EQ:
7941       id = ansi_assopname (BIT_AND_EXPR);
7942       break;
7943
7944     case CPP_OR_EQ:
7945       id = ansi_assopname (BIT_IOR_EXPR);
7946       break;
7947
7948     case CPP_LSHIFT:
7949       id = ansi_opname (LSHIFT_EXPR);
7950       break;
7951
7952     case CPP_RSHIFT:
7953       id = ansi_opname (RSHIFT_EXPR);
7954       break;
7955
7956     case CPP_LSHIFT_EQ:
7957       id = ansi_assopname (LSHIFT_EXPR);
7958       break;
7959
7960     case CPP_RSHIFT_EQ:
7961       id = ansi_assopname (RSHIFT_EXPR);
7962       break;
7963
7964     case CPP_EQ_EQ:
7965       id = ansi_opname (EQ_EXPR);
7966       break;
7967
7968     case CPP_NOT_EQ:
7969       id = ansi_opname (NE_EXPR);
7970       break;
7971
7972     case CPP_LESS_EQ:
7973       id = ansi_opname (LE_EXPR);
7974       break;
7975
7976     case CPP_GREATER_EQ:
7977       id = ansi_opname (GE_EXPR);
7978       break;
7979
7980     case CPP_AND_AND:
7981       id = ansi_opname (TRUTH_ANDIF_EXPR);
7982       break;
7983
7984     case CPP_OR_OR:
7985       id = ansi_opname (TRUTH_ORIF_EXPR);
7986       break;
7987
7988     case CPP_PLUS_PLUS:
7989       id = ansi_opname (POSTINCREMENT_EXPR);
7990       break;
7991
7992     case CPP_MINUS_MINUS:
7993       id = ansi_opname (PREDECREMENT_EXPR);
7994       break;
7995
7996     case CPP_COMMA:
7997       id = ansi_opname (COMPOUND_EXPR);
7998       break;
7999
8000     case CPP_DEREF_STAR:
8001       id = ansi_opname (MEMBER_REF);
8002       break;
8003
8004     case CPP_DEREF:
8005       id = ansi_opname (COMPONENT_REF);
8006       break;
8007
8008     case CPP_OPEN_PAREN:
8009       /* Consume the `('.  */
8010       cp_lexer_consume_token (parser->lexer);
8011       /* Look for the matching `)'.  */
8012       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8013       return ansi_opname (CALL_EXPR);
8014
8015     case CPP_OPEN_SQUARE:
8016       /* Consume the `['.  */
8017       cp_lexer_consume_token (parser->lexer);
8018       /* Look for the matching `]'.  */
8019       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8020       return ansi_opname (ARRAY_REF);
8021
8022       /* Extensions.  */
8023     case CPP_MIN:
8024       id = ansi_opname (MIN_EXPR);
8025       break;
8026
8027     case CPP_MAX:
8028       id = ansi_opname (MAX_EXPR);
8029       break;
8030
8031     case CPP_MIN_EQ:
8032       id = ansi_assopname (MIN_EXPR);
8033       break;
8034
8035     case CPP_MAX_EQ:
8036       id = ansi_assopname (MAX_EXPR);
8037       break;
8038
8039     default:
8040       /* Anything else is an error.  */
8041       break;
8042     }
8043
8044   /* If we have selected an identifier, we need to consume the
8045      operator token.  */
8046   if (id)
8047     cp_lexer_consume_token (parser->lexer);
8048   /* Otherwise, no valid operator name was present.  */
8049   else
8050     {
8051       cp_parser_error (parser, "expected operator");
8052       id = error_mark_node;
8053     }
8054
8055   return id;
8056 }
8057
8058 /* Parse a template-declaration.
8059
8060    template-declaration:
8061      export [opt] template < template-parameter-list > declaration
8062
8063    If MEMBER_P is TRUE, this template-declaration occurs within a
8064    class-specifier.
8065
8066    The grammar rule given by the standard isn't correct.  What
8067    is really meant is:
8068
8069    template-declaration:
8070      export [opt] template-parameter-list-seq
8071        decl-specifier-seq [opt] init-declarator [opt] ;
8072      export [opt] template-parameter-list-seq
8073        function-definition
8074
8075    template-parameter-list-seq:
8076      template-parameter-list-seq [opt]
8077      template < template-parameter-list >  */
8078
8079 static void
8080 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8081 {
8082   /* Check for `export'.  */
8083   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8084     {
8085       /* Consume the `export' token.  */
8086       cp_lexer_consume_token (parser->lexer);
8087       /* Warn that we do not support `export'.  */
8088       warning ("keyword %<export%> not implemented, and will be ignored");
8089     }
8090
8091   cp_parser_template_declaration_after_export (parser, member_p);
8092 }
8093
8094 /* Parse a template-parameter-list.
8095
8096    template-parameter-list:
8097      template-parameter
8098      template-parameter-list , template-parameter
8099
8100    Returns a TREE_LIST.  Each node represents a template parameter.
8101    The nodes are connected via their TREE_CHAINs.  */
8102
8103 static tree
8104 cp_parser_template_parameter_list (cp_parser* parser)
8105 {
8106   tree parameter_list = NULL_TREE;
8107
8108   while (true)
8109     {
8110       tree parameter;
8111       cp_token *token;
8112       bool is_non_type;
8113
8114       /* Parse the template-parameter.  */
8115       parameter = cp_parser_template_parameter (parser, &is_non_type);
8116       /* Add it to the list.  */
8117       if (parameter != error_mark_node)
8118         parameter_list = process_template_parm (parameter_list,
8119                                                 parameter,
8120                                                 is_non_type);
8121       /* Peek at the next token.  */
8122       token = cp_lexer_peek_token (parser->lexer);
8123       /* If it's not a `,', we're done.  */
8124       if (token->type != CPP_COMMA)
8125         break;
8126       /* Otherwise, consume the `,' token.  */
8127       cp_lexer_consume_token (parser->lexer);
8128     }
8129
8130   return parameter_list;
8131 }
8132
8133 /* Parse a template-parameter.
8134
8135    template-parameter:
8136      type-parameter
8137      parameter-declaration
8138
8139    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8140    the parameter.  The TREE_PURPOSE is the default value, if any.
8141    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8142    iff this parameter is a non-type parameter.  */
8143
8144 static tree
8145 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8146 {
8147   cp_token *token;
8148   cp_parameter_declarator *parameter_declarator;
8149   tree parm;
8150
8151   /* Assume it is a type parameter or a template parameter.  */
8152   *is_non_type = false;
8153   /* Peek at the next token.  */
8154   token = cp_lexer_peek_token (parser->lexer);
8155   /* If it is `class' or `template', we have a type-parameter.  */
8156   if (token->keyword == RID_TEMPLATE)
8157     return cp_parser_type_parameter (parser);
8158   /* If it is `class' or `typename' we do not know yet whether it is a
8159      type parameter or a non-type parameter.  Consider:
8160
8161        template <typename T, typename T::X X> ...
8162
8163      or:
8164
8165        template <class C, class D*> ...
8166
8167      Here, the first parameter is a type parameter, and the second is
8168      a non-type parameter.  We can tell by looking at the token after
8169      the identifier -- if it is a `,', `=', or `>' then we have a type
8170      parameter.  */
8171   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8172     {
8173       /* Peek at the token after `class' or `typename'.  */
8174       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8175       /* If it's an identifier, skip it.  */
8176       if (token->type == CPP_NAME)
8177         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8178       /* Now, see if the token looks like the end of a template
8179          parameter.  */
8180       if (token->type == CPP_COMMA
8181           || token->type == CPP_EQ
8182           || token->type == CPP_GREATER)
8183         return cp_parser_type_parameter (parser);
8184     }
8185
8186   /* Otherwise, it is a non-type parameter.
8187
8188      [temp.param]
8189
8190      When parsing a default template-argument for a non-type
8191      template-parameter, the first non-nested `>' is taken as the end
8192      of the template parameter-list rather than a greater-than
8193      operator.  */
8194   *is_non_type = true;
8195   parameter_declarator
8196      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8197                                         /*parenthesized_p=*/NULL);
8198   parm = grokdeclarator (parameter_declarator->declarator,
8199                          &parameter_declarator->decl_specifiers,
8200                          PARM, /*initialized=*/0,
8201                          /*attrlist=*/NULL);
8202   if (parm == error_mark_node)
8203     return error_mark_node;
8204   return build_tree_list (parameter_declarator->default_argument, parm);
8205 }
8206
8207 /* Parse a type-parameter.
8208
8209    type-parameter:
8210      class identifier [opt]
8211      class identifier [opt] = type-id
8212      typename identifier [opt]
8213      typename identifier [opt] = type-id
8214      template < template-parameter-list > class identifier [opt]
8215      template < template-parameter-list > class identifier [opt]
8216        = id-expression
8217
8218    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8219    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8220    the declaration of the parameter.  */
8221
8222 static tree
8223 cp_parser_type_parameter (cp_parser* parser)
8224 {
8225   cp_token *token;
8226   tree parameter;
8227
8228   /* Look for a keyword to tell us what kind of parameter this is.  */
8229   token = cp_parser_require (parser, CPP_KEYWORD,
8230                              "`class', `typename', or `template'");
8231   if (!token)
8232     return error_mark_node;
8233
8234   switch (token->keyword)
8235     {
8236     case RID_CLASS:
8237     case RID_TYPENAME:
8238       {
8239         tree identifier;
8240         tree default_argument;
8241
8242         /* If the next token is an identifier, then it names the
8243            parameter.  */
8244         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8245           identifier = cp_parser_identifier (parser);
8246         else
8247           identifier = NULL_TREE;
8248
8249         /* Create the parameter.  */
8250         parameter = finish_template_type_parm (class_type_node, identifier);
8251
8252         /* If the next token is an `=', we have a default argument.  */
8253         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8254           {
8255             /* Consume the `=' token.  */
8256             cp_lexer_consume_token (parser->lexer);
8257             /* Parse the default-argument.  */
8258             default_argument = cp_parser_type_id (parser);
8259           }
8260         else
8261           default_argument = NULL_TREE;
8262
8263         /* Create the combined representation of the parameter and the
8264            default argument.  */
8265         parameter = build_tree_list (default_argument, parameter);
8266       }
8267       break;
8268
8269     case RID_TEMPLATE:
8270       {
8271         tree parameter_list;
8272         tree identifier;
8273         tree default_argument;
8274
8275         /* Look for the `<'.  */
8276         cp_parser_require (parser, CPP_LESS, "`<'");
8277         /* Parse the template-parameter-list.  */
8278         begin_template_parm_list ();
8279         parameter_list
8280           = cp_parser_template_parameter_list (parser);
8281         parameter_list = end_template_parm_list (parameter_list);
8282         /* Look for the `>'.  */
8283         cp_parser_require (parser, CPP_GREATER, "`>'");
8284         /* Look for the `class' keyword.  */
8285         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8286         /* If the next token is an `=', then there is a
8287            default-argument.  If the next token is a `>', we are at
8288            the end of the parameter-list.  If the next token is a `,',
8289            then we are at the end of this parameter.  */
8290         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8291             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8292             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8293           {
8294             identifier = cp_parser_identifier (parser);
8295             /* Treat invalid names as if the parameter were nameless.  */
8296             if (identifier == error_mark_node)
8297               identifier = NULL_TREE;
8298           }
8299         else
8300           identifier = NULL_TREE;
8301
8302         /* Create the template parameter.  */
8303         parameter = finish_template_template_parm (class_type_node,
8304                                                    identifier);
8305
8306         /* If the next token is an `=', then there is a
8307            default-argument.  */
8308         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8309           {
8310             bool is_template;
8311
8312             /* Consume the `='.  */
8313             cp_lexer_consume_token (parser->lexer);
8314             /* Parse the id-expression.  */
8315             default_argument
8316               = cp_parser_id_expression (parser,
8317                                          /*template_keyword_p=*/false,
8318                                          /*check_dependency_p=*/true,
8319                                          /*template_p=*/&is_template,
8320                                          /*declarator_p=*/false);
8321             if (TREE_CODE (default_argument) == TYPE_DECL)
8322               /* If the id-expression was a template-id that refers to
8323                  a template-class, we already have the declaration here,
8324                  so no further lookup is needed.  */
8325                  ;
8326             else
8327               /* Look up the name.  */
8328               default_argument
8329                 = cp_parser_lookup_name (parser, default_argument,
8330                                          none_type,
8331                                          /*is_template=*/is_template,
8332                                          /*is_namespace=*/false,
8333                                          /*check_dependency=*/true,
8334                                          /*ambiguous_p=*/NULL);
8335             /* See if the default argument is valid.  */
8336             default_argument
8337               = check_template_template_default_arg (default_argument);
8338           }
8339         else
8340           default_argument = NULL_TREE;
8341
8342         /* Create the combined representation of the parameter and the
8343            default argument.  */
8344         parameter = build_tree_list (default_argument, parameter);
8345       }
8346       break;
8347
8348     default:
8349       gcc_unreachable ();
8350       break;
8351     }
8352
8353   return parameter;
8354 }
8355
8356 /* Parse a template-id.
8357
8358    template-id:
8359      template-name < template-argument-list [opt] >
8360
8361    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8362    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8363    returned.  Otherwise, if the template-name names a function, or set
8364    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8365    names a class, returns a TYPE_DECL for the specialization.
8366
8367    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8368    uninstantiated templates.  */
8369
8370 static tree
8371 cp_parser_template_id (cp_parser *parser,
8372                        bool template_keyword_p,
8373                        bool check_dependency_p,
8374                        bool is_declaration)
8375 {
8376   tree template;
8377   tree arguments;
8378   tree template_id;
8379   cp_token_position start_of_id = 0;
8380   tree access_check = NULL_TREE;
8381   cp_token *next_token, *next_token_2;
8382   bool is_identifier;
8383
8384   /* If the next token corresponds to a template-id, there is no need
8385      to reparse it.  */
8386   next_token = cp_lexer_peek_token (parser->lexer);
8387   if (next_token->type == CPP_TEMPLATE_ID)
8388     {
8389       tree value;
8390       tree check;
8391
8392       /* Get the stored value.  */
8393       value = cp_lexer_consume_token (parser->lexer)->value;
8394       /* Perform any access checks that were deferred.  */
8395       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8396         perform_or_defer_access_check (TREE_PURPOSE (check),
8397                                        TREE_VALUE (check));
8398       /* Return the stored value.  */
8399       return TREE_VALUE (value);
8400     }
8401
8402   /* Avoid performing name lookup if there is no possibility of
8403      finding a template-id.  */
8404   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8405       || (next_token->type == CPP_NAME
8406           && !cp_parser_nth_token_starts_template_argument_list_p
8407                (parser, 2)))
8408     {
8409       cp_parser_error (parser, "expected template-id");
8410       return error_mark_node;
8411     }
8412
8413   /* Remember where the template-id starts.  */
8414   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8415     start_of_id = cp_lexer_token_position (parser->lexer, false);
8416
8417   push_deferring_access_checks (dk_deferred);
8418
8419   /* Parse the template-name.  */
8420   is_identifier = false;
8421   template = cp_parser_template_name (parser, template_keyword_p,
8422                                       check_dependency_p,
8423                                       is_declaration,
8424                                       &is_identifier);
8425   if (template == error_mark_node || is_identifier)
8426     {
8427       pop_deferring_access_checks ();
8428       return template;
8429     }
8430
8431   /* If we find the sequence `[:' after a template-name, it's probably
8432      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8433      parse correctly the argument list.  */
8434   next_token = cp_lexer_peek_token (parser->lexer);
8435   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8436   if (next_token->type == CPP_OPEN_SQUARE
8437       && next_token->flags & DIGRAPH
8438       && next_token_2->type == CPP_COLON
8439       && !(next_token_2->flags & PREV_WHITE))
8440     {
8441       cp_parser_parse_tentatively (parser);
8442       /* Change `:' into `::'.  */
8443       next_token_2->type = CPP_SCOPE;
8444       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8445          CPP_LESS.  */
8446       cp_lexer_consume_token (parser->lexer);
8447       /* Parse the arguments.  */
8448       arguments = cp_parser_enclosed_template_argument_list (parser);
8449       if (!cp_parser_parse_definitely (parser))
8450         {
8451           /* If we couldn't parse an argument list, then we revert our changes
8452              and return simply an error. Maybe this is not a template-id
8453              after all.  */
8454           next_token_2->type = CPP_COLON;
8455           cp_parser_error (parser, "expected %<<%>");
8456           pop_deferring_access_checks ();
8457           return error_mark_node;
8458         }
8459       /* Otherwise, emit an error about the invalid digraph, but continue
8460          parsing because we got our argument list.  */
8461       pedwarn ("%<<::%> cannot begin a template-argument list");
8462       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8463               "between %<<%> and %<::%>");
8464       if (!flag_permissive)
8465         {
8466           static bool hint;
8467           if (!hint)
8468             {
8469               inform ("(if you use -fpermissive G++ will accept your code)");
8470               hint = true;
8471             }
8472         }
8473     }
8474   else
8475     {
8476       /* Look for the `<' that starts the template-argument-list.  */
8477       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8478         {
8479           pop_deferring_access_checks ();
8480           return error_mark_node;
8481         }
8482       /* Parse the arguments.  */
8483       arguments = cp_parser_enclosed_template_argument_list (parser);
8484     }
8485
8486   /* Build a representation of the specialization.  */
8487   if (TREE_CODE (template) == IDENTIFIER_NODE)
8488     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8489   else if (DECL_CLASS_TEMPLATE_P (template)
8490            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8491     template_id
8492       = finish_template_type (template, arguments,
8493                               cp_lexer_next_token_is (parser->lexer,
8494                                                       CPP_SCOPE));
8495   else
8496     {
8497       /* If it's not a class-template or a template-template, it should be
8498          a function-template.  */
8499       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8500                    || TREE_CODE (template) == OVERLOAD
8501                    || BASELINK_P (template)));
8502
8503       template_id = lookup_template_function (template, arguments);
8504     }
8505
8506   /* Retrieve any deferred checks.  Do not pop this access checks yet
8507      so the memory will not be reclaimed during token replacing below.  */
8508   access_check = get_deferred_access_checks ();
8509
8510   /* If parsing tentatively, replace the sequence of tokens that makes
8511      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8512      should we re-parse the token stream, we will not have to repeat
8513      the effort required to do the parse, nor will we issue duplicate
8514      error messages about problems during instantiation of the
8515      template.  */
8516   if (start_of_id)
8517     {
8518       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8519       
8520       /* Reset the contents of the START_OF_ID token.  */
8521       token->type = CPP_TEMPLATE_ID;
8522       token->value = build_tree_list (access_check, template_id);
8523       token->keyword = RID_MAX;
8524       
8525       /* Purge all subsequent tokens.  */
8526       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8527
8528       /* ??? Can we actually assume that, if template_id ==
8529          error_mark_node, we will have issued a diagnostic to the
8530          user, as opposed to simply marking the tentative parse as
8531          failed?  */
8532       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8533         error ("parse error in template argument list");
8534     }
8535
8536   pop_deferring_access_checks ();
8537   return template_id;
8538 }
8539
8540 /* Parse a template-name.
8541
8542    template-name:
8543      identifier
8544
8545    The standard should actually say:
8546
8547    template-name:
8548      identifier
8549      operator-function-id
8550
8551    A defect report has been filed about this issue.
8552
8553    A conversion-function-id cannot be a template name because they cannot
8554    be part of a template-id. In fact, looking at this code:
8555
8556    a.operator K<int>()
8557
8558    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8559    It is impossible to call a templated conversion-function-id with an
8560    explicit argument list, since the only allowed template parameter is
8561    the type to which it is converting.
8562
8563    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8564    `template' keyword, in a construction like:
8565
8566      T::template f<3>()
8567
8568    In that case `f' is taken to be a template-name, even though there
8569    is no way of knowing for sure.
8570
8571    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8572    name refers to a set of overloaded functions, at least one of which
8573    is a template, or an IDENTIFIER_NODE with the name of the template,
8574    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8575    names are looked up inside uninstantiated templates.  */
8576
8577 static tree
8578 cp_parser_template_name (cp_parser* parser,
8579                          bool template_keyword_p,
8580                          bool check_dependency_p,
8581                          bool is_declaration,
8582                          bool *is_identifier)
8583 {
8584   tree identifier;
8585   tree decl;
8586   tree fns;
8587
8588   /* If the next token is `operator', then we have either an
8589      operator-function-id or a conversion-function-id.  */
8590   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8591     {
8592       /* We don't know whether we're looking at an
8593          operator-function-id or a conversion-function-id.  */
8594       cp_parser_parse_tentatively (parser);
8595       /* Try an operator-function-id.  */
8596       identifier = cp_parser_operator_function_id (parser);
8597       /* If that didn't work, try a conversion-function-id.  */
8598       if (!cp_parser_parse_definitely (parser))
8599         {
8600           cp_parser_error (parser, "expected template-name");
8601           return error_mark_node;
8602         }
8603     }
8604   /* Look for the identifier.  */
8605   else
8606     identifier = cp_parser_identifier (parser);
8607
8608   /* If we didn't find an identifier, we don't have a template-id.  */
8609   if (identifier == error_mark_node)
8610     return error_mark_node;
8611
8612   /* If the name immediately followed the `template' keyword, then it
8613      is a template-name.  However, if the next token is not `<', then
8614      we do not treat it as a template-name, since it is not being used
8615      as part of a template-id.  This enables us to handle constructs
8616      like:
8617
8618        template <typename T> struct S { S(); };
8619        template <typename T> S<T>::S();
8620
8621      correctly.  We would treat `S' as a template -- if it were `S<T>'
8622      -- but we do not if there is no `<'.  */
8623
8624   if (processing_template_decl
8625       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8626     {
8627       /* In a declaration, in a dependent context, we pretend that the
8628          "template" keyword was present in order to improve error
8629          recovery.  For example, given:
8630
8631            template <typename T> void f(T::X<int>);
8632
8633          we want to treat "X<int>" as a template-id.  */
8634       if (is_declaration
8635           && !template_keyword_p
8636           && parser->scope && TYPE_P (parser->scope)
8637           && check_dependency_p
8638           && dependent_type_p (parser->scope)
8639           /* Do not do this for dtors (or ctors), since they never
8640              need the template keyword before their name.  */
8641           && !constructor_name_p (identifier, parser->scope))
8642         {
8643           cp_token_position start = 0;
8644           
8645           /* Explain what went wrong.  */
8646           error ("non-template %qD used as template", identifier);
8647           inform ("use %<%T::template %D%> to indicate that it is a template",
8648                   parser->scope, identifier);
8649           /* If parsing tentatively, find the location of the "<" token.  */
8650           if (cp_parser_simulate_error (parser))
8651             start = cp_lexer_token_position (parser->lexer, true);
8652           /* Parse the template arguments so that we can issue error
8653              messages about them.  */
8654           cp_lexer_consume_token (parser->lexer);
8655           cp_parser_enclosed_template_argument_list (parser);
8656           /* Skip tokens until we find a good place from which to
8657              continue parsing.  */
8658           cp_parser_skip_to_closing_parenthesis (parser,
8659                                                  /*recovering=*/true,
8660                                                  /*or_comma=*/true,
8661                                                  /*consume_paren=*/false);
8662           /* If parsing tentatively, permanently remove the
8663              template argument list.  That will prevent duplicate
8664              error messages from being issued about the missing
8665              "template" keyword.  */
8666           if (start)
8667             cp_lexer_purge_tokens_after (parser->lexer, start);
8668           if (is_identifier)
8669             *is_identifier = true;
8670           return identifier;
8671         }
8672
8673       /* If the "template" keyword is present, then there is generally
8674          no point in doing name-lookup, so we just return IDENTIFIER.
8675          But, if the qualifying scope is non-dependent then we can
8676          (and must) do name-lookup normally.  */
8677       if (template_keyword_p
8678           && (!parser->scope
8679               || (TYPE_P (parser->scope)
8680                   && dependent_type_p (parser->scope))))
8681         return identifier;
8682     }
8683
8684   /* Look up the name.  */
8685   decl = cp_parser_lookup_name (parser, identifier,
8686                                 none_type,
8687                                 /*is_template=*/false,
8688                                 /*is_namespace=*/false,
8689                                 check_dependency_p,
8690                                 /*ambiguous_p=*/NULL);
8691   decl = maybe_get_template_decl_from_type_decl (decl);
8692
8693   /* If DECL is a template, then the name was a template-name.  */
8694   if (TREE_CODE (decl) == TEMPLATE_DECL)
8695     ;
8696   else
8697     {
8698       /* The standard does not explicitly indicate whether a name that
8699          names a set of overloaded declarations, some of which are
8700          templates, is a template-name.  However, such a name should
8701          be a template-name; otherwise, there is no way to form a
8702          template-id for the overloaded templates.  */
8703       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8704       if (TREE_CODE (fns) == OVERLOAD)
8705         {
8706           tree fn;
8707
8708           for (fn = fns; fn; fn = OVL_NEXT (fn))
8709             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8710               break;
8711         }
8712       else
8713         {
8714           /* Otherwise, the name does not name a template.  */
8715           cp_parser_error (parser, "expected template-name");
8716           return error_mark_node;
8717         }
8718     }
8719
8720   /* If DECL is dependent, and refers to a function, then just return
8721      its name; we will look it up again during template instantiation.  */
8722   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8723     {
8724       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8725       if (TYPE_P (scope) && dependent_type_p (scope))
8726         return identifier;
8727     }
8728
8729   return decl;
8730 }
8731
8732 /* Parse a template-argument-list.
8733
8734    template-argument-list:
8735      template-argument
8736      template-argument-list , template-argument
8737
8738    Returns a TREE_VEC containing the arguments.  */
8739
8740 static tree
8741 cp_parser_template_argument_list (cp_parser* parser)
8742 {
8743   tree fixed_args[10];
8744   unsigned n_args = 0;
8745   unsigned alloced = 10;
8746   tree *arg_ary = fixed_args;
8747   tree vec;
8748   bool saved_in_template_argument_list_p;
8749
8750   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8751   parser->in_template_argument_list_p = true;
8752   do
8753     {
8754       tree argument;
8755
8756       if (n_args)
8757         /* Consume the comma.  */
8758         cp_lexer_consume_token (parser->lexer);
8759
8760       /* Parse the template-argument.  */
8761       argument = cp_parser_template_argument (parser);
8762       if (n_args == alloced)
8763         {
8764           alloced *= 2;
8765
8766           if (arg_ary == fixed_args)
8767             {
8768               arg_ary = xmalloc (sizeof (tree) * alloced);
8769               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8770             }
8771           else
8772             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8773         }
8774       arg_ary[n_args++] = argument;
8775     }
8776   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8777
8778   vec = make_tree_vec (n_args);
8779
8780   while (n_args--)
8781     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8782
8783   if (arg_ary != fixed_args)
8784     free (arg_ary);
8785   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8786   return vec;
8787 }
8788
8789 /* Parse a template-argument.
8790
8791    template-argument:
8792      assignment-expression
8793      type-id
8794      id-expression
8795
8796    The representation is that of an assignment-expression, type-id, or
8797    id-expression -- except that the qualified id-expression is
8798    evaluated, so that the value returned is either a DECL or an
8799    OVERLOAD.
8800
8801    Although the standard says "assignment-expression", it forbids
8802    throw-expressions or assignments in the template argument.
8803    Therefore, we use "conditional-expression" instead.  */
8804
8805 static tree
8806 cp_parser_template_argument (cp_parser* parser)
8807 {
8808   tree argument;
8809   bool template_p;
8810   bool address_p;
8811   bool maybe_type_id = false;
8812   cp_token *token;
8813   cp_id_kind idk;
8814   tree qualifying_class;
8815
8816   /* There's really no way to know what we're looking at, so we just
8817      try each alternative in order.
8818
8819        [temp.arg]
8820
8821        In a template-argument, an ambiguity between a type-id and an
8822        expression is resolved to a type-id, regardless of the form of
8823        the corresponding template-parameter.
8824
8825      Therefore, we try a type-id first.  */
8826   cp_parser_parse_tentatively (parser);
8827   argument = cp_parser_type_id (parser);
8828   /* If there was no error parsing the type-id but the next token is a '>>',
8829      we probably found a typo for '> >'. But there are type-id which are
8830      also valid expressions. For instance:
8831
8832      struct X { int operator >> (int); };
8833      template <int V> struct Foo {};
8834      Foo<X () >> 5> r;
8835
8836      Here 'X()' is a valid type-id of a function type, but the user just
8837      wanted to write the expression "X() >> 5". Thus, we remember that we
8838      found a valid type-id, but we still try to parse the argument as an
8839      expression to see what happens.  */
8840   if (!cp_parser_error_occurred (parser)
8841       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8842     {
8843       maybe_type_id = true;
8844       cp_parser_abort_tentative_parse (parser);
8845     }
8846   else
8847     {
8848       /* If the next token isn't a `,' or a `>', then this argument wasn't
8849       really finished. This means that the argument is not a valid
8850       type-id.  */
8851       if (!cp_parser_next_token_ends_template_argument_p (parser))
8852         cp_parser_error (parser, "expected template-argument");
8853       /* If that worked, we're done.  */
8854       if (cp_parser_parse_definitely (parser))
8855         return argument;
8856     }
8857   /* We're still not sure what the argument will be.  */
8858   cp_parser_parse_tentatively (parser);
8859   /* Try a template.  */
8860   argument = cp_parser_id_expression (parser,
8861                                       /*template_keyword_p=*/false,
8862                                       /*check_dependency_p=*/true,
8863                                       &template_p,
8864                                       /*declarator_p=*/false);
8865   /* If the next token isn't a `,' or a `>', then this argument wasn't
8866      really finished.  */
8867   if (!cp_parser_next_token_ends_template_argument_p (parser))
8868     cp_parser_error (parser, "expected template-argument");
8869   if (!cp_parser_error_occurred (parser))
8870     {
8871       /* Figure out what is being referred to.  If the id-expression
8872          was for a class template specialization, then we will have a
8873          TYPE_DECL at this point.  There is no need to do name lookup
8874          at this point in that case.  */
8875       if (TREE_CODE (argument) != TYPE_DECL)
8876         argument = cp_parser_lookup_name (parser, argument,
8877                                           none_type,
8878                                           /*is_template=*/template_p,
8879                                           /*is_namespace=*/false,
8880                                           /*check_dependency=*/true,
8881                                           /*ambiguous_p=*/NULL);
8882       if (TREE_CODE (argument) != TEMPLATE_DECL
8883           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8884         cp_parser_error (parser, "expected template-name");
8885     }
8886   if (cp_parser_parse_definitely (parser))
8887     return argument;
8888   /* It must be a non-type argument.  There permitted cases are given
8889      in [temp.arg.nontype]:
8890
8891      -- an integral constant-expression of integral or enumeration
8892         type; or
8893
8894      -- the name of a non-type template-parameter; or
8895
8896      -- the name of an object or function with external linkage...
8897
8898      -- the address of an object or function with external linkage...
8899
8900      -- a pointer to member...  */
8901   /* Look for a non-type template parameter.  */
8902   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8903     {
8904       cp_parser_parse_tentatively (parser);
8905       argument = cp_parser_primary_expression (parser,
8906                                                /*cast_p=*/false,
8907                                                &idk,
8908                                                &qualifying_class);
8909       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8910           || !cp_parser_next_token_ends_template_argument_p (parser))
8911         cp_parser_simulate_error (parser);
8912       if (cp_parser_parse_definitely (parser))
8913         return argument;
8914     }
8915
8916   /* If the next token is "&", the argument must be the address of an
8917      object or function with external linkage.  */
8918   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8919   if (address_p)
8920     cp_lexer_consume_token (parser->lexer);
8921   /* See if we might have an id-expression.  */
8922   token = cp_lexer_peek_token (parser->lexer);
8923   if (token->type == CPP_NAME
8924       || token->keyword == RID_OPERATOR
8925       || token->type == CPP_SCOPE
8926       || token->type == CPP_TEMPLATE_ID
8927       || token->type == CPP_NESTED_NAME_SPECIFIER)
8928     {
8929       cp_parser_parse_tentatively (parser);
8930       argument = cp_parser_primary_expression (parser,
8931                                                /*cast_p=*/false,
8932                                                &idk,
8933                                                &qualifying_class);
8934       if (cp_parser_error_occurred (parser)
8935           || !cp_parser_next_token_ends_template_argument_p (parser))
8936         cp_parser_abort_tentative_parse (parser);
8937       else
8938         {
8939           if (TREE_CODE (argument) == INDIRECT_REF)
8940             {
8941               gcc_assert (REFERENCE_REF_P (argument));
8942               argument = TREE_OPERAND (argument, 0);
8943             }
8944           
8945           if (qualifying_class)
8946             argument = finish_qualified_id_expr (qualifying_class,
8947                                                  argument,
8948                                                  /*done=*/true,
8949                                                  address_p);
8950           if (TREE_CODE (argument) == VAR_DECL)
8951             {
8952               /* A variable without external linkage might still be a
8953                  valid constant-expression, so no error is issued here
8954                  if the external-linkage check fails.  */
8955               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8956                 cp_parser_simulate_error (parser);
8957             }
8958           else if (is_overloaded_fn (argument))
8959             /* All overloaded functions are allowed; if the external
8960                linkage test does not pass, an error will be issued
8961                later.  */
8962             ;
8963           else if (address_p
8964                    && (TREE_CODE (argument) == OFFSET_REF
8965                        || TREE_CODE (argument) == SCOPE_REF))
8966             /* A pointer-to-member.  */
8967             ;
8968           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
8969             ;
8970           else
8971             cp_parser_simulate_error (parser);
8972
8973           if (cp_parser_parse_definitely (parser))
8974             {
8975               if (address_p)
8976                 argument = build_x_unary_op (ADDR_EXPR, argument);
8977               return argument;
8978             }
8979         }
8980     }
8981   /* If the argument started with "&", there are no other valid
8982      alternatives at this point.  */
8983   if (address_p)
8984     {
8985       cp_parser_error (parser, "invalid non-type template argument");
8986       return error_mark_node;
8987     }
8988
8989   /* If the argument wasn't successfully parsed as a type-id followed
8990      by '>>', the argument can only be a constant expression now.
8991      Otherwise, we try parsing the constant-expression tentatively,
8992      because the argument could really be a type-id.  */
8993   if (maybe_type_id)
8994     cp_parser_parse_tentatively (parser);
8995   argument = cp_parser_constant_expression (parser,
8996                                             /*allow_non_constant_p=*/false,
8997                                             /*non_constant_p=*/NULL);
8998   argument = fold_non_dependent_expr (argument);
8999   if (!maybe_type_id)
9000     return argument;
9001   if (!cp_parser_next_token_ends_template_argument_p (parser))
9002     cp_parser_error (parser, "expected template-argument");
9003   if (cp_parser_parse_definitely (parser))
9004     return argument;
9005   /* We did our best to parse the argument as a non type-id, but that
9006      was the only alternative that matched (albeit with a '>' after
9007      it). We can assume it's just a typo from the user, and a
9008      diagnostic will then be issued.  */
9009   return cp_parser_type_id (parser);
9010 }
9011
9012 /* Parse an explicit-instantiation.
9013
9014    explicit-instantiation:
9015      template declaration
9016
9017    Although the standard says `declaration', what it really means is:
9018
9019    explicit-instantiation:
9020      template decl-specifier-seq [opt] declarator [opt] ;
9021
9022    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9023    supposed to be allowed.  A defect report has been filed about this
9024    issue.
9025
9026    GNU Extension:
9027
9028    explicit-instantiation:
9029      storage-class-specifier template
9030        decl-specifier-seq [opt] declarator [opt] ;
9031      function-specifier template
9032        decl-specifier-seq [opt] declarator [opt] ;  */
9033
9034 static void
9035 cp_parser_explicit_instantiation (cp_parser* parser)
9036 {
9037   int declares_class_or_enum;
9038   cp_decl_specifier_seq decl_specifiers;
9039   tree extension_specifier = NULL_TREE;
9040
9041   /* Look for an (optional) storage-class-specifier or
9042      function-specifier.  */
9043   if (cp_parser_allow_gnu_extensions_p (parser))
9044     {
9045       extension_specifier
9046         = cp_parser_storage_class_specifier_opt (parser);
9047       if (!extension_specifier)
9048         extension_specifier
9049           = cp_parser_function_specifier_opt (parser,
9050                                               /*decl_specs=*/NULL);
9051     }
9052
9053   /* Look for the `template' keyword.  */
9054   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9055   /* Let the front end know that we are processing an explicit
9056      instantiation.  */
9057   begin_explicit_instantiation ();
9058   /* [temp.explicit] says that we are supposed to ignore access
9059      control while processing explicit instantiation directives.  */
9060   push_deferring_access_checks (dk_no_check);
9061   /* Parse a decl-specifier-seq.  */
9062   cp_parser_decl_specifier_seq (parser,
9063                                 CP_PARSER_FLAGS_OPTIONAL,
9064                                 &decl_specifiers,
9065                                 &declares_class_or_enum);
9066   /* If there was exactly one decl-specifier, and it declared a class,
9067      and there's no declarator, then we have an explicit type
9068      instantiation.  */
9069   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9070     {
9071       tree type;
9072
9073       type = check_tag_decl (&decl_specifiers);
9074       /* Turn access control back on for names used during
9075          template instantiation.  */
9076       pop_deferring_access_checks ();
9077       if (type)
9078         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9079     }
9080   else
9081     {
9082       cp_declarator *declarator;
9083       tree decl;
9084
9085       /* Parse the declarator.  */
9086       declarator
9087         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9088                                 /*ctor_dtor_or_conv_p=*/NULL,
9089                                 /*parenthesized_p=*/NULL,
9090                                 /*member_p=*/false);
9091       if (declares_class_or_enum & 2)
9092         cp_parser_check_for_definition_in_return_type (declarator,
9093                                                        decl_specifiers.type);
9094       if (declarator != cp_error_declarator)
9095         {
9096           decl = grokdeclarator (declarator, &decl_specifiers,
9097                                  NORMAL, 0, NULL);
9098           /* Turn access control back on for names used during
9099              template instantiation.  */
9100           pop_deferring_access_checks ();
9101           /* Do the explicit instantiation.  */
9102           do_decl_instantiation (decl, extension_specifier);
9103         }
9104       else
9105         {
9106           pop_deferring_access_checks ();
9107           /* Skip the body of the explicit instantiation.  */
9108           cp_parser_skip_to_end_of_statement (parser);
9109         }
9110     }
9111   /* We're done with the instantiation.  */
9112   end_explicit_instantiation ();
9113
9114   cp_parser_consume_semicolon_at_end_of_statement (parser);
9115 }
9116
9117 /* Parse an explicit-specialization.
9118
9119    explicit-specialization:
9120      template < > declaration
9121
9122    Although the standard says `declaration', what it really means is:
9123
9124    explicit-specialization:
9125      template <> decl-specifier [opt] init-declarator [opt] ;
9126      template <> function-definition
9127      template <> explicit-specialization
9128      template <> template-declaration  */
9129
9130 static void
9131 cp_parser_explicit_specialization (cp_parser* parser)
9132 {
9133   /* Look for the `template' keyword.  */
9134   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9135   /* Look for the `<'.  */
9136   cp_parser_require (parser, CPP_LESS, "`<'");
9137   /* Look for the `>'.  */
9138   cp_parser_require (parser, CPP_GREATER, "`>'");
9139   /* We have processed another parameter list.  */
9140   ++parser->num_template_parameter_lists;
9141   /* Let the front end know that we are beginning a specialization.  */
9142   begin_specialization ();
9143
9144   /* If the next keyword is `template', we need to figure out whether
9145      or not we're looking a template-declaration.  */
9146   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9147     {
9148       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9149           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9150         cp_parser_template_declaration_after_export (parser,
9151                                                      /*member_p=*/false);
9152       else
9153         cp_parser_explicit_specialization (parser);
9154     }
9155   else
9156     /* Parse the dependent declaration.  */
9157     cp_parser_single_declaration (parser,
9158                                   /*member_p=*/false,
9159                                   /*friend_p=*/NULL);
9160
9161   /* We're done with the specialization.  */
9162   end_specialization ();
9163   /* We're done with this parameter list.  */
9164   --parser->num_template_parameter_lists;
9165 }
9166
9167 /* Parse a type-specifier.
9168
9169    type-specifier:
9170      simple-type-specifier
9171      class-specifier
9172      enum-specifier
9173      elaborated-type-specifier
9174      cv-qualifier
9175
9176    GNU Extension:
9177
9178    type-specifier:
9179      __complex__
9180
9181    Returns a representation of the type-specifier.  For a
9182    class-specifier, enum-specifier, or elaborated-type-specifier, a
9183    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9184
9185    The parser flags FLAGS is used to control type-specifier parsing.
9186
9187    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9188    in a decl-specifier-seq.
9189
9190    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9191    class-specifier, enum-specifier, or elaborated-type-specifier, then
9192    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9193    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9194    zero.
9195
9196    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9197    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9198    is set to FALSE.  */
9199
9200 static tree
9201 cp_parser_type_specifier (cp_parser* parser,
9202                           cp_parser_flags flags,
9203                           cp_decl_specifier_seq *decl_specs,
9204                           bool is_declaration,
9205                           int* declares_class_or_enum,
9206                           bool* is_cv_qualifier)
9207 {
9208   tree type_spec = NULL_TREE;
9209   cp_token *token;
9210   enum rid keyword;
9211   cp_decl_spec ds = ds_last;
9212
9213   /* Assume this type-specifier does not declare a new type.  */
9214   if (declares_class_or_enum)
9215     *declares_class_or_enum = 0;
9216   /* And that it does not specify a cv-qualifier.  */
9217   if (is_cv_qualifier)
9218     *is_cv_qualifier = false;
9219   /* Peek at the next token.  */
9220   token = cp_lexer_peek_token (parser->lexer);
9221
9222   /* If we're looking at a keyword, we can use that to guide the
9223      production we choose.  */
9224   keyword = token->keyword;
9225   switch (keyword)
9226     {
9227     case RID_ENUM:
9228       /* 'enum' [identifier] '{' introduces an enum-specifier;
9229          'enum' <anything else> introduces an elaborated-type-specifier.  */
9230       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9231           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9232               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9233                  == CPP_OPEN_BRACE))
9234         {
9235           if (parser->num_template_parameter_lists)
9236             {
9237               error ("template declaration of %qs", "enum");
9238               cp_parser_skip_to_end_of_block_or_statement (parser);
9239               type_spec = error_mark_node;
9240             }
9241           else
9242             type_spec = cp_parser_enum_specifier (parser);
9243
9244           if (declares_class_or_enum)
9245             *declares_class_or_enum = 2;
9246           if (decl_specs)
9247             cp_parser_set_decl_spec_type (decl_specs,
9248                                           type_spec,
9249                                           /*user_defined_p=*/true);
9250           return type_spec;
9251         }
9252       else
9253         goto elaborated_type_specifier;
9254
9255       /* Any of these indicate either a class-specifier, or an
9256          elaborated-type-specifier.  */
9257     case RID_CLASS:
9258     case RID_STRUCT:
9259     case RID_UNION:
9260       /* Parse tentatively so that we can back up if we don't find a
9261          class-specifier.  */
9262       cp_parser_parse_tentatively (parser);
9263       /* Look for the class-specifier.  */
9264       type_spec = cp_parser_class_specifier (parser);
9265       /* If that worked, we're done.  */
9266       if (cp_parser_parse_definitely (parser))
9267         {
9268           if (declares_class_or_enum)
9269             *declares_class_or_enum = 2;
9270           if (decl_specs)
9271             cp_parser_set_decl_spec_type (decl_specs,
9272                                           type_spec,
9273                                           /*user_defined_p=*/true);
9274           return type_spec;
9275         }
9276
9277       /* Fall through.  */
9278     elaborated_type_specifier:
9279       /* We're declaring (not defining) a class or enum.  */
9280       if (declares_class_or_enum)
9281         *declares_class_or_enum = 1;
9282
9283       /* Fall through.  */
9284     case RID_TYPENAME:
9285       /* Look for an elaborated-type-specifier.  */
9286       type_spec
9287         = (cp_parser_elaborated_type_specifier
9288            (parser,
9289             decl_specs && decl_specs->specs[(int) ds_friend],
9290             is_declaration));
9291       if (decl_specs)
9292         cp_parser_set_decl_spec_type (decl_specs,
9293                                       type_spec,
9294                                       /*user_defined_p=*/true);
9295       return type_spec;
9296
9297     case RID_CONST:
9298       ds = ds_const;
9299       if (is_cv_qualifier)
9300         *is_cv_qualifier = true;
9301       break;
9302
9303     case RID_VOLATILE:
9304       ds = ds_volatile;
9305       if (is_cv_qualifier)
9306         *is_cv_qualifier = true;
9307       break;
9308
9309     case RID_RESTRICT:
9310       ds = ds_restrict;
9311       if (is_cv_qualifier)
9312         *is_cv_qualifier = true;
9313       break;
9314
9315     case RID_COMPLEX:
9316       /* The `__complex__' keyword is a GNU extension.  */
9317       ds = ds_complex;
9318       break;
9319
9320     default:
9321       break;
9322     }
9323
9324   /* Handle simple keywords.  */
9325   if (ds != ds_last)
9326     {
9327       if (decl_specs)
9328         {
9329           ++decl_specs->specs[(int)ds];
9330           decl_specs->any_specifiers_p = true;
9331         }
9332       return cp_lexer_consume_token (parser->lexer)->value;
9333     }
9334
9335   /* If we do not already have a type-specifier, assume we are looking
9336      at a simple-type-specifier.  */
9337   type_spec = cp_parser_simple_type_specifier (parser,
9338                                                decl_specs,
9339                                                flags);
9340
9341   /* If we didn't find a type-specifier, and a type-specifier was not
9342      optional in this context, issue an error message.  */
9343   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9344     {
9345       cp_parser_error (parser, "expected type specifier");
9346       return error_mark_node;
9347     }
9348
9349   return type_spec;
9350 }
9351
9352 /* Parse a simple-type-specifier.
9353
9354    simple-type-specifier:
9355      :: [opt] nested-name-specifier [opt] type-name
9356      :: [opt] nested-name-specifier template template-id
9357      char
9358      wchar_t
9359      bool
9360      short
9361      int
9362      long
9363      signed
9364      unsigned
9365      float
9366      double
9367      void
9368
9369    GNU Extension:
9370
9371    simple-type-specifier:
9372      __typeof__ unary-expression
9373      __typeof__ ( type-id )
9374
9375    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9376    appropriately updated.  */
9377
9378 static tree
9379 cp_parser_simple_type_specifier (cp_parser* parser,
9380                                  cp_decl_specifier_seq *decl_specs,
9381                                  cp_parser_flags flags)
9382 {
9383   tree type = NULL_TREE;
9384   cp_token *token;
9385
9386   /* Peek at the next token.  */
9387   token = cp_lexer_peek_token (parser->lexer);
9388
9389   /* If we're looking at a keyword, things are easy.  */
9390   switch (token->keyword)
9391     {
9392     case RID_CHAR:
9393       if (decl_specs)
9394         decl_specs->explicit_char_p = true;
9395       type = char_type_node;
9396       break;
9397     case RID_WCHAR:
9398       type = wchar_type_node;
9399       break;
9400     case RID_BOOL:
9401       type = boolean_type_node;
9402       break;
9403     case RID_SHORT:
9404       if (decl_specs)
9405         ++decl_specs->specs[(int) ds_short];
9406       type = short_integer_type_node;
9407       break;
9408     case RID_INT:
9409       if (decl_specs)
9410         decl_specs->explicit_int_p = true;
9411       type = integer_type_node;
9412       break;
9413     case RID_LONG:
9414       if (decl_specs)
9415         ++decl_specs->specs[(int) ds_long];
9416       type = long_integer_type_node;
9417       break;
9418     case RID_SIGNED:
9419       if (decl_specs)
9420         ++decl_specs->specs[(int) ds_signed];
9421       type = integer_type_node;
9422       break;
9423     case RID_UNSIGNED:
9424       if (decl_specs)
9425         ++decl_specs->specs[(int) ds_unsigned];
9426       type = unsigned_type_node;
9427       break;
9428     case RID_FLOAT:
9429       type = float_type_node;
9430       break;
9431     case RID_DOUBLE:
9432       type = double_type_node;
9433       break;
9434     case RID_VOID:
9435       type = void_type_node;
9436       break;
9437
9438     case RID_TYPEOF:
9439       /* Consume the `typeof' token.  */
9440       cp_lexer_consume_token (parser->lexer);
9441       /* Parse the operand to `typeof'.  */
9442       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9443       /* If it is not already a TYPE, take its type.  */
9444       if (!TYPE_P (type))
9445         type = finish_typeof (type);
9446
9447       if (decl_specs)
9448         cp_parser_set_decl_spec_type (decl_specs, type,
9449                                       /*user_defined_p=*/true);
9450
9451       return type;
9452
9453     default:
9454       break;
9455     }
9456
9457   /* If the type-specifier was for a built-in type, we're done.  */
9458   if (type)
9459     {
9460       tree id;
9461
9462       /* Record the type.  */
9463       if (decl_specs
9464           && (token->keyword != RID_SIGNED
9465               && token->keyword != RID_UNSIGNED
9466               && token->keyword != RID_SHORT
9467               && token->keyword != RID_LONG))
9468         cp_parser_set_decl_spec_type (decl_specs,
9469                                       type,
9470                                       /*user_defined=*/false);
9471       if (decl_specs)
9472         decl_specs->any_specifiers_p = true;
9473
9474       /* Consume the token.  */
9475       id = cp_lexer_consume_token (parser->lexer)->value;
9476
9477       /* There is no valid C++ program where a non-template type is
9478          followed by a "<".  That usually indicates that the user thought
9479          that the type was a template.  */
9480       cp_parser_check_for_invalid_template_id (parser, type);
9481
9482       return TYPE_NAME (type);
9483     }
9484
9485   /* The type-specifier must be a user-defined type.  */
9486   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9487     {
9488       bool qualified_p;
9489       bool global_p;
9490
9491       /* Don't gobble tokens or issue error messages if this is an
9492          optional type-specifier.  */
9493       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9494         cp_parser_parse_tentatively (parser);
9495
9496       /* Look for the optional `::' operator.  */
9497       global_p
9498         = (cp_parser_global_scope_opt (parser,
9499                                        /*current_scope_valid_p=*/false)
9500            != NULL_TREE);
9501       /* Look for the nested-name specifier.  */
9502       qualified_p
9503         = (cp_parser_nested_name_specifier_opt (parser,
9504                                                 /*typename_keyword_p=*/false,
9505                                                 /*check_dependency_p=*/true,
9506                                                 /*type_p=*/false,
9507                                                 /*is_declaration=*/false)
9508            != NULL_TREE);
9509       /* If we have seen a nested-name-specifier, and the next token
9510          is `template', then we are using the template-id production.  */
9511       if (parser->scope
9512           && cp_parser_optional_template_keyword (parser))
9513         {
9514           /* Look for the template-id.  */
9515           type = cp_parser_template_id (parser,
9516                                         /*template_keyword_p=*/true,
9517                                         /*check_dependency_p=*/true,
9518                                         /*is_declaration=*/false);
9519           /* If the template-id did not name a type, we are out of
9520              luck.  */
9521           if (TREE_CODE (type) != TYPE_DECL)
9522             {
9523               cp_parser_error (parser, "expected template-id for type");
9524               type = NULL_TREE;
9525             }
9526         }
9527       /* Otherwise, look for a type-name.  */
9528       else
9529         type = cp_parser_type_name (parser);
9530       /* Keep track of all name-lookups performed in class scopes.  */
9531       if (type
9532           && !global_p
9533           && !qualified_p
9534           && TREE_CODE (type) == TYPE_DECL
9535           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9536         maybe_note_name_used_in_class (DECL_NAME (type), type);
9537       /* If it didn't work out, we don't have a TYPE.  */
9538       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9539           && !cp_parser_parse_definitely (parser))
9540         type = NULL_TREE;
9541       if (type && decl_specs)
9542         cp_parser_set_decl_spec_type (decl_specs, type,
9543                                       /*user_defined=*/true);
9544     }
9545
9546   /* If we didn't get a type-name, issue an error message.  */
9547   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9548     {
9549       cp_parser_error (parser, "expected type-name");
9550       return error_mark_node;
9551     }
9552
9553   /* There is no valid C++ program where a non-template type is
9554      followed by a "<".  That usually indicates that the user thought
9555      that the type was a template.  */
9556   if (type && type != error_mark_node)
9557     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9558
9559   return type;
9560 }
9561
9562 /* Parse a type-name.
9563
9564    type-name:
9565      class-name
9566      enum-name
9567      typedef-name
9568
9569    enum-name:
9570      identifier
9571
9572    typedef-name:
9573      identifier
9574
9575    Returns a TYPE_DECL for the the type.  */
9576
9577 static tree
9578 cp_parser_type_name (cp_parser* parser)
9579 {
9580   tree type_decl;
9581   tree identifier;
9582
9583   /* We can't know yet whether it is a class-name or not.  */
9584   cp_parser_parse_tentatively (parser);
9585   /* Try a class-name.  */
9586   type_decl = cp_parser_class_name (parser,
9587                                     /*typename_keyword_p=*/false,
9588                                     /*template_keyword_p=*/false,
9589                                     none_type,
9590                                     /*check_dependency_p=*/true,
9591                                     /*class_head_p=*/false,
9592                                     /*is_declaration=*/false);
9593   /* If it's not a class-name, keep looking.  */
9594   if (!cp_parser_parse_definitely (parser))
9595     {
9596       /* It must be a typedef-name or an enum-name.  */
9597       identifier = cp_parser_identifier (parser);
9598       if (identifier == error_mark_node)
9599         return error_mark_node;
9600
9601       /* Look up the type-name.  */
9602       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9603       /* Issue an error if we did not find a type-name.  */
9604       if (TREE_CODE (type_decl) != TYPE_DECL)
9605         {
9606           if (!cp_parser_simulate_error (parser))
9607             cp_parser_name_lookup_error (parser, identifier, type_decl,
9608                                          "is not a type");
9609           type_decl = error_mark_node;
9610         }
9611       /* Remember that the name was used in the definition of the
9612          current class so that we can check later to see if the
9613          meaning would have been different after the class was
9614          entirely defined.  */
9615       else if (type_decl != error_mark_node
9616                && !parser->scope)
9617         maybe_note_name_used_in_class (identifier, type_decl);
9618     }
9619
9620   return type_decl;
9621 }
9622
9623
9624 /* Parse an elaborated-type-specifier.  Note that the grammar given
9625    here incorporates the resolution to DR68.
9626
9627    elaborated-type-specifier:
9628      class-key :: [opt] nested-name-specifier [opt] identifier
9629      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9630      enum :: [opt] nested-name-specifier [opt] identifier
9631      typename :: [opt] nested-name-specifier identifier
9632      typename :: [opt] nested-name-specifier template [opt]
9633        template-id
9634
9635    GNU extension:
9636
9637    elaborated-type-specifier:
9638      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9639      class-key attributes :: [opt] nested-name-specifier [opt]
9640                template [opt] template-id
9641      enum attributes :: [opt] nested-name-specifier [opt] identifier
9642
9643    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9644    declared `friend'.  If IS_DECLARATION is TRUE, then this
9645    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9646    something is being declared.
9647
9648    Returns the TYPE specified.  */
9649
9650 static tree
9651 cp_parser_elaborated_type_specifier (cp_parser* parser,
9652                                      bool is_friend,
9653                                      bool is_declaration)
9654 {
9655   enum tag_types tag_type;
9656   tree identifier;
9657   tree type = NULL_TREE;
9658   tree attributes = NULL_TREE;
9659
9660   /* See if we're looking at the `enum' keyword.  */
9661   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9662     {
9663       /* Consume the `enum' token.  */
9664       cp_lexer_consume_token (parser->lexer);
9665       /* Remember that it's an enumeration type.  */
9666       tag_type = enum_type;
9667       /* Parse the attributes.  */
9668       attributes = cp_parser_attributes_opt (parser);
9669     }
9670   /* Or, it might be `typename'.  */
9671   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9672                                            RID_TYPENAME))
9673     {
9674       /* Consume the `typename' token.  */
9675       cp_lexer_consume_token (parser->lexer);
9676       /* Remember that it's a `typename' type.  */
9677       tag_type = typename_type;
9678       /* The `typename' keyword is only allowed in templates.  */
9679       if (!processing_template_decl)
9680         pedwarn ("using %<typename%> outside of template");
9681     }
9682   /* Otherwise it must be a class-key.  */
9683   else
9684     {
9685       tag_type = cp_parser_class_key (parser);
9686       if (tag_type == none_type)
9687         return error_mark_node;
9688       /* Parse the attributes.  */
9689       attributes = cp_parser_attributes_opt (parser);
9690     }
9691
9692   /* Look for the `::' operator.  */
9693   cp_parser_global_scope_opt (parser,
9694                               /*current_scope_valid_p=*/false);
9695   /* Look for the nested-name-specifier.  */
9696   if (tag_type == typename_type)
9697     {
9698       if (cp_parser_nested_name_specifier (parser,
9699                                            /*typename_keyword_p=*/true,
9700                                            /*check_dependency_p=*/true,
9701                                            /*type_p=*/true,
9702                                            is_declaration)
9703           == error_mark_node)
9704         return error_mark_node;
9705     }
9706   else
9707     /* Even though `typename' is not present, the proposed resolution
9708        to Core Issue 180 says that in `class A<T>::B', `B' should be
9709        considered a type-name, even if `A<T>' is dependent.  */
9710     cp_parser_nested_name_specifier_opt (parser,
9711                                          /*typename_keyword_p=*/true,
9712                                          /*check_dependency_p=*/true,
9713                                          /*type_p=*/true,
9714                                          is_declaration);
9715   /* For everything but enumeration types, consider a template-id.  */
9716   if (tag_type != enum_type)
9717     {
9718       bool template_p = false;
9719       tree decl;
9720
9721       /* Allow the `template' keyword.  */
9722       template_p = cp_parser_optional_template_keyword (parser);
9723       /* If we didn't see `template', we don't know if there's a
9724          template-id or not.  */
9725       if (!template_p)
9726         cp_parser_parse_tentatively (parser);
9727       /* Parse the template-id.  */
9728       decl = cp_parser_template_id (parser, template_p,
9729                                     /*check_dependency_p=*/true,
9730                                     is_declaration);
9731       /* If we didn't find a template-id, look for an ordinary
9732          identifier.  */
9733       if (!template_p && !cp_parser_parse_definitely (parser))
9734         ;
9735       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9736          in effect, then we must assume that, upon instantiation, the
9737          template will correspond to a class.  */
9738       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9739                && tag_type == typename_type)
9740         type = make_typename_type (parser->scope, decl,
9741                                    typename_type,
9742                                    /*complain=*/1);
9743       else
9744         type = TREE_TYPE (decl);
9745     }
9746
9747   /* For an enumeration type, consider only a plain identifier.  */
9748   if (!type)
9749     {
9750       identifier = cp_parser_identifier (parser);
9751
9752       if (identifier == error_mark_node)
9753         {
9754           parser->scope = NULL_TREE;
9755           return error_mark_node;
9756         }
9757
9758       /* For a `typename', we needn't call xref_tag.  */
9759       if (tag_type == typename_type 
9760           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9761         return cp_parser_make_typename_type (parser, parser->scope,
9762                                              identifier);
9763       /* Look up a qualified name in the usual way.  */
9764       if (parser->scope)
9765         {
9766           tree decl;
9767
9768           decl = cp_parser_lookup_name (parser, identifier,
9769                                         tag_type,
9770                                         /*is_template=*/false,
9771                                         /*is_namespace=*/false,
9772                                         /*check_dependency=*/true,
9773                                         /*ambiguous_p=*/NULL);
9774
9775           /* If we are parsing friend declaration, DECL may be a
9776              TEMPLATE_DECL tree node here.  However, we need to check
9777              whether this TEMPLATE_DECL results in valid code.  Consider
9778              the following example:
9779
9780                namespace N {
9781                  template <class T> class C {};
9782                }
9783                class X {
9784                  template <class T> friend class N::C; // #1, valid code
9785                };
9786                template <class T> class Y {
9787                  friend class N::C;                    // #2, invalid code
9788                };
9789
9790              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9791              name lookup of `N::C'.  We see that friend declaration must
9792              be template for the code to be valid.  Note that
9793              processing_template_decl does not work here since it is
9794              always 1 for the above two cases.  */
9795
9796           decl = (cp_parser_maybe_treat_template_as_class
9797                   (decl, /*tag_name_p=*/is_friend
9798                          && parser->num_template_parameter_lists));
9799
9800           if (TREE_CODE (decl) != TYPE_DECL)
9801             {
9802               cp_parser_diagnose_invalid_type_name (parser, 
9803                                                     parser->scope,
9804                                                     identifier);
9805               return error_mark_node;
9806             }
9807
9808           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9809             check_elaborated_type_specifier
9810               (tag_type, decl,
9811                (parser->num_template_parameter_lists
9812                 || DECL_SELF_REFERENCE_P (decl)));
9813
9814           type = TREE_TYPE (decl);
9815         }
9816       else
9817         {
9818           /* An elaborated-type-specifier sometimes introduces a new type and
9819              sometimes names an existing type.  Normally, the rule is that it
9820              introduces a new type only if there is not an existing type of
9821              the same name already in scope.  For example, given:
9822
9823                struct S {};
9824                void f() { struct S s; }
9825
9826              the `struct S' in the body of `f' is the same `struct S' as in
9827              the global scope; the existing definition is used.  However, if
9828              there were no global declaration, this would introduce a new
9829              local class named `S'.
9830
9831              An exception to this rule applies to the following code:
9832
9833                namespace N { struct S; }
9834
9835              Here, the elaborated-type-specifier names a new type
9836              unconditionally; even if there is already an `S' in the
9837              containing scope this declaration names a new type.
9838              This exception only applies if the elaborated-type-specifier
9839              forms the complete declaration:
9840
9841                [class.name]
9842
9843                A declaration consisting solely of `class-key identifier ;' is
9844                either a redeclaration of the name in the current scope or a
9845                forward declaration of the identifier as a class name.  It
9846                introduces the name into the current scope.
9847
9848              We are in this situation precisely when the next token is a `;'.
9849
9850              An exception to the exception is that a `friend' declaration does
9851              *not* name a new type; i.e., given:
9852
9853                struct S { friend struct T; };
9854
9855              `T' is not a new type in the scope of `S'.
9856
9857              Also, `new struct S' or `sizeof (struct S)' never results in the
9858              definition of a new type; a new type can only be declared in a
9859              declaration context.  */
9860
9861           tag_scope ts;
9862           if (is_friend)
9863             /* Friends have special name lookup rules.  */
9864             ts = ts_within_enclosing_non_class;
9865           else if (is_declaration
9866                    && cp_lexer_next_token_is (parser->lexer,
9867                                               CPP_SEMICOLON))
9868             /* This is a `class-key identifier ;' */
9869             ts = ts_current;
9870           else
9871             ts = ts_global;
9872
9873           /* Warn about attributes. They are ignored.  */
9874           if (attributes)
9875             warning ("type attributes are honored only at type definition");
9876
9877           type = xref_tag (tag_type, identifier, ts,
9878                            parser->num_template_parameter_lists);
9879         }
9880     }
9881   if (tag_type != enum_type)
9882     cp_parser_check_class_key (tag_type, type);
9883
9884   /* A "<" cannot follow an elaborated type specifier.  If that
9885      happens, the user was probably trying to form a template-id.  */
9886   cp_parser_check_for_invalid_template_id (parser, type);
9887
9888   return type;
9889 }
9890
9891 /* Parse an enum-specifier.
9892
9893    enum-specifier:
9894      enum identifier [opt] { enumerator-list [opt] }
9895
9896    GNU Extensions:
9897      enum identifier [opt] { enumerator-list [opt] } attributes
9898
9899    Returns an ENUM_TYPE representing the enumeration.  */
9900
9901 static tree
9902 cp_parser_enum_specifier (cp_parser* parser)
9903 {
9904   tree identifier;
9905   tree type;
9906
9907   /* Caller guarantees that the current token is 'enum', an identifier
9908      possibly follows, and the token after that is an opening brace.
9909      If we don't have an identifier, fabricate an anonymous name for
9910      the enumeration being defined.  */
9911   cp_lexer_consume_token (parser->lexer);
9912
9913   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9914     identifier = cp_parser_identifier (parser);
9915   else
9916     identifier = make_anon_name ();
9917
9918   /* Issue an error message if type-definitions are forbidden here.  */
9919   cp_parser_check_type_definition (parser);
9920
9921   /* Create the new type.  We do this before consuming the opening brace
9922      so the enum will be recorded as being on the line of its tag (or the
9923      'enum' keyword, if there is no tag).  */
9924   type = start_enum (identifier);
9925
9926   /* Consume the opening brace.  */
9927   cp_lexer_consume_token (parser->lexer);
9928
9929   /* If the next token is not '}', then there are some enumerators.  */
9930   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9931     cp_parser_enumerator_list (parser, type);
9932
9933   /* Consume the final '}'.  */
9934   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9935
9936   /* Look for trailing attributes to apply to this enumeration, and
9937      apply them if appropriate.  */
9938   if (cp_parser_allow_gnu_extensions_p (parser))
9939     {
9940       tree trailing_attr = cp_parser_attributes_opt (parser);
9941       cplus_decl_attributes (&type,
9942                              trailing_attr,
9943                              (int) ATTR_FLAG_TYPE_IN_PLACE);
9944     }
9945
9946   /* Finish up the enumeration.  */
9947   finish_enum (type);
9948
9949   return type;
9950 }
9951
9952 /* Parse an enumerator-list.  The enumerators all have the indicated
9953    TYPE.
9954
9955    enumerator-list:
9956      enumerator-definition
9957      enumerator-list , enumerator-definition  */
9958
9959 static void
9960 cp_parser_enumerator_list (cp_parser* parser, tree type)
9961 {
9962   while (true)
9963     {
9964       /* Parse an enumerator-definition.  */
9965       cp_parser_enumerator_definition (parser, type);
9966
9967       /* If the next token is not a ',', we've reached the end of
9968          the list.  */
9969       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9970         break;
9971       /* Otherwise, consume the `,' and keep going.  */
9972       cp_lexer_consume_token (parser->lexer);
9973       /* If the next token is a `}', there is a trailing comma.  */
9974       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9975         {
9976           if (pedantic && !in_system_header)
9977             pedwarn ("comma at end of enumerator list");
9978           break;
9979         }
9980     }
9981 }
9982
9983 /* Parse an enumerator-definition.  The enumerator has the indicated
9984    TYPE.
9985
9986    enumerator-definition:
9987      enumerator
9988      enumerator = constant-expression
9989
9990    enumerator:
9991      identifier  */
9992
9993 static void
9994 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9995 {
9996   tree identifier;
9997   tree value;
9998
9999   /* Look for the identifier.  */
10000   identifier = cp_parser_identifier (parser);
10001   if (identifier == error_mark_node)
10002     return;
10003
10004   /* If the next token is an '=', then there is an explicit value.  */
10005   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10006     {
10007       /* Consume the `=' token.  */
10008       cp_lexer_consume_token (parser->lexer);
10009       /* Parse the value.  */
10010       value = cp_parser_constant_expression (parser,
10011                                              /*allow_non_constant_p=*/false,
10012                                              NULL);
10013     }
10014   else
10015     value = NULL_TREE;
10016
10017   /* Create the enumerator.  */
10018   build_enumerator (identifier, value, type);
10019 }
10020
10021 /* Parse a namespace-name.
10022
10023    namespace-name:
10024      original-namespace-name
10025      namespace-alias
10026
10027    Returns the NAMESPACE_DECL for the namespace.  */
10028
10029 static tree
10030 cp_parser_namespace_name (cp_parser* parser)
10031 {
10032   tree identifier;
10033   tree namespace_decl;
10034
10035   /* Get the name of the namespace.  */
10036   identifier = cp_parser_identifier (parser);
10037   if (identifier == error_mark_node)
10038     return error_mark_node;
10039
10040   /* Look up the identifier in the currently active scope.  Look only
10041      for namespaces, due to:
10042
10043        [basic.lookup.udir]
10044
10045        When looking up a namespace-name in a using-directive or alias
10046        definition, only namespace names are considered.
10047
10048      And:
10049
10050        [basic.lookup.qual]
10051
10052        During the lookup of a name preceding the :: scope resolution
10053        operator, object, function, and enumerator names are ignored.
10054
10055      (Note that cp_parser_class_or_namespace_name only calls this
10056      function if the token after the name is the scope resolution
10057      operator.)  */
10058   namespace_decl = cp_parser_lookup_name (parser, identifier,
10059                                           none_type,
10060                                           /*is_template=*/false,
10061                                           /*is_namespace=*/true,
10062                                           /*check_dependency=*/true,
10063                                           /*ambiguous_p=*/NULL);
10064   /* If it's not a namespace, issue an error.  */
10065   if (namespace_decl == error_mark_node
10066       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10067     {
10068       cp_parser_error (parser, "expected namespace-name");
10069       namespace_decl = error_mark_node;
10070     }
10071
10072   return namespace_decl;
10073 }
10074
10075 /* Parse a namespace-definition.
10076
10077    namespace-definition:
10078      named-namespace-definition
10079      unnamed-namespace-definition
10080
10081    named-namespace-definition:
10082      original-namespace-definition
10083      extension-namespace-definition
10084
10085    original-namespace-definition:
10086      namespace identifier { namespace-body }
10087
10088    extension-namespace-definition:
10089      namespace original-namespace-name { namespace-body }
10090
10091    unnamed-namespace-definition:
10092      namespace { namespace-body } */
10093
10094 static void
10095 cp_parser_namespace_definition (cp_parser* parser)
10096 {
10097   tree identifier;
10098
10099   /* Look for the `namespace' keyword.  */
10100   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10101
10102   /* Get the name of the namespace.  We do not attempt to distinguish
10103      between an original-namespace-definition and an
10104      extension-namespace-definition at this point.  The semantic
10105      analysis routines are responsible for that.  */
10106   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10107     identifier = cp_parser_identifier (parser);
10108   else
10109     identifier = NULL_TREE;
10110
10111   /* Look for the `{' to start the namespace.  */
10112   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10113   /* Start the namespace.  */
10114   push_namespace (identifier);
10115   /* Parse the body of the namespace.  */
10116   cp_parser_namespace_body (parser);
10117   /* Finish the namespace.  */
10118   pop_namespace ();
10119   /* Look for the final `}'.  */
10120   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10121 }
10122
10123 /* Parse a namespace-body.
10124
10125    namespace-body:
10126      declaration-seq [opt]  */
10127
10128 static void
10129 cp_parser_namespace_body (cp_parser* parser)
10130 {
10131   cp_parser_declaration_seq_opt (parser);
10132 }
10133
10134 /* Parse a namespace-alias-definition.
10135
10136    namespace-alias-definition:
10137      namespace identifier = qualified-namespace-specifier ;  */
10138
10139 static void
10140 cp_parser_namespace_alias_definition (cp_parser* parser)
10141 {
10142   tree identifier;
10143   tree namespace_specifier;
10144
10145   /* Look for the `namespace' keyword.  */
10146   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10147   /* Look for the identifier.  */
10148   identifier = cp_parser_identifier (parser);
10149   if (identifier == error_mark_node)
10150     return;
10151   /* Look for the `=' token.  */
10152   cp_parser_require (parser, CPP_EQ, "`='");
10153   /* Look for the qualified-namespace-specifier.  */
10154   namespace_specifier
10155     = cp_parser_qualified_namespace_specifier (parser);
10156   /* Look for the `;' token.  */
10157   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10158
10159   /* Register the alias in the symbol table.  */
10160   do_namespace_alias (identifier, namespace_specifier);
10161 }
10162
10163 /* Parse a qualified-namespace-specifier.
10164
10165    qualified-namespace-specifier:
10166      :: [opt] nested-name-specifier [opt] namespace-name
10167
10168    Returns a NAMESPACE_DECL corresponding to the specified
10169    namespace.  */
10170
10171 static tree
10172 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10173 {
10174   /* Look for the optional `::'.  */
10175   cp_parser_global_scope_opt (parser,
10176                               /*current_scope_valid_p=*/false);
10177
10178   /* Look for the optional nested-name-specifier.  */
10179   cp_parser_nested_name_specifier_opt (parser,
10180                                        /*typename_keyword_p=*/false,
10181                                        /*check_dependency_p=*/true,
10182                                        /*type_p=*/false,
10183                                        /*is_declaration=*/true);
10184
10185   return cp_parser_namespace_name (parser);
10186 }
10187
10188 /* Parse a using-declaration.
10189
10190    using-declaration:
10191      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10192      using :: unqualified-id ;  */
10193
10194 static void
10195 cp_parser_using_declaration (cp_parser* parser)
10196 {
10197   cp_token *token;
10198   bool typename_p = false;
10199   bool global_scope_p;
10200   tree decl;
10201   tree identifier;
10202   tree qscope;
10203
10204   /* Look for the `using' keyword.  */
10205   cp_parser_require_keyword (parser, RID_USING, "`using'");
10206
10207   /* Peek at the next token.  */
10208   token = cp_lexer_peek_token (parser->lexer);
10209   /* See if it's `typename'.  */
10210   if (token->keyword == RID_TYPENAME)
10211     {
10212       /* Remember that we've seen it.  */
10213       typename_p = true;
10214       /* Consume the `typename' token.  */
10215       cp_lexer_consume_token (parser->lexer);
10216     }
10217
10218   /* Look for the optional global scope qualification.  */
10219   global_scope_p
10220     = (cp_parser_global_scope_opt (parser,
10221                                    /*current_scope_valid_p=*/false)
10222        != NULL_TREE);
10223
10224   /* If we saw `typename', or didn't see `::', then there must be a
10225      nested-name-specifier present.  */
10226   if (typename_p || !global_scope_p)
10227     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10228                                               /*check_dependency_p=*/true,
10229                                               /*type_p=*/false,
10230                                               /*is_declaration=*/true);
10231   /* Otherwise, we could be in either of the two productions.  In that
10232      case, treat the nested-name-specifier as optional.  */
10233   else
10234     qscope = cp_parser_nested_name_specifier_opt (parser,
10235                                                   /*typename_keyword_p=*/false,
10236                                                   /*check_dependency_p=*/true,
10237                                                   /*type_p=*/false,
10238                                                   /*is_declaration=*/true);
10239   if (!qscope)
10240     qscope = global_namespace;
10241
10242   /* Parse the unqualified-id.  */
10243   identifier = cp_parser_unqualified_id (parser,
10244                                          /*template_keyword_p=*/false,
10245                                          /*check_dependency_p=*/true,
10246                                          /*declarator_p=*/true);
10247
10248   /* The function we call to handle a using-declaration is different
10249      depending on what scope we are in.  */
10250   if (identifier == error_mark_node)
10251     ;
10252   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10253            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10254     /* [namespace.udecl]
10255
10256        A using declaration shall not name a template-id.  */
10257     error ("a template-id may not appear in a using-declaration");
10258   else
10259     {
10260       if (at_class_scope_p ())
10261         {
10262           /* Create the USING_DECL.  */
10263           decl = do_class_using_decl (parser->scope, identifier);
10264           /* Add it to the list of members in this class.  */
10265           finish_member_declaration (decl);
10266         }
10267       else
10268         {
10269           decl = cp_parser_lookup_name_simple (parser, identifier);
10270           if (decl == error_mark_node)
10271             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10272           else if (!at_namespace_scope_p ())
10273             do_local_using_decl (decl, qscope, identifier);
10274           else
10275             do_toplevel_using_decl (decl, qscope, identifier);
10276         }
10277     }
10278
10279   /* Look for the final `;'.  */
10280   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10281 }
10282
10283 /* Parse a using-directive.
10284
10285    using-directive:
10286      using namespace :: [opt] nested-name-specifier [opt]
10287        namespace-name ;  */
10288
10289 static void
10290 cp_parser_using_directive (cp_parser* parser)
10291 {
10292   tree namespace_decl;
10293   tree attribs;
10294
10295   /* Look for the `using' keyword.  */
10296   cp_parser_require_keyword (parser, RID_USING, "`using'");
10297   /* And the `namespace' keyword.  */
10298   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10299   /* Look for the optional `::' operator.  */
10300   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10301   /* And the optional nested-name-specifier.  */
10302   cp_parser_nested_name_specifier_opt (parser,
10303                                        /*typename_keyword_p=*/false,
10304                                        /*check_dependency_p=*/true,
10305                                        /*type_p=*/false,
10306                                        /*is_declaration=*/true);
10307   /* Get the namespace being used.  */
10308   namespace_decl = cp_parser_namespace_name (parser);
10309   /* And any specified attributes.  */
10310   attribs = cp_parser_attributes_opt (parser);
10311   /* Update the symbol table.  */
10312   parse_using_directive (namespace_decl, attribs);
10313   /* Look for the final `;'.  */
10314   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10315 }
10316
10317 /* Parse an asm-definition.
10318
10319    asm-definition:
10320      asm ( string-literal ) ;
10321
10322    GNU Extension:
10323
10324    asm-definition:
10325      asm volatile [opt] ( string-literal ) ;
10326      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10327      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10328                           : asm-operand-list [opt] ) ;
10329      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10330                           : asm-operand-list [opt]
10331                           : asm-operand-list [opt] ) ;  */
10332
10333 static void
10334 cp_parser_asm_definition (cp_parser* parser)
10335 {
10336   tree string;
10337   tree outputs = NULL_TREE;
10338   tree inputs = NULL_TREE;
10339   tree clobbers = NULL_TREE;
10340   tree asm_stmt;
10341   bool volatile_p = false;
10342   bool extended_p = false;
10343
10344   /* Look for the `asm' keyword.  */
10345   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10346   /* See if the next token is `volatile'.  */
10347   if (cp_parser_allow_gnu_extensions_p (parser)
10348       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10349     {
10350       /* Remember that we saw the `volatile' keyword.  */
10351       volatile_p = true;
10352       /* Consume the token.  */
10353       cp_lexer_consume_token (parser->lexer);
10354     }
10355   /* Look for the opening `('.  */
10356   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10357     return;
10358   /* Look for the string.  */
10359   string = cp_parser_string_literal (parser, false, false);
10360   if (string == error_mark_node)
10361     {
10362       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10363                                              /*consume_paren=*/true);
10364       return;
10365     }
10366
10367   /* If we're allowing GNU extensions, check for the extended assembly
10368      syntax.  Unfortunately, the `:' tokens need not be separated by
10369      a space in C, and so, for compatibility, we tolerate that here
10370      too.  Doing that means that we have to treat the `::' operator as
10371      two `:' tokens.  */
10372   if (cp_parser_allow_gnu_extensions_p (parser)
10373       && at_function_scope_p ()
10374       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10375           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10376     {
10377       bool inputs_p = false;
10378       bool clobbers_p = false;
10379
10380       /* The extended syntax was used.  */
10381       extended_p = true;
10382
10383       /* Look for outputs.  */
10384       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10385         {
10386           /* Consume the `:'.  */
10387           cp_lexer_consume_token (parser->lexer);
10388           /* Parse the output-operands.  */
10389           if (cp_lexer_next_token_is_not (parser->lexer,
10390                                           CPP_COLON)
10391               && cp_lexer_next_token_is_not (parser->lexer,
10392                                              CPP_SCOPE)
10393               && cp_lexer_next_token_is_not (parser->lexer,
10394                                              CPP_CLOSE_PAREN))
10395             outputs = cp_parser_asm_operand_list (parser);
10396         }
10397       /* If the next token is `::', there are no outputs, and the
10398          next token is the beginning of the inputs.  */
10399       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10400         /* The inputs are coming next.  */
10401         inputs_p = true;
10402
10403       /* Look for inputs.  */
10404       if (inputs_p
10405           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10406         {
10407           /* Consume the `:' or `::'.  */
10408           cp_lexer_consume_token (parser->lexer);
10409           /* Parse the output-operands.  */
10410           if (cp_lexer_next_token_is_not (parser->lexer,
10411                                           CPP_COLON)
10412               && cp_lexer_next_token_is_not (parser->lexer,
10413                                              CPP_CLOSE_PAREN))
10414             inputs = cp_parser_asm_operand_list (parser);
10415         }
10416       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10417         /* The clobbers are coming next.  */
10418         clobbers_p = true;
10419
10420       /* Look for clobbers.  */
10421       if (clobbers_p
10422           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10423         {
10424           /* Consume the `:' or `::'.  */
10425           cp_lexer_consume_token (parser->lexer);
10426           /* Parse the clobbers.  */
10427           if (cp_lexer_next_token_is_not (parser->lexer,
10428                                           CPP_CLOSE_PAREN))
10429             clobbers = cp_parser_asm_clobber_list (parser);
10430         }
10431     }
10432   /* Look for the closing `)'.  */
10433   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10434     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10435                                            /*consume_paren=*/true);
10436   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10437
10438   /* Create the ASM_EXPR.  */
10439   if (at_function_scope_p ())
10440     {
10441       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10442                                   inputs, clobbers);
10443       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10444       if (!extended_p)
10445         {
10446           tree temp = asm_stmt;
10447           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10448             temp = TREE_OPERAND (temp, 0);
10449           
10450           ASM_INPUT_P (temp) = 1;
10451         }
10452     }
10453   else
10454     assemble_asm (string);
10455 }
10456
10457 /* Declarators [gram.dcl.decl] */
10458
10459 /* Parse an init-declarator.
10460
10461    init-declarator:
10462      declarator initializer [opt]
10463
10464    GNU Extension:
10465
10466    init-declarator:
10467      declarator asm-specification [opt] attributes [opt] initializer [opt]
10468
10469    function-definition:
10470      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10471        function-body
10472      decl-specifier-seq [opt] declarator function-try-block
10473
10474    GNU Extension:
10475
10476    function-definition:
10477      __extension__ function-definition
10478
10479    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10480    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10481    then this declarator appears in a class scope.  The new DECL created
10482    by this declarator is returned.
10483
10484    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10485    for a function-definition here as well.  If the declarator is a
10486    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10487    be TRUE upon return.  By that point, the function-definition will
10488    have been completely parsed.
10489
10490    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10491    is FALSE.  */
10492
10493 static tree
10494 cp_parser_init_declarator (cp_parser* parser,
10495                            cp_decl_specifier_seq *decl_specifiers,
10496                            bool function_definition_allowed_p,
10497                            bool member_p,
10498                            int declares_class_or_enum,
10499                            bool* function_definition_p)
10500 {
10501   cp_token *token;
10502   cp_declarator *declarator;
10503   tree prefix_attributes;
10504   tree attributes;
10505   tree asm_specification;
10506   tree initializer;
10507   tree decl = NULL_TREE;
10508   tree scope;
10509   bool is_initialized;
10510   bool is_parenthesized_init;
10511   bool is_non_constant_init;
10512   int ctor_dtor_or_conv_p;
10513   bool friend_p;
10514   tree pushed_scope = NULL;
10515
10516   /* Gather the attributes that were provided with the
10517      decl-specifiers.  */
10518   prefix_attributes = decl_specifiers->attributes;
10519
10520   /* Assume that this is not the declarator for a function
10521      definition.  */
10522   if (function_definition_p)
10523     *function_definition_p = false;
10524
10525   /* Defer access checks while parsing the declarator; we cannot know
10526      what names are accessible until we know what is being
10527      declared.  */
10528   resume_deferring_access_checks ();
10529
10530   /* Parse the declarator.  */
10531   declarator
10532     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10533                             &ctor_dtor_or_conv_p,
10534                             /*parenthesized_p=*/NULL,
10535                             /*member_p=*/false);
10536   /* Gather up the deferred checks.  */
10537   stop_deferring_access_checks ();
10538
10539   /* If the DECLARATOR was erroneous, there's no need to go
10540      further.  */
10541   if (declarator == cp_error_declarator)
10542     return error_mark_node;
10543
10544   if (declares_class_or_enum & 2)
10545     cp_parser_check_for_definition_in_return_type (declarator,
10546                                                    decl_specifiers->type);
10547
10548   /* Figure out what scope the entity declared by the DECLARATOR is
10549      located in.  `grokdeclarator' sometimes changes the scope, so
10550      we compute it now.  */
10551   scope = get_scope_of_declarator (declarator);
10552
10553   /* If we're allowing GNU extensions, look for an asm-specification
10554      and attributes.  */
10555   if (cp_parser_allow_gnu_extensions_p (parser))
10556     {
10557       /* Look for an asm-specification.  */
10558       asm_specification = cp_parser_asm_specification_opt (parser);
10559       /* And attributes.  */
10560       attributes = cp_parser_attributes_opt (parser);
10561     }
10562   else
10563     {
10564       asm_specification = NULL_TREE;
10565       attributes = NULL_TREE;
10566     }
10567
10568   /* Peek at the next token.  */
10569   token = cp_lexer_peek_token (parser->lexer);
10570   /* Check to see if the token indicates the start of a
10571      function-definition.  */
10572   if (cp_parser_token_starts_function_definition_p (token))
10573     {
10574       if (!function_definition_allowed_p)
10575         {
10576           /* If a function-definition should not appear here, issue an
10577              error message.  */
10578           cp_parser_error (parser,
10579                            "a function-definition is not allowed here");
10580           return error_mark_node;
10581         }
10582       else
10583         {
10584           /* Neither attributes nor an asm-specification are allowed
10585              on a function-definition.  */
10586           if (asm_specification)
10587             error ("an asm-specification is not allowed on a function-definition");
10588           if (attributes)
10589             error ("attributes are not allowed on a function-definition");
10590           /* This is a function-definition.  */
10591           *function_definition_p = true;
10592
10593           /* Parse the function definition.  */
10594           if (member_p)
10595             decl = cp_parser_save_member_function_body (parser,
10596                                                         decl_specifiers,
10597                                                         declarator,
10598                                                         prefix_attributes);
10599           else
10600             decl
10601               = (cp_parser_function_definition_from_specifiers_and_declarator
10602                  (parser, decl_specifiers, prefix_attributes, declarator));
10603
10604           return decl;
10605         }
10606     }
10607
10608   /* [dcl.dcl]
10609
10610      Only in function declarations for constructors, destructors, and
10611      type conversions can the decl-specifier-seq be omitted.
10612
10613      We explicitly postpone this check past the point where we handle
10614      function-definitions because we tolerate function-definitions
10615      that are missing their return types in some modes.  */
10616   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10617     {
10618       cp_parser_error (parser,
10619                        "expected constructor, destructor, or type conversion");
10620       return error_mark_node;
10621     }
10622
10623   /* An `=' or an `(' indicates an initializer.  */
10624   is_initialized = (token->type == CPP_EQ
10625                      || token->type == CPP_OPEN_PAREN);
10626   /* If the init-declarator isn't initialized and isn't followed by a
10627      `,' or `;', it's not a valid init-declarator.  */
10628   if (!is_initialized
10629       && token->type != CPP_COMMA
10630       && token->type != CPP_SEMICOLON)
10631     {
10632       cp_parser_error (parser, "expected initializer");
10633       return error_mark_node;
10634     }
10635
10636   /* Because start_decl has side-effects, we should only call it if we
10637      know we're going ahead.  By this point, we know that we cannot
10638      possibly be looking at any other construct.  */
10639   cp_parser_commit_to_tentative_parse (parser);
10640
10641   /* If the decl specifiers were bad, issue an error now that we're
10642      sure this was intended to be a declarator.  Then continue
10643      declaring the variable(s), as int, to try to cut down on further
10644      errors.  */
10645   if (decl_specifiers->any_specifiers_p
10646       && decl_specifiers->type == error_mark_node)
10647     {
10648       cp_parser_error (parser, "invalid type in declaration");
10649       decl_specifiers->type = integer_type_node;
10650     }
10651
10652   /* Check to see whether or not this declaration is a friend.  */
10653   friend_p = cp_parser_friend_p (decl_specifiers);
10654
10655   /* Check that the number of template-parameter-lists is OK.  */
10656   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10657     return error_mark_node;
10658
10659   /* Enter the newly declared entry in the symbol table.  If we're
10660      processing a declaration in a class-specifier, we wait until
10661      after processing the initializer.  */
10662   if (!member_p)
10663     {
10664       if (parser->in_unbraced_linkage_specification_p)
10665         {
10666           decl_specifiers->storage_class = sc_extern;
10667           have_extern_spec = false;
10668         }
10669       decl = start_decl (declarator, decl_specifiers,
10670                          is_initialized, attributes, prefix_attributes,
10671                          &pushed_scope);
10672     }
10673   else if (scope)
10674     /* Enter the SCOPE.  That way unqualified names appearing in the
10675        initializer will be looked up in SCOPE.  */
10676     pushed_scope = push_scope (scope);
10677
10678   /* Perform deferred access control checks, now that we know in which
10679      SCOPE the declared entity resides.  */
10680   if (!member_p && decl)
10681     {
10682       tree saved_current_function_decl = NULL_TREE;
10683
10684       /* If the entity being declared is a function, pretend that we
10685          are in its scope.  If it is a `friend', it may have access to
10686          things that would not otherwise be accessible.  */
10687       if (TREE_CODE (decl) == FUNCTION_DECL)
10688         {
10689           saved_current_function_decl = current_function_decl;
10690           current_function_decl = decl;
10691         }
10692
10693       /* Perform the access control checks for the declarator and the
10694          the decl-specifiers.  */
10695       perform_deferred_access_checks ();
10696
10697       /* Restore the saved value.  */
10698       if (TREE_CODE (decl) == FUNCTION_DECL)
10699         current_function_decl = saved_current_function_decl;
10700     }
10701
10702   /* Parse the initializer.  */
10703   if (is_initialized)
10704     initializer = cp_parser_initializer (parser,
10705                                          &is_parenthesized_init,
10706                                          &is_non_constant_init);
10707   else
10708     {
10709       initializer = NULL_TREE;
10710       is_parenthesized_init = false;
10711       is_non_constant_init = true;
10712     }
10713
10714   /* The old parser allows attributes to appear after a parenthesized
10715      initializer.  Mark Mitchell proposed removing this functionality
10716      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10717      attributes -- but ignores them.  */
10718   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10719     if (cp_parser_attributes_opt (parser))
10720       warning ("attributes after parenthesized initializer ignored");
10721
10722   /* For an in-class declaration, use `grokfield' to create the
10723      declaration.  */
10724   if (member_p)
10725     {
10726       if (pushed_scope)
10727         {
10728           pop_scope (pushed_scope);
10729           pushed_scope = false;
10730         }
10731       decl = grokfield (declarator, decl_specifiers,
10732                         initializer, /*asmspec=*/NULL_TREE,
10733                         /*attributes=*/NULL_TREE);
10734       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10735         cp_parser_save_default_args (parser, decl);
10736     }
10737
10738   /* Finish processing the declaration.  But, skip friend
10739      declarations.  */
10740   if (!friend_p && decl && decl != error_mark_node)
10741     {
10742       cp_finish_decl (decl,
10743                       initializer,
10744                       asm_specification,
10745                       /* If the initializer is in parentheses, then this is
10746                          a direct-initialization, which means that an
10747                          `explicit' constructor is OK.  Otherwise, an
10748                          `explicit' constructor cannot be used.  */
10749                       ((is_parenthesized_init || !is_initialized)
10750                      ? 0 : LOOKUP_ONLYCONVERTING));
10751     }
10752   if (!friend_p && pushed_scope)
10753     pop_scope (pushed_scope);
10754
10755   /* Remember whether or not variables were initialized by
10756      constant-expressions.  */
10757   if (decl && TREE_CODE (decl) == VAR_DECL
10758       && is_initialized && !is_non_constant_init)
10759     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10760
10761   return decl;
10762 }
10763
10764 /* Parse a declarator.
10765
10766    declarator:
10767      direct-declarator
10768      ptr-operator declarator
10769
10770    abstract-declarator:
10771      ptr-operator abstract-declarator [opt]
10772      direct-abstract-declarator
10773
10774    GNU Extensions:
10775
10776    declarator:
10777      attributes [opt] direct-declarator
10778      attributes [opt] ptr-operator declarator
10779
10780    abstract-declarator:
10781      attributes [opt] ptr-operator abstract-declarator [opt]
10782      attributes [opt] direct-abstract-declarator
10783
10784    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10785    detect constructor, destructor or conversion operators. It is set
10786    to -1 if the declarator is a name, and +1 if it is a
10787    function. Otherwise it is set to zero. Usually you just want to
10788    test for >0, but internally the negative value is used.
10789
10790    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10791    a decl-specifier-seq unless it declares a constructor, destructor,
10792    or conversion.  It might seem that we could check this condition in
10793    semantic analysis, rather than parsing, but that makes it difficult
10794    to handle something like `f()'.  We want to notice that there are
10795    no decl-specifiers, and therefore realize that this is an
10796    expression, not a declaration.)
10797
10798    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10799    the declarator is a direct-declarator of the form "(...)".  
10800
10801    MEMBER_P is true iff this declarator is a member-declarator.  */
10802
10803 static cp_declarator *
10804 cp_parser_declarator (cp_parser* parser,
10805                       cp_parser_declarator_kind dcl_kind,
10806                       int* ctor_dtor_or_conv_p,
10807                       bool* parenthesized_p,
10808                       bool member_p)
10809 {
10810   cp_token *token;
10811   cp_declarator *declarator;
10812   enum tree_code code;
10813   cp_cv_quals cv_quals;
10814   tree class_type;
10815   tree attributes = NULL_TREE;
10816
10817   /* Assume this is not a constructor, destructor, or type-conversion
10818      operator.  */
10819   if (ctor_dtor_or_conv_p)
10820     *ctor_dtor_or_conv_p = 0;
10821
10822   if (cp_parser_allow_gnu_extensions_p (parser))
10823     attributes = cp_parser_attributes_opt (parser);
10824
10825   /* Peek at the next token.  */
10826   token = cp_lexer_peek_token (parser->lexer);
10827
10828   /* Check for the ptr-operator production.  */
10829   cp_parser_parse_tentatively (parser);
10830   /* Parse the ptr-operator.  */
10831   code = cp_parser_ptr_operator (parser,
10832                                  &class_type,
10833                                  &cv_quals);
10834   /* If that worked, then we have a ptr-operator.  */
10835   if (cp_parser_parse_definitely (parser))
10836     {
10837       /* If a ptr-operator was found, then this declarator was not
10838          parenthesized.  */
10839       if (parenthesized_p)
10840         *parenthesized_p = true;
10841       /* The dependent declarator is optional if we are parsing an
10842          abstract-declarator.  */
10843       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10844         cp_parser_parse_tentatively (parser);
10845
10846       /* Parse the dependent declarator.  */
10847       declarator = cp_parser_declarator (parser, dcl_kind,
10848                                          /*ctor_dtor_or_conv_p=*/NULL,
10849                                          /*parenthesized_p=*/NULL,
10850                                          /*member_p=*/false);
10851
10852       /* If we are parsing an abstract-declarator, we must handle the
10853          case where the dependent declarator is absent.  */
10854       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10855           && !cp_parser_parse_definitely (parser))
10856         declarator = NULL;
10857
10858       /* Build the representation of the ptr-operator.  */
10859       if (class_type)
10860         declarator = make_ptrmem_declarator (cv_quals,
10861                                              class_type,
10862                                              declarator);
10863       else if (code == INDIRECT_REF)
10864         declarator = make_pointer_declarator (cv_quals, declarator);
10865       else
10866         declarator = make_reference_declarator (cv_quals, declarator);
10867     }
10868   /* Everything else is a direct-declarator.  */
10869   else
10870     {
10871       if (parenthesized_p)
10872         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10873                                                    CPP_OPEN_PAREN);
10874       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10875                                                 ctor_dtor_or_conv_p,
10876                                                 member_p);
10877     }
10878
10879   if (attributes && declarator != cp_error_declarator)
10880     declarator->attributes = attributes;
10881
10882   return declarator;
10883 }
10884
10885 /* Parse a direct-declarator or direct-abstract-declarator.
10886
10887    direct-declarator:
10888      declarator-id
10889      direct-declarator ( parameter-declaration-clause )
10890        cv-qualifier-seq [opt]
10891        exception-specification [opt]
10892      direct-declarator [ constant-expression [opt] ]
10893      ( declarator )
10894
10895    direct-abstract-declarator:
10896      direct-abstract-declarator [opt]
10897        ( parameter-declaration-clause )
10898        cv-qualifier-seq [opt]
10899        exception-specification [opt]
10900      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10901      ( abstract-declarator )
10902
10903    Returns a representation of the declarator.  DCL_KIND is
10904    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10905    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10906    we are parsing a direct-declarator.  It is
10907    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10908    of ambiguity we prefer an abstract declarator, as per
10909    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10910    cp_parser_declarator.  */
10911
10912 static cp_declarator *
10913 cp_parser_direct_declarator (cp_parser* parser,
10914                              cp_parser_declarator_kind dcl_kind,
10915                              int* ctor_dtor_or_conv_p,
10916                              bool member_p)
10917 {
10918   cp_token *token;
10919   cp_declarator *declarator = NULL;
10920   tree scope = NULL_TREE;
10921   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10922   bool saved_in_declarator_p = parser->in_declarator_p;
10923   bool first = true;
10924   tree pushed_scope = NULL_TREE;
10925
10926   while (true)
10927     {
10928       /* Peek at the next token.  */
10929       token = cp_lexer_peek_token (parser->lexer);
10930       if (token->type == CPP_OPEN_PAREN)
10931         {
10932           /* This is either a parameter-declaration-clause, or a
10933              parenthesized declarator. When we know we are parsing a
10934              named declarator, it must be a parenthesized declarator
10935              if FIRST is true. For instance, `(int)' is a
10936              parameter-declaration-clause, with an omitted
10937              direct-abstract-declarator. But `((*))', is a
10938              parenthesized abstract declarator. Finally, when T is a
10939              template parameter `(T)' is a
10940              parameter-declaration-clause, and not a parenthesized
10941              named declarator.
10942
10943              We first try and parse a parameter-declaration-clause,
10944              and then try a nested declarator (if FIRST is true).
10945
10946              It is not an error for it not to be a
10947              parameter-declaration-clause, even when FIRST is
10948              false. Consider,
10949
10950                int i (int);
10951                int i (3);
10952
10953              The first is the declaration of a function while the
10954              second is a the definition of a variable, including its
10955              initializer.
10956
10957              Having seen only the parenthesis, we cannot know which of
10958              these two alternatives should be selected.  Even more
10959              complex are examples like:
10960
10961                int i (int (a));
10962                int i (int (3));
10963
10964              The former is a function-declaration; the latter is a
10965              variable initialization.
10966
10967              Thus again, we try a parameter-declaration-clause, and if
10968              that fails, we back out and return.  */
10969
10970           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10971             {
10972               cp_parameter_declarator *params;
10973               unsigned saved_num_template_parameter_lists;
10974
10975               /* In a member-declarator, the only valid interpretation
10976                  of a parenthesis is the start of a
10977                  parameter-declaration-clause.  (It is invalid to
10978                  initialize a static data member with a parenthesized
10979                  initializer; only the "=" form of initialization is
10980                  permitted.)  */
10981               if (!member_p)
10982                 cp_parser_parse_tentatively (parser);
10983
10984               /* Consume the `('.  */
10985               cp_lexer_consume_token (parser->lexer);
10986               if (first)
10987                 {
10988                   /* If this is going to be an abstract declarator, we're
10989                      in a declarator and we can't have default args.  */
10990                   parser->default_arg_ok_p = false;
10991                   parser->in_declarator_p = true;
10992                 }
10993
10994               /* Inside the function parameter list, surrounding
10995                  template-parameter-lists do not apply.  */
10996               saved_num_template_parameter_lists
10997                 = parser->num_template_parameter_lists;
10998               parser->num_template_parameter_lists = 0;
10999
11000               /* Parse the parameter-declaration-clause.  */
11001               params = cp_parser_parameter_declaration_clause (parser);
11002
11003               parser->num_template_parameter_lists
11004                 = saved_num_template_parameter_lists;
11005
11006               /* If all went well, parse the cv-qualifier-seq and the
11007                  exception-specification.  */
11008               if (member_p || cp_parser_parse_definitely (parser))
11009                 {
11010                   cp_cv_quals cv_quals;
11011                   tree exception_specification;
11012
11013                   if (ctor_dtor_or_conv_p)
11014                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11015                   first = false;
11016                   /* Consume the `)'.  */
11017                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11018
11019                   /* Parse the cv-qualifier-seq.  */
11020                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11021                   /* And the exception-specification.  */
11022                   exception_specification
11023                     = cp_parser_exception_specification_opt (parser);
11024
11025                   /* Create the function-declarator.  */
11026                   declarator = make_call_declarator (declarator,
11027                                                      params,
11028                                                      cv_quals,
11029                                                      exception_specification);
11030                   /* Any subsequent parameter lists are to do with
11031                      return type, so are not those of the declared
11032                      function.  */
11033                   parser->default_arg_ok_p = false;
11034
11035                   /* Repeat the main loop.  */
11036                   continue;
11037                 }
11038             }
11039
11040           /* If this is the first, we can try a parenthesized
11041              declarator.  */
11042           if (first)
11043             {
11044               bool saved_in_type_id_in_expr_p;
11045
11046               parser->default_arg_ok_p = saved_default_arg_ok_p;
11047               parser->in_declarator_p = saved_in_declarator_p;
11048
11049               /* Consume the `('.  */
11050               cp_lexer_consume_token (parser->lexer);
11051               /* Parse the nested declarator.  */
11052               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11053               parser->in_type_id_in_expr_p = true;
11054               declarator
11055                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11056                                         /*parenthesized_p=*/NULL,
11057                                         member_p);
11058               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11059               first = false;
11060               /* Expect a `)'.  */
11061               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11062                 declarator = cp_error_declarator;
11063               if (declarator == cp_error_declarator)
11064                 break;
11065
11066               goto handle_declarator;
11067             }
11068           /* Otherwise, we must be done.  */
11069           else
11070             break;
11071         }
11072       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11073                && token->type == CPP_OPEN_SQUARE)
11074         {
11075           /* Parse an array-declarator.  */
11076           tree bounds;
11077
11078           if (ctor_dtor_or_conv_p)
11079             *ctor_dtor_or_conv_p = 0;
11080
11081           first = false;
11082           parser->default_arg_ok_p = false;
11083           parser->in_declarator_p = true;
11084           /* Consume the `['.  */
11085           cp_lexer_consume_token (parser->lexer);
11086           /* Peek at the next token.  */
11087           token = cp_lexer_peek_token (parser->lexer);
11088           /* If the next token is `]', then there is no
11089              constant-expression.  */
11090           if (token->type != CPP_CLOSE_SQUARE)
11091             {
11092               bool non_constant_p;
11093
11094               bounds
11095                 = cp_parser_constant_expression (parser,
11096                                                  /*allow_non_constant=*/true,
11097                                                  &non_constant_p);
11098               if (!non_constant_p)
11099                 bounds = fold_non_dependent_expr (bounds);
11100               else if (!at_function_scope_p ())
11101                 {
11102                   error ("array bound is not an integer constant");
11103                   bounds = error_mark_node;
11104                 }
11105             }
11106           else
11107             bounds = NULL_TREE;
11108           /* Look for the closing `]'.  */
11109           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11110             {
11111               declarator = cp_error_declarator;
11112               break;
11113             }
11114
11115           declarator = make_array_declarator (declarator, bounds);
11116         }
11117       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11118         {
11119           tree qualifying_scope;
11120           tree unqualified_name;
11121
11122           /* Parse a declarator-id */
11123           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11124             cp_parser_parse_tentatively (parser);
11125           unqualified_name = cp_parser_declarator_id (parser);
11126           qualifying_scope = parser->scope;
11127           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11128             {
11129               if (!cp_parser_parse_definitely (parser))
11130                 unqualified_name = error_mark_node;
11131               else if (qualifying_scope
11132                        || (TREE_CODE (unqualified_name) 
11133                            != IDENTIFIER_NODE))
11134                 {
11135                   cp_parser_error (parser, "expected unqualified-id");
11136                   unqualified_name = error_mark_node;
11137                 }
11138             }
11139
11140           if (unqualified_name == error_mark_node)
11141             {
11142               declarator = cp_error_declarator;
11143               break;
11144             }
11145
11146           if (qualifying_scope && at_namespace_scope_p ()
11147               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11148             {
11149               /* In the declaration of a member of a template class
11150                  outside of the class itself, the SCOPE will sometimes
11151                  be a TYPENAME_TYPE.  For example, given:
11152
11153                  template <typename T>
11154                  int S<T>::R::i = 3;
11155
11156                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11157                  this context, we must resolve S<T>::R to an ordinary
11158                  type, rather than a typename type.
11159
11160                  The reason we normally avoid resolving TYPENAME_TYPEs
11161                  is that a specialization of `S' might render
11162                  `S<T>::R' not a type.  However, if `S' is
11163                  specialized, then this `i' will not be used, so there
11164                  is no harm in resolving the types here.  */
11165               tree type;
11166               
11167               /* Resolve the TYPENAME_TYPE.  */
11168               type = resolve_typename_type (qualifying_scope,
11169                                             /*only_current_p=*/false);
11170               /* If that failed, the declarator is invalid.  */
11171               if (type == error_mark_node)
11172                 error ("%<%T::%D%> is not a type",
11173                        TYPE_CONTEXT (qualifying_scope),
11174                        TYPE_IDENTIFIER (qualifying_scope));
11175               qualifying_scope = type;
11176             }
11177
11178           declarator = make_id_declarator (qualifying_scope, 
11179                                            unqualified_name);
11180           if (unqualified_name)
11181             {
11182               tree class_type;
11183
11184               if (qualifying_scope
11185                   && CLASS_TYPE_P (qualifying_scope))
11186                 class_type = qualifying_scope;
11187               else
11188                 class_type = current_class_type;
11189
11190               if (class_type)
11191                 {
11192                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11193                     declarator->u.id.sfk = sfk_destructor;
11194                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11195                     declarator->u.id.sfk = sfk_conversion;
11196                   else if (/* There's no way to declare a constructor
11197                               for an anonymous type, even if the type
11198                               got a name for linkage purposes.  */
11199                            !TYPE_WAS_ANONYMOUS (class_type)
11200                            && (constructor_name_p (unqualified_name,
11201                                                    class_type)
11202                                || (TREE_CODE (unqualified_name) == TYPE_DECL
11203                                    && (same_type_p 
11204                                        (TREE_TYPE (unqualified_name),
11205                                         class_type)))))
11206                     declarator->u.id.sfk = sfk_constructor;
11207
11208                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11209                     *ctor_dtor_or_conv_p = -1;
11210                   if (qualifying_scope
11211                       && TREE_CODE (unqualified_name) == TYPE_DECL
11212                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11213                     {
11214                       error ("invalid use of constructor as a template");
11215                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11216                               "the constructor in a qualified name",
11217                               class_type,
11218                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11219                               class_type, class_type);
11220                     }
11221                 }
11222             }
11223
11224         handle_declarator:;
11225           scope = get_scope_of_declarator (declarator);
11226           if (scope)
11227             /* Any names that appear after the declarator-id for a
11228                member are looked up in the containing scope.  */
11229             pushed_scope = push_scope (scope);
11230           parser->in_declarator_p = true;
11231           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11232               || (declarator && declarator->kind == cdk_id))
11233             /* Default args are only allowed on function
11234                declarations.  */
11235             parser->default_arg_ok_p = saved_default_arg_ok_p;
11236           else
11237             parser->default_arg_ok_p = false;
11238
11239           first = false;
11240         }
11241       /* We're done.  */
11242       else
11243         break;
11244     }
11245
11246   /* For an abstract declarator, we might wind up with nothing at this
11247      point.  That's an error; the declarator is not optional.  */
11248   if (!declarator)
11249     cp_parser_error (parser, "expected declarator");
11250
11251   /* If we entered a scope, we must exit it now.  */
11252   if (pushed_scope)
11253     pop_scope (pushed_scope);
11254
11255   parser->default_arg_ok_p = saved_default_arg_ok_p;
11256   parser->in_declarator_p = saved_in_declarator_p;
11257
11258   return declarator;
11259 }
11260
11261 /* Parse a ptr-operator.
11262
11263    ptr-operator:
11264      * cv-qualifier-seq [opt]
11265      &
11266      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11267
11268    GNU Extension:
11269
11270    ptr-operator:
11271      & cv-qualifier-seq [opt]
11272
11273    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11274    Returns ADDR_EXPR if a reference was used.  In the case of a
11275    pointer-to-member, *TYPE is filled in with the TYPE containing the
11276    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11277    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11278    ERROR_MARK if an error occurred.  */
11279
11280 static enum tree_code
11281 cp_parser_ptr_operator (cp_parser* parser,
11282                         tree* type,
11283                         cp_cv_quals *cv_quals)
11284 {
11285   enum tree_code code = ERROR_MARK;
11286   cp_token *token;
11287
11288   /* Assume that it's not a pointer-to-member.  */
11289   *type = NULL_TREE;
11290   /* And that there are no cv-qualifiers.  */
11291   *cv_quals = TYPE_UNQUALIFIED;
11292
11293   /* Peek at the next token.  */
11294   token = cp_lexer_peek_token (parser->lexer);
11295   /* If it's a `*' or `&' we have a pointer or reference.  */
11296   if (token->type == CPP_MULT || token->type == CPP_AND)
11297     {
11298       /* Remember which ptr-operator we were processing.  */
11299       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11300
11301       /* Consume the `*' or `&'.  */
11302       cp_lexer_consume_token (parser->lexer);
11303
11304       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11305          `&', if we are allowing GNU extensions.  (The only qualifier
11306          that can legally appear after `&' is `restrict', but that is
11307          enforced during semantic analysis.  */
11308       if (code == INDIRECT_REF
11309           || cp_parser_allow_gnu_extensions_p (parser))
11310         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11311     }
11312   else
11313     {
11314       /* Try the pointer-to-member case.  */
11315       cp_parser_parse_tentatively (parser);
11316       /* Look for the optional `::' operator.  */
11317       cp_parser_global_scope_opt (parser,
11318                                   /*current_scope_valid_p=*/false);
11319       /* Look for the nested-name specifier.  */
11320       cp_parser_nested_name_specifier (parser,
11321                                        /*typename_keyword_p=*/false,
11322                                        /*check_dependency_p=*/true,
11323                                        /*type_p=*/false,
11324                                        /*is_declaration=*/false);
11325       /* If we found it, and the next token is a `*', then we are
11326          indeed looking at a pointer-to-member operator.  */
11327       if (!cp_parser_error_occurred (parser)
11328           && cp_parser_require (parser, CPP_MULT, "`*'"))
11329         {
11330           /* The type of which the member is a member is given by the
11331              current SCOPE.  */
11332           *type = parser->scope;
11333           /* The next name will not be qualified.  */
11334           parser->scope = NULL_TREE;
11335           parser->qualifying_scope = NULL_TREE;
11336           parser->object_scope = NULL_TREE;
11337           /* Indicate that the `*' operator was used.  */
11338           code = INDIRECT_REF;
11339           /* Look for the optional cv-qualifier-seq.  */
11340           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11341         }
11342       /* If that didn't work we don't have a ptr-operator.  */
11343       if (!cp_parser_parse_definitely (parser))
11344         cp_parser_error (parser, "expected ptr-operator");
11345     }
11346
11347   return code;
11348 }
11349
11350 /* Parse an (optional) cv-qualifier-seq.
11351
11352    cv-qualifier-seq:
11353      cv-qualifier cv-qualifier-seq [opt]
11354
11355    cv-qualifier:
11356      const
11357      volatile
11358
11359    GNU Extension:
11360
11361    cv-qualifier:
11362      __restrict__
11363
11364    Returns a bitmask representing the cv-qualifiers.  */
11365
11366 static cp_cv_quals
11367 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11368 {
11369   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11370
11371   while (true)
11372     {
11373       cp_token *token;
11374       cp_cv_quals cv_qualifier;
11375
11376       /* Peek at the next token.  */
11377       token = cp_lexer_peek_token (parser->lexer);
11378       /* See if it's a cv-qualifier.  */
11379       switch (token->keyword)
11380         {
11381         case RID_CONST:
11382           cv_qualifier = TYPE_QUAL_CONST;
11383           break;
11384
11385         case RID_VOLATILE:
11386           cv_qualifier = TYPE_QUAL_VOLATILE;
11387           break;
11388
11389         case RID_RESTRICT:
11390           cv_qualifier = TYPE_QUAL_RESTRICT;
11391           break;
11392
11393         default:
11394           cv_qualifier = TYPE_UNQUALIFIED;
11395           break;
11396         }
11397
11398       if (!cv_qualifier)
11399         break;
11400
11401       if (cv_quals & cv_qualifier)
11402         {
11403           error ("duplicate cv-qualifier");
11404           cp_lexer_purge_token (parser->lexer);
11405         }
11406       else
11407         {
11408           cp_lexer_consume_token (parser->lexer);
11409           cv_quals |= cv_qualifier;
11410         }
11411     }
11412
11413   return cv_quals;
11414 }
11415
11416 /* Parse a declarator-id.
11417
11418    declarator-id:
11419      id-expression
11420      :: [opt] nested-name-specifier [opt] type-name
11421
11422    In the `id-expression' case, the value returned is as for
11423    cp_parser_id_expression if the id-expression was an unqualified-id.
11424    If the id-expression was a qualified-id, then a SCOPE_REF is
11425    returned.  The first operand is the scope (either a NAMESPACE_DECL
11426    or TREE_TYPE), but the second is still just a representation of an
11427    unqualified-id.  */
11428
11429 static tree
11430 cp_parser_declarator_id (cp_parser* parser)
11431 {
11432   /* The expression must be an id-expression.  Assume that qualified
11433      names are the names of types so that:
11434
11435        template <class T>
11436        int S<T>::R::i = 3;
11437
11438      will work; we must treat `S<T>::R' as the name of a type.
11439      Similarly, assume that qualified names are templates, where
11440      required, so that:
11441
11442        template <class T>
11443        int S<T>::R<T>::i = 3;
11444
11445      will work, too.  */
11446   return cp_parser_id_expression (parser,
11447                                   /*template_keyword_p=*/false,
11448                                   /*check_dependency_p=*/false,
11449                                   /*template_p=*/NULL,
11450                                   /*declarator_p=*/true);
11451 }
11452
11453 /* Parse a type-id.
11454
11455    type-id:
11456      type-specifier-seq abstract-declarator [opt]
11457
11458    Returns the TYPE specified.  */
11459
11460 static tree
11461 cp_parser_type_id (cp_parser* parser)
11462 {
11463   cp_decl_specifier_seq type_specifier_seq;
11464   cp_declarator *abstract_declarator;
11465
11466   /* Parse the type-specifier-seq.  */
11467   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11468   if (type_specifier_seq.type == error_mark_node)
11469     return error_mark_node;
11470
11471   /* There might or might not be an abstract declarator.  */
11472   cp_parser_parse_tentatively (parser);
11473   /* Look for the declarator.  */
11474   abstract_declarator
11475     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11476                             /*parenthesized_p=*/NULL,
11477                             /*member_p=*/false);
11478   /* Check to see if there really was a declarator.  */
11479   if (!cp_parser_parse_definitely (parser))
11480     abstract_declarator = NULL;
11481
11482   return groktypename (&type_specifier_seq, abstract_declarator);
11483 }
11484
11485 /* Parse a type-specifier-seq.
11486
11487    type-specifier-seq:
11488      type-specifier type-specifier-seq [opt]
11489
11490    GNU extension:
11491
11492    type-specifier-seq:
11493      attributes type-specifier-seq [opt]
11494
11495    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11496
11497 static void
11498 cp_parser_type_specifier_seq (cp_parser* parser,
11499                               cp_decl_specifier_seq *type_specifier_seq)
11500 {
11501   bool seen_type_specifier = false;
11502
11503   /* Clear the TYPE_SPECIFIER_SEQ.  */
11504   clear_decl_specs (type_specifier_seq);
11505
11506   /* Parse the type-specifiers and attributes.  */
11507   while (true)
11508     {
11509       tree type_specifier;
11510
11511       /* Check for attributes first.  */
11512       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11513         {
11514           type_specifier_seq->attributes =
11515             chainon (type_specifier_seq->attributes,
11516                      cp_parser_attributes_opt (parser));
11517           continue;
11518         }
11519
11520       /* Look for the type-specifier.  */
11521       type_specifier = cp_parser_type_specifier (parser,
11522                                                  CP_PARSER_FLAGS_OPTIONAL,
11523                                                  type_specifier_seq,
11524                                                  /*is_declaration=*/false,
11525                                                  NULL,
11526                                                  NULL);
11527       /* If the first type-specifier could not be found, this is not a
11528          type-specifier-seq at all.  */
11529       if (!seen_type_specifier && !type_specifier)
11530         {
11531           cp_parser_error (parser, "expected type-specifier");
11532           type_specifier_seq->type = error_mark_node;
11533           return;
11534         }
11535       /* If subsequent type-specifiers could not be found, the
11536          type-specifier-seq is complete.  */
11537       else if (seen_type_specifier && !type_specifier)
11538         break;
11539
11540       seen_type_specifier = true;
11541     }
11542
11543   return;
11544 }
11545
11546 /* Parse a parameter-declaration-clause.
11547
11548    parameter-declaration-clause:
11549      parameter-declaration-list [opt] ... [opt]
11550      parameter-declaration-list , ...
11551
11552    Returns a representation for the parameter declarations.  A return
11553    value of NULL indicates a parameter-declaration-clause consisting
11554    only of an ellipsis.  */
11555
11556 static cp_parameter_declarator *
11557 cp_parser_parameter_declaration_clause (cp_parser* parser)
11558 {
11559   cp_parameter_declarator *parameters;
11560   cp_token *token;
11561   bool ellipsis_p;
11562   bool is_error;
11563
11564   /* Peek at the next token.  */
11565   token = cp_lexer_peek_token (parser->lexer);
11566   /* Check for trivial parameter-declaration-clauses.  */
11567   if (token->type == CPP_ELLIPSIS)
11568     {
11569       /* Consume the `...' token.  */
11570       cp_lexer_consume_token (parser->lexer);
11571       return NULL;
11572     }
11573   else if (token->type == CPP_CLOSE_PAREN)
11574     /* There are no parameters.  */
11575     {
11576 #ifndef NO_IMPLICIT_EXTERN_C
11577       if (in_system_header && current_class_type == NULL
11578           && current_lang_name == lang_name_c)
11579         return NULL;
11580       else
11581 #endif
11582         return no_parameters;
11583     }
11584   /* Check for `(void)', too, which is a special case.  */
11585   else if (token->keyword == RID_VOID
11586            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11587                == CPP_CLOSE_PAREN))
11588     {
11589       /* Consume the `void' token.  */
11590       cp_lexer_consume_token (parser->lexer);
11591       /* There are no parameters.  */
11592       return no_parameters;
11593     }
11594
11595   /* Parse the parameter-declaration-list.  */
11596   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11597   /* If a parse error occurred while parsing the
11598      parameter-declaration-list, then the entire
11599      parameter-declaration-clause is erroneous.  */
11600   if (is_error)
11601     return NULL;
11602
11603   /* Peek at the next token.  */
11604   token = cp_lexer_peek_token (parser->lexer);
11605   /* If it's a `,', the clause should terminate with an ellipsis.  */
11606   if (token->type == CPP_COMMA)
11607     {
11608       /* Consume the `,'.  */
11609       cp_lexer_consume_token (parser->lexer);
11610       /* Expect an ellipsis.  */
11611       ellipsis_p
11612         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11613     }
11614   /* It might also be `...' if the optional trailing `,' was
11615      omitted.  */
11616   else if (token->type == CPP_ELLIPSIS)
11617     {
11618       /* Consume the `...' token.  */
11619       cp_lexer_consume_token (parser->lexer);
11620       /* And remember that we saw it.  */
11621       ellipsis_p = true;
11622     }
11623   else
11624     ellipsis_p = false;
11625
11626   /* Finish the parameter list.  */
11627   if (parameters && ellipsis_p)
11628     parameters->ellipsis_p = true;
11629
11630   return parameters;
11631 }
11632
11633 /* Parse a parameter-declaration-list.
11634
11635    parameter-declaration-list:
11636      parameter-declaration
11637      parameter-declaration-list , parameter-declaration
11638
11639    Returns a representation of the parameter-declaration-list, as for
11640    cp_parser_parameter_declaration_clause.  However, the
11641    `void_list_node' is never appended to the list.  Upon return,
11642    *IS_ERROR will be true iff an error occurred.  */
11643
11644 static cp_parameter_declarator *
11645 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11646 {
11647   cp_parameter_declarator *parameters = NULL;
11648   cp_parameter_declarator **tail = &parameters;
11649
11650   /* Assume all will go well.  */
11651   *is_error = false;
11652
11653   /* Look for more parameters.  */
11654   while (true)
11655     {
11656       cp_parameter_declarator *parameter;
11657       bool parenthesized_p;
11658       /* Parse the parameter.  */
11659       parameter
11660         = cp_parser_parameter_declaration (parser,
11661                                            /*template_parm_p=*/false,
11662                                            &parenthesized_p);
11663
11664       /* If a parse error occurred parsing the parameter declaration,
11665          then the entire parameter-declaration-list is erroneous.  */
11666       if (!parameter)
11667         {
11668           *is_error = true;
11669           parameters = NULL;
11670           break;
11671         }
11672       /* Add the new parameter to the list.  */
11673       *tail = parameter;
11674       tail = &parameter->next;
11675
11676       /* Peek at the next token.  */
11677       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11678           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11679         /* The parameter-declaration-list is complete.  */
11680         break;
11681       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11682         {
11683           cp_token *token;
11684
11685           /* Peek at the next token.  */
11686           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11687           /* If it's an ellipsis, then the list is complete.  */
11688           if (token->type == CPP_ELLIPSIS)
11689             break;
11690           /* Otherwise, there must be more parameters.  Consume the
11691              `,'.  */
11692           cp_lexer_consume_token (parser->lexer);
11693           /* When parsing something like:
11694
11695                 int i(float f, double d)
11696
11697              we can tell after seeing the declaration for "f" that we
11698              are not looking at an initialization of a variable "i",
11699              but rather at the declaration of a function "i".
11700
11701              Due to the fact that the parsing of template arguments
11702              (as specified to a template-id) requires backtracking we
11703              cannot use this technique when inside a template argument
11704              list.  */
11705           if (!parser->in_template_argument_list_p
11706               && !parser->in_type_id_in_expr_p
11707               && cp_parser_uncommitted_to_tentative_parse_p (parser)
11708               /* However, a parameter-declaration of the form
11709                  "foat(f)" (which is a valid declaration of a
11710                  parameter "f") can also be interpreted as an
11711                  expression (the conversion of "f" to "float").  */
11712               && !parenthesized_p)
11713             cp_parser_commit_to_tentative_parse (parser);
11714         }
11715       else
11716         {
11717           cp_parser_error (parser, "expected %<,%> or %<...%>");
11718           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11719             cp_parser_skip_to_closing_parenthesis (parser,
11720                                                    /*recovering=*/true,
11721                                                    /*or_comma=*/false,
11722                                                    /*consume_paren=*/false);
11723           break;
11724         }
11725     }
11726
11727   return parameters;
11728 }
11729
11730 /* Parse a parameter declaration.
11731
11732    parameter-declaration:
11733      decl-specifier-seq declarator
11734      decl-specifier-seq declarator = assignment-expression
11735      decl-specifier-seq abstract-declarator [opt]
11736      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11737
11738    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11739    declares a template parameter.  (In that case, a non-nested `>'
11740    token encountered during the parsing of the assignment-expression
11741    is not interpreted as a greater-than operator.)
11742
11743    Returns a representation of the parameter, or NULL if an error
11744    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11745    true iff the declarator is of the form "(p)".  */
11746
11747 static cp_parameter_declarator *
11748 cp_parser_parameter_declaration (cp_parser *parser,
11749                                  bool template_parm_p,
11750                                  bool *parenthesized_p)
11751 {
11752   int declares_class_or_enum;
11753   bool greater_than_is_operator_p;
11754   cp_decl_specifier_seq decl_specifiers;
11755   cp_declarator *declarator;
11756   tree default_argument;
11757   cp_token *token;
11758   const char *saved_message;
11759
11760   /* In a template parameter, `>' is not an operator.
11761
11762      [temp.param]
11763
11764      When parsing a default template-argument for a non-type
11765      template-parameter, the first non-nested `>' is taken as the end
11766      of the template parameter-list rather than a greater-than
11767      operator.  */
11768   greater_than_is_operator_p = !template_parm_p;
11769
11770   /* Type definitions may not appear in parameter types.  */
11771   saved_message = parser->type_definition_forbidden_message;
11772   parser->type_definition_forbidden_message
11773     = "types may not be defined in parameter types";
11774
11775   /* Parse the declaration-specifiers.  */
11776   cp_parser_decl_specifier_seq (parser,
11777                                 CP_PARSER_FLAGS_NONE,
11778                                 &decl_specifiers,
11779                                 &declares_class_or_enum);
11780   /* If an error occurred, there's no reason to attempt to parse the
11781      rest of the declaration.  */
11782   if (cp_parser_error_occurred (parser))
11783     {
11784       parser->type_definition_forbidden_message = saved_message;
11785       return NULL;
11786     }
11787
11788   /* Peek at the next token.  */
11789   token = cp_lexer_peek_token (parser->lexer);
11790   /* If the next token is a `)', `,', `=', `>', or `...', then there
11791      is no declarator.  */
11792   if (token->type == CPP_CLOSE_PAREN
11793       || token->type == CPP_COMMA
11794       || token->type == CPP_EQ
11795       || token->type == CPP_ELLIPSIS
11796       || token->type == CPP_GREATER)
11797     {
11798       declarator = NULL;
11799       if (parenthesized_p)
11800         *parenthesized_p = false;
11801     }
11802   /* Otherwise, there should be a declarator.  */
11803   else
11804     {
11805       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11806       parser->default_arg_ok_p = false;
11807
11808       /* After seeing a decl-specifier-seq, if the next token is not a
11809          "(", there is no possibility that the code is a valid
11810          expression.  Therefore, if parsing tentatively, we commit at
11811          this point.  */
11812       if (!parser->in_template_argument_list_p
11813           /* In an expression context, having seen:
11814
11815                (int((char ...
11816
11817              we cannot be sure whether we are looking at a
11818              function-type (taking a "char" as a parameter) or a cast
11819              of some object of type "char" to "int".  */
11820           && !parser->in_type_id_in_expr_p
11821           && cp_parser_uncommitted_to_tentative_parse_p (parser)
11822           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11823         cp_parser_commit_to_tentative_parse (parser);
11824       /* Parse the declarator.  */
11825       declarator = cp_parser_declarator (parser,
11826                                          CP_PARSER_DECLARATOR_EITHER,
11827                                          /*ctor_dtor_or_conv_p=*/NULL,
11828                                          parenthesized_p,
11829                                          /*member_p=*/false);
11830       parser->default_arg_ok_p = saved_default_arg_ok_p;
11831       /* After the declarator, allow more attributes.  */
11832       decl_specifiers.attributes
11833         = chainon (decl_specifiers.attributes,
11834                    cp_parser_attributes_opt (parser));
11835     }
11836
11837   /* The restriction on defining new types applies only to the type
11838      of the parameter, not to the default argument.  */
11839   parser->type_definition_forbidden_message = saved_message;
11840
11841   /* If the next token is `=', then process a default argument.  */
11842   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11843     {
11844       bool saved_greater_than_is_operator_p;
11845       /* Consume the `='.  */
11846       cp_lexer_consume_token (parser->lexer);
11847
11848       /* If we are defining a class, then the tokens that make up the
11849          default argument must be saved and processed later.  */
11850       if (!template_parm_p && at_class_scope_p ()
11851           && TYPE_BEING_DEFINED (current_class_type))
11852         {
11853           unsigned depth = 0;
11854           cp_token *first_token;
11855           cp_token *token;
11856
11857           /* Add tokens until we have processed the entire default
11858              argument.  We add the range [first_token, token).  */
11859           first_token = cp_lexer_peek_token (parser->lexer);
11860           while (true)
11861             {
11862               bool done = false;
11863
11864               /* Peek at the next token.  */
11865               token = cp_lexer_peek_token (parser->lexer);
11866               /* What we do depends on what token we have.  */
11867               switch (token->type)
11868                 {
11869                   /* In valid code, a default argument must be
11870                      immediately followed by a `,' `)', or `...'.  */
11871                 case CPP_COMMA:
11872                 case CPP_CLOSE_PAREN:
11873                 case CPP_ELLIPSIS:
11874                   /* If we run into a non-nested `;', `}', or `]',
11875                      then the code is invalid -- but the default
11876                      argument is certainly over.  */
11877                 case CPP_SEMICOLON:
11878                 case CPP_CLOSE_BRACE:
11879                 case CPP_CLOSE_SQUARE:
11880                   if (depth == 0)
11881                     done = true;
11882                   /* Update DEPTH, if necessary.  */
11883                   else if (token->type == CPP_CLOSE_PAREN
11884                            || token->type == CPP_CLOSE_BRACE
11885                            || token->type == CPP_CLOSE_SQUARE)
11886                     --depth;
11887                   break;
11888
11889                 case CPP_OPEN_PAREN:
11890                 case CPP_OPEN_SQUARE:
11891                 case CPP_OPEN_BRACE:
11892                   ++depth;
11893                   break;
11894
11895                 case CPP_GREATER:
11896                   /* If we see a non-nested `>', and `>' is not an
11897                      operator, then it marks the end of the default
11898                      argument.  */
11899                   if (!depth && !greater_than_is_operator_p)
11900                     done = true;
11901                   break;
11902
11903                   /* If we run out of tokens, issue an error message.  */
11904                 case CPP_EOF:
11905                   error ("file ends in default argument");
11906                   done = true;
11907                   break;
11908
11909                 case CPP_NAME:
11910                 case CPP_SCOPE:
11911                   /* In these cases, we should look for template-ids.
11912                      For example, if the default argument is
11913                      `X<int, double>()', we need to do name lookup to
11914                      figure out whether or not `X' is a template; if
11915                      so, the `,' does not end the default argument.
11916
11917                      That is not yet done.  */
11918                   break;
11919
11920                 default:
11921                   break;
11922                 }
11923
11924               /* If we've reached the end, stop.  */
11925               if (done)
11926                 break;
11927
11928               /* Add the token to the token block.  */
11929               token = cp_lexer_consume_token (parser->lexer);
11930             }
11931
11932           /* Create a DEFAULT_ARG to represented the unparsed default
11933              argument.  */
11934           default_argument = make_node (DEFAULT_ARG);
11935           DEFARG_TOKENS (default_argument)
11936             = cp_token_cache_new (first_token, token);  
11937         }
11938       /* Outside of a class definition, we can just parse the
11939          assignment-expression.  */
11940       else
11941         {
11942           bool saved_local_variables_forbidden_p;
11943
11944           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11945              set correctly.  */
11946           saved_greater_than_is_operator_p
11947             = parser->greater_than_is_operator_p;
11948           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11949           /* Local variable names (and the `this' keyword) may not
11950              appear in a default argument.  */
11951           saved_local_variables_forbidden_p
11952             = parser->local_variables_forbidden_p;
11953           parser->local_variables_forbidden_p = true;
11954           /* Parse the assignment-expression.  */
11955           default_argument 
11956             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
11957           /* Restore saved state.  */
11958           parser->greater_than_is_operator_p
11959             = saved_greater_than_is_operator_p;
11960           parser->local_variables_forbidden_p
11961             = saved_local_variables_forbidden_p;
11962         }
11963       if (!parser->default_arg_ok_p)
11964         {
11965           if (!flag_pedantic_errors)
11966             warning ("deprecated use of default argument for parameter of non-function");
11967           else
11968             {
11969               error ("default arguments are only permitted for function parameters");
11970               default_argument = NULL_TREE;
11971             }
11972         }
11973     }
11974   else
11975     default_argument = NULL_TREE;
11976
11977   return make_parameter_declarator (&decl_specifiers,
11978                                     declarator,
11979                                     default_argument);
11980 }
11981
11982 /* Parse a function-body.
11983
11984    function-body:
11985      compound_statement  */
11986
11987 static void
11988 cp_parser_function_body (cp_parser *parser)
11989 {
11990   cp_parser_compound_statement (parser, NULL, false);
11991 }
11992
11993 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11994    true if a ctor-initializer was present.  */
11995
11996 static bool
11997 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11998 {
11999   tree body;
12000   bool ctor_initializer_p;
12001
12002   /* Begin the function body.  */
12003   body = begin_function_body ();
12004   /* Parse the optional ctor-initializer.  */
12005   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12006   /* Parse the function-body.  */
12007   cp_parser_function_body (parser);
12008   /* Finish the function body.  */
12009   finish_function_body (body);
12010
12011   return ctor_initializer_p;
12012 }
12013
12014 /* Parse an initializer.
12015
12016    initializer:
12017      = initializer-clause
12018      ( expression-list )
12019
12020    Returns a expression representing the initializer.  If no
12021    initializer is present, NULL_TREE is returned.
12022
12023    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12024    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12025    set to FALSE if there is no initializer present.  If there is an
12026    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12027    is set to true; otherwise it is set to false.  */
12028
12029 static tree
12030 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12031                        bool* non_constant_p)
12032 {
12033   cp_token *token;
12034   tree init;
12035
12036   /* Peek at the next token.  */
12037   token = cp_lexer_peek_token (parser->lexer);
12038
12039   /* Let our caller know whether or not this initializer was
12040      parenthesized.  */
12041   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12042   /* Assume that the initializer is constant.  */
12043   *non_constant_p = false;
12044
12045   if (token->type == CPP_EQ)
12046     {
12047       /* Consume the `='.  */
12048       cp_lexer_consume_token (parser->lexer);
12049       /* Parse the initializer-clause.  */
12050       init = cp_parser_initializer_clause (parser, non_constant_p);
12051     }
12052   else if (token->type == CPP_OPEN_PAREN)
12053     init = cp_parser_parenthesized_expression_list (parser, false,
12054                                                     /*cast_p=*/false,
12055                                                     non_constant_p);
12056   else
12057     {
12058       /* Anything else is an error.  */
12059       cp_parser_error (parser, "expected initializer");
12060       init = error_mark_node;
12061     }
12062
12063   return init;
12064 }
12065
12066 /* Parse an initializer-clause.
12067
12068    initializer-clause:
12069      assignment-expression
12070      { initializer-list , [opt] }
12071      { }
12072
12073    Returns an expression representing the initializer.
12074
12075    If the `assignment-expression' production is used the value
12076    returned is simply a representation for the expression.
12077
12078    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12079    the elements of the initializer-list (or NULL_TREE, if the last
12080    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12081    NULL_TREE.  There is no way to detect whether or not the optional
12082    trailing `,' was provided.  NON_CONSTANT_P is as for
12083    cp_parser_initializer.  */
12084
12085 static tree
12086 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12087 {
12088   tree initializer;
12089
12090   /* Assume the expression is constant.  */
12091   *non_constant_p = false;
12092
12093   /* If it is not a `{', then we are looking at an
12094      assignment-expression.  */
12095   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12096     {
12097       initializer
12098         = cp_parser_constant_expression (parser,
12099                                         /*allow_non_constant_p=*/true,
12100                                         non_constant_p);
12101       if (!*non_constant_p)
12102         initializer = fold_non_dependent_expr (initializer);
12103     }
12104   else
12105     {
12106       /* Consume the `{' token.  */
12107       cp_lexer_consume_token (parser->lexer);
12108       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12109       initializer = make_node (CONSTRUCTOR);
12110       /* If it's not a `}', then there is a non-trivial initializer.  */
12111       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12112         {
12113           /* Parse the initializer list.  */
12114           CONSTRUCTOR_ELTS (initializer)
12115             = cp_parser_initializer_list (parser, non_constant_p);
12116           /* A trailing `,' token is allowed.  */
12117           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12118             cp_lexer_consume_token (parser->lexer);
12119         }
12120       /* Now, there should be a trailing `}'.  */
12121       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12122     }
12123
12124   return initializer;
12125 }
12126
12127 /* Parse an initializer-list.
12128
12129    initializer-list:
12130      initializer-clause
12131      initializer-list , initializer-clause
12132
12133    GNU Extension:
12134
12135    initializer-list:
12136      identifier : initializer-clause
12137      initializer-list, identifier : initializer-clause
12138
12139    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12140    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12141    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12142    as for cp_parser_initializer.  */
12143
12144 static tree
12145 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12146 {
12147   tree initializers = NULL_TREE;
12148
12149   /* Assume all of the expressions are constant.  */
12150   *non_constant_p = false;
12151
12152   /* Parse the rest of the list.  */
12153   while (true)
12154     {
12155       cp_token *token;
12156       tree identifier;
12157       tree initializer;
12158       bool clause_non_constant_p;
12159
12160       /* If the next token is an identifier and the following one is a
12161          colon, we are looking at the GNU designated-initializer
12162          syntax.  */
12163       if (cp_parser_allow_gnu_extensions_p (parser)
12164           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12165           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12166         {
12167           /* Consume the identifier.  */
12168           identifier = cp_lexer_consume_token (parser->lexer)->value;
12169           /* Consume the `:'.  */
12170           cp_lexer_consume_token (parser->lexer);
12171         }
12172       else
12173         identifier = NULL_TREE;
12174
12175       /* Parse the initializer.  */
12176       initializer = cp_parser_initializer_clause (parser,
12177                                                   &clause_non_constant_p);
12178       /* If any clause is non-constant, so is the entire initializer.  */
12179       if (clause_non_constant_p)
12180         *non_constant_p = true;
12181       /* Add it to the list.  */
12182       initializers = tree_cons (identifier, initializer, initializers);
12183
12184       /* If the next token is not a comma, we have reached the end of
12185          the list.  */
12186       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12187         break;
12188
12189       /* Peek at the next token.  */
12190       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12191       /* If the next token is a `}', then we're still done.  An
12192          initializer-clause can have a trailing `,' after the
12193          initializer-list and before the closing `}'.  */
12194       if (token->type == CPP_CLOSE_BRACE)
12195         break;
12196
12197       /* Consume the `,' token.  */
12198       cp_lexer_consume_token (parser->lexer);
12199     }
12200
12201   /* The initializers were built up in reverse order, so we need to
12202      reverse them now.  */
12203   return nreverse (initializers);
12204 }
12205
12206 /* Classes [gram.class] */
12207
12208 /* Parse a class-name.
12209
12210    class-name:
12211      identifier
12212      template-id
12213
12214    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12215    to indicate that names looked up in dependent types should be
12216    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12217    keyword has been used to indicate that the name that appears next
12218    is a template.  TAG_TYPE indicates the explicit tag given before
12219    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12220    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12221    is the class being defined in a class-head.
12222
12223    Returns the TYPE_DECL representing the class.  */
12224
12225 static tree
12226 cp_parser_class_name (cp_parser *parser,
12227                       bool typename_keyword_p,
12228                       bool template_keyword_p,
12229                       enum tag_types tag_type,
12230                       bool check_dependency_p,
12231                       bool class_head_p,
12232                       bool is_declaration)
12233 {
12234   tree decl;
12235   tree scope;
12236   bool typename_p;
12237   cp_token *token;
12238
12239   /* All class-names start with an identifier.  */
12240   token = cp_lexer_peek_token (parser->lexer);
12241   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12242     {
12243       cp_parser_error (parser, "expected class-name");
12244       return error_mark_node;
12245     }
12246
12247   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12248      to a template-id, so we save it here.  */
12249   scope = parser->scope;
12250   if (scope == error_mark_node)
12251     return error_mark_node;
12252
12253   /* Any name names a type if we're following the `typename' keyword
12254      in a qualified name where the enclosing scope is type-dependent.  */
12255   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12256                 && dependent_type_p (scope));
12257   /* Handle the common case (an identifier, but not a template-id)
12258      efficiently.  */
12259   if (token->type == CPP_NAME
12260       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12261     {
12262       tree identifier;
12263
12264       /* Look for the identifier.  */
12265       identifier = cp_parser_identifier (parser);
12266       /* If the next token isn't an identifier, we are certainly not
12267          looking at a class-name.  */
12268       if (identifier == error_mark_node)
12269         decl = error_mark_node;
12270       /* If we know this is a type-name, there's no need to look it
12271          up.  */
12272       else if (typename_p)
12273         decl = identifier;
12274       else
12275         {
12276           /* If the next token is a `::', then the name must be a type
12277              name.
12278
12279              [basic.lookup.qual]
12280
12281              During the lookup for a name preceding the :: scope
12282              resolution operator, object, function, and enumerator
12283              names are ignored.  */
12284           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12285             tag_type = typename_type;
12286           /* Look up the name.  */
12287           decl = cp_parser_lookup_name (parser, identifier,
12288                                         tag_type,
12289                                         /*is_template=*/false,
12290                                         /*is_namespace=*/false,
12291                                         check_dependency_p,
12292                                         /*ambiguous_p=*/NULL);
12293         }
12294     }
12295   else
12296     {
12297       /* Try a template-id.  */
12298       decl = cp_parser_template_id (parser, template_keyword_p,
12299                                     check_dependency_p,
12300                                     is_declaration);
12301       if (decl == error_mark_node)
12302         return error_mark_node;
12303     }
12304
12305   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12306
12307   /* If this is a typename, create a TYPENAME_TYPE.  */
12308   if (typename_p && decl != error_mark_node)
12309     {
12310       decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12311       if (decl != error_mark_node)
12312         decl = TYPE_NAME (decl);
12313     }
12314
12315   /* Check to see that it is really the name of a class.  */
12316   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12317       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12318       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12319     /* Situations like this:
12320
12321          template <typename T> struct A {
12322            typename T::template X<int>::I i;
12323          };
12324
12325        are problematic.  Is `T::template X<int>' a class-name?  The
12326        standard does not seem to be definitive, but there is no other
12327        valid interpretation of the following `::'.  Therefore, those
12328        names are considered class-names.  */
12329     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12330   else if (decl == error_mark_node
12331            || TREE_CODE (decl) != TYPE_DECL
12332            || TREE_TYPE (decl) == error_mark_node
12333            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12334     {
12335       cp_parser_error (parser, "expected class-name");
12336       return error_mark_node;
12337     }
12338
12339   return decl;
12340 }
12341
12342 /* Parse a class-specifier.
12343
12344    class-specifier:
12345      class-head { member-specification [opt] }
12346
12347    Returns the TREE_TYPE representing the class.  */
12348
12349 static tree
12350 cp_parser_class_specifier (cp_parser* parser)
12351 {
12352   cp_token *token;
12353   tree type;
12354   tree attributes = NULL_TREE;
12355   int has_trailing_semicolon;
12356   bool nested_name_specifier_p;
12357   unsigned saved_num_template_parameter_lists;
12358   tree old_scope = NULL_TREE;
12359   tree scope = NULL_TREE;
12360
12361   push_deferring_access_checks (dk_no_deferred);
12362
12363   /* Parse the class-head.  */
12364   type = cp_parser_class_head (parser,
12365                                &nested_name_specifier_p,
12366                                &attributes);
12367   /* If the class-head was a semantic disaster, skip the entire body
12368      of the class.  */
12369   if (!type)
12370     {
12371       cp_parser_skip_to_end_of_block_or_statement (parser);
12372       pop_deferring_access_checks ();
12373       return error_mark_node;
12374     }
12375
12376   /* Look for the `{'.  */
12377   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12378     {
12379       pop_deferring_access_checks ();
12380       return error_mark_node;
12381     }
12382
12383   /* Issue an error message if type-definitions are forbidden here.  */
12384   cp_parser_check_type_definition (parser);
12385   /* Remember that we are defining one more class.  */
12386   ++parser->num_classes_being_defined;
12387   /* Inside the class, surrounding template-parameter-lists do not
12388      apply.  */
12389   saved_num_template_parameter_lists
12390     = parser->num_template_parameter_lists;
12391   parser->num_template_parameter_lists = 0;
12392
12393   /* Start the class.  */
12394   if (nested_name_specifier_p)
12395     {
12396       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12397       old_scope = push_inner_scope (scope);
12398     }
12399   type = begin_class_definition (type);
12400
12401   if (type == error_mark_node)
12402     /* If the type is erroneous, skip the entire body of the class.  */
12403     cp_parser_skip_to_closing_brace (parser);
12404   else
12405     /* Parse the member-specification.  */
12406     cp_parser_member_specification_opt (parser);
12407
12408   /* Look for the trailing `}'.  */
12409   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12410   /* We get better error messages by noticing a common problem: a
12411      missing trailing `;'.  */
12412   token = cp_lexer_peek_token (parser->lexer);
12413   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12414   /* Look for trailing attributes to apply to this class.  */
12415   if (cp_parser_allow_gnu_extensions_p (parser))
12416     {
12417       tree sub_attr = cp_parser_attributes_opt (parser);
12418       attributes = chainon (attributes, sub_attr);
12419     }
12420   if (type != error_mark_node)
12421     type = finish_struct (type, attributes);
12422   if (nested_name_specifier_p)
12423     pop_inner_scope (old_scope, scope);
12424   /* If this class is not itself within the scope of another class,
12425      then we need to parse the bodies of all of the queued function
12426      definitions.  Note that the queued functions defined in a class
12427      are not always processed immediately following the
12428      class-specifier for that class.  Consider:
12429
12430        struct A {
12431          struct B { void f() { sizeof (A); } };
12432        };
12433
12434      If `f' were processed before the processing of `A' were
12435      completed, there would be no way to compute the size of `A'.
12436      Note that the nesting we are interested in here is lexical --
12437      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12438      for:
12439
12440        struct A { struct B; };
12441        struct A::B { void f() { } };
12442
12443      there is no need to delay the parsing of `A::B::f'.  */
12444   if (--parser->num_classes_being_defined == 0)
12445     {
12446       tree queue_entry;
12447       tree fn;
12448       tree class_type = NULL_TREE;
12449       tree pushed_scope = NULL_TREE;
12450
12451       /* In a first pass, parse default arguments to the functions.
12452          Then, in a second pass, parse the bodies of the functions.
12453          This two-phased approach handles cases like:
12454
12455             struct S {
12456               void f() { g(); }
12457               void g(int i = 3);
12458             };
12459
12460          */
12461       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12462              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12463            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12464            TREE_PURPOSE (parser->unparsed_functions_queues)
12465              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12466         {
12467           fn = TREE_VALUE (queue_entry);
12468           /* If there are default arguments that have not yet been processed,
12469              take care of them now.  */
12470           if (class_type != TREE_PURPOSE (queue_entry))
12471             {
12472               if (pushed_scope)
12473                 pop_scope (pushed_scope);
12474               class_type = TREE_PURPOSE (queue_entry);
12475               pushed_scope = push_scope (class_type);
12476             }
12477           /* Make sure that any template parameters are in scope.  */
12478           maybe_begin_member_template_processing (fn);
12479           /* Parse the default argument expressions.  */
12480           cp_parser_late_parsing_default_args (parser, fn);
12481           /* Remove any template parameters from the symbol table.  */
12482           maybe_end_member_template_processing ();
12483         }
12484       if (pushed_scope)
12485         pop_scope (pushed_scope);
12486       /* Now parse the body of the functions.  */
12487       for (TREE_VALUE (parser->unparsed_functions_queues)
12488              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12489            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12490            TREE_VALUE (parser->unparsed_functions_queues)
12491              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12492         {
12493           /* Figure out which function we need to process.  */
12494           fn = TREE_VALUE (queue_entry);
12495
12496           /* A hack to prevent garbage collection.  */
12497           function_depth++;
12498
12499           /* Parse the function.  */
12500           cp_parser_late_parsing_for_member (parser, fn);
12501           function_depth--;
12502         }
12503     }
12504
12505   /* Put back any saved access checks.  */
12506   pop_deferring_access_checks ();
12507
12508   /* Restore the count of active template-parameter-lists.  */
12509   parser->num_template_parameter_lists
12510     = saved_num_template_parameter_lists;
12511
12512   return type;
12513 }
12514
12515 /* Parse a class-head.
12516
12517    class-head:
12518      class-key identifier [opt] base-clause [opt]
12519      class-key nested-name-specifier identifier base-clause [opt]
12520      class-key nested-name-specifier [opt] template-id
12521        base-clause [opt]
12522
12523    GNU Extensions:
12524      class-key attributes identifier [opt] base-clause [opt]
12525      class-key attributes nested-name-specifier identifier base-clause [opt]
12526      class-key attributes nested-name-specifier [opt] template-id
12527        base-clause [opt]
12528
12529    Returns the TYPE of the indicated class.  Sets
12530    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12531    involving a nested-name-specifier was used, and FALSE otherwise.
12532
12533    Returns error_mark_node if this is not a class-head.
12534    
12535    Returns NULL_TREE if the class-head is syntactically valid, but
12536    semantically invalid in a way that means we should skip the entire
12537    body of the class.  */
12538
12539 static tree
12540 cp_parser_class_head (cp_parser* parser,
12541                       bool* nested_name_specifier_p,
12542                       tree *attributes_p)
12543 {
12544   tree nested_name_specifier;
12545   enum tag_types class_key;
12546   tree id = NULL_TREE;
12547   tree type = NULL_TREE;
12548   tree attributes;
12549   bool template_id_p = false;
12550   bool qualified_p = false;
12551   bool invalid_nested_name_p = false;
12552   bool invalid_explicit_specialization_p = false;
12553   tree pushed_scope = NULL_TREE;
12554   unsigned num_templates;
12555   tree bases;
12556
12557   /* Assume no nested-name-specifier will be present.  */
12558   *nested_name_specifier_p = false;
12559   /* Assume no template parameter lists will be used in defining the
12560      type.  */
12561   num_templates = 0;
12562
12563   /* Look for the class-key.  */
12564   class_key = cp_parser_class_key (parser);
12565   if (class_key == none_type)
12566     return error_mark_node;
12567
12568   /* Parse the attributes.  */
12569   attributes = cp_parser_attributes_opt (parser);
12570
12571   /* If the next token is `::', that is invalid -- but sometimes
12572      people do try to write:
12573
12574        struct ::S {};
12575
12576      Handle this gracefully by accepting the extra qualifier, and then
12577      issuing an error about it later if this really is a
12578      class-head.  If it turns out just to be an elaborated type
12579      specifier, remain silent.  */
12580   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12581     qualified_p = true;
12582
12583   push_deferring_access_checks (dk_no_check);
12584
12585   /* Determine the name of the class.  Begin by looking for an
12586      optional nested-name-specifier.  */
12587   nested_name_specifier
12588     = cp_parser_nested_name_specifier_opt (parser,
12589                                            /*typename_keyword_p=*/false,
12590                                            /*check_dependency_p=*/false,
12591                                            /*type_p=*/false,
12592                                            /*is_declaration=*/false);
12593   /* If there was a nested-name-specifier, then there *must* be an
12594      identifier.  */
12595   if (nested_name_specifier)
12596     {
12597       /* Although the grammar says `identifier', it really means
12598          `class-name' or `template-name'.  You are only allowed to
12599          define a class that has already been declared with this
12600          syntax.
12601
12602          The proposed resolution for Core Issue 180 says that whever
12603          you see `class T::X' you should treat `X' as a type-name.
12604
12605          It is OK to define an inaccessible class; for example:
12606
12607            class A { class B; };
12608            class A::B {};
12609
12610          We do not know if we will see a class-name, or a
12611          template-name.  We look for a class-name first, in case the
12612          class-name is a template-id; if we looked for the
12613          template-name first we would stop after the template-name.  */
12614       cp_parser_parse_tentatively (parser);
12615       type = cp_parser_class_name (parser,
12616                                    /*typename_keyword_p=*/false,
12617                                    /*template_keyword_p=*/false,
12618                                    class_type,
12619                                    /*check_dependency_p=*/false,
12620                                    /*class_head_p=*/true,
12621                                    /*is_declaration=*/false);
12622       /* If that didn't work, ignore the nested-name-specifier.  */
12623       if (!cp_parser_parse_definitely (parser))
12624         {
12625           invalid_nested_name_p = true;
12626           id = cp_parser_identifier (parser);
12627           if (id == error_mark_node)
12628             id = NULL_TREE;
12629         }
12630       /* If we could not find a corresponding TYPE, treat this
12631          declaration like an unqualified declaration.  */
12632       if (type == error_mark_node)
12633         nested_name_specifier = NULL_TREE;
12634       /* Otherwise, count the number of templates used in TYPE and its
12635          containing scopes.  */
12636       else
12637         {
12638           tree scope;
12639
12640           for (scope = TREE_TYPE (type);
12641                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12642                scope = (TYPE_P (scope)
12643                         ? TYPE_CONTEXT (scope)
12644                         : DECL_CONTEXT (scope)))
12645             if (TYPE_P (scope)
12646                 && CLASS_TYPE_P (scope)
12647                 && CLASSTYPE_TEMPLATE_INFO (scope)
12648                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12649                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12650               ++num_templates;
12651         }
12652     }
12653   /* Otherwise, the identifier is optional.  */
12654   else
12655     {
12656       /* We don't know whether what comes next is a template-id,
12657          an identifier, or nothing at all.  */
12658       cp_parser_parse_tentatively (parser);
12659       /* Check for a template-id.  */
12660       id = cp_parser_template_id (parser,
12661                                   /*template_keyword_p=*/false,
12662                                   /*check_dependency_p=*/true,
12663                                   /*is_declaration=*/true);
12664       /* If that didn't work, it could still be an identifier.  */
12665       if (!cp_parser_parse_definitely (parser))
12666         {
12667           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12668             id = cp_parser_identifier (parser);
12669           else
12670             id = NULL_TREE;
12671         }
12672       else
12673         {
12674           template_id_p = true;
12675           ++num_templates;
12676         }
12677     }
12678
12679   pop_deferring_access_checks ();
12680
12681   if (id)
12682     cp_parser_check_for_invalid_template_id (parser, id);
12683
12684   /* If it's not a `:' or a `{' then we can't really be looking at a
12685      class-head, since a class-head only appears as part of a
12686      class-specifier.  We have to detect this situation before calling
12687      xref_tag, since that has irreversible side-effects.  */
12688   if (!cp_parser_next_token_starts_class_definition_p (parser))
12689     {
12690       cp_parser_error (parser, "expected %<{%> or %<:%>");
12691       return error_mark_node;
12692     }
12693
12694   /* At this point, we're going ahead with the class-specifier, even
12695      if some other problem occurs.  */
12696   cp_parser_commit_to_tentative_parse (parser);
12697   /* Issue the error about the overly-qualified name now.  */
12698   if (qualified_p)
12699     cp_parser_error (parser,
12700                      "global qualification of class name is invalid");
12701   else if (invalid_nested_name_p)
12702     cp_parser_error (parser,
12703                      "qualified name does not name a class");
12704   else if (nested_name_specifier)
12705     {
12706       tree scope;
12707
12708       /* Reject typedef-names in class heads.  */
12709       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12710         {
12711           error ("invalid class name in declaration of %qD", type);
12712           type = NULL_TREE;
12713           goto done;
12714         }
12715
12716       /* Figure out in what scope the declaration is being placed.  */
12717       scope = current_scope ();
12718       /* If that scope does not contain the scope in which the
12719          class was originally declared, the program is invalid.  */
12720       if (scope && !is_ancestor (scope, nested_name_specifier))
12721         {
12722           error ("declaration of %qD in %qD which does not enclose %qD",
12723                  type, scope, nested_name_specifier);
12724           type = NULL_TREE;
12725           goto done;
12726         }
12727       /* [dcl.meaning]
12728
12729          A declarator-id shall not be qualified exception of the
12730          definition of a ... nested class outside of its class
12731          ... [or] a the definition or explicit instantiation of a
12732          class member of a namespace outside of its namespace.  */
12733       if (scope == nested_name_specifier)
12734         {
12735           pedwarn ("extra qualification ignored");
12736           nested_name_specifier = NULL_TREE;
12737           num_templates = 0;
12738         }
12739     }
12740   /* An explicit-specialization must be preceded by "template <>".  If
12741      it is not, try to recover gracefully.  */
12742   if (at_namespace_scope_p ()
12743       && parser->num_template_parameter_lists == 0
12744       && template_id_p)
12745     {
12746       error ("an explicit specialization must be preceded by %<template <>%>");
12747       invalid_explicit_specialization_p = true;
12748       /* Take the same action that would have been taken by
12749          cp_parser_explicit_specialization.  */
12750       ++parser->num_template_parameter_lists;
12751       begin_specialization ();
12752     }
12753   /* There must be no "return" statements between this point and the
12754      end of this function; set "type "to the correct return value and
12755      use "goto done;" to return.  */
12756   /* Make sure that the right number of template parameters were
12757      present.  */
12758   if (!cp_parser_check_template_parameters (parser, num_templates))
12759     {
12760       /* If something went wrong, there is no point in even trying to
12761          process the class-definition.  */
12762       type = NULL_TREE;
12763       goto done;
12764     }
12765
12766   /* Look up the type.  */
12767   if (template_id_p)
12768     {
12769       type = TREE_TYPE (id);
12770       maybe_process_partial_specialization (type);
12771       if (nested_name_specifier)
12772         pushed_scope = push_scope (nested_name_specifier);
12773     }
12774   else if (nested_name_specifier)
12775     {
12776       tree class_type;
12777
12778       /* Given:
12779
12780             template <typename T> struct S { struct T };
12781             template <typename T> struct S<T>::T { };
12782
12783          we will get a TYPENAME_TYPE when processing the definition of
12784          `S::T'.  We need to resolve it to the actual type before we
12785          try to define it.  */
12786       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12787         {
12788           class_type = resolve_typename_type (TREE_TYPE (type),
12789                                               /*only_current_p=*/false);
12790           if (class_type != error_mark_node)
12791             type = TYPE_NAME (class_type);
12792           else
12793             {
12794               cp_parser_error (parser, "could not resolve typename type");
12795               type = error_mark_node;
12796             }
12797         }
12798
12799       maybe_process_partial_specialization (TREE_TYPE (type));
12800       class_type = current_class_type;
12801       /* Enter the scope indicated by the nested-name-specifier.  */
12802       pushed_scope = push_scope (nested_name_specifier);
12803       /* Get the canonical version of this type.  */
12804       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12805       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12806           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12807         {
12808           type = push_template_decl (type);
12809           if (type == error_mark_node)
12810             {
12811               type = NULL_TREE;
12812               goto done;
12813             }
12814         }
12815       
12816       type = TREE_TYPE (type);
12817       *nested_name_specifier_p = true;
12818     }
12819   else      /* The name is not a nested name.  */
12820     {
12821       /* If the class was unnamed, create a dummy name.  */
12822       if (!id)
12823         id = make_anon_name ();
12824       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12825                        parser->num_template_parameter_lists);
12826     }
12827
12828   /* Indicate whether this class was declared as a `class' or as a
12829      `struct'.  */
12830   if (TREE_CODE (type) == RECORD_TYPE)
12831     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12832   cp_parser_check_class_key (class_key, type);
12833
12834   /* We will have entered the scope containing the class; the names of
12835      base classes should be looked up in that context.  For example,
12836      given:
12837
12838        struct A { struct B {}; struct C; };
12839        struct A::C : B {};
12840
12841      is valid.  */
12842   bases = NULL_TREE;
12843
12844   /* Get the list of base-classes, if there is one.  */
12845   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12846     bases = cp_parser_base_clause (parser);
12847
12848   /* Process the base classes.  */
12849   xref_basetypes (type, bases);
12850
12851  done:
12852   /* Leave the scope given by the nested-name-specifier.  We will
12853      enter the class scope itself while processing the members.  */
12854   if (pushed_scope)
12855     pop_scope (pushed_scope);
12856
12857   if (invalid_explicit_specialization_p)
12858     {
12859       end_specialization ();
12860       --parser->num_template_parameter_lists;
12861     }
12862   *attributes_p = attributes;
12863   return type;
12864 }
12865
12866 /* Parse a class-key.
12867
12868    class-key:
12869      class
12870      struct
12871      union
12872
12873    Returns the kind of class-key specified, or none_type to indicate
12874    error.  */
12875
12876 static enum tag_types
12877 cp_parser_class_key (cp_parser* parser)
12878 {
12879   cp_token *token;
12880   enum tag_types tag_type;
12881
12882   /* Look for the class-key.  */
12883   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12884   if (!token)
12885     return none_type;
12886
12887   /* Check to see if the TOKEN is a class-key.  */
12888   tag_type = cp_parser_token_is_class_key (token);
12889   if (!tag_type)
12890     cp_parser_error (parser, "expected class-key");
12891   return tag_type;
12892 }
12893
12894 /* Parse an (optional) member-specification.
12895
12896    member-specification:
12897      member-declaration member-specification [opt]
12898      access-specifier : member-specification [opt]  */
12899
12900 static void
12901 cp_parser_member_specification_opt (cp_parser* parser)
12902 {
12903   while (true)
12904     {
12905       cp_token *token;
12906       enum rid keyword;
12907
12908       /* Peek at the next token.  */
12909       token = cp_lexer_peek_token (parser->lexer);
12910       /* If it's a `}', or EOF then we've seen all the members.  */
12911       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12912         break;
12913
12914       /* See if this token is a keyword.  */
12915       keyword = token->keyword;
12916       switch (keyword)
12917         {
12918         case RID_PUBLIC:
12919         case RID_PROTECTED:
12920         case RID_PRIVATE:
12921           /* Consume the access-specifier.  */
12922           cp_lexer_consume_token (parser->lexer);
12923           /* Remember which access-specifier is active.  */
12924           current_access_specifier = token->value;
12925           /* Look for the `:'.  */
12926           cp_parser_require (parser, CPP_COLON, "`:'");
12927           break;
12928
12929         default:
12930           /* Accept #pragmas at class scope.  */
12931           if (token->type == CPP_PRAGMA)
12932             {
12933               cp_lexer_handle_pragma (parser->lexer);
12934               break;
12935             }
12936
12937           /* Otherwise, the next construction must be a
12938              member-declaration.  */
12939           cp_parser_member_declaration (parser);
12940         }
12941     }
12942 }
12943
12944 /* Parse a member-declaration.
12945
12946    member-declaration:
12947      decl-specifier-seq [opt] member-declarator-list [opt] ;
12948      function-definition ; [opt]
12949      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12950      using-declaration
12951      template-declaration
12952
12953    member-declarator-list:
12954      member-declarator
12955      member-declarator-list , member-declarator
12956
12957    member-declarator:
12958      declarator pure-specifier [opt]
12959      declarator constant-initializer [opt]
12960      identifier [opt] : constant-expression
12961
12962    GNU Extensions:
12963
12964    member-declaration:
12965      __extension__ member-declaration
12966
12967    member-declarator:
12968      declarator attributes [opt] pure-specifier [opt]
12969      declarator attributes [opt] constant-initializer [opt]
12970      identifier [opt] attributes [opt] : constant-expression  */
12971
12972 static void
12973 cp_parser_member_declaration (cp_parser* parser)
12974 {
12975   cp_decl_specifier_seq decl_specifiers;
12976   tree prefix_attributes;
12977   tree decl;
12978   int declares_class_or_enum;
12979   bool friend_p;
12980   cp_token *token;
12981   int saved_pedantic;
12982
12983   /* Check for the `__extension__' keyword.  */
12984   if (cp_parser_extension_opt (parser, &saved_pedantic))
12985     {
12986       /* Recurse.  */
12987       cp_parser_member_declaration (parser);
12988       /* Restore the old value of the PEDANTIC flag.  */
12989       pedantic = saved_pedantic;
12990
12991       return;
12992     }
12993
12994   /* Check for a template-declaration.  */
12995   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12996     {
12997       /* Parse the template-declaration.  */
12998       cp_parser_template_declaration (parser, /*member_p=*/true);
12999
13000       return;
13001     }
13002
13003   /* Check for a using-declaration.  */
13004   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13005     {
13006       /* Parse the using-declaration.  */
13007       cp_parser_using_declaration (parser);
13008
13009       return;
13010     }
13011
13012   /* Parse the decl-specifier-seq.  */
13013   cp_parser_decl_specifier_seq (parser,
13014                                 CP_PARSER_FLAGS_OPTIONAL,
13015                                 &decl_specifiers,
13016                                 &declares_class_or_enum);
13017   prefix_attributes = decl_specifiers.attributes;
13018   decl_specifiers.attributes = NULL_TREE;
13019   /* Check for an invalid type-name.  */
13020   if (!decl_specifiers.type
13021       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13022     return;
13023   /* If there is no declarator, then the decl-specifier-seq should
13024      specify a type.  */
13025   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13026     {
13027       /* If there was no decl-specifier-seq, and the next token is a
13028          `;', then we have something like:
13029
13030            struct S { ; };
13031
13032          [class.mem]
13033
13034          Each member-declaration shall declare at least one member
13035          name of the class.  */
13036       if (!decl_specifiers.any_specifiers_p)
13037         {
13038           cp_token *token = cp_lexer_peek_token (parser->lexer);
13039           if (pedantic && !token->in_system_header)
13040             pedwarn ("%Hextra %<;%>", &token->location);
13041         }
13042       else
13043         {
13044           tree type;
13045
13046           /* See if this declaration is a friend.  */
13047           friend_p = cp_parser_friend_p (&decl_specifiers);
13048           /* If there were decl-specifiers, check to see if there was
13049              a class-declaration.  */
13050           type = check_tag_decl (&decl_specifiers);
13051           /* Nested classes have already been added to the class, but
13052              a `friend' needs to be explicitly registered.  */
13053           if (friend_p)
13054             {
13055               /* If the `friend' keyword was present, the friend must
13056                  be introduced with a class-key.  */
13057                if (!declares_class_or_enum)
13058                  error ("a class-key must be used when declaring a friend");
13059                /* In this case:
13060
13061                     template <typename T> struct A {
13062                       friend struct A<T>::B;
13063                     };
13064
13065                   A<T>::B will be represented by a TYPENAME_TYPE, and
13066                   therefore not recognized by check_tag_decl.  */
13067                if (!type
13068                    && decl_specifiers.type
13069                    && TYPE_P (decl_specifiers.type))
13070                  type = decl_specifiers.type;
13071                if (!type || !TYPE_P (type))
13072                  error ("friend declaration does not name a class or "
13073                         "function");
13074                else
13075                  make_friend_class (current_class_type, type,
13076                                     /*complain=*/true);
13077             }
13078           /* If there is no TYPE, an error message will already have
13079              been issued.  */
13080           else if (!type || type == error_mark_node)
13081             ;
13082           /* An anonymous aggregate has to be handled specially; such
13083              a declaration really declares a data member (with a
13084              particular type), as opposed to a nested class.  */
13085           else if (ANON_AGGR_TYPE_P (type))
13086             {
13087               /* Remove constructors and such from TYPE, now that we
13088                  know it is an anonymous aggregate.  */
13089               fixup_anonymous_aggr (type);
13090               /* And make the corresponding data member.  */
13091               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13092               /* Add it to the class.  */
13093               finish_member_declaration (decl);
13094             }
13095           else
13096             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13097         }
13098     }
13099   else
13100     {
13101       /* See if these declarations will be friends.  */
13102       friend_p = cp_parser_friend_p (&decl_specifiers);
13103
13104       /* Keep going until we hit the `;' at the end of the
13105          declaration.  */
13106       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13107         {
13108           tree attributes = NULL_TREE;
13109           tree first_attribute;
13110
13111           /* Peek at the next token.  */
13112           token = cp_lexer_peek_token (parser->lexer);
13113
13114           /* Check for a bitfield declaration.  */
13115           if (token->type == CPP_COLON
13116               || (token->type == CPP_NAME
13117                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13118                   == CPP_COLON))
13119             {
13120               tree identifier;
13121               tree width;
13122
13123               /* Get the name of the bitfield.  Note that we cannot just
13124                  check TOKEN here because it may have been invalidated by
13125                  the call to cp_lexer_peek_nth_token above.  */
13126               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13127                 identifier = cp_parser_identifier (parser);
13128               else
13129                 identifier = NULL_TREE;
13130
13131               /* Consume the `:' token.  */
13132               cp_lexer_consume_token (parser->lexer);
13133               /* Get the width of the bitfield.  */
13134               width
13135                 = cp_parser_constant_expression (parser,
13136                                                  /*allow_non_constant=*/false,
13137                                                  NULL);
13138
13139               /* Look for attributes that apply to the bitfield.  */
13140               attributes = cp_parser_attributes_opt (parser);
13141               /* Remember which attributes are prefix attributes and
13142                  which are not.  */
13143               first_attribute = attributes;
13144               /* Combine the attributes.  */
13145               attributes = chainon (prefix_attributes, attributes);
13146
13147               /* Create the bitfield declaration.  */
13148               decl = grokbitfield (identifier
13149                                    ? make_id_declarator (NULL_TREE,
13150                                                          identifier)
13151                                    : NULL,
13152                                    &decl_specifiers,
13153                                    width);
13154               /* Apply the attributes.  */
13155               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13156             }
13157           else
13158             {
13159               cp_declarator *declarator;
13160               tree initializer;
13161               tree asm_specification;
13162               int ctor_dtor_or_conv_p;
13163
13164               /* Parse the declarator.  */
13165               declarator
13166                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13167                                         &ctor_dtor_or_conv_p,
13168                                         /*parenthesized_p=*/NULL,
13169                                         /*member_p=*/true);
13170
13171               /* If something went wrong parsing the declarator, make sure
13172                  that we at least consume some tokens.  */
13173               if (declarator == cp_error_declarator)
13174                 {
13175                   /* Skip to the end of the statement.  */
13176                   cp_parser_skip_to_end_of_statement (parser);
13177                   /* If the next token is not a semicolon, that is
13178                      probably because we just skipped over the body of
13179                      a function.  So, we consume a semicolon if
13180                      present, but do not issue an error message if it
13181                      is not present.  */
13182                   if (cp_lexer_next_token_is (parser->lexer,
13183                                               CPP_SEMICOLON))
13184                     cp_lexer_consume_token (parser->lexer);
13185                   return;
13186                 }
13187
13188               if (declares_class_or_enum & 2)
13189                 cp_parser_check_for_definition_in_return_type
13190                   (declarator, decl_specifiers.type);
13191
13192               /* Look for an asm-specification.  */
13193               asm_specification = cp_parser_asm_specification_opt (parser);
13194               /* Look for attributes that apply to the declaration.  */
13195               attributes = cp_parser_attributes_opt (parser);
13196               /* Remember which attributes are prefix attributes and
13197                  which are not.  */
13198               first_attribute = attributes;
13199               /* Combine the attributes.  */
13200               attributes = chainon (prefix_attributes, attributes);
13201
13202               /* If it's an `=', then we have a constant-initializer or a
13203                  pure-specifier.  It is not correct to parse the
13204                  initializer before registering the member declaration
13205                  since the member declaration should be in scope while
13206                  its initializer is processed.  However, the rest of the
13207                  front end does not yet provide an interface that allows
13208                  us to handle this correctly.  */
13209               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13210                 {
13211                   /* In [class.mem]:
13212
13213                      A pure-specifier shall be used only in the declaration of
13214                      a virtual function.
13215
13216                      A member-declarator can contain a constant-initializer
13217                      only if it declares a static member of integral or
13218                      enumeration type.
13219
13220                      Therefore, if the DECLARATOR is for a function, we look
13221                      for a pure-specifier; otherwise, we look for a
13222                      constant-initializer.  When we call `grokfield', it will
13223                      perform more stringent semantics checks.  */
13224                   if (declarator->kind == cdk_function)
13225                     initializer = cp_parser_pure_specifier (parser);
13226                   else
13227                     /* Parse the initializer.  */
13228                     initializer = cp_parser_constant_initializer (parser);
13229                 }
13230               /* Otherwise, there is no initializer.  */
13231               else
13232                 initializer = NULL_TREE;
13233
13234               /* See if we are probably looking at a function
13235                  definition.  We are certainly not looking at a
13236                  member-declarator.  Calling `grokfield' has
13237                  side-effects, so we must not do it unless we are sure
13238                  that we are looking at a member-declarator.  */
13239               if (cp_parser_token_starts_function_definition_p
13240                   (cp_lexer_peek_token (parser->lexer)))
13241                 {
13242                   /* The grammar does not allow a pure-specifier to be
13243                      used when a member function is defined.  (It is
13244                      possible that this fact is an oversight in the
13245                      standard, since a pure function may be defined
13246                      outside of the class-specifier.  */
13247                   if (initializer)
13248                     error ("pure-specifier on function-definition");
13249                   decl = cp_parser_save_member_function_body (parser,
13250                                                               &decl_specifiers,
13251                                                               declarator,
13252                                                               attributes);
13253                   /* If the member was not a friend, declare it here.  */
13254                   if (!friend_p)
13255                     finish_member_declaration (decl);
13256                   /* Peek at the next token.  */
13257                   token = cp_lexer_peek_token (parser->lexer);
13258                   /* If the next token is a semicolon, consume it.  */
13259                   if (token->type == CPP_SEMICOLON)
13260                     cp_lexer_consume_token (parser->lexer);
13261                   return;
13262                 }
13263               else
13264                 {
13265                   /* Create the declaration.  */
13266                   decl = grokfield (declarator, &decl_specifiers,
13267                                     initializer, asm_specification,
13268                                     attributes);
13269                   /* Any initialization must have been from a
13270                      constant-expression.  */
13271                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13272                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13273                 }
13274             }
13275
13276           /* Reset PREFIX_ATTRIBUTES.  */
13277           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13278             attributes = TREE_CHAIN (attributes);
13279           if (attributes)
13280             TREE_CHAIN (attributes) = NULL_TREE;
13281
13282           /* If there is any qualification still in effect, clear it
13283              now; we will be starting fresh with the next declarator.  */
13284           parser->scope = NULL_TREE;
13285           parser->qualifying_scope = NULL_TREE;
13286           parser->object_scope = NULL_TREE;
13287           /* If it's a `,', then there are more declarators.  */
13288           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13289             cp_lexer_consume_token (parser->lexer);
13290           /* If the next token isn't a `;', then we have a parse error.  */
13291           else if (cp_lexer_next_token_is_not (parser->lexer,
13292                                                CPP_SEMICOLON))
13293             {
13294               cp_parser_error (parser, "expected %<;%>");
13295               /* Skip tokens until we find a `;'.  */
13296               cp_parser_skip_to_end_of_statement (parser);
13297
13298               break;
13299             }
13300
13301           if (decl)
13302             {
13303               /* Add DECL to the list of members.  */
13304               if (!friend_p)
13305                 finish_member_declaration (decl);
13306
13307               if (TREE_CODE (decl) == FUNCTION_DECL)
13308                 cp_parser_save_default_args (parser, decl);
13309             }
13310         }
13311     }
13312
13313   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13314 }
13315
13316 /* Parse a pure-specifier.
13317
13318    pure-specifier:
13319      = 0
13320
13321    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13322    Otherwise, ERROR_MARK_NODE is returned.  */
13323
13324 static tree
13325 cp_parser_pure_specifier (cp_parser* parser)
13326 {
13327   cp_token *token;
13328
13329   /* Look for the `=' token.  */
13330   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13331     return error_mark_node;
13332   /* Look for the `0' token.  */
13333   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13334   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13335      to get information from the lexer about how the number was
13336      spelled in order to fix this problem.  */
13337   if (!token || !integer_zerop (token->value))
13338     return error_mark_node;
13339
13340   return integer_zero_node;
13341 }
13342
13343 /* Parse a constant-initializer.
13344
13345    constant-initializer:
13346      = constant-expression
13347
13348    Returns a representation of the constant-expression.  */
13349
13350 static tree
13351 cp_parser_constant_initializer (cp_parser* parser)
13352 {
13353   /* Look for the `=' token.  */
13354   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13355     return error_mark_node;
13356
13357   /* It is invalid to write:
13358
13359        struct S { static const int i = { 7 }; };
13360
13361      */
13362   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13363     {
13364       cp_parser_error (parser,
13365                        "a brace-enclosed initializer is not allowed here");
13366       /* Consume the opening brace.  */
13367       cp_lexer_consume_token (parser->lexer);
13368       /* Skip the initializer.  */
13369       cp_parser_skip_to_closing_brace (parser);
13370       /* Look for the trailing `}'.  */
13371       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13372
13373       return error_mark_node;
13374     }
13375
13376   return cp_parser_constant_expression (parser,
13377                                         /*allow_non_constant=*/false,
13378                                         NULL);
13379 }
13380
13381 /* Derived classes [gram.class.derived] */
13382
13383 /* Parse a base-clause.
13384
13385    base-clause:
13386      : base-specifier-list
13387
13388    base-specifier-list:
13389      base-specifier
13390      base-specifier-list , base-specifier
13391
13392    Returns a TREE_LIST representing the base-classes, in the order in
13393    which they were declared.  The representation of each node is as
13394    described by cp_parser_base_specifier.
13395
13396    In the case that no bases are specified, this function will return
13397    NULL_TREE, not ERROR_MARK_NODE.  */
13398
13399 static tree
13400 cp_parser_base_clause (cp_parser* parser)
13401 {
13402   tree bases = NULL_TREE;
13403
13404   /* Look for the `:' that begins the list.  */
13405   cp_parser_require (parser, CPP_COLON, "`:'");
13406
13407   /* Scan the base-specifier-list.  */
13408   while (true)
13409     {
13410       cp_token *token;
13411       tree base;
13412
13413       /* Look for the base-specifier.  */
13414       base = cp_parser_base_specifier (parser);
13415       /* Add BASE to the front of the list.  */
13416       if (base != error_mark_node)
13417         {
13418           TREE_CHAIN (base) = bases;
13419           bases = base;
13420         }
13421       /* Peek at the next token.  */
13422       token = cp_lexer_peek_token (parser->lexer);
13423       /* If it's not a comma, then the list is complete.  */
13424       if (token->type != CPP_COMMA)
13425         break;
13426       /* Consume the `,'.  */
13427       cp_lexer_consume_token (parser->lexer);
13428     }
13429
13430   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13431      base class had a qualified name.  However, the next name that
13432      appears is certainly not qualified.  */
13433   parser->scope = NULL_TREE;
13434   parser->qualifying_scope = NULL_TREE;
13435   parser->object_scope = NULL_TREE;
13436
13437   return nreverse (bases);
13438 }
13439
13440 /* Parse a base-specifier.
13441
13442    base-specifier:
13443      :: [opt] nested-name-specifier [opt] class-name
13444      virtual access-specifier [opt] :: [opt] nested-name-specifier
13445        [opt] class-name
13446      access-specifier virtual [opt] :: [opt] nested-name-specifier
13447        [opt] class-name
13448
13449    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13450    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13451    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13452    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13453
13454 static tree
13455 cp_parser_base_specifier (cp_parser* parser)
13456 {
13457   cp_token *token;
13458   bool done = false;
13459   bool virtual_p = false;
13460   bool duplicate_virtual_error_issued_p = false;
13461   bool duplicate_access_error_issued_p = false;
13462   bool class_scope_p, template_p;
13463   tree access = access_default_node;
13464   tree type;
13465
13466   /* Process the optional `virtual' and `access-specifier'.  */
13467   while (!done)
13468     {
13469       /* Peek at the next token.  */
13470       token = cp_lexer_peek_token (parser->lexer);
13471       /* Process `virtual'.  */
13472       switch (token->keyword)
13473         {
13474         case RID_VIRTUAL:
13475           /* If `virtual' appears more than once, issue an error.  */
13476           if (virtual_p && !duplicate_virtual_error_issued_p)
13477             {
13478               cp_parser_error (parser,
13479                                "%<virtual%> specified more than once in base-specified");
13480               duplicate_virtual_error_issued_p = true;
13481             }
13482
13483           virtual_p = true;
13484
13485           /* Consume the `virtual' token.  */
13486           cp_lexer_consume_token (parser->lexer);
13487
13488           break;
13489
13490         case RID_PUBLIC:
13491         case RID_PROTECTED:
13492         case RID_PRIVATE:
13493           /* If more than one access specifier appears, issue an
13494              error.  */
13495           if (access != access_default_node
13496               && !duplicate_access_error_issued_p)
13497             {
13498               cp_parser_error (parser,
13499                                "more than one access specifier in base-specified");
13500               duplicate_access_error_issued_p = true;
13501             }
13502
13503           access = ridpointers[(int) token->keyword];
13504
13505           /* Consume the access-specifier.  */
13506           cp_lexer_consume_token (parser->lexer);
13507
13508           break;
13509
13510         default:
13511           done = true;
13512           break;
13513         }
13514     }
13515   /* It is not uncommon to see programs mechanically, erroneously, use
13516      the 'typename' keyword to denote (dependent) qualified types
13517      as base classes.  */
13518   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13519     {
13520       if (!processing_template_decl)
13521         error ("keyword %<typename%> not allowed outside of templates");
13522       else
13523         error ("keyword %<typename%> not allowed in this context "
13524                "(the base class is implicitly a type)");
13525       cp_lexer_consume_token (parser->lexer);
13526     }
13527
13528   /* Look for the optional `::' operator.  */
13529   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13530   /* Look for the nested-name-specifier.  The simplest way to
13531      implement:
13532
13533        [temp.res]
13534
13535        The keyword `typename' is not permitted in a base-specifier or
13536        mem-initializer; in these contexts a qualified name that
13537        depends on a template-parameter is implicitly assumed to be a
13538        type name.
13539
13540      is to pretend that we have seen the `typename' keyword at this
13541      point.  */
13542   cp_parser_nested_name_specifier_opt (parser,
13543                                        /*typename_keyword_p=*/true,
13544                                        /*check_dependency_p=*/true,
13545                                        typename_type,
13546                                        /*is_declaration=*/true);
13547   /* If the base class is given by a qualified name, assume that names
13548      we see are type names or templates, as appropriate.  */
13549   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13550   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13551
13552   /* Finally, look for the class-name.  */
13553   type = cp_parser_class_name (parser,
13554                                class_scope_p,
13555                                template_p,
13556                                typename_type,
13557                                /*check_dependency_p=*/true,
13558                                /*class_head_p=*/false,
13559                                /*is_declaration=*/true);
13560
13561   if (type == error_mark_node)
13562     return error_mark_node;
13563
13564   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13565 }
13566
13567 /* Exception handling [gram.exception] */
13568
13569 /* Parse an (optional) exception-specification.
13570
13571    exception-specification:
13572      throw ( type-id-list [opt] )
13573
13574    Returns a TREE_LIST representing the exception-specification.  The
13575    TREE_VALUE of each node is a type.  */
13576
13577 static tree
13578 cp_parser_exception_specification_opt (cp_parser* parser)
13579 {
13580   cp_token *token;
13581   tree type_id_list;
13582
13583   /* Peek at the next token.  */
13584   token = cp_lexer_peek_token (parser->lexer);
13585   /* If it's not `throw', then there's no exception-specification.  */
13586   if (!cp_parser_is_keyword (token, RID_THROW))
13587     return NULL_TREE;
13588
13589   /* Consume the `throw'.  */
13590   cp_lexer_consume_token (parser->lexer);
13591
13592   /* Look for the `('.  */
13593   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13594
13595   /* Peek at the next token.  */
13596   token = cp_lexer_peek_token (parser->lexer);
13597   /* If it's not a `)', then there is a type-id-list.  */
13598   if (token->type != CPP_CLOSE_PAREN)
13599     {
13600       const char *saved_message;
13601
13602       /* Types may not be defined in an exception-specification.  */
13603       saved_message = parser->type_definition_forbidden_message;
13604       parser->type_definition_forbidden_message
13605         = "types may not be defined in an exception-specification";
13606       /* Parse the type-id-list.  */
13607       type_id_list = cp_parser_type_id_list (parser);
13608       /* Restore the saved message.  */
13609       parser->type_definition_forbidden_message = saved_message;
13610     }
13611   else
13612     type_id_list = empty_except_spec;
13613
13614   /* Look for the `)'.  */
13615   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13616
13617   return type_id_list;
13618 }
13619
13620 /* Parse an (optional) type-id-list.
13621
13622    type-id-list:
13623      type-id
13624      type-id-list , type-id
13625
13626    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13627    in the order that the types were presented.  */
13628
13629 static tree
13630 cp_parser_type_id_list (cp_parser* parser)
13631 {
13632   tree types = NULL_TREE;
13633
13634   while (true)
13635     {
13636       cp_token *token;
13637       tree type;
13638
13639       /* Get the next type-id.  */
13640       type = cp_parser_type_id (parser);
13641       /* Add it to the list.  */
13642       types = add_exception_specifier (types, type, /*complain=*/1);
13643       /* Peek at the next token.  */
13644       token = cp_lexer_peek_token (parser->lexer);
13645       /* If it is not a `,', we are done.  */
13646       if (token->type != CPP_COMMA)
13647         break;
13648       /* Consume the `,'.  */
13649       cp_lexer_consume_token (parser->lexer);
13650     }
13651
13652   return nreverse (types);
13653 }
13654
13655 /* Parse a try-block.
13656
13657    try-block:
13658      try compound-statement handler-seq  */
13659
13660 static tree
13661 cp_parser_try_block (cp_parser* parser)
13662 {
13663   tree try_block;
13664
13665   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13666   try_block = begin_try_block ();
13667   cp_parser_compound_statement (parser, NULL, true);
13668   finish_try_block (try_block);
13669   cp_parser_handler_seq (parser);
13670   finish_handler_sequence (try_block);
13671
13672   return try_block;
13673 }
13674
13675 /* Parse a function-try-block.
13676
13677    function-try-block:
13678      try ctor-initializer [opt] function-body handler-seq  */
13679
13680 static bool
13681 cp_parser_function_try_block (cp_parser* parser)
13682 {
13683   tree try_block;
13684   bool ctor_initializer_p;
13685
13686   /* Look for the `try' keyword.  */
13687   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13688     return false;
13689   /* Let the rest of the front-end know where we are.  */
13690   try_block = begin_function_try_block ();
13691   /* Parse the function-body.  */
13692   ctor_initializer_p
13693     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13694   /* We're done with the `try' part.  */
13695   finish_function_try_block (try_block);
13696   /* Parse the handlers.  */
13697   cp_parser_handler_seq (parser);
13698   /* We're done with the handlers.  */
13699   finish_function_handler_sequence (try_block);
13700
13701   return ctor_initializer_p;
13702 }
13703
13704 /* Parse a handler-seq.
13705
13706    handler-seq:
13707      handler handler-seq [opt]  */
13708
13709 static void
13710 cp_parser_handler_seq (cp_parser* parser)
13711 {
13712   while (true)
13713     {
13714       cp_token *token;
13715
13716       /* Parse the handler.  */
13717       cp_parser_handler (parser);
13718       /* Peek at the next token.  */
13719       token = cp_lexer_peek_token (parser->lexer);
13720       /* If it's not `catch' then there are no more handlers.  */
13721       if (!cp_parser_is_keyword (token, RID_CATCH))
13722         break;
13723     }
13724 }
13725
13726 /* Parse a handler.
13727
13728    handler:
13729      catch ( exception-declaration ) compound-statement  */
13730
13731 static void
13732 cp_parser_handler (cp_parser* parser)
13733 {
13734   tree handler;
13735   tree declaration;
13736
13737   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13738   handler = begin_handler ();
13739   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13740   declaration = cp_parser_exception_declaration (parser);
13741   finish_handler_parms (declaration, handler);
13742   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13743   cp_parser_compound_statement (parser, NULL, false);
13744   finish_handler (handler);
13745 }
13746
13747 /* Parse an exception-declaration.
13748
13749    exception-declaration:
13750      type-specifier-seq declarator
13751      type-specifier-seq abstract-declarator
13752      type-specifier-seq
13753      ...
13754
13755    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13756    ellipsis variant is used.  */
13757
13758 static tree
13759 cp_parser_exception_declaration (cp_parser* parser)
13760 {
13761   tree decl;
13762   cp_decl_specifier_seq type_specifiers;
13763   cp_declarator *declarator;
13764   const char *saved_message;
13765
13766   /* If it's an ellipsis, it's easy to handle.  */
13767   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13768     {
13769       /* Consume the `...' token.  */
13770       cp_lexer_consume_token (parser->lexer);
13771       return NULL_TREE;
13772     }
13773
13774   /* Types may not be defined in exception-declarations.  */
13775   saved_message = parser->type_definition_forbidden_message;
13776   parser->type_definition_forbidden_message
13777     = "types may not be defined in exception-declarations";
13778
13779   /* Parse the type-specifier-seq.  */
13780   cp_parser_type_specifier_seq (parser, &type_specifiers);
13781   /* If it's a `)', then there is no declarator.  */
13782   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13783     declarator = NULL;
13784   else
13785     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13786                                        /*ctor_dtor_or_conv_p=*/NULL,
13787                                        /*parenthesized_p=*/NULL,
13788                                        /*member_p=*/false);
13789
13790   /* Restore the saved message.  */
13791   parser->type_definition_forbidden_message = saved_message;
13792
13793   if (type_specifiers.any_specifiers_p)
13794     {
13795       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13796       if (decl == NULL_TREE)
13797         error ("invalid catch parameter");
13798     }
13799   else
13800     decl = NULL_TREE;
13801
13802   return decl;
13803 }
13804
13805 /* Parse a throw-expression.
13806
13807    throw-expression:
13808      throw assignment-expression [opt]
13809
13810    Returns a THROW_EXPR representing the throw-expression.  */
13811
13812 static tree
13813 cp_parser_throw_expression (cp_parser* parser)
13814 {
13815   tree expression;
13816   cp_token* token;
13817
13818   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13819   token = cp_lexer_peek_token (parser->lexer);
13820   /* Figure out whether or not there is an assignment-expression
13821      following the "throw" keyword.  */
13822   if (token->type == CPP_COMMA
13823       || token->type == CPP_SEMICOLON
13824       || token->type == CPP_CLOSE_PAREN
13825       || token->type == CPP_CLOSE_SQUARE
13826       || token->type == CPP_CLOSE_BRACE
13827       || token->type == CPP_COLON)
13828     expression = NULL_TREE;
13829   else
13830     expression = cp_parser_assignment_expression (parser,
13831                                                   /*cast_p=*/false);
13832
13833   return build_throw (expression);
13834 }
13835
13836 /* GNU Extensions */
13837
13838 /* Parse an (optional) asm-specification.
13839
13840    asm-specification:
13841      asm ( string-literal )
13842
13843    If the asm-specification is present, returns a STRING_CST
13844    corresponding to the string-literal.  Otherwise, returns
13845    NULL_TREE.  */
13846
13847 static tree
13848 cp_parser_asm_specification_opt (cp_parser* parser)
13849 {
13850   cp_token *token;
13851   tree asm_specification;
13852
13853   /* Peek at the next token.  */
13854   token = cp_lexer_peek_token (parser->lexer);
13855   /* If the next token isn't the `asm' keyword, then there's no
13856      asm-specification.  */
13857   if (!cp_parser_is_keyword (token, RID_ASM))
13858     return NULL_TREE;
13859
13860   /* Consume the `asm' token.  */
13861   cp_lexer_consume_token (parser->lexer);
13862   /* Look for the `('.  */
13863   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13864
13865   /* Look for the string-literal.  */
13866   asm_specification = cp_parser_string_literal (parser, false, false);
13867
13868   /* Look for the `)'.  */
13869   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13870
13871   return asm_specification;
13872 }
13873
13874 /* Parse an asm-operand-list.
13875
13876    asm-operand-list:
13877      asm-operand
13878      asm-operand-list , asm-operand
13879
13880    asm-operand:
13881      string-literal ( expression )
13882      [ string-literal ] string-literal ( expression )
13883
13884    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13885    each node is the expression.  The TREE_PURPOSE is itself a
13886    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13887    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13888    is a STRING_CST for the string literal before the parenthesis.  */
13889
13890 static tree
13891 cp_parser_asm_operand_list (cp_parser* parser)
13892 {
13893   tree asm_operands = NULL_TREE;
13894
13895   while (true)
13896     {
13897       tree string_literal;
13898       tree expression;
13899       tree name;
13900
13901       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13902         {
13903           /* Consume the `[' token.  */
13904           cp_lexer_consume_token (parser->lexer);
13905           /* Read the operand name.  */
13906           name = cp_parser_identifier (parser);
13907           if (name != error_mark_node)
13908             name = build_string (IDENTIFIER_LENGTH (name),
13909                                  IDENTIFIER_POINTER (name));
13910           /* Look for the closing `]'.  */
13911           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13912         }
13913       else
13914         name = NULL_TREE;
13915       /* Look for the string-literal.  */
13916       string_literal = cp_parser_string_literal (parser, false, false);
13917
13918       /* Look for the `('.  */
13919       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13920       /* Parse the expression.  */
13921       expression = cp_parser_expression (parser, /*cast_p=*/false);
13922       /* Look for the `)'.  */
13923       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13924
13925       /* Add this operand to the list.  */
13926       asm_operands = tree_cons (build_tree_list (name, string_literal),
13927                                 expression,
13928                                 asm_operands);
13929       /* If the next token is not a `,', there are no more
13930          operands.  */
13931       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13932         break;
13933       /* Consume the `,'.  */
13934       cp_lexer_consume_token (parser->lexer);
13935     }
13936
13937   return nreverse (asm_operands);
13938 }
13939
13940 /* Parse an asm-clobber-list.
13941
13942    asm-clobber-list:
13943      string-literal
13944      asm-clobber-list , string-literal
13945
13946    Returns a TREE_LIST, indicating the clobbers in the order that they
13947    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13948
13949 static tree
13950 cp_parser_asm_clobber_list (cp_parser* parser)
13951 {
13952   tree clobbers = NULL_TREE;
13953
13954   while (true)
13955     {
13956       tree string_literal;
13957
13958       /* Look for the string literal.  */
13959       string_literal = cp_parser_string_literal (parser, false, false);
13960       /* Add it to the list.  */
13961       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13962       /* If the next token is not a `,', then the list is
13963          complete.  */
13964       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13965         break;
13966       /* Consume the `,' token.  */
13967       cp_lexer_consume_token (parser->lexer);
13968     }
13969
13970   return clobbers;
13971 }
13972
13973 /* Parse an (optional) series of attributes.
13974
13975    attributes:
13976      attributes attribute
13977
13978    attribute:
13979      __attribute__ (( attribute-list [opt] ))
13980
13981    The return value is as for cp_parser_attribute_list.  */
13982
13983 static tree
13984 cp_parser_attributes_opt (cp_parser* parser)
13985 {
13986   tree attributes = NULL_TREE;
13987
13988   while (true)
13989     {
13990       cp_token *token;
13991       tree attribute_list;
13992
13993       /* Peek at the next token.  */
13994       token = cp_lexer_peek_token (parser->lexer);
13995       /* If it's not `__attribute__', then we're done.  */
13996       if (token->keyword != RID_ATTRIBUTE)
13997         break;
13998
13999       /* Consume the `__attribute__' keyword.  */
14000       cp_lexer_consume_token (parser->lexer);
14001       /* Look for the two `(' tokens.  */
14002       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14003       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14004
14005       /* Peek at the next token.  */
14006       token = cp_lexer_peek_token (parser->lexer);
14007       if (token->type != CPP_CLOSE_PAREN)
14008         /* Parse the attribute-list.  */
14009         attribute_list = cp_parser_attribute_list (parser);
14010       else
14011         /* If the next token is a `)', then there is no attribute
14012            list.  */
14013         attribute_list = NULL;
14014
14015       /* Look for the two `)' tokens.  */
14016       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14017       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14018
14019       /* Add these new attributes to the list.  */
14020       attributes = chainon (attributes, attribute_list);
14021     }
14022
14023   return attributes;
14024 }
14025
14026 /* Parse an attribute-list.
14027
14028    attribute-list:
14029      attribute
14030      attribute-list , attribute
14031
14032    attribute:
14033      identifier
14034      identifier ( identifier )
14035      identifier ( identifier , expression-list )
14036      identifier ( expression-list )
14037
14038    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
14039    TREE_PURPOSE of each node is the identifier indicating which
14040    attribute is in use.  The TREE_VALUE represents the arguments, if
14041    any.  */
14042
14043 static tree
14044 cp_parser_attribute_list (cp_parser* parser)
14045 {
14046   tree attribute_list = NULL_TREE;
14047   bool save_translate_strings_p = parser->translate_strings_p;
14048
14049   parser->translate_strings_p = false;
14050   while (true)
14051     {
14052       cp_token *token;
14053       tree identifier;
14054       tree attribute;
14055
14056       /* Look for the identifier.  We also allow keywords here; for
14057          example `__attribute__ ((const))' is legal.  */
14058       token = cp_lexer_peek_token (parser->lexer);
14059       if (token->type != CPP_NAME
14060           && token->type != CPP_KEYWORD)
14061         return error_mark_node;
14062       /* Consume the token.  */
14063       token = cp_lexer_consume_token (parser->lexer);
14064
14065       /* Save away the identifier that indicates which attribute this is.  */
14066       identifier = token->value;
14067       attribute = build_tree_list (identifier, NULL_TREE);
14068
14069       /* Peek at the next token.  */
14070       token = cp_lexer_peek_token (parser->lexer);
14071       /* If it's an `(', then parse the attribute arguments.  */
14072       if (token->type == CPP_OPEN_PAREN)
14073         {
14074           tree arguments;
14075
14076           arguments = (cp_parser_parenthesized_expression_list
14077                        (parser, true, /*cast_p=*/false, 
14078                         /*non_constant_p=*/NULL));
14079           /* Save the identifier and arguments away.  */
14080           TREE_VALUE (attribute) = arguments;
14081         }
14082
14083       /* Add this attribute to the list.  */
14084       TREE_CHAIN (attribute) = attribute_list;
14085       attribute_list = attribute;
14086
14087       /* Now, look for more attributes.  */
14088       token = cp_lexer_peek_token (parser->lexer);
14089       /* If the next token isn't a `,', we're done.  */
14090       if (token->type != CPP_COMMA)
14091         break;
14092
14093       /* Consume the comma and keep going.  */
14094       cp_lexer_consume_token (parser->lexer);
14095     }
14096   parser->translate_strings_p = save_translate_strings_p;
14097
14098   /* We built up the list in reverse order.  */
14099   return nreverse (attribute_list);
14100 }
14101
14102 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14103    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14104    current value of the PEDANTIC flag, regardless of whether or not
14105    the `__extension__' keyword is present.  The caller is responsible
14106    for restoring the value of the PEDANTIC flag.  */
14107
14108 static bool
14109 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14110 {
14111   /* Save the old value of the PEDANTIC flag.  */
14112   *saved_pedantic = pedantic;
14113
14114   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14115     {
14116       /* Consume the `__extension__' token.  */
14117       cp_lexer_consume_token (parser->lexer);
14118       /* We're not being pedantic while the `__extension__' keyword is
14119          in effect.  */
14120       pedantic = 0;
14121
14122       return true;
14123     }
14124
14125   return false;
14126 }
14127
14128 /* Parse a label declaration.
14129
14130    label-declaration:
14131      __label__ label-declarator-seq ;
14132
14133    label-declarator-seq:
14134      identifier , label-declarator-seq
14135      identifier  */
14136
14137 static void
14138 cp_parser_label_declaration (cp_parser* parser)
14139 {
14140   /* Look for the `__label__' keyword.  */
14141   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14142
14143   while (true)
14144     {
14145       tree identifier;
14146
14147       /* Look for an identifier.  */
14148       identifier = cp_parser_identifier (parser);
14149       /* Declare it as a lobel.  */
14150       finish_label_decl (identifier);
14151       /* If the next token is a `;', stop.  */
14152       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14153         break;
14154       /* Look for the `,' separating the label declarations.  */
14155       cp_parser_require (parser, CPP_COMMA, "`,'");
14156     }
14157
14158   /* Look for the final `;'.  */
14159   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14160 }
14161
14162 /* Support Functions */
14163
14164 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14165    NAME should have one of the representations used for an
14166    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14167    is returned.  If PARSER->SCOPE is a dependent type, then a
14168    SCOPE_REF is returned.
14169
14170    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14171    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14172    was formed.  Abstractly, such entities should not be passed to this
14173    function, because they do not need to be looked up, but it is
14174    simpler to check for this special case here, rather than at the
14175    call-sites.
14176
14177    In cases not explicitly covered above, this function returns a
14178    DECL, OVERLOAD, or baselink representing the result of the lookup.
14179    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14180    is returned.
14181
14182    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14183    (e.g., "struct") that was used.  In that case bindings that do not
14184    refer to types are ignored.
14185
14186    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14187    ignored.
14188
14189    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14190    are ignored.
14191
14192    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14193    types.  
14194
14195    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14196    results in an ambiguity, and false otherwise.  */
14197
14198 static tree
14199 cp_parser_lookup_name (cp_parser *parser, tree name,
14200                        enum tag_types tag_type,
14201                        bool is_template, bool is_namespace,
14202                        bool check_dependency,
14203                        bool *ambiguous_p)
14204 {
14205   tree decl;
14206   tree object_type = parser->context->object_type;
14207
14208   /* Assume that the lookup will be unambiguous.  */
14209   if (ambiguous_p)
14210     *ambiguous_p = false;
14211
14212   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14213      no longer valid.  Note that if we are parsing tentatively, and
14214      the parse fails, OBJECT_TYPE will be automatically restored.  */
14215   parser->context->object_type = NULL_TREE;
14216
14217   if (name == error_mark_node)
14218     return error_mark_node;
14219
14220   /* A template-id has already been resolved; there is no lookup to
14221      do.  */
14222   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14223     return name;
14224   if (BASELINK_P (name))
14225     {
14226       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14227                   == TEMPLATE_ID_EXPR);
14228       return name;
14229     }
14230
14231   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14232      it should already have been checked to make sure that the name
14233      used matches the type being destroyed.  */
14234   if (TREE_CODE (name) == BIT_NOT_EXPR)
14235     {
14236       tree type;
14237
14238       /* Figure out to which type this destructor applies.  */
14239       if (parser->scope)
14240         type = parser->scope;
14241       else if (object_type)
14242         type = object_type;
14243       else
14244         type = current_class_type;
14245       /* If that's not a class type, there is no destructor.  */
14246       if (!type || !CLASS_TYPE_P (type))
14247         return error_mark_node;
14248       if (!CLASSTYPE_DESTRUCTORS (type))
14249           return error_mark_node;
14250       /* If it was a class type, return the destructor.  */
14251       return CLASSTYPE_DESTRUCTORS (type);
14252     }
14253
14254   /* By this point, the NAME should be an ordinary identifier.  If
14255      the id-expression was a qualified name, the qualifying scope is
14256      stored in PARSER->SCOPE at this point.  */
14257   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14258
14259   /* Perform the lookup.  */
14260   if (parser->scope)
14261     {
14262       bool dependent_p;
14263
14264       if (parser->scope == error_mark_node)
14265         return error_mark_node;
14266
14267       /* If the SCOPE is dependent, the lookup must be deferred until
14268          the template is instantiated -- unless we are explicitly
14269          looking up names in uninstantiated templates.  Even then, we
14270          cannot look up the name if the scope is not a class type; it
14271          might, for example, be a template type parameter.  */
14272       dependent_p = (TYPE_P (parser->scope)
14273                      && !(parser->in_declarator_p
14274                           && currently_open_class (parser->scope))
14275                      && dependent_type_p (parser->scope));
14276       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14277            && dependent_p)
14278         {
14279           if (tag_type)
14280             {
14281               tree type;
14282
14283               /* The resolution to Core Issue 180 says that `struct
14284                  A::B' should be considered a type-name, even if `A'
14285                  is dependent.  */
14286               type = make_typename_type (parser->scope, name, tag_type,
14287                                          /*complain=*/1);
14288               decl = TYPE_NAME (type);
14289             }
14290           else if (is_template)
14291             decl = make_unbound_class_template (parser->scope,
14292                                                 name, NULL_TREE,
14293                                                 /*complain=*/1);
14294           else
14295             decl = build_nt (SCOPE_REF, parser->scope, name);
14296         }
14297       else
14298         {
14299           tree pushed_scope = NULL_TREE;
14300
14301           /* If PARSER->SCOPE is a dependent type, then it must be a
14302              class type, and we must not be checking dependencies;
14303              otherwise, we would have processed this lookup above.  So
14304              that PARSER->SCOPE is not considered a dependent base by
14305              lookup_member, we must enter the scope here.  */
14306           if (dependent_p)
14307             pushed_scope = push_scope (parser->scope);
14308           /* If the PARSER->SCOPE is a a template specialization, it
14309              may be instantiated during name lookup.  In that case,
14310              errors may be issued.  Even if we rollback the current
14311              tentative parse, those errors are valid.  */
14312           decl = lookup_qualified_name (parser->scope, name, 
14313                                         tag_type != none_type, 
14314                                         /*complain=*/true);
14315           if (pushed_scope)
14316             pop_scope (pushed_scope);
14317         }
14318       parser->qualifying_scope = parser->scope;
14319       parser->object_scope = NULL_TREE;
14320     }
14321   else if (object_type)
14322     {
14323       tree object_decl = NULL_TREE;
14324       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14325          OBJECT_TYPE is not a class.  */
14326       if (CLASS_TYPE_P (object_type))
14327         /* If the OBJECT_TYPE is a template specialization, it may
14328            be instantiated during name lookup.  In that case, errors
14329            may be issued.  Even if we rollback the current tentative
14330            parse, those errors are valid.  */
14331         object_decl = lookup_member (object_type,
14332                                      name,
14333                                      /*protect=*/0, 
14334                                      tag_type != none_type);
14335       /* Look it up in the enclosing context, too.  */
14336       decl = lookup_name_real (name, tag_type != none_type, 
14337                                /*nonclass=*/0,
14338                                /*block_p=*/true, is_namespace,
14339                                /*flags=*/0);
14340       parser->object_scope = object_type;
14341       parser->qualifying_scope = NULL_TREE;
14342       if (object_decl)
14343         decl = object_decl;
14344     }
14345   else
14346     {
14347       decl = lookup_name_real (name, tag_type != none_type, 
14348                                /*nonclass=*/0,
14349                                /*block_p=*/true, is_namespace,
14350                                /*flags=*/0);
14351       parser->qualifying_scope = NULL_TREE;
14352       parser->object_scope = NULL_TREE;
14353     }
14354
14355   /* If the lookup failed, let our caller know.  */
14356   if (!decl
14357       || decl == error_mark_node
14358       || (TREE_CODE (decl) == FUNCTION_DECL
14359           && DECL_ANTICIPATED (decl)))
14360     return error_mark_node;
14361
14362   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14363   if (TREE_CODE (decl) == TREE_LIST)
14364     {
14365       if (ambiguous_p)
14366         *ambiguous_p = true;
14367       /* The error message we have to print is too complicated for
14368          cp_parser_error, so we incorporate its actions directly.  */
14369       if (!cp_parser_simulate_error (parser))
14370         {
14371           error ("reference to %qD is ambiguous", name);
14372           print_candidates (decl);
14373         }
14374       return error_mark_node;
14375     }
14376
14377   gcc_assert (DECL_P (decl)
14378               || TREE_CODE (decl) == OVERLOAD
14379               || TREE_CODE (decl) == SCOPE_REF
14380               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14381               || BASELINK_P (decl));
14382
14383   /* If we have resolved the name of a member declaration, check to
14384      see if the declaration is accessible.  When the name resolves to
14385      set of overloaded functions, accessibility is checked when
14386      overload resolution is done.
14387
14388      During an explicit instantiation, access is not checked at all,
14389      as per [temp.explicit].  */
14390   if (DECL_P (decl))
14391     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14392
14393   return decl;
14394 }
14395
14396 /* Like cp_parser_lookup_name, but for use in the typical case where
14397    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14398    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14399
14400 static tree
14401 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14402 {
14403   return cp_parser_lookup_name (parser, name,
14404                                 none_type,
14405                                 /*is_template=*/false,
14406                                 /*is_namespace=*/false,
14407                                 /*check_dependency=*/true,
14408                                 /*ambiguous_p=*/NULL);
14409 }
14410
14411 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14412    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14413    true, the DECL indicates the class being defined in a class-head,
14414    or declared in an elaborated-type-specifier.
14415
14416    Otherwise, return DECL.  */
14417
14418 static tree
14419 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14420 {
14421   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14422      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14423
14424        struct A {
14425          template <typename T> struct B;
14426        };
14427
14428        template <typename T> struct A::B {};
14429
14430      Similarly, in a elaborated-type-specifier:
14431
14432        namespace N { struct X{}; }
14433
14434        struct A {
14435          template <typename T> friend struct N::X;
14436        };
14437
14438      However, if the DECL refers to a class type, and we are in
14439      the scope of the class, then the name lookup automatically
14440      finds the TYPE_DECL created by build_self_reference rather
14441      than a TEMPLATE_DECL.  For example, in:
14442
14443        template <class T> struct S {
14444          S s;
14445        };
14446
14447      there is no need to handle such case.  */
14448
14449   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14450     return DECL_TEMPLATE_RESULT (decl);
14451
14452   return decl;
14453 }
14454
14455 /* If too many, or too few, template-parameter lists apply to the
14456    declarator, issue an error message.  Returns TRUE if all went well,
14457    and FALSE otherwise.  */
14458
14459 static bool
14460 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14461                                                 cp_declarator *declarator)
14462 {
14463   unsigned num_templates;
14464
14465   /* We haven't seen any classes that involve template parameters yet.  */
14466   num_templates = 0;
14467
14468   switch (declarator->kind)
14469     {
14470     case cdk_id:
14471       if (declarator->u.id.qualifying_scope)
14472         {
14473           tree scope;
14474           tree member;
14475
14476           scope = declarator->u.id.qualifying_scope;
14477           member = declarator->u.id.unqualified_name;
14478
14479           while (scope && CLASS_TYPE_P (scope))
14480             {
14481               /* You're supposed to have one `template <...>'
14482                  for every template class, but you don't need one
14483                  for a full specialization.  For example:
14484
14485                  template <class T> struct S{};
14486                  template <> struct S<int> { void f(); };
14487                  void S<int>::f () {}
14488
14489                  is correct; there shouldn't be a `template <>' for
14490                  the definition of `S<int>::f'.  */
14491               if (CLASSTYPE_TEMPLATE_INFO (scope)
14492                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14493                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14494                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14495                 ++num_templates;
14496
14497               scope = TYPE_CONTEXT (scope);
14498             }
14499         }
14500       else if (TREE_CODE (declarator->u.id.unqualified_name) 
14501                == TEMPLATE_ID_EXPR)
14502         /* If the DECLARATOR has the form `X<y>' then it uses one
14503            additional level of template parameters.  */
14504         ++num_templates;
14505
14506       return cp_parser_check_template_parameters (parser,
14507                                                   num_templates);
14508
14509     case cdk_function:
14510     case cdk_array:
14511     case cdk_pointer:
14512     case cdk_reference:
14513     case cdk_ptrmem:
14514       return (cp_parser_check_declarator_template_parameters
14515               (parser, declarator->declarator));
14516
14517     case cdk_error:
14518       return true;
14519
14520     default:
14521       gcc_unreachable ();
14522     }
14523   return false;
14524 }
14525
14526 /* NUM_TEMPLATES were used in the current declaration.  If that is
14527    invalid, return FALSE and issue an error messages.  Otherwise,
14528    return TRUE.  */
14529
14530 static bool
14531 cp_parser_check_template_parameters (cp_parser* parser,
14532                                      unsigned num_templates)
14533 {
14534   /* If there are more template classes than parameter lists, we have
14535      something like:
14536
14537        template <class T> void S<T>::R<T>::f ();  */
14538   if (parser->num_template_parameter_lists < num_templates)
14539     {
14540       error ("too few template-parameter-lists");
14541       return false;
14542     }
14543   /* If there are the same number of template classes and parameter
14544      lists, that's OK.  */
14545   if (parser->num_template_parameter_lists == num_templates)
14546     return true;
14547   /* If there are more, but only one more, then we are referring to a
14548      member template.  That's OK too.  */
14549   if (parser->num_template_parameter_lists == num_templates + 1)
14550       return true;
14551   /* Otherwise, there are too many template parameter lists.  We have
14552      something like:
14553
14554      template <class T> template <class U> void S::f();  */
14555   error ("too many template-parameter-lists");
14556   return false;
14557 }
14558
14559 /* Parse an optional `::' token indicating that the following name is
14560    from the global namespace.  If so, PARSER->SCOPE is set to the
14561    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14562    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14563    Returns the new value of PARSER->SCOPE, if the `::' token is
14564    present, and NULL_TREE otherwise.  */
14565
14566 static tree
14567 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14568 {
14569   cp_token *token;
14570
14571   /* Peek at the next token.  */
14572   token = cp_lexer_peek_token (parser->lexer);
14573   /* If we're looking at a `::' token then we're starting from the
14574      global namespace, not our current location.  */
14575   if (token->type == CPP_SCOPE)
14576     {
14577       /* Consume the `::' token.  */
14578       cp_lexer_consume_token (parser->lexer);
14579       /* Set the SCOPE so that we know where to start the lookup.  */
14580       parser->scope = global_namespace;
14581       parser->qualifying_scope = global_namespace;
14582       parser->object_scope = NULL_TREE;
14583
14584       return parser->scope;
14585     }
14586   else if (!current_scope_valid_p)
14587     {
14588       parser->scope = NULL_TREE;
14589       parser->qualifying_scope = NULL_TREE;
14590       parser->object_scope = NULL_TREE;
14591     }
14592
14593   return NULL_TREE;
14594 }
14595
14596 /* Returns TRUE if the upcoming token sequence is the start of a
14597    constructor declarator.  If FRIEND_P is true, the declarator is
14598    preceded by the `friend' specifier.  */
14599
14600 static bool
14601 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14602 {
14603   bool constructor_p;
14604   tree type_decl = NULL_TREE;
14605   bool nested_name_p;
14606   cp_token *next_token;
14607
14608   /* The common case is that this is not a constructor declarator, so
14609      try to avoid doing lots of work if at all possible.  It's not
14610      valid declare a constructor at function scope.  */
14611   if (at_function_scope_p ())
14612     return false;
14613   /* And only certain tokens can begin a constructor declarator.  */
14614   next_token = cp_lexer_peek_token (parser->lexer);
14615   if (next_token->type != CPP_NAME
14616       && next_token->type != CPP_SCOPE
14617       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14618       && next_token->type != CPP_TEMPLATE_ID)
14619     return false;
14620
14621   /* Parse tentatively; we are going to roll back all of the tokens
14622      consumed here.  */
14623   cp_parser_parse_tentatively (parser);
14624   /* Assume that we are looking at a constructor declarator.  */
14625   constructor_p = true;
14626
14627   /* Look for the optional `::' operator.  */
14628   cp_parser_global_scope_opt (parser,
14629                               /*current_scope_valid_p=*/false);
14630   /* Look for the nested-name-specifier.  */
14631   nested_name_p
14632     = (cp_parser_nested_name_specifier_opt (parser,
14633                                             /*typename_keyword_p=*/false,
14634                                             /*check_dependency_p=*/false,
14635                                             /*type_p=*/false,
14636                                             /*is_declaration=*/false)
14637        != NULL_TREE);
14638   /* Outside of a class-specifier, there must be a
14639      nested-name-specifier.  */
14640   if (!nested_name_p &&
14641       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14642        || friend_p))
14643     constructor_p = false;
14644   /* If we still think that this might be a constructor-declarator,
14645      look for a class-name.  */
14646   if (constructor_p)
14647     {
14648       /* If we have:
14649
14650            template <typename T> struct S { S(); };
14651            template <typename T> S<T>::S ();
14652
14653          we must recognize that the nested `S' names a class.
14654          Similarly, for:
14655
14656            template <typename T> S<T>::S<T> ();
14657
14658          we must recognize that the nested `S' names a template.  */
14659       type_decl = cp_parser_class_name (parser,
14660                                         /*typename_keyword_p=*/false,
14661                                         /*template_keyword_p=*/false,
14662                                         none_type,
14663                                         /*check_dependency_p=*/false,
14664                                         /*class_head_p=*/false,
14665                                         /*is_declaration=*/false);
14666       /* If there was no class-name, then this is not a constructor.  */
14667       constructor_p = !cp_parser_error_occurred (parser);
14668     }
14669
14670   /* If we're still considering a constructor, we have to see a `(',
14671      to begin the parameter-declaration-clause, followed by either a
14672      `)', an `...', or a decl-specifier.  We need to check for a
14673      type-specifier to avoid being fooled into thinking that:
14674
14675        S::S (f) (int);
14676
14677      is a constructor.  (It is actually a function named `f' that
14678      takes one parameter (of type `int') and returns a value of type
14679      `S::S'.  */
14680   if (constructor_p
14681       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14682     {
14683       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14684           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14685           /* A parameter declaration begins with a decl-specifier,
14686              which is either the "attribute" keyword, a storage class
14687              specifier, or (usually) a type-specifier.  */
14688           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14689           && !cp_parser_storage_class_specifier_opt (parser))
14690         {
14691           tree type;
14692           tree pushed_scope = NULL_TREE;
14693           unsigned saved_num_template_parameter_lists;
14694
14695           /* Names appearing in the type-specifier should be looked up
14696              in the scope of the class.  */
14697           if (current_class_type)
14698             type = NULL_TREE;
14699           else
14700             {
14701               type = TREE_TYPE (type_decl);
14702               if (TREE_CODE (type) == TYPENAME_TYPE)
14703                 {
14704                   type = resolve_typename_type (type,
14705                                                 /*only_current_p=*/false);
14706                   if (type == error_mark_node)
14707                     {
14708                       cp_parser_abort_tentative_parse (parser);
14709                       return false;
14710                     }
14711                 }
14712               pushed_scope = push_scope (type);
14713             }
14714
14715           /* Inside the constructor parameter list, surrounding
14716              template-parameter-lists do not apply.  */
14717           saved_num_template_parameter_lists
14718             = parser->num_template_parameter_lists;
14719           parser->num_template_parameter_lists = 0;
14720
14721           /* Look for the type-specifier.  */
14722           cp_parser_type_specifier (parser,
14723                                     CP_PARSER_FLAGS_NONE,
14724                                     /*decl_specs=*/NULL,
14725                                     /*is_declarator=*/true,
14726                                     /*declares_class_or_enum=*/NULL,
14727                                     /*is_cv_qualifier=*/NULL);
14728
14729           parser->num_template_parameter_lists
14730             = saved_num_template_parameter_lists;
14731
14732           /* Leave the scope of the class.  */
14733           if (pushed_scope)
14734             pop_scope (pushed_scope);
14735
14736           constructor_p = !cp_parser_error_occurred (parser);
14737         }
14738     }
14739   else
14740     constructor_p = false;
14741   /* We did not really want to consume any tokens.  */
14742   cp_parser_abort_tentative_parse (parser);
14743
14744   return constructor_p;
14745 }
14746
14747 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14748    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14749    they must be performed once we are in the scope of the function.
14750
14751    Returns the function defined.  */
14752
14753 static tree
14754 cp_parser_function_definition_from_specifiers_and_declarator
14755   (cp_parser* parser,
14756    cp_decl_specifier_seq *decl_specifiers,
14757    tree attributes,
14758    const cp_declarator *declarator)
14759 {
14760   tree fn;
14761   bool success_p;
14762
14763   /* Begin the function-definition.  */
14764   success_p = start_function (decl_specifiers, declarator, attributes);
14765
14766   /* The things we're about to see are not directly qualified by any
14767      template headers we've seen thus far.  */
14768   reset_specialization ();
14769
14770   /* If there were names looked up in the decl-specifier-seq that we
14771      did not check, check them now.  We must wait until we are in the
14772      scope of the function to perform the checks, since the function
14773      might be a friend.  */
14774   perform_deferred_access_checks ();
14775
14776   if (!success_p)
14777     {
14778       /* Skip the entire function.  */
14779       error ("invalid function declaration");
14780       cp_parser_skip_to_end_of_block_or_statement (parser);
14781       fn = error_mark_node;
14782     }
14783   else
14784     fn = cp_parser_function_definition_after_declarator (parser,
14785                                                          /*inline_p=*/false);
14786
14787   return fn;
14788 }
14789
14790 /* Parse the part of a function-definition that follows the
14791    declarator.  INLINE_P is TRUE iff this function is an inline
14792    function defined with a class-specifier.
14793
14794    Returns the function defined.  */
14795
14796 static tree
14797 cp_parser_function_definition_after_declarator (cp_parser* parser,
14798                                                 bool inline_p)
14799 {
14800   tree fn;
14801   bool ctor_initializer_p = false;
14802   bool saved_in_unbraced_linkage_specification_p;
14803   unsigned saved_num_template_parameter_lists;
14804
14805   /* If the next token is `return', then the code may be trying to
14806      make use of the "named return value" extension that G++ used to
14807      support.  */
14808   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14809     {
14810       /* Consume the `return' keyword.  */
14811       cp_lexer_consume_token (parser->lexer);
14812       /* Look for the identifier that indicates what value is to be
14813          returned.  */
14814       cp_parser_identifier (parser);
14815       /* Issue an error message.  */
14816       error ("named return values are no longer supported");
14817       /* Skip tokens until we reach the start of the function body.  */
14818       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14819              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14820         cp_lexer_consume_token (parser->lexer);
14821     }
14822   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14823      anything declared inside `f'.  */
14824   saved_in_unbraced_linkage_specification_p
14825     = parser->in_unbraced_linkage_specification_p;
14826   parser->in_unbraced_linkage_specification_p = false;
14827   /* Inside the function, surrounding template-parameter-lists do not
14828      apply.  */
14829   saved_num_template_parameter_lists
14830     = parser->num_template_parameter_lists;
14831   parser->num_template_parameter_lists = 0;
14832   /* If the next token is `try', then we are looking at a
14833      function-try-block.  */
14834   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14835     ctor_initializer_p = cp_parser_function_try_block (parser);
14836   /* A function-try-block includes the function-body, so we only do
14837      this next part if we're not processing a function-try-block.  */
14838   else
14839     ctor_initializer_p
14840       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14841
14842   /* Finish the function.  */
14843   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14844                         (inline_p ? 2 : 0));
14845   /* Generate code for it, if necessary.  */
14846   expand_or_defer_fn (fn);
14847   /* Restore the saved values.  */
14848   parser->in_unbraced_linkage_specification_p
14849     = saved_in_unbraced_linkage_specification_p;
14850   parser->num_template_parameter_lists
14851     = saved_num_template_parameter_lists;
14852
14853   return fn;
14854 }
14855
14856 /* Parse a template-declaration, assuming that the `export' (and
14857    `extern') keywords, if present, has already been scanned.  MEMBER_P
14858    is as for cp_parser_template_declaration.  */
14859
14860 static void
14861 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14862 {
14863   tree decl = NULL_TREE;
14864   tree parameter_list;
14865   bool friend_p = false;
14866
14867   /* Look for the `template' keyword.  */
14868   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14869     return;
14870
14871   /* And the `<'.  */
14872   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14873     return;
14874
14875   /* If the next token is `>', then we have an invalid
14876      specialization.  Rather than complain about an invalid template
14877      parameter, issue an error message here.  */
14878   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14879     {
14880       cp_parser_error (parser, "invalid explicit specialization");
14881       begin_specialization ();
14882       parameter_list = NULL_TREE;
14883     }
14884   else
14885     {
14886       /* Parse the template parameters.  */
14887       begin_template_parm_list ();
14888       parameter_list = cp_parser_template_parameter_list (parser);
14889       parameter_list = end_template_parm_list (parameter_list);
14890     }
14891
14892   /* Look for the `>'.  */
14893   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14894   /* We just processed one more parameter list.  */
14895   ++parser->num_template_parameter_lists;
14896   /* If the next token is `template', there are more template
14897      parameters.  */
14898   if (cp_lexer_next_token_is_keyword (parser->lexer,
14899                                       RID_TEMPLATE))
14900     cp_parser_template_declaration_after_export (parser, member_p);
14901   else
14902     {
14903       /* There are no access checks when parsing a template, as we do not
14904          know if a specialization will be a friend.  */
14905       push_deferring_access_checks (dk_no_check);
14906
14907       decl = cp_parser_single_declaration (parser,
14908                                            member_p,
14909                                            &friend_p);
14910
14911       pop_deferring_access_checks ();
14912
14913       /* If this is a member template declaration, let the front
14914          end know.  */
14915       if (member_p && !friend_p && decl)
14916         {
14917           if (TREE_CODE (decl) == TYPE_DECL)
14918             cp_parser_check_access_in_redeclaration (decl);
14919
14920           decl = finish_member_template_decl (decl);
14921         }
14922       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14923         make_friend_class (current_class_type, TREE_TYPE (decl),
14924                            /*complain=*/true);
14925     }
14926   /* We are done with the current parameter list.  */
14927   --parser->num_template_parameter_lists;
14928
14929   /* Finish up.  */
14930   finish_template_decl (parameter_list);
14931
14932   /* Register member declarations.  */
14933   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14934     finish_member_declaration (decl);
14935
14936   /* If DECL is a function template, we must return to parse it later.
14937      (Even though there is no definition, there might be default
14938      arguments that need handling.)  */
14939   if (member_p && decl
14940       && (TREE_CODE (decl) == FUNCTION_DECL
14941           || DECL_FUNCTION_TEMPLATE_P (decl)))
14942     TREE_VALUE (parser->unparsed_functions_queues)
14943       = tree_cons (NULL_TREE, decl,
14944                    TREE_VALUE (parser->unparsed_functions_queues));
14945 }
14946
14947 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14948    `function-definition' sequence.  MEMBER_P is true, this declaration
14949    appears in a class scope.
14950
14951    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14952    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14953
14954 static tree
14955 cp_parser_single_declaration (cp_parser* parser,
14956                               bool member_p,
14957                               bool* friend_p)
14958 {
14959   int declares_class_or_enum;
14960   tree decl = NULL_TREE;
14961   cp_decl_specifier_seq decl_specifiers;
14962   bool function_definition_p = false;
14963
14964   /* This function is only used when processing a template
14965      declaration.  */
14966   gcc_assert (innermost_scope_kind () == sk_template_parms
14967               || innermost_scope_kind () == sk_template_spec);
14968
14969   /* Defer access checks until we know what is being declared.  */
14970   push_deferring_access_checks (dk_deferred);
14971
14972   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14973      alternative.  */
14974   cp_parser_decl_specifier_seq (parser,
14975                                 CP_PARSER_FLAGS_OPTIONAL,
14976                                 &decl_specifiers,
14977                                 &declares_class_or_enum);
14978   if (friend_p)
14979     *friend_p = cp_parser_friend_p (&decl_specifiers);
14980
14981   /* There are no template typedefs.  */
14982   if (decl_specifiers.specs[(int) ds_typedef])
14983     {
14984       error ("template declaration of %qs", "typedef");
14985       decl = error_mark_node;
14986     }
14987
14988   /* Gather up the access checks that occurred the
14989      decl-specifier-seq.  */
14990   stop_deferring_access_checks ();
14991
14992   /* Check for the declaration of a template class.  */
14993   if (declares_class_or_enum)
14994     {
14995       if (cp_parser_declares_only_class_p (parser))
14996         {
14997           decl = shadow_tag (&decl_specifiers);
14998
14999           /* In this case:
15000
15001                struct C {
15002                  friend template <typename T> struct A<T>::B;
15003                };
15004
15005              A<T>::B will be represented by a TYPENAME_TYPE, and
15006              therefore not recognized by shadow_tag.  */
15007           if (friend_p && *friend_p
15008               && !decl
15009               && decl_specifiers.type
15010               && TYPE_P (decl_specifiers.type))
15011             decl = decl_specifiers.type;
15012
15013           if (decl && decl != error_mark_node)
15014             decl = TYPE_NAME (decl);
15015           else
15016             decl = error_mark_node;
15017         }
15018     }
15019   /* If it's not a template class, try for a template function.  If
15020      the next token is a `;', then this declaration does not declare
15021      anything.  But, if there were errors in the decl-specifiers, then
15022      the error might well have come from an attempted class-specifier.
15023      In that case, there's no need to warn about a missing declarator.  */
15024   if (!decl
15025       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15026           || decl_specifiers.type != error_mark_node))
15027     decl = cp_parser_init_declarator (parser,
15028                                       &decl_specifiers,
15029                                       /*function_definition_allowed_p=*/true,
15030                                       member_p,
15031                                       declares_class_or_enum,
15032                                       &function_definition_p);
15033
15034   pop_deferring_access_checks ();
15035
15036   /* Clear any current qualification; whatever comes next is the start
15037      of something new.  */
15038   parser->scope = NULL_TREE;
15039   parser->qualifying_scope = NULL_TREE;
15040   parser->object_scope = NULL_TREE;
15041   /* Look for a trailing `;' after the declaration.  */
15042   if (!function_definition_p
15043       && (decl == error_mark_node
15044           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15045     cp_parser_skip_to_end_of_block_or_statement (parser);
15046
15047   return decl;
15048 }
15049
15050 /* Parse a cast-expression that is not the operand of a unary "&".  */
15051
15052 static tree
15053 cp_parser_simple_cast_expression (cp_parser *parser)
15054 {
15055   return cp_parser_cast_expression (parser, /*address_p=*/false,
15056                                     /*cast_p=*/false);
15057 }
15058
15059 /* Parse a functional cast to TYPE.  Returns an expression
15060    representing the cast.  */
15061
15062 static tree
15063 cp_parser_functional_cast (cp_parser* parser, tree type)
15064 {
15065   tree expression_list;
15066   tree cast;
15067
15068   expression_list
15069     = cp_parser_parenthesized_expression_list (parser, false,
15070                                                /*cast_p=*/true,
15071                                                /*non_constant_p=*/NULL);
15072
15073   cast = build_functional_cast (type, expression_list);
15074   /* [expr.const]/1: In an integral constant expression "only type
15075      conversions to integral or enumeration type can be used".  */
15076   if (cast != error_mark_node && !type_dependent_expression_p (type)
15077       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15078     {
15079       if (cp_parser_non_integral_constant_expression
15080           (parser, "a call to a constructor"))
15081         return error_mark_node;
15082     }
15083   return cast;
15084 }
15085
15086 /* Save the tokens that make up the body of a member function defined
15087    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15088    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15089    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15090    for the member function.  */
15091
15092 static tree
15093 cp_parser_save_member_function_body (cp_parser* parser,
15094                                      cp_decl_specifier_seq *decl_specifiers,
15095                                      cp_declarator *declarator,
15096                                      tree attributes)
15097 {
15098   cp_token *first;
15099   cp_token *last;
15100   tree fn;
15101
15102   /* Create the function-declaration.  */
15103   fn = start_method (decl_specifiers, declarator, attributes);
15104   /* If something went badly wrong, bail out now.  */
15105   if (fn == error_mark_node)
15106     {
15107       /* If there's a function-body, skip it.  */
15108       if (cp_parser_token_starts_function_definition_p
15109           (cp_lexer_peek_token (parser->lexer)))
15110         cp_parser_skip_to_end_of_block_or_statement (parser);
15111       return error_mark_node;
15112     }
15113
15114   /* Remember it, if there default args to post process.  */
15115   cp_parser_save_default_args (parser, fn);
15116
15117   /* Save away the tokens that make up the body of the
15118      function.  */
15119   first = parser->lexer->next_token;
15120   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15121   /* Handle function try blocks.  */
15122   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15123     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15124   last = parser->lexer->next_token;
15125
15126   /* Save away the inline definition; we will process it when the
15127      class is complete.  */
15128   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15129   DECL_PENDING_INLINE_P (fn) = 1;
15130
15131   /* We need to know that this was defined in the class, so that
15132      friend templates are handled correctly.  */
15133   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15134
15135   /* We're done with the inline definition.  */
15136   finish_method (fn);
15137
15138   /* Add FN to the queue of functions to be parsed later.  */
15139   TREE_VALUE (parser->unparsed_functions_queues)
15140     = tree_cons (NULL_TREE, fn,
15141                  TREE_VALUE (parser->unparsed_functions_queues));
15142
15143   return fn;
15144 }
15145
15146 /* Parse a template-argument-list, as well as the trailing ">" (but
15147    not the opening ">").  See cp_parser_template_argument_list for the
15148    return value.  */
15149
15150 static tree
15151 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15152 {
15153   tree arguments;
15154   tree saved_scope;
15155   tree saved_qualifying_scope;
15156   tree saved_object_scope;
15157   bool saved_greater_than_is_operator_p;
15158
15159   /* [temp.names]
15160
15161      When parsing a template-id, the first non-nested `>' is taken as
15162      the end of the template-argument-list rather than a greater-than
15163      operator.  */
15164   saved_greater_than_is_operator_p
15165     = parser->greater_than_is_operator_p;
15166   parser->greater_than_is_operator_p = false;
15167   /* Parsing the argument list may modify SCOPE, so we save it
15168      here.  */
15169   saved_scope = parser->scope;
15170   saved_qualifying_scope = parser->qualifying_scope;
15171   saved_object_scope = parser->object_scope;
15172   /* Parse the template-argument-list itself.  */
15173   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15174     arguments = NULL_TREE;
15175   else
15176     arguments = cp_parser_template_argument_list (parser);
15177   /* Look for the `>' that ends the template-argument-list. If we find
15178      a '>>' instead, it's probably just a typo.  */
15179   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15180     {
15181       if (!saved_greater_than_is_operator_p)
15182         {
15183           /* If we're in a nested template argument list, the '>>' has
15184             to be a typo for '> >'. We emit the error message, but we
15185             continue parsing and we push a '>' as next token, so that
15186             the argument list will be parsed correctly.  Note that the
15187             global source location is still on the token before the
15188             '>>', so we need to say explicitly where we want it.  */
15189           cp_token *token = cp_lexer_peek_token (parser->lexer);
15190           error ("%H%<>>%> should be %<> >%> "
15191                  "within a nested template argument list",
15192                  &token->location);
15193
15194           /* ??? Proper recovery should terminate two levels of
15195              template argument list here.  */
15196           token->type = CPP_GREATER;
15197         }
15198       else
15199         {
15200           /* If this is not a nested template argument list, the '>>'
15201             is a typo for '>'. Emit an error message and continue.
15202             Same deal about the token location, but here we can get it
15203             right by consuming the '>>' before issuing the diagnostic.  */
15204           cp_lexer_consume_token (parser->lexer);
15205           error ("spurious %<>>%>, use %<>%> to terminate "
15206                  "a template argument list");
15207         }
15208     }
15209   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15210     error ("missing %<>%> to terminate the template argument list");
15211   else
15212     /* It's what we want, a '>'; consume it.  */
15213     cp_lexer_consume_token (parser->lexer);
15214   /* The `>' token might be a greater-than operator again now.  */
15215   parser->greater_than_is_operator_p
15216     = saved_greater_than_is_operator_p;
15217   /* Restore the SAVED_SCOPE.  */
15218   parser->scope = saved_scope;
15219   parser->qualifying_scope = saved_qualifying_scope;
15220   parser->object_scope = saved_object_scope;
15221
15222   return arguments;
15223 }
15224
15225 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15226    arguments, or the body of the function have not yet been parsed,
15227    parse them now.  */
15228
15229 static void
15230 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15231 {
15232   /* If this member is a template, get the underlying
15233      FUNCTION_DECL.  */
15234   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15235     member_function = DECL_TEMPLATE_RESULT (member_function);
15236
15237   /* There should not be any class definitions in progress at this
15238      point; the bodies of members are only parsed outside of all class
15239      definitions.  */
15240   gcc_assert (parser->num_classes_being_defined == 0);
15241   /* While we're parsing the member functions we might encounter more
15242      classes.  We want to handle them right away, but we don't want
15243      them getting mixed up with functions that are currently in the
15244      queue.  */
15245   parser->unparsed_functions_queues
15246     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15247
15248   /* Make sure that any template parameters are in scope.  */
15249   maybe_begin_member_template_processing (member_function);
15250
15251   /* If the body of the function has not yet been parsed, parse it
15252      now.  */
15253   if (DECL_PENDING_INLINE_P (member_function))
15254     {
15255       tree function_scope;
15256       cp_token_cache *tokens;
15257
15258       /* The function is no longer pending; we are processing it.  */
15259       tokens = DECL_PENDING_INLINE_INFO (member_function);
15260       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15261       DECL_PENDING_INLINE_P (member_function) = 0;
15262       /* If this was an inline function in a local class, enter the scope
15263          of the containing function.  */
15264       function_scope = decl_function_context (member_function);
15265       if (function_scope)
15266         push_function_context_to (function_scope);
15267
15268       /* Push the body of the function onto the lexer stack.  */
15269       cp_parser_push_lexer_for_tokens (parser, tokens);
15270
15271       /* Let the front end know that we going to be defining this
15272          function.  */
15273       start_preparsed_function (member_function, NULL_TREE,
15274                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15275
15276       /* Now, parse the body of the function.  */
15277       cp_parser_function_definition_after_declarator (parser,
15278                                                       /*inline_p=*/true);
15279
15280       /* Leave the scope of the containing function.  */
15281       if (function_scope)
15282         pop_function_context_from (function_scope);
15283       cp_parser_pop_lexer (parser);
15284     }
15285
15286   /* Remove any template parameters from the symbol table.  */
15287   maybe_end_member_template_processing ();
15288
15289   /* Restore the queue.  */
15290   parser->unparsed_functions_queues
15291     = TREE_CHAIN (parser->unparsed_functions_queues);
15292 }
15293
15294 /* If DECL contains any default args, remember it on the unparsed
15295    functions queue.  */
15296
15297 static void
15298 cp_parser_save_default_args (cp_parser* parser, tree decl)
15299 {
15300   tree probe;
15301
15302   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15303        probe;
15304        probe = TREE_CHAIN (probe))
15305     if (TREE_PURPOSE (probe))
15306       {
15307         TREE_PURPOSE (parser->unparsed_functions_queues)
15308           = tree_cons (current_class_type, decl,
15309                        TREE_PURPOSE (parser->unparsed_functions_queues));
15310         break;
15311       }
15312   return;
15313 }
15314
15315 /* FN is a FUNCTION_DECL which may contains a parameter with an
15316    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15317    assumes that the current scope is the scope in which the default
15318    argument should be processed.  */
15319
15320 static void
15321 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15322 {
15323   bool saved_local_variables_forbidden_p;
15324   tree parm;
15325
15326   /* While we're parsing the default args, we might (due to the
15327      statement expression extension) encounter more classes.  We want
15328      to handle them right away, but we don't want them getting mixed
15329      up with default args that are currently in the queue.  */
15330   parser->unparsed_functions_queues
15331     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15332
15333   /* Local variable names (and the `this' keyword) may not appear
15334      in a default argument.  */
15335   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15336   parser->local_variables_forbidden_p = true;
15337
15338   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15339        parm;
15340        parm = TREE_CHAIN (parm))
15341     {
15342       cp_token_cache *tokens;
15343
15344       if (!TREE_PURPOSE (parm)
15345           || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15346         continue;
15347
15348        /* Push the saved tokens for the default argument onto the parser's
15349           lexer stack.  */
15350       tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15351       cp_parser_push_lexer_for_tokens (parser, tokens);
15352
15353       /* Parse the assignment-expression.  */
15354       TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser,
15355                                                              /*cast_p=*/false);
15356
15357       /* If the token stream has not been completely used up, then
15358          there was extra junk after the end of the default
15359          argument.  */
15360       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15361         cp_parser_error (parser, "expected %<,%>");
15362
15363       /* Revert to the main lexer.  */
15364       cp_parser_pop_lexer (parser);
15365     }
15366
15367   /* Restore the state of local_variables_forbidden_p.  */
15368   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15369
15370   /* Restore the queue.  */
15371   parser->unparsed_functions_queues
15372     = TREE_CHAIN (parser->unparsed_functions_queues);
15373 }
15374
15375 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15376    either a TYPE or an expression, depending on the form of the
15377    input.  The KEYWORD indicates which kind of expression we have
15378    encountered.  */
15379
15380 static tree
15381 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15382 {
15383   static const char *format;
15384   tree expr = NULL_TREE;
15385   const char *saved_message;
15386   bool saved_integral_constant_expression_p;
15387   bool saved_non_integral_constant_expression_p;
15388
15389   /* Initialize FORMAT the first time we get here.  */
15390   if (!format)
15391     format = "types may not be defined in '%s' expressions";
15392
15393   /* Types cannot be defined in a `sizeof' expression.  Save away the
15394      old message.  */
15395   saved_message = parser->type_definition_forbidden_message;
15396   /* And create the new one.  */
15397   parser->type_definition_forbidden_message
15398     = xmalloc (strlen (format)
15399                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15400                + 1 /* `\0' */);
15401   sprintf ((char *) parser->type_definition_forbidden_message,
15402            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15403
15404   /* The restrictions on constant-expressions do not apply inside
15405      sizeof expressions.  */
15406   saved_integral_constant_expression_p 
15407     = parser->integral_constant_expression_p;
15408   saved_non_integral_constant_expression_p
15409     = parser->non_integral_constant_expression_p;
15410   parser->integral_constant_expression_p = false;
15411
15412   /* Do not actually evaluate the expression.  */
15413   ++skip_evaluation;
15414   /* If it's a `(', then we might be looking at the type-id
15415      construction.  */
15416   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15417     {
15418       tree type;
15419       bool saved_in_type_id_in_expr_p;
15420
15421       /* We can't be sure yet whether we're looking at a type-id or an
15422          expression.  */
15423       cp_parser_parse_tentatively (parser);
15424       /* Consume the `('.  */
15425       cp_lexer_consume_token (parser->lexer);
15426       /* Parse the type-id.  */
15427       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15428       parser->in_type_id_in_expr_p = true;
15429       type = cp_parser_type_id (parser);
15430       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15431       /* Now, look for the trailing `)'.  */
15432       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15433       /* If all went well, then we're done.  */
15434       if (cp_parser_parse_definitely (parser))
15435         {
15436           cp_decl_specifier_seq decl_specs;
15437
15438           /* Build a trivial decl-specifier-seq.  */
15439           clear_decl_specs (&decl_specs);
15440           decl_specs.type = type;
15441
15442           /* Call grokdeclarator to figure out what type this is.  */
15443           expr = grokdeclarator (NULL,
15444                                  &decl_specs,
15445                                  TYPENAME,
15446                                  /*initialized=*/0,
15447                                  /*attrlist=*/NULL);
15448         }
15449     }
15450
15451   /* If the type-id production did not work out, then we must be
15452      looking at the unary-expression production.  */
15453   if (!expr)
15454     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15455                                        /*cast_p=*/false);
15456   /* Go back to evaluating expressions.  */
15457   --skip_evaluation;
15458
15459   /* Free the message we created.  */
15460   free ((char *) parser->type_definition_forbidden_message);
15461   /* And restore the old one.  */
15462   parser->type_definition_forbidden_message = saved_message;
15463   parser->integral_constant_expression_p 
15464     = saved_integral_constant_expression_p;
15465   parser->non_integral_constant_expression_p
15466     = saved_non_integral_constant_expression_p;
15467
15468   return expr;
15469 }
15470
15471 /* If the current declaration has no declarator, return true.  */
15472
15473 static bool
15474 cp_parser_declares_only_class_p (cp_parser *parser)
15475 {
15476   /* If the next token is a `;' or a `,' then there is no
15477      declarator.  */
15478   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15479           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15480 }
15481
15482 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15483
15484 static void
15485 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15486                              cp_storage_class storage_class)
15487 {
15488   if (decl_specs->storage_class != sc_none)
15489     decl_specs->multiple_storage_classes_p = true;
15490   else
15491     decl_specs->storage_class = storage_class;
15492 }
15493
15494 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15495    is true, the type is a user-defined type; otherwise it is a
15496    built-in type specified by a keyword.  */
15497
15498 static void
15499 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15500                               tree type_spec,
15501                               bool user_defined_p)
15502 {
15503   decl_specs->any_specifiers_p = true;
15504
15505   /* If the user tries to redeclare bool or wchar_t (with, for
15506      example, in "typedef int wchar_t;") we remember that this is what
15507      happened.  In system headers, we ignore these declarations so
15508      that G++ can work with system headers that are not C++-safe.  */
15509   if (decl_specs->specs[(int) ds_typedef]
15510       && !user_defined_p
15511       && (type_spec == boolean_type_node
15512           || type_spec == wchar_type_node)
15513       && (decl_specs->type
15514           || decl_specs->specs[(int) ds_long]
15515           || decl_specs->specs[(int) ds_short]
15516           || decl_specs->specs[(int) ds_unsigned]
15517           || decl_specs->specs[(int) ds_signed]))
15518     {
15519       decl_specs->redefined_builtin_type = type_spec;
15520       if (!decl_specs->type)
15521         {
15522           decl_specs->type = type_spec;
15523           decl_specs->user_defined_type_p = false;
15524         }
15525     }
15526   else if (decl_specs->type)
15527     decl_specs->multiple_types_p = true;
15528   else
15529     {
15530       decl_specs->type = type_spec;
15531       decl_specs->user_defined_type_p = user_defined_p;
15532       decl_specs->redefined_builtin_type = NULL_TREE;
15533     }
15534 }
15535
15536 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15537    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15538
15539 static bool
15540 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15541 {
15542   return decl_specifiers->specs[(int) ds_friend] != 0;
15543 }
15544
15545 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15546    issue an error message indicating that TOKEN_DESC was expected.
15547
15548    Returns the token consumed, if the token had the appropriate type.
15549    Otherwise, returns NULL.  */
15550
15551 static cp_token *
15552 cp_parser_require (cp_parser* parser,
15553                    enum cpp_ttype type,
15554                    const char* token_desc)
15555 {
15556   if (cp_lexer_next_token_is (parser->lexer, type))
15557     return cp_lexer_consume_token (parser->lexer);
15558   else
15559     {
15560       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15561       if (!cp_parser_simulate_error (parser))
15562         {
15563           char *message = concat ("expected ", token_desc, NULL);
15564           cp_parser_error (parser, message);
15565           free (message);
15566         }
15567       return NULL;
15568     }
15569 }
15570
15571 /* Like cp_parser_require, except that tokens will be skipped until
15572    the desired token is found.  An error message is still produced if
15573    the next token is not as expected.  */
15574
15575 static void
15576 cp_parser_skip_until_found (cp_parser* parser,
15577                             enum cpp_ttype type,
15578                             const char* token_desc)
15579 {
15580   cp_token *token;
15581   unsigned nesting_depth = 0;
15582
15583   if (cp_parser_require (parser, type, token_desc))
15584     return;
15585
15586   /* Skip tokens until the desired token is found.  */
15587   while (true)
15588     {
15589       /* Peek at the next token.  */
15590       token = cp_lexer_peek_token (parser->lexer);
15591       /* If we've reached the token we want, consume it and
15592          stop.  */
15593       if (token->type == type && !nesting_depth)
15594         {
15595           cp_lexer_consume_token (parser->lexer);
15596           return;
15597         }
15598       /* If we've run out of tokens, stop.  */
15599       if (token->type == CPP_EOF)
15600         return;
15601       if (token->type == CPP_OPEN_BRACE
15602           || token->type == CPP_OPEN_PAREN
15603           || token->type == CPP_OPEN_SQUARE)
15604         ++nesting_depth;
15605       else if (token->type == CPP_CLOSE_BRACE
15606                || token->type == CPP_CLOSE_PAREN
15607                || token->type == CPP_CLOSE_SQUARE)
15608         {
15609           if (nesting_depth-- == 0)
15610             return;
15611         }
15612       /* Consume this token.  */
15613       cp_lexer_consume_token (parser->lexer);
15614     }
15615 }
15616
15617 /* If the next token is the indicated keyword, consume it.  Otherwise,
15618    issue an error message indicating that TOKEN_DESC was expected.
15619
15620    Returns the token consumed, if the token had the appropriate type.
15621    Otherwise, returns NULL.  */
15622
15623 static cp_token *
15624 cp_parser_require_keyword (cp_parser* parser,
15625                            enum rid keyword,
15626                            const char* token_desc)
15627 {
15628   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15629
15630   if (token && token->keyword != keyword)
15631     {
15632       dyn_string_t error_msg;
15633
15634       /* Format the error message.  */
15635       error_msg = dyn_string_new (0);
15636       dyn_string_append_cstr (error_msg, "expected ");
15637       dyn_string_append_cstr (error_msg, token_desc);
15638       cp_parser_error (parser, error_msg->s);
15639       dyn_string_delete (error_msg);
15640       return NULL;
15641     }
15642
15643   return token;
15644 }
15645
15646 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15647    function-definition.  */
15648
15649 static bool
15650 cp_parser_token_starts_function_definition_p (cp_token* token)
15651 {
15652   return (/* An ordinary function-body begins with an `{'.  */
15653           token->type == CPP_OPEN_BRACE
15654           /* A ctor-initializer begins with a `:'.  */
15655           || token->type == CPP_COLON
15656           /* A function-try-block begins with `try'.  */
15657           || token->keyword == RID_TRY
15658           /* The named return value extension begins with `return'.  */
15659           || token->keyword == RID_RETURN);
15660 }
15661
15662 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15663    definition.  */
15664
15665 static bool
15666 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15667 {
15668   cp_token *token;
15669
15670   token = cp_lexer_peek_token (parser->lexer);
15671   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15672 }
15673
15674 /* Returns TRUE iff the next token is the "," or ">" ending a
15675    template-argument.  */
15676
15677 static bool
15678 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15679 {
15680   cp_token *token;
15681
15682   token = cp_lexer_peek_token (parser->lexer);
15683   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15684 }
15685
15686 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15687    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15688
15689 static bool
15690 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15691                                                      size_t n)
15692 {
15693   cp_token *token;
15694
15695   token = cp_lexer_peek_nth_token (parser->lexer, n);
15696   if (token->type == CPP_LESS)
15697     return true;
15698   /* Check for the sequence `<::' in the original code. It would be lexed as
15699      `[:', where `[' is a digraph, and there is no whitespace before
15700      `:'.  */
15701   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15702     {
15703       cp_token *token2;
15704       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15705       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15706         return true;
15707     }
15708   return false;
15709 }
15710
15711 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15712    or none_type otherwise.  */
15713
15714 static enum tag_types
15715 cp_parser_token_is_class_key (cp_token* token)
15716 {
15717   switch (token->keyword)
15718     {
15719     case RID_CLASS:
15720       return class_type;
15721     case RID_STRUCT:
15722       return record_type;
15723     case RID_UNION:
15724       return union_type;
15725
15726     default:
15727       return none_type;
15728     }
15729 }
15730
15731 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15732
15733 static void
15734 cp_parser_check_class_key (enum tag_types class_key, tree type)
15735 {
15736   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15737     pedwarn ("%qs tag used in naming %q#T",
15738             class_key == union_type ? "union"
15739              : class_key == record_type ? "struct" : "class",
15740              type);
15741 }
15742
15743 /* Issue an error message if DECL is redeclared with different
15744    access than its original declaration [class.access.spec/3].
15745    This applies to nested classes and nested class templates.
15746    [class.mem/1].  */
15747
15748 static void
15749 cp_parser_check_access_in_redeclaration (tree decl)
15750 {
15751   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15752     return;
15753
15754   if ((TREE_PRIVATE (decl)
15755        != (current_access_specifier == access_private_node))
15756       || (TREE_PROTECTED (decl)
15757           != (current_access_specifier == access_protected_node)))
15758     error ("%qD redeclared with different access", decl);
15759 }
15760
15761 /* Look for the `template' keyword, as a syntactic disambiguator.
15762    Return TRUE iff it is present, in which case it will be
15763    consumed.  */
15764
15765 static bool
15766 cp_parser_optional_template_keyword (cp_parser *parser)
15767 {
15768   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15769     {
15770       /* The `template' keyword can only be used within templates;
15771          outside templates the parser can always figure out what is a
15772          template and what is not.  */
15773       if (!processing_template_decl)
15774         {
15775           error ("%<template%> (as a disambiguator) is only allowed "
15776                  "within templates");
15777           /* If this part of the token stream is rescanned, the same
15778              error message would be generated.  So, we purge the token
15779              from the stream.  */
15780           cp_lexer_purge_token (parser->lexer);
15781           return false;
15782         }
15783       else
15784         {
15785           /* Consume the `template' keyword.  */
15786           cp_lexer_consume_token (parser->lexer);
15787           return true;
15788         }
15789     }
15790
15791   return false;
15792 }
15793
15794 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15795    set PARSER->SCOPE, and perform other related actions.  */
15796
15797 static void
15798 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15799 {
15800   tree value;
15801   tree check;
15802
15803   /* Get the stored value.  */
15804   value = cp_lexer_consume_token (parser->lexer)->value;
15805   /* Perform any access checks that were deferred.  */
15806   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15807     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15808   /* Set the scope from the stored value.  */
15809   parser->scope = TREE_VALUE (value);
15810   parser->qualifying_scope = TREE_TYPE (value);
15811   parser->object_scope = NULL_TREE;
15812 }
15813
15814 /* Consume tokens up through a non-nested END token.  */
15815
15816 static void
15817 cp_parser_cache_group (cp_parser *parser,
15818                        enum cpp_ttype end,
15819                        unsigned depth)
15820 {
15821   while (true)
15822     {
15823       cp_token *token;
15824
15825       /* Abort a parenthesized expression if we encounter a brace.  */
15826       if ((end == CPP_CLOSE_PAREN || depth == 0)
15827           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15828         return;
15829       /* If we've reached the end of the file, stop.  */
15830       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15831         return;
15832       /* Consume the next token.  */
15833       token = cp_lexer_consume_token (parser->lexer);
15834       /* See if it starts a new group.  */
15835       if (token->type == CPP_OPEN_BRACE)
15836         {
15837           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15838           if (depth == 0)
15839             return;
15840         }
15841       else if (token->type == CPP_OPEN_PAREN)
15842         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15843       else if (token->type == end)
15844         return;
15845     }
15846 }
15847
15848 /* Begin parsing tentatively.  We always save tokens while parsing
15849    tentatively so that if the tentative parsing fails we can restore the
15850    tokens.  */
15851
15852 static void
15853 cp_parser_parse_tentatively (cp_parser* parser)
15854 {
15855   /* Enter a new parsing context.  */
15856   parser->context = cp_parser_context_new (parser->context);
15857   /* Begin saving tokens.  */
15858   cp_lexer_save_tokens (parser->lexer);
15859   /* In order to avoid repetitive access control error messages,
15860      access checks are queued up until we are no longer parsing
15861      tentatively.  */
15862   push_deferring_access_checks (dk_deferred);
15863 }
15864
15865 /* Commit to the currently active tentative parse.  */
15866
15867 static void
15868 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15869 {
15870   cp_parser_context *context;
15871   cp_lexer *lexer;
15872
15873   /* Mark all of the levels as committed.  */
15874   lexer = parser->lexer;
15875   for (context = parser->context; context->next; context = context->next)
15876     {
15877       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15878         break;
15879       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15880       while (!cp_lexer_saving_tokens (lexer))
15881         lexer = lexer->next;
15882       cp_lexer_commit_tokens (lexer);
15883     }
15884 }
15885
15886 /* Abort the currently active tentative parse.  All consumed tokens
15887    will be rolled back, and no diagnostics will be issued.  */
15888
15889 static void
15890 cp_parser_abort_tentative_parse (cp_parser* parser)
15891 {
15892   cp_parser_simulate_error (parser);
15893   /* Now, pretend that we want to see if the construct was
15894      successfully parsed.  */
15895   cp_parser_parse_definitely (parser);
15896 }
15897
15898 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15899    token stream.  Otherwise, commit to the tokens we have consumed.
15900    Returns true if no error occurred; false otherwise.  */
15901
15902 static bool
15903 cp_parser_parse_definitely (cp_parser* parser)
15904 {
15905   bool error_occurred;
15906   cp_parser_context *context;
15907
15908   /* Remember whether or not an error occurred, since we are about to
15909      destroy that information.  */
15910   error_occurred = cp_parser_error_occurred (parser);
15911   /* Remove the topmost context from the stack.  */
15912   context = parser->context;
15913   parser->context = context->next;
15914   /* If no parse errors occurred, commit to the tentative parse.  */
15915   if (!error_occurred)
15916     {
15917       /* Commit to the tokens read tentatively, unless that was
15918          already done.  */
15919       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15920         cp_lexer_commit_tokens (parser->lexer);
15921
15922       pop_to_parent_deferring_access_checks ();
15923     }
15924   /* Otherwise, if errors occurred, roll back our state so that things
15925      are just as they were before we began the tentative parse.  */
15926   else
15927     {
15928       cp_lexer_rollback_tokens (parser->lexer);
15929       pop_deferring_access_checks ();
15930     }
15931   /* Add the context to the front of the free list.  */
15932   context->next = cp_parser_context_free_list;
15933   cp_parser_context_free_list = context;
15934
15935   return !error_occurred;
15936 }
15937
15938 /* Returns true if we are parsing tentatively and are not committed to
15939    this tentative parse.  */
15940
15941 static bool
15942 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
15943 {
15944   return (cp_parser_parsing_tentatively (parser)
15945           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
15946 }
15947
15948 /* Returns nonzero iff an error has occurred during the most recent
15949    tentative parse.  */
15950
15951 static bool
15952 cp_parser_error_occurred (cp_parser* parser)
15953 {
15954   return (cp_parser_parsing_tentatively (parser)
15955           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15956 }
15957
15958 /* Returns nonzero if GNU extensions are allowed.  */
15959
15960 static bool
15961 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15962 {
15963   return parser->allow_gnu_extensions_p;
15964 }
15965
15966 \f
15967 /* The parser.  */
15968
15969 static GTY (()) cp_parser *the_parser;
15970
15971 /* External interface.  */
15972
15973 /* Parse one entire translation unit.  */
15974
15975 void
15976 c_parse_file (void)
15977 {
15978   bool error_occurred;
15979   static bool already_called = false;
15980
15981   if (already_called)
15982     {
15983       sorry ("inter-module optimizations not implemented for C++");
15984       return;
15985     }
15986   already_called = true;
15987
15988   the_parser = cp_parser_new ();
15989   push_deferring_access_checks (flag_access_control
15990                                 ? dk_no_deferred : dk_no_check);
15991   error_occurred = cp_parser_translation_unit (the_parser);
15992   the_parser = NULL;
15993 }
15994
15995 /* This variable must be provided by every front end.  */
15996
15997 int yydebug;
15998
15999 #include "gt-cp-parser.h"