Import of virgin gcc 4.0.0 distribution.
[dragonfly.git] / contrib / gcc-4.0 / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING.  If not, write to the Free
20    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39
40 \f
41 /* The lexer.  */
42
43 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
44    and c-lex.c) and the C++ parser.  */
45
46 /* A C++ token.  */
47
48 typedef struct cp_token GTY (())
49 {
50   /* The kind of token.  */
51   ENUM_BITFIELD (cpp_ttype) type : 8;
52   /* If this token is a keyword, this value indicates which keyword.
53      Otherwise, this value is RID_MAX.  */
54   ENUM_BITFIELD (rid) keyword : 8;
55   /* Token flags.  */
56   unsigned char flags;
57   /* True if this token is from a system header.  */
58   BOOL_BITFIELD in_system_header : 1;
59   /* True if this token is from a context where it is implicitly extern "C" */
60   BOOL_BITFIELD implicit_extern_c : 1;
61   /* The value associated with this token, if any.  */
62   tree value;
63   /* The location at which this token was found.  */
64   location_t location;
65 } cp_token;
66
67 /* We use a stack of token pointer for saving token sets.  */
68 typedef struct cp_token *cp_token_position;
69 DEF_VEC_MALLOC_P (cp_token_position);
70
71 static const cp_token eof_token =
72 {
73   CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
74 #if USE_MAPPED_LOCATION
75   0
76 #else
77   {0, 0}
78 #endif
79 };
80
81 /* The cp_lexer structure represents the C++ lexer.  It is responsible
82    for managing the token stream from the preprocessor and supplying
83    it to the parser.  Tokens are never added to the cp_lexer after
84    it is created.  */
85
86 typedef struct cp_lexer GTY (())
87 {
88   /* The memory allocated for the buffer.  NULL if this lexer does not
89      own the token buffer.  */
90   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
91   /* If the lexer owns the buffer, this is the number of tokens in the
92      buffer.  */
93   size_t buffer_length;
94   
95   /* A pointer just past the last available token.  The tokens
96      in this lexer are [buffer, last_token).  */
97   cp_token_position GTY ((skip)) last_token;
98
99   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
100      no more available tokens.  */
101   cp_token_position GTY ((skip)) next_token;
102
103   /* A stack indicating positions at which cp_lexer_save_tokens was
104      called.  The top entry is the most recent position at which we
105      began saving tokens.  If the stack is non-empty, we are saving
106      tokens.  */
107   VEC (cp_token_position) *GTY ((skip)) saved_tokens;
108
109   /* True if we should output debugging information.  */
110   bool debugging_p;
111
112   /* The next lexer in a linked list of lexers.  */
113   struct cp_lexer *next;
114 } cp_lexer;
115
116 /* cp_token_cache is a range of tokens.  There is no need to represent
117    allocate heap memory for it, since tokens are never removed from the
118    lexer's array.  There is also no need for the GC to walk through
119    a cp_token_cache, since everything in here is referenced through
120    a lexer.  */
121
122 typedef struct cp_token_cache GTY(())
123 {
124   /* The beginning of the token range.  */
125   cp_token * GTY((skip)) first;
126
127   /* Points immediately after the last token in the range.  */
128   cp_token * GTY ((skip)) last;
129 } cp_token_cache;
130
131 /* Prototypes.  */
132
133 static cp_lexer *cp_lexer_new_main
134   (void);
135 static cp_lexer *cp_lexer_new_from_tokens
136   (cp_token_cache *tokens);
137 static void cp_lexer_destroy
138   (cp_lexer *);
139 static int cp_lexer_saving_tokens
140   (const cp_lexer *);
141 static cp_token_position cp_lexer_token_position
142   (cp_lexer *, bool);
143 static cp_token *cp_lexer_token_at
144   (cp_lexer *, cp_token_position);
145 static void cp_lexer_get_preprocessor_token
146   (cp_lexer *, cp_token *);
147 static inline cp_token *cp_lexer_peek_token
148   (cp_lexer *);
149 static cp_token *cp_lexer_peek_nth_token
150   (cp_lexer *, size_t);
151 static inline bool cp_lexer_next_token_is
152   (cp_lexer *, enum cpp_ttype);
153 static bool cp_lexer_next_token_is_not
154   (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_keyword
156   (cp_lexer *, enum rid);
157 static cp_token *cp_lexer_consume_token
158   (cp_lexer *);
159 static void cp_lexer_purge_token
160   (cp_lexer *);
161 static void cp_lexer_purge_tokens_after
162   (cp_lexer *, cp_token_position);
163 static void cp_lexer_handle_pragma
164   (cp_lexer *);
165 static void cp_lexer_save_tokens
166   (cp_lexer *);
167 static void cp_lexer_commit_tokens
168   (cp_lexer *);
169 static void cp_lexer_rollback_tokens
170   (cp_lexer *);
171 #ifdef ENABLE_CHECKING
172 static void cp_lexer_print_token
173   (FILE *, cp_token *);
174 static inline bool cp_lexer_debugging_p
175   (cp_lexer *);
176 static void cp_lexer_start_debugging
177   (cp_lexer *) ATTRIBUTE_UNUSED;
178 static void cp_lexer_stop_debugging
179   (cp_lexer *) ATTRIBUTE_UNUSED;
180 #else
181 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
182    about passing NULL to functions that require non-NULL arguments
183    (fputs, fprintf).  It will never be used, so all we need is a value
184    of the right type that's guaranteed not to be NULL.  */
185 #define cp_lexer_debug_stream stdout
186 #define cp_lexer_print_token(str, tok) (void) 0
187 #define cp_lexer_debugging_p(lexer) 0
188 #endif /* ENABLE_CHECKING */
189
190 static cp_token_cache *cp_token_cache_new
191   (cp_token *, cp_token *);
192
193 /* Manifest constants.  */
194 #define CP_LEXER_BUFFER_SIZE 10000
195 #define CP_SAVED_TOKEN_STACK 5
196
197 /* A token type for keywords, as opposed to ordinary identifiers.  */
198 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
199
200 /* A token type for template-ids.  If a template-id is processed while
201    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
202    the value of the CPP_TEMPLATE_ID is whatever was returned by
203    cp_parser_template_id.  */
204 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
205
206 /* A token type for nested-name-specifiers.  If a
207    nested-name-specifier is processed while parsing tentatively, it is
208    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
209    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
210    cp_parser_nested_name_specifier_opt.  */
211 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
212
213 /* A token type for tokens that are not tokens at all; these are used
214    to represent slots in the array where there used to be a token
215    that has now been deleted.  */
216 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
217
218 /* The number of token types, including C++-specific ones.  */
219 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
220
221 /* Variables.  */
222
223 #ifdef ENABLE_CHECKING
224 /* The stream to which debugging output should be written.  */
225 static FILE *cp_lexer_debug_stream;
226 #endif /* ENABLE_CHECKING */
227
228 /* Create a new main C++ lexer, the lexer that gets tokens from the
229    preprocessor.  */
230
231 static cp_lexer *
232 cp_lexer_new_main (void)
233 {
234   cp_token first_token;
235   cp_lexer *lexer;
236   cp_token *pos;
237   size_t alloc;
238   size_t space;
239   cp_token *buffer;
240
241   /* It's possible that lexing the first token will load a PCH file,
242      which is a GC collection point.  So we have to grab the first
243      token before allocating any memory.  Pragmas must not be deferred
244      as -fpch-preprocess can generate a pragma to load the PCH file in
245      the preprocessed output used by -save-temps.  */
246   cp_lexer_get_preprocessor_token (NULL, &first_token);
247
248   /* Tell cpplib we want CPP_PRAGMA tokens.  */
249   cpp_get_options (parse_in)->defer_pragmas = true;
250
251   /* Tell c_lex not to merge string constants.  */
252   c_lex_return_raw_strings = true;
253
254   c_common_no_more_pch ();
255
256   /* Allocate the memory.  */
257   lexer = GGC_CNEW (cp_lexer);
258
259 #ifdef ENABLE_CHECKING  
260   /* Initially we are not debugging.  */
261   lexer->debugging_p = false;
262 #endif /* ENABLE_CHECKING */
263   lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
264          
265   /* Create the buffer.  */
266   alloc = CP_LEXER_BUFFER_SIZE;
267   buffer = ggc_alloc (alloc * sizeof (cp_token));
268
269   /* Put the first token in the buffer.  */
270   space = alloc;
271   pos = buffer;
272   *pos = first_token;
273   
274   /* Get the remaining tokens from the preprocessor.  */
275   while (pos->type != CPP_EOF)
276     {
277       pos++;
278       if (!--space)
279         {
280           space = alloc;
281           alloc *= 2;
282           buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
283           pos = buffer + space;
284         }
285       cp_lexer_get_preprocessor_token (lexer, pos);
286     }
287   lexer->buffer = buffer;
288   lexer->buffer_length = alloc - space;
289   lexer->last_token = pos;
290   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
291
292   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
293      direct calls to c_lex.  Those callers all expect c_lex to do
294      string constant concatenation.  */
295   c_lex_return_raw_strings = false;
296
297   gcc_assert (lexer->next_token->type != CPP_PURGED);
298   return lexer;
299 }
300
301 /* Create a new lexer whose token stream is primed with the tokens in
302    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
303
304 static cp_lexer *
305 cp_lexer_new_from_tokens (cp_token_cache *cache)
306 {
307   cp_token *first = cache->first;
308   cp_token *last = cache->last;
309   cp_lexer *lexer = GGC_CNEW (cp_lexer);
310
311   /* We do not own the buffer.  */
312   lexer->buffer = NULL;
313   lexer->buffer_length = 0;
314   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
315   lexer->last_token = last;
316   
317   lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
318
319 #ifdef ENABLE_CHECKING
320   /* Initially we are not debugging.  */
321   lexer->debugging_p = false;
322 #endif
323
324   gcc_assert (lexer->next_token->type != CPP_PURGED);
325   return lexer;
326 }
327
328 /* Frees all resources associated with LEXER.  */
329
330 static void
331 cp_lexer_destroy (cp_lexer *lexer)
332 {
333   if (lexer->buffer)
334     ggc_free (lexer->buffer);
335   VEC_free (cp_token_position, lexer->saved_tokens);
336   ggc_free (lexer);
337 }
338
339 /* Returns nonzero if debugging information should be output.  */
340
341 #ifdef ENABLE_CHECKING
342
343 static inline bool
344 cp_lexer_debugging_p (cp_lexer *lexer)
345 {
346   return lexer->debugging_p;
347 }
348
349 #endif /* ENABLE_CHECKING */
350
351 static inline cp_token_position
352 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
353 {
354   gcc_assert (!previous_p || lexer->next_token != &eof_token);
355   
356   return lexer->next_token - previous_p;
357 }
358
359 static inline cp_token *
360 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
361 {
362   return pos;
363 }
364
365 /* nonzero if we are presently saving tokens.  */
366
367 static inline int
368 cp_lexer_saving_tokens (const cp_lexer* lexer)
369 {
370   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
371 }
372
373 /* Store the next token from the preprocessor in *TOKEN.  Return true
374    if we reach EOF.  */
375
376 static void
377 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
378                                  cp_token *token)
379 {
380   static int is_extern_c = 0;
381
382    /* Get a new token from the preprocessor.  */
383   token->type = c_lex_with_flags (&token->value, &token->flags);
384   token->location = input_location;
385   token->in_system_header = in_system_header;
386
387   /* On some systems, some header files are surrounded by an 
388      implicit extern "C" block.  Set a flag in the token if it
389      comes from such a header.  */
390   is_extern_c += pending_lang_change;
391   pending_lang_change = 0;
392   token->implicit_extern_c = is_extern_c > 0;
393
394   /* Check to see if this token is a keyword.  */
395   if (token->type == CPP_NAME
396       && C_IS_RESERVED_WORD (token->value))
397     {
398       /* Mark this token as a keyword.  */
399       token->type = CPP_KEYWORD;
400       /* Record which keyword.  */
401       token->keyword = C_RID_CODE (token->value);
402       /* Update the value.  Some keywords are mapped to particular
403          entities, rather than simply having the value of the
404          corresponding IDENTIFIER_NODE.  For example, `__const' is
405          mapped to `const'.  */
406       token->value = ridpointers[token->keyword];
407     }
408   else
409     token->keyword = RID_MAX;
410 }
411
412 /* Update the globals input_location and in_system_header from TOKEN.  */
413 static inline void
414 cp_lexer_set_source_position_from_token (cp_token *token)
415 {
416   if (token->type != CPP_EOF)
417     {
418       input_location = token->location;
419       in_system_header = token->in_system_header;
420     }
421 }
422
423 /* Return a pointer to the next token in the token stream, but do not
424    consume it.  */
425
426 static inline cp_token *
427 cp_lexer_peek_token (cp_lexer *lexer)
428 {
429   if (cp_lexer_debugging_p (lexer))
430     {
431       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
432       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
433       putc ('\n', cp_lexer_debug_stream);
434     }
435   return lexer->next_token;
436 }
437
438 /* Return true if the next token has the indicated TYPE.  */
439
440 static inline bool
441 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
442 {
443   return cp_lexer_peek_token (lexer)->type == type;
444 }
445
446 /* Return true if the next token does not have the indicated TYPE.  */
447
448 static inline bool
449 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
450 {
451   return !cp_lexer_next_token_is (lexer, type);
452 }
453
454 /* Return true if the next token is the indicated KEYWORD.  */
455
456 static inline bool
457 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
458 {
459   cp_token *token;
460
461   /* Peek at the next token.  */
462   token = cp_lexer_peek_token (lexer);
463   /* Check to see if it is the indicated keyword.  */
464   return token->keyword == keyword;
465 }
466
467 /* Return a pointer to the Nth token in the token stream.  If N is 1,
468    then this is precisely equivalent to cp_lexer_peek_token (except
469    that it is not inline).  One would like to disallow that case, but
470    there is one case (cp_parser_nth_token_starts_template_id) where
471    the caller passes a variable for N and it might be 1.  */
472
473 static cp_token *
474 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
475 {
476   cp_token *token;
477
478   /* N is 1-based, not zero-based.  */
479   gcc_assert (n > 0 && lexer->next_token != &eof_token);
480
481   if (cp_lexer_debugging_p (lexer))
482     fprintf (cp_lexer_debug_stream,
483              "cp_lexer: peeking ahead %ld at token: ", (long)n);
484
485   --n;
486   token = lexer->next_token;
487   while (n != 0)
488     {
489       ++token;
490       if (token == lexer->last_token)
491         {
492           token = (cp_token *)&eof_token;
493           break;
494         }
495       
496       if (token->type != CPP_PURGED)
497         --n;
498     }
499
500   if (cp_lexer_debugging_p (lexer))
501     {
502       cp_lexer_print_token (cp_lexer_debug_stream, token);
503       putc ('\n', cp_lexer_debug_stream);
504     }
505
506   return token;
507 }
508
509 /* Return the next token, and advance the lexer's next_token pointer
510    to point to the next non-purged token.  */
511
512 static cp_token *
513 cp_lexer_consume_token (cp_lexer* lexer)
514 {
515   cp_token *token = lexer->next_token;
516
517   gcc_assert (token != &eof_token);
518   
519   do
520     {
521       lexer->next_token++;
522       if (lexer->next_token == lexer->last_token)
523         {
524           lexer->next_token = (cp_token *)&eof_token;
525           break;
526         }
527       
528     }
529   while (lexer->next_token->type == CPP_PURGED);
530   
531   cp_lexer_set_source_position_from_token (token);
532   
533   /* Provide debugging output.  */
534   if (cp_lexer_debugging_p (lexer))
535     {
536       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
537       cp_lexer_print_token (cp_lexer_debug_stream, token);
538       putc ('\n', cp_lexer_debug_stream);
539     }
540   
541   return token;
542 }
543
544 /* Permanently remove the next token from the token stream, and
545    advance the next_token pointer to refer to the next non-purged
546    token.  */
547
548 static void
549 cp_lexer_purge_token (cp_lexer *lexer)
550 {
551   cp_token *tok = lexer->next_token;
552   
553   gcc_assert (tok != &eof_token);
554   tok->type = CPP_PURGED;
555   tok->location = UNKNOWN_LOCATION;
556   tok->value = NULL_TREE;
557   tok->keyword = RID_MAX;
558
559   do
560     {
561       tok++;
562       if (tok == lexer->last_token)
563         {
564           tok = (cp_token *)&eof_token;
565           break;
566         }
567     }
568   while (tok->type == CPP_PURGED);
569   lexer->next_token = tok;
570 }
571
572 /* Permanently remove all tokens after TOK, up to, but not
573    including, the token that will be returned next by
574    cp_lexer_peek_token.  */
575
576 static void
577 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
578 {
579   cp_token *peek = lexer->next_token;
580
581   if (peek == &eof_token)
582     peek = lexer->last_token;
583   
584   gcc_assert (tok < peek);
585
586   for ( tok += 1; tok != peek; tok += 1)
587     {
588       tok->type = CPP_PURGED;
589       tok->location = UNKNOWN_LOCATION;
590       tok->value = NULL_TREE;
591       tok->keyword = RID_MAX;
592     }
593 }
594
595 /* Consume and handle a pragma token.  */
596 static void
597 cp_lexer_handle_pragma (cp_lexer *lexer)
598 {
599   cpp_string s;
600   cp_token *token = cp_lexer_consume_token (lexer);
601   gcc_assert (token->type == CPP_PRAGMA);
602   gcc_assert (token->value);
603
604   s.len = TREE_STRING_LENGTH (token->value);
605   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
606
607   cpp_handle_deferred_pragma (parse_in, &s);
608
609   /* Clearing token->value here means that we will get an ICE if we
610      try to process this #pragma again (which should be impossible).  */
611   token->value = NULL;
612 }
613
614 /* Begin saving tokens.  All tokens consumed after this point will be
615    preserved.  */
616
617 static void
618 cp_lexer_save_tokens (cp_lexer* lexer)
619 {
620   /* Provide debugging output.  */
621   if (cp_lexer_debugging_p (lexer))
622     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
623
624   VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
625 }
626
627 /* Commit to the portion of the token stream most recently saved.  */
628
629 static void
630 cp_lexer_commit_tokens (cp_lexer* lexer)
631 {
632   /* Provide debugging output.  */
633   if (cp_lexer_debugging_p (lexer))
634     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
635
636   VEC_pop (cp_token_position, lexer->saved_tokens);
637 }
638
639 /* Return all tokens saved since the last call to cp_lexer_save_tokens
640    to the token stream.  Stop saving tokens.  */
641
642 static void
643 cp_lexer_rollback_tokens (cp_lexer* lexer)
644 {
645   /* Provide debugging output.  */
646   if (cp_lexer_debugging_p (lexer))
647     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
648
649   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
650 }
651
652 /* Print a representation of the TOKEN on the STREAM.  */
653
654 #ifdef ENABLE_CHECKING
655
656 static void
657 cp_lexer_print_token (FILE * stream, cp_token *token)
658 {
659   /* We don't use cpp_type2name here because the parser defines
660      a few tokens of its own.  */
661   static const char *const token_names[] = {
662     /* cpplib-defined token types */
663 #define OP(e, s) #e,
664 #define TK(e, s) #e,
665     TTYPE_TABLE
666 #undef OP
667 #undef TK
668     /* C++ parser token types - see "Manifest constants", above.  */
669     "KEYWORD",
670     "TEMPLATE_ID",
671     "NESTED_NAME_SPECIFIER",
672     "PURGED"
673   };
674   
675   /* If we have a name for the token, print it out.  Otherwise, we
676      simply give the numeric code.  */
677   gcc_assert (token->type < ARRAY_SIZE(token_names));
678   fputs (token_names[token->type], stream);
679
680   /* For some tokens, print the associated data.  */
681   switch (token->type)
682     {
683     case CPP_KEYWORD:
684       /* Some keywords have a value that is not an IDENTIFIER_NODE.
685          For example, `struct' is mapped to an INTEGER_CST.  */
686       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
687         break;
688       /* else fall through */
689     case CPP_NAME:
690       fputs (IDENTIFIER_POINTER (token->value), stream);
691       break;
692
693     case CPP_STRING:
694     case CPP_WSTRING:
695     case CPP_PRAGMA:
696       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
697       break;
698
699     default:
700       break;
701     }
702 }
703
704 /* Start emitting debugging information.  */
705
706 static void
707 cp_lexer_start_debugging (cp_lexer* lexer)
708 {
709   lexer->debugging_p = 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 *, bool, 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 /* A minimum or maximum operator has been seen.  As these are
1791    deprecated, issue a warning.  */
1792
1793 static inline void
1794 cp_parser_warn_min_max (void)
1795 {
1796   if (warn_deprecated && !in_system_header)
1797     warning ("minimum/maximum operators are deprecated");
1798 }
1799
1800 /* If not parsing tentatively, issue a diagnostic of the form
1801       FILE:LINE: MESSAGE before TOKEN
1802    where TOKEN is the next token in the input stream.  MESSAGE
1803    (specified by the caller) is usually of the form "expected
1804    OTHER-TOKEN".  */
1805
1806 static void
1807 cp_parser_error (cp_parser* parser, const char* message)
1808 {
1809   if (!cp_parser_simulate_error (parser))
1810     {
1811       cp_token *token = cp_lexer_peek_token (parser->lexer);
1812       /* This diagnostic makes more sense if it is tagged to the line
1813          of the token we just peeked at.  */
1814       cp_lexer_set_source_position_from_token (token);
1815       if (token->type == CPP_PRAGMA)
1816         {
1817           error ("%<#pragma%> is not allowed here"); 
1818           cp_lexer_purge_token (parser->lexer);
1819           return;
1820         }
1821       c_parse_error (message,
1822                      /* Because c_parser_error does not understand
1823                         CPP_KEYWORD, keywords are treated like
1824                         identifiers.  */
1825                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1826                      token->value);
1827     }
1828 }
1829
1830 /* Issue an error about name-lookup failing.  NAME is the
1831    IDENTIFIER_NODE DECL is the result of
1832    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1833    the thing that we hoped to find.  */
1834
1835 static void
1836 cp_parser_name_lookup_error (cp_parser* parser,
1837                              tree name,
1838                              tree decl,
1839                              const char* desired)
1840 {
1841   /* If name lookup completely failed, tell the user that NAME was not
1842      declared.  */
1843   if (decl == error_mark_node)
1844     {
1845       if (parser->scope && parser->scope != global_namespace)
1846         error ("%<%D::%D%> has not been declared",
1847                parser->scope, name);
1848       else if (parser->scope == global_namespace)
1849         error ("%<::%D%> has not been declared", name);
1850       else if (parser->object_scope 
1851                && !CLASS_TYPE_P (parser->object_scope))
1852         error ("request for member %qD in non-class type %qT",
1853                name, parser->object_scope);
1854       else if (parser->object_scope)
1855         error ("%<%T::%D%> has not been declared", 
1856                parser->object_scope, name);
1857       else
1858         error ("%qD has not been declared", name);
1859     }
1860   else if (parser->scope && parser->scope != global_namespace)
1861     error ("%<%D::%D%> %s", parser->scope, name, desired);
1862   else if (parser->scope == global_namespace)
1863     error ("%<::%D%> %s", name, desired);
1864   else
1865     error ("%qD %s", name, desired);
1866 }
1867
1868 /* If we are parsing tentatively, remember that an error has occurred
1869    during this tentative parse.  Returns true if the error was
1870    simulated; false if a message should be issued by the caller.  */
1871
1872 static bool
1873 cp_parser_simulate_error (cp_parser* parser)
1874 {
1875   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1876     {
1877       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1878       return true;
1879     }
1880   return false;
1881 }
1882
1883 /* This function is called when a type is defined.  If type
1884    definitions are forbidden at this point, an error message is
1885    issued.  */
1886
1887 static void
1888 cp_parser_check_type_definition (cp_parser* parser)
1889 {
1890   /* If types are forbidden here, issue a message.  */
1891   if (parser->type_definition_forbidden_message)
1892     /* Use `%s' to print the string in case there are any escape
1893        characters in the message.  */
1894     error ("%s", parser->type_definition_forbidden_message);
1895 }
1896
1897 /* This function is called when the DECLARATOR is processed.  The TYPE
1898    was a type defined in the decl-specifiers.  If it is invalid to
1899    define a type in the decl-specifiers for DECLARATOR, an error is
1900    issued.  */
1901
1902 static void
1903 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1904                                                tree type)
1905 {
1906   /* [dcl.fct] forbids type definitions in return types.
1907      Unfortunately, it's not easy to know whether or not we are
1908      processing a return type until after the fact.  */
1909   while (declarator
1910          && (declarator->kind == cdk_pointer
1911              || declarator->kind == cdk_reference
1912              || declarator->kind == cdk_ptrmem))
1913     declarator = declarator->declarator;
1914   if (declarator
1915       && declarator->kind == cdk_function)
1916     {
1917       error ("new types may not be defined in a return type");
1918       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1919               type);
1920     }
1921 }
1922
1923 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1924    "<" in any valid C++ program.  If the next token is indeed "<",
1925    issue a message warning the user about what appears to be an
1926    invalid attempt to form a template-id.  */
1927
1928 static void
1929 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1930                                          tree type)
1931 {
1932   cp_token_position start = 0;
1933
1934   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1935     {
1936       if (TYPE_P (type))
1937         error ("%qT is not a template", type);
1938       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1939         error ("%qE is not a template", type);
1940       else
1941         error ("invalid template-id");
1942       /* Remember the location of the invalid "<".  */
1943       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1944         start = cp_lexer_token_position (parser->lexer, true);
1945       /* Consume the "<".  */
1946       cp_lexer_consume_token (parser->lexer);
1947       /* Parse the template arguments.  */
1948       cp_parser_enclosed_template_argument_list (parser);
1949       /* Permanently remove the invalid template arguments so that
1950          this error message is not issued again.  */
1951       if (start)
1952         cp_lexer_purge_tokens_after (parser->lexer, start);
1953     }
1954 }
1955
1956 /* If parsing an integral constant-expression, issue an error message
1957    about the fact that THING appeared and return true.  Otherwise,
1958    return false.  In either case, set
1959    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */ 
1960
1961 static bool
1962 cp_parser_non_integral_constant_expression (cp_parser  *parser,
1963                                             const char *thing)
1964 {
1965   parser->non_integral_constant_expression_p = true;
1966   if (parser->integral_constant_expression_p)
1967     {
1968       if (!parser->allow_non_integral_constant_expression_p)
1969         {
1970           error ("%s cannot appear in a constant-expression", thing);
1971           return true;
1972         }
1973     }
1974   return false;
1975 }
1976
1977 /* Emit a diagnostic for an invalid type name.  SCOPE is the
1978    qualifying scope (or NULL, if none) for ID.  This function commits
1979    to the current active tentative parse, if any.  (Otherwise, the
1980    problematic construct might be encountered again later, resulting
1981    in duplicate error messages.)  */
1982
1983 static void
1984 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1985 {
1986   tree decl, old_scope;
1987   /* Try to lookup the identifier.  */
1988   old_scope = parser->scope;
1989   parser->scope = scope;
1990   decl = cp_parser_lookup_name_simple (parser, id);
1991   parser->scope = old_scope;
1992   /* If the lookup found a template-name, it means that the user forgot
1993   to specify an argument list. Emit an useful error message.  */
1994   if (TREE_CODE (decl) == TEMPLATE_DECL)
1995     error ("invalid use of template-name %qE without an argument list",
1996       decl);
1997   else if (!parser->scope)
1998     {
1999       /* Issue an error message.  */
2000       error ("%qE does not name a type", id);
2001       /* If we're in a template class, it's possible that the user was
2002          referring to a type from a base class.  For example:
2003
2004            template <typename T> struct A { typedef T X; };
2005            template <typename T> struct B : public A<T> { X x; };
2006
2007          The user should have said "typename A<T>::X".  */
2008       if (processing_template_decl && current_class_type
2009           && TYPE_BINFO (current_class_type))
2010         {
2011           tree b;
2012
2013           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2014                b;
2015                b = TREE_CHAIN (b))
2016             {
2017               tree base_type = BINFO_TYPE (b);
2018               if (CLASS_TYPE_P (base_type)
2019                   && dependent_type_p (base_type))
2020                 {
2021                   tree field;
2022                   /* Go from a particular instantiation of the
2023                      template (which will have an empty TYPE_FIELDs),
2024                      to the main version.  */
2025                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2026                   for (field = TYPE_FIELDS (base_type);
2027                        field;
2028                        field = TREE_CHAIN (field))
2029                     if (TREE_CODE (field) == TYPE_DECL
2030                         && DECL_NAME (field) == id)
2031                       {
2032                         inform ("(perhaps %<typename %T::%E%> was intended)",
2033                                 BINFO_TYPE (b), id);
2034                         break;
2035                       }
2036                   if (field)
2037                     break;
2038                 }
2039             }
2040         }
2041     }
2042   /* Here we diagnose qualified-ids where the scope is actually correct,
2043      but the identifier does not resolve to a valid type name.  */
2044   else
2045     {
2046       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2047         error ("%qE in namespace %qE does not name a type",
2048                id, parser->scope);
2049       else if (TYPE_P (parser->scope))
2050         error ("%qE in class %qT does not name a type", id, parser->scope);
2051       else
2052         gcc_unreachable ();
2053     }
2054   cp_parser_commit_to_tentative_parse (parser);
2055 }
2056
2057 /* Check for a common situation where a type-name should be present,
2058    but is not, and issue a sensible error message.  Returns true if an
2059    invalid type-name was detected.
2060
2061    The situation handled by this function are variable declarations of the
2062    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2063    Usually, `ID' should name a type, but if we got here it means that it
2064    does not. We try to emit the best possible error message depending on
2065    how exactly the id-expression looks like.
2066 */
2067
2068 static bool
2069 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2070 {
2071   tree id;
2072
2073   cp_parser_parse_tentatively (parser);
2074   id = cp_parser_id_expression (parser,
2075                                 /*template_keyword_p=*/false,
2076                                 /*check_dependency_p=*/true,
2077                                 /*template_p=*/NULL,
2078                                 /*declarator_p=*/true);
2079   /* After the id-expression, there should be a plain identifier,
2080      otherwise this is not a simple variable declaration. Also, if
2081      the scope is dependent, we cannot do much.  */
2082   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2083       || (parser->scope && TYPE_P (parser->scope)
2084           && dependent_type_p (parser->scope)))
2085     {
2086       cp_parser_abort_tentative_parse (parser);
2087       return false;
2088     }
2089   if (!cp_parser_parse_definitely (parser)
2090       || TREE_CODE (id) != IDENTIFIER_NODE)
2091     return false;
2092
2093   /* Emit a diagnostic for the invalid type.  */
2094   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2095   /* Skip to the end of the declaration; there's no point in
2096      trying to process it.  */
2097   cp_parser_skip_to_end_of_block_or_statement (parser);
2098   return true;
2099 }
2100
2101 /* Consume tokens up to, and including, the next non-nested closing `)'.
2102    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2103    are doing error recovery. Returns -1 if OR_COMMA is true and we
2104    found an unnested comma.  */
2105
2106 static int
2107 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2108                                        bool recovering,
2109                                        bool or_comma,
2110                                        bool consume_paren)
2111 {
2112   unsigned paren_depth = 0;
2113   unsigned brace_depth = 0;
2114   int result;
2115
2116   if (recovering && !or_comma
2117       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2118     return 0;
2119
2120   while (true)
2121     {
2122       cp_token *token;
2123
2124       /* If we've run out of tokens, then there is no closing `)'.  */
2125       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2126         {
2127           result = 0;
2128           break;
2129         }
2130
2131       token = cp_lexer_peek_token (parser->lexer);
2132
2133       /* This matches the processing in skip_to_end_of_statement.  */
2134       if (token->type == CPP_SEMICOLON && !brace_depth)
2135         {
2136           result = 0;
2137           break;
2138         }
2139       if (token->type == CPP_OPEN_BRACE)
2140         ++brace_depth;
2141       if (token->type == CPP_CLOSE_BRACE)
2142         {
2143           if (!brace_depth--)
2144             {
2145               result = 0;
2146               break;
2147             }
2148         }
2149       if (recovering && or_comma && token->type == CPP_COMMA
2150           && !brace_depth && !paren_depth)
2151         {
2152           result = -1;
2153           break;
2154         }
2155
2156       if (!brace_depth)
2157         {
2158           /* If it is an `(', we have entered another level of nesting.  */
2159           if (token->type == CPP_OPEN_PAREN)
2160             ++paren_depth;
2161           /* If it is a `)', then we might be done.  */
2162           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2163             {
2164               if (consume_paren)
2165                 cp_lexer_consume_token (parser->lexer);
2166               {
2167                 result = 1;
2168                 break;
2169               }
2170             }
2171         }
2172
2173       /* Consume the token.  */
2174       cp_lexer_consume_token (parser->lexer);
2175     }
2176
2177   return result;
2178 }
2179
2180 /* Consume tokens until we reach the end of the current statement.
2181    Normally, that will be just before consuming a `;'.  However, if a
2182    non-nested `}' comes first, then we stop before consuming that.  */
2183
2184 static void
2185 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2186 {
2187   unsigned nesting_depth = 0;
2188
2189   while (true)
2190     {
2191       cp_token *token;
2192
2193       /* Peek at the next token.  */
2194       token = cp_lexer_peek_token (parser->lexer);
2195       /* If we've run out of tokens, stop.  */
2196       if (token->type == CPP_EOF)
2197         break;
2198       /* If the next token is a `;', we have reached the end of the
2199          statement.  */
2200       if (token->type == CPP_SEMICOLON && !nesting_depth)
2201         break;
2202       /* If the next token is a non-nested `}', then we have reached
2203          the end of the current block.  */
2204       if (token->type == CPP_CLOSE_BRACE)
2205         {
2206           /* If this is a non-nested `}', stop before consuming it.
2207              That way, when confronted with something like:
2208
2209                { 3 + }
2210
2211              we stop before consuming the closing `}', even though we
2212              have not yet reached a `;'.  */
2213           if (nesting_depth == 0)
2214             break;
2215           /* If it is the closing `}' for a block that we have
2216              scanned, stop -- but only after consuming the token.
2217              That way given:
2218
2219                 void f g () { ... }
2220                 typedef int I;
2221
2222              we will stop after the body of the erroneously declared
2223              function, but before consuming the following `typedef'
2224              declaration.  */
2225           if (--nesting_depth == 0)
2226             {
2227               cp_lexer_consume_token (parser->lexer);
2228               break;
2229             }
2230         }
2231       /* If it the next token is a `{', then we are entering a new
2232          block.  Consume the entire block.  */
2233       else if (token->type == CPP_OPEN_BRACE)
2234         ++nesting_depth;
2235       /* Consume the token.  */
2236       cp_lexer_consume_token (parser->lexer);
2237     }
2238 }
2239
2240 /* This function is called at the end of a statement or declaration.
2241    If the next token is a semicolon, it is consumed; otherwise, error
2242    recovery is attempted.  */
2243
2244 static void
2245 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2246 {
2247   /* Look for the trailing `;'.  */
2248   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2249     {
2250       /* If there is additional (erroneous) input, skip to the end of
2251          the statement.  */
2252       cp_parser_skip_to_end_of_statement (parser);
2253       /* If the next token is now a `;', consume it.  */
2254       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2255         cp_lexer_consume_token (parser->lexer);
2256     }
2257 }
2258
2259 /* Skip tokens until we have consumed an entire block, or until we
2260    have consumed a non-nested `;'.  */
2261
2262 static void
2263 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2264 {
2265   unsigned nesting_depth = 0;
2266
2267   while (true)
2268     {
2269       cp_token *token;
2270
2271       /* Peek at the next token.  */
2272       token = cp_lexer_peek_token (parser->lexer);
2273       /* If we've run out of tokens, stop.  */
2274       if (token->type == CPP_EOF)
2275         break;
2276       /* If the next token is a `;', we have reached the end of the
2277          statement.  */
2278       if (token->type == CPP_SEMICOLON && !nesting_depth)
2279         {
2280           /* Consume the `;'.  */
2281           cp_lexer_consume_token (parser->lexer);
2282           break;
2283         }
2284       /* Consume the token.  */
2285       token = cp_lexer_consume_token (parser->lexer);
2286       /* If the next token is a non-nested `}', then we have reached
2287          the end of the current block.  */
2288       if (token->type == CPP_CLOSE_BRACE
2289           && (nesting_depth == 0 || --nesting_depth == 0))
2290         break;
2291       /* If it the next token is a `{', then we are entering a new
2292          block.  Consume the entire block.  */
2293       if (token->type == CPP_OPEN_BRACE)
2294         ++nesting_depth;
2295     }
2296 }
2297
2298 /* Skip tokens until a non-nested closing curly brace is the next
2299    token.  */
2300
2301 static void
2302 cp_parser_skip_to_closing_brace (cp_parser *parser)
2303 {
2304   unsigned nesting_depth = 0;
2305
2306   while (true)
2307     {
2308       cp_token *token;
2309
2310       /* Peek at the next token.  */
2311       token = cp_lexer_peek_token (parser->lexer);
2312       /* If we've run out of tokens, stop.  */
2313       if (token->type == CPP_EOF)
2314         break;
2315       /* If the next token is a non-nested `}', then we have reached
2316          the end of the current block.  */
2317       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2318         break;
2319       /* If it the next token is a `{', then we are entering a new
2320          block.  Consume the entire block.  */
2321       else if (token->type == CPP_OPEN_BRACE)
2322         ++nesting_depth;
2323       /* Consume the token.  */
2324       cp_lexer_consume_token (parser->lexer);
2325     }
2326 }
2327
2328 /* This is a simple wrapper around make_typename_type. When the id is
2329    an unresolved identifier node, we can provide a superior diagnostic
2330    using cp_parser_diagnose_invalid_type_name.  */
2331
2332 static tree
2333 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2334 {
2335   tree result;
2336   if (TREE_CODE (id) == IDENTIFIER_NODE)
2337     {
2338       result = make_typename_type (scope, id, typename_type,
2339                                    /*complain=*/0);
2340       if (result == error_mark_node)
2341         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2342       return result;
2343     }
2344   return make_typename_type (scope, id, typename_type, tf_error);
2345 }
2346
2347
2348 /* Create a new C++ parser.  */
2349
2350 static cp_parser *
2351 cp_parser_new (void)
2352 {
2353   cp_parser *parser;
2354   cp_lexer *lexer;
2355   unsigned i;
2356
2357   /* cp_lexer_new_main is called before calling ggc_alloc because
2358      cp_lexer_new_main might load a PCH file.  */
2359   lexer = cp_lexer_new_main ();
2360
2361   /* Initialize the binops_by_token so that we can get the tree
2362      directly from the token.  */
2363   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2364     binops_by_token[binops[i].token_type] = binops[i];
2365
2366   parser = GGC_CNEW (cp_parser);
2367   parser->lexer = lexer;
2368   parser->context = cp_parser_context_new (NULL);
2369
2370   /* For now, we always accept GNU extensions.  */
2371   parser->allow_gnu_extensions_p = 1;
2372
2373   /* The `>' token is a greater-than operator, not the end of a
2374      template-id.  */
2375   parser->greater_than_is_operator_p = true;
2376
2377   parser->default_arg_ok_p = true;
2378
2379   /* We are not parsing a constant-expression.  */
2380   parser->integral_constant_expression_p = false;
2381   parser->allow_non_integral_constant_expression_p = false;
2382   parser->non_integral_constant_expression_p = false;
2383
2384   /* Local variable names are not forbidden.  */
2385   parser->local_variables_forbidden_p = false;
2386
2387   /* We are not processing an `extern "C"' declaration.  */
2388   parser->in_unbraced_linkage_specification_p = false;
2389
2390   /* We are not processing a declarator.  */
2391   parser->in_declarator_p = false;
2392
2393   /* We are not processing a template-argument-list.  */
2394   parser->in_template_argument_list_p = false;
2395
2396   /* We are not in an iteration statement.  */
2397   parser->in_iteration_statement_p = false;
2398
2399   /* We are not in a switch statement.  */
2400   parser->in_switch_statement_p = false;
2401
2402   /* We are not parsing a type-id inside an expression.  */
2403   parser->in_type_id_in_expr_p = false;
2404
2405   /* Declarations aren't implicitly extern "C".  */
2406   parser->implicit_extern_c = false;
2407
2408   /* String literals should be translated to the execution character set.  */
2409   parser->translate_strings_p = true;
2410
2411   /* The unparsed function queue is empty.  */
2412   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2413
2414   /* There are no classes being defined.  */
2415   parser->num_classes_being_defined = 0;
2416
2417   /* No template parameters apply.  */
2418   parser->num_template_parameter_lists = 0;
2419
2420   return parser;
2421 }
2422
2423 /* Create a cp_lexer structure which will emit the tokens in CACHE
2424    and push it onto the parser's lexer stack.  This is used for delayed
2425    parsing of in-class method bodies and default arguments, and should
2426    not be confused with tentative parsing.  */
2427 static void
2428 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2429 {
2430   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2431   lexer->next = parser->lexer;
2432   parser->lexer = lexer;
2433
2434   /* Move the current source position to that of the first token in the
2435      new lexer.  */
2436   cp_lexer_set_source_position_from_token (lexer->next_token);
2437 }
2438
2439 /* Pop the top lexer off the parser stack.  This is never used for the
2440    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2441 static void
2442 cp_parser_pop_lexer (cp_parser *parser)
2443 {
2444   cp_lexer *lexer = parser->lexer;
2445   parser->lexer = lexer->next;
2446   cp_lexer_destroy (lexer);
2447
2448   /* Put the current source position back where it was before this
2449      lexer was pushed.  */
2450   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2451 }
2452
2453 /* Lexical conventions [gram.lex]  */
2454
2455 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2456    identifier.  */
2457
2458 static tree
2459 cp_parser_identifier (cp_parser* parser)
2460 {
2461   cp_token *token;
2462
2463   /* Look for the identifier.  */
2464   token = cp_parser_require (parser, CPP_NAME, "identifier");
2465   /* Return the value.  */
2466   return token ? token->value : error_mark_node;
2467 }
2468
2469 /* Parse a sequence of adjacent string constants.  Returns a
2470    TREE_STRING representing the combined, nul-terminated string
2471    constant.  If TRANSLATE is true, translate the string to the
2472    execution character set.  If WIDE_OK is true, a wide string is
2473    invalid here.
2474
2475    C++98 [lex.string] says that if a narrow string literal token is
2476    adjacent to a wide string literal token, the behavior is undefined.
2477    However, C99 6.4.5p4 says that this results in a wide string literal.
2478    We follow C99 here, for consistency with the C front end.
2479
2480    This code is largely lifted from lex_string() in c-lex.c.
2481
2482    FUTURE: ObjC++ will need to handle @-strings here.  */
2483 static tree
2484 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2485 {
2486   tree value;
2487   bool wide = false;
2488   size_t count;
2489   struct obstack str_ob;
2490   cpp_string str, istr, *strs;
2491   cp_token *tok;
2492
2493   tok = cp_lexer_peek_token (parser->lexer);
2494   if (!cp_parser_is_string_literal (tok))
2495     {
2496       cp_parser_error (parser, "expected string-literal");
2497       return error_mark_node;
2498     }
2499
2500   /* Try to avoid the overhead of creating and destroying an obstack
2501      for the common case of just one string.  */
2502   if (!cp_parser_is_string_literal
2503       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2504     {
2505       cp_lexer_consume_token (parser->lexer);
2506
2507       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2508       str.len = TREE_STRING_LENGTH (tok->value);
2509       count = 1;
2510       if (tok->type == CPP_WSTRING)
2511         wide = true;
2512
2513       strs = &str;
2514     }
2515   else
2516     {
2517       gcc_obstack_init (&str_ob);
2518       count = 0;
2519
2520       do
2521         {
2522           cp_lexer_consume_token (parser->lexer);
2523           count++;
2524           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2525           str.len = TREE_STRING_LENGTH (tok->value);
2526           if (tok->type == CPP_WSTRING)
2527             wide = true;
2528
2529           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2530
2531           tok = cp_lexer_peek_token (parser->lexer);
2532         }
2533       while (cp_parser_is_string_literal (tok));
2534
2535       strs = (cpp_string *) obstack_finish (&str_ob);
2536     }
2537
2538   if (wide && !wide_ok)
2539     {
2540       cp_parser_error (parser, "a wide string is invalid in this context");
2541       wide = false;
2542     }
2543
2544   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2545       (parse_in, strs, count, &istr, wide))
2546     {
2547       value = build_string (istr.len, (char *)istr.text);
2548       free ((void *)istr.text);
2549
2550       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2551       value = fix_string_type (value);
2552     }
2553   else
2554     /* cpp_interpret_string has issued an error.  */
2555     value = error_mark_node;
2556
2557   if (count > 1)
2558     obstack_free (&str_ob, 0);
2559
2560   return value;
2561 }
2562
2563
2564 /* Basic concepts [gram.basic]  */
2565
2566 /* Parse a translation-unit.
2567
2568    translation-unit:
2569      declaration-seq [opt]
2570
2571    Returns TRUE if all went well.  */
2572
2573 static bool
2574 cp_parser_translation_unit (cp_parser* parser)
2575 {
2576   /* The address of the first non-permanent object on the declarator
2577      obstack.  */
2578   static void *declarator_obstack_base;
2579
2580   bool success;
2581
2582   /* Create the declarator obstack, if necessary.  */
2583   if (!cp_error_declarator)
2584     {
2585       gcc_obstack_init (&declarator_obstack);
2586       /* Create the error declarator.  */
2587       cp_error_declarator = make_declarator (cdk_error);
2588       /* Create the empty parameter list.  */
2589       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2590       /* Remember where the base of the declarator obstack lies.  */
2591       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2592     }
2593
2594   while (true)
2595     {
2596       cp_parser_declaration_seq_opt (parser);
2597
2598       /* If there are no tokens left then all went well.  */
2599       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2600         {
2601           /* Get rid of the token array; we don't need it any more.  */
2602           cp_lexer_destroy (parser->lexer);
2603           parser->lexer = NULL;
2604
2605           /* This file might have been a context that's implicitly extern
2606              "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2607           if (parser->implicit_extern_c)
2608             {
2609               pop_lang_context ();
2610               parser->implicit_extern_c = false;
2611             }
2612
2613           /* Finish up.  */
2614           finish_translation_unit ();
2615
2616           success = true;
2617           break;
2618         }
2619       else
2620         {
2621           cp_parser_error (parser, "expected declaration");
2622           success = false;
2623           break;
2624         }
2625     }
2626
2627   /* Make sure the declarator obstack was fully cleaned up.  */
2628   gcc_assert (obstack_next_free (&declarator_obstack)
2629               == declarator_obstack_base);
2630
2631   /* All went well.  */
2632   return success;
2633 }
2634
2635 /* Expressions [gram.expr] */
2636
2637 /* Parse a primary-expression.
2638
2639    primary-expression:
2640      literal
2641      this
2642      ( expression )
2643      id-expression
2644
2645    GNU Extensions:
2646
2647    primary-expression:
2648      ( compound-statement )
2649      __builtin_va_arg ( assignment-expression , type-id )
2650
2651    literal:
2652      __null
2653
2654    CAST_P is true if this primary expression is the target of a cast.
2655
2656    Returns a representation of the expression.
2657
2658    *IDK indicates what kind of id-expression (if any) was present.
2659
2660    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2661    used as the operand of a pointer-to-member.  In that case,
2662    *QUALIFYING_CLASS gives the class that is used as the qualifying
2663    class in the pointer-to-member.  */
2664
2665 static tree
2666 cp_parser_primary_expression (cp_parser *parser,
2667                               bool cast_p,
2668                               cp_id_kind *idk,
2669                               tree *qualifying_class)
2670 {
2671   cp_token *token;
2672
2673   /* Assume the primary expression is not an id-expression.  */
2674   *idk = CP_ID_KIND_NONE;
2675   /* And that it cannot be used as pointer-to-member.  */
2676   *qualifying_class = NULL_TREE;
2677
2678   /* Peek at the next token.  */
2679   token = cp_lexer_peek_token (parser->lexer);
2680   switch (token->type)
2681     {
2682       /* literal:
2683            integer-literal
2684            character-literal
2685            floating-literal
2686            string-literal
2687            boolean-literal  */
2688     case CPP_CHAR:
2689     case CPP_WCHAR:
2690     case CPP_NUMBER:
2691       token = cp_lexer_consume_token (parser->lexer);
2692       /* Floating-point literals are only allowed in an integral
2693          constant expression if they are cast to an integral or
2694          enumeration type.  */
2695       if (TREE_CODE (token->value) == REAL_CST
2696           && parser->integral_constant_expression_p
2697           && pedantic)
2698         {
2699           /* CAST_P will be set even in invalid code like "int(2.7 +
2700              ...)".   Therefore, we have to check that the next token
2701              is sure to end the cast.  */
2702           if (cast_p)
2703             {
2704               cp_token *next_token;
2705
2706               next_token = cp_lexer_peek_token (parser->lexer);
2707               if (/* The comma at the end of an
2708                      enumerator-definition.  */
2709                   next_token->type != CPP_COMMA
2710                   /* The curly brace at the end of an enum-specifier.  */
2711                   && next_token->type != CPP_CLOSE_BRACE
2712                   /* The end of a statement.  */
2713                   && next_token->type != CPP_SEMICOLON
2714                   /* The end of the cast-expression.  */
2715                   && next_token->type != CPP_CLOSE_PAREN
2716                   /* The end of an array bound.  */
2717                   && next_token->type != CPP_CLOSE_SQUARE)
2718                 cast_p = false;
2719             }
2720
2721           /* If we are within a cast, then the constraint that the
2722              cast is to an integral or enumeration type will be
2723              checked at that point.  If we are not within a cast, then
2724              this code is invalid.  */
2725           if (!cast_p)
2726             cp_parser_non_integral_constant_expression 
2727               (parser, "floating-point literal");
2728         }
2729       return token->value;
2730
2731     case CPP_STRING:
2732     case CPP_WSTRING:
2733       /* ??? Should wide strings be allowed when parser->translate_strings_p
2734          is false (i.e. in attributes)?  If not, we can kill the third
2735          argument to cp_parser_string_literal.  */
2736       return cp_parser_string_literal (parser,
2737                                        parser->translate_strings_p,
2738                                        true);
2739
2740     case CPP_OPEN_PAREN:
2741       {
2742         tree expr;
2743         bool saved_greater_than_is_operator_p;
2744
2745         /* Consume the `('.  */
2746         cp_lexer_consume_token (parser->lexer);
2747         /* Within a parenthesized expression, a `>' token is always
2748            the greater-than operator.  */
2749         saved_greater_than_is_operator_p
2750           = parser->greater_than_is_operator_p;
2751         parser->greater_than_is_operator_p = true;
2752         /* If we see `( { ' then we are looking at the beginning of
2753            a GNU statement-expression.  */
2754         if (cp_parser_allow_gnu_extensions_p (parser)
2755             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2756           {
2757             /* Statement-expressions are not allowed by the standard.  */
2758             if (pedantic)
2759               pedwarn ("ISO C++ forbids braced-groups within expressions");
2760
2761             /* And they're not allowed outside of a function-body; you
2762                cannot, for example, write:
2763
2764                  int i = ({ int j = 3; j + 1; });
2765
2766                at class or namespace scope.  */
2767             if (!at_function_scope_p ())
2768               error ("statement-expressions are allowed only inside functions");
2769             /* Start the statement-expression.  */
2770             expr = begin_stmt_expr ();
2771             /* Parse the compound-statement.  */
2772             cp_parser_compound_statement (parser, expr, false);
2773             /* Finish up.  */
2774             expr = finish_stmt_expr (expr, false);
2775           }
2776         else
2777           {
2778             /* Parse the parenthesized expression.  */
2779             expr = cp_parser_expression (parser, cast_p);
2780             /* Let the front end know that this expression was
2781                enclosed in parentheses. This matters in case, for
2782                example, the expression is of the form `A::B', since
2783                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2784                not.  */
2785             finish_parenthesized_expr (expr);
2786           }
2787         /* The `>' token might be the end of a template-id or
2788            template-parameter-list now.  */
2789         parser->greater_than_is_operator_p
2790           = saved_greater_than_is_operator_p;
2791         /* Consume the `)'.  */
2792         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2793           cp_parser_skip_to_end_of_statement (parser);
2794
2795         return expr;
2796       }
2797
2798     case CPP_KEYWORD:
2799       switch (token->keyword)
2800         {
2801           /* These two are the boolean literals.  */
2802         case RID_TRUE:
2803           cp_lexer_consume_token (parser->lexer);
2804           return boolean_true_node;
2805         case RID_FALSE:
2806           cp_lexer_consume_token (parser->lexer);
2807           return boolean_false_node;
2808
2809           /* The `__null' literal.  */
2810         case RID_NULL:
2811           cp_lexer_consume_token (parser->lexer);
2812           return null_node;
2813
2814           /* Recognize the `this' keyword.  */
2815         case RID_THIS:
2816           cp_lexer_consume_token (parser->lexer);
2817           if (parser->local_variables_forbidden_p)
2818             {
2819               error ("%<this%> may not be used in this context");
2820               return error_mark_node;
2821             }
2822           /* Pointers cannot appear in constant-expressions.  */
2823           if (cp_parser_non_integral_constant_expression (parser,
2824                                                           "`this'"))
2825             return error_mark_node;
2826           return finish_this_expr ();
2827
2828           /* The `operator' keyword can be the beginning of an
2829              id-expression.  */
2830         case RID_OPERATOR:
2831           goto id_expression;
2832
2833         case RID_FUNCTION_NAME:
2834         case RID_PRETTY_FUNCTION_NAME:
2835         case RID_C99_FUNCTION_NAME:
2836           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2837              __func__ are the names of variables -- but they are
2838              treated specially.  Therefore, they are handled here,
2839              rather than relying on the generic id-expression logic
2840              below.  Grammatically, these names are id-expressions.
2841
2842              Consume the token.  */
2843           token = cp_lexer_consume_token (parser->lexer);
2844           /* Look up the name.  */
2845           return finish_fname (token->value);
2846
2847         case RID_VA_ARG:
2848           {
2849             tree expression;
2850             tree type;
2851
2852             /* The `__builtin_va_arg' construct is used to handle
2853                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2854             cp_lexer_consume_token (parser->lexer);
2855             /* Look for the opening `('.  */
2856             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2857             /* Now, parse the assignment-expression.  */
2858             expression = cp_parser_assignment_expression (parser,
2859                                                           /*cast_p=*/false);
2860             /* Look for the `,'.  */
2861             cp_parser_require (parser, CPP_COMMA, "`,'");
2862             /* Parse the type-id.  */
2863             type = cp_parser_type_id (parser);
2864             /* Look for the closing `)'.  */
2865             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2866             /* Using `va_arg' in a constant-expression is not
2867                allowed.  */
2868             if (cp_parser_non_integral_constant_expression (parser,
2869                                                             "`va_arg'"))
2870               return error_mark_node;
2871             return build_x_va_arg (expression, type);
2872           }
2873
2874         case RID_OFFSETOF:
2875           return cp_parser_builtin_offsetof (parser);
2876
2877         default:
2878           cp_parser_error (parser, "expected primary-expression");
2879           return error_mark_node;
2880         }
2881
2882       /* An id-expression can start with either an identifier, a
2883          `::' as the beginning of a qualified-id, or the "operator"
2884          keyword.  */
2885     case CPP_NAME:
2886     case CPP_SCOPE:
2887     case CPP_TEMPLATE_ID:
2888     case CPP_NESTED_NAME_SPECIFIER:
2889       {
2890         tree id_expression;
2891         tree decl;
2892         const char *error_msg;
2893
2894       id_expression:
2895         /* Parse the id-expression.  */
2896         id_expression
2897           = cp_parser_id_expression (parser,
2898                                      /*template_keyword_p=*/false,
2899                                      /*check_dependency_p=*/true,
2900                                      /*template_p=*/NULL,
2901                                      /*declarator_p=*/false);
2902         if (id_expression == error_mark_node)
2903           return error_mark_node;
2904         /* If we have a template-id, then no further lookup is
2905            required.  If the template-id was for a template-class, we
2906            will sometimes have a TYPE_DECL at this point.  */
2907         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2908             || TREE_CODE (id_expression) == TYPE_DECL)
2909           decl = id_expression;
2910         /* Look up the name.  */
2911         else
2912           {
2913             bool ambiguous_p;
2914
2915             decl = cp_parser_lookup_name (parser, id_expression,
2916                                           none_type,
2917                                           /*is_template=*/false,
2918                                           /*is_namespace=*/false,
2919                                           /*check_dependency=*/true,
2920                                           &ambiguous_p);
2921             /* If the lookup was ambiguous, an error will already have
2922                been issued.  */
2923             if (ambiguous_p)
2924               return error_mark_node;
2925             /* If name lookup gives us a SCOPE_REF, then the
2926                qualifying scope was dependent.  Just propagate the
2927                name.  */
2928             if (TREE_CODE (decl) == SCOPE_REF)
2929               {
2930                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2931                   *qualifying_class = TREE_OPERAND (decl, 0);
2932                 return decl;
2933               }
2934             /* Check to see if DECL is a local variable in a context
2935                where that is forbidden.  */
2936             if (parser->local_variables_forbidden_p
2937                 && local_variable_p (decl))
2938               {
2939                 /* It might be that we only found DECL because we are
2940                    trying to be generous with pre-ISO scoping rules.
2941                    For example, consider:
2942
2943                      int i;
2944                      void g() {
2945                        for (int i = 0; i < 10; ++i) {}
2946                        extern void f(int j = i);
2947                      }
2948
2949                    Here, name look up will originally find the out
2950                    of scope `i'.  We need to issue a warning message,
2951                    but then use the global `i'.  */
2952                 decl = check_for_out_of_scope_variable (decl);
2953                 if (local_variable_p (decl))
2954                   {
2955                     error ("local variable %qD may not appear in this context",
2956                            decl);
2957                     return error_mark_node;
2958                   }
2959               }
2960           }
2961
2962         decl = finish_id_expression (id_expression, decl, parser->scope,
2963                                      idk, qualifying_class,
2964                                      parser->integral_constant_expression_p,
2965                                      parser->allow_non_integral_constant_expression_p,
2966                                      &parser->non_integral_constant_expression_p,
2967                                      &error_msg);
2968         if (error_msg)
2969           cp_parser_error (parser, error_msg);
2970         return decl;
2971       }
2972
2973       /* Anything else is an error.  */
2974     default:
2975       cp_parser_error (parser, "expected primary-expression");
2976       return error_mark_node;
2977     }
2978 }
2979
2980 /* Parse an id-expression.
2981
2982    id-expression:
2983      unqualified-id
2984      qualified-id
2985
2986    qualified-id:
2987      :: [opt] nested-name-specifier template [opt] unqualified-id
2988      :: identifier
2989      :: operator-function-id
2990      :: template-id
2991
2992    Return a representation of the unqualified portion of the
2993    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2994    a `::' or nested-name-specifier.
2995
2996    Often, if the id-expression was a qualified-id, the caller will
2997    want to make a SCOPE_REF to represent the qualified-id.  This
2998    function does not do this in order to avoid wastefully creating
2999    SCOPE_REFs when they are not required.
3000
3001    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3002    `template' keyword.
3003
3004    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3005    uninstantiated templates.
3006
3007    If *TEMPLATE_P is non-NULL, it is set to true iff the
3008    `template' keyword is used to explicitly indicate that the entity
3009    named is a template.
3010
3011    If DECLARATOR_P is true, the id-expression is appearing as part of
3012    a declarator, rather than as part of an expression.  */
3013
3014 static tree
3015 cp_parser_id_expression (cp_parser *parser,
3016                          bool template_keyword_p,
3017                          bool check_dependency_p,
3018                          bool *template_p,
3019                          bool declarator_p)
3020 {
3021   bool global_scope_p;
3022   bool nested_name_specifier_p;
3023
3024   /* Assume the `template' keyword was not used.  */
3025   if (template_p)
3026     *template_p = false;
3027
3028   /* Look for the optional `::' operator.  */
3029   global_scope_p
3030     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3031        != NULL_TREE);
3032   /* Look for the optional nested-name-specifier.  */
3033   nested_name_specifier_p
3034     = (cp_parser_nested_name_specifier_opt (parser,
3035                                             /*typename_keyword_p=*/false,
3036                                             check_dependency_p,
3037                                             /*type_p=*/false,
3038                                             declarator_p)
3039        != NULL_TREE);
3040   /* If there is a nested-name-specifier, then we are looking at
3041      the first qualified-id production.  */
3042   if (nested_name_specifier_p)
3043     {
3044       tree saved_scope;
3045       tree saved_object_scope;
3046       tree saved_qualifying_scope;
3047       tree unqualified_id;
3048       bool is_template;
3049
3050       /* See if the next token is the `template' keyword.  */
3051       if (!template_p)
3052         template_p = &is_template;
3053       *template_p = cp_parser_optional_template_keyword (parser);
3054       /* Name lookup we do during the processing of the
3055          unqualified-id might obliterate SCOPE.  */
3056       saved_scope = parser->scope;
3057       saved_object_scope = parser->object_scope;
3058       saved_qualifying_scope = parser->qualifying_scope;
3059       /* Process the final unqualified-id.  */
3060       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3061                                                  check_dependency_p,
3062                                                  declarator_p);
3063       /* Restore the SAVED_SCOPE for our caller.  */
3064       parser->scope = saved_scope;
3065       parser->object_scope = saved_object_scope;
3066       parser->qualifying_scope = saved_qualifying_scope;
3067
3068       return unqualified_id;
3069     }
3070   /* Otherwise, if we are in global scope, then we are looking at one
3071      of the other qualified-id productions.  */
3072   else if (global_scope_p)
3073     {
3074       cp_token *token;
3075       tree id;
3076
3077       /* Peek at the next token.  */
3078       token = cp_lexer_peek_token (parser->lexer);
3079
3080       /* If it's an identifier, and the next token is not a "<", then
3081          we can avoid the template-id case.  This is an optimization
3082          for this common case.  */
3083       if (token->type == CPP_NAME
3084           && !cp_parser_nth_token_starts_template_argument_list_p
3085                (parser, 2))
3086         return cp_parser_identifier (parser);
3087
3088       cp_parser_parse_tentatively (parser);
3089       /* Try a template-id.  */
3090       id = cp_parser_template_id (parser,
3091                                   /*template_keyword_p=*/false,
3092                                   /*check_dependency_p=*/true,
3093                                   declarator_p);
3094       /* If that worked, we're done.  */
3095       if (cp_parser_parse_definitely (parser))
3096         return id;
3097
3098       /* Peek at the next token.  (Changes in the token buffer may
3099          have invalidated the pointer obtained above.)  */
3100       token = cp_lexer_peek_token (parser->lexer);
3101
3102       switch (token->type)
3103         {
3104         case CPP_NAME:
3105           return cp_parser_identifier (parser);
3106
3107         case CPP_KEYWORD:
3108           if (token->keyword == RID_OPERATOR)
3109             return cp_parser_operator_function_id (parser);
3110           /* Fall through.  */
3111
3112         default:
3113           cp_parser_error (parser, "expected id-expression");
3114           return error_mark_node;
3115         }
3116     }
3117   else
3118     return cp_parser_unqualified_id (parser, template_keyword_p,
3119                                      /*check_dependency_p=*/true,
3120                                      declarator_p);
3121 }
3122
3123 /* Parse an unqualified-id.
3124
3125    unqualified-id:
3126      identifier
3127      operator-function-id
3128      conversion-function-id
3129      ~ class-name
3130      template-id
3131
3132    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3133    keyword, in a construct like `A::template ...'.
3134
3135    Returns a representation of unqualified-id.  For the `identifier'
3136    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3137    production a BIT_NOT_EXPR is returned; the operand of the
3138    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3139    other productions, see the documentation accompanying the
3140    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3141    names are looked up in uninstantiated templates.  If DECLARATOR_P
3142    is true, the unqualified-id is appearing as part of a declarator,
3143    rather than as part of an expression.  */
3144
3145 static tree
3146 cp_parser_unqualified_id (cp_parser* parser,
3147                           bool template_keyword_p,
3148                           bool check_dependency_p,
3149                           bool declarator_p)
3150 {
3151   cp_token *token;
3152
3153   /* Peek at the next token.  */
3154   token = cp_lexer_peek_token (parser->lexer);
3155
3156   switch (token->type)
3157     {
3158     case CPP_NAME:
3159       {
3160         tree id;
3161
3162         /* We don't know yet whether or not this will be a
3163            template-id.  */
3164         cp_parser_parse_tentatively (parser);
3165         /* Try a template-id.  */
3166         id = cp_parser_template_id (parser, template_keyword_p,
3167                                     check_dependency_p,
3168                                     declarator_p);
3169         /* If it worked, we're done.  */
3170         if (cp_parser_parse_definitely (parser))
3171           return id;
3172         /* Otherwise, it's an ordinary identifier.  */
3173         return cp_parser_identifier (parser);
3174       }
3175
3176     case CPP_TEMPLATE_ID:
3177       return cp_parser_template_id (parser, template_keyword_p,
3178                                     check_dependency_p,
3179                                     declarator_p);
3180
3181     case CPP_COMPL:
3182       {
3183         tree type_decl;
3184         tree qualifying_scope;
3185         tree object_scope;
3186         tree scope;
3187         bool done;
3188
3189         /* Consume the `~' token.  */
3190         cp_lexer_consume_token (parser->lexer);
3191         /* Parse the class-name.  The standard, as written, seems to
3192            say that:
3193
3194              template <typename T> struct S { ~S (); };
3195              template <typename T> S<T>::~S() {}
3196
3197            is invalid, since `~' must be followed by a class-name, but
3198            `S<T>' is dependent, and so not known to be a class.
3199            That's not right; we need to look in uninstantiated
3200            templates.  A further complication arises from:
3201
3202              template <typename T> void f(T t) {
3203                t.T::~T();
3204              }
3205
3206            Here, it is not possible to look up `T' in the scope of `T'
3207            itself.  We must look in both the current scope, and the
3208            scope of the containing complete expression.
3209
3210            Yet another issue is:
3211
3212              struct S {
3213                int S;
3214                ~S();
3215              };
3216
3217              S::~S() {}
3218
3219            The standard does not seem to say that the `S' in `~S'
3220            should refer to the type `S' and not the data member
3221            `S::S'.  */
3222
3223         /* DR 244 says that we look up the name after the "~" in the
3224            same scope as we looked up the qualifying name.  That idea
3225            isn't fully worked out; it's more complicated than that.  */
3226         scope = parser->scope;
3227         object_scope = parser->object_scope;
3228         qualifying_scope = parser->qualifying_scope;
3229
3230         /* If the name is of the form "X::~X" it's OK.  */
3231         if (scope && TYPE_P (scope)
3232             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3233             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3234                 == CPP_OPEN_PAREN)
3235             && (cp_lexer_peek_token (parser->lexer)->value
3236                 == TYPE_IDENTIFIER (scope)))
3237           {
3238             cp_lexer_consume_token (parser->lexer);
3239             return build_nt (BIT_NOT_EXPR, scope);
3240           }
3241
3242         /* If there was an explicit qualification (S::~T), first look
3243            in the scope given by the qualification (i.e., S).  */
3244         done = false;
3245         type_decl = NULL_TREE;
3246         if (scope)
3247           {
3248             cp_parser_parse_tentatively (parser);
3249             type_decl = cp_parser_class_name (parser,
3250                                               /*typename_keyword_p=*/false,
3251                                               /*template_keyword_p=*/false,
3252                                               none_type,
3253                                               /*check_dependency=*/false,
3254                                               /*class_head_p=*/false,
3255                                               declarator_p);
3256             if (cp_parser_parse_definitely (parser))
3257               done = true;
3258           }
3259         /* In "N::S::~S", look in "N" as well.  */
3260         if (!done && scope && qualifying_scope)
3261           {
3262             cp_parser_parse_tentatively (parser);
3263             parser->scope = qualifying_scope;
3264             parser->object_scope = NULL_TREE;
3265             parser->qualifying_scope = NULL_TREE;
3266             type_decl
3267               = cp_parser_class_name (parser,
3268                                       /*typename_keyword_p=*/false,
3269                                       /*template_keyword_p=*/false,
3270                                       none_type,
3271                                       /*check_dependency=*/false,
3272                                       /*class_head_p=*/false,
3273                                       declarator_p);
3274             if (cp_parser_parse_definitely (parser))
3275               done = true;
3276           }
3277         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3278         else if (!done && object_scope)
3279           {
3280             cp_parser_parse_tentatively (parser);
3281             parser->scope = object_scope;
3282             parser->object_scope = NULL_TREE;
3283             parser->qualifying_scope = NULL_TREE;
3284             type_decl
3285               = cp_parser_class_name (parser,
3286                                       /*typename_keyword_p=*/false,
3287                                       /*template_keyword_p=*/false,
3288                                       none_type,
3289                                       /*check_dependency=*/false,
3290                                       /*class_head_p=*/false,
3291                                       declarator_p);
3292             if (cp_parser_parse_definitely (parser))
3293               done = true;
3294           }
3295         /* Look in the surrounding context.  */
3296         if (!done)
3297           {
3298             parser->scope = NULL_TREE;
3299             parser->object_scope = NULL_TREE;
3300             parser->qualifying_scope = NULL_TREE;
3301             type_decl
3302               = cp_parser_class_name (parser,
3303                                       /*typename_keyword_p=*/false,
3304                                       /*template_keyword_p=*/false,
3305                                       none_type,
3306                                       /*check_dependency=*/false,
3307                                       /*class_head_p=*/false,
3308                                       declarator_p);
3309           }
3310         /* If an error occurred, assume that the name of the
3311            destructor is the same as the name of the qualifying
3312            class.  That allows us to keep parsing after running
3313            into ill-formed destructor names.  */
3314         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3315           return build_nt (BIT_NOT_EXPR, scope);
3316         else if (type_decl == error_mark_node)
3317           return error_mark_node;
3318
3319         /* [class.dtor]
3320
3321            A typedef-name that names a class shall not be used as the
3322            identifier in the declarator for a destructor declaration.  */
3323         if (declarator_p
3324             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3325             && !DECL_SELF_REFERENCE_P (type_decl)
3326             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3327           error ("typedef-name %qD used as destructor declarator",
3328                  type_decl);
3329
3330         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3331       }
3332
3333     case CPP_KEYWORD:
3334       if (token->keyword == RID_OPERATOR)
3335         {
3336           tree id;
3337
3338           /* This could be a template-id, so we try that first.  */
3339           cp_parser_parse_tentatively (parser);
3340           /* Try a template-id.  */
3341           id = cp_parser_template_id (parser, template_keyword_p,
3342                                       /*check_dependency_p=*/true,
3343                                       declarator_p);
3344           /* If that worked, we're done.  */
3345           if (cp_parser_parse_definitely (parser))
3346             return id;
3347           /* We still don't know whether we're looking at an
3348              operator-function-id or a conversion-function-id.  */
3349           cp_parser_parse_tentatively (parser);
3350           /* Try an operator-function-id.  */
3351           id = cp_parser_operator_function_id (parser);
3352           /* If that didn't work, try a conversion-function-id.  */
3353           if (!cp_parser_parse_definitely (parser))
3354             id = cp_parser_conversion_function_id (parser);
3355
3356           return id;
3357         }
3358       /* Fall through.  */
3359
3360     default:
3361       cp_parser_error (parser, "expected unqualified-id");
3362       return error_mark_node;
3363     }
3364 }
3365
3366 /* Parse an (optional) nested-name-specifier.
3367
3368    nested-name-specifier:
3369      class-or-namespace-name :: nested-name-specifier [opt]
3370      class-or-namespace-name :: template nested-name-specifier [opt]
3371
3372    PARSER->SCOPE should be set appropriately before this function is
3373    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3374    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3375    in name lookups.
3376
3377    Sets PARSER->SCOPE to the class (TYPE) or namespace
3378    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3379    it unchanged if there is no nested-name-specifier.  Returns the new
3380    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3381
3382    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3383    part of a declaration and/or decl-specifier.  */
3384
3385 static tree
3386 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3387                                      bool typename_keyword_p,
3388                                      bool check_dependency_p,
3389                                      bool type_p,
3390                                      bool is_declaration)
3391 {
3392   bool success = false;
3393   tree access_check = NULL_TREE;
3394   cp_token_position start = 0;
3395   cp_token *token;
3396
3397   /* If the next token corresponds to a nested name specifier, there
3398      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3399      false, it may have been true before, in which case something
3400      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3401      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3402      CHECK_DEPENDENCY_P is false, we have to fall through into the
3403      main loop.  */
3404   if (check_dependency_p
3405       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3406     {
3407       cp_parser_pre_parsed_nested_name_specifier (parser);
3408       return parser->scope;
3409     }
3410
3411   /* Remember where the nested-name-specifier starts.  */
3412   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3413     start = cp_lexer_token_position (parser->lexer, false);
3414
3415   push_deferring_access_checks (dk_deferred);
3416
3417   while (true)
3418     {
3419       tree new_scope;
3420       tree old_scope;
3421       tree saved_qualifying_scope;
3422       bool template_keyword_p;
3423
3424       /* Spot cases that cannot be the beginning of a
3425          nested-name-specifier.  */
3426       token = cp_lexer_peek_token (parser->lexer);
3427
3428       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3429          the already parsed nested-name-specifier.  */
3430       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3431         {
3432           /* Grab the nested-name-specifier and continue the loop.  */
3433           cp_parser_pre_parsed_nested_name_specifier (parser);
3434           success = true;
3435           continue;
3436         }
3437
3438       /* Spot cases that cannot be the beginning of a
3439          nested-name-specifier.  On the second and subsequent times
3440          through the loop, we look for the `template' keyword.  */
3441       if (success && token->keyword == RID_TEMPLATE)
3442         ;
3443       /* A template-id can start a nested-name-specifier.  */
3444       else if (token->type == CPP_TEMPLATE_ID)
3445         ;
3446       else
3447         {
3448           /* If the next token is not an identifier, then it is
3449              definitely not a class-or-namespace-name.  */
3450           if (token->type != CPP_NAME)
3451             break;
3452           /* If the following token is neither a `<' (to begin a
3453              template-id), nor a `::', then we are not looking at a
3454              nested-name-specifier.  */
3455           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3456           if (token->type != CPP_SCOPE
3457               && !cp_parser_nth_token_starts_template_argument_list_p
3458                   (parser, 2))
3459             break;
3460         }
3461
3462       /* The nested-name-specifier is optional, so we parse
3463          tentatively.  */
3464       cp_parser_parse_tentatively (parser);
3465
3466       /* Look for the optional `template' keyword, if this isn't the
3467          first time through the loop.  */
3468       if (success)
3469         template_keyword_p = cp_parser_optional_template_keyword (parser);
3470       else
3471         template_keyword_p = false;
3472
3473       /* Save the old scope since the name lookup we are about to do
3474          might destroy it.  */
3475       old_scope = parser->scope;
3476       saved_qualifying_scope = parser->qualifying_scope;
3477       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3478          look up names in "X<T>::I" in order to determine that "Y" is
3479          a template.  So, if we have a typename at this point, we make
3480          an effort to look through it.  */
3481       if (is_declaration 
3482           && !typename_keyword_p
3483           && parser->scope 
3484           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3485         parser->scope = resolve_typename_type (parser->scope, 
3486                                                /*only_current_p=*/false);
3487       /* Parse the qualifying entity.  */
3488       new_scope
3489         = cp_parser_class_or_namespace_name (parser,
3490                                              typename_keyword_p,
3491                                              template_keyword_p,
3492                                              check_dependency_p,
3493                                              type_p,
3494                                              is_declaration);
3495       /* Look for the `::' token.  */
3496       cp_parser_require (parser, CPP_SCOPE, "`::'");
3497
3498       /* If we found what we wanted, we keep going; otherwise, we're
3499          done.  */
3500       if (!cp_parser_parse_definitely (parser))
3501         {
3502           bool error_p = false;
3503
3504           /* Restore the OLD_SCOPE since it was valid before the
3505              failed attempt at finding the last
3506              class-or-namespace-name.  */
3507           parser->scope = old_scope;
3508           parser->qualifying_scope = saved_qualifying_scope;
3509           /* If the next token is an identifier, and the one after
3510              that is a `::', then any valid interpretation would have
3511              found a class-or-namespace-name.  */
3512           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3513                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3514                      == CPP_SCOPE)
3515                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3516                      != CPP_COMPL))
3517             {
3518               token = cp_lexer_consume_token (parser->lexer);
3519               if (!error_p)
3520                 {
3521                   tree decl;
3522
3523                   decl = cp_parser_lookup_name_simple (parser, token->value);
3524                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3525                     error ("%qD used without template parameters", decl);
3526                   else
3527                     cp_parser_name_lookup_error
3528                       (parser, token->value, decl,
3529                        "is not a class or namespace");
3530                   parser->scope = NULL_TREE;
3531                   error_p = true;
3532                   /* Treat this as a successful nested-name-specifier
3533                      due to:
3534
3535                      [basic.lookup.qual]
3536
3537                      If the name found is not a class-name (clause
3538                      _class_) or namespace-name (_namespace.def_), the
3539                      program is ill-formed.  */
3540                   success = true;
3541                 }
3542               cp_lexer_consume_token (parser->lexer);
3543             }
3544           break;
3545         }
3546
3547       /* We've found one valid nested-name-specifier.  */
3548       success = true;
3549       /* Make sure we look in the right scope the next time through
3550          the loop.  */
3551       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3552                        ? TREE_TYPE (new_scope)
3553                        : new_scope);
3554       /* If it is a class scope, try to complete it; we are about to
3555          be looking up names inside the class.  */
3556       if (TYPE_P (parser->scope)
3557           /* Since checking types for dependency can be expensive,
3558              avoid doing it if the type is already complete.  */
3559           && !COMPLETE_TYPE_P (parser->scope)
3560           /* Do not try to complete dependent types.  */
3561           && !dependent_type_p (parser->scope))
3562         complete_type (parser->scope);
3563     }
3564
3565   /* Retrieve any deferred checks.  Do not pop this access checks yet
3566      so the memory will not be reclaimed during token replacing below.  */
3567   access_check = get_deferred_access_checks ();
3568
3569   /* If parsing tentatively, replace the sequence of tokens that makes
3570      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3571      token.  That way, should we re-parse the token stream, we will
3572      not have to repeat the effort required to do the parse, nor will
3573      we issue duplicate error messages.  */
3574   if (success && start)
3575     {
3576       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3577       
3578       /* Reset the contents of the START token.  */
3579       token->type = CPP_NESTED_NAME_SPECIFIER;
3580       token->value = build_tree_list (access_check, parser->scope);
3581       TREE_TYPE (token->value) = parser->qualifying_scope;
3582       token->keyword = RID_MAX;
3583       
3584       /* Purge all subsequent tokens.  */
3585       cp_lexer_purge_tokens_after (parser->lexer, start);
3586     }
3587
3588   pop_deferring_access_checks ();
3589   return success ? parser->scope : NULL_TREE;
3590 }
3591
3592 /* Parse a nested-name-specifier.  See
3593    cp_parser_nested_name_specifier_opt for details.  This function
3594    behaves identically, except that it will an issue an error if no
3595    nested-name-specifier is present, and it will return
3596    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3597    is present.  */
3598
3599 static tree
3600 cp_parser_nested_name_specifier (cp_parser *parser,
3601                                  bool typename_keyword_p,
3602                                  bool check_dependency_p,
3603                                  bool type_p,
3604                                  bool is_declaration)
3605 {
3606   tree scope;
3607
3608   /* Look for the nested-name-specifier.  */
3609   scope = cp_parser_nested_name_specifier_opt (parser,
3610                                                typename_keyword_p,
3611                                                check_dependency_p,
3612                                                type_p,
3613                                                is_declaration);
3614   /* If it was not present, issue an error message.  */
3615   if (!scope)
3616     {
3617       cp_parser_error (parser, "expected nested-name-specifier");
3618       parser->scope = NULL_TREE;
3619       return error_mark_node;
3620     }
3621
3622   return scope;
3623 }
3624
3625 /* Parse a class-or-namespace-name.
3626
3627    class-or-namespace-name:
3628      class-name
3629      namespace-name
3630
3631    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3632    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3633    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3634    TYPE_P is TRUE iff the next name should be taken as a class-name,
3635    even the same name is declared to be another entity in the same
3636    scope.
3637
3638    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3639    specified by the class-or-namespace-name.  If neither is found the
3640    ERROR_MARK_NODE is returned.  */
3641
3642 static tree
3643 cp_parser_class_or_namespace_name (cp_parser *parser,
3644                                    bool typename_keyword_p,
3645                                    bool template_keyword_p,
3646                                    bool check_dependency_p,
3647                                    bool type_p,
3648                                    bool is_declaration)
3649 {
3650   tree saved_scope;
3651   tree saved_qualifying_scope;
3652   tree saved_object_scope;
3653   tree scope;
3654   bool only_class_p;
3655
3656   /* Before we try to parse the class-name, we must save away the
3657      current PARSER->SCOPE since cp_parser_class_name will destroy
3658      it.  */
3659   saved_scope = parser->scope;
3660   saved_qualifying_scope = parser->qualifying_scope;
3661   saved_object_scope = parser->object_scope;
3662   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3663      there is no need to look for a namespace-name.  */
3664   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3665   if (!only_class_p)
3666     cp_parser_parse_tentatively (parser);
3667   scope = cp_parser_class_name (parser,
3668                                 typename_keyword_p,
3669                                 template_keyword_p,
3670                                 type_p ? class_type : none_type,
3671                                 check_dependency_p,
3672                                 /*class_head_p=*/false,
3673                                 is_declaration);
3674   /* If that didn't work, try for a namespace-name.  */
3675   if (!only_class_p && !cp_parser_parse_definitely (parser))
3676     {
3677       /* Restore the saved scope.  */
3678       parser->scope = saved_scope;
3679       parser->qualifying_scope = saved_qualifying_scope;
3680       parser->object_scope = saved_object_scope;
3681       /* If we are not looking at an identifier followed by the scope
3682          resolution operator, then this is not part of a
3683          nested-name-specifier.  (Note that this function is only used
3684          to parse the components of a nested-name-specifier.)  */
3685       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3686           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3687         return error_mark_node;
3688       scope = cp_parser_namespace_name (parser);
3689     }
3690
3691   return scope;
3692 }
3693
3694 /* Parse a postfix-expression.
3695
3696    postfix-expression:
3697      primary-expression
3698      postfix-expression [ expression ]
3699      postfix-expression ( expression-list [opt] )
3700      simple-type-specifier ( expression-list [opt] )
3701      typename :: [opt] nested-name-specifier identifier
3702        ( expression-list [opt] )
3703      typename :: [opt] nested-name-specifier template [opt] template-id
3704        ( expression-list [opt] )
3705      postfix-expression . template [opt] id-expression
3706      postfix-expression -> template [opt] id-expression
3707      postfix-expression . pseudo-destructor-name
3708      postfix-expression -> pseudo-destructor-name
3709      postfix-expression ++
3710      postfix-expression --
3711      dynamic_cast < type-id > ( expression )
3712      static_cast < type-id > ( expression )
3713      reinterpret_cast < type-id > ( expression )
3714      const_cast < type-id > ( expression )
3715      typeid ( expression )
3716      typeid ( type-id )
3717
3718    GNU Extension:
3719
3720    postfix-expression:
3721      ( type-id ) { initializer-list , [opt] }
3722
3723    This extension is a GNU version of the C99 compound-literal
3724    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3725    but they are essentially the same concept.)
3726
3727    If ADDRESS_P is true, the postfix expression is the operand of the
3728    `&' operator.  CAST_P is true if this expression is the target of a
3729    cast. 
3730
3731    Returns a representation of the expression.  */
3732
3733 static tree
3734 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3735 {
3736   cp_token *token;
3737   enum rid keyword;
3738   cp_id_kind idk = CP_ID_KIND_NONE;
3739   tree postfix_expression = NULL_TREE;
3740   /* Non-NULL only if the current postfix-expression can be used to
3741      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3742      class used to qualify the member.  */
3743   tree qualifying_class = NULL_TREE;
3744
3745   /* Peek at the next token.  */
3746   token = cp_lexer_peek_token (parser->lexer);
3747   /* Some of the productions are determined by keywords.  */
3748   keyword = token->keyword;
3749   switch (keyword)
3750     {
3751     case RID_DYNCAST:
3752     case RID_STATCAST:
3753     case RID_REINTCAST:
3754     case RID_CONSTCAST:
3755       {
3756         tree type;
3757         tree expression;
3758         const char *saved_message;
3759
3760         /* All of these can be handled in the same way from the point
3761            of view of parsing.  Begin by consuming the token
3762            identifying the cast.  */
3763         cp_lexer_consume_token (parser->lexer);
3764
3765         /* New types cannot be defined in the cast.  */
3766         saved_message = parser->type_definition_forbidden_message;
3767         parser->type_definition_forbidden_message
3768           = "types may not be defined in casts";
3769
3770         /* Look for the opening `<'.  */
3771         cp_parser_require (parser, CPP_LESS, "`<'");
3772         /* Parse the type to which we are casting.  */
3773         type = cp_parser_type_id (parser);
3774         /* Look for the closing `>'.  */
3775         cp_parser_require (parser, CPP_GREATER, "`>'");
3776         /* Restore the old message.  */
3777         parser->type_definition_forbidden_message = saved_message;
3778
3779         /* And the expression which is being cast.  */
3780         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3781         expression = cp_parser_expression (parser, /*cast_p=*/true);
3782         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3783
3784         /* Only type conversions to integral or enumeration types
3785            can be used in constant-expressions.  */
3786         if (parser->integral_constant_expression_p
3787             && !dependent_type_p (type)
3788             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3789             && (cp_parser_non_integral_constant_expression
3790                 (parser,
3791                  "a cast to a type other than an integral or "
3792                  "enumeration type")))
3793           return error_mark_node;
3794
3795         switch (keyword)
3796           {
3797           case RID_DYNCAST:
3798             postfix_expression
3799               = build_dynamic_cast (type, expression);
3800             break;
3801           case RID_STATCAST:
3802             postfix_expression
3803               = build_static_cast (type, expression);
3804             break;
3805           case RID_REINTCAST:
3806             postfix_expression
3807               = build_reinterpret_cast (type, expression);
3808             break;
3809           case RID_CONSTCAST:
3810             postfix_expression
3811               = build_const_cast (type, expression);
3812             break;
3813           default:
3814             gcc_unreachable ();
3815           }
3816       }
3817       break;
3818
3819     case RID_TYPEID:
3820       {
3821         tree type;
3822         const char *saved_message;
3823         bool saved_in_type_id_in_expr_p;
3824
3825         /* Consume the `typeid' token.  */
3826         cp_lexer_consume_token (parser->lexer);
3827         /* Look for the `(' token.  */
3828         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3829         /* Types cannot be defined in a `typeid' expression.  */
3830         saved_message = parser->type_definition_forbidden_message;
3831         parser->type_definition_forbidden_message
3832           = "types may not be defined in a `typeid\' expression";
3833         /* We can't be sure yet whether we're looking at a type-id or an
3834            expression.  */
3835         cp_parser_parse_tentatively (parser);
3836         /* Try a type-id first.  */
3837         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3838         parser->in_type_id_in_expr_p = true;
3839         type = cp_parser_type_id (parser);
3840         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3841         /* Look for the `)' token.  Otherwise, we can't be sure that
3842            we're not looking at an expression: consider `typeid (int
3843            (3))', for example.  */
3844         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3845         /* If all went well, simply lookup the type-id.  */
3846         if (cp_parser_parse_definitely (parser))
3847           postfix_expression = get_typeid (type);
3848         /* Otherwise, fall back to the expression variant.  */
3849         else
3850           {
3851             tree expression;
3852
3853             /* Look for an expression.  */
3854             expression = cp_parser_expression (parser, /*cast_p=*/false);
3855             /* Compute its typeid.  */
3856             postfix_expression = build_typeid (expression);
3857             /* Look for the `)' token.  */
3858             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3859           }
3860         /* `typeid' may not appear in an integral constant expression.  */
3861         if (cp_parser_non_integral_constant_expression(parser,
3862                                                        "`typeid' operator"))
3863           return error_mark_node;
3864         /* Restore the saved message.  */
3865         parser->type_definition_forbidden_message = saved_message;
3866       }
3867       break;
3868
3869     case RID_TYPENAME:
3870       {
3871         bool template_p = false;
3872         tree id;
3873         tree type;
3874         tree scope;
3875
3876         /* Consume the `typename' token.  */
3877         cp_lexer_consume_token (parser->lexer);
3878         /* Look for the optional `::' operator.  */
3879         cp_parser_global_scope_opt (parser,
3880                                     /*current_scope_valid_p=*/false);
3881         /* Look for the nested-name-specifier.  In case of error here,
3882            consume the trailing id to avoid subsequent error messages
3883            for usual cases.  */
3884         scope = cp_parser_nested_name_specifier (parser,
3885                                                  /*typename_keyword_p=*/true,
3886                                                  /*check_dependency_p=*/true,
3887                                                  /*type_p=*/true,
3888                                                  /*is_declaration=*/true);
3889
3890         /* Look for the optional `template' keyword.  */
3891         template_p = cp_parser_optional_template_keyword (parser);
3892         /* We don't know whether we're looking at a template-id or an
3893            identifier.  */
3894         cp_parser_parse_tentatively (parser);
3895         /* Try a template-id.  */
3896         id = cp_parser_template_id (parser, template_p,
3897                                     /*check_dependency_p=*/true,
3898                                     /*is_declaration=*/true);
3899         /* If that didn't work, try an identifier.  */
3900         if (!cp_parser_parse_definitely (parser))
3901           id = cp_parser_identifier (parser);
3902
3903         /* Don't process id if nested name specifier is invalid.  */
3904         if (scope == error_mark_node)
3905           return error_mark_node;
3906         /* If we look up a template-id in a non-dependent qualifying
3907            scope, there's no need to create a dependent type.  */
3908         else if (TREE_CODE (id) == TYPE_DECL
3909             && !dependent_type_p (parser->scope))
3910           type = TREE_TYPE (id);
3911         /* Create a TYPENAME_TYPE to represent the type to which the
3912            functional cast is being performed.  */
3913         else
3914           type = make_typename_type (parser->scope, id,
3915                                      typename_type,
3916                                      /*complain=*/1);
3917
3918         postfix_expression = cp_parser_functional_cast (parser, type);
3919       }
3920       break;
3921
3922     default:
3923       {
3924         tree type;
3925
3926         /* If the next thing is a simple-type-specifier, we may be
3927            looking at a functional cast.  We could also be looking at
3928            an id-expression.  So, we try the functional cast, and if
3929            that doesn't work we fall back to the primary-expression.  */
3930         cp_parser_parse_tentatively (parser);
3931         /* Look for the simple-type-specifier.  */
3932         type = cp_parser_simple_type_specifier (parser,
3933                                                 /*decl_specs=*/NULL,
3934                                                 CP_PARSER_FLAGS_NONE);
3935         /* Parse the cast itself.  */
3936         if (!cp_parser_error_occurred (parser))
3937           postfix_expression
3938             = cp_parser_functional_cast (parser, type);
3939         /* If that worked, we're done.  */
3940         if (cp_parser_parse_definitely (parser))
3941           break;
3942
3943         /* If the functional-cast didn't work out, try a
3944            compound-literal.  */
3945         if (cp_parser_allow_gnu_extensions_p (parser)
3946             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3947           {
3948             tree initializer_list = NULL_TREE;
3949             bool saved_in_type_id_in_expr_p;
3950
3951             cp_parser_parse_tentatively (parser);
3952             /* Consume the `('.  */
3953             cp_lexer_consume_token (parser->lexer);
3954             /* Parse the type.  */
3955             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3956             parser->in_type_id_in_expr_p = true;
3957             type = cp_parser_type_id (parser);
3958             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3959             /* Look for the `)'.  */
3960             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3961             /* Look for the `{'.  */
3962             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3963             /* If things aren't going well, there's no need to
3964                keep going.  */
3965             if (!cp_parser_error_occurred (parser))
3966               {
3967                 bool non_constant_p;
3968                 /* Parse the initializer-list.  */
3969                 initializer_list
3970                   = cp_parser_initializer_list (parser, &non_constant_p);
3971                 /* Allow a trailing `,'.  */
3972                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3973                   cp_lexer_consume_token (parser->lexer);
3974                 /* Look for the final `}'.  */
3975                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3976               }
3977             /* If that worked, we're definitely looking at a
3978                compound-literal expression.  */
3979             if (cp_parser_parse_definitely (parser))
3980               {
3981                 /* Warn the user that a compound literal is not
3982                    allowed in standard C++.  */
3983                 if (pedantic)
3984                   pedwarn ("ISO C++ forbids compound-literals");
3985                 /* Form the representation of the compound-literal.  */
3986                 postfix_expression
3987                   = finish_compound_literal (type, initializer_list);
3988                 break;
3989               }
3990           }
3991
3992         /* It must be a primary-expression.  */
3993         postfix_expression = cp_parser_primary_expression (parser,
3994                                                            cast_p,
3995                                                            &idk,
3996                                                            &qualifying_class);
3997       }
3998       break;
3999     }
4000
4001   /* If we were avoiding committing to the processing of a
4002      qualified-id until we knew whether or not we had a
4003      pointer-to-member, we now know.  */
4004   if (qualifying_class)
4005     {
4006       bool done;
4007
4008       /* Peek at the next token.  */
4009       token = cp_lexer_peek_token (parser->lexer);
4010       done = (token->type != CPP_OPEN_SQUARE
4011               && token->type != CPP_OPEN_PAREN
4012               && token->type != CPP_DOT
4013               && token->type != CPP_DEREF
4014               && token->type != CPP_PLUS_PLUS
4015               && token->type != CPP_MINUS_MINUS);
4016
4017       postfix_expression = finish_qualified_id_expr (qualifying_class,
4018                                                      postfix_expression,
4019                                                      done,
4020                                                      address_p);
4021       if (done)
4022         return postfix_expression;
4023     }
4024
4025   /* Keep looping until the postfix-expression is complete.  */
4026   while (true)
4027     {
4028       if (idk == CP_ID_KIND_UNQUALIFIED
4029           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4030           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4031         /* It is not a Koenig lookup function call.  */
4032         postfix_expression
4033           = unqualified_name_lookup_error (postfix_expression);
4034
4035       /* Peek at the next token.  */
4036       token = cp_lexer_peek_token (parser->lexer);
4037
4038       switch (token->type)
4039         {
4040         case CPP_OPEN_SQUARE:
4041           postfix_expression
4042             = cp_parser_postfix_open_square_expression (parser,
4043                                                         postfix_expression,
4044                                                         false);
4045           idk = CP_ID_KIND_NONE;
4046           break;
4047
4048         case CPP_OPEN_PAREN:
4049           /* postfix-expression ( expression-list [opt] ) */
4050           {
4051             bool koenig_p;
4052             tree args = (cp_parser_parenthesized_expression_list
4053                          (parser, false, 
4054                           /*cast_p=*/false,
4055                           /*non_constant_p=*/NULL));
4056
4057             if (args == error_mark_node)
4058               {
4059                 postfix_expression = error_mark_node;
4060                 break;
4061               }
4062
4063             /* Function calls are not permitted in
4064                constant-expressions.  */
4065             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4066                 && cp_parser_non_integral_constant_expression (parser,
4067                                                                "a function call"))
4068               {
4069                 postfix_expression = error_mark_node;
4070                 break;
4071               }
4072
4073             koenig_p = false;
4074             if (idk == CP_ID_KIND_UNQUALIFIED)
4075               {
4076                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4077                   {
4078                     if (args)
4079                       {
4080                         koenig_p = true;
4081                         postfix_expression
4082                           = perform_koenig_lookup (postfix_expression, args);
4083                       }
4084                     else
4085                       postfix_expression
4086                         = unqualified_fn_lookup_error (postfix_expression);
4087                   }
4088                 /* We do not perform argument-dependent lookup if
4089                    normal lookup finds a non-function, in accordance
4090                    with the expected resolution of DR 218.  */
4091                 else if (args && is_overloaded_fn (postfix_expression))
4092                   {
4093                     tree fn = get_first_fn (postfix_expression);
4094
4095                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4096                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4097
4098                     /* Only do argument dependent lookup if regular
4099                        lookup does not find a set of member functions.
4100                        [basic.lookup.koenig]/2a  */
4101                     if (!DECL_FUNCTION_MEMBER_P (fn))
4102                       {
4103                         koenig_p = true;
4104                         postfix_expression
4105                           = perform_koenig_lookup (postfix_expression, args);
4106                       }
4107                   }
4108               }
4109
4110             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4111               {
4112                 tree instance = TREE_OPERAND (postfix_expression, 0);
4113                 tree fn = TREE_OPERAND (postfix_expression, 1);
4114
4115                 if (processing_template_decl
4116                     && (type_dependent_expression_p (instance)
4117                         || (!BASELINK_P (fn)
4118                             && TREE_CODE (fn) != FIELD_DECL)
4119                         || type_dependent_expression_p (fn)
4120                         || any_type_dependent_arguments_p (args)))
4121                   {
4122                     postfix_expression
4123                       = build_min_nt (CALL_EXPR, postfix_expression,
4124                                       args, NULL_TREE);
4125                     break;
4126                   }
4127
4128                 if (BASELINK_P (fn))
4129                   postfix_expression
4130                     = (build_new_method_call
4131                        (instance, fn, args, NULL_TREE,
4132                         (idk == CP_ID_KIND_QUALIFIED
4133                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4134                 else
4135                   postfix_expression
4136                     = finish_call_expr (postfix_expression, args,
4137                                         /*disallow_virtual=*/false,
4138                                         /*koenig_p=*/false);
4139               }
4140             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4141                      || TREE_CODE (postfix_expression) == MEMBER_REF
4142                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4143               postfix_expression = (build_offset_ref_call_from_tree
4144                                     (postfix_expression, args));
4145             else if (idk == CP_ID_KIND_QUALIFIED)
4146               /* A call to a static class member, or a namespace-scope
4147                  function.  */
4148               postfix_expression
4149                 = finish_call_expr (postfix_expression, args,
4150                                     /*disallow_virtual=*/true,
4151                                     koenig_p);
4152             else
4153               /* All other function calls.  */
4154               postfix_expression
4155                 = finish_call_expr (postfix_expression, args,
4156                                     /*disallow_virtual=*/false,
4157                                     koenig_p);
4158
4159             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4160             idk = CP_ID_KIND_NONE;
4161           }
4162           break;
4163
4164         case CPP_DOT:
4165         case CPP_DEREF:
4166           /* postfix-expression . template [opt] id-expression
4167              postfix-expression . pseudo-destructor-name
4168              postfix-expression -> template [opt] id-expression
4169              postfix-expression -> pseudo-destructor-name */
4170
4171           /* Consume the `.' or `->' operator.  */
4172           cp_lexer_consume_token (parser->lexer);
4173
4174           postfix_expression
4175             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4176                                                       postfix_expression,
4177                                                       false, &idk);
4178           break;
4179
4180         case CPP_PLUS_PLUS:
4181           /* postfix-expression ++  */
4182           /* Consume the `++' token.  */
4183           cp_lexer_consume_token (parser->lexer);
4184           /* Generate a representation for the complete expression.  */
4185           postfix_expression
4186             = finish_increment_expr (postfix_expression,
4187                                      POSTINCREMENT_EXPR);
4188           /* Increments may not appear in constant-expressions.  */
4189           if (cp_parser_non_integral_constant_expression (parser,
4190                                                           "an increment"))
4191             postfix_expression = error_mark_node;
4192           idk = CP_ID_KIND_NONE;
4193           break;
4194
4195         case CPP_MINUS_MINUS:
4196           /* postfix-expression -- */
4197           /* Consume the `--' token.  */
4198           cp_lexer_consume_token (parser->lexer);
4199           /* Generate a representation for the complete expression.  */
4200           postfix_expression
4201             = finish_increment_expr (postfix_expression,
4202                                      POSTDECREMENT_EXPR);
4203           /* Decrements may not appear in constant-expressions.  */
4204           if (cp_parser_non_integral_constant_expression (parser,
4205                                                           "a decrement"))
4206             postfix_expression = error_mark_node;
4207           idk = CP_ID_KIND_NONE;
4208           break;
4209
4210         default:
4211           return postfix_expression;
4212         }
4213     }
4214
4215   /* We should never get here.  */
4216   gcc_unreachable ();
4217   return error_mark_node;
4218 }
4219
4220 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4221    by cp_parser_builtin_offsetof.  We're looking for
4222
4223      postfix-expression [ expression ]
4224
4225    FOR_OFFSETOF is set if we're being called in that context, which
4226    changes how we deal with integer constant expressions.  */
4227
4228 static tree
4229 cp_parser_postfix_open_square_expression (cp_parser *parser,
4230                                           tree postfix_expression,
4231                                           bool for_offsetof)
4232 {
4233   tree index;
4234
4235   /* Consume the `[' token.  */
4236   cp_lexer_consume_token (parser->lexer);
4237
4238   /* Parse the index expression.  */
4239   /* ??? For offsetof, there is a question of what to allow here.  If
4240      offsetof is not being used in an integral constant expression context,
4241      then we *could* get the right answer by computing the value at runtime.
4242      If we are in an integral constant expression context, then we might
4243      could accept any constant expression; hard to say without analysis.
4244      Rather than open the barn door too wide right away, allow only integer
4245      constant expressions here.  */
4246   if (for_offsetof)
4247     index = cp_parser_constant_expression (parser, false, NULL);
4248   else
4249     index = cp_parser_expression (parser, /*cast_p=*/false);
4250
4251   /* Look for the closing `]'.  */
4252   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4253
4254   /* Build the ARRAY_REF.  */
4255   postfix_expression = grok_array_decl (postfix_expression, index);
4256
4257   /* When not doing offsetof, array references are not permitted in
4258      constant-expressions.  */
4259   if (!for_offsetof
4260       && (cp_parser_non_integral_constant_expression
4261           (parser, "an array reference")))
4262     postfix_expression = error_mark_node;
4263
4264   return postfix_expression;
4265 }
4266
4267 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4268    by cp_parser_builtin_offsetof.  We're looking for
4269
4270      postfix-expression . template [opt] id-expression
4271      postfix-expression . pseudo-destructor-name
4272      postfix-expression -> template [opt] id-expression
4273      postfix-expression -> pseudo-destructor-name
4274
4275    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4276    limits what of the above we'll actually accept, but nevermind.
4277    TOKEN_TYPE is the "." or "->" token, which will already have been
4278    removed from the stream.  */
4279
4280 static tree
4281 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4282                                         enum cpp_ttype token_type,
4283                                         tree postfix_expression,
4284                                         bool for_offsetof, cp_id_kind *idk)
4285 {
4286   tree name;
4287   bool dependent_p;
4288   bool template_p;
4289   bool pseudo_destructor_p;
4290   tree scope = NULL_TREE;
4291
4292   /* If this is a `->' operator, dereference the pointer.  */
4293   if (token_type == CPP_DEREF)
4294     postfix_expression = build_x_arrow (postfix_expression);
4295   /* Check to see whether or not the expression is type-dependent.  */
4296   dependent_p = type_dependent_expression_p (postfix_expression);
4297   /* The identifier following the `->' or `.' is not qualified.  */
4298   parser->scope = NULL_TREE;
4299   parser->qualifying_scope = NULL_TREE;
4300   parser->object_scope = NULL_TREE;
4301   *idk = CP_ID_KIND_NONE;
4302   /* Enter the scope corresponding to the type of the object
4303      given by the POSTFIX_EXPRESSION.  */
4304   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4305     {
4306       scope = TREE_TYPE (postfix_expression);
4307       /* According to the standard, no expression should ever have
4308          reference type.  Unfortunately, we do not currently match
4309          the standard in this respect in that our internal representation
4310          of an expression may have reference type even when the standard
4311          says it does not.  Therefore, we have to manually obtain the
4312          underlying type here.  */
4313       scope = non_reference (scope);
4314       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4315       scope = complete_type_or_else (scope, NULL_TREE);
4316       /* Let the name lookup machinery know that we are processing a
4317          class member access expression.  */
4318       parser->context->object_type = scope;
4319       /* If something went wrong, we want to be able to discern that case,
4320          as opposed to the case where there was no SCOPE due to the type
4321          of expression being dependent.  */
4322       if (!scope)
4323         scope = error_mark_node;
4324       /* If the SCOPE was erroneous, make the various semantic analysis
4325          functions exit quickly -- and without issuing additional error
4326          messages.  */
4327       if (scope == error_mark_node)
4328         postfix_expression = error_mark_node;
4329     }
4330
4331   /* Assume this expression is not a pseudo-destructor access.  */
4332   pseudo_destructor_p = false;
4333
4334   /* If the SCOPE is a scalar type, then, if this is a valid program,
4335      we must be looking at a pseudo-destructor-name.  */
4336   if (scope && SCALAR_TYPE_P (scope))
4337     {
4338       tree s;
4339       tree type;
4340
4341       cp_parser_parse_tentatively (parser);
4342       /* Parse the pseudo-destructor-name.  */
4343       s = NULL_TREE;
4344       cp_parser_pseudo_destructor_name (parser, &s, &type);
4345       if (cp_parser_parse_definitely (parser))
4346         {
4347           pseudo_destructor_p = true;
4348           postfix_expression
4349             = finish_pseudo_destructor_expr (postfix_expression,
4350                                              s, TREE_TYPE (type));
4351         }
4352     }
4353
4354   if (!pseudo_destructor_p)
4355     {
4356       /* If the SCOPE is not a scalar type, we are looking at an
4357          ordinary class member access expression, rather than a
4358          pseudo-destructor-name.  */
4359       template_p = cp_parser_optional_template_keyword (parser);
4360       /* Parse the id-expression.  */
4361       name = cp_parser_id_expression (parser, template_p,
4362                                       /*check_dependency_p=*/true,
4363                                       /*template_p=*/NULL,
4364                                       /*declarator_p=*/false);
4365       /* In general, build a SCOPE_REF if the member name is qualified.
4366          However, if the name was not dependent and has already been
4367          resolved; there is no need to build the SCOPE_REF.  For example;
4368
4369              struct X { void f(); };
4370              template <typename T> void f(T* t) { t->X::f(); }
4371
4372          Even though "t" is dependent, "X::f" is not and has been resolved
4373          to a BASELINK; there is no need to include scope information.  */
4374
4375       /* But we do need to remember that there was an explicit scope for
4376          virtual function calls.  */
4377       if (parser->scope)
4378         *idk = CP_ID_KIND_QUALIFIED;
4379
4380       /* If the name is a template-id that names a type, we will get a
4381          TYPE_DECL here.  That is invalid code.  */
4382       if (TREE_CODE (name) == TYPE_DECL)
4383         {
4384           error ("invalid use of %qD", name);
4385           postfix_expression = error_mark_node;
4386         }
4387       else
4388         {
4389           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4390             {
4391               name = build_nt (SCOPE_REF, parser->scope, name);
4392               parser->scope = NULL_TREE;
4393               parser->qualifying_scope = NULL_TREE;
4394               parser->object_scope = NULL_TREE;
4395             }
4396           if (scope && name && BASELINK_P (name))
4397             adjust_result_of_qualified_name_lookup
4398               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4399           postfix_expression
4400             = finish_class_member_access_expr (postfix_expression, name);
4401         }
4402     }
4403
4404   /* We no longer need to look up names in the scope of the object on
4405      the left-hand side of the `.' or `->' operator.  */
4406   parser->context->object_type = NULL_TREE;
4407
4408   /* Outside of offsetof, these operators may not appear in
4409      constant-expressions.  */
4410   if (!for_offsetof
4411       && (cp_parser_non_integral_constant_expression
4412           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4413     postfix_expression = error_mark_node;
4414
4415   return postfix_expression;
4416 }
4417
4418 /* Parse a parenthesized expression-list.
4419
4420    expression-list:
4421      assignment-expression
4422      expression-list, assignment-expression
4423
4424    attribute-list:
4425      expression-list
4426      identifier
4427      identifier, expression-list
4428
4429    CAST_P is true if this expression is the target of a cast.
4430
4431    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4432    representation of an assignment-expression.  Note that a TREE_LIST
4433    is returned even if there is only a single expression in the list.
4434    error_mark_node is returned if the ( and or ) are
4435    missing. NULL_TREE is returned on no expressions. The parentheses
4436    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4437    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4438    indicates whether or not all of the expressions in the list were
4439    constant.  */
4440
4441 static tree
4442 cp_parser_parenthesized_expression_list (cp_parser* parser,
4443                                          bool is_attribute_list,
4444                                          bool cast_p,
4445                                          bool *non_constant_p)
4446 {
4447   tree expression_list = NULL_TREE;
4448   bool fold_expr_p = is_attribute_list;
4449   tree identifier = NULL_TREE;
4450
4451   /* Assume all the expressions will be constant.  */
4452   if (non_constant_p)
4453     *non_constant_p = false;
4454
4455   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4456     return error_mark_node;
4457
4458   /* Consume expressions until there are no more.  */
4459   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4460     while (true)
4461       {
4462         tree expr;
4463
4464         /* At the beginning of attribute lists, check to see if the
4465            next token is an identifier.  */
4466         if (is_attribute_list
4467             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4468           {
4469             cp_token *token;
4470
4471             /* Consume the identifier.  */
4472             token = cp_lexer_consume_token (parser->lexer);
4473             /* Save the identifier.  */
4474             identifier = token->value;
4475           }
4476         else
4477           {
4478             /* Parse the next assignment-expression.  */
4479             if (non_constant_p)
4480               {
4481                 bool expr_non_constant_p;
4482                 expr = (cp_parser_constant_expression
4483                         (parser, /*allow_non_constant_p=*/true,
4484                          &expr_non_constant_p));
4485                 if (expr_non_constant_p)
4486                   *non_constant_p = true;
4487               }
4488             else
4489               expr = cp_parser_assignment_expression (parser, cast_p);
4490
4491             if (fold_expr_p)
4492               expr = fold_non_dependent_expr (expr);
4493
4494              /* Add it to the list.  We add error_mark_node
4495                 expressions to the list, so that we can still tell if
4496                 the correct form for a parenthesized expression-list
4497                 is found. That gives better errors.  */
4498             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4499
4500             if (expr == error_mark_node)
4501               goto skip_comma;
4502           }
4503
4504         /* After the first item, attribute lists look the same as
4505            expression lists.  */
4506         is_attribute_list = false;
4507
4508       get_comma:;
4509         /* If the next token isn't a `,', then we are done.  */
4510         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4511           break;
4512
4513         /* Otherwise, consume the `,' and keep going.  */
4514         cp_lexer_consume_token (parser->lexer);
4515       }
4516
4517   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4518     {
4519       int ending;
4520
4521     skip_comma:;
4522       /* We try and resync to an unnested comma, as that will give the
4523          user better diagnostics.  */
4524       ending = cp_parser_skip_to_closing_parenthesis (parser,
4525                                                       /*recovering=*/true,
4526                                                       /*or_comma=*/true,
4527                                                       /*consume_paren=*/true);
4528       if (ending < 0)
4529         goto get_comma;
4530       if (!ending)
4531         return error_mark_node;
4532     }
4533
4534   /* We built up the list in reverse order so we must reverse it now.  */
4535   expression_list = nreverse (expression_list);
4536   if (identifier)
4537     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4538
4539   return expression_list;
4540 }
4541
4542 /* Parse a pseudo-destructor-name.
4543
4544    pseudo-destructor-name:
4545      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4546      :: [opt] nested-name-specifier template template-id :: ~ type-name
4547      :: [opt] nested-name-specifier [opt] ~ type-name
4548
4549    If either of the first two productions is used, sets *SCOPE to the
4550    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4551    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4552    or ERROR_MARK_NODE if the parse fails.  */
4553
4554 static void
4555 cp_parser_pseudo_destructor_name (cp_parser* parser,
4556                                   tree* scope,
4557                                   tree* type)
4558 {
4559   bool nested_name_specifier_p;
4560
4561   /* Assume that things will not work out.  */
4562   *type = error_mark_node;
4563
4564   /* Look for the optional `::' operator.  */
4565   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4566   /* Look for the optional nested-name-specifier.  */
4567   nested_name_specifier_p
4568     = (cp_parser_nested_name_specifier_opt (parser,
4569                                             /*typename_keyword_p=*/false,
4570                                             /*check_dependency_p=*/true,
4571                                             /*type_p=*/false,
4572                                             /*is_declaration=*/true)
4573        != NULL_TREE);
4574   /* Now, if we saw a nested-name-specifier, we might be doing the
4575      second production.  */
4576   if (nested_name_specifier_p
4577       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4578     {
4579       /* Consume the `template' keyword.  */
4580       cp_lexer_consume_token (parser->lexer);
4581       /* Parse the template-id.  */
4582       cp_parser_template_id (parser,
4583                              /*template_keyword_p=*/true,
4584                              /*check_dependency_p=*/false,
4585                              /*is_declaration=*/true);
4586       /* Look for the `::' token.  */
4587       cp_parser_require (parser, CPP_SCOPE, "`::'");
4588     }
4589   /* If the next token is not a `~', then there might be some
4590      additional qualification.  */
4591   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4592     {
4593       /* Look for the type-name.  */
4594       *scope = TREE_TYPE (cp_parser_type_name (parser));
4595
4596       if (*scope == error_mark_node)
4597         return;
4598
4599       /* If we don't have ::~, then something has gone wrong.  Since
4600          the only caller of this function is looking for something
4601          after `.' or `->' after a scalar type, most likely the
4602          program is trying to get a member of a non-aggregate
4603          type.  */
4604       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4605           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4606         {
4607           cp_parser_error (parser, "request for member of non-aggregate type");
4608           return;
4609         }
4610
4611       /* Look for the `::' token.  */
4612       cp_parser_require (parser, CPP_SCOPE, "`::'");
4613     }
4614   else
4615     *scope = NULL_TREE;
4616
4617   /* Look for the `~'.  */
4618   cp_parser_require (parser, CPP_COMPL, "`~'");
4619   /* Look for the type-name again.  We are not responsible for
4620      checking that it matches the first type-name.  */
4621   *type = cp_parser_type_name (parser);
4622 }
4623
4624 /* Parse a unary-expression.
4625
4626    unary-expression:
4627      postfix-expression
4628      ++ cast-expression
4629      -- cast-expression
4630      unary-operator cast-expression
4631      sizeof unary-expression
4632      sizeof ( type-id )
4633      new-expression
4634      delete-expression
4635
4636    GNU Extensions:
4637
4638    unary-expression:
4639      __extension__ cast-expression
4640      __alignof__ unary-expression
4641      __alignof__ ( type-id )
4642      __real__ cast-expression
4643      __imag__ cast-expression
4644      && identifier
4645
4646    ADDRESS_P is true iff the unary-expression is appearing as the
4647    operand of the `&' operator.   CAST_P is true if this expression is
4648    the target of a cast.
4649
4650    Returns a representation of the expression.  */
4651
4652 static tree
4653 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4654 {
4655   cp_token *token;
4656   enum tree_code unary_operator;
4657
4658   /* Peek at the next token.  */
4659   token = cp_lexer_peek_token (parser->lexer);
4660   /* Some keywords give away the kind of expression.  */
4661   if (token->type == CPP_KEYWORD)
4662     {
4663       enum rid keyword = token->keyword;
4664
4665       switch (keyword)
4666         {
4667         case RID_ALIGNOF:
4668         case RID_SIZEOF:
4669           {
4670             tree operand;
4671             enum tree_code op;
4672
4673             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4674             /* Consume the token.  */
4675             cp_lexer_consume_token (parser->lexer);
4676             /* Parse the operand.  */
4677             operand = cp_parser_sizeof_operand (parser, keyword);
4678
4679             if (TYPE_P (operand))
4680               return cxx_sizeof_or_alignof_type (operand, op, true);
4681             else
4682               return cxx_sizeof_or_alignof_expr (operand, op);
4683           }
4684
4685         case RID_NEW:
4686           return cp_parser_new_expression (parser);
4687
4688         case RID_DELETE:
4689           return cp_parser_delete_expression (parser);
4690
4691         case RID_EXTENSION:
4692           {
4693             /* The saved value of the PEDANTIC flag.  */
4694             int saved_pedantic;
4695             tree expr;
4696
4697             /* Save away the PEDANTIC flag.  */
4698             cp_parser_extension_opt (parser, &saved_pedantic);
4699             /* Parse the cast-expression.  */
4700             expr = cp_parser_simple_cast_expression (parser);
4701             /* Restore the PEDANTIC flag.  */
4702             pedantic = saved_pedantic;
4703
4704             return expr;
4705           }
4706
4707         case RID_REALPART:
4708         case RID_IMAGPART:
4709           {
4710             tree expression;
4711
4712             /* Consume the `__real__' or `__imag__' token.  */
4713             cp_lexer_consume_token (parser->lexer);
4714             /* Parse the cast-expression.  */
4715             expression = cp_parser_simple_cast_expression (parser);
4716             /* Create the complete representation.  */
4717             return build_x_unary_op ((keyword == RID_REALPART
4718                                       ? REALPART_EXPR : IMAGPART_EXPR),
4719                                      expression);
4720           }
4721           break;
4722
4723         default:
4724           break;
4725         }
4726     }
4727
4728   /* Look for the `:: new' and `:: delete', which also signal the
4729      beginning of a new-expression, or delete-expression,
4730      respectively.  If the next token is `::', then it might be one of
4731      these.  */
4732   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4733     {
4734       enum rid keyword;
4735
4736       /* See if the token after the `::' is one of the keywords in
4737          which we're interested.  */
4738       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4739       /* If it's `new', we have a new-expression.  */
4740       if (keyword == RID_NEW)
4741         return cp_parser_new_expression (parser);
4742       /* Similarly, for `delete'.  */
4743       else if (keyword == RID_DELETE)
4744         return cp_parser_delete_expression (parser);
4745     }
4746
4747   /* Look for a unary operator.  */
4748   unary_operator = cp_parser_unary_operator (token);
4749   /* The `++' and `--' operators can be handled similarly, even though
4750      they are not technically unary-operators in the grammar.  */
4751   if (unary_operator == ERROR_MARK)
4752     {
4753       if (token->type == CPP_PLUS_PLUS)
4754         unary_operator = PREINCREMENT_EXPR;
4755       else if (token->type == CPP_MINUS_MINUS)
4756         unary_operator = PREDECREMENT_EXPR;
4757       /* Handle the GNU address-of-label extension.  */
4758       else if (cp_parser_allow_gnu_extensions_p (parser)
4759                && token->type == CPP_AND_AND)
4760         {
4761           tree identifier;
4762
4763           /* Consume the '&&' token.  */
4764           cp_lexer_consume_token (parser->lexer);
4765           /* Look for the identifier.  */
4766           identifier = cp_parser_identifier (parser);
4767           /* Create an expression representing the address.  */
4768           return finish_label_address_expr (identifier);
4769         }
4770     }
4771   if (unary_operator != ERROR_MARK)
4772     {
4773       tree cast_expression;
4774       tree expression = error_mark_node;
4775       const char *non_constant_p = NULL;
4776
4777       /* Consume the operator token.  */
4778       token = cp_lexer_consume_token (parser->lexer);
4779       /* Parse the cast-expression.  */
4780       cast_expression
4781         = cp_parser_cast_expression (parser, 
4782                                      unary_operator == ADDR_EXPR,
4783                                      /*cast_p=*/false);
4784       /* Now, build an appropriate representation.  */
4785       switch (unary_operator)
4786         {
4787         case INDIRECT_REF:
4788           non_constant_p = "`*'";
4789           expression = build_x_indirect_ref (cast_expression, "unary *");
4790           break;
4791
4792         case ADDR_EXPR:
4793           non_constant_p = "`&'";
4794           /* Fall through.  */
4795         case BIT_NOT_EXPR:
4796           expression = build_x_unary_op (unary_operator, cast_expression);
4797           break;
4798
4799         case PREINCREMENT_EXPR:
4800         case PREDECREMENT_EXPR:
4801           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4802                             ? "`++'" : "`--'");
4803           /* Fall through.  */
4804         case CONVERT_EXPR:
4805         case NEGATE_EXPR:
4806         case TRUTH_NOT_EXPR:
4807           expression = finish_unary_op_expr (unary_operator, cast_expression);
4808           break;
4809
4810         default:
4811           gcc_unreachable ();
4812         }
4813
4814       if (non_constant_p
4815           && cp_parser_non_integral_constant_expression (parser,
4816                                                          non_constant_p))
4817         expression = error_mark_node;
4818
4819       return expression;
4820     }
4821
4822   return cp_parser_postfix_expression (parser, address_p, cast_p);
4823 }
4824
4825 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4826    unary-operator, the corresponding tree code is returned.  */
4827
4828 static enum tree_code
4829 cp_parser_unary_operator (cp_token* token)
4830 {
4831   switch (token->type)
4832     {
4833     case CPP_MULT:
4834       return INDIRECT_REF;
4835
4836     case CPP_AND:
4837       return ADDR_EXPR;
4838
4839     case CPP_PLUS:
4840       return CONVERT_EXPR;
4841
4842     case CPP_MINUS:
4843       return NEGATE_EXPR;
4844
4845     case CPP_NOT:
4846       return TRUTH_NOT_EXPR;
4847
4848     case CPP_COMPL:
4849       return BIT_NOT_EXPR;
4850
4851     default:
4852       return ERROR_MARK;
4853     }
4854 }
4855
4856 /* Parse a new-expression.
4857
4858    new-expression:
4859      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4860      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4861
4862    Returns a representation of the expression.  */
4863
4864 static tree
4865 cp_parser_new_expression (cp_parser* parser)
4866 {
4867   bool global_scope_p;
4868   tree placement;
4869   tree type;
4870   tree initializer;
4871   tree nelts;
4872
4873   /* Look for the optional `::' operator.  */
4874   global_scope_p
4875     = (cp_parser_global_scope_opt (parser,
4876                                    /*current_scope_valid_p=*/false)
4877        != NULL_TREE);
4878   /* Look for the `new' operator.  */
4879   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4880   /* There's no easy way to tell a new-placement from the
4881      `( type-id )' construct.  */
4882   cp_parser_parse_tentatively (parser);
4883   /* Look for a new-placement.  */
4884   placement = cp_parser_new_placement (parser);
4885   /* If that didn't work out, there's no new-placement.  */
4886   if (!cp_parser_parse_definitely (parser))
4887     placement = NULL_TREE;
4888
4889   /* If the next token is a `(', then we have a parenthesized
4890      type-id.  */
4891   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4892     {
4893       /* Consume the `('.  */
4894       cp_lexer_consume_token (parser->lexer);
4895       /* Parse the type-id.  */
4896       type = cp_parser_type_id (parser);
4897       /* Look for the closing `)'.  */
4898       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4899       /* There should not be a direct-new-declarator in this production,
4900          but GCC used to allowed this, so we check and emit a sensible error
4901          message for this case.  */
4902       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4903         {
4904           error ("array bound forbidden after parenthesized type-id");
4905           inform ("try removing the parentheses around the type-id");
4906           cp_parser_direct_new_declarator (parser);
4907         }
4908       nelts = NULL_TREE;
4909     }
4910   /* Otherwise, there must be a new-type-id.  */
4911   else
4912     type = cp_parser_new_type_id (parser, &nelts);
4913
4914   /* If the next token is a `(', then we have a new-initializer.  */
4915   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4916     initializer = cp_parser_new_initializer (parser);
4917   else
4918     initializer = NULL_TREE;
4919
4920   /* A new-expression may not appear in an integral constant
4921      expression.  */
4922   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4923     return error_mark_node;
4924
4925   /* Create a representation of the new-expression.  */
4926   return build_new (placement, type, nelts, initializer, global_scope_p);
4927 }
4928
4929 /* Parse a new-placement.
4930
4931    new-placement:
4932      ( expression-list )
4933
4934    Returns the same representation as for an expression-list.  */
4935
4936 static tree
4937 cp_parser_new_placement (cp_parser* parser)
4938 {
4939   tree expression_list;
4940
4941   /* Parse the expression-list.  */
4942   expression_list = (cp_parser_parenthesized_expression_list
4943                      (parser, false, /*cast_p=*/false,
4944                       /*non_constant_p=*/NULL));
4945
4946   return expression_list;
4947 }
4948
4949 /* Parse a new-type-id.
4950
4951    new-type-id:
4952      type-specifier-seq new-declarator [opt]
4953
4954    Returns the TYPE allocated.  If the new-type-id indicates an array
4955    type, *NELTS is set to the number of elements in the last array
4956    bound; the TYPE will not include the last array bound.  */
4957
4958 static tree
4959 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4960 {
4961   cp_decl_specifier_seq type_specifier_seq;
4962   cp_declarator *new_declarator;
4963   cp_declarator *declarator;
4964   cp_declarator *outer_declarator;
4965   const char *saved_message;
4966   tree type;
4967
4968   /* The type-specifier sequence must not contain type definitions.
4969      (It cannot contain declarations of new types either, but if they
4970      are not definitions we will catch that because they are not
4971      complete.)  */
4972   saved_message = parser->type_definition_forbidden_message;
4973   parser->type_definition_forbidden_message
4974     = "types may not be defined in a new-type-id";
4975   /* Parse the type-specifier-seq.  */
4976   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
4977                                 &type_specifier_seq);
4978   /* Restore the old message.  */
4979   parser->type_definition_forbidden_message = saved_message;
4980   /* Parse the new-declarator.  */
4981   new_declarator = cp_parser_new_declarator_opt (parser);
4982
4983   /* Determine the number of elements in the last array dimension, if
4984      any.  */
4985   *nelts = NULL_TREE;
4986   /* Skip down to the last array dimension.  */
4987   declarator = new_declarator;
4988   outer_declarator = NULL;
4989   while (declarator && (declarator->kind == cdk_pointer
4990                         || declarator->kind == cdk_ptrmem))
4991     {
4992       outer_declarator = declarator;
4993       declarator = declarator->declarator;
4994     }
4995   while (declarator
4996          && declarator->kind == cdk_array
4997          && declarator->declarator
4998          && declarator->declarator->kind == cdk_array)
4999     {
5000       outer_declarator = declarator;
5001       declarator = declarator->declarator;
5002     }
5003
5004   if (declarator && declarator->kind == cdk_array)
5005     {
5006       *nelts = declarator->u.array.bounds;
5007       if (*nelts == error_mark_node)
5008         *nelts = integer_one_node;
5009       
5010       if (outer_declarator)
5011         outer_declarator->declarator = declarator->declarator;
5012       else
5013         new_declarator = NULL;
5014     }
5015
5016   type = groktypename (&type_specifier_seq, new_declarator);
5017   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5018     {
5019       *nelts = array_type_nelts_top (type);
5020       type = TREE_TYPE (type);
5021     }
5022   return type;
5023 }
5024
5025 /* Parse an (optional) new-declarator.
5026
5027    new-declarator:
5028      ptr-operator new-declarator [opt]
5029      direct-new-declarator
5030
5031    Returns the declarator.  */
5032
5033 static cp_declarator *
5034 cp_parser_new_declarator_opt (cp_parser* parser)
5035 {
5036   enum tree_code code;
5037   tree type;
5038   cp_cv_quals cv_quals;
5039
5040   /* We don't know if there's a ptr-operator next, or not.  */
5041   cp_parser_parse_tentatively (parser);
5042   /* Look for a ptr-operator.  */
5043   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5044   /* If that worked, look for more new-declarators.  */
5045   if (cp_parser_parse_definitely (parser))
5046     {
5047       cp_declarator *declarator;
5048
5049       /* Parse another optional declarator.  */
5050       declarator = cp_parser_new_declarator_opt (parser);
5051
5052       /* Create the representation of the declarator.  */
5053       if (type)
5054         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5055       else if (code == INDIRECT_REF)
5056         declarator = make_pointer_declarator (cv_quals, declarator);
5057       else
5058         declarator = make_reference_declarator (cv_quals, declarator);
5059
5060       return declarator;
5061     }
5062
5063   /* If the next token is a `[', there is a direct-new-declarator.  */
5064   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5065     return cp_parser_direct_new_declarator (parser);
5066
5067   return NULL;
5068 }
5069
5070 /* Parse a direct-new-declarator.
5071
5072    direct-new-declarator:
5073      [ expression ]
5074      direct-new-declarator [constant-expression]
5075
5076    */
5077
5078 static cp_declarator *
5079 cp_parser_direct_new_declarator (cp_parser* parser)
5080 {
5081   cp_declarator *declarator = NULL;
5082
5083   while (true)
5084     {
5085       tree expression;
5086
5087       /* Look for the opening `['.  */
5088       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5089       /* The first expression is not required to be constant.  */
5090       if (!declarator)
5091         {
5092           expression = cp_parser_expression (parser, /*cast_p=*/false);
5093           /* The standard requires that the expression have integral
5094              type.  DR 74 adds enumeration types.  We believe that the
5095              real intent is that these expressions be handled like the
5096              expression in a `switch' condition, which also allows
5097              classes with a single conversion to integral or
5098              enumeration type.  */
5099           if (!processing_template_decl)
5100             {
5101               expression
5102                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5103                                               expression,
5104                                               /*complain=*/true);
5105               if (!expression)
5106                 {
5107                   error ("expression in new-declarator must have integral "
5108                          "or enumeration type");
5109                   expression = error_mark_node;
5110                 }
5111             }
5112         }
5113       /* But all the other expressions must be.  */
5114       else
5115         expression
5116           = cp_parser_constant_expression (parser,
5117                                            /*allow_non_constant=*/false,
5118                                            NULL);
5119       /* Look for the closing `]'.  */
5120       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5121
5122       /* Add this bound to the declarator.  */
5123       declarator = make_array_declarator (declarator, expression);
5124
5125       /* If the next token is not a `[', then there are no more
5126          bounds.  */
5127       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5128         break;
5129     }
5130
5131   return declarator;
5132 }
5133
5134 /* Parse a new-initializer.
5135
5136    new-initializer:
5137      ( expression-list [opt] )
5138
5139    Returns a representation of the expression-list.  If there is no
5140    expression-list, VOID_ZERO_NODE is returned.  */
5141
5142 static tree
5143 cp_parser_new_initializer (cp_parser* parser)
5144 {
5145   tree expression_list;
5146
5147   expression_list = (cp_parser_parenthesized_expression_list
5148                      (parser, false, /*cast_p=*/false,
5149                       /*non_constant_p=*/NULL));
5150   if (!expression_list)
5151     expression_list = void_zero_node;
5152
5153   return expression_list;
5154 }
5155
5156 /* Parse a delete-expression.
5157
5158    delete-expression:
5159      :: [opt] delete cast-expression
5160      :: [opt] delete [ ] cast-expression
5161
5162    Returns a representation of the expression.  */
5163
5164 static tree
5165 cp_parser_delete_expression (cp_parser* parser)
5166 {
5167   bool global_scope_p;
5168   bool array_p;
5169   tree expression;
5170
5171   /* Look for the optional `::' operator.  */
5172   global_scope_p
5173     = (cp_parser_global_scope_opt (parser,
5174                                    /*current_scope_valid_p=*/false)
5175        != NULL_TREE);
5176   /* Look for the `delete' keyword.  */
5177   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5178   /* See if the array syntax is in use.  */
5179   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5180     {
5181       /* Consume the `[' token.  */
5182       cp_lexer_consume_token (parser->lexer);
5183       /* Look for the `]' token.  */
5184       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5185       /* Remember that this is the `[]' construct.  */
5186       array_p = true;
5187     }
5188   else
5189     array_p = false;
5190
5191   /* Parse the cast-expression.  */
5192   expression = cp_parser_simple_cast_expression (parser);
5193
5194   /* A delete-expression may not appear in an integral constant
5195      expression.  */
5196   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5197     return error_mark_node;
5198
5199   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5200 }
5201
5202 /* Parse a cast-expression.
5203
5204    cast-expression:
5205      unary-expression
5206      ( type-id ) cast-expression
5207
5208    ADDRESS_P is true iff the unary-expression is appearing as the
5209    operand of the `&' operator.   CAST_P is true if this expression is
5210    the target of a cast.
5211
5212    Returns a representation of the expression.  */
5213
5214 static tree
5215 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5216 {
5217   /* If it's a `(', then we might be looking at a cast.  */
5218   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5219     {
5220       tree type = NULL_TREE;
5221       tree expr = NULL_TREE;
5222       bool compound_literal_p;
5223       const char *saved_message;
5224
5225       /* There's no way to know yet whether or not this is a cast.
5226          For example, `(int (3))' is a unary-expression, while `(int)
5227          3' is a cast.  So, we resort to parsing tentatively.  */
5228       cp_parser_parse_tentatively (parser);
5229       /* Types may not be defined in a cast.  */
5230       saved_message = parser->type_definition_forbidden_message;
5231       parser->type_definition_forbidden_message
5232         = "types may not be defined in casts";
5233       /* Consume the `('.  */
5234       cp_lexer_consume_token (parser->lexer);
5235       /* A very tricky bit is that `(struct S) { 3 }' is a
5236          compound-literal (which we permit in C++ as an extension).
5237          But, that construct is not a cast-expression -- it is a
5238          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5239          is legal; if the compound-literal were a cast-expression,
5240          you'd need an extra set of parentheses.)  But, if we parse
5241          the type-id, and it happens to be a class-specifier, then we
5242          will commit to the parse at that point, because we cannot
5243          undo the action that is done when creating a new class.  So,
5244          then we cannot back up and do a postfix-expression.
5245
5246          Therefore, we scan ahead to the closing `)', and check to see
5247          if the token after the `)' is a `{'.  If so, we are not
5248          looking at a cast-expression.
5249
5250          Save tokens so that we can put them back.  */
5251       cp_lexer_save_tokens (parser->lexer);
5252       /* Skip tokens until the next token is a closing parenthesis.
5253          If we find the closing `)', and the next token is a `{', then
5254          we are looking at a compound-literal.  */
5255       compound_literal_p
5256         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5257                                                   /*consume_paren=*/true)
5258            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5259       /* Roll back the tokens we skipped.  */
5260       cp_lexer_rollback_tokens (parser->lexer);
5261       /* If we were looking at a compound-literal, simulate an error
5262          so that the call to cp_parser_parse_definitely below will
5263          fail.  */
5264       if (compound_literal_p)
5265         cp_parser_simulate_error (parser);
5266       else
5267         {
5268           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5269           parser->in_type_id_in_expr_p = true;
5270           /* Look for the type-id.  */
5271           type = cp_parser_type_id (parser);
5272           /* Look for the closing `)'.  */
5273           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5274           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5275         }
5276
5277       /* Restore the saved message.  */
5278       parser->type_definition_forbidden_message = saved_message;
5279
5280       /* If ok so far, parse the dependent expression. We cannot be
5281          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5282          ctor of T, but looks like a cast to function returning T
5283          without a dependent expression.  */
5284       if (!cp_parser_error_occurred (parser))
5285         expr = cp_parser_cast_expression (parser, 
5286                                           /*address_p=*/false,
5287                                           /*cast_p=*/true);
5288
5289       if (cp_parser_parse_definitely (parser))
5290         {
5291           /* Warn about old-style casts, if so requested.  */
5292           if (warn_old_style_cast
5293               && !in_system_header
5294               && !VOID_TYPE_P (type)
5295               && current_lang_name != lang_name_c)
5296             warning ("use of old-style cast");
5297
5298           /* Only type conversions to integral or enumeration types
5299              can be used in constant-expressions.  */
5300           if (parser->integral_constant_expression_p
5301               && !dependent_type_p (type)
5302               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5303               && (cp_parser_non_integral_constant_expression
5304                   (parser,
5305                    "a cast to a type other than an integral or "
5306                    "enumeration type")))
5307             return error_mark_node;
5308
5309           /* Perform the cast.  */
5310           expr = build_c_cast (type, expr);
5311           return expr;
5312         }
5313     }
5314
5315   /* If we get here, then it's not a cast, so it must be a
5316      unary-expression.  */
5317   return cp_parser_unary_expression (parser, address_p, cast_p);
5318 }
5319
5320 /* Parse a binary expression of the general form:
5321
5322    pm-expression:
5323      cast-expression
5324      pm-expression .* cast-expression
5325      pm-expression ->* cast-expression
5326
5327    multiplicative-expression:
5328      pm-expression
5329      multiplicative-expression * pm-expression
5330      multiplicative-expression / pm-expression
5331      multiplicative-expression % pm-expression
5332
5333    additive-expression:
5334      multiplicative-expression
5335      additive-expression + multiplicative-expression
5336      additive-expression - multiplicative-expression
5337
5338    shift-expression:
5339      additive-expression
5340      shift-expression << additive-expression
5341      shift-expression >> additive-expression
5342
5343    relational-expression:
5344      shift-expression
5345      relational-expression < shift-expression
5346      relational-expression > shift-expression
5347      relational-expression <= shift-expression
5348      relational-expression >= shift-expression
5349
5350   GNU Extension:
5351   
5352    relational-expression:
5353      relational-expression <? shift-expression
5354      relational-expression >? shift-expression
5355
5356    equality-expression:
5357      relational-expression
5358      equality-expression == relational-expression
5359      equality-expression != relational-expression
5360
5361    and-expression:
5362      equality-expression
5363      and-expression & equality-expression
5364
5365    exclusive-or-expression:
5366      and-expression
5367      exclusive-or-expression ^ and-expression
5368
5369    inclusive-or-expression:
5370      exclusive-or-expression
5371      inclusive-or-expression | exclusive-or-expression
5372
5373    logical-and-expression:
5374      inclusive-or-expression
5375      logical-and-expression && inclusive-or-expression
5376
5377    logical-or-expression:
5378      logical-and-expression
5379      logical-or-expression || logical-and-expression
5380
5381    All these are implemented with a single function like:
5382
5383    binary-expression:
5384      simple-cast-expression
5385      binary-expression <token> binary-expression
5386
5387    CAST_P is true if this expression is the target of a cast.
5388
5389    The binops_by_token map is used to get the tree codes for each <token> type.
5390    binary-expressions are associated according to a precedence table.  */
5391
5392 #define TOKEN_PRECEDENCE(token) \
5393   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5394    ? PREC_NOT_OPERATOR \
5395    : binops_by_token[token->type].prec)
5396
5397 static tree
5398 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5399 {
5400   cp_parser_expression_stack stack;
5401   cp_parser_expression_stack_entry *sp = &stack[0];
5402   tree lhs, rhs;
5403   cp_token *token;
5404   enum tree_code tree_type;
5405   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5406   bool overloaded_p;
5407
5408   /* Parse the first expression.  */
5409   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5410
5411   for (;;)
5412     {
5413       /* Get an operator token.  */
5414       token = cp_lexer_peek_token (parser->lexer);
5415       if (token->type == CPP_MIN || token->type == CPP_MAX)
5416         cp_parser_warn_min_max ();
5417
5418       new_prec = TOKEN_PRECEDENCE (token);
5419
5420       /* Popping an entry off the stack means we completed a subexpression:
5421          - either we found a token which is not an operator (`>' where it is not
5422            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5423            will happen repeatedly;
5424          - or, we found an operator which has lower priority.  This is the case 
5425            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5426            parsing `3 * 4'.  */
5427       if (new_prec <= prec)
5428         {
5429           if (sp == stack)
5430             break;
5431           else
5432             goto pop;
5433         }
5434
5435      get_rhs:
5436       tree_type = binops_by_token[token->type].tree_type;
5437
5438       /* We used the operator token.  */
5439       cp_lexer_consume_token (parser->lexer);
5440
5441       /* Extract another operand.  It may be the RHS of this expression
5442          or the LHS of a new, higher priority expression.  */
5443       rhs = cp_parser_simple_cast_expression (parser);
5444
5445       /* Get another operator token.  Look up its precedence to avoid
5446          building a useless (immediately popped) stack entry for common
5447          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5448       token = cp_lexer_peek_token (parser->lexer);
5449       lookahead_prec = TOKEN_PRECEDENCE (token);
5450       if (lookahead_prec > new_prec)
5451         {
5452           /* ... and prepare to parse the RHS of the new, higher priority
5453              expression.  Since precedence levels on the stack are
5454              monotonically increasing, we do not have to care about
5455              stack overflows.  */
5456           sp->prec = prec;
5457           sp->tree_type = tree_type;
5458           sp->lhs = lhs;
5459           sp++;
5460           lhs = rhs;
5461           prec = new_prec;
5462           new_prec = lookahead_prec;
5463           goto get_rhs;
5464
5465          pop:
5466           /* If the stack is not empty, we have parsed into LHS the right side
5467              (`4' in the example above) of an expression we had suspended.
5468              We can use the information on the stack to recover the LHS (`3') 
5469              from the stack together with the tree code (`MULT_EXPR'), and
5470              the precedence of the higher level subexpression
5471              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5472              which will be used to actually build the additive expression.  */
5473           --sp;
5474           prec = sp->prec;
5475           tree_type = sp->tree_type;
5476           rhs = lhs;
5477           lhs = sp->lhs;
5478         }
5479
5480       overloaded_p = false;
5481       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5482
5483       /* If the binary operator required the use of an overloaded operator,
5484          then this expression cannot be an integral constant-expression.
5485          An overloaded operator can be used even if both operands are
5486          otherwise permissible in an integral constant-expression if at
5487          least one of the operands is of enumeration type.  */
5488
5489       if (overloaded_p
5490           && (cp_parser_non_integral_constant_expression 
5491               (parser, "calls to overloaded operators")))
5492         return error_mark_node;
5493     }
5494
5495   return lhs;
5496 }
5497
5498
5499 /* Parse the `? expression : assignment-expression' part of a
5500    conditional-expression.  The LOGICAL_OR_EXPR is the
5501    logical-or-expression that started the conditional-expression.
5502    Returns a representation of the entire conditional-expression.
5503
5504    This routine is used by cp_parser_assignment_expression.
5505
5506      ? expression : assignment-expression
5507
5508    GNU Extensions:
5509
5510      ? : assignment-expression */
5511
5512 static tree
5513 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5514 {
5515   tree expr;
5516   tree assignment_expr;
5517
5518   /* Consume the `?' token.  */
5519   cp_lexer_consume_token (parser->lexer);
5520   if (cp_parser_allow_gnu_extensions_p (parser)
5521       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5522     /* Implicit true clause.  */
5523     expr = NULL_TREE;
5524   else
5525     /* Parse the expression.  */
5526     expr = cp_parser_expression (parser, /*cast_p=*/false);
5527
5528   /* The next token should be a `:'.  */
5529   cp_parser_require (parser, CPP_COLON, "`:'");
5530   /* Parse the assignment-expression.  */
5531   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5532
5533   /* Build the conditional-expression.  */
5534   return build_x_conditional_expr (logical_or_expr,
5535                                    expr,
5536                                    assignment_expr);
5537 }
5538
5539 /* Parse an assignment-expression.
5540
5541    assignment-expression:
5542      conditional-expression
5543      logical-or-expression assignment-operator assignment_expression
5544      throw-expression
5545
5546    CAST_P is true if this expression is the target of a cast.
5547
5548    Returns a representation for the expression.  */
5549
5550 static tree
5551 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5552 {
5553   tree expr;
5554
5555   /* If the next token is the `throw' keyword, then we're looking at
5556      a throw-expression.  */
5557   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5558     expr = cp_parser_throw_expression (parser);
5559   /* Otherwise, it must be that we are looking at a
5560      logical-or-expression.  */
5561   else
5562     {
5563       /* Parse the binary expressions (logical-or-expression).  */
5564       expr = cp_parser_binary_expression (parser, cast_p);
5565       /* If the next token is a `?' then we're actually looking at a
5566          conditional-expression.  */
5567       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5568         return cp_parser_question_colon_clause (parser, expr);
5569       else
5570         {
5571           enum tree_code assignment_operator;
5572
5573           /* If it's an assignment-operator, we're using the second
5574              production.  */
5575           assignment_operator
5576             = cp_parser_assignment_operator_opt (parser);
5577           if (assignment_operator != ERROR_MARK)
5578             {
5579               tree rhs;
5580
5581               /* Parse the right-hand side of the assignment.  */
5582               rhs = cp_parser_assignment_expression (parser, cast_p);
5583               /* An assignment may not appear in a
5584                  constant-expression.  */
5585               if (cp_parser_non_integral_constant_expression (parser,
5586                                                               "an assignment"))
5587                 return error_mark_node;
5588               /* Build the assignment expression.  */
5589               expr = build_x_modify_expr (expr,
5590                                           assignment_operator,
5591                                           rhs);
5592             }
5593         }
5594     }
5595
5596   return expr;
5597 }
5598
5599 /* Parse an (optional) assignment-operator.
5600
5601    assignment-operator: one of
5602      = *= /= %= += -= >>= <<= &= ^= |=
5603
5604    GNU Extension:
5605
5606    assignment-operator: one of
5607      <?= >?=
5608
5609    If the next token is an assignment operator, the corresponding tree
5610    code is returned, and the token is consumed.  For example, for
5611    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5612    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5613    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5614    operator, ERROR_MARK is returned.  */
5615
5616 static enum tree_code
5617 cp_parser_assignment_operator_opt (cp_parser* parser)
5618 {
5619   enum tree_code op;
5620   cp_token *token;
5621
5622   /* Peek at the next toen.  */
5623   token = cp_lexer_peek_token (parser->lexer);
5624
5625   switch (token->type)
5626     {
5627     case CPP_EQ:
5628       op = NOP_EXPR;
5629       break;
5630
5631     case CPP_MULT_EQ:
5632       op = MULT_EXPR;
5633       break;
5634
5635     case CPP_DIV_EQ:
5636       op = TRUNC_DIV_EXPR;
5637       break;
5638
5639     case CPP_MOD_EQ:
5640       op = TRUNC_MOD_EXPR;
5641       break;
5642
5643     case CPP_PLUS_EQ:
5644       op = PLUS_EXPR;
5645       break;
5646
5647     case CPP_MINUS_EQ:
5648       op = MINUS_EXPR;
5649       break;
5650
5651     case CPP_RSHIFT_EQ:
5652       op = RSHIFT_EXPR;
5653       break;
5654
5655     case CPP_LSHIFT_EQ:
5656       op = LSHIFT_EXPR;
5657       break;
5658
5659     case CPP_AND_EQ:
5660       op = BIT_AND_EXPR;
5661       break;
5662
5663     case CPP_XOR_EQ:
5664       op = BIT_XOR_EXPR;
5665       break;
5666
5667     case CPP_OR_EQ:
5668       op = BIT_IOR_EXPR;
5669       break;
5670
5671     case CPP_MIN_EQ:
5672       op = MIN_EXPR;
5673       cp_parser_warn_min_max ();
5674       break;
5675
5676     case CPP_MAX_EQ:
5677       op = MAX_EXPR;
5678       cp_parser_warn_min_max ();
5679       break;
5680
5681     default:
5682       /* Nothing else is an assignment operator.  */
5683       op = ERROR_MARK;
5684     }
5685
5686   /* If it was an assignment operator, consume it.  */
5687   if (op != ERROR_MARK)
5688     cp_lexer_consume_token (parser->lexer);
5689
5690   return op;
5691 }
5692
5693 /* Parse an expression.
5694
5695    expression:
5696      assignment-expression
5697      expression , assignment-expression
5698
5699    CAST_P is true if this expression is the target of a cast.
5700
5701    Returns a representation of the expression.  */
5702
5703 static tree
5704 cp_parser_expression (cp_parser* parser, bool cast_p)
5705 {
5706   tree expression = NULL_TREE;
5707
5708   while (true)
5709     {
5710       tree assignment_expression;
5711
5712       /* Parse the next assignment-expression.  */
5713       assignment_expression
5714         = cp_parser_assignment_expression (parser, cast_p);
5715       /* If this is the first assignment-expression, we can just
5716          save it away.  */
5717       if (!expression)
5718         expression = assignment_expression;
5719       else
5720         expression = build_x_compound_expr (expression,
5721                                             assignment_expression);
5722       /* If the next token is not a comma, then we are done with the
5723          expression.  */
5724       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5725         break;
5726       /* Consume the `,'.  */
5727       cp_lexer_consume_token (parser->lexer);
5728       /* A comma operator cannot appear in a constant-expression.  */
5729       if (cp_parser_non_integral_constant_expression (parser,
5730                                                       "a comma operator"))
5731         expression = error_mark_node;
5732     }
5733
5734   return expression;
5735 }
5736
5737 /* Parse a constant-expression.
5738
5739    constant-expression:
5740      conditional-expression
5741
5742   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5743   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5744   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5745   is false, NON_CONSTANT_P should be NULL.  */
5746
5747 static tree
5748 cp_parser_constant_expression (cp_parser* parser,
5749                                bool allow_non_constant_p,
5750                                bool *non_constant_p)
5751 {
5752   bool saved_integral_constant_expression_p;
5753   bool saved_allow_non_integral_constant_expression_p;
5754   bool saved_non_integral_constant_expression_p;
5755   tree expression;
5756
5757   /* It might seem that we could simply parse the
5758      conditional-expression, and then check to see if it were
5759      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5760      one that the compiler can figure out is constant, possibly after
5761      doing some simplifications or optimizations.  The standard has a
5762      precise definition of constant-expression, and we must honor
5763      that, even though it is somewhat more restrictive.
5764
5765      For example:
5766
5767        int i[(2, 3)];
5768
5769      is not a legal declaration, because `(2, 3)' is not a
5770      constant-expression.  The `,' operator is forbidden in a
5771      constant-expression.  However, GCC's constant-folding machinery
5772      will fold this operation to an INTEGER_CST for `3'.  */
5773
5774   /* Save the old settings.  */
5775   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5776   saved_allow_non_integral_constant_expression_p
5777     = parser->allow_non_integral_constant_expression_p;
5778   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5779   /* We are now parsing a constant-expression.  */
5780   parser->integral_constant_expression_p = true;
5781   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5782   parser->non_integral_constant_expression_p = false;
5783   /* Although the grammar says "conditional-expression", we parse an
5784      "assignment-expression", which also permits "throw-expression"
5785      and the use of assignment operators.  In the case that
5786      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5787      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5788      actually essential that we look for an assignment-expression.
5789      For example, cp_parser_initializer_clauses uses this function to
5790      determine whether a particular assignment-expression is in fact
5791      constant.  */
5792   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5793   /* Restore the old settings.  */
5794   parser->integral_constant_expression_p 
5795     = saved_integral_constant_expression_p;
5796   parser->allow_non_integral_constant_expression_p
5797     = saved_allow_non_integral_constant_expression_p;
5798   if (allow_non_constant_p)
5799     *non_constant_p = parser->non_integral_constant_expression_p;
5800   else if (parser->non_integral_constant_expression_p)
5801     expression = error_mark_node;
5802   parser->non_integral_constant_expression_p 
5803     = saved_non_integral_constant_expression_p;
5804
5805   return expression;
5806 }
5807
5808 /* Parse __builtin_offsetof.
5809
5810    offsetof-expression:
5811      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5812
5813    offsetof-member-designator:
5814      id-expression
5815      | offsetof-member-designator "." id-expression
5816      | offsetof-member-designator "[" expression "]"
5817 */
5818
5819 static tree
5820 cp_parser_builtin_offsetof (cp_parser *parser)
5821 {
5822   int save_ice_p, save_non_ice_p;
5823   tree type, expr;
5824   cp_id_kind dummy;
5825
5826   /* We're about to accept non-integral-constant things, but will
5827      definitely yield an integral constant expression.  Save and
5828      restore these values around our local parsing.  */
5829   save_ice_p = parser->integral_constant_expression_p;
5830   save_non_ice_p = parser->non_integral_constant_expression_p;
5831
5832   /* Consume the "__builtin_offsetof" token.  */
5833   cp_lexer_consume_token (parser->lexer);
5834   /* Consume the opening `('.  */
5835   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5836   /* Parse the type-id.  */
5837   type = cp_parser_type_id (parser);
5838   /* Look for the `,'.  */
5839   cp_parser_require (parser, CPP_COMMA, "`,'");
5840
5841   /* Build the (type *)null that begins the traditional offsetof macro.  */
5842   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5843
5844   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5845   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5846                                                  true, &dummy);
5847   while (true)
5848     {
5849       cp_token *token = cp_lexer_peek_token (parser->lexer);
5850       switch (token->type)
5851         {
5852         case CPP_OPEN_SQUARE:
5853           /* offsetof-member-designator "[" expression "]" */
5854           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5855           break;
5856
5857         case CPP_DOT:
5858           /* offsetof-member-designator "." identifier */
5859           cp_lexer_consume_token (parser->lexer);
5860           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5861                                                          true, &dummy);
5862           break;
5863
5864         case CPP_CLOSE_PAREN:
5865           /* Consume the ")" token.  */
5866           cp_lexer_consume_token (parser->lexer);
5867           goto success;
5868
5869         default:
5870           /* Error.  We know the following require will fail, but
5871              that gives the proper error message.  */
5872           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5873           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5874           expr = error_mark_node;
5875           goto failure;
5876         }
5877     }
5878
5879  success:
5880   /* If we're processing a template, we can't finish the semantics yet.
5881      Otherwise we can fold the entire expression now.  */
5882   if (processing_template_decl)
5883     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5884   else
5885     expr = fold_offsetof (expr);
5886
5887  failure:
5888   parser->integral_constant_expression_p = save_ice_p;
5889   parser->non_integral_constant_expression_p = save_non_ice_p;
5890
5891   return expr;
5892 }
5893
5894 /* Statements [gram.stmt.stmt]  */
5895
5896 /* Parse a statement.
5897
5898    statement:
5899      labeled-statement
5900      expression-statement
5901      compound-statement
5902      selection-statement
5903      iteration-statement
5904      jump-statement
5905      declaration-statement
5906      try-block  */
5907
5908 static void
5909 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5910 {
5911   tree statement;
5912   cp_token *token;
5913   location_t statement_location;
5914
5915   /* There is no statement yet.  */
5916   statement = NULL_TREE;
5917   /* Peek at the next token.  */
5918   token = cp_lexer_peek_token (parser->lexer);
5919   /* Remember the location of the first token in the statement.  */
5920   statement_location = token->location;
5921   /* If this is a keyword, then that will often determine what kind of
5922      statement we have.  */
5923   if (token->type == CPP_KEYWORD)
5924     {
5925       enum rid keyword = token->keyword;
5926
5927       switch (keyword)
5928         {
5929         case RID_CASE:
5930         case RID_DEFAULT:
5931           statement = cp_parser_labeled_statement (parser,
5932                                                    in_statement_expr);
5933           break;
5934
5935         case RID_IF:
5936         case RID_SWITCH:
5937           statement = cp_parser_selection_statement (parser);
5938           break;
5939
5940         case RID_WHILE:
5941         case RID_DO:
5942         case RID_FOR:
5943           statement = cp_parser_iteration_statement (parser);
5944           break;
5945
5946         case RID_BREAK:
5947         case RID_CONTINUE:
5948         case RID_RETURN:
5949         case RID_GOTO:
5950           statement = cp_parser_jump_statement (parser);
5951           break;
5952
5953         case RID_TRY:
5954           statement = cp_parser_try_block (parser);
5955           break;
5956
5957         default:
5958           /* It might be a keyword like `int' that can start a
5959              declaration-statement.  */
5960           break;
5961         }
5962     }
5963   else if (token->type == CPP_NAME)
5964     {
5965       /* If the next token is a `:', then we are looking at a
5966          labeled-statement.  */
5967       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5968       if (token->type == CPP_COLON)
5969         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5970     }
5971   /* Anything that starts with a `{' must be a compound-statement.  */
5972   else if (token->type == CPP_OPEN_BRACE)
5973     statement = cp_parser_compound_statement (parser, NULL, false);
5974   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5975      a statement all its own.  */
5976   else if (token->type == CPP_PRAGMA)
5977     {
5978       cp_lexer_handle_pragma (parser->lexer);
5979       return;
5980     }
5981
5982   /* Everything else must be a declaration-statement or an
5983      expression-statement.  Try for the declaration-statement
5984      first, unless we are looking at a `;', in which case we know that
5985      we have an expression-statement.  */
5986   if (!statement)
5987     {
5988       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5989         {
5990           cp_parser_parse_tentatively (parser);
5991           /* Try to parse the declaration-statement.  */
5992           cp_parser_declaration_statement (parser);
5993           /* If that worked, we're done.  */
5994           if (cp_parser_parse_definitely (parser))
5995             return;
5996         }
5997       /* Look for an expression-statement instead.  */
5998       statement = cp_parser_expression_statement (parser, in_statement_expr);
5999     }
6000
6001   /* Set the line number for the statement.  */
6002   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6003     SET_EXPR_LOCATION (statement, statement_location);
6004 }
6005
6006 /* Parse a labeled-statement.
6007
6008    labeled-statement:
6009      identifier : statement
6010      case constant-expression : statement
6011      default : statement
6012
6013    GNU Extension:
6014
6015    labeled-statement:
6016      case constant-expression ... constant-expression : statement
6017
6018    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6019    For an ordinary label, returns a LABEL_EXPR.  */
6020
6021 static tree
6022 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6023 {
6024   cp_token *token;
6025   tree statement = error_mark_node;
6026
6027   /* The next token should be an identifier.  */
6028   token = cp_lexer_peek_token (parser->lexer);
6029   if (token->type != CPP_NAME
6030       && token->type != CPP_KEYWORD)
6031     {
6032       cp_parser_error (parser, "expected labeled-statement");
6033       return error_mark_node;
6034     }
6035
6036   switch (token->keyword)
6037     {
6038     case RID_CASE:
6039       {
6040         tree expr, expr_hi;
6041         cp_token *ellipsis;
6042
6043         /* Consume the `case' token.  */
6044         cp_lexer_consume_token (parser->lexer);
6045         /* Parse the constant-expression.  */
6046         expr = cp_parser_constant_expression (parser,
6047                                               /*allow_non_constant_p=*/false,
6048                                               NULL);
6049
6050         ellipsis = cp_lexer_peek_token (parser->lexer);
6051         if (ellipsis->type == CPP_ELLIPSIS)
6052           {
6053             /* Consume the `...' token.  */
6054             cp_lexer_consume_token (parser->lexer);
6055             expr_hi =
6056               cp_parser_constant_expression (parser,
6057                                              /*allow_non_constant_p=*/false,
6058                                              NULL);
6059             /* We don't need to emit warnings here, as the common code
6060                will do this for us.  */
6061           }
6062         else
6063           expr_hi = NULL_TREE;
6064
6065         if (!parser->in_switch_statement_p)
6066           error ("case label %qE not within a switch statement", expr);
6067         else
6068           statement = finish_case_label (expr, expr_hi);
6069       }
6070       break;
6071
6072     case RID_DEFAULT:
6073       /* Consume the `default' token.  */
6074       cp_lexer_consume_token (parser->lexer);
6075       if (!parser->in_switch_statement_p)
6076         error ("case label not within a switch statement");
6077       else
6078         statement = finish_case_label (NULL_TREE, NULL_TREE);
6079       break;
6080
6081     default:
6082       /* Anything else must be an ordinary label.  */
6083       statement = finish_label_stmt (cp_parser_identifier (parser));
6084       break;
6085     }
6086
6087   /* Require the `:' token.  */
6088   cp_parser_require (parser, CPP_COLON, "`:'");
6089   /* Parse the labeled statement.  */
6090   cp_parser_statement (parser, in_statement_expr);
6091
6092   /* Return the label, in the case of a `case' or `default' label.  */
6093   return statement;
6094 }
6095
6096 /* Parse an expression-statement.
6097
6098    expression-statement:
6099      expression [opt] ;
6100
6101    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6102    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6103    indicates whether this expression-statement is part of an
6104    expression statement.  */
6105
6106 static tree
6107 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6108 {
6109   tree statement = NULL_TREE;
6110
6111   /* If the next token is a ';', then there is no expression
6112      statement.  */
6113   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6114     statement = cp_parser_expression (parser, /*cast_p=*/false);
6115
6116   /* Consume the final `;'.  */
6117   cp_parser_consume_semicolon_at_end_of_statement (parser);
6118
6119   if (in_statement_expr
6120       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6121     /* This is the final expression statement of a statement
6122        expression.  */
6123     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6124   else if (statement)
6125     statement = finish_expr_stmt (statement);
6126   else
6127     finish_stmt ();
6128
6129   return statement;
6130 }
6131
6132 /* Parse a compound-statement.
6133
6134    compound-statement:
6135      { statement-seq [opt] }
6136
6137    Returns a tree representing the statement.  */
6138
6139 static tree
6140 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6141                               bool in_try)
6142 {
6143   tree compound_stmt;
6144
6145   /* Consume the `{'.  */
6146   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6147     return error_mark_node;
6148   /* Begin the compound-statement.  */
6149   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6150   /* Parse an (optional) statement-seq.  */
6151   cp_parser_statement_seq_opt (parser, in_statement_expr);
6152   /* Finish the compound-statement.  */
6153   finish_compound_stmt (compound_stmt);
6154   /* Consume the `}'.  */
6155   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6156
6157   return compound_stmt;
6158 }
6159
6160 /* Parse an (optional) statement-seq.
6161
6162    statement-seq:
6163      statement
6164      statement-seq [opt] statement  */
6165
6166 static void
6167 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6168 {
6169   /* Scan statements until there aren't any more.  */
6170   while (true)
6171     {
6172       /* If we're looking at a `}', then we've run out of statements.  */
6173       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6174           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6175         break;
6176
6177       /* Parse the statement.  */
6178       cp_parser_statement (parser, in_statement_expr);
6179     }
6180 }
6181
6182 /* Parse a selection-statement.
6183
6184    selection-statement:
6185      if ( condition ) statement
6186      if ( condition ) statement else statement
6187      switch ( condition ) statement
6188
6189    Returns the new IF_STMT or SWITCH_STMT.  */
6190
6191 static tree
6192 cp_parser_selection_statement (cp_parser* parser)
6193 {
6194   cp_token *token;
6195   enum rid keyword;
6196
6197   /* Peek at the next token.  */
6198   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6199
6200   /* See what kind of keyword it is.  */
6201   keyword = token->keyword;
6202   switch (keyword)
6203     {
6204     case RID_IF:
6205     case RID_SWITCH:
6206       {
6207         tree statement;
6208         tree condition;
6209
6210         /* Look for the `('.  */
6211         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6212           {
6213             cp_parser_skip_to_end_of_statement (parser);
6214             return error_mark_node;
6215           }
6216
6217         /* Begin the selection-statement.  */
6218         if (keyword == RID_IF)
6219           statement = begin_if_stmt ();
6220         else
6221           statement = begin_switch_stmt ();
6222
6223         /* Parse the condition.  */
6224         condition = cp_parser_condition (parser);
6225         /* Look for the `)'.  */
6226         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6227           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6228                                                  /*consume_paren=*/true);
6229
6230         if (keyword == RID_IF)
6231           {
6232             /* Add the condition.  */
6233             finish_if_stmt_cond (condition, statement);
6234
6235             /* Parse the then-clause.  */
6236             cp_parser_implicitly_scoped_statement (parser);
6237             finish_then_clause (statement);
6238
6239             /* If the next token is `else', parse the else-clause.  */
6240             if (cp_lexer_next_token_is_keyword (parser->lexer,
6241                                                 RID_ELSE))
6242               {
6243                 /* Consume the `else' keyword.  */
6244                 cp_lexer_consume_token (parser->lexer);
6245                 begin_else_clause (statement);
6246                 /* Parse the else-clause.  */
6247                 cp_parser_implicitly_scoped_statement (parser);
6248                 finish_else_clause (statement);
6249               }
6250
6251             /* Now we're all done with the if-statement.  */
6252             finish_if_stmt (statement);
6253           }
6254         else
6255           {
6256             bool in_switch_statement_p;
6257
6258             /* Add the condition.  */
6259             finish_switch_cond (condition, statement);
6260
6261             /* Parse the body of the switch-statement.  */
6262             in_switch_statement_p = parser->in_switch_statement_p;
6263             parser->in_switch_statement_p = true;
6264             cp_parser_implicitly_scoped_statement (parser);
6265             parser->in_switch_statement_p = in_switch_statement_p;
6266
6267             /* Now we're all done with the switch-statement.  */
6268             finish_switch_stmt (statement);
6269           }
6270
6271         return statement;
6272       }
6273       break;
6274
6275     default:
6276       cp_parser_error (parser, "expected selection-statement");
6277       return error_mark_node;
6278     }
6279 }
6280
6281 /* Parse a condition.
6282
6283    condition:
6284      expression
6285      type-specifier-seq declarator = assignment-expression
6286
6287    GNU Extension:
6288
6289    condition:
6290      type-specifier-seq declarator asm-specification [opt]
6291        attributes [opt] = assignment-expression
6292
6293    Returns the expression that should be tested.  */
6294
6295 static tree
6296 cp_parser_condition (cp_parser* parser)
6297 {
6298   cp_decl_specifier_seq type_specifiers;
6299   const char *saved_message;
6300
6301   /* Try the declaration first.  */
6302   cp_parser_parse_tentatively (parser);
6303   /* New types are not allowed in the type-specifier-seq for a
6304      condition.  */
6305   saved_message = parser->type_definition_forbidden_message;
6306   parser->type_definition_forbidden_message
6307     = "types may not be defined in conditions";
6308   /* Parse the type-specifier-seq.  */
6309   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6310                                 &type_specifiers);
6311   /* Restore the saved message.  */
6312   parser->type_definition_forbidden_message = saved_message;
6313   /* If all is well, we might be looking at a declaration.  */
6314   if (!cp_parser_error_occurred (parser))
6315     {
6316       tree decl;
6317       tree asm_specification;
6318       tree attributes;
6319       cp_declarator *declarator;
6320       tree initializer = NULL_TREE;
6321
6322       /* Parse the declarator.  */
6323       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6324                                          /*ctor_dtor_or_conv_p=*/NULL,
6325                                          /*parenthesized_p=*/NULL,
6326                                          /*member_p=*/false);
6327       /* Parse the attributes.  */
6328       attributes = cp_parser_attributes_opt (parser);
6329       /* Parse the asm-specification.  */
6330       asm_specification = cp_parser_asm_specification_opt (parser);
6331       /* If the next token is not an `=', then we might still be
6332          looking at an expression.  For example:
6333
6334            if (A(a).x)
6335
6336          looks like a decl-specifier-seq and a declarator -- but then
6337          there is no `=', so this is an expression.  */
6338       cp_parser_require (parser, CPP_EQ, "`='");
6339       /* If we did see an `=', then we are looking at a declaration
6340          for sure.  */
6341       if (cp_parser_parse_definitely (parser))
6342         {
6343           tree pushed_scope;    
6344
6345           /* Create the declaration.  */
6346           decl = start_decl (declarator, &type_specifiers,
6347                              /*initialized_p=*/true,
6348                              attributes, /*prefix_attributes=*/NULL_TREE,
6349                              &pushed_scope);
6350           /* Parse the assignment-expression.  */
6351           initializer = cp_parser_assignment_expression (parser,
6352                                                          /*cast_p=*/false);
6353
6354           /* Process the initializer.  */
6355           cp_finish_decl (decl,
6356                           initializer,
6357                           asm_specification,
6358                           LOOKUP_ONLYCONVERTING);
6359
6360           if (pushed_scope)
6361             pop_scope (pushed_scope);
6362
6363           return convert_from_reference (decl);
6364         }
6365     }
6366   /* If we didn't even get past the declarator successfully, we are
6367      definitely not looking at a declaration.  */
6368   else
6369     cp_parser_abort_tentative_parse (parser);
6370
6371   /* Otherwise, we are looking at an expression.  */
6372   return cp_parser_expression (parser, /*cast_p=*/false);
6373 }
6374
6375 /* Parse an iteration-statement.
6376
6377    iteration-statement:
6378      while ( condition ) statement
6379      do statement while ( expression ) ;
6380      for ( for-init-statement condition [opt] ; expression [opt] )
6381        statement
6382
6383    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6384
6385 static tree
6386 cp_parser_iteration_statement (cp_parser* parser)
6387 {
6388   cp_token *token;
6389   enum rid keyword;
6390   tree statement;
6391   bool in_iteration_statement_p;
6392
6393
6394   /* Peek at the next token.  */
6395   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6396   if (!token)
6397     return error_mark_node;
6398
6399   /* Remember whether or not we are already within an iteration
6400      statement.  */
6401   in_iteration_statement_p = parser->in_iteration_statement_p;
6402
6403   /* See what kind of keyword it is.  */
6404   keyword = token->keyword;
6405   switch (keyword)
6406     {
6407     case RID_WHILE:
6408       {
6409         tree condition;
6410
6411         /* Begin the while-statement.  */
6412         statement = begin_while_stmt ();
6413         /* Look for the `('.  */
6414         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6415         /* Parse the condition.  */
6416         condition = cp_parser_condition (parser);
6417         finish_while_stmt_cond (condition, statement);
6418         /* Look for the `)'.  */
6419         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6420         /* Parse the dependent statement.  */
6421         parser->in_iteration_statement_p = true;
6422         cp_parser_already_scoped_statement (parser);
6423         parser->in_iteration_statement_p = in_iteration_statement_p;
6424         /* We're done with the while-statement.  */
6425         finish_while_stmt (statement);
6426       }
6427       break;
6428
6429     case RID_DO:
6430       {
6431         tree expression;
6432
6433         /* Begin the do-statement.  */
6434         statement = begin_do_stmt ();
6435         /* Parse the body of the do-statement.  */
6436         parser->in_iteration_statement_p = true;
6437         cp_parser_implicitly_scoped_statement (parser);
6438         parser->in_iteration_statement_p = in_iteration_statement_p;
6439         finish_do_body (statement);
6440         /* Look for the `while' keyword.  */
6441         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6442         /* Look for the `('.  */
6443         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6444         /* Parse the expression.  */
6445         expression = cp_parser_expression (parser, /*cast_p=*/false);
6446         /* We're done with the do-statement.  */
6447         finish_do_stmt (expression, statement);
6448         /* Look for the `)'.  */
6449         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6450         /* Look for the `;'.  */
6451         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6452       }
6453       break;
6454
6455     case RID_FOR:
6456       {
6457         tree condition = NULL_TREE;
6458         tree expression = NULL_TREE;
6459
6460         /* Begin the for-statement.  */
6461         statement = begin_for_stmt ();
6462         /* Look for the `('.  */
6463         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6464         /* Parse the initialization.  */
6465         cp_parser_for_init_statement (parser);
6466         finish_for_init_stmt (statement);
6467
6468         /* If there's a condition, process it.  */
6469         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6470           condition = cp_parser_condition (parser);
6471         finish_for_cond (condition, statement);
6472         /* Look for the `;'.  */
6473         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6474
6475         /* If there's an expression, process it.  */
6476         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6477           expression = cp_parser_expression (parser, /*cast_p=*/false);
6478         finish_for_expr (expression, statement);
6479         /* Look for the `)'.  */
6480         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6481
6482         /* Parse the body of the for-statement.  */
6483         parser->in_iteration_statement_p = true;
6484         cp_parser_already_scoped_statement (parser);
6485         parser->in_iteration_statement_p = in_iteration_statement_p;
6486
6487         /* We're done with the for-statement.  */
6488         finish_for_stmt (statement);
6489       }
6490       break;
6491
6492     default:
6493       cp_parser_error (parser, "expected iteration-statement");
6494       statement = error_mark_node;
6495       break;
6496     }
6497
6498   return statement;
6499 }
6500
6501 /* Parse a for-init-statement.
6502
6503    for-init-statement:
6504      expression-statement
6505      simple-declaration  */
6506
6507 static void
6508 cp_parser_for_init_statement (cp_parser* parser)
6509 {
6510   /* If the next token is a `;', then we have an empty
6511      expression-statement.  Grammatically, this is also a
6512      simple-declaration, but an invalid one, because it does not
6513      declare anything.  Therefore, if we did not handle this case
6514      specially, we would issue an error message about an invalid
6515      declaration.  */
6516   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6517     {
6518       /* We're going to speculatively look for a declaration, falling back
6519          to an expression, if necessary.  */
6520       cp_parser_parse_tentatively (parser);
6521       /* Parse the declaration.  */
6522       cp_parser_simple_declaration (parser,
6523                                     /*function_definition_allowed_p=*/false);
6524       /* If the tentative parse failed, then we shall need to look for an
6525          expression-statement.  */
6526       if (cp_parser_parse_definitely (parser))
6527         return;
6528     }
6529
6530   cp_parser_expression_statement (parser, false);
6531 }
6532
6533 /* Parse a jump-statement.
6534
6535    jump-statement:
6536      break ;
6537      continue ;
6538      return expression [opt] ;
6539      goto identifier ;
6540
6541    GNU extension:
6542
6543    jump-statement:
6544      goto * expression ;
6545
6546    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6547
6548 static tree
6549 cp_parser_jump_statement (cp_parser* parser)
6550 {
6551   tree statement = error_mark_node;
6552   cp_token *token;
6553   enum rid keyword;
6554
6555   /* Peek at the next token.  */
6556   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6557   if (!token)
6558     return error_mark_node;
6559
6560   /* See what kind of keyword it is.  */
6561   keyword = token->keyword;
6562   switch (keyword)
6563     {
6564     case RID_BREAK:
6565       if (!parser->in_switch_statement_p
6566           && !parser->in_iteration_statement_p)
6567         {
6568           error ("break statement not within loop or switch");
6569           statement = error_mark_node;
6570         }
6571       else
6572         statement = finish_break_stmt ();
6573       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6574       break;
6575
6576     case RID_CONTINUE:
6577       if (!parser->in_iteration_statement_p)
6578         {
6579           error ("continue statement not within a loop");
6580           statement = error_mark_node;
6581         }
6582       else
6583         statement = finish_continue_stmt ();
6584       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6585       break;
6586
6587     case RID_RETURN:
6588       {
6589         tree expr;
6590
6591         /* If the next token is a `;', then there is no
6592            expression.  */
6593         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6594           expr = cp_parser_expression (parser, /*cast_p=*/false);
6595         else
6596           expr = NULL_TREE;
6597         /* Build the return-statement.  */
6598         statement = finish_return_stmt (expr);
6599         /* Look for the final `;'.  */
6600         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6601       }
6602       break;
6603
6604     case RID_GOTO:
6605       /* Create the goto-statement.  */
6606       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6607         {
6608           /* Issue a warning about this use of a GNU extension.  */
6609           if (pedantic)
6610             pedwarn ("ISO C++ forbids computed gotos");
6611           /* Consume the '*' token.  */
6612           cp_lexer_consume_token (parser->lexer);
6613           /* Parse the dependent expression.  */
6614           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6615         }
6616       else
6617         finish_goto_stmt (cp_parser_identifier (parser));
6618       /* Look for the final `;'.  */
6619       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6620       break;
6621
6622     default:
6623       cp_parser_error (parser, "expected jump-statement");
6624       break;
6625     }
6626
6627   return statement;
6628 }
6629
6630 /* Parse a declaration-statement.
6631
6632    declaration-statement:
6633      block-declaration  */
6634
6635 static void
6636 cp_parser_declaration_statement (cp_parser* parser)
6637 {
6638   void *p;
6639
6640   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6641   p = obstack_alloc (&declarator_obstack, 0);
6642
6643  /* Parse the block-declaration.  */
6644   cp_parser_block_declaration (parser, /*statement_p=*/true);
6645
6646   /* Free any declarators allocated.  */
6647   obstack_free (&declarator_obstack, p);
6648
6649   /* Finish off the statement.  */
6650   finish_stmt ();
6651 }
6652
6653 /* Some dependent statements (like `if (cond) statement'), are
6654    implicitly in their own scope.  In other words, if the statement is
6655    a single statement (as opposed to a compound-statement), it is
6656    none-the-less treated as if it were enclosed in braces.  Any
6657    declarations appearing in the dependent statement are out of scope
6658    after control passes that point.  This function parses a statement,
6659    but ensures that is in its own scope, even if it is not a
6660    compound-statement.
6661
6662    Returns the new statement.  */
6663
6664 static tree
6665 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6666 {
6667   tree statement;
6668
6669   /* If the token is not a `{', then we must take special action.  */
6670   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6671     {
6672       /* Create a compound-statement.  */
6673       statement = begin_compound_stmt (0);
6674       /* Parse the dependent-statement.  */
6675       cp_parser_statement (parser, false);
6676       /* Finish the dummy compound-statement.  */
6677       finish_compound_stmt (statement);
6678     }
6679   /* Otherwise, we simply parse the statement directly.  */
6680   else
6681     statement = cp_parser_compound_statement (parser, NULL, false);
6682
6683   /* Return the statement.  */
6684   return statement;
6685 }
6686
6687 /* For some dependent statements (like `while (cond) statement'), we
6688    have already created a scope.  Therefore, even if the dependent
6689    statement is a compound-statement, we do not want to create another
6690    scope.  */
6691
6692 static void
6693 cp_parser_already_scoped_statement (cp_parser* parser)
6694 {
6695   /* If the token is a `{', then we must take special action.  */
6696   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6697     cp_parser_statement (parser, false);
6698   else
6699     {
6700       /* Avoid calling cp_parser_compound_statement, so that we
6701          don't create a new scope.  Do everything else by hand.  */
6702       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6703       cp_parser_statement_seq_opt (parser, false);
6704       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6705     }
6706 }
6707
6708 /* Declarations [gram.dcl.dcl] */
6709
6710 /* Parse an optional declaration-sequence.
6711
6712    declaration-seq:
6713      declaration
6714      declaration-seq declaration  */
6715
6716 static void
6717 cp_parser_declaration_seq_opt (cp_parser* parser)
6718 {
6719   while (true)
6720     {
6721       cp_token *token;
6722
6723       token = cp_lexer_peek_token (parser->lexer);
6724
6725       if (token->type == CPP_CLOSE_BRACE
6726           || token->type == CPP_EOF)
6727         break;
6728
6729       if (token->type == CPP_SEMICOLON)
6730         {
6731           /* A declaration consisting of a single semicolon is
6732              invalid.  Allow it unless we're being pedantic.  */
6733           cp_lexer_consume_token (parser->lexer);
6734           if (pedantic && !in_system_header)
6735             pedwarn ("extra %<;%>");
6736           continue;
6737         }
6738
6739       /* If we're entering or exiting a region that's implicitly
6740          extern "C", modify the lang context appropriately.  */
6741       if (!parser->implicit_extern_c && token->implicit_extern_c)
6742         {
6743           push_lang_context (lang_name_c);
6744           parser->implicit_extern_c = true;
6745         }
6746       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6747         {
6748           pop_lang_context ();
6749           parser->implicit_extern_c = false;
6750         }
6751
6752       if (token->type == CPP_PRAGMA)
6753         {
6754           /* A top-level declaration can consist solely of a #pragma.
6755              A nested declaration cannot, so this is done here and not
6756              in cp_parser_declaration.  (A #pragma at block scope is
6757              handled in cp_parser_statement.)  */
6758           cp_lexer_handle_pragma (parser->lexer);
6759           continue;
6760         }
6761
6762       /* Parse the declaration itself.  */
6763       cp_parser_declaration (parser);
6764     }
6765 }
6766
6767 /* Parse a declaration.
6768
6769    declaration:
6770      block-declaration
6771      function-definition
6772      template-declaration
6773      explicit-instantiation
6774      explicit-specialization
6775      linkage-specification
6776      namespace-definition
6777
6778    GNU extension:
6779
6780    declaration:
6781       __extension__ declaration */
6782
6783 static void
6784 cp_parser_declaration (cp_parser* parser)
6785 {
6786   cp_token token1;
6787   cp_token token2;
6788   int saved_pedantic;
6789   void *p;
6790
6791   /* Check for the `__extension__' keyword.  */
6792   if (cp_parser_extension_opt (parser, &saved_pedantic))
6793     {
6794       /* Parse the qualified declaration.  */
6795       cp_parser_declaration (parser);
6796       /* Restore the PEDANTIC flag.  */
6797       pedantic = saved_pedantic;
6798
6799       return;
6800     }
6801
6802   /* Try to figure out what kind of declaration is present.  */
6803   token1 = *cp_lexer_peek_token (parser->lexer);
6804
6805   if (token1.type != CPP_EOF)
6806     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6807
6808   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6809   p = obstack_alloc (&declarator_obstack, 0);
6810
6811   /* If the next token is `extern' and the following token is a string
6812      literal, then we have a linkage specification.  */
6813   if (token1.keyword == RID_EXTERN
6814       && cp_parser_is_string_literal (&token2))
6815     cp_parser_linkage_specification (parser);
6816   /* If the next token is `template', then we have either a template
6817      declaration, an explicit instantiation, or an explicit
6818      specialization.  */
6819   else if (token1.keyword == RID_TEMPLATE)
6820     {
6821       /* `template <>' indicates a template specialization.  */
6822       if (token2.type == CPP_LESS
6823           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6824         cp_parser_explicit_specialization (parser);
6825       /* `template <' indicates a template declaration.  */
6826       else if (token2.type == CPP_LESS)
6827         cp_parser_template_declaration (parser, /*member_p=*/false);
6828       /* Anything else must be an explicit instantiation.  */
6829       else
6830         cp_parser_explicit_instantiation (parser);
6831     }
6832   /* If the next token is `export', then we have a template
6833      declaration.  */
6834   else if (token1.keyword == RID_EXPORT)
6835     cp_parser_template_declaration (parser, /*member_p=*/false);
6836   /* If the next token is `extern', 'static' or 'inline' and the one
6837      after that is `template', we have a GNU extended explicit
6838      instantiation directive.  */
6839   else if (cp_parser_allow_gnu_extensions_p (parser)
6840            && (token1.keyword == RID_EXTERN
6841                || token1.keyword == RID_STATIC
6842                || token1.keyword == RID_INLINE)
6843            && token2.keyword == RID_TEMPLATE)
6844     cp_parser_explicit_instantiation (parser);
6845   /* If the next token is `namespace', check for a named or unnamed
6846      namespace definition.  */
6847   else if (token1.keyword == RID_NAMESPACE
6848            && (/* A named namespace definition.  */
6849                (token2.type == CPP_NAME
6850                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6851                     == CPP_OPEN_BRACE))
6852                /* An unnamed namespace definition.  */
6853                || token2.type == CPP_OPEN_BRACE))
6854     cp_parser_namespace_definition (parser);
6855   /* We must have either a block declaration or a function
6856      definition.  */
6857   else
6858     /* Try to parse a block-declaration, or a function-definition.  */
6859     cp_parser_block_declaration (parser, /*statement_p=*/false);
6860
6861   /* Free any declarators allocated.  */
6862   obstack_free (&declarator_obstack, p);
6863 }
6864
6865 /* Parse a block-declaration.
6866
6867    block-declaration:
6868      simple-declaration
6869      asm-definition
6870      namespace-alias-definition
6871      using-declaration
6872      using-directive
6873
6874    GNU Extension:
6875
6876    block-declaration:
6877      __extension__ block-declaration
6878      label-declaration
6879
6880    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6881    part of a declaration-statement.  */
6882
6883 static void
6884 cp_parser_block_declaration (cp_parser *parser,
6885                              bool      statement_p)
6886 {
6887   cp_token *token1;
6888   int saved_pedantic;
6889
6890   /* Check for the `__extension__' keyword.  */
6891   if (cp_parser_extension_opt (parser, &saved_pedantic))
6892     {
6893       /* Parse the qualified declaration.  */
6894       cp_parser_block_declaration (parser, statement_p);
6895       /* Restore the PEDANTIC flag.  */
6896       pedantic = saved_pedantic;
6897
6898       return;
6899     }
6900
6901   /* Peek at the next token to figure out which kind of declaration is
6902      present.  */
6903   token1 = cp_lexer_peek_token (parser->lexer);
6904
6905   /* If the next keyword is `asm', we have an asm-definition.  */
6906   if (token1->keyword == RID_ASM)
6907     {
6908       if (statement_p)
6909         cp_parser_commit_to_tentative_parse (parser);
6910       cp_parser_asm_definition (parser);
6911     }
6912   /* If the next keyword is `namespace', we have a
6913      namespace-alias-definition.  */
6914   else if (token1->keyword == RID_NAMESPACE)
6915     cp_parser_namespace_alias_definition (parser);
6916   /* If the next keyword is `using', we have either a
6917      using-declaration or a using-directive.  */
6918   else if (token1->keyword == RID_USING)
6919     {
6920       cp_token *token2;
6921
6922       if (statement_p)
6923         cp_parser_commit_to_tentative_parse (parser);
6924       /* If the token after `using' is `namespace', then we have a
6925          using-directive.  */
6926       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6927       if (token2->keyword == RID_NAMESPACE)
6928         cp_parser_using_directive (parser);
6929       /* Otherwise, it's a using-declaration.  */
6930       else
6931         cp_parser_using_declaration (parser);
6932     }
6933   /* If the next keyword is `__label__' we have a label declaration.  */
6934   else if (token1->keyword == RID_LABEL)
6935     {
6936       if (statement_p)
6937         cp_parser_commit_to_tentative_parse (parser);
6938       cp_parser_label_declaration (parser);
6939     }
6940   /* Anything else must be a simple-declaration.  */
6941   else
6942     cp_parser_simple_declaration (parser, !statement_p);
6943 }
6944
6945 /* Parse a simple-declaration.
6946
6947    simple-declaration:
6948      decl-specifier-seq [opt] init-declarator-list [opt] ;
6949
6950    init-declarator-list:
6951      init-declarator
6952      init-declarator-list , init-declarator
6953
6954    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6955    function-definition as a simple-declaration.  */
6956
6957 static void
6958 cp_parser_simple_declaration (cp_parser* parser,
6959                               bool function_definition_allowed_p)
6960 {
6961   cp_decl_specifier_seq decl_specifiers;
6962   int declares_class_or_enum;
6963   bool saw_declarator;
6964
6965   /* Defer access checks until we know what is being declared; the
6966      checks for names appearing in the decl-specifier-seq should be
6967      done as if we were in the scope of the thing being declared.  */
6968   push_deferring_access_checks (dk_deferred);
6969
6970   /* Parse the decl-specifier-seq.  We have to keep track of whether
6971      or not the decl-specifier-seq declares a named class or
6972      enumeration type, since that is the only case in which the
6973      init-declarator-list is allowed to be empty.
6974
6975      [dcl.dcl]
6976
6977      In a simple-declaration, the optional init-declarator-list can be
6978      omitted only when declaring a class or enumeration, that is when
6979      the decl-specifier-seq contains either a class-specifier, an
6980      elaborated-type-specifier, or an enum-specifier.  */
6981   cp_parser_decl_specifier_seq (parser,
6982                                 CP_PARSER_FLAGS_OPTIONAL,
6983                                 &decl_specifiers,
6984                                 &declares_class_or_enum);
6985   /* We no longer need to defer access checks.  */
6986   stop_deferring_access_checks ();
6987
6988   /* In a block scope, a valid declaration must always have a
6989      decl-specifier-seq.  By not trying to parse declarators, we can
6990      resolve the declaration/expression ambiguity more quickly.  */
6991   if (!function_definition_allowed_p
6992       && !decl_specifiers.any_specifiers_p)
6993     {
6994       cp_parser_error (parser, "expected declaration");
6995       goto done;
6996     }
6997
6998   /* If the next two tokens are both identifiers, the code is
6999      erroneous. The usual cause of this situation is code like:
7000
7001        T t;
7002
7003      where "T" should name a type -- but does not.  */
7004   if (!decl_specifiers.type
7005       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7006     {
7007       /* If parsing tentatively, we should commit; we really are
7008          looking at a declaration.  */
7009       cp_parser_commit_to_tentative_parse (parser);
7010       /* Give up.  */
7011       goto done;
7012     }
7013   
7014   /* If we have seen at least one decl-specifier, and the next token
7015      is not a parenthesis, then we must be looking at a declaration.
7016      (After "int (" we might be looking at a functional cast.)  */
7017   if (decl_specifiers.any_specifiers_p 
7018       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7019     cp_parser_commit_to_tentative_parse (parser);
7020
7021   /* Keep going until we hit the `;' at the end of the simple
7022      declaration.  */
7023   saw_declarator = false;
7024   while (cp_lexer_next_token_is_not (parser->lexer,
7025                                      CPP_SEMICOLON))
7026     {
7027       cp_token *token;
7028       bool function_definition_p;
7029       tree decl;
7030
7031       saw_declarator = true;
7032       /* Parse the init-declarator.  */
7033       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7034                                         function_definition_allowed_p,
7035                                         /*member_p=*/false,
7036                                         declares_class_or_enum,
7037                                         &function_definition_p);
7038       /* If an error occurred while parsing tentatively, exit quickly.
7039          (That usually happens when in the body of a function; each
7040          statement is treated as a declaration-statement until proven
7041          otherwise.)  */
7042       if (cp_parser_error_occurred (parser))
7043         goto done;
7044       /* Handle function definitions specially.  */
7045       if (function_definition_p)
7046         {
7047           /* If the next token is a `,', then we are probably
7048              processing something like:
7049
7050                void f() {}, *p;
7051
7052              which is erroneous.  */
7053           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7054             error ("mixing declarations and function-definitions is forbidden");
7055           /* Otherwise, we're done with the list of declarators.  */
7056           else
7057             {
7058               pop_deferring_access_checks ();
7059               return;
7060             }
7061         }
7062       /* The next token should be either a `,' or a `;'.  */
7063       token = cp_lexer_peek_token (parser->lexer);
7064       /* If it's a `,', there are more declarators to come.  */
7065       if (token->type == CPP_COMMA)
7066         cp_lexer_consume_token (parser->lexer);
7067       /* If it's a `;', we are done.  */
7068       else if (token->type == CPP_SEMICOLON)
7069         break;
7070       /* Anything else is an error.  */
7071       else
7072         {
7073           /* If we have already issued an error message we don't need
7074              to issue another one.  */
7075           if (decl != error_mark_node
7076               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7077             cp_parser_error (parser, "expected %<,%> or %<;%>");
7078           /* Skip tokens until we reach the end of the statement.  */
7079           cp_parser_skip_to_end_of_statement (parser);
7080           /* If the next token is now a `;', consume it.  */
7081           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7082             cp_lexer_consume_token (parser->lexer);
7083           goto done;
7084         }
7085       /* After the first time around, a function-definition is not
7086          allowed -- even if it was OK at first.  For example:
7087
7088            int i, f() {}
7089
7090          is not valid.  */
7091       function_definition_allowed_p = false;
7092     }
7093
7094   /* Issue an error message if no declarators are present, and the
7095      decl-specifier-seq does not itself declare a class or
7096      enumeration.  */
7097   if (!saw_declarator)
7098     {
7099       if (cp_parser_declares_only_class_p (parser))
7100         shadow_tag (&decl_specifiers);
7101       /* Perform any deferred access checks.  */
7102       perform_deferred_access_checks ();
7103     }
7104
7105   /* Consume the `;'.  */
7106   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7107
7108  done:
7109   pop_deferring_access_checks ();
7110 }
7111
7112 /* Parse a decl-specifier-seq.
7113
7114    decl-specifier-seq:
7115      decl-specifier-seq [opt] decl-specifier
7116
7117    decl-specifier:
7118      storage-class-specifier
7119      type-specifier
7120      function-specifier
7121      friend
7122      typedef
7123
7124    GNU Extension:
7125
7126    decl-specifier:
7127      attributes
7128
7129    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7130
7131    The parser flags FLAGS is used to control type-specifier parsing.
7132
7133    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7134    flags:
7135
7136      1: one of the decl-specifiers is an elaborated-type-specifier
7137         (i.e., a type declaration)
7138      2: one of the decl-specifiers is an enum-specifier or a
7139         class-specifier (i.e., a type definition)
7140
7141    */
7142
7143 static void
7144 cp_parser_decl_specifier_seq (cp_parser* parser,
7145                               cp_parser_flags flags,
7146                               cp_decl_specifier_seq *decl_specs,
7147                               int* declares_class_or_enum)
7148 {
7149   bool constructor_possible_p = !parser->in_declarator_p;
7150
7151   /* Clear DECL_SPECS.  */
7152   clear_decl_specs (decl_specs);
7153
7154   /* Assume no class or enumeration type is declared.  */
7155   *declares_class_or_enum = 0;
7156
7157   /* Keep reading specifiers until there are no more to read.  */
7158   while (true)
7159     {
7160       bool constructor_p;
7161       bool found_decl_spec;
7162       cp_token *token;
7163
7164       /* Peek at the next token.  */
7165       token = cp_lexer_peek_token (parser->lexer);
7166       /* Handle attributes.  */
7167       if (token->keyword == RID_ATTRIBUTE)
7168         {
7169           /* Parse the attributes.  */
7170           decl_specs->attributes
7171             = chainon (decl_specs->attributes,
7172                        cp_parser_attributes_opt (parser));
7173           continue;
7174         }
7175       /* Assume we will find a decl-specifier keyword.  */
7176       found_decl_spec = true;
7177       /* If the next token is an appropriate keyword, we can simply
7178          add it to the list.  */
7179       switch (token->keyword)
7180         {
7181           /* decl-specifier:
7182                friend  */
7183         case RID_FRIEND:
7184           if (decl_specs->specs[(int) ds_friend]++)
7185             error ("duplicate %<friend%>");
7186           /* Consume the token.  */
7187           cp_lexer_consume_token (parser->lexer);
7188           break;
7189
7190           /* function-specifier:
7191                inline
7192                virtual
7193                explicit  */
7194         case RID_INLINE:
7195         case RID_VIRTUAL:
7196         case RID_EXPLICIT:
7197           cp_parser_function_specifier_opt (parser, decl_specs);
7198           break;
7199
7200           /* decl-specifier:
7201                typedef  */
7202         case RID_TYPEDEF:
7203           ++decl_specs->specs[(int) ds_typedef];
7204           /* Consume the token.  */
7205           cp_lexer_consume_token (parser->lexer);
7206           /* A constructor declarator cannot appear in a typedef.  */
7207           constructor_possible_p = false;
7208           /* The "typedef" keyword can only occur in a declaration; we
7209              may as well commit at this point.  */
7210           cp_parser_commit_to_tentative_parse (parser);
7211           break;
7212
7213           /* storage-class-specifier:
7214                auto
7215                register
7216                static
7217                extern
7218                mutable
7219
7220              GNU Extension:
7221                thread  */
7222         case RID_AUTO:
7223           /* Consume the token.  */
7224           cp_lexer_consume_token (parser->lexer);
7225           cp_parser_set_storage_class (decl_specs, sc_auto);
7226           break;
7227         case RID_REGISTER:
7228           /* Consume the token.  */
7229           cp_lexer_consume_token (parser->lexer);
7230           cp_parser_set_storage_class (decl_specs, sc_register);
7231           break;
7232         case RID_STATIC:
7233           /* Consume the token.  */
7234           cp_lexer_consume_token (parser->lexer);
7235           if (decl_specs->specs[(int) ds_thread])
7236             {
7237               error ("%<__thread%> before %<static%>");
7238               decl_specs->specs[(int) ds_thread] = 0;
7239             }
7240           cp_parser_set_storage_class (decl_specs, sc_static);
7241           break;
7242         case RID_EXTERN:
7243           /* Consume the token.  */
7244           cp_lexer_consume_token (parser->lexer);
7245           if (decl_specs->specs[(int) ds_thread])
7246             {
7247               error ("%<__thread%> before %<extern%>");
7248               decl_specs->specs[(int) ds_thread] = 0;
7249             }
7250           cp_parser_set_storage_class (decl_specs, sc_extern);
7251           break;
7252         case RID_MUTABLE:
7253           /* Consume the token.  */
7254           cp_lexer_consume_token (parser->lexer);
7255           cp_parser_set_storage_class (decl_specs, sc_mutable);
7256           break;
7257         case RID_THREAD:
7258           /* Consume the token.  */
7259           cp_lexer_consume_token (parser->lexer);
7260           ++decl_specs->specs[(int) ds_thread];
7261           break;
7262
7263         default:
7264           /* We did not yet find a decl-specifier yet.  */
7265           found_decl_spec = false;
7266           break;
7267         }
7268
7269       /* Constructors are a special case.  The `S' in `S()' is not a
7270          decl-specifier; it is the beginning of the declarator.  */
7271       constructor_p
7272         = (!found_decl_spec
7273            && constructor_possible_p
7274            && (cp_parser_constructor_declarator_p
7275                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7276
7277       /* If we don't have a DECL_SPEC yet, then we must be looking at
7278          a type-specifier.  */
7279       if (!found_decl_spec && !constructor_p)
7280         {
7281           int decl_spec_declares_class_or_enum;
7282           bool is_cv_qualifier;
7283           tree type_spec;
7284
7285           type_spec
7286             = cp_parser_type_specifier (parser, flags,
7287                                         decl_specs,
7288                                         /*is_declaration=*/true,
7289                                         &decl_spec_declares_class_or_enum,
7290                                         &is_cv_qualifier);
7291
7292           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7293
7294           /* If this type-specifier referenced a user-defined type
7295              (a typedef, class-name, etc.), then we can't allow any
7296              more such type-specifiers henceforth.
7297
7298              [dcl.spec]
7299
7300              The longest sequence of decl-specifiers that could
7301              possibly be a type name is taken as the
7302              decl-specifier-seq of a declaration.  The sequence shall
7303              be self-consistent as described below.
7304
7305              [dcl.type]
7306
7307              As a general rule, at most one type-specifier is allowed
7308              in the complete decl-specifier-seq of a declaration.  The
7309              only exceptions are the following:
7310
7311              -- const or volatile can be combined with any other
7312                 type-specifier.
7313
7314              -- signed or unsigned can be combined with char, long,
7315                 short, or int.
7316
7317              -- ..
7318
7319              Example:
7320
7321                typedef char* Pc;
7322                void g (const int Pc);
7323
7324              Here, Pc is *not* part of the decl-specifier seq; it's
7325              the declarator.  Therefore, once we see a type-specifier
7326              (other than a cv-qualifier), we forbid any additional
7327              user-defined types.  We *do* still allow things like `int
7328              int' to be considered a decl-specifier-seq, and issue the
7329              error message later.  */
7330           if (type_spec && !is_cv_qualifier)
7331             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7332           /* A constructor declarator cannot follow a type-specifier.  */
7333           if (type_spec)
7334             {
7335               constructor_possible_p = false;
7336               found_decl_spec = true;
7337             }
7338         }
7339
7340       /* If we still do not have a DECL_SPEC, then there are no more
7341          decl-specifiers.  */
7342       if (!found_decl_spec)
7343         break;
7344
7345       decl_specs->any_specifiers_p = true;
7346       /* After we see one decl-specifier, further decl-specifiers are
7347          always optional.  */
7348       flags |= CP_PARSER_FLAGS_OPTIONAL;
7349     }
7350
7351   /* Don't allow a friend specifier with a class definition.  */
7352   if (decl_specs->specs[(int) ds_friend] != 0
7353       && (*declares_class_or_enum & 2))
7354     error ("class definition may not be declared a friend");
7355 }
7356
7357 /* Parse an (optional) storage-class-specifier.
7358
7359    storage-class-specifier:
7360      auto
7361      register
7362      static
7363      extern
7364      mutable
7365
7366    GNU Extension:
7367
7368    storage-class-specifier:
7369      thread
7370
7371    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7372
7373 static tree
7374 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7375 {
7376   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7377     {
7378     case RID_AUTO:
7379     case RID_REGISTER:
7380     case RID_STATIC:
7381     case RID_EXTERN:
7382     case RID_MUTABLE:
7383     case RID_THREAD:
7384       /* Consume the token.  */
7385       return cp_lexer_consume_token (parser->lexer)->value;
7386
7387     default:
7388       return NULL_TREE;
7389     }
7390 }
7391
7392 /* Parse an (optional) function-specifier.
7393
7394    function-specifier:
7395      inline
7396      virtual
7397      explicit
7398
7399    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7400    Updates DECL_SPECS, if it is non-NULL.  */
7401
7402 static tree
7403 cp_parser_function_specifier_opt (cp_parser* parser,
7404                                   cp_decl_specifier_seq *decl_specs)
7405 {
7406   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7407     {
7408     case RID_INLINE:
7409       if (decl_specs)
7410         ++decl_specs->specs[(int) ds_inline];
7411       break;
7412
7413     case RID_VIRTUAL:
7414       if (decl_specs)
7415         ++decl_specs->specs[(int) ds_virtual];
7416       break;
7417
7418     case RID_EXPLICIT:
7419       if (decl_specs)
7420         ++decl_specs->specs[(int) ds_explicit];
7421       break;
7422
7423     default:
7424       return NULL_TREE;
7425     }
7426
7427   /* Consume the token.  */
7428   return cp_lexer_consume_token (parser->lexer)->value;
7429 }
7430
7431 /* Parse a linkage-specification.
7432
7433    linkage-specification:
7434      extern string-literal { declaration-seq [opt] }
7435      extern string-literal declaration  */
7436
7437 static void
7438 cp_parser_linkage_specification (cp_parser* parser)
7439 {
7440   tree linkage;
7441
7442   /* Look for the `extern' keyword.  */
7443   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7444
7445   /* Look for the string-literal.  */
7446   linkage = cp_parser_string_literal (parser, false, false);
7447
7448   /* Transform the literal into an identifier.  If the literal is a
7449      wide-character string, or contains embedded NULs, then we can't
7450      handle it as the user wants.  */
7451   if (strlen (TREE_STRING_POINTER (linkage))
7452       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7453     {
7454       cp_parser_error (parser, "invalid linkage-specification");
7455       /* Assume C++ linkage.  */
7456       linkage = lang_name_cplusplus;
7457     }
7458   else
7459     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7460
7461   /* We're now using the new linkage.  */
7462   push_lang_context (linkage);
7463
7464   /* If the next token is a `{', then we're using the first
7465      production.  */
7466   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7467     {
7468       /* Consume the `{' token.  */
7469       cp_lexer_consume_token (parser->lexer);
7470       /* Parse the declarations.  */
7471       cp_parser_declaration_seq_opt (parser);
7472       /* Look for the closing `}'.  */
7473       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7474     }
7475   /* Otherwise, there's just one declaration.  */
7476   else
7477     {
7478       bool saved_in_unbraced_linkage_specification_p;
7479
7480       saved_in_unbraced_linkage_specification_p
7481         = parser->in_unbraced_linkage_specification_p;
7482       parser->in_unbraced_linkage_specification_p = true;
7483       have_extern_spec = true;
7484       cp_parser_declaration (parser);
7485       have_extern_spec = false;
7486       parser->in_unbraced_linkage_specification_p
7487         = saved_in_unbraced_linkage_specification_p;
7488     }
7489
7490   /* We're done with the linkage-specification.  */
7491   pop_lang_context ();
7492 }
7493
7494 /* Special member functions [gram.special] */
7495
7496 /* Parse a conversion-function-id.
7497
7498    conversion-function-id:
7499      operator conversion-type-id
7500
7501    Returns an IDENTIFIER_NODE representing the operator.  */
7502
7503 static tree
7504 cp_parser_conversion_function_id (cp_parser* parser)
7505 {
7506   tree type;
7507   tree saved_scope;
7508   tree saved_qualifying_scope;
7509   tree saved_object_scope;
7510   tree pushed_scope = NULL_TREE;
7511
7512   /* Look for the `operator' token.  */
7513   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7514     return error_mark_node;
7515   /* When we parse the conversion-type-id, the current scope will be
7516      reset.  However, we need that information in able to look up the
7517      conversion function later, so we save it here.  */
7518   saved_scope = parser->scope;
7519   saved_qualifying_scope = parser->qualifying_scope;
7520   saved_object_scope = parser->object_scope;
7521   /* We must enter the scope of the class so that the names of
7522      entities declared within the class are available in the
7523      conversion-type-id.  For example, consider:
7524
7525        struct S {
7526          typedef int I;
7527          operator I();
7528        };
7529
7530        S::operator I() { ... }
7531
7532      In order to see that `I' is a type-name in the definition, we
7533      must be in the scope of `S'.  */
7534   if (saved_scope)
7535     pushed_scope = push_scope (saved_scope);
7536   /* Parse the conversion-type-id.  */
7537   type = cp_parser_conversion_type_id (parser);
7538   /* Leave the scope of the class, if any.  */
7539   if (pushed_scope)
7540     pop_scope (pushed_scope);
7541   /* Restore the saved scope.  */
7542   parser->scope = saved_scope;
7543   parser->qualifying_scope = saved_qualifying_scope;
7544   parser->object_scope = saved_object_scope;
7545   /* If the TYPE is invalid, indicate failure.  */
7546   if (type == error_mark_node)
7547     return error_mark_node;
7548   return mangle_conv_op_name_for_type (type);
7549 }
7550
7551 /* Parse a conversion-type-id:
7552
7553    conversion-type-id:
7554      type-specifier-seq conversion-declarator [opt]
7555
7556    Returns the TYPE specified.  */
7557
7558 static tree
7559 cp_parser_conversion_type_id (cp_parser* parser)
7560 {
7561   tree attributes;
7562   cp_decl_specifier_seq type_specifiers;
7563   cp_declarator *declarator;
7564   tree type_specified;
7565
7566   /* Parse the attributes.  */
7567   attributes = cp_parser_attributes_opt (parser);
7568   /* Parse the type-specifiers.  */
7569   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7570                                 &type_specifiers);
7571   /* If that didn't work, stop.  */
7572   if (type_specifiers.type == error_mark_node)
7573     return error_mark_node;
7574   /* Parse the conversion-declarator.  */
7575   declarator = cp_parser_conversion_declarator_opt (parser);
7576
7577   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7578                                     /*initialized=*/0, &attributes);
7579   if (attributes)
7580     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7581   return type_specified;
7582 }
7583
7584 /* Parse an (optional) conversion-declarator.
7585
7586    conversion-declarator:
7587      ptr-operator conversion-declarator [opt]
7588
7589    */
7590
7591 static cp_declarator *
7592 cp_parser_conversion_declarator_opt (cp_parser* parser)
7593 {
7594   enum tree_code code;
7595   tree class_type;
7596   cp_cv_quals cv_quals;
7597
7598   /* We don't know if there's a ptr-operator next, or not.  */
7599   cp_parser_parse_tentatively (parser);
7600   /* Try the ptr-operator.  */
7601   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7602   /* If it worked, look for more conversion-declarators.  */
7603   if (cp_parser_parse_definitely (parser))
7604     {
7605       cp_declarator *declarator;
7606
7607       /* Parse another optional declarator.  */
7608       declarator = cp_parser_conversion_declarator_opt (parser);
7609
7610       /* Create the representation of the declarator.  */
7611       if (class_type)
7612         declarator = make_ptrmem_declarator (cv_quals, class_type,
7613                                              declarator);
7614       else if (code == INDIRECT_REF)
7615         declarator = make_pointer_declarator (cv_quals, declarator);
7616       else
7617         declarator = make_reference_declarator (cv_quals, declarator);
7618
7619       return declarator;
7620    }
7621
7622   return NULL;
7623 }
7624
7625 /* Parse an (optional) ctor-initializer.
7626
7627    ctor-initializer:
7628      : mem-initializer-list
7629
7630    Returns TRUE iff the ctor-initializer was actually present.  */
7631
7632 static bool
7633 cp_parser_ctor_initializer_opt (cp_parser* parser)
7634 {
7635   /* If the next token is not a `:', then there is no
7636      ctor-initializer.  */
7637   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7638     {
7639       /* Do default initialization of any bases and members.  */
7640       if (DECL_CONSTRUCTOR_P (current_function_decl))
7641         finish_mem_initializers (NULL_TREE);
7642
7643       return false;
7644     }
7645
7646   /* Consume the `:' token.  */
7647   cp_lexer_consume_token (parser->lexer);
7648   /* And the mem-initializer-list.  */
7649   cp_parser_mem_initializer_list (parser);
7650
7651   return true;
7652 }
7653
7654 /* Parse a mem-initializer-list.
7655
7656    mem-initializer-list:
7657      mem-initializer
7658      mem-initializer , mem-initializer-list  */
7659
7660 static void
7661 cp_parser_mem_initializer_list (cp_parser* parser)
7662 {
7663   tree mem_initializer_list = NULL_TREE;
7664
7665   /* Let the semantic analysis code know that we are starting the
7666      mem-initializer-list.  */
7667   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7668     error ("only constructors take base initializers");
7669
7670   /* Loop through the list.  */
7671   while (true)
7672     {
7673       tree mem_initializer;
7674
7675       /* Parse the mem-initializer.  */
7676       mem_initializer = cp_parser_mem_initializer (parser);
7677       /* Add it to the list, unless it was erroneous.  */
7678       if (mem_initializer)
7679         {
7680           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7681           mem_initializer_list = mem_initializer;
7682         }
7683       /* If the next token is not a `,', we're done.  */
7684       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7685         break;
7686       /* Consume the `,' token.  */
7687       cp_lexer_consume_token (parser->lexer);
7688     }
7689
7690   /* Perform semantic analysis.  */
7691   if (DECL_CONSTRUCTOR_P (current_function_decl))
7692     finish_mem_initializers (mem_initializer_list);
7693 }
7694
7695 /* Parse a mem-initializer.
7696
7697    mem-initializer:
7698      mem-initializer-id ( expression-list [opt] )
7699
7700    GNU extension:
7701
7702    mem-initializer:
7703      ( expression-list [opt] )
7704
7705    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7706    class) or FIELD_DECL (for a non-static data member) to initialize;
7707    the TREE_VALUE is the expression-list.  */
7708
7709 static tree
7710 cp_parser_mem_initializer (cp_parser* parser)
7711 {
7712   tree mem_initializer_id;
7713   tree expression_list;
7714   tree member;
7715
7716   /* Find out what is being initialized.  */
7717   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7718     {
7719       pedwarn ("anachronistic old-style base class initializer");
7720       mem_initializer_id = NULL_TREE;
7721     }
7722   else
7723     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7724   member = expand_member_init (mem_initializer_id);
7725   if (member && !DECL_P (member))
7726     in_base_initializer = 1;
7727
7728   expression_list
7729     = cp_parser_parenthesized_expression_list (parser, false,
7730                                                /*cast_p=*/false,
7731                                                /*non_constant_p=*/NULL);
7732   if (!expression_list)
7733     expression_list = void_type_node;
7734
7735   in_base_initializer = 0;
7736
7737   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7738 }
7739
7740 /* Parse a mem-initializer-id.
7741
7742    mem-initializer-id:
7743      :: [opt] nested-name-specifier [opt] class-name
7744      identifier
7745
7746    Returns a TYPE indicating the class to be initializer for the first
7747    production.  Returns an IDENTIFIER_NODE indicating the data member
7748    to be initialized for the second production.  */
7749
7750 static tree
7751 cp_parser_mem_initializer_id (cp_parser* parser)
7752 {
7753   bool global_scope_p;
7754   bool nested_name_specifier_p;
7755   bool template_p = false;
7756   tree id;
7757
7758   /* `typename' is not allowed in this context ([temp.res]).  */
7759   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7760     {
7761       error ("keyword %<typename%> not allowed in this context (a qualified "
7762              "member initializer is implicitly a type)");
7763       cp_lexer_consume_token (parser->lexer);
7764     }
7765   /* Look for the optional `::' operator.  */
7766   global_scope_p
7767     = (cp_parser_global_scope_opt (parser,
7768                                    /*current_scope_valid_p=*/false)
7769        != NULL_TREE);
7770   /* Look for the optional nested-name-specifier.  The simplest way to
7771      implement:
7772
7773        [temp.res]
7774
7775        The keyword `typename' is not permitted in a base-specifier or
7776        mem-initializer; in these contexts a qualified name that
7777        depends on a template-parameter is implicitly assumed to be a
7778        type name.
7779
7780      is to assume that we have seen the `typename' keyword at this
7781      point.  */
7782   nested_name_specifier_p
7783     = (cp_parser_nested_name_specifier_opt (parser,
7784                                             /*typename_keyword_p=*/true,
7785                                             /*check_dependency_p=*/true,
7786                                             /*type_p=*/true,
7787                                             /*is_declaration=*/true)
7788        != NULL_TREE);
7789   if (nested_name_specifier_p)
7790     template_p = cp_parser_optional_template_keyword (parser);
7791   /* If there is a `::' operator or a nested-name-specifier, then we
7792      are definitely looking for a class-name.  */
7793   if (global_scope_p || nested_name_specifier_p)
7794     return cp_parser_class_name (parser,
7795                                  /*typename_keyword_p=*/true,
7796                                  /*template_keyword_p=*/template_p,
7797                                  none_type,
7798                                  /*check_dependency_p=*/true,
7799                                  /*class_head_p=*/false,
7800                                  /*is_declaration=*/true);
7801   /* Otherwise, we could also be looking for an ordinary identifier.  */
7802   cp_parser_parse_tentatively (parser);
7803   /* Try a class-name.  */
7804   id = cp_parser_class_name (parser,
7805                              /*typename_keyword_p=*/true,
7806                              /*template_keyword_p=*/false,
7807                              none_type,
7808                              /*check_dependency_p=*/true,
7809                              /*class_head_p=*/false,
7810                              /*is_declaration=*/true);
7811   /* If we found one, we're done.  */
7812   if (cp_parser_parse_definitely (parser))
7813     return id;
7814   /* Otherwise, look for an ordinary identifier.  */
7815   return cp_parser_identifier (parser);
7816 }
7817
7818 /* Overloading [gram.over] */
7819
7820 /* Parse an operator-function-id.
7821
7822    operator-function-id:
7823      operator operator
7824
7825    Returns an IDENTIFIER_NODE for the operator which is a
7826    human-readable spelling of the identifier, e.g., `operator +'.  */
7827
7828 static tree
7829 cp_parser_operator_function_id (cp_parser* parser)
7830 {
7831   /* Look for the `operator' keyword.  */
7832   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7833     return error_mark_node;
7834   /* And then the name of the operator itself.  */
7835   return cp_parser_operator (parser);
7836 }
7837
7838 /* Parse an operator.
7839
7840    operator:
7841      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7842      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7843      || ++ -- , ->* -> () []
7844
7845    GNU Extensions:
7846
7847    operator:
7848      <? >? <?= >?=
7849
7850    Returns an IDENTIFIER_NODE for the operator which is a
7851    human-readable spelling of the identifier, e.g., `operator +'.  */
7852
7853 static tree
7854 cp_parser_operator (cp_parser* parser)
7855 {
7856   tree id = NULL_TREE;
7857   cp_token *token;
7858
7859   /* Peek at the next token.  */
7860   token = cp_lexer_peek_token (parser->lexer);
7861   /* Figure out which operator we have.  */
7862   switch (token->type)
7863     {
7864     case CPP_KEYWORD:
7865       {
7866         enum tree_code op;
7867
7868         /* The keyword should be either `new' or `delete'.  */
7869         if (token->keyword == RID_NEW)
7870           op = NEW_EXPR;
7871         else if (token->keyword == RID_DELETE)
7872           op = DELETE_EXPR;
7873         else
7874           break;
7875
7876         /* Consume the `new' or `delete' token.  */
7877         cp_lexer_consume_token (parser->lexer);
7878
7879         /* Peek at the next token.  */
7880         token = cp_lexer_peek_token (parser->lexer);
7881         /* If it's a `[' token then this is the array variant of the
7882            operator.  */
7883         if (token->type == CPP_OPEN_SQUARE)
7884           {
7885             /* Consume the `[' token.  */
7886             cp_lexer_consume_token (parser->lexer);
7887             /* Look for the `]' token.  */
7888             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7889             id = ansi_opname (op == NEW_EXPR
7890                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7891           }
7892         /* Otherwise, we have the non-array variant.  */
7893         else
7894           id = ansi_opname (op);
7895
7896         return id;
7897       }
7898
7899     case CPP_PLUS:
7900       id = ansi_opname (PLUS_EXPR);
7901       break;
7902
7903     case CPP_MINUS:
7904       id = ansi_opname (MINUS_EXPR);
7905       break;
7906
7907     case CPP_MULT:
7908       id = ansi_opname (MULT_EXPR);
7909       break;
7910
7911     case CPP_DIV:
7912       id = ansi_opname (TRUNC_DIV_EXPR);
7913       break;
7914
7915     case CPP_MOD:
7916       id = ansi_opname (TRUNC_MOD_EXPR);
7917       break;
7918
7919     case CPP_XOR:
7920       id = ansi_opname (BIT_XOR_EXPR);
7921       break;
7922
7923     case CPP_AND:
7924       id = ansi_opname (BIT_AND_EXPR);
7925       break;
7926
7927     case CPP_OR:
7928       id = ansi_opname (BIT_IOR_EXPR);
7929       break;
7930
7931     case CPP_COMPL:
7932       id = ansi_opname (BIT_NOT_EXPR);
7933       break;
7934
7935     case CPP_NOT:
7936       id = ansi_opname (TRUTH_NOT_EXPR);
7937       break;
7938
7939     case CPP_EQ:
7940       id = ansi_assopname (NOP_EXPR);
7941       break;
7942
7943     case CPP_LESS:
7944       id = ansi_opname (LT_EXPR);
7945       break;
7946
7947     case CPP_GREATER:
7948       id = ansi_opname (GT_EXPR);
7949       break;
7950
7951     case CPP_PLUS_EQ:
7952       id = ansi_assopname (PLUS_EXPR);
7953       break;
7954
7955     case CPP_MINUS_EQ:
7956       id = ansi_assopname (MINUS_EXPR);
7957       break;
7958
7959     case CPP_MULT_EQ:
7960       id = ansi_assopname (MULT_EXPR);
7961       break;
7962
7963     case CPP_DIV_EQ:
7964       id = ansi_assopname (TRUNC_DIV_EXPR);
7965       break;
7966
7967     case CPP_MOD_EQ:
7968       id = ansi_assopname (TRUNC_MOD_EXPR);
7969       break;
7970
7971     case CPP_XOR_EQ:
7972       id = ansi_assopname (BIT_XOR_EXPR);
7973       break;
7974
7975     case CPP_AND_EQ:
7976       id = ansi_assopname (BIT_AND_EXPR);
7977       break;
7978
7979     case CPP_OR_EQ:
7980       id = ansi_assopname (BIT_IOR_EXPR);
7981       break;
7982
7983     case CPP_LSHIFT:
7984       id = ansi_opname (LSHIFT_EXPR);
7985       break;
7986
7987     case CPP_RSHIFT:
7988       id = ansi_opname (RSHIFT_EXPR);
7989       break;
7990
7991     case CPP_LSHIFT_EQ:
7992       id = ansi_assopname (LSHIFT_EXPR);
7993       break;
7994
7995     case CPP_RSHIFT_EQ:
7996       id = ansi_assopname (RSHIFT_EXPR);
7997       break;
7998
7999     case CPP_EQ_EQ:
8000       id = ansi_opname (EQ_EXPR);
8001       break;
8002
8003     case CPP_NOT_EQ:
8004       id = ansi_opname (NE_EXPR);
8005       break;
8006
8007     case CPP_LESS_EQ:
8008       id = ansi_opname (LE_EXPR);
8009       break;
8010
8011     case CPP_GREATER_EQ:
8012       id = ansi_opname (GE_EXPR);
8013       break;
8014
8015     case CPP_AND_AND:
8016       id = ansi_opname (TRUTH_ANDIF_EXPR);
8017       break;
8018
8019     case CPP_OR_OR:
8020       id = ansi_opname (TRUTH_ORIF_EXPR);
8021       break;
8022
8023     case CPP_PLUS_PLUS:
8024       id = ansi_opname (POSTINCREMENT_EXPR);
8025       break;
8026
8027     case CPP_MINUS_MINUS:
8028       id = ansi_opname (PREDECREMENT_EXPR);
8029       break;
8030
8031     case CPP_COMMA:
8032       id = ansi_opname (COMPOUND_EXPR);
8033       break;
8034
8035     case CPP_DEREF_STAR:
8036       id = ansi_opname (MEMBER_REF);
8037       break;
8038
8039     case CPP_DEREF:
8040       id = ansi_opname (COMPONENT_REF);
8041       break;
8042
8043     case CPP_OPEN_PAREN:
8044       /* Consume the `('.  */
8045       cp_lexer_consume_token (parser->lexer);
8046       /* Look for the matching `)'.  */
8047       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8048       return ansi_opname (CALL_EXPR);
8049
8050     case CPP_OPEN_SQUARE:
8051       /* Consume the `['.  */
8052       cp_lexer_consume_token (parser->lexer);
8053       /* Look for the matching `]'.  */
8054       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8055       return ansi_opname (ARRAY_REF);
8056
8057       /* Extensions.  */
8058     case CPP_MIN:
8059       id = ansi_opname (MIN_EXPR);
8060       cp_parser_warn_min_max ();
8061       break;
8062
8063     case CPP_MAX:
8064       id = ansi_opname (MAX_EXPR);
8065       cp_parser_warn_min_max ();
8066       break;
8067
8068     case CPP_MIN_EQ:
8069       id = ansi_assopname (MIN_EXPR);
8070       cp_parser_warn_min_max ();
8071       break;
8072
8073     case CPP_MAX_EQ:
8074       id = ansi_assopname (MAX_EXPR);
8075       cp_parser_warn_min_max ();
8076       break;
8077
8078     default:
8079       /* Anything else is an error.  */
8080       break;
8081     }
8082
8083   /* If we have selected an identifier, we need to consume the
8084      operator token.  */
8085   if (id)
8086     cp_lexer_consume_token (parser->lexer);
8087   /* Otherwise, no valid operator name was present.  */
8088   else
8089     {
8090       cp_parser_error (parser, "expected operator");
8091       id = error_mark_node;
8092     }
8093
8094   return id;
8095 }
8096
8097 /* Parse a template-declaration.
8098
8099    template-declaration:
8100      export [opt] template < template-parameter-list > declaration
8101
8102    If MEMBER_P is TRUE, this template-declaration occurs within a
8103    class-specifier.
8104
8105    The grammar rule given by the standard isn't correct.  What
8106    is really meant is:
8107
8108    template-declaration:
8109      export [opt] template-parameter-list-seq
8110        decl-specifier-seq [opt] init-declarator [opt] ;
8111      export [opt] template-parameter-list-seq
8112        function-definition
8113
8114    template-parameter-list-seq:
8115      template-parameter-list-seq [opt]
8116      template < template-parameter-list >  */
8117
8118 static void
8119 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8120 {
8121   /* Check for `export'.  */
8122   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8123     {
8124       /* Consume the `export' token.  */
8125       cp_lexer_consume_token (parser->lexer);
8126       /* Warn that we do not support `export'.  */
8127       warning ("keyword %<export%> not implemented, and will be ignored");
8128     }
8129
8130   cp_parser_template_declaration_after_export (parser, member_p);
8131 }
8132
8133 /* Parse a template-parameter-list.
8134
8135    template-parameter-list:
8136      template-parameter
8137      template-parameter-list , template-parameter
8138
8139    Returns a TREE_LIST.  Each node represents a template parameter.
8140    The nodes are connected via their TREE_CHAINs.  */
8141
8142 static tree
8143 cp_parser_template_parameter_list (cp_parser* parser)
8144 {
8145   tree parameter_list = NULL_TREE;
8146
8147   while (true)
8148     {
8149       tree parameter;
8150       cp_token *token;
8151       bool is_non_type;
8152
8153       /* Parse the template-parameter.  */
8154       parameter = cp_parser_template_parameter (parser, &is_non_type);
8155       /* Add it to the list.  */
8156       if (parameter != error_mark_node)
8157         parameter_list = process_template_parm (parameter_list,
8158                                                 parameter,
8159                                                 is_non_type);
8160       /* Peek at the next token.  */
8161       token = cp_lexer_peek_token (parser->lexer);
8162       /* If it's not a `,', we're done.  */
8163       if (token->type != CPP_COMMA)
8164         break;
8165       /* Otherwise, consume the `,' token.  */
8166       cp_lexer_consume_token (parser->lexer);
8167     }
8168
8169   return parameter_list;
8170 }
8171
8172 /* Parse a template-parameter.
8173
8174    template-parameter:
8175      type-parameter
8176      parameter-declaration
8177
8178    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8179    the parameter.  The TREE_PURPOSE is the default value, if any.
8180    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8181    iff this parameter is a non-type parameter.  */
8182
8183 static tree
8184 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8185 {
8186   cp_token *token;
8187   cp_parameter_declarator *parameter_declarator;
8188   tree parm;
8189
8190   /* Assume it is a type parameter or a template parameter.  */
8191   *is_non_type = false;
8192   /* Peek at the next token.  */
8193   token = cp_lexer_peek_token (parser->lexer);
8194   /* If it is `class' or `template', we have a type-parameter.  */
8195   if (token->keyword == RID_TEMPLATE)
8196     return cp_parser_type_parameter (parser);
8197   /* If it is `class' or `typename' we do not know yet whether it is a
8198      type parameter or a non-type parameter.  Consider:
8199
8200        template <typename T, typename T::X X> ...
8201
8202      or:
8203
8204        template <class C, class D*> ...
8205
8206      Here, the first parameter is a type parameter, and the second is
8207      a non-type parameter.  We can tell by looking at the token after
8208      the identifier -- if it is a `,', `=', or `>' then we have a type
8209      parameter.  */
8210   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8211     {
8212       /* Peek at the token after `class' or `typename'.  */
8213       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8214       /* If it's an identifier, skip it.  */
8215       if (token->type == CPP_NAME)
8216         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8217       /* Now, see if the token looks like the end of a template
8218          parameter.  */
8219       if (token->type == CPP_COMMA
8220           || token->type == CPP_EQ
8221           || token->type == CPP_GREATER)
8222         return cp_parser_type_parameter (parser);
8223     }
8224
8225   /* Otherwise, it is a non-type parameter.
8226
8227      [temp.param]
8228
8229      When parsing a default template-argument for a non-type
8230      template-parameter, the first non-nested `>' is taken as the end
8231      of the template parameter-list rather than a greater-than
8232      operator.  */
8233   *is_non_type = true;
8234   parameter_declarator
8235      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8236                                         /*parenthesized_p=*/NULL);
8237   parm = grokdeclarator (parameter_declarator->declarator,
8238                          &parameter_declarator->decl_specifiers,
8239                          PARM, /*initialized=*/0,
8240                          /*attrlist=*/NULL);
8241   if (parm == error_mark_node)
8242     return error_mark_node;
8243   return build_tree_list (parameter_declarator->default_argument, parm);
8244 }
8245
8246 /* Parse a type-parameter.
8247
8248    type-parameter:
8249      class identifier [opt]
8250      class identifier [opt] = type-id
8251      typename identifier [opt]
8252      typename identifier [opt] = type-id
8253      template < template-parameter-list > class identifier [opt]
8254      template < template-parameter-list > class identifier [opt]
8255        = id-expression
8256
8257    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8258    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8259    the declaration of the parameter.  */
8260
8261 static tree
8262 cp_parser_type_parameter (cp_parser* parser)
8263 {
8264   cp_token *token;
8265   tree parameter;
8266
8267   /* Look for a keyword to tell us what kind of parameter this is.  */
8268   token = cp_parser_require (parser, CPP_KEYWORD,
8269                              "`class', `typename', or `template'");
8270   if (!token)
8271     return error_mark_node;
8272
8273   switch (token->keyword)
8274     {
8275     case RID_CLASS:
8276     case RID_TYPENAME:
8277       {
8278         tree identifier;
8279         tree default_argument;
8280
8281         /* If the next token is an identifier, then it names the
8282            parameter.  */
8283         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8284           identifier = cp_parser_identifier (parser);
8285         else
8286           identifier = NULL_TREE;
8287
8288         /* Create the parameter.  */
8289         parameter = finish_template_type_parm (class_type_node, identifier);
8290
8291         /* If the next token is an `=', we have a default argument.  */
8292         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8293           {
8294             /* Consume the `=' token.  */
8295             cp_lexer_consume_token (parser->lexer);
8296             /* Parse the default-argument.  */
8297             default_argument = cp_parser_type_id (parser);
8298           }
8299         else
8300           default_argument = NULL_TREE;
8301
8302         /* Create the combined representation of the parameter and the
8303            default argument.  */
8304         parameter = build_tree_list (default_argument, parameter);
8305       }
8306       break;
8307
8308     case RID_TEMPLATE:
8309       {
8310         tree parameter_list;
8311         tree identifier;
8312         tree default_argument;
8313
8314         /* Look for the `<'.  */
8315         cp_parser_require (parser, CPP_LESS, "`<'");
8316         /* Parse the template-parameter-list.  */
8317         begin_template_parm_list ();
8318         parameter_list
8319           = cp_parser_template_parameter_list (parser);
8320         parameter_list = end_template_parm_list (parameter_list);
8321         /* Look for the `>'.  */
8322         cp_parser_require (parser, CPP_GREATER, "`>'");
8323         /* Look for the `class' keyword.  */
8324         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8325         /* If the next token is an `=', then there is a
8326            default-argument.  If the next token is a `>', we are at
8327            the end of the parameter-list.  If the next token is a `,',
8328            then we are at the end of this parameter.  */
8329         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8330             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8331             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8332           {
8333             identifier = cp_parser_identifier (parser);
8334             /* Treat invalid names as if the parameter were nameless.  */
8335             if (identifier == error_mark_node)
8336               identifier = NULL_TREE;
8337           }
8338         else
8339           identifier = NULL_TREE;
8340
8341         /* Create the template parameter.  */
8342         parameter = finish_template_template_parm (class_type_node,
8343                                                    identifier);
8344
8345         /* If the next token is an `=', then there is a
8346            default-argument.  */
8347         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8348           {
8349             bool is_template;
8350
8351             /* Consume the `='.  */
8352             cp_lexer_consume_token (parser->lexer);
8353             /* Parse the id-expression.  */
8354             default_argument
8355               = cp_parser_id_expression (parser,
8356                                          /*template_keyword_p=*/false,
8357                                          /*check_dependency_p=*/true,
8358                                          /*template_p=*/&is_template,
8359                                          /*declarator_p=*/false);
8360             if (TREE_CODE (default_argument) == TYPE_DECL)
8361               /* If the id-expression was a template-id that refers to
8362                  a template-class, we already have the declaration here,
8363                  so no further lookup is needed.  */
8364                  ;
8365             else
8366               /* Look up the name.  */
8367               default_argument
8368                 = cp_parser_lookup_name (parser, default_argument,
8369                                          none_type,
8370                                          /*is_template=*/is_template,
8371                                          /*is_namespace=*/false,
8372                                          /*check_dependency=*/true,
8373                                          /*ambiguous_p=*/NULL);
8374             /* See if the default argument is valid.  */
8375             default_argument
8376               = check_template_template_default_arg (default_argument);
8377           }
8378         else
8379           default_argument = NULL_TREE;
8380
8381         /* Create the combined representation of the parameter and the
8382            default argument.  */
8383         parameter = build_tree_list (default_argument, parameter);
8384       }
8385       break;
8386
8387     default:
8388       gcc_unreachable ();
8389       break;
8390     }
8391
8392   return parameter;
8393 }
8394
8395 /* Parse a template-id.
8396
8397    template-id:
8398      template-name < template-argument-list [opt] >
8399
8400    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8401    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8402    returned.  Otherwise, if the template-name names a function, or set
8403    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8404    names a class, returns a TYPE_DECL for the specialization.
8405
8406    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8407    uninstantiated templates.  */
8408
8409 static tree
8410 cp_parser_template_id (cp_parser *parser,
8411                        bool template_keyword_p,
8412                        bool check_dependency_p,
8413                        bool is_declaration)
8414 {
8415   tree template;
8416   tree arguments;
8417   tree template_id;
8418   cp_token_position start_of_id = 0;
8419   tree access_check = NULL_TREE;
8420   cp_token *next_token, *next_token_2;
8421   bool is_identifier;
8422
8423   /* If the next token corresponds to a template-id, there is no need
8424      to reparse it.  */
8425   next_token = cp_lexer_peek_token (parser->lexer);
8426   if (next_token->type == CPP_TEMPLATE_ID)
8427     {
8428       tree value;
8429       tree check;
8430
8431       /* Get the stored value.  */
8432       value = cp_lexer_consume_token (parser->lexer)->value;
8433       /* Perform any access checks that were deferred.  */
8434       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8435         perform_or_defer_access_check (TREE_PURPOSE (check),
8436                                        TREE_VALUE (check));
8437       /* Return the stored value.  */
8438       return TREE_VALUE (value);
8439     }
8440
8441   /* Avoid performing name lookup if there is no possibility of
8442      finding a template-id.  */
8443   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8444       || (next_token->type == CPP_NAME
8445           && !cp_parser_nth_token_starts_template_argument_list_p
8446                (parser, 2)))
8447     {
8448       cp_parser_error (parser, "expected template-id");
8449       return error_mark_node;
8450     }
8451
8452   /* Remember where the template-id starts.  */
8453   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8454     start_of_id = cp_lexer_token_position (parser->lexer, false);
8455
8456   push_deferring_access_checks (dk_deferred);
8457
8458   /* Parse the template-name.  */
8459   is_identifier = false;
8460   template = cp_parser_template_name (parser, template_keyword_p,
8461                                       check_dependency_p,
8462                                       is_declaration,
8463                                       &is_identifier);
8464   if (template == error_mark_node || is_identifier)
8465     {
8466       pop_deferring_access_checks ();
8467       return template;
8468     }
8469
8470   /* If we find the sequence `[:' after a template-name, it's probably
8471      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8472      parse correctly the argument list.  */
8473   next_token = cp_lexer_peek_token (parser->lexer);
8474   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8475   if (next_token->type == CPP_OPEN_SQUARE
8476       && next_token->flags & DIGRAPH
8477       && next_token_2->type == CPP_COLON
8478       && !(next_token_2->flags & PREV_WHITE))
8479     {
8480       cp_parser_parse_tentatively (parser);
8481       /* Change `:' into `::'.  */
8482       next_token_2->type = CPP_SCOPE;
8483       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8484          CPP_LESS.  */
8485       cp_lexer_consume_token (parser->lexer);
8486       /* Parse the arguments.  */
8487       arguments = cp_parser_enclosed_template_argument_list (parser);
8488       if (!cp_parser_parse_definitely (parser))
8489         {
8490           /* If we couldn't parse an argument list, then we revert our changes
8491              and return simply an error. Maybe this is not a template-id
8492              after all.  */
8493           next_token_2->type = CPP_COLON;
8494           cp_parser_error (parser, "expected %<<%>");
8495           pop_deferring_access_checks ();
8496           return error_mark_node;
8497         }
8498       /* Otherwise, emit an error about the invalid digraph, but continue
8499          parsing because we got our argument list.  */
8500       pedwarn ("%<<::%> cannot begin a template-argument list");
8501       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8502               "between %<<%> and %<::%>");
8503       if (!flag_permissive)
8504         {
8505           static bool hint;
8506           if (!hint)
8507             {
8508               inform ("(if you use -fpermissive G++ will accept your code)");
8509               hint = true;
8510             }
8511         }
8512     }
8513   else
8514     {
8515       /* Look for the `<' that starts the template-argument-list.  */
8516       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8517         {
8518           pop_deferring_access_checks ();
8519           return error_mark_node;
8520         }
8521       /* Parse the arguments.  */
8522       arguments = cp_parser_enclosed_template_argument_list (parser);
8523     }
8524
8525   /* Build a representation of the specialization.  */
8526   if (TREE_CODE (template) == IDENTIFIER_NODE)
8527     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8528   else if (DECL_CLASS_TEMPLATE_P (template)
8529            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8530     template_id
8531       = finish_template_type (template, arguments,
8532                               cp_lexer_next_token_is (parser->lexer,
8533                                                       CPP_SCOPE));
8534   else
8535     {
8536       /* If it's not a class-template or a template-template, it should be
8537          a function-template.  */
8538       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8539                    || TREE_CODE (template) == OVERLOAD
8540                    || BASELINK_P (template)));
8541
8542       template_id = lookup_template_function (template, arguments);
8543     }
8544
8545   /* Retrieve any deferred checks.  Do not pop this access checks yet
8546      so the memory will not be reclaimed during token replacing below.  */
8547   access_check = get_deferred_access_checks ();
8548
8549   /* If parsing tentatively, replace the sequence of tokens that makes
8550      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8551      should we re-parse the token stream, we will not have to repeat
8552      the effort required to do the parse, nor will we issue duplicate
8553      error messages about problems during instantiation of the
8554      template.  */
8555   if (start_of_id)
8556     {
8557       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8558       
8559       /* Reset the contents of the START_OF_ID token.  */
8560       token->type = CPP_TEMPLATE_ID;
8561       token->value = build_tree_list (access_check, template_id);
8562       token->keyword = RID_MAX;
8563       
8564       /* Purge all subsequent tokens.  */
8565       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8566
8567       /* ??? Can we actually assume that, if template_id ==
8568          error_mark_node, we will have issued a diagnostic to the
8569          user, as opposed to simply marking the tentative parse as
8570          failed?  */
8571       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8572         error ("parse error in template argument list");
8573     }
8574
8575   pop_deferring_access_checks ();
8576   return template_id;
8577 }
8578
8579 /* Parse a template-name.
8580
8581    template-name:
8582      identifier
8583
8584    The standard should actually say:
8585
8586    template-name:
8587      identifier
8588      operator-function-id
8589
8590    A defect report has been filed about this issue.
8591
8592    A conversion-function-id cannot be a template name because they cannot
8593    be part of a template-id. In fact, looking at this code:
8594
8595    a.operator K<int>()
8596
8597    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8598    It is impossible to call a templated conversion-function-id with an
8599    explicit argument list, since the only allowed template parameter is
8600    the type to which it is converting.
8601
8602    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8603    `template' keyword, in a construction like:
8604
8605      T::template f<3>()
8606
8607    In that case `f' is taken to be a template-name, even though there
8608    is no way of knowing for sure.
8609
8610    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8611    name refers to a set of overloaded functions, at least one of which
8612    is a template, or an IDENTIFIER_NODE with the name of the template,
8613    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8614    names are looked up inside uninstantiated templates.  */
8615
8616 static tree
8617 cp_parser_template_name (cp_parser* parser,
8618                          bool template_keyword_p,
8619                          bool check_dependency_p,
8620                          bool is_declaration,
8621                          bool *is_identifier)
8622 {
8623   tree identifier;
8624   tree decl;
8625   tree fns;
8626
8627   /* If the next token is `operator', then we have either an
8628      operator-function-id or a conversion-function-id.  */
8629   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8630     {
8631       /* We don't know whether we're looking at an
8632          operator-function-id or a conversion-function-id.  */
8633       cp_parser_parse_tentatively (parser);
8634       /* Try an operator-function-id.  */
8635       identifier = cp_parser_operator_function_id (parser);
8636       /* If that didn't work, try a conversion-function-id.  */
8637       if (!cp_parser_parse_definitely (parser))
8638         {
8639           cp_parser_error (parser, "expected template-name");
8640           return error_mark_node;
8641         }
8642     }
8643   /* Look for the identifier.  */
8644   else
8645     identifier = cp_parser_identifier (parser);
8646
8647   /* If we didn't find an identifier, we don't have a template-id.  */
8648   if (identifier == error_mark_node)
8649     return error_mark_node;
8650
8651   /* If the name immediately followed the `template' keyword, then it
8652      is a template-name.  However, if the next token is not `<', then
8653      we do not treat it as a template-name, since it is not being used
8654      as part of a template-id.  This enables us to handle constructs
8655      like:
8656
8657        template <typename T> struct S { S(); };
8658        template <typename T> S<T>::S();
8659
8660      correctly.  We would treat `S' as a template -- if it were `S<T>'
8661      -- but we do not if there is no `<'.  */
8662
8663   if (processing_template_decl
8664       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8665     {
8666       /* In a declaration, in a dependent context, we pretend that the
8667          "template" keyword was present in order to improve error
8668          recovery.  For example, given:
8669
8670            template <typename T> void f(T::X<int>);
8671
8672          we want to treat "X<int>" as a template-id.  */
8673       if (is_declaration
8674           && !template_keyword_p
8675           && parser->scope && TYPE_P (parser->scope)
8676           && check_dependency_p
8677           && dependent_type_p (parser->scope)
8678           /* Do not do this for dtors (or ctors), since they never
8679              need the template keyword before their name.  */
8680           && !constructor_name_p (identifier, parser->scope))
8681         {
8682           cp_token_position start = 0;
8683           
8684           /* Explain what went wrong.  */
8685           error ("non-template %qD used as template", identifier);
8686           inform ("use %<%T::template %D%> to indicate that it is a template",
8687                   parser->scope, identifier);
8688           /* If parsing tentatively, find the location of the "<" token.  */
8689           if (cp_parser_simulate_error (parser))
8690             start = cp_lexer_token_position (parser->lexer, true);
8691           /* Parse the template arguments so that we can issue error
8692              messages about them.  */
8693           cp_lexer_consume_token (parser->lexer);
8694           cp_parser_enclosed_template_argument_list (parser);
8695           /* Skip tokens until we find a good place from which to
8696              continue parsing.  */
8697           cp_parser_skip_to_closing_parenthesis (parser,
8698                                                  /*recovering=*/true,
8699                                                  /*or_comma=*/true,
8700                                                  /*consume_paren=*/false);
8701           /* If parsing tentatively, permanently remove the
8702              template argument list.  That will prevent duplicate
8703              error messages from being issued about the missing
8704              "template" keyword.  */
8705           if (start)
8706             cp_lexer_purge_tokens_after (parser->lexer, start);
8707           if (is_identifier)
8708             *is_identifier = true;
8709           return identifier;
8710         }
8711
8712       /* If the "template" keyword is present, then there is generally
8713          no point in doing name-lookup, so we just return IDENTIFIER.
8714          But, if the qualifying scope is non-dependent then we can
8715          (and must) do name-lookup normally.  */
8716       if (template_keyword_p
8717           && (!parser->scope
8718               || (TYPE_P (parser->scope)
8719                   && dependent_type_p (parser->scope))))
8720         return identifier;
8721     }
8722
8723   /* Look up the name.  */
8724   decl = cp_parser_lookup_name (parser, identifier,
8725                                 none_type,
8726                                 /*is_template=*/false,
8727                                 /*is_namespace=*/false,
8728                                 check_dependency_p,
8729                                 /*ambiguous_p=*/NULL);
8730   decl = maybe_get_template_decl_from_type_decl (decl);
8731
8732   /* If DECL is a template, then the name was a template-name.  */
8733   if (TREE_CODE (decl) == TEMPLATE_DECL)
8734     ;
8735   else
8736     {
8737       tree fn = NULL_TREE;
8738
8739       /* The standard does not explicitly indicate whether a name that
8740          names a set of overloaded declarations, some of which are
8741          templates, is a template-name.  However, such a name should
8742          be a template-name; otherwise, there is no way to form a
8743          template-id for the overloaded templates.  */
8744       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8745       if (TREE_CODE (fns) == OVERLOAD)
8746         for (fn = fns; fn; fn = OVL_NEXT (fn))
8747           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8748             break;
8749
8750       if (!fn)
8751         {
8752           /* The name does not name a template.  */
8753           cp_parser_error (parser, "expected template-name");
8754           return error_mark_node;
8755         }
8756     }
8757
8758   /* If DECL is dependent, and refers to a function, then just return
8759      its name; we will look it up again during template instantiation.  */
8760   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8761     {
8762       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8763       if (TYPE_P (scope) && dependent_type_p (scope))
8764         return identifier;
8765     }
8766
8767   return decl;
8768 }
8769
8770 /* Parse a template-argument-list.
8771
8772    template-argument-list:
8773      template-argument
8774      template-argument-list , template-argument
8775
8776    Returns a TREE_VEC containing the arguments.  */
8777
8778 static tree
8779 cp_parser_template_argument_list (cp_parser* parser)
8780 {
8781   tree fixed_args[10];
8782   unsigned n_args = 0;
8783   unsigned alloced = 10;
8784   tree *arg_ary = fixed_args;
8785   tree vec;
8786   bool saved_in_template_argument_list_p;
8787
8788   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8789   parser->in_template_argument_list_p = true;
8790   do
8791     {
8792       tree argument;
8793
8794       if (n_args)
8795         /* Consume the comma.  */
8796         cp_lexer_consume_token (parser->lexer);
8797
8798       /* Parse the template-argument.  */
8799       argument = cp_parser_template_argument (parser);
8800       if (n_args == alloced)
8801         {
8802           alloced *= 2;
8803
8804           if (arg_ary == fixed_args)
8805             {
8806               arg_ary = xmalloc (sizeof (tree) * alloced);
8807               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8808             }
8809           else
8810             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8811         }
8812       arg_ary[n_args++] = argument;
8813     }
8814   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8815
8816   vec = make_tree_vec (n_args);
8817
8818   while (n_args--)
8819     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8820
8821   if (arg_ary != fixed_args)
8822     free (arg_ary);
8823   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8824   return vec;
8825 }
8826
8827 /* Parse a template-argument.
8828
8829    template-argument:
8830      assignment-expression
8831      type-id
8832      id-expression
8833
8834    The representation is that of an assignment-expression, type-id, or
8835    id-expression -- except that the qualified id-expression is
8836    evaluated, so that the value returned is either a DECL or an
8837    OVERLOAD.
8838
8839    Although the standard says "assignment-expression", it forbids
8840    throw-expressions or assignments in the template argument.
8841    Therefore, we use "conditional-expression" instead.  */
8842
8843 static tree
8844 cp_parser_template_argument (cp_parser* parser)
8845 {
8846   tree argument;
8847   bool template_p;
8848   bool address_p;
8849   bool maybe_type_id = false;
8850   cp_token *token;
8851   cp_id_kind idk;
8852   tree qualifying_class;
8853
8854   /* There's really no way to know what we're looking at, so we just
8855      try each alternative in order.
8856
8857        [temp.arg]
8858
8859        In a template-argument, an ambiguity between a type-id and an
8860        expression is resolved to a type-id, regardless of the form of
8861        the corresponding template-parameter.
8862
8863      Therefore, we try a type-id first.  */
8864   cp_parser_parse_tentatively (parser);
8865   argument = cp_parser_type_id (parser);
8866   /* If there was no error parsing the type-id but the next token is a '>>',
8867      we probably found a typo for '> >'. But there are type-id which are
8868      also valid expressions. For instance:
8869
8870      struct X { int operator >> (int); };
8871      template <int V> struct Foo {};
8872      Foo<X () >> 5> r;
8873
8874      Here 'X()' is a valid type-id of a function type, but the user just
8875      wanted to write the expression "X() >> 5". Thus, we remember that we
8876      found a valid type-id, but we still try to parse the argument as an
8877      expression to see what happens.  */
8878   if (!cp_parser_error_occurred (parser)
8879       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8880     {
8881       maybe_type_id = true;
8882       cp_parser_abort_tentative_parse (parser);
8883     }
8884   else
8885     {
8886       /* If the next token isn't a `,' or a `>', then this argument wasn't
8887       really finished. This means that the argument is not a valid
8888       type-id.  */
8889       if (!cp_parser_next_token_ends_template_argument_p (parser))
8890         cp_parser_error (parser, "expected template-argument");
8891       /* If that worked, we're done.  */
8892       if (cp_parser_parse_definitely (parser))
8893         return argument;
8894     }
8895   /* We're still not sure what the argument will be.  */
8896   cp_parser_parse_tentatively (parser);
8897   /* Try a template.  */
8898   argument = cp_parser_id_expression (parser,
8899                                       /*template_keyword_p=*/false,
8900                                       /*check_dependency_p=*/true,
8901                                       &template_p,
8902                                       /*declarator_p=*/false);
8903   /* If the next token isn't a `,' or a `>', then this argument wasn't
8904      really finished.  */
8905   if (!cp_parser_next_token_ends_template_argument_p (parser))
8906     cp_parser_error (parser, "expected template-argument");
8907   if (!cp_parser_error_occurred (parser))
8908     {
8909       /* Figure out what is being referred to.  If the id-expression
8910          was for a class template specialization, then we will have a
8911          TYPE_DECL at this point.  There is no need to do name lookup
8912          at this point in that case.  */
8913       if (TREE_CODE (argument) != TYPE_DECL)
8914         argument = cp_parser_lookup_name (parser, argument,
8915                                           none_type,
8916                                           /*is_template=*/template_p,
8917                                           /*is_namespace=*/false,
8918                                           /*check_dependency=*/true,
8919                                           /*ambiguous_p=*/NULL);
8920       if (TREE_CODE (argument) != TEMPLATE_DECL
8921           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8922         cp_parser_error (parser, "expected template-name");
8923     }
8924   if (cp_parser_parse_definitely (parser))
8925     return argument;
8926   /* It must be a non-type argument.  There permitted cases are given
8927      in [temp.arg.nontype]:
8928
8929      -- an integral constant-expression of integral or enumeration
8930         type; or
8931
8932      -- the name of a non-type template-parameter; or
8933
8934      -- the name of an object or function with external linkage...
8935
8936      -- the address of an object or function with external linkage...
8937
8938      -- a pointer to member...  */
8939   /* Look for a non-type template parameter.  */
8940   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8941     {
8942       cp_parser_parse_tentatively (parser);
8943       argument = cp_parser_primary_expression (parser,
8944                                                /*cast_p=*/false,
8945                                                &idk,
8946                                                &qualifying_class);
8947       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8948           || !cp_parser_next_token_ends_template_argument_p (parser))
8949         cp_parser_simulate_error (parser);
8950       if (cp_parser_parse_definitely (parser))
8951         return argument;
8952     }
8953
8954   /* If the next token is "&", the argument must be the address of an
8955      object or function with external linkage.  */
8956   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8957   if (address_p)
8958     cp_lexer_consume_token (parser->lexer);
8959   /* See if we might have an id-expression.  */
8960   token = cp_lexer_peek_token (parser->lexer);
8961   if (token->type == CPP_NAME
8962       || token->keyword == RID_OPERATOR
8963       || token->type == CPP_SCOPE
8964       || token->type == CPP_TEMPLATE_ID
8965       || token->type == CPP_NESTED_NAME_SPECIFIER)
8966     {
8967       cp_parser_parse_tentatively (parser);
8968       argument = cp_parser_primary_expression (parser,
8969                                                /*cast_p=*/false,
8970                                                &idk,
8971                                                &qualifying_class);
8972       if (cp_parser_error_occurred (parser)
8973           || !cp_parser_next_token_ends_template_argument_p (parser))
8974         cp_parser_abort_tentative_parse (parser);
8975       else
8976         {
8977           if (TREE_CODE (argument) == INDIRECT_REF)
8978             {
8979               gcc_assert (REFERENCE_REF_P (argument));
8980               argument = TREE_OPERAND (argument, 0);
8981             }
8982           
8983           if (qualifying_class)
8984             argument = finish_qualified_id_expr (qualifying_class,
8985                                                  argument,
8986                                                  /*done=*/true,
8987                                                  address_p);
8988           if (TREE_CODE (argument) == VAR_DECL)
8989             {
8990               /* A variable without external linkage might still be a
8991                  valid constant-expression, so no error is issued here
8992                  if the external-linkage check fails.  */
8993               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8994                 cp_parser_simulate_error (parser);
8995             }
8996           else if (is_overloaded_fn (argument))
8997             /* All overloaded functions are allowed; if the external
8998                linkage test does not pass, an error will be issued
8999                later.  */
9000             ;
9001           else if (address_p
9002                    && (TREE_CODE (argument) == OFFSET_REF
9003                        || TREE_CODE (argument) == SCOPE_REF))
9004             /* A pointer-to-member.  */
9005             ;
9006           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9007             ;
9008           else
9009             cp_parser_simulate_error (parser);
9010
9011           if (cp_parser_parse_definitely (parser))
9012             {
9013               if (address_p)
9014                 argument = build_x_unary_op (ADDR_EXPR, argument);
9015               return argument;
9016             }
9017         }
9018     }
9019   /* If the argument started with "&", there are no other valid
9020      alternatives at this point.  */
9021   if (address_p)
9022     {
9023       cp_parser_error (parser, "invalid non-type template argument");
9024       return error_mark_node;
9025     }
9026
9027   /* If the argument wasn't successfully parsed as a type-id followed
9028      by '>>', the argument can only be a constant expression now.
9029      Otherwise, we try parsing the constant-expression tentatively,
9030      because the argument could really be a type-id.  */
9031   if (maybe_type_id)
9032     cp_parser_parse_tentatively (parser);
9033   argument = cp_parser_constant_expression (parser,
9034                                             /*allow_non_constant_p=*/false,
9035                                             /*non_constant_p=*/NULL);
9036   argument = fold_non_dependent_expr (argument);
9037   if (!maybe_type_id)
9038     return argument;
9039   if (!cp_parser_next_token_ends_template_argument_p (parser))
9040     cp_parser_error (parser, "expected template-argument");
9041   if (cp_parser_parse_definitely (parser))
9042     return argument;
9043   /* We did our best to parse the argument as a non type-id, but that
9044      was the only alternative that matched (albeit with a '>' after
9045      it). We can assume it's just a typo from the user, and a
9046      diagnostic will then be issued.  */
9047   return cp_parser_type_id (parser);
9048 }
9049
9050 /* Parse an explicit-instantiation.
9051
9052    explicit-instantiation:
9053      template declaration
9054
9055    Although the standard says `declaration', what it really means is:
9056
9057    explicit-instantiation:
9058      template decl-specifier-seq [opt] declarator [opt] ;
9059
9060    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9061    supposed to be allowed.  A defect report has been filed about this
9062    issue.
9063
9064    GNU Extension:
9065
9066    explicit-instantiation:
9067      storage-class-specifier template
9068        decl-specifier-seq [opt] declarator [opt] ;
9069      function-specifier template
9070        decl-specifier-seq [opt] declarator [opt] ;  */
9071
9072 static void
9073 cp_parser_explicit_instantiation (cp_parser* parser)
9074 {
9075   int declares_class_or_enum;
9076   cp_decl_specifier_seq decl_specifiers;
9077   tree extension_specifier = NULL_TREE;
9078
9079   /* Look for an (optional) storage-class-specifier or
9080      function-specifier.  */
9081   if (cp_parser_allow_gnu_extensions_p (parser))
9082     {
9083       extension_specifier
9084         = cp_parser_storage_class_specifier_opt (parser);
9085       if (!extension_specifier)
9086         extension_specifier
9087           = cp_parser_function_specifier_opt (parser,
9088                                               /*decl_specs=*/NULL);
9089     }
9090
9091   /* Look for the `template' keyword.  */
9092   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9093   /* Let the front end know that we are processing an explicit
9094      instantiation.  */
9095   begin_explicit_instantiation ();
9096   /* [temp.explicit] says that we are supposed to ignore access
9097      control while processing explicit instantiation directives.  */
9098   push_deferring_access_checks (dk_no_check);
9099   /* Parse a decl-specifier-seq.  */
9100   cp_parser_decl_specifier_seq (parser,
9101                                 CP_PARSER_FLAGS_OPTIONAL,
9102                                 &decl_specifiers,
9103                                 &declares_class_or_enum);
9104   /* If there was exactly one decl-specifier, and it declared a class,
9105      and there's no declarator, then we have an explicit type
9106      instantiation.  */
9107   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9108     {
9109       tree type;
9110
9111       type = check_tag_decl (&decl_specifiers);
9112       /* Turn access control back on for names used during
9113          template instantiation.  */
9114       pop_deferring_access_checks ();
9115       if (type)
9116         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9117     }
9118   else
9119     {
9120       cp_declarator *declarator;
9121       tree decl;
9122
9123       /* Parse the declarator.  */
9124       declarator
9125         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9126                                 /*ctor_dtor_or_conv_p=*/NULL,
9127                                 /*parenthesized_p=*/NULL,
9128                                 /*member_p=*/false);
9129       if (declares_class_or_enum & 2)
9130         cp_parser_check_for_definition_in_return_type (declarator,
9131                                                        decl_specifiers.type);
9132       if (declarator != cp_error_declarator)
9133         {
9134           decl = grokdeclarator (declarator, &decl_specifiers,
9135                                  NORMAL, 0, NULL);
9136           /* Turn access control back on for names used during
9137              template instantiation.  */
9138           pop_deferring_access_checks ();
9139           /* Do the explicit instantiation.  */
9140           do_decl_instantiation (decl, extension_specifier);
9141         }
9142       else
9143         {
9144           pop_deferring_access_checks ();
9145           /* Skip the body of the explicit instantiation.  */
9146           cp_parser_skip_to_end_of_statement (parser);
9147         }
9148     }
9149   /* We're done with the instantiation.  */
9150   end_explicit_instantiation ();
9151
9152   cp_parser_consume_semicolon_at_end_of_statement (parser);
9153 }
9154
9155 /* Parse an explicit-specialization.
9156
9157    explicit-specialization:
9158      template < > declaration
9159
9160    Although the standard says `declaration', what it really means is:
9161
9162    explicit-specialization:
9163      template <> decl-specifier [opt] init-declarator [opt] ;
9164      template <> function-definition
9165      template <> explicit-specialization
9166      template <> template-declaration  */
9167
9168 static void
9169 cp_parser_explicit_specialization (cp_parser* parser)
9170 {
9171   /* Look for the `template' keyword.  */
9172   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9173   /* Look for the `<'.  */
9174   cp_parser_require (parser, CPP_LESS, "`<'");
9175   /* Look for the `>'.  */
9176   cp_parser_require (parser, CPP_GREATER, "`>'");
9177   /* We have processed another parameter list.  */
9178   ++parser->num_template_parameter_lists;
9179   /* Let the front end know that we are beginning a specialization.  */
9180   begin_specialization ();
9181
9182   /* If the next keyword is `template', we need to figure out whether
9183      or not we're looking a template-declaration.  */
9184   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9185     {
9186       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9187           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9188         cp_parser_template_declaration_after_export (parser,
9189                                                      /*member_p=*/false);
9190       else
9191         cp_parser_explicit_specialization (parser);
9192     }
9193   else
9194     /* Parse the dependent declaration.  */
9195     cp_parser_single_declaration (parser,
9196                                   /*member_p=*/false,
9197                                   /*friend_p=*/NULL);
9198
9199   /* We're done with the specialization.  */
9200   end_specialization ();
9201   /* We're done with this parameter list.  */
9202   --parser->num_template_parameter_lists;
9203 }
9204
9205 /* Parse a type-specifier.
9206
9207    type-specifier:
9208      simple-type-specifier
9209      class-specifier
9210      enum-specifier
9211      elaborated-type-specifier
9212      cv-qualifier
9213
9214    GNU Extension:
9215
9216    type-specifier:
9217      __complex__
9218
9219    Returns a representation of the type-specifier.  For a
9220    class-specifier, enum-specifier, or elaborated-type-specifier, a
9221    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9222
9223    The parser flags FLAGS is used to control type-specifier parsing.
9224
9225    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9226    in a decl-specifier-seq.
9227
9228    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9229    class-specifier, enum-specifier, or elaborated-type-specifier, then
9230    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9231    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9232    zero.
9233
9234    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9235    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9236    is set to FALSE.  */
9237
9238 static tree
9239 cp_parser_type_specifier (cp_parser* parser,
9240                           cp_parser_flags flags,
9241                           cp_decl_specifier_seq *decl_specs,
9242                           bool is_declaration,
9243                           int* declares_class_or_enum,
9244                           bool* is_cv_qualifier)
9245 {
9246   tree type_spec = NULL_TREE;
9247   cp_token *token;
9248   enum rid keyword;
9249   cp_decl_spec ds = ds_last;
9250
9251   /* Assume this type-specifier does not declare a new type.  */
9252   if (declares_class_or_enum)
9253     *declares_class_or_enum = 0;
9254   /* And that it does not specify a cv-qualifier.  */
9255   if (is_cv_qualifier)
9256     *is_cv_qualifier = false;
9257   /* Peek at the next token.  */
9258   token = cp_lexer_peek_token (parser->lexer);
9259
9260   /* If we're looking at a keyword, we can use that to guide the
9261      production we choose.  */
9262   keyword = token->keyword;
9263   switch (keyword)
9264     {
9265     case RID_ENUM:
9266       /* 'enum' [identifier] '{' introduces an enum-specifier;
9267          'enum' <anything else> introduces an elaborated-type-specifier.  */
9268       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9269           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9270               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9271                  == CPP_OPEN_BRACE))
9272         {
9273           if (parser->num_template_parameter_lists)
9274             {
9275               error ("template declaration of %qs", "enum");
9276               cp_parser_skip_to_end_of_block_or_statement (parser);
9277               type_spec = error_mark_node;
9278             }
9279           else
9280             type_spec = cp_parser_enum_specifier (parser);
9281
9282           if (declares_class_or_enum)
9283             *declares_class_or_enum = 2;
9284           if (decl_specs)
9285             cp_parser_set_decl_spec_type (decl_specs,
9286                                           type_spec,
9287                                           /*user_defined_p=*/true);
9288           return type_spec;
9289         }
9290       else
9291         goto elaborated_type_specifier;
9292
9293       /* Any of these indicate either a class-specifier, or an
9294          elaborated-type-specifier.  */
9295     case RID_CLASS:
9296     case RID_STRUCT:
9297     case RID_UNION:
9298       /* Parse tentatively so that we can back up if we don't find a
9299          class-specifier.  */
9300       cp_parser_parse_tentatively (parser);
9301       /* Look for the class-specifier.  */
9302       type_spec = cp_parser_class_specifier (parser);
9303       /* If that worked, we're done.  */
9304       if (cp_parser_parse_definitely (parser))
9305         {
9306           if (declares_class_or_enum)
9307             *declares_class_or_enum = 2;
9308           if (decl_specs)
9309             cp_parser_set_decl_spec_type (decl_specs,
9310                                           type_spec,
9311                                           /*user_defined_p=*/true);
9312           return type_spec;
9313         }
9314
9315       /* Fall through.  */
9316     elaborated_type_specifier:
9317       /* We're declaring (not defining) a class or enum.  */
9318       if (declares_class_or_enum)
9319         *declares_class_or_enum = 1;
9320
9321       /* Fall through.  */
9322     case RID_TYPENAME:
9323       /* Look for an elaborated-type-specifier.  */
9324       type_spec
9325         = (cp_parser_elaborated_type_specifier
9326            (parser,
9327             decl_specs && decl_specs->specs[(int) ds_friend],
9328             is_declaration));
9329       if (decl_specs)
9330         cp_parser_set_decl_spec_type (decl_specs,
9331                                       type_spec,
9332                                       /*user_defined_p=*/true);
9333       return type_spec;
9334
9335     case RID_CONST:
9336       ds = ds_const;
9337       if (is_cv_qualifier)
9338         *is_cv_qualifier = true;
9339       break;
9340
9341     case RID_VOLATILE:
9342       ds = ds_volatile;
9343       if (is_cv_qualifier)
9344         *is_cv_qualifier = true;
9345       break;
9346
9347     case RID_RESTRICT:
9348       ds = ds_restrict;
9349       if (is_cv_qualifier)
9350         *is_cv_qualifier = true;
9351       break;
9352
9353     case RID_COMPLEX:
9354       /* The `__complex__' keyword is a GNU extension.  */
9355       ds = ds_complex;
9356       break;
9357
9358     default:
9359       break;
9360     }
9361
9362   /* Handle simple keywords.  */
9363   if (ds != ds_last)
9364     {
9365       if (decl_specs)
9366         {
9367           ++decl_specs->specs[(int)ds];
9368           decl_specs->any_specifiers_p = true;
9369         }
9370       return cp_lexer_consume_token (parser->lexer)->value;
9371     }
9372
9373   /* If we do not already have a type-specifier, assume we are looking
9374      at a simple-type-specifier.  */
9375   type_spec = cp_parser_simple_type_specifier (parser,
9376                                                decl_specs,
9377                                                flags);
9378
9379   /* If we didn't find a type-specifier, and a type-specifier was not
9380      optional in this context, issue an error message.  */
9381   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9382     {
9383       cp_parser_error (parser, "expected type specifier");
9384       return error_mark_node;
9385     }
9386
9387   return type_spec;
9388 }
9389
9390 /* Parse a simple-type-specifier.
9391
9392    simple-type-specifier:
9393      :: [opt] nested-name-specifier [opt] type-name
9394      :: [opt] nested-name-specifier template template-id
9395      char
9396      wchar_t
9397      bool
9398      short
9399      int
9400      long
9401      signed
9402      unsigned
9403      float
9404      double
9405      void
9406
9407    GNU Extension:
9408
9409    simple-type-specifier:
9410      __typeof__ unary-expression
9411      __typeof__ ( type-id )
9412
9413    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9414    appropriately updated.  */
9415
9416 static tree
9417 cp_parser_simple_type_specifier (cp_parser* parser,
9418                                  cp_decl_specifier_seq *decl_specs,
9419                                  cp_parser_flags flags)
9420 {
9421   tree type = NULL_TREE;
9422   cp_token *token;
9423
9424   /* Peek at the next token.  */
9425   token = cp_lexer_peek_token (parser->lexer);
9426
9427   /* If we're looking at a keyword, things are easy.  */
9428   switch (token->keyword)
9429     {
9430     case RID_CHAR:
9431       if (decl_specs)
9432         decl_specs->explicit_char_p = true;
9433       type = char_type_node;
9434       break;
9435     case RID_WCHAR:
9436       type = wchar_type_node;
9437       break;
9438     case RID_BOOL:
9439       type = boolean_type_node;
9440       break;
9441     case RID_SHORT:
9442       if (decl_specs)
9443         ++decl_specs->specs[(int) ds_short];
9444       type = short_integer_type_node;
9445       break;
9446     case RID_INT:
9447       if (decl_specs)
9448         decl_specs->explicit_int_p = true;
9449       type = integer_type_node;
9450       break;
9451     case RID_LONG:
9452       if (decl_specs)
9453         ++decl_specs->specs[(int) ds_long];
9454       type = long_integer_type_node;
9455       break;
9456     case RID_SIGNED:
9457       if (decl_specs)
9458         ++decl_specs->specs[(int) ds_signed];
9459       type = integer_type_node;
9460       break;
9461     case RID_UNSIGNED:
9462       if (decl_specs)
9463         ++decl_specs->specs[(int) ds_unsigned];
9464       type = unsigned_type_node;
9465       break;
9466     case RID_FLOAT:
9467       type = float_type_node;
9468       break;
9469     case RID_DOUBLE:
9470       type = double_type_node;
9471       break;
9472     case RID_VOID:
9473       type = void_type_node;
9474       break;
9475
9476     case RID_TYPEOF:
9477       /* Consume the `typeof' token.  */
9478       cp_lexer_consume_token (parser->lexer);
9479       /* Parse the operand to `typeof'.  */
9480       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9481       /* If it is not already a TYPE, take its type.  */
9482       if (!TYPE_P (type))
9483         type = finish_typeof (type);
9484
9485       if (decl_specs)
9486         cp_parser_set_decl_spec_type (decl_specs, type,
9487                                       /*user_defined_p=*/true);
9488
9489       return type;
9490
9491     default:
9492       break;
9493     }
9494
9495   /* If the type-specifier was for a built-in type, we're done.  */
9496   if (type)
9497     {
9498       tree id;
9499
9500       /* Record the type.  */
9501       if (decl_specs
9502           && (token->keyword != RID_SIGNED
9503               && token->keyword != RID_UNSIGNED
9504               && token->keyword != RID_SHORT
9505               && token->keyword != RID_LONG))
9506         cp_parser_set_decl_spec_type (decl_specs,
9507                                       type,
9508                                       /*user_defined=*/false);
9509       if (decl_specs)
9510         decl_specs->any_specifiers_p = true;
9511
9512       /* Consume the token.  */
9513       id = cp_lexer_consume_token (parser->lexer)->value;
9514
9515       /* There is no valid C++ program where a non-template type is
9516          followed by a "<".  That usually indicates that the user thought
9517          that the type was a template.  */
9518       cp_parser_check_for_invalid_template_id (parser, type);
9519
9520       return TYPE_NAME (type);
9521     }
9522
9523   /* The type-specifier must be a user-defined type.  */
9524   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9525     {
9526       bool qualified_p;
9527       bool global_p;
9528
9529       /* Don't gobble tokens or issue error messages if this is an
9530          optional type-specifier.  */
9531       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9532         cp_parser_parse_tentatively (parser);
9533
9534       /* Look for the optional `::' operator.  */
9535       global_p
9536         = (cp_parser_global_scope_opt (parser,
9537                                        /*current_scope_valid_p=*/false)
9538            != NULL_TREE);
9539       /* Look for the nested-name specifier.  */
9540       qualified_p
9541         = (cp_parser_nested_name_specifier_opt (parser,
9542                                                 /*typename_keyword_p=*/false,
9543                                                 /*check_dependency_p=*/true,
9544                                                 /*type_p=*/false,
9545                                                 /*is_declaration=*/false)
9546            != NULL_TREE);
9547       /* If we have seen a nested-name-specifier, and the next token
9548          is `template', then we are using the template-id production.  */
9549       if (parser->scope
9550           && cp_parser_optional_template_keyword (parser))
9551         {
9552           /* Look for the template-id.  */
9553           type = cp_parser_template_id (parser,
9554                                         /*template_keyword_p=*/true,
9555                                         /*check_dependency_p=*/true,
9556                                         /*is_declaration=*/false);
9557           /* If the template-id did not name a type, we are out of
9558              luck.  */
9559           if (TREE_CODE (type) != TYPE_DECL)
9560             {
9561               cp_parser_error (parser, "expected template-id for type");
9562               type = NULL_TREE;
9563             }
9564         }
9565       /* Otherwise, look for a type-name.  */
9566       else
9567         type = cp_parser_type_name (parser);
9568       /* Keep track of all name-lookups performed in class scopes.  */
9569       if (type
9570           && !global_p
9571           && !qualified_p
9572           && TREE_CODE (type) == TYPE_DECL
9573           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9574         maybe_note_name_used_in_class (DECL_NAME (type), type);
9575       /* If it didn't work out, we don't have a TYPE.  */
9576       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9577           && !cp_parser_parse_definitely (parser))
9578         type = NULL_TREE;
9579       if (type && decl_specs)
9580         cp_parser_set_decl_spec_type (decl_specs, type,
9581                                       /*user_defined=*/true);
9582     }
9583
9584   /* If we didn't get a type-name, issue an error message.  */
9585   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9586     {
9587       cp_parser_error (parser, "expected type-name");
9588       return error_mark_node;
9589     }
9590
9591   /* There is no valid C++ program where a non-template type is
9592      followed by a "<".  That usually indicates that the user thought
9593      that the type was a template.  */
9594   if (type && type != error_mark_node)
9595     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9596
9597   return type;
9598 }
9599
9600 /* Parse a type-name.
9601
9602    type-name:
9603      class-name
9604      enum-name
9605      typedef-name
9606
9607    enum-name:
9608      identifier
9609
9610    typedef-name:
9611      identifier
9612
9613    Returns a TYPE_DECL for the type.  */
9614
9615 static tree
9616 cp_parser_type_name (cp_parser* parser)
9617 {
9618   tree type_decl;
9619   tree identifier;
9620
9621   /* We can't know yet whether it is a class-name or not.  */
9622   cp_parser_parse_tentatively (parser);
9623   /* Try a class-name.  */
9624   type_decl = cp_parser_class_name (parser,
9625                                     /*typename_keyword_p=*/false,
9626                                     /*template_keyword_p=*/false,
9627                                     none_type,
9628                                     /*check_dependency_p=*/true,
9629                                     /*class_head_p=*/false,
9630                                     /*is_declaration=*/false);
9631   /* If it's not a class-name, keep looking.  */
9632   if (!cp_parser_parse_definitely (parser))
9633     {
9634       /* It must be a typedef-name or an enum-name.  */
9635       identifier = cp_parser_identifier (parser);
9636       if (identifier == error_mark_node)
9637         return error_mark_node;
9638
9639       /* Look up the type-name.  */
9640       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9641       /* Issue an error if we did not find a type-name.  */
9642       if (TREE_CODE (type_decl) != TYPE_DECL)
9643         {
9644           if (!cp_parser_simulate_error (parser))
9645             cp_parser_name_lookup_error (parser, identifier, type_decl,
9646                                          "is not a type");
9647           type_decl = error_mark_node;
9648         }
9649       /* Remember that the name was used in the definition of the
9650          current class so that we can check later to see if the
9651          meaning would have been different after the class was
9652          entirely defined.  */
9653       else if (type_decl != error_mark_node
9654                && !parser->scope)
9655         maybe_note_name_used_in_class (identifier, type_decl);
9656     }
9657
9658   return type_decl;
9659 }
9660
9661
9662 /* Parse an elaborated-type-specifier.  Note that the grammar given
9663    here incorporates the resolution to DR68.
9664
9665    elaborated-type-specifier:
9666      class-key :: [opt] nested-name-specifier [opt] identifier
9667      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9668      enum :: [opt] nested-name-specifier [opt] identifier
9669      typename :: [opt] nested-name-specifier identifier
9670      typename :: [opt] nested-name-specifier template [opt]
9671        template-id
9672
9673    GNU extension:
9674
9675    elaborated-type-specifier:
9676      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9677      class-key attributes :: [opt] nested-name-specifier [opt]
9678                template [opt] template-id
9679      enum attributes :: [opt] nested-name-specifier [opt] identifier
9680
9681    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9682    declared `friend'.  If IS_DECLARATION is TRUE, then this
9683    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9684    something is being declared.
9685
9686    Returns the TYPE specified.  */
9687
9688 static tree
9689 cp_parser_elaborated_type_specifier (cp_parser* parser,
9690                                      bool is_friend,
9691                                      bool is_declaration)
9692 {
9693   enum tag_types tag_type;
9694   tree identifier;
9695   tree type = NULL_TREE;
9696   tree attributes = NULL_TREE;
9697
9698   /* See if we're looking at the `enum' keyword.  */
9699   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9700     {
9701       /* Consume the `enum' token.  */
9702       cp_lexer_consume_token (parser->lexer);
9703       /* Remember that it's an enumeration type.  */
9704       tag_type = enum_type;
9705       /* Parse the attributes.  */
9706       attributes = cp_parser_attributes_opt (parser);
9707     }
9708   /* Or, it might be `typename'.  */
9709   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9710                                            RID_TYPENAME))
9711     {
9712       /* Consume the `typename' token.  */
9713       cp_lexer_consume_token (parser->lexer);
9714       /* Remember that it's a `typename' type.  */
9715       tag_type = typename_type;
9716       /* The `typename' keyword is only allowed in templates.  */
9717       if (!processing_template_decl)
9718         pedwarn ("using %<typename%> outside of template");
9719     }
9720   /* Otherwise it must be a class-key.  */
9721   else
9722     {
9723       tag_type = cp_parser_class_key (parser);
9724       if (tag_type == none_type)
9725         return error_mark_node;
9726       /* Parse the attributes.  */
9727       attributes = cp_parser_attributes_opt (parser);
9728     }
9729
9730   /* Look for the `::' operator.  */
9731   cp_parser_global_scope_opt (parser,
9732                               /*current_scope_valid_p=*/false);
9733   /* Look for the nested-name-specifier.  */
9734   if (tag_type == typename_type)
9735     {
9736       if (cp_parser_nested_name_specifier (parser,
9737                                            /*typename_keyword_p=*/true,
9738                                            /*check_dependency_p=*/true,
9739                                            /*type_p=*/true,
9740                                            is_declaration)
9741           == error_mark_node)
9742         return error_mark_node;
9743     }
9744   else
9745     /* Even though `typename' is not present, the proposed resolution
9746        to Core Issue 180 says that in `class A<T>::B', `B' should be
9747        considered a type-name, even if `A<T>' is dependent.  */
9748     cp_parser_nested_name_specifier_opt (parser,
9749                                          /*typename_keyword_p=*/true,
9750                                          /*check_dependency_p=*/true,
9751                                          /*type_p=*/true,
9752                                          is_declaration);
9753   /* For everything but enumeration types, consider a template-id.  */
9754   if (tag_type != enum_type)
9755     {
9756       bool template_p = false;
9757       tree decl;
9758
9759       /* Allow the `template' keyword.  */
9760       template_p = cp_parser_optional_template_keyword (parser);
9761       /* If we didn't see `template', we don't know if there's a
9762          template-id or not.  */
9763       if (!template_p)
9764         cp_parser_parse_tentatively (parser);
9765       /* Parse the template-id.  */
9766       decl = cp_parser_template_id (parser, template_p,
9767                                     /*check_dependency_p=*/true,
9768                                     is_declaration);
9769       /* If we didn't find a template-id, look for an ordinary
9770          identifier.  */
9771       if (!template_p && !cp_parser_parse_definitely (parser))
9772         ;
9773       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9774          in effect, then we must assume that, upon instantiation, the
9775          template will correspond to a class.  */
9776       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9777                && tag_type == typename_type)
9778         type = make_typename_type (parser->scope, decl,
9779                                    typename_type,
9780                                    /*complain=*/1);
9781       else
9782         type = TREE_TYPE (decl);
9783     }
9784
9785   /* For an enumeration type, consider only a plain identifier.  */
9786   if (!type)
9787     {
9788       identifier = cp_parser_identifier (parser);
9789
9790       if (identifier == error_mark_node)
9791         {
9792           parser->scope = NULL_TREE;
9793           return error_mark_node;
9794         }
9795
9796       /* For a `typename', we needn't call xref_tag.  */
9797       if (tag_type == typename_type 
9798           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9799         return cp_parser_make_typename_type (parser, parser->scope,
9800                                              identifier);
9801       /* Look up a qualified name in the usual way.  */
9802       if (parser->scope)
9803         {
9804           tree decl;
9805
9806           decl = cp_parser_lookup_name (parser, identifier,
9807                                         tag_type,
9808                                         /*is_template=*/false,
9809                                         /*is_namespace=*/false,
9810                                         /*check_dependency=*/true,
9811                                         /*ambiguous_p=*/NULL);
9812
9813           /* If we are parsing friend declaration, DECL may be a
9814              TEMPLATE_DECL tree node here.  However, we need to check
9815              whether this TEMPLATE_DECL results in valid code.  Consider
9816              the following example:
9817
9818                namespace N {
9819                  template <class T> class C {};
9820                }
9821                class X {
9822                  template <class T> friend class N::C; // #1, valid code
9823                };
9824                template <class T> class Y {
9825                  friend class N::C;                    // #2, invalid code
9826                };
9827
9828              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9829              name lookup of `N::C'.  We see that friend declaration must
9830              be template for the code to be valid.  Note that
9831              processing_template_decl does not work here since it is
9832              always 1 for the above two cases.  */
9833
9834           decl = (cp_parser_maybe_treat_template_as_class
9835                   (decl, /*tag_name_p=*/is_friend
9836                          && parser->num_template_parameter_lists));
9837
9838           if (TREE_CODE (decl) != TYPE_DECL)
9839             {
9840               cp_parser_diagnose_invalid_type_name (parser, 
9841                                                     parser->scope,
9842                                                     identifier);
9843               return error_mark_node;
9844             }
9845
9846           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9847             check_elaborated_type_specifier
9848               (tag_type, decl,
9849                (parser->num_template_parameter_lists
9850                 || DECL_SELF_REFERENCE_P (decl)));
9851
9852           type = TREE_TYPE (decl);
9853         }
9854       else
9855         {
9856           /* An elaborated-type-specifier sometimes introduces a new type and
9857              sometimes names an existing type.  Normally, the rule is that it
9858              introduces a new type only if there is not an existing type of
9859              the same name already in scope.  For example, given:
9860
9861                struct S {};
9862                void f() { struct S s; }
9863
9864              the `struct S' in the body of `f' is the same `struct S' as in
9865              the global scope; the existing definition is used.  However, if
9866              there were no global declaration, this would introduce a new
9867              local class named `S'.
9868
9869              An exception to this rule applies to the following code:
9870
9871                namespace N { struct S; }
9872
9873              Here, the elaborated-type-specifier names a new type
9874              unconditionally; even if there is already an `S' in the
9875              containing scope this declaration names a new type.
9876              This exception only applies if the elaborated-type-specifier
9877              forms the complete declaration:
9878
9879                [class.name]
9880
9881                A declaration consisting solely of `class-key identifier ;' is
9882                either a redeclaration of the name in the current scope or a
9883                forward declaration of the identifier as a class name.  It
9884                introduces the name into the current scope.
9885
9886              We are in this situation precisely when the next token is a `;'.
9887
9888              An exception to the exception is that a `friend' declaration does
9889              *not* name a new type; i.e., given:
9890
9891                struct S { friend struct T; };
9892
9893              `T' is not a new type in the scope of `S'.
9894
9895              Also, `new struct S' or `sizeof (struct S)' never results in the
9896              definition of a new type; a new type can only be declared in a
9897              declaration context.  */
9898
9899           tag_scope ts;
9900           if (is_friend)
9901             /* Friends have special name lookup rules.  */
9902             ts = ts_within_enclosing_non_class;
9903           else if (is_declaration
9904                    && cp_lexer_next_token_is (parser->lexer,
9905                                               CPP_SEMICOLON))
9906             /* This is a `class-key identifier ;' */
9907             ts = ts_current;
9908           else
9909             ts = ts_global;
9910
9911           /* Warn about attributes. They are ignored.  */
9912           if (attributes)
9913             warning ("type attributes are honored only at type definition");
9914
9915           type = xref_tag (tag_type, identifier, ts,
9916                            parser->num_template_parameter_lists);
9917         }
9918     }
9919   if (tag_type != enum_type)
9920     cp_parser_check_class_key (tag_type, type);
9921
9922   /* A "<" cannot follow an elaborated type specifier.  If that
9923      happens, the user was probably trying to form a template-id.  */
9924   cp_parser_check_for_invalid_template_id (parser, type);
9925
9926   return type;
9927 }
9928
9929 /* Parse an enum-specifier.
9930
9931    enum-specifier:
9932      enum identifier [opt] { enumerator-list [opt] }
9933
9934    GNU Extensions:
9935      enum identifier [opt] { enumerator-list [opt] } attributes
9936
9937    Returns an ENUM_TYPE representing the enumeration.  */
9938
9939 static tree
9940 cp_parser_enum_specifier (cp_parser* parser)
9941 {
9942   tree identifier;
9943   tree type;
9944
9945   /* Caller guarantees that the current token is 'enum', an identifier
9946      possibly follows, and the token after that is an opening brace.
9947      If we don't have an identifier, fabricate an anonymous name for
9948      the enumeration being defined.  */
9949   cp_lexer_consume_token (parser->lexer);
9950
9951   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9952     identifier = cp_parser_identifier (parser);
9953   else
9954     identifier = make_anon_name ();
9955
9956   /* Issue an error message if type-definitions are forbidden here.  */
9957   cp_parser_check_type_definition (parser);
9958
9959   /* Create the new type.  We do this before consuming the opening brace
9960      so the enum will be recorded as being on the line of its tag (or the
9961      'enum' keyword, if there is no tag).  */
9962   type = start_enum (identifier);
9963
9964   /* Consume the opening brace.  */
9965   cp_lexer_consume_token (parser->lexer);
9966
9967   /* If the next token is not '}', then there are some enumerators.  */
9968   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9969     cp_parser_enumerator_list (parser, type);
9970
9971   /* Consume the final '}'.  */
9972   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9973
9974   /* Look for trailing attributes to apply to this enumeration, and
9975      apply them if appropriate.  */
9976   if (cp_parser_allow_gnu_extensions_p (parser))
9977     {
9978       tree trailing_attr = cp_parser_attributes_opt (parser);
9979       cplus_decl_attributes (&type,
9980                              trailing_attr,
9981                              (int) ATTR_FLAG_TYPE_IN_PLACE);
9982     }
9983
9984   /* Finish up the enumeration.  */
9985   finish_enum (type);
9986
9987   return type;
9988 }
9989
9990 /* Parse an enumerator-list.  The enumerators all have the indicated
9991    TYPE.
9992
9993    enumerator-list:
9994      enumerator-definition
9995      enumerator-list , enumerator-definition  */
9996
9997 static void
9998 cp_parser_enumerator_list (cp_parser* parser, tree type)
9999 {
10000   while (true)
10001     {
10002       /* Parse an enumerator-definition.  */
10003       cp_parser_enumerator_definition (parser, type);
10004
10005       /* If the next token is not a ',', we've reached the end of
10006          the list.  */
10007       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10008         break;
10009       /* Otherwise, consume the `,' and keep going.  */
10010       cp_lexer_consume_token (parser->lexer);
10011       /* If the next token is a `}', there is a trailing comma.  */
10012       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10013         {
10014           if (pedantic && !in_system_header)
10015             pedwarn ("comma at end of enumerator list");
10016           break;
10017         }
10018     }
10019 }
10020
10021 /* Parse an enumerator-definition.  The enumerator has the indicated
10022    TYPE.
10023
10024    enumerator-definition:
10025      enumerator
10026      enumerator = constant-expression
10027
10028    enumerator:
10029      identifier  */
10030
10031 static void
10032 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10033 {
10034   tree identifier;
10035   tree value;
10036
10037   /* Look for the identifier.  */
10038   identifier = cp_parser_identifier (parser);
10039   if (identifier == error_mark_node)
10040     return;
10041
10042   /* If the next token is an '=', then there is an explicit value.  */
10043   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10044     {
10045       /* Consume the `=' token.  */
10046       cp_lexer_consume_token (parser->lexer);
10047       /* Parse the value.  */
10048       value = cp_parser_constant_expression (parser,
10049                                              /*allow_non_constant_p=*/false,
10050                                              NULL);
10051     }
10052   else
10053     value = NULL_TREE;
10054
10055   /* Create the enumerator.  */
10056   build_enumerator (identifier, value, type);
10057 }
10058
10059 /* Parse a namespace-name.
10060
10061    namespace-name:
10062      original-namespace-name
10063      namespace-alias
10064
10065    Returns the NAMESPACE_DECL for the namespace.  */
10066
10067 static tree
10068 cp_parser_namespace_name (cp_parser* parser)
10069 {
10070   tree identifier;
10071   tree namespace_decl;
10072
10073   /* Get the name of the namespace.  */
10074   identifier = cp_parser_identifier (parser);
10075   if (identifier == error_mark_node)
10076     return error_mark_node;
10077
10078   /* Look up the identifier in the currently active scope.  Look only
10079      for namespaces, due to:
10080
10081        [basic.lookup.udir]
10082
10083        When looking up a namespace-name in a using-directive or alias
10084        definition, only namespace names are considered.
10085
10086      And:
10087
10088        [basic.lookup.qual]
10089
10090        During the lookup of a name preceding the :: scope resolution
10091        operator, object, function, and enumerator names are ignored.
10092
10093      (Note that cp_parser_class_or_namespace_name only calls this
10094      function if the token after the name is the scope resolution
10095      operator.)  */
10096   namespace_decl = cp_parser_lookup_name (parser, identifier,
10097                                           none_type,
10098                                           /*is_template=*/false,
10099                                           /*is_namespace=*/true,
10100                                           /*check_dependency=*/true,
10101                                           /*ambiguous_p=*/NULL);
10102   /* If it's not a namespace, issue an error.  */
10103   if (namespace_decl == error_mark_node
10104       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10105     {
10106       cp_parser_error (parser, "expected namespace-name");
10107       namespace_decl = error_mark_node;
10108     }
10109
10110   return namespace_decl;
10111 }
10112
10113 /* Parse a namespace-definition.
10114
10115    namespace-definition:
10116      named-namespace-definition
10117      unnamed-namespace-definition
10118
10119    named-namespace-definition:
10120      original-namespace-definition
10121      extension-namespace-definition
10122
10123    original-namespace-definition:
10124      namespace identifier { namespace-body }
10125
10126    extension-namespace-definition:
10127      namespace original-namespace-name { namespace-body }
10128
10129    unnamed-namespace-definition:
10130      namespace { namespace-body } */
10131
10132 static void
10133 cp_parser_namespace_definition (cp_parser* parser)
10134 {
10135   tree identifier;
10136
10137   /* Look for the `namespace' keyword.  */
10138   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10139
10140   /* Get the name of the namespace.  We do not attempt to distinguish
10141      between an original-namespace-definition and an
10142      extension-namespace-definition at this point.  The semantic
10143      analysis routines are responsible for that.  */
10144   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10145     identifier = cp_parser_identifier (parser);
10146   else
10147     identifier = NULL_TREE;
10148
10149   /* Look for the `{' to start the namespace.  */
10150   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10151   /* Start the namespace.  */
10152   push_namespace (identifier);
10153   /* Parse the body of the namespace.  */
10154   cp_parser_namespace_body (parser);
10155   /* Finish the namespace.  */
10156   pop_namespace ();
10157   /* Look for the final `}'.  */
10158   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10159 }
10160
10161 /* Parse a namespace-body.
10162
10163    namespace-body:
10164      declaration-seq [opt]  */
10165
10166 static void
10167 cp_parser_namespace_body (cp_parser* parser)
10168 {
10169   cp_parser_declaration_seq_opt (parser);
10170 }
10171
10172 /* Parse a namespace-alias-definition.
10173
10174    namespace-alias-definition:
10175      namespace identifier = qualified-namespace-specifier ;  */
10176
10177 static void
10178 cp_parser_namespace_alias_definition (cp_parser* parser)
10179 {
10180   tree identifier;
10181   tree namespace_specifier;
10182
10183   /* Look for the `namespace' keyword.  */
10184   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10185   /* Look for the identifier.  */
10186   identifier = cp_parser_identifier (parser);
10187   if (identifier == error_mark_node)
10188     return;
10189   /* Look for the `=' token.  */
10190   cp_parser_require (parser, CPP_EQ, "`='");
10191   /* Look for the qualified-namespace-specifier.  */
10192   namespace_specifier
10193     = cp_parser_qualified_namespace_specifier (parser);
10194   /* Look for the `;' token.  */
10195   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10196
10197   /* Register the alias in the symbol table.  */
10198   do_namespace_alias (identifier, namespace_specifier);
10199 }
10200
10201 /* Parse a qualified-namespace-specifier.
10202
10203    qualified-namespace-specifier:
10204      :: [opt] nested-name-specifier [opt] namespace-name
10205
10206    Returns a NAMESPACE_DECL corresponding to the specified
10207    namespace.  */
10208
10209 static tree
10210 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10211 {
10212   /* Look for the optional `::'.  */
10213   cp_parser_global_scope_opt (parser,
10214                               /*current_scope_valid_p=*/false);
10215
10216   /* Look for the optional nested-name-specifier.  */
10217   cp_parser_nested_name_specifier_opt (parser,
10218                                        /*typename_keyword_p=*/false,
10219                                        /*check_dependency_p=*/true,
10220                                        /*type_p=*/false,
10221                                        /*is_declaration=*/true);
10222
10223   return cp_parser_namespace_name (parser);
10224 }
10225
10226 /* Parse a using-declaration.
10227
10228    using-declaration:
10229      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10230      using :: unqualified-id ;  */
10231
10232 static void
10233 cp_parser_using_declaration (cp_parser* parser)
10234 {
10235   cp_token *token;
10236   bool typename_p = false;
10237   bool global_scope_p;
10238   tree decl;
10239   tree identifier;
10240   tree qscope;
10241
10242   /* Look for the `using' keyword.  */
10243   cp_parser_require_keyword (parser, RID_USING, "`using'");
10244
10245   /* Peek at the next token.  */
10246   token = cp_lexer_peek_token (parser->lexer);
10247   /* See if it's `typename'.  */
10248   if (token->keyword == RID_TYPENAME)
10249     {
10250       /* Remember that we've seen it.  */
10251       typename_p = true;
10252       /* Consume the `typename' token.  */
10253       cp_lexer_consume_token (parser->lexer);
10254     }
10255
10256   /* Look for the optional global scope qualification.  */
10257   global_scope_p
10258     = (cp_parser_global_scope_opt (parser,
10259                                    /*current_scope_valid_p=*/false)
10260        != NULL_TREE);
10261
10262   /* If we saw `typename', or didn't see `::', then there must be a
10263      nested-name-specifier present.  */
10264   if (typename_p || !global_scope_p)
10265     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10266                                               /*check_dependency_p=*/true,
10267                                               /*type_p=*/false,
10268                                               /*is_declaration=*/true);
10269   /* Otherwise, we could be in either of the two productions.  In that
10270      case, treat the nested-name-specifier as optional.  */
10271   else
10272     qscope = cp_parser_nested_name_specifier_opt (parser,
10273                                                   /*typename_keyword_p=*/false,
10274                                                   /*check_dependency_p=*/true,
10275                                                   /*type_p=*/false,
10276                                                   /*is_declaration=*/true);
10277   if (!qscope)
10278     qscope = global_namespace;
10279
10280   /* Parse the unqualified-id.  */
10281   identifier = cp_parser_unqualified_id (parser,
10282                                          /*template_keyword_p=*/false,
10283                                          /*check_dependency_p=*/true,
10284                                          /*declarator_p=*/true);
10285
10286   /* The function we call to handle a using-declaration is different
10287      depending on what scope we are in.  */
10288   if (identifier == error_mark_node)
10289     ;
10290   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10291            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10292     /* [namespace.udecl]
10293
10294        A using declaration shall not name a template-id.  */
10295     error ("a template-id may not appear in a using-declaration");
10296   else
10297     {
10298       if (at_class_scope_p ())
10299         {
10300           /* Create the USING_DECL.  */
10301           decl = do_class_using_decl (parser->scope, identifier);
10302           /* Add it to the list of members in this class.  */
10303           finish_member_declaration (decl);
10304         }
10305       else
10306         {
10307           decl = cp_parser_lookup_name_simple (parser, identifier);
10308           if (decl == error_mark_node)
10309             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10310           else if (!at_namespace_scope_p ())
10311             do_local_using_decl (decl, qscope, identifier);
10312           else
10313             do_toplevel_using_decl (decl, qscope, identifier);
10314         }
10315     }
10316
10317   /* Look for the final `;'.  */
10318   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10319 }
10320
10321 /* Parse a using-directive.
10322
10323    using-directive:
10324      using namespace :: [opt] nested-name-specifier [opt]
10325        namespace-name ;  */
10326
10327 static void
10328 cp_parser_using_directive (cp_parser* parser)
10329 {
10330   tree namespace_decl;
10331   tree attribs;
10332
10333   /* Look for the `using' keyword.  */
10334   cp_parser_require_keyword (parser, RID_USING, "`using'");
10335   /* And the `namespace' keyword.  */
10336   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10337   /* Look for the optional `::' operator.  */
10338   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10339   /* And the optional nested-name-specifier.  */
10340   cp_parser_nested_name_specifier_opt (parser,
10341                                        /*typename_keyword_p=*/false,
10342                                        /*check_dependency_p=*/true,
10343                                        /*type_p=*/false,
10344                                        /*is_declaration=*/true);
10345   /* Get the namespace being used.  */
10346   namespace_decl = cp_parser_namespace_name (parser);
10347   /* And any specified attributes.  */
10348   attribs = cp_parser_attributes_opt (parser);
10349   /* Update the symbol table.  */
10350   parse_using_directive (namespace_decl, attribs);
10351   /* Look for the final `;'.  */
10352   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10353 }
10354
10355 /* Parse an asm-definition.
10356
10357    asm-definition:
10358      asm ( string-literal ) ;
10359
10360    GNU Extension:
10361
10362    asm-definition:
10363      asm volatile [opt] ( string-literal ) ;
10364      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10365      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10366                           : asm-operand-list [opt] ) ;
10367      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10368                           : asm-operand-list [opt]
10369                           : asm-operand-list [opt] ) ;  */
10370
10371 static void
10372 cp_parser_asm_definition (cp_parser* parser)
10373 {
10374   tree string;
10375   tree outputs = NULL_TREE;
10376   tree inputs = NULL_TREE;
10377   tree clobbers = NULL_TREE;
10378   tree asm_stmt;
10379   bool volatile_p = false;
10380   bool extended_p = false;
10381
10382   /* Look for the `asm' keyword.  */
10383   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10384   /* See if the next token is `volatile'.  */
10385   if (cp_parser_allow_gnu_extensions_p (parser)
10386       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10387     {
10388       /* Remember that we saw the `volatile' keyword.  */
10389       volatile_p = true;
10390       /* Consume the token.  */
10391       cp_lexer_consume_token (parser->lexer);
10392     }
10393   /* Look for the opening `('.  */
10394   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10395     return;
10396   /* Look for the string.  */
10397   string = cp_parser_string_literal (parser, false, false);
10398   if (string == error_mark_node)
10399     {
10400       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10401                                              /*consume_paren=*/true);
10402       return;
10403     }
10404
10405   /* If we're allowing GNU extensions, check for the extended assembly
10406      syntax.  Unfortunately, the `:' tokens need not be separated by
10407      a space in C, and so, for compatibility, we tolerate that here
10408      too.  Doing that means that we have to treat the `::' operator as
10409      two `:' tokens.  */
10410   if (cp_parser_allow_gnu_extensions_p (parser)
10411       && at_function_scope_p ()
10412       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10413           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10414     {
10415       bool inputs_p = false;
10416       bool clobbers_p = false;
10417
10418       /* The extended syntax was used.  */
10419       extended_p = true;
10420
10421       /* Look for outputs.  */
10422       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10423         {
10424           /* Consume the `:'.  */
10425           cp_lexer_consume_token (parser->lexer);
10426           /* Parse the output-operands.  */
10427           if (cp_lexer_next_token_is_not (parser->lexer,
10428                                           CPP_COLON)
10429               && cp_lexer_next_token_is_not (parser->lexer,
10430                                              CPP_SCOPE)
10431               && cp_lexer_next_token_is_not (parser->lexer,
10432                                              CPP_CLOSE_PAREN))
10433             outputs = cp_parser_asm_operand_list (parser);
10434         }
10435       /* If the next token is `::', there are no outputs, and the
10436          next token is the beginning of the inputs.  */
10437       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10438         /* The inputs are coming next.  */
10439         inputs_p = true;
10440
10441       /* Look for inputs.  */
10442       if (inputs_p
10443           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10444         {
10445           /* Consume the `:' or `::'.  */
10446           cp_lexer_consume_token (parser->lexer);
10447           /* Parse the output-operands.  */
10448           if (cp_lexer_next_token_is_not (parser->lexer,
10449                                           CPP_COLON)
10450               && cp_lexer_next_token_is_not (parser->lexer,
10451                                              CPP_CLOSE_PAREN))
10452             inputs = cp_parser_asm_operand_list (parser);
10453         }
10454       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10455         /* The clobbers are coming next.  */
10456         clobbers_p = true;
10457
10458       /* Look for clobbers.  */
10459       if (clobbers_p
10460           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10461         {
10462           /* Consume the `:' or `::'.  */
10463           cp_lexer_consume_token (parser->lexer);
10464           /* Parse the clobbers.  */
10465           if (cp_lexer_next_token_is_not (parser->lexer,
10466                                           CPP_CLOSE_PAREN))
10467             clobbers = cp_parser_asm_clobber_list (parser);
10468         }
10469     }
10470   /* Look for the closing `)'.  */
10471   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10472     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10473                                            /*consume_paren=*/true);
10474   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10475
10476   /* Create the ASM_EXPR.  */
10477   if (at_function_scope_p ())
10478     {
10479       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10480                                   inputs, clobbers);
10481       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10482       if (!extended_p)
10483         {
10484           tree temp = asm_stmt;
10485           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10486             temp = TREE_OPERAND (temp, 0);
10487           
10488           ASM_INPUT_P (temp) = 1;
10489         }
10490     }
10491   else
10492     assemble_asm (string);
10493 }
10494
10495 /* Declarators [gram.dcl.decl] */
10496
10497 /* Parse an init-declarator.
10498
10499    init-declarator:
10500      declarator initializer [opt]
10501
10502    GNU Extension:
10503
10504    init-declarator:
10505      declarator asm-specification [opt] attributes [opt] initializer [opt]
10506
10507    function-definition:
10508      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10509        function-body
10510      decl-specifier-seq [opt] declarator function-try-block
10511
10512    GNU Extension:
10513
10514    function-definition:
10515      __extension__ function-definition
10516
10517    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10518    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10519    then this declarator appears in a class scope.  The new DECL created
10520    by this declarator is returned.
10521
10522    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10523    for a function-definition here as well.  If the declarator is a
10524    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10525    be TRUE upon return.  By that point, the function-definition will
10526    have been completely parsed.
10527
10528    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10529    is FALSE.  */
10530
10531 static tree
10532 cp_parser_init_declarator (cp_parser* parser,
10533                            cp_decl_specifier_seq *decl_specifiers,
10534                            bool function_definition_allowed_p,
10535                            bool member_p,
10536                            int declares_class_or_enum,
10537                            bool* function_definition_p)
10538 {
10539   cp_token *token;
10540   cp_declarator *declarator;
10541   tree prefix_attributes;
10542   tree attributes;
10543   tree asm_specification;
10544   tree initializer;
10545   tree decl = NULL_TREE;
10546   tree scope;
10547   bool is_initialized;
10548   bool is_parenthesized_init;
10549   bool is_non_constant_init;
10550   int ctor_dtor_or_conv_p;
10551   bool friend_p;
10552   tree pushed_scope = NULL;
10553
10554   /* Gather the attributes that were provided with the
10555      decl-specifiers.  */
10556   prefix_attributes = decl_specifiers->attributes;
10557
10558   /* Assume that this is not the declarator for a function
10559      definition.  */
10560   if (function_definition_p)
10561     *function_definition_p = false;
10562
10563   /* Defer access checks while parsing the declarator; we cannot know
10564      what names are accessible until we know what is being
10565      declared.  */
10566   resume_deferring_access_checks ();
10567
10568   /* Parse the declarator.  */
10569   declarator
10570     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10571                             &ctor_dtor_or_conv_p,
10572                             /*parenthesized_p=*/NULL,
10573                             /*member_p=*/false);
10574   /* Gather up the deferred checks.  */
10575   stop_deferring_access_checks ();
10576
10577   /* If the DECLARATOR was erroneous, there's no need to go
10578      further.  */
10579   if (declarator == cp_error_declarator)
10580     return error_mark_node;
10581
10582   if (declares_class_or_enum & 2)
10583     cp_parser_check_for_definition_in_return_type (declarator,
10584                                                    decl_specifiers->type);
10585
10586   /* Figure out what scope the entity declared by the DECLARATOR is
10587      located in.  `grokdeclarator' sometimes changes the scope, so
10588      we compute it now.  */
10589   scope = get_scope_of_declarator (declarator);
10590
10591   /* If we're allowing GNU extensions, look for an asm-specification
10592      and attributes.  */
10593   if (cp_parser_allow_gnu_extensions_p (parser))
10594     {
10595       /* Look for an asm-specification.  */
10596       asm_specification = cp_parser_asm_specification_opt (parser);
10597       /* And attributes.  */
10598       attributes = cp_parser_attributes_opt (parser);
10599     }
10600   else
10601     {
10602       asm_specification = NULL_TREE;
10603       attributes = NULL_TREE;
10604     }
10605
10606   /* Peek at the next token.  */
10607   token = cp_lexer_peek_token (parser->lexer);
10608   /* Check to see if the token indicates the start of a
10609      function-definition.  */
10610   if (cp_parser_token_starts_function_definition_p (token))
10611     {
10612       if (!function_definition_allowed_p)
10613         {
10614           /* If a function-definition should not appear here, issue an
10615              error message.  */
10616           cp_parser_error (parser,
10617                            "a function-definition is not allowed here");
10618           return error_mark_node;
10619         }
10620       else
10621         {
10622           /* Neither attributes nor an asm-specification are allowed
10623              on a function-definition.  */
10624           if (asm_specification)
10625             error ("an asm-specification is not allowed on a function-definition");
10626           if (attributes)
10627             error ("attributes are not allowed on a function-definition");
10628           /* This is a function-definition.  */
10629           *function_definition_p = true;
10630
10631           /* Parse the function definition.  */
10632           if (member_p)
10633             decl = cp_parser_save_member_function_body (parser,
10634                                                         decl_specifiers,
10635                                                         declarator,
10636                                                         prefix_attributes);
10637           else
10638             decl
10639               = (cp_parser_function_definition_from_specifiers_and_declarator
10640                  (parser, decl_specifiers, prefix_attributes, declarator));
10641
10642           return decl;
10643         }
10644     }
10645
10646   /* [dcl.dcl]
10647
10648      Only in function declarations for constructors, destructors, and
10649      type conversions can the decl-specifier-seq be omitted.
10650
10651      We explicitly postpone this check past the point where we handle
10652      function-definitions because we tolerate function-definitions
10653      that are missing their return types in some modes.  */
10654   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10655     {
10656       cp_parser_error (parser,
10657                        "expected constructor, destructor, or type conversion");
10658       return error_mark_node;
10659     }
10660
10661   /* An `=' or an `(' indicates an initializer.  */
10662   is_initialized = (token->type == CPP_EQ
10663                      || token->type == CPP_OPEN_PAREN);
10664   /* If the init-declarator isn't initialized and isn't followed by a
10665      `,' or `;', it's not a valid init-declarator.  */
10666   if (!is_initialized
10667       && token->type != CPP_COMMA
10668       && token->type != CPP_SEMICOLON)
10669     {
10670       cp_parser_error (parser, "expected initializer");
10671       return error_mark_node;
10672     }
10673
10674   /* Because start_decl has side-effects, we should only call it if we
10675      know we're going ahead.  By this point, we know that we cannot
10676      possibly be looking at any other construct.  */
10677   cp_parser_commit_to_tentative_parse (parser);
10678
10679   /* If the decl specifiers were bad, issue an error now that we're
10680      sure this was intended to be a declarator.  Then continue
10681      declaring the variable(s), as int, to try to cut down on further
10682      errors.  */
10683   if (decl_specifiers->any_specifiers_p
10684       && decl_specifiers->type == error_mark_node)
10685     {
10686       cp_parser_error (parser, "invalid type in declaration");
10687       decl_specifiers->type = integer_type_node;
10688     }
10689
10690   /* Check to see whether or not this declaration is a friend.  */
10691   friend_p = cp_parser_friend_p (decl_specifiers);
10692
10693   /* Check that the number of template-parameter-lists is OK.  */
10694   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10695     return error_mark_node;
10696
10697   /* Enter the newly declared entry in the symbol table.  If we're
10698      processing a declaration in a class-specifier, we wait until
10699      after processing the initializer.  */
10700   if (!member_p)
10701     {
10702       if (parser->in_unbraced_linkage_specification_p)
10703         {
10704           decl_specifiers->storage_class = sc_extern;
10705           have_extern_spec = false;
10706         }
10707       decl = start_decl (declarator, decl_specifiers,
10708                          is_initialized, attributes, prefix_attributes,
10709                          &pushed_scope);
10710     }
10711   else if (scope)
10712     /* Enter the SCOPE.  That way unqualified names appearing in the
10713        initializer will be looked up in SCOPE.  */
10714     pushed_scope = push_scope (scope);
10715
10716   /* Perform deferred access control checks, now that we know in which
10717      SCOPE the declared entity resides.  */
10718   if (!member_p && decl)
10719     {
10720       tree saved_current_function_decl = NULL_TREE;
10721
10722       /* If the entity being declared is a function, pretend that we
10723          are in its scope.  If it is a `friend', it may have access to
10724          things that would not otherwise be accessible.  */
10725       if (TREE_CODE (decl) == FUNCTION_DECL)
10726         {
10727           saved_current_function_decl = current_function_decl;
10728           current_function_decl = decl;
10729         }
10730
10731       /* Perform the access control checks for the declarator and the
10732          the decl-specifiers.  */
10733       perform_deferred_access_checks ();
10734
10735       /* Restore the saved value.  */
10736       if (TREE_CODE (decl) == FUNCTION_DECL)
10737         current_function_decl = saved_current_function_decl;
10738     }
10739
10740   /* Parse the initializer.  */
10741   if (is_initialized)
10742     initializer = cp_parser_initializer (parser,
10743                                          &is_parenthesized_init,
10744                                          &is_non_constant_init);
10745   else
10746     {
10747       initializer = NULL_TREE;
10748       is_parenthesized_init = false;
10749       is_non_constant_init = true;
10750     }
10751
10752   /* The old parser allows attributes to appear after a parenthesized
10753      initializer.  Mark Mitchell proposed removing this functionality
10754      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10755      attributes -- but ignores them.  */
10756   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10757     if (cp_parser_attributes_opt (parser))
10758       warning ("attributes after parenthesized initializer ignored");
10759
10760   /* For an in-class declaration, use `grokfield' to create the
10761      declaration.  */
10762   if (member_p)
10763     {
10764       if (pushed_scope)
10765         {
10766           pop_scope (pushed_scope);
10767           pushed_scope = false;
10768         }
10769       decl = grokfield (declarator, decl_specifiers,
10770                         initializer, /*asmspec=*/NULL_TREE,
10771                         /*attributes=*/NULL_TREE);
10772       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10773         cp_parser_save_default_args (parser, decl);
10774     }
10775
10776   /* Finish processing the declaration.  But, skip friend
10777      declarations.  */
10778   if (!friend_p && decl && decl != error_mark_node)
10779     {
10780       cp_finish_decl (decl,
10781                       initializer,
10782                       asm_specification,
10783                       /* If the initializer is in parentheses, then this is
10784                          a direct-initialization, which means that an
10785                          `explicit' constructor is OK.  Otherwise, an
10786                          `explicit' constructor cannot be used.  */
10787                       ((is_parenthesized_init || !is_initialized)
10788                      ? 0 : LOOKUP_ONLYCONVERTING));
10789     }
10790   if (!friend_p && pushed_scope)
10791     pop_scope (pushed_scope);
10792
10793   /* Remember whether or not variables were initialized by
10794      constant-expressions.  */
10795   if (decl && TREE_CODE (decl) == VAR_DECL
10796       && is_initialized && !is_non_constant_init)
10797     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10798
10799   return decl;
10800 }
10801
10802 /* Parse a declarator.
10803
10804    declarator:
10805      direct-declarator
10806      ptr-operator declarator
10807
10808    abstract-declarator:
10809      ptr-operator abstract-declarator [opt]
10810      direct-abstract-declarator
10811
10812    GNU Extensions:
10813
10814    declarator:
10815      attributes [opt] direct-declarator
10816      attributes [opt] ptr-operator declarator
10817
10818    abstract-declarator:
10819      attributes [opt] ptr-operator abstract-declarator [opt]
10820      attributes [opt] direct-abstract-declarator
10821
10822    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10823    detect constructor, destructor or conversion operators. It is set
10824    to -1 if the declarator is a name, and +1 if it is a
10825    function. Otherwise it is set to zero. Usually you just want to
10826    test for >0, but internally the negative value is used.
10827
10828    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10829    a decl-specifier-seq unless it declares a constructor, destructor,
10830    or conversion.  It might seem that we could check this condition in
10831    semantic analysis, rather than parsing, but that makes it difficult
10832    to handle something like `f()'.  We want to notice that there are
10833    no decl-specifiers, and therefore realize that this is an
10834    expression, not a declaration.)
10835
10836    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10837    the declarator is a direct-declarator of the form "(...)".  
10838
10839    MEMBER_P is true iff this declarator is a member-declarator.  */
10840
10841 static cp_declarator *
10842 cp_parser_declarator (cp_parser* parser,
10843                       cp_parser_declarator_kind dcl_kind,
10844                       int* ctor_dtor_or_conv_p,
10845                       bool* parenthesized_p,
10846                       bool member_p)
10847 {
10848   cp_token *token;
10849   cp_declarator *declarator;
10850   enum tree_code code;
10851   cp_cv_quals cv_quals;
10852   tree class_type;
10853   tree attributes = NULL_TREE;
10854
10855   /* Assume this is not a constructor, destructor, or type-conversion
10856      operator.  */
10857   if (ctor_dtor_or_conv_p)
10858     *ctor_dtor_or_conv_p = 0;
10859
10860   if (cp_parser_allow_gnu_extensions_p (parser))
10861     attributes = cp_parser_attributes_opt (parser);
10862
10863   /* Peek at the next token.  */
10864   token = cp_lexer_peek_token (parser->lexer);
10865
10866   /* Check for the ptr-operator production.  */
10867   cp_parser_parse_tentatively (parser);
10868   /* Parse the ptr-operator.  */
10869   code = cp_parser_ptr_operator (parser,
10870                                  &class_type,
10871                                  &cv_quals);
10872   /* If that worked, then we have a ptr-operator.  */
10873   if (cp_parser_parse_definitely (parser))
10874     {
10875       /* If a ptr-operator was found, then this declarator was not
10876          parenthesized.  */
10877       if (parenthesized_p)
10878         *parenthesized_p = true;
10879       /* The dependent declarator is optional if we are parsing an
10880          abstract-declarator.  */
10881       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10882         cp_parser_parse_tentatively (parser);
10883
10884       /* Parse the dependent declarator.  */
10885       declarator = cp_parser_declarator (parser, dcl_kind,
10886                                          /*ctor_dtor_or_conv_p=*/NULL,
10887                                          /*parenthesized_p=*/NULL,
10888                                          /*member_p=*/false);
10889
10890       /* If we are parsing an abstract-declarator, we must handle the
10891          case where the dependent declarator is absent.  */
10892       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10893           && !cp_parser_parse_definitely (parser))
10894         declarator = NULL;
10895
10896       /* Build the representation of the ptr-operator.  */
10897       if (class_type)
10898         declarator = make_ptrmem_declarator (cv_quals,
10899                                              class_type,
10900                                              declarator);
10901       else if (code == INDIRECT_REF)
10902         declarator = make_pointer_declarator (cv_quals, declarator);
10903       else
10904         declarator = make_reference_declarator (cv_quals, declarator);
10905     }
10906   /* Everything else is a direct-declarator.  */
10907   else
10908     {
10909       if (parenthesized_p)
10910         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10911                                                    CPP_OPEN_PAREN);
10912       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10913                                                 ctor_dtor_or_conv_p,
10914                                                 member_p);
10915     }
10916
10917   if (attributes && declarator != cp_error_declarator)
10918     declarator->attributes = attributes;
10919
10920   return declarator;
10921 }
10922
10923 /* Parse a direct-declarator or direct-abstract-declarator.
10924
10925    direct-declarator:
10926      declarator-id
10927      direct-declarator ( parameter-declaration-clause )
10928        cv-qualifier-seq [opt]
10929        exception-specification [opt]
10930      direct-declarator [ constant-expression [opt] ]
10931      ( declarator )
10932
10933    direct-abstract-declarator:
10934      direct-abstract-declarator [opt]
10935        ( parameter-declaration-clause )
10936        cv-qualifier-seq [opt]
10937        exception-specification [opt]
10938      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10939      ( abstract-declarator )
10940
10941    Returns a representation of the declarator.  DCL_KIND is
10942    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10943    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10944    we are parsing a direct-declarator.  It is
10945    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10946    of ambiguity we prefer an abstract declarator, as per
10947    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10948    cp_parser_declarator.  */
10949
10950 static cp_declarator *
10951 cp_parser_direct_declarator (cp_parser* parser,
10952                              cp_parser_declarator_kind dcl_kind,
10953                              int* ctor_dtor_or_conv_p,
10954                              bool member_p)
10955 {
10956   cp_token *token;
10957   cp_declarator *declarator = NULL;
10958   tree scope = NULL_TREE;
10959   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10960   bool saved_in_declarator_p = parser->in_declarator_p;
10961   bool first = true;
10962   tree pushed_scope = NULL_TREE;
10963
10964   while (true)
10965     {
10966       /* Peek at the next token.  */
10967       token = cp_lexer_peek_token (parser->lexer);
10968       if (token->type == CPP_OPEN_PAREN)
10969         {
10970           /* This is either a parameter-declaration-clause, or a
10971              parenthesized declarator. When we know we are parsing a
10972              named declarator, it must be a parenthesized declarator
10973              if FIRST is true. For instance, `(int)' is a
10974              parameter-declaration-clause, with an omitted
10975              direct-abstract-declarator. But `((*))', is a
10976              parenthesized abstract declarator. Finally, when T is a
10977              template parameter `(T)' is a
10978              parameter-declaration-clause, and not a parenthesized
10979              named declarator.
10980
10981              We first try and parse a parameter-declaration-clause,
10982              and then try a nested declarator (if FIRST is true).
10983
10984              It is not an error for it not to be a
10985              parameter-declaration-clause, even when FIRST is
10986              false. Consider,
10987
10988                int i (int);
10989                int i (3);
10990
10991              The first is the declaration of a function while the
10992              second is a the definition of a variable, including its
10993              initializer.
10994
10995              Having seen only the parenthesis, we cannot know which of
10996              these two alternatives should be selected.  Even more
10997              complex are examples like:
10998
10999                int i (int (a));
11000                int i (int (3));
11001
11002              The former is a function-declaration; the latter is a
11003              variable initialization.
11004
11005              Thus again, we try a parameter-declaration-clause, and if
11006              that fails, we back out and return.  */
11007
11008           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11009             {
11010               cp_parameter_declarator *params;
11011               unsigned saved_num_template_parameter_lists;
11012
11013               /* In a member-declarator, the only valid interpretation
11014                  of a parenthesis is the start of a
11015                  parameter-declaration-clause.  (It is invalid to
11016                  initialize a static data member with a parenthesized
11017                  initializer; only the "=" form of initialization is
11018                  permitted.)  */
11019               if (!member_p)
11020                 cp_parser_parse_tentatively (parser);
11021
11022               /* Consume the `('.  */
11023               cp_lexer_consume_token (parser->lexer);
11024               if (first)
11025                 {
11026                   /* If this is going to be an abstract declarator, we're
11027                      in a declarator and we can't have default args.  */
11028                   parser->default_arg_ok_p = false;
11029                   parser->in_declarator_p = true;
11030                 }
11031
11032               /* Inside the function parameter list, surrounding
11033                  template-parameter-lists do not apply.  */
11034               saved_num_template_parameter_lists
11035                 = parser->num_template_parameter_lists;
11036               parser->num_template_parameter_lists = 0;
11037
11038               /* Parse the parameter-declaration-clause.  */
11039               params = cp_parser_parameter_declaration_clause (parser);
11040
11041               parser->num_template_parameter_lists
11042                 = saved_num_template_parameter_lists;
11043
11044               /* If all went well, parse the cv-qualifier-seq and the
11045                  exception-specification.  */
11046               if (member_p || cp_parser_parse_definitely (parser))
11047                 {
11048                   cp_cv_quals cv_quals;
11049                   tree exception_specification;
11050
11051                   if (ctor_dtor_or_conv_p)
11052                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11053                   first = false;
11054                   /* Consume the `)'.  */
11055                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11056
11057                   /* Parse the cv-qualifier-seq.  */
11058                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11059                   /* And the exception-specification.  */
11060                   exception_specification
11061                     = cp_parser_exception_specification_opt (parser);
11062
11063                   /* Create the function-declarator.  */
11064                   declarator = make_call_declarator (declarator,
11065                                                      params,
11066                                                      cv_quals,
11067                                                      exception_specification);
11068                   /* Any subsequent parameter lists are to do with
11069                      return type, so are not those of the declared
11070                      function.  */
11071                   parser->default_arg_ok_p = false;
11072
11073                   /* Repeat the main loop.  */
11074                   continue;
11075                 }
11076             }
11077
11078           /* If this is the first, we can try a parenthesized
11079              declarator.  */
11080           if (first)
11081             {
11082               bool saved_in_type_id_in_expr_p;
11083
11084               parser->default_arg_ok_p = saved_default_arg_ok_p;
11085               parser->in_declarator_p = saved_in_declarator_p;
11086
11087               /* Consume the `('.  */
11088               cp_lexer_consume_token (parser->lexer);
11089               /* Parse the nested declarator.  */
11090               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11091               parser->in_type_id_in_expr_p = true;
11092               declarator
11093                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11094                                         /*parenthesized_p=*/NULL,
11095                                         member_p);
11096               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11097               first = false;
11098               /* Expect a `)'.  */
11099               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11100                 declarator = cp_error_declarator;
11101               if (declarator == cp_error_declarator)
11102                 break;
11103
11104               goto handle_declarator;
11105             }
11106           /* Otherwise, we must be done.  */
11107           else
11108             break;
11109         }
11110       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11111                && token->type == CPP_OPEN_SQUARE)
11112         {
11113           /* Parse an array-declarator.  */
11114           tree bounds;
11115
11116           if (ctor_dtor_or_conv_p)
11117             *ctor_dtor_or_conv_p = 0;
11118
11119           first = false;
11120           parser->default_arg_ok_p = false;
11121           parser->in_declarator_p = true;
11122           /* Consume the `['.  */
11123           cp_lexer_consume_token (parser->lexer);
11124           /* Peek at the next token.  */
11125           token = cp_lexer_peek_token (parser->lexer);
11126           /* If the next token is `]', then there is no
11127              constant-expression.  */
11128           if (token->type != CPP_CLOSE_SQUARE)
11129             {
11130               bool non_constant_p;
11131
11132               bounds
11133                 = cp_parser_constant_expression (parser,
11134                                                  /*allow_non_constant=*/true,
11135                                                  &non_constant_p);
11136               if (!non_constant_p)
11137                 bounds = fold_non_dependent_expr (bounds);
11138               /* Normally, the array bound must be an integral constant
11139                  expression.  However, as an extension, we allow VLAs
11140                  in function scopes.  */  
11141               else if (!at_function_scope_p ())
11142                 {
11143                   error ("array bound is not an integer constant");
11144                   bounds = error_mark_node;
11145                 }
11146             }
11147           else
11148             bounds = NULL_TREE;
11149           /* Look for the closing `]'.  */
11150           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11151             {
11152               declarator = cp_error_declarator;
11153               break;
11154             }
11155
11156           declarator = make_array_declarator (declarator, bounds);
11157         }
11158       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11159         {
11160           tree qualifying_scope;
11161           tree unqualified_name;
11162
11163           /* Parse a declarator-id */
11164           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11165             cp_parser_parse_tentatively (parser);
11166           unqualified_name = cp_parser_declarator_id (parser);
11167           qualifying_scope = parser->scope;
11168           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11169             {
11170               if (!cp_parser_parse_definitely (parser))
11171                 unqualified_name = error_mark_node;
11172               else if (qualifying_scope
11173                        || (TREE_CODE (unqualified_name) 
11174                            != IDENTIFIER_NODE))
11175                 {
11176                   cp_parser_error (parser, "expected unqualified-id");
11177                   unqualified_name = error_mark_node;
11178                 }
11179             }
11180
11181           if (unqualified_name == error_mark_node)
11182             {
11183               declarator = cp_error_declarator;
11184               break;
11185             }
11186
11187           if (qualifying_scope && at_namespace_scope_p ()
11188               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11189             {
11190               /* In the declaration of a member of a template class
11191                  outside of the class itself, the SCOPE will sometimes
11192                  be a TYPENAME_TYPE.  For example, given:
11193
11194                  template <typename T>
11195                  int S<T>::R::i = 3;
11196
11197                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11198                  this context, we must resolve S<T>::R to an ordinary
11199                  type, rather than a typename type.
11200
11201                  The reason we normally avoid resolving TYPENAME_TYPEs
11202                  is that a specialization of `S' might render
11203                  `S<T>::R' not a type.  However, if `S' is
11204                  specialized, then this `i' will not be used, so there
11205                  is no harm in resolving the types here.  */
11206               tree type;
11207               
11208               /* Resolve the TYPENAME_TYPE.  */
11209               type = resolve_typename_type (qualifying_scope,
11210                                             /*only_current_p=*/false);
11211               /* If that failed, the declarator is invalid.  */
11212               if (type == error_mark_node)
11213                 error ("%<%T::%D%> is not a type",
11214                        TYPE_CONTEXT (qualifying_scope),
11215                        TYPE_IDENTIFIER (qualifying_scope));
11216               qualifying_scope = type;
11217             }
11218
11219           declarator = make_id_declarator (qualifying_scope, 
11220                                            unqualified_name);
11221           if (unqualified_name)
11222             {
11223               tree class_type;
11224
11225               if (qualifying_scope
11226                   && CLASS_TYPE_P (qualifying_scope))
11227                 class_type = qualifying_scope;
11228               else
11229                 class_type = current_class_type;
11230
11231               if (class_type)
11232                 {
11233                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11234                     declarator->u.id.sfk = sfk_destructor;
11235                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11236                     declarator->u.id.sfk = sfk_conversion;
11237                   else if (/* There's no way to declare a constructor
11238                               for an anonymous type, even if the type
11239                               got a name for linkage purposes.  */
11240                            !TYPE_WAS_ANONYMOUS (class_type)
11241                            && (constructor_name_p (unqualified_name,
11242                                                    class_type)
11243                                || (TREE_CODE (unqualified_name) == TYPE_DECL
11244                                    && (same_type_p 
11245                                        (TREE_TYPE (unqualified_name),
11246                                         class_type)))))
11247                     declarator->u.id.sfk = sfk_constructor;
11248
11249                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11250                     *ctor_dtor_or_conv_p = -1;
11251                   if (qualifying_scope
11252                       && TREE_CODE (unqualified_name) == TYPE_DECL
11253                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11254                     {
11255                       error ("invalid use of constructor as a template");
11256                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11257                               "the constructor in a qualified name",
11258                               class_type,
11259                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11260                               class_type, class_type);
11261                     }
11262                 }
11263             }
11264
11265         handle_declarator:;
11266           scope = get_scope_of_declarator (declarator);
11267           if (scope)
11268             /* Any names that appear after the declarator-id for a
11269                member are looked up in the containing scope.  */
11270             pushed_scope = push_scope (scope);
11271           parser->in_declarator_p = true;
11272           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11273               || (declarator && declarator->kind == cdk_id))
11274             /* Default args are only allowed on function
11275                declarations.  */
11276             parser->default_arg_ok_p = saved_default_arg_ok_p;
11277           else
11278             parser->default_arg_ok_p = false;
11279
11280           first = false;
11281         }
11282       /* We're done.  */
11283       else
11284         break;
11285     }
11286
11287   /* For an abstract declarator, we might wind up with nothing at this
11288      point.  That's an error; the declarator is not optional.  */
11289   if (!declarator)
11290     cp_parser_error (parser, "expected declarator");
11291
11292   /* If we entered a scope, we must exit it now.  */
11293   if (pushed_scope)
11294     pop_scope (pushed_scope);
11295
11296   parser->default_arg_ok_p = saved_default_arg_ok_p;
11297   parser->in_declarator_p = saved_in_declarator_p;
11298
11299   return declarator;
11300 }
11301
11302 /* Parse a ptr-operator.
11303
11304    ptr-operator:
11305      * cv-qualifier-seq [opt]
11306      &
11307      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11308
11309    GNU Extension:
11310
11311    ptr-operator:
11312      & cv-qualifier-seq [opt]
11313
11314    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11315    Returns ADDR_EXPR if a reference was used.  In the case of a
11316    pointer-to-member, *TYPE is filled in with the TYPE containing the
11317    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11318    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11319    ERROR_MARK if an error occurred.  */
11320
11321 static enum tree_code
11322 cp_parser_ptr_operator (cp_parser* parser,
11323                         tree* type,
11324                         cp_cv_quals *cv_quals)
11325 {
11326   enum tree_code code = ERROR_MARK;
11327   cp_token *token;
11328
11329   /* Assume that it's not a pointer-to-member.  */
11330   *type = NULL_TREE;
11331   /* And that there are no cv-qualifiers.  */
11332   *cv_quals = TYPE_UNQUALIFIED;
11333
11334   /* Peek at the next token.  */
11335   token = cp_lexer_peek_token (parser->lexer);
11336   /* If it's a `*' or `&' we have a pointer or reference.  */
11337   if (token->type == CPP_MULT || token->type == CPP_AND)
11338     {
11339       /* Remember which ptr-operator we were processing.  */
11340       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11341
11342       /* Consume the `*' or `&'.  */
11343       cp_lexer_consume_token (parser->lexer);
11344
11345       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11346          `&', if we are allowing GNU extensions.  (The only qualifier
11347          that can legally appear after `&' is `restrict', but that is
11348          enforced during semantic analysis.  */
11349       if (code == INDIRECT_REF
11350           || cp_parser_allow_gnu_extensions_p (parser))
11351         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11352     }
11353   else
11354     {
11355       /* Try the pointer-to-member case.  */
11356       cp_parser_parse_tentatively (parser);
11357       /* Look for the optional `::' operator.  */
11358       cp_parser_global_scope_opt (parser,
11359                                   /*current_scope_valid_p=*/false);
11360       /* Look for the nested-name specifier.  */
11361       cp_parser_nested_name_specifier (parser,
11362                                        /*typename_keyword_p=*/false,
11363                                        /*check_dependency_p=*/true,
11364                                        /*type_p=*/false,
11365                                        /*is_declaration=*/false);
11366       /* If we found it, and the next token is a `*', then we are
11367          indeed looking at a pointer-to-member operator.  */
11368       if (!cp_parser_error_occurred (parser)
11369           && cp_parser_require (parser, CPP_MULT, "`*'"))
11370         {
11371           /* The type of which the member is a member is given by the
11372              current SCOPE.  */
11373           *type = parser->scope;
11374           /* The next name will not be qualified.  */
11375           parser->scope = NULL_TREE;
11376           parser->qualifying_scope = NULL_TREE;
11377           parser->object_scope = NULL_TREE;
11378           /* Indicate that the `*' operator was used.  */
11379           code = INDIRECT_REF;
11380           /* Look for the optional cv-qualifier-seq.  */
11381           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11382         }
11383       /* If that didn't work we don't have a ptr-operator.  */
11384       if (!cp_parser_parse_definitely (parser))
11385         cp_parser_error (parser, "expected ptr-operator");
11386     }
11387
11388   return code;
11389 }
11390
11391 /* Parse an (optional) cv-qualifier-seq.
11392
11393    cv-qualifier-seq:
11394      cv-qualifier cv-qualifier-seq [opt]
11395
11396    cv-qualifier:
11397      const
11398      volatile
11399
11400    GNU Extension:
11401
11402    cv-qualifier:
11403      __restrict__
11404
11405    Returns a bitmask representing the cv-qualifiers.  */
11406
11407 static cp_cv_quals
11408 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11409 {
11410   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11411
11412   while (true)
11413     {
11414       cp_token *token;
11415       cp_cv_quals cv_qualifier;
11416
11417       /* Peek at the next token.  */
11418       token = cp_lexer_peek_token (parser->lexer);
11419       /* See if it's a cv-qualifier.  */
11420       switch (token->keyword)
11421         {
11422         case RID_CONST:
11423           cv_qualifier = TYPE_QUAL_CONST;
11424           break;
11425
11426         case RID_VOLATILE:
11427           cv_qualifier = TYPE_QUAL_VOLATILE;
11428           break;
11429
11430         case RID_RESTRICT:
11431           cv_qualifier = TYPE_QUAL_RESTRICT;
11432           break;
11433
11434         default:
11435           cv_qualifier = TYPE_UNQUALIFIED;
11436           break;
11437         }
11438
11439       if (!cv_qualifier)
11440         break;
11441
11442       if (cv_quals & cv_qualifier)
11443         {
11444           error ("duplicate cv-qualifier");
11445           cp_lexer_purge_token (parser->lexer);
11446         }
11447       else
11448         {
11449           cp_lexer_consume_token (parser->lexer);
11450           cv_quals |= cv_qualifier;
11451         }
11452     }
11453
11454   return cv_quals;
11455 }
11456
11457 /* Parse a declarator-id.
11458
11459    declarator-id:
11460      id-expression
11461      :: [opt] nested-name-specifier [opt] type-name
11462
11463    In the `id-expression' case, the value returned is as for
11464    cp_parser_id_expression if the id-expression was an unqualified-id.
11465    If the id-expression was a qualified-id, then a SCOPE_REF is
11466    returned.  The first operand is the scope (either a NAMESPACE_DECL
11467    or TREE_TYPE), but the second is still just a representation of an
11468    unqualified-id.  */
11469
11470 static tree
11471 cp_parser_declarator_id (cp_parser* parser)
11472 {
11473   /* The expression must be an id-expression.  Assume that qualified
11474      names are the names of types so that:
11475
11476        template <class T>
11477        int S<T>::R::i = 3;
11478
11479      will work; we must treat `S<T>::R' as the name of a type.
11480      Similarly, assume that qualified names are templates, where
11481      required, so that:
11482
11483        template <class T>
11484        int S<T>::R<T>::i = 3;
11485
11486      will work, too.  */
11487   return cp_parser_id_expression (parser,
11488                                   /*template_keyword_p=*/false,
11489                                   /*check_dependency_p=*/false,
11490                                   /*template_p=*/NULL,
11491                                   /*declarator_p=*/true);
11492 }
11493
11494 /* Parse a type-id.
11495
11496    type-id:
11497      type-specifier-seq abstract-declarator [opt]
11498
11499    Returns the TYPE specified.  */
11500
11501 static tree
11502 cp_parser_type_id (cp_parser* parser)
11503 {
11504   cp_decl_specifier_seq type_specifier_seq;
11505   cp_declarator *abstract_declarator;
11506
11507   /* Parse the type-specifier-seq.  */
11508   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11509                                 &type_specifier_seq);
11510   if (type_specifier_seq.type == error_mark_node)
11511     return error_mark_node;
11512
11513   /* There might or might not be an abstract declarator.  */
11514   cp_parser_parse_tentatively (parser);
11515   /* Look for the declarator.  */
11516   abstract_declarator
11517     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11518                             /*parenthesized_p=*/NULL,
11519                             /*member_p=*/false);
11520   /* Check to see if there really was a declarator.  */
11521   if (!cp_parser_parse_definitely (parser))
11522     abstract_declarator = NULL;
11523
11524   return groktypename (&type_specifier_seq, abstract_declarator);
11525 }
11526
11527 /* Parse a type-specifier-seq.
11528
11529    type-specifier-seq:
11530      type-specifier type-specifier-seq [opt]
11531
11532    GNU extension:
11533
11534    type-specifier-seq:
11535      attributes type-specifier-seq [opt]
11536
11537    If IS_CONDITION is true, we are at the start of a "condition",
11538    e.g., we've just seen "if (".
11539
11540    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11541
11542 static void
11543 cp_parser_type_specifier_seq (cp_parser* parser,
11544                               bool is_condition,
11545                               cp_decl_specifier_seq *type_specifier_seq)
11546 {
11547   bool seen_type_specifier = false;
11548   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11549
11550   /* Clear the TYPE_SPECIFIER_SEQ.  */
11551   clear_decl_specs (type_specifier_seq);
11552
11553   /* Parse the type-specifiers and attributes.  */
11554   while (true)
11555     {
11556       tree type_specifier;
11557       bool is_cv_qualifier;
11558
11559       /* Check for attributes first.  */
11560       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11561         {
11562           type_specifier_seq->attributes =
11563             chainon (type_specifier_seq->attributes,
11564                      cp_parser_attributes_opt (parser));
11565           continue;
11566         }
11567
11568       /* Look for the type-specifier.  */
11569       type_specifier = cp_parser_type_specifier (parser,
11570                                                  flags,
11571                                                  type_specifier_seq,
11572                                                  /*is_declaration=*/false,
11573                                                  NULL,
11574                                                  &is_cv_qualifier);
11575       if (!type_specifier)
11576         {
11577           /* If the first type-specifier could not be found, this is not a
11578              type-specifier-seq at all.  */
11579           if (!seen_type_specifier)
11580             {
11581               cp_parser_error (parser, "expected type-specifier");
11582               type_specifier_seq->type = error_mark_node;
11583               return;
11584             }
11585           /* If subsequent type-specifiers could not be found, the
11586              type-specifier-seq is complete.  */
11587           break;
11588         }
11589
11590       seen_type_specifier = true;
11591       /* The standard says that a condition can be:
11592
11593             type-specifier-seq declarator = assignment-expression
11594       
11595          However, given:
11596
11597            struct S {};
11598            if (int S = ...)
11599
11600          we should treat the "S" as a declarator, not as a
11601          type-specifier.  The standard doesn't say that explicitly for
11602          type-specifier-seq, but it does say that for
11603          decl-specifier-seq in an ordinary declaration.  Perhaps it
11604          would be clearer just to allow a decl-specifier-seq here, and
11605          then add a semantic restriction that if any decl-specifiers
11606          that are not type-specifiers appear, the program is invalid.  */
11607       if (is_condition && !is_cv_qualifier)
11608         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; 
11609     }
11610
11611   return;
11612 }
11613
11614 /* Parse a parameter-declaration-clause.
11615
11616    parameter-declaration-clause:
11617      parameter-declaration-list [opt] ... [opt]
11618      parameter-declaration-list , ...
11619
11620    Returns a representation for the parameter declarations.  A return
11621    value of NULL indicates a parameter-declaration-clause consisting
11622    only of an ellipsis.  */
11623
11624 static cp_parameter_declarator *
11625 cp_parser_parameter_declaration_clause (cp_parser* parser)
11626 {
11627   cp_parameter_declarator *parameters;
11628   cp_token *token;
11629   bool ellipsis_p;
11630   bool is_error;
11631
11632   /* Peek at the next token.  */
11633   token = cp_lexer_peek_token (parser->lexer);
11634   /* Check for trivial parameter-declaration-clauses.  */
11635   if (token->type == CPP_ELLIPSIS)
11636     {
11637       /* Consume the `...' token.  */
11638       cp_lexer_consume_token (parser->lexer);
11639       return NULL;
11640     }
11641   else if (token->type == CPP_CLOSE_PAREN)
11642     /* There are no parameters.  */
11643     {
11644 #ifndef NO_IMPLICIT_EXTERN_C
11645       if (in_system_header && current_class_type == NULL
11646           && current_lang_name == lang_name_c)
11647         return NULL;
11648       else
11649 #endif
11650         return no_parameters;
11651     }
11652   /* Check for `(void)', too, which is a special case.  */
11653   else if (token->keyword == RID_VOID
11654            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11655                == CPP_CLOSE_PAREN))
11656     {
11657       /* Consume the `void' token.  */
11658       cp_lexer_consume_token (parser->lexer);
11659       /* There are no parameters.  */
11660       return no_parameters;
11661     }
11662
11663   /* Parse the parameter-declaration-list.  */
11664   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11665   /* If a parse error occurred while parsing the
11666      parameter-declaration-list, then the entire
11667      parameter-declaration-clause is erroneous.  */
11668   if (is_error)
11669     return NULL;
11670
11671   /* Peek at the next token.  */
11672   token = cp_lexer_peek_token (parser->lexer);
11673   /* If it's a `,', the clause should terminate with an ellipsis.  */
11674   if (token->type == CPP_COMMA)
11675     {
11676       /* Consume the `,'.  */
11677       cp_lexer_consume_token (parser->lexer);
11678       /* Expect an ellipsis.  */
11679       ellipsis_p
11680         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11681     }
11682   /* It might also be `...' if the optional trailing `,' was
11683      omitted.  */
11684   else if (token->type == CPP_ELLIPSIS)
11685     {
11686       /* Consume the `...' token.  */
11687       cp_lexer_consume_token (parser->lexer);
11688       /* And remember that we saw it.  */
11689       ellipsis_p = true;
11690     }
11691   else
11692     ellipsis_p = false;
11693
11694   /* Finish the parameter list.  */
11695   if (parameters && ellipsis_p)
11696     parameters->ellipsis_p = true;
11697
11698   return parameters;
11699 }
11700
11701 /* Parse a parameter-declaration-list.
11702
11703    parameter-declaration-list:
11704      parameter-declaration
11705      parameter-declaration-list , parameter-declaration
11706
11707    Returns a representation of the parameter-declaration-list, as for
11708    cp_parser_parameter_declaration_clause.  However, the
11709    `void_list_node' is never appended to the list.  Upon return,
11710    *IS_ERROR will be true iff an error occurred.  */
11711
11712 static cp_parameter_declarator *
11713 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11714 {
11715   cp_parameter_declarator *parameters = NULL;
11716   cp_parameter_declarator **tail = &parameters;
11717
11718   /* Assume all will go well.  */
11719   *is_error = false;
11720
11721   /* Look for more parameters.  */
11722   while (true)
11723     {
11724       cp_parameter_declarator *parameter;
11725       bool parenthesized_p;
11726       /* Parse the parameter.  */
11727       parameter
11728         = cp_parser_parameter_declaration (parser,
11729                                            /*template_parm_p=*/false,
11730                                            &parenthesized_p);
11731
11732       /* If a parse error occurred parsing the parameter declaration,
11733          then the entire parameter-declaration-list is erroneous.  */
11734       if (!parameter)
11735         {
11736           *is_error = true;
11737           parameters = NULL;
11738           break;
11739         }
11740       /* Add the new parameter to the list.  */
11741       *tail = parameter;
11742       tail = &parameter->next;
11743
11744       /* Peek at the next token.  */
11745       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11746           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11747         /* The parameter-declaration-list is complete.  */
11748         break;
11749       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11750         {
11751           cp_token *token;
11752
11753           /* Peek at the next token.  */
11754           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11755           /* If it's an ellipsis, then the list is complete.  */
11756           if (token->type == CPP_ELLIPSIS)
11757             break;
11758           /* Otherwise, there must be more parameters.  Consume the
11759              `,'.  */
11760           cp_lexer_consume_token (parser->lexer);
11761           /* When parsing something like:
11762
11763                 int i(float f, double d)
11764
11765              we can tell after seeing the declaration for "f" that we
11766              are not looking at an initialization of a variable "i",
11767              but rather at the declaration of a function "i".
11768
11769              Due to the fact that the parsing of template arguments
11770              (as specified to a template-id) requires backtracking we
11771              cannot use this technique when inside a template argument
11772              list.  */
11773           if (!parser->in_template_argument_list_p
11774               && !parser->in_type_id_in_expr_p
11775               && cp_parser_uncommitted_to_tentative_parse_p (parser)
11776               /* However, a parameter-declaration of the form
11777                  "foat(f)" (which is a valid declaration of a
11778                  parameter "f") can also be interpreted as an
11779                  expression (the conversion of "f" to "float").  */
11780               && !parenthesized_p)
11781             cp_parser_commit_to_tentative_parse (parser);
11782         }
11783       else
11784         {
11785           cp_parser_error (parser, "expected %<,%> or %<...%>");
11786           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11787             cp_parser_skip_to_closing_parenthesis (parser,
11788                                                    /*recovering=*/true,
11789                                                    /*or_comma=*/false,
11790                                                    /*consume_paren=*/false);
11791           break;
11792         }
11793     }
11794
11795   return parameters;
11796 }
11797
11798 /* Parse a parameter declaration.
11799
11800    parameter-declaration:
11801      decl-specifier-seq declarator
11802      decl-specifier-seq declarator = assignment-expression
11803      decl-specifier-seq abstract-declarator [opt]
11804      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11805
11806    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11807    declares a template parameter.  (In that case, a non-nested `>'
11808    token encountered during the parsing of the assignment-expression
11809    is not interpreted as a greater-than operator.)
11810
11811    Returns a representation of the parameter, or NULL if an error
11812    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11813    true iff the declarator is of the form "(p)".  */
11814
11815 static cp_parameter_declarator *
11816 cp_parser_parameter_declaration (cp_parser *parser,
11817                                  bool template_parm_p,
11818                                  bool *parenthesized_p)
11819 {
11820   int declares_class_or_enum;
11821   bool greater_than_is_operator_p;
11822   cp_decl_specifier_seq decl_specifiers;
11823   cp_declarator *declarator;
11824   tree default_argument;
11825   cp_token *token;
11826   const char *saved_message;
11827
11828   /* In a template parameter, `>' is not an operator.
11829
11830      [temp.param]
11831
11832      When parsing a default template-argument for a non-type
11833      template-parameter, the first non-nested `>' is taken as the end
11834      of the template parameter-list rather than a greater-than
11835      operator.  */
11836   greater_than_is_operator_p = !template_parm_p;
11837
11838   /* Type definitions may not appear in parameter types.  */
11839   saved_message = parser->type_definition_forbidden_message;
11840   parser->type_definition_forbidden_message
11841     = "types may not be defined in parameter types";
11842
11843   /* Parse the declaration-specifiers.  */
11844   cp_parser_decl_specifier_seq (parser,
11845                                 CP_PARSER_FLAGS_NONE,
11846                                 &decl_specifiers,
11847                                 &declares_class_or_enum);
11848   /* If an error occurred, there's no reason to attempt to parse the
11849      rest of the declaration.  */
11850   if (cp_parser_error_occurred (parser))
11851     {
11852       parser->type_definition_forbidden_message = saved_message;
11853       return NULL;
11854     }
11855
11856   /* Peek at the next token.  */
11857   token = cp_lexer_peek_token (parser->lexer);
11858   /* If the next token is a `)', `,', `=', `>', or `...', then there
11859      is no declarator.  */
11860   if (token->type == CPP_CLOSE_PAREN
11861       || token->type == CPP_COMMA
11862       || token->type == CPP_EQ
11863       || token->type == CPP_ELLIPSIS
11864       || token->type == CPP_GREATER)
11865     {
11866       declarator = NULL;
11867       if (parenthesized_p)
11868         *parenthesized_p = false;
11869     }
11870   /* Otherwise, there should be a declarator.  */
11871   else
11872     {
11873       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11874       parser->default_arg_ok_p = false;
11875
11876       /* After seeing a decl-specifier-seq, if the next token is not a
11877          "(", there is no possibility that the code is a valid
11878          expression.  Therefore, if parsing tentatively, we commit at
11879          this point.  */
11880       if (!parser->in_template_argument_list_p
11881           /* In an expression context, having seen:
11882
11883                (int((char ...
11884
11885              we cannot be sure whether we are looking at a
11886              function-type (taking a "char" as a parameter) or a cast
11887              of some object of type "char" to "int".  */
11888           && !parser->in_type_id_in_expr_p
11889           && cp_parser_uncommitted_to_tentative_parse_p (parser)
11890           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11891         cp_parser_commit_to_tentative_parse (parser);
11892       /* Parse the declarator.  */
11893       declarator = cp_parser_declarator (parser,
11894                                          CP_PARSER_DECLARATOR_EITHER,
11895                                          /*ctor_dtor_or_conv_p=*/NULL,
11896                                          parenthesized_p,
11897                                          /*member_p=*/false);
11898       parser->default_arg_ok_p = saved_default_arg_ok_p;
11899       /* After the declarator, allow more attributes.  */
11900       decl_specifiers.attributes
11901         = chainon (decl_specifiers.attributes,
11902                    cp_parser_attributes_opt (parser));
11903     }
11904
11905   /* The restriction on defining new types applies only to the type
11906      of the parameter, not to the default argument.  */
11907   parser->type_definition_forbidden_message = saved_message;
11908
11909   /* If the next token is `=', then process a default argument.  */
11910   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11911     {
11912       bool saved_greater_than_is_operator_p;
11913       /* Consume the `='.  */
11914       cp_lexer_consume_token (parser->lexer);
11915
11916       /* If we are defining a class, then the tokens that make up the
11917          default argument must be saved and processed later.  */
11918       if (!template_parm_p && at_class_scope_p ()
11919           && TYPE_BEING_DEFINED (current_class_type))
11920         {
11921           unsigned depth = 0;
11922           cp_token *first_token;
11923           cp_token *token;
11924
11925           /* Add tokens until we have processed the entire default
11926              argument.  We add the range [first_token, token).  */
11927           first_token = cp_lexer_peek_token (parser->lexer);
11928           while (true)
11929             {
11930               bool done = false;
11931
11932               /* Peek at the next token.  */
11933               token = cp_lexer_peek_token (parser->lexer);
11934               /* What we do depends on what token we have.  */
11935               switch (token->type)
11936                 {
11937                   /* In valid code, a default argument must be
11938                      immediately followed by a `,' `)', or `...'.  */
11939                 case CPP_COMMA:
11940                 case CPP_CLOSE_PAREN:
11941                 case CPP_ELLIPSIS:
11942                   /* If we run into a non-nested `;', `}', or `]',
11943                      then the code is invalid -- but the default
11944                      argument is certainly over.  */
11945                 case CPP_SEMICOLON:
11946                 case CPP_CLOSE_BRACE:
11947                 case CPP_CLOSE_SQUARE:
11948                   if (depth == 0)
11949                     done = true;
11950                   /* Update DEPTH, if necessary.  */
11951                   else if (token->type == CPP_CLOSE_PAREN
11952                            || token->type == CPP_CLOSE_BRACE
11953                            || token->type == CPP_CLOSE_SQUARE)
11954                     --depth;
11955                   break;
11956
11957                 case CPP_OPEN_PAREN:
11958                 case CPP_OPEN_SQUARE:
11959                 case CPP_OPEN_BRACE:
11960                   ++depth;
11961                   break;
11962
11963                 case CPP_GREATER:
11964                   /* If we see a non-nested `>', and `>' is not an
11965                      operator, then it marks the end of the default
11966                      argument.  */
11967                   if (!depth && !greater_than_is_operator_p)
11968                     done = true;
11969                   break;
11970
11971                   /* If we run out of tokens, issue an error message.  */
11972                 case CPP_EOF:
11973                   error ("file ends in default argument");
11974                   done = true;
11975                   break;
11976
11977                 case CPP_NAME:
11978                 case CPP_SCOPE:
11979                   /* In these cases, we should look for template-ids.
11980                      For example, if the default argument is
11981                      `X<int, double>()', we need to do name lookup to
11982                      figure out whether or not `X' is a template; if
11983                      so, the `,' does not end the default argument.
11984
11985                      That is not yet done.  */
11986                   break;
11987
11988                 default:
11989                   break;
11990                 }
11991
11992               /* If we've reached the end, stop.  */
11993               if (done)
11994                 break;
11995
11996               /* Add the token to the token block.  */
11997               token = cp_lexer_consume_token (parser->lexer);
11998             }
11999
12000           /* Create a DEFAULT_ARG to represented the unparsed default
12001              argument.  */
12002           default_argument = make_node (DEFAULT_ARG);
12003           DEFARG_TOKENS (default_argument)
12004             = cp_token_cache_new (first_token, token);  
12005         }
12006       /* Outside of a class definition, we can just parse the
12007          assignment-expression.  */
12008       else
12009         {
12010           bool saved_local_variables_forbidden_p;
12011
12012           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12013              set correctly.  */
12014           saved_greater_than_is_operator_p
12015             = parser->greater_than_is_operator_p;
12016           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12017           /* Local variable names (and the `this' keyword) may not
12018              appear in a default argument.  */
12019           saved_local_variables_forbidden_p
12020             = parser->local_variables_forbidden_p;
12021           parser->local_variables_forbidden_p = true;
12022           /* Parse the assignment-expression.  */
12023           default_argument 
12024             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12025           /* Restore saved state.  */
12026           parser->greater_than_is_operator_p
12027             = saved_greater_than_is_operator_p;
12028           parser->local_variables_forbidden_p
12029             = saved_local_variables_forbidden_p;
12030         }
12031       if (!parser->default_arg_ok_p)
12032         {
12033           if (!flag_pedantic_errors)
12034             warning ("deprecated use of default argument for parameter of non-function");
12035           else
12036             {
12037               error ("default arguments are only permitted for function parameters");
12038               default_argument = NULL_TREE;
12039             }
12040         }
12041     }
12042   else
12043     default_argument = NULL_TREE;
12044
12045   return make_parameter_declarator (&decl_specifiers,
12046                                     declarator,
12047                                     default_argument);
12048 }
12049
12050 /* Parse a function-body.
12051
12052    function-body:
12053      compound_statement  */
12054
12055 static void
12056 cp_parser_function_body (cp_parser *parser)
12057 {
12058   cp_parser_compound_statement (parser, NULL, false);
12059 }
12060
12061 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12062    true if a ctor-initializer was present.  */
12063
12064 static bool
12065 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12066 {
12067   tree body;
12068   bool ctor_initializer_p;
12069
12070   /* Begin the function body.  */
12071   body = begin_function_body ();
12072   /* Parse the optional ctor-initializer.  */
12073   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12074   /* Parse the function-body.  */
12075   cp_parser_function_body (parser);
12076   /* Finish the function body.  */
12077   finish_function_body (body);
12078
12079   return ctor_initializer_p;
12080 }
12081
12082 /* Parse an initializer.
12083
12084    initializer:
12085      = initializer-clause
12086      ( expression-list )
12087
12088    Returns a expression representing the initializer.  If no
12089    initializer is present, NULL_TREE is returned.
12090
12091    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12092    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12093    set to FALSE if there is no initializer present.  If there is an
12094    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12095    is set to true; otherwise it is set to false.  */
12096
12097 static tree
12098 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12099                        bool* non_constant_p)
12100 {
12101   cp_token *token;
12102   tree init;
12103
12104   /* Peek at the next token.  */
12105   token = cp_lexer_peek_token (parser->lexer);
12106
12107   /* Let our caller know whether or not this initializer was
12108      parenthesized.  */
12109   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12110   /* Assume that the initializer is constant.  */
12111   *non_constant_p = false;
12112
12113   if (token->type == CPP_EQ)
12114     {
12115       /* Consume the `='.  */
12116       cp_lexer_consume_token (parser->lexer);
12117       /* Parse the initializer-clause.  */
12118       init = cp_parser_initializer_clause (parser, non_constant_p);
12119     }
12120   else if (token->type == CPP_OPEN_PAREN)
12121     init = cp_parser_parenthesized_expression_list (parser, false,
12122                                                     /*cast_p=*/false,
12123                                                     non_constant_p);
12124   else
12125     {
12126       /* Anything else is an error.  */
12127       cp_parser_error (parser, "expected initializer");
12128       init = error_mark_node;
12129     }
12130
12131   return init;
12132 }
12133
12134 /* Parse an initializer-clause.
12135
12136    initializer-clause:
12137      assignment-expression
12138      { initializer-list , [opt] }
12139      { }
12140
12141    Returns an expression representing the initializer.
12142
12143    If the `assignment-expression' production is used the value
12144    returned is simply a representation for the expression.
12145
12146    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12147    the elements of the initializer-list (or NULL_TREE, if the last
12148    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12149    NULL_TREE.  There is no way to detect whether or not the optional
12150    trailing `,' was provided.  NON_CONSTANT_P is as for
12151    cp_parser_initializer.  */
12152
12153 static tree
12154 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12155 {
12156   tree initializer;
12157
12158   /* Assume the expression is constant.  */
12159   *non_constant_p = false;
12160
12161   /* If it is not a `{', then we are looking at an
12162      assignment-expression.  */
12163   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12164     {
12165       initializer
12166         = cp_parser_constant_expression (parser,
12167                                         /*allow_non_constant_p=*/true,
12168                                         non_constant_p);
12169       if (!*non_constant_p)
12170         initializer = fold_non_dependent_expr (initializer);
12171     }
12172   else
12173     {
12174       /* Consume the `{' token.  */
12175       cp_lexer_consume_token (parser->lexer);
12176       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12177       initializer = make_node (CONSTRUCTOR);
12178       /* If it's not a `}', then there is a non-trivial initializer.  */
12179       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12180         {
12181           /* Parse the initializer list.  */
12182           CONSTRUCTOR_ELTS (initializer)
12183             = cp_parser_initializer_list (parser, non_constant_p);
12184           /* A trailing `,' token is allowed.  */
12185           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12186             cp_lexer_consume_token (parser->lexer);
12187         }
12188       /* Now, there should be a trailing `}'.  */
12189       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12190     }
12191
12192   return initializer;
12193 }
12194
12195 /* Parse an initializer-list.
12196
12197    initializer-list:
12198      initializer-clause
12199      initializer-list , initializer-clause
12200
12201    GNU Extension:
12202
12203    initializer-list:
12204      identifier : initializer-clause
12205      initializer-list, identifier : initializer-clause
12206
12207    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12208    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12209    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12210    as for cp_parser_initializer.  */
12211
12212 static tree
12213 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12214 {
12215   tree initializers = NULL_TREE;
12216
12217   /* Assume all of the expressions are constant.  */
12218   *non_constant_p = false;
12219
12220   /* Parse the rest of the list.  */
12221   while (true)
12222     {
12223       cp_token *token;
12224       tree identifier;
12225       tree initializer;
12226       bool clause_non_constant_p;
12227
12228       /* If the next token is an identifier and the following one is a
12229          colon, we are looking at the GNU designated-initializer
12230          syntax.  */
12231       if (cp_parser_allow_gnu_extensions_p (parser)
12232           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12233           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12234         {
12235           /* Consume the identifier.  */
12236           identifier = cp_lexer_consume_token (parser->lexer)->value;
12237           /* Consume the `:'.  */
12238           cp_lexer_consume_token (parser->lexer);
12239         }
12240       else
12241         identifier = NULL_TREE;
12242
12243       /* Parse the initializer.  */
12244       initializer = cp_parser_initializer_clause (parser,
12245                                                   &clause_non_constant_p);
12246       /* If any clause is non-constant, so is the entire initializer.  */
12247       if (clause_non_constant_p)
12248         *non_constant_p = true;
12249       /* Add it to the list.  */
12250       initializers = tree_cons (identifier, initializer, initializers);
12251
12252       /* If the next token is not a comma, we have reached the end of
12253          the list.  */
12254       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12255         break;
12256
12257       /* Peek at the next token.  */
12258       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12259       /* If the next token is a `}', then we're still done.  An
12260          initializer-clause can have a trailing `,' after the
12261          initializer-list and before the closing `}'.  */
12262       if (token->type == CPP_CLOSE_BRACE)
12263         break;
12264
12265       /* Consume the `,' token.  */
12266       cp_lexer_consume_token (parser->lexer);
12267     }
12268
12269   /* The initializers were built up in reverse order, so we need to
12270      reverse them now.  */
12271   return nreverse (initializers);
12272 }
12273
12274 /* Classes [gram.class] */
12275
12276 /* Parse a class-name.
12277
12278    class-name:
12279      identifier
12280      template-id
12281
12282    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12283    to indicate that names looked up in dependent types should be
12284    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12285    keyword has been used to indicate that the name that appears next
12286    is a template.  TAG_TYPE indicates the explicit tag given before
12287    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12288    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12289    is the class being defined in a class-head.
12290
12291    Returns the TYPE_DECL representing the class.  */
12292
12293 static tree
12294 cp_parser_class_name (cp_parser *parser,
12295                       bool typename_keyword_p,
12296                       bool template_keyword_p,
12297                       enum tag_types tag_type,
12298                       bool check_dependency_p,
12299                       bool class_head_p,
12300                       bool is_declaration)
12301 {
12302   tree decl;
12303   tree scope;
12304   bool typename_p;
12305   cp_token *token;
12306
12307   /* All class-names start with an identifier.  */
12308   token = cp_lexer_peek_token (parser->lexer);
12309   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12310     {
12311       cp_parser_error (parser, "expected class-name");
12312       return error_mark_node;
12313     }
12314
12315   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12316      to a template-id, so we save it here.  */
12317   scope = parser->scope;
12318   if (scope == error_mark_node)
12319     return error_mark_node;
12320
12321   /* Any name names a type if we're following the `typename' keyword
12322      in a qualified name where the enclosing scope is type-dependent.  */
12323   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12324                 && dependent_type_p (scope));
12325   /* Handle the common case (an identifier, but not a template-id)
12326      efficiently.  */
12327   if (token->type == CPP_NAME
12328       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12329     {
12330       tree identifier;
12331
12332       /* Look for the identifier.  */
12333       identifier = cp_parser_identifier (parser);
12334       /* If the next token isn't an identifier, we are certainly not
12335          looking at a class-name.  */
12336       if (identifier == error_mark_node)
12337         decl = error_mark_node;
12338       /* If we know this is a type-name, there's no need to look it
12339          up.  */
12340       else if (typename_p)
12341         decl = identifier;
12342       else
12343         {
12344           /* If the next token is a `::', then the name must be a type
12345              name.
12346
12347              [basic.lookup.qual]
12348
12349              During the lookup for a name preceding the :: scope
12350              resolution operator, object, function, and enumerator
12351              names are ignored.  */
12352           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12353             tag_type = typename_type;
12354           /* Look up the name.  */
12355           decl = cp_parser_lookup_name (parser, identifier,
12356                                         tag_type,
12357                                         /*is_template=*/false,
12358                                         /*is_namespace=*/false,
12359                                         check_dependency_p,
12360                                         /*ambiguous_p=*/NULL);
12361         }
12362     }
12363   else
12364     {
12365       /* Try a template-id.  */
12366       decl = cp_parser_template_id (parser, template_keyword_p,
12367                                     check_dependency_p,
12368                                     is_declaration);
12369       if (decl == error_mark_node)
12370         return error_mark_node;
12371     }
12372
12373   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12374
12375   /* If this is a typename, create a TYPENAME_TYPE.  */
12376   if (typename_p && decl != error_mark_node)
12377     {
12378       decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12379       if (decl != error_mark_node)
12380         decl = TYPE_NAME (decl);
12381     }
12382
12383   /* Check to see that it is really the name of a class.  */
12384   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12385       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12386       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12387     /* Situations like this:
12388
12389          template <typename T> struct A {
12390            typename T::template X<int>::I i;
12391          };
12392
12393        are problematic.  Is `T::template X<int>' a class-name?  The
12394        standard does not seem to be definitive, but there is no other
12395        valid interpretation of the following `::'.  Therefore, those
12396        names are considered class-names.  */
12397     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12398   else if (decl == error_mark_node
12399            || TREE_CODE (decl) != TYPE_DECL
12400            || TREE_TYPE (decl) == error_mark_node
12401            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12402     {
12403       cp_parser_error (parser, "expected class-name");
12404       return error_mark_node;
12405     }
12406
12407   return decl;
12408 }
12409
12410 /* Parse a class-specifier.
12411
12412    class-specifier:
12413      class-head { member-specification [opt] }
12414
12415    Returns the TREE_TYPE representing the class.  */
12416
12417 static tree
12418 cp_parser_class_specifier (cp_parser* parser)
12419 {
12420   cp_token *token;
12421   tree type;
12422   tree attributes = NULL_TREE;
12423   int has_trailing_semicolon;
12424   bool nested_name_specifier_p;
12425   unsigned saved_num_template_parameter_lists;
12426   tree old_scope = NULL_TREE;
12427   tree scope = NULL_TREE;
12428
12429   push_deferring_access_checks (dk_no_deferred);
12430
12431   /* Parse the class-head.  */
12432   type = cp_parser_class_head (parser,
12433                                &nested_name_specifier_p,
12434                                &attributes);
12435   /* If the class-head was a semantic disaster, skip the entire body
12436      of the class.  */
12437   if (!type)
12438     {
12439       cp_parser_skip_to_end_of_block_or_statement (parser);
12440       pop_deferring_access_checks ();
12441       return error_mark_node;
12442     }
12443
12444   /* Look for the `{'.  */
12445   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12446     {
12447       pop_deferring_access_checks ();
12448       return error_mark_node;
12449     }
12450
12451   /* Issue an error message if type-definitions are forbidden here.  */
12452   cp_parser_check_type_definition (parser);
12453   /* Remember that we are defining one more class.  */
12454   ++parser->num_classes_being_defined;
12455   /* Inside the class, surrounding template-parameter-lists do not
12456      apply.  */
12457   saved_num_template_parameter_lists
12458     = parser->num_template_parameter_lists;
12459   parser->num_template_parameter_lists = 0;
12460
12461   /* Start the class.  */
12462   if (nested_name_specifier_p)
12463     {
12464       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12465       old_scope = push_inner_scope (scope);
12466     }
12467   type = begin_class_definition (type);
12468
12469   if (type == error_mark_node)
12470     /* If the type is erroneous, skip the entire body of the class.  */
12471     cp_parser_skip_to_closing_brace (parser);
12472   else
12473     /* Parse the member-specification.  */
12474     cp_parser_member_specification_opt (parser);
12475
12476   /* Look for the trailing `}'.  */
12477   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12478   /* We get better error messages by noticing a common problem: a
12479      missing trailing `;'.  */
12480   token = cp_lexer_peek_token (parser->lexer);
12481   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12482   /* Look for trailing attributes to apply to this class.  */
12483   if (cp_parser_allow_gnu_extensions_p (parser))
12484     {
12485       tree sub_attr = cp_parser_attributes_opt (parser);
12486       attributes = chainon (attributes, sub_attr);
12487     }
12488   if (type != error_mark_node)
12489     type = finish_struct (type, attributes);
12490   if (nested_name_specifier_p)
12491     pop_inner_scope (old_scope, scope);
12492   /* If this class is not itself within the scope of another class,
12493      then we need to parse the bodies of all of the queued function
12494      definitions.  Note that the queued functions defined in a class
12495      are not always processed immediately following the
12496      class-specifier for that class.  Consider:
12497
12498        struct A {
12499          struct B { void f() { sizeof (A); } };
12500        };
12501
12502      If `f' were processed before the processing of `A' were
12503      completed, there would be no way to compute the size of `A'.
12504      Note that the nesting we are interested in here is lexical --
12505      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12506      for:
12507
12508        struct A { struct B; };
12509        struct A::B { void f() { } };
12510
12511      there is no need to delay the parsing of `A::B::f'.  */
12512   if (--parser->num_classes_being_defined == 0)
12513     {
12514       tree queue_entry;
12515       tree fn;
12516       tree class_type = NULL_TREE;
12517       tree pushed_scope = NULL_TREE;
12518
12519       /* In a first pass, parse default arguments to the functions.
12520          Then, in a second pass, parse the bodies of the functions.
12521          This two-phased approach handles cases like:
12522
12523             struct S {
12524               void f() { g(); }
12525               void g(int i = 3);
12526             };
12527
12528          */
12529       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12530              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12531            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12532            TREE_PURPOSE (parser->unparsed_functions_queues)
12533              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12534         {
12535           fn = TREE_VALUE (queue_entry);
12536           /* If there are default arguments that have not yet been processed,
12537              take care of them now.  */
12538           if (class_type != TREE_PURPOSE (queue_entry))
12539             {
12540               if (pushed_scope)
12541                 pop_scope (pushed_scope);
12542               class_type = TREE_PURPOSE (queue_entry);
12543               pushed_scope = push_scope (class_type);
12544             }
12545           /* Make sure that any template parameters are in scope.  */
12546           maybe_begin_member_template_processing (fn);
12547           /* Parse the default argument expressions.  */
12548           cp_parser_late_parsing_default_args (parser, fn);
12549           /* Remove any template parameters from the symbol table.  */
12550           maybe_end_member_template_processing ();
12551         }
12552       if (pushed_scope)
12553         pop_scope (pushed_scope);
12554       /* Now parse the body of the functions.  */
12555       for (TREE_VALUE (parser->unparsed_functions_queues)
12556              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12557            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12558            TREE_VALUE (parser->unparsed_functions_queues)
12559              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12560         {
12561           /* Figure out which function we need to process.  */
12562           fn = TREE_VALUE (queue_entry);
12563
12564           /* A hack to prevent garbage collection.  */
12565           function_depth++;
12566
12567           /* Parse the function.  */
12568           cp_parser_late_parsing_for_member (parser, fn);
12569           function_depth--;
12570         }
12571     }
12572
12573   /* Put back any saved access checks.  */
12574   pop_deferring_access_checks ();
12575
12576   /* Restore the count of active template-parameter-lists.  */
12577   parser->num_template_parameter_lists
12578     = saved_num_template_parameter_lists;
12579
12580   return type;
12581 }
12582
12583 /* Parse a class-head.
12584
12585    class-head:
12586      class-key identifier [opt] base-clause [opt]
12587      class-key nested-name-specifier identifier base-clause [opt]
12588      class-key nested-name-specifier [opt] template-id
12589        base-clause [opt]
12590
12591    GNU Extensions:
12592      class-key attributes identifier [opt] base-clause [opt]
12593      class-key attributes nested-name-specifier identifier base-clause [opt]
12594      class-key attributes nested-name-specifier [opt] template-id
12595        base-clause [opt]
12596
12597    Returns the TYPE of the indicated class.  Sets
12598    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12599    involving a nested-name-specifier was used, and FALSE otherwise.
12600
12601    Returns error_mark_node if this is not a class-head.
12602    
12603    Returns NULL_TREE if the class-head is syntactically valid, but
12604    semantically invalid in a way that means we should skip the entire
12605    body of the class.  */
12606
12607 static tree
12608 cp_parser_class_head (cp_parser* parser,
12609                       bool* nested_name_specifier_p,
12610                       tree *attributes_p)
12611 {
12612   tree nested_name_specifier;
12613   enum tag_types class_key;
12614   tree id = NULL_TREE;
12615   tree type = NULL_TREE;
12616   tree attributes;
12617   bool template_id_p = false;
12618   bool qualified_p = false;
12619   bool invalid_nested_name_p = false;
12620   bool invalid_explicit_specialization_p = false;
12621   tree pushed_scope = NULL_TREE;
12622   unsigned num_templates;
12623   tree bases;
12624
12625   /* Assume no nested-name-specifier will be present.  */
12626   *nested_name_specifier_p = false;
12627   /* Assume no template parameter lists will be used in defining the
12628      type.  */
12629   num_templates = 0;
12630
12631   /* Look for the class-key.  */
12632   class_key = cp_parser_class_key (parser);
12633   if (class_key == none_type)
12634     return error_mark_node;
12635
12636   /* Parse the attributes.  */
12637   attributes = cp_parser_attributes_opt (parser);
12638
12639   /* If the next token is `::', that is invalid -- but sometimes
12640      people do try to write:
12641
12642        struct ::S {};
12643
12644      Handle this gracefully by accepting the extra qualifier, and then
12645      issuing an error about it later if this really is a
12646      class-head.  If it turns out just to be an elaborated type
12647      specifier, remain silent.  */
12648   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12649     qualified_p = true;
12650
12651   push_deferring_access_checks (dk_no_check);
12652
12653   /* Determine the name of the class.  Begin by looking for an
12654      optional nested-name-specifier.  */
12655   nested_name_specifier
12656     = cp_parser_nested_name_specifier_opt (parser,
12657                                            /*typename_keyword_p=*/false,
12658                                            /*check_dependency_p=*/false,
12659                                            /*type_p=*/false,
12660                                            /*is_declaration=*/false);
12661   /* If there was a nested-name-specifier, then there *must* be an
12662      identifier.  */
12663   if (nested_name_specifier)
12664     {
12665       /* Although the grammar says `identifier', it really means
12666          `class-name' or `template-name'.  You are only allowed to
12667          define a class that has already been declared with this
12668          syntax.
12669
12670          The proposed resolution for Core Issue 180 says that whever
12671          you see `class T::X' you should treat `X' as a type-name.
12672
12673          It is OK to define an inaccessible class; for example:
12674
12675            class A { class B; };
12676            class A::B {};
12677
12678          We do not know if we will see a class-name, or a
12679          template-name.  We look for a class-name first, in case the
12680          class-name is a template-id; if we looked for the
12681          template-name first we would stop after the template-name.  */
12682       cp_parser_parse_tentatively (parser);
12683       type = cp_parser_class_name (parser,
12684                                    /*typename_keyword_p=*/false,
12685                                    /*template_keyword_p=*/false,
12686                                    class_type,
12687                                    /*check_dependency_p=*/false,
12688                                    /*class_head_p=*/true,
12689                                    /*is_declaration=*/false);
12690       /* If that didn't work, ignore the nested-name-specifier.  */
12691       if (!cp_parser_parse_definitely (parser))
12692         {
12693           invalid_nested_name_p = true;
12694           id = cp_parser_identifier (parser);
12695           if (id == error_mark_node)
12696             id = NULL_TREE;
12697         }
12698       /* If we could not find a corresponding TYPE, treat this
12699          declaration like an unqualified declaration.  */
12700       if (type == error_mark_node)
12701         nested_name_specifier = NULL_TREE;
12702       /* Otherwise, count the number of templates used in TYPE and its
12703          containing scopes.  */
12704       else
12705         {
12706           tree scope;
12707
12708           for (scope = TREE_TYPE (type);
12709                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12710                scope = (TYPE_P (scope)
12711                         ? TYPE_CONTEXT (scope)
12712                         : DECL_CONTEXT (scope)))
12713             if (TYPE_P (scope)
12714                 && CLASS_TYPE_P (scope)
12715                 && CLASSTYPE_TEMPLATE_INFO (scope)
12716                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12717                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12718               ++num_templates;
12719         }
12720     }
12721   /* Otherwise, the identifier is optional.  */
12722   else
12723     {
12724       /* We don't know whether what comes next is a template-id,
12725          an identifier, or nothing at all.  */
12726       cp_parser_parse_tentatively (parser);
12727       /* Check for a template-id.  */
12728       id = cp_parser_template_id (parser,
12729                                   /*template_keyword_p=*/false,
12730                                   /*check_dependency_p=*/true,
12731                                   /*is_declaration=*/true);
12732       /* If that didn't work, it could still be an identifier.  */
12733       if (!cp_parser_parse_definitely (parser))
12734         {
12735           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12736             id = cp_parser_identifier (parser);
12737           else
12738             id = NULL_TREE;
12739         }
12740       else
12741         {
12742           template_id_p = true;
12743           ++num_templates;
12744         }
12745     }
12746
12747   pop_deferring_access_checks ();
12748
12749   if (id)
12750     cp_parser_check_for_invalid_template_id (parser, id);
12751
12752   /* If it's not a `:' or a `{' then we can't really be looking at a
12753      class-head, since a class-head only appears as part of a
12754      class-specifier.  We have to detect this situation before calling
12755      xref_tag, since that has irreversible side-effects.  */
12756   if (!cp_parser_next_token_starts_class_definition_p (parser))
12757     {
12758       cp_parser_error (parser, "expected %<{%> or %<:%>");
12759       return error_mark_node;
12760     }
12761
12762   /* At this point, we're going ahead with the class-specifier, even
12763      if some other problem occurs.  */
12764   cp_parser_commit_to_tentative_parse (parser);
12765   /* Issue the error about the overly-qualified name now.  */
12766   if (qualified_p)
12767     cp_parser_error (parser,
12768                      "global qualification of class name is invalid");
12769   else if (invalid_nested_name_p)
12770     cp_parser_error (parser,
12771                      "qualified name does not name a class");
12772   else if (nested_name_specifier)
12773     {
12774       tree scope;
12775
12776       /* Reject typedef-names in class heads.  */
12777       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12778         {
12779           error ("invalid class name in declaration of %qD", type);
12780           type = NULL_TREE;
12781           goto done;
12782         }
12783
12784       /* Figure out in what scope the declaration is being placed.  */
12785       scope = current_scope ();
12786       /* If that scope does not contain the scope in which the
12787          class was originally declared, the program is invalid.  */
12788       if (scope && !is_ancestor (scope, nested_name_specifier))
12789         {
12790           error ("declaration of %qD in %qD which does not enclose %qD",
12791                  type, scope, nested_name_specifier);
12792           type = NULL_TREE;
12793           goto done;
12794         }
12795       /* [dcl.meaning]
12796
12797          A declarator-id shall not be qualified exception of the
12798          definition of a ... nested class outside of its class
12799          ... [or] a the definition or explicit instantiation of a
12800          class member of a namespace outside of its namespace.  */
12801       if (scope == nested_name_specifier)
12802         {
12803           pedwarn ("extra qualification ignored");
12804           nested_name_specifier = NULL_TREE;
12805           num_templates = 0;
12806         }
12807     }
12808   /* An explicit-specialization must be preceded by "template <>".  If
12809      it is not, try to recover gracefully.  */
12810   if (at_namespace_scope_p ()
12811       && parser->num_template_parameter_lists == 0
12812       && template_id_p)
12813     {
12814       error ("an explicit specialization must be preceded by %<template <>%>");
12815       invalid_explicit_specialization_p = true;
12816       /* Take the same action that would have been taken by
12817          cp_parser_explicit_specialization.  */
12818       ++parser->num_template_parameter_lists;
12819       begin_specialization ();
12820     }
12821   /* There must be no "return" statements between this point and the
12822      end of this function; set "type "to the correct return value and
12823      use "goto done;" to return.  */
12824   /* Make sure that the right number of template parameters were
12825      present.  */
12826   if (!cp_parser_check_template_parameters (parser, num_templates))
12827     {
12828       /* If something went wrong, there is no point in even trying to
12829          process the class-definition.  */
12830       type = NULL_TREE;
12831       goto done;
12832     }
12833
12834   /* Look up the type.  */
12835   if (template_id_p)
12836     {
12837       type = TREE_TYPE (id);
12838       maybe_process_partial_specialization (type);
12839       if (nested_name_specifier)
12840         pushed_scope = push_scope (nested_name_specifier);
12841     }
12842   else if (nested_name_specifier)
12843     {
12844       tree class_type;
12845
12846       /* Given:
12847
12848             template <typename T> struct S { struct T };
12849             template <typename T> struct S<T>::T { };
12850
12851          we will get a TYPENAME_TYPE when processing the definition of
12852          `S::T'.  We need to resolve it to the actual type before we
12853          try to define it.  */
12854       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12855         {
12856           class_type = resolve_typename_type (TREE_TYPE (type),
12857                                               /*only_current_p=*/false);
12858           if (class_type != error_mark_node)
12859             type = TYPE_NAME (class_type);
12860           else
12861             {
12862               cp_parser_error (parser, "could not resolve typename type");
12863               type = error_mark_node;
12864             }
12865         }
12866
12867       maybe_process_partial_specialization (TREE_TYPE (type));
12868       class_type = current_class_type;
12869       /* Enter the scope indicated by the nested-name-specifier.  */
12870       pushed_scope = push_scope (nested_name_specifier);
12871       /* Get the canonical version of this type.  */
12872       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12873       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12874           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12875         {
12876           type = push_template_decl (type);
12877           if (type == error_mark_node)
12878             {
12879               type = NULL_TREE;
12880               goto done;
12881             }
12882         }
12883       
12884       type = TREE_TYPE (type);
12885       *nested_name_specifier_p = true;
12886     }
12887   else      /* The name is not a nested name.  */
12888     {
12889       /* If the class was unnamed, create a dummy name.  */
12890       if (!id)
12891         id = make_anon_name ();
12892       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12893                        parser->num_template_parameter_lists);
12894     }
12895
12896   /* Indicate whether this class was declared as a `class' or as a
12897      `struct'.  */
12898   if (TREE_CODE (type) == RECORD_TYPE)
12899     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12900   cp_parser_check_class_key (class_key, type);
12901
12902   /* If this type was already complete, and we see another definition,
12903      that's an error.  */
12904   if (type != error_mark_node && COMPLETE_TYPE_P (type))
12905     {
12906       error ("redefinition of %q#T", type);
12907       cp_error_at ("previous definition of %q#T", type);
12908       type = NULL_TREE;
12909       goto done;
12910     }
12911
12912   /* We will have entered the scope containing the class; the names of
12913      base classes should be looked up in that context.  For example:
12914
12915        struct A { struct B {}; struct C; };
12916        struct A::C : B {};
12917
12918      is valid.  */
12919   bases = NULL_TREE;
12920
12921   /* Get the list of base-classes, if there is one.  */
12922   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12923     bases = cp_parser_base_clause (parser);
12924
12925   /* Process the base classes.  */
12926   xref_basetypes (type, bases);
12927
12928  done:
12929   /* Leave the scope given by the nested-name-specifier.  We will
12930      enter the class scope itself while processing the members.  */
12931   if (pushed_scope)
12932     pop_scope (pushed_scope);
12933
12934   if (invalid_explicit_specialization_p)
12935     {
12936       end_specialization ();
12937       --parser->num_template_parameter_lists;
12938     }
12939   *attributes_p = attributes;
12940   return type;
12941 }
12942
12943 /* Parse a class-key.
12944
12945    class-key:
12946      class
12947      struct
12948      union
12949
12950    Returns the kind of class-key specified, or none_type to indicate
12951    error.  */
12952
12953 static enum tag_types
12954 cp_parser_class_key (cp_parser* parser)
12955 {
12956   cp_token *token;
12957   enum tag_types tag_type;
12958
12959   /* Look for the class-key.  */
12960   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12961   if (!token)
12962     return none_type;
12963
12964   /* Check to see if the TOKEN is a class-key.  */
12965   tag_type = cp_parser_token_is_class_key (token);
12966   if (!tag_type)
12967     cp_parser_error (parser, "expected class-key");
12968   return tag_type;
12969 }
12970
12971 /* Parse an (optional) member-specification.
12972
12973    member-specification:
12974      member-declaration member-specification [opt]
12975      access-specifier : member-specification [opt]  */
12976
12977 static void
12978 cp_parser_member_specification_opt (cp_parser* parser)
12979 {
12980   while (true)
12981     {
12982       cp_token *token;
12983       enum rid keyword;
12984
12985       /* Peek at the next token.  */
12986       token = cp_lexer_peek_token (parser->lexer);
12987       /* If it's a `}', or EOF then we've seen all the members.  */
12988       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12989         break;
12990
12991       /* See if this token is a keyword.  */
12992       keyword = token->keyword;
12993       switch (keyword)
12994         {
12995         case RID_PUBLIC:
12996         case RID_PROTECTED:
12997         case RID_PRIVATE:
12998           /* Consume the access-specifier.  */
12999           cp_lexer_consume_token (parser->lexer);
13000           /* Remember which access-specifier is active.  */
13001           current_access_specifier = token->value;
13002           /* Look for the `:'.  */
13003           cp_parser_require (parser, CPP_COLON, "`:'");
13004           break;
13005
13006         default:
13007           /* Accept #pragmas at class scope.  */
13008           if (token->type == CPP_PRAGMA)
13009             {
13010               cp_lexer_handle_pragma (parser->lexer);
13011               break;
13012             }
13013
13014           /* Otherwise, the next construction must be a
13015              member-declaration.  */
13016           cp_parser_member_declaration (parser);
13017         }
13018     }
13019 }
13020
13021 /* Parse a member-declaration.
13022
13023    member-declaration:
13024      decl-specifier-seq [opt] member-declarator-list [opt] ;
13025      function-definition ; [opt]
13026      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13027      using-declaration
13028      template-declaration
13029
13030    member-declarator-list:
13031      member-declarator
13032      member-declarator-list , member-declarator
13033
13034    member-declarator:
13035      declarator pure-specifier [opt]
13036      declarator constant-initializer [opt]
13037      identifier [opt] : constant-expression
13038
13039    GNU Extensions:
13040
13041    member-declaration:
13042      __extension__ member-declaration
13043
13044    member-declarator:
13045      declarator attributes [opt] pure-specifier [opt]
13046      declarator attributes [opt] constant-initializer [opt]
13047      identifier [opt] attributes [opt] : constant-expression  */
13048
13049 static void
13050 cp_parser_member_declaration (cp_parser* parser)
13051 {
13052   cp_decl_specifier_seq decl_specifiers;
13053   tree prefix_attributes;
13054   tree decl;
13055   int declares_class_or_enum;
13056   bool friend_p;
13057   cp_token *token;
13058   int saved_pedantic;
13059
13060   /* Check for the `__extension__' keyword.  */
13061   if (cp_parser_extension_opt (parser, &saved_pedantic))
13062     {
13063       /* Recurse.  */
13064       cp_parser_member_declaration (parser);
13065       /* Restore the old value of the PEDANTIC flag.  */
13066       pedantic = saved_pedantic;
13067
13068       return;
13069     }
13070
13071   /* Check for a template-declaration.  */
13072   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13073     {
13074       /* Parse the template-declaration.  */
13075       cp_parser_template_declaration (parser, /*member_p=*/true);
13076
13077       return;
13078     }
13079
13080   /* Check for a using-declaration.  */
13081   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13082     {
13083       /* Parse the using-declaration.  */
13084       cp_parser_using_declaration (parser);
13085
13086       return;
13087     }
13088
13089   /* Parse the decl-specifier-seq.  */
13090   cp_parser_decl_specifier_seq (parser,
13091                                 CP_PARSER_FLAGS_OPTIONAL,
13092                                 &decl_specifiers,
13093                                 &declares_class_or_enum);
13094   prefix_attributes = decl_specifiers.attributes;
13095   decl_specifiers.attributes = NULL_TREE;
13096   /* Check for an invalid type-name.  */
13097   if (!decl_specifiers.type
13098       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13099     return;
13100   /* If there is no declarator, then the decl-specifier-seq should
13101      specify a type.  */
13102   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13103     {
13104       /* If there was no decl-specifier-seq, and the next token is a
13105          `;', then we have something like:
13106
13107            struct S { ; };
13108
13109          [class.mem]
13110
13111          Each member-declaration shall declare at least one member
13112          name of the class.  */
13113       if (!decl_specifiers.any_specifiers_p)
13114         {
13115           cp_token *token = cp_lexer_peek_token (parser->lexer);
13116           if (pedantic && !token->in_system_header)
13117             pedwarn ("%Hextra %<;%>", &token->location);
13118         }
13119       else
13120         {
13121           tree type;
13122
13123           /* See if this declaration is a friend.  */
13124           friend_p = cp_parser_friend_p (&decl_specifiers);
13125           /* If there were decl-specifiers, check to see if there was
13126              a class-declaration.  */
13127           type = check_tag_decl (&decl_specifiers);
13128           /* Nested classes have already been added to the class, but
13129              a `friend' needs to be explicitly registered.  */
13130           if (friend_p)
13131             {
13132               /* If the `friend' keyword was present, the friend must
13133                  be introduced with a class-key.  */
13134                if (!declares_class_or_enum)
13135                  error ("a class-key must be used when declaring a friend");
13136                /* In this case:
13137
13138                     template <typename T> struct A {
13139                       friend struct A<T>::B;
13140                     };
13141
13142                   A<T>::B will be represented by a TYPENAME_TYPE, and
13143                   therefore not recognized by check_tag_decl.  */
13144                if (!type
13145                    && decl_specifiers.type
13146                    && TYPE_P (decl_specifiers.type))
13147                  type = decl_specifiers.type;
13148                if (!type || !TYPE_P (type))
13149                  error ("friend declaration does not name a class or "
13150                         "function");
13151                else
13152                  make_friend_class (current_class_type, type,
13153                                     /*complain=*/true);
13154             }
13155           /* If there is no TYPE, an error message will already have
13156              been issued.  */
13157           else if (!type || type == error_mark_node)
13158             ;
13159           /* An anonymous aggregate has to be handled specially; such
13160              a declaration really declares a data member (with a
13161              particular type), as opposed to a nested class.  */
13162           else if (ANON_AGGR_TYPE_P (type))
13163             {
13164               /* Remove constructors and such from TYPE, now that we
13165                  know it is an anonymous aggregate.  */
13166               fixup_anonymous_aggr (type);
13167               /* And make the corresponding data member.  */
13168               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13169               /* Add it to the class.  */
13170               finish_member_declaration (decl);
13171             }
13172           else
13173             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13174         }
13175     }
13176   else
13177     {
13178       /* See if these declarations will be friends.  */
13179       friend_p = cp_parser_friend_p (&decl_specifiers);
13180
13181       /* Keep going until we hit the `;' at the end of the
13182          declaration.  */
13183       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13184         {
13185           tree attributes = NULL_TREE;
13186           tree first_attribute;
13187
13188           /* Peek at the next token.  */
13189           token = cp_lexer_peek_token (parser->lexer);
13190
13191           /* Check for a bitfield declaration.  */
13192           if (token->type == CPP_COLON
13193               || (token->type == CPP_NAME
13194                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13195                   == CPP_COLON))
13196             {
13197               tree identifier;
13198               tree width;
13199
13200               /* Get the name of the bitfield.  Note that we cannot just
13201                  check TOKEN here because it may have been invalidated by
13202                  the call to cp_lexer_peek_nth_token above.  */
13203               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13204                 identifier = cp_parser_identifier (parser);
13205               else
13206                 identifier = NULL_TREE;
13207
13208               /* Consume the `:' token.  */
13209               cp_lexer_consume_token (parser->lexer);
13210               /* Get the width of the bitfield.  */
13211               width
13212                 = cp_parser_constant_expression (parser,
13213                                                  /*allow_non_constant=*/false,
13214                                                  NULL);
13215
13216               /* Look for attributes that apply to the bitfield.  */
13217               attributes = cp_parser_attributes_opt (parser);
13218               /* Remember which attributes are prefix attributes and
13219                  which are not.  */
13220               first_attribute = attributes;
13221               /* Combine the attributes.  */
13222               attributes = chainon (prefix_attributes, attributes);
13223
13224               /* Create the bitfield declaration.  */
13225               decl = grokbitfield (identifier
13226                                    ? make_id_declarator (NULL_TREE,
13227                                                          identifier)
13228                                    : NULL,
13229                                    &decl_specifiers,
13230                                    width);
13231               /* Apply the attributes.  */
13232               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13233             }
13234           else
13235             {
13236               cp_declarator *declarator;
13237               tree initializer;
13238               tree asm_specification;
13239               int ctor_dtor_or_conv_p;
13240
13241               /* Parse the declarator.  */
13242               declarator
13243                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13244                                         &ctor_dtor_or_conv_p,
13245                                         /*parenthesized_p=*/NULL,
13246                                         /*member_p=*/true);
13247
13248               /* If something went wrong parsing the declarator, make sure
13249                  that we at least consume some tokens.  */
13250               if (declarator == cp_error_declarator)
13251                 {
13252                   /* Skip to the end of the statement.  */
13253                   cp_parser_skip_to_end_of_statement (parser);
13254                   /* If the next token is not a semicolon, that is
13255                      probably because we just skipped over the body of
13256                      a function.  So, we consume a semicolon if
13257                      present, but do not issue an error message if it
13258                      is not present.  */
13259                   if (cp_lexer_next_token_is (parser->lexer,
13260                                               CPP_SEMICOLON))
13261                     cp_lexer_consume_token (parser->lexer);
13262                   return;
13263                 }
13264
13265               if (declares_class_or_enum & 2)
13266                 cp_parser_check_for_definition_in_return_type
13267                   (declarator, decl_specifiers.type);
13268
13269               /* Look for an asm-specification.  */
13270               asm_specification = cp_parser_asm_specification_opt (parser);
13271               /* Look for attributes that apply to the declaration.  */
13272               attributes = cp_parser_attributes_opt (parser);
13273               /* Remember which attributes are prefix attributes and
13274                  which are not.  */
13275               first_attribute = attributes;
13276               /* Combine the attributes.  */
13277               attributes = chainon (prefix_attributes, attributes);
13278
13279               /* If it's an `=', then we have a constant-initializer or a
13280                  pure-specifier.  It is not correct to parse the
13281                  initializer before registering the member declaration
13282                  since the member declaration should be in scope while
13283                  its initializer is processed.  However, the rest of the
13284                  front end does not yet provide an interface that allows
13285                  us to handle this correctly.  */
13286               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13287                 {
13288                   /* In [class.mem]:
13289
13290                      A pure-specifier shall be used only in the declaration of
13291                      a virtual function.
13292
13293                      A member-declarator can contain a constant-initializer
13294                      only if it declares a static member of integral or
13295                      enumeration type.
13296
13297                      Therefore, if the DECLARATOR is for a function, we look
13298                      for a pure-specifier; otherwise, we look for a
13299                      constant-initializer.  When we call `grokfield', it will
13300                      perform more stringent semantics checks.  */
13301                   if (declarator->kind == cdk_function)
13302                     initializer = cp_parser_pure_specifier (parser);
13303                   else
13304                     /* Parse the initializer.  */
13305                     initializer = cp_parser_constant_initializer (parser);
13306                 }
13307               /* Otherwise, there is no initializer.  */
13308               else
13309                 initializer = NULL_TREE;
13310
13311               /* See if we are probably looking at a function
13312                  definition.  We are certainly not looking at a
13313                  member-declarator.  Calling `grokfield' has
13314                  side-effects, so we must not do it unless we are sure
13315                  that we are looking at a member-declarator.  */
13316               if (cp_parser_token_starts_function_definition_p
13317                   (cp_lexer_peek_token (parser->lexer)))
13318                 {
13319                   /* The grammar does not allow a pure-specifier to be
13320                      used when a member function is defined.  (It is
13321                      possible that this fact is an oversight in the
13322                      standard, since a pure function may be defined
13323                      outside of the class-specifier.  */
13324                   if (initializer)
13325                     error ("pure-specifier on function-definition");
13326                   decl = cp_parser_save_member_function_body (parser,
13327                                                               &decl_specifiers,
13328                                                               declarator,
13329                                                               attributes);
13330                   /* If the member was not a friend, declare it here.  */
13331                   if (!friend_p)
13332                     finish_member_declaration (decl);
13333                   /* Peek at the next token.  */
13334                   token = cp_lexer_peek_token (parser->lexer);
13335                   /* If the next token is a semicolon, consume it.  */
13336                   if (token->type == CPP_SEMICOLON)
13337                     cp_lexer_consume_token (parser->lexer);
13338                   return;
13339                 }
13340               else
13341                 {
13342                   /* Create the declaration.  */
13343                   decl = grokfield (declarator, &decl_specifiers,
13344                                     initializer, asm_specification,
13345                                     attributes);
13346                   /* Any initialization must have been from a
13347                      constant-expression.  */
13348                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13349                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13350                 }
13351             }
13352
13353           /* Reset PREFIX_ATTRIBUTES.  */
13354           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13355             attributes = TREE_CHAIN (attributes);
13356           if (attributes)
13357             TREE_CHAIN (attributes) = NULL_TREE;
13358
13359           /* If there is any qualification still in effect, clear it
13360              now; we will be starting fresh with the next declarator.  */
13361           parser->scope = NULL_TREE;
13362           parser->qualifying_scope = NULL_TREE;
13363           parser->object_scope = NULL_TREE;
13364           /* If it's a `,', then there are more declarators.  */
13365           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13366             cp_lexer_consume_token (parser->lexer);
13367           /* If the next token isn't a `;', then we have a parse error.  */
13368           else if (cp_lexer_next_token_is_not (parser->lexer,
13369                                                CPP_SEMICOLON))
13370             {
13371               cp_parser_error (parser, "expected %<;%>");
13372               /* Skip tokens until we find a `;'.  */
13373               cp_parser_skip_to_end_of_statement (parser);
13374
13375               break;
13376             }
13377
13378           if (decl)
13379             {
13380               /* Add DECL to the list of members.  */
13381               if (!friend_p)
13382                 finish_member_declaration (decl);
13383
13384               if (TREE_CODE (decl) == FUNCTION_DECL)
13385                 cp_parser_save_default_args (parser, decl);
13386             }
13387         }
13388     }
13389
13390   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13391 }
13392
13393 /* Parse a pure-specifier.
13394
13395    pure-specifier:
13396      = 0
13397
13398    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13399    Otherwise, ERROR_MARK_NODE is returned.  */
13400
13401 static tree
13402 cp_parser_pure_specifier (cp_parser* parser)
13403 {
13404   cp_token *token;
13405
13406   /* Look for the `=' token.  */
13407   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13408     return error_mark_node;
13409   /* Look for the `0' token.  */
13410   token = cp_lexer_consume_token (parser->lexer);
13411   if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13412     {
13413       cp_parser_error (parser,
13414                        "invalid pure specifier (only `= 0' is allowed)");
13415       cp_parser_skip_to_end_of_statement (parser);
13416       return error_mark_node;
13417     }
13418
13419   /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13420      We need to get information from the lexer about how the number
13421      was spelled in order to fix this problem.  */
13422   return integer_zero_node;
13423 }
13424
13425 /* Parse a constant-initializer.
13426
13427    constant-initializer:
13428      = constant-expression
13429
13430    Returns a representation of the constant-expression.  */
13431
13432 static tree
13433 cp_parser_constant_initializer (cp_parser* parser)
13434 {
13435   /* Look for the `=' token.  */
13436   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13437     return error_mark_node;
13438
13439   /* It is invalid to write:
13440
13441        struct S { static const int i = { 7 }; };
13442
13443      */
13444   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13445     {
13446       cp_parser_error (parser,
13447                        "a brace-enclosed initializer is not allowed here");
13448       /* Consume the opening brace.  */
13449       cp_lexer_consume_token (parser->lexer);
13450       /* Skip the initializer.  */
13451       cp_parser_skip_to_closing_brace (parser);
13452       /* Look for the trailing `}'.  */
13453       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13454
13455       return error_mark_node;
13456     }
13457
13458   return cp_parser_constant_expression (parser,
13459                                         /*allow_non_constant=*/false,
13460                                         NULL);
13461 }
13462
13463 /* Derived classes [gram.class.derived] */
13464
13465 /* Parse a base-clause.
13466
13467    base-clause:
13468      : base-specifier-list
13469
13470    base-specifier-list:
13471      base-specifier
13472      base-specifier-list , base-specifier
13473
13474    Returns a TREE_LIST representing the base-classes, in the order in
13475    which they were declared.  The representation of each node is as
13476    described by cp_parser_base_specifier.
13477
13478    In the case that no bases are specified, this function will return
13479    NULL_TREE, not ERROR_MARK_NODE.  */
13480
13481 static tree
13482 cp_parser_base_clause (cp_parser* parser)
13483 {
13484   tree bases = NULL_TREE;
13485
13486   /* Look for the `:' that begins the list.  */
13487   cp_parser_require (parser, CPP_COLON, "`:'");
13488
13489   /* Scan the base-specifier-list.  */
13490   while (true)
13491     {
13492       cp_token *token;
13493       tree base;
13494
13495       /* Look for the base-specifier.  */
13496       base = cp_parser_base_specifier (parser);
13497       /* Add BASE to the front of the list.  */
13498       if (base != error_mark_node)
13499         {
13500           TREE_CHAIN (base) = bases;
13501           bases = base;
13502         }
13503       /* Peek at the next token.  */
13504       token = cp_lexer_peek_token (parser->lexer);
13505       /* If it's not a comma, then the list is complete.  */
13506       if (token->type != CPP_COMMA)
13507         break;
13508       /* Consume the `,'.  */
13509       cp_lexer_consume_token (parser->lexer);
13510     }
13511
13512   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13513      base class had a qualified name.  However, the next name that
13514      appears is certainly not qualified.  */
13515   parser->scope = NULL_TREE;
13516   parser->qualifying_scope = NULL_TREE;
13517   parser->object_scope = NULL_TREE;
13518
13519   return nreverse (bases);
13520 }
13521
13522 /* Parse a base-specifier.
13523
13524    base-specifier:
13525      :: [opt] nested-name-specifier [opt] class-name
13526      virtual access-specifier [opt] :: [opt] nested-name-specifier
13527        [opt] class-name
13528      access-specifier virtual [opt] :: [opt] nested-name-specifier
13529        [opt] class-name
13530
13531    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13532    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13533    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13534    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13535
13536 static tree
13537 cp_parser_base_specifier (cp_parser* parser)
13538 {
13539   cp_token *token;
13540   bool done = false;
13541   bool virtual_p = false;
13542   bool duplicate_virtual_error_issued_p = false;
13543   bool duplicate_access_error_issued_p = false;
13544   bool class_scope_p, template_p;
13545   tree access = access_default_node;
13546   tree type;
13547
13548   /* Process the optional `virtual' and `access-specifier'.  */
13549   while (!done)
13550     {
13551       /* Peek at the next token.  */
13552       token = cp_lexer_peek_token (parser->lexer);
13553       /* Process `virtual'.  */
13554       switch (token->keyword)
13555         {
13556         case RID_VIRTUAL:
13557           /* If `virtual' appears more than once, issue an error.  */
13558           if (virtual_p && !duplicate_virtual_error_issued_p)
13559             {
13560               cp_parser_error (parser,
13561                                "%<virtual%> specified more than once in base-specified");
13562               duplicate_virtual_error_issued_p = true;
13563             }
13564
13565           virtual_p = true;
13566
13567           /* Consume the `virtual' token.  */
13568           cp_lexer_consume_token (parser->lexer);
13569
13570           break;
13571
13572         case RID_PUBLIC:
13573         case RID_PROTECTED:
13574         case RID_PRIVATE:
13575           /* If more than one access specifier appears, issue an
13576              error.  */
13577           if (access != access_default_node
13578               && !duplicate_access_error_issued_p)
13579             {
13580               cp_parser_error (parser,
13581                                "more than one access specifier in base-specified");
13582               duplicate_access_error_issued_p = true;
13583             }
13584
13585           access = ridpointers[(int) token->keyword];
13586
13587           /* Consume the access-specifier.  */
13588           cp_lexer_consume_token (parser->lexer);
13589
13590           break;
13591
13592         default:
13593           done = true;
13594           break;
13595         }
13596     }
13597   /* It is not uncommon to see programs mechanically, erroneously, use
13598      the 'typename' keyword to denote (dependent) qualified types
13599      as base classes.  */
13600   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13601     {
13602       if (!processing_template_decl)
13603         error ("keyword %<typename%> not allowed outside of templates");
13604       else
13605         error ("keyword %<typename%> not allowed in this context "
13606                "(the base class is implicitly a type)");
13607       cp_lexer_consume_token (parser->lexer);
13608     }
13609
13610   /* Look for the optional `::' operator.  */
13611   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13612   /* Look for the nested-name-specifier.  The simplest way to
13613      implement:
13614
13615        [temp.res]
13616
13617        The keyword `typename' is not permitted in a base-specifier or
13618        mem-initializer; in these contexts a qualified name that
13619        depends on a template-parameter is implicitly assumed to be a
13620        type name.
13621
13622      is to pretend that we have seen the `typename' keyword at this
13623      point.  */
13624   cp_parser_nested_name_specifier_opt (parser,
13625                                        /*typename_keyword_p=*/true,
13626                                        /*check_dependency_p=*/true,
13627                                        typename_type,
13628                                        /*is_declaration=*/true);
13629   /* If the base class is given by a qualified name, assume that names
13630      we see are type names or templates, as appropriate.  */
13631   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13632   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13633
13634   /* Finally, look for the class-name.  */
13635   type = cp_parser_class_name (parser,
13636                                class_scope_p,
13637                                template_p,
13638                                typename_type,
13639                                /*check_dependency_p=*/true,
13640                                /*class_head_p=*/false,
13641                                /*is_declaration=*/true);
13642
13643   if (type == error_mark_node)
13644     return error_mark_node;
13645
13646   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13647 }
13648
13649 /* Exception handling [gram.exception] */
13650
13651 /* Parse an (optional) exception-specification.
13652
13653    exception-specification:
13654      throw ( type-id-list [opt] )
13655
13656    Returns a TREE_LIST representing the exception-specification.  The
13657    TREE_VALUE of each node is a type.  */
13658
13659 static tree
13660 cp_parser_exception_specification_opt (cp_parser* parser)
13661 {
13662   cp_token *token;
13663   tree type_id_list;
13664
13665   /* Peek at the next token.  */
13666   token = cp_lexer_peek_token (parser->lexer);
13667   /* If it's not `throw', then there's no exception-specification.  */
13668   if (!cp_parser_is_keyword (token, RID_THROW))
13669     return NULL_TREE;
13670
13671   /* Consume the `throw'.  */
13672   cp_lexer_consume_token (parser->lexer);
13673
13674   /* Look for the `('.  */
13675   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13676
13677   /* Peek at the next token.  */
13678   token = cp_lexer_peek_token (parser->lexer);
13679   /* If it's not a `)', then there is a type-id-list.  */
13680   if (token->type != CPP_CLOSE_PAREN)
13681     {
13682       const char *saved_message;
13683
13684       /* Types may not be defined in an exception-specification.  */
13685       saved_message = parser->type_definition_forbidden_message;
13686       parser->type_definition_forbidden_message
13687         = "types may not be defined in an exception-specification";
13688       /* Parse the type-id-list.  */
13689       type_id_list = cp_parser_type_id_list (parser);
13690       /* Restore the saved message.  */
13691       parser->type_definition_forbidden_message = saved_message;
13692     }
13693   else
13694     type_id_list = empty_except_spec;
13695
13696   /* Look for the `)'.  */
13697   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13698
13699   return type_id_list;
13700 }
13701
13702 /* Parse an (optional) type-id-list.
13703
13704    type-id-list:
13705      type-id
13706      type-id-list , type-id
13707
13708    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13709    in the order that the types were presented.  */
13710
13711 static tree
13712 cp_parser_type_id_list (cp_parser* parser)
13713 {
13714   tree types = NULL_TREE;
13715
13716   while (true)
13717     {
13718       cp_token *token;
13719       tree type;
13720
13721       /* Get the next type-id.  */
13722       type = cp_parser_type_id (parser);
13723       /* Add it to the list.  */
13724       types = add_exception_specifier (types, type, /*complain=*/1);
13725       /* Peek at the next token.  */
13726       token = cp_lexer_peek_token (parser->lexer);
13727       /* If it is not a `,', we are done.  */
13728       if (token->type != CPP_COMMA)
13729         break;
13730       /* Consume the `,'.  */
13731       cp_lexer_consume_token (parser->lexer);
13732     }
13733
13734   return nreverse (types);
13735 }
13736
13737 /* Parse a try-block.
13738
13739    try-block:
13740      try compound-statement handler-seq  */
13741
13742 static tree
13743 cp_parser_try_block (cp_parser* parser)
13744 {
13745   tree try_block;
13746
13747   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13748   try_block = begin_try_block ();
13749   cp_parser_compound_statement (parser, NULL, true);
13750   finish_try_block (try_block);
13751   cp_parser_handler_seq (parser);
13752   finish_handler_sequence (try_block);
13753
13754   return try_block;
13755 }
13756
13757 /* Parse a function-try-block.
13758
13759    function-try-block:
13760      try ctor-initializer [opt] function-body handler-seq  */
13761
13762 static bool
13763 cp_parser_function_try_block (cp_parser* parser)
13764 {
13765   tree try_block;
13766   bool ctor_initializer_p;
13767
13768   /* Look for the `try' keyword.  */
13769   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13770     return false;
13771   /* Let the rest of the front-end know where we are.  */
13772   try_block = begin_function_try_block ();
13773   /* Parse the function-body.  */
13774   ctor_initializer_p
13775     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13776   /* We're done with the `try' part.  */
13777   finish_function_try_block (try_block);
13778   /* Parse the handlers.  */
13779   cp_parser_handler_seq (parser);
13780   /* We're done with the handlers.  */
13781   finish_function_handler_sequence (try_block);
13782
13783   return ctor_initializer_p;
13784 }
13785
13786 /* Parse a handler-seq.
13787
13788    handler-seq:
13789      handler handler-seq [opt]  */
13790
13791 static void
13792 cp_parser_handler_seq (cp_parser* parser)
13793 {
13794   while (true)
13795     {
13796       cp_token *token;
13797
13798       /* Parse the handler.  */
13799       cp_parser_handler (parser);
13800       /* Peek at the next token.  */
13801       token = cp_lexer_peek_token (parser->lexer);
13802       /* If it's not `catch' then there are no more handlers.  */
13803       if (!cp_parser_is_keyword (token, RID_CATCH))
13804         break;
13805     }
13806 }
13807
13808 /* Parse a handler.
13809
13810    handler:
13811      catch ( exception-declaration ) compound-statement  */
13812
13813 static void
13814 cp_parser_handler (cp_parser* parser)
13815 {
13816   tree handler;
13817   tree declaration;
13818
13819   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13820   handler = begin_handler ();
13821   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13822   declaration = cp_parser_exception_declaration (parser);
13823   finish_handler_parms (declaration, handler);
13824   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13825   cp_parser_compound_statement (parser, NULL, false);
13826   finish_handler (handler);
13827 }
13828
13829 /* Parse an exception-declaration.
13830
13831    exception-declaration:
13832      type-specifier-seq declarator
13833      type-specifier-seq abstract-declarator
13834      type-specifier-seq
13835      ...
13836
13837    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13838    ellipsis variant is used.  */
13839
13840 static tree
13841 cp_parser_exception_declaration (cp_parser* parser)
13842 {
13843   tree decl;
13844   cp_decl_specifier_seq type_specifiers;
13845   cp_declarator *declarator;
13846   const char *saved_message;
13847
13848   /* If it's an ellipsis, it's easy to handle.  */
13849   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13850     {
13851       /* Consume the `...' token.  */
13852       cp_lexer_consume_token (parser->lexer);
13853       return NULL_TREE;
13854     }
13855
13856   /* Types may not be defined in exception-declarations.  */
13857   saved_message = parser->type_definition_forbidden_message;
13858   parser->type_definition_forbidden_message
13859     = "types may not be defined in exception-declarations";
13860
13861   /* Parse the type-specifier-seq.  */
13862   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13863                                 &type_specifiers);
13864   /* If it's a `)', then there is no declarator.  */
13865   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13866     declarator = NULL;
13867   else
13868     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13869                                        /*ctor_dtor_or_conv_p=*/NULL,
13870                                        /*parenthesized_p=*/NULL,
13871                                        /*member_p=*/false);
13872
13873   /* Restore the saved message.  */
13874   parser->type_definition_forbidden_message = saved_message;
13875
13876   if (type_specifiers.any_specifiers_p)
13877     {
13878       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13879       if (decl == NULL_TREE)
13880         error ("invalid catch parameter");
13881     }
13882   else
13883     decl = NULL_TREE;
13884
13885   return decl;
13886 }
13887
13888 /* Parse a throw-expression.
13889
13890    throw-expression:
13891      throw assignment-expression [opt]
13892
13893    Returns a THROW_EXPR representing the throw-expression.  */
13894
13895 static tree
13896 cp_parser_throw_expression (cp_parser* parser)
13897 {
13898   tree expression;
13899   cp_token* token;
13900
13901   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13902   token = cp_lexer_peek_token (parser->lexer);
13903   /* Figure out whether or not there is an assignment-expression
13904      following the "throw" keyword.  */
13905   if (token->type == CPP_COMMA
13906       || token->type == CPP_SEMICOLON
13907       || token->type == CPP_CLOSE_PAREN
13908       || token->type == CPP_CLOSE_SQUARE
13909       || token->type == CPP_CLOSE_BRACE
13910       || token->type == CPP_COLON)
13911     expression = NULL_TREE;
13912   else
13913     expression = cp_parser_assignment_expression (parser,
13914                                                   /*cast_p=*/false);
13915
13916   return build_throw (expression);
13917 }
13918
13919 /* GNU Extensions */
13920
13921 /* Parse an (optional) asm-specification.
13922
13923    asm-specification:
13924      asm ( string-literal )
13925
13926    If the asm-specification is present, returns a STRING_CST
13927    corresponding to the string-literal.  Otherwise, returns
13928    NULL_TREE.  */
13929
13930 static tree
13931 cp_parser_asm_specification_opt (cp_parser* parser)
13932 {
13933   cp_token *token;
13934   tree asm_specification;
13935
13936   /* Peek at the next token.  */
13937   token = cp_lexer_peek_token (parser->lexer);
13938   /* If the next token isn't the `asm' keyword, then there's no
13939      asm-specification.  */
13940   if (!cp_parser_is_keyword (token, RID_ASM))
13941     return NULL_TREE;
13942
13943   /* Consume the `asm' token.  */
13944   cp_lexer_consume_token (parser->lexer);
13945   /* Look for the `('.  */
13946   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13947
13948   /* Look for the string-literal.  */
13949   asm_specification = cp_parser_string_literal (parser, false, false);
13950
13951   /* Look for the `)'.  */
13952   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13953
13954   return asm_specification;
13955 }
13956
13957 /* Parse an asm-operand-list.
13958
13959    asm-operand-list:
13960      asm-operand
13961      asm-operand-list , asm-operand
13962
13963    asm-operand:
13964      string-literal ( expression )
13965      [ string-literal ] string-literal ( expression )
13966
13967    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13968    each node is the expression.  The TREE_PURPOSE is itself a
13969    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13970    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13971    is a STRING_CST for the string literal before the parenthesis.  */
13972
13973 static tree
13974 cp_parser_asm_operand_list (cp_parser* parser)
13975 {
13976   tree asm_operands = NULL_TREE;
13977
13978   while (true)
13979     {
13980       tree string_literal;
13981       tree expression;
13982       tree name;
13983
13984       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13985         {
13986           /* Consume the `[' token.  */
13987           cp_lexer_consume_token (parser->lexer);
13988           /* Read the operand name.  */
13989           name = cp_parser_identifier (parser);
13990           if (name != error_mark_node)
13991             name = build_string (IDENTIFIER_LENGTH (name),
13992                                  IDENTIFIER_POINTER (name));
13993           /* Look for the closing `]'.  */
13994           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13995         }
13996       else
13997         name = NULL_TREE;
13998       /* Look for the string-literal.  */
13999       string_literal = cp_parser_string_literal (parser, false, false);
14000
14001       /* Look for the `('.  */
14002       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14003       /* Parse the expression.  */
14004       expression = cp_parser_expression (parser, /*cast_p=*/false);
14005       /* Look for the `)'.  */
14006       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14007
14008       /* Add this operand to the list.  */
14009       asm_operands = tree_cons (build_tree_list (name, string_literal),
14010                                 expression,
14011                                 asm_operands);
14012       /* If the next token is not a `,', there are no more
14013          operands.  */
14014       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14015         break;
14016       /* Consume the `,'.  */
14017       cp_lexer_consume_token (parser->lexer);
14018     }
14019
14020   return nreverse (asm_operands);
14021 }
14022
14023 /* Parse an asm-clobber-list.
14024
14025    asm-clobber-list:
14026      string-literal
14027      asm-clobber-list , string-literal
14028
14029    Returns a TREE_LIST, indicating the clobbers in the order that they
14030    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14031
14032 static tree
14033 cp_parser_asm_clobber_list (cp_parser* parser)
14034 {
14035   tree clobbers = NULL_TREE;
14036
14037   while (true)
14038     {
14039       tree string_literal;
14040
14041       /* Look for the string literal.  */
14042       string_literal = cp_parser_string_literal (parser, false, false);
14043       /* Add it to the list.  */
14044       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14045       /* If the next token is not a `,', then the list is
14046          complete.  */
14047       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14048         break;
14049       /* Consume the `,' token.  */
14050       cp_lexer_consume_token (parser->lexer);
14051     }
14052
14053   return clobbers;
14054 }
14055
14056 /* Parse an (optional) series of attributes.
14057
14058    attributes:
14059      attributes attribute
14060
14061    attribute:
14062      __attribute__ (( attribute-list [opt] ))
14063
14064    The return value is as for cp_parser_attribute_list.  */
14065
14066 static tree
14067 cp_parser_attributes_opt (cp_parser* parser)
14068 {
14069   tree attributes = NULL_TREE;
14070
14071   while (true)
14072     {
14073       cp_token *token;
14074       tree attribute_list;
14075
14076       /* Peek at the next token.  */
14077       token = cp_lexer_peek_token (parser->lexer);
14078       /* If it's not `__attribute__', then we're done.  */
14079       if (token->keyword != RID_ATTRIBUTE)
14080         break;
14081
14082       /* Consume the `__attribute__' keyword.  */
14083       cp_lexer_consume_token (parser->lexer);
14084       /* Look for the two `(' tokens.  */
14085       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14086       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14087
14088       /* Peek at the next token.  */
14089       token = cp_lexer_peek_token (parser->lexer);
14090       if (token->type != CPP_CLOSE_PAREN)
14091         /* Parse the attribute-list.  */
14092         attribute_list = cp_parser_attribute_list (parser);
14093       else
14094         /* If the next token is a `)', then there is no attribute
14095            list.  */
14096         attribute_list = NULL;
14097
14098       /* Look for the two `)' tokens.  */
14099       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14100       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14101
14102       /* Add these new attributes to the list.  */
14103       attributes = chainon (attributes, attribute_list);
14104     }
14105
14106   return attributes;
14107 }
14108
14109 /* Parse an attribute-list.
14110
14111    attribute-list:
14112      attribute
14113      attribute-list , attribute
14114
14115    attribute:
14116      identifier
14117      identifier ( identifier )
14118      identifier ( identifier , expression-list )
14119      identifier ( expression-list )
14120
14121    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14122    to an attribute.  The TREE_PURPOSE of each node is the identifier
14123    indicating which attribute is in use.  The TREE_VALUE represents
14124    the arguments, if any.  */
14125
14126 static tree
14127 cp_parser_attribute_list (cp_parser* parser)
14128 {
14129   tree attribute_list = NULL_TREE;
14130   bool save_translate_strings_p = parser->translate_strings_p;
14131
14132   parser->translate_strings_p = false;
14133   while (true)
14134     {
14135       cp_token *token;
14136       tree identifier;
14137       tree attribute;
14138
14139       /* Look for the identifier.  We also allow keywords here; for
14140          example `__attribute__ ((const))' is legal.  */
14141       token = cp_lexer_peek_token (parser->lexer);
14142       if (token->type == CPP_NAME
14143           || token->type == CPP_KEYWORD)
14144         {
14145           /* Consume the token.  */
14146           token = cp_lexer_consume_token (parser->lexer);
14147
14148           /* Save away the identifier that indicates which attribute
14149              this is.  */ 
14150           identifier = token->value;
14151           attribute = build_tree_list (identifier, NULL_TREE);
14152
14153           /* Peek at the next token.  */
14154           token = cp_lexer_peek_token (parser->lexer);
14155           /* If it's an `(', then parse the attribute arguments.  */
14156           if (token->type == CPP_OPEN_PAREN)
14157             {
14158               tree arguments;
14159
14160               arguments = (cp_parser_parenthesized_expression_list
14161                            (parser, true, /*cast_p=*/false, 
14162                             /*non_constant_p=*/NULL));
14163               /* Save the identifier and arguments away.  */
14164               TREE_VALUE (attribute) = arguments;
14165             }
14166
14167           /* Add this attribute to the list.  */
14168           TREE_CHAIN (attribute) = attribute_list;
14169           attribute_list = attribute;
14170
14171           token = cp_lexer_peek_token (parser->lexer);
14172         }
14173       /* Now, look for more attributes.  If the next token isn't a
14174          `,', we're done.  */
14175       if (token->type != CPP_COMMA)
14176         break;
14177
14178       /* Consume the comma and keep going.  */
14179       cp_lexer_consume_token (parser->lexer);
14180     }
14181   parser->translate_strings_p = save_translate_strings_p;
14182
14183   /* We built up the list in reverse order.  */
14184   return nreverse (attribute_list);
14185 }
14186
14187 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14188    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14189    current value of the PEDANTIC flag, regardless of whether or not
14190    the `__extension__' keyword is present.  The caller is responsible
14191    for restoring the value of the PEDANTIC flag.  */
14192
14193 static bool
14194 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14195 {
14196   /* Save the old value of the PEDANTIC flag.  */
14197   *saved_pedantic = pedantic;
14198
14199   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14200     {
14201       /* Consume the `__extension__' token.  */
14202       cp_lexer_consume_token (parser->lexer);
14203       /* We're not being pedantic while the `__extension__' keyword is
14204          in effect.  */
14205       pedantic = 0;
14206
14207       return true;
14208     }
14209
14210   return false;
14211 }
14212
14213 /* Parse a label declaration.
14214
14215    label-declaration:
14216      __label__ label-declarator-seq ;
14217
14218    label-declarator-seq:
14219      identifier , label-declarator-seq
14220      identifier  */
14221
14222 static void
14223 cp_parser_label_declaration (cp_parser* parser)
14224 {
14225   /* Look for the `__label__' keyword.  */
14226   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14227
14228   while (true)
14229     {
14230       tree identifier;
14231
14232       /* Look for an identifier.  */
14233       identifier = cp_parser_identifier (parser);
14234       /* Declare it as a lobel.  */
14235       finish_label_decl (identifier);
14236       /* If the next token is a `;', stop.  */
14237       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14238         break;
14239       /* Look for the `,' separating the label declarations.  */
14240       cp_parser_require (parser, CPP_COMMA, "`,'");
14241     }
14242
14243   /* Look for the final `;'.  */
14244   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14245 }
14246
14247 /* Support Functions */
14248
14249 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14250    NAME should have one of the representations used for an
14251    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14252    is returned.  If PARSER->SCOPE is a dependent type, then a
14253    SCOPE_REF is returned.
14254
14255    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14256    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14257    was formed.  Abstractly, such entities should not be passed to this
14258    function, because they do not need to be looked up, but it is
14259    simpler to check for this special case here, rather than at the
14260    call-sites.
14261
14262    In cases not explicitly covered above, this function returns a
14263    DECL, OVERLOAD, or baselink representing the result of the lookup.
14264    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14265    is returned.
14266
14267    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14268    (e.g., "struct") that was used.  In that case bindings that do not
14269    refer to types are ignored.
14270
14271    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14272    ignored.
14273
14274    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14275    are ignored.
14276
14277    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14278    types.  
14279
14280    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14281    results in an ambiguity, and false otherwise.  */
14282
14283 static tree
14284 cp_parser_lookup_name (cp_parser *parser, tree name,
14285                        enum tag_types tag_type,
14286                        bool is_template, bool is_namespace,
14287                        bool check_dependency,
14288                        bool *ambiguous_p)
14289 {
14290   tree decl;
14291   tree object_type = parser->context->object_type;
14292
14293   /* Assume that the lookup will be unambiguous.  */
14294   if (ambiguous_p)
14295     *ambiguous_p = false;
14296
14297   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14298      no longer valid.  Note that if we are parsing tentatively, and
14299      the parse fails, OBJECT_TYPE will be automatically restored.  */
14300   parser->context->object_type = NULL_TREE;
14301
14302   if (name == error_mark_node)
14303     return error_mark_node;
14304
14305   /* A template-id has already been resolved; there is no lookup to
14306      do.  */
14307   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14308     return name;
14309   if (BASELINK_P (name))
14310     {
14311       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14312                   == TEMPLATE_ID_EXPR);
14313       return name;
14314     }
14315
14316   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14317      it should already have been checked to make sure that the name
14318      used matches the type being destroyed.  */
14319   if (TREE_CODE (name) == BIT_NOT_EXPR)
14320     {
14321       tree type;
14322
14323       /* Figure out to which type this destructor applies.  */
14324       if (parser->scope)
14325         type = parser->scope;
14326       else if (object_type)
14327         type = object_type;
14328       else
14329         type = current_class_type;
14330       /* If that's not a class type, there is no destructor.  */
14331       if (!type || !CLASS_TYPE_P (type))
14332         return error_mark_node;
14333       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14334         lazily_declare_fn (sfk_destructor, type);
14335       if (!CLASSTYPE_DESTRUCTORS (type))
14336           return error_mark_node;
14337       /* If it was a class type, return the destructor.  */
14338       return CLASSTYPE_DESTRUCTORS (type);
14339     }
14340
14341   /* By this point, the NAME should be an ordinary identifier.  If
14342      the id-expression was a qualified name, the qualifying scope is
14343      stored in PARSER->SCOPE at this point.  */
14344   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14345
14346   /* Perform the lookup.  */
14347   if (parser->scope)
14348     {
14349       bool dependent_p;
14350
14351       if (parser->scope == error_mark_node)
14352         return error_mark_node;
14353
14354       /* If the SCOPE is dependent, the lookup must be deferred until
14355          the template is instantiated -- unless we are explicitly
14356          looking up names in uninstantiated templates.  Even then, we
14357          cannot look up the name if the scope is not a class type; it
14358          might, for example, be a template type parameter.  */
14359       dependent_p = (TYPE_P (parser->scope)
14360                      && !(parser->in_declarator_p
14361                           && currently_open_class (parser->scope))
14362                      && dependent_type_p (parser->scope));
14363       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14364            && dependent_p)
14365         {
14366           if (tag_type)
14367             {
14368               tree type;
14369
14370               /* The resolution to Core Issue 180 says that `struct
14371                  A::B' should be considered a type-name, even if `A'
14372                  is dependent.  */
14373               type = make_typename_type (parser->scope, name, tag_type,
14374                                          /*complain=*/1);
14375               decl = TYPE_NAME (type);
14376             }
14377           else if (is_template)
14378             decl = make_unbound_class_template (parser->scope,
14379                                                 name, NULL_TREE,
14380                                                 /*complain=*/1);
14381           else
14382             decl = build_nt (SCOPE_REF, parser->scope, name);
14383         }
14384       else
14385         {
14386           tree pushed_scope = NULL_TREE;
14387
14388           /* If PARSER->SCOPE is a dependent type, then it must be a
14389              class type, and we must not be checking dependencies;
14390              otherwise, we would have processed this lookup above.  So
14391              that PARSER->SCOPE is not considered a dependent base by
14392              lookup_member, we must enter the scope here.  */
14393           if (dependent_p)
14394             pushed_scope = push_scope (parser->scope);
14395           /* If the PARSER->SCOPE is a template specialization, it
14396              may be instantiated during name lookup.  In that case,
14397              errors may be issued.  Even if we rollback the current
14398              tentative parse, those errors are valid.  */
14399           decl = lookup_qualified_name (parser->scope, name, 
14400                                         tag_type != none_type, 
14401                                         /*complain=*/true);
14402           if (pushed_scope)
14403             pop_scope (pushed_scope);
14404         }
14405       parser->qualifying_scope = parser->scope;
14406       parser->object_scope = NULL_TREE;
14407     }
14408   else if (object_type)
14409     {
14410       tree object_decl = NULL_TREE;
14411       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14412          OBJECT_TYPE is not a class.  */
14413       if (CLASS_TYPE_P (object_type))
14414         /* If the OBJECT_TYPE is a template specialization, it may
14415            be instantiated during name lookup.  In that case, errors
14416            may be issued.  Even if we rollback the current tentative
14417            parse, those errors are valid.  */
14418         object_decl = lookup_member (object_type,
14419                                      name,
14420                                      /*protect=*/0, 
14421                                      tag_type != none_type);
14422       /* Look it up in the enclosing context, too.  */
14423       decl = lookup_name_real (name, tag_type != none_type, 
14424                                /*nonclass=*/0,
14425                                /*block_p=*/true, is_namespace,
14426                                /*flags=*/0);
14427       parser->object_scope = object_type;
14428       parser->qualifying_scope = NULL_TREE;
14429       if (object_decl)
14430         decl = object_decl;
14431     }
14432   else
14433     {
14434       decl = lookup_name_real (name, tag_type != none_type, 
14435                                /*nonclass=*/0,
14436                                /*block_p=*/true, is_namespace,
14437                                /*flags=*/0);
14438       parser->qualifying_scope = NULL_TREE;
14439       parser->object_scope = NULL_TREE;
14440     }
14441
14442   /* If the lookup failed, let our caller know.  */
14443   if (!decl
14444       || decl == error_mark_node
14445       || (TREE_CODE (decl) == FUNCTION_DECL
14446           && DECL_ANTICIPATED (decl)))
14447     return error_mark_node;
14448
14449   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14450   if (TREE_CODE (decl) == TREE_LIST)
14451     {
14452       if (ambiguous_p)
14453         *ambiguous_p = true;
14454       /* The error message we have to print is too complicated for
14455          cp_parser_error, so we incorporate its actions directly.  */
14456       if (!cp_parser_simulate_error (parser))
14457         {
14458           error ("reference to %qD is ambiguous", name);
14459           print_candidates (decl);
14460         }
14461       return error_mark_node;
14462     }
14463
14464   gcc_assert (DECL_P (decl)
14465               || TREE_CODE (decl) == OVERLOAD
14466               || TREE_CODE (decl) == SCOPE_REF
14467               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14468               || BASELINK_P (decl));
14469
14470   /* If we have resolved the name of a member declaration, check to
14471      see if the declaration is accessible.  When the name resolves to
14472      set of overloaded functions, accessibility is checked when
14473      overload resolution is done.
14474
14475      During an explicit instantiation, access is not checked at all,
14476      as per [temp.explicit].  */
14477   if (DECL_P (decl))
14478     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14479
14480   return decl;
14481 }
14482
14483 /* Like cp_parser_lookup_name, but for use in the typical case where
14484    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14485    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14486
14487 static tree
14488 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14489 {
14490   return cp_parser_lookup_name (parser, name,
14491                                 none_type,
14492                                 /*is_template=*/false,
14493                                 /*is_namespace=*/false,
14494                                 /*check_dependency=*/true,
14495                                 /*ambiguous_p=*/NULL);
14496 }
14497
14498 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14499    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14500    true, the DECL indicates the class being defined in a class-head,
14501    or declared in an elaborated-type-specifier.
14502
14503    Otherwise, return DECL.  */
14504
14505 static tree
14506 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14507 {
14508   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14509      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14510
14511        struct A {
14512          template <typename T> struct B;
14513        };
14514
14515        template <typename T> struct A::B {};
14516
14517      Similarly, in a elaborated-type-specifier:
14518
14519        namespace N { struct X{}; }
14520
14521        struct A {
14522          template <typename T> friend struct N::X;
14523        };
14524
14525      However, if the DECL refers to a class type, and we are in
14526      the scope of the class, then the name lookup automatically
14527      finds the TYPE_DECL created by build_self_reference rather
14528      than a TEMPLATE_DECL.  For example, in:
14529
14530        template <class T> struct S {
14531          S s;
14532        };
14533
14534      there is no need to handle such case.  */
14535
14536   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14537     return DECL_TEMPLATE_RESULT (decl);
14538
14539   return decl;
14540 }
14541
14542 /* If too many, or too few, template-parameter lists apply to the
14543    declarator, issue an error message.  Returns TRUE if all went well,
14544    and FALSE otherwise.  */
14545
14546 static bool
14547 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14548                                                 cp_declarator *declarator)
14549 {
14550   unsigned num_templates;
14551
14552   /* We haven't seen any classes that involve template parameters yet.  */
14553   num_templates = 0;
14554
14555   switch (declarator->kind)
14556     {
14557     case cdk_id:
14558       if (declarator->u.id.qualifying_scope)
14559         {
14560           tree scope;
14561           tree member;
14562
14563           scope = declarator->u.id.qualifying_scope;
14564           member = declarator->u.id.unqualified_name;
14565
14566           while (scope && CLASS_TYPE_P (scope))
14567             {
14568               /* You're supposed to have one `template <...>'
14569                  for every template class, but you don't need one
14570                  for a full specialization.  For example:
14571
14572                  template <class T> struct S{};
14573                  template <> struct S<int> { void f(); };
14574                  void S<int>::f () {}
14575
14576                  is correct; there shouldn't be a `template <>' for
14577                  the definition of `S<int>::f'.  */
14578               if (CLASSTYPE_TEMPLATE_INFO (scope)
14579                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14580                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14581                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14582                 ++num_templates;
14583
14584               scope = TYPE_CONTEXT (scope);
14585             }
14586         }
14587       else if (TREE_CODE (declarator->u.id.unqualified_name) 
14588                == TEMPLATE_ID_EXPR)
14589         /* If the DECLARATOR has the form `X<y>' then it uses one
14590            additional level of template parameters.  */
14591         ++num_templates;
14592
14593       return cp_parser_check_template_parameters (parser,
14594                                                   num_templates);
14595
14596     case cdk_function:
14597     case cdk_array:
14598     case cdk_pointer:
14599     case cdk_reference:
14600     case cdk_ptrmem:
14601       return (cp_parser_check_declarator_template_parameters
14602               (parser, declarator->declarator));
14603
14604     case cdk_error:
14605       return true;
14606
14607     default:
14608       gcc_unreachable ();
14609     }
14610   return false;
14611 }
14612
14613 /* NUM_TEMPLATES were used in the current declaration.  If that is
14614    invalid, return FALSE and issue an error messages.  Otherwise,
14615    return TRUE.  */
14616
14617 static bool
14618 cp_parser_check_template_parameters (cp_parser* parser,
14619                                      unsigned num_templates)
14620 {
14621   /* If there are more template classes than parameter lists, we have
14622      something like:
14623
14624        template <class T> void S<T>::R<T>::f ();  */
14625   if (parser->num_template_parameter_lists < num_templates)
14626     {
14627       error ("too few template-parameter-lists");
14628       return false;
14629     }
14630   /* If there are the same number of template classes and parameter
14631      lists, that's OK.  */
14632   if (parser->num_template_parameter_lists == num_templates)
14633     return true;
14634   /* If there are more, but only one more, then we are referring to a
14635      member template.  That's OK too.  */
14636   if (parser->num_template_parameter_lists == num_templates + 1)
14637       return true;
14638   /* Otherwise, there are too many template parameter lists.  We have
14639      something like:
14640
14641      template <class T> template <class U> void S::f();  */
14642   error ("too many template-parameter-lists");
14643   return false;
14644 }
14645
14646 /* Parse an optional `::' token indicating that the following name is
14647    from the global namespace.  If so, PARSER->SCOPE is set to the
14648    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14649    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14650    Returns the new value of PARSER->SCOPE, if the `::' token is
14651    present, and NULL_TREE otherwise.  */
14652
14653 static tree
14654 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14655 {
14656   cp_token *token;
14657
14658   /* Peek at the next token.  */
14659   token = cp_lexer_peek_token (parser->lexer);
14660   /* If we're looking at a `::' token then we're starting from the
14661      global namespace, not our current location.  */
14662   if (token->type == CPP_SCOPE)
14663     {
14664       /* Consume the `::' token.  */
14665       cp_lexer_consume_token (parser->lexer);
14666       /* Set the SCOPE so that we know where to start the lookup.  */
14667       parser->scope = global_namespace;
14668       parser->qualifying_scope = global_namespace;
14669       parser->object_scope = NULL_TREE;
14670
14671       return parser->scope;
14672     }
14673   else if (!current_scope_valid_p)
14674     {
14675       parser->scope = NULL_TREE;
14676       parser->qualifying_scope = NULL_TREE;
14677       parser->object_scope = NULL_TREE;
14678     }
14679
14680   return NULL_TREE;
14681 }
14682
14683 /* Returns TRUE if the upcoming token sequence is the start of a
14684    constructor declarator.  If FRIEND_P is true, the declarator is
14685    preceded by the `friend' specifier.  */
14686
14687 static bool
14688 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14689 {
14690   bool constructor_p;
14691   tree type_decl = NULL_TREE;
14692   bool nested_name_p;
14693   cp_token *next_token;
14694
14695   /* The common case is that this is not a constructor declarator, so
14696      try to avoid doing lots of work if at all possible.  It's not
14697      valid declare a constructor at function scope.  */
14698   if (at_function_scope_p ())
14699     return false;
14700   /* And only certain tokens can begin a constructor declarator.  */
14701   next_token = cp_lexer_peek_token (parser->lexer);
14702   if (next_token->type != CPP_NAME
14703       && next_token->type != CPP_SCOPE
14704       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14705       && next_token->type != CPP_TEMPLATE_ID)
14706     return false;
14707
14708   /* Parse tentatively; we are going to roll back all of the tokens
14709      consumed here.  */
14710   cp_parser_parse_tentatively (parser);
14711   /* Assume that we are looking at a constructor declarator.  */
14712   constructor_p = true;
14713
14714   /* Look for the optional `::' operator.  */
14715   cp_parser_global_scope_opt (parser,
14716                               /*current_scope_valid_p=*/false);
14717   /* Look for the nested-name-specifier.  */
14718   nested_name_p
14719     = (cp_parser_nested_name_specifier_opt (parser,
14720                                             /*typename_keyword_p=*/false,
14721                                             /*check_dependency_p=*/false,
14722                                             /*type_p=*/false,
14723                                             /*is_declaration=*/false)
14724        != NULL_TREE);
14725   /* Outside of a class-specifier, there must be a
14726      nested-name-specifier.  */
14727   if (!nested_name_p &&
14728       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14729        || friend_p))
14730     constructor_p = false;
14731   /* If we still think that this might be a constructor-declarator,
14732      look for a class-name.  */
14733   if (constructor_p)
14734     {
14735       /* If we have:
14736
14737            template <typename T> struct S { S(); };
14738            template <typename T> S<T>::S ();
14739
14740          we must recognize that the nested `S' names a class.
14741          Similarly, for:
14742
14743            template <typename T> S<T>::S<T> ();
14744
14745          we must recognize that the nested `S' names a template.  */
14746       type_decl = cp_parser_class_name (parser,
14747                                         /*typename_keyword_p=*/false,
14748                                         /*template_keyword_p=*/false,
14749                                         none_type,
14750                                         /*check_dependency_p=*/false,
14751                                         /*class_head_p=*/false,
14752                                         /*is_declaration=*/false);
14753       /* If there was no class-name, then this is not a constructor.  */
14754       constructor_p = !cp_parser_error_occurred (parser);
14755     }
14756
14757   /* If we're still considering a constructor, we have to see a `(',
14758      to begin the parameter-declaration-clause, followed by either a
14759      `)', an `...', or a decl-specifier.  We need to check for a
14760      type-specifier to avoid being fooled into thinking that:
14761
14762        S::S (f) (int);
14763
14764      is a constructor.  (It is actually a function named `f' that
14765      takes one parameter (of type `int') and returns a value of type
14766      `S::S'.  */
14767   if (constructor_p
14768       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14769     {
14770       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14771           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14772           /* A parameter declaration begins with a decl-specifier,
14773              which is either the "attribute" keyword, a storage class
14774              specifier, or (usually) a type-specifier.  */
14775           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14776           && !cp_parser_storage_class_specifier_opt (parser))
14777         {
14778           tree type;
14779           tree pushed_scope = NULL_TREE;
14780           unsigned saved_num_template_parameter_lists;
14781
14782           /* Names appearing in the type-specifier should be looked up
14783              in the scope of the class.  */
14784           if (current_class_type)
14785             type = NULL_TREE;
14786           else
14787             {
14788               type = TREE_TYPE (type_decl);
14789               if (TREE_CODE (type) == TYPENAME_TYPE)
14790                 {
14791                   type = resolve_typename_type (type,
14792                                                 /*only_current_p=*/false);
14793                   if (type == error_mark_node)
14794                     {
14795                       cp_parser_abort_tentative_parse (parser);
14796                       return false;
14797                     }
14798                 }
14799               pushed_scope = push_scope (type);
14800             }
14801
14802           /* Inside the constructor parameter list, surrounding
14803              template-parameter-lists do not apply.  */
14804           saved_num_template_parameter_lists
14805             = parser->num_template_parameter_lists;
14806           parser->num_template_parameter_lists = 0;
14807
14808           /* Look for the type-specifier.  */
14809           cp_parser_type_specifier (parser,
14810                                     CP_PARSER_FLAGS_NONE,
14811                                     /*decl_specs=*/NULL,
14812                                     /*is_declarator=*/true,
14813                                     /*declares_class_or_enum=*/NULL,
14814                                     /*is_cv_qualifier=*/NULL);
14815
14816           parser->num_template_parameter_lists
14817             = saved_num_template_parameter_lists;
14818
14819           /* Leave the scope of the class.  */
14820           if (pushed_scope)
14821             pop_scope (pushed_scope);
14822
14823           constructor_p = !cp_parser_error_occurred (parser);
14824         }
14825     }
14826   else
14827     constructor_p = false;
14828   /* We did not really want to consume any tokens.  */
14829   cp_parser_abort_tentative_parse (parser);
14830
14831   return constructor_p;
14832 }
14833
14834 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14835    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14836    they must be performed once we are in the scope of the function.
14837
14838    Returns the function defined.  */
14839
14840 static tree
14841 cp_parser_function_definition_from_specifiers_and_declarator
14842   (cp_parser* parser,
14843    cp_decl_specifier_seq *decl_specifiers,
14844    tree attributes,
14845    const cp_declarator *declarator)
14846 {
14847   tree fn;
14848   bool success_p;
14849
14850   /* Begin the function-definition.  */
14851   success_p = start_function (decl_specifiers, declarator, attributes);
14852
14853   /* The things we're about to see are not directly qualified by any
14854      template headers we've seen thus far.  */
14855   reset_specialization ();
14856
14857   /* If there were names looked up in the decl-specifier-seq that we
14858      did not check, check them now.  We must wait until we are in the
14859      scope of the function to perform the checks, since the function
14860      might be a friend.  */
14861   perform_deferred_access_checks ();
14862
14863   if (!success_p)
14864     {
14865       /* Skip the entire function.  */
14866       error ("invalid function declaration");
14867       cp_parser_skip_to_end_of_block_or_statement (parser);
14868       fn = error_mark_node;
14869     }
14870   else
14871     fn = cp_parser_function_definition_after_declarator (parser,
14872                                                          /*inline_p=*/false);
14873
14874   return fn;
14875 }
14876
14877 /* Parse the part of a function-definition that follows the
14878    declarator.  INLINE_P is TRUE iff this function is an inline
14879    function defined with a class-specifier.
14880
14881    Returns the function defined.  */
14882
14883 static tree
14884 cp_parser_function_definition_after_declarator (cp_parser* parser,
14885                                                 bool inline_p)
14886 {
14887   tree fn;
14888   bool ctor_initializer_p = false;
14889   bool saved_in_unbraced_linkage_specification_p;
14890   unsigned saved_num_template_parameter_lists;
14891
14892   /* If the next token is `return', then the code may be trying to
14893      make use of the "named return value" extension that G++ used to
14894      support.  */
14895   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14896     {
14897       /* Consume the `return' keyword.  */
14898       cp_lexer_consume_token (parser->lexer);
14899       /* Look for the identifier that indicates what value is to be
14900          returned.  */
14901       cp_parser_identifier (parser);
14902       /* Issue an error message.  */
14903       error ("named return values are no longer supported");
14904       /* Skip tokens until we reach the start of the function body.  */
14905       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14906              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14907         cp_lexer_consume_token (parser->lexer);
14908     }
14909   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14910      anything declared inside `f'.  */
14911   saved_in_unbraced_linkage_specification_p
14912     = parser->in_unbraced_linkage_specification_p;
14913   parser->in_unbraced_linkage_specification_p = false;
14914   /* Inside the function, surrounding template-parameter-lists do not
14915      apply.  */
14916   saved_num_template_parameter_lists
14917     = parser->num_template_parameter_lists;
14918   parser->num_template_parameter_lists = 0;
14919   /* If the next token is `try', then we are looking at a
14920      function-try-block.  */
14921   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14922     ctor_initializer_p = cp_parser_function_try_block (parser);
14923   /* A function-try-block includes the function-body, so we only do
14924      this next part if we're not processing a function-try-block.  */
14925   else
14926     ctor_initializer_p
14927       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14928
14929   /* Finish the function.  */
14930   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14931                         (inline_p ? 2 : 0));
14932   /* Generate code for it, if necessary.  */
14933   expand_or_defer_fn (fn);
14934   /* Restore the saved values.  */
14935   parser->in_unbraced_linkage_specification_p
14936     = saved_in_unbraced_linkage_specification_p;
14937   parser->num_template_parameter_lists
14938     = saved_num_template_parameter_lists;
14939
14940   return fn;
14941 }
14942
14943 /* Parse a template-declaration, assuming that the `export' (and
14944    `extern') keywords, if present, has already been scanned.  MEMBER_P
14945    is as for cp_parser_template_declaration.  */
14946
14947 static void
14948 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14949 {
14950   tree decl = NULL_TREE;
14951   tree parameter_list;
14952   bool friend_p = false;
14953
14954   /* Look for the `template' keyword.  */
14955   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14956     return;
14957
14958   /* And the `<'.  */
14959   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14960     return;
14961
14962   /* If the next token is `>', then we have an invalid
14963      specialization.  Rather than complain about an invalid template
14964      parameter, issue an error message here.  */
14965   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14966     {
14967       cp_parser_error (parser, "invalid explicit specialization");
14968       begin_specialization ();
14969       parameter_list = NULL_TREE;
14970     }
14971   else
14972     {
14973       /* Parse the template parameters.  */
14974       begin_template_parm_list ();
14975       parameter_list = cp_parser_template_parameter_list (parser);
14976       parameter_list = end_template_parm_list (parameter_list);
14977     }
14978
14979   /* Look for the `>'.  */
14980   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14981   /* We just processed one more parameter list.  */
14982   ++parser->num_template_parameter_lists;
14983   /* If the next token is `template', there are more template
14984      parameters.  */
14985   if (cp_lexer_next_token_is_keyword (parser->lexer,
14986                                       RID_TEMPLATE))
14987     cp_parser_template_declaration_after_export (parser, member_p);
14988   else
14989     {
14990       /* There are no access checks when parsing a template, as we do not
14991          know if a specialization will be a friend.  */
14992       push_deferring_access_checks (dk_no_check);
14993
14994       decl = cp_parser_single_declaration (parser,
14995                                            member_p,
14996                                            &friend_p);
14997
14998       pop_deferring_access_checks ();
14999
15000       /* If this is a member template declaration, let the front
15001          end know.  */
15002       if (member_p && !friend_p && decl)
15003         {
15004           if (TREE_CODE (decl) == TYPE_DECL)
15005             cp_parser_check_access_in_redeclaration (decl);
15006
15007           decl = finish_member_template_decl (decl);
15008         }
15009       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15010         make_friend_class (current_class_type, TREE_TYPE (decl),
15011                            /*complain=*/true);
15012     }
15013   /* We are done with the current parameter list.  */
15014   --parser->num_template_parameter_lists;
15015
15016   /* Finish up.  */
15017   finish_template_decl (parameter_list);
15018
15019   /* Register member declarations.  */
15020   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15021     finish_member_declaration (decl);
15022
15023   /* If DECL is a function template, we must return to parse it later.
15024      (Even though there is no definition, there might be default
15025      arguments that need handling.)  */
15026   if (member_p && decl
15027       && (TREE_CODE (decl) == FUNCTION_DECL
15028           || DECL_FUNCTION_TEMPLATE_P (decl)))
15029     TREE_VALUE (parser->unparsed_functions_queues)
15030       = tree_cons (NULL_TREE, decl,
15031                    TREE_VALUE (parser->unparsed_functions_queues));
15032 }
15033
15034 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15035    `function-definition' sequence.  MEMBER_P is true, this declaration
15036    appears in a class scope.
15037
15038    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15039    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15040
15041 static tree
15042 cp_parser_single_declaration (cp_parser* parser,
15043                               bool member_p,
15044                               bool* friend_p)
15045 {
15046   int declares_class_or_enum;
15047   tree decl = NULL_TREE;
15048   cp_decl_specifier_seq decl_specifiers;
15049   bool function_definition_p = false;
15050
15051   /* This function is only used when processing a template
15052      declaration.  */
15053   gcc_assert (innermost_scope_kind () == sk_template_parms
15054               || innermost_scope_kind () == sk_template_spec);
15055
15056   /* Defer access checks until we know what is being declared.  */
15057   push_deferring_access_checks (dk_deferred);
15058
15059   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15060      alternative.  */
15061   cp_parser_decl_specifier_seq (parser,
15062                                 CP_PARSER_FLAGS_OPTIONAL,
15063                                 &decl_specifiers,
15064                                 &declares_class_or_enum);
15065   if (friend_p)
15066     *friend_p = cp_parser_friend_p (&decl_specifiers);
15067
15068   /* There are no template typedefs.  */
15069   if (decl_specifiers.specs[(int) ds_typedef])
15070     {
15071       error ("template declaration of %qs", "typedef");
15072       decl = error_mark_node;
15073     }
15074
15075   /* Gather up the access checks that occurred the
15076      decl-specifier-seq.  */
15077   stop_deferring_access_checks ();
15078
15079   /* Check for the declaration of a template class.  */
15080   if (declares_class_or_enum)
15081     {
15082       if (cp_parser_declares_only_class_p (parser))
15083         {
15084           decl = shadow_tag (&decl_specifiers);
15085
15086           /* In this case:
15087
15088                struct C {
15089                  friend template <typename T> struct A<T>::B;
15090                };
15091
15092              A<T>::B will be represented by a TYPENAME_TYPE, and
15093              therefore not recognized by shadow_tag.  */
15094           if (friend_p && *friend_p
15095               && !decl
15096               && decl_specifiers.type
15097               && TYPE_P (decl_specifiers.type))
15098             decl = decl_specifiers.type;
15099
15100           if (decl && decl != error_mark_node)
15101             decl = TYPE_NAME (decl);
15102           else
15103             decl = error_mark_node;
15104         }
15105     }
15106   /* If it's not a template class, try for a template function.  If
15107      the next token is a `;', then this declaration does not declare
15108      anything.  But, if there were errors in the decl-specifiers, then
15109      the error might well have come from an attempted class-specifier.
15110      In that case, there's no need to warn about a missing declarator.  */
15111   if (!decl
15112       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15113           || decl_specifiers.type != error_mark_node))
15114     decl = cp_parser_init_declarator (parser,
15115                                       &decl_specifiers,
15116                                       /*function_definition_allowed_p=*/true,
15117                                       member_p,
15118                                       declares_class_or_enum,
15119                                       &function_definition_p);
15120
15121   pop_deferring_access_checks ();
15122
15123   /* Clear any current qualification; whatever comes next is the start
15124      of something new.  */
15125   parser->scope = NULL_TREE;
15126   parser->qualifying_scope = NULL_TREE;
15127   parser->object_scope = NULL_TREE;
15128   /* Look for a trailing `;' after the declaration.  */
15129   if (!function_definition_p
15130       && (decl == error_mark_node
15131           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15132     cp_parser_skip_to_end_of_block_or_statement (parser);
15133
15134   return decl;
15135 }
15136
15137 /* Parse a cast-expression that is not the operand of a unary "&".  */
15138
15139 static tree
15140 cp_parser_simple_cast_expression (cp_parser *parser)
15141 {
15142   return cp_parser_cast_expression (parser, /*address_p=*/false,
15143                                     /*cast_p=*/false);
15144 }
15145
15146 /* Parse a functional cast to TYPE.  Returns an expression
15147    representing the cast.  */
15148
15149 static tree
15150 cp_parser_functional_cast (cp_parser* parser, tree type)
15151 {
15152   tree expression_list;
15153   tree cast;
15154
15155   expression_list
15156     = cp_parser_parenthesized_expression_list (parser, false,
15157                                                /*cast_p=*/true,
15158                                                /*non_constant_p=*/NULL);
15159
15160   cast = build_functional_cast (type, expression_list);
15161   /* [expr.const]/1: In an integral constant expression "only type
15162      conversions to integral or enumeration type can be used".  */
15163   if (cast != error_mark_node && !type_dependent_expression_p (type)
15164       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15165     {
15166       if (cp_parser_non_integral_constant_expression
15167           (parser, "a call to a constructor"))
15168         return error_mark_node;
15169     }
15170   return cast;
15171 }
15172
15173 /* Save the tokens that make up the body of a member function defined
15174    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15175    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15176    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15177    for the member function.  */
15178
15179 static tree
15180 cp_parser_save_member_function_body (cp_parser* parser,
15181                                      cp_decl_specifier_seq *decl_specifiers,
15182                                      cp_declarator *declarator,
15183                                      tree attributes)
15184 {
15185   cp_token *first;
15186   cp_token *last;
15187   tree fn;
15188
15189   /* Create the function-declaration.  */
15190   fn = start_method (decl_specifiers, declarator, attributes);
15191   /* If something went badly wrong, bail out now.  */
15192   if (fn == error_mark_node)
15193     {
15194       /* If there's a function-body, skip it.  */
15195       if (cp_parser_token_starts_function_definition_p
15196           (cp_lexer_peek_token (parser->lexer)))
15197         cp_parser_skip_to_end_of_block_or_statement (parser);
15198       return error_mark_node;
15199     }
15200
15201   /* Remember it, if there default args to post process.  */
15202   cp_parser_save_default_args (parser, fn);
15203
15204   /* Save away the tokens that make up the body of the
15205      function.  */
15206   first = parser->lexer->next_token;
15207   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15208   /* Handle function try blocks.  */
15209   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15210     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15211   last = parser->lexer->next_token;
15212
15213   /* Save away the inline definition; we will process it when the
15214      class is complete.  */
15215   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15216   DECL_PENDING_INLINE_P (fn) = 1;
15217
15218   /* We need to know that this was defined in the class, so that
15219      friend templates are handled correctly.  */
15220   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15221
15222   /* We're done with the inline definition.  */
15223   finish_method (fn);
15224
15225   /* Add FN to the queue of functions to be parsed later.  */
15226   TREE_VALUE (parser->unparsed_functions_queues)
15227     = tree_cons (NULL_TREE, fn,
15228                  TREE_VALUE (parser->unparsed_functions_queues));
15229
15230   return fn;
15231 }
15232
15233 /* Parse a template-argument-list, as well as the trailing ">" (but
15234    not the opening ">").  See cp_parser_template_argument_list for the
15235    return value.  */
15236
15237 static tree
15238 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15239 {
15240   tree arguments;
15241   tree saved_scope;
15242   tree saved_qualifying_scope;
15243   tree saved_object_scope;
15244   bool saved_greater_than_is_operator_p;
15245
15246   /* [temp.names]
15247
15248      When parsing a template-id, the first non-nested `>' is taken as
15249      the end of the template-argument-list rather than a greater-than
15250      operator.  */
15251   saved_greater_than_is_operator_p
15252     = parser->greater_than_is_operator_p;
15253   parser->greater_than_is_operator_p = false;
15254   /* Parsing the argument list may modify SCOPE, so we save it
15255      here.  */
15256   saved_scope = parser->scope;
15257   saved_qualifying_scope = parser->qualifying_scope;
15258   saved_object_scope = parser->object_scope;
15259   /* Parse the template-argument-list itself.  */
15260   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15261     arguments = NULL_TREE;
15262   else
15263     arguments = cp_parser_template_argument_list (parser);
15264   /* Look for the `>' that ends the template-argument-list. If we find
15265      a '>>' instead, it's probably just a typo.  */
15266   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15267     {
15268       if (!saved_greater_than_is_operator_p)
15269         {
15270           /* If we're in a nested template argument list, the '>>' has
15271             to be a typo for '> >'. We emit the error message, but we
15272             continue parsing and we push a '>' as next token, so that
15273             the argument list will be parsed correctly.  Note that the
15274             global source location is still on the token before the
15275             '>>', so we need to say explicitly where we want it.  */
15276           cp_token *token = cp_lexer_peek_token (parser->lexer);
15277           error ("%H%<>>%> should be %<> >%> "
15278                  "within a nested template argument list",
15279                  &token->location);
15280
15281           /* ??? Proper recovery should terminate two levels of
15282              template argument list here.  */
15283           token->type = CPP_GREATER;
15284         }
15285       else
15286         {
15287           /* If this is not a nested template argument list, the '>>'
15288             is a typo for '>'. Emit an error message and continue.
15289             Same deal about the token location, but here we can get it
15290             right by consuming the '>>' before issuing the diagnostic.  */
15291           cp_lexer_consume_token (parser->lexer);
15292           error ("spurious %<>>%>, use %<>%> to terminate "
15293                  "a template argument list");
15294         }
15295     }
15296   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15297     error ("missing %<>%> to terminate the template argument list");
15298   else
15299     /* It's what we want, a '>'; consume it.  */
15300     cp_lexer_consume_token (parser->lexer);
15301   /* The `>' token might be a greater-than operator again now.  */
15302   parser->greater_than_is_operator_p
15303     = saved_greater_than_is_operator_p;
15304   /* Restore the SAVED_SCOPE.  */
15305   parser->scope = saved_scope;
15306   parser->qualifying_scope = saved_qualifying_scope;
15307   parser->object_scope = saved_object_scope;
15308
15309   return arguments;
15310 }
15311
15312 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15313    arguments, or the body of the function have not yet been parsed,
15314    parse them now.  */
15315
15316 static void
15317 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15318 {
15319   /* If this member is a template, get the underlying
15320      FUNCTION_DECL.  */
15321   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15322     member_function = DECL_TEMPLATE_RESULT (member_function);
15323
15324   /* There should not be any class definitions in progress at this
15325      point; the bodies of members are only parsed outside of all class
15326      definitions.  */
15327   gcc_assert (parser->num_classes_being_defined == 0);
15328   /* While we're parsing the member functions we might encounter more
15329      classes.  We want to handle them right away, but we don't want
15330      them getting mixed up with functions that are currently in the
15331      queue.  */
15332   parser->unparsed_functions_queues
15333     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15334
15335   /* Make sure that any template parameters are in scope.  */
15336   maybe_begin_member_template_processing (member_function);
15337
15338   /* If the body of the function has not yet been parsed, parse it
15339      now.  */
15340   if (DECL_PENDING_INLINE_P (member_function))
15341     {
15342       tree function_scope;
15343       cp_token_cache *tokens;
15344
15345       /* The function is no longer pending; we are processing it.  */
15346       tokens = DECL_PENDING_INLINE_INFO (member_function);
15347       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15348       DECL_PENDING_INLINE_P (member_function) = 0;
15349       
15350       /* If this is a local class, enter the scope of the containing
15351          function.  */
15352       function_scope = current_function_decl;
15353       if (function_scope)
15354         push_function_context_to (function_scope);
15355
15356       /* Push the body of the function onto the lexer stack.  */
15357       cp_parser_push_lexer_for_tokens (parser, tokens);
15358
15359       /* Let the front end know that we going to be defining this
15360          function.  */
15361       start_preparsed_function (member_function, NULL_TREE,
15362                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15363
15364       /* Now, parse the body of the function.  */
15365       cp_parser_function_definition_after_declarator (parser,
15366                                                       /*inline_p=*/true);
15367
15368       /* Leave the scope of the containing function.  */
15369       if (function_scope)
15370         pop_function_context_from (function_scope);
15371       cp_parser_pop_lexer (parser);
15372     }
15373
15374   /* Remove any template parameters from the symbol table.  */
15375   maybe_end_member_template_processing ();
15376
15377   /* Restore the queue.  */
15378   parser->unparsed_functions_queues
15379     = TREE_CHAIN (parser->unparsed_functions_queues);
15380 }
15381
15382 /* If DECL contains any default args, remember it on the unparsed
15383    functions queue.  */
15384
15385 static void
15386 cp_parser_save_default_args (cp_parser* parser, tree decl)
15387 {
15388   tree probe;
15389
15390   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15391        probe;
15392        probe = TREE_CHAIN (probe))
15393     if (TREE_PURPOSE (probe))
15394       {
15395         TREE_PURPOSE (parser->unparsed_functions_queues)
15396           = tree_cons (current_class_type, decl,
15397                        TREE_PURPOSE (parser->unparsed_functions_queues));
15398         break;
15399       }
15400   return;
15401 }
15402
15403 /* FN is a FUNCTION_DECL which may contains a parameter with an
15404    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15405    assumes that the current scope is the scope in which the default
15406    argument should be processed.  */
15407
15408 static void
15409 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15410 {
15411   bool saved_local_variables_forbidden_p;
15412   tree parm;
15413
15414   /* While we're parsing the default args, we might (due to the
15415      statement expression extension) encounter more classes.  We want
15416      to handle them right away, but we don't want them getting mixed
15417      up with default args that are currently in the queue.  */
15418   parser->unparsed_functions_queues
15419     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15420
15421   /* Local variable names (and the `this' keyword) may not appear
15422      in a default argument.  */
15423   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15424   parser->local_variables_forbidden_p = true;
15425
15426   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15427        parm;
15428        parm = TREE_CHAIN (parm))
15429     {
15430       cp_token_cache *tokens;
15431
15432       if (!TREE_PURPOSE (parm)
15433           || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15434         continue;
15435
15436        /* Push the saved tokens for the default argument onto the parser's
15437           lexer stack.  */
15438       tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15439       cp_parser_push_lexer_for_tokens (parser, tokens);
15440
15441       /* Parse the assignment-expression.  */
15442       TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser,
15443                                                              /*cast_p=*/false);
15444
15445       /* If the token stream has not been completely used up, then
15446          there was extra junk after the end of the default
15447          argument.  */
15448       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15449         cp_parser_error (parser, "expected %<,%>");
15450
15451       /* Revert to the main lexer.  */
15452       cp_parser_pop_lexer (parser);
15453     }
15454
15455   /* Restore the state of local_variables_forbidden_p.  */
15456   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15457
15458   /* Restore the queue.  */
15459   parser->unparsed_functions_queues
15460     = TREE_CHAIN (parser->unparsed_functions_queues);
15461 }
15462
15463 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15464    either a TYPE or an expression, depending on the form of the
15465    input.  The KEYWORD indicates which kind of expression we have
15466    encountered.  */
15467
15468 static tree
15469 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15470 {
15471   static const char *format;
15472   tree expr = NULL_TREE;
15473   const char *saved_message;
15474   bool saved_integral_constant_expression_p;
15475   bool saved_non_integral_constant_expression_p;
15476
15477   /* Initialize FORMAT the first time we get here.  */
15478   if (!format)
15479     format = "types may not be defined in '%s' expressions";
15480
15481   /* Types cannot be defined in a `sizeof' expression.  Save away the
15482      old message.  */
15483   saved_message = parser->type_definition_forbidden_message;
15484   /* And create the new one.  */
15485   parser->type_definition_forbidden_message
15486     = xmalloc (strlen (format)
15487                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15488                + 1 /* `\0' */);
15489   sprintf ((char *) parser->type_definition_forbidden_message,
15490            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15491
15492   /* The restrictions on constant-expressions do not apply inside
15493      sizeof expressions.  */
15494   saved_integral_constant_expression_p 
15495     = parser->integral_constant_expression_p;
15496   saved_non_integral_constant_expression_p
15497     = parser->non_integral_constant_expression_p;
15498   parser->integral_constant_expression_p = false;
15499
15500   /* Do not actually evaluate the expression.  */
15501   ++skip_evaluation;
15502   /* If it's a `(', then we might be looking at the type-id
15503      construction.  */
15504   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15505     {
15506       tree type;
15507       bool saved_in_type_id_in_expr_p;
15508
15509       /* We can't be sure yet whether we're looking at a type-id or an
15510          expression.  */
15511       cp_parser_parse_tentatively (parser);
15512       /* Consume the `('.  */
15513       cp_lexer_consume_token (parser->lexer);
15514       /* Parse the type-id.  */
15515       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15516       parser->in_type_id_in_expr_p = true;
15517       type = cp_parser_type_id (parser);
15518       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15519       /* Now, look for the trailing `)'.  */
15520       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15521       /* If all went well, then we're done.  */
15522       if (cp_parser_parse_definitely (parser))
15523         {
15524           cp_decl_specifier_seq decl_specs;
15525
15526           /* Build a trivial decl-specifier-seq.  */
15527           clear_decl_specs (&decl_specs);
15528           decl_specs.type = type;
15529
15530           /* Call grokdeclarator to figure out what type this is.  */
15531           expr = grokdeclarator (NULL,
15532                                  &decl_specs,
15533                                  TYPENAME,
15534                                  /*initialized=*/0,
15535                                  /*attrlist=*/NULL);
15536         }
15537     }
15538
15539   /* If the type-id production did not work out, then we must be
15540      looking at the unary-expression production.  */
15541   if (!expr)
15542     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15543                                        /*cast_p=*/false);
15544   /* Go back to evaluating expressions.  */
15545   --skip_evaluation;
15546
15547   /* Free the message we created.  */
15548   free ((char *) parser->type_definition_forbidden_message);
15549   /* And restore the old one.  */
15550   parser->type_definition_forbidden_message = saved_message;
15551   parser->integral_constant_expression_p 
15552     = saved_integral_constant_expression_p;
15553   parser->non_integral_constant_expression_p
15554     = saved_non_integral_constant_expression_p;
15555
15556   return expr;
15557 }
15558
15559 /* If the current declaration has no declarator, return true.  */
15560
15561 static bool
15562 cp_parser_declares_only_class_p (cp_parser *parser)
15563 {
15564   /* If the next token is a `;' or a `,' then there is no
15565      declarator.  */
15566   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15567           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15568 }
15569
15570 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15571
15572 static void
15573 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15574                              cp_storage_class storage_class)
15575 {
15576   if (decl_specs->storage_class != sc_none)
15577     decl_specs->multiple_storage_classes_p = true;
15578   else
15579     decl_specs->storage_class = storage_class;
15580 }
15581
15582 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15583    is true, the type is a user-defined type; otherwise it is a
15584    built-in type specified by a keyword.  */
15585
15586 static void
15587 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15588                               tree type_spec,
15589                               bool user_defined_p)
15590 {
15591   decl_specs->any_specifiers_p = true;
15592
15593   /* If the user tries to redeclare bool or wchar_t (with, for
15594      example, in "typedef int wchar_t;") we remember that this is what
15595      happened.  In system headers, we ignore these declarations so
15596      that G++ can work with system headers that are not C++-safe.  */
15597   if (decl_specs->specs[(int) ds_typedef]
15598       && !user_defined_p
15599       && (type_spec == boolean_type_node
15600           || type_spec == wchar_type_node)
15601       && (decl_specs->type
15602           || decl_specs->specs[(int) ds_long]
15603           || decl_specs->specs[(int) ds_short]
15604           || decl_specs->specs[(int) ds_unsigned]
15605           || decl_specs->specs[(int) ds_signed]))
15606     {
15607       decl_specs->redefined_builtin_type = type_spec;
15608       if (!decl_specs->type)
15609         {
15610           decl_specs->type = type_spec;
15611           decl_specs->user_defined_type_p = false;
15612         }
15613     }
15614   else if (decl_specs->type)
15615     decl_specs->multiple_types_p = true;
15616   else
15617     {
15618       decl_specs->type = type_spec;
15619       decl_specs->user_defined_type_p = user_defined_p;
15620       decl_specs->redefined_builtin_type = NULL_TREE;
15621     }
15622 }
15623
15624 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15625    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15626
15627 static bool
15628 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15629 {
15630   return decl_specifiers->specs[(int) ds_friend] != 0;
15631 }
15632
15633 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15634    issue an error message indicating that TOKEN_DESC was expected.
15635
15636    Returns the token consumed, if the token had the appropriate type.
15637    Otherwise, returns NULL.  */
15638
15639 static cp_token *
15640 cp_parser_require (cp_parser* parser,
15641                    enum cpp_ttype type,
15642                    const char* token_desc)
15643 {
15644   if (cp_lexer_next_token_is (parser->lexer, type))
15645     return cp_lexer_consume_token (parser->lexer);
15646   else
15647     {
15648       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15649       if (!cp_parser_simulate_error (parser))
15650         {
15651           char *message = concat ("expected ", token_desc, NULL);
15652           cp_parser_error (parser, message);
15653           free (message);
15654         }
15655       return NULL;
15656     }
15657 }
15658
15659 /* Like cp_parser_require, except that tokens will be skipped until
15660    the desired token is found.  An error message is still produced if
15661    the next token is not as expected.  */
15662
15663 static void
15664 cp_parser_skip_until_found (cp_parser* parser,
15665                             enum cpp_ttype type,
15666                             const char* token_desc)
15667 {
15668   cp_token *token;
15669   unsigned nesting_depth = 0;
15670
15671   if (cp_parser_require (parser, type, token_desc))
15672     return;
15673
15674   /* Skip tokens until the desired token is found.  */
15675   while (true)
15676     {
15677       /* Peek at the next token.  */
15678       token = cp_lexer_peek_token (parser->lexer);
15679       /* If we've reached the token we want, consume it and
15680          stop.  */
15681       if (token->type == type && !nesting_depth)
15682         {
15683           cp_lexer_consume_token (parser->lexer);
15684           return;
15685         }
15686       /* If we've run out of tokens, stop.  */
15687       if (token->type == CPP_EOF)
15688         return;
15689       if (token->type == CPP_OPEN_BRACE
15690           || token->type == CPP_OPEN_PAREN
15691           || token->type == CPP_OPEN_SQUARE)
15692         ++nesting_depth;
15693       else if (token->type == CPP_CLOSE_BRACE
15694                || token->type == CPP_CLOSE_PAREN
15695                || token->type == CPP_CLOSE_SQUARE)
15696         {
15697           if (nesting_depth-- == 0)
15698             return;
15699         }
15700       /* Consume this token.  */
15701       cp_lexer_consume_token (parser->lexer);
15702     }
15703 }
15704
15705 /* If the next token is the indicated keyword, consume it.  Otherwise,
15706    issue an error message indicating that TOKEN_DESC was expected.
15707
15708    Returns the token consumed, if the token had the appropriate type.
15709    Otherwise, returns NULL.  */
15710
15711 static cp_token *
15712 cp_parser_require_keyword (cp_parser* parser,
15713                            enum rid keyword,
15714                            const char* token_desc)
15715 {
15716   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15717
15718   if (token && token->keyword != keyword)
15719     {
15720       dyn_string_t error_msg;
15721
15722       /* Format the error message.  */
15723       error_msg = dyn_string_new (0);
15724       dyn_string_append_cstr (error_msg, "expected ");
15725       dyn_string_append_cstr (error_msg, token_desc);
15726       cp_parser_error (parser, error_msg->s);
15727       dyn_string_delete (error_msg);
15728       return NULL;
15729     }
15730
15731   return token;
15732 }
15733
15734 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15735    function-definition.  */
15736
15737 static bool
15738 cp_parser_token_starts_function_definition_p (cp_token* token)
15739 {
15740   return (/* An ordinary function-body begins with an `{'.  */
15741           token->type == CPP_OPEN_BRACE
15742           /* A ctor-initializer begins with a `:'.  */
15743           || token->type == CPP_COLON
15744           /* A function-try-block begins with `try'.  */
15745           || token->keyword == RID_TRY
15746           /* The named return value extension begins with `return'.  */
15747           || token->keyword == RID_RETURN);
15748 }
15749
15750 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15751    definition.  */
15752
15753 static bool
15754 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15755 {
15756   cp_token *token;
15757
15758   token = cp_lexer_peek_token (parser->lexer);
15759   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15760 }
15761
15762 /* Returns TRUE iff the next token is the "," or ">" ending a
15763    template-argument.  */
15764
15765 static bool
15766 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15767 {
15768   cp_token *token;
15769
15770   token = cp_lexer_peek_token (parser->lexer);
15771   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15772 }
15773
15774 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15775    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15776
15777 static bool
15778 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15779                                                      size_t n)
15780 {
15781   cp_token *token;
15782
15783   token = cp_lexer_peek_nth_token (parser->lexer, n);
15784   if (token->type == CPP_LESS)
15785     return true;
15786   /* Check for the sequence `<::' in the original code. It would be lexed as
15787      `[:', where `[' is a digraph, and there is no whitespace before
15788      `:'.  */
15789   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15790     {
15791       cp_token *token2;
15792       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15793       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15794         return true;
15795     }
15796   return false;
15797 }
15798
15799 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15800    or none_type otherwise.  */
15801
15802 static enum tag_types
15803 cp_parser_token_is_class_key (cp_token* token)
15804 {
15805   switch (token->keyword)
15806     {
15807     case RID_CLASS:
15808       return class_type;
15809     case RID_STRUCT:
15810       return record_type;
15811     case RID_UNION:
15812       return union_type;
15813
15814     default:
15815       return none_type;
15816     }
15817 }
15818
15819 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15820
15821 static void
15822 cp_parser_check_class_key (enum tag_types class_key, tree type)
15823 {
15824   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15825     pedwarn ("%qs tag used in naming %q#T",
15826             class_key == union_type ? "union"
15827              : class_key == record_type ? "struct" : "class",
15828              type);
15829 }
15830
15831 /* Issue an error message if DECL is redeclared with different
15832    access than its original declaration [class.access.spec/3].
15833    This applies to nested classes and nested class templates.
15834    [class.mem/1].  */
15835
15836 static void
15837 cp_parser_check_access_in_redeclaration (tree decl)
15838 {
15839   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15840     return;
15841
15842   if ((TREE_PRIVATE (decl)
15843        != (current_access_specifier == access_private_node))
15844       || (TREE_PROTECTED (decl)
15845           != (current_access_specifier == access_protected_node)))
15846     error ("%qD redeclared with different access", decl);
15847 }
15848
15849 /* Look for the `template' keyword, as a syntactic disambiguator.
15850    Return TRUE iff it is present, in which case it will be
15851    consumed.  */
15852
15853 static bool
15854 cp_parser_optional_template_keyword (cp_parser *parser)
15855 {
15856   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15857     {
15858       /* The `template' keyword can only be used within templates;
15859          outside templates the parser can always figure out what is a
15860          template and what is not.  */
15861       if (!processing_template_decl)
15862         {
15863           error ("%<template%> (as a disambiguator) is only allowed "
15864                  "within templates");
15865           /* If this part of the token stream is rescanned, the same
15866              error message would be generated.  So, we purge the token
15867              from the stream.  */
15868           cp_lexer_purge_token (parser->lexer);
15869           return false;
15870         }
15871       else
15872         {
15873           /* Consume the `template' keyword.  */
15874           cp_lexer_consume_token (parser->lexer);
15875           return true;
15876         }
15877     }
15878
15879   return false;
15880 }
15881
15882 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15883    set PARSER->SCOPE, and perform other related actions.  */
15884
15885 static void
15886 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15887 {
15888   tree value;
15889   tree check;
15890
15891   /* Get the stored value.  */
15892   value = cp_lexer_consume_token (parser->lexer)->value;
15893   /* Perform any access checks that were deferred.  */
15894   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15895     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15896   /* Set the scope from the stored value.  */
15897   parser->scope = TREE_VALUE (value);
15898   parser->qualifying_scope = TREE_TYPE (value);
15899   parser->object_scope = NULL_TREE;
15900 }
15901
15902 /* Consume tokens up through a non-nested END token.  */
15903
15904 static void
15905 cp_parser_cache_group (cp_parser *parser,
15906                        enum cpp_ttype end,
15907                        unsigned depth)
15908 {
15909   while (true)
15910     {
15911       cp_token *token;
15912
15913       /* Abort a parenthesized expression if we encounter a brace.  */
15914       if ((end == CPP_CLOSE_PAREN || depth == 0)
15915           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15916         return;
15917       /* If we've reached the end of the file, stop.  */
15918       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15919         return;
15920       /* Consume the next token.  */
15921       token = cp_lexer_consume_token (parser->lexer);
15922       /* See if it starts a new group.  */
15923       if (token->type == CPP_OPEN_BRACE)
15924         {
15925           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15926           if (depth == 0)
15927             return;
15928         }
15929       else if (token->type == CPP_OPEN_PAREN)
15930         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15931       else if (token->type == end)
15932         return;
15933     }
15934 }
15935
15936 /* Begin parsing tentatively.  We always save tokens while parsing
15937    tentatively so that if the tentative parsing fails we can restore the
15938    tokens.  */
15939
15940 static void
15941 cp_parser_parse_tentatively (cp_parser* parser)
15942 {
15943   /* Enter a new parsing context.  */
15944   parser->context = cp_parser_context_new (parser->context);
15945   /* Begin saving tokens.  */
15946   cp_lexer_save_tokens (parser->lexer);
15947   /* In order to avoid repetitive access control error messages,
15948      access checks are queued up until we are no longer parsing
15949      tentatively.  */
15950   push_deferring_access_checks (dk_deferred);
15951 }
15952
15953 /* Commit to the currently active tentative parse.  */
15954
15955 static void
15956 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15957 {
15958   cp_parser_context *context;
15959   cp_lexer *lexer;
15960
15961   /* Mark all of the levels as committed.  */
15962   lexer = parser->lexer;
15963   for (context = parser->context; context->next; context = context->next)
15964     {
15965       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15966         break;
15967       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15968       while (!cp_lexer_saving_tokens (lexer))
15969         lexer = lexer->next;
15970       cp_lexer_commit_tokens (lexer);
15971     }
15972 }
15973
15974 /* Abort the currently active tentative parse.  All consumed tokens
15975    will be rolled back, and no diagnostics will be issued.  */
15976
15977 static void
15978 cp_parser_abort_tentative_parse (cp_parser* parser)
15979 {
15980   cp_parser_simulate_error (parser);
15981   /* Now, pretend that we want to see if the construct was
15982      successfully parsed.  */
15983   cp_parser_parse_definitely (parser);
15984 }
15985
15986 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15987    token stream.  Otherwise, commit to the tokens we have consumed.
15988    Returns true if no error occurred; false otherwise.  */
15989
15990 static bool
15991 cp_parser_parse_definitely (cp_parser* parser)
15992 {
15993   bool error_occurred;
15994   cp_parser_context *context;
15995
15996   /* Remember whether or not an error occurred, since we are about to
15997      destroy that information.  */
15998   error_occurred = cp_parser_error_occurred (parser);
15999   /* Remove the topmost context from the stack.  */
16000   context = parser->context;
16001   parser->context = context->next;
16002   /* If no parse errors occurred, commit to the tentative parse.  */
16003   if (!error_occurred)
16004     {
16005       /* Commit to the tokens read tentatively, unless that was
16006          already done.  */
16007       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16008         cp_lexer_commit_tokens (parser->lexer);
16009
16010       pop_to_parent_deferring_access_checks ();
16011     }
16012   /* Otherwise, if errors occurred, roll back our state so that things
16013      are just as they were before we began the tentative parse.  */
16014   else
16015     {
16016       cp_lexer_rollback_tokens (parser->lexer);
16017       pop_deferring_access_checks ();
16018     }
16019   /* Add the context to the front of the free list.  */
16020   context->next = cp_parser_context_free_list;
16021   cp_parser_context_free_list = context;
16022
16023   return !error_occurred;
16024 }
16025
16026 /* Returns true if we are parsing tentatively and are not committed to
16027    this tentative parse.  */
16028
16029 static bool
16030 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16031 {
16032   return (cp_parser_parsing_tentatively (parser)
16033           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16034 }
16035
16036 /* Returns nonzero iff an error has occurred during the most recent
16037    tentative parse.  */
16038
16039 static bool
16040 cp_parser_error_occurred (cp_parser* parser)
16041 {
16042   return (cp_parser_parsing_tentatively (parser)
16043           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16044 }
16045
16046 /* Returns nonzero if GNU extensions are allowed.  */
16047
16048 static bool
16049 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16050 {
16051   return parser->allow_gnu_extensions_p;
16052 }
16053
16054 \f
16055 /* The parser.  */
16056
16057 static GTY (()) cp_parser *the_parser;
16058
16059 /* External interface.  */
16060
16061 /* Parse one entire translation unit.  */
16062
16063 void
16064 c_parse_file (void)
16065 {
16066   bool error_occurred;
16067   static bool already_called = false;
16068
16069   if (already_called)
16070     {
16071       sorry ("inter-module optimizations not implemented for C++");
16072       return;
16073     }
16074   already_called = true;
16075
16076   the_parser = cp_parser_new ();
16077   push_deferring_access_checks (flag_access_control
16078                                 ? dk_no_deferred : dk_no_check);
16079   error_occurred = cp_parser_translation_unit (the_parser);
16080   the_parser = NULL;
16081 }
16082
16083 /* This variable must be provided by every front end.  */
16084
16085 int yydebug;
16086
16087 #include "gt-cp-parser.h"