OSDN Git Service

2005-12-22 Volker Reichelt <reichelt@igpm.rwth-aachen.de>
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING.  If not, write to the Free
20    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, 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 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A C++ token.  */
48
49 typedef struct cp_token GTY (())
50 {
51   /* The kind of token.  */
52   ENUM_BITFIELD (cpp_ttype) type : 8;
53   /* If this token is a keyword, this value indicates which keyword.
54      Otherwise, this value is RID_MAX.  */
55   ENUM_BITFIELD (rid) keyword : 8;
56   /* Token flags.  */
57   unsigned char flags;
58   /* True if this token is from a system header.  */
59   BOOL_BITFIELD in_system_header : 1;
60   /* True if this token is from a context where it is implicitly extern "C" */
61   BOOL_BITFIELD implicit_extern_c : 1;
62   /* True for a CPP_NAME token that is not a keyword (i.e., for which
63      KEYWORD is RID_MAX) iff this name was looked up and found to be
64      ambiguous.  An error has already been reported.  */
65   BOOL_BITFIELD ambiguous_p : 1;
66   /* The value associated with this token, if any.  */
67   tree value;
68   /* The location at which this token was found.  */
69   location_t location;
70 } cp_token;
71
72 /* We use a stack of token pointer for saving token sets.  */
73 typedef struct cp_token *cp_token_position;
74 DEF_VEC_P (cp_token_position);
75 DEF_VEC_ALLOC_P (cp_token_position,heap);
76
77 static const cp_token eof_token =
78 {
79   CPP_EOF, RID_MAX, 0, 0, 0, false, NULL_TREE,
80 #if USE_MAPPED_LOCATION
81   0
82 #else
83   {0, 0}
84 #endif
85 };
86
87 /* The cp_lexer structure represents the C++ lexer.  It is responsible
88    for managing the token stream from the preprocessor and supplying
89    it to the parser.  Tokens are never added to the cp_lexer after
90    it is created.  */
91
92 typedef struct cp_lexer GTY (())
93 {
94   /* The memory allocated for the buffer.  NULL if this lexer does not
95      own the token buffer.  */
96   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
97   /* If the lexer owns the buffer, this is the number of tokens in the
98      buffer.  */
99   size_t buffer_length;
100
101   /* A pointer just past the last available token.  The tokens
102      in this lexer are [buffer, last_token).  */
103   cp_token_position GTY ((skip)) last_token;
104
105   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
106      no more available tokens.  */
107   cp_token_position GTY ((skip)) next_token;
108
109   /* A stack indicating positions at which cp_lexer_save_tokens was
110      called.  The top entry is the most recent position at which we
111      began saving tokens.  If the stack is non-empty, we are saving
112      tokens.  */
113   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
114
115   /* True if we should output debugging information.  */
116   bool debugging_p;
117
118   /* The next lexer in a linked list of lexers.  */
119   struct cp_lexer *next;
120 } cp_lexer;
121
122 /* cp_token_cache is a range of tokens.  There is no need to represent
123    allocate heap memory for it, since tokens are never removed from the
124    lexer's array.  There is also no need for the GC to walk through
125    a cp_token_cache, since everything in here is referenced through
126    a lexer.  */
127
128 typedef struct cp_token_cache GTY(())
129 {
130   /* The beginning of the token range.  */
131   cp_token * GTY((skip)) first;
132
133   /* Points immediately after the last token in the range.  */
134   cp_token * GTY ((skip)) last;
135 } cp_token_cache;
136
137 /* Prototypes.  */
138
139 static cp_lexer *cp_lexer_new_main
140   (void);
141 static cp_lexer *cp_lexer_new_from_tokens
142   (cp_token_cache *tokens);
143 static void cp_lexer_destroy
144   (cp_lexer *);
145 static int cp_lexer_saving_tokens
146   (const cp_lexer *);
147 static cp_token_position cp_lexer_token_position
148   (cp_lexer *, bool);
149 static cp_token *cp_lexer_token_at
150   (cp_lexer *, cp_token_position);
151 static void cp_lexer_get_preprocessor_token
152   (cp_lexer *, cp_token *);
153 static inline cp_token *cp_lexer_peek_token
154   (cp_lexer *);
155 static cp_token *cp_lexer_peek_nth_token
156   (cp_lexer *, size_t);
157 static inline bool cp_lexer_next_token_is
158   (cp_lexer *, enum cpp_ttype);
159 static bool cp_lexer_next_token_is_not
160   (cp_lexer *, enum cpp_ttype);
161 static bool cp_lexer_next_token_is_keyword
162   (cp_lexer *, enum rid);
163 static cp_token *cp_lexer_consume_token
164   (cp_lexer *);
165 static void cp_lexer_purge_token
166   (cp_lexer *);
167 static void cp_lexer_purge_tokens_after
168   (cp_lexer *, cp_token_position);
169 static void cp_lexer_handle_pragma
170   (cp_lexer *);
171 static void cp_lexer_save_tokens
172   (cp_lexer *);
173 static void cp_lexer_commit_tokens
174   (cp_lexer *);
175 static void cp_lexer_rollback_tokens
176   (cp_lexer *);
177 #ifdef ENABLE_CHECKING
178 static void cp_lexer_print_token
179   (FILE *, cp_token *);
180 static inline bool cp_lexer_debugging_p
181   (cp_lexer *);
182 static void cp_lexer_start_debugging
183   (cp_lexer *) ATTRIBUTE_UNUSED;
184 static void cp_lexer_stop_debugging
185   (cp_lexer *) ATTRIBUTE_UNUSED;
186 #else
187 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
188    about passing NULL to functions that require non-NULL arguments
189    (fputs, fprintf).  It will never be used, so all we need is a value
190    of the right type that's guaranteed not to be NULL.  */
191 #define cp_lexer_debug_stream stdout
192 #define cp_lexer_print_token(str, tok) (void) 0
193 #define cp_lexer_debugging_p(lexer) 0
194 #endif /* ENABLE_CHECKING */
195
196 static cp_token_cache *cp_token_cache_new
197   (cp_token *, cp_token *);
198
199 /* Manifest constants.  */
200 #define CP_LEXER_BUFFER_SIZE 10000
201 #define CP_SAVED_TOKEN_STACK 5
202
203 /* A token type for keywords, as opposed to ordinary identifiers.  */
204 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
205
206 /* A token type for template-ids.  If a template-id is processed while
207    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
208    the value of the CPP_TEMPLATE_ID is whatever was returned by
209    cp_parser_template_id.  */
210 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
211
212 /* A token type for nested-name-specifiers.  If a
213    nested-name-specifier is processed while parsing tentatively, it is
214    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
215    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
216    cp_parser_nested_name_specifier_opt.  */
217 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
218
219 /* A token type for tokens that are not tokens at all; these are used
220    to represent slots in the array where there used to be a token
221    that has now been deleted.  */
222 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
223
224 /* The number of token types, including C++-specific ones.  */
225 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
226
227 /* Variables.  */
228
229 #ifdef ENABLE_CHECKING
230 /* The stream to which debugging output should be written.  */
231 static FILE *cp_lexer_debug_stream;
232 #endif /* ENABLE_CHECKING */
233
234 /* Create a new main C++ lexer, the lexer that gets tokens from the
235    preprocessor.  */
236
237 static cp_lexer *
238 cp_lexer_new_main (void)
239 {
240   cp_token first_token;
241   cp_lexer *lexer;
242   cp_token *pos;
243   size_t alloc;
244   size_t space;
245   cp_token *buffer;
246
247   /* It's possible that lexing the first token will load a PCH file,
248      which is a GC collection point.  So we have to grab the first
249      token before allocating any memory.  Pragmas must not be deferred
250      as -fpch-preprocess can generate a pragma to load the PCH file in
251      the preprocessed output used by -save-temps.  */
252   cp_lexer_get_preprocessor_token (NULL, &first_token);
253
254   /* Tell cpplib we want CPP_PRAGMA tokens.  */
255   cpp_get_options (parse_in)->defer_pragmas = true;
256
257   /* Tell pragma_lex not to merge string constants.  */
258   c_lex_return_raw_strings = true;
259
260   c_common_no_more_pch ();
261
262   /* Allocate the memory.  */
263   lexer = GGC_CNEW (cp_lexer);
264
265 #ifdef ENABLE_CHECKING
266   /* Initially we are not debugging.  */
267   lexer->debugging_p = false;
268 #endif /* ENABLE_CHECKING */
269   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
270                                    CP_SAVED_TOKEN_STACK);
271
272   /* Create the buffer.  */
273   alloc = CP_LEXER_BUFFER_SIZE;
274   buffer = GGC_NEWVEC (cp_token, alloc);
275
276   /* Put the first token in the buffer.  */
277   space = alloc;
278   pos = buffer;
279   *pos = first_token;
280
281   /* Get the remaining tokens from the preprocessor.  */
282   while (pos->type != CPP_EOF)
283     {
284       pos++;
285       if (!--space)
286         {
287           space = alloc;
288           alloc *= 2;
289           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
290           pos = buffer + space;
291         }
292       cp_lexer_get_preprocessor_token (lexer, pos);
293     }
294   lexer->buffer = buffer;
295   lexer->buffer_length = alloc - space;
296   lexer->last_token = pos;
297   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
298
299   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
300      direct calls to pragma_lex.  Those callers all expect pragma_lex
301      to do string constant concatenation.  */
302   c_lex_return_raw_strings = false;
303
304   /* Subsequent preprocessor diagnostics should use compiler
305      diagnostic functions to get the compiler source location.  */
306   cpp_get_options (parse_in)->client_diagnostic = true;
307   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
308
309   gcc_assert (lexer->next_token->type != CPP_PURGED);
310   return lexer;
311 }
312
313 /* Create a new lexer whose token stream is primed with the tokens in
314    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
315
316 static cp_lexer *
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
318 {
319   cp_token *first = cache->first;
320   cp_token *last = cache->last;
321   cp_lexer *lexer = GGC_CNEW (cp_lexer);
322
323   /* We do not own the buffer.  */
324   lexer->buffer = NULL;
325   lexer->buffer_length = 0;
326   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327   lexer->last_token = last;
328
329   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330                                    CP_SAVED_TOKEN_STACK);
331
332 #ifdef ENABLE_CHECKING
333   /* Initially we are not debugging.  */
334   lexer->debugging_p = false;
335 #endif
336
337   gcc_assert (lexer->next_token->type != CPP_PURGED);
338   return lexer;
339 }
340
341 /* Frees all resources associated with LEXER.  */
342
343 static void
344 cp_lexer_destroy (cp_lexer *lexer)
345 {
346   if (lexer->buffer)
347     ggc_free (lexer->buffer);
348   VEC_free (cp_token_position, heap, lexer->saved_tokens);
349   ggc_free (lexer);
350 }
351
352 /* Returns nonzero if debugging information should be output.  */
353
354 #ifdef ENABLE_CHECKING
355
356 static inline bool
357 cp_lexer_debugging_p (cp_lexer *lexer)
358 {
359   return lexer->debugging_p;
360 }
361
362 #endif /* ENABLE_CHECKING */
363
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
366 {
367   gcc_assert (!previous_p || lexer->next_token != &eof_token);
368
369   return lexer->next_token - previous_p;
370 }
371
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
374 {
375   return pos;
376 }
377
378 /* nonzero if we are presently saving tokens.  */
379
380 static inline int
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
382 {
383   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 }
385
386 /* Store the next token from the preprocessor in *TOKEN.  Return true
387    if we reach EOF.  */
388
389 static void
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391                                  cp_token *token)
392 {
393   static int is_extern_c = 0;
394
395    /* Get a new token from the preprocessor.  */
396   token->type
397     = c_lex_with_flags (&token->value, &token->location, &token->flags);
398   token->in_system_header = in_system_header;
399
400   /* On some systems, some header files are surrounded by an
401      implicit extern "C" block.  Set a flag in the token if it
402      comes from such a header.  */
403   is_extern_c += pending_lang_change;
404   pending_lang_change = 0;
405   token->implicit_extern_c = is_extern_c > 0;
406
407   /* Check to see if this token is a keyword.  */
408   if (token->type == CPP_NAME)
409     {
410       if (C_IS_RESERVED_WORD (token->value))
411         {
412           /* Mark this token as a keyword.  */
413           token->type = CPP_KEYWORD;
414           /* Record which keyword.  */
415           token->keyword = C_RID_CODE (token->value);
416           /* Update the value.  Some keywords are mapped to particular
417              entities, rather than simply having the value of the
418              corresponding IDENTIFIER_NODE.  For example, `__const' is
419              mapped to `const'.  */
420           token->value = ridpointers[token->keyword];
421         }
422       else
423         {
424           token->ambiguous_p = false;
425           token->keyword = RID_MAX;
426         }
427     }
428   /* Handle Objective-C++ keywords.  */
429   else if (token->type == CPP_AT_NAME)
430     {
431       token->type = CPP_KEYWORD;
432       switch (C_RID_CODE (token->value))
433         {
434         /* Map 'class' to '@class', 'private' to '@private', etc.  */
435         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439         case RID_THROW: token->keyword = RID_AT_THROW; break;
440         case RID_TRY: token->keyword = RID_AT_TRY; break;
441         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442         default: token->keyword = C_RID_CODE (token->value);
443         }
444     }
445   else
446     token->keyword = RID_MAX;
447 }
448
449 /* Update the globals input_location and in_system_header from TOKEN.  */
450 static inline void
451 cp_lexer_set_source_position_from_token (cp_token *token)
452 {
453   if (token->type != CPP_EOF)
454     {
455       input_location = token->location;
456       in_system_header = token->in_system_header;
457     }
458 }
459
460 /* Return a pointer to the next token in the token stream, but do not
461    consume it.  */
462
463 static inline cp_token *
464 cp_lexer_peek_token (cp_lexer *lexer)
465 {
466   if (cp_lexer_debugging_p (lexer))
467     {
468       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
469       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
470       putc ('\n', cp_lexer_debug_stream);
471     }
472   return lexer->next_token;
473 }
474
475 /* Return true if the next token has the indicated TYPE.  */
476
477 static inline bool
478 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
479 {
480   return cp_lexer_peek_token (lexer)->type == type;
481 }
482
483 /* Return true if the next token does not have the indicated TYPE.  */
484
485 static inline bool
486 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
487 {
488   return !cp_lexer_next_token_is (lexer, type);
489 }
490
491 /* Return true if the next token is the indicated KEYWORD.  */
492
493 static inline bool
494 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
495 {
496   cp_token *token;
497
498   /* Peek at the next token.  */
499   token = cp_lexer_peek_token (lexer);
500   /* Check to see if it is the indicated keyword.  */
501   return token->keyword == keyword;
502 }
503
504 /* Return a pointer to the Nth token in the token stream.  If N is 1,
505    then this is precisely equivalent to cp_lexer_peek_token (except
506    that it is not inline).  One would like to disallow that case, but
507    there is one case (cp_parser_nth_token_starts_template_id) where
508    the caller passes a variable for N and it might be 1.  */
509
510 static cp_token *
511 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
512 {
513   cp_token *token;
514
515   /* N is 1-based, not zero-based.  */
516   gcc_assert (n > 0);
517   
518   if (cp_lexer_debugging_p (lexer))
519     fprintf (cp_lexer_debug_stream,
520              "cp_lexer: peeking ahead %ld at token: ", (long)n);
521
522   --n;
523   token = lexer->next_token;
524   gcc_assert (!n || token != &eof_token);
525   while (n != 0)
526     {
527       ++token;
528       if (token == lexer->last_token)
529         {
530           token = (cp_token *)&eof_token;
531           break;
532         }
533
534       if (token->type != CPP_PURGED)
535         --n;
536     }
537
538   if (cp_lexer_debugging_p (lexer))
539     {
540       cp_lexer_print_token (cp_lexer_debug_stream, token);
541       putc ('\n', cp_lexer_debug_stream);
542     }
543
544   return token;
545 }
546
547 /* Return the next token, and advance the lexer's next_token pointer
548    to point to the next non-purged token.  */
549
550 static cp_token *
551 cp_lexer_consume_token (cp_lexer* lexer)
552 {
553   cp_token *token = lexer->next_token;
554
555   gcc_assert (token != &eof_token);
556
557   do
558     {
559       lexer->next_token++;
560       if (lexer->next_token == lexer->last_token)
561         {
562           lexer->next_token = (cp_token *)&eof_token;
563           break;
564         }
565
566     }
567   while (lexer->next_token->type == CPP_PURGED);
568
569   cp_lexer_set_source_position_from_token (token);
570
571   /* Provide debugging output.  */
572   if (cp_lexer_debugging_p (lexer))
573     {
574       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575       cp_lexer_print_token (cp_lexer_debug_stream, token);
576       putc ('\n', cp_lexer_debug_stream);
577     }
578
579   return token;
580 }
581
582 /* Permanently remove the next token from the token stream, and
583    advance the next_token pointer to refer to the next non-purged
584    token.  */
585
586 static void
587 cp_lexer_purge_token (cp_lexer *lexer)
588 {
589   cp_token *tok = lexer->next_token;
590
591   gcc_assert (tok != &eof_token);
592   tok->type = CPP_PURGED;
593   tok->location = UNKNOWN_LOCATION;
594   tok->value = NULL_TREE;
595   tok->keyword = RID_MAX;
596
597   do
598     {
599       tok++;
600       if (tok == lexer->last_token)
601         {
602           tok = (cp_token *)&eof_token;
603           break;
604         }
605     }
606   while (tok->type == CPP_PURGED);
607   lexer->next_token = tok;
608 }
609
610 /* Permanently remove all tokens after TOK, up to, but not
611    including, the token that will be returned next by
612    cp_lexer_peek_token.  */
613
614 static void
615 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
616 {
617   cp_token *peek = lexer->next_token;
618
619   if (peek == &eof_token)
620     peek = lexer->last_token;
621
622   gcc_assert (tok < peek);
623
624   for ( tok += 1; tok != peek; tok += 1)
625     {
626       tok->type = CPP_PURGED;
627       tok->location = UNKNOWN_LOCATION;
628       tok->value = NULL_TREE;
629       tok->keyword = RID_MAX;
630     }
631 }
632
633 /* Consume and handle a pragma token.  */
634 static void
635 cp_lexer_handle_pragma (cp_lexer *lexer)
636 {
637   cpp_string s;
638   cp_token *token = cp_lexer_consume_token (lexer);
639   gcc_assert (token->type == CPP_PRAGMA);
640   gcc_assert (token->value);
641
642   s.len = TREE_STRING_LENGTH (token->value);
643   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
644
645   cpp_handle_deferred_pragma (parse_in, &s);
646
647   /* Clearing token->value here means that we will get an ICE if we
648      try to process this #pragma again (which should be impossible).  */
649   token->value = NULL;
650 }
651
652 /* Begin saving tokens.  All tokens consumed after this point will be
653    preserved.  */
654
655 static void
656 cp_lexer_save_tokens (cp_lexer* lexer)
657 {
658   /* Provide debugging output.  */
659   if (cp_lexer_debugging_p (lexer))
660     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
661
662   VEC_safe_push (cp_token_position, heap,
663                  lexer->saved_tokens, lexer->next_token);
664 }
665
666 /* Commit to the portion of the token stream most recently saved.  */
667
668 static void
669 cp_lexer_commit_tokens (cp_lexer* lexer)
670 {
671   /* Provide debugging output.  */
672   if (cp_lexer_debugging_p (lexer))
673     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
674
675   VEC_pop (cp_token_position, lexer->saved_tokens);
676 }
677
678 /* Return all tokens saved since the last call to cp_lexer_save_tokens
679    to the token stream.  Stop saving tokens.  */
680
681 static void
682 cp_lexer_rollback_tokens (cp_lexer* lexer)
683 {
684   /* Provide debugging output.  */
685   if (cp_lexer_debugging_p (lexer))
686     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
687
688   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
689 }
690
691 /* Print a representation of the TOKEN on the STREAM.  */
692
693 #ifdef ENABLE_CHECKING
694
695 static void
696 cp_lexer_print_token (FILE * stream, cp_token *token)
697 {
698   /* We don't use cpp_type2name here because the parser defines
699      a few tokens of its own.  */
700   static const char *const token_names[] = {
701     /* cpplib-defined token types */
702 #define OP(e, s) #e,
703 #define TK(e, s) #e,
704     TTYPE_TABLE
705 #undef OP
706 #undef TK
707     /* C++ parser token types - see "Manifest constants", above.  */
708     "KEYWORD",
709     "TEMPLATE_ID",
710     "NESTED_NAME_SPECIFIER",
711     "PURGED"
712   };
713
714   /* If we have a name for the token, print it out.  Otherwise, we
715      simply give the numeric code.  */
716   gcc_assert (token->type < ARRAY_SIZE(token_names));
717   fputs (token_names[token->type], stream);
718
719   /* For some tokens, print the associated data.  */
720   switch (token->type)
721     {
722     case CPP_KEYWORD:
723       /* Some keywords have a value that is not an IDENTIFIER_NODE.
724          For example, `struct' is mapped to an INTEGER_CST.  */
725       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
726         break;
727       /* else fall through */
728     case CPP_NAME:
729       fputs (IDENTIFIER_POINTER (token->value), stream);
730       break;
731
732     case CPP_STRING:
733     case CPP_WSTRING:
734     case CPP_PRAGMA:
735       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
736       break;
737
738     default:
739       break;
740     }
741 }
742
743 /* Start emitting debugging information.  */
744
745 static void
746 cp_lexer_start_debugging (cp_lexer* lexer)
747 {
748   lexer->debugging_p = true;
749 }
750
751 /* Stop emitting debugging information.  */
752
753 static void
754 cp_lexer_stop_debugging (cp_lexer* lexer)
755 {
756   lexer->debugging_p = false;
757 }
758
759 #endif /* ENABLE_CHECKING */
760
761 /* Create a new cp_token_cache, representing a range of tokens.  */
762
763 static cp_token_cache *
764 cp_token_cache_new (cp_token *first, cp_token *last)
765 {
766   cp_token_cache *cache = GGC_NEW (cp_token_cache);
767   cache->first = first;
768   cache->last = last;
769   return cache;
770 }
771
772 \f
773 /* Decl-specifiers.  */
774
775 static void clear_decl_specs
776   (cp_decl_specifier_seq *);
777
778 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
779
780 static void
781 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
782 {
783   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
784 }
785
786 /* Declarators.  */
787
788 /* Nothing other than the parser should be creating declarators;
789    declarators are a semi-syntactic representation of C++ entities.
790    Other parts of the front end that need to create entities (like
791    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
792
793 static cp_declarator *make_call_declarator
794   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
795 static cp_declarator *make_array_declarator
796   (cp_declarator *, tree);
797 static cp_declarator *make_pointer_declarator
798   (cp_cv_quals, cp_declarator *);
799 static cp_declarator *make_reference_declarator
800   (cp_cv_quals, cp_declarator *);
801 static cp_parameter_declarator *make_parameter_declarator
802   (cp_decl_specifier_seq *, cp_declarator *, tree);
803 static cp_declarator *make_ptrmem_declarator
804   (cp_cv_quals, tree, cp_declarator *);
805
806 cp_declarator *cp_error_declarator;
807
808 /* The obstack on which declarators and related data structures are
809    allocated.  */
810 static struct obstack declarator_obstack;
811
812 /* Alloc BYTES from the declarator memory pool.  */
813
814 static inline void *
815 alloc_declarator (size_t bytes)
816 {
817   return obstack_alloc (&declarator_obstack, bytes);
818 }
819
820 /* Allocate a declarator of the indicated KIND.  Clear fields that are
821    common to all declarators.  */
822
823 static cp_declarator *
824 make_declarator (cp_declarator_kind kind)
825 {
826   cp_declarator *declarator;
827
828   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
829   declarator->kind = kind;
830   declarator->attributes = NULL_TREE;
831   declarator->declarator = NULL;
832
833   return declarator;
834 }
835
836 /* Make a declarator for a generalized identifier.  If non-NULL, the
837    identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
838    just UNQUALIFIED_NAME.  */
839
840 static cp_declarator *
841 make_id_declarator (tree qualifying_scope, tree unqualified_name)
842 {
843   cp_declarator *declarator;
844
845   /* It is valid to write:
846
847        class C { void f(); };
848        typedef C D;
849        void D::f();
850
851      The standard is not clear about whether `typedef const C D' is
852      legal; as of 2002-09-15 the committee is considering that
853      question.  EDG 3.0 allows that syntax.  Therefore, we do as
854      well.  */
855   if (qualifying_scope && TYPE_P (qualifying_scope))
856     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
857
858   declarator = make_declarator (cdk_id);
859   declarator->u.id.qualifying_scope = qualifying_scope;
860   declarator->u.id.unqualified_name = unqualified_name;
861   declarator->u.id.sfk = sfk_none;
862
863   return declarator;
864 }
865
866 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
867    of modifiers such as const or volatile to apply to the pointer
868    type, represented as identifiers.  */
869
870 cp_declarator *
871 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
872 {
873   cp_declarator *declarator;
874
875   declarator = make_declarator (cdk_pointer);
876   declarator->declarator = target;
877   declarator->u.pointer.qualifiers = cv_qualifiers;
878   declarator->u.pointer.class_type = NULL_TREE;
879
880   return declarator;
881 }
882
883 /* Like make_pointer_declarator -- but for references.  */
884
885 cp_declarator *
886 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
887 {
888   cp_declarator *declarator;
889
890   declarator = make_declarator (cdk_reference);
891   declarator->declarator = target;
892   declarator->u.pointer.qualifiers = cv_qualifiers;
893   declarator->u.pointer.class_type = NULL_TREE;
894
895   return declarator;
896 }
897
898 /* Like make_pointer_declarator -- but for a pointer to a non-static
899    member of CLASS_TYPE.  */
900
901 cp_declarator *
902 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
903                         cp_declarator *pointee)
904 {
905   cp_declarator *declarator;
906
907   declarator = make_declarator (cdk_ptrmem);
908   declarator->declarator = pointee;
909   declarator->u.pointer.qualifiers = cv_qualifiers;
910   declarator->u.pointer.class_type = class_type;
911
912   return declarator;
913 }
914
915 /* Make a declarator for the function given by TARGET, with the
916    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
917    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
918    indicates what exceptions can be thrown.  */
919
920 cp_declarator *
921 make_call_declarator (cp_declarator *target,
922                       cp_parameter_declarator *parms,
923                       cp_cv_quals cv_qualifiers,
924                       tree exception_specification)
925 {
926   cp_declarator *declarator;
927
928   declarator = make_declarator (cdk_function);
929   declarator->declarator = target;
930   declarator->u.function.parameters = parms;
931   declarator->u.function.qualifiers = cv_qualifiers;
932   declarator->u.function.exception_specification = exception_specification;
933
934   return declarator;
935 }
936
937 /* Make a declarator for an array of BOUNDS elements, each of which is
938    defined by ELEMENT.  */
939
940 cp_declarator *
941 make_array_declarator (cp_declarator *element, tree bounds)
942 {
943   cp_declarator *declarator;
944
945   declarator = make_declarator (cdk_array);
946   declarator->declarator = element;
947   declarator->u.array.bounds = bounds;
948
949   return declarator;
950 }
951
952 cp_parameter_declarator *no_parameters;
953
954 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
955    DECLARATOR and DEFAULT_ARGUMENT.  */
956
957 cp_parameter_declarator *
958 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
959                            cp_declarator *declarator,
960                            tree default_argument)
961 {
962   cp_parameter_declarator *parameter;
963
964   parameter = ((cp_parameter_declarator *)
965                alloc_declarator (sizeof (cp_parameter_declarator)));
966   parameter->next = NULL;
967   if (decl_specifiers)
968     parameter->decl_specifiers = *decl_specifiers;
969   else
970     clear_decl_specs (&parameter->decl_specifiers);
971   parameter->declarator = declarator;
972   parameter->default_argument = default_argument;
973   parameter->ellipsis_p = false;
974
975   return parameter;
976 }
977
978 /* The parser.  */
979
980 /* Overview
981    --------
982
983    A cp_parser parses the token stream as specified by the C++
984    grammar.  Its job is purely parsing, not semantic analysis.  For
985    example, the parser breaks the token stream into declarators,
986    expressions, statements, and other similar syntactic constructs.
987    It does not check that the types of the expressions on either side
988    of an assignment-statement are compatible, or that a function is
989    not declared with a parameter of type `void'.
990
991    The parser invokes routines elsewhere in the compiler to perform
992    semantic analysis and to build up the abstract syntax tree for the
993    code processed.
994
995    The parser (and the template instantiation code, which is, in a
996    way, a close relative of parsing) are the only parts of the
997    compiler that should be calling push_scope and pop_scope, or
998    related functions.  The parser (and template instantiation code)
999    keeps track of what scope is presently active; everything else
1000    should simply honor that.  (The code that generates static
1001    initializers may also need to set the scope, in order to check
1002    access control correctly when emitting the initializers.)
1003
1004    Methodology
1005    -----------
1006
1007    The parser is of the standard recursive-descent variety.  Upcoming
1008    tokens in the token stream are examined in order to determine which
1009    production to use when parsing a non-terminal.  Some C++ constructs
1010    require arbitrary look ahead to disambiguate.  For example, it is
1011    impossible, in the general case, to tell whether a statement is an
1012    expression or declaration without scanning the entire statement.
1013    Therefore, the parser is capable of "parsing tentatively."  When the
1014    parser is not sure what construct comes next, it enters this mode.
1015    Then, while we attempt to parse the construct, the parser queues up
1016    error messages, rather than issuing them immediately, and saves the
1017    tokens it consumes.  If the construct is parsed successfully, the
1018    parser "commits", i.e., it issues any queued error messages and
1019    the tokens that were being preserved are permanently discarded.
1020    If, however, the construct is not parsed successfully, the parser
1021    rolls back its state completely so that it can resume parsing using
1022    a different alternative.
1023
1024    Future Improvements
1025    -------------------
1026
1027    The performance of the parser could probably be improved substantially.
1028    We could often eliminate the need to parse tentatively by looking ahead
1029    a little bit.  In some places, this approach might not entirely eliminate
1030    the need to parse tentatively, but it might still speed up the average
1031    case.  */
1032
1033 /* Flags that are passed to some parsing functions.  These values can
1034    be bitwise-ored together.  */
1035
1036 typedef enum cp_parser_flags
1037 {
1038   /* No flags.  */
1039   CP_PARSER_FLAGS_NONE = 0x0,
1040   /* The construct is optional.  If it is not present, then no error
1041      should be issued.  */
1042   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1043   /* When parsing a type-specifier, do not allow user-defined types.  */
1044   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1045 } cp_parser_flags;
1046
1047 /* The different kinds of declarators we want to parse.  */
1048
1049 typedef enum cp_parser_declarator_kind
1050 {
1051   /* We want an abstract declarator.  */
1052   CP_PARSER_DECLARATOR_ABSTRACT,
1053   /* We want a named declarator.  */
1054   CP_PARSER_DECLARATOR_NAMED,
1055   /* We don't mind, but the name must be an unqualified-id.  */
1056   CP_PARSER_DECLARATOR_EITHER
1057 } cp_parser_declarator_kind;
1058
1059 /* The precedence values used to parse binary expressions.  The minimum value
1060    of PREC must be 1, because zero is reserved to quickly discriminate
1061    binary operators from other tokens.  */
1062
1063 enum cp_parser_prec
1064 {
1065   PREC_NOT_OPERATOR,
1066   PREC_LOGICAL_OR_EXPRESSION,
1067   PREC_LOGICAL_AND_EXPRESSION,
1068   PREC_INCLUSIVE_OR_EXPRESSION,
1069   PREC_EXCLUSIVE_OR_EXPRESSION,
1070   PREC_AND_EXPRESSION,
1071   PREC_EQUALITY_EXPRESSION,
1072   PREC_RELATIONAL_EXPRESSION,
1073   PREC_SHIFT_EXPRESSION,
1074   PREC_ADDITIVE_EXPRESSION,
1075   PREC_MULTIPLICATIVE_EXPRESSION,
1076   PREC_PM_EXPRESSION,
1077   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1078 };
1079
1080 /* A mapping from a token type to a corresponding tree node type, with a
1081    precedence value.  */
1082
1083 typedef struct cp_parser_binary_operations_map_node
1084 {
1085   /* The token type.  */
1086   enum cpp_ttype token_type;
1087   /* The corresponding tree code.  */
1088   enum tree_code tree_type;
1089   /* The precedence of this operator.  */
1090   enum cp_parser_prec prec;
1091 } cp_parser_binary_operations_map_node;
1092
1093 /* The status of a tentative parse.  */
1094
1095 typedef enum cp_parser_status_kind
1096 {
1097   /* No errors have occurred.  */
1098   CP_PARSER_STATUS_KIND_NO_ERROR,
1099   /* An error has occurred.  */
1100   CP_PARSER_STATUS_KIND_ERROR,
1101   /* We are committed to this tentative parse, whether or not an error
1102      has occurred.  */
1103   CP_PARSER_STATUS_KIND_COMMITTED
1104 } cp_parser_status_kind;
1105
1106 typedef struct cp_parser_expression_stack_entry
1107 {
1108   tree lhs;
1109   enum tree_code tree_type;
1110   int prec;
1111 } cp_parser_expression_stack_entry;
1112
1113 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1114    entries because precedence levels on the stack are monotonically
1115    increasing.  */
1116 typedef struct cp_parser_expression_stack_entry
1117   cp_parser_expression_stack[NUM_PREC_VALUES];
1118
1119 /* Context that is saved and restored when parsing tentatively.  */
1120 typedef struct cp_parser_context GTY (())
1121 {
1122   /* If this is a tentative parsing context, the status of the
1123      tentative parse.  */
1124   enum cp_parser_status_kind status;
1125   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1126      that are looked up in this context must be looked up both in the
1127      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1128      the context of the containing expression.  */
1129   tree object_type;
1130
1131   /* The next parsing context in the stack.  */
1132   struct cp_parser_context *next;
1133 } cp_parser_context;
1134
1135 /* Prototypes.  */
1136
1137 /* Constructors and destructors.  */
1138
1139 static cp_parser_context *cp_parser_context_new
1140   (cp_parser_context *);
1141
1142 /* Class variables.  */
1143
1144 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1145
1146 /* The operator-precedence table used by cp_parser_binary_expression.
1147    Transformed into an associative array (binops_by_token) by
1148    cp_parser_new.  */
1149
1150 static const cp_parser_binary_operations_map_node binops[] = {
1151   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1152   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1153
1154   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1155   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1156   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1157
1158   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1159   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1160
1161   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1162   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1163
1164   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1165   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1166   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1167   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1168   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1169   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1170
1171   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1172   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1173
1174   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1175
1176   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1177
1178   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1179
1180   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1181
1182   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1183 };
1184
1185 /* The same as binops, but initialized by cp_parser_new so that
1186    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1187    for speed.  */
1188 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1189
1190 /* Constructors and destructors.  */
1191
1192 /* Construct a new context.  The context below this one on the stack
1193    is given by NEXT.  */
1194
1195 static cp_parser_context *
1196 cp_parser_context_new (cp_parser_context* next)
1197 {
1198   cp_parser_context *context;
1199
1200   /* Allocate the storage.  */
1201   if (cp_parser_context_free_list != NULL)
1202     {
1203       /* Pull the first entry from the free list.  */
1204       context = cp_parser_context_free_list;
1205       cp_parser_context_free_list = context->next;
1206       memset (context, 0, sizeof (*context));
1207     }
1208   else
1209     context = GGC_CNEW (cp_parser_context);
1210
1211   /* No errors have occurred yet in this context.  */
1212   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1213   /* If this is not the bottomost context, copy information that we
1214      need from the previous context.  */
1215   if (next)
1216     {
1217       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1218          expression, then we are parsing one in this context, too.  */
1219       context->object_type = next->object_type;
1220       /* Thread the stack.  */
1221       context->next = next;
1222     }
1223
1224   return context;
1225 }
1226
1227 /* The cp_parser structure represents the C++ parser.  */
1228
1229 typedef struct cp_parser GTY(())
1230 {
1231   /* The lexer from which we are obtaining tokens.  */
1232   cp_lexer *lexer;
1233
1234   /* The scope in which names should be looked up.  If NULL_TREE, then
1235      we look up names in the scope that is currently open in the
1236      source program.  If non-NULL, this is either a TYPE or
1237      NAMESPACE_DECL for the scope in which we should look.  It can
1238      also be ERROR_MARK, when we've parsed a bogus scope.
1239
1240      This value is not cleared automatically after a name is looked
1241      up, so we must be careful to clear it before starting a new look
1242      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1243      will look up `Z' in the scope of `X', rather than the current
1244      scope.)  Unfortunately, it is difficult to tell when name lookup
1245      is complete, because we sometimes peek at a token, look it up,
1246      and then decide not to consume it.   */
1247   tree scope;
1248
1249   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1250      last lookup took place.  OBJECT_SCOPE is used if an expression
1251      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1252      respectively.  QUALIFYING_SCOPE is used for an expression of the
1253      form "X::Y"; it refers to X.  */
1254   tree object_scope;
1255   tree qualifying_scope;
1256
1257   /* A stack of parsing contexts.  All but the bottom entry on the
1258      stack will be tentative contexts.
1259
1260      We parse tentatively in order to determine which construct is in
1261      use in some situations.  For example, in order to determine
1262      whether a statement is an expression-statement or a
1263      declaration-statement we parse it tentatively as a
1264      declaration-statement.  If that fails, we then reparse the same
1265      token stream as an expression-statement.  */
1266   cp_parser_context *context;
1267
1268   /* True if we are parsing GNU C++.  If this flag is not set, then
1269      GNU extensions are not recognized.  */
1270   bool allow_gnu_extensions_p;
1271
1272   /* TRUE if the `>' token should be interpreted as the greater-than
1273      operator.  FALSE if it is the end of a template-id or
1274      template-parameter-list.  */
1275   bool greater_than_is_operator_p;
1276
1277   /* TRUE if default arguments are allowed within a parameter list
1278      that starts at this point. FALSE if only a gnu extension makes
1279      them permissible.  */
1280   bool default_arg_ok_p;
1281
1282   /* TRUE if we are parsing an integral constant-expression.  See
1283      [expr.const] for a precise definition.  */
1284   bool integral_constant_expression_p;
1285
1286   /* TRUE if we are parsing an integral constant-expression -- but a
1287      non-constant expression should be permitted as well.  This flag
1288      is used when parsing an array bound so that GNU variable-length
1289      arrays are tolerated.  */
1290   bool allow_non_integral_constant_expression_p;
1291
1292   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1293      been seen that makes the expression non-constant.  */
1294   bool non_integral_constant_expression_p;
1295
1296   /* TRUE if local variable names and `this' are forbidden in the
1297      current context.  */
1298   bool local_variables_forbidden_p;
1299
1300   /* TRUE if the declaration we are parsing is part of a
1301      linkage-specification of the form `extern string-literal
1302      declaration'.  */
1303   bool in_unbraced_linkage_specification_p;
1304
1305   /* TRUE if we are presently parsing a declarator, after the
1306      direct-declarator.  */
1307   bool in_declarator_p;
1308
1309   /* TRUE if we are presently parsing a template-argument-list.  */
1310   bool in_template_argument_list_p;
1311
1312   /* TRUE if we are presently parsing the body of an
1313      iteration-statement.  */
1314   bool in_iteration_statement_p;
1315
1316   /* TRUE if we are presently parsing the body of a switch
1317      statement.  */
1318   bool in_switch_statement_p;
1319
1320   /* TRUE if we are parsing a type-id in an expression context.  In
1321      such a situation, both "type (expr)" and "type (type)" are valid
1322      alternatives.  */
1323   bool in_type_id_in_expr_p;
1324
1325   /* TRUE if we are currently in a header file where declarations are
1326      implicitly extern "C".  */
1327   bool implicit_extern_c;
1328
1329   /* TRUE if strings in expressions should be translated to the execution
1330      character set.  */
1331   bool translate_strings_p;
1332
1333   /* If non-NULL, then we are parsing a construct where new type
1334      definitions are not permitted.  The string stored here will be
1335      issued as an error message if a type is defined.  */
1336   const char *type_definition_forbidden_message;
1337
1338   /* A list of lists. The outer list is a stack, used for member
1339      functions of local classes. At each level there are two sub-list,
1340      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1341      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1342      TREE_VALUE's. The functions are chained in reverse declaration
1343      order.
1344
1345      The TREE_PURPOSE sublist contains those functions with default
1346      arguments that need post processing, and the TREE_VALUE sublist
1347      contains those functions with definitions that need post
1348      processing.
1349
1350      These lists can only be processed once the outermost class being
1351      defined is complete.  */
1352   tree unparsed_functions_queues;
1353
1354   /* The number of classes whose definitions are currently in
1355      progress.  */
1356   unsigned num_classes_being_defined;
1357
1358   /* The number of template parameter lists that apply directly to the
1359      current declaration.  */
1360   unsigned num_template_parameter_lists;
1361 } cp_parser;
1362
1363 /* The type of a function that parses some kind of expression.  */
1364 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1365
1366 /* Prototypes.  */
1367
1368 /* Constructors and destructors.  */
1369
1370 static cp_parser *cp_parser_new
1371   (void);
1372
1373 /* Routines to parse various constructs.
1374
1375    Those that return `tree' will return the error_mark_node (rather
1376    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1377    Sometimes, they will return an ordinary node if error-recovery was
1378    attempted, even though a parse error occurred.  So, to check
1379    whether or not a parse error occurred, you should always use
1380    cp_parser_error_occurred.  If the construct is optional (indicated
1381    either by an `_opt' in the name of the function that does the
1382    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1383    the construct is not present.  */
1384
1385 /* Lexical conventions [gram.lex]  */
1386
1387 static tree cp_parser_identifier
1388   (cp_parser *);
1389 static tree cp_parser_string_literal
1390   (cp_parser *, bool, bool);
1391
1392 /* Basic concepts [gram.basic]  */
1393
1394 static bool cp_parser_translation_unit
1395   (cp_parser *);
1396
1397 /* Expressions [gram.expr]  */
1398
1399 static tree cp_parser_primary_expression
1400   (cp_parser *, bool, bool, bool, cp_id_kind *);
1401 static tree cp_parser_id_expression
1402   (cp_parser *, bool, bool, bool *, bool);
1403 static tree cp_parser_unqualified_id
1404   (cp_parser *, bool, bool, bool);
1405 static tree cp_parser_nested_name_specifier_opt
1406   (cp_parser *, bool, bool, bool, bool);
1407 static tree cp_parser_nested_name_specifier
1408   (cp_parser *, bool, bool, bool, bool);
1409 static tree cp_parser_class_or_namespace_name
1410   (cp_parser *, bool, bool, bool, bool, bool);
1411 static tree cp_parser_postfix_expression
1412   (cp_parser *, bool, bool);
1413 static tree cp_parser_postfix_open_square_expression
1414   (cp_parser *, tree, bool);
1415 static tree cp_parser_postfix_dot_deref_expression
1416   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1417 static tree cp_parser_parenthesized_expression_list
1418   (cp_parser *, bool, bool, bool *);
1419 static void cp_parser_pseudo_destructor_name
1420   (cp_parser *, tree *, tree *);
1421 static tree cp_parser_unary_expression
1422   (cp_parser *, bool, bool);
1423 static enum tree_code cp_parser_unary_operator
1424   (cp_token *);
1425 static tree cp_parser_new_expression
1426   (cp_parser *);
1427 static tree cp_parser_new_placement
1428   (cp_parser *);
1429 static tree cp_parser_new_type_id
1430   (cp_parser *, tree *);
1431 static cp_declarator *cp_parser_new_declarator_opt
1432   (cp_parser *);
1433 static cp_declarator *cp_parser_direct_new_declarator
1434   (cp_parser *);
1435 static tree cp_parser_new_initializer
1436   (cp_parser *);
1437 static tree cp_parser_delete_expression
1438   (cp_parser *);
1439 static tree cp_parser_cast_expression
1440   (cp_parser *, bool, bool);
1441 static tree cp_parser_binary_expression
1442   (cp_parser *, bool);
1443 static tree cp_parser_question_colon_clause
1444   (cp_parser *, tree);
1445 static tree cp_parser_assignment_expression
1446   (cp_parser *, bool);
1447 static enum tree_code cp_parser_assignment_operator_opt
1448   (cp_parser *);
1449 static tree cp_parser_expression
1450   (cp_parser *, bool);
1451 static tree cp_parser_constant_expression
1452   (cp_parser *, bool, bool *);
1453 static tree cp_parser_builtin_offsetof
1454   (cp_parser *);
1455
1456 /* Statements [gram.stmt.stmt]  */
1457
1458 static void cp_parser_statement
1459   (cp_parser *, tree);
1460 static tree cp_parser_labeled_statement
1461   (cp_parser *, tree);
1462 static tree cp_parser_expression_statement
1463   (cp_parser *, tree);
1464 static tree cp_parser_compound_statement
1465   (cp_parser *, tree, bool);
1466 static void cp_parser_statement_seq_opt
1467   (cp_parser *, tree);
1468 static tree cp_parser_selection_statement
1469   (cp_parser *);
1470 static tree cp_parser_condition
1471   (cp_parser *);
1472 static tree cp_parser_iteration_statement
1473   (cp_parser *);
1474 static void cp_parser_for_init_statement
1475   (cp_parser *);
1476 static tree cp_parser_jump_statement
1477   (cp_parser *);
1478 static void cp_parser_declaration_statement
1479   (cp_parser *);
1480
1481 static tree cp_parser_implicitly_scoped_statement
1482   (cp_parser *);
1483 static void cp_parser_already_scoped_statement
1484   (cp_parser *);
1485
1486 /* Declarations [gram.dcl.dcl] */
1487
1488 static void cp_parser_declaration_seq_opt
1489   (cp_parser *);
1490 static void cp_parser_declaration
1491   (cp_parser *);
1492 static void cp_parser_block_declaration
1493   (cp_parser *, bool);
1494 static void cp_parser_simple_declaration
1495   (cp_parser *, bool);
1496 static void cp_parser_decl_specifier_seq
1497   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1498 static tree cp_parser_storage_class_specifier_opt
1499   (cp_parser *);
1500 static tree cp_parser_function_specifier_opt
1501   (cp_parser *, cp_decl_specifier_seq *);
1502 static tree cp_parser_type_specifier
1503   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1504    int *, bool *);
1505 static tree cp_parser_simple_type_specifier
1506   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1507 static tree cp_parser_type_name
1508   (cp_parser *);
1509 static tree cp_parser_elaborated_type_specifier
1510   (cp_parser *, bool, bool);
1511 static tree cp_parser_enum_specifier
1512   (cp_parser *);
1513 static void cp_parser_enumerator_list
1514   (cp_parser *, tree);
1515 static void cp_parser_enumerator_definition
1516   (cp_parser *, tree);
1517 static tree cp_parser_namespace_name
1518   (cp_parser *);
1519 static void cp_parser_namespace_definition
1520   (cp_parser *);
1521 static void cp_parser_namespace_body
1522   (cp_parser *);
1523 static tree cp_parser_qualified_namespace_specifier
1524   (cp_parser *);
1525 static void cp_parser_namespace_alias_definition
1526   (cp_parser *);
1527 static void cp_parser_using_declaration
1528   (cp_parser *);
1529 static void cp_parser_using_directive
1530   (cp_parser *);
1531 static void cp_parser_asm_definition
1532   (cp_parser *);
1533 static void cp_parser_linkage_specification
1534   (cp_parser *);
1535
1536 /* Declarators [gram.dcl.decl] */
1537
1538 static tree cp_parser_init_declarator
1539   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1540 static cp_declarator *cp_parser_declarator
1541   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1542 static cp_declarator *cp_parser_direct_declarator
1543   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1544 static enum tree_code cp_parser_ptr_operator
1545   (cp_parser *, tree *, cp_cv_quals *);
1546 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1547   (cp_parser *);
1548 static tree cp_parser_declarator_id
1549   (cp_parser *);
1550 static tree cp_parser_type_id
1551   (cp_parser *);
1552 static void cp_parser_type_specifier_seq
1553   (cp_parser *, bool, cp_decl_specifier_seq *);
1554 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1555   (cp_parser *);
1556 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1557   (cp_parser *, bool *);
1558 static cp_parameter_declarator *cp_parser_parameter_declaration
1559   (cp_parser *, bool, bool *);
1560 static void cp_parser_function_body
1561   (cp_parser *);
1562 static tree cp_parser_initializer
1563   (cp_parser *, bool *, bool *);
1564 static tree cp_parser_initializer_clause
1565   (cp_parser *, bool *);
1566 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1567   (cp_parser *, bool *);
1568
1569 static bool cp_parser_ctor_initializer_opt_and_function_body
1570   (cp_parser *);
1571
1572 /* Classes [gram.class] */
1573
1574 static tree cp_parser_class_name
1575   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1576 static tree cp_parser_class_specifier
1577   (cp_parser *);
1578 static tree cp_parser_class_head
1579   (cp_parser *, bool *, tree *);
1580 static enum tag_types cp_parser_class_key
1581   (cp_parser *);
1582 static void cp_parser_member_specification_opt
1583   (cp_parser *);
1584 static void cp_parser_member_declaration
1585   (cp_parser *);
1586 static tree cp_parser_pure_specifier
1587   (cp_parser *);
1588 static tree cp_parser_constant_initializer
1589   (cp_parser *);
1590
1591 /* Derived classes [gram.class.derived] */
1592
1593 static tree cp_parser_base_clause
1594   (cp_parser *);
1595 static tree cp_parser_base_specifier
1596   (cp_parser *);
1597
1598 /* Special member functions [gram.special] */
1599
1600 static tree cp_parser_conversion_function_id
1601   (cp_parser *);
1602 static tree cp_parser_conversion_type_id
1603   (cp_parser *);
1604 static cp_declarator *cp_parser_conversion_declarator_opt
1605   (cp_parser *);
1606 static bool cp_parser_ctor_initializer_opt
1607   (cp_parser *);
1608 static void cp_parser_mem_initializer_list
1609   (cp_parser *);
1610 static tree cp_parser_mem_initializer
1611   (cp_parser *);
1612 static tree cp_parser_mem_initializer_id
1613   (cp_parser *);
1614
1615 /* Overloading [gram.over] */
1616
1617 static tree cp_parser_operator_function_id
1618   (cp_parser *);
1619 static tree cp_parser_operator
1620   (cp_parser *);
1621
1622 /* Templates [gram.temp] */
1623
1624 static void cp_parser_template_declaration
1625   (cp_parser *, bool);
1626 static tree cp_parser_template_parameter_list
1627   (cp_parser *);
1628 static tree cp_parser_template_parameter
1629   (cp_parser *, bool *);
1630 static tree cp_parser_type_parameter
1631   (cp_parser *);
1632 static tree cp_parser_template_id
1633   (cp_parser *, bool, bool, bool);
1634 static tree cp_parser_template_name
1635   (cp_parser *, bool, bool, bool, bool *);
1636 static tree cp_parser_template_argument_list
1637   (cp_parser *);
1638 static tree cp_parser_template_argument
1639   (cp_parser *);
1640 static void cp_parser_explicit_instantiation
1641   (cp_parser *);
1642 static void cp_parser_explicit_specialization
1643   (cp_parser *);
1644
1645 /* Exception handling [gram.exception] */
1646
1647 static tree cp_parser_try_block
1648   (cp_parser *);
1649 static bool cp_parser_function_try_block
1650   (cp_parser *);
1651 static void cp_parser_handler_seq
1652   (cp_parser *);
1653 static void cp_parser_handler
1654   (cp_parser *);
1655 static tree cp_parser_exception_declaration
1656   (cp_parser *);
1657 static tree cp_parser_throw_expression
1658   (cp_parser *);
1659 static tree cp_parser_exception_specification_opt
1660   (cp_parser *);
1661 static tree cp_parser_type_id_list
1662   (cp_parser *);
1663
1664 /* GNU Extensions */
1665
1666 static tree cp_parser_asm_specification_opt
1667   (cp_parser *);
1668 static tree cp_parser_asm_operand_list
1669   (cp_parser *);
1670 static tree cp_parser_asm_clobber_list
1671   (cp_parser *);
1672 static tree cp_parser_attributes_opt
1673   (cp_parser *);
1674 static tree cp_parser_attribute_list
1675   (cp_parser *);
1676 static bool cp_parser_extension_opt
1677   (cp_parser *, int *);
1678 static void cp_parser_label_declaration
1679   (cp_parser *);
1680
1681 /* Objective-C++ Productions */
1682
1683 static tree cp_parser_objc_message_receiver
1684   (cp_parser *);
1685 static tree cp_parser_objc_message_args
1686   (cp_parser *);
1687 static tree cp_parser_objc_message_expression
1688   (cp_parser *);
1689 static tree cp_parser_objc_encode_expression
1690   (cp_parser *);
1691 static tree cp_parser_objc_defs_expression
1692   (cp_parser *);
1693 static tree cp_parser_objc_protocol_expression
1694   (cp_parser *);
1695 static tree cp_parser_objc_selector_expression
1696   (cp_parser *);
1697 static tree cp_parser_objc_expression
1698   (cp_parser *);
1699 static bool cp_parser_objc_selector_p
1700   (enum cpp_ttype);
1701 static tree cp_parser_objc_selector
1702   (cp_parser *);
1703 static tree cp_parser_objc_protocol_refs_opt
1704   (cp_parser *);
1705 static void cp_parser_objc_declaration
1706   (cp_parser *);
1707 static tree cp_parser_objc_statement
1708   (cp_parser *);
1709
1710 /* Utility Routines */
1711
1712 static tree cp_parser_lookup_name
1713   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1714 static tree cp_parser_lookup_name_simple
1715   (cp_parser *, tree);
1716 static tree cp_parser_maybe_treat_template_as_class
1717   (tree, bool);
1718 static bool cp_parser_check_declarator_template_parameters
1719   (cp_parser *, cp_declarator *);
1720 static bool cp_parser_check_template_parameters
1721   (cp_parser *, unsigned);
1722 static tree cp_parser_simple_cast_expression
1723   (cp_parser *);
1724 static tree cp_parser_global_scope_opt
1725   (cp_parser *, bool);
1726 static bool cp_parser_constructor_declarator_p
1727   (cp_parser *, bool);
1728 static tree cp_parser_function_definition_from_specifiers_and_declarator
1729   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1730 static tree cp_parser_function_definition_after_declarator
1731   (cp_parser *, bool);
1732 static void cp_parser_template_declaration_after_export
1733   (cp_parser *, bool);
1734 static tree cp_parser_single_declaration
1735   (cp_parser *, bool, bool *);
1736 static tree cp_parser_functional_cast
1737   (cp_parser *, tree);
1738 static tree cp_parser_save_member_function_body
1739   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1740 static tree cp_parser_enclosed_template_argument_list
1741   (cp_parser *);
1742 static void cp_parser_save_default_args
1743   (cp_parser *, tree);
1744 static void cp_parser_late_parsing_for_member
1745   (cp_parser *, tree);
1746 static void cp_parser_late_parsing_default_args
1747   (cp_parser *, tree);
1748 static tree cp_parser_sizeof_operand
1749   (cp_parser *, enum rid);
1750 static bool cp_parser_declares_only_class_p
1751   (cp_parser *);
1752 static void cp_parser_set_storage_class
1753   (cp_decl_specifier_seq *, cp_storage_class);
1754 static void cp_parser_set_decl_spec_type
1755   (cp_decl_specifier_seq *, tree, bool);
1756 static bool cp_parser_friend_p
1757   (const cp_decl_specifier_seq *);
1758 static cp_token *cp_parser_require
1759   (cp_parser *, enum cpp_ttype, const char *);
1760 static cp_token *cp_parser_require_keyword
1761   (cp_parser *, enum rid, const char *);
1762 static bool cp_parser_token_starts_function_definition_p
1763   (cp_token *);
1764 static bool cp_parser_next_token_starts_class_definition_p
1765   (cp_parser *);
1766 static bool cp_parser_next_token_ends_template_argument_p
1767   (cp_parser *);
1768 static bool cp_parser_nth_token_starts_template_argument_list_p
1769   (cp_parser *, size_t);
1770 static enum tag_types cp_parser_token_is_class_key
1771   (cp_token *);
1772 static void cp_parser_check_class_key
1773   (enum tag_types, tree type);
1774 static void cp_parser_check_access_in_redeclaration
1775   (tree type);
1776 static bool cp_parser_optional_template_keyword
1777   (cp_parser *);
1778 static void cp_parser_pre_parsed_nested_name_specifier
1779   (cp_parser *);
1780 static void cp_parser_cache_group
1781   (cp_parser *, enum cpp_ttype, unsigned);
1782 static void cp_parser_parse_tentatively
1783   (cp_parser *);
1784 static void cp_parser_commit_to_tentative_parse
1785   (cp_parser *);
1786 static void cp_parser_abort_tentative_parse
1787   (cp_parser *);
1788 static bool cp_parser_parse_definitely
1789   (cp_parser *);
1790 static inline bool cp_parser_parsing_tentatively
1791   (cp_parser *);
1792 static bool cp_parser_uncommitted_to_tentative_parse_p
1793   (cp_parser *);
1794 static void cp_parser_error
1795   (cp_parser *, const char *);
1796 static void cp_parser_name_lookup_error
1797   (cp_parser *, tree, tree, const char *);
1798 static bool cp_parser_simulate_error
1799   (cp_parser *);
1800 static void cp_parser_check_type_definition
1801   (cp_parser *);
1802 static void cp_parser_check_for_definition_in_return_type
1803   (cp_declarator *, tree);
1804 static void cp_parser_check_for_invalid_template_id
1805   (cp_parser *, tree);
1806 static bool cp_parser_non_integral_constant_expression
1807   (cp_parser *, const char *);
1808 static void cp_parser_diagnose_invalid_type_name
1809   (cp_parser *, tree, tree);
1810 static bool cp_parser_parse_and_diagnose_invalid_type_name
1811   (cp_parser *);
1812 static int cp_parser_skip_to_closing_parenthesis
1813   (cp_parser *, bool, bool, bool);
1814 static void cp_parser_skip_to_end_of_statement
1815   (cp_parser *);
1816 static void cp_parser_consume_semicolon_at_end_of_statement
1817   (cp_parser *);
1818 static void cp_parser_skip_to_end_of_block_or_statement
1819   (cp_parser *);
1820 static void cp_parser_skip_to_closing_brace
1821   (cp_parser *);
1822 static void cp_parser_skip_until_found
1823   (cp_parser *, enum cpp_ttype, const char *);
1824 static bool cp_parser_error_occurred
1825   (cp_parser *);
1826 static bool cp_parser_allow_gnu_extensions_p
1827   (cp_parser *);
1828 static bool cp_parser_is_string_literal
1829   (cp_token *);
1830 static bool cp_parser_is_keyword
1831   (cp_token *, enum rid);
1832 static tree cp_parser_make_typename_type
1833   (cp_parser *, tree, tree);
1834
1835 /* Returns nonzero if we are parsing tentatively.  */
1836
1837 static inline bool
1838 cp_parser_parsing_tentatively (cp_parser* parser)
1839 {
1840   return parser->context->next != NULL;
1841 }
1842
1843 /* Returns nonzero if TOKEN is a string literal.  */
1844
1845 static bool
1846 cp_parser_is_string_literal (cp_token* token)
1847 {
1848   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1849 }
1850
1851 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1852
1853 static bool
1854 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1855 {
1856   return token->keyword == keyword;
1857 }
1858
1859 /* A minimum or maximum operator has been seen.  As these are
1860    deprecated, issue a warning.  */
1861
1862 static inline void
1863 cp_parser_warn_min_max (void)
1864 {
1865   if (warn_deprecated && !in_system_header)
1866     warning (0, "minimum/maximum operators are deprecated");
1867 }
1868
1869 /* If not parsing tentatively, issue a diagnostic of the form
1870       FILE:LINE: MESSAGE before TOKEN
1871    where TOKEN is the next token in the input stream.  MESSAGE
1872    (specified by the caller) is usually of the form "expected
1873    OTHER-TOKEN".  */
1874
1875 static void
1876 cp_parser_error (cp_parser* parser, const char* message)
1877 {
1878   if (!cp_parser_simulate_error (parser))
1879     {
1880       cp_token *token = cp_lexer_peek_token (parser->lexer);
1881       /* This diagnostic makes more sense if it is tagged to the line
1882          of the token we just peeked at.  */
1883       cp_lexer_set_source_position_from_token (token);
1884       if (token->type == CPP_PRAGMA)
1885         {
1886           error ("%<#pragma%> is not allowed here");
1887           cp_lexer_purge_token (parser->lexer);
1888           return;
1889         }
1890       c_parse_error (message,
1891                      /* Because c_parser_error does not understand
1892                         CPP_KEYWORD, keywords are treated like
1893                         identifiers.  */
1894                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1895                      token->value);
1896     }
1897 }
1898
1899 /* Issue an error about name-lookup failing.  NAME is the
1900    IDENTIFIER_NODE DECL is the result of
1901    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1902    the thing that we hoped to find.  */
1903
1904 static void
1905 cp_parser_name_lookup_error (cp_parser* parser,
1906                              tree name,
1907                              tree decl,
1908                              const char* desired)
1909 {
1910   /* If name lookup completely failed, tell the user that NAME was not
1911      declared.  */
1912   if (decl == error_mark_node)
1913     {
1914       if (parser->scope && parser->scope != global_namespace)
1915         error ("%<%D::%D%> has not been declared",
1916                parser->scope, name);
1917       else if (parser->scope == global_namespace)
1918         error ("%<::%D%> has not been declared", name);
1919       else if (parser->object_scope
1920                && !CLASS_TYPE_P (parser->object_scope))
1921         error ("request for member %qD in non-class type %qT",
1922                name, parser->object_scope);
1923       else if (parser->object_scope)
1924         error ("%<%T::%D%> has not been declared",
1925                parser->object_scope, name);
1926       else
1927         error ("%qD has not been declared", name);
1928     }
1929   else if (parser->scope && parser->scope != global_namespace)
1930     error ("%<%D::%D%> %s", parser->scope, name, desired);
1931   else if (parser->scope == global_namespace)
1932     error ("%<::%D%> %s", name, desired);
1933   else
1934     error ("%qD %s", name, desired);
1935 }
1936
1937 /* If we are parsing tentatively, remember that an error has occurred
1938    during this tentative parse.  Returns true if the error was
1939    simulated; false if a message should be issued by the caller.  */
1940
1941 static bool
1942 cp_parser_simulate_error (cp_parser* parser)
1943 {
1944   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1945     {
1946       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1947       return true;
1948     }
1949   return false;
1950 }
1951
1952 /* This function is called when a type is defined.  If type
1953    definitions are forbidden at this point, an error message is
1954    issued.  */
1955
1956 static void
1957 cp_parser_check_type_definition (cp_parser* parser)
1958 {
1959   /* If types are forbidden here, issue a message.  */
1960   if (parser->type_definition_forbidden_message)
1961     /* Use `%s' to print the string in case there are any escape
1962        characters in the message.  */
1963     error ("%s", parser->type_definition_forbidden_message);
1964 }
1965
1966 /* This function is called when the DECLARATOR is processed.  The TYPE
1967    was a type defined in the decl-specifiers.  If it is invalid to
1968    define a type in the decl-specifiers for DECLARATOR, an error is
1969    issued.  */
1970
1971 static void
1972 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1973                                                tree type)
1974 {
1975   /* [dcl.fct] forbids type definitions in return types.
1976      Unfortunately, it's not easy to know whether or not we are
1977      processing a return type until after the fact.  */
1978   while (declarator
1979          && (declarator->kind == cdk_pointer
1980              || declarator->kind == cdk_reference
1981              || declarator->kind == cdk_ptrmem))
1982     declarator = declarator->declarator;
1983   if (declarator
1984       && declarator->kind == cdk_function)
1985     {
1986       error ("new types may not be defined in a return type");
1987       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1988               type);
1989     }
1990 }
1991
1992 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1993    "<" in any valid C++ program.  If the next token is indeed "<",
1994    issue a message warning the user about what appears to be an
1995    invalid attempt to form a template-id.  */
1996
1997 static void
1998 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1999                                          tree type)
2000 {
2001   cp_token_position start = 0;
2002
2003   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2004     {
2005       if (TYPE_P (type))
2006         error ("%qT is not a template", type);
2007       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2008         error ("%qE is not a template", type);
2009       else
2010         error ("invalid template-id");
2011       /* Remember the location of the invalid "<".  */
2012       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2013         start = cp_lexer_token_position (parser->lexer, true);
2014       /* Consume the "<".  */
2015       cp_lexer_consume_token (parser->lexer);
2016       /* Parse the template arguments.  */
2017       cp_parser_enclosed_template_argument_list (parser);
2018       /* Permanently remove the invalid template arguments so that
2019          this error message is not issued again.  */
2020       if (start)
2021         cp_lexer_purge_tokens_after (parser->lexer, start);
2022     }
2023 }
2024
2025 /* If parsing an integral constant-expression, issue an error message
2026    about the fact that THING appeared and return true.  Otherwise,
2027    return false.  In either case, set
2028    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2029
2030 static bool
2031 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2032                                             const char *thing)
2033 {
2034   parser->non_integral_constant_expression_p = true;
2035   if (parser->integral_constant_expression_p)
2036     {
2037       if (!parser->allow_non_integral_constant_expression_p)
2038         {
2039           error ("%s cannot appear in a constant-expression", thing);
2040           return true;
2041         }
2042     }
2043   return false;
2044 }
2045
2046 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2047    qualifying scope (or NULL, if none) for ID.  This function commits
2048    to the current active tentative parse, if any.  (Otherwise, the
2049    problematic construct might be encountered again later, resulting
2050    in duplicate error messages.)  */
2051
2052 static void
2053 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2054 {
2055   tree decl, old_scope;
2056   /* Try to lookup the identifier.  */
2057   old_scope = parser->scope;
2058   parser->scope = scope;
2059   decl = cp_parser_lookup_name_simple (parser, id);
2060   parser->scope = old_scope;
2061   /* If the lookup found a template-name, it means that the user forgot
2062   to specify an argument list. Emit a useful error message.  */
2063   if (TREE_CODE (decl) == TEMPLATE_DECL)
2064     error ("invalid use of template-name %qE without an argument list",
2065       decl);
2066   else if (!parser->scope)
2067     {
2068       /* Issue an error message.  */
2069       error ("%qE does not name a type", id);
2070       /* If we're in a template class, it's possible that the user was
2071          referring to a type from a base class.  For example:
2072
2073            template <typename T> struct A { typedef T X; };
2074            template <typename T> struct B : public A<T> { X x; };
2075
2076          The user should have said "typename A<T>::X".  */
2077       if (processing_template_decl && current_class_type
2078           && TYPE_BINFO (current_class_type))
2079         {
2080           tree b;
2081
2082           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2083                b;
2084                b = TREE_CHAIN (b))
2085             {
2086               tree base_type = BINFO_TYPE (b);
2087               if (CLASS_TYPE_P (base_type)
2088                   && dependent_type_p (base_type))
2089                 {
2090                   tree field;
2091                   /* Go from a particular instantiation of the
2092                      template (which will have an empty TYPE_FIELDs),
2093                      to the main version.  */
2094                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2095                   for (field = TYPE_FIELDS (base_type);
2096                        field;
2097                        field = TREE_CHAIN (field))
2098                     if (TREE_CODE (field) == TYPE_DECL
2099                         && DECL_NAME (field) == id)
2100                       {
2101                         inform ("(perhaps %<typename %T::%E%> was intended)",
2102                                 BINFO_TYPE (b), id);
2103                         break;
2104                       }
2105                   if (field)
2106                     break;
2107                 }
2108             }
2109         }
2110     }
2111   /* Here we diagnose qualified-ids where the scope is actually correct,
2112      but the identifier does not resolve to a valid type name.  */
2113   else if (parser->scope != error_mark_node)
2114     {
2115       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2116         error ("%qE in namespace %qE does not name a type",
2117                id, parser->scope);
2118       else if (TYPE_P (parser->scope))
2119         error ("%qE in class %qT does not name a type", id, parser->scope);
2120       else
2121         gcc_unreachable ();
2122     }
2123   cp_parser_commit_to_tentative_parse (parser);
2124 }
2125
2126 /* Check for a common situation where a type-name should be present,
2127    but is not, and issue a sensible error message.  Returns true if an
2128    invalid type-name was detected.
2129
2130    The situation handled by this function are variable declarations of the
2131    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2132    Usually, `ID' should name a type, but if we got here it means that it
2133    does not. We try to emit the best possible error message depending on
2134    how exactly the id-expression looks like.
2135 */
2136
2137 static bool
2138 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2139 {
2140   tree id;
2141
2142   cp_parser_parse_tentatively (parser);
2143   id = cp_parser_id_expression (parser,
2144                                 /*template_keyword_p=*/false,
2145                                 /*check_dependency_p=*/true,
2146                                 /*template_p=*/NULL,
2147                                 /*declarator_p=*/true);
2148   /* After the id-expression, there should be a plain identifier,
2149      otherwise this is not a simple variable declaration. Also, if
2150      the scope is dependent, we cannot do much.  */
2151   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2152       || (parser->scope && TYPE_P (parser->scope)
2153           && dependent_type_p (parser->scope)))
2154     {
2155       cp_parser_abort_tentative_parse (parser);
2156       return false;
2157     }
2158   if (!cp_parser_parse_definitely (parser)
2159       || TREE_CODE (id) != IDENTIFIER_NODE)
2160     return false;
2161
2162   /* Emit a diagnostic for the invalid type.  */
2163   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2164   /* Skip to the end of the declaration; there's no point in
2165      trying to process it.  */
2166   cp_parser_skip_to_end_of_block_or_statement (parser);
2167   return true;
2168 }
2169
2170 /* Consume tokens up to, and including, the next non-nested closing `)'.
2171    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2172    are doing error recovery. Returns -1 if OR_COMMA is true and we
2173    found an unnested comma.  */
2174
2175 static int
2176 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2177                                        bool recovering,
2178                                        bool or_comma,
2179                                        bool consume_paren)
2180 {
2181   unsigned paren_depth = 0;
2182   unsigned brace_depth = 0;
2183   int result;
2184
2185   if (recovering && !or_comma
2186       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2187     return 0;
2188
2189   while (true)
2190     {
2191       cp_token *token;
2192
2193       /* If we've run out of tokens, then there is no closing `)'.  */
2194       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2195         {
2196           result = 0;
2197           break;
2198         }
2199
2200       token = cp_lexer_peek_token (parser->lexer);
2201
2202       /* This matches the processing in skip_to_end_of_statement.  */
2203       if (token->type == CPP_SEMICOLON && !brace_depth)
2204         {
2205           result = 0;
2206           break;
2207         }
2208       if (token->type == CPP_OPEN_BRACE)
2209         ++brace_depth;
2210       if (token->type == CPP_CLOSE_BRACE)
2211         {
2212           if (!brace_depth--)
2213             {
2214               result = 0;
2215               break;
2216             }
2217         }
2218       if (recovering && or_comma && token->type == CPP_COMMA
2219           && !brace_depth && !paren_depth)
2220         {
2221           result = -1;
2222           break;
2223         }
2224
2225       if (!brace_depth)
2226         {
2227           /* If it is an `(', we have entered another level of nesting.  */
2228           if (token->type == CPP_OPEN_PAREN)
2229             ++paren_depth;
2230           /* If it is a `)', then we might be done.  */
2231           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2232             {
2233               if (consume_paren)
2234                 cp_lexer_consume_token (parser->lexer);
2235               {
2236                 result = 1;
2237                 break;
2238               }
2239             }
2240         }
2241
2242       /* Consume the token.  */
2243       cp_lexer_consume_token (parser->lexer);
2244     }
2245
2246   return result;
2247 }
2248
2249 /* Consume tokens until we reach the end of the current statement.
2250    Normally, that will be just before consuming a `;'.  However, if a
2251    non-nested `}' comes first, then we stop before consuming that.  */
2252
2253 static void
2254 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2255 {
2256   unsigned nesting_depth = 0;
2257
2258   while (true)
2259     {
2260       cp_token *token;
2261
2262       /* Peek at the next token.  */
2263       token = cp_lexer_peek_token (parser->lexer);
2264       /* If we've run out of tokens, stop.  */
2265       if (token->type == CPP_EOF)
2266         break;
2267       /* If the next token is a `;', we have reached the end of the
2268          statement.  */
2269       if (token->type == CPP_SEMICOLON && !nesting_depth)
2270         break;
2271       /* If the next token is a non-nested `}', then we have reached
2272          the end of the current block.  */
2273       if (token->type == CPP_CLOSE_BRACE)
2274         {
2275           /* If this is a non-nested `}', stop before consuming it.
2276              That way, when confronted with something like:
2277
2278                { 3 + }
2279
2280              we stop before consuming the closing `}', even though we
2281              have not yet reached a `;'.  */
2282           if (nesting_depth == 0)
2283             break;
2284           /* If it is the closing `}' for a block that we have
2285              scanned, stop -- but only after consuming the token.
2286              That way given:
2287
2288                 void f g () { ... }
2289                 typedef int I;
2290
2291              we will stop after the body of the erroneously declared
2292              function, but before consuming the following `typedef'
2293              declaration.  */
2294           if (--nesting_depth == 0)
2295             {
2296               cp_lexer_consume_token (parser->lexer);
2297               break;
2298             }
2299         }
2300       /* If it the next token is a `{', then we are entering a new
2301          block.  Consume the entire block.  */
2302       else if (token->type == CPP_OPEN_BRACE)
2303         ++nesting_depth;
2304       /* Consume the token.  */
2305       cp_lexer_consume_token (parser->lexer);
2306     }
2307 }
2308
2309 /* This function is called at the end of a statement or declaration.
2310    If the next token is a semicolon, it is consumed; otherwise, error
2311    recovery is attempted.  */
2312
2313 static void
2314 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2315 {
2316   /* Look for the trailing `;'.  */
2317   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2318     {
2319       /* If there is additional (erroneous) input, skip to the end of
2320          the statement.  */
2321       cp_parser_skip_to_end_of_statement (parser);
2322       /* If the next token is now a `;', consume it.  */
2323       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2324         cp_lexer_consume_token (parser->lexer);
2325     }
2326 }
2327
2328 /* Skip tokens until we have consumed an entire block, or until we
2329    have consumed a non-nested `;'.  */
2330
2331 static void
2332 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2333 {
2334   int nesting_depth = 0;
2335
2336   while (nesting_depth >= 0)
2337     {
2338       cp_token *token = cp_lexer_peek_token (parser->lexer);
2339
2340       if (token->type == CPP_EOF)
2341         break;
2342
2343       switch (token->type)
2344         {
2345         case CPP_EOF:
2346           /* If we've run out of tokens, stop.  */
2347           nesting_depth = -1;
2348           continue;
2349
2350         case CPP_SEMICOLON:
2351           /* Stop if this is an unnested ';'. */
2352           if (!nesting_depth)
2353             nesting_depth = -1;
2354           break;
2355
2356         case CPP_CLOSE_BRACE:
2357           /* Stop if this is an unnested '}', or closes the outermost
2358              nesting level.  */
2359           nesting_depth--;
2360           if (!nesting_depth)
2361             nesting_depth = -1;
2362           break;
2363
2364         case CPP_OPEN_BRACE:
2365           /* Nest. */
2366           nesting_depth++;
2367           break;
2368
2369         default:
2370           break;
2371         }
2372
2373       /* Consume the token.  */
2374       cp_lexer_consume_token (parser->lexer);
2375
2376     }
2377 }
2378
2379 /* Skip tokens until a non-nested closing curly brace is the next
2380    token.  */
2381
2382 static void
2383 cp_parser_skip_to_closing_brace (cp_parser *parser)
2384 {
2385   unsigned nesting_depth = 0;
2386
2387   while (true)
2388     {
2389       cp_token *token;
2390
2391       /* Peek at the next token.  */
2392       token = cp_lexer_peek_token (parser->lexer);
2393       /* If we've run out of tokens, stop.  */
2394       if (token->type == CPP_EOF)
2395         break;
2396       /* If the next token is a non-nested `}', then we have reached
2397          the end of the current block.  */
2398       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2399         break;
2400       /* If it the next token is a `{', then we are entering a new
2401          block.  Consume the entire block.  */
2402       else if (token->type == CPP_OPEN_BRACE)
2403         ++nesting_depth;
2404       /* Consume the token.  */
2405       cp_lexer_consume_token (parser->lexer);
2406     }
2407 }
2408
2409 /* This is a simple wrapper around make_typename_type. When the id is
2410    an unresolved identifier node, we can provide a superior diagnostic
2411    using cp_parser_diagnose_invalid_type_name.  */
2412
2413 static tree
2414 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2415 {
2416   tree result;
2417   if (TREE_CODE (id) == IDENTIFIER_NODE)
2418     {
2419       result = make_typename_type (scope, id, typename_type,
2420                                    /*complain=*/tf_none);
2421       if (result == error_mark_node)
2422         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2423       return result;
2424     }
2425   return make_typename_type (scope, id, typename_type, tf_error);
2426 }
2427
2428
2429 /* Create a new C++ parser.  */
2430
2431 static cp_parser *
2432 cp_parser_new (void)
2433 {
2434   cp_parser *parser;
2435   cp_lexer *lexer;
2436   unsigned i;
2437
2438   /* cp_lexer_new_main is called before calling ggc_alloc because
2439      cp_lexer_new_main might load a PCH file.  */
2440   lexer = cp_lexer_new_main ();
2441
2442   /* Initialize the binops_by_token so that we can get the tree
2443      directly from the token.  */
2444   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2445     binops_by_token[binops[i].token_type] = binops[i];
2446
2447   parser = GGC_CNEW (cp_parser);
2448   parser->lexer = lexer;
2449   parser->context = cp_parser_context_new (NULL);
2450
2451   /* For now, we always accept GNU extensions.  */
2452   parser->allow_gnu_extensions_p = 1;
2453
2454   /* The `>' token is a greater-than operator, not the end of a
2455      template-id.  */
2456   parser->greater_than_is_operator_p = true;
2457
2458   parser->default_arg_ok_p = true;
2459
2460   /* We are not parsing a constant-expression.  */
2461   parser->integral_constant_expression_p = false;
2462   parser->allow_non_integral_constant_expression_p = false;
2463   parser->non_integral_constant_expression_p = false;
2464
2465   /* Local variable names are not forbidden.  */
2466   parser->local_variables_forbidden_p = false;
2467
2468   /* We are not processing an `extern "C"' declaration.  */
2469   parser->in_unbraced_linkage_specification_p = false;
2470
2471   /* We are not processing a declarator.  */
2472   parser->in_declarator_p = false;
2473
2474   /* We are not processing a template-argument-list.  */
2475   parser->in_template_argument_list_p = false;
2476
2477   /* We are not in an iteration statement.  */
2478   parser->in_iteration_statement_p = false;
2479
2480   /* We are not in a switch statement.  */
2481   parser->in_switch_statement_p = false;
2482
2483   /* We are not parsing a type-id inside an expression.  */
2484   parser->in_type_id_in_expr_p = false;
2485
2486   /* Declarations aren't implicitly extern "C".  */
2487   parser->implicit_extern_c = false;
2488
2489   /* String literals should be translated to the execution character set.  */
2490   parser->translate_strings_p = true;
2491
2492   /* The unparsed function queue is empty.  */
2493   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2494
2495   /* There are no classes being defined.  */
2496   parser->num_classes_being_defined = 0;
2497
2498   /* No template parameters apply.  */
2499   parser->num_template_parameter_lists = 0;
2500
2501   return parser;
2502 }
2503
2504 /* Create a cp_lexer structure which will emit the tokens in CACHE
2505    and push it onto the parser's lexer stack.  This is used for delayed
2506    parsing of in-class method bodies and default arguments, and should
2507    not be confused with tentative parsing.  */
2508 static void
2509 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2510 {
2511   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2512   lexer->next = parser->lexer;
2513   parser->lexer = lexer;
2514
2515   /* Move the current source position to that of the first token in the
2516      new lexer.  */
2517   cp_lexer_set_source_position_from_token (lexer->next_token);
2518 }
2519
2520 /* Pop the top lexer off the parser stack.  This is never used for the
2521    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2522 static void
2523 cp_parser_pop_lexer (cp_parser *parser)
2524 {
2525   cp_lexer *lexer = parser->lexer;
2526   parser->lexer = lexer->next;
2527   cp_lexer_destroy (lexer);
2528
2529   /* Put the current source position back where it was before this
2530      lexer was pushed.  */
2531   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2532 }
2533
2534 /* Lexical conventions [gram.lex]  */
2535
2536 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2537    identifier.  */
2538
2539 static tree
2540 cp_parser_identifier (cp_parser* parser)
2541 {
2542   cp_token *token;
2543
2544   /* Look for the identifier.  */
2545   token = cp_parser_require (parser, CPP_NAME, "identifier");
2546   /* Return the value.  */
2547   return token ? token->value : error_mark_node;
2548 }
2549
2550 /* Parse a sequence of adjacent string constants.  Returns a
2551    TREE_STRING representing the combined, nul-terminated string
2552    constant.  If TRANSLATE is true, translate the string to the
2553    execution character set.  If WIDE_OK is true, a wide string is
2554    invalid here.
2555
2556    C++98 [lex.string] says that if a narrow string literal token is
2557    adjacent to a wide string literal token, the behavior is undefined.
2558    However, C99 6.4.5p4 says that this results in a wide string literal.
2559    We follow C99 here, for consistency with the C front end.
2560
2561    This code is largely lifted from lex_string() in c-lex.c.
2562
2563    FUTURE: ObjC++ will need to handle @-strings here.  */
2564 static tree
2565 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2566 {
2567   tree value;
2568   bool wide = false;
2569   size_t count;
2570   struct obstack str_ob;
2571   cpp_string str, istr, *strs;
2572   cp_token *tok;
2573
2574   tok = cp_lexer_peek_token (parser->lexer);
2575   if (!cp_parser_is_string_literal (tok))
2576     {
2577       cp_parser_error (parser, "expected string-literal");
2578       return error_mark_node;
2579     }
2580
2581   /* Try to avoid the overhead of creating and destroying an obstack
2582      for the common case of just one string.  */
2583   if (!cp_parser_is_string_literal
2584       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2585     {
2586       cp_lexer_consume_token (parser->lexer);
2587
2588       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2589       str.len = TREE_STRING_LENGTH (tok->value);
2590       count = 1;
2591       if (tok->type == CPP_WSTRING)
2592         wide = true;
2593
2594       strs = &str;
2595     }
2596   else
2597     {
2598       gcc_obstack_init (&str_ob);
2599       count = 0;
2600
2601       do
2602         {
2603           cp_lexer_consume_token (parser->lexer);
2604           count++;
2605           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2606           str.len = TREE_STRING_LENGTH (tok->value);
2607           if (tok->type == CPP_WSTRING)
2608             wide = true;
2609
2610           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2611
2612           tok = cp_lexer_peek_token (parser->lexer);
2613         }
2614       while (cp_parser_is_string_literal (tok));
2615
2616       strs = (cpp_string *) obstack_finish (&str_ob);
2617     }
2618
2619   if (wide && !wide_ok)
2620     {
2621       cp_parser_error (parser, "a wide string is invalid in this context");
2622       wide = false;
2623     }
2624
2625   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2626       (parse_in, strs, count, &istr, wide))
2627     {
2628       value = build_string (istr.len, (char *)istr.text);
2629       free ((void *)istr.text);
2630
2631       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2632       value = fix_string_type (value);
2633     }
2634   else
2635     /* cpp_interpret_string has issued an error.  */
2636     value = error_mark_node;
2637
2638   if (count > 1)
2639     obstack_free (&str_ob, 0);
2640
2641   return value;
2642 }
2643
2644
2645 /* Basic concepts [gram.basic]  */
2646
2647 /* Parse a translation-unit.
2648
2649    translation-unit:
2650      declaration-seq [opt]
2651
2652    Returns TRUE if all went well.  */
2653
2654 static bool
2655 cp_parser_translation_unit (cp_parser* parser)
2656 {
2657   /* The address of the first non-permanent object on the declarator
2658      obstack.  */
2659   static void *declarator_obstack_base;
2660
2661   bool success;
2662
2663   /* Create the declarator obstack, if necessary.  */
2664   if (!cp_error_declarator)
2665     {
2666       gcc_obstack_init (&declarator_obstack);
2667       /* Create the error declarator.  */
2668       cp_error_declarator = make_declarator (cdk_error);
2669       /* Create the empty parameter list.  */
2670       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2671       /* Remember where the base of the declarator obstack lies.  */
2672       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2673     }
2674
2675   cp_parser_declaration_seq_opt (parser);
2676   
2677   /* If there are no tokens left then all went well.  */
2678   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2679     {
2680       /* Get rid of the token array; we don't need it any more.  */
2681       cp_lexer_destroy (parser->lexer);
2682       parser->lexer = NULL;
2683       
2684       /* This file might have been a context that's implicitly extern
2685          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2686       if (parser->implicit_extern_c)
2687         {
2688           pop_lang_context ();
2689           parser->implicit_extern_c = false;
2690         }
2691       
2692       /* Finish up.  */
2693       finish_translation_unit ();
2694       
2695       success = true;
2696     }
2697   else
2698     {
2699       cp_parser_error (parser, "expected declaration");
2700       success = false;
2701     }
2702   
2703   /* Make sure the declarator obstack was fully cleaned up.  */
2704   gcc_assert (obstack_next_free (&declarator_obstack)
2705               == declarator_obstack_base);
2706
2707   /* All went well.  */
2708   return success;
2709 }
2710
2711 /* Expressions [gram.expr] */
2712
2713 /* Parse a primary-expression.
2714
2715    primary-expression:
2716      literal
2717      this
2718      ( expression )
2719      id-expression
2720
2721    GNU Extensions:
2722
2723    primary-expression:
2724      ( compound-statement )
2725      __builtin_va_arg ( assignment-expression , type-id )
2726
2727    Objective-C++ Extension:
2728
2729    primary-expression:
2730      objc-expression
2731
2732    literal:
2733      __null
2734
2735    ADDRESS_P is true iff this expression was immediately preceded by
2736    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2737    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2738    true iff this expression is a template argument.
2739
2740    Returns a representation of the expression.  Upon return, *IDK
2741    indicates what kind of id-expression (if any) was present.  */
2742
2743 static tree
2744 cp_parser_primary_expression (cp_parser *parser,
2745                               bool address_p,
2746                               bool cast_p,
2747                               bool template_arg_p,
2748                               cp_id_kind *idk)
2749 {
2750   cp_token *token;
2751
2752   /* Assume the primary expression is not an id-expression.  */
2753   *idk = CP_ID_KIND_NONE;
2754
2755   /* Peek at the next token.  */
2756   token = cp_lexer_peek_token (parser->lexer);
2757   switch (token->type)
2758     {
2759       /* literal:
2760            integer-literal
2761            character-literal
2762            floating-literal
2763            string-literal
2764            boolean-literal  */
2765     case CPP_CHAR:
2766     case CPP_WCHAR:
2767     case CPP_NUMBER:
2768       token = cp_lexer_consume_token (parser->lexer);
2769       /* Floating-point literals are only allowed in an integral
2770          constant expression if they are cast to an integral or
2771          enumeration type.  */
2772       if (TREE_CODE (token->value) == REAL_CST
2773           && parser->integral_constant_expression_p
2774           && pedantic)
2775         {
2776           /* CAST_P will be set even in invalid code like "int(2.7 +
2777              ...)".   Therefore, we have to check that the next token
2778              is sure to end the cast.  */
2779           if (cast_p)
2780             {
2781               cp_token *next_token;
2782
2783               next_token = cp_lexer_peek_token (parser->lexer);
2784               if (/* The comma at the end of an
2785                      enumerator-definition.  */
2786                   next_token->type != CPP_COMMA
2787                   /* The curly brace at the end of an enum-specifier.  */
2788                   && next_token->type != CPP_CLOSE_BRACE
2789                   /* The end of a statement.  */
2790                   && next_token->type != CPP_SEMICOLON
2791                   /* The end of the cast-expression.  */
2792                   && next_token->type != CPP_CLOSE_PAREN
2793                   /* The end of an array bound.  */
2794                   && next_token->type != CPP_CLOSE_SQUARE
2795                   /* The closing ">" in a template-argument-list.  */
2796                   && (next_token->type != CPP_GREATER
2797                       || parser->greater_than_is_operator_p))
2798                 cast_p = false;
2799             }
2800
2801           /* If we are within a cast, then the constraint that the
2802              cast is to an integral or enumeration type will be
2803              checked at that point.  If we are not within a cast, then
2804              this code is invalid.  */
2805           if (!cast_p)
2806             cp_parser_non_integral_constant_expression
2807               (parser, "floating-point literal");
2808         }
2809       return token->value;
2810
2811     case CPP_STRING:
2812     case CPP_WSTRING:
2813       /* ??? Should wide strings be allowed when parser->translate_strings_p
2814          is false (i.e. in attributes)?  If not, we can kill the third
2815          argument to cp_parser_string_literal.  */
2816       return cp_parser_string_literal (parser,
2817                                        parser->translate_strings_p,
2818                                        true);
2819
2820     case CPP_OPEN_PAREN:
2821       {
2822         tree expr;
2823         bool saved_greater_than_is_operator_p;
2824
2825         /* Consume the `('.  */
2826         cp_lexer_consume_token (parser->lexer);
2827         /* Within a parenthesized expression, a `>' token is always
2828            the greater-than operator.  */
2829         saved_greater_than_is_operator_p
2830           = parser->greater_than_is_operator_p;
2831         parser->greater_than_is_operator_p = true;
2832         /* If we see `( { ' then we are looking at the beginning of
2833            a GNU statement-expression.  */
2834         if (cp_parser_allow_gnu_extensions_p (parser)
2835             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2836           {
2837             /* Statement-expressions are not allowed by the standard.  */
2838             if (pedantic)
2839               pedwarn ("ISO C++ forbids braced-groups within expressions");
2840
2841             /* And they're not allowed outside of a function-body; you
2842                cannot, for example, write:
2843
2844                  int i = ({ int j = 3; j + 1; });
2845
2846                at class or namespace scope.  */
2847             if (!at_function_scope_p ())
2848               error ("statement-expressions are allowed only inside functions");
2849             /* Start the statement-expression.  */
2850             expr = begin_stmt_expr ();
2851             /* Parse the compound-statement.  */
2852             cp_parser_compound_statement (parser, expr, false);
2853             /* Finish up.  */
2854             expr = finish_stmt_expr (expr, false);
2855           }
2856         else
2857           {
2858             /* Parse the parenthesized expression.  */
2859             expr = cp_parser_expression (parser, cast_p);
2860             /* Let the front end know that this expression was
2861                enclosed in parentheses. This matters in case, for
2862                example, the expression is of the form `A::B', since
2863                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2864                not.  */
2865             finish_parenthesized_expr (expr);
2866           }
2867         /* The `>' token might be the end of a template-id or
2868            template-parameter-list now.  */
2869         parser->greater_than_is_operator_p
2870           = saved_greater_than_is_operator_p;
2871         /* Consume the `)'.  */
2872         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2873           cp_parser_skip_to_end_of_statement (parser);
2874
2875         return expr;
2876       }
2877
2878     case CPP_KEYWORD:
2879       switch (token->keyword)
2880         {
2881           /* These two are the boolean literals.  */
2882         case RID_TRUE:
2883           cp_lexer_consume_token (parser->lexer);
2884           return boolean_true_node;
2885         case RID_FALSE:
2886           cp_lexer_consume_token (parser->lexer);
2887           return boolean_false_node;
2888
2889           /* The `__null' literal.  */
2890         case RID_NULL:
2891           cp_lexer_consume_token (parser->lexer);
2892           return null_node;
2893
2894           /* Recognize the `this' keyword.  */
2895         case RID_THIS:
2896           cp_lexer_consume_token (parser->lexer);
2897           if (parser->local_variables_forbidden_p)
2898             {
2899               error ("%<this%> may not be used in this context");
2900               return error_mark_node;
2901             }
2902           /* Pointers cannot appear in constant-expressions.  */
2903           if (cp_parser_non_integral_constant_expression (parser,
2904                                                           "`this'"))
2905             return error_mark_node;
2906           return finish_this_expr ();
2907
2908           /* The `operator' keyword can be the beginning of an
2909              id-expression.  */
2910         case RID_OPERATOR:
2911           goto id_expression;
2912
2913         case RID_FUNCTION_NAME:
2914         case RID_PRETTY_FUNCTION_NAME:
2915         case RID_C99_FUNCTION_NAME:
2916           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2917              __func__ are the names of variables -- but they are
2918              treated specially.  Therefore, they are handled here,
2919              rather than relying on the generic id-expression logic
2920              below.  Grammatically, these names are id-expressions.
2921
2922              Consume the token.  */
2923           token = cp_lexer_consume_token (parser->lexer);
2924           /* Look up the name.  */
2925           return finish_fname (token->value);
2926
2927         case RID_VA_ARG:
2928           {
2929             tree expression;
2930             tree type;
2931
2932             /* The `__builtin_va_arg' construct is used to handle
2933                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2934             cp_lexer_consume_token (parser->lexer);
2935             /* Look for the opening `('.  */
2936             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2937             /* Now, parse the assignment-expression.  */
2938             expression = cp_parser_assignment_expression (parser,
2939                                                           /*cast_p=*/false);
2940             /* Look for the `,'.  */
2941             cp_parser_require (parser, CPP_COMMA, "`,'");
2942             /* Parse the type-id.  */
2943             type = cp_parser_type_id (parser);
2944             /* Look for the closing `)'.  */
2945             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2946             /* Using `va_arg' in a constant-expression is not
2947                allowed.  */
2948             if (cp_parser_non_integral_constant_expression (parser,
2949                                                             "`va_arg'"))
2950               return error_mark_node;
2951             return build_x_va_arg (expression, type);
2952           }
2953
2954         case RID_OFFSETOF:
2955           return cp_parser_builtin_offsetof (parser);
2956
2957           /* Objective-C++ expressions.  */
2958         case RID_AT_ENCODE:
2959         case RID_AT_PROTOCOL:
2960         case RID_AT_SELECTOR:
2961           return cp_parser_objc_expression (parser);
2962
2963         default:
2964           cp_parser_error (parser, "expected primary-expression");
2965           return error_mark_node;
2966         }
2967
2968       /* An id-expression can start with either an identifier, a
2969          `::' as the beginning of a qualified-id, or the "operator"
2970          keyword.  */
2971     case CPP_NAME:
2972     case CPP_SCOPE:
2973     case CPP_TEMPLATE_ID:
2974     case CPP_NESTED_NAME_SPECIFIER:
2975       {
2976         tree id_expression;
2977         tree decl;
2978         const char *error_msg;
2979         bool template_p;
2980         bool done;
2981
2982       id_expression:
2983         /* Parse the id-expression.  */
2984         id_expression
2985           = cp_parser_id_expression (parser,
2986                                      /*template_keyword_p=*/false,
2987                                      /*check_dependency_p=*/true,
2988                                      &template_p,
2989                                      /*declarator_p=*/false);
2990         if (id_expression == error_mark_node)
2991           return error_mark_node;
2992         token = cp_lexer_peek_token (parser->lexer);
2993         done = (token->type != CPP_OPEN_SQUARE
2994                 && token->type != CPP_OPEN_PAREN
2995                 && token->type != CPP_DOT
2996                 && token->type != CPP_DEREF
2997                 && token->type != CPP_PLUS_PLUS
2998                 && token->type != CPP_MINUS_MINUS);
2999         /* If we have a template-id, then no further lookup is
3000            required.  If the template-id was for a template-class, we
3001            will sometimes have a TYPE_DECL at this point.  */
3002         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3003                  || TREE_CODE (id_expression) == TYPE_DECL)
3004           decl = id_expression;
3005         /* Look up the name.  */
3006         else
3007           {
3008             tree ambiguous_decls;
3009
3010             decl = cp_parser_lookup_name (parser, id_expression,
3011                                           none_type,
3012                                           template_p,
3013                                           /*is_namespace=*/false,
3014                                           /*check_dependency=*/true,
3015                                           &ambiguous_decls);
3016             /* If the lookup was ambiguous, an error will already have
3017                been issued.  */
3018             if (ambiguous_decls)
3019               return error_mark_node;
3020
3021             /* In Objective-C++, an instance variable (ivar) may be preferred
3022                to whatever cp_parser_lookup_name() found.  */
3023             decl = objc_lookup_ivar (decl, id_expression);
3024
3025             /* If name lookup gives us a SCOPE_REF, then the
3026                qualifying scope was dependent.  */
3027             if (TREE_CODE (decl) == SCOPE_REF)
3028               return decl;
3029             /* Check to see if DECL is a local variable in a context
3030                where that is forbidden.  */
3031             if (parser->local_variables_forbidden_p
3032                 && local_variable_p (decl))
3033               {
3034                 /* It might be that we only found DECL because we are
3035                    trying to be generous with pre-ISO scoping rules.
3036                    For example, consider:
3037
3038                      int i;
3039                      void g() {
3040                        for (int i = 0; i < 10; ++i) {}
3041                        extern void f(int j = i);
3042                      }
3043
3044                    Here, name look up will originally find the out
3045                    of scope `i'.  We need to issue a warning message,
3046                    but then use the global `i'.  */
3047                 decl = check_for_out_of_scope_variable (decl);
3048                 if (local_variable_p (decl))
3049                   {
3050                     error ("local variable %qD may not appear in this context",
3051                            decl);
3052                     return error_mark_node;
3053                   }
3054               }
3055           }
3056
3057         decl = (finish_id_expression 
3058                 (id_expression, decl, parser->scope,
3059                  idk,
3060                  parser->integral_constant_expression_p,
3061                  parser->allow_non_integral_constant_expression_p,
3062                  &parser->non_integral_constant_expression_p,
3063                  template_p, done, address_p,
3064                  template_arg_p,
3065                  &error_msg));
3066         if (error_msg)
3067           cp_parser_error (parser, error_msg);
3068         return decl;
3069       }
3070
3071       /* Anything else is an error.  */
3072     default:
3073       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3074       if (c_dialect_objc ()
3075           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3076         return cp_parser_objc_expression (parser);
3077
3078       cp_parser_error (parser, "expected primary-expression");
3079       return error_mark_node;
3080     }
3081 }
3082
3083 /* Parse an id-expression.
3084
3085    id-expression:
3086      unqualified-id
3087      qualified-id
3088
3089    qualified-id:
3090      :: [opt] nested-name-specifier template [opt] unqualified-id
3091      :: identifier
3092      :: operator-function-id
3093      :: template-id
3094
3095    Return a representation of the unqualified portion of the
3096    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3097    a `::' or nested-name-specifier.
3098
3099    Often, if the id-expression was a qualified-id, the caller will
3100    want to make a SCOPE_REF to represent the qualified-id.  This
3101    function does not do this in order to avoid wastefully creating
3102    SCOPE_REFs when they are not required.
3103
3104    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3105    `template' keyword.
3106
3107    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3108    uninstantiated templates.
3109
3110    If *TEMPLATE_P is non-NULL, it is set to true iff the
3111    `template' keyword is used to explicitly indicate that the entity
3112    named is a template.
3113
3114    If DECLARATOR_P is true, the id-expression is appearing as part of
3115    a declarator, rather than as part of an expression.  */
3116
3117 static tree
3118 cp_parser_id_expression (cp_parser *parser,
3119                          bool template_keyword_p,
3120                          bool check_dependency_p,
3121                          bool *template_p,
3122                          bool declarator_p)
3123 {
3124   bool global_scope_p;
3125   bool nested_name_specifier_p;
3126
3127   /* Assume the `template' keyword was not used.  */
3128   if (template_p)
3129     *template_p = template_keyword_p;
3130
3131   /* Look for the optional `::' operator.  */
3132   global_scope_p
3133     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3134        != NULL_TREE);
3135   /* Look for the optional nested-name-specifier.  */
3136   nested_name_specifier_p
3137     = (cp_parser_nested_name_specifier_opt (parser,
3138                                             /*typename_keyword_p=*/false,
3139                                             check_dependency_p,
3140                                             /*type_p=*/false,
3141                                             declarator_p)
3142        != NULL_TREE);
3143   /* If there is a nested-name-specifier, then we are looking at
3144      the first qualified-id production.  */
3145   if (nested_name_specifier_p)
3146     {
3147       tree saved_scope;
3148       tree saved_object_scope;
3149       tree saved_qualifying_scope;
3150       tree unqualified_id;
3151       bool is_template;
3152
3153       /* See if the next token is the `template' keyword.  */
3154       if (!template_p)
3155         template_p = &is_template;
3156       *template_p = cp_parser_optional_template_keyword (parser);
3157       /* Name lookup we do during the processing of the
3158          unqualified-id might obliterate SCOPE.  */
3159       saved_scope = parser->scope;
3160       saved_object_scope = parser->object_scope;
3161       saved_qualifying_scope = parser->qualifying_scope;
3162       /* Process the final unqualified-id.  */
3163       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3164                                                  check_dependency_p,
3165                                                  declarator_p);
3166       /* Restore the SAVED_SCOPE for our caller.  */
3167       parser->scope = saved_scope;
3168       parser->object_scope = saved_object_scope;
3169       parser->qualifying_scope = saved_qualifying_scope;
3170
3171       return unqualified_id;
3172     }
3173   /* Otherwise, if we are in global scope, then we are looking at one
3174      of the other qualified-id productions.  */
3175   else if (global_scope_p)
3176     {
3177       cp_token *token;
3178       tree id;
3179
3180       /* Peek at the next token.  */
3181       token = cp_lexer_peek_token (parser->lexer);
3182
3183       /* If it's an identifier, and the next token is not a "<", then
3184          we can avoid the template-id case.  This is an optimization
3185          for this common case.  */
3186       if (token->type == CPP_NAME
3187           && !cp_parser_nth_token_starts_template_argument_list_p
3188                (parser, 2))
3189         return cp_parser_identifier (parser);
3190
3191       cp_parser_parse_tentatively (parser);
3192       /* Try a template-id.  */
3193       id = cp_parser_template_id (parser,
3194                                   /*template_keyword_p=*/false,
3195                                   /*check_dependency_p=*/true,
3196                                   declarator_p);
3197       /* If that worked, we're done.  */
3198       if (cp_parser_parse_definitely (parser))
3199         return id;
3200
3201       /* Peek at the next token.  (Changes in the token buffer may
3202          have invalidated the pointer obtained above.)  */
3203       token = cp_lexer_peek_token (parser->lexer);
3204
3205       switch (token->type)
3206         {
3207         case CPP_NAME:
3208           return cp_parser_identifier (parser);
3209
3210         case CPP_KEYWORD:
3211           if (token->keyword == RID_OPERATOR)
3212             return cp_parser_operator_function_id (parser);
3213           /* Fall through.  */
3214
3215         default:
3216           cp_parser_error (parser, "expected id-expression");
3217           return error_mark_node;
3218         }
3219     }
3220   else
3221     return cp_parser_unqualified_id (parser, template_keyword_p,
3222                                      /*check_dependency_p=*/true,
3223                                      declarator_p);
3224 }
3225
3226 /* Parse an unqualified-id.
3227
3228    unqualified-id:
3229      identifier
3230      operator-function-id
3231      conversion-function-id
3232      ~ class-name
3233      template-id
3234
3235    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3236    keyword, in a construct like `A::template ...'.
3237
3238    Returns a representation of unqualified-id.  For the `identifier'
3239    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3240    production a BIT_NOT_EXPR is returned; the operand of the
3241    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3242    other productions, see the documentation accompanying the
3243    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3244    names are looked up in uninstantiated templates.  If DECLARATOR_P
3245    is true, the unqualified-id is appearing as part of a declarator,
3246    rather than as part of an expression.  */
3247
3248 static tree
3249 cp_parser_unqualified_id (cp_parser* parser,
3250                           bool template_keyword_p,
3251                           bool check_dependency_p,
3252                           bool declarator_p)
3253 {
3254   cp_token *token;
3255
3256   /* Peek at the next token.  */
3257   token = cp_lexer_peek_token (parser->lexer);
3258
3259   switch (token->type)
3260     {
3261     case CPP_NAME:
3262       {
3263         tree id;
3264
3265         /* We don't know yet whether or not this will be a
3266            template-id.  */
3267         cp_parser_parse_tentatively (parser);
3268         /* Try a template-id.  */
3269         id = cp_parser_template_id (parser, template_keyword_p,
3270                                     check_dependency_p,
3271                                     declarator_p);
3272         /* If it worked, we're done.  */
3273         if (cp_parser_parse_definitely (parser))
3274           return id;
3275         /* Otherwise, it's an ordinary identifier.  */
3276         return cp_parser_identifier (parser);
3277       }
3278
3279     case CPP_TEMPLATE_ID:
3280       return cp_parser_template_id (parser, template_keyword_p,
3281                                     check_dependency_p,
3282                                     declarator_p);
3283
3284     case CPP_COMPL:
3285       {
3286         tree type_decl;
3287         tree qualifying_scope;
3288         tree object_scope;
3289         tree scope;
3290         bool done;
3291
3292         /* Consume the `~' token.  */
3293         cp_lexer_consume_token (parser->lexer);
3294         /* Parse the class-name.  The standard, as written, seems to
3295            say that:
3296
3297              template <typename T> struct S { ~S (); };
3298              template <typename T> S<T>::~S() {}
3299
3300            is invalid, since `~' must be followed by a class-name, but
3301            `S<T>' is dependent, and so not known to be a class.
3302            That's not right; we need to look in uninstantiated
3303            templates.  A further complication arises from:
3304
3305              template <typename T> void f(T t) {
3306                t.T::~T();
3307              }
3308
3309            Here, it is not possible to look up `T' in the scope of `T'
3310            itself.  We must look in both the current scope, and the
3311            scope of the containing complete expression.
3312
3313            Yet another issue is:
3314
3315              struct S {
3316                int S;
3317                ~S();
3318              };
3319
3320              S::~S() {}
3321
3322            The standard does not seem to say that the `S' in `~S'
3323            should refer to the type `S' and not the data member
3324            `S::S'.  */
3325
3326         /* DR 244 says that we look up the name after the "~" in the
3327            same scope as we looked up the qualifying name.  That idea
3328            isn't fully worked out; it's more complicated than that.  */
3329         scope = parser->scope;
3330         object_scope = parser->object_scope;
3331         qualifying_scope = parser->qualifying_scope;
3332
3333         /* If the name is of the form "X::~X" it's OK.  */
3334         if (scope && TYPE_P (scope)
3335             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3336             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3337                 == CPP_OPEN_PAREN)
3338             && (cp_lexer_peek_token (parser->lexer)->value
3339                 == TYPE_IDENTIFIER (scope)))
3340           {
3341             cp_lexer_consume_token (parser->lexer);
3342             return build_nt (BIT_NOT_EXPR, scope);
3343           }
3344
3345         /* If there was an explicit qualification (S::~T), first look
3346            in the scope given by the qualification (i.e., S).  */
3347         done = false;
3348         type_decl = NULL_TREE;
3349         if (scope)
3350           {
3351             cp_parser_parse_tentatively (parser);
3352             type_decl = cp_parser_class_name (parser,
3353                                               /*typename_keyword_p=*/false,
3354                                               /*template_keyword_p=*/false,
3355                                               none_type,
3356                                               /*check_dependency=*/false,
3357                                               /*class_head_p=*/false,
3358                                               declarator_p);
3359             if (cp_parser_parse_definitely (parser))
3360               done = true;
3361           }
3362         /* In "N::S::~S", look in "N" as well.  */
3363         if (!done && scope && qualifying_scope)
3364           {
3365             cp_parser_parse_tentatively (parser);
3366             parser->scope = qualifying_scope;
3367             parser->object_scope = NULL_TREE;
3368             parser->qualifying_scope = NULL_TREE;
3369             type_decl
3370               = cp_parser_class_name (parser,
3371                                       /*typename_keyword_p=*/false,
3372                                       /*template_keyword_p=*/false,
3373                                       none_type,
3374                                       /*check_dependency=*/false,
3375                                       /*class_head_p=*/false,
3376                                       declarator_p);
3377             if (cp_parser_parse_definitely (parser))
3378               done = true;
3379           }
3380         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3381         else if (!done && object_scope)
3382           {
3383             cp_parser_parse_tentatively (parser);
3384             parser->scope = object_scope;
3385             parser->object_scope = NULL_TREE;
3386             parser->qualifying_scope = NULL_TREE;
3387             type_decl
3388               = cp_parser_class_name (parser,
3389                                       /*typename_keyword_p=*/false,
3390                                       /*template_keyword_p=*/false,
3391                                       none_type,
3392                                       /*check_dependency=*/false,
3393                                       /*class_head_p=*/false,
3394                                       declarator_p);
3395             if (cp_parser_parse_definitely (parser))
3396               done = true;
3397           }
3398         /* Look in the surrounding context.  */
3399         if (!done)
3400           {
3401             parser->scope = NULL_TREE;
3402             parser->object_scope = NULL_TREE;
3403             parser->qualifying_scope = NULL_TREE;
3404             type_decl
3405               = cp_parser_class_name (parser,
3406                                       /*typename_keyword_p=*/false,
3407                                       /*template_keyword_p=*/false,
3408                                       none_type,
3409                                       /*check_dependency=*/false,
3410                                       /*class_head_p=*/false,
3411                                       declarator_p);
3412           }
3413         /* If an error occurred, assume that the name of the
3414            destructor is the same as the name of the qualifying
3415            class.  That allows us to keep parsing after running
3416            into ill-formed destructor names.  */
3417         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3418           return build_nt (BIT_NOT_EXPR, scope);
3419         else if (type_decl == error_mark_node)
3420           return error_mark_node;
3421
3422         /* [class.dtor]
3423
3424            A typedef-name that names a class shall not be used as the
3425            identifier in the declarator for a destructor declaration.  */
3426         if (declarator_p
3427             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3428             && !DECL_SELF_REFERENCE_P (type_decl)
3429             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3430           error ("typedef-name %qD used as destructor declarator",
3431                  type_decl);
3432
3433         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3434       }
3435
3436     case CPP_KEYWORD:
3437       if (token->keyword == RID_OPERATOR)
3438         {
3439           tree id;
3440
3441           /* This could be a template-id, so we try that first.  */
3442           cp_parser_parse_tentatively (parser);
3443           /* Try a template-id.  */
3444           id = cp_parser_template_id (parser, template_keyword_p,
3445                                       /*check_dependency_p=*/true,
3446                                       declarator_p);
3447           /* If that worked, we're done.  */
3448           if (cp_parser_parse_definitely (parser))
3449             return id;
3450           /* We still don't know whether we're looking at an
3451              operator-function-id or a conversion-function-id.  */
3452           cp_parser_parse_tentatively (parser);
3453           /* Try an operator-function-id.  */
3454           id = cp_parser_operator_function_id (parser);
3455           /* If that didn't work, try a conversion-function-id.  */
3456           if (!cp_parser_parse_definitely (parser))
3457             id = cp_parser_conversion_function_id (parser);
3458
3459           return id;
3460         }
3461       /* Fall through.  */
3462
3463     default:
3464       cp_parser_error (parser, "expected unqualified-id");
3465       return error_mark_node;
3466     }
3467 }
3468
3469 /* Parse an (optional) nested-name-specifier.
3470
3471    nested-name-specifier:
3472      class-or-namespace-name :: nested-name-specifier [opt]
3473      class-or-namespace-name :: template nested-name-specifier [opt]
3474
3475    PARSER->SCOPE should be set appropriately before this function is
3476    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3477    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3478    in name lookups.
3479
3480    Sets PARSER->SCOPE to the class (TYPE) or namespace
3481    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3482    it unchanged if there is no nested-name-specifier.  Returns the new
3483    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3484
3485    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3486    part of a declaration and/or decl-specifier.  */
3487
3488 static tree
3489 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3490                                      bool typename_keyword_p,
3491                                      bool check_dependency_p,
3492                                      bool type_p,
3493                                      bool is_declaration)
3494 {
3495   bool success = false;
3496   tree access_check = NULL_TREE;
3497   cp_token_position start = 0;
3498   cp_token *token;
3499
3500   /* If the next token corresponds to a nested name specifier, there
3501      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3502      false, it may have been true before, in which case something
3503      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3504      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3505      CHECK_DEPENDENCY_P is false, we have to fall through into the
3506      main loop.  */
3507   if (check_dependency_p
3508       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3509     {
3510       cp_parser_pre_parsed_nested_name_specifier (parser);
3511       return parser->scope;
3512     }
3513
3514   /* Remember where the nested-name-specifier starts.  */
3515   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3516     start = cp_lexer_token_position (parser->lexer, false);
3517
3518   push_deferring_access_checks (dk_deferred);
3519
3520   while (true)
3521     {
3522       tree new_scope;
3523       tree old_scope;
3524       tree saved_qualifying_scope;
3525       bool template_keyword_p;
3526
3527       /* Spot cases that cannot be the beginning of a
3528          nested-name-specifier.  */
3529       token = cp_lexer_peek_token (parser->lexer);
3530
3531       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3532          the already parsed nested-name-specifier.  */
3533       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3534         {
3535           /* Grab the nested-name-specifier and continue the loop.  */
3536           cp_parser_pre_parsed_nested_name_specifier (parser);
3537           success = true;
3538           continue;
3539         }
3540
3541       /* Spot cases that cannot be the beginning of a
3542          nested-name-specifier.  On the second and subsequent times
3543          through the loop, we look for the `template' keyword.  */
3544       if (success && token->keyword == RID_TEMPLATE)
3545         ;
3546       /* A template-id can start a nested-name-specifier.  */
3547       else if (token->type == CPP_TEMPLATE_ID)
3548         ;
3549       else
3550         {
3551           /* If the next token is not an identifier, then it is
3552              definitely not a class-or-namespace-name.  */
3553           if (token->type != CPP_NAME)
3554             break;
3555           /* If the following token is neither a `<' (to begin a
3556              template-id), nor a `::', then we are not looking at a
3557              nested-name-specifier.  */
3558           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3559           if (token->type != CPP_SCOPE
3560               && !cp_parser_nth_token_starts_template_argument_list_p
3561                   (parser, 2))
3562             break;
3563         }
3564
3565       /* The nested-name-specifier is optional, so we parse
3566          tentatively.  */
3567       cp_parser_parse_tentatively (parser);
3568
3569       /* Look for the optional `template' keyword, if this isn't the
3570          first time through the loop.  */
3571       if (success)
3572         template_keyword_p = cp_parser_optional_template_keyword (parser);
3573       else
3574         template_keyword_p = false;
3575
3576       /* Save the old scope since the name lookup we are about to do
3577          might destroy it.  */
3578       old_scope = parser->scope;
3579       saved_qualifying_scope = parser->qualifying_scope;
3580       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3581          look up names in "X<T>::I" in order to determine that "Y" is
3582          a template.  So, if we have a typename at this point, we make
3583          an effort to look through it.  */
3584       if (is_declaration
3585           && !typename_keyword_p
3586           && parser->scope
3587           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3588         parser->scope = resolve_typename_type (parser->scope,
3589                                                /*only_current_p=*/false);
3590       /* Parse the qualifying entity.  */
3591       new_scope
3592         = cp_parser_class_or_namespace_name (parser,
3593                                              typename_keyword_p,
3594                                              template_keyword_p,
3595                                              check_dependency_p,
3596                                              type_p,
3597                                              is_declaration);
3598       /* Look for the `::' token.  */
3599       cp_parser_require (parser, CPP_SCOPE, "`::'");
3600
3601       /* If we found what we wanted, we keep going; otherwise, we're
3602          done.  */
3603       if (!cp_parser_parse_definitely (parser))
3604         {
3605           bool error_p = false;
3606
3607           /* Restore the OLD_SCOPE since it was valid before the
3608              failed attempt at finding the last
3609              class-or-namespace-name.  */
3610           parser->scope = old_scope;
3611           parser->qualifying_scope = saved_qualifying_scope;
3612           /* If the next token is an identifier, and the one after
3613              that is a `::', then any valid interpretation would have
3614              found a class-or-namespace-name.  */
3615           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3616                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3617                      == CPP_SCOPE)
3618                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3619                      != CPP_COMPL))
3620             {
3621               token = cp_lexer_consume_token (parser->lexer);
3622               if (!error_p)
3623                 {
3624                   if (!token->ambiguous_p)
3625                     {
3626                       tree decl;
3627                       tree ambiguous_decls;
3628
3629                       decl = cp_parser_lookup_name (parser, token->value,
3630                                                     none_type,
3631                                                     /*is_template=*/false,
3632                                                     /*is_namespace=*/false,
3633                                                     /*check_dependency=*/true,
3634                                                     &ambiguous_decls);
3635                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3636                         error ("%qD used without template parameters", decl);
3637                       else if (ambiguous_decls)
3638                         {
3639                           error ("reference to %qD is ambiguous", 
3640                                  token->value);
3641                           print_candidates (ambiguous_decls);
3642                           decl = error_mark_node;
3643                         }
3644                       else
3645                         cp_parser_name_lookup_error
3646                           (parser, token->value, decl,
3647                            "is not a class or namespace");
3648                     }
3649                   parser->scope = error_mark_node;
3650                   error_p = true;
3651                   /* Treat this as a successful nested-name-specifier
3652                      due to:
3653
3654                      [basic.lookup.qual]
3655
3656                      If the name found is not a class-name (clause
3657                      _class_) or namespace-name (_namespace.def_), the
3658                      program is ill-formed.  */
3659                   success = true;
3660                 }
3661               cp_lexer_consume_token (parser->lexer);
3662             }
3663           break;
3664         }
3665       /* We've found one valid nested-name-specifier.  */
3666       success = true;
3667       /* Name lookup always gives us a DECL.  */
3668       if (TREE_CODE (new_scope) == TYPE_DECL)
3669         new_scope = TREE_TYPE (new_scope);
3670       /* Uses of "template" must be followed by actual templates.  */
3671       if (template_keyword_p
3672           && !(CLASS_TYPE_P (new_scope)
3673                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3674                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3675                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3676           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3677                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3678                    == TEMPLATE_ID_EXPR)))
3679         pedwarn (TYPE_P (new_scope)
3680                  ? "%qT is not a template"
3681                  : "%qD is not a template",
3682                  new_scope);
3683       /* If it is a class scope, try to complete it; we are about to
3684          be looking up names inside the class.  */
3685       if (TYPE_P (new_scope)
3686           /* Since checking types for dependency can be expensive,
3687              avoid doing it if the type is already complete.  */
3688           && !COMPLETE_TYPE_P (new_scope)
3689           /* Do not try to complete dependent types.  */
3690           && !dependent_type_p (new_scope))
3691         new_scope = complete_type (new_scope);
3692       /* Make sure we look in the right scope the next time through
3693          the loop.  */
3694       parser->scope = new_scope;
3695     }
3696
3697   /* Retrieve any deferred checks.  Do not pop this access checks yet
3698      so the memory will not be reclaimed during token replacing below.  */
3699   access_check = get_deferred_access_checks ();
3700
3701   /* If parsing tentatively, replace the sequence of tokens that makes
3702      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3703      token.  That way, should we re-parse the token stream, we will
3704      not have to repeat the effort required to do the parse, nor will
3705      we issue duplicate error messages.  */
3706   if (success && start)
3707     {
3708       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3709
3710       /* Reset the contents of the START token.  */
3711       token->type = CPP_NESTED_NAME_SPECIFIER;
3712       token->value = build_tree_list (access_check, parser->scope);
3713       TREE_TYPE (token->value) = parser->qualifying_scope;
3714       token->keyword = RID_MAX;
3715
3716       /* Purge all subsequent tokens.  */
3717       cp_lexer_purge_tokens_after (parser->lexer, start);
3718     }
3719
3720   pop_deferring_access_checks ();
3721   return success ? parser->scope : NULL_TREE;
3722 }
3723
3724 /* Parse a nested-name-specifier.  See
3725    cp_parser_nested_name_specifier_opt for details.  This function
3726    behaves identically, except that it will an issue an error if no
3727    nested-name-specifier is present.  */
3728
3729 static tree
3730 cp_parser_nested_name_specifier (cp_parser *parser,
3731                                  bool typename_keyword_p,
3732                                  bool check_dependency_p,
3733                                  bool type_p,
3734                                  bool is_declaration)
3735 {
3736   tree scope;
3737
3738   /* Look for the nested-name-specifier.  */
3739   scope = cp_parser_nested_name_specifier_opt (parser,
3740                                                typename_keyword_p,
3741                                                check_dependency_p,
3742                                                type_p,
3743                                                is_declaration);
3744   /* If it was not present, issue an error message.  */
3745   if (!scope)
3746     {
3747       cp_parser_error (parser, "expected nested-name-specifier");
3748       parser->scope = NULL_TREE;
3749     }
3750
3751   return scope;
3752 }
3753
3754 /* Parse a class-or-namespace-name.
3755
3756    class-or-namespace-name:
3757      class-name
3758      namespace-name
3759
3760    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3761    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3762    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3763    TYPE_P is TRUE iff the next name should be taken as a class-name,
3764    even the same name is declared to be another entity in the same
3765    scope.
3766
3767    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3768    specified by the class-or-namespace-name.  If neither is found the
3769    ERROR_MARK_NODE is returned.  */
3770
3771 static tree
3772 cp_parser_class_or_namespace_name (cp_parser *parser,
3773                                    bool typename_keyword_p,
3774                                    bool template_keyword_p,
3775                                    bool check_dependency_p,
3776                                    bool type_p,
3777                                    bool is_declaration)
3778 {
3779   tree saved_scope;
3780   tree saved_qualifying_scope;
3781   tree saved_object_scope;
3782   tree scope;
3783   bool only_class_p;
3784
3785   /* Before we try to parse the class-name, we must save away the
3786      current PARSER->SCOPE since cp_parser_class_name will destroy
3787      it.  */
3788   saved_scope = parser->scope;
3789   saved_qualifying_scope = parser->qualifying_scope;
3790   saved_object_scope = parser->object_scope;
3791   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3792      there is no need to look for a namespace-name.  */
3793   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3794   if (!only_class_p)
3795     cp_parser_parse_tentatively (parser);
3796   scope = cp_parser_class_name (parser,
3797                                 typename_keyword_p,
3798                                 template_keyword_p,
3799                                 type_p ? class_type : none_type,
3800                                 check_dependency_p,
3801                                 /*class_head_p=*/false,
3802                                 is_declaration);
3803   /* If that didn't work, try for a namespace-name.  */
3804   if (!only_class_p && !cp_parser_parse_definitely (parser))
3805     {
3806       /* Restore the saved scope.  */
3807       parser->scope = saved_scope;
3808       parser->qualifying_scope = saved_qualifying_scope;
3809       parser->object_scope = saved_object_scope;
3810       /* If we are not looking at an identifier followed by the scope
3811          resolution operator, then this is not part of a
3812          nested-name-specifier.  (Note that this function is only used
3813          to parse the components of a nested-name-specifier.)  */
3814       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3815           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3816         return error_mark_node;
3817       scope = cp_parser_namespace_name (parser);
3818     }
3819
3820   return scope;
3821 }
3822
3823 /* Parse a postfix-expression.
3824
3825    postfix-expression:
3826      primary-expression
3827      postfix-expression [ expression ]
3828      postfix-expression ( expression-list [opt] )
3829      simple-type-specifier ( expression-list [opt] )
3830      typename :: [opt] nested-name-specifier identifier
3831        ( expression-list [opt] )
3832      typename :: [opt] nested-name-specifier template [opt] template-id
3833        ( expression-list [opt] )
3834      postfix-expression . template [opt] id-expression
3835      postfix-expression -> template [opt] id-expression
3836      postfix-expression . pseudo-destructor-name
3837      postfix-expression -> pseudo-destructor-name
3838      postfix-expression ++
3839      postfix-expression --
3840      dynamic_cast < type-id > ( expression )
3841      static_cast < type-id > ( expression )
3842      reinterpret_cast < type-id > ( expression )
3843      const_cast < type-id > ( expression )
3844      typeid ( expression )
3845      typeid ( type-id )
3846
3847    GNU Extension:
3848
3849    postfix-expression:
3850      ( type-id ) { initializer-list , [opt] }
3851
3852    This extension is a GNU version of the C99 compound-literal
3853    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3854    but they are essentially the same concept.)
3855
3856    If ADDRESS_P is true, the postfix expression is the operand of the
3857    `&' operator.  CAST_P is true if this expression is the target of a
3858    cast.
3859
3860    Returns a representation of the expression.  */
3861
3862 static tree
3863 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3864 {
3865   cp_token *token;
3866   enum rid keyword;
3867   cp_id_kind idk = CP_ID_KIND_NONE;
3868   tree postfix_expression = NULL_TREE;
3869
3870   /* Peek at the next token.  */
3871   token = cp_lexer_peek_token (parser->lexer);
3872   /* Some of the productions are determined by keywords.  */
3873   keyword = token->keyword;
3874   switch (keyword)
3875     {
3876     case RID_DYNCAST:
3877     case RID_STATCAST:
3878     case RID_REINTCAST:
3879     case RID_CONSTCAST:
3880       {
3881         tree type;
3882         tree expression;
3883         const char *saved_message;
3884
3885         /* All of these can be handled in the same way from the point
3886            of view of parsing.  Begin by consuming the token
3887            identifying the cast.  */
3888         cp_lexer_consume_token (parser->lexer);
3889
3890         /* New types cannot be defined in the cast.  */
3891         saved_message = parser->type_definition_forbidden_message;
3892         parser->type_definition_forbidden_message
3893           = "types may not be defined in casts";
3894
3895         /* Look for the opening `<'.  */
3896         cp_parser_require (parser, CPP_LESS, "`<'");
3897         /* Parse the type to which we are casting.  */
3898         type = cp_parser_type_id (parser);
3899         /* Look for the closing `>'.  */
3900         cp_parser_require (parser, CPP_GREATER, "`>'");
3901         /* Restore the old message.  */
3902         parser->type_definition_forbidden_message = saved_message;
3903
3904         /* And the expression which is being cast.  */
3905         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3906         expression = cp_parser_expression (parser, /*cast_p=*/true);
3907         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3908
3909         /* Only type conversions to integral or enumeration types
3910            can be used in constant-expressions.  */
3911         if (parser->integral_constant_expression_p
3912             && !dependent_type_p (type)
3913             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3914             && (cp_parser_non_integral_constant_expression
3915                 (parser,
3916                  "a cast to a type other than an integral or "
3917                  "enumeration type")))
3918           return error_mark_node;
3919
3920         switch (keyword)
3921           {
3922           case RID_DYNCAST:
3923             postfix_expression
3924               = build_dynamic_cast (type, expression);
3925             break;
3926           case RID_STATCAST:
3927             postfix_expression
3928               = build_static_cast (type, expression);
3929             break;
3930           case RID_REINTCAST:
3931             postfix_expression
3932               = build_reinterpret_cast (type, expression);
3933             break;
3934           case RID_CONSTCAST:
3935             postfix_expression
3936               = build_const_cast (type, expression);
3937             break;
3938           default:
3939             gcc_unreachable ();
3940           }
3941       }
3942       break;
3943
3944     case RID_TYPEID:
3945       {
3946         tree type;
3947         const char *saved_message;
3948         bool saved_in_type_id_in_expr_p;
3949
3950         /* Consume the `typeid' token.  */
3951         cp_lexer_consume_token (parser->lexer);
3952         /* Look for the `(' token.  */
3953         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3954         /* Types cannot be defined in a `typeid' expression.  */
3955         saved_message = parser->type_definition_forbidden_message;
3956         parser->type_definition_forbidden_message
3957           = "types may not be defined in a `typeid\' expression";
3958         /* We can't be sure yet whether we're looking at a type-id or an
3959            expression.  */
3960         cp_parser_parse_tentatively (parser);
3961         /* Try a type-id first.  */
3962         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3963         parser->in_type_id_in_expr_p = true;
3964         type = cp_parser_type_id (parser);
3965         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3966         /* Look for the `)' token.  Otherwise, we can't be sure that
3967            we're not looking at an expression: consider `typeid (int
3968            (3))', for example.  */
3969         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3970         /* If all went well, simply lookup the type-id.  */
3971         if (cp_parser_parse_definitely (parser))
3972           postfix_expression = get_typeid (type);
3973         /* Otherwise, fall back to the expression variant.  */
3974         else
3975           {
3976             tree expression;
3977
3978             /* Look for an expression.  */
3979             expression = cp_parser_expression (parser, /*cast_p=*/false);
3980             /* Compute its typeid.  */
3981             postfix_expression = build_typeid (expression);
3982             /* Look for the `)' token.  */
3983             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3984           }
3985         /* `typeid' may not appear in an integral constant expression.  */
3986         if (cp_parser_non_integral_constant_expression(parser,
3987                                                        "`typeid' operator"))
3988           return error_mark_node;
3989         /* Restore the saved message.  */
3990         parser->type_definition_forbidden_message = saved_message;
3991       }
3992       break;
3993
3994     case RID_TYPENAME:
3995       {
3996         tree type;
3997         /* The syntax permitted here is the same permitted for an
3998            elaborated-type-specifier.  */
3999         type = cp_parser_elaborated_type_specifier (parser,
4000                                                     /*is_friend=*/false,
4001                                                     /*is_declaration=*/false);
4002         postfix_expression = cp_parser_functional_cast (parser, type);
4003       }
4004       break;
4005
4006     default:
4007       {
4008         tree type;
4009
4010         /* If the next thing is a simple-type-specifier, we may be
4011            looking at a functional cast.  We could also be looking at
4012            an id-expression.  So, we try the functional cast, and if
4013            that doesn't work we fall back to the primary-expression.  */
4014         cp_parser_parse_tentatively (parser);
4015         /* Look for the simple-type-specifier.  */
4016         type = cp_parser_simple_type_specifier (parser,
4017                                                 /*decl_specs=*/NULL,
4018                                                 CP_PARSER_FLAGS_NONE);
4019         /* Parse the cast itself.  */
4020         if (!cp_parser_error_occurred (parser))
4021           postfix_expression
4022             = cp_parser_functional_cast (parser, type);
4023         /* If that worked, we're done.  */
4024         if (cp_parser_parse_definitely (parser))
4025           break;
4026
4027         /* If the functional-cast didn't work out, try a
4028            compound-literal.  */
4029         if (cp_parser_allow_gnu_extensions_p (parser)
4030             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4031           {
4032             VEC(constructor_elt,gc) *initializer_list = NULL;
4033             bool saved_in_type_id_in_expr_p;
4034
4035             cp_parser_parse_tentatively (parser);
4036             /* Consume the `('.  */
4037             cp_lexer_consume_token (parser->lexer);
4038             /* Parse the type.  */
4039             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4040             parser->in_type_id_in_expr_p = true;
4041             type = cp_parser_type_id (parser);
4042             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4043             /* Look for the `)'.  */
4044             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4045             /* Look for the `{'.  */
4046             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4047             /* If things aren't going well, there's no need to
4048                keep going.  */
4049             if (!cp_parser_error_occurred (parser))
4050               {
4051                 bool non_constant_p;
4052                 /* Parse the initializer-list.  */
4053                 initializer_list
4054                   = cp_parser_initializer_list (parser, &non_constant_p);
4055                 /* Allow a trailing `,'.  */
4056                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4057                   cp_lexer_consume_token (parser->lexer);
4058                 /* Look for the final `}'.  */
4059                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4060               }
4061             /* If that worked, we're definitely looking at a
4062                compound-literal expression.  */
4063             if (cp_parser_parse_definitely (parser))
4064               {
4065                 /* Warn the user that a compound literal is not
4066                    allowed in standard C++.  */
4067                 if (pedantic)
4068                   pedwarn ("ISO C++ forbids compound-literals");
4069                 /* Form the representation of the compound-literal.  */
4070                 postfix_expression
4071                   = finish_compound_literal (type, initializer_list);
4072                 break;
4073               }
4074           }
4075
4076         /* It must be a primary-expression.  */
4077         postfix_expression 
4078           = cp_parser_primary_expression (parser, address_p, cast_p, 
4079                                           /*template_arg_p=*/false,
4080                                           &idk);
4081       }
4082       break;
4083     }
4084
4085   /* Keep looping until the postfix-expression is complete.  */
4086   while (true)
4087     {
4088       if (idk == CP_ID_KIND_UNQUALIFIED
4089           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4090           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4091         /* It is not a Koenig lookup function call.  */
4092         postfix_expression
4093           = unqualified_name_lookup_error (postfix_expression);
4094
4095       /* Peek at the next token.  */
4096       token = cp_lexer_peek_token (parser->lexer);
4097
4098       switch (token->type)
4099         {
4100         case CPP_OPEN_SQUARE:
4101           postfix_expression
4102             = cp_parser_postfix_open_square_expression (parser,
4103                                                         postfix_expression,
4104                                                         false);
4105           idk = CP_ID_KIND_NONE;
4106           break;
4107
4108         case CPP_OPEN_PAREN:
4109           /* postfix-expression ( expression-list [opt] ) */
4110           {
4111             bool koenig_p;
4112             bool is_builtin_constant_p;
4113             bool saved_integral_constant_expression_p = false;
4114             bool saved_non_integral_constant_expression_p = false;
4115             tree args;
4116
4117             is_builtin_constant_p
4118               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4119             if (is_builtin_constant_p)
4120               {
4121                 /* The whole point of __builtin_constant_p is to allow
4122                    non-constant expressions to appear as arguments.  */
4123                 saved_integral_constant_expression_p
4124                   = parser->integral_constant_expression_p;
4125                 saved_non_integral_constant_expression_p
4126                   = parser->non_integral_constant_expression_p;
4127                 parser->integral_constant_expression_p = false;
4128               }
4129             args = (cp_parser_parenthesized_expression_list
4130                     (parser, /*is_attribute_list=*/false,
4131                      /*cast_p=*/false,
4132                      /*non_constant_p=*/NULL));
4133             if (is_builtin_constant_p)
4134               {
4135                 parser->integral_constant_expression_p
4136                   = saved_integral_constant_expression_p;
4137                 parser->non_integral_constant_expression_p
4138                   = saved_non_integral_constant_expression_p;
4139               }
4140
4141             if (args == error_mark_node)
4142               {
4143                 postfix_expression = error_mark_node;
4144                 break;
4145               }
4146
4147             /* Function calls are not permitted in
4148                constant-expressions.  */
4149             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4150                 && cp_parser_non_integral_constant_expression (parser,
4151                                                                "a function call"))
4152               {
4153                 postfix_expression = error_mark_node;
4154                 break;
4155               }
4156
4157             koenig_p = false;
4158             if (idk == CP_ID_KIND_UNQUALIFIED)
4159               {
4160                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4161                   {
4162                     if (args)
4163                       {
4164                         koenig_p = true;
4165                         postfix_expression
4166                           = perform_koenig_lookup (postfix_expression, args);
4167                       }
4168                     else
4169                       postfix_expression
4170                         = unqualified_fn_lookup_error (postfix_expression);
4171                   }
4172                 /* We do not perform argument-dependent lookup if
4173                    normal lookup finds a non-function, in accordance
4174                    with the expected resolution of DR 218.  */
4175                 else if (args && is_overloaded_fn (postfix_expression))
4176                   {
4177                     tree fn = get_first_fn (postfix_expression);
4178
4179                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4180                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4181
4182                     /* Only do argument dependent lookup if regular
4183                        lookup does not find a set of member functions.
4184                        [basic.lookup.koenig]/2a  */
4185                     if (!DECL_FUNCTION_MEMBER_P (fn))
4186                       {
4187                         koenig_p = true;
4188                         postfix_expression
4189                           = perform_koenig_lookup (postfix_expression, args);
4190                       }
4191                   }
4192               }
4193
4194             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4195               {
4196                 tree instance = TREE_OPERAND (postfix_expression, 0);
4197                 tree fn = TREE_OPERAND (postfix_expression, 1);
4198
4199                 if (processing_template_decl
4200                     && (type_dependent_expression_p (instance)
4201                         || (!BASELINK_P (fn)
4202                             && TREE_CODE (fn) != FIELD_DECL)
4203                         || type_dependent_expression_p (fn)
4204                         || any_type_dependent_arguments_p (args)))
4205                   {
4206                     postfix_expression
4207                       = build_min_nt (CALL_EXPR, postfix_expression,
4208                                       args, NULL_TREE);
4209                     break;
4210                   }
4211
4212                 if (BASELINK_P (fn))
4213                   postfix_expression
4214                     = (build_new_method_call
4215                        (instance, fn, args, NULL_TREE,
4216                         (idk == CP_ID_KIND_QUALIFIED
4217                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4218                 else
4219                   postfix_expression
4220                     = finish_call_expr (postfix_expression, args,
4221                                         /*disallow_virtual=*/false,
4222                                         /*koenig_p=*/false);
4223               }
4224             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4225                      || TREE_CODE (postfix_expression) == MEMBER_REF
4226                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4227               postfix_expression = (build_offset_ref_call_from_tree
4228                                     (postfix_expression, args));
4229             else if (idk == CP_ID_KIND_QUALIFIED)
4230               /* A call to a static class member, or a namespace-scope
4231                  function.  */
4232               postfix_expression
4233                 = finish_call_expr (postfix_expression, args,
4234                                     /*disallow_virtual=*/true,
4235                                     koenig_p);
4236             else
4237               /* All other function calls.  */
4238               postfix_expression
4239                 = finish_call_expr (postfix_expression, args,
4240                                     /*disallow_virtual=*/false,
4241                                     koenig_p);
4242
4243             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4244             idk = CP_ID_KIND_NONE;
4245           }
4246           break;
4247
4248         case CPP_DOT:
4249         case CPP_DEREF:
4250           /* postfix-expression . template [opt] id-expression
4251              postfix-expression . pseudo-destructor-name
4252              postfix-expression -> template [opt] id-expression
4253              postfix-expression -> pseudo-destructor-name */
4254
4255           /* Consume the `.' or `->' operator.  */
4256           cp_lexer_consume_token (parser->lexer);
4257
4258           postfix_expression
4259             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4260                                                       postfix_expression,
4261                                                       false, &idk);
4262           break;
4263
4264         case CPP_PLUS_PLUS:
4265           /* postfix-expression ++  */
4266           /* Consume the `++' token.  */
4267           cp_lexer_consume_token (parser->lexer);
4268           /* Generate a representation for the complete expression.  */
4269           postfix_expression
4270             = finish_increment_expr (postfix_expression,
4271                                      POSTINCREMENT_EXPR);
4272           /* Increments may not appear in constant-expressions.  */
4273           if (cp_parser_non_integral_constant_expression (parser,
4274                                                           "an increment"))
4275             postfix_expression = error_mark_node;
4276           idk = CP_ID_KIND_NONE;
4277           break;
4278
4279         case CPP_MINUS_MINUS:
4280           /* postfix-expression -- */
4281           /* Consume the `--' token.  */
4282           cp_lexer_consume_token (parser->lexer);
4283           /* Generate a representation for the complete expression.  */
4284           postfix_expression
4285             = finish_increment_expr (postfix_expression,
4286                                      POSTDECREMENT_EXPR);
4287           /* Decrements may not appear in constant-expressions.  */
4288           if (cp_parser_non_integral_constant_expression (parser,
4289                                                           "a decrement"))
4290             postfix_expression = error_mark_node;
4291           idk = CP_ID_KIND_NONE;
4292           break;
4293
4294         default:
4295           return postfix_expression;
4296         }
4297     }
4298
4299   /* We should never get here.  */
4300   gcc_unreachable ();
4301   return error_mark_node;
4302 }
4303
4304 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4305    by cp_parser_builtin_offsetof.  We're looking for
4306
4307      postfix-expression [ expression ]
4308
4309    FOR_OFFSETOF is set if we're being called in that context, which
4310    changes how we deal with integer constant expressions.  */
4311
4312 static tree
4313 cp_parser_postfix_open_square_expression (cp_parser *parser,
4314                                           tree postfix_expression,
4315                                           bool for_offsetof)
4316 {
4317   tree index;
4318
4319   /* Consume the `[' token.  */
4320   cp_lexer_consume_token (parser->lexer);
4321
4322   /* Parse the index expression.  */
4323   /* ??? For offsetof, there is a question of what to allow here.  If
4324      offsetof is not being used in an integral constant expression context,
4325      then we *could* get the right answer by computing the value at runtime.
4326      If we are in an integral constant expression context, then we might
4327      could accept any constant expression; hard to say without analysis.
4328      Rather than open the barn door too wide right away, allow only integer
4329      constant expressions here.  */
4330   if (for_offsetof)
4331     index = cp_parser_constant_expression (parser, false, NULL);
4332   else
4333     index = cp_parser_expression (parser, /*cast_p=*/false);
4334
4335   /* Look for the closing `]'.  */
4336   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4337
4338   /* Build the ARRAY_REF.  */
4339   postfix_expression = grok_array_decl (postfix_expression, index);
4340
4341   /* When not doing offsetof, array references are not permitted in
4342      constant-expressions.  */
4343   if (!for_offsetof
4344       && (cp_parser_non_integral_constant_expression
4345           (parser, "an array reference")))
4346     postfix_expression = error_mark_node;
4347
4348   return postfix_expression;
4349 }
4350
4351 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4352    by cp_parser_builtin_offsetof.  We're looking for
4353
4354      postfix-expression . template [opt] id-expression
4355      postfix-expression . pseudo-destructor-name
4356      postfix-expression -> template [opt] id-expression
4357      postfix-expression -> pseudo-destructor-name
4358
4359    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4360    limits what of the above we'll actually accept, but nevermind.
4361    TOKEN_TYPE is the "." or "->" token, which will already have been
4362    removed from the stream.  */
4363
4364 static tree
4365 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4366                                         enum cpp_ttype token_type,
4367                                         tree postfix_expression,
4368                                         bool for_offsetof, cp_id_kind *idk)
4369 {
4370   tree name;
4371   bool dependent_p;
4372   bool pseudo_destructor_p;
4373   tree scope = NULL_TREE;
4374
4375   /* If this is a `->' operator, dereference the pointer.  */
4376   if (token_type == CPP_DEREF)
4377     postfix_expression = build_x_arrow (postfix_expression);
4378   /* Check to see whether or not the expression is type-dependent.  */
4379   dependent_p = type_dependent_expression_p (postfix_expression);
4380   /* The identifier following the `->' or `.' is not qualified.  */
4381   parser->scope = NULL_TREE;
4382   parser->qualifying_scope = NULL_TREE;
4383   parser->object_scope = NULL_TREE;
4384   *idk = CP_ID_KIND_NONE;
4385   /* Enter the scope corresponding to the type of the object
4386      given by the POSTFIX_EXPRESSION.  */
4387   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4388     {
4389       scope = TREE_TYPE (postfix_expression);
4390       /* According to the standard, no expression should ever have
4391          reference type.  Unfortunately, we do not currently match
4392          the standard in this respect in that our internal representation
4393          of an expression may have reference type even when the standard
4394          says it does not.  Therefore, we have to manually obtain the
4395          underlying type here.  */
4396       scope = non_reference (scope);
4397       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4398       if (scope == unknown_type_node)
4399         {
4400           error ("%qE does not have class type", postfix_expression);
4401           scope = NULL_TREE;
4402         }
4403       else
4404         scope = complete_type_or_else (scope, NULL_TREE);
4405       /* Let the name lookup machinery know that we are processing a
4406          class member access expression.  */
4407       parser->context->object_type = scope;
4408       /* If something went wrong, we want to be able to discern that case,
4409          as opposed to the case where there was no SCOPE due to the type
4410          of expression being dependent.  */
4411       if (!scope)
4412         scope = error_mark_node;
4413       /* If the SCOPE was erroneous, make the various semantic analysis
4414          functions exit quickly -- and without issuing additional error
4415          messages.  */
4416       if (scope == error_mark_node)
4417         postfix_expression = error_mark_node;
4418     }
4419
4420   /* Assume this expression is not a pseudo-destructor access.  */
4421   pseudo_destructor_p = false;
4422
4423   /* If the SCOPE is a scalar type, then, if this is a valid program,
4424      we must be looking at a pseudo-destructor-name.  */
4425   if (scope && SCALAR_TYPE_P (scope))
4426     {
4427       tree s;
4428       tree type;
4429
4430       cp_parser_parse_tentatively (parser);
4431       /* Parse the pseudo-destructor-name.  */
4432       s = NULL_TREE;
4433       cp_parser_pseudo_destructor_name (parser, &s, &type);
4434       if (cp_parser_parse_definitely (parser))
4435         {
4436           pseudo_destructor_p = true;
4437           postfix_expression
4438             = finish_pseudo_destructor_expr (postfix_expression,
4439                                              s, TREE_TYPE (type));
4440         }
4441     }
4442
4443   if (!pseudo_destructor_p)
4444     {
4445       /* If the SCOPE is not a scalar type, we are looking at an
4446          ordinary class member access expression, rather than a
4447          pseudo-destructor-name.  */
4448       bool template_p;
4449       /* Parse the id-expression.  */
4450       name = (cp_parser_id_expression 
4451               (parser, 
4452                cp_parser_optional_template_keyword (parser),
4453                /*check_dependency_p=*/true,
4454                &template_p,
4455                /*declarator_p=*/false));
4456       /* In general, build a SCOPE_REF if the member name is qualified.
4457          However, if the name was not dependent and has already been
4458          resolved; there is no need to build the SCOPE_REF.  For example;
4459
4460              struct X { void f(); };
4461              template <typename T> void f(T* t) { t->X::f(); }
4462
4463          Even though "t" is dependent, "X::f" is not and has been resolved
4464          to a BASELINK; there is no need to include scope information.  */
4465
4466       /* But we do need to remember that there was an explicit scope for
4467          virtual function calls.  */
4468       if (parser->scope)
4469         *idk = CP_ID_KIND_QUALIFIED;
4470
4471       /* If the name is a template-id that names a type, we will get a
4472          TYPE_DECL here.  That is invalid code.  */
4473       if (TREE_CODE (name) == TYPE_DECL)
4474         {
4475           error ("invalid use of %qD", name);
4476           postfix_expression = error_mark_node;
4477         }
4478       else
4479         {
4480           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4481             {
4482               name = build_qualified_name (/*type=*/NULL_TREE,
4483                                            parser->scope,
4484                                            name,
4485                                            template_p);
4486               parser->scope = NULL_TREE;
4487               parser->qualifying_scope = NULL_TREE;
4488               parser->object_scope = NULL_TREE;
4489             }
4490           if (scope && name && BASELINK_P (name))
4491             adjust_result_of_qualified_name_lookup
4492               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4493           postfix_expression
4494             = finish_class_member_access_expr (postfix_expression, name,
4495                                                template_p);
4496         }
4497     }
4498
4499   /* We no longer need to look up names in the scope of the object on
4500      the left-hand side of the `.' or `->' operator.  */
4501   parser->context->object_type = NULL_TREE;
4502
4503   /* Outside of offsetof, these operators may not appear in
4504      constant-expressions.  */
4505   if (!for_offsetof
4506       && (cp_parser_non_integral_constant_expression
4507           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4508     postfix_expression = error_mark_node;
4509
4510   return postfix_expression;
4511 }
4512
4513 /* Parse a parenthesized expression-list.
4514
4515    expression-list:
4516      assignment-expression
4517      expression-list, assignment-expression
4518
4519    attribute-list:
4520      expression-list
4521      identifier
4522      identifier, expression-list
4523
4524    CAST_P is true if this expression is the target of a cast.
4525
4526    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4527    representation of an assignment-expression.  Note that a TREE_LIST
4528    is returned even if there is only a single expression in the list.
4529    error_mark_node is returned if the ( and or ) are
4530    missing. NULL_TREE is returned on no expressions. The parentheses
4531    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4532    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4533    indicates whether or not all of the expressions in the list were
4534    constant.  */
4535
4536 static tree
4537 cp_parser_parenthesized_expression_list (cp_parser* parser,
4538                                          bool is_attribute_list,
4539                                          bool cast_p,
4540                                          bool *non_constant_p)
4541 {
4542   tree expression_list = NULL_TREE;
4543   bool fold_expr_p = is_attribute_list;
4544   tree identifier = NULL_TREE;
4545
4546   /* Assume all the expressions will be constant.  */
4547   if (non_constant_p)
4548     *non_constant_p = false;
4549
4550   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4551     return error_mark_node;
4552
4553   /* Consume expressions until there are no more.  */
4554   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4555     while (true)
4556       {
4557         tree expr;
4558
4559         /* At the beginning of attribute lists, check to see if the
4560            next token is an identifier.  */
4561         if (is_attribute_list
4562             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4563           {
4564             cp_token *token;
4565
4566             /* Consume the identifier.  */
4567             token = cp_lexer_consume_token (parser->lexer);
4568             /* Save the identifier.  */
4569             identifier = token->value;
4570           }
4571         else
4572           {
4573             /* Parse the next assignment-expression.  */
4574             if (non_constant_p)
4575               {
4576                 bool expr_non_constant_p;
4577                 expr = (cp_parser_constant_expression
4578                         (parser, /*allow_non_constant_p=*/true,
4579                          &expr_non_constant_p));
4580                 if (expr_non_constant_p)
4581                   *non_constant_p = true;
4582               }
4583             else
4584               expr = cp_parser_assignment_expression (parser, cast_p);
4585
4586             if (fold_expr_p)
4587               expr = fold_non_dependent_expr (expr);
4588
4589              /* Add it to the list.  We add error_mark_node
4590                 expressions to the list, so that we can still tell if
4591                 the correct form for a parenthesized expression-list
4592                 is found. That gives better errors.  */
4593             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4594
4595             if (expr == error_mark_node)
4596               goto skip_comma;
4597           }
4598
4599         /* After the first item, attribute lists look the same as
4600            expression lists.  */
4601         is_attribute_list = false;
4602
4603       get_comma:;
4604         /* If the next token isn't a `,', then we are done.  */
4605         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4606           break;
4607
4608         /* Otherwise, consume the `,' and keep going.  */
4609         cp_lexer_consume_token (parser->lexer);
4610       }
4611
4612   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4613     {
4614       int ending;
4615
4616     skip_comma:;
4617       /* We try and resync to an unnested comma, as that will give the
4618          user better diagnostics.  */
4619       ending = cp_parser_skip_to_closing_parenthesis (parser,
4620                                                       /*recovering=*/true,
4621                                                       /*or_comma=*/true,
4622                                                       /*consume_paren=*/true);
4623       if (ending < 0)
4624         goto get_comma;
4625       if (!ending)
4626         return error_mark_node;
4627     }
4628
4629   /* We built up the list in reverse order so we must reverse it now.  */
4630   expression_list = nreverse (expression_list);
4631   if (identifier)
4632     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4633
4634   return expression_list;
4635 }
4636
4637 /* Parse a pseudo-destructor-name.
4638
4639    pseudo-destructor-name:
4640      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4641      :: [opt] nested-name-specifier template template-id :: ~ type-name
4642      :: [opt] nested-name-specifier [opt] ~ type-name
4643
4644    If either of the first two productions is used, sets *SCOPE to the
4645    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4646    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4647    or ERROR_MARK_NODE if the parse fails.  */
4648
4649 static void
4650 cp_parser_pseudo_destructor_name (cp_parser* parser,
4651                                   tree* scope,
4652                                   tree* type)
4653 {
4654   bool nested_name_specifier_p;
4655
4656   /* Assume that things will not work out.  */
4657   *type = error_mark_node;
4658
4659   /* Look for the optional `::' operator.  */
4660   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4661   /* Look for the optional nested-name-specifier.  */
4662   nested_name_specifier_p
4663     = (cp_parser_nested_name_specifier_opt (parser,
4664                                             /*typename_keyword_p=*/false,
4665                                             /*check_dependency_p=*/true,
4666                                             /*type_p=*/false,
4667                                             /*is_declaration=*/true)
4668        != NULL_TREE);
4669   /* Now, if we saw a nested-name-specifier, we might be doing the
4670      second production.  */
4671   if (nested_name_specifier_p
4672       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4673     {
4674       /* Consume the `template' keyword.  */
4675       cp_lexer_consume_token (parser->lexer);
4676       /* Parse the template-id.  */
4677       cp_parser_template_id (parser,
4678                              /*template_keyword_p=*/true,
4679                              /*check_dependency_p=*/false,
4680                              /*is_declaration=*/true);
4681       /* Look for the `::' token.  */
4682       cp_parser_require (parser, CPP_SCOPE, "`::'");
4683     }
4684   /* If the next token is not a `~', then there might be some
4685      additional qualification.  */
4686   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4687     {
4688       /* Look for the type-name.  */
4689       *scope = TREE_TYPE (cp_parser_type_name (parser));
4690
4691       if (*scope == error_mark_node)
4692         return;
4693
4694       /* If we don't have ::~, then something has gone wrong.  Since
4695          the only caller of this function is looking for something
4696          after `.' or `->' after a scalar type, most likely the
4697          program is trying to get a member of a non-aggregate
4698          type.  */
4699       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4700           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4701         {
4702           cp_parser_error (parser, "request for member of non-aggregate type");
4703           return;
4704         }
4705
4706       /* Look for the `::' token.  */
4707       cp_parser_require (parser, CPP_SCOPE, "`::'");
4708     }
4709   else
4710     *scope = NULL_TREE;
4711
4712   /* Look for the `~'.  */
4713   cp_parser_require (parser, CPP_COMPL, "`~'");
4714   /* Look for the type-name again.  We are not responsible for
4715      checking that it matches the first type-name.  */
4716   *type = cp_parser_type_name (parser);
4717 }
4718
4719 /* Parse a unary-expression.
4720
4721    unary-expression:
4722      postfix-expression
4723      ++ cast-expression
4724      -- cast-expression
4725      unary-operator cast-expression
4726      sizeof unary-expression
4727      sizeof ( type-id )
4728      new-expression
4729      delete-expression
4730
4731    GNU Extensions:
4732
4733    unary-expression:
4734      __extension__ cast-expression
4735      __alignof__ unary-expression
4736      __alignof__ ( type-id )
4737      __real__ cast-expression
4738      __imag__ cast-expression
4739      && identifier
4740
4741    ADDRESS_P is true iff the unary-expression is appearing as the
4742    operand of the `&' operator.   CAST_P is true if this expression is
4743    the target of a cast.
4744
4745    Returns a representation of the expression.  */
4746
4747 static tree
4748 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4749 {
4750   cp_token *token;
4751   enum tree_code unary_operator;
4752
4753   /* Peek at the next token.  */
4754   token = cp_lexer_peek_token (parser->lexer);
4755   /* Some keywords give away the kind of expression.  */
4756   if (token->type == CPP_KEYWORD)
4757     {
4758       enum rid keyword = token->keyword;
4759
4760       switch (keyword)
4761         {
4762         case RID_ALIGNOF:
4763         case RID_SIZEOF:
4764           {
4765             tree operand;
4766             enum tree_code op;
4767
4768             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4769             /* Consume the token.  */
4770             cp_lexer_consume_token (parser->lexer);
4771             /* Parse the operand.  */
4772             operand = cp_parser_sizeof_operand (parser, keyword);
4773
4774             if (TYPE_P (operand))
4775               return cxx_sizeof_or_alignof_type (operand, op, true);
4776             else
4777               return cxx_sizeof_or_alignof_expr (operand, op);
4778           }
4779
4780         case RID_NEW:
4781           return cp_parser_new_expression (parser);
4782
4783         case RID_DELETE:
4784           return cp_parser_delete_expression (parser);
4785
4786         case RID_EXTENSION:
4787           {
4788             /* The saved value of the PEDANTIC flag.  */
4789             int saved_pedantic;
4790             tree expr;
4791
4792             /* Save away the PEDANTIC flag.  */
4793             cp_parser_extension_opt (parser, &saved_pedantic);
4794             /* Parse the cast-expression.  */
4795             expr = cp_parser_simple_cast_expression (parser);
4796             /* Restore the PEDANTIC flag.  */
4797             pedantic = saved_pedantic;
4798
4799             return expr;
4800           }
4801
4802         case RID_REALPART:
4803         case RID_IMAGPART:
4804           {
4805             tree expression;
4806
4807             /* Consume the `__real__' or `__imag__' token.  */
4808             cp_lexer_consume_token (parser->lexer);
4809             /* Parse the cast-expression.  */
4810             expression = cp_parser_simple_cast_expression (parser);
4811             /* Create the complete representation.  */
4812             return build_x_unary_op ((keyword == RID_REALPART
4813                                       ? REALPART_EXPR : IMAGPART_EXPR),
4814                                      expression);
4815           }
4816           break;
4817
4818         default:
4819           break;
4820         }
4821     }
4822
4823   /* Look for the `:: new' and `:: delete', which also signal the
4824      beginning of a new-expression, or delete-expression,
4825      respectively.  If the next token is `::', then it might be one of
4826      these.  */
4827   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4828     {
4829       enum rid keyword;
4830
4831       /* See if the token after the `::' is one of the keywords in
4832          which we're interested.  */
4833       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4834       /* If it's `new', we have a new-expression.  */
4835       if (keyword == RID_NEW)
4836         return cp_parser_new_expression (parser);
4837       /* Similarly, for `delete'.  */
4838       else if (keyword == RID_DELETE)
4839         return cp_parser_delete_expression (parser);
4840     }
4841
4842   /* Look for a unary operator.  */
4843   unary_operator = cp_parser_unary_operator (token);
4844   /* The `++' and `--' operators can be handled similarly, even though
4845      they are not technically unary-operators in the grammar.  */
4846   if (unary_operator == ERROR_MARK)
4847     {
4848       if (token->type == CPP_PLUS_PLUS)
4849         unary_operator = PREINCREMENT_EXPR;
4850       else if (token->type == CPP_MINUS_MINUS)
4851         unary_operator = PREDECREMENT_EXPR;
4852       /* Handle the GNU address-of-label extension.  */
4853       else if (cp_parser_allow_gnu_extensions_p (parser)
4854                && token->type == CPP_AND_AND)
4855         {
4856           tree identifier;
4857
4858           /* Consume the '&&' token.  */
4859           cp_lexer_consume_token (parser->lexer);
4860           /* Look for the identifier.  */
4861           identifier = cp_parser_identifier (parser);
4862           /* Create an expression representing the address.  */
4863           return finish_label_address_expr (identifier);
4864         }
4865     }
4866   if (unary_operator != ERROR_MARK)
4867     {
4868       tree cast_expression;
4869       tree expression = error_mark_node;
4870       const char *non_constant_p = NULL;
4871
4872       /* Consume the operator token.  */
4873       token = cp_lexer_consume_token (parser->lexer);
4874       /* Parse the cast-expression.  */
4875       cast_expression
4876         = cp_parser_cast_expression (parser,
4877                                      unary_operator == ADDR_EXPR,
4878                                      /*cast_p=*/false);
4879       /* Now, build an appropriate representation.  */
4880       switch (unary_operator)
4881         {
4882         case INDIRECT_REF:
4883           non_constant_p = "`*'";
4884           expression = build_x_indirect_ref (cast_expression, "unary *");
4885           break;
4886
4887         case ADDR_EXPR:
4888           non_constant_p = "`&'";
4889           /* Fall through.  */
4890         case BIT_NOT_EXPR:
4891           expression = build_x_unary_op (unary_operator, cast_expression);
4892           break;
4893
4894         case PREINCREMENT_EXPR:
4895         case PREDECREMENT_EXPR:
4896           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4897                             ? "`++'" : "`--'");
4898           /* Fall through.  */
4899         case UNARY_PLUS_EXPR:
4900         case NEGATE_EXPR:
4901         case TRUTH_NOT_EXPR:
4902           expression = finish_unary_op_expr (unary_operator, cast_expression);
4903           break;
4904
4905         default:
4906           gcc_unreachable ();
4907         }
4908
4909       if (non_constant_p
4910           && cp_parser_non_integral_constant_expression (parser,
4911                                                          non_constant_p))
4912         expression = error_mark_node;
4913
4914       return expression;
4915     }
4916
4917   return cp_parser_postfix_expression (parser, address_p, cast_p);
4918 }
4919
4920 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4921    unary-operator, the corresponding tree code is returned.  */
4922
4923 static enum tree_code
4924 cp_parser_unary_operator (cp_token* token)
4925 {
4926   switch (token->type)
4927     {
4928     case CPP_MULT:
4929       return INDIRECT_REF;
4930
4931     case CPP_AND:
4932       return ADDR_EXPR;
4933
4934     case CPP_PLUS:
4935       return UNARY_PLUS_EXPR;
4936
4937     case CPP_MINUS:
4938       return NEGATE_EXPR;
4939
4940     case CPP_NOT:
4941       return TRUTH_NOT_EXPR;
4942
4943     case CPP_COMPL:
4944       return BIT_NOT_EXPR;
4945
4946     default:
4947       return ERROR_MARK;
4948     }
4949 }
4950
4951 /* Parse a new-expression.
4952
4953    new-expression:
4954      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4955      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4956
4957    Returns a representation of the expression.  */
4958
4959 static tree
4960 cp_parser_new_expression (cp_parser* parser)
4961 {
4962   bool global_scope_p;
4963   tree placement;
4964   tree type;
4965   tree initializer;
4966   tree nelts;
4967
4968   /* Look for the optional `::' operator.  */
4969   global_scope_p
4970     = (cp_parser_global_scope_opt (parser,
4971                                    /*current_scope_valid_p=*/false)
4972        != NULL_TREE);
4973   /* Look for the `new' operator.  */
4974   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4975   /* There's no easy way to tell a new-placement from the
4976      `( type-id )' construct.  */
4977   cp_parser_parse_tentatively (parser);
4978   /* Look for a new-placement.  */
4979   placement = cp_parser_new_placement (parser);
4980   /* If that didn't work out, there's no new-placement.  */
4981   if (!cp_parser_parse_definitely (parser))
4982     placement = NULL_TREE;
4983
4984   /* If the next token is a `(', then we have a parenthesized
4985      type-id.  */
4986   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4987     {
4988       /* Consume the `('.  */
4989       cp_lexer_consume_token (parser->lexer);
4990       /* Parse the type-id.  */
4991       type = cp_parser_type_id (parser);
4992       /* Look for the closing `)'.  */
4993       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4994       /* There should not be a direct-new-declarator in this production,
4995          but GCC used to allowed this, so we check and emit a sensible error
4996          message for this case.  */
4997       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4998         {
4999           error ("array bound forbidden after parenthesized type-id");
5000           inform ("try removing the parentheses around the type-id");
5001           cp_parser_direct_new_declarator (parser);
5002         }
5003       nelts = NULL_TREE;
5004     }
5005   /* Otherwise, there must be a new-type-id.  */
5006   else
5007     type = cp_parser_new_type_id (parser, &nelts);
5008
5009   /* If the next token is a `(', then we have a new-initializer.  */
5010   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5011     initializer = cp_parser_new_initializer (parser);
5012   else
5013     initializer = NULL_TREE;
5014
5015   /* A new-expression may not appear in an integral constant
5016      expression.  */
5017   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5018     return error_mark_node;
5019
5020   /* Create a representation of the new-expression.  */
5021   return build_new (placement, type, nelts, initializer, global_scope_p);
5022 }
5023
5024 /* Parse a new-placement.
5025
5026    new-placement:
5027      ( expression-list )
5028
5029    Returns the same representation as for an expression-list.  */
5030
5031 static tree
5032 cp_parser_new_placement (cp_parser* parser)
5033 {
5034   tree expression_list;
5035
5036   /* Parse the expression-list.  */
5037   expression_list = (cp_parser_parenthesized_expression_list
5038                      (parser, false, /*cast_p=*/false,
5039                       /*non_constant_p=*/NULL));
5040
5041   return expression_list;
5042 }
5043
5044 /* Parse a new-type-id.
5045
5046    new-type-id:
5047      type-specifier-seq new-declarator [opt]
5048
5049    Returns the TYPE allocated.  If the new-type-id indicates an array
5050    type, *NELTS is set to the number of elements in the last array
5051    bound; the TYPE will not include the last array bound.  */
5052
5053 static tree
5054 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5055 {
5056   cp_decl_specifier_seq type_specifier_seq;
5057   cp_declarator *new_declarator;
5058   cp_declarator *declarator;
5059   cp_declarator *outer_declarator;
5060   const char *saved_message;
5061   tree type;
5062
5063   /* The type-specifier sequence must not contain type definitions.
5064      (It cannot contain declarations of new types either, but if they
5065      are not definitions we will catch that because they are not
5066      complete.)  */
5067   saved_message = parser->type_definition_forbidden_message;
5068   parser->type_definition_forbidden_message
5069     = "types may not be defined in a new-type-id";
5070   /* Parse the type-specifier-seq.  */
5071   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5072                                 &type_specifier_seq);
5073   /* Restore the old message.  */
5074   parser->type_definition_forbidden_message = saved_message;
5075   /* Parse the new-declarator.  */
5076   new_declarator = cp_parser_new_declarator_opt (parser);
5077
5078   /* Determine the number of elements in the last array dimension, if
5079      any.  */
5080   *nelts = NULL_TREE;
5081   /* Skip down to the last array dimension.  */
5082   declarator = new_declarator;
5083   outer_declarator = NULL;
5084   while (declarator && (declarator->kind == cdk_pointer
5085                         || declarator->kind == cdk_ptrmem))
5086     {
5087       outer_declarator = declarator;
5088       declarator = declarator->declarator;
5089     }
5090   while (declarator
5091          && declarator->kind == cdk_array
5092          && declarator->declarator
5093          && declarator->declarator->kind == cdk_array)
5094     {
5095       outer_declarator = declarator;
5096       declarator = declarator->declarator;
5097     }
5098
5099   if (declarator && declarator->kind == cdk_array)
5100     {
5101       *nelts = declarator->u.array.bounds;
5102       if (*nelts == error_mark_node)
5103         *nelts = integer_one_node;
5104
5105       if (outer_declarator)
5106         outer_declarator->declarator = declarator->declarator;
5107       else
5108         new_declarator = NULL;
5109     }
5110
5111   type = groktypename (&type_specifier_seq, new_declarator);
5112   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5113     {
5114       *nelts = array_type_nelts_top (type);
5115       type = TREE_TYPE (type);
5116     }
5117   return type;
5118 }
5119
5120 /* Parse an (optional) new-declarator.
5121
5122    new-declarator:
5123      ptr-operator new-declarator [opt]
5124      direct-new-declarator
5125
5126    Returns the declarator.  */
5127
5128 static cp_declarator *
5129 cp_parser_new_declarator_opt (cp_parser* parser)
5130 {
5131   enum tree_code code;
5132   tree type;
5133   cp_cv_quals cv_quals;
5134
5135   /* We don't know if there's a ptr-operator next, or not.  */
5136   cp_parser_parse_tentatively (parser);
5137   /* Look for a ptr-operator.  */
5138   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5139   /* If that worked, look for more new-declarators.  */
5140   if (cp_parser_parse_definitely (parser))
5141     {
5142       cp_declarator *declarator;
5143
5144       /* Parse another optional declarator.  */
5145       declarator = cp_parser_new_declarator_opt (parser);
5146
5147       /* Create the representation of the declarator.  */
5148       if (type)
5149         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5150       else if (code == INDIRECT_REF)
5151         declarator = make_pointer_declarator (cv_quals, declarator);
5152       else
5153         declarator = make_reference_declarator (cv_quals, declarator);
5154
5155       return declarator;
5156     }
5157
5158   /* If the next token is a `[', there is a direct-new-declarator.  */
5159   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5160     return cp_parser_direct_new_declarator (parser);
5161
5162   return NULL;
5163 }
5164
5165 /* Parse a direct-new-declarator.
5166
5167    direct-new-declarator:
5168      [ expression ]
5169      direct-new-declarator [constant-expression]
5170
5171    */
5172
5173 static cp_declarator *
5174 cp_parser_direct_new_declarator (cp_parser* parser)
5175 {
5176   cp_declarator *declarator = NULL;
5177
5178   while (true)
5179     {
5180       tree expression;
5181
5182       /* Look for the opening `['.  */
5183       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5184       /* The first expression is not required to be constant.  */
5185       if (!declarator)
5186         {
5187           expression = cp_parser_expression (parser, /*cast_p=*/false);
5188           /* The standard requires that the expression have integral
5189              type.  DR 74 adds enumeration types.  We believe that the
5190              real intent is that these expressions be handled like the
5191              expression in a `switch' condition, which also allows
5192              classes with a single conversion to integral or
5193              enumeration type.  */
5194           if (!processing_template_decl)
5195             {
5196               expression
5197                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5198                                               expression,
5199                                               /*complain=*/true);
5200               if (!expression)
5201                 {
5202                   error ("expression in new-declarator must have integral "
5203                          "or enumeration type");
5204                   expression = error_mark_node;
5205                 }
5206             }
5207         }
5208       /* But all the other expressions must be.  */
5209       else
5210         expression
5211           = cp_parser_constant_expression (parser,
5212                                            /*allow_non_constant=*/false,
5213                                            NULL);
5214       /* Look for the closing `]'.  */
5215       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5216
5217       /* Add this bound to the declarator.  */
5218       declarator = make_array_declarator (declarator, expression);
5219
5220       /* If the next token is not a `[', then there are no more
5221          bounds.  */
5222       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5223         break;
5224     }
5225
5226   return declarator;
5227 }
5228
5229 /* Parse a new-initializer.
5230
5231    new-initializer:
5232      ( expression-list [opt] )
5233
5234    Returns a representation of the expression-list.  If there is no
5235    expression-list, VOID_ZERO_NODE is returned.  */
5236
5237 static tree
5238 cp_parser_new_initializer (cp_parser* parser)
5239 {
5240   tree expression_list;
5241
5242   expression_list = (cp_parser_parenthesized_expression_list
5243                      (parser, false, /*cast_p=*/false,
5244                       /*non_constant_p=*/NULL));
5245   if (!expression_list)
5246     expression_list = void_zero_node;
5247
5248   return expression_list;
5249 }
5250
5251 /* Parse a delete-expression.
5252
5253    delete-expression:
5254      :: [opt] delete cast-expression
5255      :: [opt] delete [ ] cast-expression
5256
5257    Returns a representation of the expression.  */
5258
5259 static tree
5260 cp_parser_delete_expression (cp_parser* parser)
5261 {
5262   bool global_scope_p;
5263   bool array_p;
5264   tree expression;
5265
5266   /* Look for the optional `::' operator.  */
5267   global_scope_p
5268     = (cp_parser_global_scope_opt (parser,
5269                                    /*current_scope_valid_p=*/false)
5270        != NULL_TREE);
5271   /* Look for the `delete' keyword.  */
5272   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5273   /* See if the array syntax is in use.  */
5274   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5275     {
5276       /* Consume the `[' token.  */
5277       cp_lexer_consume_token (parser->lexer);
5278       /* Look for the `]' token.  */
5279       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5280       /* Remember that this is the `[]' construct.  */
5281       array_p = true;
5282     }
5283   else
5284     array_p = false;
5285
5286   /* Parse the cast-expression.  */
5287   expression = cp_parser_simple_cast_expression (parser);
5288
5289   /* A delete-expression may not appear in an integral constant
5290      expression.  */
5291   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5292     return error_mark_node;
5293
5294   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5295 }
5296
5297 /* Parse a cast-expression.
5298
5299    cast-expression:
5300      unary-expression
5301      ( type-id ) cast-expression
5302
5303    ADDRESS_P is true iff the unary-expression is appearing as the
5304    operand of the `&' operator.   CAST_P is true if this expression is
5305    the target of a cast.
5306
5307    Returns a representation of the expression.  */
5308
5309 static tree
5310 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5311 {
5312   /* If it's a `(', then we might be looking at a cast.  */
5313   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5314     {
5315       tree type = NULL_TREE;
5316       tree expr = NULL_TREE;
5317       bool compound_literal_p;
5318       const char *saved_message;
5319
5320       /* There's no way to know yet whether or not this is a cast.
5321          For example, `(int (3))' is a unary-expression, while `(int)
5322          3' is a cast.  So, we resort to parsing tentatively.  */
5323       cp_parser_parse_tentatively (parser);
5324       /* Types may not be defined in a cast.  */
5325       saved_message = parser->type_definition_forbidden_message;
5326       parser->type_definition_forbidden_message
5327         = "types may not be defined in casts";
5328       /* Consume the `('.  */
5329       cp_lexer_consume_token (parser->lexer);
5330       /* A very tricky bit is that `(struct S) { 3 }' is a
5331          compound-literal (which we permit in C++ as an extension).
5332          But, that construct is not a cast-expression -- it is a
5333          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5334          is legal; if the compound-literal were a cast-expression,
5335          you'd need an extra set of parentheses.)  But, if we parse
5336          the type-id, and it happens to be a class-specifier, then we
5337          will commit to the parse at that point, because we cannot
5338          undo the action that is done when creating a new class.  So,
5339          then we cannot back up and do a postfix-expression.
5340
5341          Therefore, we scan ahead to the closing `)', and check to see
5342          if the token after the `)' is a `{'.  If so, we are not
5343          looking at a cast-expression.
5344
5345          Save tokens so that we can put them back.  */
5346       cp_lexer_save_tokens (parser->lexer);
5347       /* Skip tokens until the next token is a closing parenthesis.
5348          If we find the closing `)', and the next token is a `{', then
5349          we are looking at a compound-literal.  */
5350       compound_literal_p
5351         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5352                                                   /*consume_paren=*/true)
5353            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5354       /* Roll back the tokens we skipped.  */
5355       cp_lexer_rollback_tokens (parser->lexer);
5356       /* If we were looking at a compound-literal, simulate an error
5357          so that the call to cp_parser_parse_definitely below will
5358          fail.  */
5359       if (compound_literal_p)
5360         cp_parser_simulate_error (parser);
5361       else
5362         {
5363           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5364           parser->in_type_id_in_expr_p = true;
5365           /* Look for the type-id.  */
5366           type = cp_parser_type_id (parser);
5367           /* Look for the closing `)'.  */
5368           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5369           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5370         }
5371
5372       /* Restore the saved message.  */
5373       parser->type_definition_forbidden_message = saved_message;
5374
5375       /* If ok so far, parse the dependent expression. We cannot be
5376          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5377          ctor of T, but looks like a cast to function returning T
5378          without a dependent expression.  */
5379       if (!cp_parser_error_occurred (parser))
5380         expr = cp_parser_cast_expression (parser,
5381                                           /*address_p=*/false,
5382                                           /*cast_p=*/true);
5383
5384       if (cp_parser_parse_definitely (parser))
5385         {
5386           /* Warn about old-style casts, if so requested.  */
5387           if (warn_old_style_cast
5388               && !in_system_header
5389               && !VOID_TYPE_P (type)
5390               && current_lang_name != lang_name_c)
5391             warning (0, "use of old-style cast");
5392
5393           /* Only type conversions to integral or enumeration types
5394              can be used in constant-expressions.  */
5395           if (parser->integral_constant_expression_p
5396               && !dependent_type_p (type)
5397               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5398               && (cp_parser_non_integral_constant_expression
5399                   (parser,
5400                    "a cast to a type other than an integral or "
5401                    "enumeration type")))
5402             return error_mark_node;
5403
5404           /* Perform the cast.  */
5405           expr = build_c_cast (type, expr);
5406           return expr;
5407         }
5408     }
5409
5410   /* If we get here, then it's not a cast, so it must be a
5411      unary-expression.  */
5412   return cp_parser_unary_expression (parser, address_p, cast_p);
5413 }
5414
5415 /* Parse a binary expression of the general form:
5416
5417    pm-expression:
5418      cast-expression
5419      pm-expression .* cast-expression
5420      pm-expression ->* cast-expression
5421
5422    multiplicative-expression:
5423      pm-expression
5424      multiplicative-expression * pm-expression
5425      multiplicative-expression / pm-expression
5426      multiplicative-expression % pm-expression
5427
5428    additive-expression:
5429      multiplicative-expression
5430      additive-expression + multiplicative-expression
5431      additive-expression - multiplicative-expression
5432
5433    shift-expression:
5434      additive-expression
5435      shift-expression << additive-expression
5436      shift-expression >> additive-expression
5437
5438    relational-expression:
5439      shift-expression
5440      relational-expression < shift-expression
5441      relational-expression > shift-expression
5442      relational-expression <= shift-expression
5443      relational-expression >= shift-expression
5444
5445   GNU Extension:
5446
5447    relational-expression:
5448      relational-expression <? shift-expression
5449      relational-expression >? shift-expression
5450
5451    equality-expression:
5452      relational-expression
5453      equality-expression == relational-expression
5454      equality-expression != relational-expression
5455
5456    and-expression:
5457      equality-expression
5458      and-expression & equality-expression
5459
5460    exclusive-or-expression:
5461      and-expression
5462      exclusive-or-expression ^ and-expression
5463
5464    inclusive-or-expression:
5465      exclusive-or-expression
5466      inclusive-or-expression | exclusive-or-expression
5467
5468    logical-and-expression:
5469      inclusive-or-expression
5470      logical-and-expression && inclusive-or-expression
5471
5472    logical-or-expression:
5473      logical-and-expression
5474      logical-or-expression || logical-and-expression
5475
5476    All these are implemented with a single function like:
5477
5478    binary-expression:
5479      simple-cast-expression
5480      binary-expression <token> binary-expression
5481
5482    CAST_P is true if this expression is the target of a cast.
5483
5484    The binops_by_token map is used to get the tree codes for each <token> type.
5485    binary-expressions are associated according to a precedence table.  */
5486
5487 #define TOKEN_PRECEDENCE(token) \
5488   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5489    ? PREC_NOT_OPERATOR \
5490    : binops_by_token[token->type].prec)
5491
5492 static tree
5493 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5494 {
5495   cp_parser_expression_stack stack;
5496   cp_parser_expression_stack_entry *sp = &stack[0];
5497   tree lhs, rhs;
5498   cp_token *token;
5499   enum tree_code tree_type;
5500   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5501   bool overloaded_p;
5502
5503   /* Parse the first expression.  */
5504   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5505
5506   for (;;)
5507     {
5508       /* Get an operator token.  */
5509       token = cp_lexer_peek_token (parser->lexer);
5510       if (token->type == CPP_MIN || token->type == CPP_MAX)
5511         cp_parser_warn_min_max ();
5512
5513       new_prec = TOKEN_PRECEDENCE (token);
5514
5515       /* Popping an entry off the stack means we completed a subexpression:
5516          - either we found a token which is not an operator (`>' where it is not
5517            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5518            will happen repeatedly;
5519          - or, we found an operator which has lower priority.  This is the case
5520            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5521            parsing `3 * 4'.  */
5522       if (new_prec <= prec)
5523         {
5524           if (sp == stack)
5525             break;
5526           else
5527             goto pop;
5528         }
5529
5530      get_rhs:
5531       tree_type = binops_by_token[token->type].tree_type;
5532
5533       /* We used the operator token.  */
5534       cp_lexer_consume_token (parser->lexer);
5535
5536       /* Extract another operand.  It may be the RHS of this expression
5537          or the LHS of a new, higher priority expression.  */
5538       rhs = cp_parser_simple_cast_expression (parser);
5539
5540       /* Get another operator token.  Look up its precedence to avoid
5541          building a useless (immediately popped) stack entry for common
5542          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5543       token = cp_lexer_peek_token (parser->lexer);
5544       lookahead_prec = TOKEN_PRECEDENCE (token);
5545       if (lookahead_prec > new_prec)
5546         {
5547           /* ... and prepare to parse the RHS of the new, higher priority
5548              expression.  Since precedence levels on the stack are
5549              monotonically increasing, we do not have to care about
5550              stack overflows.  */
5551           sp->prec = prec;
5552           sp->tree_type = tree_type;
5553           sp->lhs = lhs;
5554           sp++;
5555           lhs = rhs;
5556           prec = new_prec;
5557           new_prec = lookahead_prec;
5558           goto get_rhs;
5559
5560          pop:
5561           /* If the stack is not empty, we have parsed into LHS the right side
5562              (`4' in the example above) of an expression we had suspended.
5563              We can use the information on the stack to recover the LHS (`3')
5564              from the stack together with the tree code (`MULT_EXPR'), and
5565              the precedence of the higher level subexpression
5566              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5567              which will be used to actually build the additive expression.  */
5568           --sp;
5569           prec = sp->prec;
5570           tree_type = sp->tree_type;
5571           rhs = lhs;
5572           lhs = sp->lhs;
5573         }
5574
5575       overloaded_p = false;
5576       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5577
5578       /* If the binary operator required the use of an overloaded operator,
5579          then this expression cannot be an integral constant-expression.
5580          An overloaded operator can be used even if both operands are
5581          otherwise permissible in an integral constant-expression if at
5582          least one of the operands is of enumeration type.  */
5583
5584       if (overloaded_p
5585           && (cp_parser_non_integral_constant_expression
5586               (parser, "calls to overloaded operators")))
5587         return error_mark_node;
5588     }
5589
5590   return lhs;
5591 }
5592
5593
5594 /* Parse the `? expression : assignment-expression' part of a
5595    conditional-expression.  The LOGICAL_OR_EXPR is the
5596    logical-or-expression that started the conditional-expression.
5597    Returns a representation of the entire conditional-expression.
5598
5599    This routine is used by cp_parser_assignment_expression.
5600
5601      ? expression : assignment-expression
5602
5603    GNU Extensions:
5604
5605      ? : assignment-expression */
5606
5607 static tree
5608 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5609 {
5610   tree expr;
5611   tree assignment_expr;
5612
5613   /* Consume the `?' token.  */
5614   cp_lexer_consume_token (parser->lexer);
5615   if (cp_parser_allow_gnu_extensions_p (parser)
5616       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5617     /* Implicit true clause.  */
5618     expr = NULL_TREE;
5619   else
5620     /* Parse the expression.  */
5621     expr = cp_parser_expression (parser, /*cast_p=*/false);
5622
5623   /* The next token should be a `:'.  */
5624   cp_parser_require (parser, CPP_COLON, "`:'");
5625   /* Parse the assignment-expression.  */
5626   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5627
5628   /* Build the conditional-expression.  */
5629   return build_x_conditional_expr (logical_or_expr,
5630                                    expr,
5631                                    assignment_expr);
5632 }
5633
5634 /* Parse an assignment-expression.
5635
5636    assignment-expression:
5637      conditional-expression
5638      logical-or-expression assignment-operator assignment_expression
5639      throw-expression
5640
5641    CAST_P is true if this expression is the target of a cast.
5642
5643    Returns a representation for the expression.  */
5644
5645 static tree
5646 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5647 {
5648   tree expr;
5649
5650   /* If the next token is the `throw' keyword, then we're looking at
5651      a throw-expression.  */
5652   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5653     expr = cp_parser_throw_expression (parser);
5654   /* Otherwise, it must be that we are looking at a
5655      logical-or-expression.  */
5656   else
5657     {
5658       /* Parse the binary expressions (logical-or-expression).  */
5659       expr = cp_parser_binary_expression (parser, cast_p);
5660       /* If the next token is a `?' then we're actually looking at a
5661          conditional-expression.  */
5662       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5663         return cp_parser_question_colon_clause (parser, expr);
5664       else
5665         {
5666           enum tree_code assignment_operator;
5667
5668           /* If it's an assignment-operator, we're using the second
5669              production.  */
5670           assignment_operator
5671             = cp_parser_assignment_operator_opt (parser);
5672           if (assignment_operator != ERROR_MARK)
5673             {
5674               tree rhs;
5675
5676               /* Parse the right-hand side of the assignment.  */
5677               rhs = cp_parser_assignment_expression (parser, cast_p);
5678               /* An assignment may not appear in a
5679                  constant-expression.  */
5680               if (cp_parser_non_integral_constant_expression (parser,
5681                                                               "an assignment"))
5682                 return error_mark_node;
5683               /* Build the assignment expression.  */
5684               expr = build_x_modify_expr (expr,
5685                                           assignment_operator,
5686                                           rhs);
5687             }
5688         }
5689     }
5690
5691   return expr;
5692 }
5693
5694 /* Parse an (optional) assignment-operator.
5695
5696    assignment-operator: one of
5697      = *= /= %= += -= >>= <<= &= ^= |=
5698
5699    GNU Extension:
5700
5701    assignment-operator: one of
5702      <?= >?=
5703
5704    If the next token is an assignment operator, the corresponding tree
5705    code is returned, and the token is consumed.  For example, for
5706    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5707    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5708    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5709    operator, ERROR_MARK is returned.  */
5710
5711 static enum tree_code
5712 cp_parser_assignment_operator_opt (cp_parser* parser)
5713 {
5714   enum tree_code op;
5715   cp_token *token;
5716
5717   /* Peek at the next toen.  */
5718   token = cp_lexer_peek_token (parser->lexer);
5719
5720   switch (token->type)
5721     {
5722     case CPP_EQ:
5723       op = NOP_EXPR;
5724       break;
5725
5726     case CPP_MULT_EQ:
5727       op = MULT_EXPR;
5728       break;
5729
5730     case CPP_DIV_EQ:
5731       op = TRUNC_DIV_EXPR;
5732       break;
5733
5734     case CPP_MOD_EQ:
5735       op = TRUNC_MOD_EXPR;
5736       break;
5737
5738     case CPP_PLUS_EQ:
5739       op = PLUS_EXPR;
5740       break;
5741
5742     case CPP_MINUS_EQ:
5743       op = MINUS_EXPR;
5744       break;
5745
5746     case CPP_RSHIFT_EQ:
5747       op = RSHIFT_EXPR;
5748       break;
5749
5750     case CPP_LSHIFT_EQ:
5751       op = LSHIFT_EXPR;
5752       break;
5753
5754     case CPP_AND_EQ:
5755       op = BIT_AND_EXPR;
5756       break;
5757
5758     case CPP_XOR_EQ:
5759       op = BIT_XOR_EXPR;
5760       break;
5761
5762     case CPP_OR_EQ:
5763       op = BIT_IOR_EXPR;
5764       break;
5765
5766     case CPP_MIN_EQ:
5767       op = MIN_EXPR;
5768       cp_parser_warn_min_max ();
5769       break;
5770
5771     case CPP_MAX_EQ:
5772       op = MAX_EXPR;
5773       cp_parser_warn_min_max ();
5774       break;
5775
5776     default:
5777       /* Nothing else is an assignment operator.  */
5778       op = ERROR_MARK;
5779     }
5780
5781   /* If it was an assignment operator, consume it.  */
5782   if (op != ERROR_MARK)
5783     cp_lexer_consume_token (parser->lexer);
5784
5785   return op;
5786 }
5787
5788 /* Parse an expression.
5789
5790    expression:
5791      assignment-expression
5792      expression , assignment-expression
5793
5794    CAST_P is true if this expression is the target of a cast.
5795
5796    Returns a representation of the expression.  */
5797
5798 static tree
5799 cp_parser_expression (cp_parser* parser, bool cast_p)
5800 {
5801   tree expression = NULL_TREE;
5802
5803   while (true)
5804     {
5805       tree assignment_expression;
5806
5807       /* Parse the next assignment-expression.  */
5808       assignment_expression
5809         = cp_parser_assignment_expression (parser, cast_p);
5810       /* If this is the first assignment-expression, we can just
5811          save it away.  */
5812       if (!expression)
5813         expression = assignment_expression;
5814       else
5815         expression = build_x_compound_expr (expression,
5816                                             assignment_expression);
5817       /* If the next token is not a comma, then we are done with the
5818          expression.  */
5819       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5820         break;
5821       /* Consume the `,'.  */
5822       cp_lexer_consume_token (parser->lexer);
5823       /* A comma operator cannot appear in a constant-expression.  */
5824       if (cp_parser_non_integral_constant_expression (parser,
5825                                                       "a comma operator"))
5826         expression = error_mark_node;
5827     }
5828
5829   return expression;
5830 }
5831
5832 /* Parse a constant-expression.
5833
5834    constant-expression:
5835      conditional-expression
5836
5837   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5838   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5839   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5840   is false, NON_CONSTANT_P should be NULL.  */
5841
5842 static tree
5843 cp_parser_constant_expression (cp_parser* parser,
5844                                bool allow_non_constant_p,
5845                                bool *non_constant_p)
5846 {
5847   bool saved_integral_constant_expression_p;
5848   bool saved_allow_non_integral_constant_expression_p;
5849   bool saved_non_integral_constant_expression_p;
5850   tree expression;
5851
5852   /* It might seem that we could simply parse the
5853      conditional-expression, and then check to see if it were
5854      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5855      one that the compiler can figure out is constant, possibly after
5856      doing some simplifications or optimizations.  The standard has a
5857      precise definition of constant-expression, and we must honor
5858      that, even though it is somewhat more restrictive.
5859
5860      For example:
5861
5862        int i[(2, 3)];
5863
5864      is not a legal declaration, because `(2, 3)' is not a
5865      constant-expression.  The `,' operator is forbidden in a
5866      constant-expression.  However, GCC's constant-folding machinery
5867      will fold this operation to an INTEGER_CST for `3'.  */
5868
5869   /* Save the old settings.  */
5870   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5871   saved_allow_non_integral_constant_expression_p
5872     = parser->allow_non_integral_constant_expression_p;
5873   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5874   /* We are now parsing a constant-expression.  */
5875   parser->integral_constant_expression_p = true;
5876   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5877   parser->non_integral_constant_expression_p = false;
5878   /* Although the grammar says "conditional-expression", we parse an
5879      "assignment-expression", which also permits "throw-expression"
5880      and the use of assignment operators.  In the case that
5881      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5882      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5883      actually essential that we look for an assignment-expression.
5884      For example, cp_parser_initializer_clauses uses this function to
5885      determine whether a particular assignment-expression is in fact
5886      constant.  */
5887   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5888   /* Restore the old settings.  */
5889   parser->integral_constant_expression_p
5890     = saved_integral_constant_expression_p;
5891   parser->allow_non_integral_constant_expression_p
5892     = saved_allow_non_integral_constant_expression_p;
5893   if (allow_non_constant_p)
5894     *non_constant_p = parser->non_integral_constant_expression_p;
5895   else if (parser->non_integral_constant_expression_p)
5896     expression = error_mark_node;
5897   parser->non_integral_constant_expression_p
5898     = saved_non_integral_constant_expression_p;
5899
5900   return expression;
5901 }
5902
5903 /* Parse __builtin_offsetof.
5904
5905    offsetof-expression:
5906      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5907
5908    offsetof-member-designator:
5909      id-expression
5910      | offsetof-member-designator "." id-expression
5911      | offsetof-member-designator "[" expression "]"
5912 */
5913
5914 static tree
5915 cp_parser_builtin_offsetof (cp_parser *parser)
5916 {
5917   int save_ice_p, save_non_ice_p;
5918   tree type, expr;
5919   cp_id_kind dummy;
5920
5921   /* We're about to accept non-integral-constant things, but will
5922      definitely yield an integral constant expression.  Save and
5923      restore these values around our local parsing.  */
5924   save_ice_p = parser->integral_constant_expression_p;
5925   save_non_ice_p = parser->non_integral_constant_expression_p;
5926
5927   /* Consume the "__builtin_offsetof" token.  */
5928   cp_lexer_consume_token (parser->lexer);
5929   /* Consume the opening `('.  */
5930   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5931   /* Parse the type-id.  */
5932   type = cp_parser_type_id (parser);
5933   /* Look for the `,'.  */
5934   cp_parser_require (parser, CPP_COMMA, "`,'");
5935
5936   /* Build the (type *)null that begins the traditional offsetof macro.  */
5937   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5938
5939   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5940   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5941                                                  true, &dummy);
5942   while (true)
5943     {
5944       cp_token *token = cp_lexer_peek_token (parser->lexer);
5945       switch (token->type)
5946         {
5947         case CPP_OPEN_SQUARE:
5948           /* offsetof-member-designator "[" expression "]" */
5949           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5950           break;
5951
5952         case CPP_DOT:
5953           /* offsetof-member-designator "." identifier */
5954           cp_lexer_consume_token (parser->lexer);
5955           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5956                                                          true, &dummy);
5957           break;
5958
5959         case CPP_CLOSE_PAREN:
5960           /* Consume the ")" token.  */
5961           cp_lexer_consume_token (parser->lexer);
5962           goto success;
5963
5964         default:
5965           /* Error.  We know the following require will fail, but
5966              that gives the proper error message.  */
5967           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5968           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5969           expr = error_mark_node;
5970           goto failure;
5971         }
5972     }
5973
5974  success:
5975   /* If we're processing a template, we can't finish the semantics yet.
5976      Otherwise we can fold the entire expression now.  */
5977   if (processing_template_decl)
5978     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5979   else
5980     expr = fold_offsetof (expr);
5981
5982  failure:
5983   parser->integral_constant_expression_p = save_ice_p;
5984   parser->non_integral_constant_expression_p = save_non_ice_p;
5985
5986   return expr;
5987 }
5988
5989 /* Statements [gram.stmt.stmt]  */
5990
5991 /* Parse a statement.
5992
5993    statement:
5994      labeled-statement
5995      expression-statement
5996      compound-statement
5997      selection-statement
5998      iteration-statement
5999      jump-statement
6000      declaration-statement
6001      try-block  */
6002
6003 static void
6004 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6005 {
6006   tree statement;
6007   cp_token *token;
6008   location_t statement_location;
6009
6010   /* There is no statement yet.  */
6011   statement = NULL_TREE;
6012   /* Peek at the next token.  */
6013   token = cp_lexer_peek_token (parser->lexer);
6014   /* Remember the location of the first token in the statement.  */
6015   statement_location = token->location;
6016   /* If this is a keyword, then that will often determine what kind of
6017      statement we have.  */
6018   if (token->type == CPP_KEYWORD)
6019     {
6020       enum rid keyword = token->keyword;
6021
6022       switch (keyword)
6023         {
6024         case RID_CASE:
6025         case RID_DEFAULT:
6026           statement = cp_parser_labeled_statement (parser,
6027                                                    in_statement_expr);
6028           break;
6029
6030         case RID_IF:
6031         case RID_SWITCH:
6032           statement = cp_parser_selection_statement (parser);
6033           break;
6034
6035         case RID_WHILE:
6036         case RID_DO:
6037         case RID_FOR:
6038           statement = cp_parser_iteration_statement (parser);
6039           break;
6040
6041         case RID_BREAK:
6042         case RID_CONTINUE:
6043         case RID_RETURN:
6044         case RID_GOTO:
6045           statement = cp_parser_jump_statement (parser);
6046           break;
6047
6048           /* Objective-C++ exception-handling constructs.  */
6049         case RID_AT_TRY:
6050         case RID_AT_CATCH:
6051         case RID_AT_FINALLY:
6052         case RID_AT_SYNCHRONIZED:
6053         case RID_AT_THROW:
6054           statement = cp_parser_objc_statement (parser);
6055           break;
6056
6057         case RID_TRY:
6058           statement = cp_parser_try_block (parser);
6059           break;
6060
6061         default:
6062           /* It might be a keyword like `int' that can start a
6063              declaration-statement.  */
6064           break;
6065         }
6066     }
6067   else if (token->type == CPP_NAME)
6068     {
6069       /* If the next token is a `:', then we are looking at a
6070          labeled-statement.  */
6071       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6072       if (token->type == CPP_COLON)
6073         statement = cp_parser_labeled_statement (parser, in_statement_expr);
6074     }
6075   /* Anything that starts with a `{' must be a compound-statement.  */
6076   else if (token->type == CPP_OPEN_BRACE)
6077     statement = cp_parser_compound_statement (parser, NULL, false);
6078   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6079      a statement all its own.  */
6080   else if (token->type == CPP_PRAGMA)
6081     {
6082       cp_lexer_handle_pragma (parser->lexer);
6083       return;
6084     }
6085   else if (token->type == CPP_EOF)
6086     {
6087       cp_parser_error (parser, "expected statement");
6088       return;
6089     }
6090
6091   /* Everything else must be a declaration-statement or an
6092      expression-statement.  Try for the declaration-statement
6093      first, unless we are looking at a `;', in which case we know that
6094      we have an expression-statement.  */
6095   if (!statement)
6096     {
6097       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6098         {
6099           cp_parser_parse_tentatively (parser);
6100           /* Try to parse the declaration-statement.  */
6101           cp_parser_declaration_statement (parser);
6102           /* If that worked, we're done.  */
6103           if (cp_parser_parse_definitely (parser))
6104             return;
6105         }
6106       /* Look for an expression-statement instead.  */
6107       statement = cp_parser_expression_statement (parser, in_statement_expr);
6108     }
6109
6110   /* Set the line number for the statement.  */
6111   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6112     SET_EXPR_LOCATION (statement, statement_location);
6113 }
6114
6115 /* Parse a labeled-statement.
6116
6117    labeled-statement:
6118      identifier : statement
6119      case constant-expression : statement
6120      default : statement
6121
6122    GNU Extension:
6123
6124    labeled-statement:
6125      case constant-expression ... constant-expression : statement
6126
6127    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6128    For an ordinary label, returns a LABEL_EXPR.  */
6129
6130 static tree
6131 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6132 {
6133   cp_token *token;
6134   tree statement = error_mark_node;
6135
6136   /* The next token should be an identifier.  */
6137   token = cp_lexer_peek_token (parser->lexer);
6138   if (token->type != CPP_NAME
6139       && token->type != CPP_KEYWORD)
6140     {
6141       cp_parser_error (parser, "expected labeled-statement");
6142       return error_mark_node;
6143     }
6144
6145   switch (token->keyword)
6146     {
6147     case RID_CASE:
6148       {
6149         tree expr, expr_hi;
6150         cp_token *ellipsis;
6151
6152         /* Consume the `case' token.  */
6153         cp_lexer_consume_token (parser->lexer);
6154         /* Parse the constant-expression.  */
6155         expr = cp_parser_constant_expression (parser,
6156                                               /*allow_non_constant_p=*/false,
6157                                               NULL);
6158
6159         ellipsis = cp_lexer_peek_token (parser->lexer);
6160         if (ellipsis->type == CPP_ELLIPSIS)
6161           {
6162             /* Consume the `...' token.  */
6163             cp_lexer_consume_token (parser->lexer);
6164             expr_hi =
6165               cp_parser_constant_expression (parser,
6166                                              /*allow_non_constant_p=*/false,
6167                                              NULL);
6168             /* We don't need to emit warnings here, as the common code
6169                will do this for us.  */
6170           }
6171         else
6172           expr_hi = NULL_TREE;
6173
6174         if (!parser->in_switch_statement_p)
6175           error ("case label %qE not within a switch statement", expr);
6176         else
6177           statement = finish_case_label (expr, expr_hi);
6178       }
6179       break;
6180
6181     case RID_DEFAULT:
6182       /* Consume the `default' token.  */
6183       cp_lexer_consume_token (parser->lexer);
6184       if (!parser->in_switch_statement_p)
6185         error ("case label not within a switch statement");
6186       else
6187         statement = finish_case_label (NULL_TREE, NULL_TREE);
6188       break;
6189
6190     default:
6191       /* Anything else must be an ordinary label.  */
6192       statement = finish_label_stmt (cp_parser_identifier (parser));
6193       break;
6194     }
6195
6196   /* Require the `:' token.  */
6197   cp_parser_require (parser, CPP_COLON, "`:'");
6198   /* Parse the labeled statement.  */
6199   cp_parser_statement (parser, in_statement_expr);
6200
6201   /* Return the label, in the case of a `case' or `default' label.  */
6202   return statement;
6203 }
6204
6205 /* Parse an expression-statement.
6206
6207    expression-statement:
6208      expression [opt] ;
6209
6210    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6211    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6212    indicates whether this expression-statement is part of an
6213    expression statement.  */
6214
6215 static tree
6216 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6217 {
6218   tree statement = NULL_TREE;
6219
6220   /* If the next token is a ';', then there is no expression
6221      statement.  */
6222   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6223     statement = cp_parser_expression (parser, /*cast_p=*/false);
6224
6225   /* Consume the final `;'.  */
6226   cp_parser_consume_semicolon_at_end_of_statement (parser);
6227
6228   if (in_statement_expr
6229       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6230     /* This is the final expression statement of a statement
6231        expression.  */
6232     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6233   else if (statement)
6234     statement = finish_expr_stmt (statement);
6235   else
6236     finish_stmt ();
6237
6238   return statement;
6239 }
6240
6241 /* Parse a compound-statement.
6242
6243    compound-statement:
6244      { statement-seq [opt] }
6245
6246    Returns a tree representing the statement.  */
6247
6248 static tree
6249 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6250                               bool in_try)
6251 {
6252   tree compound_stmt;
6253
6254   /* Consume the `{'.  */
6255   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6256     return error_mark_node;
6257   /* Begin the compound-statement.  */
6258   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6259   /* Parse an (optional) statement-seq.  */
6260   cp_parser_statement_seq_opt (parser, in_statement_expr);
6261   /* Finish the compound-statement.  */
6262   finish_compound_stmt (compound_stmt);
6263   /* Consume the `}'.  */
6264   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6265
6266   return compound_stmt;
6267 }
6268
6269 /* Parse an (optional) statement-seq.
6270
6271    statement-seq:
6272      statement
6273      statement-seq [opt] statement  */
6274
6275 static void
6276 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6277 {
6278   /* Scan statements until there aren't any more.  */
6279   while (true)
6280     {
6281       /* If we're looking at a `}', then we've run out of statements.  */
6282       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6283           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6284         break;
6285
6286       /* Parse the statement.  */
6287       cp_parser_statement (parser, in_statement_expr);
6288     }
6289 }
6290
6291 /* Parse a selection-statement.
6292
6293    selection-statement:
6294      if ( condition ) statement
6295      if ( condition ) statement else statement
6296      switch ( condition ) statement
6297
6298    Returns the new IF_STMT or SWITCH_STMT.  */
6299
6300 static tree
6301 cp_parser_selection_statement (cp_parser* parser)
6302 {
6303   cp_token *token;
6304   enum rid keyword;
6305
6306   /* Peek at the next token.  */
6307   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6308
6309   /* See what kind of keyword it is.  */
6310   keyword = token->keyword;
6311   switch (keyword)
6312     {
6313     case RID_IF:
6314     case RID_SWITCH:
6315       {
6316         tree statement;
6317         tree condition;
6318
6319         /* Look for the `('.  */
6320         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6321           {
6322             cp_parser_skip_to_end_of_statement (parser);
6323             return error_mark_node;
6324           }
6325
6326         /* Begin the selection-statement.  */
6327         if (keyword == RID_IF)
6328           statement = begin_if_stmt ();
6329         else
6330           statement = begin_switch_stmt ();
6331
6332         /* Parse the condition.  */
6333         condition = cp_parser_condition (parser);
6334         /* Look for the `)'.  */
6335         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6336           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6337                                                  /*consume_paren=*/true);
6338
6339         if (keyword == RID_IF)
6340           {
6341             /* Add the condition.  */
6342             finish_if_stmt_cond (condition, statement);
6343
6344             /* Parse the then-clause.  */
6345             cp_parser_implicitly_scoped_statement (parser);
6346             finish_then_clause (statement);
6347
6348             /* If the next token is `else', parse the else-clause.  */
6349             if (cp_lexer_next_token_is_keyword (parser->lexer,
6350                                                 RID_ELSE))
6351               {
6352                 /* Consume the `else' keyword.  */
6353                 cp_lexer_consume_token (parser->lexer);
6354                 begin_else_clause (statement);
6355                 /* Parse the else-clause.  */
6356                 cp_parser_implicitly_scoped_statement (parser);
6357                 finish_else_clause (statement);
6358               }
6359
6360             /* Now we're all done with the if-statement.  */
6361             finish_if_stmt (statement);
6362           }
6363         else
6364           {
6365             bool in_switch_statement_p;
6366
6367             /* Add the condition.  */
6368             finish_switch_cond (condition, statement);
6369
6370             /* Parse the body of the switch-statement.  */
6371             in_switch_statement_p = parser->in_switch_statement_p;
6372             parser->in_switch_statement_p = true;
6373             cp_parser_implicitly_scoped_statement (parser);
6374             parser->in_switch_statement_p = in_switch_statement_p;
6375
6376             /* Now we're all done with the switch-statement.  */
6377             finish_switch_stmt (statement);
6378           }
6379
6380         return statement;
6381       }
6382       break;
6383
6384     default:
6385       cp_parser_error (parser, "expected selection-statement");
6386       return error_mark_node;
6387     }
6388 }
6389
6390 /* Parse a condition.
6391
6392    condition:
6393      expression
6394      type-specifier-seq declarator = assignment-expression
6395
6396    GNU Extension:
6397
6398    condition:
6399      type-specifier-seq declarator asm-specification [opt]
6400        attributes [opt] = assignment-expression
6401
6402    Returns the expression that should be tested.  */
6403
6404 static tree
6405 cp_parser_condition (cp_parser* parser)
6406 {
6407   cp_decl_specifier_seq type_specifiers;
6408   const char *saved_message;
6409
6410   /* Try the declaration first.  */
6411   cp_parser_parse_tentatively (parser);
6412   /* New types are not allowed in the type-specifier-seq for a
6413      condition.  */
6414   saved_message = parser->type_definition_forbidden_message;
6415   parser->type_definition_forbidden_message
6416     = "types may not be defined in conditions";
6417   /* Parse the type-specifier-seq.  */
6418   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6419                                 &type_specifiers);
6420   /* Restore the saved message.  */
6421   parser->type_definition_forbidden_message = saved_message;
6422   /* If all is well, we might be looking at a declaration.  */
6423   if (!cp_parser_error_occurred (parser))
6424     {
6425       tree decl;
6426       tree asm_specification;
6427       tree attributes;
6428       cp_declarator *declarator;
6429       tree initializer = NULL_TREE;
6430
6431       /* Parse the declarator.  */
6432       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6433                                          /*ctor_dtor_or_conv_p=*/NULL,
6434                                          /*parenthesized_p=*/NULL,
6435                                          /*member_p=*/false);
6436       /* Parse the attributes.  */
6437       attributes = cp_parser_attributes_opt (parser);
6438       /* Parse the asm-specification.  */
6439       asm_specification = cp_parser_asm_specification_opt (parser);
6440       /* If the next token is not an `=', then we might still be
6441          looking at an expression.  For example:
6442
6443            if (A(a).x)
6444
6445          looks like a decl-specifier-seq and a declarator -- but then
6446          there is no `=', so this is an expression.  */
6447       cp_parser_require (parser, CPP_EQ, "`='");
6448       /* If we did see an `=', then we are looking at a declaration
6449          for sure.  */
6450       if (cp_parser_parse_definitely (parser))
6451         {
6452           tree pushed_scope;
6453
6454           /* Create the declaration.  */
6455           decl = start_decl (declarator, &type_specifiers,
6456                              /*initialized_p=*/true,
6457                              attributes, /*prefix_attributes=*/NULL_TREE,
6458                              &pushed_scope);
6459           /* Parse the assignment-expression.  */
6460           initializer = cp_parser_assignment_expression (parser,
6461                                                          /*cast_p=*/false);
6462
6463           /* Process the initializer.  */
6464           cp_finish_decl (decl,
6465                           initializer,
6466                           asm_specification,
6467                           LOOKUP_ONLYCONVERTING);
6468
6469           if (pushed_scope)
6470             pop_scope (pushed_scope);
6471
6472           return convert_from_reference (decl);
6473         }
6474     }
6475   /* If we didn't even get past the declarator successfully, we are
6476      definitely not looking at a declaration.  */
6477   else
6478     cp_parser_abort_tentative_parse (parser);
6479
6480   /* Otherwise, we are looking at an expression.  */
6481   return cp_parser_expression (parser, /*cast_p=*/false);
6482 }
6483
6484 /* Parse an iteration-statement.
6485
6486    iteration-statement:
6487      while ( condition ) statement
6488      do statement while ( expression ) ;
6489      for ( for-init-statement condition [opt] ; expression [opt] )
6490        statement
6491
6492    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6493
6494 static tree
6495 cp_parser_iteration_statement (cp_parser* parser)
6496 {
6497   cp_token *token;
6498   enum rid keyword;
6499   tree statement;
6500   bool in_iteration_statement_p;
6501
6502
6503   /* Peek at the next token.  */
6504   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6505   if (!token)
6506     return error_mark_node;
6507
6508   /* Remember whether or not we are already within an iteration
6509      statement.  */
6510   in_iteration_statement_p = parser->in_iteration_statement_p;
6511
6512   /* See what kind of keyword it is.  */
6513   keyword = token->keyword;
6514   switch (keyword)
6515     {
6516     case RID_WHILE:
6517       {
6518         tree condition;
6519
6520         /* Begin the while-statement.  */
6521         statement = begin_while_stmt ();
6522         /* Look for the `('.  */
6523         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6524         /* Parse the condition.  */
6525         condition = cp_parser_condition (parser);
6526         finish_while_stmt_cond (condition, statement);
6527         /* Look for the `)'.  */
6528         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6529         /* Parse the dependent statement.  */
6530         parser->in_iteration_statement_p = true;
6531         cp_parser_already_scoped_statement (parser);
6532         parser->in_iteration_statement_p = in_iteration_statement_p;
6533         /* We're done with the while-statement.  */
6534         finish_while_stmt (statement);
6535       }
6536       break;
6537
6538     case RID_DO:
6539       {
6540         tree expression;
6541
6542         /* Begin the do-statement.  */
6543         statement = begin_do_stmt ();
6544         /* Parse the body of the do-statement.  */
6545         parser->in_iteration_statement_p = true;
6546         cp_parser_implicitly_scoped_statement (parser);
6547         parser->in_iteration_statement_p = in_iteration_statement_p;
6548         finish_do_body (statement);
6549         /* Look for the `while' keyword.  */
6550         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6551         /* Look for the `('.  */
6552         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6553         /* Parse the expression.  */
6554         expression = cp_parser_expression (parser, /*cast_p=*/false);
6555         /* We're done with the do-statement.  */
6556         finish_do_stmt (expression, statement);
6557         /* Look for the `)'.  */
6558         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6559         /* Look for the `;'.  */
6560         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6561       }
6562       break;
6563
6564     case RID_FOR:
6565       {
6566         tree condition = NULL_TREE;
6567         tree expression = NULL_TREE;
6568
6569         /* Begin the for-statement.  */
6570         statement = begin_for_stmt ();
6571         /* Look for the `('.  */
6572         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6573         /* Parse the initialization.  */
6574         cp_parser_for_init_statement (parser);
6575         finish_for_init_stmt (statement);
6576
6577         /* If there's a condition, process it.  */
6578         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6579           condition = cp_parser_condition (parser);
6580         finish_for_cond (condition, statement);
6581         /* Look for the `;'.  */
6582         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6583
6584         /* If there's an expression, process it.  */
6585         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6586           expression = cp_parser_expression (parser, /*cast_p=*/false);
6587         finish_for_expr (expression, statement);
6588         /* Look for the `)'.  */
6589         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6590
6591         /* Parse the body of the for-statement.  */
6592         parser->in_iteration_statement_p = true;
6593         cp_parser_already_scoped_statement (parser);
6594         parser->in_iteration_statement_p = in_iteration_statement_p;
6595
6596         /* We're done with the for-statement.  */
6597         finish_for_stmt (statement);
6598       }
6599       break;
6600
6601     default:
6602       cp_parser_error (parser, "expected iteration-statement");
6603       statement = error_mark_node;
6604       break;
6605     }
6606
6607   return statement;
6608 }
6609
6610 /* Parse a for-init-statement.
6611
6612    for-init-statement:
6613      expression-statement
6614      simple-declaration  */
6615
6616 static void
6617 cp_parser_for_init_statement (cp_parser* parser)
6618 {
6619   /* If the next token is a `;', then we have an empty
6620      expression-statement.  Grammatically, this is also a
6621      simple-declaration, but an invalid one, because it does not
6622      declare anything.  Therefore, if we did not handle this case
6623      specially, we would issue an error message about an invalid
6624      declaration.  */
6625   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6626     {
6627       /* We're going to speculatively look for a declaration, falling back
6628          to an expression, if necessary.  */
6629       cp_parser_parse_tentatively (parser);
6630       /* Parse the declaration.  */
6631       cp_parser_simple_declaration (parser,
6632                                     /*function_definition_allowed_p=*/false);
6633       /* If the tentative parse failed, then we shall need to look for an
6634          expression-statement.  */
6635       if (cp_parser_parse_definitely (parser))
6636         return;
6637     }
6638
6639   cp_parser_expression_statement (parser, false);
6640 }
6641
6642 /* Parse a jump-statement.
6643
6644    jump-statement:
6645      break ;
6646      continue ;
6647      return expression [opt] ;
6648      goto identifier ;
6649
6650    GNU extension:
6651
6652    jump-statement:
6653      goto * expression ;
6654
6655    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6656
6657 static tree
6658 cp_parser_jump_statement (cp_parser* parser)
6659 {
6660   tree statement = error_mark_node;
6661   cp_token *token;
6662   enum rid keyword;
6663
6664   /* Peek at the next token.  */
6665   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6666   if (!token)
6667     return error_mark_node;
6668
6669   /* See what kind of keyword it is.  */
6670   keyword = token->keyword;
6671   switch (keyword)
6672     {
6673     case RID_BREAK:
6674       if (!parser->in_switch_statement_p
6675           && !parser->in_iteration_statement_p)
6676         {
6677           error ("break statement not within loop or switch");
6678           statement = error_mark_node;
6679         }
6680       else
6681         statement = finish_break_stmt ();
6682       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6683       break;
6684
6685     case RID_CONTINUE:
6686       if (!parser->in_iteration_statement_p)
6687         {
6688           error ("continue statement not within a loop");
6689           statement = error_mark_node;
6690         }
6691       else
6692         statement = finish_continue_stmt ();
6693       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6694       break;
6695
6696     case RID_RETURN:
6697       {
6698         tree expr;
6699
6700         /* If the next token is a `;', then there is no
6701            expression.  */
6702         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6703           expr = cp_parser_expression (parser, /*cast_p=*/false);
6704         else
6705           expr = NULL_TREE;
6706         /* Build the return-statement.  */
6707         statement = finish_return_stmt (expr);
6708         /* Look for the final `;'.  */
6709         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6710       }
6711       break;
6712
6713     case RID_GOTO:
6714       /* Create the goto-statement.  */
6715       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6716         {
6717           /* Issue a warning about this use of a GNU extension.  */
6718           if (pedantic)
6719             pedwarn ("ISO C++ forbids computed gotos");
6720           /* Consume the '*' token.  */
6721           cp_lexer_consume_token (parser->lexer);
6722           /* Parse the dependent expression.  */
6723           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6724         }
6725       else
6726         finish_goto_stmt (cp_parser_identifier (parser));
6727       /* Look for the final `;'.  */
6728       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6729       break;
6730
6731     default:
6732       cp_parser_error (parser, "expected jump-statement");
6733       break;
6734     }
6735
6736   return statement;
6737 }
6738
6739 /* Parse a declaration-statement.
6740
6741    declaration-statement:
6742      block-declaration  */
6743
6744 static void
6745 cp_parser_declaration_statement (cp_parser* parser)
6746 {
6747   void *p;
6748
6749   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6750   p = obstack_alloc (&declarator_obstack, 0);
6751
6752  /* Parse the block-declaration.  */
6753   cp_parser_block_declaration (parser, /*statement_p=*/true);
6754
6755   /* Free any declarators allocated.  */
6756   obstack_free (&declarator_obstack, p);
6757
6758   /* Finish off the statement.  */
6759   finish_stmt ();
6760 }
6761
6762 /* Some dependent statements (like `if (cond) statement'), are
6763    implicitly in their own scope.  In other words, if the statement is
6764    a single statement (as opposed to a compound-statement), it is
6765    none-the-less treated as if it were enclosed in braces.  Any
6766    declarations appearing in the dependent statement are out of scope
6767    after control passes that point.  This function parses a statement,
6768    but ensures that is in its own scope, even if it is not a
6769    compound-statement.
6770
6771    Returns the new statement.  */
6772
6773 static tree
6774 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6775 {
6776   tree statement;
6777
6778   /* If the token is not a `{', then we must take special action.  */
6779   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6780     {
6781       /* Create a compound-statement.  */
6782       statement = begin_compound_stmt (0);
6783       /* Parse the dependent-statement.  */
6784       cp_parser_statement (parser, false);
6785       /* Finish the dummy compound-statement.  */
6786       finish_compound_stmt (statement);
6787     }
6788   /* Otherwise, we simply parse the statement directly.  */
6789   else
6790     statement = cp_parser_compound_statement (parser, NULL, false);
6791
6792   /* Return the statement.  */
6793   return statement;
6794 }
6795
6796 /* For some dependent statements (like `while (cond) statement'), we
6797    have already created a scope.  Therefore, even if the dependent
6798    statement is a compound-statement, we do not want to create another
6799    scope.  */
6800
6801 static void
6802 cp_parser_already_scoped_statement (cp_parser* parser)
6803 {
6804   /* If the token is a `{', then we must take special action.  */
6805   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6806     cp_parser_statement (parser, false);
6807   else
6808     {
6809       /* Avoid calling cp_parser_compound_statement, so that we
6810          don't create a new scope.  Do everything else by hand.  */
6811       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6812       cp_parser_statement_seq_opt (parser, false);
6813       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6814     }
6815 }
6816
6817 /* Declarations [gram.dcl.dcl] */
6818
6819 /* Parse an optional declaration-sequence.
6820
6821    declaration-seq:
6822      declaration
6823      declaration-seq declaration  */
6824
6825 static void
6826 cp_parser_declaration_seq_opt (cp_parser* parser)
6827 {
6828   while (true)
6829     {
6830       cp_token *token;
6831
6832       token = cp_lexer_peek_token (parser->lexer);
6833
6834       if (token->type == CPP_CLOSE_BRACE
6835           || token->type == CPP_EOF)
6836         break;
6837
6838       if (token->type == CPP_SEMICOLON)
6839         {
6840           /* A declaration consisting of a single semicolon is
6841              invalid.  Allow it unless we're being pedantic.  */
6842           cp_lexer_consume_token (parser->lexer);
6843           if (pedantic && !in_system_header)
6844             pedwarn ("extra %<;%>");
6845           continue;
6846         }
6847
6848       /* If we're entering or exiting a region that's implicitly
6849          extern "C", modify the lang context appropriately.  */
6850       if (!parser->implicit_extern_c && token->implicit_extern_c)
6851         {
6852           push_lang_context (lang_name_c);
6853           parser->implicit_extern_c = true;
6854         }
6855       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6856         {
6857           pop_lang_context ();
6858           parser->implicit_extern_c = false;
6859         }
6860
6861       if (token->type == CPP_PRAGMA)
6862         {
6863           /* A top-level declaration can consist solely of a #pragma.
6864              A nested declaration cannot, so this is done here and not
6865              in cp_parser_declaration.  (A #pragma at block scope is
6866              handled in cp_parser_statement.)  */
6867           cp_lexer_handle_pragma (parser->lexer);
6868           continue;
6869         }
6870
6871       /* Parse the declaration itself.  */
6872       cp_parser_declaration (parser);
6873     }
6874 }
6875
6876 /* Parse a declaration.
6877
6878    declaration:
6879      block-declaration
6880      function-definition
6881      template-declaration
6882      explicit-instantiation
6883      explicit-specialization
6884      linkage-specification
6885      namespace-definition
6886
6887    GNU extension:
6888
6889    declaration:
6890       __extension__ declaration */
6891
6892 static void
6893 cp_parser_declaration (cp_parser* parser)
6894 {
6895   cp_token token1;
6896   cp_token token2;
6897   int saved_pedantic;
6898   void *p;
6899
6900   /* Check for the `__extension__' keyword.  */
6901   if (cp_parser_extension_opt (parser, &saved_pedantic))
6902     {
6903       /* Parse the qualified declaration.  */
6904       cp_parser_declaration (parser);
6905       /* Restore the PEDANTIC flag.  */
6906       pedantic = saved_pedantic;
6907
6908       return;
6909     }
6910
6911   /* Try to figure out what kind of declaration is present.  */
6912   token1 = *cp_lexer_peek_token (parser->lexer);
6913
6914   if (token1.type != CPP_EOF)
6915     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6916   else
6917     {
6918       token2.type = CPP_EOF;
6919       token2.keyword = RID_MAX;
6920     }
6921
6922   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6923   p = obstack_alloc (&declarator_obstack, 0);
6924
6925   /* If the next token is `extern' and the following token is a string
6926      literal, then we have a linkage specification.  */
6927   if (token1.keyword == RID_EXTERN
6928       && cp_parser_is_string_literal (&token2))
6929     cp_parser_linkage_specification (parser);
6930   /* If the next token is `template', then we have either a template
6931      declaration, an explicit instantiation, or an explicit
6932      specialization.  */
6933   else if (token1.keyword == RID_TEMPLATE)
6934     {
6935       /* `template <>' indicates a template specialization.  */
6936       if (token2.type == CPP_LESS
6937           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6938         cp_parser_explicit_specialization (parser);
6939       /* `template <' indicates a template declaration.  */
6940       else if (token2.type == CPP_LESS)
6941         cp_parser_template_declaration (parser, /*member_p=*/false);
6942       /* Anything else must be an explicit instantiation.  */
6943       else
6944         cp_parser_explicit_instantiation (parser);
6945     }
6946   /* If the next token is `export', then we have a template
6947      declaration.  */
6948   else if (token1.keyword == RID_EXPORT)
6949     cp_parser_template_declaration (parser, /*member_p=*/false);
6950   /* If the next token is `extern', 'static' or 'inline' and the one
6951      after that is `template', we have a GNU extended explicit
6952      instantiation directive.  */
6953   else if (cp_parser_allow_gnu_extensions_p (parser)
6954            && (token1.keyword == RID_EXTERN
6955                || token1.keyword == RID_STATIC
6956                || token1.keyword == RID_INLINE)
6957            && token2.keyword == RID_TEMPLATE)
6958     cp_parser_explicit_instantiation (parser);
6959   /* If the next token is `namespace', check for a named or unnamed
6960      namespace definition.  */
6961   else if (token1.keyword == RID_NAMESPACE
6962            && (/* A named namespace definition.  */
6963                (token2.type == CPP_NAME
6964                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6965                     == CPP_OPEN_BRACE))
6966                /* An unnamed namespace definition.  */
6967                || token2.type == CPP_OPEN_BRACE))
6968     cp_parser_namespace_definition (parser);
6969   /* Objective-C++ declaration/definition.  */
6970   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6971     cp_parser_objc_declaration (parser);
6972   /* We must have either a block declaration or a function
6973      definition.  */
6974   else
6975     /* Try to parse a block-declaration, or a function-definition.  */
6976     cp_parser_block_declaration (parser, /*statement_p=*/false);
6977
6978   /* Free any declarators allocated.  */
6979   obstack_free (&declarator_obstack, p);
6980 }
6981
6982 /* Parse a block-declaration.
6983
6984    block-declaration:
6985      simple-declaration
6986      asm-definition
6987      namespace-alias-definition
6988      using-declaration
6989      using-directive
6990
6991    GNU Extension:
6992
6993    block-declaration:
6994      __extension__ block-declaration
6995      label-declaration
6996
6997    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6998    part of a declaration-statement.  */
6999
7000 static void
7001 cp_parser_block_declaration (cp_parser *parser,
7002                              bool      statement_p)
7003 {
7004   cp_token *token1;
7005   int saved_pedantic;
7006
7007   /* Check for the `__extension__' keyword.  */
7008   if (cp_parser_extension_opt (parser, &saved_pedantic))
7009     {
7010       /* Parse the qualified declaration.  */
7011       cp_parser_block_declaration (parser, statement_p);
7012       /* Restore the PEDANTIC flag.  */
7013       pedantic = saved_pedantic;
7014
7015       return;
7016     }
7017
7018   /* Peek at the next token to figure out which kind of declaration is
7019      present.  */
7020   token1 = cp_lexer_peek_token (parser->lexer);
7021
7022   /* If the next keyword is `asm', we have an asm-definition.  */
7023   if (token1->keyword == RID_ASM)
7024     {
7025       if (statement_p)
7026         cp_parser_commit_to_tentative_parse (parser);
7027       cp_parser_asm_definition (parser);
7028     }
7029   /* If the next keyword is `namespace', we have a
7030      namespace-alias-definition.  */
7031   else if (token1->keyword == RID_NAMESPACE)
7032     cp_parser_namespace_alias_definition (parser);
7033   /* If the next keyword is `using', we have either a
7034      using-declaration or a using-directive.  */
7035   else if (token1->keyword == RID_USING)
7036     {
7037       cp_token *token2;
7038
7039       if (statement_p)
7040         cp_parser_commit_to_tentative_parse (parser);
7041       /* If the token after `using' is `namespace', then we have a
7042          using-directive.  */
7043       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7044       if (token2->keyword == RID_NAMESPACE)
7045         cp_parser_using_directive (parser);
7046       /* Otherwise, it's a using-declaration.  */
7047       else
7048         cp_parser_using_declaration (parser);
7049     }
7050   /* If the next keyword is `__label__' we have a label declaration.  */
7051   else if (token1->keyword == RID_LABEL)
7052     {
7053       if (statement_p)
7054         cp_parser_commit_to_tentative_parse (parser);
7055       cp_parser_label_declaration (parser);
7056     }
7057   /* Anything else must be a simple-declaration.  */
7058   else
7059     cp_parser_simple_declaration (parser, !statement_p);
7060 }
7061
7062 /* Parse a simple-declaration.
7063
7064    simple-declaration:
7065      decl-specifier-seq [opt] init-declarator-list [opt] ;
7066
7067    init-declarator-list:
7068      init-declarator
7069      init-declarator-list , init-declarator
7070
7071    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7072    function-definition as a simple-declaration.  */
7073
7074 static void
7075 cp_parser_simple_declaration (cp_parser* parser,
7076                               bool function_definition_allowed_p)
7077 {
7078   cp_decl_specifier_seq decl_specifiers;
7079   int declares_class_or_enum;
7080   bool saw_declarator;
7081
7082   /* Defer access checks until we know what is being declared; the
7083      checks for names appearing in the decl-specifier-seq should be
7084      done as if we were in the scope of the thing being declared.  */
7085   push_deferring_access_checks (dk_deferred);
7086
7087   /* Parse the decl-specifier-seq.  We have to keep track of whether
7088      or not the decl-specifier-seq declares a named class or
7089      enumeration type, since that is the only case in which the
7090      init-declarator-list is allowed to be empty.
7091
7092      [dcl.dcl]
7093
7094      In a simple-declaration, the optional init-declarator-list can be
7095      omitted only when declaring a class or enumeration, that is when
7096      the decl-specifier-seq contains either a class-specifier, an
7097      elaborated-type-specifier, or an enum-specifier.  */
7098   cp_parser_decl_specifier_seq (parser,
7099                                 CP_PARSER_FLAGS_OPTIONAL,
7100                                 &decl_specifiers,
7101                                 &declares_class_or_enum);
7102   /* We no longer need to defer access checks.  */
7103   stop_deferring_access_checks ();
7104
7105   /* In a block scope, a valid declaration must always have a
7106      decl-specifier-seq.  By not trying to parse declarators, we can
7107      resolve the declaration/expression ambiguity more quickly.  */
7108   if (!function_definition_allowed_p
7109       && !decl_specifiers.any_specifiers_p)
7110     {
7111       cp_parser_error (parser, "expected declaration");
7112       goto done;
7113     }
7114
7115   /* If the next two tokens are both identifiers, the code is
7116      erroneous. The usual cause of this situation is code like:
7117
7118        T t;
7119
7120      where "T" should name a type -- but does not.  */
7121   if (!decl_specifiers.type
7122       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7123     {
7124       /* If parsing tentatively, we should commit; we really are
7125          looking at a declaration.  */
7126       cp_parser_commit_to_tentative_parse (parser);
7127       /* Give up.  */
7128       goto done;
7129     }
7130
7131   /* If we have seen at least one decl-specifier, and the next token
7132      is not a parenthesis, then we must be looking at a declaration.
7133      (After "int (" we might be looking at a functional cast.)  */
7134   if (decl_specifiers.any_specifiers_p
7135       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7136     cp_parser_commit_to_tentative_parse (parser);
7137
7138   /* Keep going until we hit the `;' at the end of the simple
7139      declaration.  */
7140   saw_declarator = false;
7141   while (cp_lexer_next_token_is_not (parser->lexer,
7142                                      CPP_SEMICOLON))
7143     {
7144       cp_token *token;
7145       bool function_definition_p;
7146       tree decl;
7147
7148       if (saw_declarator)
7149         {
7150           /* If we are processing next declarator, coma is expected */
7151           token = cp_lexer_peek_token (parser->lexer);
7152           gcc_assert (token->type == CPP_COMMA);
7153           cp_lexer_consume_token (parser->lexer);
7154         }
7155       else
7156         saw_declarator = true;
7157
7158       /* Parse the init-declarator.  */
7159       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7160                                         function_definition_allowed_p,
7161                                         /*member_p=*/false,
7162                                         declares_class_or_enum,
7163                                         &function_definition_p);
7164       /* If an error occurred while parsing tentatively, exit quickly.
7165          (That usually happens when in the body of a function; each
7166          statement is treated as a declaration-statement until proven
7167          otherwise.)  */
7168       if (cp_parser_error_occurred (parser))
7169         goto done;
7170       /* Handle function definitions specially.  */
7171       if (function_definition_p)
7172         {
7173           /* If the next token is a `,', then we are probably
7174              processing something like:
7175
7176                void f() {}, *p;
7177
7178              which is erroneous.  */
7179           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7180             error ("mixing declarations and function-definitions is forbidden");
7181           /* Otherwise, we're done with the list of declarators.  */
7182           else
7183             {
7184               pop_deferring_access_checks ();
7185               return;
7186             }
7187         }
7188       /* The next token should be either a `,' or a `;'.  */
7189       token = cp_lexer_peek_token (parser->lexer);
7190       /* If it's a `,', there are more declarators to come.  */
7191       if (token->type == CPP_COMMA)
7192         /* will be consumed next time around */;
7193       /* If it's a `;', we are done.  */
7194       else if (token->type == CPP_SEMICOLON)
7195         break;
7196       /* Anything else is an error.  */
7197       else
7198         {
7199           /* If we have already issued an error message we don't need
7200              to issue another one.  */
7201           if (decl != error_mark_node
7202               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7203             cp_parser_error (parser, "expected %<,%> or %<;%>");
7204           /* Skip tokens until we reach the end of the statement.  */
7205           cp_parser_skip_to_end_of_statement (parser);
7206           /* If the next token is now a `;', consume it.  */
7207           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7208             cp_lexer_consume_token (parser->lexer);
7209           goto done;
7210         }
7211       /* After the first time around, a function-definition is not
7212          allowed -- even if it was OK at first.  For example:
7213
7214            int i, f() {}
7215
7216          is not valid.  */
7217       function_definition_allowed_p = false;
7218     }
7219
7220   /* Issue an error message if no declarators are present, and the
7221      decl-specifier-seq does not itself declare a class or
7222      enumeration.  */
7223   if (!saw_declarator)
7224     {
7225       if (cp_parser_declares_only_class_p (parser))
7226         shadow_tag (&decl_specifiers);
7227       /* Perform any deferred access checks.  */
7228       perform_deferred_access_checks ();
7229     }
7230
7231   /* Consume the `;'.  */
7232   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7233
7234  done:
7235   pop_deferring_access_checks ();
7236 }
7237
7238 /* Parse a decl-specifier-seq.
7239
7240    decl-specifier-seq:
7241      decl-specifier-seq [opt] decl-specifier
7242
7243    decl-specifier:
7244      storage-class-specifier
7245      type-specifier
7246      function-specifier
7247      friend
7248      typedef
7249
7250    GNU Extension:
7251
7252    decl-specifier:
7253      attributes
7254
7255    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7256
7257    The parser flags FLAGS is used to control type-specifier parsing.
7258
7259    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7260    flags:
7261
7262      1: one of the decl-specifiers is an elaborated-type-specifier
7263         (i.e., a type declaration)
7264      2: one of the decl-specifiers is an enum-specifier or a
7265         class-specifier (i.e., a type definition)
7266
7267    */
7268
7269 static void
7270 cp_parser_decl_specifier_seq (cp_parser* parser,
7271                               cp_parser_flags flags,
7272                               cp_decl_specifier_seq *decl_specs,
7273                               int* declares_class_or_enum)
7274 {
7275   bool constructor_possible_p = !parser->in_declarator_p;
7276
7277   /* Clear DECL_SPECS.  */
7278   clear_decl_specs (decl_specs);
7279
7280   /* Assume no class or enumeration type is declared.  */
7281   *declares_class_or_enum = 0;
7282
7283   /* Keep reading specifiers until there are no more to read.  */
7284   while (true)
7285     {
7286       bool constructor_p;
7287       bool found_decl_spec;
7288       cp_token *token;
7289
7290       /* Peek at the next token.  */
7291       token = cp_lexer_peek_token (parser->lexer);
7292       /* Handle attributes.  */
7293       if (token->keyword == RID_ATTRIBUTE)
7294         {
7295           /* Parse the attributes.  */
7296           decl_specs->attributes
7297             = chainon (decl_specs->attributes,
7298                        cp_parser_attributes_opt (parser));
7299           continue;
7300         }
7301       /* Assume we will find a decl-specifier keyword.  */
7302       found_decl_spec = true;
7303       /* If the next token is an appropriate keyword, we can simply
7304          add it to the list.  */
7305       switch (token->keyword)
7306         {
7307           /* decl-specifier:
7308                friend  */
7309         case RID_FRIEND:
7310           if (decl_specs->specs[(int) ds_friend]++)
7311             error ("duplicate %<friend%>");
7312           /* Consume the token.  */
7313           cp_lexer_consume_token (parser->lexer);
7314           break;
7315
7316           /* function-specifier:
7317                inline
7318                virtual
7319                explicit  */
7320         case RID_INLINE:
7321         case RID_VIRTUAL:
7322         case RID_EXPLICIT:
7323           cp_parser_function_specifier_opt (parser, decl_specs);
7324           break;
7325
7326           /* decl-specifier:
7327                typedef  */
7328         case RID_TYPEDEF:
7329           ++decl_specs->specs[(int) ds_typedef];
7330           /* Consume the token.  */
7331           cp_lexer_consume_token (parser->lexer);
7332           /* A constructor declarator cannot appear in a typedef.  */
7333           constructor_possible_p = false;
7334           /* The "typedef" keyword can only occur in a declaration; we
7335              may as well commit at this point.  */
7336           cp_parser_commit_to_tentative_parse (parser);
7337           break;
7338
7339           /* storage-class-specifier:
7340                auto
7341                register
7342                static
7343                extern
7344                mutable
7345
7346              GNU Extension:
7347                thread  */
7348         case RID_AUTO:
7349           /* Consume the token.  */
7350           cp_lexer_consume_token (parser->lexer);
7351           cp_parser_set_storage_class (decl_specs, sc_auto);
7352           break;
7353         case RID_REGISTER:
7354           /* Consume the token.  */
7355           cp_lexer_consume_token (parser->lexer);
7356           cp_parser_set_storage_class (decl_specs, sc_register);
7357           break;
7358         case RID_STATIC:
7359           /* Consume the token.  */
7360           cp_lexer_consume_token (parser->lexer);
7361           if (decl_specs->specs[(int) ds_thread])
7362             {
7363               error ("%<__thread%> before %<static%>");
7364               decl_specs->specs[(int) ds_thread] = 0;
7365             }
7366           cp_parser_set_storage_class (decl_specs, sc_static);
7367           break;
7368         case RID_EXTERN:
7369           /* Consume the token.  */
7370           cp_lexer_consume_token (parser->lexer);
7371           if (decl_specs->specs[(int) ds_thread])
7372             {
7373               error ("%<__thread%> before %<extern%>");
7374               decl_specs->specs[(int) ds_thread] = 0;
7375             }
7376           cp_parser_set_storage_class (decl_specs, sc_extern);
7377           break;
7378         case RID_MUTABLE:
7379           /* Consume the token.  */
7380           cp_lexer_consume_token (parser->lexer);
7381           cp_parser_set_storage_class (decl_specs, sc_mutable);
7382           break;
7383         case RID_THREAD:
7384           /* Consume the token.  */
7385           cp_lexer_consume_token (parser->lexer);
7386           ++decl_specs->specs[(int) ds_thread];
7387           break;
7388
7389         default:
7390           /* We did not yet find a decl-specifier yet.  */
7391           found_decl_spec = false;
7392           break;
7393         }
7394
7395       /* Constructors are a special case.  The `S' in `S()' is not a
7396          decl-specifier; it is the beginning of the declarator.  */
7397       constructor_p
7398         = (!found_decl_spec
7399            && constructor_possible_p
7400            && (cp_parser_constructor_declarator_p
7401                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7402
7403       /* If we don't have a DECL_SPEC yet, then we must be looking at
7404          a type-specifier.  */
7405       if (!found_decl_spec && !constructor_p)
7406         {
7407           int decl_spec_declares_class_or_enum;
7408           bool is_cv_qualifier;
7409           tree type_spec;
7410
7411           type_spec
7412             = cp_parser_type_specifier (parser, flags,
7413                                         decl_specs,
7414                                         /*is_declaration=*/true,
7415                                         &decl_spec_declares_class_or_enum,
7416                                         &is_cv_qualifier);
7417
7418           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7419
7420           /* If this type-specifier referenced a user-defined type
7421              (a typedef, class-name, etc.), then we can't allow any
7422              more such type-specifiers henceforth.
7423
7424              [dcl.spec]
7425
7426              The longest sequence of decl-specifiers that could
7427              possibly be a type name is taken as the
7428              decl-specifier-seq of a declaration.  The sequence shall
7429              be self-consistent as described below.
7430
7431              [dcl.type]
7432
7433              As a general rule, at most one type-specifier is allowed
7434              in the complete decl-specifier-seq of a declaration.  The
7435              only exceptions are the following:
7436
7437              -- const or volatile can be combined with any other
7438                 type-specifier.
7439
7440              -- signed or unsigned can be combined with char, long,
7441                 short, or int.
7442
7443              -- ..
7444
7445              Example:
7446
7447                typedef char* Pc;
7448                void g (const int Pc);
7449
7450              Here, Pc is *not* part of the decl-specifier seq; it's
7451              the declarator.  Therefore, once we see a type-specifier
7452              (other than a cv-qualifier), we forbid any additional
7453              user-defined types.  We *do* still allow things like `int
7454              int' to be considered a decl-specifier-seq, and issue the
7455              error message later.  */
7456           if (type_spec && !is_cv_qualifier)
7457             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7458           /* A constructor declarator cannot follow a type-specifier.  */
7459           if (type_spec)
7460             {
7461               constructor_possible_p = false;
7462               found_decl_spec = true;
7463             }
7464         }
7465
7466       /* If we still do not have a DECL_SPEC, then there are no more
7467          decl-specifiers.  */
7468       if (!found_decl_spec)
7469         break;
7470
7471       decl_specs->any_specifiers_p = true;
7472       /* After we see one decl-specifier, further decl-specifiers are
7473          always optional.  */
7474       flags |= CP_PARSER_FLAGS_OPTIONAL;
7475     }
7476
7477   /* Don't allow a friend specifier with a class definition.  */
7478   if (decl_specs->specs[(int) ds_friend] != 0
7479       && (*declares_class_or_enum & 2))
7480     error ("class definition may not be declared a friend");
7481 }
7482
7483 /* Parse an (optional) storage-class-specifier.
7484
7485    storage-class-specifier:
7486      auto
7487      register
7488      static
7489      extern
7490      mutable
7491
7492    GNU Extension:
7493
7494    storage-class-specifier:
7495      thread
7496
7497    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7498
7499 static tree
7500 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7501 {
7502   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7503     {
7504     case RID_AUTO:
7505     case RID_REGISTER:
7506     case RID_STATIC:
7507     case RID_EXTERN:
7508     case RID_MUTABLE:
7509     case RID_THREAD:
7510       /* Consume the token.  */
7511       return cp_lexer_consume_token (parser->lexer)->value;
7512
7513     default:
7514       return NULL_TREE;
7515     }
7516 }
7517
7518 /* Parse an (optional) function-specifier.
7519
7520    function-specifier:
7521      inline
7522      virtual
7523      explicit
7524
7525    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7526    Updates DECL_SPECS, if it is non-NULL.  */
7527
7528 static tree
7529 cp_parser_function_specifier_opt (cp_parser* parser,
7530                                   cp_decl_specifier_seq *decl_specs)
7531 {
7532   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7533     {
7534     case RID_INLINE:
7535       if (decl_specs)
7536         ++decl_specs->specs[(int) ds_inline];
7537       break;
7538
7539     case RID_VIRTUAL:
7540       if (decl_specs)
7541         ++decl_specs->specs[(int) ds_virtual];
7542       break;
7543
7544     case RID_EXPLICIT:
7545       if (decl_specs)
7546         ++decl_specs->specs[(int) ds_explicit];
7547       break;
7548
7549     default:
7550       return NULL_TREE;
7551     }
7552
7553   /* Consume the token.  */
7554   return cp_lexer_consume_token (parser->lexer)->value;
7555 }
7556
7557 /* Parse a linkage-specification.
7558
7559    linkage-specification:
7560      extern string-literal { declaration-seq [opt] }
7561      extern string-literal declaration  */
7562
7563 static void
7564 cp_parser_linkage_specification (cp_parser* parser)
7565 {
7566   tree linkage;
7567
7568   /* Look for the `extern' keyword.  */
7569   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7570
7571   /* Look for the string-literal.  */
7572   linkage = cp_parser_string_literal (parser, false, false);
7573
7574   /* Transform the literal into an identifier.  If the literal is a
7575      wide-character string, or contains embedded NULs, then we can't
7576      handle it as the user wants.  */
7577   if (strlen (TREE_STRING_POINTER (linkage))
7578       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7579     {
7580       cp_parser_error (parser, "invalid linkage-specification");
7581       /* Assume C++ linkage.  */
7582       linkage = lang_name_cplusplus;
7583     }
7584   else
7585     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7586
7587   /* We're now using the new linkage.  */
7588   push_lang_context (linkage);
7589
7590   /* If the next token is a `{', then we're using the first
7591      production.  */
7592   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7593     {
7594       /* Consume the `{' token.  */
7595       cp_lexer_consume_token (parser->lexer);
7596       /* Parse the declarations.  */
7597       cp_parser_declaration_seq_opt (parser);
7598       /* Look for the closing `}'.  */
7599       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7600     }
7601   /* Otherwise, there's just one declaration.  */
7602   else
7603     {
7604       bool saved_in_unbraced_linkage_specification_p;
7605
7606       saved_in_unbraced_linkage_specification_p
7607         = parser->in_unbraced_linkage_specification_p;
7608       parser->in_unbraced_linkage_specification_p = true;
7609       have_extern_spec = true;
7610       cp_parser_declaration (parser);
7611       have_extern_spec = false;
7612       parser->in_unbraced_linkage_specification_p
7613         = saved_in_unbraced_linkage_specification_p;
7614     }
7615
7616   /* We're done with the linkage-specification.  */
7617   pop_lang_context ();
7618 }
7619
7620 /* Special member functions [gram.special] */
7621
7622 /* Parse a conversion-function-id.
7623
7624    conversion-function-id:
7625      operator conversion-type-id
7626
7627    Returns an IDENTIFIER_NODE representing the operator.  */
7628
7629 static tree
7630 cp_parser_conversion_function_id (cp_parser* parser)
7631 {
7632   tree type;
7633   tree saved_scope;
7634   tree saved_qualifying_scope;
7635   tree saved_object_scope;
7636   tree pushed_scope = NULL_TREE;
7637
7638   /* Look for the `operator' token.  */
7639   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7640     return error_mark_node;
7641   /* When we parse the conversion-type-id, the current scope will be
7642      reset.  However, we need that information in able to look up the
7643      conversion function later, so we save it here.  */
7644   saved_scope = parser->scope;
7645   saved_qualifying_scope = parser->qualifying_scope;
7646   saved_object_scope = parser->object_scope;
7647   /* We must enter the scope of the class so that the names of
7648      entities declared within the class are available in the
7649      conversion-type-id.  For example, consider:
7650
7651        struct S {
7652          typedef int I;
7653          operator I();
7654        };
7655
7656        S::operator I() { ... }
7657
7658      In order to see that `I' is a type-name in the definition, we
7659      must be in the scope of `S'.  */
7660   if (saved_scope)
7661     pushed_scope = push_scope (saved_scope);
7662   /* Parse the conversion-type-id.  */
7663   type = cp_parser_conversion_type_id (parser);
7664   /* Leave the scope of the class, if any.  */
7665   if (pushed_scope)
7666     pop_scope (pushed_scope);
7667   /* Restore the saved scope.  */
7668   parser->scope = saved_scope;
7669   parser->qualifying_scope = saved_qualifying_scope;
7670   parser->object_scope = saved_object_scope;
7671   /* If the TYPE is invalid, indicate failure.  */
7672   if (type == error_mark_node)
7673     return error_mark_node;
7674   return mangle_conv_op_name_for_type (type);
7675 }
7676
7677 /* Parse a conversion-type-id:
7678
7679    conversion-type-id:
7680      type-specifier-seq conversion-declarator [opt]
7681
7682    Returns the TYPE specified.  */
7683
7684 static tree
7685 cp_parser_conversion_type_id (cp_parser* parser)
7686 {
7687   tree attributes;
7688   cp_decl_specifier_seq type_specifiers;
7689   cp_declarator *declarator;
7690   tree type_specified;
7691
7692   /* Parse the attributes.  */
7693   attributes = cp_parser_attributes_opt (parser);
7694   /* Parse the type-specifiers.  */
7695   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7696                                 &type_specifiers);
7697   /* If that didn't work, stop.  */
7698   if (type_specifiers.type == error_mark_node)
7699     return error_mark_node;
7700   /* Parse the conversion-declarator.  */
7701   declarator = cp_parser_conversion_declarator_opt (parser);
7702
7703   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7704                                     /*initialized=*/0, &attributes);
7705   if (attributes)
7706     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7707   return type_specified;
7708 }
7709
7710 /* Parse an (optional) conversion-declarator.
7711
7712    conversion-declarator:
7713      ptr-operator conversion-declarator [opt]
7714
7715    */
7716
7717 static cp_declarator *
7718 cp_parser_conversion_declarator_opt (cp_parser* parser)
7719 {
7720   enum tree_code code;
7721   tree class_type;
7722   cp_cv_quals cv_quals;
7723
7724   /* We don't know if there's a ptr-operator next, or not.  */
7725   cp_parser_parse_tentatively (parser);
7726   /* Try the ptr-operator.  */
7727   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7728   /* If it worked, look for more conversion-declarators.  */
7729   if (cp_parser_parse_definitely (parser))
7730     {
7731       cp_declarator *declarator;
7732
7733       /* Parse another optional declarator.  */
7734       declarator = cp_parser_conversion_declarator_opt (parser);
7735
7736       /* Create the representation of the declarator.  */
7737       if (class_type)
7738         declarator = make_ptrmem_declarator (cv_quals, class_type,
7739                                              declarator);
7740       else if (code == INDIRECT_REF)
7741         declarator = make_pointer_declarator (cv_quals, declarator);
7742       else
7743         declarator = make_reference_declarator (cv_quals, declarator);
7744
7745       return declarator;
7746    }
7747
7748   return NULL;
7749 }
7750
7751 /* Parse an (optional) ctor-initializer.
7752
7753    ctor-initializer:
7754      : mem-initializer-list
7755
7756    Returns TRUE iff the ctor-initializer was actually present.  */
7757
7758 static bool
7759 cp_parser_ctor_initializer_opt (cp_parser* parser)
7760 {
7761   /* If the next token is not a `:', then there is no
7762      ctor-initializer.  */
7763   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7764     {
7765       /* Do default initialization of any bases and members.  */
7766       if (DECL_CONSTRUCTOR_P (current_function_decl))
7767         finish_mem_initializers (NULL_TREE);
7768
7769       return false;
7770     }
7771
7772   /* Consume the `:' token.  */
7773   cp_lexer_consume_token (parser->lexer);
7774   /* And the mem-initializer-list.  */
7775   cp_parser_mem_initializer_list (parser);
7776
7777   return true;
7778 }
7779
7780 /* Parse a mem-initializer-list.
7781
7782    mem-initializer-list:
7783      mem-initializer
7784      mem-initializer , mem-initializer-list  */
7785
7786 static void
7787 cp_parser_mem_initializer_list (cp_parser* parser)
7788 {
7789   tree mem_initializer_list = NULL_TREE;
7790
7791   /* Let the semantic analysis code know that we are starting the
7792      mem-initializer-list.  */
7793   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7794     error ("only constructors take base initializers");
7795
7796   /* Loop through the list.  */
7797   while (true)
7798     {
7799       tree mem_initializer;
7800
7801       /* Parse the mem-initializer.  */
7802       mem_initializer = cp_parser_mem_initializer (parser);
7803       /* Add it to the list, unless it was erroneous.  */
7804       if (mem_initializer)
7805         {
7806           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7807           mem_initializer_list = mem_initializer;
7808         }
7809       /* If the next token is not a `,', we're done.  */
7810       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7811         break;
7812       /* Consume the `,' token.  */
7813       cp_lexer_consume_token (parser->lexer);
7814     }
7815
7816   /* Perform semantic analysis.  */
7817   if (DECL_CONSTRUCTOR_P (current_function_decl))
7818     finish_mem_initializers (mem_initializer_list);
7819 }
7820
7821 /* Parse a mem-initializer.
7822
7823    mem-initializer:
7824      mem-initializer-id ( expression-list [opt] )
7825
7826    GNU extension:
7827
7828    mem-initializer:
7829      ( expression-list [opt] )
7830
7831    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7832    class) or FIELD_DECL (for a non-static data member) to initialize;
7833    the TREE_VALUE is the expression-list.  */
7834
7835 static tree
7836 cp_parser_mem_initializer (cp_parser* parser)
7837 {
7838   tree mem_initializer_id;
7839   tree expression_list;
7840   tree member;
7841
7842   /* Find out what is being initialized.  */
7843   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7844     {
7845       pedwarn ("anachronistic old-style base class initializer");
7846       mem_initializer_id = NULL_TREE;
7847     }
7848   else
7849     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7850   member = expand_member_init (mem_initializer_id);
7851   if (member && !DECL_P (member))
7852     in_base_initializer = 1;
7853
7854   expression_list
7855     = cp_parser_parenthesized_expression_list (parser, false,
7856                                                /*cast_p=*/false,
7857                                                /*non_constant_p=*/NULL);
7858   if (!expression_list)
7859     expression_list = void_type_node;
7860
7861   in_base_initializer = 0;
7862
7863   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7864 }
7865
7866 /* Parse a mem-initializer-id.
7867
7868    mem-initializer-id:
7869      :: [opt] nested-name-specifier [opt] class-name
7870      identifier
7871
7872    Returns a TYPE indicating the class to be initializer for the first
7873    production.  Returns an IDENTIFIER_NODE indicating the data member
7874    to be initialized for the second production.  */
7875
7876 static tree
7877 cp_parser_mem_initializer_id (cp_parser* parser)
7878 {
7879   bool global_scope_p;
7880   bool nested_name_specifier_p;
7881   bool template_p = false;
7882   tree id;
7883
7884   /* `typename' is not allowed in this context ([temp.res]).  */
7885   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7886     {
7887       error ("keyword %<typename%> not allowed in this context (a qualified "
7888              "member initializer is implicitly a type)");
7889       cp_lexer_consume_token (parser->lexer);
7890     }
7891   /* Look for the optional `::' operator.  */
7892   global_scope_p
7893     = (cp_parser_global_scope_opt (parser,
7894                                    /*current_scope_valid_p=*/false)
7895        != NULL_TREE);
7896   /* Look for the optional nested-name-specifier.  The simplest way to
7897      implement:
7898
7899        [temp.res]
7900
7901        The keyword `typename' is not permitted in a base-specifier or
7902        mem-initializer; in these contexts a qualified name that
7903        depends on a template-parameter is implicitly assumed to be a
7904        type name.
7905
7906      is to assume that we have seen the `typename' keyword at this
7907      point.  */
7908   nested_name_specifier_p
7909     = (cp_parser_nested_name_specifier_opt (parser,
7910                                             /*typename_keyword_p=*/true,
7911                                             /*check_dependency_p=*/true,
7912                                             /*type_p=*/true,
7913                                             /*is_declaration=*/true)
7914        != NULL_TREE);
7915   if (nested_name_specifier_p)
7916     template_p = cp_parser_optional_template_keyword (parser);
7917   /* If there is a `::' operator or a nested-name-specifier, then we
7918      are definitely looking for a class-name.  */
7919   if (global_scope_p || nested_name_specifier_p)
7920     return cp_parser_class_name (parser,
7921                                  /*typename_keyword_p=*/true,
7922                                  /*template_keyword_p=*/template_p,
7923                                  none_type,
7924                                  /*check_dependency_p=*/true,
7925                                  /*class_head_p=*/false,
7926                                  /*is_declaration=*/true);
7927   /* Otherwise, we could also be looking for an ordinary identifier.  */
7928   cp_parser_parse_tentatively (parser);
7929   /* Try a class-name.  */
7930   id = cp_parser_class_name (parser,
7931                              /*typename_keyword_p=*/true,
7932                              /*template_keyword_p=*/false,
7933                              none_type,
7934                              /*check_dependency_p=*/true,
7935                              /*class_head_p=*/false,
7936                              /*is_declaration=*/true);
7937   /* If we found one, we're done.  */
7938   if (cp_parser_parse_definitely (parser))
7939     return id;
7940   /* Otherwise, look for an ordinary identifier.  */
7941   return cp_parser_identifier (parser);
7942 }
7943
7944 /* Overloading [gram.over] */
7945
7946 /* Parse an operator-function-id.
7947
7948    operator-function-id:
7949      operator operator
7950
7951    Returns an IDENTIFIER_NODE for the operator which is a
7952    human-readable spelling of the identifier, e.g., `operator +'.  */
7953
7954 static tree
7955 cp_parser_operator_function_id (cp_parser* parser)
7956 {
7957   /* Look for the `operator' keyword.  */
7958   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7959     return error_mark_node;
7960   /* And then the name of the operator itself.  */
7961   return cp_parser_operator (parser);
7962 }
7963
7964 /* Parse an operator.
7965
7966    operator:
7967      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7968      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7969      || ++ -- , ->* -> () []
7970
7971    GNU Extensions:
7972
7973    operator:
7974      <? >? <?= >?=
7975
7976    Returns an IDENTIFIER_NODE for the operator which is a
7977    human-readable spelling of the identifier, e.g., `operator +'.  */
7978
7979 static tree
7980 cp_parser_operator (cp_parser* parser)
7981 {
7982   tree id = NULL_TREE;
7983   cp_token *token;
7984
7985   /* Peek at the next token.  */
7986   token = cp_lexer_peek_token (parser->lexer);
7987   /* Figure out which operator we have.  */
7988   switch (token->type)
7989     {
7990     case CPP_KEYWORD:
7991       {
7992         enum tree_code op;
7993
7994         /* The keyword should be either `new' or `delete'.  */
7995         if (token->keyword == RID_NEW)
7996           op = NEW_EXPR;
7997         else if (token->keyword == RID_DELETE)
7998           op = DELETE_EXPR;
7999         else
8000           break;
8001
8002         /* Consume the `new' or `delete' token.  */
8003         cp_lexer_consume_token (parser->lexer);
8004
8005         /* Peek at the next token.  */
8006         token = cp_lexer_peek_token (parser->lexer);
8007         /* If it's a `[' token then this is the array variant of the
8008            operator.  */
8009         if (token->type == CPP_OPEN_SQUARE)
8010           {
8011             /* Consume the `[' token.  */
8012             cp_lexer_consume_token (parser->lexer);
8013             /* Look for the `]' token.  */
8014             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8015             id = ansi_opname (op == NEW_EXPR
8016                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8017           }
8018         /* Otherwise, we have the non-array variant.  */
8019         else
8020           id = ansi_opname (op);
8021
8022         return id;
8023       }
8024
8025     case CPP_PLUS:
8026       id = ansi_opname (PLUS_EXPR);
8027       break;
8028
8029     case CPP_MINUS:
8030       id = ansi_opname (MINUS_EXPR);
8031       break;
8032
8033     case CPP_MULT:
8034       id = ansi_opname (MULT_EXPR);
8035       break;
8036
8037     case CPP_DIV:
8038       id = ansi_opname (TRUNC_DIV_EXPR);
8039       break;
8040
8041     case CPP_MOD:
8042       id = ansi_opname (TRUNC_MOD_EXPR);
8043       break;
8044
8045     case CPP_XOR:
8046       id = ansi_opname (BIT_XOR_EXPR);
8047       break;
8048
8049     case CPP_AND:
8050       id = ansi_opname (BIT_AND_EXPR);
8051       break;
8052
8053     case CPP_OR:
8054       id = ansi_opname (BIT_IOR_EXPR);
8055       break;
8056
8057     case CPP_COMPL:
8058       id = ansi_opname (BIT_NOT_EXPR);
8059       break;
8060
8061     case CPP_NOT:
8062       id = ansi_opname (TRUTH_NOT_EXPR);
8063       break;
8064
8065     case CPP_EQ:
8066       id = ansi_assopname (NOP_EXPR);
8067       break;
8068
8069     case CPP_LESS:
8070       id = ansi_opname (LT_EXPR);
8071       break;
8072
8073     case CPP_GREATER:
8074       id = ansi_opname (GT_EXPR);
8075       break;
8076
8077     case CPP_PLUS_EQ:
8078       id = ansi_assopname (PLUS_EXPR);
8079       break;
8080
8081     case CPP_MINUS_EQ:
8082       id = ansi_assopname (MINUS_EXPR);
8083       break;
8084
8085     case CPP_MULT_EQ:
8086       id = ansi_assopname (MULT_EXPR);
8087       break;
8088
8089     case CPP_DIV_EQ:
8090       id = ansi_assopname (TRUNC_DIV_EXPR);
8091       break;
8092
8093     case CPP_MOD_EQ:
8094       id = ansi_assopname (TRUNC_MOD_EXPR);
8095       break;
8096
8097     case CPP_XOR_EQ:
8098       id = ansi_assopname (BIT_XOR_EXPR);
8099       break;
8100
8101     case CPP_AND_EQ:
8102       id = ansi_assopname (BIT_AND_EXPR);
8103       break;
8104
8105     case CPP_OR_EQ:
8106       id = ansi_assopname (BIT_IOR_EXPR);
8107       break;
8108
8109     case CPP_LSHIFT:
8110       id = ansi_opname (LSHIFT_EXPR);
8111       break;
8112
8113     case CPP_RSHIFT:
8114       id = ansi_opname (RSHIFT_EXPR);
8115       break;
8116
8117     case CPP_LSHIFT_EQ:
8118       id = ansi_assopname (LSHIFT_EXPR);
8119       break;
8120
8121     case CPP_RSHIFT_EQ:
8122       id = ansi_assopname (RSHIFT_EXPR);
8123       break;
8124
8125     case CPP_EQ_EQ:
8126       id = ansi_opname (EQ_EXPR);
8127       break;
8128
8129     case CPP_NOT_EQ:
8130       id = ansi_opname (NE_EXPR);
8131       break;
8132
8133     case CPP_LESS_EQ:
8134       id = ansi_opname (LE_EXPR);
8135       break;
8136
8137     case CPP_GREATER_EQ:
8138       id = ansi_opname (GE_EXPR);
8139       break;
8140
8141     case CPP_AND_AND:
8142       id = ansi_opname (TRUTH_ANDIF_EXPR);
8143       break;
8144
8145     case CPP_OR_OR:
8146       id = ansi_opname (TRUTH_ORIF_EXPR);
8147       break;
8148
8149     case CPP_PLUS_PLUS:
8150       id = ansi_opname (POSTINCREMENT_EXPR);
8151       break;
8152
8153     case CPP_MINUS_MINUS:
8154       id = ansi_opname (PREDECREMENT_EXPR);
8155       break;
8156
8157     case CPP_COMMA:
8158       id = ansi_opname (COMPOUND_EXPR);
8159       break;
8160
8161     case CPP_DEREF_STAR:
8162       id = ansi_opname (MEMBER_REF);
8163       break;
8164
8165     case CPP_DEREF:
8166       id = ansi_opname (COMPONENT_REF);
8167       break;
8168
8169     case CPP_OPEN_PAREN:
8170       /* Consume the `('.  */
8171       cp_lexer_consume_token (parser->lexer);
8172       /* Look for the matching `)'.  */
8173       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8174       return ansi_opname (CALL_EXPR);
8175
8176     case CPP_OPEN_SQUARE:
8177       /* Consume the `['.  */
8178       cp_lexer_consume_token (parser->lexer);
8179       /* Look for the matching `]'.  */
8180       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8181       return ansi_opname (ARRAY_REF);
8182
8183       /* Extensions.  */
8184     case CPP_MIN:
8185       id = ansi_opname (MIN_EXPR);
8186       cp_parser_warn_min_max ();
8187       break;
8188
8189     case CPP_MAX:
8190       id = ansi_opname (MAX_EXPR);
8191       cp_parser_warn_min_max ();
8192       break;
8193
8194     case CPP_MIN_EQ:
8195       id = ansi_assopname (MIN_EXPR);
8196       cp_parser_warn_min_max ();
8197       break;
8198
8199     case CPP_MAX_EQ:
8200       id = ansi_assopname (MAX_EXPR);
8201       cp_parser_warn_min_max ();
8202       break;
8203
8204     default:
8205       /* Anything else is an error.  */
8206       break;
8207     }
8208
8209   /* If we have selected an identifier, we need to consume the
8210      operator token.  */
8211   if (id)
8212     cp_lexer_consume_token (parser->lexer);
8213   /* Otherwise, no valid operator name was present.  */
8214   else
8215     {
8216       cp_parser_error (parser, "expected operator");
8217       id = error_mark_node;
8218     }
8219
8220   return id;
8221 }
8222
8223 /* Parse a template-declaration.
8224
8225    template-declaration:
8226      export [opt] template < template-parameter-list > declaration
8227
8228    If MEMBER_P is TRUE, this template-declaration occurs within a
8229    class-specifier.
8230
8231    The grammar rule given by the standard isn't correct.  What
8232    is really meant is:
8233
8234    template-declaration:
8235      export [opt] template-parameter-list-seq
8236        decl-specifier-seq [opt] init-declarator [opt] ;
8237      export [opt] template-parameter-list-seq
8238        function-definition
8239
8240    template-parameter-list-seq:
8241      template-parameter-list-seq [opt]
8242      template < template-parameter-list >  */
8243
8244 static void
8245 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8246 {
8247   /* Check for `export'.  */
8248   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8249     {
8250       /* Consume the `export' token.  */
8251       cp_lexer_consume_token (parser->lexer);
8252       /* Warn that we do not support `export'.  */
8253       warning (0, "keyword %<export%> not implemented, and will be ignored");
8254     }
8255
8256   cp_parser_template_declaration_after_export (parser, member_p);
8257 }
8258
8259 /* Parse a template-parameter-list.
8260
8261    template-parameter-list:
8262      template-parameter
8263      template-parameter-list , template-parameter
8264
8265    Returns a TREE_LIST.  Each node represents a template parameter.
8266    The nodes are connected via their TREE_CHAINs.  */
8267
8268 static tree
8269 cp_parser_template_parameter_list (cp_parser* parser)
8270 {
8271   tree parameter_list = NULL_TREE;
8272
8273   while (true)
8274     {
8275       tree parameter;
8276       cp_token *token;
8277       bool is_non_type;
8278
8279       /* Parse the template-parameter.  */
8280       parameter = cp_parser_template_parameter (parser, &is_non_type);
8281       /* Add it to the list.  */
8282       if (parameter != error_mark_node)
8283         parameter_list = process_template_parm (parameter_list,
8284                                                 parameter,
8285                                                 is_non_type);
8286       /* Peek at the next token.  */
8287       token = cp_lexer_peek_token (parser->lexer);
8288       /* If it's not a `,', we're done.  */
8289       if (token->type != CPP_COMMA)
8290         break;
8291       /* Otherwise, consume the `,' token.  */
8292       cp_lexer_consume_token (parser->lexer);
8293     }
8294
8295   return parameter_list;
8296 }
8297
8298 /* Parse a template-parameter.
8299
8300    template-parameter:
8301      type-parameter
8302      parameter-declaration
8303
8304    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8305    the parameter.  The TREE_PURPOSE is the default value, if any.
8306    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8307    iff this parameter is a non-type parameter.  */
8308
8309 static tree
8310 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8311 {
8312   cp_token *token;
8313   cp_parameter_declarator *parameter_declarator;
8314   tree parm;
8315
8316   /* Assume it is a type parameter or a template parameter.  */
8317   *is_non_type = false;
8318   /* Peek at the next token.  */
8319   token = cp_lexer_peek_token (parser->lexer);
8320   /* If it is `class' or `template', we have a type-parameter.  */
8321   if (token->keyword == RID_TEMPLATE)
8322     return cp_parser_type_parameter (parser);
8323   /* If it is `class' or `typename' we do not know yet whether it is a
8324      type parameter or a non-type parameter.  Consider:
8325
8326        template <typename T, typename T::X X> ...
8327
8328      or:
8329
8330        template <class C, class D*> ...
8331
8332      Here, the first parameter is a type parameter, and the second is
8333      a non-type parameter.  We can tell by looking at the token after
8334      the identifier -- if it is a `,', `=', or `>' then we have a type
8335      parameter.  */
8336   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8337     {
8338       /* Peek at the token after `class' or `typename'.  */
8339       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8340       /* If it's an identifier, skip it.  */
8341       if (token->type == CPP_NAME)
8342         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8343       /* Now, see if the token looks like the end of a template
8344          parameter.  */
8345       if (token->type == CPP_COMMA
8346           || token->type == CPP_EQ
8347           || token->type == CPP_GREATER)
8348         return cp_parser_type_parameter (parser);
8349     }
8350
8351   /* Otherwise, it is a non-type parameter.
8352
8353      [temp.param]
8354
8355      When parsing a default template-argument for a non-type
8356      template-parameter, the first non-nested `>' is taken as the end
8357      of the template parameter-list rather than a greater-than
8358      operator.  */
8359   *is_non_type = true;
8360   parameter_declarator
8361      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8362                                         /*parenthesized_p=*/NULL);
8363   parm = grokdeclarator (parameter_declarator->declarator,
8364                          &parameter_declarator->decl_specifiers,
8365                          PARM, /*initialized=*/0,
8366                          /*attrlist=*/NULL);
8367   if (parm == error_mark_node)
8368     return error_mark_node;
8369   return build_tree_list (parameter_declarator->default_argument, parm);
8370 }
8371
8372 /* Parse a type-parameter.
8373
8374    type-parameter:
8375      class identifier [opt]
8376      class identifier [opt] = type-id
8377      typename identifier [opt]
8378      typename identifier [opt] = type-id
8379      template < template-parameter-list > class identifier [opt]
8380      template < template-parameter-list > class identifier [opt]
8381        = id-expression
8382
8383    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8384    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8385    the declaration of the parameter.  */
8386
8387 static tree
8388 cp_parser_type_parameter (cp_parser* parser)
8389 {
8390   cp_token *token;
8391   tree parameter;
8392
8393   /* Look for a keyword to tell us what kind of parameter this is.  */
8394   token = cp_parser_require (parser, CPP_KEYWORD,
8395                              "`class', `typename', or `template'");
8396   if (!token)
8397     return error_mark_node;
8398
8399   switch (token->keyword)
8400     {
8401     case RID_CLASS:
8402     case RID_TYPENAME:
8403       {
8404         tree identifier;
8405         tree default_argument;
8406
8407         /* If the next token is an identifier, then it names the
8408            parameter.  */
8409         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8410           identifier = cp_parser_identifier (parser);
8411         else
8412           identifier = NULL_TREE;
8413
8414         /* Create the parameter.  */
8415         parameter = finish_template_type_parm (class_type_node, identifier);
8416
8417         /* If the next token is an `=', we have a default argument.  */
8418         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8419           {
8420             /* Consume the `=' token.  */
8421             cp_lexer_consume_token (parser->lexer);
8422             /* Parse the default-argument.  */
8423             default_argument = cp_parser_type_id (parser);
8424           }
8425         else
8426           default_argument = NULL_TREE;
8427
8428         /* Create the combined representation of the parameter and the
8429            default argument.  */
8430         parameter = build_tree_list (default_argument, parameter);
8431       }
8432       break;
8433
8434     case RID_TEMPLATE:
8435       {
8436         tree parameter_list;
8437         tree identifier;
8438         tree default_argument;
8439
8440         /* Look for the `<'.  */
8441         cp_parser_require (parser, CPP_LESS, "`<'");
8442         /* Parse the template-parameter-list.  */
8443         begin_template_parm_list ();
8444         parameter_list
8445           = cp_parser_template_parameter_list (parser);
8446         parameter_list = end_template_parm_list (parameter_list);
8447         /* Look for the `>'.  */
8448         cp_parser_require (parser, CPP_GREATER, "`>'");
8449         /* Look for the `class' keyword.  */
8450         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8451         /* If the next token is an `=', then there is a
8452            default-argument.  If the next token is a `>', we are at
8453            the end of the parameter-list.  If the next token is a `,',
8454            then we are at the end of this parameter.  */
8455         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8456             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8457             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8458           {
8459             identifier = cp_parser_identifier (parser);
8460             /* Treat invalid names as if the parameter were nameless.  */
8461             if (identifier == error_mark_node)
8462               identifier = NULL_TREE;
8463           }
8464         else
8465           identifier = NULL_TREE;
8466
8467         /* Create the template parameter.  */
8468         parameter = finish_template_template_parm (class_type_node,
8469                                                    identifier);
8470
8471         /* If the next token is an `=', then there is a
8472            default-argument.  */
8473         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8474           {
8475             bool is_template;
8476
8477             /* Consume the `='.  */
8478             cp_lexer_consume_token (parser->lexer);
8479             /* Parse the id-expression.  */
8480             default_argument
8481               = cp_parser_id_expression (parser,
8482                                          /*template_keyword_p=*/false,
8483                                          /*check_dependency_p=*/true,
8484                                          /*template_p=*/&is_template,
8485                                          /*declarator_p=*/false);
8486             if (TREE_CODE (default_argument) == TYPE_DECL)
8487               /* If the id-expression was a template-id that refers to
8488                  a template-class, we already have the declaration here,
8489                  so no further lookup is needed.  */
8490                  ;
8491             else
8492               /* Look up the name.  */
8493               default_argument
8494                 = cp_parser_lookup_name (parser, default_argument,
8495                                          none_type,
8496                                          /*is_template=*/is_template,
8497                                          /*is_namespace=*/false,
8498                                          /*check_dependency=*/true,
8499                                          /*ambiguous_decls=*/NULL);
8500             /* See if the default argument is valid.  */
8501             default_argument
8502               = check_template_template_default_arg (default_argument);
8503           }
8504         else
8505           default_argument = NULL_TREE;
8506
8507         /* Create the combined representation of the parameter and the
8508            default argument.  */
8509         parameter = build_tree_list (default_argument, parameter);
8510       }
8511       break;
8512
8513     default:
8514       gcc_unreachable ();
8515       break;
8516     }
8517
8518   return parameter;
8519 }
8520
8521 /* Parse a template-id.
8522
8523    template-id:
8524      template-name < template-argument-list [opt] >
8525
8526    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8527    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8528    returned.  Otherwise, if the template-name names a function, or set
8529    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8530    names a class, returns a TYPE_DECL for the specialization.
8531
8532    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8533    uninstantiated templates.  */
8534
8535 static tree
8536 cp_parser_template_id (cp_parser *parser,
8537                        bool template_keyword_p,
8538                        bool check_dependency_p,
8539                        bool is_declaration)
8540 {
8541   tree template;
8542   tree arguments;
8543   tree template_id;
8544   cp_token_position start_of_id = 0;
8545   tree access_check = NULL_TREE;
8546   cp_token *next_token, *next_token_2;
8547   bool is_identifier;
8548
8549   /* If the next token corresponds to a template-id, there is no need
8550      to reparse it.  */
8551   next_token = cp_lexer_peek_token (parser->lexer);
8552   if (next_token->type == CPP_TEMPLATE_ID)
8553     {
8554       tree value;
8555       tree check;
8556
8557       /* Get the stored value.  */
8558       value = cp_lexer_consume_token (parser->lexer)->value;
8559       /* Perform any access checks that were deferred.  */
8560       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8561         perform_or_defer_access_check (TREE_PURPOSE (check),
8562                                        TREE_VALUE (check));
8563       /* Return the stored value.  */
8564       return TREE_VALUE (value);
8565     }
8566
8567   /* Avoid performing name lookup if there is no possibility of
8568      finding a template-id.  */
8569   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8570       || (next_token->type == CPP_NAME
8571           && !cp_parser_nth_token_starts_template_argument_list_p
8572                (parser, 2)))
8573     {
8574       cp_parser_error (parser, "expected template-id");
8575       return error_mark_node;
8576     }
8577
8578   /* Remember where the template-id starts.  */
8579   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8580     start_of_id = cp_lexer_token_position (parser->lexer, false);
8581
8582   push_deferring_access_checks (dk_deferred);
8583
8584   /* Parse the template-name.  */
8585   is_identifier = false;
8586   template = cp_parser_template_name (parser, template_keyword_p,
8587                                       check_dependency_p,
8588                                       is_declaration,
8589                                       &is_identifier);
8590   if (template == error_mark_node || is_identifier)
8591     {
8592       pop_deferring_access_checks ();
8593       return template;
8594     }
8595
8596   /* If we find the sequence `[:' after a template-name, it's probably
8597      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8598      parse correctly the argument list.  */
8599   next_token = cp_lexer_peek_token (parser->lexer);
8600   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8601   if (next_token->type == CPP_OPEN_SQUARE
8602       && next_token->flags & DIGRAPH
8603       && next_token_2->type == CPP_COLON
8604       && !(next_token_2->flags & PREV_WHITE))
8605     {
8606       cp_parser_parse_tentatively (parser);
8607       /* Change `:' into `::'.  */
8608       next_token_2->type = CPP_SCOPE;
8609       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8610          CPP_LESS.  */
8611       cp_lexer_consume_token (parser->lexer);
8612       /* Parse the arguments.  */
8613       arguments = cp_parser_enclosed_template_argument_list (parser);
8614       if (!cp_parser_parse_definitely (parser))
8615         {
8616           /* If we couldn't parse an argument list, then we revert our changes
8617              and return simply an error. Maybe this is not a template-id
8618              after all.  */
8619           next_token_2->type = CPP_COLON;
8620           cp_parser_error (parser, "expected %<<%>");
8621           pop_deferring_access_checks ();
8622           return error_mark_node;
8623         }
8624       /* Otherwise, emit an error about the invalid digraph, but continue
8625          parsing because we got our argument list.  */
8626       pedwarn ("%<<::%> cannot begin a template-argument list");
8627       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8628               "between %<<%> and %<::%>");
8629       if (!flag_permissive)
8630         {
8631           static bool hint;
8632           if (!hint)
8633             {
8634               inform ("(if you use -fpermissive G++ will accept your code)");
8635               hint = true;
8636             }
8637         }
8638     }
8639   else
8640     {
8641       /* Look for the `<' that starts the template-argument-list.  */
8642       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8643         {
8644           pop_deferring_access_checks ();
8645           return error_mark_node;
8646         }
8647       /* Parse the arguments.  */
8648       arguments = cp_parser_enclosed_template_argument_list (parser);
8649     }
8650
8651   /* Build a representation of the specialization.  */
8652   if (TREE_CODE (template) == IDENTIFIER_NODE)
8653     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8654   else if (DECL_CLASS_TEMPLATE_P (template)
8655            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8656     template_id
8657       = finish_template_type (template, arguments,
8658                               cp_lexer_next_token_is (parser->lexer,
8659                                                       CPP_SCOPE));
8660   else
8661     {
8662       /* If it's not a class-template or a template-template, it should be
8663          a function-template.  */
8664       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8665                    || TREE_CODE (template) == OVERLOAD
8666                    || BASELINK_P (template)));
8667
8668       template_id = lookup_template_function (template, arguments);
8669     }
8670
8671   /* Retrieve any deferred checks.  Do not pop this access checks yet
8672      so the memory will not be reclaimed during token replacing below.  */
8673   access_check = get_deferred_access_checks ();
8674
8675   /* If parsing tentatively, replace the sequence of tokens that makes
8676      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8677      should we re-parse the token stream, we will not have to repeat
8678      the effort required to do the parse, nor will we issue duplicate
8679      error messages about problems during instantiation of the
8680      template.  */
8681   if (start_of_id)
8682     {
8683       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8684
8685       /* Reset the contents of the START_OF_ID token.  */
8686       token->type = CPP_TEMPLATE_ID;
8687       token->value = build_tree_list (access_check, template_id);
8688       token->keyword = RID_MAX;
8689
8690       /* Purge all subsequent tokens.  */
8691       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8692
8693       /* ??? Can we actually assume that, if template_id ==
8694          error_mark_node, we will have issued a diagnostic to the
8695          user, as opposed to simply marking the tentative parse as
8696          failed?  */
8697       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8698         error ("parse error in template argument list");
8699     }
8700
8701   pop_deferring_access_checks ();
8702   return template_id;
8703 }
8704
8705 /* Parse a template-name.
8706
8707    template-name:
8708      identifier
8709
8710    The standard should actually say:
8711
8712    template-name:
8713      identifier
8714      operator-function-id
8715
8716    A defect report has been filed about this issue.
8717
8718    A conversion-function-id cannot be a template name because they cannot
8719    be part of a template-id. In fact, looking at this code:
8720
8721    a.operator K<int>()
8722
8723    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8724    It is impossible to call a templated conversion-function-id with an
8725    explicit argument list, since the only allowed template parameter is
8726    the type to which it is converting.
8727
8728    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8729    `template' keyword, in a construction like:
8730
8731      T::template f<3>()
8732
8733    In that case `f' is taken to be a template-name, even though there
8734    is no way of knowing for sure.
8735
8736    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8737    name refers to a set of overloaded functions, at least one of which
8738    is a template, or an IDENTIFIER_NODE with the name of the template,
8739    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8740    names are looked up inside uninstantiated templates.  */
8741
8742 static tree
8743 cp_parser_template_name (cp_parser* parser,
8744                          bool template_keyword_p,
8745                          bool check_dependency_p,
8746                          bool is_declaration,
8747                          bool *is_identifier)
8748 {
8749   tree identifier;
8750   tree decl;
8751   tree fns;
8752
8753   /* If the next token is `operator', then we have either an
8754      operator-function-id or a conversion-function-id.  */
8755   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8756     {
8757       /* We don't know whether we're looking at an
8758          operator-function-id or a conversion-function-id.  */
8759       cp_parser_parse_tentatively (parser);
8760       /* Try an operator-function-id.  */
8761       identifier = cp_parser_operator_function_id (parser);
8762       /* If that didn't work, try a conversion-function-id.  */
8763       if (!cp_parser_parse_definitely (parser))
8764         {
8765           cp_parser_error (parser, "expected template-name");
8766           return error_mark_node;
8767         }
8768     }
8769   /* Look for the identifier.  */
8770   else
8771     identifier = cp_parser_identifier (parser);
8772
8773   /* If we didn't find an identifier, we don't have a template-id.  */
8774   if (identifier == error_mark_node)
8775     return error_mark_node;
8776
8777   /* If the name immediately followed the `template' keyword, then it
8778      is a template-name.  However, if the next token is not `<', then
8779      we do not treat it as a template-name, since it is not being used
8780      as part of a template-id.  This enables us to handle constructs
8781      like:
8782
8783        template <typename T> struct S { S(); };
8784        template <typename T> S<T>::S();
8785
8786      correctly.  We would treat `S' as a template -- if it were `S<T>'
8787      -- but we do not if there is no `<'.  */
8788
8789   if (processing_template_decl
8790       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8791     {
8792       /* In a declaration, in a dependent context, we pretend that the
8793          "template" keyword was present in order to improve error
8794          recovery.  For example, given:
8795
8796            template <typename T> void f(T::X<int>);
8797
8798          we want to treat "X<int>" as a template-id.  */
8799       if (is_declaration
8800           && !template_keyword_p
8801           && parser->scope && TYPE_P (parser->scope)
8802           && check_dependency_p
8803           && dependent_type_p (parser->scope)
8804           /* Do not do this for dtors (or ctors), since they never
8805              need the template keyword before their name.  */
8806           && !constructor_name_p (identifier, parser->scope))
8807         {
8808           cp_token_position start = 0;
8809
8810           /* Explain what went wrong.  */
8811           error ("non-template %qD used as template", identifier);
8812           inform ("use %<%T::template %D%> to indicate that it is a template",
8813                   parser->scope, identifier);
8814           /* If parsing tentatively, find the location of the "<" token.  */
8815           if (cp_parser_simulate_error (parser))
8816             start = cp_lexer_token_position (parser->lexer, true);
8817           /* Parse the template arguments so that we can issue error
8818              messages about them.  */
8819           cp_lexer_consume_token (parser->lexer);
8820           cp_parser_enclosed_template_argument_list (parser);
8821           /* Skip tokens until we find a good place from which to
8822              continue parsing.  */
8823           cp_parser_skip_to_closing_parenthesis (parser,
8824                                                  /*recovering=*/true,
8825                                                  /*or_comma=*/true,
8826                                                  /*consume_paren=*/false);
8827           /* If parsing tentatively, permanently remove the
8828              template argument list.  That will prevent duplicate
8829              error messages from being issued about the missing
8830              "template" keyword.  */
8831           if (start)
8832             cp_lexer_purge_tokens_after (parser->lexer, start);
8833           if (is_identifier)
8834             *is_identifier = true;
8835           return identifier;
8836         }
8837
8838       /* If the "template" keyword is present, then there is generally
8839          no point in doing name-lookup, so we just return IDENTIFIER.
8840          But, if the qualifying scope is non-dependent then we can
8841          (and must) do name-lookup normally.  */
8842       if (template_keyword_p
8843           && (!parser->scope
8844               || (TYPE_P (parser->scope)
8845                   && dependent_type_p (parser->scope))))
8846         return identifier;
8847     }
8848
8849   /* Look up the name.  */
8850   decl = cp_parser_lookup_name (parser, identifier,
8851                                 none_type,
8852                                 /*is_template=*/false,
8853                                 /*is_namespace=*/false,
8854                                 check_dependency_p,
8855                                 /*ambiguous_decls=*/NULL);
8856   decl = maybe_get_template_decl_from_type_decl (decl);
8857
8858   /* If DECL is a template, then the name was a template-name.  */
8859   if (TREE_CODE (decl) == TEMPLATE_DECL)
8860     ;
8861   else
8862     {
8863       tree fn = NULL_TREE;
8864
8865       /* The standard does not explicitly indicate whether a name that
8866          names a set of overloaded declarations, some of which are
8867          templates, is a template-name.  However, such a name should
8868          be a template-name; otherwise, there is no way to form a
8869          template-id for the overloaded templates.  */
8870       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8871       if (TREE_CODE (fns) == OVERLOAD)
8872         for (fn = fns; fn; fn = OVL_NEXT (fn))
8873           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8874             break;
8875
8876       if (!fn)
8877         {
8878           /* The name does not name a template.  */
8879           cp_parser_error (parser, "expected template-name");
8880           return error_mark_node;
8881         }
8882     }
8883
8884   /* If DECL is dependent, and refers to a function, then just return
8885      its name; we will look it up again during template instantiation.  */
8886   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8887     {
8888       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8889       if (TYPE_P (scope) && dependent_type_p (scope))
8890         return identifier;
8891     }
8892
8893   return decl;
8894 }
8895
8896 /* Parse a template-argument-list.
8897
8898    template-argument-list:
8899      template-argument
8900      template-argument-list , template-argument
8901
8902    Returns a TREE_VEC containing the arguments.  */
8903
8904 static tree
8905 cp_parser_template_argument_list (cp_parser* parser)
8906 {
8907   tree fixed_args[10];
8908   unsigned n_args = 0;
8909   unsigned alloced = 10;
8910   tree *arg_ary = fixed_args;
8911   tree vec;
8912   bool saved_in_template_argument_list_p;
8913   bool saved_ice_p;
8914   bool saved_non_ice_p;
8915
8916   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8917   parser->in_template_argument_list_p = true;
8918   /* Even if the template-id appears in an integral
8919      constant-expression, the contents of the argument list do 
8920      not.  */ 
8921   saved_ice_p = parser->integral_constant_expression_p;
8922   parser->integral_constant_expression_p = false;
8923   saved_non_ice_p = parser->non_integral_constant_expression_p;
8924   parser->non_integral_constant_expression_p = false;
8925   /* Parse the arguments.  */
8926   do
8927     {
8928       tree argument;
8929
8930       if (n_args)
8931         /* Consume the comma.  */
8932         cp_lexer_consume_token (parser->lexer);
8933
8934       /* Parse the template-argument.  */
8935       argument = cp_parser_template_argument (parser);
8936       if (n_args == alloced)
8937         {
8938           alloced *= 2;
8939
8940           if (arg_ary == fixed_args)
8941             {
8942               arg_ary = XNEWVEC (tree, alloced);
8943               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8944             }
8945           else
8946             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
8947         }
8948       arg_ary[n_args++] = argument;
8949     }
8950   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8951
8952   vec = make_tree_vec (n_args);
8953
8954   while (n_args--)
8955     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8956
8957   if (arg_ary != fixed_args)
8958     free (arg_ary);
8959   parser->non_integral_constant_expression_p = saved_non_ice_p;
8960   parser->integral_constant_expression_p = saved_ice_p;
8961   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8962   return vec;
8963 }
8964
8965 /* Parse a template-argument.
8966
8967    template-argument:
8968      assignment-expression
8969      type-id
8970      id-expression
8971
8972    The representation is that of an assignment-expression, type-id, or
8973    id-expression -- except that the qualified id-expression is
8974    evaluated, so that the value returned is either a DECL or an
8975    OVERLOAD.
8976
8977    Although the standard says "assignment-expression", it forbids
8978    throw-expressions or assignments in the template argument.
8979    Therefore, we use "conditional-expression" instead.  */
8980
8981 static tree
8982 cp_parser_template_argument (cp_parser* parser)
8983 {
8984   tree argument;
8985   bool template_p;
8986   bool address_p;
8987   bool maybe_type_id = false;
8988   cp_token *token;
8989   cp_id_kind idk;
8990
8991   /* There's really no way to know what we're looking at, so we just
8992      try each alternative in order.
8993
8994        [temp.arg]
8995
8996        In a template-argument, an ambiguity between a type-id and an
8997        expression is resolved to a type-id, regardless of the form of
8998        the corresponding template-parameter.
8999
9000      Therefore, we try a type-id first.  */
9001   cp_parser_parse_tentatively (parser);
9002   argument = cp_parser_type_id (parser);
9003   /* If there was no error parsing the type-id but the next token is a '>>',
9004      we probably found a typo for '> >'. But there are type-id which are
9005      also valid expressions. For instance:
9006
9007      struct X { int operator >> (int); };
9008      template <int V> struct Foo {};
9009      Foo<X () >> 5> r;
9010
9011      Here 'X()' is a valid type-id of a function type, but the user just
9012      wanted to write the expression "X() >> 5". Thus, we remember that we
9013      found a valid type-id, but we still try to parse the argument as an
9014      expression to see what happens.  */
9015   if (!cp_parser_error_occurred (parser)
9016       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9017     {
9018       maybe_type_id = true;
9019       cp_parser_abort_tentative_parse (parser);
9020     }
9021   else
9022     {
9023       /* If the next token isn't a `,' or a `>', then this argument wasn't
9024       really finished. This means that the argument is not a valid
9025       type-id.  */
9026       if (!cp_parser_next_token_ends_template_argument_p (parser))
9027         cp_parser_error (parser, "expected template-argument");
9028       /* If that worked, we're done.  */
9029       if (cp_parser_parse_definitely (parser))
9030         return argument;
9031     }
9032   /* We're still not sure what the argument will be.  */
9033   cp_parser_parse_tentatively (parser);
9034   /* Try a template.  */
9035   argument = cp_parser_id_expression (parser,
9036                                       /*template_keyword_p=*/false,
9037                                       /*check_dependency_p=*/true,
9038                                       &template_p,
9039                                       /*declarator_p=*/false);
9040   /* If the next token isn't a `,' or a `>', then this argument wasn't
9041      really finished.  */
9042   if (!cp_parser_next_token_ends_template_argument_p (parser))
9043     cp_parser_error (parser, "expected template-argument");
9044   if (!cp_parser_error_occurred (parser))
9045     {
9046       /* Figure out what is being referred to.  If the id-expression
9047          was for a class template specialization, then we will have a
9048          TYPE_DECL at this point.  There is no need to do name lookup
9049          at this point in that case.  */
9050       if (TREE_CODE (argument) != TYPE_DECL)
9051         argument = cp_parser_lookup_name (parser, argument,
9052                                           none_type,
9053                                           /*is_template=*/template_p,
9054                                           /*is_namespace=*/false,
9055                                           /*check_dependency=*/true,
9056                                           /*ambiguous_decls=*/NULL);
9057       if (TREE_CODE (argument) != TEMPLATE_DECL
9058           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9059         cp_parser_error (parser, "expected template-name");
9060     }
9061   if (cp_parser_parse_definitely (parser))
9062     return argument;
9063   /* It must be a non-type argument.  There permitted cases are given
9064      in [temp.arg.nontype]:
9065
9066      -- an integral constant-expression of integral or enumeration
9067         type; or
9068
9069      -- the name of a non-type template-parameter; or
9070
9071      -- the name of an object or function with external linkage...
9072
9073      -- the address of an object or function with external linkage...
9074
9075      -- a pointer to member...  */
9076   /* Look for a non-type template parameter.  */
9077   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9078     {
9079       cp_parser_parse_tentatively (parser);
9080       argument = cp_parser_primary_expression (parser,
9081                                                /*adress_p=*/false,
9082                                                /*cast_p=*/false,
9083                                                /*template_arg_p=*/true,
9084                                                &idk);
9085       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9086           || !cp_parser_next_token_ends_template_argument_p (parser))
9087         cp_parser_simulate_error (parser);
9088       if (cp_parser_parse_definitely (parser))
9089         return argument;
9090     }
9091
9092   /* If the next token is "&", the argument must be the address of an
9093      object or function with external linkage.  */
9094   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9095   if (address_p)
9096     cp_lexer_consume_token (parser->lexer);
9097   /* See if we might have an id-expression.  */
9098   token = cp_lexer_peek_token (parser->lexer);
9099   if (token->type == CPP_NAME
9100       || token->keyword == RID_OPERATOR
9101       || token->type == CPP_SCOPE
9102       || token->type == CPP_TEMPLATE_ID
9103       || token->type == CPP_NESTED_NAME_SPECIFIER)
9104     {
9105       cp_parser_parse_tentatively (parser);
9106       argument = cp_parser_primary_expression (parser,
9107                                                address_p,
9108                                                /*cast_p=*/false,
9109                                                /*template_arg_p=*/true,
9110                                                &idk);
9111       if (cp_parser_error_occurred (parser)
9112           || !cp_parser_next_token_ends_template_argument_p (parser))
9113         cp_parser_abort_tentative_parse (parser);
9114       else
9115         {
9116           if (TREE_CODE (argument) == INDIRECT_REF)
9117             {
9118               gcc_assert (REFERENCE_REF_P (argument));
9119               argument = TREE_OPERAND (argument, 0);
9120             }
9121
9122           if (TREE_CODE (argument) == BASELINK)
9123             /* We don't need the information about what class was used
9124                to name the overloaded functions.  */  
9125             argument = BASELINK_FUNCTIONS (argument);
9126
9127           if (TREE_CODE (argument) == VAR_DECL)
9128             {
9129               /* A variable without external linkage might still be a
9130                  valid constant-expression, so no error is issued here
9131                  if the external-linkage check fails.  */
9132               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9133                 cp_parser_simulate_error (parser);
9134             }
9135           else if (is_overloaded_fn (argument))
9136             /* All overloaded functions are allowed; if the external
9137                linkage test does not pass, an error will be issued
9138                later.  */
9139             ;
9140           else if (address_p
9141                    && (TREE_CODE (argument) == OFFSET_REF
9142                        || TREE_CODE (argument) == SCOPE_REF))
9143             /* A pointer-to-member.  */
9144             ;
9145           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9146             ;
9147           else
9148             cp_parser_simulate_error (parser);
9149
9150           if (cp_parser_parse_definitely (parser))
9151             {
9152               if (address_p)
9153                 argument = build_x_unary_op (ADDR_EXPR, argument);
9154               return argument;
9155             }
9156         }
9157     }
9158   /* If the argument started with "&", there are no other valid
9159      alternatives at this point.  */
9160   if (address_p)
9161     {
9162       cp_parser_error (parser, "invalid non-type template argument");
9163       return error_mark_node;
9164     }
9165
9166   /* If the argument wasn't successfully parsed as a type-id followed
9167      by '>>', the argument can only be a constant expression now.
9168      Otherwise, we try parsing the constant-expression tentatively,
9169      because the argument could really be a type-id.  */
9170   if (maybe_type_id)
9171     cp_parser_parse_tentatively (parser);
9172   argument = cp_parser_constant_expression (parser,
9173                                             /*allow_non_constant_p=*/false,
9174                                             /*non_constant_p=*/NULL);
9175   argument = fold_non_dependent_expr (argument);
9176   if (!maybe_type_id)
9177     return argument;
9178   if (!cp_parser_next_token_ends_template_argument_p (parser))
9179     cp_parser_error (parser, "expected template-argument");
9180   if (cp_parser_parse_definitely (parser))
9181     return argument;
9182   /* We did our best to parse the argument as a non type-id, but that
9183      was the only alternative that matched (albeit with a '>' after
9184      it). We can assume it's just a typo from the user, and a
9185      diagnostic will then be issued.  */
9186   return cp_parser_type_id (parser);
9187 }
9188
9189 /* Parse an explicit-instantiation.
9190
9191    explicit-instantiation:
9192      template declaration
9193
9194    Although the standard says `declaration', what it really means is:
9195
9196    explicit-instantiation:
9197      template decl-specifier-seq [opt] declarator [opt] ;
9198
9199    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9200    supposed to be allowed.  A defect report has been filed about this
9201    issue.
9202
9203    GNU Extension:
9204
9205    explicit-instantiation:
9206      storage-class-specifier template
9207        decl-specifier-seq [opt] declarator [opt] ;
9208      function-specifier template
9209        decl-specifier-seq [opt] declarator [opt] ;  */
9210
9211 static void
9212 cp_parser_explicit_instantiation (cp_parser* parser)
9213 {
9214   int declares_class_or_enum;
9215   cp_decl_specifier_seq decl_specifiers;
9216   tree extension_specifier = NULL_TREE;
9217
9218   /* Look for an (optional) storage-class-specifier or
9219      function-specifier.  */
9220   if (cp_parser_allow_gnu_extensions_p (parser))
9221     {
9222       extension_specifier
9223         = cp_parser_storage_class_specifier_opt (parser);
9224       if (!extension_specifier)
9225         extension_specifier
9226           = cp_parser_function_specifier_opt (parser,
9227                                               /*decl_specs=*/NULL);
9228     }
9229
9230   /* Look for the `template' keyword.  */
9231   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9232   /* Let the front end know that we are processing an explicit
9233      instantiation.  */
9234   begin_explicit_instantiation ();
9235   /* [temp.explicit] says that we are supposed to ignore access
9236      control while processing explicit instantiation directives.  */
9237   push_deferring_access_checks (dk_no_check);
9238   /* Parse a decl-specifier-seq.  */
9239   cp_parser_decl_specifier_seq (parser,
9240                                 CP_PARSER_FLAGS_OPTIONAL,
9241                                 &decl_specifiers,
9242                                 &declares_class_or_enum);
9243   /* If there was exactly one decl-specifier, and it declared a class,
9244      and there's no declarator, then we have an explicit type
9245      instantiation.  */
9246   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9247     {
9248       tree type;
9249
9250       type = check_tag_decl (&decl_specifiers);
9251       /* Turn access control back on for names used during
9252          template instantiation.  */
9253       pop_deferring_access_checks ();
9254       if (type)
9255         do_type_instantiation (type, extension_specifier,
9256                                /*complain=*/tf_error);
9257     }
9258   else
9259     {
9260       cp_declarator *declarator;
9261       tree decl;
9262
9263       /* Parse the declarator.  */
9264       declarator
9265         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9266                                 /*ctor_dtor_or_conv_p=*/NULL,
9267                                 /*parenthesized_p=*/NULL,
9268                                 /*member_p=*/false);
9269       if (declares_class_or_enum & 2)
9270         cp_parser_check_for_definition_in_return_type (declarator,
9271                                                        decl_specifiers.type);
9272       if (declarator != cp_error_declarator)
9273         {
9274           decl = grokdeclarator (declarator, &decl_specifiers,
9275                                  NORMAL, 0, NULL);
9276           /* Turn access control back on for names used during
9277              template instantiation.  */
9278           pop_deferring_access_checks ();
9279           /* Do the explicit instantiation.  */
9280           do_decl_instantiation (decl, extension_specifier);
9281         }
9282       else
9283         {
9284           pop_deferring_access_checks ();
9285           /* Skip the body of the explicit instantiation.  */
9286           cp_parser_skip_to_end_of_statement (parser);
9287         }
9288     }
9289   /* We're done with the instantiation.  */
9290   end_explicit_instantiation ();
9291
9292   cp_parser_consume_semicolon_at_end_of_statement (parser);
9293 }
9294
9295 /* Parse an explicit-specialization.
9296
9297    explicit-specialization:
9298      template < > declaration
9299
9300    Although the standard says `declaration', what it really means is:
9301
9302    explicit-specialization:
9303      template <> decl-specifier [opt] init-declarator [opt] ;
9304      template <> function-definition
9305      template <> explicit-specialization
9306      template <> template-declaration  */
9307
9308 static void
9309 cp_parser_explicit_specialization (cp_parser* parser)
9310 {
9311   bool need_lang_pop;
9312   /* Look for the `template' keyword.  */
9313   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9314   /* Look for the `<'.  */
9315   cp_parser_require (parser, CPP_LESS, "`<'");
9316   /* Look for the `>'.  */
9317   cp_parser_require (parser, CPP_GREATER, "`>'");
9318   /* We have processed another parameter list.  */
9319   ++parser->num_template_parameter_lists;
9320   /* [temp]
9321    
9322      A template ... explicit specialization ... shall not have C
9323      linkage.  */ 
9324   if (current_lang_name == lang_name_c)
9325     {
9326       error ("template specialization with C linkage");
9327       /* Give it C++ linkage to avoid confusing other parts of the
9328          front end.  */
9329       push_lang_context (lang_name_cplusplus);
9330       need_lang_pop = true;
9331     }
9332   else
9333     need_lang_pop = false;
9334   /* Let the front end know that we are beginning a specialization.  */
9335   begin_specialization ();
9336   /* If the next keyword is `template', we need to figure out whether
9337      or not we're looking a template-declaration.  */
9338   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9339     {
9340       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9341           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9342         cp_parser_template_declaration_after_export (parser,
9343                                                      /*member_p=*/false);
9344       else
9345         cp_parser_explicit_specialization (parser);
9346     }
9347   else
9348     /* Parse the dependent declaration.  */
9349     cp_parser_single_declaration (parser,
9350                                   /*member_p=*/false,
9351                                   /*friend_p=*/NULL);
9352   /* We're done with the specialization.  */
9353   end_specialization ();
9354   /* For the erroneous case of a template with C linkage, we pushed an
9355      implicit C++ linkage scope; exit that scope now.  */
9356   if (need_lang_pop)
9357     pop_lang_context ();
9358   /* We're done with this parameter list.  */
9359   --parser->num_template_parameter_lists;
9360 }
9361
9362 /* Parse a type-specifier.
9363
9364    type-specifier:
9365      simple-type-specifier
9366      class-specifier
9367      enum-specifier
9368      elaborated-type-specifier
9369      cv-qualifier
9370
9371    GNU Extension:
9372
9373    type-specifier:
9374      __complex__
9375
9376    Returns a representation of the type-specifier.  For a
9377    class-specifier, enum-specifier, or elaborated-type-specifier, a
9378    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9379
9380    The parser flags FLAGS is used to control type-specifier parsing.
9381
9382    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9383    in a decl-specifier-seq.
9384
9385    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9386    class-specifier, enum-specifier, or elaborated-type-specifier, then
9387    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9388    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9389    zero.
9390
9391    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9392    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9393    is set to FALSE.  */
9394
9395 static tree
9396 cp_parser_type_specifier (cp_parser* parser,
9397                           cp_parser_flags flags,
9398                           cp_decl_specifier_seq *decl_specs,
9399                           bool is_declaration,
9400                           int* declares_class_or_enum,
9401                           bool* is_cv_qualifier)
9402 {
9403   tree type_spec = NULL_TREE;
9404   cp_token *token;
9405   enum rid keyword;
9406   cp_decl_spec ds = ds_last;
9407
9408   /* Assume this type-specifier does not declare a new type.  */
9409   if (declares_class_or_enum)
9410     *declares_class_or_enum = 0;
9411   /* And that it does not specify a cv-qualifier.  */
9412   if (is_cv_qualifier)
9413     *is_cv_qualifier = false;
9414   /* Peek at the next token.  */
9415   token = cp_lexer_peek_token (parser->lexer);
9416
9417   /* If we're looking at a keyword, we can use that to guide the
9418      production we choose.  */
9419   keyword = token->keyword;
9420   switch (keyword)
9421     {
9422     case RID_ENUM:
9423       /* 'enum' [identifier] '{' introduces an enum-specifier;
9424          'enum' <anything else> introduces an elaborated-type-specifier.  */
9425       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9426           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9427               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9428                  == CPP_OPEN_BRACE))
9429         {
9430           if (parser->num_template_parameter_lists)
9431             {
9432               error ("template declaration of %qs", "enum");
9433               cp_parser_skip_to_end_of_block_or_statement (parser);
9434               type_spec = error_mark_node;
9435             }
9436           else
9437             type_spec = cp_parser_enum_specifier (parser);
9438
9439           if (declares_class_or_enum)
9440             *declares_class_or_enum = 2;
9441           if (decl_specs)
9442             cp_parser_set_decl_spec_type (decl_specs,
9443                                           type_spec,
9444                                           /*user_defined_p=*/true);
9445           return type_spec;
9446         }
9447       else
9448         goto elaborated_type_specifier;
9449
9450       /* Any of these indicate either a class-specifier, or an
9451          elaborated-type-specifier.  */
9452     case RID_CLASS:
9453     case RID_STRUCT:
9454     case RID_UNION:
9455       /* Parse tentatively so that we can back up if we don't find a
9456          class-specifier.  */
9457       cp_parser_parse_tentatively (parser);
9458       /* Look for the class-specifier.  */
9459       type_spec = cp_parser_class_specifier (parser);
9460       /* If that worked, we're done.  */
9461       if (cp_parser_parse_definitely (parser))
9462         {
9463           if (declares_class_or_enum)
9464             *declares_class_or_enum = 2;
9465           if (decl_specs)
9466             cp_parser_set_decl_spec_type (decl_specs,
9467                                           type_spec,
9468                                           /*user_defined_p=*/true);
9469           return type_spec;
9470         }
9471
9472       /* Fall through.  */
9473     elaborated_type_specifier:
9474       /* We're declaring (not defining) a class or enum.  */
9475       if (declares_class_or_enum)
9476         *declares_class_or_enum = 1;
9477
9478       /* Fall through.  */
9479     case RID_TYPENAME:
9480       /* Look for an elaborated-type-specifier.  */
9481       type_spec
9482         = (cp_parser_elaborated_type_specifier
9483            (parser,
9484             decl_specs && decl_specs->specs[(int) ds_friend],
9485             is_declaration));
9486       if (decl_specs)
9487         cp_parser_set_decl_spec_type (decl_specs,
9488                                       type_spec,
9489                                       /*user_defined_p=*/true);
9490       return type_spec;
9491
9492     case RID_CONST:
9493       ds = ds_const;
9494       if (is_cv_qualifier)
9495         *is_cv_qualifier = true;
9496       break;
9497
9498     case RID_VOLATILE:
9499       ds = ds_volatile;
9500       if (is_cv_qualifier)
9501         *is_cv_qualifier = true;
9502       break;
9503
9504     case RID_RESTRICT:
9505       ds = ds_restrict;
9506       if (is_cv_qualifier)
9507         *is_cv_qualifier = true;
9508       break;
9509
9510     case RID_COMPLEX:
9511       /* The `__complex__' keyword is a GNU extension.  */
9512       ds = ds_complex;
9513       break;
9514
9515     default:
9516       break;
9517     }
9518
9519   /* Handle simple keywords.  */
9520   if (ds != ds_last)
9521     {
9522       if (decl_specs)
9523         {
9524           ++decl_specs->specs[(int)ds];
9525           decl_specs->any_specifiers_p = true;
9526         }
9527       return cp_lexer_consume_token (parser->lexer)->value;
9528     }
9529
9530   /* If we do not already have a type-specifier, assume we are looking
9531      at a simple-type-specifier.  */
9532   type_spec = cp_parser_simple_type_specifier (parser,
9533                                                decl_specs,
9534                                                flags);
9535
9536   /* If we didn't find a type-specifier, and a type-specifier was not
9537      optional in this context, issue an error message.  */
9538   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9539     {
9540       cp_parser_error (parser, "expected type specifier");
9541       return error_mark_node;
9542     }
9543
9544   return type_spec;
9545 }
9546
9547 /* Parse a simple-type-specifier.
9548
9549    simple-type-specifier:
9550      :: [opt] nested-name-specifier [opt] type-name
9551      :: [opt] nested-name-specifier template template-id
9552      char
9553      wchar_t
9554      bool
9555      short
9556      int
9557      long
9558      signed
9559      unsigned
9560      float
9561      double
9562      void
9563
9564    GNU Extension:
9565
9566    simple-type-specifier:
9567      __typeof__ unary-expression
9568      __typeof__ ( type-id )
9569
9570    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9571    appropriately updated.  */
9572
9573 static tree
9574 cp_parser_simple_type_specifier (cp_parser* parser,
9575                                  cp_decl_specifier_seq *decl_specs,
9576                                  cp_parser_flags flags)
9577 {
9578   tree type = NULL_TREE;
9579   cp_token *token;
9580
9581   /* Peek at the next token.  */
9582   token = cp_lexer_peek_token (parser->lexer);
9583
9584   /* If we're looking at a keyword, things are easy.  */
9585   switch (token->keyword)
9586     {
9587     case RID_CHAR:
9588       if (decl_specs)
9589         decl_specs->explicit_char_p = true;
9590       type = char_type_node;
9591       break;
9592     case RID_WCHAR:
9593       type = wchar_type_node;
9594       break;
9595     case RID_BOOL:
9596       type = boolean_type_node;
9597       break;
9598     case RID_SHORT:
9599       if (decl_specs)
9600         ++decl_specs->specs[(int) ds_short];
9601       type = short_integer_type_node;
9602       break;
9603     case RID_INT:
9604       if (decl_specs)
9605         decl_specs->explicit_int_p = true;
9606       type = integer_type_node;
9607       break;
9608     case RID_LONG:
9609       if (decl_specs)
9610         ++decl_specs->specs[(int) ds_long];
9611       type = long_integer_type_node;
9612       break;
9613     case RID_SIGNED:
9614       if (decl_specs)
9615         ++decl_specs->specs[(int) ds_signed];
9616       type = integer_type_node;
9617       break;
9618     case RID_UNSIGNED:
9619       if (decl_specs)
9620         ++decl_specs->specs[(int) ds_unsigned];
9621       type = unsigned_type_node;
9622       break;
9623     case RID_FLOAT:
9624       type = float_type_node;
9625       break;
9626     case RID_DOUBLE:
9627       type = double_type_node;
9628       break;
9629     case RID_VOID:
9630       type = void_type_node;
9631       break;
9632
9633     case RID_TYPEOF:
9634       /* Consume the `typeof' token.  */
9635       cp_lexer_consume_token (parser->lexer);
9636       /* Parse the operand to `typeof'.  */
9637       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9638       /* If it is not already a TYPE, take its type.  */
9639       if (!TYPE_P (type))
9640         type = finish_typeof (type);
9641
9642       if (decl_specs)
9643         cp_parser_set_decl_spec_type (decl_specs, type,
9644                                       /*user_defined_p=*/true);
9645
9646       return type;
9647
9648     default:
9649       break;
9650     }
9651
9652   /* If the type-specifier was for a built-in type, we're done.  */
9653   if (type)
9654     {
9655       tree id;
9656
9657       /* Record the type.  */
9658       if (decl_specs
9659           && (token->keyword != RID_SIGNED
9660               && token->keyword != RID_UNSIGNED
9661               && token->keyword != RID_SHORT
9662               && token->keyword != RID_LONG))
9663         cp_parser_set_decl_spec_type (decl_specs,
9664                                       type,
9665                                       /*user_defined=*/false);
9666       if (decl_specs)
9667         decl_specs->any_specifiers_p = true;
9668
9669       /* Consume the token.  */
9670       id = cp_lexer_consume_token (parser->lexer)->value;
9671
9672       /* There is no valid C++ program where a non-template type is
9673          followed by a "<".  That usually indicates that the user thought
9674          that the type was a template.  */
9675       cp_parser_check_for_invalid_template_id (parser, type);
9676
9677       return TYPE_NAME (type);
9678     }
9679
9680   /* The type-specifier must be a user-defined type.  */
9681   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9682     {
9683       bool qualified_p;
9684       bool global_p;
9685
9686       /* Don't gobble tokens or issue error messages if this is an
9687          optional type-specifier.  */
9688       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9689         cp_parser_parse_tentatively (parser);
9690
9691       /* Look for the optional `::' operator.  */
9692       global_p
9693         = (cp_parser_global_scope_opt (parser,
9694                                        /*current_scope_valid_p=*/false)
9695            != NULL_TREE);
9696       /* Look for the nested-name specifier.  */
9697       qualified_p
9698         = (cp_parser_nested_name_specifier_opt (parser,
9699                                                 /*typename_keyword_p=*/false,
9700                                                 /*check_dependency_p=*/true,
9701                                                 /*type_p=*/false,
9702                                                 /*is_declaration=*/false)
9703            != NULL_TREE);
9704       /* If we have seen a nested-name-specifier, and the next token
9705          is `template', then we are using the template-id production.  */
9706       if (parser->scope
9707           && cp_parser_optional_template_keyword (parser))
9708         {
9709           /* Look for the template-id.  */
9710           type = cp_parser_template_id (parser,
9711                                         /*template_keyword_p=*/true,
9712                                         /*check_dependency_p=*/true,
9713                                         /*is_declaration=*/false);
9714           /* If the template-id did not name a type, we are out of
9715              luck.  */
9716           if (TREE_CODE (type) != TYPE_DECL)
9717             {
9718               cp_parser_error (parser, "expected template-id for type");
9719               type = NULL_TREE;
9720             }
9721         }
9722       /* Otherwise, look for a type-name.  */
9723       else
9724         type = cp_parser_type_name (parser);
9725       /* Keep track of all name-lookups performed in class scopes.  */
9726       if (type
9727           && !global_p
9728           && !qualified_p
9729           && TREE_CODE (type) == TYPE_DECL
9730           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9731         maybe_note_name_used_in_class (DECL_NAME (type), type);
9732       /* If it didn't work out, we don't have a TYPE.  */
9733       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9734           && !cp_parser_parse_definitely (parser))
9735         type = NULL_TREE;
9736       if (type && decl_specs)
9737         cp_parser_set_decl_spec_type (decl_specs, type,
9738                                       /*user_defined=*/true);
9739     }
9740
9741   /* If we didn't get a type-name, issue an error message.  */
9742   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9743     {
9744       cp_parser_error (parser, "expected type-name");
9745       return error_mark_node;
9746     }
9747
9748   /* There is no valid C++ program where a non-template type is
9749      followed by a "<".  That usually indicates that the user thought
9750      that the type was a template.  */
9751   if (type && type != error_mark_node)
9752     {
9753       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9754          If it is, then the '<'...'>' enclose protocol names rather than
9755          template arguments, and so everything is fine.  */
9756       if (c_dialect_objc ()
9757           && (objc_is_id (type) || objc_is_class_name (type)))
9758         {
9759           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9760           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9761
9762           /* Clobber the "unqualified" type previously entered into
9763              DECL_SPECS with the new, improved protocol-qualified version.  */
9764           if (decl_specs)
9765             decl_specs->type = qual_type;
9766
9767           return qual_type;
9768         }
9769
9770       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9771     }
9772
9773   return type;
9774 }
9775
9776 /* Parse a type-name.
9777
9778    type-name:
9779      class-name
9780      enum-name
9781      typedef-name
9782
9783    enum-name:
9784      identifier
9785
9786    typedef-name:
9787      identifier
9788
9789    Returns a TYPE_DECL for the type.  */
9790
9791 static tree
9792 cp_parser_type_name (cp_parser* parser)
9793 {
9794   tree type_decl;
9795   tree identifier;
9796
9797   /* We can't know yet whether it is a class-name or not.  */
9798   cp_parser_parse_tentatively (parser);
9799   /* Try a class-name.  */
9800   type_decl = cp_parser_class_name (parser,
9801                                     /*typename_keyword_p=*/false,
9802                                     /*template_keyword_p=*/false,
9803                                     none_type,
9804                                     /*check_dependency_p=*/true,
9805                                     /*class_head_p=*/false,
9806                                     /*is_declaration=*/false);
9807   /* If it's not a class-name, keep looking.  */
9808   if (!cp_parser_parse_definitely (parser))
9809     {
9810       /* It must be a typedef-name or an enum-name.  */
9811       identifier = cp_parser_identifier (parser);
9812       if (identifier == error_mark_node)
9813         return error_mark_node;
9814
9815       /* Look up the type-name.  */
9816       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9817
9818       if (TREE_CODE (type_decl) != TYPE_DECL
9819           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9820         {
9821           /* See if this is an Objective-C type.  */
9822           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9823           tree type = objc_get_protocol_qualified_type (identifier, protos);
9824           if (type)
9825             type_decl = TYPE_NAME (type);
9826         }
9827
9828       /* Issue an error if we did not find a type-name.  */
9829       if (TREE_CODE (type_decl) != TYPE_DECL)
9830         {
9831           if (!cp_parser_simulate_error (parser))
9832             cp_parser_name_lookup_error (parser, identifier, type_decl,
9833                                          "is not a type");
9834           type_decl = error_mark_node;
9835         }
9836       /* Remember that the name was used in the definition of the
9837          current class so that we can check later to see if the
9838          meaning would have been different after the class was
9839          entirely defined.  */
9840       else if (type_decl != error_mark_node
9841                && !parser->scope)
9842         maybe_note_name_used_in_class (identifier, type_decl);
9843     }
9844
9845   return type_decl;
9846 }
9847
9848
9849 /* Parse an elaborated-type-specifier.  Note that the grammar given
9850    here incorporates the resolution to DR68.
9851
9852    elaborated-type-specifier:
9853      class-key :: [opt] nested-name-specifier [opt] identifier
9854      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9855      enum :: [opt] nested-name-specifier [opt] identifier
9856      typename :: [opt] nested-name-specifier identifier
9857      typename :: [opt] nested-name-specifier template [opt]
9858        template-id
9859
9860    GNU extension:
9861
9862    elaborated-type-specifier:
9863      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9864      class-key attributes :: [opt] nested-name-specifier [opt]
9865                template [opt] template-id
9866      enum attributes :: [opt] nested-name-specifier [opt] identifier
9867
9868    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9869    declared `friend'.  If IS_DECLARATION is TRUE, then this
9870    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9871    something is being declared.
9872
9873    Returns the TYPE specified.  */
9874
9875 static tree
9876 cp_parser_elaborated_type_specifier (cp_parser* parser,
9877                                      bool is_friend,
9878                                      bool is_declaration)
9879 {
9880   enum tag_types tag_type;
9881   tree identifier;
9882   tree type = NULL_TREE;
9883   tree attributes = NULL_TREE;
9884
9885   /* See if we're looking at the `enum' keyword.  */
9886   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9887     {
9888       /* Consume the `enum' token.  */
9889       cp_lexer_consume_token (parser->lexer);
9890       /* Remember that it's an enumeration type.  */
9891       tag_type = enum_type;
9892       /* Parse the attributes.  */
9893       attributes = cp_parser_attributes_opt (parser);
9894     }
9895   /* Or, it might be `typename'.  */
9896   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9897                                            RID_TYPENAME))
9898     {
9899       /* Consume the `typename' token.  */
9900       cp_lexer_consume_token (parser->lexer);
9901       /* Remember that it's a `typename' type.  */
9902       tag_type = typename_type;
9903       /* The `typename' keyword is only allowed in templates.  */
9904       if (!processing_template_decl)
9905         pedwarn ("using %<typename%> outside of template");
9906     }
9907   /* Otherwise it must be a class-key.  */
9908   else
9909     {
9910       tag_type = cp_parser_class_key (parser);
9911       if (tag_type == none_type)
9912         return error_mark_node;
9913       /* Parse the attributes.  */
9914       attributes = cp_parser_attributes_opt (parser);
9915     }
9916
9917   /* Look for the `::' operator.  */
9918   cp_parser_global_scope_opt (parser,
9919                               /*current_scope_valid_p=*/false);
9920   /* Look for the nested-name-specifier.  */
9921   if (tag_type == typename_type)
9922     {
9923       if (!cp_parser_nested_name_specifier (parser,
9924                                            /*typename_keyword_p=*/true,
9925                                            /*check_dependency_p=*/true,
9926                                            /*type_p=*/true,
9927                                             is_declaration))
9928         return error_mark_node;
9929     }
9930   else
9931     /* Even though `typename' is not present, the proposed resolution
9932        to Core Issue 180 says that in `class A<T>::B', `B' should be
9933        considered a type-name, even if `A<T>' is dependent.  */
9934     cp_parser_nested_name_specifier_opt (parser,
9935                                          /*typename_keyword_p=*/true,
9936                                          /*check_dependency_p=*/true,
9937                                          /*type_p=*/true,
9938                                          is_declaration);
9939   /* For everything but enumeration types, consider a template-id.  */
9940   if (tag_type != enum_type)
9941     {
9942       bool template_p = false;
9943       tree decl;
9944
9945       /* Allow the `template' keyword.  */
9946       template_p = cp_parser_optional_template_keyword (parser);
9947       /* If we didn't see `template', we don't know if there's a
9948          template-id or not.  */
9949       if (!template_p)
9950         cp_parser_parse_tentatively (parser);
9951       /* Parse the template-id.  */
9952       decl = cp_parser_template_id (parser, template_p,
9953                                     /*check_dependency_p=*/true,
9954                                     is_declaration);
9955       /* If we didn't find a template-id, look for an ordinary
9956          identifier.  */
9957       if (!template_p && !cp_parser_parse_definitely (parser))
9958         ;
9959       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9960          in effect, then we must assume that, upon instantiation, the
9961          template will correspond to a class.  */
9962       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9963                && tag_type == typename_type)
9964         type = make_typename_type (parser->scope, decl,
9965                                    typename_type,
9966                                    /*complain=*/tf_error);
9967       else
9968         type = TREE_TYPE (decl);
9969     }
9970
9971   /* For an enumeration type, consider only a plain identifier.  */
9972   if (!type)
9973     {
9974       identifier = cp_parser_identifier (parser);
9975
9976       if (identifier == error_mark_node)
9977         {
9978           parser->scope = NULL_TREE;
9979           return error_mark_node;
9980         }
9981
9982       /* For a `typename', we needn't call xref_tag.  */
9983       if (tag_type == typename_type
9984           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9985         return cp_parser_make_typename_type (parser, parser->scope,
9986                                              identifier);
9987       /* Look up a qualified name in the usual way.  */
9988       if (parser->scope)
9989         {
9990           tree decl;
9991
9992           decl = cp_parser_lookup_name (parser, identifier,
9993                                         tag_type,
9994                                         /*is_template=*/false,
9995                                         /*is_namespace=*/false,
9996                                         /*check_dependency=*/true,
9997                                         /*ambiguous_decls=*/NULL);
9998
9999           /* If we are parsing friend declaration, DECL may be a
10000              TEMPLATE_DECL tree node here.  However, we need to check
10001              whether this TEMPLATE_DECL results in valid code.  Consider
10002              the following example:
10003
10004                namespace N {
10005                  template <class T> class C {};
10006                }
10007                class X {
10008                  template <class T> friend class N::C; // #1, valid code
10009                };
10010                template <class T> class Y {
10011                  friend class N::C;                    // #2, invalid code
10012                };
10013
10014              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10015              name lookup of `N::C'.  We see that friend declaration must
10016              be template for the code to be valid.  Note that
10017              processing_template_decl does not work here since it is
10018              always 1 for the above two cases.  */
10019
10020           decl = (cp_parser_maybe_treat_template_as_class
10021                   (decl, /*tag_name_p=*/is_friend
10022                          && parser->num_template_parameter_lists));
10023
10024           if (TREE_CODE (decl) != TYPE_DECL)
10025             {
10026               cp_parser_diagnose_invalid_type_name (parser,
10027                                                     parser->scope,
10028                                                     identifier);
10029               return error_mark_node;
10030             }
10031
10032           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10033             check_elaborated_type_specifier
10034               (tag_type, decl,
10035                (parser->num_template_parameter_lists
10036                 || DECL_SELF_REFERENCE_P (decl)));
10037
10038           type = TREE_TYPE (decl);
10039         }
10040       else
10041         {
10042           /* An elaborated-type-specifier sometimes introduces a new type and
10043              sometimes names an existing type.  Normally, the rule is that it
10044              introduces a new type only if there is not an existing type of
10045              the same name already in scope.  For example, given:
10046
10047                struct S {};
10048                void f() { struct S s; }
10049
10050              the `struct S' in the body of `f' is the same `struct S' as in
10051              the global scope; the existing definition is used.  However, if
10052              there were no global declaration, this would introduce a new
10053              local class named `S'.
10054
10055              An exception to this rule applies to the following code:
10056
10057                namespace N { struct S; }
10058
10059              Here, the elaborated-type-specifier names a new type
10060              unconditionally; even if there is already an `S' in the
10061              containing scope this declaration names a new type.
10062              This exception only applies if the elaborated-type-specifier
10063              forms the complete declaration:
10064
10065                [class.name]
10066
10067                A declaration consisting solely of `class-key identifier ;' is
10068                either a redeclaration of the name in the current scope or a
10069                forward declaration of the identifier as a class name.  It
10070                introduces the name into the current scope.
10071
10072              We are in this situation precisely when the next token is a `;'.
10073
10074              An exception to the exception is that a `friend' declaration does
10075              *not* name a new type; i.e., given:
10076
10077                struct S { friend struct T; };
10078
10079              `T' is not a new type in the scope of `S'.
10080
10081              Also, `new struct S' or `sizeof (struct S)' never results in the
10082              definition of a new type; a new type can only be declared in a
10083              declaration context.  */
10084
10085           tag_scope ts;
10086           bool template_p;
10087
10088           if (is_friend)
10089             /* Friends have special name lookup rules.  */
10090             ts = ts_within_enclosing_non_class;
10091           else if (is_declaration
10092                    && cp_lexer_next_token_is (parser->lexer,
10093                                               CPP_SEMICOLON))
10094             /* This is a `class-key identifier ;' */
10095             ts = ts_current;
10096           else
10097             ts = ts_global;
10098
10099           /* Warn about attributes. They are ignored.  */
10100           if (attributes)
10101             warning (OPT_Wattributes,
10102                      "type attributes are honored only at type definition");
10103
10104           template_p = 
10105             (parser->num_template_parameter_lists
10106              && (cp_parser_next_token_starts_class_definition_p (parser)
10107                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10108           type = xref_tag (tag_type, identifier, ts, template_p);
10109         }
10110     }
10111   if (tag_type != enum_type)
10112     cp_parser_check_class_key (tag_type, type);
10113
10114   /* A "<" cannot follow an elaborated type specifier.  If that
10115      happens, the user was probably trying to form a template-id.  */
10116   cp_parser_check_for_invalid_template_id (parser, type);
10117
10118   return type;
10119 }
10120
10121 /* Parse an enum-specifier.
10122
10123    enum-specifier:
10124      enum identifier [opt] { enumerator-list [opt] }
10125
10126    GNU Extensions:
10127      enum identifier [opt] { enumerator-list [opt] } attributes
10128
10129    Returns an ENUM_TYPE representing the enumeration.  */
10130
10131 static tree
10132 cp_parser_enum_specifier (cp_parser* parser)
10133 {
10134   tree identifier;
10135   tree type;
10136
10137   /* Caller guarantees that the current token is 'enum', an identifier
10138      possibly follows, and the token after that is an opening brace.
10139      If we don't have an identifier, fabricate an anonymous name for
10140      the enumeration being defined.  */
10141   cp_lexer_consume_token (parser->lexer);
10142
10143   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10144     identifier = cp_parser_identifier (parser);
10145   else
10146     identifier = make_anon_name ();
10147
10148   /* Issue an error message if type-definitions are forbidden here.  */
10149   cp_parser_check_type_definition (parser);
10150
10151   /* Create the new type.  We do this before consuming the opening brace
10152      so the enum will be recorded as being on the line of its tag (or the
10153      'enum' keyword, if there is no tag).  */
10154   type = start_enum (identifier);
10155
10156   /* Consume the opening brace.  */
10157   cp_lexer_consume_token (parser->lexer);
10158
10159   /* If the next token is not '}', then there are some enumerators.  */
10160   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10161     cp_parser_enumerator_list (parser, type);
10162
10163   /* Consume the final '}'.  */
10164   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10165
10166   /* Look for trailing attributes to apply to this enumeration, and
10167      apply them if appropriate.  */
10168   if (cp_parser_allow_gnu_extensions_p (parser))
10169     {
10170       tree trailing_attr = cp_parser_attributes_opt (parser);
10171       cplus_decl_attributes (&type,
10172                              trailing_attr,
10173                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10174     }
10175
10176   /* Finish up the enumeration.  */
10177   finish_enum (type);
10178
10179   return type;
10180 }
10181
10182 /* Parse an enumerator-list.  The enumerators all have the indicated
10183    TYPE.
10184
10185    enumerator-list:
10186      enumerator-definition
10187      enumerator-list , enumerator-definition  */
10188
10189 static void
10190 cp_parser_enumerator_list (cp_parser* parser, tree type)
10191 {
10192   while (true)
10193     {
10194       /* Parse an enumerator-definition.  */
10195       cp_parser_enumerator_definition (parser, type);
10196
10197       /* If the next token is not a ',', we've reached the end of
10198          the list.  */
10199       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10200         break;
10201       /* Otherwise, consume the `,' and keep going.  */
10202       cp_lexer_consume_token (parser->lexer);
10203       /* If the next token is a `}', there is a trailing comma.  */
10204       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10205         {
10206           if (pedantic && !in_system_header)
10207             pedwarn ("comma at end of enumerator list");
10208           break;
10209         }
10210     }
10211 }
10212
10213 /* Parse an enumerator-definition.  The enumerator has the indicated
10214    TYPE.
10215
10216    enumerator-definition:
10217      enumerator
10218      enumerator = constant-expression
10219
10220    enumerator:
10221      identifier  */
10222
10223 static void
10224 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10225 {
10226   tree identifier;
10227   tree value;
10228
10229   /* Look for the identifier.  */
10230   identifier = cp_parser_identifier (parser);
10231   if (identifier == error_mark_node)
10232     return;
10233
10234   /* If the next token is an '=', then there is an explicit value.  */
10235   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10236     {
10237       /* Consume the `=' token.  */
10238       cp_lexer_consume_token (parser->lexer);
10239       /* Parse the value.  */
10240       value = cp_parser_constant_expression (parser,
10241                                              /*allow_non_constant_p=*/false,
10242                                              NULL);
10243     }
10244   else
10245     value = NULL_TREE;
10246
10247   /* Create the enumerator.  */
10248   build_enumerator (identifier, value, type);
10249 }
10250
10251 /* Parse a namespace-name.
10252
10253    namespace-name:
10254      original-namespace-name
10255      namespace-alias
10256
10257    Returns the NAMESPACE_DECL for the namespace.  */
10258
10259 static tree
10260 cp_parser_namespace_name (cp_parser* parser)
10261 {
10262   tree identifier;
10263   tree namespace_decl;
10264
10265   /* Get the name of the namespace.  */
10266   identifier = cp_parser_identifier (parser);
10267   if (identifier == error_mark_node)
10268     return error_mark_node;
10269
10270   /* Look up the identifier in the currently active scope.  Look only
10271      for namespaces, due to:
10272
10273        [basic.lookup.udir]
10274
10275        When looking up a namespace-name in a using-directive or alias
10276        definition, only namespace names are considered.
10277
10278      And:
10279
10280        [basic.lookup.qual]
10281
10282        During the lookup of a name preceding the :: scope resolution
10283        operator, object, function, and enumerator names are ignored.
10284
10285      (Note that cp_parser_class_or_namespace_name only calls this
10286      function if the token after the name is the scope resolution
10287      operator.)  */
10288   namespace_decl = cp_parser_lookup_name (parser, identifier,
10289                                           none_type,
10290                                           /*is_template=*/false,
10291                                           /*is_namespace=*/true,
10292                                           /*check_dependency=*/true,
10293                                           /*ambiguous_decls=*/NULL);
10294   /* If it's not a namespace, issue an error.  */
10295   if (namespace_decl == error_mark_node
10296       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10297     {
10298       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10299         error ("%qD is not a namespace-name", identifier);
10300       cp_parser_error (parser, "expected namespace-name");
10301       namespace_decl = error_mark_node;
10302     }
10303
10304   return namespace_decl;
10305 }
10306
10307 /* Parse a namespace-definition.
10308
10309    namespace-definition:
10310      named-namespace-definition
10311      unnamed-namespace-definition
10312
10313    named-namespace-definition:
10314      original-namespace-definition
10315      extension-namespace-definition
10316
10317    original-namespace-definition:
10318      namespace identifier { namespace-body }
10319
10320    extension-namespace-definition:
10321      namespace original-namespace-name { namespace-body }
10322
10323    unnamed-namespace-definition:
10324      namespace { namespace-body } */
10325
10326 static void
10327 cp_parser_namespace_definition (cp_parser* parser)
10328 {
10329   tree identifier;
10330
10331   /* Look for the `namespace' keyword.  */
10332   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10333
10334   /* Get the name of the namespace.  We do not attempt to distinguish
10335      between an original-namespace-definition and an
10336      extension-namespace-definition at this point.  The semantic
10337      analysis routines are responsible for that.  */
10338   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10339     identifier = cp_parser_identifier (parser);
10340   else
10341     identifier = NULL_TREE;
10342
10343   /* Look for the `{' to start the namespace.  */
10344   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10345   /* Start the namespace.  */
10346   push_namespace (identifier);
10347   /* Parse the body of the namespace.  */
10348   cp_parser_namespace_body (parser);
10349   /* Finish the namespace.  */
10350   pop_namespace ();
10351   /* Look for the final `}'.  */
10352   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10353 }
10354
10355 /* Parse a namespace-body.
10356
10357    namespace-body:
10358      declaration-seq [opt]  */
10359
10360 static void
10361 cp_parser_namespace_body (cp_parser* parser)
10362 {
10363   cp_parser_declaration_seq_opt (parser);
10364 }
10365
10366 /* Parse a namespace-alias-definition.
10367
10368    namespace-alias-definition:
10369      namespace identifier = qualified-namespace-specifier ;  */
10370
10371 static void
10372 cp_parser_namespace_alias_definition (cp_parser* parser)
10373 {
10374   tree identifier;
10375   tree namespace_specifier;
10376
10377   /* Look for the `namespace' keyword.  */
10378   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10379   /* Look for the identifier.  */
10380   identifier = cp_parser_identifier (parser);
10381   if (identifier == error_mark_node)
10382     return;
10383   /* Look for the `=' token.  */
10384   cp_parser_require (parser, CPP_EQ, "`='");
10385   /* Look for the qualified-namespace-specifier.  */
10386   namespace_specifier
10387     = cp_parser_qualified_namespace_specifier (parser);
10388   /* Look for the `;' token.  */
10389   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10390
10391   /* Register the alias in the symbol table.  */
10392   do_namespace_alias (identifier, namespace_specifier);
10393 }
10394
10395 /* Parse a qualified-namespace-specifier.
10396
10397    qualified-namespace-specifier:
10398      :: [opt] nested-name-specifier [opt] namespace-name
10399
10400    Returns a NAMESPACE_DECL corresponding to the specified
10401    namespace.  */
10402
10403 static tree
10404 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10405 {
10406   /* Look for the optional `::'.  */
10407   cp_parser_global_scope_opt (parser,
10408                               /*current_scope_valid_p=*/false);
10409
10410   /* Look for the optional nested-name-specifier.  */
10411   cp_parser_nested_name_specifier_opt (parser,
10412                                        /*typename_keyword_p=*/false,
10413                                        /*check_dependency_p=*/true,
10414                                        /*type_p=*/false,
10415                                        /*is_declaration=*/true);
10416
10417   return cp_parser_namespace_name (parser);
10418 }
10419
10420 /* Parse a using-declaration.
10421
10422    using-declaration:
10423      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10424      using :: unqualified-id ;  */
10425
10426 static void
10427 cp_parser_using_declaration (cp_parser* parser)
10428 {
10429   cp_token *token;
10430   bool typename_p = false;
10431   bool global_scope_p;
10432   tree decl;
10433   tree identifier;
10434   tree qscope;
10435
10436   /* Look for the `using' keyword.  */
10437   cp_parser_require_keyword (parser, RID_USING, "`using'");
10438
10439   /* Peek at the next token.  */
10440   token = cp_lexer_peek_token (parser->lexer);
10441   /* See if it's `typename'.  */
10442   if (token->keyword == RID_TYPENAME)
10443     {
10444       /* Remember that we've seen it.  */
10445       typename_p = true;
10446       /* Consume the `typename' token.  */
10447       cp_lexer_consume_token (parser->lexer);
10448     }
10449
10450   /* Look for the optional global scope qualification.  */
10451   global_scope_p
10452     = (cp_parser_global_scope_opt (parser,
10453                                    /*current_scope_valid_p=*/false)
10454        != NULL_TREE);
10455
10456   /* If we saw `typename', or didn't see `::', then there must be a
10457      nested-name-specifier present.  */
10458   if (typename_p || !global_scope_p)
10459     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10460                                               /*check_dependency_p=*/true,
10461                                               /*type_p=*/false,
10462                                               /*is_declaration=*/true);
10463   /* Otherwise, we could be in either of the two productions.  In that
10464      case, treat the nested-name-specifier as optional.  */
10465   else
10466     qscope = cp_parser_nested_name_specifier_opt (parser,
10467                                                   /*typename_keyword_p=*/false,
10468                                                   /*check_dependency_p=*/true,
10469                                                   /*type_p=*/false,
10470                                                   /*is_declaration=*/true);
10471   if (!qscope)
10472     qscope = global_namespace;
10473
10474   /* Parse the unqualified-id.  */
10475   identifier = cp_parser_unqualified_id (parser,
10476                                          /*template_keyword_p=*/false,
10477                                          /*check_dependency_p=*/true,
10478                                          /*declarator_p=*/true);
10479
10480   /* The function we call to handle a using-declaration is different
10481      depending on what scope we are in.  */
10482   if (identifier == error_mark_node)
10483     ;
10484   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10485            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10486     /* [namespace.udecl]
10487
10488        A using declaration shall not name a template-id.  */
10489     error ("a template-id may not appear in a using-declaration");
10490   else
10491     {
10492       if (at_class_scope_p ())
10493         {
10494           /* Create the USING_DECL.  */
10495           decl = do_class_using_decl (parser->scope, identifier);
10496           /* Add it to the list of members in this class.  */
10497           finish_member_declaration (decl);
10498         }
10499       else
10500         {
10501           decl = cp_parser_lookup_name_simple (parser, identifier);
10502           if (decl == error_mark_node)
10503             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10504           else if (!at_namespace_scope_p ())
10505             do_local_using_decl (decl, qscope, identifier);
10506           else
10507             do_toplevel_using_decl (decl, qscope, identifier);
10508         }
10509     }
10510
10511   /* Look for the final `;'.  */
10512   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10513 }
10514
10515 /* Parse a using-directive.
10516
10517    using-directive:
10518      using namespace :: [opt] nested-name-specifier [opt]
10519        namespace-name ;  */
10520
10521 static void
10522 cp_parser_using_directive (cp_parser* parser)
10523 {
10524   tree namespace_decl;
10525   tree attribs;
10526
10527   /* Look for the `using' keyword.  */
10528   cp_parser_require_keyword (parser, RID_USING, "`using'");
10529   /* And the `namespace' keyword.  */
10530   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10531   /* Look for the optional `::' operator.  */
10532   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10533   /* And the optional nested-name-specifier.  */
10534   cp_parser_nested_name_specifier_opt (parser,
10535                                        /*typename_keyword_p=*/false,
10536                                        /*check_dependency_p=*/true,
10537                                        /*type_p=*/false,
10538                                        /*is_declaration=*/true);
10539   /* Get the namespace being used.  */
10540   namespace_decl = cp_parser_namespace_name (parser);
10541   /* And any specified attributes.  */
10542   attribs = cp_parser_attributes_opt (parser);
10543   /* Update the symbol table.  */
10544   parse_using_directive (namespace_decl, attribs);
10545   /* Look for the final `;'.  */
10546   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10547 }
10548
10549 /* Parse an asm-definition.
10550
10551    asm-definition:
10552      asm ( string-literal ) ;
10553
10554    GNU Extension:
10555
10556    asm-definition:
10557      asm volatile [opt] ( string-literal ) ;
10558      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10559      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10560                           : asm-operand-list [opt] ) ;
10561      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10562                           : asm-operand-list [opt]
10563                           : asm-operand-list [opt] ) ;  */
10564
10565 static void
10566 cp_parser_asm_definition (cp_parser* parser)
10567 {
10568   tree string;
10569   tree outputs = NULL_TREE;
10570   tree inputs = NULL_TREE;
10571   tree clobbers = NULL_TREE;
10572   tree asm_stmt;
10573   bool volatile_p = false;
10574   bool extended_p = false;
10575
10576   /* Look for the `asm' keyword.  */
10577   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10578   /* See if the next token is `volatile'.  */
10579   if (cp_parser_allow_gnu_extensions_p (parser)
10580       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10581     {
10582       /* Remember that we saw the `volatile' keyword.  */
10583       volatile_p = true;
10584       /* Consume the token.  */
10585       cp_lexer_consume_token (parser->lexer);
10586     }
10587   /* Look for the opening `('.  */
10588   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10589     return;
10590   /* Look for the string.  */
10591   string = cp_parser_string_literal (parser, false, false);
10592   if (string == error_mark_node)
10593     {
10594       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10595                                              /*consume_paren=*/true);
10596       return;
10597     }
10598
10599   /* If we're allowing GNU extensions, check for the extended assembly
10600      syntax.  Unfortunately, the `:' tokens need not be separated by
10601      a space in C, and so, for compatibility, we tolerate that here
10602      too.  Doing that means that we have to treat the `::' operator as
10603      two `:' tokens.  */
10604   if (cp_parser_allow_gnu_extensions_p (parser)
10605       && at_function_scope_p ()
10606       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10607           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10608     {
10609       bool inputs_p = false;
10610       bool clobbers_p = false;
10611
10612       /* The extended syntax was used.  */
10613       extended_p = true;
10614
10615       /* Look for outputs.  */
10616       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10617         {
10618           /* Consume the `:'.  */
10619           cp_lexer_consume_token (parser->lexer);
10620           /* Parse the output-operands.  */
10621           if (cp_lexer_next_token_is_not (parser->lexer,
10622                                           CPP_COLON)
10623               && cp_lexer_next_token_is_not (parser->lexer,
10624                                              CPP_SCOPE)
10625               && cp_lexer_next_token_is_not (parser->lexer,
10626                                              CPP_CLOSE_PAREN))
10627             outputs = cp_parser_asm_operand_list (parser);
10628         }
10629       /* If the next token is `::', there are no outputs, and the
10630          next token is the beginning of the inputs.  */
10631       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10632         /* The inputs are coming next.  */
10633         inputs_p = true;
10634
10635       /* Look for inputs.  */
10636       if (inputs_p
10637           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10638         {
10639           /* Consume the `:' or `::'.  */
10640           cp_lexer_consume_token (parser->lexer);
10641           /* Parse the output-operands.  */
10642           if (cp_lexer_next_token_is_not (parser->lexer,
10643                                           CPP_COLON)
10644               && cp_lexer_next_token_is_not (parser->lexer,
10645                                              CPP_CLOSE_PAREN))
10646             inputs = cp_parser_asm_operand_list (parser);
10647         }
10648       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10649         /* The clobbers are coming next.  */
10650         clobbers_p = true;
10651
10652       /* Look for clobbers.  */
10653       if (clobbers_p
10654           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10655         {
10656           /* Consume the `:' or `::'.  */
10657           cp_lexer_consume_token (parser->lexer);
10658           /* Parse the clobbers.  */
10659           if (cp_lexer_next_token_is_not (parser->lexer,
10660                                           CPP_CLOSE_PAREN))
10661             clobbers = cp_parser_asm_clobber_list (parser);
10662         }
10663     }
10664   /* Look for the closing `)'.  */
10665   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10666     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10667                                            /*consume_paren=*/true);
10668   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10669
10670   /* Create the ASM_EXPR.  */
10671   if (at_function_scope_p ())
10672     {
10673       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10674                                   inputs, clobbers);
10675       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10676       if (!extended_p)
10677         {
10678           tree temp = asm_stmt;
10679           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10680             temp = TREE_OPERAND (temp, 0);
10681
10682           ASM_INPUT_P (temp) = 1;
10683         }
10684     }
10685   else
10686     assemble_asm (string);
10687 }
10688
10689 /* Declarators [gram.dcl.decl] */
10690
10691 /* Parse an init-declarator.
10692
10693    init-declarator:
10694      declarator initializer [opt]
10695
10696    GNU Extension:
10697
10698    init-declarator:
10699      declarator asm-specification [opt] attributes [opt] initializer [opt]
10700
10701    function-definition:
10702      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10703        function-body
10704      decl-specifier-seq [opt] declarator function-try-block
10705
10706    GNU Extension:
10707
10708    function-definition:
10709      __extension__ function-definition
10710
10711    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10712    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10713    then this declarator appears in a class scope.  The new DECL created
10714    by this declarator is returned.
10715
10716    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10717    for a function-definition here as well.  If the declarator is a
10718    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10719    be TRUE upon return.  By that point, the function-definition will
10720    have been completely parsed.
10721
10722    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10723    is FALSE.  */
10724
10725 static tree
10726 cp_parser_init_declarator (cp_parser* parser,
10727                            cp_decl_specifier_seq *decl_specifiers,
10728                            bool function_definition_allowed_p,
10729                            bool member_p,
10730                            int declares_class_or_enum,
10731                            bool* function_definition_p)
10732 {
10733   cp_token *token;
10734   cp_declarator *declarator;
10735   tree prefix_attributes;
10736   tree attributes;
10737   tree asm_specification;
10738   tree initializer;
10739   tree decl = NULL_TREE;
10740   tree scope;
10741   bool is_initialized;
10742   bool is_parenthesized_init;
10743   bool is_non_constant_init;
10744   int ctor_dtor_or_conv_p;
10745   bool friend_p;
10746   tree pushed_scope = NULL;
10747
10748   /* Gather the attributes that were provided with the
10749      decl-specifiers.  */
10750   prefix_attributes = decl_specifiers->attributes;
10751
10752   /* Assume that this is not the declarator for a function
10753      definition.  */
10754   if (function_definition_p)
10755     *function_definition_p = false;
10756
10757   /* Defer access checks while parsing the declarator; we cannot know
10758      what names are accessible until we know what is being
10759      declared.  */
10760   resume_deferring_access_checks ();
10761
10762   /* Parse the declarator.  */
10763   declarator
10764     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10765                             &ctor_dtor_or_conv_p,
10766                             /*parenthesized_p=*/NULL,
10767                             /*member_p=*/false);
10768   /* Gather up the deferred checks.  */
10769   stop_deferring_access_checks ();
10770
10771   /* If the DECLARATOR was erroneous, there's no need to go
10772      further.  */
10773   if (declarator == cp_error_declarator)
10774     return error_mark_node;
10775
10776   if (declares_class_or_enum & 2)
10777     cp_parser_check_for_definition_in_return_type (declarator,
10778                                                    decl_specifiers->type);
10779
10780   /* Figure out what scope the entity declared by the DECLARATOR is
10781      located in.  `grokdeclarator' sometimes changes the scope, so
10782      we compute it now.  */
10783   scope = get_scope_of_declarator (declarator);
10784
10785   /* If we're allowing GNU extensions, look for an asm-specification
10786      and attributes.  */
10787   if (cp_parser_allow_gnu_extensions_p (parser))
10788     {
10789       /* Look for an asm-specification.  */
10790       asm_specification = cp_parser_asm_specification_opt (parser);
10791       /* And attributes.  */
10792       attributes = cp_parser_attributes_opt (parser);
10793     }
10794   else
10795     {
10796       asm_specification = NULL_TREE;
10797       attributes = NULL_TREE;
10798     }
10799
10800   /* Peek at the next token.  */
10801   token = cp_lexer_peek_token (parser->lexer);
10802   /* Check to see if the token indicates the start of a
10803      function-definition.  */
10804   if (cp_parser_token_starts_function_definition_p (token))
10805     {
10806       if (!function_definition_allowed_p)
10807         {
10808           /* If a function-definition should not appear here, issue an
10809              error message.  */
10810           cp_parser_error (parser,
10811                            "a function-definition is not allowed here");
10812           return error_mark_node;
10813         }
10814       else
10815         {
10816           /* Neither attributes nor an asm-specification are allowed
10817              on a function-definition.  */
10818           if (asm_specification)
10819             error ("an asm-specification is not allowed on a function-definition");
10820           if (attributes)
10821             error ("attributes are not allowed on a function-definition");
10822           /* This is a function-definition.  */
10823           *function_definition_p = true;
10824
10825           /* Parse the function definition.  */
10826           if (member_p)
10827             decl = cp_parser_save_member_function_body (parser,
10828                                                         decl_specifiers,
10829                                                         declarator,
10830                                                         prefix_attributes);
10831           else
10832             decl
10833               = (cp_parser_function_definition_from_specifiers_and_declarator
10834                  (parser, decl_specifiers, prefix_attributes, declarator));
10835
10836           return decl;
10837         }
10838     }
10839
10840   /* [dcl.dcl]
10841
10842      Only in function declarations for constructors, destructors, and
10843      type conversions can the decl-specifier-seq be omitted.
10844
10845      We explicitly postpone this check past the point where we handle
10846      function-definitions because we tolerate function-definitions
10847      that are missing their return types in some modes.  */
10848   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10849     {
10850       cp_parser_error (parser,
10851                        "expected constructor, destructor, or type conversion");
10852       return error_mark_node;
10853     }
10854
10855   /* An `=' or an `(' indicates an initializer.  */
10856   is_initialized = (token->type == CPP_EQ
10857                      || token->type == CPP_OPEN_PAREN);
10858   /* If the init-declarator isn't initialized and isn't followed by a
10859      `,' or `;', it's not a valid init-declarator.  */
10860   if (!is_initialized
10861       && token->type != CPP_COMMA
10862       && token->type != CPP_SEMICOLON)
10863     {
10864       cp_parser_error (parser, "expected initializer");
10865       return error_mark_node;
10866     }
10867
10868   /* Because start_decl has side-effects, we should only call it if we
10869      know we're going ahead.  By this point, we know that we cannot
10870      possibly be looking at any other construct.  */
10871   cp_parser_commit_to_tentative_parse (parser);
10872
10873   /* If the decl specifiers were bad, issue an error now that we're
10874      sure this was intended to be a declarator.  Then continue
10875      declaring the variable(s), as int, to try to cut down on further
10876      errors.  */
10877   if (decl_specifiers->any_specifiers_p
10878       && decl_specifiers->type == error_mark_node)
10879     {
10880       cp_parser_error (parser, "invalid type in declaration");
10881       decl_specifiers->type = integer_type_node;
10882     }
10883
10884   /* Check to see whether or not this declaration is a friend.  */
10885   friend_p = cp_parser_friend_p (decl_specifiers);
10886
10887   /* Check that the number of template-parameter-lists is OK.  */
10888   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10889     return error_mark_node;
10890
10891   /* Enter the newly declared entry in the symbol table.  If we're
10892      processing a declaration in a class-specifier, we wait until
10893      after processing the initializer.  */
10894   if (!member_p)
10895     {
10896       if (parser->in_unbraced_linkage_specification_p)
10897         {
10898           decl_specifiers->storage_class = sc_extern;
10899           have_extern_spec = false;
10900         }
10901       decl = start_decl (declarator, decl_specifiers,
10902                          is_initialized, attributes, prefix_attributes,
10903                          &pushed_scope);
10904     }
10905   else if (scope)
10906     /* Enter the SCOPE.  That way unqualified names appearing in the
10907        initializer will be looked up in SCOPE.  */
10908     pushed_scope = push_scope (scope);
10909
10910   /* Perform deferred access control checks, now that we know in which
10911      SCOPE the declared entity resides.  */
10912   if (!member_p && decl)
10913     {
10914       tree saved_current_function_decl = NULL_TREE;
10915
10916       /* If the entity being declared is a function, pretend that we
10917          are in its scope.  If it is a `friend', it may have access to
10918          things that would not otherwise be accessible.  */
10919       if (TREE_CODE (decl) == FUNCTION_DECL)
10920         {
10921           saved_current_function_decl = current_function_decl;
10922           current_function_decl = decl;
10923         }
10924
10925       /* Perform the access control checks for the declarator and the
10926          the decl-specifiers.  */
10927       perform_deferred_access_checks ();
10928
10929       /* Restore the saved value.  */
10930       if (TREE_CODE (decl) == FUNCTION_DECL)
10931         current_function_decl = saved_current_function_decl;
10932     }
10933
10934   /* Parse the initializer.  */
10935   if (is_initialized)
10936     initializer = cp_parser_initializer (parser,
10937                                          &is_parenthesized_init,
10938                                          &is_non_constant_init);
10939   else
10940     {
10941       initializer = NULL_TREE;
10942       is_parenthesized_init = false;
10943       is_non_constant_init = true;
10944     }
10945
10946   /* The old parser allows attributes to appear after a parenthesized
10947      initializer.  Mark Mitchell proposed removing this functionality
10948      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10949      attributes -- but ignores them.  */
10950   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10951     if (cp_parser_attributes_opt (parser))
10952       warning (OPT_Wattributes,
10953                "attributes after parenthesized initializer ignored");
10954
10955   /* For an in-class declaration, use `grokfield' to create the
10956      declaration.  */
10957   if (member_p)
10958     {
10959       if (pushed_scope)
10960         {
10961           pop_scope (pushed_scope);
10962           pushed_scope = false;
10963         }
10964       decl = grokfield (declarator, decl_specifiers,
10965                         initializer, /*asmspec=*/NULL_TREE,
10966                         prefix_attributes);
10967       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10968         cp_parser_save_default_args (parser, decl);
10969     }
10970
10971   /* Finish processing the declaration.  But, skip friend
10972      declarations.  */
10973   if (!friend_p && decl && decl != error_mark_node)
10974     {
10975       cp_finish_decl (decl,
10976                       initializer,
10977                       asm_specification,
10978                       /* If the initializer is in parentheses, then this is
10979                          a direct-initialization, which means that an
10980                          `explicit' constructor is OK.  Otherwise, an
10981                          `explicit' constructor cannot be used.  */
10982                       ((is_parenthesized_init || !is_initialized)
10983                      ? 0 : LOOKUP_ONLYCONVERTING));
10984     }
10985   if (!friend_p && pushed_scope)
10986     pop_scope (pushed_scope);
10987
10988   /* Remember whether or not variables were initialized by
10989      constant-expressions.  */
10990   if (decl && TREE_CODE (decl) == VAR_DECL
10991       && is_initialized && !is_non_constant_init)
10992     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10993
10994   return decl;
10995 }
10996
10997 /* Parse a declarator.
10998
10999    declarator:
11000      direct-declarator
11001      ptr-operator declarator
11002
11003    abstract-declarator:
11004      ptr-operator abstract-declarator [opt]
11005      direct-abstract-declarator
11006
11007    GNU Extensions:
11008
11009    declarator:
11010      attributes [opt] direct-declarator
11011      attributes [opt] ptr-operator declarator
11012
11013    abstract-declarator:
11014      attributes [opt] ptr-operator abstract-declarator [opt]
11015      attributes [opt] direct-abstract-declarator
11016
11017    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11018    detect constructor, destructor or conversion operators. It is set
11019    to -1 if the declarator is a name, and +1 if it is a
11020    function. Otherwise it is set to zero. Usually you just want to
11021    test for >0, but internally the negative value is used.
11022
11023    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11024    a decl-specifier-seq unless it declares a constructor, destructor,
11025    or conversion.  It might seem that we could check this condition in
11026    semantic analysis, rather than parsing, but that makes it difficult
11027    to handle something like `f()'.  We want to notice that there are
11028    no decl-specifiers, and therefore realize that this is an
11029    expression, not a declaration.)
11030
11031    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11032    the declarator is a direct-declarator of the form "(...)".
11033
11034    MEMBER_P is true iff this declarator is a member-declarator.  */
11035
11036 static cp_declarator *
11037 cp_parser_declarator (cp_parser* parser,
11038                       cp_parser_declarator_kind dcl_kind,
11039                       int* ctor_dtor_or_conv_p,
11040                       bool* parenthesized_p,
11041                       bool member_p)
11042 {
11043   cp_token *token;
11044   cp_declarator *declarator;
11045   enum tree_code code;
11046   cp_cv_quals cv_quals;
11047   tree class_type;
11048   tree attributes = NULL_TREE;
11049
11050   /* Assume this is not a constructor, destructor, or type-conversion
11051      operator.  */
11052   if (ctor_dtor_or_conv_p)
11053     *ctor_dtor_or_conv_p = 0;
11054
11055   if (cp_parser_allow_gnu_extensions_p (parser))
11056     attributes = cp_parser_attributes_opt (parser);
11057
11058   /* Peek at the next token.  */
11059   token = cp_lexer_peek_token (parser->lexer);
11060
11061   /* Check for the ptr-operator production.  */
11062   cp_parser_parse_tentatively (parser);
11063   /* Parse the ptr-operator.  */
11064   code = cp_parser_ptr_operator (parser,
11065                                  &class_type,
11066                                  &cv_quals);
11067   /* If that worked, then we have a ptr-operator.  */
11068   if (cp_parser_parse_definitely (parser))
11069     {
11070       /* If a ptr-operator was found, then this declarator was not
11071          parenthesized.  */
11072       if (parenthesized_p)
11073         *parenthesized_p = true;
11074       /* The dependent declarator is optional if we are parsing an
11075          abstract-declarator.  */
11076       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11077         cp_parser_parse_tentatively (parser);
11078
11079       /* Parse the dependent declarator.  */
11080       declarator = cp_parser_declarator (parser, dcl_kind,
11081                                          /*ctor_dtor_or_conv_p=*/NULL,
11082                                          /*parenthesized_p=*/NULL,
11083                                          /*member_p=*/false);
11084
11085       /* If we are parsing an abstract-declarator, we must handle the
11086          case where the dependent declarator is absent.  */
11087       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11088           && !cp_parser_parse_definitely (parser))
11089         declarator = NULL;
11090
11091       /* Build the representation of the ptr-operator.  */
11092       if (class_type)
11093         declarator = make_ptrmem_declarator (cv_quals,
11094                                              class_type,
11095                                              declarator);
11096       else if (code == INDIRECT_REF)
11097         declarator = make_pointer_declarator (cv_quals, declarator);
11098       else
11099         declarator = make_reference_declarator (cv_quals, declarator);
11100     }
11101   /* Everything else is a direct-declarator.  */
11102   else
11103     {
11104       if (parenthesized_p)
11105         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11106                                                    CPP_OPEN_PAREN);
11107       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11108                                                 ctor_dtor_or_conv_p,
11109                                                 member_p);
11110     }
11111
11112   if (attributes && declarator != cp_error_declarator)
11113     declarator->attributes = attributes;
11114
11115   return declarator;
11116 }
11117
11118 /* Parse a direct-declarator or direct-abstract-declarator.
11119
11120    direct-declarator:
11121      declarator-id
11122      direct-declarator ( parameter-declaration-clause )
11123        cv-qualifier-seq [opt]
11124        exception-specification [opt]
11125      direct-declarator [ constant-expression [opt] ]
11126      ( declarator )
11127
11128    direct-abstract-declarator:
11129      direct-abstract-declarator [opt]
11130        ( parameter-declaration-clause )
11131        cv-qualifier-seq [opt]
11132        exception-specification [opt]
11133      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11134      ( abstract-declarator )
11135
11136    Returns a representation of the declarator.  DCL_KIND is
11137    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11138    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11139    we are parsing a direct-declarator.  It is
11140    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11141    of ambiguity we prefer an abstract declarator, as per
11142    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11143    cp_parser_declarator.  */
11144
11145 static cp_declarator *
11146 cp_parser_direct_declarator (cp_parser* parser,
11147                              cp_parser_declarator_kind dcl_kind,
11148                              int* ctor_dtor_or_conv_p,
11149                              bool member_p)
11150 {
11151   cp_token *token;
11152   cp_declarator *declarator = NULL;
11153   tree scope = NULL_TREE;
11154   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11155   bool saved_in_declarator_p = parser->in_declarator_p;
11156   bool first = true;
11157   tree pushed_scope = NULL_TREE;
11158
11159   while (true)
11160     {
11161       /* Peek at the next token.  */
11162       token = cp_lexer_peek_token (parser->lexer);
11163       if (token->type == CPP_OPEN_PAREN)
11164         {
11165           /* This is either a parameter-declaration-clause, or a
11166              parenthesized declarator. When we know we are parsing a
11167              named declarator, it must be a parenthesized declarator
11168              if FIRST is true. For instance, `(int)' is a
11169              parameter-declaration-clause, with an omitted
11170              direct-abstract-declarator. But `((*))', is a
11171              parenthesized abstract declarator. Finally, when T is a
11172              template parameter `(T)' is a
11173              parameter-declaration-clause, and not a parenthesized
11174              named declarator.
11175
11176              We first try and parse a parameter-declaration-clause,
11177              and then try a nested declarator (if FIRST is true).
11178
11179              It is not an error for it not to be a
11180              parameter-declaration-clause, even when FIRST is
11181              false. Consider,
11182
11183                int i (int);
11184                int i (3);
11185
11186              The first is the declaration of a function while the
11187              second is a the definition of a variable, including its
11188              initializer.
11189
11190              Having seen only the parenthesis, we cannot know which of
11191              these two alternatives should be selected.  Even more
11192              complex are examples like:
11193
11194                int i (int (a));
11195                int i (int (3));
11196
11197              The former is a function-declaration; the latter is a
11198              variable initialization.
11199
11200              Thus again, we try a parameter-declaration-clause, and if
11201              that fails, we back out and return.  */
11202
11203           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11204             {
11205               cp_parameter_declarator *params;
11206               unsigned saved_num_template_parameter_lists;
11207
11208               /* In a member-declarator, the only valid interpretation
11209                  of a parenthesis is the start of a
11210                  parameter-declaration-clause.  (It is invalid to
11211                  initialize a static data member with a parenthesized
11212                  initializer; only the "=" form of initialization is
11213                  permitted.)  */
11214               if (!member_p)
11215                 cp_parser_parse_tentatively (parser);
11216
11217               /* Consume the `('.  */
11218               cp_lexer_consume_token (parser->lexer);
11219               if (first)
11220                 {
11221                   /* If this is going to be an abstract declarator, we're
11222                      in a declarator and we can't have default args.  */
11223                   parser->default_arg_ok_p = false;
11224                   parser->in_declarator_p = true;
11225                 }
11226
11227               /* Inside the function parameter list, surrounding
11228                  template-parameter-lists do not apply.  */
11229               saved_num_template_parameter_lists
11230                 = parser->num_template_parameter_lists;
11231               parser->num_template_parameter_lists = 0;
11232
11233               /* Parse the parameter-declaration-clause.  */
11234               params = cp_parser_parameter_declaration_clause (parser);
11235
11236               parser->num_template_parameter_lists
11237                 = saved_num_template_parameter_lists;
11238
11239               /* If all went well, parse the cv-qualifier-seq and the
11240                  exception-specification.  */
11241               if (member_p || cp_parser_parse_definitely (parser))
11242                 {
11243                   cp_cv_quals cv_quals;
11244                   tree exception_specification;
11245
11246                   if (ctor_dtor_or_conv_p)
11247                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11248                   first = false;
11249                   /* Consume the `)'.  */
11250                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11251
11252                   /* Parse the cv-qualifier-seq.  */
11253                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11254                   /* And the exception-specification.  */
11255                   exception_specification
11256                     = cp_parser_exception_specification_opt (parser);
11257
11258                   /* Create the function-declarator.  */
11259                   declarator = make_call_declarator (declarator,
11260                                                      params,
11261                                                      cv_quals,
11262                                                      exception_specification);
11263                   /* Any subsequent parameter lists are to do with
11264                      return type, so are not those of the declared
11265                      function.  */
11266                   parser->default_arg_ok_p = false;
11267
11268                   /* Repeat the main loop.  */
11269                   continue;
11270                 }
11271             }
11272
11273           /* If this is the first, we can try a parenthesized
11274              declarator.  */
11275           if (first)
11276             {
11277               bool saved_in_type_id_in_expr_p;
11278
11279               parser->default_arg_ok_p = saved_default_arg_ok_p;
11280               parser->in_declarator_p = saved_in_declarator_p;
11281
11282               /* Consume the `('.  */
11283               cp_lexer_consume_token (parser->lexer);
11284               /* Parse the nested declarator.  */
11285               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11286               parser->in_type_id_in_expr_p = true;
11287               declarator
11288                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11289                                         /*parenthesized_p=*/NULL,
11290                                         member_p);
11291               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11292               first = false;
11293               /* Expect a `)'.  */
11294               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11295                 declarator = cp_error_declarator;
11296               if (declarator == cp_error_declarator)
11297                 break;
11298
11299               goto handle_declarator;
11300             }
11301           /* Otherwise, we must be done.  */
11302           else
11303             break;
11304         }
11305       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11306                && token->type == CPP_OPEN_SQUARE)
11307         {
11308           /* Parse an array-declarator.  */
11309           tree bounds;
11310
11311           if (ctor_dtor_or_conv_p)
11312             *ctor_dtor_or_conv_p = 0;
11313
11314           first = false;
11315           parser->default_arg_ok_p = false;
11316           parser->in_declarator_p = true;
11317           /* Consume the `['.  */
11318           cp_lexer_consume_token (parser->lexer);
11319           /* Peek at the next token.  */
11320           token = cp_lexer_peek_token (parser->lexer);
11321           /* If the next token is `]', then there is no
11322              constant-expression.  */
11323           if (token->type != CPP_CLOSE_SQUARE)
11324             {
11325               bool non_constant_p;
11326
11327               bounds
11328                 = cp_parser_constant_expression (parser,
11329                                                  /*allow_non_constant=*/true,
11330                                                  &non_constant_p);
11331               if (!non_constant_p)
11332                 bounds = fold_non_dependent_expr (bounds);
11333               /* Normally, the array bound must be an integral constant
11334                  expression.  However, as an extension, we allow VLAs
11335                  in function scopes.  */
11336               else if (!at_function_scope_p ())
11337                 {
11338                   error ("array bound is not an integer constant");
11339                   bounds = error_mark_node;
11340                 }
11341             }
11342           else
11343             bounds = NULL_TREE;
11344           /* Look for the closing `]'.  */
11345           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11346             {
11347               declarator = cp_error_declarator;
11348               break;
11349             }
11350
11351           declarator = make_array_declarator (declarator, bounds);
11352         }
11353       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11354         {
11355           tree qualifying_scope;
11356           tree unqualified_name;
11357
11358           /* Parse a declarator-id */
11359           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11360             cp_parser_parse_tentatively (parser);
11361           unqualified_name = cp_parser_declarator_id (parser);
11362           qualifying_scope = parser->scope;
11363           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11364             {
11365               if (!cp_parser_parse_definitely (parser))
11366                 unqualified_name = error_mark_node;
11367               else if (qualifying_scope
11368                        || (TREE_CODE (unqualified_name)
11369                            != IDENTIFIER_NODE))
11370                 {
11371                   cp_parser_error (parser, "expected unqualified-id");
11372                   unqualified_name = error_mark_node;
11373                 }
11374             }
11375
11376           if (unqualified_name == error_mark_node)
11377             {
11378               declarator = cp_error_declarator;
11379               break;
11380             }
11381
11382           if (qualifying_scope && at_namespace_scope_p ()
11383               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11384             {
11385               /* In the declaration of a member of a template class
11386                  outside of the class itself, the SCOPE will sometimes
11387                  be a TYPENAME_TYPE.  For example, given:
11388
11389                  template <typename T>
11390                  int S<T>::R::i = 3;
11391
11392                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11393                  this context, we must resolve S<T>::R to an ordinary
11394                  type, rather than a typename type.
11395
11396                  The reason we normally avoid resolving TYPENAME_TYPEs
11397                  is that a specialization of `S' might render
11398                  `S<T>::R' not a type.  However, if `S' is
11399                  specialized, then this `i' will not be used, so there
11400                  is no harm in resolving the types here.  */
11401               tree type;
11402
11403               /* Resolve the TYPENAME_TYPE.  */
11404               type = resolve_typename_type (qualifying_scope,
11405                                             /*only_current_p=*/false);
11406               /* If that failed, the declarator is invalid.  */
11407               if (type == error_mark_node)
11408                 error ("%<%T::%D%> is not a type",
11409                        TYPE_CONTEXT (qualifying_scope),
11410                        TYPE_IDENTIFIER (qualifying_scope));
11411               qualifying_scope = type;
11412             }
11413
11414           declarator = make_id_declarator (qualifying_scope,
11415                                            unqualified_name);
11416           declarator->id_loc = token->location;
11417           if (unqualified_name)
11418             {
11419               tree class_type;
11420
11421               if (qualifying_scope
11422                   && CLASS_TYPE_P (qualifying_scope))
11423                 class_type = qualifying_scope;
11424               else
11425                 class_type = current_class_type;
11426
11427               if (class_type)
11428                 {
11429                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11430                     declarator->u.id.sfk = sfk_destructor;
11431                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11432                     declarator->u.id.sfk = sfk_conversion;
11433                   else if (/* There's no way to declare a constructor
11434                               for an anonymous type, even if the type
11435                               got a name for linkage purposes.  */
11436                            !TYPE_WAS_ANONYMOUS (class_type)
11437                            && (constructor_name_p (unqualified_name,
11438                                                    class_type)
11439                                || (TREE_CODE (unqualified_name) == TYPE_DECL
11440                                    && (same_type_p
11441                                        (TREE_TYPE (unqualified_name),
11442                                         class_type)))))
11443                     declarator->u.id.sfk = sfk_constructor;
11444
11445                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11446                     *ctor_dtor_or_conv_p = -1;
11447                   if (qualifying_scope
11448                       && TREE_CODE (unqualified_name) == TYPE_DECL
11449                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11450                     {
11451                       error ("invalid use of constructor as a template");
11452                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11453                               "the constructor in a qualified name",
11454                               class_type,
11455                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11456                               class_type, class_type);
11457                     }
11458                 }
11459             }
11460
11461         handle_declarator:;
11462           scope = get_scope_of_declarator (declarator);
11463           if (scope)
11464             /* Any names that appear after the declarator-id for a
11465                member are looked up in the containing scope.  */
11466             pushed_scope = push_scope (scope);
11467           parser->in_declarator_p = true;
11468           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11469               || (declarator && declarator->kind == cdk_id))
11470             /* Default args are only allowed on function
11471                declarations.  */
11472             parser->default_arg_ok_p = saved_default_arg_ok_p;
11473           else
11474             parser->default_arg_ok_p = false;
11475
11476           first = false;
11477         }
11478       /* We're done.  */
11479       else
11480         break;
11481     }
11482
11483   /* For an abstract declarator, we might wind up with nothing at this
11484      point.  That's an error; the declarator is not optional.  */
11485   if (!declarator)
11486     cp_parser_error (parser, "expected declarator");
11487
11488   /* If we entered a scope, we must exit it now.  */
11489   if (pushed_scope)
11490     pop_scope (pushed_scope);
11491
11492   parser->default_arg_ok_p = saved_default_arg_ok_p;
11493   parser->in_declarator_p = saved_in_declarator_p;
11494
11495   return declarator;
11496 }
11497
11498 /* Parse a ptr-operator.
11499
11500    ptr-operator:
11501      * cv-qualifier-seq [opt]
11502      &
11503      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11504
11505    GNU Extension:
11506
11507    ptr-operator:
11508      & cv-qualifier-seq [opt]
11509
11510    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11511    Returns ADDR_EXPR if a reference was used.  In the case of a
11512    pointer-to-member, *TYPE is filled in with the TYPE containing the
11513    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11514    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11515    ERROR_MARK if an error occurred.  */
11516
11517 static enum tree_code
11518 cp_parser_ptr_operator (cp_parser* parser,
11519                         tree* type,
11520                         cp_cv_quals *cv_quals)
11521 {
11522   enum tree_code code = ERROR_MARK;
11523   cp_token *token;
11524
11525   /* Assume that it's not a pointer-to-member.  */
11526   *type = NULL_TREE;
11527   /* And that there are no cv-qualifiers.  */
11528   *cv_quals = TYPE_UNQUALIFIED;
11529
11530   /* Peek at the next token.  */
11531   token = cp_lexer_peek_token (parser->lexer);
11532   /* If it's a `*' or `&' we have a pointer or reference.  */
11533   if (token->type == CPP_MULT || token->type == CPP_AND)
11534     {
11535       /* Remember which ptr-operator we were processing.  */
11536       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11537
11538       /* Consume the `*' or `&'.  */
11539       cp_lexer_consume_token (parser->lexer);
11540
11541       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11542          `&', if we are allowing GNU extensions.  (The only qualifier
11543          that can legally appear after `&' is `restrict', but that is
11544          enforced during semantic analysis.  */
11545       if (code == INDIRECT_REF
11546           || cp_parser_allow_gnu_extensions_p (parser))
11547         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11548     }
11549   else
11550     {
11551       /* Try the pointer-to-member case.  */
11552       cp_parser_parse_tentatively (parser);
11553       /* Look for the optional `::' operator.  */
11554       cp_parser_global_scope_opt (parser,
11555                                   /*current_scope_valid_p=*/false);
11556       /* Look for the nested-name specifier.  */
11557       cp_parser_nested_name_specifier (parser,
11558                                        /*typename_keyword_p=*/false,
11559                                        /*check_dependency_p=*/true,
11560                                        /*type_p=*/false,
11561                                        /*is_declaration=*/false);
11562       /* If we found it, and the next token is a `*', then we are
11563          indeed looking at a pointer-to-member operator.  */
11564       if (!cp_parser_error_occurred (parser)
11565           && cp_parser_require (parser, CPP_MULT, "`*'"))
11566         {
11567           /* The type of which the member is a member is given by the
11568              current SCOPE.  */
11569           *type = parser->scope;
11570           /* The next name will not be qualified.  */
11571           parser->scope = NULL_TREE;
11572           parser->qualifying_scope = NULL_TREE;
11573           parser->object_scope = NULL_TREE;
11574           /* Indicate that the `*' operator was used.  */
11575           code = INDIRECT_REF;
11576           /* Look for the optional cv-qualifier-seq.  */
11577           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11578         }
11579       /* If that didn't work we don't have a ptr-operator.  */
11580       if (!cp_parser_parse_definitely (parser))
11581         cp_parser_error (parser, "expected ptr-operator");
11582     }
11583
11584   return code;
11585 }
11586
11587 /* Parse an (optional) cv-qualifier-seq.
11588
11589    cv-qualifier-seq:
11590      cv-qualifier cv-qualifier-seq [opt]
11591
11592    cv-qualifier:
11593      const
11594      volatile
11595
11596    GNU Extension:
11597
11598    cv-qualifier:
11599      __restrict__
11600
11601    Returns a bitmask representing the cv-qualifiers.  */
11602
11603 static cp_cv_quals
11604 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11605 {
11606   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11607
11608   while (true)
11609     {
11610       cp_token *token;
11611       cp_cv_quals cv_qualifier;
11612
11613       /* Peek at the next token.  */
11614       token = cp_lexer_peek_token (parser->lexer);
11615       /* See if it's a cv-qualifier.  */
11616       switch (token->keyword)
11617         {
11618         case RID_CONST:
11619           cv_qualifier = TYPE_QUAL_CONST;
11620           break;
11621
11622         case RID_VOLATILE:
11623           cv_qualifier = TYPE_QUAL_VOLATILE;
11624           break;
11625
11626         case RID_RESTRICT:
11627           cv_qualifier = TYPE_QUAL_RESTRICT;
11628           break;
11629
11630         default:
11631           cv_qualifier = TYPE_UNQUALIFIED;
11632           break;
11633         }
11634
11635       if (!cv_qualifier)
11636         break;
11637
11638       if (cv_quals & cv_qualifier)
11639         {
11640           error ("duplicate cv-qualifier");
11641           cp_lexer_purge_token (parser->lexer);
11642         }
11643       else
11644         {
11645           cp_lexer_consume_token (parser->lexer);
11646           cv_quals |= cv_qualifier;
11647         }
11648     }
11649
11650   return cv_quals;
11651 }
11652
11653 /* Parse a declarator-id.
11654
11655    declarator-id:
11656      id-expression
11657      :: [opt] nested-name-specifier [opt] type-name
11658
11659    In the `id-expression' case, the value returned is as for
11660    cp_parser_id_expression if the id-expression was an unqualified-id.
11661    If the id-expression was a qualified-id, then a SCOPE_REF is
11662    returned.  The first operand is the scope (either a NAMESPACE_DECL
11663    or TREE_TYPE), but the second is still just a representation of an
11664    unqualified-id.  */
11665
11666 static tree
11667 cp_parser_declarator_id (cp_parser* parser)
11668 {
11669   /* The expression must be an id-expression.  Assume that qualified
11670      names are the names of types so that:
11671
11672        template <class T>
11673        int S<T>::R::i = 3;
11674
11675      will work; we must treat `S<T>::R' as the name of a type.
11676      Similarly, assume that qualified names are templates, where
11677      required, so that:
11678
11679        template <class T>
11680        int S<T>::R<T>::i = 3;
11681
11682      will work, too.  */
11683   return cp_parser_id_expression (parser,
11684                                   /*template_keyword_p=*/false,
11685                                   /*check_dependency_p=*/false,
11686                                   /*template_p=*/NULL,
11687                                   /*declarator_p=*/true);
11688 }
11689
11690 /* Parse a type-id.
11691
11692    type-id:
11693      type-specifier-seq abstract-declarator [opt]
11694
11695    Returns the TYPE specified.  */
11696
11697 static tree
11698 cp_parser_type_id (cp_parser* parser)
11699 {
11700   cp_decl_specifier_seq type_specifier_seq;
11701   cp_declarator *abstract_declarator;
11702
11703   /* Parse the type-specifier-seq.  */
11704   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11705                                 &type_specifier_seq);
11706   if (type_specifier_seq.type == error_mark_node)
11707     return error_mark_node;
11708
11709   /* There might or might not be an abstract declarator.  */
11710   cp_parser_parse_tentatively (parser);
11711   /* Look for the declarator.  */
11712   abstract_declarator
11713     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11714                             /*parenthesized_p=*/NULL,
11715                             /*member_p=*/false);
11716   /* Check to see if there really was a declarator.  */
11717   if (!cp_parser_parse_definitely (parser))
11718     abstract_declarator = NULL;
11719
11720   return groktypename (&type_specifier_seq, abstract_declarator);
11721 }
11722
11723 /* Parse a type-specifier-seq.
11724
11725    type-specifier-seq:
11726      type-specifier type-specifier-seq [opt]
11727
11728    GNU extension:
11729
11730    type-specifier-seq:
11731      attributes type-specifier-seq [opt]
11732
11733    If IS_CONDITION is true, we are at the start of a "condition",
11734    e.g., we've just seen "if (".
11735
11736    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11737
11738 static void
11739 cp_parser_type_specifier_seq (cp_parser* parser,
11740                               bool is_condition,
11741                               cp_decl_specifier_seq *type_specifier_seq)
11742 {
11743   bool seen_type_specifier = false;
11744   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11745
11746   /* Clear the TYPE_SPECIFIER_SEQ.  */
11747   clear_decl_specs (type_specifier_seq);
11748
11749   /* Parse the type-specifiers and attributes.  */
11750   while (true)
11751     {
11752       tree type_specifier;
11753       bool is_cv_qualifier;
11754
11755       /* Check for attributes first.  */
11756       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11757         {
11758           type_specifier_seq->attributes =
11759             chainon (type_specifier_seq->attributes,
11760                      cp_parser_attributes_opt (parser));
11761           continue;
11762         }
11763
11764       /* Look for the type-specifier.  */
11765       type_specifier = cp_parser_type_specifier (parser,
11766                                                  flags,
11767                                                  type_specifier_seq,
11768                                                  /*is_declaration=*/false,
11769                                                  NULL,
11770                                                  &is_cv_qualifier);
11771       if (!type_specifier)
11772         {
11773           /* If the first type-specifier could not be found, this is not a
11774              type-specifier-seq at all.  */
11775           if (!seen_type_specifier)
11776             {
11777               cp_parser_error (parser, "expected type-specifier");
11778               type_specifier_seq->type = error_mark_node;
11779               return;
11780             }
11781           /* If subsequent type-specifiers could not be found, the
11782              type-specifier-seq is complete.  */
11783           break;
11784         }
11785
11786       seen_type_specifier = true;
11787       /* The standard says that a condition can be:
11788
11789             type-specifier-seq declarator = assignment-expression
11790
11791          However, given:
11792
11793            struct S {};
11794            if (int S = ...)
11795
11796          we should treat the "S" as a declarator, not as a
11797          type-specifier.  The standard doesn't say that explicitly for
11798          type-specifier-seq, but it does say that for
11799          decl-specifier-seq in an ordinary declaration.  Perhaps it
11800          would be clearer just to allow a decl-specifier-seq here, and
11801          then add a semantic restriction that if any decl-specifiers
11802          that are not type-specifiers appear, the program is invalid.  */
11803       if (is_condition && !is_cv_qualifier)
11804         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11805     }
11806
11807   return;
11808 }
11809
11810 /* Parse a parameter-declaration-clause.
11811
11812    parameter-declaration-clause:
11813      parameter-declaration-list [opt] ... [opt]
11814      parameter-declaration-list , ...
11815
11816    Returns a representation for the parameter declarations.  A return
11817    value of NULL indicates a parameter-declaration-clause consisting
11818    only of an ellipsis.  */
11819
11820 static cp_parameter_declarator *
11821 cp_parser_parameter_declaration_clause (cp_parser* parser)
11822 {
11823   cp_parameter_declarator *parameters;
11824   cp_token *token;
11825   bool ellipsis_p;
11826   bool is_error;
11827
11828   /* Peek at the next token.  */
11829   token = cp_lexer_peek_token (parser->lexer);
11830   /* Check for trivial parameter-declaration-clauses.  */
11831   if (token->type == CPP_ELLIPSIS)
11832     {
11833       /* Consume the `...' token.  */
11834       cp_lexer_consume_token (parser->lexer);
11835       return NULL;
11836     }
11837   else if (token->type == CPP_CLOSE_PAREN)
11838     /* There are no parameters.  */
11839     {
11840 #ifndef NO_IMPLICIT_EXTERN_C
11841       if (in_system_header && current_class_type == NULL
11842           && current_lang_name == lang_name_c)
11843         return NULL;
11844       else
11845 #endif
11846         return no_parameters;
11847     }
11848   /* Check for `(void)', too, which is a special case.  */
11849   else if (token->keyword == RID_VOID
11850            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11851                == CPP_CLOSE_PAREN))
11852     {
11853       /* Consume the `void' token.  */
11854       cp_lexer_consume_token (parser->lexer);
11855       /* There are no parameters.  */
11856       return no_parameters;
11857     }
11858
11859   /* Parse the parameter-declaration-list.  */
11860   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11861   /* If a parse error occurred while parsing the
11862      parameter-declaration-list, then the entire
11863      parameter-declaration-clause is erroneous.  */
11864   if (is_error)
11865     return NULL;
11866
11867   /* Peek at the next token.  */
11868   token = cp_lexer_peek_token (parser->lexer);
11869   /* If it's a `,', the clause should terminate with an ellipsis.  */
11870   if (token->type == CPP_COMMA)
11871     {
11872       /* Consume the `,'.  */
11873       cp_lexer_consume_token (parser->lexer);
11874       /* Expect an ellipsis.  */
11875       ellipsis_p
11876         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11877     }
11878   /* It might also be `...' if the optional trailing `,' was
11879      omitted.  */
11880   else if (token->type == CPP_ELLIPSIS)
11881     {
11882       /* Consume the `...' token.  */
11883       cp_lexer_consume_token (parser->lexer);
11884       /* And remember that we saw it.  */
11885       ellipsis_p = true;
11886     }
11887   else
11888     ellipsis_p = false;
11889
11890   /* Finish the parameter list.  */
11891   if (parameters && ellipsis_p)
11892     parameters->ellipsis_p = true;
11893
11894   return parameters;
11895 }
11896
11897 /* Parse a parameter-declaration-list.
11898
11899    parameter-declaration-list:
11900      parameter-declaration
11901      parameter-declaration-list , parameter-declaration
11902
11903    Returns a representation of the parameter-declaration-list, as for
11904    cp_parser_parameter_declaration_clause.  However, the
11905    `void_list_node' is never appended to the list.  Upon return,
11906    *IS_ERROR will be true iff an error occurred.  */
11907
11908 static cp_parameter_declarator *
11909 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11910 {
11911   cp_parameter_declarator *parameters = NULL;
11912   cp_parameter_declarator **tail = &parameters;
11913
11914   /* Assume all will go well.  */
11915   *is_error = false;
11916
11917   /* Look for more parameters.  */
11918   while (true)
11919     {
11920       cp_parameter_declarator *parameter;
11921       bool parenthesized_p;
11922       /* Parse the parameter.  */
11923       parameter
11924         = cp_parser_parameter_declaration (parser,
11925                                            /*template_parm_p=*/false,
11926                                            &parenthesized_p);
11927
11928       /* If a parse error occurred parsing the parameter declaration,
11929          then the entire parameter-declaration-list is erroneous.  */
11930       if (!parameter)
11931         {
11932           *is_error = true;
11933           parameters = NULL;
11934           break;
11935         }
11936       /* Add the new parameter to the list.  */
11937       *tail = parameter;
11938       tail = &parameter->next;
11939
11940       /* Peek at the next token.  */
11941       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11942           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11943           /* These are for Objective-C++ */
11944           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11945           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11946         /* The parameter-declaration-list is complete.  */
11947         break;
11948       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11949         {
11950           cp_token *token;
11951
11952           /* Peek at the next token.  */
11953           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11954           /* If it's an ellipsis, then the list is complete.  */
11955           if (token->type == CPP_ELLIPSIS)
11956             break;
11957           /* Otherwise, there must be more parameters.  Consume the
11958              `,'.  */
11959           cp_lexer_consume_token (parser->lexer);
11960           /* When parsing something like:
11961
11962                 int i(float f, double d)
11963
11964              we can tell after seeing the declaration for "f" that we
11965              are not looking at an initialization of a variable "i",
11966              but rather at the declaration of a function "i".
11967
11968              Due to the fact that the parsing of template arguments
11969              (as specified to a template-id) requires backtracking we
11970              cannot use this technique when inside a template argument
11971              list.  */
11972           if (!parser->in_template_argument_list_p
11973               && !parser->in_type_id_in_expr_p
11974               && cp_parser_uncommitted_to_tentative_parse_p (parser)
11975               /* However, a parameter-declaration of the form
11976                  "foat(f)" (which is a valid declaration of a
11977                  parameter "f") can also be interpreted as an
11978                  expression (the conversion of "f" to "float").  */
11979               && !parenthesized_p)
11980             cp_parser_commit_to_tentative_parse (parser);
11981         }
11982       else
11983         {
11984           cp_parser_error (parser, "expected %<,%> or %<...%>");
11985           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11986             cp_parser_skip_to_closing_parenthesis (parser,
11987                                                    /*recovering=*/true,
11988                                                    /*or_comma=*/false,
11989                                                    /*consume_paren=*/false);
11990           break;
11991         }
11992     }
11993
11994   return parameters;
11995 }
11996
11997 /* Parse a parameter declaration.
11998
11999    parameter-declaration:
12000      decl-specifier-seq declarator
12001      decl-specifier-seq declarator = assignment-expression
12002      decl-specifier-seq abstract-declarator [opt]
12003      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12004
12005    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12006    declares a template parameter.  (In that case, a non-nested `>'
12007    token encountered during the parsing of the assignment-expression
12008    is not interpreted as a greater-than operator.)
12009
12010    Returns a representation of the parameter, or NULL if an error
12011    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12012    true iff the declarator is of the form "(p)".  */
12013
12014 static cp_parameter_declarator *
12015 cp_parser_parameter_declaration (cp_parser *parser,
12016                                  bool template_parm_p,
12017                                  bool *parenthesized_p)
12018 {
12019   int declares_class_or_enum;
12020   bool greater_than_is_operator_p;
12021   cp_decl_specifier_seq decl_specifiers;
12022   cp_declarator *declarator;
12023   tree default_argument;
12024   cp_token *token;
12025   const char *saved_message;
12026
12027   /* In a template parameter, `>' is not an operator.
12028
12029      [temp.param]
12030
12031      When parsing a default template-argument for a non-type
12032      template-parameter, the first non-nested `>' is taken as the end
12033      of the template parameter-list rather than a greater-than
12034      operator.  */
12035   greater_than_is_operator_p = !template_parm_p;
12036
12037   /* Type definitions may not appear in parameter types.  */
12038   saved_message = parser->type_definition_forbidden_message;
12039   parser->type_definition_forbidden_message
12040     = "types may not be defined in parameter types";
12041
12042   /* Parse the declaration-specifiers.  */
12043   cp_parser_decl_specifier_seq (parser,
12044                                 CP_PARSER_FLAGS_NONE,
12045                                 &decl_specifiers,
12046                                 &declares_class_or_enum);
12047   /* If an error occurred, there's no reason to attempt to parse the
12048      rest of the declaration.  */
12049   if (cp_parser_error_occurred (parser))
12050     {
12051       parser->type_definition_forbidden_message = saved_message;
12052       return NULL;
12053     }
12054
12055   /* Peek at the next token.  */
12056   token = cp_lexer_peek_token (parser->lexer);
12057   /* If the next token is a `)', `,', `=', `>', or `...', then there
12058      is no declarator.  */
12059   if (token->type == CPP_CLOSE_PAREN
12060       || token->type == CPP_COMMA
12061       || token->type == CPP_EQ
12062       || token->type == CPP_ELLIPSIS
12063       || token->type == CPP_GREATER)
12064     {
12065       declarator = NULL;
12066       if (parenthesized_p)
12067         *parenthesized_p = false;
12068     }
12069   /* Otherwise, there should be a declarator.  */
12070   else
12071     {
12072       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12073       parser->default_arg_ok_p = false;
12074
12075       /* After seeing a decl-specifier-seq, if the next token is not a
12076          "(", there is no possibility that the code is a valid
12077          expression.  Therefore, if parsing tentatively, we commit at
12078          this point.  */
12079       if (!parser->in_template_argument_list_p
12080           /* In an expression context, having seen:
12081
12082                (int((char ...
12083
12084              we cannot be sure whether we are looking at a
12085              function-type (taking a "char" as a parameter) or a cast
12086              of some object of type "char" to "int".  */
12087           && !parser->in_type_id_in_expr_p
12088           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12089           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12090         cp_parser_commit_to_tentative_parse (parser);
12091       /* Parse the declarator.  */
12092       declarator = cp_parser_declarator (parser,
12093                                          CP_PARSER_DECLARATOR_EITHER,
12094                                          /*ctor_dtor_or_conv_p=*/NULL,
12095                                          parenthesized_p,
12096                                          /*member_p=*/false);
12097       parser->default_arg_ok_p = saved_default_arg_ok_p;
12098       /* After the declarator, allow more attributes.  */
12099       decl_specifiers.attributes
12100         = chainon (decl_specifiers.attributes,
12101                    cp_parser_attributes_opt (parser));
12102     }
12103
12104   /* The restriction on defining new types applies only to the type
12105      of the parameter, not to the default argument.  */
12106   parser->type_definition_forbidden_message = saved_message;
12107
12108   /* If the next token is `=', then process a default argument.  */
12109   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12110     {
12111       bool saved_greater_than_is_operator_p;
12112       /* Consume the `='.  */
12113       cp_lexer_consume_token (parser->lexer);
12114
12115       /* If we are defining a class, then the tokens that make up the
12116          default argument must be saved and processed later.  */
12117       if (!template_parm_p && at_class_scope_p ()
12118           && TYPE_BEING_DEFINED (current_class_type))
12119         {
12120           unsigned depth = 0;
12121           cp_token *first_token;
12122           cp_token *token;
12123
12124           /* Add tokens until we have processed the entire default
12125              argument.  We add the range [first_token, token).  */
12126           first_token = cp_lexer_peek_token (parser->lexer);
12127           while (true)
12128             {
12129               bool done = false;
12130
12131               /* Peek at the next token.  */
12132               token = cp_lexer_peek_token (parser->lexer);
12133               /* What we do depends on what token we have.  */
12134               switch (token->type)
12135                 {
12136                   /* In valid code, a default argument must be
12137                      immediately followed by a `,' `)', or `...'.  */
12138                 case CPP_COMMA:
12139                 case CPP_CLOSE_PAREN:
12140                 case CPP_ELLIPSIS:
12141                   /* If we run into a non-nested `;', `}', or `]',
12142                      then the code is invalid -- but the default
12143                      argument is certainly over.  */
12144                 case CPP_SEMICOLON:
12145                 case CPP_CLOSE_BRACE:
12146                 case CPP_CLOSE_SQUARE:
12147                   if (depth == 0)
12148                     done = true;
12149                   /* Update DEPTH, if necessary.  */
12150                   else if (token->type == CPP_CLOSE_PAREN
12151                            || token->type == CPP_CLOSE_BRACE
12152                            || token->type == CPP_CLOSE_SQUARE)
12153                     --depth;
12154                   break;
12155
12156                 case CPP_OPEN_PAREN:
12157                 case CPP_OPEN_SQUARE:
12158                 case CPP_OPEN_BRACE:
12159                   ++depth;
12160                   break;
12161
12162                 case CPP_GREATER:
12163                   /* If we see a non-nested `>', and `>' is not an
12164                      operator, then it marks the end of the default
12165                      argument.  */
12166                   if (!depth && !greater_than_is_operator_p)
12167                     done = true;
12168                   break;
12169
12170                   /* If we run out of tokens, issue an error message.  */
12171                 case CPP_EOF:
12172                   error ("file ends in default argument");
12173                   done = true;
12174                   break;
12175
12176                 case CPP_NAME:
12177                 case CPP_SCOPE:
12178                   /* In these cases, we should look for template-ids.
12179                      For example, if the default argument is
12180                      `X<int, double>()', we need to do name lookup to
12181                      figure out whether or not `X' is a template; if
12182                      so, the `,' does not end the default argument.
12183
12184                      That is not yet done.  */
12185                   break;
12186
12187                 default:
12188                   break;
12189                 }
12190
12191               /* If we've reached the end, stop.  */
12192               if (done)
12193                 break;
12194
12195               /* Add the token to the token block.  */
12196               token = cp_lexer_consume_token (parser->lexer);
12197             }
12198
12199           /* Create a DEFAULT_ARG to represented the unparsed default
12200              argument.  */
12201           default_argument = make_node (DEFAULT_ARG);
12202           DEFARG_TOKENS (default_argument)
12203             = cp_token_cache_new (first_token, token);
12204           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12205         }
12206       /* Outside of a class definition, we can just parse the
12207          assignment-expression.  */
12208       else
12209         {
12210           bool saved_local_variables_forbidden_p;
12211
12212           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12213              set correctly.  */
12214           saved_greater_than_is_operator_p
12215             = parser->greater_than_is_operator_p;
12216           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12217           /* Local variable names (and the `this' keyword) may not
12218              appear in a default argument.  */
12219           saved_local_variables_forbidden_p
12220             = parser->local_variables_forbidden_p;
12221           parser->local_variables_forbidden_p = true;
12222           /* Parse the assignment-expression.  */
12223           default_argument
12224             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12225           /* Restore saved state.  */
12226           parser->greater_than_is_operator_p
12227             = saved_greater_than_is_operator_p;
12228           parser->local_variables_forbidden_p
12229             = saved_local_variables_forbidden_p;
12230         }
12231       if (!parser->default_arg_ok_p)
12232         {
12233           if (!flag_pedantic_errors)
12234             warning (0, "deprecated use of default argument for parameter of non-function");
12235           else
12236             {
12237               error ("default arguments are only permitted for function parameters");
12238               default_argument = NULL_TREE;
12239             }
12240         }
12241     }
12242   else
12243     default_argument = NULL_TREE;
12244
12245   return make_parameter_declarator (&decl_specifiers,
12246                                     declarator,
12247                                     default_argument);
12248 }
12249
12250 /* Parse a function-body.
12251
12252    function-body:
12253      compound_statement  */
12254
12255 static void
12256 cp_parser_function_body (cp_parser *parser)
12257 {
12258   cp_parser_compound_statement (parser, NULL, false);
12259 }
12260
12261 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12262    true if a ctor-initializer was present.  */
12263
12264 static bool
12265 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12266 {
12267   tree body;
12268   bool ctor_initializer_p;
12269
12270   /* Begin the function body.  */
12271   body = begin_function_body ();
12272   /* Parse the optional ctor-initializer.  */
12273   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12274   /* Parse the function-body.  */
12275   cp_parser_function_body (parser);
12276   /* Finish the function body.  */
12277   finish_function_body (body);
12278
12279   return ctor_initializer_p;
12280 }
12281
12282 /* Parse an initializer.
12283
12284    initializer:
12285      = initializer-clause
12286      ( expression-list )
12287
12288    Returns an expression representing the initializer.  If no
12289    initializer is present, NULL_TREE is returned.
12290
12291    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12292    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12293    set to FALSE if there is no initializer present.  If there is an
12294    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12295    is set to true; otherwise it is set to false.  */
12296
12297 static tree
12298 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12299                        bool* non_constant_p)
12300 {
12301   cp_token *token;
12302   tree init;
12303
12304   /* Peek at the next token.  */
12305   token = cp_lexer_peek_token (parser->lexer);
12306
12307   /* Let our caller know whether or not this initializer was
12308      parenthesized.  */
12309   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12310   /* Assume that the initializer is constant.  */
12311   *non_constant_p = false;
12312
12313   if (token->type == CPP_EQ)
12314     {
12315       /* Consume the `='.  */
12316       cp_lexer_consume_token (parser->lexer);
12317       /* Parse the initializer-clause.  */
12318       init = cp_parser_initializer_clause (parser, non_constant_p);
12319     }
12320   else if (token->type == CPP_OPEN_PAREN)
12321     init = cp_parser_parenthesized_expression_list (parser, false,
12322                                                     /*cast_p=*/false,
12323                                                     non_constant_p);
12324   else
12325     {
12326       /* Anything else is an error.  */
12327       cp_parser_error (parser, "expected initializer");
12328       init = error_mark_node;
12329     }
12330
12331   return init;
12332 }
12333
12334 /* Parse an initializer-clause.
12335
12336    initializer-clause:
12337      assignment-expression
12338      { initializer-list , [opt] }
12339      { }
12340
12341    Returns an expression representing the initializer.
12342
12343    If the `assignment-expression' production is used the value
12344    returned is simply a representation for the expression.
12345
12346    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12347    the elements of the initializer-list (or NULL, if the last
12348    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12349    NULL_TREE.  There is no way to detect whether or not the optional
12350    trailing `,' was provided.  NON_CONSTANT_P is as for
12351    cp_parser_initializer.  */
12352
12353 static tree
12354 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12355 {
12356   tree initializer;
12357
12358   /* Assume the expression is constant.  */
12359   *non_constant_p = false;
12360
12361   /* If it is not a `{', then we are looking at an
12362      assignment-expression.  */
12363   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12364     {
12365       initializer
12366         = cp_parser_constant_expression (parser,
12367                                         /*allow_non_constant_p=*/true,
12368                                         non_constant_p);
12369       if (!*non_constant_p)
12370         initializer = fold_non_dependent_expr (initializer);
12371     }
12372   else
12373     {
12374       /* Consume the `{' token.  */
12375       cp_lexer_consume_token (parser->lexer);
12376       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12377       initializer = make_node (CONSTRUCTOR);
12378       /* If it's not a `}', then there is a non-trivial initializer.  */
12379       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12380         {
12381           /* Parse the initializer list.  */
12382           CONSTRUCTOR_ELTS (initializer)
12383             = cp_parser_initializer_list (parser, non_constant_p);
12384           /* A trailing `,' token is allowed.  */
12385           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12386             cp_lexer_consume_token (parser->lexer);
12387         }
12388       /* Now, there should be a trailing `}'.  */
12389       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12390     }
12391
12392   return initializer;
12393 }
12394
12395 /* Parse an initializer-list.
12396
12397    initializer-list:
12398      initializer-clause
12399      initializer-list , initializer-clause
12400
12401    GNU Extension:
12402
12403    initializer-list:
12404      identifier : initializer-clause
12405      initializer-list, identifier : initializer-clause
12406
12407    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12408    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12409    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12410    as for cp_parser_initializer.  */
12411
12412 static VEC(constructor_elt,gc) *
12413 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12414 {
12415   VEC(constructor_elt,gc) *v = NULL;
12416
12417   /* Assume all of the expressions are constant.  */
12418   *non_constant_p = false;
12419
12420   /* Parse the rest of the list.  */
12421   while (true)
12422     {
12423       cp_token *token;
12424       tree identifier;
12425       tree initializer;
12426       bool clause_non_constant_p;
12427
12428       /* If the next token is an identifier and the following one is a
12429          colon, we are looking at the GNU designated-initializer
12430          syntax.  */
12431       if (cp_parser_allow_gnu_extensions_p (parser)
12432           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12433           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12434         {
12435           /* Consume the identifier.  */
12436           identifier = cp_lexer_consume_token (parser->lexer)->value;
12437           /* Consume the `:'.  */
12438           cp_lexer_consume_token (parser->lexer);
12439         }
12440       else
12441         identifier = NULL_TREE;
12442
12443       /* Parse the initializer.  */
12444       initializer = cp_parser_initializer_clause (parser,
12445                                                   &clause_non_constant_p);
12446       /* If any clause is non-constant, so is the entire initializer.  */
12447       if (clause_non_constant_p)
12448         *non_constant_p = true;
12449
12450       /* Add it to the vector.  */
12451       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12452
12453       /* If the next token is not a comma, we have reached the end of
12454          the list.  */
12455       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12456         break;
12457
12458       /* Peek at the next token.  */
12459       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12460       /* If the next token is a `}', then we're still done.  An
12461          initializer-clause can have a trailing `,' after the
12462          initializer-list and before the closing `}'.  */
12463       if (token->type == CPP_CLOSE_BRACE)
12464         break;
12465
12466       /* Consume the `,' token.  */
12467       cp_lexer_consume_token (parser->lexer);
12468     }
12469
12470   return v;
12471 }
12472
12473 /* Classes [gram.class] */
12474
12475 /* Parse a class-name.
12476
12477    class-name:
12478      identifier
12479      template-id
12480
12481    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12482    to indicate that names looked up in dependent types should be
12483    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12484    keyword has been used to indicate that the name that appears next
12485    is a template.  TAG_TYPE indicates the explicit tag given before
12486    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12487    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12488    is the class being defined in a class-head.
12489
12490    Returns the TYPE_DECL representing the class.  */
12491
12492 static tree
12493 cp_parser_class_name (cp_parser *parser,
12494                       bool typename_keyword_p,
12495                       bool template_keyword_p,
12496                       enum tag_types tag_type,
12497                       bool check_dependency_p,
12498                       bool class_head_p,
12499                       bool is_declaration)
12500 {
12501   tree decl;
12502   tree scope;
12503   bool typename_p;
12504   cp_token *token;
12505
12506   /* All class-names start with an identifier.  */
12507   token = cp_lexer_peek_token (parser->lexer);
12508   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12509     {
12510       cp_parser_error (parser, "expected class-name");
12511       return error_mark_node;
12512     }
12513
12514   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12515      to a template-id, so we save it here.  */
12516   scope = parser->scope;
12517   if (scope == error_mark_node)
12518     return error_mark_node;
12519
12520   /* Any name names a type if we're following the `typename' keyword
12521      in a qualified name where the enclosing scope is type-dependent.  */
12522   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12523                 && dependent_type_p (scope));
12524   /* Handle the common case (an identifier, but not a template-id)
12525      efficiently.  */
12526   if (token->type == CPP_NAME
12527       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12528     {
12529       cp_token *identifier_token;
12530       tree identifier;
12531       bool ambiguous_p;
12532
12533       /* Look for the identifier.  */
12534       identifier_token = cp_lexer_peek_token (parser->lexer);
12535       ambiguous_p = identifier_token->ambiguous_p;
12536       identifier = cp_parser_identifier (parser);
12537       /* If the next token isn't an identifier, we are certainly not
12538          looking at a class-name.  */
12539       if (identifier == error_mark_node)
12540         decl = error_mark_node;
12541       /* If we know this is a type-name, there's no need to look it
12542          up.  */
12543       else if (typename_p)
12544         decl = identifier;
12545       else
12546         {
12547           tree ambiguous_decls;
12548           /* If we already know that this lookup is ambiguous, then
12549              we've already issued an error message; there's no reason
12550              to check again.  */
12551           if (ambiguous_p)
12552             {
12553               cp_parser_simulate_error (parser);
12554               return error_mark_node;
12555             }
12556           /* If the next token is a `::', then the name must be a type
12557              name.
12558
12559              [basic.lookup.qual]
12560
12561              During the lookup for a name preceding the :: scope
12562              resolution operator, object, function, and enumerator
12563              names are ignored.  */
12564           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12565             tag_type = typename_type;
12566           /* Look up the name.  */
12567           decl = cp_parser_lookup_name (parser, identifier,
12568                                         tag_type,
12569                                         /*is_template=*/false,
12570                                         /*is_namespace=*/false,
12571                                         check_dependency_p,
12572                                         &ambiguous_decls);
12573           if (ambiguous_decls)
12574             {
12575               error ("reference to %qD is ambiguous", identifier);
12576               print_candidates (ambiguous_decls);
12577               if (cp_parser_parsing_tentatively (parser))
12578                 {
12579                   identifier_token->ambiguous_p = true;
12580                   cp_parser_simulate_error (parser);
12581                 }
12582               return error_mark_node;
12583             }
12584         }
12585     }
12586   else
12587     {
12588       /* Try a template-id.  */
12589       decl = cp_parser_template_id (parser, template_keyword_p,
12590                                     check_dependency_p,
12591                                     is_declaration);
12592       if (decl == error_mark_node)
12593         return error_mark_node;
12594     }
12595
12596   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12597
12598   /* If this is a typename, create a TYPENAME_TYPE.  */
12599   if (typename_p && decl != error_mark_node)
12600     {
12601       decl = make_typename_type (scope, decl, typename_type,
12602                                  /*complain=*/tf_error);
12603       if (decl != error_mark_node)
12604         decl = TYPE_NAME (decl);
12605     }
12606
12607   /* Check to see that it is really the name of a class.  */
12608   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12609       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12610       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12611     /* Situations like this:
12612
12613          template <typename T> struct A {
12614            typename T::template X<int>::I i;
12615          };
12616
12617        are problematic.  Is `T::template X<int>' a class-name?  The
12618        standard does not seem to be definitive, but there is no other
12619        valid interpretation of the following `::'.  Therefore, those
12620        names are considered class-names.  */
12621     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12622   else if (decl == error_mark_node
12623            || TREE_CODE (decl) != TYPE_DECL
12624            || TREE_TYPE (decl) == error_mark_node
12625            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12626     {
12627       cp_parser_error (parser, "expected class-name");
12628       return error_mark_node;
12629     }
12630
12631   return decl;
12632 }
12633
12634 /* Parse a class-specifier.
12635
12636    class-specifier:
12637      class-head { member-specification [opt] }
12638
12639    Returns the TREE_TYPE representing the class.  */
12640
12641 static tree
12642 cp_parser_class_specifier (cp_parser* parser)
12643 {
12644   cp_token *token;
12645   tree type;
12646   tree attributes = NULL_TREE;
12647   int has_trailing_semicolon;
12648   bool nested_name_specifier_p;
12649   unsigned saved_num_template_parameter_lists;
12650   tree old_scope = NULL_TREE;
12651   tree scope = NULL_TREE;
12652
12653   push_deferring_access_checks (dk_no_deferred);
12654
12655   /* Parse the class-head.  */
12656   type = cp_parser_class_head (parser,
12657                                &nested_name_specifier_p,
12658                                &attributes);
12659   /* If the class-head was a semantic disaster, skip the entire body
12660      of the class.  */
12661   if (!type)
12662     {
12663       cp_parser_skip_to_end_of_block_or_statement (parser);
12664       pop_deferring_access_checks ();
12665       return error_mark_node;
12666     }
12667
12668   /* Look for the `{'.  */
12669   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12670     {
12671       pop_deferring_access_checks ();
12672       return error_mark_node;
12673     }
12674
12675   /* Issue an error message if type-definitions are forbidden here.  */
12676   cp_parser_check_type_definition (parser);
12677   /* Remember that we are defining one more class.  */
12678   ++parser->num_classes_being_defined;
12679   /* Inside the class, surrounding template-parameter-lists do not
12680      apply.  */
12681   saved_num_template_parameter_lists
12682     = parser->num_template_parameter_lists;
12683   parser->num_template_parameter_lists = 0;
12684
12685   /* Start the class.  */
12686   if (nested_name_specifier_p)
12687     {
12688       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12689       old_scope = push_inner_scope (scope);
12690     }
12691   type = begin_class_definition (type);
12692
12693   if (type == error_mark_node)
12694     /* If the type is erroneous, skip the entire body of the class.  */
12695     cp_parser_skip_to_closing_brace (parser);
12696   else
12697     /* Parse the member-specification.  */
12698     cp_parser_member_specification_opt (parser);
12699
12700   /* Look for the trailing `}'.  */
12701   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12702   /* We get better error messages by noticing a common problem: a
12703      missing trailing `;'.  */
12704   token = cp_lexer_peek_token (parser->lexer);
12705   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12706   /* Look for trailing attributes to apply to this class.  */
12707   if (cp_parser_allow_gnu_extensions_p (parser))
12708     {
12709       tree sub_attr = cp_parser_attributes_opt (parser);
12710       attributes = chainon (attributes, sub_attr);
12711     }
12712   if (type != error_mark_node)
12713     type = finish_struct (type, attributes);
12714   if (nested_name_specifier_p)
12715     pop_inner_scope (old_scope, scope);
12716   /* If this class is not itself within the scope of another class,
12717      then we need to parse the bodies of all of the queued function
12718      definitions.  Note that the queued functions defined in a class
12719      are not always processed immediately following the
12720      class-specifier for that class.  Consider:
12721
12722        struct A {
12723          struct B { void f() { sizeof (A); } };
12724        };
12725
12726      If `f' were processed before the processing of `A' were
12727      completed, there would be no way to compute the size of `A'.
12728      Note that the nesting we are interested in here is lexical --
12729      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12730      for:
12731
12732        struct A { struct B; };
12733        struct A::B { void f() { } };
12734
12735      there is no need to delay the parsing of `A::B::f'.  */
12736   if (--parser->num_classes_being_defined == 0)
12737     {
12738       tree queue_entry;
12739       tree fn;
12740       tree class_type = NULL_TREE;
12741       tree pushed_scope = NULL_TREE;
12742  
12743       /* In a first pass, parse default arguments to the functions.
12744          Then, in a second pass, parse the bodies of the functions.
12745          This two-phased approach handles cases like:
12746
12747             struct S {
12748               void f() { g(); }
12749               void g(int i = 3);
12750             };
12751
12752          */
12753       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12754              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12755            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12756            TREE_PURPOSE (parser->unparsed_functions_queues)
12757              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12758         {
12759           fn = TREE_VALUE (queue_entry);
12760           /* If there are default arguments that have not yet been processed,
12761              take care of them now.  */
12762           if (class_type != TREE_PURPOSE (queue_entry))
12763             {
12764               if (pushed_scope)
12765                 pop_scope (pushed_scope);
12766               class_type = TREE_PURPOSE (queue_entry);
12767               pushed_scope = push_scope (class_type);
12768             }
12769           /* Make sure that any template parameters are in scope.  */
12770           maybe_begin_member_template_processing (fn);
12771           /* Parse the default argument expressions.  */
12772           cp_parser_late_parsing_default_args (parser, fn);
12773           /* Remove any template parameters from the symbol table.  */
12774           maybe_end_member_template_processing ();
12775         }
12776       if (pushed_scope)
12777         pop_scope (pushed_scope);
12778       /* Now parse the body of the functions.  */
12779       for (TREE_VALUE (parser->unparsed_functions_queues)
12780              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12781            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12782            TREE_VALUE (parser->unparsed_functions_queues)
12783              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12784         {
12785           /* Figure out which function we need to process.  */
12786           fn = TREE_VALUE (queue_entry);
12787           /* Parse the function.  */
12788           cp_parser_late_parsing_for_member (parser, fn);
12789         }
12790     }
12791
12792   /* Put back any saved access checks.  */
12793   pop_deferring_access_checks ();
12794
12795   /* Restore the count of active template-parameter-lists.  */
12796   parser->num_template_parameter_lists
12797     = saved_num_template_parameter_lists;
12798
12799   return type;
12800 }
12801
12802 /* Parse a class-head.
12803
12804    class-head:
12805      class-key identifier [opt] base-clause [opt]
12806      class-key nested-name-specifier identifier base-clause [opt]
12807      class-key nested-name-specifier [opt] template-id
12808        base-clause [opt]
12809
12810    GNU Extensions:
12811      class-key attributes identifier [opt] base-clause [opt]
12812      class-key attributes nested-name-specifier identifier base-clause [opt]
12813      class-key attributes nested-name-specifier [opt] template-id
12814        base-clause [opt]
12815
12816    Returns the TYPE of the indicated class.  Sets
12817    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12818    involving a nested-name-specifier was used, and FALSE otherwise.
12819
12820    Returns error_mark_node if this is not a class-head.
12821
12822    Returns NULL_TREE if the class-head is syntactically valid, but
12823    semantically invalid in a way that means we should skip the entire
12824    body of the class.  */
12825
12826 static tree
12827 cp_parser_class_head (cp_parser* parser,
12828                       bool* nested_name_specifier_p,
12829                       tree *attributes_p)
12830 {
12831   tree nested_name_specifier;
12832   enum tag_types class_key;
12833   tree id = NULL_TREE;
12834   tree type = NULL_TREE;
12835   tree attributes;
12836   bool template_id_p = false;
12837   bool qualified_p = false;
12838   bool invalid_nested_name_p = false;
12839   bool invalid_explicit_specialization_p = false;
12840   tree pushed_scope = NULL_TREE;
12841   unsigned num_templates;
12842   tree bases;
12843
12844   /* Assume no nested-name-specifier will be present.  */
12845   *nested_name_specifier_p = false;
12846   /* Assume no template parameter lists will be used in defining the
12847      type.  */
12848   num_templates = 0;
12849
12850   /* Look for the class-key.  */
12851   class_key = cp_parser_class_key (parser);
12852   if (class_key == none_type)
12853     return error_mark_node;
12854
12855   /* Parse the attributes.  */
12856   attributes = cp_parser_attributes_opt (parser);
12857
12858   /* If the next token is `::', that is invalid -- but sometimes
12859      people do try to write:
12860
12861        struct ::S {};
12862
12863      Handle this gracefully by accepting the extra qualifier, and then
12864      issuing an error about it later if this really is a
12865      class-head.  If it turns out just to be an elaborated type
12866      specifier, remain silent.  */
12867   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12868     qualified_p = true;
12869
12870   push_deferring_access_checks (dk_no_check);
12871
12872   /* Determine the name of the class.  Begin by looking for an
12873      optional nested-name-specifier.  */
12874   nested_name_specifier
12875     = cp_parser_nested_name_specifier_opt (parser,
12876                                            /*typename_keyword_p=*/false,
12877                                            /*check_dependency_p=*/false,
12878                                            /*type_p=*/false,
12879                                            /*is_declaration=*/false);
12880   /* If there was a nested-name-specifier, then there *must* be an
12881      identifier.  */
12882   if (nested_name_specifier)
12883     {
12884       /* Although the grammar says `identifier', it really means
12885          `class-name' or `template-name'.  You are only allowed to
12886          define a class that has already been declared with this
12887          syntax.
12888
12889          The proposed resolution for Core Issue 180 says that whever
12890          you see `class T::X' you should treat `X' as a type-name.
12891
12892          It is OK to define an inaccessible class; for example:
12893
12894            class A { class B; };
12895            class A::B {};
12896
12897          We do not know if we will see a class-name, or a
12898          template-name.  We look for a class-name first, in case the
12899          class-name is a template-id; if we looked for the
12900          template-name first we would stop after the template-name.  */
12901       cp_parser_parse_tentatively (parser);
12902       type = cp_parser_class_name (parser,
12903                                    /*typename_keyword_p=*/false,
12904                                    /*template_keyword_p=*/false,
12905                                    class_type,
12906                                    /*check_dependency_p=*/false,
12907                                    /*class_head_p=*/true,
12908                                    /*is_declaration=*/false);
12909       /* If that didn't work, ignore the nested-name-specifier.  */
12910       if (!cp_parser_parse_definitely (parser))
12911         {
12912           invalid_nested_name_p = true;
12913           id = cp_parser_identifier (parser);
12914           if (id == error_mark_node)
12915             id = NULL_TREE;
12916         }
12917       /* If we could not find a corresponding TYPE, treat this
12918          declaration like an unqualified declaration.  */
12919       if (type == error_mark_node)
12920         nested_name_specifier = NULL_TREE;
12921       /* Otherwise, count the number of templates used in TYPE and its
12922          containing scopes.  */
12923       else
12924         {
12925           tree scope;
12926
12927           for (scope = TREE_TYPE (type);
12928                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12929                scope = (TYPE_P (scope)
12930                         ? TYPE_CONTEXT (scope)
12931                         : DECL_CONTEXT (scope)))
12932             if (TYPE_P (scope)
12933                 && CLASS_TYPE_P (scope)
12934                 && CLASSTYPE_TEMPLATE_INFO (scope)
12935                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12936                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12937               ++num_templates;
12938         }
12939     }
12940   /* Otherwise, the identifier is optional.  */
12941   else
12942     {
12943       /* We don't know whether what comes next is a template-id,
12944          an identifier, or nothing at all.  */
12945       cp_parser_parse_tentatively (parser);
12946       /* Check for a template-id.  */
12947       id = cp_parser_template_id (parser,
12948                                   /*template_keyword_p=*/false,
12949                                   /*check_dependency_p=*/true,
12950                                   /*is_declaration=*/true);
12951       /* If that didn't work, it could still be an identifier.  */
12952       if (!cp_parser_parse_definitely (parser))
12953         {
12954           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12955             id = cp_parser_identifier (parser);
12956           else
12957             id = NULL_TREE;
12958         }
12959       else
12960         {
12961           template_id_p = true;
12962           ++num_templates;
12963         }
12964     }
12965
12966   pop_deferring_access_checks ();
12967
12968   if (id)
12969     cp_parser_check_for_invalid_template_id (parser, id);
12970
12971   /* If it's not a `:' or a `{' then we can't really be looking at a
12972      class-head, since a class-head only appears as part of a
12973      class-specifier.  We have to detect this situation before calling
12974      xref_tag, since that has irreversible side-effects.  */
12975   if (!cp_parser_next_token_starts_class_definition_p (parser))
12976     {
12977       cp_parser_error (parser, "expected %<{%> or %<:%>");
12978       return error_mark_node;
12979     }
12980
12981   /* At this point, we're going ahead with the class-specifier, even
12982      if some other problem occurs.  */
12983   cp_parser_commit_to_tentative_parse (parser);
12984   /* Issue the error about the overly-qualified name now.  */
12985   if (qualified_p)
12986     cp_parser_error (parser,
12987                      "global qualification of class name is invalid");
12988   else if (invalid_nested_name_p)
12989     cp_parser_error (parser,
12990                      "qualified name does not name a class");
12991   else if (nested_name_specifier)
12992     {
12993       tree scope;
12994
12995       /* Reject typedef-names in class heads.  */
12996       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12997         {
12998           error ("invalid class name in declaration of %qD", type);
12999           type = NULL_TREE;
13000           goto done;
13001         }
13002
13003       /* Figure out in what scope the declaration is being placed.  */
13004       scope = current_scope ();
13005       /* If that scope does not contain the scope in which the
13006          class was originally declared, the program is invalid.  */
13007       if (scope && !is_ancestor (scope, nested_name_specifier))
13008         {
13009           error ("declaration of %qD in %qD which does not enclose %qD",
13010                  type, scope, nested_name_specifier);
13011           type = NULL_TREE;
13012           goto done;
13013         }
13014       /* [dcl.meaning]
13015
13016          A declarator-id shall not be qualified exception of the
13017          definition of a ... nested class outside of its class
13018          ... [or] a the definition or explicit instantiation of a
13019          class member of a namespace outside of its namespace.  */
13020       if (scope == nested_name_specifier)
13021         {
13022           pedwarn ("extra qualification ignored");
13023           nested_name_specifier = NULL_TREE;
13024           num_templates = 0;
13025         }
13026     }
13027   /* An explicit-specialization must be preceded by "template <>".  If
13028      it is not, try to recover gracefully.  */
13029   if (at_namespace_scope_p ()
13030       && parser->num_template_parameter_lists == 0
13031       && template_id_p)
13032     {
13033       error ("an explicit specialization must be preceded by %<template <>%>");
13034       invalid_explicit_specialization_p = true;
13035       /* Take the same action that would have been taken by
13036          cp_parser_explicit_specialization.  */
13037       ++parser->num_template_parameter_lists;
13038       begin_specialization ();
13039     }
13040   /* There must be no "return" statements between this point and the
13041      end of this function; set "type "to the correct return value and
13042      use "goto done;" to return.  */
13043   /* Make sure that the right number of template parameters were
13044      present.  */
13045   if (!cp_parser_check_template_parameters (parser, num_templates))
13046     {
13047       /* If something went wrong, there is no point in even trying to
13048          process the class-definition.  */
13049       type = NULL_TREE;
13050       goto done;
13051     }
13052
13053   /* Look up the type.  */
13054   if (template_id_p)
13055     {
13056       type = TREE_TYPE (id);
13057       maybe_process_partial_specialization (type);
13058       if (nested_name_specifier)
13059         pushed_scope = push_scope (nested_name_specifier);
13060     }
13061   else if (nested_name_specifier)
13062     {
13063       tree class_type;
13064
13065       /* Given:
13066
13067             template <typename T> struct S { struct T };
13068             template <typename T> struct S<T>::T { };
13069
13070          we will get a TYPENAME_TYPE when processing the definition of
13071          `S::T'.  We need to resolve it to the actual type before we
13072          try to define it.  */
13073       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13074         {
13075           class_type = resolve_typename_type (TREE_TYPE (type),
13076                                               /*only_current_p=*/false);
13077           if (class_type != error_mark_node)
13078             type = TYPE_NAME (class_type);
13079           else
13080             {
13081               cp_parser_error (parser, "could not resolve typename type");
13082               type = error_mark_node;
13083             }
13084         }
13085
13086       maybe_process_partial_specialization (TREE_TYPE (type));
13087       class_type = current_class_type;
13088       /* Enter the scope indicated by the nested-name-specifier.  */
13089       pushed_scope = push_scope (nested_name_specifier);
13090       /* Get the canonical version of this type.  */
13091       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13092       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13093           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13094         {
13095           type = push_template_decl (type);
13096           if (type == error_mark_node)
13097             {
13098               type = NULL_TREE;
13099               goto done;
13100             }
13101         }
13102
13103       type = TREE_TYPE (type);
13104       *nested_name_specifier_p = true;
13105     }
13106   else      /* The name is not a nested name.  */
13107     {
13108       /* If the class was unnamed, create a dummy name.  */
13109       if (!id)
13110         id = make_anon_name ();
13111       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13112                        parser->num_template_parameter_lists);
13113     }
13114
13115   /* Indicate whether this class was declared as a `class' or as a
13116      `struct'.  */
13117   if (TREE_CODE (type) == RECORD_TYPE)
13118     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13119   cp_parser_check_class_key (class_key, type);
13120
13121   /* If this type was already complete, and we see another definition,
13122      that's an error.  */
13123   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13124     {
13125       error ("redefinition of %q#T", type);
13126       error ("previous definition of %q+#T", type);
13127       type = NULL_TREE;
13128       goto done;
13129     }
13130
13131   /* We will have entered the scope containing the class; the names of
13132      base classes should be looked up in that context.  For example:
13133
13134        struct A { struct B {}; struct C; };
13135        struct A::C : B {};
13136
13137      is valid.  */
13138   bases = NULL_TREE;
13139
13140   /* Get the list of base-classes, if there is one.  */
13141   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13142     bases = cp_parser_base_clause (parser);
13143
13144   /* Process the base classes.  */
13145   xref_basetypes (type, bases);
13146
13147  done:
13148   /* Leave the scope given by the nested-name-specifier.  We will
13149      enter the class scope itself while processing the members.  */
13150   if (pushed_scope)
13151     pop_scope (pushed_scope);
13152
13153   if (invalid_explicit_specialization_p)
13154     {
13155       end_specialization ();
13156       --parser->num_template_parameter_lists;
13157     }
13158   *attributes_p = attributes;
13159   return type;
13160 }
13161
13162 /* Parse a class-key.
13163
13164    class-key:
13165      class
13166      struct
13167      union
13168
13169    Returns the kind of class-key specified, or none_type to indicate
13170    error.  */
13171
13172 static enum tag_types
13173 cp_parser_class_key (cp_parser* parser)
13174 {
13175   cp_token *token;
13176   enum tag_types tag_type;
13177
13178   /* Look for the class-key.  */
13179   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13180   if (!token)
13181     return none_type;
13182
13183   /* Check to see if the TOKEN is a class-key.  */
13184   tag_type = cp_parser_token_is_class_key (token);
13185   if (!tag_type)
13186     cp_parser_error (parser, "expected class-key");
13187   return tag_type;
13188 }
13189
13190 /* Parse an (optional) member-specification.
13191
13192    member-specification:
13193      member-declaration member-specification [opt]
13194      access-specifier : member-specification [opt]  */
13195
13196 static void
13197 cp_parser_member_specification_opt (cp_parser* parser)
13198 {
13199   while (true)
13200     {
13201       cp_token *token;
13202       enum rid keyword;
13203
13204       /* Peek at the next token.  */
13205       token = cp_lexer_peek_token (parser->lexer);
13206       /* If it's a `}', or EOF then we've seen all the members.  */
13207       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13208         break;
13209
13210       /* See if this token is a keyword.  */
13211       keyword = token->keyword;
13212       switch (keyword)
13213         {
13214         case RID_PUBLIC:
13215         case RID_PROTECTED:
13216         case RID_PRIVATE:
13217           /* Consume the access-specifier.  */
13218           cp_lexer_consume_token (parser->lexer);
13219           /* Remember which access-specifier is active.  */
13220           current_access_specifier = token->value;
13221           /* Look for the `:'.  */
13222           cp_parser_require (parser, CPP_COLON, "`:'");
13223           break;
13224
13225         default:
13226           /* Accept #pragmas at class scope.  */
13227           if (token->type == CPP_PRAGMA)
13228             {
13229               cp_lexer_handle_pragma (parser->lexer);
13230               break;
13231             }
13232
13233           /* Otherwise, the next construction must be a
13234              member-declaration.  */
13235           cp_parser_member_declaration (parser);
13236         }
13237     }
13238 }
13239
13240 /* Parse a member-declaration.
13241
13242    member-declaration:
13243      decl-specifier-seq [opt] member-declarator-list [opt] ;
13244      function-definition ; [opt]
13245      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13246      using-declaration
13247      template-declaration
13248
13249    member-declarator-list:
13250      member-declarator
13251      member-declarator-list , member-declarator
13252
13253    member-declarator:
13254      declarator pure-specifier [opt]
13255      declarator constant-initializer [opt]
13256      identifier [opt] : constant-expression
13257
13258    GNU Extensions:
13259
13260    member-declaration:
13261      __extension__ member-declaration
13262
13263    member-declarator:
13264      declarator attributes [opt] pure-specifier [opt]
13265      declarator attributes [opt] constant-initializer [opt]
13266      identifier [opt] attributes [opt] : constant-expression  */
13267
13268 static void
13269 cp_parser_member_declaration (cp_parser* parser)
13270 {
13271   cp_decl_specifier_seq decl_specifiers;
13272   tree prefix_attributes;
13273   tree decl;
13274   int declares_class_or_enum;
13275   bool friend_p;
13276   cp_token *token;
13277   int saved_pedantic;
13278
13279   /* Check for the `__extension__' keyword.  */
13280   if (cp_parser_extension_opt (parser, &saved_pedantic))
13281     {
13282       /* Recurse.  */
13283       cp_parser_member_declaration (parser);
13284       /* Restore the old value of the PEDANTIC flag.  */
13285       pedantic = saved_pedantic;
13286
13287       return;
13288     }
13289
13290   /* Check for a template-declaration.  */
13291   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13292     {
13293       /* An explicit specialization here is an error condition, and we
13294          expect the specialization handler to detect and report this.  */
13295       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13296           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13297         cp_parser_explicit_specialization (parser);
13298       else
13299         cp_parser_template_declaration (parser, /*member_p=*/true);
13300
13301       return;
13302     }
13303
13304   /* Check for a using-declaration.  */
13305   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13306     {
13307       /* Parse the using-declaration.  */
13308       cp_parser_using_declaration (parser);
13309
13310       return;
13311     }
13312
13313   /* Check for @defs.  */
13314   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13315     {
13316       tree ivar, member;
13317       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13318       ivar = ivar_chains;
13319       while (ivar)
13320         {
13321           member = ivar;
13322           ivar = TREE_CHAIN (member);
13323           TREE_CHAIN (member) = NULL_TREE;
13324           finish_member_declaration (member);
13325         }
13326       return;
13327     }
13328
13329   /* Parse the decl-specifier-seq.  */
13330   cp_parser_decl_specifier_seq (parser,
13331                                 CP_PARSER_FLAGS_OPTIONAL,
13332                                 &decl_specifiers,
13333                                 &declares_class_or_enum);
13334   prefix_attributes = decl_specifiers.attributes;
13335   decl_specifiers.attributes = NULL_TREE;
13336   /* Check for an invalid type-name.  */
13337   if (!decl_specifiers.type
13338       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13339     return;
13340   /* If there is no declarator, then the decl-specifier-seq should
13341      specify a type.  */
13342   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13343     {
13344       /* If there was no decl-specifier-seq, and the next token is a
13345          `;', then we have something like:
13346
13347            struct S { ; };
13348
13349          [class.mem]
13350
13351          Each member-declaration shall declare at least one member
13352          name of the class.  */
13353       if (!decl_specifiers.any_specifiers_p)
13354         {
13355           cp_token *token = cp_lexer_peek_token (parser->lexer);
13356           if (pedantic && !token->in_system_header)
13357             pedwarn ("%Hextra %<;%>", &token->location);
13358         }
13359       else
13360         {
13361           tree type;
13362
13363           /* See if this declaration is a friend.  */
13364           friend_p = cp_parser_friend_p (&decl_specifiers);
13365           /* If there were decl-specifiers, check to see if there was
13366              a class-declaration.  */
13367           type = check_tag_decl (&decl_specifiers);
13368           /* Nested classes have already been added to the class, but
13369              a `friend' needs to be explicitly registered.  */
13370           if (friend_p)
13371             {
13372               /* If the `friend' keyword was present, the friend must
13373                  be introduced with a class-key.  */
13374                if (!declares_class_or_enum)
13375                  error ("a class-key must be used when declaring a friend");
13376                /* In this case:
13377
13378                     template <typename T> struct A {
13379                       friend struct A<T>::B;
13380                     };
13381
13382                   A<T>::B will be represented by a TYPENAME_TYPE, and
13383                   therefore not recognized by check_tag_decl.  */
13384                if (!type
13385                    && decl_specifiers.type
13386                    && TYPE_P (decl_specifiers.type))
13387                  type = decl_specifiers.type;
13388                if (!type || !TYPE_P (type))
13389                  error ("friend declaration does not name a class or "
13390                         "function");
13391                else
13392                  make_friend_class (current_class_type, type,
13393                                     /*complain=*/true);
13394             }
13395           /* If there is no TYPE, an error message will already have
13396              been issued.  */
13397           else if (!type || type == error_mark_node)
13398             ;
13399           /* An anonymous aggregate has to be handled specially; such
13400              a declaration really declares a data member (with a
13401              particular type), as opposed to a nested class.  */
13402           else if (ANON_AGGR_TYPE_P (type))
13403             {
13404               /* Remove constructors and such from TYPE, now that we
13405                  know it is an anonymous aggregate.  */
13406               fixup_anonymous_aggr (type);
13407               /* And make the corresponding data member.  */
13408               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13409               /* Add it to the class.  */
13410               finish_member_declaration (decl);
13411             }
13412           else
13413             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13414         }
13415     }
13416   else
13417     {
13418       /* See if these declarations will be friends.  */
13419       friend_p = cp_parser_friend_p (&decl_specifiers);
13420
13421       /* Keep going until we hit the `;' at the end of the
13422          declaration.  */
13423       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13424         {
13425           tree attributes = NULL_TREE;
13426           tree first_attribute;
13427
13428           /* Peek at the next token.  */
13429           token = cp_lexer_peek_token (parser->lexer);
13430
13431           /* Check for a bitfield declaration.  */
13432           if (token->type == CPP_COLON
13433               || (token->type == CPP_NAME
13434                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13435                   == CPP_COLON))
13436             {
13437               tree identifier;
13438               tree width;
13439
13440               /* Get the name of the bitfield.  Note that we cannot just
13441                  check TOKEN here because it may have been invalidated by
13442                  the call to cp_lexer_peek_nth_token above.  */
13443               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13444                 identifier = cp_parser_identifier (parser);
13445               else
13446                 identifier = NULL_TREE;
13447
13448               /* Consume the `:' token.  */
13449               cp_lexer_consume_token (parser->lexer);
13450               /* Get the width of the bitfield.  */
13451               width
13452                 = cp_parser_constant_expression (parser,
13453                                                  /*allow_non_constant=*/false,
13454                                                  NULL);
13455
13456               /* Look for attributes that apply to the bitfield.  */
13457               attributes = cp_parser_attributes_opt (parser);
13458               /* Remember which attributes are prefix attributes and
13459                  which are not.  */
13460               first_attribute = attributes;
13461               /* Combine the attributes.  */
13462               attributes = chainon (prefix_attributes, attributes);
13463
13464               /* Create the bitfield declaration.  */
13465               decl = grokbitfield (identifier
13466                                    ? make_id_declarator (NULL_TREE,
13467                                                          identifier)
13468                                    : NULL,
13469                                    &decl_specifiers,
13470                                    width);
13471               /* Apply the attributes.  */
13472               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13473             }
13474           else
13475             {
13476               cp_declarator *declarator;
13477               tree initializer;
13478               tree asm_specification;
13479               int ctor_dtor_or_conv_p;
13480
13481               /* Parse the declarator.  */
13482               declarator
13483                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13484                                         &ctor_dtor_or_conv_p,
13485                                         /*parenthesized_p=*/NULL,
13486                                         /*member_p=*/true);
13487
13488               /* If something went wrong parsing the declarator, make sure
13489                  that we at least consume some tokens.  */
13490               if (declarator == cp_error_declarator)
13491                 {
13492                   /* Skip to the end of the statement.  */
13493                   cp_parser_skip_to_end_of_statement (parser);
13494                   /* If the next token is not a semicolon, that is
13495                      probably because we just skipped over the body of
13496                      a function.  So, we consume a semicolon if
13497                      present, but do not issue an error message if it
13498                      is not present.  */
13499                   if (cp_lexer_next_token_is (parser->lexer,
13500                                               CPP_SEMICOLON))
13501                     cp_lexer_consume_token (parser->lexer);
13502                   return;
13503                 }
13504
13505               if (declares_class_or_enum & 2)
13506                 cp_parser_check_for_definition_in_return_type
13507                   (declarator, decl_specifiers.type);
13508
13509               /* Look for an asm-specification.  */
13510               asm_specification = cp_parser_asm_specification_opt (parser);
13511               /* Look for attributes that apply to the declaration.  */
13512               attributes = cp_parser_attributes_opt (parser);
13513               /* Remember which attributes are prefix attributes and
13514                  which are not.  */
13515               first_attribute = attributes;
13516               /* Combine the attributes.  */
13517               attributes = chainon (prefix_attributes, attributes);
13518
13519               /* If it's an `=', then we have a constant-initializer or a
13520                  pure-specifier.  It is not correct to parse the
13521                  initializer before registering the member declaration
13522                  since the member declaration should be in scope while
13523                  its initializer is processed.  However, the rest of the
13524                  front end does not yet provide an interface that allows
13525                  us to handle this correctly.  */
13526               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13527                 {
13528                   /* In [class.mem]:
13529
13530                      A pure-specifier shall be used only in the declaration of
13531                      a virtual function.
13532
13533                      A member-declarator can contain a constant-initializer
13534                      only if it declares a static member of integral or
13535                      enumeration type.
13536
13537                      Therefore, if the DECLARATOR is for a function, we look
13538                      for a pure-specifier; otherwise, we look for a
13539                      constant-initializer.  When we call `grokfield', it will
13540                      perform more stringent semantics checks.  */
13541                   if (declarator->kind == cdk_function)
13542                     initializer = cp_parser_pure_specifier (parser);
13543                   else
13544                     /* Parse the initializer.  */
13545                     initializer = cp_parser_constant_initializer (parser);
13546                 }
13547               /* Otherwise, there is no initializer.  */
13548               else
13549                 initializer = NULL_TREE;
13550
13551               /* See if we are probably looking at a function
13552                  definition.  We are certainly not looking at a
13553                  member-declarator.  Calling `grokfield' has
13554                  side-effects, so we must not do it unless we are sure
13555                  that we are looking at a member-declarator.  */
13556               if (cp_parser_token_starts_function_definition_p
13557                   (cp_lexer_peek_token (parser->lexer)))
13558                 {
13559                   /* The grammar does not allow a pure-specifier to be
13560                      used when a member function is defined.  (It is
13561                      possible that this fact is an oversight in the
13562                      standard, since a pure function may be defined
13563                      outside of the class-specifier.  */
13564                   if (initializer)
13565                     error ("pure-specifier on function-definition");
13566                   decl = cp_parser_save_member_function_body (parser,
13567                                                               &decl_specifiers,
13568                                                               declarator,
13569                                                               attributes);
13570                   /* If the member was not a friend, declare it here.  */
13571                   if (!friend_p)
13572                     finish_member_declaration (decl);
13573                   /* Peek at the next token.  */
13574                   token = cp_lexer_peek_token (parser->lexer);
13575                   /* If the next token is a semicolon, consume it.  */
13576                   if (token->type == CPP_SEMICOLON)
13577                     cp_lexer_consume_token (parser->lexer);
13578                   return;
13579                 }
13580               else
13581                 {
13582                   /* Create the declaration.  */
13583                   decl = grokfield (declarator, &decl_specifiers,
13584                                     initializer, asm_specification,
13585                                     attributes);
13586                   /* Any initialization must have been from a
13587                      constant-expression.  */
13588                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13589                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13590                 }
13591             }
13592
13593           /* Reset PREFIX_ATTRIBUTES.  */
13594           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13595             attributes = TREE_CHAIN (attributes);
13596           if (attributes)
13597             TREE_CHAIN (attributes) = NULL_TREE;
13598
13599           /* If there is any qualification still in effect, clear it
13600              now; we will be starting fresh with the next declarator.  */
13601           parser->scope = NULL_TREE;
13602           parser->qualifying_scope = NULL_TREE;
13603           parser->object_scope = NULL_TREE;
13604           /* If it's a `,', then there are more declarators.  */
13605           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13606             cp_lexer_consume_token (parser->lexer);
13607           /* If the next token isn't a `;', then we have a parse error.  */
13608           else if (cp_lexer_next_token_is_not (parser->lexer,
13609                                                CPP_SEMICOLON))
13610             {
13611               cp_parser_error (parser, "expected %<;%>");
13612               /* Skip tokens until we find a `;'.  */
13613               cp_parser_skip_to_end_of_statement (parser);
13614
13615               break;
13616             }
13617
13618           if (decl)
13619             {
13620               /* Add DECL to the list of members.  */
13621               if (!friend_p)
13622                 finish_member_declaration (decl);
13623
13624               if (TREE_CODE (decl) == FUNCTION_DECL)
13625                 cp_parser_save_default_args (parser, decl);
13626             }
13627         }
13628     }
13629
13630   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13631 }
13632
13633 /* Parse a pure-specifier.
13634
13635    pure-specifier:
13636      = 0
13637
13638    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13639    Otherwise, ERROR_MARK_NODE is returned.  */
13640
13641 static tree
13642 cp_parser_pure_specifier (cp_parser* parser)
13643 {
13644   cp_token *token;
13645
13646   /* Look for the `=' token.  */
13647   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13648     return error_mark_node;
13649   /* Look for the `0' token.  */
13650   token = cp_lexer_consume_token (parser->lexer);
13651   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
13652   if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13653     return integer_zero_node;
13654
13655   cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13656   cp_parser_skip_to_end_of_statement (parser);
13657   return error_mark_node;
13658 }
13659
13660 /* Parse a constant-initializer.
13661
13662    constant-initializer:
13663      = constant-expression
13664
13665    Returns a representation of the constant-expression.  */
13666
13667 static tree
13668 cp_parser_constant_initializer (cp_parser* parser)
13669 {
13670   /* Look for the `=' token.  */
13671   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13672     return error_mark_node;
13673
13674   /* It is invalid to write:
13675
13676        struct S { static const int i = { 7 }; };
13677
13678      */
13679   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13680     {
13681       cp_parser_error (parser,
13682                        "a brace-enclosed initializer is not allowed here");
13683       /* Consume the opening brace.  */
13684       cp_lexer_consume_token (parser->lexer);
13685       /* Skip the initializer.  */
13686       cp_parser_skip_to_closing_brace (parser);
13687       /* Look for the trailing `}'.  */
13688       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13689
13690       return error_mark_node;
13691     }
13692
13693   return cp_parser_constant_expression (parser,
13694                                         /*allow_non_constant=*/false,
13695                                         NULL);
13696 }
13697
13698 /* Derived classes [gram.class.derived] */
13699
13700 /* Parse a base-clause.
13701
13702    base-clause:
13703      : base-specifier-list
13704
13705    base-specifier-list:
13706      base-specifier
13707      base-specifier-list , base-specifier
13708
13709    Returns a TREE_LIST representing the base-classes, in the order in
13710    which they were declared.  The representation of each node is as
13711    described by cp_parser_base_specifier.
13712
13713    In the case that no bases are specified, this function will return
13714    NULL_TREE, not ERROR_MARK_NODE.  */
13715
13716 static tree
13717 cp_parser_base_clause (cp_parser* parser)
13718 {
13719   tree bases = NULL_TREE;
13720
13721   /* Look for the `:' that begins the list.  */
13722   cp_parser_require (parser, CPP_COLON, "`:'");
13723
13724   /* Scan the base-specifier-list.  */
13725   while (true)
13726     {
13727       cp_token *token;
13728       tree base;
13729
13730       /* Look for the base-specifier.  */
13731       base = cp_parser_base_specifier (parser);
13732       /* Add BASE to the front of the list.  */
13733       if (base != error_mark_node)
13734         {
13735           TREE_CHAIN (base) = bases;
13736           bases = base;
13737         }
13738       /* Peek at the next token.  */
13739       token = cp_lexer_peek_token (parser->lexer);
13740       /* If it's not a comma, then the list is complete.  */
13741       if (token->type != CPP_COMMA)
13742         break;
13743       /* Consume the `,'.  */
13744       cp_lexer_consume_token (parser->lexer);
13745     }
13746
13747   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13748      base class had a qualified name.  However, the next name that
13749      appears is certainly not qualified.  */
13750   parser->scope = NULL_TREE;
13751   parser->qualifying_scope = NULL_TREE;
13752   parser->object_scope = NULL_TREE;
13753
13754   return nreverse (bases);
13755 }
13756
13757 /* Parse a base-specifier.
13758
13759    base-specifier:
13760      :: [opt] nested-name-specifier [opt] class-name
13761      virtual access-specifier [opt] :: [opt] nested-name-specifier
13762        [opt] class-name
13763      access-specifier virtual [opt] :: [opt] nested-name-specifier
13764        [opt] class-name
13765
13766    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13767    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13768    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13769    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13770
13771 static tree
13772 cp_parser_base_specifier (cp_parser* parser)
13773 {
13774   cp_token *token;
13775   bool done = false;
13776   bool virtual_p = false;
13777   bool duplicate_virtual_error_issued_p = false;
13778   bool duplicate_access_error_issued_p = false;
13779   bool class_scope_p, template_p;
13780   tree access = access_default_node;
13781   tree type;
13782
13783   /* Process the optional `virtual' and `access-specifier'.  */
13784   while (!done)
13785     {
13786       /* Peek at the next token.  */
13787       token = cp_lexer_peek_token (parser->lexer);
13788       /* Process `virtual'.  */
13789       switch (token->keyword)
13790         {
13791         case RID_VIRTUAL:
13792           /* If `virtual' appears more than once, issue an error.  */
13793           if (virtual_p && !duplicate_virtual_error_issued_p)
13794             {
13795               cp_parser_error (parser,
13796                                "%<virtual%> specified more than once in base-specified");
13797               duplicate_virtual_error_issued_p = true;
13798             }
13799
13800           virtual_p = true;
13801
13802           /* Consume the `virtual' token.  */
13803           cp_lexer_consume_token (parser->lexer);
13804
13805           break;
13806
13807         case RID_PUBLIC:
13808         case RID_PROTECTED:
13809         case RID_PRIVATE:
13810           /* If more than one access specifier appears, issue an
13811              error.  */
13812           if (access != access_default_node
13813               && !duplicate_access_error_issued_p)
13814             {
13815               cp_parser_error (parser,
13816                                "more than one access specifier in base-specified");
13817               duplicate_access_error_issued_p = true;
13818             }
13819
13820           access = ridpointers[(int) token->keyword];
13821
13822           /* Consume the access-specifier.  */
13823           cp_lexer_consume_token (parser->lexer);
13824
13825           break;
13826
13827         default:
13828           done = true;
13829           break;
13830         }
13831     }
13832   /* It is not uncommon to see programs mechanically, erroneously, use
13833      the 'typename' keyword to denote (dependent) qualified types
13834      as base classes.  */
13835   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13836     {
13837       if (!processing_template_decl)
13838         error ("keyword %<typename%> not allowed outside of templates");
13839       else
13840         error ("keyword %<typename%> not allowed in this context "
13841                "(the base class is implicitly a type)");
13842       cp_lexer_consume_token (parser->lexer);
13843     }
13844
13845   /* Look for the optional `::' operator.  */
13846   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13847   /* Look for the nested-name-specifier.  The simplest way to
13848      implement:
13849
13850        [temp.res]
13851
13852        The keyword `typename' is not permitted in a base-specifier or
13853        mem-initializer; in these contexts a qualified name that
13854        depends on a template-parameter is implicitly assumed to be a
13855        type name.
13856
13857      is to pretend that we have seen the `typename' keyword at this
13858      point.  */
13859   cp_parser_nested_name_specifier_opt (parser,
13860                                        /*typename_keyword_p=*/true,
13861                                        /*check_dependency_p=*/true,
13862                                        typename_type,
13863                                        /*is_declaration=*/true);
13864   /* If the base class is given by a qualified name, assume that names
13865      we see are type names or templates, as appropriate.  */
13866   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13867   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13868
13869   /* Finally, look for the class-name.  */
13870   type = cp_parser_class_name (parser,
13871                                class_scope_p,
13872                                template_p,
13873                                typename_type,
13874                                /*check_dependency_p=*/true,
13875                                /*class_head_p=*/false,
13876                                /*is_declaration=*/true);
13877
13878   if (type == error_mark_node)
13879     return error_mark_node;
13880
13881   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13882 }
13883
13884 /* Exception handling [gram.exception] */
13885
13886 /* Parse an (optional) exception-specification.
13887
13888    exception-specification:
13889      throw ( type-id-list [opt] )
13890
13891    Returns a TREE_LIST representing the exception-specification.  The
13892    TREE_VALUE of each node is a type.  */
13893
13894 static tree
13895 cp_parser_exception_specification_opt (cp_parser* parser)
13896 {
13897   cp_token *token;
13898   tree type_id_list;
13899
13900   /* Peek at the next token.  */
13901   token = cp_lexer_peek_token (parser->lexer);
13902   /* If it's not `throw', then there's no exception-specification.  */
13903   if (!cp_parser_is_keyword (token, RID_THROW))
13904     return NULL_TREE;
13905
13906   /* Consume the `throw'.  */
13907   cp_lexer_consume_token (parser->lexer);
13908
13909   /* Look for the `('.  */
13910   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13911
13912   /* Peek at the next token.  */
13913   token = cp_lexer_peek_token (parser->lexer);
13914   /* If it's not a `)', then there is a type-id-list.  */
13915   if (token->type != CPP_CLOSE_PAREN)
13916     {
13917       const char *saved_message;
13918
13919       /* Types may not be defined in an exception-specification.  */
13920       saved_message = parser->type_definition_forbidden_message;
13921       parser->type_definition_forbidden_message
13922         = "types may not be defined in an exception-specification";
13923       /* Parse the type-id-list.  */
13924       type_id_list = cp_parser_type_id_list (parser);
13925       /* Restore the saved message.  */
13926       parser->type_definition_forbidden_message = saved_message;
13927     }
13928   else
13929     type_id_list = empty_except_spec;
13930
13931   /* Look for the `)'.  */
13932   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13933
13934   return type_id_list;
13935 }
13936
13937 /* Parse an (optional) type-id-list.
13938
13939    type-id-list:
13940      type-id
13941      type-id-list , type-id
13942
13943    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13944    in the order that the types were presented.  */
13945
13946 static tree
13947 cp_parser_type_id_list (cp_parser* parser)
13948 {
13949   tree types = NULL_TREE;
13950
13951   while (true)
13952     {
13953       cp_token *token;
13954       tree type;
13955
13956       /* Get the next type-id.  */
13957       type = cp_parser_type_id (parser);
13958       /* Add it to the list.  */
13959       types = add_exception_specifier (types, type, /*complain=*/1);
13960       /* Peek at the next token.  */
13961       token = cp_lexer_peek_token (parser->lexer);
13962       /* If it is not a `,', we are done.  */
13963       if (token->type != CPP_COMMA)
13964         break;
13965       /* Consume the `,'.  */
13966       cp_lexer_consume_token (parser->lexer);
13967     }
13968
13969   return nreverse (types);
13970 }
13971
13972 /* Parse a try-block.
13973
13974    try-block:
13975      try compound-statement handler-seq  */
13976
13977 static tree
13978 cp_parser_try_block (cp_parser* parser)
13979 {
13980   tree try_block;
13981
13982   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13983   try_block = begin_try_block ();
13984   cp_parser_compound_statement (parser, NULL, true);
13985   finish_try_block (try_block);
13986   cp_parser_handler_seq (parser);
13987   finish_handler_sequence (try_block);
13988
13989   return try_block;
13990 }
13991
13992 /* Parse a function-try-block.
13993
13994    function-try-block:
13995      try ctor-initializer [opt] function-body handler-seq  */
13996
13997 static bool
13998 cp_parser_function_try_block (cp_parser* parser)
13999 {
14000   tree try_block;
14001   bool ctor_initializer_p;
14002
14003   /* Look for the `try' keyword.  */
14004   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14005     return false;
14006   /* Let the rest of the front-end know where we are.  */
14007   try_block = begin_function_try_block ();
14008   /* Parse the function-body.  */
14009   ctor_initializer_p
14010     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14011   /* We're done with the `try' part.  */
14012   finish_function_try_block (try_block);
14013   /* Parse the handlers.  */
14014   cp_parser_handler_seq (parser);
14015   /* We're done with the handlers.  */
14016   finish_function_handler_sequence (try_block);
14017
14018   return ctor_initializer_p;
14019 }
14020
14021 /* Parse a handler-seq.
14022
14023    handler-seq:
14024      handler handler-seq [opt]  */
14025
14026 static void
14027 cp_parser_handler_seq (cp_parser* parser)
14028 {
14029   while (true)
14030     {
14031       cp_token *token;
14032
14033       /* Parse the handler.  */
14034       cp_parser_handler (parser);
14035       /* Peek at the next token.  */
14036       token = cp_lexer_peek_token (parser->lexer);
14037       /* If it's not `catch' then there are no more handlers.  */
14038       if (!cp_parser_is_keyword (token, RID_CATCH))
14039         break;
14040     }
14041 }
14042
14043 /* Parse a handler.
14044
14045    handler:
14046      catch ( exception-declaration ) compound-statement  */
14047
14048 static void
14049 cp_parser_handler (cp_parser* parser)
14050 {
14051   tree handler;
14052   tree declaration;
14053
14054   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14055   handler = begin_handler ();
14056   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14057   declaration = cp_parser_exception_declaration (parser);
14058   finish_handler_parms (declaration, handler);
14059   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14060   cp_parser_compound_statement (parser, NULL, false);
14061   finish_handler (handler);
14062 }
14063
14064 /* Parse an exception-declaration.
14065
14066    exception-declaration:
14067      type-specifier-seq declarator
14068      type-specifier-seq abstract-declarator
14069      type-specifier-seq
14070      ...
14071
14072    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14073    ellipsis variant is used.  */
14074
14075 static tree
14076 cp_parser_exception_declaration (cp_parser* parser)
14077 {
14078   tree decl;
14079   cp_decl_specifier_seq type_specifiers;
14080   cp_declarator *declarator;
14081   const char *saved_message;
14082
14083   /* If it's an ellipsis, it's easy to handle.  */
14084   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14085     {
14086       /* Consume the `...' token.  */
14087       cp_lexer_consume_token (parser->lexer);
14088       return NULL_TREE;
14089     }
14090
14091   /* Types may not be defined in exception-declarations.  */
14092   saved_message = parser->type_definition_forbidden_message;
14093   parser->type_definition_forbidden_message
14094     = "types may not be defined in exception-declarations";
14095
14096   /* Parse the type-specifier-seq.  */
14097   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14098                                 &type_specifiers);
14099   /* If it's a `)', then there is no declarator.  */
14100   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14101     declarator = NULL;
14102   else
14103     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14104                                        /*ctor_dtor_or_conv_p=*/NULL,
14105                                        /*parenthesized_p=*/NULL,
14106                                        /*member_p=*/false);
14107
14108   /* Restore the saved message.  */
14109   parser->type_definition_forbidden_message = saved_message;
14110
14111   if (type_specifiers.any_specifiers_p)
14112     {
14113       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14114       if (decl == NULL_TREE)
14115         error ("invalid catch parameter");
14116     }
14117   else
14118     decl = NULL_TREE;
14119
14120   return decl;
14121 }
14122
14123 /* Parse a throw-expression.
14124
14125    throw-expression:
14126      throw assignment-expression [opt]
14127
14128    Returns a THROW_EXPR representing the throw-expression.  */
14129
14130 static tree
14131 cp_parser_throw_expression (cp_parser* parser)
14132 {
14133   tree expression;
14134   cp_token* token;
14135
14136   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14137   token = cp_lexer_peek_token (parser->lexer);
14138   /* Figure out whether or not there is an assignment-expression
14139      following the "throw" keyword.  */
14140   if (token->type == CPP_COMMA
14141       || token->type == CPP_SEMICOLON
14142       || token->type == CPP_CLOSE_PAREN
14143       || token->type == CPP_CLOSE_SQUARE
14144       || token->type == CPP_CLOSE_BRACE
14145       || token->type == CPP_COLON)
14146     expression = NULL_TREE;
14147   else
14148     expression = cp_parser_assignment_expression (parser,
14149                                                   /*cast_p=*/false);
14150
14151   return build_throw (expression);
14152 }
14153
14154 /* GNU Extensions */
14155
14156 /* Parse an (optional) asm-specification.
14157
14158    asm-specification:
14159      asm ( string-literal )
14160
14161    If the asm-specification is present, returns a STRING_CST
14162    corresponding to the string-literal.  Otherwise, returns
14163    NULL_TREE.  */
14164
14165 static tree
14166 cp_parser_asm_specification_opt (cp_parser* parser)
14167 {
14168   cp_token *token;
14169   tree asm_specification;
14170
14171   /* Peek at the next token.  */
14172   token = cp_lexer_peek_token (parser->lexer);
14173   /* If the next token isn't the `asm' keyword, then there's no
14174      asm-specification.  */
14175   if (!cp_parser_is_keyword (token, RID_ASM))
14176     return NULL_TREE;
14177
14178   /* Consume the `asm' token.  */
14179   cp_lexer_consume_token (parser->lexer);
14180   /* Look for the `('.  */
14181   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14182
14183   /* Look for the string-literal.  */
14184   asm_specification = cp_parser_string_literal (parser, false, false);
14185
14186   /* Look for the `)'.  */
14187   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14188
14189   return asm_specification;
14190 }
14191
14192 /* Parse an asm-operand-list.
14193
14194    asm-operand-list:
14195      asm-operand
14196      asm-operand-list , asm-operand
14197
14198    asm-operand:
14199      string-literal ( expression )
14200      [ string-literal ] string-literal ( expression )
14201
14202    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14203    each node is the expression.  The TREE_PURPOSE is itself a
14204    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14205    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14206    is a STRING_CST for the string literal before the parenthesis.  */
14207
14208 static tree
14209 cp_parser_asm_operand_list (cp_parser* parser)
14210 {
14211   tree asm_operands = NULL_TREE;
14212
14213   while (true)
14214     {
14215       tree string_literal;
14216       tree expression;
14217       tree name;
14218
14219       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14220         {
14221           /* Consume the `[' token.  */
14222           cp_lexer_consume_token (parser->lexer);
14223           /* Read the operand name.  */
14224           name = cp_parser_identifier (parser);
14225           if (name != error_mark_node)
14226             name = build_string (IDENTIFIER_LENGTH (name),
14227                                  IDENTIFIER_POINTER (name));
14228           /* Look for the closing `]'.  */
14229           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14230         }
14231       else
14232         name = NULL_TREE;
14233       /* Look for the string-literal.  */
14234       string_literal = cp_parser_string_literal (parser, false, false);
14235
14236       /* Look for the `('.  */
14237       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14238       /* Parse the expression.  */
14239       expression = cp_parser_expression (parser, /*cast_p=*/false);
14240       /* Look for the `)'.  */
14241       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14242
14243       /* Add this operand to the list.  */
14244       asm_operands = tree_cons (build_tree_list (name, string_literal),
14245                                 expression,
14246                                 asm_operands);
14247       /* If the next token is not a `,', there are no more
14248          operands.  */
14249       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14250         break;
14251       /* Consume the `,'.  */
14252       cp_lexer_consume_token (parser->lexer);
14253     }
14254
14255   return nreverse (asm_operands);
14256 }
14257
14258 /* Parse an asm-clobber-list.
14259
14260    asm-clobber-list:
14261      string-literal
14262      asm-clobber-list , string-literal
14263
14264    Returns a TREE_LIST, indicating the clobbers in the order that they
14265    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14266
14267 static tree
14268 cp_parser_asm_clobber_list (cp_parser* parser)
14269 {
14270   tree clobbers = NULL_TREE;
14271
14272   while (true)
14273     {
14274       tree string_literal;
14275
14276       /* Look for the string literal.  */
14277       string_literal = cp_parser_string_literal (parser, false, false);
14278       /* Add it to the list.  */
14279       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14280       /* If the next token is not a `,', then the list is
14281          complete.  */
14282       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14283         break;
14284       /* Consume the `,' token.  */
14285       cp_lexer_consume_token (parser->lexer);
14286     }
14287
14288   return clobbers;
14289 }
14290
14291 /* Parse an (optional) series of attributes.
14292
14293    attributes:
14294      attributes attribute
14295
14296    attribute:
14297      __attribute__ (( attribute-list [opt] ))
14298
14299    The return value is as for cp_parser_attribute_list.  */
14300
14301 static tree
14302 cp_parser_attributes_opt (cp_parser* parser)
14303 {
14304   tree attributes = NULL_TREE;
14305
14306   while (true)
14307     {
14308       cp_token *token;
14309       tree attribute_list;
14310
14311       /* Peek at the next token.  */
14312       token = cp_lexer_peek_token (parser->lexer);
14313       /* If it's not `__attribute__', then we're done.  */
14314       if (token->keyword != RID_ATTRIBUTE)
14315         break;
14316
14317       /* Consume the `__attribute__' keyword.  */
14318       cp_lexer_consume_token (parser->lexer);
14319       /* Look for the two `(' tokens.  */
14320       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14321       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14322
14323       /* Peek at the next token.  */
14324       token = cp_lexer_peek_token (parser->lexer);
14325       if (token->type != CPP_CLOSE_PAREN)
14326         /* Parse the attribute-list.  */
14327         attribute_list = cp_parser_attribute_list (parser);
14328       else
14329         /* If the next token is a `)', then there is no attribute
14330            list.  */
14331         attribute_list = NULL;
14332
14333       /* Look for the two `)' tokens.  */
14334       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14335       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14336
14337       /* Add these new attributes to the list.  */
14338       attributes = chainon (attributes, attribute_list);
14339     }
14340
14341   return attributes;
14342 }
14343
14344 /* Parse an attribute-list.
14345
14346    attribute-list:
14347      attribute
14348      attribute-list , attribute
14349
14350    attribute:
14351      identifier
14352      identifier ( identifier )
14353      identifier ( identifier , expression-list )
14354      identifier ( expression-list )
14355
14356    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14357    to an attribute.  The TREE_PURPOSE of each node is the identifier
14358    indicating which attribute is in use.  The TREE_VALUE represents
14359    the arguments, if any.  */
14360
14361 static tree
14362 cp_parser_attribute_list (cp_parser* parser)
14363 {
14364   tree attribute_list = NULL_TREE;
14365   bool save_translate_strings_p = parser->translate_strings_p;
14366
14367   parser->translate_strings_p = false;
14368   while (true)
14369     {
14370       cp_token *token;
14371       tree identifier;
14372       tree attribute;
14373
14374       /* Look for the identifier.  We also allow keywords here; for
14375          example `__attribute__ ((const))' is legal.  */
14376       token = cp_lexer_peek_token (parser->lexer);
14377       if (token->type == CPP_NAME
14378           || token->type == CPP_KEYWORD)
14379         {
14380           /* Consume the token.  */
14381           token = cp_lexer_consume_token (parser->lexer);
14382
14383           /* Save away the identifier that indicates which attribute
14384              this is.  */
14385           identifier = token->value;
14386           attribute = build_tree_list (identifier, NULL_TREE);
14387
14388           /* Peek at the next token.  */
14389           token = cp_lexer_peek_token (parser->lexer);
14390           /* If it's an `(', then parse the attribute arguments.  */
14391           if (token->type == CPP_OPEN_PAREN)
14392             {
14393               tree arguments;
14394
14395               arguments = (cp_parser_parenthesized_expression_list
14396                            (parser, true, /*cast_p=*/false,
14397                             /*non_constant_p=*/NULL));
14398               /* Save the identifier and arguments away.  */
14399               TREE_VALUE (attribute) = arguments;
14400             }
14401
14402           /* Add this attribute to the list.  */
14403           TREE_CHAIN (attribute) = attribute_list;
14404           attribute_list = attribute;
14405
14406           token = cp_lexer_peek_token (parser->lexer);
14407         }
14408       /* Now, look for more attributes.  If the next token isn't a
14409          `,', we're done.  */
14410       if (token->type != CPP_COMMA)
14411         break;
14412
14413       /* Consume the comma and keep going.  */
14414       cp_lexer_consume_token (parser->lexer);
14415     }
14416   parser->translate_strings_p = save_translate_strings_p;
14417
14418   /* We built up the list in reverse order.  */
14419   return nreverse (attribute_list);
14420 }
14421
14422 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14423    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14424    current value of the PEDANTIC flag, regardless of whether or not
14425    the `__extension__' keyword is present.  The caller is responsible
14426    for restoring the value of the PEDANTIC flag.  */
14427
14428 static bool
14429 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14430 {
14431   /* Save the old value of the PEDANTIC flag.  */
14432   *saved_pedantic = pedantic;
14433
14434   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14435     {
14436       /* Consume the `__extension__' token.  */
14437       cp_lexer_consume_token (parser->lexer);
14438       /* We're not being pedantic while the `__extension__' keyword is
14439          in effect.  */
14440       pedantic = 0;
14441
14442       return true;
14443     }
14444
14445   return false;
14446 }
14447
14448 /* Parse a label declaration.
14449
14450    label-declaration:
14451      __label__ label-declarator-seq ;
14452
14453    label-declarator-seq:
14454      identifier , label-declarator-seq
14455      identifier  */
14456
14457 static void
14458 cp_parser_label_declaration (cp_parser* parser)
14459 {
14460   /* Look for the `__label__' keyword.  */
14461   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14462
14463   while (true)
14464     {
14465       tree identifier;
14466
14467       /* Look for an identifier.  */
14468       identifier = cp_parser_identifier (parser);
14469       /* If we failed, stop.  */
14470       if (identifier == error_mark_node)
14471         break;
14472       /* Declare it as a label.  */
14473       finish_label_decl (identifier);
14474       /* If the next token is a `;', stop.  */
14475       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14476         break;
14477       /* Look for the `,' separating the label declarations.  */
14478       cp_parser_require (parser, CPP_COMMA, "`,'");
14479     }
14480
14481   /* Look for the final `;'.  */
14482   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14483 }
14484
14485 /* Support Functions */
14486
14487 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14488    NAME should have one of the representations used for an
14489    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14490    is returned.  If PARSER->SCOPE is a dependent type, then a
14491    SCOPE_REF is returned.
14492
14493    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14494    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14495    was formed.  Abstractly, such entities should not be passed to this
14496    function, because they do not need to be looked up, but it is
14497    simpler to check for this special case here, rather than at the
14498    call-sites.
14499
14500    In cases not explicitly covered above, this function returns a
14501    DECL, OVERLOAD, or baselink representing the result of the lookup.
14502    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14503    is returned.
14504
14505    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14506    (e.g., "struct") that was used.  In that case bindings that do not
14507    refer to types are ignored.
14508
14509    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14510    ignored.
14511
14512    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14513    are ignored.
14514
14515    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14516    types.
14517
14518    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14519    TREE_LIST of candidates if name-lookup results in an ambiguity, and
14520    NULL_TREE otherwise.  */ 
14521
14522 static tree
14523 cp_parser_lookup_name (cp_parser *parser, tree name,
14524                        enum tag_types tag_type,
14525                        bool is_template, 
14526                        bool is_namespace,
14527                        bool check_dependency,
14528                        tree *ambiguous_decls)
14529 {
14530   int flags = 0;
14531   tree decl;
14532   tree object_type = parser->context->object_type;
14533
14534   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14535     flags |= LOOKUP_COMPLAIN;
14536
14537   /* Assume that the lookup will be unambiguous.  */
14538   if (ambiguous_decls)
14539     *ambiguous_decls = NULL_TREE;
14540
14541   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14542      no longer valid.  Note that if we are parsing tentatively, and
14543      the parse fails, OBJECT_TYPE will be automatically restored.  */
14544   parser->context->object_type = NULL_TREE;
14545
14546   if (name == error_mark_node)
14547     return error_mark_node;
14548
14549   /* A template-id has already been resolved; there is no lookup to
14550      do.  */
14551   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14552     return name;
14553   if (BASELINK_P (name))
14554     {
14555       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14556                   == TEMPLATE_ID_EXPR);
14557       return name;
14558     }
14559
14560   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14561      it should already have been checked to make sure that the name
14562      used matches the type being destroyed.  */
14563   if (TREE_CODE (name) == BIT_NOT_EXPR)
14564     {
14565       tree type;
14566
14567       /* Figure out to which type this destructor applies.  */
14568       if (parser->scope)
14569         type = parser->scope;
14570       else if (object_type)
14571         type = object_type;
14572       else
14573         type = current_class_type;
14574       /* If that's not a class type, there is no destructor.  */
14575       if (!type || !CLASS_TYPE_P (type))
14576         return error_mark_node;
14577       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14578         lazily_declare_fn (sfk_destructor, type);
14579       if (!CLASSTYPE_DESTRUCTORS (type))
14580           return error_mark_node;
14581       /* If it was a class type, return the destructor.  */
14582       return CLASSTYPE_DESTRUCTORS (type);
14583     }
14584
14585   /* By this point, the NAME should be an ordinary identifier.  If
14586      the id-expression was a qualified name, the qualifying scope is
14587      stored in PARSER->SCOPE at this point.  */
14588   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14589
14590   /* Perform the lookup.  */
14591   if (parser->scope)
14592     {
14593       bool dependent_p;
14594
14595       if (parser->scope == error_mark_node)
14596         return error_mark_node;
14597
14598       /* If the SCOPE is dependent, the lookup must be deferred until
14599          the template is instantiated -- unless we are explicitly
14600          looking up names in uninstantiated templates.  Even then, we
14601          cannot look up the name if the scope is not a class type; it
14602          might, for example, be a template type parameter.  */
14603       dependent_p = (TYPE_P (parser->scope)
14604                      && !(parser->in_declarator_p
14605                           && currently_open_class (parser->scope))
14606                      && dependent_type_p (parser->scope));
14607       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14608            && dependent_p)
14609         {
14610           if (tag_type)
14611             {
14612               tree type;
14613
14614               /* The resolution to Core Issue 180 says that `struct
14615                  A::B' should be considered a type-name, even if `A'
14616                  is dependent.  */
14617               type = make_typename_type (parser->scope, name, tag_type,
14618                                          /*complain=*/tf_error);
14619               decl = TYPE_NAME (type);
14620             }
14621           else if (is_template
14622                    && (cp_parser_next_token_ends_template_argument_p (parser)
14623                        || cp_lexer_next_token_is (parser->lexer,
14624                                                   CPP_CLOSE_PAREN)))
14625             decl = make_unbound_class_template (parser->scope,
14626                                                 name, NULL_TREE,
14627                                                 /*complain=*/tf_error);
14628           else
14629             decl = build_qualified_name (/*type=*/NULL_TREE,
14630                                          parser->scope, name,
14631                                          is_template);
14632         }
14633       else
14634         {
14635           tree pushed_scope = NULL_TREE;
14636
14637           /* If PARSER->SCOPE is a dependent type, then it must be a
14638              class type, and we must not be checking dependencies;
14639              otherwise, we would have processed this lookup above.  So
14640              that PARSER->SCOPE is not considered a dependent base by
14641              lookup_member, we must enter the scope here.  */
14642           if (dependent_p)
14643             pushed_scope = push_scope (parser->scope);
14644           /* If the PARSER->SCOPE is a template specialization, it
14645              may be instantiated during name lookup.  In that case,
14646              errors may be issued.  Even if we rollback the current
14647              tentative parse, those errors are valid.  */
14648           decl = lookup_qualified_name (parser->scope, name,
14649                                         tag_type != none_type,
14650                                         /*complain=*/true);
14651           if (pushed_scope)
14652             pop_scope (pushed_scope);
14653         }
14654       parser->qualifying_scope = parser->scope;
14655       parser->object_scope = NULL_TREE;
14656     }
14657   else if (object_type)
14658     {
14659       tree object_decl = NULL_TREE;
14660       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14661          OBJECT_TYPE is not a class.  */
14662       if (CLASS_TYPE_P (object_type))
14663         /* If the OBJECT_TYPE is a template specialization, it may
14664            be instantiated during name lookup.  In that case, errors
14665            may be issued.  Even if we rollback the current tentative
14666            parse, those errors are valid.  */
14667         object_decl = lookup_member (object_type,
14668                                      name,
14669                                      /*protect=*/0,
14670                                      tag_type != none_type);
14671       /* Look it up in the enclosing context, too.  */
14672       decl = lookup_name_real (name, tag_type != none_type,
14673                                /*nonclass=*/0,
14674                                /*block_p=*/true, is_namespace, flags);
14675       parser->object_scope = object_type;
14676       parser->qualifying_scope = NULL_TREE;
14677       if (object_decl)
14678         decl = object_decl;
14679     }
14680   else
14681     {
14682       decl = lookup_name_real (name, tag_type != none_type,
14683                                /*nonclass=*/0,
14684                                /*block_p=*/true, is_namespace, flags);
14685       parser->qualifying_scope = NULL_TREE;
14686       parser->object_scope = NULL_TREE;
14687     }
14688
14689   /* If the lookup failed, let our caller know.  */
14690   if (!decl || decl == error_mark_node)
14691     return error_mark_node;
14692
14693   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14694   if (TREE_CODE (decl) == TREE_LIST)
14695     {
14696       if (ambiguous_decls)
14697         *ambiguous_decls = decl;
14698       /* The error message we have to print is too complicated for
14699          cp_parser_error, so we incorporate its actions directly.  */
14700       if (!cp_parser_simulate_error (parser))
14701         {
14702           error ("reference to %qD is ambiguous", name);
14703           print_candidates (decl);
14704         }
14705       return error_mark_node;
14706     }
14707
14708   gcc_assert (DECL_P (decl)
14709               || TREE_CODE (decl) == OVERLOAD
14710               || TREE_CODE (decl) == SCOPE_REF
14711               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14712               || BASELINK_P (decl));
14713
14714   /* If we have resolved the name of a member declaration, check to
14715      see if the declaration is accessible.  When the name resolves to
14716      set of overloaded functions, accessibility is checked when
14717      overload resolution is done.
14718
14719      During an explicit instantiation, access is not checked at all,
14720      as per [temp.explicit].  */
14721   if (DECL_P (decl))
14722     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14723
14724   return decl;
14725 }
14726
14727 /* Like cp_parser_lookup_name, but for use in the typical case where
14728    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14729    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14730
14731 static tree
14732 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14733 {
14734   return cp_parser_lookup_name (parser, name,
14735                                 none_type,
14736                                 /*is_template=*/false,
14737                                 /*is_namespace=*/false,
14738                                 /*check_dependency=*/true,
14739                                 /*ambiguous_decls=*/NULL);
14740 }
14741
14742 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14743    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14744    true, the DECL indicates the class being defined in a class-head,
14745    or declared in an elaborated-type-specifier.
14746
14747    Otherwise, return DECL.  */
14748
14749 static tree
14750 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14751 {
14752   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14753      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14754
14755        struct A {
14756          template <typename T> struct B;
14757        };
14758
14759        template <typename T> struct A::B {};
14760
14761      Similarly, in an elaborated-type-specifier:
14762
14763        namespace N { struct X{}; }
14764
14765        struct A {
14766          template <typename T> friend struct N::X;
14767        };
14768
14769      However, if the DECL refers to a class type, and we are in
14770      the scope of the class, then the name lookup automatically
14771      finds the TYPE_DECL created by build_self_reference rather
14772      than a TEMPLATE_DECL.  For example, in:
14773
14774        template <class T> struct S {
14775          S s;
14776        };
14777
14778      there is no need to handle such case.  */
14779
14780   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14781     return DECL_TEMPLATE_RESULT (decl);
14782
14783   return decl;
14784 }
14785
14786 /* If too many, or too few, template-parameter lists apply to the
14787    declarator, issue an error message.  Returns TRUE if all went well,
14788    and FALSE otherwise.  */
14789
14790 static bool
14791 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14792                                                 cp_declarator *declarator)
14793 {
14794   unsigned num_templates;
14795
14796   /* We haven't seen any classes that involve template parameters yet.  */
14797   num_templates = 0;
14798
14799   switch (declarator->kind)
14800     {
14801     case cdk_id:
14802       if (declarator->u.id.qualifying_scope)
14803         {
14804           tree scope;
14805           tree member;
14806
14807           scope = declarator->u.id.qualifying_scope;
14808           member = declarator->u.id.unqualified_name;
14809
14810           while (scope && CLASS_TYPE_P (scope))
14811             {
14812               /* You're supposed to have one `template <...>'
14813                  for every template class, but you don't need one
14814                  for a full specialization.  For example:
14815
14816                  template <class T> struct S{};
14817                  template <> struct S<int> { void f(); };
14818                  void S<int>::f () {}
14819
14820                  is correct; there shouldn't be a `template <>' for
14821                  the definition of `S<int>::f'.  */
14822               if (CLASSTYPE_TEMPLATE_INFO (scope)
14823                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14824                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14825                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14826                 ++num_templates;
14827
14828               scope = TYPE_CONTEXT (scope);
14829             }
14830         }
14831       else if (TREE_CODE (declarator->u.id.unqualified_name)
14832                == TEMPLATE_ID_EXPR)
14833         /* If the DECLARATOR has the form `X<y>' then it uses one
14834            additional level of template parameters.  */
14835         ++num_templates;
14836
14837       return cp_parser_check_template_parameters (parser,
14838                                                   num_templates);
14839
14840     case cdk_function:
14841     case cdk_array:
14842     case cdk_pointer:
14843     case cdk_reference:
14844     case cdk_ptrmem:
14845       return (cp_parser_check_declarator_template_parameters
14846               (parser, declarator->declarator));
14847
14848     case cdk_error:
14849       return true;
14850
14851     default:
14852       gcc_unreachable ();
14853     }
14854   return false;
14855 }
14856
14857 /* NUM_TEMPLATES were used in the current declaration.  If that is
14858    invalid, return FALSE and issue an error messages.  Otherwise,
14859    return TRUE.  */
14860
14861 static bool
14862 cp_parser_check_template_parameters (cp_parser* parser,
14863                                      unsigned num_templates)
14864 {
14865   /* If there are more template classes than parameter lists, we have
14866      something like:
14867
14868        template <class T> void S<T>::R<T>::f ();  */
14869   if (parser->num_template_parameter_lists < num_templates)
14870     {
14871       error ("too few template-parameter-lists");
14872       return false;
14873     }
14874   /* If there are the same number of template classes and parameter
14875      lists, that's OK.  */
14876   if (parser->num_template_parameter_lists == num_templates)
14877     return true;
14878   /* If there are more, but only one more, then we are referring to a
14879      member template.  That's OK too.  */
14880   if (parser->num_template_parameter_lists == num_templates + 1)
14881       return true;
14882   /* Otherwise, there are too many template parameter lists.  We have
14883      something like:
14884
14885      template <class T> template <class U> void S::f();  */
14886   error ("too many template-parameter-lists");
14887   return false;
14888 }
14889
14890 /* Parse an optional `::' token indicating that the following name is
14891    from the global namespace.  If so, PARSER->SCOPE is set to the
14892    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14893    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14894    Returns the new value of PARSER->SCOPE, if the `::' token is
14895    present, and NULL_TREE otherwise.  */
14896
14897 static tree
14898 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14899 {
14900   cp_token *token;
14901
14902   /* Peek at the next token.  */
14903   token = cp_lexer_peek_token (parser->lexer);
14904   /* If we're looking at a `::' token then we're starting from the
14905      global namespace, not our current location.  */
14906   if (token->type == CPP_SCOPE)
14907     {
14908       /* Consume the `::' token.  */
14909       cp_lexer_consume_token (parser->lexer);
14910       /* Set the SCOPE so that we know where to start the lookup.  */
14911       parser->scope = global_namespace;
14912       parser->qualifying_scope = global_namespace;
14913       parser->object_scope = NULL_TREE;
14914
14915       return parser->scope;
14916     }
14917   else if (!current_scope_valid_p)
14918     {
14919       parser->scope = NULL_TREE;
14920       parser->qualifying_scope = NULL_TREE;
14921       parser->object_scope = NULL_TREE;
14922     }
14923
14924   return NULL_TREE;
14925 }
14926
14927 /* Returns TRUE if the upcoming token sequence is the start of a
14928    constructor declarator.  If FRIEND_P is true, the declarator is
14929    preceded by the `friend' specifier.  */
14930
14931 static bool
14932 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14933 {
14934   bool constructor_p;
14935   tree type_decl = NULL_TREE;
14936   bool nested_name_p;
14937   cp_token *next_token;
14938
14939   /* The common case is that this is not a constructor declarator, so
14940      try to avoid doing lots of work if at all possible.  It's not
14941      valid declare a constructor at function scope.  */
14942   if (at_function_scope_p ())
14943     return false;
14944   /* And only certain tokens can begin a constructor declarator.  */
14945   next_token = cp_lexer_peek_token (parser->lexer);
14946   if (next_token->type != CPP_NAME
14947       && next_token->type != CPP_SCOPE
14948       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14949       && next_token->type != CPP_TEMPLATE_ID)
14950     return false;
14951
14952   /* Parse tentatively; we are going to roll back all of the tokens
14953      consumed here.  */
14954   cp_parser_parse_tentatively (parser);
14955   /* Assume that we are looking at a constructor declarator.  */
14956   constructor_p = true;
14957
14958   /* Look for the optional `::' operator.  */
14959   cp_parser_global_scope_opt (parser,
14960                               /*current_scope_valid_p=*/false);
14961   /* Look for the nested-name-specifier.  */
14962   nested_name_p
14963     = (cp_parser_nested_name_specifier_opt (parser,
14964                                             /*typename_keyword_p=*/false,
14965                                             /*check_dependency_p=*/false,
14966                                             /*type_p=*/false,
14967                                             /*is_declaration=*/false)
14968        != NULL_TREE);
14969   /* Outside of a class-specifier, there must be a
14970      nested-name-specifier.  */
14971   if (!nested_name_p &&
14972       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14973        || friend_p))
14974     constructor_p = false;
14975   /* If we still think that this might be a constructor-declarator,
14976      look for a class-name.  */
14977   if (constructor_p)
14978     {
14979       /* If we have:
14980
14981            template <typename T> struct S { S(); };
14982            template <typename T> S<T>::S ();
14983
14984          we must recognize that the nested `S' names a class.
14985          Similarly, for:
14986
14987            template <typename T> S<T>::S<T> ();
14988
14989          we must recognize that the nested `S' names a template.  */
14990       type_decl = cp_parser_class_name (parser,
14991                                         /*typename_keyword_p=*/false,
14992                                         /*template_keyword_p=*/false,
14993                                         none_type,
14994                                         /*check_dependency_p=*/false,
14995                                         /*class_head_p=*/false,
14996                                         /*is_declaration=*/false);
14997       /* If there was no class-name, then this is not a constructor.  */
14998       constructor_p = !cp_parser_error_occurred (parser);
14999     }
15000
15001   /* If we're still considering a constructor, we have to see a `(',
15002      to begin the parameter-declaration-clause, followed by either a
15003      `)', an `...', or a decl-specifier.  We need to check for a
15004      type-specifier to avoid being fooled into thinking that:
15005
15006        S::S (f) (int);
15007
15008      is a constructor.  (It is actually a function named `f' that
15009      takes one parameter (of type `int') and returns a value of type
15010      `S::S'.  */
15011   if (constructor_p
15012       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15013     {
15014       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15015           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15016           /* A parameter declaration begins with a decl-specifier,
15017              which is either the "attribute" keyword, a storage class
15018              specifier, or (usually) a type-specifier.  */
15019           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15020           && !cp_parser_storage_class_specifier_opt (parser))
15021         {
15022           tree type;
15023           tree pushed_scope = NULL_TREE;
15024           unsigned saved_num_template_parameter_lists;
15025
15026           /* Names appearing in the type-specifier should be looked up
15027              in the scope of the class.  */
15028           if (current_class_type)
15029             type = NULL_TREE;
15030           else
15031             {
15032               type = TREE_TYPE (type_decl);
15033               if (TREE_CODE (type) == TYPENAME_TYPE)
15034                 {
15035                   type = resolve_typename_type (type,
15036                                                 /*only_current_p=*/false);
15037                   if (type == error_mark_node)
15038                     {
15039                       cp_parser_abort_tentative_parse (parser);
15040                       return false;
15041                     }
15042                 }
15043               pushed_scope = push_scope (type);
15044             }
15045
15046           /* Inside the constructor parameter list, surrounding
15047              template-parameter-lists do not apply.  */
15048           saved_num_template_parameter_lists
15049             = parser->num_template_parameter_lists;
15050           parser->num_template_parameter_lists = 0;
15051
15052           /* Look for the type-specifier.  */
15053           cp_parser_type_specifier (parser,
15054                                     CP_PARSER_FLAGS_NONE,
15055                                     /*decl_specs=*/NULL,
15056                                     /*is_declarator=*/true,
15057                                     /*declares_class_or_enum=*/NULL,
15058                                     /*is_cv_qualifier=*/NULL);
15059
15060           parser->num_template_parameter_lists
15061             = saved_num_template_parameter_lists;
15062
15063           /* Leave the scope of the class.  */
15064           if (pushed_scope)
15065             pop_scope (pushed_scope);
15066
15067           constructor_p = !cp_parser_error_occurred (parser);
15068         }
15069     }
15070   else
15071     constructor_p = false;
15072   /* We did not really want to consume any tokens.  */
15073   cp_parser_abort_tentative_parse (parser);
15074
15075   return constructor_p;
15076 }
15077
15078 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15079    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15080    they must be performed once we are in the scope of the function.
15081
15082    Returns the function defined.  */
15083
15084 static tree
15085 cp_parser_function_definition_from_specifiers_and_declarator
15086   (cp_parser* parser,
15087    cp_decl_specifier_seq *decl_specifiers,
15088    tree attributes,
15089    const cp_declarator *declarator)
15090 {
15091   tree fn;
15092   bool success_p;
15093
15094   /* Begin the function-definition.  */
15095   success_p = start_function (decl_specifiers, declarator, attributes);
15096
15097   /* The things we're about to see are not directly qualified by any
15098      template headers we've seen thus far.  */
15099   reset_specialization ();
15100
15101   /* If there were names looked up in the decl-specifier-seq that we
15102      did not check, check them now.  We must wait until we are in the
15103      scope of the function to perform the checks, since the function
15104      might be a friend.  */
15105   perform_deferred_access_checks ();
15106
15107   if (!success_p)
15108     {
15109       /* Skip the entire function.  */
15110       error ("invalid function declaration");
15111       cp_parser_skip_to_end_of_block_or_statement (parser);
15112       fn = error_mark_node;
15113     }
15114   else
15115     fn = cp_parser_function_definition_after_declarator (parser,
15116                                                          /*inline_p=*/false);
15117
15118   return fn;
15119 }
15120
15121 /* Parse the part of a function-definition that follows the
15122    declarator.  INLINE_P is TRUE iff this function is an inline
15123    function defined with a class-specifier.
15124
15125    Returns the function defined.  */
15126
15127 static tree
15128 cp_parser_function_definition_after_declarator (cp_parser* parser,
15129                                                 bool inline_p)
15130 {
15131   tree fn;
15132   bool ctor_initializer_p = false;
15133   bool saved_in_unbraced_linkage_specification_p;
15134   unsigned saved_num_template_parameter_lists;
15135
15136   /* If the next token is `return', then the code may be trying to
15137      make use of the "named return value" extension that G++ used to
15138      support.  */
15139   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15140     {
15141       /* Consume the `return' keyword.  */
15142       cp_lexer_consume_token (parser->lexer);
15143       /* Look for the identifier that indicates what value is to be
15144          returned.  */
15145       cp_parser_identifier (parser);
15146       /* Issue an error message.  */
15147       error ("named return values are no longer supported");
15148       /* Skip tokens until we reach the start of the function body.  */
15149       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15150              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15151         cp_lexer_consume_token (parser->lexer);
15152     }
15153   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15154      anything declared inside `f'.  */
15155   saved_in_unbraced_linkage_specification_p
15156     = parser->in_unbraced_linkage_specification_p;
15157   parser->in_unbraced_linkage_specification_p = false;
15158   /* Inside the function, surrounding template-parameter-lists do not
15159      apply.  */
15160   saved_num_template_parameter_lists
15161     = parser->num_template_parameter_lists;
15162   parser->num_template_parameter_lists = 0;
15163   /* If the next token is `try', then we are looking at a
15164      function-try-block.  */
15165   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15166     ctor_initializer_p = cp_parser_function_try_block (parser);
15167   /* A function-try-block includes the function-body, so we only do
15168      this next part if we're not processing a function-try-block.  */
15169   else
15170     ctor_initializer_p
15171       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15172
15173   /* Finish the function.  */
15174   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15175                         (inline_p ? 2 : 0));
15176   /* Generate code for it, if necessary.  */
15177   expand_or_defer_fn (fn);
15178   /* Restore the saved values.  */
15179   parser->in_unbraced_linkage_specification_p
15180     = saved_in_unbraced_linkage_specification_p;
15181   parser->num_template_parameter_lists
15182     = saved_num_template_parameter_lists;
15183
15184   return fn;
15185 }
15186
15187 /* Parse a template-declaration, assuming that the `export' (and
15188    `extern') keywords, if present, has already been scanned.  MEMBER_P
15189    is as for cp_parser_template_declaration.  */
15190
15191 static void
15192 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15193 {
15194   tree decl = NULL_TREE;
15195   tree parameter_list;
15196   bool friend_p = false;
15197   bool need_lang_pop;
15198
15199   /* Look for the `template' keyword.  */
15200   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15201     return;
15202
15203   /* And the `<'.  */
15204   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15205     return;
15206   /* [temp]
15207    
15208      A template ... shall not have C linkage.  */
15209   if (current_lang_name == lang_name_c)
15210     {
15211       error ("template with C linkage");
15212       /* Give it C++ linkage to avoid confusing other parts of the
15213          front end.  */
15214       push_lang_context (lang_name_cplusplus);
15215       need_lang_pop = true;
15216     }
15217   else
15218     need_lang_pop = false;
15219   /* If the next token is `>', then we have an invalid
15220      specialization.  Rather than complain about an invalid template
15221      parameter, issue an error message here.  */
15222   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15223     {
15224       cp_parser_error (parser, "invalid explicit specialization");
15225       begin_specialization ();
15226       parameter_list = NULL_TREE;
15227     }
15228   else
15229     {
15230       /* Parse the template parameters.  */
15231       begin_template_parm_list ();
15232       parameter_list = cp_parser_template_parameter_list (parser);
15233       parameter_list = end_template_parm_list (parameter_list);
15234     }
15235
15236   /* Look for the `>'.  */
15237   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15238   /* We just processed one more parameter list.  */
15239   ++parser->num_template_parameter_lists;
15240   /* If the next token is `template', there are more template
15241      parameters.  */
15242   if (cp_lexer_next_token_is_keyword (parser->lexer,
15243                                       RID_TEMPLATE))
15244     cp_parser_template_declaration_after_export (parser, member_p);
15245   else
15246     {
15247       /* There are no access checks when parsing a template, as we do not
15248          know if a specialization will be a friend.  */
15249       push_deferring_access_checks (dk_no_check);
15250
15251       decl = cp_parser_single_declaration (parser,
15252                                            member_p,
15253                                            &friend_p);
15254
15255       pop_deferring_access_checks ();
15256
15257       /* If this is a member template declaration, let the front
15258          end know.  */
15259       if (member_p && !friend_p && decl)
15260         {
15261           if (TREE_CODE (decl) == TYPE_DECL)
15262             cp_parser_check_access_in_redeclaration (decl);
15263
15264           decl = finish_member_template_decl (decl);
15265         }
15266       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15267         make_friend_class (current_class_type, TREE_TYPE (decl),
15268                            /*complain=*/true);
15269     }
15270   /* We are done with the current parameter list.  */
15271   --parser->num_template_parameter_lists;
15272
15273   /* Finish up.  */
15274   finish_template_decl (parameter_list);
15275
15276   /* Register member declarations.  */
15277   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15278     finish_member_declaration (decl);
15279   /* For the erroneous case of a template with C linkage, we pushed an
15280      implicit C++ linkage scope; exit that scope now.  */
15281   if (need_lang_pop)
15282     pop_lang_context ();
15283   /* If DECL is a function template, we must return to parse it later.
15284      (Even though there is no definition, there might be default
15285      arguments that need handling.)  */
15286   if (member_p && decl
15287       && (TREE_CODE (decl) == FUNCTION_DECL
15288           || DECL_FUNCTION_TEMPLATE_P (decl)))
15289     TREE_VALUE (parser->unparsed_functions_queues)
15290       = tree_cons (NULL_TREE, decl,
15291                    TREE_VALUE (parser->unparsed_functions_queues));
15292 }
15293
15294 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15295    `function-definition' sequence.  MEMBER_P is true, this declaration
15296    appears in a class scope.
15297
15298    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15299    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15300
15301 static tree
15302 cp_parser_single_declaration (cp_parser* parser,
15303                               bool member_p,
15304                               bool* friend_p)
15305 {
15306   int declares_class_or_enum;
15307   tree decl = NULL_TREE;
15308   cp_decl_specifier_seq decl_specifiers;
15309   bool function_definition_p = false;
15310
15311   /* This function is only used when processing a template
15312      declaration.  */
15313   gcc_assert (innermost_scope_kind () == sk_template_parms
15314               || innermost_scope_kind () == sk_template_spec);
15315
15316   /* Defer access checks until we know what is being declared.  */
15317   push_deferring_access_checks (dk_deferred);
15318
15319   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15320      alternative.  */
15321   cp_parser_decl_specifier_seq (parser,
15322                                 CP_PARSER_FLAGS_OPTIONAL,
15323                                 &decl_specifiers,
15324                                 &declares_class_or_enum);
15325   if (friend_p)
15326     *friend_p = cp_parser_friend_p (&decl_specifiers);
15327
15328   /* There are no template typedefs.  */
15329   if (decl_specifiers.specs[(int) ds_typedef])
15330     {
15331       error ("template declaration of %qs", "typedef");
15332       decl = error_mark_node;
15333     }
15334
15335   /* Gather up the access checks that occurred the
15336      decl-specifier-seq.  */
15337   stop_deferring_access_checks ();
15338
15339   /* Check for the declaration of a template class.  */
15340   if (declares_class_or_enum)
15341     {
15342       if (cp_parser_declares_only_class_p (parser))
15343         {
15344           decl = shadow_tag (&decl_specifiers);
15345
15346           /* In this case:
15347
15348                struct C {
15349                  friend template <typename T> struct A<T>::B;
15350                };
15351
15352              A<T>::B will be represented by a TYPENAME_TYPE, and
15353              therefore not recognized by shadow_tag.  */
15354           if (friend_p && *friend_p
15355               && !decl
15356               && decl_specifiers.type
15357               && TYPE_P (decl_specifiers.type))
15358             decl = decl_specifiers.type;
15359
15360           if (decl && decl != error_mark_node)
15361             decl = TYPE_NAME (decl);
15362           else
15363             decl = error_mark_node;
15364         }
15365     }
15366   /* If it's not a template class, try for a template function.  If
15367      the next token is a `;', then this declaration does not declare
15368      anything.  But, if there were errors in the decl-specifiers, then
15369      the error might well have come from an attempted class-specifier.
15370      In that case, there's no need to warn about a missing declarator.  */
15371   if (!decl
15372       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15373           || decl_specifiers.type != error_mark_node))
15374     decl = cp_parser_init_declarator (parser,
15375                                       &decl_specifiers,
15376                                       /*function_definition_allowed_p=*/true,
15377                                       member_p,
15378                                       declares_class_or_enum,
15379                                       &function_definition_p);
15380
15381   pop_deferring_access_checks ();
15382
15383   /* Clear any current qualification; whatever comes next is the start
15384      of something new.  */
15385   parser->scope = NULL_TREE;
15386   parser->qualifying_scope = NULL_TREE;
15387   parser->object_scope = NULL_TREE;
15388   /* Look for a trailing `;' after the declaration.  */
15389   if (!function_definition_p
15390       && (decl == error_mark_node
15391           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15392     cp_parser_skip_to_end_of_block_or_statement (parser);
15393
15394   return decl;
15395 }
15396
15397 /* Parse a cast-expression that is not the operand of a unary "&".  */
15398
15399 static tree
15400 cp_parser_simple_cast_expression (cp_parser *parser)
15401 {
15402   return cp_parser_cast_expression (parser, /*address_p=*/false,
15403                                     /*cast_p=*/false);
15404 }
15405
15406 /* Parse a functional cast to TYPE.  Returns an expression
15407    representing the cast.  */
15408
15409 static tree
15410 cp_parser_functional_cast (cp_parser* parser, tree type)
15411 {
15412   tree expression_list;
15413   tree cast;
15414
15415   expression_list
15416     = cp_parser_parenthesized_expression_list (parser, false,
15417                                                /*cast_p=*/true,
15418                                                /*non_constant_p=*/NULL);
15419
15420   cast = build_functional_cast (type, expression_list);
15421   /* [expr.const]/1: In an integral constant expression "only type
15422      conversions to integral or enumeration type can be used".  */
15423   if (TREE_CODE (type) == TYPE_DECL)
15424     type = TREE_TYPE (type);
15425   if (cast != error_mark_node && !dependent_type_p (type)
15426       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15427     {
15428       if (cp_parser_non_integral_constant_expression
15429           (parser, "a call to a constructor"))
15430         return error_mark_node;
15431     }
15432   return cast;
15433 }
15434
15435 /* Save the tokens that make up the body of a member function defined
15436    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15437    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15438    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15439    for the member function.  */
15440
15441 static tree
15442 cp_parser_save_member_function_body (cp_parser* parser,
15443                                      cp_decl_specifier_seq *decl_specifiers,
15444                                      cp_declarator *declarator,
15445                                      tree attributes)
15446 {
15447   cp_token *first;
15448   cp_token *last;
15449   tree fn;
15450
15451   /* Create the function-declaration.  */
15452   fn = start_method (decl_specifiers, declarator, attributes);
15453   /* If something went badly wrong, bail out now.  */
15454   if (fn == error_mark_node)
15455     {
15456       /* If there's a function-body, skip it.  */
15457       if (cp_parser_token_starts_function_definition_p
15458           (cp_lexer_peek_token (parser->lexer)))
15459         cp_parser_skip_to_end_of_block_or_statement (parser);
15460       return error_mark_node;
15461     }
15462
15463   /* Remember it, if there default args to post process.  */
15464   cp_parser_save_default_args (parser, fn);
15465
15466   /* Save away the tokens that make up the body of the
15467      function.  */
15468   first = parser->lexer->next_token;
15469   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15470   /* Handle function try blocks.  */
15471   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15472     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15473   last = parser->lexer->next_token;
15474
15475   /* Save away the inline definition; we will process it when the
15476      class is complete.  */
15477   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15478   DECL_PENDING_INLINE_P (fn) = 1;
15479
15480   /* We need to know that this was defined in the class, so that
15481      friend templates are handled correctly.  */
15482   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15483
15484   /* We're done with the inline definition.  */
15485   finish_method (fn);
15486
15487   /* Add FN to the queue of functions to be parsed later.  */
15488   TREE_VALUE (parser->unparsed_functions_queues)
15489     = tree_cons (NULL_TREE, fn,
15490                  TREE_VALUE (parser->unparsed_functions_queues));
15491
15492   return fn;
15493 }
15494
15495 /* Parse a template-argument-list, as well as the trailing ">" (but
15496    not the opening ">").  See cp_parser_template_argument_list for the
15497    return value.  */
15498
15499 static tree
15500 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15501 {
15502   tree arguments;
15503   tree saved_scope;
15504   tree saved_qualifying_scope;
15505   tree saved_object_scope;
15506   bool saved_greater_than_is_operator_p;
15507   bool saved_skip_evaluation;
15508
15509   /* [temp.names]
15510
15511      When parsing a template-id, the first non-nested `>' is taken as
15512      the end of the template-argument-list rather than a greater-than
15513      operator.  */
15514   saved_greater_than_is_operator_p
15515     = parser->greater_than_is_operator_p;
15516   parser->greater_than_is_operator_p = false;
15517   /* Parsing the argument list may modify SCOPE, so we save it
15518      here.  */
15519   saved_scope = parser->scope;
15520   saved_qualifying_scope = parser->qualifying_scope;
15521   saved_object_scope = parser->object_scope;
15522   /* We need to evaluate the template arguments, even though this
15523      template-id may be nested within a "sizeof".  */
15524   saved_skip_evaluation = skip_evaluation;
15525   skip_evaluation = false;
15526   /* Parse the template-argument-list itself.  */
15527   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15528     arguments = NULL_TREE;
15529   else
15530     arguments = cp_parser_template_argument_list (parser);
15531   /* Look for the `>' that ends the template-argument-list. If we find
15532      a '>>' instead, it's probably just a typo.  */
15533   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15534     {
15535       if (!saved_greater_than_is_operator_p)
15536         {
15537           /* If we're in a nested template argument list, the '>>' has
15538             to be a typo for '> >'. We emit the error message, but we
15539             continue parsing and we push a '>' as next token, so that
15540             the argument list will be parsed correctly.  Note that the
15541             global source location is still on the token before the
15542             '>>', so we need to say explicitly where we want it.  */
15543           cp_token *token = cp_lexer_peek_token (parser->lexer);
15544           error ("%H%<>>%> should be %<> >%> "
15545                  "within a nested template argument list",
15546                  &token->location);
15547
15548           /* ??? Proper recovery should terminate two levels of
15549              template argument list here.  */
15550           token->type = CPP_GREATER;
15551         }
15552       else
15553         {
15554           /* If this is not a nested template argument list, the '>>'
15555             is a typo for '>'. Emit an error message and continue.
15556             Same deal about the token location, but here we can get it
15557             right by consuming the '>>' before issuing the diagnostic.  */
15558           cp_lexer_consume_token (parser->lexer);
15559           error ("spurious %<>>%>, use %<>%> to terminate "
15560                  "a template argument list");
15561         }
15562     }
15563   else
15564     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15565   /* The `>' token might be a greater-than operator again now.  */
15566   parser->greater_than_is_operator_p
15567     = saved_greater_than_is_operator_p;
15568   /* Restore the SAVED_SCOPE.  */
15569   parser->scope = saved_scope;
15570   parser->qualifying_scope = saved_qualifying_scope;
15571   parser->object_scope = saved_object_scope;
15572   skip_evaluation = saved_skip_evaluation;
15573
15574   return arguments;
15575 }
15576
15577 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15578    arguments, or the body of the function have not yet been parsed,
15579    parse them now.  */
15580
15581 static void
15582 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15583 {
15584   /* If this member is a template, get the underlying
15585      FUNCTION_DECL.  */
15586   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15587     member_function = DECL_TEMPLATE_RESULT (member_function);
15588
15589   /* There should not be any class definitions in progress at this
15590      point; the bodies of members are only parsed outside of all class
15591      definitions.  */
15592   gcc_assert (parser->num_classes_being_defined == 0);
15593   /* While we're parsing the member functions we might encounter more
15594      classes.  We want to handle them right away, but we don't want
15595      them getting mixed up with functions that are currently in the
15596      queue.  */
15597   parser->unparsed_functions_queues
15598     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15599
15600   /* Make sure that any template parameters are in scope.  */
15601   maybe_begin_member_template_processing (member_function);
15602
15603   /* If the body of the function has not yet been parsed, parse it
15604      now.  */
15605   if (DECL_PENDING_INLINE_P (member_function))
15606     {
15607       tree function_scope;
15608       cp_token_cache *tokens;
15609
15610       /* The function is no longer pending; we are processing it.  */
15611       tokens = DECL_PENDING_INLINE_INFO (member_function);
15612       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15613       DECL_PENDING_INLINE_P (member_function) = 0;
15614
15615       /* If this is a local class, enter the scope of the containing
15616          function.  */
15617       function_scope = current_function_decl;
15618       if (function_scope)
15619         push_function_context_to (function_scope);
15620
15621
15622       /* Push the body of the function onto the lexer stack.  */
15623       cp_parser_push_lexer_for_tokens (parser, tokens);
15624
15625       /* Let the front end know that we going to be defining this
15626          function.  */
15627       start_preparsed_function (member_function, NULL_TREE,
15628                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15629
15630       /* Don't do access checking if it is a templated function.  */
15631       if (processing_template_decl)
15632         push_deferring_access_checks (dk_no_check);
15633
15634       /* Now, parse the body of the function.  */
15635       cp_parser_function_definition_after_declarator (parser,
15636                                                       /*inline_p=*/true);
15637
15638       if (processing_template_decl)
15639         pop_deferring_access_checks ();
15640
15641       /* Leave the scope of the containing function.  */
15642       if (function_scope)
15643         pop_function_context_from (function_scope);
15644       cp_parser_pop_lexer (parser);
15645     }
15646
15647   /* Remove any template parameters from the symbol table.  */
15648   maybe_end_member_template_processing ();
15649
15650   /* Restore the queue.  */
15651   parser->unparsed_functions_queues
15652     = TREE_CHAIN (parser->unparsed_functions_queues);
15653 }
15654
15655 /* If DECL contains any default args, remember it on the unparsed
15656    functions queue.  */
15657
15658 static void
15659 cp_parser_save_default_args (cp_parser* parser, tree decl)
15660 {
15661   tree probe;
15662
15663   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15664        probe;
15665        probe = TREE_CHAIN (probe))
15666     if (TREE_PURPOSE (probe))
15667       {
15668         TREE_PURPOSE (parser->unparsed_functions_queues)
15669           = tree_cons (current_class_type, decl,
15670                        TREE_PURPOSE (parser->unparsed_functions_queues));
15671         break;
15672       }
15673   return;
15674 }
15675
15676 /* FN is a FUNCTION_DECL which may contains a parameter with an
15677    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15678    assumes that the current scope is the scope in which the default
15679    argument should be processed.  */
15680
15681 static void
15682 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15683 {
15684   bool saved_local_variables_forbidden_p;
15685   tree parm;
15686
15687   /* While we're parsing the default args, we might (due to the
15688      statement expression extension) encounter more classes.  We want
15689      to handle them right away, but we don't want them getting mixed
15690      up with default args that are currently in the queue.  */
15691   parser->unparsed_functions_queues
15692     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15693
15694   /* Local variable names (and the `this' keyword) may not appear
15695      in a default argument.  */
15696   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15697   parser->local_variables_forbidden_p = true;
15698
15699   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15700        parm;
15701        parm = TREE_CHAIN (parm))
15702     {
15703       cp_token_cache *tokens;
15704       tree default_arg = TREE_PURPOSE (parm);
15705       tree parsed_arg;
15706       VEC(tree,gc) *insts;
15707       tree copy;
15708       unsigned ix;
15709
15710       if (!default_arg)
15711         continue;
15712
15713       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15714         /* This can happen for a friend declaration for a function
15715            already declared with default arguments.  */
15716         continue;
15717
15718        /* Push the saved tokens for the default argument onto the parser's
15719           lexer stack.  */
15720       tokens = DEFARG_TOKENS (default_arg);
15721       cp_parser_push_lexer_for_tokens (parser, tokens);
15722
15723       /* Parse the assignment-expression.  */
15724       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15725
15726       if (!processing_template_decl)
15727         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15728       
15729       TREE_PURPOSE (parm) = parsed_arg;
15730
15731       /* Update any instantiations we've already created.  */
15732       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15733            VEC_iterate (tree, insts, ix, copy); ix++)
15734         TREE_PURPOSE (copy) = parsed_arg;
15735
15736       /* If the token stream has not been completely used up, then
15737          there was extra junk after the end of the default
15738          argument.  */
15739       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15740         cp_parser_error (parser, "expected %<,%>");
15741
15742       /* Revert to the main lexer.  */
15743       cp_parser_pop_lexer (parser);
15744     }
15745
15746   /* Restore the state of local_variables_forbidden_p.  */
15747   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15748
15749   /* Restore the queue.  */
15750   parser->unparsed_functions_queues
15751     = TREE_CHAIN (parser->unparsed_functions_queues);
15752 }
15753
15754 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15755    either a TYPE or an expression, depending on the form of the
15756    input.  The KEYWORD indicates which kind of expression we have
15757    encountered.  */
15758
15759 static tree
15760 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15761 {
15762   static const char *format;
15763   tree expr = NULL_TREE;
15764   const char *saved_message;
15765   bool saved_integral_constant_expression_p;
15766   bool saved_non_integral_constant_expression_p;
15767
15768   /* Initialize FORMAT the first time we get here.  */
15769   if (!format)
15770     format = "types may not be defined in '%s' expressions";
15771
15772   /* Types cannot be defined in a `sizeof' expression.  Save away the
15773      old message.  */
15774   saved_message = parser->type_definition_forbidden_message;
15775   /* And create the new one.  */
15776   parser->type_definition_forbidden_message
15777     = XNEWVEC (const char, strlen (format)
15778                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15779                + 1 /* `\0' */);
15780   sprintf ((char *) parser->type_definition_forbidden_message,
15781            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15782
15783   /* The restrictions on constant-expressions do not apply inside
15784      sizeof expressions.  */
15785   saved_integral_constant_expression_p
15786     = parser->integral_constant_expression_p;
15787   saved_non_integral_constant_expression_p
15788     = parser->non_integral_constant_expression_p;
15789   parser->integral_constant_expression_p = false;
15790
15791   /* Do not actually evaluate the expression.  */
15792   ++skip_evaluation;
15793   /* If it's a `(', then we might be looking at the type-id
15794      construction.  */
15795   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15796     {
15797       tree type;
15798       bool saved_in_type_id_in_expr_p;
15799
15800       /* We can't be sure yet whether we're looking at a type-id or an
15801          expression.  */
15802       cp_parser_parse_tentatively (parser);
15803       /* Consume the `('.  */
15804       cp_lexer_consume_token (parser->lexer);
15805       /* Parse the type-id.  */
15806       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15807       parser->in_type_id_in_expr_p = true;
15808       type = cp_parser_type_id (parser);
15809       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15810       /* Now, look for the trailing `)'.  */
15811       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15812       /* If all went well, then we're done.  */
15813       if (cp_parser_parse_definitely (parser))
15814         {
15815           cp_decl_specifier_seq decl_specs;
15816
15817           /* Build a trivial decl-specifier-seq.  */
15818           clear_decl_specs (&decl_specs);
15819           decl_specs.type = type;
15820
15821           /* Call grokdeclarator to figure out what type this is.  */
15822           expr = grokdeclarator (NULL,
15823                                  &decl_specs,
15824                                  TYPENAME,
15825                                  /*initialized=*/0,
15826                                  /*attrlist=*/NULL);
15827         }
15828     }
15829
15830   /* If the type-id production did not work out, then we must be
15831      looking at the unary-expression production.  */
15832   if (!expr)
15833     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15834                                        /*cast_p=*/false);
15835   /* Go back to evaluating expressions.  */
15836   --skip_evaluation;
15837
15838   /* Free the message we created.  */
15839   free ((char *) parser->type_definition_forbidden_message);
15840   /* And restore the old one.  */
15841   parser->type_definition_forbidden_message = saved_message;
15842   parser->integral_constant_expression_p
15843     = saved_integral_constant_expression_p;
15844   parser->non_integral_constant_expression_p
15845     = saved_non_integral_constant_expression_p;
15846
15847   return expr;
15848 }
15849
15850 /* If the current declaration has no declarator, return true.  */
15851
15852 static bool
15853 cp_parser_declares_only_class_p (cp_parser *parser)
15854 {
15855   /* If the next token is a `;' or a `,' then there is no
15856      declarator.  */
15857   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15858           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15859 }
15860
15861 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15862
15863 static void
15864 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15865                              cp_storage_class storage_class)
15866 {
15867   if (decl_specs->storage_class != sc_none)
15868     decl_specs->multiple_storage_classes_p = true;
15869   else
15870     decl_specs->storage_class = storage_class;
15871 }
15872
15873 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15874    is true, the type is a user-defined type; otherwise it is a
15875    built-in type specified by a keyword.  */
15876
15877 static void
15878 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15879                               tree type_spec,
15880                               bool user_defined_p)
15881 {
15882   decl_specs->any_specifiers_p = true;
15883
15884   /* If the user tries to redeclare bool or wchar_t (with, for
15885      example, in "typedef int wchar_t;") we remember that this is what
15886      happened.  In system headers, we ignore these declarations so
15887      that G++ can work with system headers that are not C++-safe.  */
15888   if (decl_specs->specs[(int) ds_typedef]
15889       && !user_defined_p
15890       && (type_spec == boolean_type_node
15891           || type_spec == wchar_type_node)
15892       && (decl_specs->type
15893           || decl_specs->specs[(int) ds_long]
15894           || decl_specs->specs[(int) ds_short]
15895           || decl_specs->specs[(int) ds_unsigned]
15896           || decl_specs->specs[(int) ds_signed]))
15897     {
15898       decl_specs->redefined_builtin_type = type_spec;
15899       if (!decl_specs->type)
15900         {
15901           decl_specs->type = type_spec;
15902           decl_specs->user_defined_type_p = false;
15903         }
15904     }
15905   else if (decl_specs->type)
15906     decl_specs->multiple_types_p = true;
15907   else
15908     {
15909       decl_specs->type = type_spec;
15910       decl_specs->user_defined_type_p = user_defined_p;
15911       decl_specs->redefined_builtin_type = NULL_TREE;
15912     }
15913 }
15914
15915 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15916    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15917
15918 static bool
15919 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15920 {
15921   return decl_specifiers->specs[(int) ds_friend] != 0;
15922 }
15923
15924 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15925    issue an error message indicating that TOKEN_DESC was expected.
15926
15927    Returns the token consumed, if the token had the appropriate type.
15928    Otherwise, returns NULL.  */
15929
15930 static cp_token *
15931 cp_parser_require (cp_parser* parser,
15932                    enum cpp_ttype type,
15933                    const char* token_desc)
15934 {
15935   if (cp_lexer_next_token_is (parser->lexer, type))
15936     return cp_lexer_consume_token (parser->lexer);
15937   else
15938     {
15939       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15940       if (!cp_parser_simulate_error (parser))
15941         {
15942           char *message = concat ("expected ", token_desc, NULL);
15943           cp_parser_error (parser, message);
15944           free (message);
15945         }
15946       return NULL;
15947     }
15948 }
15949
15950 /* Like cp_parser_require, except that tokens will be skipped until
15951    the desired token is found.  An error message is still produced if
15952    the next token is not as expected.  */
15953
15954 static void
15955 cp_parser_skip_until_found (cp_parser* parser,
15956                             enum cpp_ttype type,
15957                             const char* token_desc)
15958 {
15959   cp_token *token;
15960   unsigned nesting_depth = 0;
15961
15962   if (cp_parser_require (parser, type, token_desc))
15963     return;
15964
15965   /* Skip tokens until the desired token is found.  */
15966   while (true)
15967     {
15968       /* Peek at the next token.  */
15969       token = cp_lexer_peek_token (parser->lexer);
15970       /* If we've reached the token we want, consume it and
15971          stop.  */
15972       if (token->type == type && !nesting_depth)
15973         {
15974           cp_lexer_consume_token (parser->lexer);
15975           return;
15976         }
15977       /* If we've run out of tokens, stop.  */
15978       if (token->type == CPP_EOF)
15979         return;
15980       if (token->type == CPP_OPEN_BRACE
15981           || token->type == CPP_OPEN_PAREN
15982           || token->type == CPP_OPEN_SQUARE)
15983         ++nesting_depth;
15984       else if (token->type == CPP_CLOSE_BRACE
15985                || token->type == CPP_CLOSE_PAREN
15986                || token->type == CPP_CLOSE_SQUARE)
15987         {
15988           if (nesting_depth-- == 0)
15989             return;
15990         }
15991       /* Consume this token.  */
15992       cp_lexer_consume_token (parser->lexer);
15993     }
15994 }
15995
15996 /* If the next token is the indicated keyword, consume it.  Otherwise,
15997    issue an error message indicating that TOKEN_DESC was expected.
15998
15999    Returns the token consumed, if the token had the appropriate type.
16000    Otherwise, returns NULL.  */
16001
16002 static cp_token *
16003 cp_parser_require_keyword (cp_parser* parser,
16004                            enum rid keyword,
16005                            const char* token_desc)
16006 {
16007   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16008
16009   if (token && token->keyword != keyword)
16010     {
16011       dyn_string_t error_msg;
16012
16013       /* Format the error message.  */
16014       error_msg = dyn_string_new (0);
16015       dyn_string_append_cstr (error_msg, "expected ");
16016       dyn_string_append_cstr (error_msg, token_desc);
16017       cp_parser_error (parser, error_msg->s);
16018       dyn_string_delete (error_msg);
16019       return NULL;
16020     }
16021
16022   return token;
16023 }
16024
16025 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16026    function-definition.  */
16027
16028 static bool
16029 cp_parser_token_starts_function_definition_p (cp_token* token)
16030 {
16031   return (/* An ordinary function-body begins with an `{'.  */
16032           token->type == CPP_OPEN_BRACE
16033           /* A ctor-initializer begins with a `:'.  */
16034           || token->type == CPP_COLON
16035           /* A function-try-block begins with `try'.  */
16036           || token->keyword == RID_TRY
16037           /* The named return value extension begins with `return'.  */
16038           || token->keyword == RID_RETURN);
16039 }
16040
16041 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16042    definition.  */
16043
16044 static bool
16045 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16046 {
16047   cp_token *token;
16048
16049   token = cp_lexer_peek_token (parser->lexer);
16050   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16051 }
16052
16053 /* Returns TRUE iff the next token is the "," or ">" ending a
16054    template-argument.  */
16055
16056 static bool
16057 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16058 {
16059   cp_token *token;
16060
16061   token = cp_lexer_peek_token (parser->lexer);
16062   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16063 }
16064
16065 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16066    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16067
16068 static bool
16069 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16070                                                      size_t n)
16071 {
16072   cp_token *token;
16073
16074   token = cp_lexer_peek_nth_token (parser->lexer, n);
16075   if (token->type == CPP_LESS)
16076     return true;
16077   /* Check for the sequence `<::' in the original code. It would be lexed as
16078      `[:', where `[' is a digraph, and there is no whitespace before
16079      `:'.  */
16080   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16081     {
16082       cp_token *token2;
16083       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16084       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16085         return true;
16086     }
16087   return false;
16088 }
16089
16090 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16091    or none_type otherwise.  */
16092
16093 static enum tag_types
16094 cp_parser_token_is_class_key (cp_token* token)
16095 {
16096   switch (token->keyword)
16097     {
16098     case RID_CLASS:
16099       return class_type;
16100     case RID_STRUCT:
16101       return record_type;
16102     case RID_UNION:
16103       return union_type;
16104
16105     default:
16106       return none_type;
16107     }
16108 }
16109
16110 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16111
16112 static void
16113 cp_parser_check_class_key (enum tag_types class_key, tree type)
16114 {
16115   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16116     pedwarn ("%qs tag used in naming %q#T",
16117             class_key == union_type ? "union"
16118              : class_key == record_type ? "struct" : "class",
16119              type);
16120 }
16121
16122 /* Issue an error message if DECL is redeclared with different
16123    access than its original declaration [class.access.spec/3].
16124    This applies to nested classes and nested class templates.
16125    [class.mem/1].  */
16126
16127 static void
16128 cp_parser_check_access_in_redeclaration (tree decl)
16129 {
16130   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16131     return;
16132
16133   if ((TREE_PRIVATE (decl)
16134        != (current_access_specifier == access_private_node))
16135       || (TREE_PROTECTED (decl)
16136           != (current_access_specifier == access_protected_node)))
16137     error ("%qD redeclared with different access", decl);
16138 }
16139
16140 /* Look for the `template' keyword, as a syntactic disambiguator.
16141    Return TRUE iff it is present, in which case it will be
16142    consumed.  */
16143
16144 static bool
16145 cp_parser_optional_template_keyword (cp_parser *parser)
16146 {
16147   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16148     {
16149       /* The `template' keyword can only be used within templates;
16150          outside templates the parser can always figure out what is a
16151          template and what is not.  */
16152       if (!processing_template_decl)
16153         {
16154           error ("%<template%> (as a disambiguator) is only allowed "
16155                  "within templates");
16156           /* If this part of the token stream is rescanned, the same
16157              error message would be generated.  So, we purge the token
16158              from the stream.  */
16159           cp_lexer_purge_token (parser->lexer);
16160           return false;
16161         }
16162       else
16163         {
16164           /* Consume the `template' keyword.  */
16165           cp_lexer_consume_token (parser->lexer);
16166           return true;
16167         }
16168     }
16169
16170   return false;
16171 }
16172
16173 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16174    set PARSER->SCOPE, and perform other related actions.  */
16175
16176 static void
16177 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16178 {
16179   tree value;
16180   tree check;
16181
16182   /* Get the stored value.  */
16183   value = cp_lexer_consume_token (parser->lexer)->value;
16184   /* Perform any access checks that were deferred.  */
16185   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16186     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16187   /* Set the scope from the stored value.  */
16188   parser->scope = TREE_VALUE (value);
16189   parser->qualifying_scope = TREE_TYPE (value);
16190   parser->object_scope = NULL_TREE;
16191 }
16192
16193 /* Consume tokens up through a non-nested END token.  */
16194
16195 static void
16196 cp_parser_cache_group (cp_parser *parser,
16197                        enum cpp_ttype end,
16198                        unsigned depth)
16199 {
16200   while (true)
16201     {
16202       cp_token *token;
16203
16204       /* Abort a parenthesized expression if we encounter a brace.  */
16205       if ((end == CPP_CLOSE_PAREN || depth == 0)
16206           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16207         return;
16208       /* If we've reached the end of the file, stop.  */
16209       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16210         return;
16211       /* Consume the next token.  */
16212       token = cp_lexer_consume_token (parser->lexer);
16213       /* See if it starts a new group.  */
16214       if (token->type == CPP_OPEN_BRACE)
16215         {
16216           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16217           if (depth == 0)
16218             return;
16219         }
16220       else if (token->type == CPP_OPEN_PAREN)
16221         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16222       else if (token->type == end)
16223         return;
16224     }
16225 }
16226
16227 /* Begin parsing tentatively.  We always save tokens while parsing
16228    tentatively so that if the tentative parsing fails we can restore the
16229    tokens.  */
16230
16231 static void
16232 cp_parser_parse_tentatively (cp_parser* parser)
16233 {
16234   /* Enter a new parsing context.  */
16235   parser->context = cp_parser_context_new (parser->context);
16236   /* Begin saving tokens.  */
16237   cp_lexer_save_tokens (parser->lexer);
16238   /* In order to avoid repetitive access control error messages,
16239      access checks are queued up until we are no longer parsing
16240      tentatively.  */
16241   push_deferring_access_checks (dk_deferred);
16242 }
16243
16244 /* Commit to the currently active tentative parse.  */
16245
16246 static void
16247 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16248 {
16249   cp_parser_context *context;
16250   cp_lexer *lexer;
16251
16252   /* Mark all of the levels as committed.  */
16253   lexer = parser->lexer;
16254   for (context = parser->context; context->next; context = context->next)
16255     {
16256       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16257         break;
16258       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16259       while (!cp_lexer_saving_tokens (lexer))
16260         lexer = lexer->next;
16261       cp_lexer_commit_tokens (lexer);
16262     }
16263 }
16264
16265 /* Abort the currently active tentative parse.  All consumed tokens
16266    will be rolled back, and no diagnostics will be issued.  */
16267
16268 static void
16269 cp_parser_abort_tentative_parse (cp_parser* parser)
16270 {
16271   cp_parser_simulate_error (parser);
16272   /* Now, pretend that we want to see if the construct was
16273      successfully parsed.  */
16274   cp_parser_parse_definitely (parser);
16275 }
16276
16277 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16278    token stream.  Otherwise, commit to the tokens we have consumed.
16279    Returns true if no error occurred; false otherwise.  */
16280
16281 static bool
16282 cp_parser_parse_definitely (cp_parser* parser)
16283 {
16284   bool error_occurred;
16285   cp_parser_context *context;
16286
16287   /* Remember whether or not an error occurred, since we are about to
16288      destroy that information.  */
16289   error_occurred = cp_parser_error_occurred (parser);
16290   /* Remove the topmost context from the stack.  */
16291   context = parser->context;
16292   parser->context = context->next;
16293   /* If no parse errors occurred, commit to the tentative parse.  */
16294   if (!error_occurred)
16295     {
16296       /* Commit to the tokens read tentatively, unless that was
16297          already done.  */
16298       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16299         cp_lexer_commit_tokens (parser->lexer);
16300
16301       pop_to_parent_deferring_access_checks ();
16302     }
16303   /* Otherwise, if errors occurred, roll back our state so that things
16304      are just as they were before we began the tentative parse.  */
16305   else
16306     {
16307       cp_lexer_rollback_tokens (parser->lexer);
16308       pop_deferring_access_checks ();
16309     }
16310   /* Add the context to the front of the free list.  */
16311   context->next = cp_parser_context_free_list;
16312   cp_parser_context_free_list = context;
16313
16314   return !error_occurred;
16315 }
16316
16317 /* Returns true if we are parsing tentatively and are not committed to
16318    this tentative parse.  */
16319
16320 static bool
16321 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16322 {
16323   return (cp_parser_parsing_tentatively (parser)
16324           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16325 }
16326
16327 /* Returns nonzero iff an error has occurred during the most recent
16328    tentative parse.  */
16329
16330 static bool
16331 cp_parser_error_occurred (cp_parser* parser)
16332 {
16333   return (cp_parser_parsing_tentatively (parser)
16334           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16335 }
16336
16337 /* Returns nonzero if GNU extensions are allowed.  */
16338
16339 static bool
16340 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16341 {
16342   return parser->allow_gnu_extensions_p;
16343 }
16344 \f
16345 /* Objective-C++ Productions */
16346
16347
16348 /* Parse an Objective-C expression, which feeds into a primary-expression
16349    above.
16350
16351    objc-expression:
16352      objc-message-expression
16353      objc-string-literal
16354      objc-encode-expression
16355      objc-protocol-expression
16356      objc-selector-expression
16357
16358   Returns a tree representation of the expression.  */
16359
16360 static tree
16361 cp_parser_objc_expression (cp_parser* parser)
16362 {
16363   /* Try to figure out what kind of declaration is present.  */
16364   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16365
16366   switch (kwd->type)
16367     {
16368     case CPP_OPEN_SQUARE:
16369       return cp_parser_objc_message_expression (parser);
16370
16371     case CPP_OBJC_STRING:
16372       kwd = cp_lexer_consume_token (parser->lexer);
16373       return objc_build_string_object (kwd->value);
16374
16375     case CPP_KEYWORD:
16376       switch (kwd->keyword)
16377         {
16378         case RID_AT_ENCODE:
16379           return cp_parser_objc_encode_expression (parser);
16380
16381         case RID_AT_PROTOCOL:
16382           return cp_parser_objc_protocol_expression (parser);
16383
16384         case RID_AT_SELECTOR:
16385           return cp_parser_objc_selector_expression (parser);
16386
16387         default:
16388           break;
16389         }
16390     default:
16391       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16392       cp_parser_skip_to_end_of_block_or_statement (parser);
16393     }
16394
16395   return error_mark_node;
16396 }
16397
16398 /* Parse an Objective-C message expression.
16399
16400    objc-message-expression:
16401      [ objc-message-receiver objc-message-args ]
16402
16403    Returns a representation of an Objective-C message.  */
16404
16405 static tree
16406 cp_parser_objc_message_expression (cp_parser* parser)
16407 {
16408   tree receiver, messageargs;
16409
16410   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16411   receiver = cp_parser_objc_message_receiver (parser);
16412   messageargs = cp_parser_objc_message_args (parser);
16413   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16414
16415   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16416 }
16417
16418 /* Parse an objc-message-receiver.
16419
16420    objc-message-receiver:
16421      expression
16422      simple-type-specifier
16423
16424   Returns a representation of the type or expression.  */
16425
16426 static tree
16427 cp_parser_objc_message_receiver (cp_parser* parser)
16428 {
16429   tree rcv;
16430
16431   /* An Objective-C message receiver may be either (1) a type
16432      or (2) an expression.  */
16433   cp_parser_parse_tentatively (parser);
16434   rcv = cp_parser_expression (parser, false);
16435
16436   if (cp_parser_parse_definitely (parser))
16437     return rcv;
16438
16439   rcv = cp_parser_simple_type_specifier (parser,
16440                                          /*decl_specs=*/NULL,
16441                                          CP_PARSER_FLAGS_NONE);
16442
16443   return objc_get_class_reference (rcv);
16444 }
16445
16446 /* Parse the arguments and selectors comprising an Objective-C message.
16447
16448    objc-message-args:
16449      objc-selector
16450      objc-selector-args
16451      objc-selector-args , objc-comma-args
16452
16453    objc-selector-args:
16454      objc-selector [opt] : assignment-expression
16455      objc-selector-args objc-selector [opt] : assignment-expression
16456
16457    objc-comma-args:
16458      assignment-expression
16459      objc-comma-args , assignment-expression
16460
16461    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16462    selector arguments and TREE_VALUE containing a list of comma
16463    arguments.  */
16464
16465 static tree
16466 cp_parser_objc_message_args (cp_parser* parser)
16467 {
16468   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16469   bool maybe_unary_selector_p = true;
16470   cp_token *token = cp_lexer_peek_token (parser->lexer);
16471
16472   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16473     {
16474       tree selector = NULL_TREE, arg;
16475
16476       if (token->type != CPP_COLON)
16477         selector = cp_parser_objc_selector (parser);
16478
16479       /* Detect if we have a unary selector.  */
16480       if (maybe_unary_selector_p
16481           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16482         return build_tree_list (selector, NULL_TREE);
16483
16484       maybe_unary_selector_p = false;
16485       cp_parser_require (parser, CPP_COLON, "`:'");
16486       arg = cp_parser_assignment_expression (parser, false);
16487
16488       sel_args
16489         = chainon (sel_args,
16490                    build_tree_list (selector, arg));
16491
16492       token = cp_lexer_peek_token (parser->lexer);
16493     }
16494
16495   /* Handle non-selector arguments, if any. */
16496   while (token->type == CPP_COMMA)
16497     {
16498       tree arg;
16499
16500       cp_lexer_consume_token (parser->lexer);
16501       arg = cp_parser_assignment_expression (parser, false);
16502
16503       addl_args
16504         = chainon (addl_args,
16505                    build_tree_list (NULL_TREE, arg));
16506
16507       token = cp_lexer_peek_token (parser->lexer);
16508     }
16509
16510   return build_tree_list (sel_args, addl_args);
16511 }
16512
16513 /* Parse an Objective-C encode expression.
16514
16515    objc-encode-expression:
16516      @encode objc-typename
16517
16518    Returns an encoded representation of the type argument.  */
16519
16520 static tree
16521 cp_parser_objc_encode_expression (cp_parser* parser)
16522 {
16523   tree type;
16524
16525   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16526   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16527   type = complete_type (cp_parser_type_id (parser));
16528   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16529
16530   if (!type)
16531     {
16532       error ("%<@encode%> must specify a type as an argument");
16533       return error_mark_node;
16534     }
16535
16536   return objc_build_encode_expr (type);
16537 }
16538
16539 /* Parse an Objective-C @defs expression.  */
16540
16541 static tree
16542 cp_parser_objc_defs_expression (cp_parser *parser)
16543 {
16544   tree name;
16545
16546   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16547   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16548   name = cp_parser_identifier (parser);
16549   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16550
16551   return objc_get_class_ivars (name);
16552 }
16553
16554 /* Parse an Objective-C protocol expression.
16555
16556   objc-protocol-expression:
16557     @protocol ( identifier )
16558
16559   Returns a representation of the protocol expression.  */
16560
16561 static tree
16562 cp_parser_objc_protocol_expression (cp_parser* parser)
16563 {
16564   tree proto;
16565
16566   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16567   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16568   proto = cp_parser_identifier (parser);
16569   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16570
16571   return objc_build_protocol_expr (proto);
16572 }
16573
16574 /* Parse an Objective-C selector expression.
16575
16576    objc-selector-expression:
16577      @selector ( objc-method-signature )
16578
16579    objc-method-signature:
16580      objc-selector
16581      objc-selector-seq
16582
16583    objc-selector-seq:
16584      objc-selector :
16585      objc-selector-seq objc-selector :
16586
16587   Returns a representation of the method selector.  */
16588
16589 static tree
16590 cp_parser_objc_selector_expression (cp_parser* parser)
16591 {
16592   tree sel_seq = NULL_TREE;
16593   bool maybe_unary_selector_p = true;
16594   cp_token *token;
16595
16596   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16597   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16598   token = cp_lexer_peek_token (parser->lexer);
16599
16600   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16601          || token->type == CPP_SCOPE)
16602     {
16603       tree selector = NULL_TREE;
16604
16605       if (token->type != CPP_COLON
16606           || token->type == CPP_SCOPE)
16607         selector = cp_parser_objc_selector (parser);
16608
16609       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16610           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16611         {
16612           /* Detect if we have a unary selector.  */
16613           if (maybe_unary_selector_p)
16614             {
16615               sel_seq = selector;
16616               goto finish_selector;
16617             }
16618           else
16619             {
16620               cp_parser_error (parser, "expected %<:%>");
16621             }
16622         }
16623       maybe_unary_selector_p = false;
16624       token = cp_lexer_consume_token (parser->lexer);
16625       
16626       if (token->type == CPP_SCOPE)
16627         {
16628           sel_seq
16629             = chainon (sel_seq,
16630                        build_tree_list (selector, NULL_TREE));
16631           sel_seq
16632             = chainon (sel_seq,
16633                        build_tree_list (NULL_TREE, NULL_TREE));
16634         }
16635       else
16636         sel_seq
16637           = chainon (sel_seq,
16638                      build_tree_list (selector, NULL_TREE));
16639
16640       token = cp_lexer_peek_token (parser->lexer);
16641     }
16642
16643  finish_selector:
16644   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16645
16646   return objc_build_selector_expr (sel_seq);
16647 }
16648
16649 /* Parse a list of identifiers.
16650
16651    objc-identifier-list:
16652      identifier
16653      objc-identifier-list , identifier
16654
16655    Returns a TREE_LIST of identifier nodes.  */
16656
16657 static tree
16658 cp_parser_objc_identifier_list (cp_parser* parser)
16659 {
16660   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16661   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16662
16663   while (sep->type == CPP_COMMA)
16664     {
16665       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16666       list = chainon (list,
16667                       build_tree_list (NULL_TREE,
16668                                        cp_parser_identifier (parser)));
16669       sep = cp_lexer_peek_token (parser->lexer);
16670     }
16671
16672   return list;
16673 }
16674
16675 /* Parse an Objective-C alias declaration.
16676
16677    objc-alias-declaration:
16678      @compatibility_alias identifier identifier ;
16679
16680    This function registers the alias mapping with the Objective-C front-end.
16681    It returns nothing.  */
16682
16683 static void
16684 cp_parser_objc_alias_declaration (cp_parser* parser)
16685 {
16686   tree alias, orig;
16687
16688   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
16689   alias = cp_parser_identifier (parser);
16690   orig = cp_parser_identifier (parser);
16691   objc_declare_alias (alias, orig);
16692   cp_parser_consume_semicolon_at_end_of_statement (parser);
16693 }
16694
16695 /* Parse an Objective-C class forward-declaration.
16696
16697    objc-class-declaration:
16698      @class objc-identifier-list ;
16699
16700    The function registers the forward declarations with the Objective-C
16701    front-end.  It returns nothing.  */
16702
16703 static void
16704 cp_parser_objc_class_declaration (cp_parser* parser)
16705 {
16706   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
16707   objc_declare_class (cp_parser_objc_identifier_list (parser));
16708   cp_parser_consume_semicolon_at_end_of_statement (parser);
16709 }
16710
16711 /* Parse a list of Objective-C protocol references.
16712
16713    objc-protocol-refs-opt:
16714      objc-protocol-refs [opt]
16715
16716    objc-protocol-refs:
16717      < objc-identifier-list >
16718
16719    Returns a TREE_LIST of identifiers, if any.  */
16720
16721 static tree
16722 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16723 {
16724   tree protorefs = NULL_TREE;
16725
16726   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16727     {
16728       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
16729       protorefs = cp_parser_objc_identifier_list (parser);
16730       cp_parser_require (parser, CPP_GREATER, "`>'");
16731     }
16732
16733   return protorefs;
16734 }
16735
16736 /* Parse a Objective-C visibility specification.  */
16737
16738 static void
16739 cp_parser_objc_visibility_spec (cp_parser* parser)
16740 {
16741   cp_token *vis = cp_lexer_peek_token (parser->lexer);
16742
16743   switch (vis->keyword)
16744     {
16745     case RID_AT_PRIVATE:
16746       objc_set_visibility (2);
16747       break;
16748     case RID_AT_PROTECTED:
16749       objc_set_visibility (0);
16750       break;
16751     case RID_AT_PUBLIC:
16752       objc_set_visibility (1);
16753       break;
16754     default:
16755       return;
16756     }
16757
16758   /* Eat '@private'/'@protected'/'@public'.  */
16759   cp_lexer_consume_token (parser->lexer);
16760 }
16761
16762 /* Parse an Objective-C method type.  */
16763
16764 static void
16765 cp_parser_objc_method_type (cp_parser* parser)
16766 {
16767   objc_set_method_type
16768    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16769     ? PLUS_EXPR
16770     : MINUS_EXPR);
16771 }
16772
16773 /* Parse an Objective-C protocol qualifier.  */
16774
16775 static tree
16776 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16777 {
16778   tree quals = NULL_TREE, node;
16779   cp_token *token = cp_lexer_peek_token (parser->lexer);
16780
16781   node = token->value;
16782
16783   while (node && TREE_CODE (node) == IDENTIFIER_NODE
16784          && (node == ridpointers [(int) RID_IN]
16785              || node == ridpointers [(int) RID_OUT]
16786              || node == ridpointers [(int) RID_INOUT]
16787              || node == ridpointers [(int) RID_BYCOPY]
16788              || node == ridpointers [(int) RID_BYREF]
16789              || node == ridpointers [(int) RID_ONEWAY]))
16790     {
16791       quals = tree_cons (NULL_TREE, node, quals);
16792       cp_lexer_consume_token (parser->lexer);
16793       token = cp_lexer_peek_token (parser->lexer);
16794       node = token->value;
16795     }
16796
16797   return quals;
16798 }
16799
16800 /* Parse an Objective-C typename.  */
16801
16802 static tree
16803 cp_parser_objc_typename (cp_parser* parser)
16804 {
16805   tree typename = NULL_TREE;
16806
16807   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16808     {
16809       tree proto_quals, cp_type = NULL_TREE;
16810
16811       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
16812       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16813
16814       /* An ObjC type name may consist of just protocol qualifiers, in which
16815          case the type shall default to 'id'.  */
16816       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16817         cp_type = cp_parser_type_id (parser);
16818
16819       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16820       typename = build_tree_list (proto_quals, cp_type);
16821     }
16822
16823   return typename;
16824 }
16825
16826 /* Check to see if TYPE refers to an Objective-C selector name.  */
16827
16828 static bool
16829 cp_parser_objc_selector_p (enum cpp_ttype type)
16830 {
16831   return (type == CPP_NAME || type == CPP_KEYWORD
16832           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16833           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16834           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16835           || type == CPP_XOR || type == CPP_XOR_EQ);
16836 }
16837
16838 /* Parse an Objective-C selector.  */
16839
16840 static tree
16841 cp_parser_objc_selector (cp_parser* parser)
16842 {
16843   cp_token *token = cp_lexer_consume_token (parser->lexer);
16844
16845   if (!cp_parser_objc_selector_p (token->type))
16846     {
16847       error ("invalid Objective-C++ selector name");
16848       return error_mark_node;
16849     }
16850
16851   /* C++ operator names are allowed to appear in ObjC selectors.  */
16852   switch (token->type)
16853     {
16854     case CPP_AND_AND: return get_identifier ("and");
16855     case CPP_AND_EQ: return get_identifier ("and_eq");
16856     case CPP_AND: return get_identifier ("bitand");
16857     case CPP_OR: return get_identifier ("bitor");
16858     case CPP_COMPL: return get_identifier ("compl");
16859     case CPP_NOT: return get_identifier ("not");
16860     case CPP_NOT_EQ: return get_identifier ("not_eq");
16861     case CPP_OR_OR: return get_identifier ("or");
16862     case CPP_OR_EQ: return get_identifier ("or_eq");
16863     case CPP_XOR: return get_identifier ("xor");
16864     case CPP_XOR_EQ: return get_identifier ("xor_eq");
16865     default: return token->value;
16866     }
16867 }
16868
16869 /* Parse an Objective-C params list.  */
16870
16871 static tree
16872 cp_parser_objc_method_keyword_params (cp_parser* parser)
16873 {
16874   tree params = NULL_TREE;
16875   bool maybe_unary_selector_p = true;
16876   cp_token *token = cp_lexer_peek_token (parser->lexer);
16877
16878   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16879     {
16880       tree selector = NULL_TREE, typename, identifier;
16881
16882       if (token->type != CPP_COLON)
16883         selector = cp_parser_objc_selector (parser);
16884
16885       /* Detect if we have a unary selector.  */
16886       if (maybe_unary_selector_p
16887           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16888         return selector;
16889
16890       maybe_unary_selector_p = false;
16891       cp_parser_require (parser, CPP_COLON, "`:'");
16892       typename = cp_parser_objc_typename (parser);
16893       identifier = cp_parser_identifier (parser);
16894
16895       params
16896         = chainon (params,
16897                    objc_build_keyword_decl (selector,
16898                                             typename,
16899                                             identifier));
16900
16901       token = cp_lexer_peek_token (parser->lexer);
16902     }
16903
16904   return params;
16905 }
16906
16907 /* Parse the non-keyword Objective-C params.  */
16908
16909 static tree
16910 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16911 {
16912   tree params = make_node (TREE_LIST);
16913   cp_token *token = cp_lexer_peek_token (parser->lexer);
16914   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
16915
16916   while (token->type == CPP_COMMA)
16917     {
16918       cp_parameter_declarator *parmdecl;
16919       tree parm;
16920
16921       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16922       token = cp_lexer_peek_token (parser->lexer);
16923
16924       if (token->type == CPP_ELLIPSIS)
16925         {
16926           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
16927           *ellipsisp = true;
16928           break;
16929         }
16930
16931       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16932       parm = grokdeclarator (parmdecl->declarator,
16933                              &parmdecl->decl_specifiers,
16934                              PARM, /*initialized=*/0,
16935                              /*attrlist=*/NULL);
16936
16937       chainon (params, build_tree_list (NULL_TREE, parm));
16938       token = cp_lexer_peek_token (parser->lexer);
16939     }
16940
16941   return params;
16942 }
16943
16944 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
16945
16946 static void
16947 cp_parser_objc_interstitial_code (cp_parser* parser)
16948 {
16949   cp_token *token = cp_lexer_peek_token (parser->lexer);
16950
16951   /* If the next token is `extern' and the following token is a string
16952      literal, then we have a linkage specification.  */
16953   if (token->keyword == RID_EXTERN
16954       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16955     cp_parser_linkage_specification (parser);
16956   /* Handle #pragma, if any.  */
16957   else if (token->type == CPP_PRAGMA)
16958     cp_lexer_handle_pragma (parser->lexer);
16959   /* Allow stray semicolons.  */
16960   else if (token->type == CPP_SEMICOLON)
16961     cp_lexer_consume_token (parser->lexer);
16962   /* Finally, try to parse a block-declaration, or a function-definition.  */
16963   else
16964     cp_parser_block_declaration (parser, /*statement_p=*/false);
16965 }
16966
16967 /* Parse a method signature.  */
16968
16969 static tree
16970 cp_parser_objc_method_signature (cp_parser* parser)
16971 {
16972   tree rettype, kwdparms, optparms;
16973   bool ellipsis = false;
16974
16975   cp_parser_objc_method_type (parser);
16976   rettype = cp_parser_objc_typename (parser);
16977   kwdparms = cp_parser_objc_method_keyword_params (parser);
16978   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16979
16980   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16981 }
16982
16983 /* Pars an Objective-C method prototype list.  */
16984
16985 static void
16986 cp_parser_objc_method_prototype_list (cp_parser* parser)
16987 {
16988   cp_token *token = cp_lexer_peek_token (parser->lexer);
16989
16990   while (token->keyword != RID_AT_END)
16991     {
16992       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16993         {
16994           objc_add_method_declaration
16995            (cp_parser_objc_method_signature (parser));
16996           cp_parser_consume_semicolon_at_end_of_statement (parser);
16997         }
16998       else
16999         /* Allow for interspersed non-ObjC++ code.  */
17000         cp_parser_objc_interstitial_code (parser);
17001
17002       token = cp_lexer_peek_token (parser->lexer);
17003     }
17004
17005   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17006   objc_finish_interface ();
17007 }
17008
17009 /* Parse an Objective-C method definition list.  */
17010
17011 static void
17012 cp_parser_objc_method_definition_list (cp_parser* parser)
17013 {
17014   cp_token *token = cp_lexer_peek_token (parser->lexer);
17015
17016   while (token->keyword != RID_AT_END)
17017     {
17018       tree meth;
17019
17020       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17021         {
17022           push_deferring_access_checks (dk_deferred);
17023           objc_start_method_definition
17024            (cp_parser_objc_method_signature (parser));
17025
17026           /* For historical reasons, we accept an optional semicolon.  */
17027           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17028             cp_lexer_consume_token (parser->lexer);
17029
17030           perform_deferred_access_checks ();
17031           stop_deferring_access_checks ();
17032           meth = cp_parser_function_definition_after_declarator (parser,
17033                                                                  false);
17034           pop_deferring_access_checks ();
17035           objc_finish_method_definition (meth);
17036         }
17037       else
17038         /* Allow for interspersed non-ObjC++ code.  */
17039         cp_parser_objc_interstitial_code (parser);
17040
17041       token = cp_lexer_peek_token (parser->lexer);
17042     }
17043
17044   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17045   objc_finish_implementation ();
17046 }
17047
17048 /* Parse Objective-C ivars.  */
17049
17050 static void
17051 cp_parser_objc_class_ivars (cp_parser* parser)
17052 {
17053   cp_token *token = cp_lexer_peek_token (parser->lexer);
17054
17055   if (token->type != CPP_OPEN_BRACE)
17056     return;     /* No ivars specified.  */
17057
17058   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17059   token = cp_lexer_peek_token (parser->lexer);
17060
17061   while (token->type != CPP_CLOSE_BRACE)
17062     {
17063       cp_decl_specifier_seq declspecs;
17064       int decl_class_or_enum_p;
17065       tree prefix_attributes;
17066
17067       cp_parser_objc_visibility_spec (parser);
17068
17069       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17070         break;
17071
17072       cp_parser_decl_specifier_seq (parser,
17073                                     CP_PARSER_FLAGS_OPTIONAL,
17074                                     &declspecs,
17075                                     &decl_class_or_enum_p);
17076       prefix_attributes = declspecs.attributes;
17077       declspecs.attributes = NULL_TREE;
17078
17079       /* Keep going until we hit the `;' at the end of the
17080          declaration.  */
17081       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17082         {
17083           tree width = NULL_TREE, attributes, first_attribute, decl;
17084           cp_declarator *declarator = NULL;
17085           int ctor_dtor_or_conv_p;
17086
17087           /* Check for a (possibly unnamed) bitfield declaration.  */
17088           token = cp_lexer_peek_token (parser->lexer);
17089           if (token->type == CPP_COLON)
17090             goto eat_colon;
17091
17092           if (token->type == CPP_NAME
17093               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17094                   == CPP_COLON))
17095             {
17096               /* Get the name of the bitfield.  */
17097               declarator = make_id_declarator (NULL_TREE,
17098                                                cp_parser_identifier (parser));
17099
17100              eat_colon:
17101               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17102               /* Get the width of the bitfield.  */
17103               width
17104                 = cp_parser_constant_expression (parser,
17105                                                  /*allow_non_constant=*/false,
17106                                                  NULL);
17107             }
17108           else
17109             {
17110               /* Parse the declarator.  */
17111               declarator
17112                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17113                                         &ctor_dtor_or_conv_p,
17114                                         /*parenthesized_p=*/NULL,
17115                                         /*member_p=*/false);
17116             }
17117
17118           /* Look for attributes that apply to the ivar.  */
17119           attributes = cp_parser_attributes_opt (parser);
17120           /* Remember which attributes are prefix attributes and
17121              which are not.  */
17122           first_attribute = attributes;
17123           /* Combine the attributes.  */
17124           attributes = chainon (prefix_attributes, attributes);
17125
17126           if (width)
17127             {
17128               /* Create the bitfield declaration.  */
17129               decl = grokbitfield (declarator, &declspecs, width);
17130               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17131             }
17132           else
17133             decl = grokfield (declarator, &declspecs, NULL_TREE,
17134                               NULL_TREE, attributes);
17135
17136           /* Add the instance variable.  */
17137           objc_add_instance_variable (decl);
17138
17139           /* Reset PREFIX_ATTRIBUTES.  */
17140           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17141             attributes = TREE_CHAIN (attributes);
17142           if (attributes)
17143             TREE_CHAIN (attributes) = NULL_TREE;
17144
17145           token = cp_lexer_peek_token (parser->lexer);
17146
17147           if (token->type == CPP_COMMA)
17148             {
17149               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17150               continue;
17151             }
17152           break;
17153         }
17154
17155       cp_parser_consume_semicolon_at_end_of_statement (parser);
17156       token = cp_lexer_peek_token (parser->lexer);
17157     }
17158
17159   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17160   /* For historical reasons, we accept an optional semicolon.  */
17161   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17162     cp_lexer_consume_token (parser->lexer);
17163 }
17164
17165 /* Parse an Objective-C protocol declaration.  */
17166
17167 static void
17168 cp_parser_objc_protocol_declaration (cp_parser* parser)
17169 {
17170   tree proto, protorefs;
17171   cp_token *tok;
17172
17173   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17174   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17175     {
17176       error ("identifier expected after %<@protocol%>");
17177       goto finish;
17178     }
17179
17180   /* See if we have a forward declaration or a definition.  */
17181   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17182
17183   /* Try a forward declaration first.  */
17184   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17185     {
17186       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17187      finish:
17188       cp_parser_consume_semicolon_at_end_of_statement (parser);
17189     }
17190
17191   /* Ok, we got a full-fledged definition (or at least should).  */
17192   else
17193     {
17194       proto = cp_parser_identifier (parser);
17195       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17196       objc_start_protocol (proto, protorefs);
17197       cp_parser_objc_method_prototype_list (parser);
17198     }
17199 }
17200
17201 /* Parse an Objective-C superclass or category.  */
17202
17203 static void
17204 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17205                                                           tree *categ)
17206 {
17207   cp_token *next = cp_lexer_peek_token (parser->lexer);
17208
17209   *super = *categ = NULL_TREE;
17210   if (next->type == CPP_COLON)
17211     {
17212       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17213       *super = cp_parser_identifier (parser);
17214     }
17215   else if (next->type == CPP_OPEN_PAREN)
17216     {
17217       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17218       *categ = cp_parser_identifier (parser);
17219       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17220     }
17221 }
17222
17223 /* Parse an Objective-C class interface.  */
17224
17225 static void
17226 cp_parser_objc_class_interface (cp_parser* parser)
17227 {
17228   tree name, super, categ, protos;
17229
17230   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17231   name = cp_parser_identifier (parser);
17232   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17233   protos = cp_parser_objc_protocol_refs_opt (parser);
17234
17235   /* We have either a class or a category on our hands.  */
17236   if (categ)
17237     objc_start_category_interface (name, categ, protos);
17238   else
17239     {
17240       objc_start_class_interface (name, super, protos);
17241       /* Handle instance variable declarations, if any.  */
17242       cp_parser_objc_class_ivars (parser);
17243       objc_continue_interface ();
17244     }
17245
17246   cp_parser_objc_method_prototype_list (parser);
17247 }
17248
17249 /* Parse an Objective-C class implementation.  */
17250
17251 static void
17252 cp_parser_objc_class_implementation (cp_parser* parser)
17253 {
17254   tree name, super, categ;
17255
17256   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17257   name = cp_parser_identifier (parser);
17258   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17259
17260   /* We have either a class or a category on our hands.  */
17261   if (categ)
17262     objc_start_category_implementation (name, categ);
17263   else
17264     {
17265       objc_start_class_implementation (name, super);
17266       /* Handle instance variable declarations, if any.  */
17267       cp_parser_objc_class_ivars (parser);
17268       objc_continue_implementation ();
17269     }
17270
17271   cp_parser_objc_method_definition_list (parser);
17272 }
17273
17274 /* Consume the @end token and finish off the implementation.  */
17275
17276 static void
17277 cp_parser_objc_end_implementation (cp_parser* parser)
17278 {
17279   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17280   objc_finish_implementation ();
17281 }
17282
17283 /* Parse an Objective-C declaration.  */
17284
17285 static void
17286 cp_parser_objc_declaration (cp_parser* parser)
17287 {
17288   /* Try to figure out what kind of declaration is present.  */
17289   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17290
17291   switch (kwd->keyword)
17292     {
17293     case RID_AT_ALIAS:
17294       cp_parser_objc_alias_declaration (parser);
17295       break;
17296     case RID_AT_CLASS:
17297       cp_parser_objc_class_declaration (parser);
17298       break;
17299     case RID_AT_PROTOCOL:
17300       cp_parser_objc_protocol_declaration (parser);
17301       break;
17302     case RID_AT_INTERFACE:
17303       cp_parser_objc_class_interface (parser);
17304       break;
17305     case RID_AT_IMPLEMENTATION:
17306       cp_parser_objc_class_implementation (parser);
17307       break;
17308     case RID_AT_END:
17309       cp_parser_objc_end_implementation (parser);
17310       break;
17311     default:
17312       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17313       cp_parser_skip_to_end_of_block_or_statement (parser);
17314     }
17315 }
17316
17317 /* Parse an Objective-C try-catch-finally statement.
17318
17319    objc-try-catch-finally-stmt:
17320      @try compound-statement objc-catch-clause-seq [opt]
17321        objc-finally-clause [opt]
17322
17323    objc-catch-clause-seq:
17324      objc-catch-clause objc-catch-clause-seq [opt]
17325
17326    objc-catch-clause:
17327      @catch ( exception-declaration ) compound-statement
17328
17329    objc-finally-clause
17330      @finally compound-statement
17331
17332    Returns NULL_TREE.  */
17333
17334 static tree
17335 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17336   location_t location;
17337   tree stmt;
17338
17339   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17340   location = cp_lexer_peek_token (parser->lexer)->location;
17341   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17342      node, lest it get absorbed into the surrounding block.  */
17343   stmt = push_stmt_list ();
17344   cp_parser_compound_statement (parser, NULL, false);
17345   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17346
17347   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17348     {
17349       cp_parameter_declarator *parmdecl;
17350       tree parm;
17351
17352       cp_lexer_consume_token (parser->lexer);
17353       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17354       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17355       parm = grokdeclarator (parmdecl->declarator,
17356                              &parmdecl->decl_specifiers,
17357                              PARM, /*initialized=*/0,
17358                              /*attrlist=*/NULL);
17359       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17360       objc_begin_catch_clause (parm);
17361       cp_parser_compound_statement (parser, NULL, false);
17362       objc_finish_catch_clause ();
17363     }
17364
17365   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17366     {
17367       cp_lexer_consume_token (parser->lexer);
17368       location = cp_lexer_peek_token (parser->lexer)->location;
17369       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17370          node, lest it get absorbed into the surrounding block.  */
17371       stmt = push_stmt_list ();
17372       cp_parser_compound_statement (parser, NULL, false);
17373       objc_build_finally_clause (location, pop_stmt_list (stmt));
17374     }
17375
17376   return objc_finish_try_stmt ();
17377 }
17378
17379 /* Parse an Objective-C synchronized statement.
17380
17381    objc-synchronized-stmt:
17382      @synchronized ( expression ) compound-statement
17383
17384    Returns NULL_TREE.  */
17385
17386 static tree
17387 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17388   location_t location;
17389   tree lock, stmt;
17390
17391   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17392
17393   location = cp_lexer_peek_token (parser->lexer)->location;
17394   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17395   lock = cp_parser_expression (parser, false);
17396   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17397
17398   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17399      node, lest it get absorbed into the surrounding block.  */
17400   stmt = push_stmt_list ();
17401   cp_parser_compound_statement (parser, NULL, false);
17402
17403   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17404 }
17405
17406 /* Parse an Objective-C throw statement.
17407
17408    objc-throw-stmt:
17409      @throw assignment-expression [opt] ;
17410
17411    Returns a constructed '@throw' statement.  */
17412
17413 static tree
17414 cp_parser_objc_throw_statement (cp_parser *parser) {
17415   tree expr = NULL_TREE;
17416
17417   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17418
17419   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17420     expr = cp_parser_assignment_expression (parser, false);
17421
17422   cp_parser_consume_semicolon_at_end_of_statement (parser);
17423
17424   return objc_build_throw_stmt (expr);
17425 }
17426
17427 /* Parse an Objective-C statement.  */
17428
17429 static tree
17430 cp_parser_objc_statement (cp_parser * parser) {
17431   /* Try to figure out what kind of declaration is present.  */
17432   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17433
17434   switch (kwd->keyword)
17435     {
17436     case RID_AT_TRY:
17437       return cp_parser_objc_try_catch_finally_statement (parser);
17438     case RID_AT_SYNCHRONIZED:
17439       return cp_parser_objc_synchronized_statement (parser);
17440     case RID_AT_THROW:
17441       return cp_parser_objc_throw_statement (parser);
17442     default:
17443       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17444       cp_parser_skip_to_end_of_block_or_statement (parser);
17445     }
17446
17447   return error_mark_node;
17448 }
17449 \f
17450 /* The parser.  */
17451
17452 static GTY (()) cp_parser *the_parser;
17453
17454 /* External interface.  */
17455
17456 /* Parse one entire translation unit.  */
17457
17458 void
17459 c_parse_file (void)
17460 {
17461   bool error_occurred;
17462   static bool already_called = false;
17463
17464   if (already_called)
17465     {
17466       sorry ("inter-module optimizations not implemented for C++");
17467       return;
17468     }
17469   already_called = true;
17470
17471   the_parser = cp_parser_new ();
17472   push_deferring_access_checks (flag_access_control
17473                                 ? dk_no_deferred : dk_no_check);
17474   error_occurred = cp_parser_translation_unit (the_parser);
17475   the_parser = NULL;
17476 }
17477
17478 /* This variable must be provided by every front end.  */
17479
17480 int yydebug;
17481
17482 #include "gt-cp-parser.h"