OSDN Git Service

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