OSDN Git Service

2008-06-14 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008  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 3, 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 COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.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 token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a system header.  */
75   BOOL_BITFIELD in_system_header : 1;
76   /* True if this token is from a context where it is implicitly extern "C" */
77   BOOL_BITFIELD implicit_extern_c : 1;
78   /* True for a CPP_NAME token that is not a keyword (i.e., for which
79      KEYWORD is RID_MAX) iff this name was looked up and found to be
80      ambiguous.  An error has already been reported.  */
81   BOOL_BITFIELD ambiguous_p : 1;
82   /* The value associated with this token, if any.  */
83   union cp_token_value {
84     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
85     struct tree_check* GTY((tag ("1"))) tree_check_value;
86     /* Use for all other tokens.  */
87     tree GTY((tag ("0"))) value;
88   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89   /* The location at which this token was found.  */
90   location_t location;
91 } cp_token;
92
93 /* We use a stack of token pointer for saving token sets.  */
94 typedef struct cp_token *cp_token_position;
95 DEF_VEC_P (cp_token_position);
96 DEF_VEC_ALLOC_P (cp_token_position,heap);
97
98 static cp_token eof_token =
99 {
100   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, false, 0, { NULL },
101   0
102 };
103
104 /* The cp_lexer structure represents the C++ lexer.  It is responsible
105    for managing the token stream from the preprocessor and supplying
106    it to the parser.  Tokens are never added to the cp_lexer after
107    it is created.  */
108
109 typedef struct cp_lexer GTY (())
110 {
111   /* The memory allocated for the buffer.  NULL if this lexer does not
112      own the token buffer.  */
113   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
114   /* If the lexer owns the buffer, this is the number of tokens in the
115      buffer.  */
116   size_t buffer_length;
117
118   /* A pointer just past the last available token.  The tokens
119      in this lexer are [buffer, last_token).  */
120   cp_token_position GTY ((skip)) last_token;
121
122   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
123      no more available tokens.  */
124   cp_token_position GTY ((skip)) next_token;
125
126   /* A stack indicating positions at which cp_lexer_save_tokens was
127      called.  The top entry is the most recent position at which we
128      began saving tokens.  If the stack is non-empty, we are saving
129      tokens.  */
130   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
131
132   /* The next lexer in a linked list of lexers.  */
133   struct cp_lexer *next;
134
135   /* True if we should output debugging information.  */
136   bool debugging_p;
137
138   /* True if we're in the context of parsing a pragma, and should not
139      increment past the end-of-line marker.  */
140   bool in_pragma;
141 } cp_lexer;
142
143 /* cp_token_cache is a range of tokens.  There is no need to represent
144    allocate heap memory for it, since tokens are never removed from the
145    lexer's array.  There is also no need for the GC to walk through
146    a cp_token_cache, since everything in here is referenced through
147    a lexer.  */
148
149 typedef struct cp_token_cache GTY(())
150 {
151   /* The beginning of the token range.  */
152   cp_token * GTY((skip)) first;
153
154   /* Points immediately after the last token in the range.  */
155   cp_token * GTY ((skip)) last;
156 } cp_token_cache;
157
158 /* Prototypes.  */
159
160 static cp_lexer *cp_lexer_new_main
161   (void);
162 static cp_lexer *cp_lexer_new_from_tokens
163   (cp_token_cache *tokens);
164 static void cp_lexer_destroy
165   (cp_lexer *);
166 static int cp_lexer_saving_tokens
167   (const cp_lexer *);
168 static cp_token_position cp_lexer_token_position
169   (cp_lexer *, bool);
170 static cp_token *cp_lexer_token_at
171   (cp_lexer *, cp_token_position);
172 static void cp_lexer_get_preprocessor_token
173   (cp_lexer *, cp_token *);
174 static inline cp_token *cp_lexer_peek_token
175   (cp_lexer *);
176 static cp_token *cp_lexer_peek_nth_token
177   (cp_lexer *, size_t);
178 static inline bool cp_lexer_next_token_is
179   (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_not
181   (cp_lexer *, enum cpp_ttype);
182 static bool cp_lexer_next_token_is_keyword
183   (cp_lexer *, enum rid);
184 static cp_token *cp_lexer_consume_token
185   (cp_lexer *);
186 static void cp_lexer_purge_token
187   (cp_lexer *);
188 static void cp_lexer_purge_tokens_after
189   (cp_lexer *, cp_token_position);
190 static void cp_lexer_save_tokens
191   (cp_lexer *);
192 static void cp_lexer_commit_tokens
193   (cp_lexer *);
194 static void cp_lexer_rollback_tokens
195   (cp_lexer *);
196 #ifdef ENABLE_CHECKING
197 static void cp_lexer_print_token
198   (FILE *, cp_token *);
199 static inline bool cp_lexer_debugging_p
200   (cp_lexer *);
201 static void cp_lexer_start_debugging
202   (cp_lexer *) ATTRIBUTE_UNUSED;
203 static void cp_lexer_stop_debugging
204   (cp_lexer *) ATTRIBUTE_UNUSED;
205 #else
206 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
207    about passing NULL to functions that require non-NULL arguments
208    (fputs, fprintf).  It will never be used, so all we need is a value
209    of the right type that's guaranteed not to be NULL.  */
210 #define cp_lexer_debug_stream stdout
211 #define cp_lexer_print_token(str, tok) (void) 0
212 #define cp_lexer_debugging_p(lexer) 0
213 #endif /* ENABLE_CHECKING */
214
215 static cp_token_cache *cp_token_cache_new
216   (cp_token *, cp_token *);
217
218 static void cp_parser_initial_pragma
219   (cp_token *);
220
221 /* Manifest constants.  */
222 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
223 #define CP_SAVED_TOKEN_STACK 5
224
225 /* A token type for keywords, as opposed to ordinary identifiers.  */
226 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
227
228 /* A token type for template-ids.  If a template-id is processed while
229    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
230    the value of the CPP_TEMPLATE_ID is whatever was returned by
231    cp_parser_template_id.  */
232 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
233
234 /* A token type for nested-name-specifiers.  If a
235    nested-name-specifier is processed while parsing tentatively, it is
236    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
237    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
238    cp_parser_nested_name_specifier_opt.  */
239 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
240
241 /* A token type for tokens that are not tokens at all; these are used
242    to represent slots in the array where there used to be a token
243    that has now been deleted.  */
244 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
245
246 /* The number of token types, including C++-specific ones.  */
247 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
248
249 /* Variables.  */
250
251 #ifdef ENABLE_CHECKING
252 /* The stream to which debugging output should be written.  */
253 static FILE *cp_lexer_debug_stream;
254 #endif /* ENABLE_CHECKING */
255
256 /* Create a new main C++ lexer, the lexer that gets tokens from the
257    preprocessor.  */
258
259 static cp_lexer *
260 cp_lexer_new_main (void)
261 {
262   cp_token first_token;
263   cp_lexer *lexer;
264   cp_token *pos;
265   size_t alloc;
266   size_t space;
267   cp_token *buffer;
268
269   /* It's possible that parsing the first pragma will load a PCH file,
270      which is a GC collection point.  So we have to do that before
271      allocating any memory.  */
272   cp_parser_initial_pragma (&first_token);
273
274   c_common_no_more_pch ();
275
276   /* Allocate the memory.  */
277   lexer = GGC_CNEW (cp_lexer);
278
279 #ifdef ENABLE_CHECKING
280   /* Initially we are not debugging.  */
281   lexer->debugging_p = false;
282 #endif /* ENABLE_CHECKING */
283   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
284                                    CP_SAVED_TOKEN_STACK);
285
286   /* Create the buffer.  */
287   alloc = CP_LEXER_BUFFER_SIZE;
288   buffer = GGC_NEWVEC (cp_token, alloc);
289
290   /* Put the first token in the buffer.  */
291   space = alloc;
292   pos = buffer;
293   *pos = first_token;
294
295   /* Get the remaining tokens from the preprocessor.  */
296   while (pos->type != CPP_EOF)
297     {
298       pos++;
299       if (!--space)
300         {
301           space = alloc;
302           alloc *= 2;
303           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
304           pos = buffer + space;
305         }
306       cp_lexer_get_preprocessor_token (lexer, pos);
307     }
308   lexer->buffer = buffer;
309   lexer->buffer_length = alloc - space;
310   lexer->last_token = pos;
311   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
312
313   /* Subsequent preprocessor diagnostics should use compiler
314      diagnostic functions to get the compiler source location.  */
315   cpp_get_options (parse_in)->client_diagnostic = true;
316   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
317
318   gcc_assert (lexer->next_token->type != CPP_PURGED);
319   return lexer;
320 }
321
322 /* Create a new lexer whose token stream is primed with the tokens in
323    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
324
325 static cp_lexer *
326 cp_lexer_new_from_tokens (cp_token_cache *cache)
327 {
328   cp_token *first = cache->first;
329   cp_token *last = cache->last;
330   cp_lexer *lexer = GGC_CNEW (cp_lexer);
331
332   /* We do not own the buffer.  */
333   lexer->buffer = NULL;
334   lexer->buffer_length = 0;
335   lexer->next_token = first == last ? &eof_token : first;
336   lexer->last_token = last;
337
338   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
339                                    CP_SAVED_TOKEN_STACK);
340
341 #ifdef ENABLE_CHECKING
342   /* Initially we are not debugging.  */
343   lexer->debugging_p = false;
344 #endif
345
346   gcc_assert (lexer->next_token->type != CPP_PURGED);
347   return lexer;
348 }
349
350 /* Frees all resources associated with LEXER.  */
351
352 static void
353 cp_lexer_destroy (cp_lexer *lexer)
354 {
355   if (lexer->buffer)
356     ggc_free (lexer->buffer);
357   VEC_free (cp_token_position, heap, lexer->saved_tokens);
358   ggc_free (lexer);
359 }
360
361 /* Returns nonzero if debugging information should be output.  */
362
363 #ifdef ENABLE_CHECKING
364
365 static inline bool
366 cp_lexer_debugging_p (cp_lexer *lexer)
367 {
368   return lexer->debugging_p;
369 }
370
371 #endif /* ENABLE_CHECKING */
372
373 static inline cp_token_position
374 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
375 {
376   gcc_assert (!previous_p || lexer->next_token != &eof_token);
377
378   return lexer->next_token - previous_p;
379 }
380
381 static inline cp_token *
382 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
383 {
384   return pos;
385 }
386
387 /* nonzero if we are presently saving tokens.  */
388
389 static inline int
390 cp_lexer_saving_tokens (const cp_lexer* lexer)
391 {
392   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 }
394
395 /* Store the next token from the preprocessor in *TOKEN.  Return true
396    if we reach EOF.  If LEXER is NULL, assume we are handling an
397    initial #pragma pch_preprocess, and thus want the lexer to return
398    processed strings.  */
399
400 static void
401 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
402 {
403   static int is_extern_c = 0;
404
405    /* Get a new token from the preprocessor.  */
406   token->type
407     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
408                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
409   token->keyword = RID_MAX;
410   token->pragma_kind = PRAGMA_NONE;
411   token->in_system_header = in_system_header;
412
413   /* On some systems, some header files are surrounded by an
414      implicit extern "C" block.  Set a flag in the token if it
415      comes from such a header.  */
416   is_extern_c += pending_lang_change;
417   pending_lang_change = 0;
418   token->implicit_extern_c = is_extern_c > 0;
419
420   /* Check to see if this token is a keyword.  */
421   if (token->type == CPP_NAME)
422     {
423       if (C_IS_RESERVED_WORD (token->u.value))
424         {
425           /* Mark this token as a keyword.  */
426           token->type = CPP_KEYWORD;
427           /* Record which keyword.  */
428           token->keyword = C_RID_CODE (token->u.value);
429           /* Update the value.  Some keywords are mapped to particular
430              entities, rather than simply having the value of the
431              corresponding IDENTIFIER_NODE.  For example, `__const' is
432              mapped to `const'.  */
433           token->u.value = ridpointers[token->keyword];
434         }
435       else
436         {
437           if (warn_cxx0x_compat
438               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
439               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
440             {
441               /* Warn about the C++0x keyword (but still treat it as
442                  an identifier).  */
443               warning (OPT_Wc__0x_compat, 
444                        "identifier %<%s%> will become a keyword in C++0x",
445                        IDENTIFIER_POINTER (token->u.value));
446
447               /* Clear out the C_RID_CODE so we don't warn about this
448                  particular identifier-turned-keyword again.  */
449               C_RID_CODE (token->u.value) = RID_MAX;
450             }
451
452           token->ambiguous_p = false;
453           token->keyword = RID_MAX;
454         }
455     }
456   /* Handle Objective-C++ keywords.  */
457   else if (token->type == CPP_AT_NAME)
458     {
459       token->type = CPP_KEYWORD;
460       switch (C_RID_CODE (token->u.value))
461         {
462         /* Map 'class' to '@class', 'private' to '@private', etc.  */
463         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
464         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
465         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
466         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
467         case RID_THROW: token->keyword = RID_AT_THROW; break;
468         case RID_TRY: token->keyword = RID_AT_TRY; break;
469         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
470         default: token->keyword = C_RID_CODE (token->u.value);
471         }
472     }
473   else if (token->type == CPP_PRAGMA)
474     {
475       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
476       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
477       token->u.value = NULL_TREE;
478     }
479 }
480
481 /* Update the globals input_location and in_system_header and the
482    input file stack from TOKEN.  */
483 static inline void
484 cp_lexer_set_source_position_from_token (cp_token *token)
485 {
486   if (token->type != CPP_EOF)
487     {
488       input_location = token->location;
489       in_system_header = token->in_system_header;
490     }
491 }
492
493 /* Return a pointer to the next token in the token stream, but do not
494    consume it.  */
495
496 static inline cp_token *
497 cp_lexer_peek_token (cp_lexer *lexer)
498 {
499   if (cp_lexer_debugging_p (lexer))
500     {
501       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
502       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
503       putc ('\n', cp_lexer_debug_stream);
504     }
505   return lexer->next_token;
506 }
507
508 /* Return true if the next token has the indicated TYPE.  */
509
510 static inline bool
511 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
512 {
513   return cp_lexer_peek_token (lexer)->type == type;
514 }
515
516 /* Return true if the next token does not have the indicated TYPE.  */
517
518 static inline bool
519 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
520 {
521   return !cp_lexer_next_token_is (lexer, type);
522 }
523
524 /* Return true if the next token is the indicated KEYWORD.  */
525
526 static inline bool
527 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
528 {
529   return cp_lexer_peek_token (lexer)->keyword == keyword;
530 }
531
532 /* Return true if the next token is a keyword for a decl-specifier.  */
533
534 static bool
535 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
536 {
537   cp_token *token;
538
539   token = cp_lexer_peek_token (lexer);
540   switch (token->keyword) 
541     {
542       /* auto specifier: storage-class-specifier in C++,
543          simple-type-specifier in C++0x.  */
544     case RID_AUTO:
545       /* Storage classes.  */
546     case RID_REGISTER:
547     case RID_STATIC:
548     case RID_EXTERN:
549     case RID_MUTABLE:
550     case RID_THREAD:
551       /* Elaborated type specifiers.  */
552     case RID_ENUM:
553     case RID_CLASS:
554     case RID_STRUCT:
555     case RID_UNION:
556     case RID_TYPENAME:
557       /* Simple type specifiers.  */
558     case RID_CHAR:
559     case RID_CHAR16:
560     case RID_CHAR32:
561     case RID_WCHAR:
562     case RID_BOOL:
563     case RID_SHORT:
564     case RID_INT:
565     case RID_LONG:
566     case RID_SIGNED:
567     case RID_UNSIGNED:
568     case RID_FLOAT:
569     case RID_DOUBLE:
570     case RID_VOID:
571       /* GNU extensions.  */ 
572     case RID_ATTRIBUTE:
573     case RID_TYPEOF:
574       /* C++0x extensions.  */
575     case RID_DECLTYPE:
576       return true;
577
578     default:
579       return false;
580     }
581 }
582
583 /* Return a pointer to the Nth token in the token stream.  If N is 1,
584    then this is precisely equivalent to cp_lexer_peek_token (except
585    that it is not inline).  One would like to disallow that case, but
586    there is one case (cp_parser_nth_token_starts_template_id) where
587    the caller passes a variable for N and it might be 1.  */
588
589 static cp_token *
590 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
591 {
592   cp_token *token;
593
594   /* N is 1-based, not zero-based.  */
595   gcc_assert (n > 0);
596
597   if (cp_lexer_debugging_p (lexer))
598     fprintf (cp_lexer_debug_stream,
599              "cp_lexer: peeking ahead %ld at token: ", (long)n);
600
601   --n;
602   token = lexer->next_token;
603   gcc_assert (!n || token != &eof_token);
604   while (n != 0)
605     {
606       ++token;
607       if (token == lexer->last_token)
608         {
609           token = &eof_token;
610           break;
611         }
612
613       if (token->type != CPP_PURGED)
614         --n;
615     }
616
617   if (cp_lexer_debugging_p (lexer))
618     {
619       cp_lexer_print_token (cp_lexer_debug_stream, token);
620       putc ('\n', cp_lexer_debug_stream);
621     }
622
623   return token;
624 }
625
626 /* Return the next token, and advance the lexer's next_token pointer
627    to point to the next non-purged token.  */
628
629 static cp_token *
630 cp_lexer_consume_token (cp_lexer* lexer)
631 {
632   cp_token *token = lexer->next_token;
633
634   gcc_assert (token != &eof_token);
635   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
636
637   do
638     {
639       lexer->next_token++;
640       if (lexer->next_token == lexer->last_token)
641         {
642           lexer->next_token = &eof_token;
643           break;
644         }
645
646     }
647   while (lexer->next_token->type == CPP_PURGED);
648
649   cp_lexer_set_source_position_from_token (token);
650
651   /* Provide debugging output.  */
652   if (cp_lexer_debugging_p (lexer))
653     {
654       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
655       cp_lexer_print_token (cp_lexer_debug_stream, token);
656       putc ('\n', cp_lexer_debug_stream);
657     }
658
659   return token;
660 }
661
662 /* Permanently remove the next token from the token stream, and
663    advance the next_token pointer to refer to the next non-purged
664    token.  */
665
666 static void
667 cp_lexer_purge_token (cp_lexer *lexer)
668 {
669   cp_token *tok = lexer->next_token;
670
671   gcc_assert (tok != &eof_token);
672   tok->type = CPP_PURGED;
673   tok->location = UNKNOWN_LOCATION;
674   tok->u.value = NULL_TREE;
675   tok->keyword = RID_MAX;
676
677   do
678     {
679       tok++;
680       if (tok == lexer->last_token)
681         {
682           tok = &eof_token;
683           break;
684         }
685     }
686   while (tok->type == CPP_PURGED);
687   lexer->next_token = tok;
688 }
689
690 /* Permanently remove all tokens after TOK, up to, but not
691    including, the token that will be returned next by
692    cp_lexer_peek_token.  */
693
694 static void
695 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
696 {
697   cp_token *peek = lexer->next_token;
698
699   if (peek == &eof_token)
700     peek = lexer->last_token;
701
702   gcc_assert (tok < peek);
703
704   for ( tok += 1; tok != peek; tok += 1)
705     {
706       tok->type = CPP_PURGED;
707       tok->location = UNKNOWN_LOCATION;
708       tok->u.value = NULL_TREE;
709       tok->keyword = RID_MAX;
710     }
711 }
712
713 /* Begin saving tokens.  All tokens consumed after this point will be
714    preserved.  */
715
716 static void
717 cp_lexer_save_tokens (cp_lexer* lexer)
718 {
719   /* Provide debugging output.  */
720   if (cp_lexer_debugging_p (lexer))
721     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
722
723   VEC_safe_push (cp_token_position, heap,
724                  lexer->saved_tokens, lexer->next_token);
725 }
726
727 /* Commit to the portion of the token stream most recently saved.  */
728
729 static void
730 cp_lexer_commit_tokens (cp_lexer* lexer)
731 {
732   /* Provide debugging output.  */
733   if (cp_lexer_debugging_p (lexer))
734     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
735
736   VEC_pop (cp_token_position, lexer->saved_tokens);
737 }
738
739 /* Return all tokens saved since the last call to cp_lexer_save_tokens
740    to the token stream.  Stop saving tokens.  */
741
742 static void
743 cp_lexer_rollback_tokens (cp_lexer* lexer)
744 {
745   /* Provide debugging output.  */
746   if (cp_lexer_debugging_p (lexer))
747     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
748
749   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
750 }
751
752 /* Print a representation of the TOKEN on the STREAM.  */
753
754 #ifdef ENABLE_CHECKING
755
756 static void
757 cp_lexer_print_token (FILE * stream, cp_token *token)
758 {
759   /* We don't use cpp_type2name here because the parser defines
760      a few tokens of its own.  */
761   static const char *const token_names[] = {
762     /* cpplib-defined token types */
763 #define OP(e, s) #e,
764 #define TK(e, s) #e,
765     TTYPE_TABLE
766 #undef OP
767 #undef TK
768     /* C++ parser token types - see "Manifest constants", above.  */
769     "KEYWORD",
770     "TEMPLATE_ID",
771     "NESTED_NAME_SPECIFIER",
772     "PURGED"
773   };
774
775   /* If we have a name for the token, print it out.  Otherwise, we
776      simply give the numeric code.  */
777   gcc_assert (token->type < ARRAY_SIZE(token_names));
778   fputs (token_names[token->type], stream);
779
780   /* For some tokens, print the associated data.  */
781   switch (token->type)
782     {
783     case CPP_KEYWORD:
784       /* Some keywords have a value that is not an IDENTIFIER_NODE.
785          For example, `struct' is mapped to an INTEGER_CST.  */
786       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
787         break;
788       /* else fall through */
789     case CPP_NAME:
790       fputs (IDENTIFIER_POINTER (token->u.value), stream);
791       break;
792
793     case CPP_STRING:
794     case CPP_STRING16:
795     case CPP_STRING32:
796     case CPP_WSTRING:
797       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
798       break;
799
800     default:
801       break;
802     }
803 }
804
805 /* Start emitting debugging information.  */
806
807 static void
808 cp_lexer_start_debugging (cp_lexer* lexer)
809 {
810   lexer->debugging_p = true;
811 }
812
813 /* Stop emitting debugging information.  */
814
815 static void
816 cp_lexer_stop_debugging (cp_lexer* lexer)
817 {
818   lexer->debugging_p = false;
819 }
820
821 #endif /* ENABLE_CHECKING */
822
823 /* Create a new cp_token_cache, representing a range of tokens.  */
824
825 static cp_token_cache *
826 cp_token_cache_new (cp_token *first, cp_token *last)
827 {
828   cp_token_cache *cache = GGC_NEW (cp_token_cache);
829   cache->first = first;
830   cache->last = last;
831   return cache;
832 }
833
834 \f
835 /* Decl-specifiers.  */
836
837 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
838
839 static void
840 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
841 {
842   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
843 }
844
845 /* Declarators.  */
846
847 /* Nothing other than the parser should be creating declarators;
848    declarators are a semi-syntactic representation of C++ entities.
849    Other parts of the front end that need to create entities (like
850    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
851
852 static cp_declarator *make_call_declarator
853   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
854 static cp_declarator *make_array_declarator
855   (cp_declarator *, tree);
856 static cp_declarator *make_pointer_declarator
857   (cp_cv_quals, cp_declarator *);
858 static cp_declarator *make_reference_declarator
859   (cp_cv_quals, cp_declarator *, bool);
860 static cp_parameter_declarator *make_parameter_declarator
861   (cp_decl_specifier_seq *, cp_declarator *, tree);
862 static cp_declarator *make_ptrmem_declarator
863   (cp_cv_quals, tree, cp_declarator *);
864
865 /* An erroneous declarator.  */
866 static cp_declarator *cp_error_declarator;
867
868 /* The obstack on which declarators and related data structures are
869    allocated.  */
870 static struct obstack declarator_obstack;
871
872 /* Alloc BYTES from the declarator memory pool.  */
873
874 static inline void *
875 alloc_declarator (size_t bytes)
876 {
877   return obstack_alloc (&declarator_obstack, bytes);
878 }
879
880 /* Allocate a declarator of the indicated KIND.  Clear fields that are
881    common to all declarators.  */
882
883 static cp_declarator *
884 make_declarator (cp_declarator_kind kind)
885 {
886   cp_declarator *declarator;
887
888   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889   declarator->kind = kind;
890   declarator->attributes = NULL_TREE;
891   declarator->declarator = NULL;
892   declarator->parameter_pack_p = false;
893
894   return declarator;
895 }
896
897 /* Make a declarator for a generalized identifier.  If
898    QUALIFYING_SCOPE is non-NULL, the identifier is
899    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
900    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
901    is, if any.   */
902
903 static cp_declarator *
904 make_id_declarator (tree qualifying_scope, tree unqualified_name,
905                     special_function_kind sfk)
906 {
907   cp_declarator *declarator;
908
909   /* It is valid to write:
910
911        class C { void f(); };
912        typedef C D;
913        void D::f();
914
915      The standard is not clear about whether `typedef const C D' is
916      legal; as of 2002-09-15 the committee is considering that
917      question.  EDG 3.0 allows that syntax.  Therefore, we do as
918      well.  */
919   if (qualifying_scope && TYPE_P (qualifying_scope))
920     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
921
922   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
923               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
924               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
925
926   declarator = make_declarator (cdk_id);
927   declarator->u.id.qualifying_scope = qualifying_scope;
928   declarator->u.id.unqualified_name = unqualified_name;
929   declarator->u.id.sfk = sfk;
930   
931   return declarator;
932 }
933
934 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
935    of modifiers such as const or volatile to apply to the pointer
936    type, represented as identifiers.  */
937
938 cp_declarator *
939 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
940 {
941   cp_declarator *declarator;
942
943   declarator = make_declarator (cdk_pointer);
944   declarator->declarator = target;
945   declarator->u.pointer.qualifiers = cv_qualifiers;
946   declarator->u.pointer.class_type = NULL_TREE;
947   if (target)
948     {
949       declarator->parameter_pack_p = target->parameter_pack_p;
950       target->parameter_pack_p = false;
951     }
952   else
953     declarator->parameter_pack_p = false;
954
955   return declarator;
956 }
957
958 /* Like make_pointer_declarator -- but for references.  */
959
960 cp_declarator *
961 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
962                            bool rvalue_ref)
963 {
964   cp_declarator *declarator;
965
966   declarator = make_declarator (cdk_reference);
967   declarator->declarator = target;
968   declarator->u.reference.qualifiers = cv_qualifiers;
969   declarator->u.reference.rvalue_ref = rvalue_ref;
970   if (target)
971     {
972       declarator->parameter_pack_p = target->parameter_pack_p;
973       target->parameter_pack_p = false;
974     }
975   else
976     declarator->parameter_pack_p = false;
977
978   return declarator;
979 }
980
981 /* Like make_pointer_declarator -- but for a pointer to a non-static
982    member of CLASS_TYPE.  */
983
984 cp_declarator *
985 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
986                         cp_declarator *pointee)
987 {
988   cp_declarator *declarator;
989
990   declarator = make_declarator (cdk_ptrmem);
991   declarator->declarator = pointee;
992   declarator->u.pointer.qualifiers = cv_qualifiers;
993   declarator->u.pointer.class_type = class_type;
994
995   if (pointee)
996     {
997       declarator->parameter_pack_p = pointee->parameter_pack_p;
998       pointee->parameter_pack_p = false;
999     }
1000   else
1001     declarator->parameter_pack_p = false;
1002
1003   return declarator;
1004 }
1005
1006 /* Make a declarator for the function given by TARGET, with the
1007    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1008    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1009    indicates what exceptions can be thrown.  */
1010
1011 cp_declarator *
1012 make_call_declarator (cp_declarator *target,
1013                       cp_parameter_declarator *parms,
1014                       cp_cv_quals cv_qualifiers,
1015                       tree exception_specification)
1016 {
1017   cp_declarator *declarator;
1018
1019   declarator = make_declarator (cdk_function);
1020   declarator->declarator = target;
1021   declarator->u.function.parameters = parms;
1022   declarator->u.function.qualifiers = cv_qualifiers;
1023   declarator->u.function.exception_specification = exception_specification;
1024   if (target)
1025     {
1026       declarator->parameter_pack_p = target->parameter_pack_p;
1027       target->parameter_pack_p = false;
1028     }
1029   else
1030     declarator->parameter_pack_p = false;
1031
1032   return declarator;
1033 }
1034
1035 /* Make a declarator for an array of BOUNDS elements, each of which is
1036    defined by ELEMENT.  */
1037
1038 cp_declarator *
1039 make_array_declarator (cp_declarator *element, tree bounds)
1040 {
1041   cp_declarator *declarator;
1042
1043   declarator = make_declarator (cdk_array);
1044   declarator->declarator = element;
1045   declarator->u.array.bounds = bounds;
1046   if (element)
1047     {
1048       declarator->parameter_pack_p = element->parameter_pack_p;
1049       element->parameter_pack_p = false;
1050     }
1051   else
1052     declarator->parameter_pack_p = false;
1053
1054   return declarator;
1055 }
1056
1057 /* Determine whether the declarator we've seen so far can be a
1058    parameter pack, when followed by an ellipsis.  */
1059 static bool 
1060 declarator_can_be_parameter_pack (cp_declarator *declarator)
1061 {
1062   /* Search for a declarator name, or any other declarator that goes
1063      after the point where the ellipsis could appear in a parameter
1064      pack. If we find any of these, then this declarator can not be
1065      made into a parameter pack.  */
1066   bool found = false;
1067   while (declarator && !found)
1068     {
1069       switch ((int)declarator->kind)
1070         {
1071         case cdk_id:
1072         case cdk_array:
1073           found = true;
1074           break;
1075
1076         case cdk_error:
1077           return true;
1078
1079         default:
1080           declarator = declarator->declarator;
1081           break;
1082         }
1083     }
1084
1085   return !found;
1086 }
1087
1088 cp_parameter_declarator *no_parameters;
1089
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091    DECLARATOR and DEFAULT_ARGUMENT.  */
1092
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095                            cp_declarator *declarator,
1096                            tree default_argument)
1097 {
1098   cp_parameter_declarator *parameter;
1099
1100   parameter = ((cp_parameter_declarator *)
1101                alloc_declarator (sizeof (cp_parameter_declarator)));
1102   parameter->next = NULL;
1103   if (decl_specifiers)
1104     parameter->decl_specifiers = *decl_specifiers;
1105   else
1106     clear_decl_specs (&parameter->decl_specifiers);
1107   parameter->declarator = declarator;
1108   parameter->default_argument = default_argument;
1109   parameter->ellipsis_p = false;
1110
1111   return parameter;
1112 }
1113
1114 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1115
1116 static bool
1117 function_declarator_p (const cp_declarator *declarator)
1118 {
1119   while (declarator)
1120     {
1121       if (declarator->kind == cdk_function
1122           && declarator->declarator->kind == cdk_id)
1123         return true;
1124       if (declarator->kind == cdk_id
1125           || declarator->kind == cdk_error)
1126         return false;
1127       declarator = declarator->declarator;
1128     }
1129   return false;
1130 }
1131  
1132 /* The parser.  */
1133
1134 /* Overview
1135    --------
1136
1137    A cp_parser parses the token stream as specified by the C++
1138    grammar.  Its job is purely parsing, not semantic analysis.  For
1139    example, the parser breaks the token stream into declarators,
1140    expressions, statements, and other similar syntactic constructs.
1141    It does not check that the types of the expressions on either side
1142    of an assignment-statement are compatible, or that a function is
1143    not declared with a parameter of type `void'.
1144
1145    The parser invokes routines elsewhere in the compiler to perform
1146    semantic analysis and to build up the abstract syntax tree for the
1147    code processed.
1148
1149    The parser (and the template instantiation code, which is, in a
1150    way, a close relative of parsing) are the only parts of the
1151    compiler that should be calling push_scope and pop_scope, or
1152    related functions.  The parser (and template instantiation code)
1153    keeps track of what scope is presently active; everything else
1154    should simply honor that.  (The code that generates static
1155    initializers may also need to set the scope, in order to check
1156    access control correctly when emitting the initializers.)
1157
1158    Methodology
1159    -----------
1160
1161    The parser is of the standard recursive-descent variety.  Upcoming
1162    tokens in the token stream are examined in order to determine which
1163    production to use when parsing a non-terminal.  Some C++ constructs
1164    require arbitrary look ahead to disambiguate.  For example, it is
1165    impossible, in the general case, to tell whether a statement is an
1166    expression or declaration without scanning the entire statement.
1167    Therefore, the parser is capable of "parsing tentatively."  When the
1168    parser is not sure what construct comes next, it enters this mode.
1169    Then, while we attempt to parse the construct, the parser queues up
1170    error messages, rather than issuing them immediately, and saves the
1171    tokens it consumes.  If the construct is parsed successfully, the
1172    parser "commits", i.e., it issues any queued error messages and
1173    the tokens that were being preserved are permanently discarded.
1174    If, however, the construct is not parsed successfully, the parser
1175    rolls back its state completely so that it can resume parsing using
1176    a different alternative.
1177
1178    Future Improvements
1179    -------------------
1180
1181    The performance of the parser could probably be improved substantially.
1182    We could often eliminate the need to parse tentatively by looking ahead
1183    a little bit.  In some places, this approach might not entirely eliminate
1184    the need to parse tentatively, but it might still speed up the average
1185    case.  */
1186
1187 /* Flags that are passed to some parsing functions.  These values can
1188    be bitwise-ored together.  */
1189
1190 typedef enum cp_parser_flags
1191 {
1192   /* No flags.  */
1193   CP_PARSER_FLAGS_NONE = 0x0,
1194   /* The construct is optional.  If it is not present, then no error
1195      should be issued.  */
1196   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197   /* When parsing a type-specifier, do not allow user-defined types.  */
1198   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1199 } cp_parser_flags;
1200
1201 /* The different kinds of declarators we want to parse.  */
1202
1203 typedef enum cp_parser_declarator_kind
1204 {
1205   /* We want an abstract declarator.  */
1206   CP_PARSER_DECLARATOR_ABSTRACT,
1207   /* We want a named declarator.  */
1208   CP_PARSER_DECLARATOR_NAMED,
1209   /* We don't mind, but the name must be an unqualified-id.  */
1210   CP_PARSER_DECLARATOR_EITHER
1211 } cp_parser_declarator_kind;
1212
1213 /* The precedence values used to parse binary expressions.  The minimum value
1214    of PREC must be 1, because zero is reserved to quickly discriminate
1215    binary operators from other tokens.  */
1216
1217 enum cp_parser_prec
1218 {
1219   PREC_NOT_OPERATOR,
1220   PREC_LOGICAL_OR_EXPRESSION,
1221   PREC_LOGICAL_AND_EXPRESSION,
1222   PREC_INCLUSIVE_OR_EXPRESSION,
1223   PREC_EXCLUSIVE_OR_EXPRESSION,
1224   PREC_AND_EXPRESSION,
1225   PREC_EQUALITY_EXPRESSION,
1226   PREC_RELATIONAL_EXPRESSION,
1227   PREC_SHIFT_EXPRESSION,
1228   PREC_ADDITIVE_EXPRESSION,
1229   PREC_MULTIPLICATIVE_EXPRESSION,
1230   PREC_PM_EXPRESSION,
1231   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1232 };
1233
1234 /* A mapping from a token type to a corresponding tree node type, with a
1235    precedence value.  */
1236
1237 typedef struct cp_parser_binary_operations_map_node
1238 {
1239   /* The token type.  */
1240   enum cpp_ttype token_type;
1241   /* The corresponding tree code.  */
1242   enum tree_code tree_type;
1243   /* The precedence of this operator.  */
1244   enum cp_parser_prec prec;
1245 } cp_parser_binary_operations_map_node;
1246
1247 /* The status of a tentative parse.  */
1248
1249 typedef enum cp_parser_status_kind
1250 {
1251   /* No errors have occurred.  */
1252   CP_PARSER_STATUS_KIND_NO_ERROR,
1253   /* An error has occurred.  */
1254   CP_PARSER_STATUS_KIND_ERROR,
1255   /* We are committed to this tentative parse, whether or not an error
1256      has occurred.  */
1257   CP_PARSER_STATUS_KIND_COMMITTED
1258 } cp_parser_status_kind;
1259
1260 typedef struct cp_parser_expression_stack_entry
1261 {
1262   /* Left hand side of the binary operation we are currently
1263      parsing.  */
1264   tree lhs;
1265   /* Original tree code for left hand side, if it was a binary
1266      expression itself (used for -Wparentheses).  */
1267   enum tree_code lhs_type;
1268   /* Tree code for the binary operation we are parsing.  */
1269   enum tree_code tree_type;
1270   /* Precedence of the binary operation we are parsing.  */
1271   int prec;
1272 } cp_parser_expression_stack_entry;
1273
1274 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1275    entries because precedence levels on the stack are monotonically
1276    increasing.  */
1277 typedef struct cp_parser_expression_stack_entry
1278   cp_parser_expression_stack[NUM_PREC_VALUES];
1279
1280 /* Context that is saved and restored when parsing tentatively.  */
1281 typedef struct cp_parser_context GTY (())
1282 {
1283   /* If this is a tentative parsing context, the status of the
1284      tentative parse.  */
1285   enum cp_parser_status_kind status;
1286   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1287      that are looked up in this context must be looked up both in the
1288      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1289      the context of the containing expression.  */
1290   tree object_type;
1291
1292   /* The next parsing context in the stack.  */
1293   struct cp_parser_context *next;
1294 } cp_parser_context;
1295
1296 /* Prototypes.  */
1297
1298 /* Constructors and destructors.  */
1299
1300 static cp_parser_context *cp_parser_context_new
1301   (cp_parser_context *);
1302
1303 /* Class variables.  */
1304
1305 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1306
1307 /* The operator-precedence table used by cp_parser_binary_expression.
1308    Transformed into an associative array (binops_by_token) by
1309    cp_parser_new.  */
1310
1311 static const cp_parser_binary_operations_map_node binops[] = {
1312   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1313   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1314
1315   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1316   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318
1319   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1320   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1321
1322   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1323   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1324
1325   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1326   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1327   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1328   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1329
1330   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1331   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1332
1333   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1334
1335   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1336
1337   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1338
1339   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1340
1341   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1342 };
1343
1344 /* The same as binops, but initialized by cp_parser_new so that
1345    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1346    for speed.  */
1347 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1348
1349 /* Constructors and destructors.  */
1350
1351 /* Construct a new context.  The context below this one on the stack
1352    is given by NEXT.  */
1353
1354 static cp_parser_context *
1355 cp_parser_context_new (cp_parser_context* next)
1356 {
1357   cp_parser_context *context;
1358
1359   /* Allocate the storage.  */
1360   if (cp_parser_context_free_list != NULL)
1361     {
1362       /* Pull the first entry from the free list.  */
1363       context = cp_parser_context_free_list;
1364       cp_parser_context_free_list = context->next;
1365       memset (context, 0, sizeof (*context));
1366     }
1367   else
1368     context = GGC_CNEW (cp_parser_context);
1369
1370   /* No errors have occurred yet in this context.  */
1371   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1372   /* If this is not the bottomost context, copy information that we
1373      need from the previous context.  */
1374   if (next)
1375     {
1376       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1377          expression, then we are parsing one in this context, too.  */
1378       context->object_type = next->object_type;
1379       /* Thread the stack.  */
1380       context->next = next;
1381     }
1382
1383   return context;
1384 }
1385
1386 /* The cp_parser structure represents the C++ parser.  */
1387
1388 typedef struct cp_parser GTY(())
1389 {
1390   /* The lexer from which we are obtaining tokens.  */
1391   cp_lexer *lexer;
1392
1393   /* The scope in which names should be looked up.  If NULL_TREE, then
1394      we look up names in the scope that is currently open in the
1395      source program.  If non-NULL, this is either a TYPE or
1396      NAMESPACE_DECL for the scope in which we should look.  It can
1397      also be ERROR_MARK, when we've parsed a bogus scope.
1398
1399      This value is not cleared automatically after a name is looked
1400      up, so we must be careful to clear it before starting a new look
1401      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1402      will look up `Z' in the scope of `X', rather than the current
1403      scope.)  Unfortunately, it is difficult to tell when name lookup
1404      is complete, because we sometimes peek at a token, look it up,
1405      and then decide not to consume it.   */
1406   tree scope;
1407
1408   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1409      last lookup took place.  OBJECT_SCOPE is used if an expression
1410      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1411      respectively.  QUALIFYING_SCOPE is used for an expression of the
1412      form "X::Y"; it refers to X.  */
1413   tree object_scope;
1414   tree qualifying_scope;
1415
1416   /* A stack of parsing contexts.  All but the bottom entry on the
1417      stack will be tentative contexts.
1418
1419      We parse tentatively in order to determine which construct is in
1420      use in some situations.  For example, in order to determine
1421      whether a statement is an expression-statement or a
1422      declaration-statement we parse it tentatively as a
1423      declaration-statement.  If that fails, we then reparse the same
1424      token stream as an expression-statement.  */
1425   cp_parser_context *context;
1426
1427   /* True if we are parsing GNU C++.  If this flag is not set, then
1428      GNU extensions are not recognized.  */
1429   bool allow_gnu_extensions_p;
1430
1431   /* TRUE if the `>' token should be interpreted as the greater-than
1432      operator.  FALSE if it is the end of a template-id or
1433      template-parameter-list. In C++0x mode, this flag also applies to
1434      `>>' tokens, which are viewed as two consecutive `>' tokens when
1435      this flag is FALSE.  */
1436   bool greater_than_is_operator_p;
1437
1438   /* TRUE if default arguments are allowed within a parameter list
1439      that starts at this point. FALSE if only a gnu extension makes
1440      them permissible.  */
1441   bool default_arg_ok_p;
1442
1443   /* TRUE if we are parsing an integral constant-expression.  See
1444      [expr.const] for a precise definition.  */
1445   bool integral_constant_expression_p;
1446
1447   /* TRUE if we are parsing an integral constant-expression -- but a
1448      non-constant expression should be permitted as well.  This flag
1449      is used when parsing an array bound so that GNU variable-length
1450      arrays are tolerated.  */
1451   bool allow_non_integral_constant_expression_p;
1452
1453   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1454      been seen that makes the expression non-constant.  */
1455   bool non_integral_constant_expression_p;
1456
1457   /* TRUE if local variable names and `this' are forbidden in the
1458      current context.  */
1459   bool local_variables_forbidden_p;
1460
1461   /* TRUE if the declaration we are parsing is part of a
1462      linkage-specification of the form `extern string-literal
1463      declaration'.  */
1464   bool in_unbraced_linkage_specification_p;
1465
1466   /* TRUE if we are presently parsing a declarator, after the
1467      direct-declarator.  */
1468   bool in_declarator_p;
1469
1470   /* TRUE if we are presently parsing a template-argument-list.  */
1471   bool in_template_argument_list_p;
1472
1473   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1474      to IN_OMP_BLOCK if parsing OpenMP structured block and
1475      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1476      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1477      iteration-statement, OpenMP block or loop within that switch.  */
1478 #define IN_SWITCH_STMT          1
1479 #define IN_ITERATION_STMT       2
1480 #define IN_OMP_BLOCK            4
1481 #define IN_OMP_FOR              8
1482 #define IN_IF_STMT             16
1483   unsigned char in_statement;
1484
1485   /* TRUE if we are presently parsing the body of a switch statement.
1486      Note that this doesn't quite overlap with in_statement above.
1487      The difference relates to giving the right sets of error messages:
1488      "case not in switch" vs "break statement used with OpenMP...".  */
1489   bool in_switch_statement_p;
1490
1491   /* TRUE if we are parsing a type-id in an expression context.  In
1492      such a situation, both "type (expr)" and "type (type)" are valid
1493      alternatives.  */
1494   bool in_type_id_in_expr_p;
1495
1496   /* TRUE if we are currently in a header file where declarations are
1497      implicitly extern "C".  */
1498   bool implicit_extern_c;
1499
1500   /* TRUE if strings in expressions should be translated to the execution
1501      character set.  */
1502   bool translate_strings_p;
1503
1504   /* TRUE if we are presently parsing the body of a function, but not
1505      a local class.  */
1506   bool in_function_body;
1507
1508   /* If non-NULL, then we are parsing a construct where new type
1509      definitions are not permitted.  The string stored here will be
1510      issued as an error message if a type is defined.  */
1511   const char *type_definition_forbidden_message;
1512
1513   /* A list of lists. The outer list is a stack, used for member
1514      functions of local classes. At each level there are two sub-list,
1515      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1516      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1517      TREE_VALUE's. The functions are chained in reverse declaration
1518      order.
1519
1520      The TREE_PURPOSE sublist contains those functions with default
1521      arguments that need post processing, and the TREE_VALUE sublist
1522      contains those functions with definitions that need post
1523      processing.
1524
1525      These lists can only be processed once the outermost class being
1526      defined is complete.  */
1527   tree unparsed_functions_queues;
1528
1529   /* The number of classes whose definitions are currently in
1530      progress.  */
1531   unsigned num_classes_being_defined;
1532
1533   /* The number of template parameter lists that apply directly to the
1534      current declaration.  */
1535   unsigned num_template_parameter_lists;
1536 } cp_parser;
1537
1538 /* Prototypes.  */
1539
1540 /* Constructors and destructors.  */
1541
1542 static cp_parser *cp_parser_new
1543   (void);
1544
1545 /* Routines to parse various constructs.
1546
1547    Those that return `tree' will return the error_mark_node (rather
1548    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1549    Sometimes, they will return an ordinary node if error-recovery was
1550    attempted, even though a parse error occurred.  So, to check
1551    whether or not a parse error occurred, you should always use
1552    cp_parser_error_occurred.  If the construct is optional (indicated
1553    either by an `_opt' in the name of the function that does the
1554    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1555    the construct is not present.  */
1556
1557 /* Lexical conventions [gram.lex]  */
1558
1559 static tree cp_parser_identifier
1560   (cp_parser *);
1561 static tree cp_parser_string_literal
1562   (cp_parser *, bool, bool);
1563
1564 /* Basic concepts [gram.basic]  */
1565
1566 static bool cp_parser_translation_unit
1567   (cp_parser *);
1568
1569 /* Expressions [gram.expr]  */
1570
1571 static tree cp_parser_primary_expression
1572   (cp_parser *, bool, bool, bool, cp_id_kind *);
1573 static tree cp_parser_id_expression
1574   (cp_parser *, bool, bool, bool *, bool, bool);
1575 static tree cp_parser_unqualified_id
1576   (cp_parser *, bool, bool, bool, bool);
1577 static tree cp_parser_nested_name_specifier_opt
1578   (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier
1580   (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_class_or_namespace_name
1582   (cp_parser *, bool, bool, bool, bool, bool);
1583 static tree cp_parser_postfix_expression
1584   (cp_parser *, bool, bool, bool);
1585 static tree cp_parser_postfix_open_square_expression
1586   (cp_parser *, tree, bool);
1587 static tree cp_parser_postfix_dot_deref_expression
1588   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1589 static tree cp_parser_parenthesized_expression_list
1590   (cp_parser *, bool, bool, bool, bool *);
1591 static void cp_parser_pseudo_destructor_name
1592   (cp_parser *, tree *, tree *);
1593 static tree cp_parser_unary_expression
1594   (cp_parser *, bool, bool);
1595 static enum tree_code cp_parser_unary_operator
1596   (cp_token *);
1597 static tree cp_parser_new_expression
1598   (cp_parser *);
1599 static tree cp_parser_new_placement
1600   (cp_parser *);
1601 static tree cp_parser_new_type_id
1602   (cp_parser *, tree *);
1603 static cp_declarator *cp_parser_new_declarator_opt
1604   (cp_parser *);
1605 static cp_declarator *cp_parser_direct_new_declarator
1606   (cp_parser *);
1607 static tree cp_parser_new_initializer
1608   (cp_parser *);
1609 static tree cp_parser_delete_expression
1610   (cp_parser *);
1611 static tree cp_parser_cast_expression
1612   (cp_parser *, bool, bool);
1613 static tree cp_parser_binary_expression
1614   (cp_parser *, bool, enum cp_parser_prec);
1615 static tree cp_parser_question_colon_clause
1616   (cp_parser *, tree);
1617 static tree cp_parser_assignment_expression
1618   (cp_parser *, bool);
1619 static enum tree_code cp_parser_assignment_operator_opt
1620   (cp_parser *);
1621 static tree cp_parser_expression
1622   (cp_parser *, bool);
1623 static tree cp_parser_constant_expression
1624   (cp_parser *, bool, bool *);
1625 static tree cp_parser_builtin_offsetof
1626   (cp_parser *);
1627
1628 /* Statements [gram.stmt.stmt]  */
1629
1630 static void cp_parser_statement
1631   (cp_parser *, tree, bool, bool *);
1632 static void cp_parser_label_for_labeled_statement
1633   (cp_parser *);
1634 static tree cp_parser_expression_statement
1635   (cp_parser *, tree);
1636 static tree cp_parser_compound_statement
1637   (cp_parser *, tree, bool);
1638 static void cp_parser_statement_seq_opt
1639   (cp_parser *, tree);
1640 static tree cp_parser_selection_statement
1641   (cp_parser *, bool *);
1642 static tree cp_parser_condition
1643   (cp_parser *);
1644 static tree cp_parser_iteration_statement
1645   (cp_parser *);
1646 static void cp_parser_for_init_statement
1647   (cp_parser *);
1648 static tree cp_parser_jump_statement
1649   (cp_parser *);
1650 static void cp_parser_declaration_statement
1651   (cp_parser *);
1652
1653 static tree cp_parser_implicitly_scoped_statement
1654   (cp_parser *, bool *);
1655 static void cp_parser_already_scoped_statement
1656   (cp_parser *);
1657
1658 /* Declarations [gram.dcl.dcl] */
1659
1660 static void cp_parser_declaration_seq_opt
1661   (cp_parser *);
1662 static void cp_parser_declaration
1663   (cp_parser *);
1664 static void cp_parser_block_declaration
1665   (cp_parser *, bool);
1666 static void cp_parser_simple_declaration
1667   (cp_parser *, bool);
1668 static void cp_parser_decl_specifier_seq
1669   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1670 static tree cp_parser_storage_class_specifier_opt
1671   (cp_parser *);
1672 static tree cp_parser_function_specifier_opt
1673   (cp_parser *, cp_decl_specifier_seq *);
1674 static tree cp_parser_type_specifier
1675   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1676    int *, bool *);
1677 static tree cp_parser_simple_type_specifier
1678   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1679 static tree cp_parser_type_name
1680   (cp_parser *);
1681 static tree cp_parser_nonclass_name 
1682   (cp_parser* parser);
1683 static tree cp_parser_elaborated_type_specifier
1684   (cp_parser *, bool, bool);
1685 static tree cp_parser_enum_specifier
1686   (cp_parser *);
1687 static void cp_parser_enumerator_list
1688   (cp_parser *, tree);
1689 static void cp_parser_enumerator_definition
1690   (cp_parser *, tree);
1691 static tree cp_parser_namespace_name
1692   (cp_parser *);
1693 static void cp_parser_namespace_definition
1694   (cp_parser *);
1695 static void cp_parser_namespace_body
1696   (cp_parser *);
1697 static tree cp_parser_qualified_namespace_specifier
1698   (cp_parser *);
1699 static void cp_parser_namespace_alias_definition
1700   (cp_parser *);
1701 static bool cp_parser_using_declaration
1702   (cp_parser *, bool);
1703 static void cp_parser_using_directive
1704   (cp_parser *);
1705 static void cp_parser_asm_definition
1706   (cp_parser *);
1707 static void cp_parser_linkage_specification
1708   (cp_parser *);
1709 static void cp_parser_static_assert
1710   (cp_parser *, bool);
1711 static tree cp_parser_decltype
1712   (cp_parser *);
1713
1714 /* Declarators [gram.dcl.decl] */
1715
1716 static tree cp_parser_init_declarator
1717   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1718 static cp_declarator *cp_parser_declarator
1719   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1720 static cp_declarator *cp_parser_direct_declarator
1721   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1722 static enum tree_code cp_parser_ptr_operator
1723   (cp_parser *, tree *, cp_cv_quals *);
1724 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1725   (cp_parser *);
1726 static tree cp_parser_declarator_id
1727   (cp_parser *, bool);
1728 static tree cp_parser_type_id
1729   (cp_parser *);
1730 static void cp_parser_type_specifier_seq
1731   (cp_parser *, bool, cp_decl_specifier_seq *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1733   (cp_parser *);
1734 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1735   (cp_parser *, bool *);
1736 static cp_parameter_declarator *cp_parser_parameter_declaration
1737   (cp_parser *, bool, bool *);
1738 static tree cp_parser_default_argument 
1739   (cp_parser *, bool);
1740 static void cp_parser_function_body
1741   (cp_parser *);
1742 static tree cp_parser_initializer
1743   (cp_parser *, bool *, bool *);
1744 static tree cp_parser_initializer_clause
1745   (cp_parser *, bool *);
1746 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1747   (cp_parser *, bool *);
1748
1749 static bool cp_parser_ctor_initializer_opt_and_function_body
1750   (cp_parser *);
1751
1752 /* Classes [gram.class] */
1753
1754 static tree cp_parser_class_name
1755   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1756 static tree cp_parser_class_specifier
1757   (cp_parser *);
1758 static tree cp_parser_class_head
1759   (cp_parser *, bool *, tree *, tree *);
1760 static enum tag_types cp_parser_class_key
1761   (cp_parser *);
1762 static void cp_parser_member_specification_opt
1763   (cp_parser *);
1764 static void cp_parser_member_declaration
1765   (cp_parser *);
1766 static tree cp_parser_pure_specifier
1767   (cp_parser *);
1768 static tree cp_parser_constant_initializer
1769   (cp_parser *);
1770
1771 /* Derived classes [gram.class.derived] */
1772
1773 static tree cp_parser_base_clause
1774   (cp_parser *);
1775 static tree cp_parser_base_specifier
1776   (cp_parser *);
1777
1778 /* Special member functions [gram.special] */
1779
1780 static tree cp_parser_conversion_function_id
1781   (cp_parser *);
1782 static tree cp_parser_conversion_type_id
1783   (cp_parser *);
1784 static cp_declarator *cp_parser_conversion_declarator_opt
1785   (cp_parser *);
1786 static bool cp_parser_ctor_initializer_opt
1787   (cp_parser *);
1788 static void cp_parser_mem_initializer_list
1789   (cp_parser *);
1790 static tree cp_parser_mem_initializer
1791   (cp_parser *);
1792 static tree cp_parser_mem_initializer_id
1793   (cp_parser *);
1794
1795 /* Overloading [gram.over] */
1796
1797 static tree cp_parser_operator_function_id
1798   (cp_parser *);
1799 static tree cp_parser_operator
1800   (cp_parser *);
1801
1802 /* Templates [gram.temp] */
1803
1804 static void cp_parser_template_declaration
1805   (cp_parser *, bool);
1806 static tree cp_parser_template_parameter_list
1807   (cp_parser *);
1808 static tree cp_parser_template_parameter
1809   (cp_parser *, bool *, bool *);
1810 static tree cp_parser_type_parameter
1811   (cp_parser *, bool *);
1812 static tree cp_parser_template_id
1813   (cp_parser *, bool, bool, bool);
1814 static tree cp_parser_template_name
1815   (cp_parser *, bool, bool, bool, bool *);
1816 static tree cp_parser_template_argument_list
1817   (cp_parser *);
1818 static tree cp_parser_template_argument
1819   (cp_parser *);
1820 static void cp_parser_explicit_instantiation
1821   (cp_parser *);
1822 static void cp_parser_explicit_specialization
1823   (cp_parser *);
1824
1825 /* Exception handling [gram.exception] */
1826
1827 static tree cp_parser_try_block
1828   (cp_parser *);
1829 static bool cp_parser_function_try_block
1830   (cp_parser *);
1831 static void cp_parser_handler_seq
1832   (cp_parser *);
1833 static void cp_parser_handler
1834   (cp_parser *);
1835 static tree cp_parser_exception_declaration
1836   (cp_parser *);
1837 static tree cp_parser_throw_expression
1838   (cp_parser *);
1839 static tree cp_parser_exception_specification_opt
1840   (cp_parser *);
1841 static tree cp_parser_type_id_list
1842   (cp_parser *);
1843
1844 /* GNU Extensions */
1845
1846 static tree cp_parser_asm_specification_opt
1847   (cp_parser *);
1848 static tree cp_parser_asm_operand_list
1849   (cp_parser *);
1850 static tree cp_parser_asm_clobber_list
1851   (cp_parser *);
1852 static tree cp_parser_attributes_opt
1853   (cp_parser *);
1854 static tree cp_parser_attribute_list
1855   (cp_parser *);
1856 static bool cp_parser_extension_opt
1857   (cp_parser *, int *);
1858 static void cp_parser_label_declaration
1859   (cp_parser *);
1860
1861 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1862 static bool cp_parser_pragma
1863   (cp_parser *, enum pragma_context);
1864
1865 /* Objective-C++ Productions */
1866
1867 static tree cp_parser_objc_message_receiver
1868   (cp_parser *);
1869 static tree cp_parser_objc_message_args
1870   (cp_parser *);
1871 static tree cp_parser_objc_message_expression
1872   (cp_parser *);
1873 static tree cp_parser_objc_encode_expression
1874   (cp_parser *);
1875 static tree cp_parser_objc_defs_expression
1876   (cp_parser *);
1877 static tree cp_parser_objc_protocol_expression
1878   (cp_parser *);
1879 static tree cp_parser_objc_selector_expression
1880   (cp_parser *);
1881 static tree cp_parser_objc_expression
1882   (cp_parser *);
1883 static bool cp_parser_objc_selector_p
1884   (enum cpp_ttype);
1885 static tree cp_parser_objc_selector
1886   (cp_parser *);
1887 static tree cp_parser_objc_protocol_refs_opt
1888   (cp_parser *);
1889 static void cp_parser_objc_declaration
1890   (cp_parser *);
1891 static tree cp_parser_objc_statement
1892   (cp_parser *);
1893
1894 /* Utility Routines */
1895
1896 static tree cp_parser_lookup_name
1897   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1898 static tree cp_parser_lookup_name_simple
1899   (cp_parser *, tree);
1900 static tree cp_parser_maybe_treat_template_as_class
1901   (tree, bool);
1902 static bool cp_parser_check_declarator_template_parameters
1903   (cp_parser *, cp_declarator *);
1904 static bool cp_parser_check_template_parameters
1905   (cp_parser *, unsigned);
1906 static tree cp_parser_simple_cast_expression
1907   (cp_parser *);
1908 static tree cp_parser_global_scope_opt
1909   (cp_parser *, bool);
1910 static bool cp_parser_constructor_declarator_p
1911   (cp_parser *, bool);
1912 static tree cp_parser_function_definition_from_specifiers_and_declarator
1913   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1914 static tree cp_parser_function_definition_after_declarator
1915   (cp_parser *, bool);
1916 static void cp_parser_template_declaration_after_export
1917   (cp_parser *, bool);
1918 static void cp_parser_perform_template_parameter_access_checks
1919   (VEC (deferred_access_check,gc)*);
1920 static tree cp_parser_single_declaration
1921   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1922 static tree cp_parser_functional_cast
1923   (cp_parser *, tree);
1924 static tree cp_parser_save_member_function_body
1925   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1926 static tree cp_parser_enclosed_template_argument_list
1927   (cp_parser *);
1928 static void cp_parser_save_default_args
1929   (cp_parser *, tree);
1930 static void cp_parser_late_parsing_for_member
1931   (cp_parser *, tree);
1932 static void cp_parser_late_parsing_default_args
1933   (cp_parser *, tree);
1934 static tree cp_parser_sizeof_operand
1935   (cp_parser *, enum rid);
1936 static tree cp_parser_trait_expr
1937   (cp_parser *, enum rid);
1938 static bool cp_parser_declares_only_class_p
1939   (cp_parser *);
1940 static void cp_parser_set_storage_class
1941   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1942 static void cp_parser_set_decl_spec_type
1943   (cp_decl_specifier_seq *, tree, bool);
1944 static bool cp_parser_friend_p
1945   (const cp_decl_specifier_seq *);
1946 static cp_token *cp_parser_require
1947   (cp_parser *, enum cpp_ttype, const char *);
1948 static cp_token *cp_parser_require_keyword
1949   (cp_parser *, enum rid, const char *);
1950 static bool cp_parser_token_starts_function_definition_p
1951   (cp_token *);
1952 static bool cp_parser_next_token_starts_class_definition_p
1953   (cp_parser *);
1954 static bool cp_parser_next_token_ends_template_argument_p
1955   (cp_parser *);
1956 static bool cp_parser_nth_token_starts_template_argument_list_p
1957   (cp_parser *, size_t);
1958 static enum tag_types cp_parser_token_is_class_key
1959   (cp_token *);
1960 static void cp_parser_check_class_key
1961   (enum tag_types, tree type);
1962 static void cp_parser_check_access_in_redeclaration
1963   (tree type);
1964 static bool cp_parser_optional_template_keyword
1965   (cp_parser *);
1966 static void cp_parser_pre_parsed_nested_name_specifier
1967   (cp_parser *);
1968 static void cp_parser_cache_group
1969   (cp_parser *, enum cpp_ttype, unsigned);
1970 static void cp_parser_parse_tentatively
1971   (cp_parser *);
1972 static void cp_parser_commit_to_tentative_parse
1973   (cp_parser *);
1974 static void cp_parser_abort_tentative_parse
1975   (cp_parser *);
1976 static bool cp_parser_parse_definitely
1977   (cp_parser *);
1978 static inline bool cp_parser_parsing_tentatively
1979   (cp_parser *);
1980 static bool cp_parser_uncommitted_to_tentative_parse_p
1981   (cp_parser *);
1982 static void cp_parser_error
1983   (cp_parser *, const char *);
1984 static void cp_parser_name_lookup_error
1985   (cp_parser *, tree, tree, const char *);
1986 static bool cp_parser_simulate_error
1987   (cp_parser *);
1988 static bool cp_parser_check_type_definition
1989   (cp_parser *);
1990 static void cp_parser_check_for_definition_in_return_type
1991   (cp_declarator *, tree);
1992 static void cp_parser_check_for_invalid_template_id
1993   (cp_parser *, tree);
1994 static bool cp_parser_non_integral_constant_expression
1995   (cp_parser *, const char *);
1996 static void cp_parser_diagnose_invalid_type_name
1997   (cp_parser *, tree, tree);
1998 static bool cp_parser_parse_and_diagnose_invalid_type_name
1999   (cp_parser *);
2000 static int cp_parser_skip_to_closing_parenthesis
2001   (cp_parser *, bool, bool, bool);
2002 static void cp_parser_skip_to_end_of_statement
2003   (cp_parser *);
2004 static void cp_parser_consume_semicolon_at_end_of_statement
2005   (cp_parser *);
2006 static void cp_parser_skip_to_end_of_block_or_statement
2007   (cp_parser *);
2008 static bool cp_parser_skip_to_closing_brace
2009   (cp_parser *);
2010 static void cp_parser_skip_to_end_of_template_parameter_list
2011   (cp_parser *);
2012 static void cp_parser_skip_to_pragma_eol
2013   (cp_parser*, cp_token *);
2014 static bool cp_parser_error_occurred
2015   (cp_parser *);
2016 static bool cp_parser_allow_gnu_extensions_p
2017   (cp_parser *);
2018 static bool cp_parser_is_string_literal
2019   (cp_token *);
2020 static bool cp_parser_is_keyword
2021   (cp_token *, enum rid);
2022 static tree cp_parser_make_typename_type
2023   (cp_parser *, tree, tree);
2024 static cp_declarator * cp_parser_make_indirect_declarator
2025   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2026
2027 /* Returns nonzero if we are parsing tentatively.  */
2028
2029 static inline bool
2030 cp_parser_parsing_tentatively (cp_parser* parser)
2031 {
2032   return parser->context->next != NULL;
2033 }
2034
2035 /* Returns nonzero if TOKEN is a string literal.  */
2036
2037 static bool
2038 cp_parser_is_string_literal (cp_token* token)
2039 {
2040   return (token->type == CPP_STRING ||
2041           token->type == CPP_STRING16 ||
2042           token->type == CPP_STRING32 ||
2043           token->type == CPP_WSTRING);
2044 }
2045
2046 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2047
2048 static bool
2049 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2050 {
2051   return token->keyword == keyword;
2052 }
2053
2054 /* If not parsing tentatively, issue a diagnostic of the form
2055       FILE:LINE: MESSAGE before TOKEN
2056    where TOKEN is the next token in the input stream.  MESSAGE
2057    (specified by the caller) is usually of the form "expected
2058    OTHER-TOKEN".  */
2059
2060 static void
2061 cp_parser_error (cp_parser* parser, const char* message)
2062 {
2063   if (!cp_parser_simulate_error (parser))
2064     {
2065       cp_token *token = cp_lexer_peek_token (parser->lexer);
2066       /* This diagnostic makes more sense if it is tagged to the line
2067          of the token we just peeked at.  */
2068       cp_lexer_set_source_position_from_token (token);
2069
2070       if (token->type == CPP_PRAGMA)
2071         {
2072           error ("%<#pragma%> is not allowed here");
2073           cp_parser_skip_to_pragma_eol (parser, token);
2074           return;
2075         }
2076
2077       c_parse_error (message,
2078                      /* Because c_parser_error does not understand
2079                         CPP_KEYWORD, keywords are treated like
2080                         identifiers.  */
2081                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2082                      token->u.value);
2083     }
2084 }
2085
2086 /* Issue an error about name-lookup failing.  NAME is the
2087    IDENTIFIER_NODE DECL is the result of
2088    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2089    the thing that we hoped to find.  */
2090
2091 static void
2092 cp_parser_name_lookup_error (cp_parser* parser,
2093                              tree name,
2094                              tree decl,
2095                              const char* desired)
2096 {
2097   /* If name lookup completely failed, tell the user that NAME was not
2098      declared.  */
2099   if (decl == error_mark_node)
2100     {
2101       if (parser->scope && parser->scope != global_namespace)
2102         error ("%<%E::%E%> has not been declared",
2103                parser->scope, name);
2104       else if (parser->scope == global_namespace)
2105         error ("%<::%E%> has not been declared", name);
2106       else if (parser->object_scope
2107                && !CLASS_TYPE_P (parser->object_scope))
2108         error ("request for member %qE in non-class type %qT",
2109                name, parser->object_scope);
2110       else if (parser->object_scope)
2111         error ("%<%T::%E%> has not been declared",
2112                parser->object_scope, name);
2113       else
2114         error ("%qE has not been declared", name);
2115     }
2116   else if (parser->scope && parser->scope != global_namespace)
2117     error ("%<%E::%E%> %s", parser->scope, name, desired);
2118   else if (parser->scope == global_namespace)
2119     error ("%<::%E%> %s", name, desired);
2120   else
2121     error ("%qE %s", name, desired);
2122 }
2123
2124 /* If we are parsing tentatively, remember that an error has occurred
2125    during this tentative parse.  Returns true if the error was
2126    simulated; false if a message should be issued by the caller.  */
2127
2128 static bool
2129 cp_parser_simulate_error (cp_parser* parser)
2130 {
2131   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2132     {
2133       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2134       return true;
2135     }
2136   return false;
2137 }
2138
2139 /* Check for repeated decl-specifiers.  */
2140
2141 static void
2142 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2143 {
2144   cp_decl_spec ds;
2145
2146   for (ds = ds_first; ds != ds_last; ++ds)
2147     {
2148       unsigned count = decl_specs->specs[(int)ds];
2149       if (count < 2)
2150         continue;
2151       /* The "long" specifier is a special case because of "long long".  */
2152       if (ds == ds_long)
2153         {
2154           if (count > 2)
2155             error ("%<long long long%> is too long for GCC");
2156           else if (pedantic && !in_system_header && warn_long_long
2157                    && cxx_dialect == cxx98)
2158             pedwarn ("ISO C++ 1998 does not support %<long long%>");
2159         }
2160       else if (count > 1)
2161         {
2162           static const char *const decl_spec_names[] = {
2163             "signed",
2164             "unsigned",
2165             "short",
2166             "long",
2167             "const",
2168             "volatile",
2169             "restrict",
2170             "inline",
2171             "virtual",
2172             "explicit",
2173             "friend",
2174             "typedef",
2175             "__complex",
2176             "__thread"
2177           };
2178           error ("duplicate %qs", decl_spec_names[(int)ds]);
2179         }
2180     }
2181 }
2182
2183 /* This function is called when a type is defined.  If type
2184    definitions are forbidden at this point, an error message is
2185    issued.  */
2186
2187 static bool
2188 cp_parser_check_type_definition (cp_parser* parser)
2189 {
2190   /* If types are forbidden here, issue a message.  */
2191   if (parser->type_definition_forbidden_message)
2192     {
2193       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2194          in the message need to be interpreted.  */
2195       error (parser->type_definition_forbidden_message);
2196       return false;
2197     }
2198   return true;
2199 }
2200
2201 /* This function is called when the DECLARATOR is processed.  The TYPE
2202    was a type defined in the decl-specifiers.  If it is invalid to
2203    define a type in the decl-specifiers for DECLARATOR, an error is
2204    issued.  */
2205
2206 static void
2207 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2208                                                tree type)
2209 {
2210   /* [dcl.fct] forbids type definitions in return types.
2211      Unfortunately, it's not easy to know whether or not we are
2212      processing a return type until after the fact.  */
2213   while (declarator
2214          && (declarator->kind == cdk_pointer
2215              || declarator->kind == cdk_reference
2216              || declarator->kind == cdk_ptrmem))
2217     declarator = declarator->declarator;
2218   if (declarator
2219       && declarator->kind == cdk_function)
2220     {
2221       error ("new types may not be defined in a return type");
2222       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2223               type);
2224     }
2225 }
2226
2227 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2228    "<" in any valid C++ program.  If the next token is indeed "<",
2229    issue a message warning the user about what appears to be an
2230    invalid attempt to form a template-id.  */
2231
2232 static void
2233 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2234                                          tree type)
2235 {
2236   cp_token_position start = 0;
2237
2238   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2239     {
2240       if (TYPE_P (type))
2241         error ("%qT is not a template", type);
2242       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2243         error ("%qE is not a template", type);
2244       else
2245         error ("invalid template-id");
2246       /* Remember the location of the invalid "<".  */
2247       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2248         start = cp_lexer_token_position (parser->lexer, true);
2249       /* Consume the "<".  */
2250       cp_lexer_consume_token (parser->lexer);
2251       /* Parse the template arguments.  */
2252       cp_parser_enclosed_template_argument_list (parser);
2253       /* Permanently remove the invalid template arguments so that
2254          this error message is not issued again.  */
2255       if (start)
2256         cp_lexer_purge_tokens_after (parser->lexer, start);
2257     }
2258 }
2259
2260 /* If parsing an integral constant-expression, issue an error message
2261    about the fact that THING appeared and return true.  Otherwise,
2262    return false.  In either case, set
2263    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2264
2265 static bool
2266 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2267                                             const char *thing)
2268 {
2269   parser->non_integral_constant_expression_p = true;
2270   if (parser->integral_constant_expression_p)
2271     {
2272       if (!parser->allow_non_integral_constant_expression_p)
2273         {
2274           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2275              in the message need to be interpreted.  */
2276           char *message = concat (thing,
2277                                   " cannot appear in a constant-expression",
2278                                   NULL);
2279           error (message);
2280           free (message);
2281           return true;
2282         }
2283     }
2284   return false;
2285 }
2286
2287 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2288    qualifying scope (or NULL, if none) for ID.  This function commits
2289    to the current active tentative parse, if any.  (Otherwise, the
2290    problematic construct might be encountered again later, resulting
2291    in duplicate error messages.)  */
2292
2293 static void
2294 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2295 {
2296   tree decl, old_scope;
2297   /* Try to lookup the identifier.  */
2298   old_scope = parser->scope;
2299   parser->scope = scope;
2300   decl = cp_parser_lookup_name_simple (parser, id);
2301   parser->scope = old_scope;
2302   /* If the lookup found a template-name, it means that the user forgot
2303   to specify an argument list. Emit a useful error message.  */
2304   if (TREE_CODE (decl) == TEMPLATE_DECL)
2305     error ("invalid use of template-name %qE without an argument list", decl);
2306   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2307     error ("invalid use of destructor %qD as a type", id);
2308   else if (TREE_CODE (decl) == TYPE_DECL)
2309     /* Something like 'unsigned A a;'  */
2310     error ("invalid combination of multiple type-specifiers");
2311   else if (!parser->scope)
2312     {
2313       /* Issue an error message.  */
2314       error ("%qE does not name a type", id);
2315       /* If we're in a template class, it's possible that the user was
2316          referring to a type from a base class.  For example:
2317
2318            template <typename T> struct A { typedef T X; };
2319            template <typename T> struct B : public A<T> { X x; };
2320
2321          The user should have said "typename A<T>::X".  */
2322       if (processing_template_decl && current_class_type
2323           && TYPE_BINFO (current_class_type))
2324         {
2325           tree b;
2326
2327           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2328                b;
2329                b = TREE_CHAIN (b))
2330             {
2331               tree base_type = BINFO_TYPE (b);
2332               if (CLASS_TYPE_P (base_type)
2333                   && dependent_type_p (base_type))
2334                 {
2335                   tree field;
2336                   /* Go from a particular instantiation of the
2337                      template (which will have an empty TYPE_FIELDs),
2338                      to the main version.  */
2339                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2340                   for (field = TYPE_FIELDS (base_type);
2341                        field;
2342                        field = TREE_CHAIN (field))
2343                     if (TREE_CODE (field) == TYPE_DECL
2344                         && DECL_NAME (field) == id)
2345                       {
2346                         inform ("(perhaps %<typename %T::%E%> was intended)",
2347                                 BINFO_TYPE (b), id);
2348                         break;
2349                       }
2350                   if (field)
2351                     break;
2352                 }
2353             }
2354         }
2355     }
2356   /* Here we diagnose qualified-ids where the scope is actually correct,
2357      but the identifier does not resolve to a valid type name.  */
2358   else if (parser->scope != error_mark_node)
2359     {
2360       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2361         error ("%qE in namespace %qE does not name a type",
2362                id, parser->scope);
2363       else if (TYPE_P (parser->scope))
2364         error ("%qE in class %qT does not name a type", id, parser->scope);
2365       else
2366         gcc_unreachable ();
2367     }
2368   cp_parser_commit_to_tentative_parse (parser);
2369 }
2370
2371 /* Check for a common situation where a type-name should be present,
2372    but is not, and issue a sensible error message.  Returns true if an
2373    invalid type-name was detected.
2374
2375    The situation handled by this function are variable declarations of the
2376    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2377    Usually, `ID' should name a type, but if we got here it means that it
2378    does not. We try to emit the best possible error message depending on
2379    how exactly the id-expression looks like.  */
2380
2381 static bool
2382 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2383 {
2384   tree id;
2385
2386   cp_parser_parse_tentatively (parser);
2387   id = cp_parser_id_expression (parser,
2388                                 /*template_keyword_p=*/false,
2389                                 /*check_dependency_p=*/true,
2390                                 /*template_p=*/NULL,
2391                                 /*declarator_p=*/true,
2392                                 /*optional_p=*/false);
2393   /* After the id-expression, there should be a plain identifier,
2394      otherwise this is not a simple variable declaration. Also, if
2395      the scope is dependent, we cannot do much.  */
2396   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2397       || (parser->scope && TYPE_P (parser->scope)
2398           && dependent_type_p (parser->scope))
2399       || TREE_CODE (id) == TYPE_DECL)
2400     {
2401       cp_parser_abort_tentative_parse (parser);
2402       return false;
2403     }
2404   if (!cp_parser_parse_definitely (parser))
2405     return false;
2406
2407   /* Emit a diagnostic for the invalid type.  */
2408   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2409   /* Skip to the end of the declaration; there's no point in
2410      trying to process it.  */
2411   cp_parser_skip_to_end_of_block_or_statement (parser);
2412   return true;
2413 }
2414
2415 /* Consume tokens up to, and including, the next non-nested closing `)'.
2416    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2417    are doing error recovery. Returns -1 if OR_COMMA is true and we
2418    found an unnested comma.  */
2419
2420 static int
2421 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2422                                        bool recovering,
2423                                        bool or_comma,
2424                                        bool consume_paren)
2425 {
2426   unsigned paren_depth = 0;
2427   unsigned brace_depth = 0;
2428
2429   if (recovering && !or_comma
2430       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2431     return 0;
2432
2433   while (true)
2434     {
2435       cp_token * token = cp_lexer_peek_token (parser->lexer);
2436
2437       switch (token->type)
2438         {
2439         case CPP_EOF:
2440         case CPP_PRAGMA_EOL:
2441           /* If we've run out of tokens, then there is no closing `)'.  */
2442           return 0;
2443
2444         case CPP_SEMICOLON:
2445           /* This matches the processing in skip_to_end_of_statement.  */
2446           if (!brace_depth)
2447             return 0;
2448           break;
2449
2450         case CPP_OPEN_BRACE:
2451           ++brace_depth;
2452           break;
2453         case CPP_CLOSE_BRACE:
2454           if (!brace_depth--)
2455             return 0;
2456           break;
2457
2458         case CPP_COMMA:
2459           if (recovering && or_comma && !brace_depth && !paren_depth)
2460             return -1;
2461           break;
2462
2463         case CPP_OPEN_PAREN:
2464           if (!brace_depth)
2465             ++paren_depth;
2466           break;
2467
2468         case CPP_CLOSE_PAREN:
2469           if (!brace_depth && !paren_depth--)
2470             {
2471               if (consume_paren)
2472                 cp_lexer_consume_token (parser->lexer);
2473               return 1;
2474             }
2475           break;
2476
2477         default:
2478           break;
2479         }
2480
2481       /* Consume the token.  */
2482       cp_lexer_consume_token (parser->lexer);
2483     }
2484 }
2485
2486 /* Consume tokens until we reach the end of the current statement.
2487    Normally, that will be just before consuming a `;'.  However, if a
2488    non-nested `}' comes first, then we stop before consuming that.  */
2489
2490 static void
2491 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2492 {
2493   unsigned nesting_depth = 0;
2494
2495   while (true)
2496     {
2497       cp_token *token = cp_lexer_peek_token (parser->lexer);
2498
2499       switch (token->type)
2500         {
2501         case CPP_EOF:
2502         case CPP_PRAGMA_EOL:
2503           /* If we've run out of tokens, stop.  */
2504           return;
2505
2506         case CPP_SEMICOLON:
2507           /* If the next token is a `;', we have reached the end of the
2508              statement.  */
2509           if (!nesting_depth)
2510             return;
2511           break;
2512
2513         case CPP_CLOSE_BRACE:
2514           /* If this is a non-nested '}', stop before consuming it.
2515              That way, when confronted with something like:
2516
2517                { 3 + }
2518
2519              we stop before consuming the closing '}', even though we
2520              have not yet reached a `;'.  */
2521           if (nesting_depth == 0)
2522             return;
2523
2524           /* If it is the closing '}' for a block that we have
2525              scanned, stop -- but only after consuming the token.
2526              That way given:
2527
2528                 void f g () { ... }
2529                 typedef int I;
2530
2531              we will stop after the body of the erroneously declared
2532              function, but before consuming the following `typedef'
2533              declaration.  */
2534           if (--nesting_depth == 0)
2535             {
2536               cp_lexer_consume_token (parser->lexer);
2537               return;
2538             }
2539
2540         case CPP_OPEN_BRACE:
2541           ++nesting_depth;
2542           break;
2543
2544         default:
2545           break;
2546         }
2547
2548       /* Consume the token.  */
2549       cp_lexer_consume_token (parser->lexer);
2550     }
2551 }
2552
2553 /* This function is called at the end of a statement or declaration.
2554    If the next token is a semicolon, it is consumed; otherwise, error
2555    recovery is attempted.  */
2556
2557 static void
2558 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2559 {
2560   /* Look for the trailing `;'.  */
2561   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2562     {
2563       /* If there is additional (erroneous) input, skip to the end of
2564          the statement.  */
2565       cp_parser_skip_to_end_of_statement (parser);
2566       /* If the next token is now a `;', consume it.  */
2567       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2568         cp_lexer_consume_token (parser->lexer);
2569     }
2570 }
2571
2572 /* Skip tokens until we have consumed an entire block, or until we
2573    have consumed a non-nested `;'.  */
2574
2575 static void
2576 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2577 {
2578   int nesting_depth = 0;
2579
2580   while (nesting_depth >= 0)
2581     {
2582       cp_token *token = cp_lexer_peek_token (parser->lexer);
2583
2584       switch (token->type)
2585         {
2586         case CPP_EOF:
2587         case CPP_PRAGMA_EOL:
2588           /* If we've run out of tokens, stop.  */
2589           return;
2590
2591         case CPP_SEMICOLON:
2592           /* Stop if this is an unnested ';'. */
2593           if (!nesting_depth)
2594             nesting_depth = -1;
2595           break;
2596
2597         case CPP_CLOSE_BRACE:
2598           /* Stop if this is an unnested '}', or closes the outermost
2599              nesting level.  */
2600           nesting_depth--;
2601           if (!nesting_depth)
2602             nesting_depth = -1;
2603           break;
2604
2605         case CPP_OPEN_BRACE:
2606           /* Nest. */
2607           nesting_depth++;
2608           break;
2609
2610         default:
2611           break;
2612         }
2613
2614       /* Consume the token.  */
2615       cp_lexer_consume_token (parser->lexer);
2616     }
2617 }
2618
2619 /* Skip tokens until a non-nested closing curly brace is the next
2620    token, or there are no more tokens. Return true in the first case,
2621    false otherwise.  */
2622
2623 static bool
2624 cp_parser_skip_to_closing_brace (cp_parser *parser)
2625 {
2626   unsigned nesting_depth = 0;
2627
2628   while (true)
2629     {
2630       cp_token *token = cp_lexer_peek_token (parser->lexer);
2631
2632       switch (token->type)
2633         {
2634         case CPP_EOF:
2635         case CPP_PRAGMA_EOL:
2636           /* If we've run out of tokens, stop.  */
2637           return false;
2638
2639         case CPP_CLOSE_BRACE:
2640           /* If the next token is a non-nested `}', then we have reached
2641              the end of the current block.  */
2642           if (nesting_depth-- == 0)
2643             return true;
2644           break;
2645
2646         case CPP_OPEN_BRACE:
2647           /* If it the next token is a `{', then we are entering a new
2648              block.  Consume the entire block.  */
2649           ++nesting_depth;
2650           break;
2651
2652         default:
2653           break;
2654         }
2655
2656       /* Consume the token.  */
2657       cp_lexer_consume_token (parser->lexer);
2658     }
2659 }
2660
2661 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2662    parameter is the PRAGMA token, allowing us to purge the entire pragma
2663    sequence.  */
2664
2665 static void
2666 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2667 {
2668   cp_token *token;
2669
2670   parser->lexer->in_pragma = false;
2671
2672   do
2673     token = cp_lexer_consume_token (parser->lexer);
2674   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2675
2676   /* Ensure that the pragma is not parsed again.  */
2677   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2678 }
2679
2680 /* Require pragma end of line, resyncing with it as necessary.  The
2681    arguments are as for cp_parser_skip_to_pragma_eol.  */
2682
2683 static void
2684 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2685 {
2686   parser->lexer->in_pragma = false;
2687   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2688     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2689 }
2690
2691 /* This is a simple wrapper around make_typename_type. When the id is
2692    an unresolved identifier node, we can provide a superior diagnostic
2693    using cp_parser_diagnose_invalid_type_name.  */
2694
2695 static tree
2696 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2697 {
2698   tree result;
2699   if (TREE_CODE (id) == IDENTIFIER_NODE)
2700     {
2701       result = make_typename_type (scope, id, typename_type,
2702                                    /*complain=*/tf_none);
2703       if (result == error_mark_node)
2704         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2705       return result;
2706     }
2707   return make_typename_type (scope, id, typename_type, tf_error);
2708 }
2709
2710 /* This is a wrapper around the
2711    make_{pointer,ptrmem,reference}_declarator functions that decides
2712    which one to call based on the CODE and CLASS_TYPE arguments. The
2713    CODE argument should be one of the values returned by
2714    cp_parser_ptr_operator. */
2715 static cp_declarator *
2716 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2717                                     cp_cv_quals cv_qualifiers,
2718                                     cp_declarator *target)
2719 {
2720   if (code == ERROR_MARK)
2721     return cp_error_declarator;
2722
2723   if (code == INDIRECT_REF)
2724     if (class_type == NULL_TREE)
2725       return make_pointer_declarator (cv_qualifiers, target);
2726     else
2727       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2728   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2729     return make_reference_declarator (cv_qualifiers, target, false);
2730   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2731     return make_reference_declarator (cv_qualifiers, target, true);
2732   gcc_unreachable ();
2733 }
2734
2735 /* Create a new C++ parser.  */
2736
2737 static cp_parser *
2738 cp_parser_new (void)
2739 {
2740   cp_parser *parser;
2741   cp_lexer *lexer;
2742   unsigned i;
2743
2744   /* cp_lexer_new_main is called before calling ggc_alloc because
2745      cp_lexer_new_main might load a PCH file.  */
2746   lexer = cp_lexer_new_main ();
2747
2748   /* Initialize the binops_by_token so that we can get the tree
2749      directly from the token.  */
2750   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2751     binops_by_token[binops[i].token_type] = binops[i];
2752
2753   parser = GGC_CNEW (cp_parser);
2754   parser->lexer = lexer;
2755   parser->context = cp_parser_context_new (NULL);
2756
2757   /* For now, we always accept GNU extensions.  */
2758   parser->allow_gnu_extensions_p = 1;
2759
2760   /* The `>' token is a greater-than operator, not the end of a
2761      template-id.  */
2762   parser->greater_than_is_operator_p = true;
2763
2764   parser->default_arg_ok_p = true;
2765
2766   /* We are not parsing a constant-expression.  */
2767   parser->integral_constant_expression_p = false;
2768   parser->allow_non_integral_constant_expression_p = false;
2769   parser->non_integral_constant_expression_p = false;
2770
2771   /* Local variable names are not forbidden.  */
2772   parser->local_variables_forbidden_p = false;
2773
2774   /* We are not processing an `extern "C"' declaration.  */
2775   parser->in_unbraced_linkage_specification_p = false;
2776
2777   /* We are not processing a declarator.  */
2778   parser->in_declarator_p = false;
2779
2780   /* We are not processing a template-argument-list.  */
2781   parser->in_template_argument_list_p = false;
2782
2783   /* We are not in an iteration statement.  */
2784   parser->in_statement = 0;
2785
2786   /* We are not in a switch statement.  */
2787   parser->in_switch_statement_p = false;
2788
2789   /* We are not parsing a type-id inside an expression.  */
2790   parser->in_type_id_in_expr_p = false;
2791
2792   /* Declarations aren't implicitly extern "C".  */
2793   parser->implicit_extern_c = false;
2794
2795   /* String literals should be translated to the execution character set.  */
2796   parser->translate_strings_p = true;
2797
2798   /* We are not parsing a function body.  */
2799   parser->in_function_body = false;
2800
2801   /* The unparsed function queue is empty.  */
2802   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2803
2804   /* There are no classes being defined.  */
2805   parser->num_classes_being_defined = 0;
2806
2807   /* No template parameters apply.  */
2808   parser->num_template_parameter_lists = 0;
2809
2810   return parser;
2811 }
2812
2813 /* Create a cp_lexer structure which will emit the tokens in CACHE
2814    and push it onto the parser's lexer stack.  This is used for delayed
2815    parsing of in-class method bodies and default arguments, and should
2816    not be confused with tentative parsing.  */
2817 static void
2818 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2819 {
2820   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2821   lexer->next = parser->lexer;
2822   parser->lexer = lexer;
2823
2824   /* Move the current source position to that of the first token in the
2825      new lexer.  */
2826   cp_lexer_set_source_position_from_token (lexer->next_token);
2827 }
2828
2829 /* Pop the top lexer off the parser stack.  This is never used for the
2830    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2831 static void
2832 cp_parser_pop_lexer (cp_parser *parser)
2833 {
2834   cp_lexer *lexer = parser->lexer;
2835   parser->lexer = lexer->next;
2836   cp_lexer_destroy (lexer);
2837
2838   /* Put the current source position back where it was before this
2839      lexer was pushed.  */
2840   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2841 }
2842
2843 /* Lexical conventions [gram.lex]  */
2844
2845 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2846    identifier.  */
2847
2848 static tree
2849 cp_parser_identifier (cp_parser* parser)
2850 {
2851   cp_token *token;
2852
2853   /* Look for the identifier.  */
2854   token = cp_parser_require (parser, CPP_NAME, "identifier");
2855   /* Return the value.  */
2856   return token ? token->u.value : error_mark_node;
2857 }
2858
2859 /* Parse a sequence of adjacent string constants.  Returns a
2860    TREE_STRING representing the combined, nul-terminated string
2861    constant.  If TRANSLATE is true, translate the string to the
2862    execution character set.  If WIDE_OK is true, a wide string is
2863    invalid here.
2864
2865    C++98 [lex.string] says that if a narrow string literal token is
2866    adjacent to a wide string literal token, the behavior is undefined.
2867    However, C99 6.4.5p4 says that this results in a wide string literal.
2868    We follow C99 here, for consistency with the C front end.
2869
2870    This code is largely lifted from lex_string() in c-lex.c.
2871
2872    FUTURE: ObjC++ will need to handle @-strings here.  */
2873 static tree
2874 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2875 {
2876   tree value;
2877   size_t count;
2878   struct obstack str_ob;
2879   cpp_string str, istr, *strs;
2880   cp_token *tok;
2881   enum cpp_ttype type;
2882
2883   tok = cp_lexer_peek_token (parser->lexer);
2884   if (!cp_parser_is_string_literal (tok))
2885     {
2886       cp_parser_error (parser, "expected string-literal");
2887       return error_mark_node;
2888     }
2889
2890   type = tok->type;
2891
2892   /* Try to avoid the overhead of creating and destroying an obstack
2893      for the common case of just one string.  */
2894   if (!cp_parser_is_string_literal
2895       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2896     {
2897       cp_lexer_consume_token (parser->lexer);
2898
2899       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2900       str.len = TREE_STRING_LENGTH (tok->u.value);
2901       count = 1;
2902
2903       strs = &str;
2904     }
2905   else
2906     {
2907       gcc_obstack_init (&str_ob);
2908       count = 0;
2909
2910       do
2911         {
2912           cp_lexer_consume_token (parser->lexer);
2913           count++;
2914           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2915           str.len = TREE_STRING_LENGTH (tok->u.value);
2916
2917           if (type != tok->type)
2918             {
2919               if (type == CPP_STRING)
2920                 type = tok->type;
2921               else if (tok->type != CPP_STRING)
2922                 error ("unsupported non-standard concatenation of string literals");
2923             }
2924
2925           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2926
2927           tok = cp_lexer_peek_token (parser->lexer);
2928         }
2929       while (cp_parser_is_string_literal (tok));
2930
2931       strs = (cpp_string *) obstack_finish (&str_ob);
2932     }
2933
2934   if (type != CPP_STRING && !wide_ok)
2935     {
2936       cp_parser_error (parser, "a wide string is invalid in this context");
2937       type = CPP_STRING;
2938     }
2939
2940   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2941       (parse_in, strs, count, &istr, type))
2942     {
2943       value = build_string (istr.len, (const char *)istr.text);
2944       free (CONST_CAST (unsigned char *, istr.text));
2945
2946       switch (type)
2947         {
2948         default:
2949         case CPP_STRING:
2950           TREE_TYPE (value) = char_array_type_node;
2951           break;
2952         case CPP_STRING16:
2953           TREE_TYPE (value) = char16_array_type_node;
2954           break;
2955         case CPP_STRING32:
2956           TREE_TYPE (value) = char32_array_type_node;
2957           break;
2958         case CPP_WSTRING:
2959           TREE_TYPE (value) = wchar_array_type_node;
2960           break;
2961         }
2962
2963       value = fix_string_type (value);
2964     }
2965   else
2966     /* cpp_interpret_string has issued an error.  */
2967     value = error_mark_node;
2968
2969   if (count > 1)
2970     obstack_free (&str_ob, 0);
2971
2972   return value;
2973 }
2974
2975
2976 /* Basic concepts [gram.basic]  */
2977
2978 /* Parse a translation-unit.
2979
2980    translation-unit:
2981      declaration-seq [opt]
2982
2983    Returns TRUE if all went well.  */
2984
2985 static bool
2986 cp_parser_translation_unit (cp_parser* parser)
2987 {
2988   /* The address of the first non-permanent object on the declarator
2989      obstack.  */
2990   static void *declarator_obstack_base;
2991
2992   bool success;
2993
2994   /* Create the declarator obstack, if necessary.  */
2995   if (!cp_error_declarator)
2996     {
2997       gcc_obstack_init (&declarator_obstack);
2998       /* Create the error declarator.  */
2999       cp_error_declarator = make_declarator (cdk_error);
3000       /* Create the empty parameter list.  */
3001       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3002       /* Remember where the base of the declarator obstack lies.  */
3003       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3004     }
3005
3006   cp_parser_declaration_seq_opt (parser);
3007
3008   /* If there are no tokens left then all went well.  */
3009   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3010     {
3011       /* Get rid of the token array; we don't need it any more.  */
3012       cp_lexer_destroy (parser->lexer);
3013       parser->lexer = NULL;
3014
3015       /* This file might have been a context that's implicitly extern
3016          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3017       if (parser->implicit_extern_c)
3018         {
3019           pop_lang_context ();
3020           parser->implicit_extern_c = false;
3021         }
3022
3023       /* Finish up.  */
3024       finish_translation_unit ();
3025
3026       success = true;
3027     }
3028   else
3029     {
3030       cp_parser_error (parser, "expected declaration");
3031       success = false;
3032     }
3033
3034   /* Make sure the declarator obstack was fully cleaned up.  */
3035   gcc_assert (obstack_next_free (&declarator_obstack)
3036               == declarator_obstack_base);
3037
3038   /* All went well.  */
3039   return success;
3040 }
3041
3042 /* Expressions [gram.expr] */
3043
3044 /* Parse a primary-expression.
3045
3046    primary-expression:
3047      literal
3048      this
3049      ( expression )
3050      id-expression
3051
3052    GNU Extensions:
3053
3054    primary-expression:
3055      ( compound-statement )
3056      __builtin_va_arg ( assignment-expression , type-id )
3057      __builtin_offsetof ( type-id , offsetof-expression )
3058
3059    C++ Extensions:
3060      __has_nothrow_assign ( type-id )   
3061      __has_nothrow_constructor ( type-id )
3062      __has_nothrow_copy ( type-id )
3063      __has_trivial_assign ( type-id )   
3064      __has_trivial_constructor ( type-id )
3065      __has_trivial_copy ( type-id )
3066      __has_trivial_destructor ( type-id )
3067      __has_virtual_destructor ( type-id )     
3068      __is_abstract ( type-id )
3069      __is_base_of ( type-id , type-id )
3070      __is_class ( type-id )
3071      __is_convertible_to ( type-id , type-id )     
3072      __is_empty ( type-id )
3073      __is_enum ( type-id )
3074      __is_pod ( type-id )
3075      __is_polymorphic ( type-id )
3076      __is_union ( type-id )
3077
3078    Objective-C++ Extension:
3079
3080    primary-expression:
3081      objc-expression
3082
3083    literal:
3084      __null
3085
3086    ADDRESS_P is true iff this expression was immediately preceded by
3087    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3088    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3089    true iff this expression is a template argument.
3090
3091    Returns a representation of the expression.  Upon return, *IDK
3092    indicates what kind of id-expression (if any) was present.  */
3093
3094 static tree
3095 cp_parser_primary_expression (cp_parser *parser,
3096                               bool address_p,
3097                               bool cast_p,
3098                               bool template_arg_p,
3099                               cp_id_kind *idk)
3100 {
3101   cp_token *token;
3102
3103   /* Assume the primary expression is not an id-expression.  */
3104   *idk = CP_ID_KIND_NONE;
3105
3106   /* Peek at the next token.  */
3107   token = cp_lexer_peek_token (parser->lexer);
3108   switch (token->type)
3109     {
3110       /* literal:
3111            integer-literal
3112            character-literal
3113            floating-literal
3114            string-literal
3115            boolean-literal  */
3116     case CPP_CHAR:
3117     case CPP_CHAR16:
3118     case CPP_CHAR32:
3119     case CPP_WCHAR:
3120     case CPP_NUMBER:
3121       token = cp_lexer_consume_token (parser->lexer);
3122       /* Floating-point literals are only allowed in an integral
3123          constant expression if they are cast to an integral or
3124          enumeration type.  */
3125       if (TREE_CODE (token->u.value) == REAL_CST
3126           && parser->integral_constant_expression_p
3127           && pedantic)
3128         {
3129           /* CAST_P will be set even in invalid code like "int(2.7 +
3130              ...)".   Therefore, we have to check that the next token
3131              is sure to end the cast.  */
3132           if (cast_p)
3133             {
3134               cp_token *next_token;
3135
3136               next_token = cp_lexer_peek_token (parser->lexer);
3137               if (/* The comma at the end of an
3138                      enumerator-definition.  */
3139                   next_token->type != CPP_COMMA
3140                   /* The curly brace at the end of an enum-specifier.  */
3141                   && next_token->type != CPP_CLOSE_BRACE
3142                   /* The end of a statement.  */
3143                   && next_token->type != CPP_SEMICOLON
3144                   /* The end of the cast-expression.  */
3145                   && next_token->type != CPP_CLOSE_PAREN
3146                   /* The end of an array bound.  */
3147                   && next_token->type != CPP_CLOSE_SQUARE
3148                   /* The closing ">" in a template-argument-list.  */
3149                   && (next_token->type != CPP_GREATER
3150                       || parser->greater_than_is_operator_p)
3151                   /* C++0x only: A ">>" treated like two ">" tokens,
3152                      in a template-argument-list.  */
3153                   && (next_token->type != CPP_RSHIFT
3154                       || (cxx_dialect == cxx98)
3155                       || parser->greater_than_is_operator_p))
3156                 cast_p = false;
3157             }
3158
3159           /* If we are within a cast, then the constraint that the
3160              cast is to an integral or enumeration type will be
3161              checked at that point.  If we are not within a cast, then
3162              this code is invalid.  */
3163           if (!cast_p)
3164             cp_parser_non_integral_constant_expression
3165               (parser, "floating-point literal");
3166         }
3167       return token->u.value;
3168
3169     case CPP_STRING:
3170     case CPP_STRING16:
3171     case CPP_STRING32:
3172     case CPP_WSTRING:
3173       /* ??? Should wide strings be allowed when parser->translate_strings_p
3174          is false (i.e. in attributes)?  If not, we can kill the third
3175          argument to cp_parser_string_literal.  */
3176       return cp_parser_string_literal (parser,
3177                                        parser->translate_strings_p,
3178                                        true);
3179
3180     case CPP_OPEN_PAREN:
3181       {
3182         tree expr;
3183         bool saved_greater_than_is_operator_p;
3184
3185         /* Consume the `('.  */
3186         cp_lexer_consume_token (parser->lexer);
3187         /* Within a parenthesized expression, a `>' token is always
3188            the greater-than operator.  */
3189         saved_greater_than_is_operator_p
3190           = parser->greater_than_is_operator_p;
3191         parser->greater_than_is_operator_p = true;
3192         /* If we see `( { ' then we are looking at the beginning of
3193            a GNU statement-expression.  */
3194         if (cp_parser_allow_gnu_extensions_p (parser)
3195             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3196           {
3197             /* Statement-expressions are not allowed by the standard.  */
3198             if (pedantic)
3199               pedwarn ("ISO C++ forbids braced-groups within expressions");
3200
3201             /* And they're not allowed outside of a function-body; you
3202                cannot, for example, write:
3203
3204                  int i = ({ int j = 3; j + 1; });
3205
3206                at class or namespace scope.  */
3207             if (!parser->in_function_body
3208                 || parser->in_template_argument_list_p)
3209               {
3210                 error ("statement-expressions are not allowed outside "
3211                        "functions nor in template-argument lists");
3212                 cp_parser_skip_to_end_of_block_or_statement (parser);
3213                 expr = error_mark_node;
3214               }
3215             else
3216               {
3217                 /* Start the statement-expression.  */
3218                 expr = begin_stmt_expr ();
3219                 /* Parse the compound-statement.  */
3220                 cp_parser_compound_statement (parser, expr, false);
3221                 /* Finish up.  */
3222                 expr = finish_stmt_expr (expr, false);
3223               }
3224           }
3225         else
3226           {
3227             /* Parse the parenthesized expression.  */
3228             expr = cp_parser_expression (parser, cast_p);
3229             /* Let the front end know that this expression was
3230                enclosed in parentheses. This matters in case, for
3231                example, the expression is of the form `A::B', since
3232                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3233                not.  */
3234             finish_parenthesized_expr (expr);
3235           }
3236         /* The `>' token might be the end of a template-id or
3237            template-parameter-list now.  */
3238         parser->greater_than_is_operator_p
3239           = saved_greater_than_is_operator_p;
3240         /* Consume the `)'.  */
3241         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3242           cp_parser_skip_to_end_of_statement (parser);
3243
3244         return expr;
3245       }
3246
3247     case CPP_KEYWORD:
3248       switch (token->keyword)
3249         {
3250           /* These two are the boolean literals.  */
3251         case RID_TRUE:
3252           cp_lexer_consume_token (parser->lexer);
3253           return boolean_true_node;
3254         case RID_FALSE:
3255           cp_lexer_consume_token (parser->lexer);
3256           return boolean_false_node;
3257
3258           /* The `__null' literal.  */
3259         case RID_NULL:
3260           cp_lexer_consume_token (parser->lexer);
3261           return null_node;
3262
3263           /* Recognize the `this' keyword.  */
3264         case RID_THIS:
3265           cp_lexer_consume_token (parser->lexer);
3266           if (parser->local_variables_forbidden_p)
3267             {
3268               error ("%<this%> may not be used in this context");
3269               return error_mark_node;
3270             }
3271           /* Pointers cannot appear in constant-expressions.  */
3272           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3273             return error_mark_node;
3274           return finish_this_expr ();
3275
3276           /* The `operator' keyword can be the beginning of an
3277              id-expression.  */
3278         case RID_OPERATOR:
3279           goto id_expression;
3280
3281         case RID_FUNCTION_NAME:
3282         case RID_PRETTY_FUNCTION_NAME:
3283         case RID_C99_FUNCTION_NAME:
3284           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3285              __func__ are the names of variables -- but they are
3286              treated specially.  Therefore, they are handled here,
3287              rather than relying on the generic id-expression logic
3288              below.  Grammatically, these names are id-expressions.
3289
3290              Consume the token.  */
3291           token = cp_lexer_consume_token (parser->lexer);
3292           /* Look up the name.  */
3293           return finish_fname (token->u.value);
3294
3295         case RID_VA_ARG:
3296           {
3297             tree expression;
3298             tree type;
3299
3300             /* The `__builtin_va_arg' construct is used to handle
3301                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3302             cp_lexer_consume_token (parser->lexer);
3303             /* Look for the opening `('.  */
3304             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3305             /* Now, parse the assignment-expression.  */
3306             expression = cp_parser_assignment_expression (parser,
3307                                                           /*cast_p=*/false);
3308             /* Look for the `,'.  */
3309             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3310             /* Parse the type-id.  */
3311             type = cp_parser_type_id (parser);
3312             /* Look for the closing `)'.  */
3313             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3314             /* Using `va_arg' in a constant-expression is not
3315                allowed.  */
3316             if (cp_parser_non_integral_constant_expression (parser,
3317                                                             "%<va_arg%>"))
3318               return error_mark_node;
3319             return build_x_va_arg (expression, type);
3320           }
3321
3322         case RID_OFFSETOF:
3323           return cp_parser_builtin_offsetof (parser);
3324
3325         case RID_HAS_NOTHROW_ASSIGN:
3326         case RID_HAS_NOTHROW_CONSTRUCTOR:
3327         case RID_HAS_NOTHROW_COPY:        
3328         case RID_HAS_TRIVIAL_ASSIGN:
3329         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3330         case RID_HAS_TRIVIAL_COPY:        
3331         case RID_HAS_TRIVIAL_DESTRUCTOR:
3332         case RID_HAS_VIRTUAL_DESTRUCTOR:
3333         case RID_IS_ABSTRACT:
3334         case RID_IS_BASE_OF:
3335         case RID_IS_CLASS:
3336         case RID_IS_CONVERTIBLE_TO:
3337         case RID_IS_EMPTY:
3338         case RID_IS_ENUM:
3339         case RID_IS_POD:
3340         case RID_IS_POLYMORPHIC:
3341         case RID_IS_UNION:
3342           return cp_parser_trait_expr (parser, token->keyword);
3343
3344         /* Objective-C++ expressions.  */
3345         case RID_AT_ENCODE:
3346         case RID_AT_PROTOCOL:
3347         case RID_AT_SELECTOR:
3348           return cp_parser_objc_expression (parser);
3349
3350         default:
3351           cp_parser_error (parser, "expected primary-expression");
3352           return error_mark_node;
3353         }
3354
3355       /* An id-expression can start with either an identifier, a
3356          `::' as the beginning of a qualified-id, or the "operator"
3357          keyword.  */
3358     case CPP_NAME:
3359     case CPP_SCOPE:
3360     case CPP_TEMPLATE_ID:
3361     case CPP_NESTED_NAME_SPECIFIER:
3362       {
3363         tree id_expression;
3364         tree decl;
3365         const char *error_msg;
3366         bool template_p;
3367         bool done;
3368
3369       id_expression:
3370         /* Parse the id-expression.  */
3371         id_expression
3372           = cp_parser_id_expression (parser,
3373                                      /*template_keyword_p=*/false,
3374                                      /*check_dependency_p=*/true,
3375                                      &template_p,
3376                                      /*declarator_p=*/false,
3377                                      /*optional_p=*/false);
3378         if (id_expression == error_mark_node)
3379           return error_mark_node;
3380         token = cp_lexer_peek_token (parser->lexer);
3381         done = (token->type != CPP_OPEN_SQUARE
3382                 && token->type != CPP_OPEN_PAREN
3383                 && token->type != CPP_DOT
3384                 && token->type != CPP_DEREF
3385                 && token->type != CPP_PLUS_PLUS
3386                 && token->type != CPP_MINUS_MINUS);
3387         /* If we have a template-id, then no further lookup is
3388            required.  If the template-id was for a template-class, we
3389            will sometimes have a TYPE_DECL at this point.  */
3390         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3391                  || TREE_CODE (id_expression) == TYPE_DECL)
3392           decl = id_expression;
3393         /* Look up the name.  */
3394         else
3395           {
3396             tree ambiguous_decls;
3397
3398             decl = cp_parser_lookup_name (parser, id_expression,
3399                                           none_type,
3400                                           template_p,
3401                                           /*is_namespace=*/false,
3402                                           /*check_dependency=*/true,
3403                                           &ambiguous_decls);
3404             /* If the lookup was ambiguous, an error will already have
3405                been issued.  */
3406             if (ambiguous_decls)
3407               return error_mark_node;
3408
3409             /* In Objective-C++, an instance variable (ivar) may be preferred
3410                to whatever cp_parser_lookup_name() found.  */
3411             decl = objc_lookup_ivar (decl, id_expression);
3412
3413             /* If name lookup gives us a SCOPE_REF, then the
3414                qualifying scope was dependent.  */
3415             if (TREE_CODE (decl) == SCOPE_REF)
3416               {
3417                 /* At this point, we do not know if DECL is a valid
3418                    integral constant expression.  We assume that it is
3419                    in fact such an expression, so that code like:
3420
3421                       template <int N> struct A {
3422                         int a[B<N>::i];
3423                       };
3424                      
3425                    is accepted.  At template-instantiation time, we
3426                    will check that B<N>::i is actually a constant.  */
3427                 return decl;
3428               }
3429             /* Check to see if DECL is a local variable in a context
3430                where that is forbidden.  */
3431             if (parser->local_variables_forbidden_p
3432                 && local_variable_p (decl))
3433               {
3434                 /* It might be that we only found DECL because we are
3435                    trying to be generous with pre-ISO scoping rules.
3436                    For example, consider:
3437
3438                      int i;
3439                      void g() {
3440                        for (int i = 0; i < 10; ++i) {}
3441                        extern void f(int j = i);
3442                      }
3443
3444                    Here, name look up will originally find the out
3445                    of scope `i'.  We need to issue a warning message,
3446                    but then use the global `i'.  */
3447                 decl = check_for_out_of_scope_variable (decl);
3448                 if (local_variable_p (decl))
3449                   {
3450                     error ("local variable %qD may not appear in this context",
3451                            decl);
3452                     return error_mark_node;
3453                   }
3454               }
3455           }
3456
3457         decl = (finish_id_expression
3458                 (id_expression, decl, parser->scope,
3459                  idk,
3460                  parser->integral_constant_expression_p,
3461                  parser->allow_non_integral_constant_expression_p,
3462                  &parser->non_integral_constant_expression_p,
3463                  template_p, done, address_p,
3464                  template_arg_p,
3465                  &error_msg));
3466         if (error_msg)
3467           cp_parser_error (parser, error_msg);
3468         return decl;
3469       }
3470
3471       /* Anything else is an error.  */
3472     default:
3473       /* ...unless we have an Objective-C++ message or string literal,
3474          that is.  */
3475       if (c_dialect_objc ()
3476           && (token->type == CPP_OPEN_SQUARE
3477               || token->type == CPP_OBJC_STRING))
3478         return cp_parser_objc_expression (parser);
3479
3480       cp_parser_error (parser, "expected primary-expression");
3481       return error_mark_node;
3482     }
3483 }
3484
3485 /* Parse an id-expression.
3486
3487    id-expression:
3488      unqualified-id
3489      qualified-id
3490
3491    qualified-id:
3492      :: [opt] nested-name-specifier template [opt] unqualified-id
3493      :: identifier
3494      :: operator-function-id
3495      :: template-id
3496
3497    Return a representation of the unqualified portion of the
3498    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3499    a `::' or nested-name-specifier.
3500
3501    Often, if the id-expression was a qualified-id, the caller will
3502    want to make a SCOPE_REF to represent the qualified-id.  This
3503    function does not do this in order to avoid wastefully creating
3504    SCOPE_REFs when they are not required.
3505
3506    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3507    `template' keyword.
3508
3509    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3510    uninstantiated templates.
3511
3512    If *TEMPLATE_P is non-NULL, it is set to true iff the
3513    `template' keyword is used to explicitly indicate that the entity
3514    named is a template.
3515
3516    If DECLARATOR_P is true, the id-expression is appearing as part of
3517    a declarator, rather than as part of an expression.  */
3518
3519 static tree
3520 cp_parser_id_expression (cp_parser *parser,
3521                          bool template_keyword_p,
3522                          bool check_dependency_p,
3523                          bool *template_p,
3524                          bool declarator_p,
3525                          bool optional_p)
3526 {
3527   bool global_scope_p;
3528   bool nested_name_specifier_p;
3529
3530   /* Assume the `template' keyword was not used.  */
3531   if (template_p)
3532     *template_p = template_keyword_p;
3533
3534   /* Look for the optional `::' operator.  */
3535   global_scope_p
3536     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3537        != NULL_TREE);
3538   /* Look for the optional nested-name-specifier.  */
3539   nested_name_specifier_p
3540     = (cp_parser_nested_name_specifier_opt (parser,
3541                                             /*typename_keyword_p=*/false,
3542                                             check_dependency_p,
3543                                             /*type_p=*/false,
3544                                             declarator_p)
3545        != NULL_TREE);
3546   /* If there is a nested-name-specifier, then we are looking at
3547      the first qualified-id production.  */
3548   if (nested_name_specifier_p)
3549     {
3550       tree saved_scope;
3551       tree saved_object_scope;
3552       tree saved_qualifying_scope;
3553       tree unqualified_id;
3554       bool is_template;
3555
3556       /* See if the next token is the `template' keyword.  */
3557       if (!template_p)
3558         template_p = &is_template;
3559       *template_p = cp_parser_optional_template_keyword (parser);
3560       /* Name lookup we do during the processing of the
3561          unqualified-id might obliterate SCOPE.  */
3562       saved_scope = parser->scope;
3563       saved_object_scope = parser->object_scope;
3564       saved_qualifying_scope = parser->qualifying_scope;
3565       /* Process the final unqualified-id.  */
3566       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3567                                                  check_dependency_p,
3568                                                  declarator_p,
3569                                                  /*optional_p=*/false);
3570       /* Restore the SAVED_SCOPE for our caller.  */
3571       parser->scope = saved_scope;
3572       parser->object_scope = saved_object_scope;
3573       parser->qualifying_scope = saved_qualifying_scope;
3574
3575       return unqualified_id;
3576     }
3577   /* Otherwise, if we are in global scope, then we are looking at one
3578      of the other qualified-id productions.  */
3579   else if (global_scope_p)
3580     {
3581       cp_token *token;
3582       tree id;
3583
3584       /* Peek at the next token.  */
3585       token = cp_lexer_peek_token (parser->lexer);
3586
3587       /* If it's an identifier, and the next token is not a "<", then
3588          we can avoid the template-id case.  This is an optimization
3589          for this common case.  */
3590       if (token->type == CPP_NAME
3591           && !cp_parser_nth_token_starts_template_argument_list_p
3592                (parser, 2))
3593         return cp_parser_identifier (parser);
3594
3595       cp_parser_parse_tentatively (parser);
3596       /* Try a template-id.  */
3597       id = cp_parser_template_id (parser,
3598                                   /*template_keyword_p=*/false,
3599                                   /*check_dependency_p=*/true,
3600                                   declarator_p);
3601       /* If that worked, we're done.  */
3602       if (cp_parser_parse_definitely (parser))
3603         return id;
3604
3605       /* Peek at the next token.  (Changes in the token buffer may
3606          have invalidated the pointer obtained above.)  */
3607       token = cp_lexer_peek_token (parser->lexer);
3608
3609       switch (token->type)
3610         {
3611         case CPP_NAME:
3612           return cp_parser_identifier (parser);
3613
3614         case CPP_KEYWORD:
3615           if (token->keyword == RID_OPERATOR)
3616             return cp_parser_operator_function_id (parser);
3617           /* Fall through.  */
3618
3619         default:
3620           cp_parser_error (parser, "expected id-expression");
3621           return error_mark_node;
3622         }
3623     }
3624   else
3625     return cp_parser_unqualified_id (parser, template_keyword_p,
3626                                      /*check_dependency_p=*/true,
3627                                      declarator_p,
3628                                      optional_p);
3629 }
3630
3631 /* Parse an unqualified-id.
3632
3633    unqualified-id:
3634      identifier
3635      operator-function-id
3636      conversion-function-id
3637      ~ class-name
3638      template-id
3639
3640    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3641    keyword, in a construct like `A::template ...'.
3642
3643    Returns a representation of unqualified-id.  For the `identifier'
3644    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3645    production a BIT_NOT_EXPR is returned; the operand of the
3646    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3647    other productions, see the documentation accompanying the
3648    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3649    names are looked up in uninstantiated templates.  If DECLARATOR_P
3650    is true, the unqualified-id is appearing as part of a declarator,
3651    rather than as part of an expression.  */
3652
3653 static tree
3654 cp_parser_unqualified_id (cp_parser* parser,
3655                           bool template_keyword_p,
3656                           bool check_dependency_p,
3657                           bool declarator_p,
3658                           bool optional_p)
3659 {
3660   cp_token *token;
3661
3662   /* Peek at the next token.  */
3663   token = cp_lexer_peek_token (parser->lexer);
3664
3665   switch (token->type)
3666     {
3667     case CPP_NAME:
3668       {
3669         tree id;
3670
3671         /* We don't know yet whether or not this will be a
3672            template-id.  */
3673         cp_parser_parse_tentatively (parser);
3674         /* Try a template-id.  */
3675         id = cp_parser_template_id (parser, template_keyword_p,
3676                                     check_dependency_p,
3677                                     declarator_p);
3678         /* If it worked, we're done.  */
3679         if (cp_parser_parse_definitely (parser))
3680           return id;
3681         /* Otherwise, it's an ordinary identifier.  */
3682         return cp_parser_identifier (parser);
3683       }
3684
3685     case CPP_TEMPLATE_ID:
3686       return cp_parser_template_id (parser, template_keyword_p,
3687                                     check_dependency_p,
3688                                     declarator_p);
3689
3690     case CPP_COMPL:
3691       {
3692         tree type_decl;
3693         tree qualifying_scope;
3694         tree object_scope;
3695         tree scope;
3696         bool done;
3697
3698         /* Consume the `~' token.  */
3699         cp_lexer_consume_token (parser->lexer);
3700         /* Parse the class-name.  The standard, as written, seems to
3701            say that:
3702
3703              template <typename T> struct S { ~S (); };
3704              template <typename T> S<T>::~S() {}
3705
3706            is invalid, since `~' must be followed by a class-name, but
3707            `S<T>' is dependent, and so not known to be a class.
3708            That's not right; we need to look in uninstantiated
3709            templates.  A further complication arises from:
3710
3711              template <typename T> void f(T t) {
3712                t.T::~T();
3713              }
3714
3715            Here, it is not possible to look up `T' in the scope of `T'
3716            itself.  We must look in both the current scope, and the
3717            scope of the containing complete expression.
3718
3719            Yet another issue is:
3720
3721              struct S {
3722                int S;
3723                ~S();
3724              };
3725
3726              S::~S() {}
3727
3728            The standard does not seem to say that the `S' in `~S'
3729            should refer to the type `S' and not the data member
3730            `S::S'.  */
3731
3732         /* DR 244 says that we look up the name after the "~" in the
3733            same scope as we looked up the qualifying name.  That idea
3734            isn't fully worked out; it's more complicated than that.  */
3735         scope = parser->scope;
3736         object_scope = parser->object_scope;
3737         qualifying_scope = parser->qualifying_scope;
3738
3739         /* Check for invalid scopes.  */
3740         if (scope == error_mark_node)
3741           {
3742             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3743               cp_lexer_consume_token (parser->lexer);
3744             return error_mark_node;
3745           }
3746         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3747           {
3748             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3749               error ("scope %qT before %<~%> is not a class-name", scope);
3750             cp_parser_simulate_error (parser);
3751             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3752               cp_lexer_consume_token (parser->lexer);
3753             return error_mark_node;
3754           }
3755         gcc_assert (!scope || TYPE_P (scope));
3756
3757         /* If the name is of the form "X::~X" it's OK.  */
3758         token = cp_lexer_peek_token (parser->lexer);
3759         if (scope
3760             && token->type == CPP_NAME
3761             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3762                 == CPP_OPEN_PAREN)
3763             && constructor_name_p (token->u.value, scope))
3764           {
3765             cp_lexer_consume_token (parser->lexer);
3766             return build_nt (BIT_NOT_EXPR, scope);
3767           }
3768
3769         /* If there was an explicit qualification (S::~T), first look
3770            in the scope given by the qualification (i.e., S).  */
3771         done = false;
3772         type_decl = NULL_TREE;
3773         if (scope)
3774           {
3775             cp_parser_parse_tentatively (parser);
3776             type_decl = cp_parser_class_name (parser,
3777                                               /*typename_keyword_p=*/false,
3778                                               /*template_keyword_p=*/false,
3779                                               none_type,
3780                                               /*check_dependency=*/false,
3781                                               /*class_head_p=*/false,
3782                                               declarator_p);
3783             if (cp_parser_parse_definitely (parser))
3784               done = true;
3785           }
3786         /* In "N::S::~S", look in "N" as well.  */
3787         if (!done && scope && qualifying_scope)
3788           {
3789             cp_parser_parse_tentatively (parser);
3790             parser->scope = qualifying_scope;
3791             parser->object_scope = NULL_TREE;
3792             parser->qualifying_scope = NULL_TREE;
3793             type_decl
3794               = cp_parser_class_name (parser,
3795                                       /*typename_keyword_p=*/false,
3796                                       /*template_keyword_p=*/false,
3797                                       none_type,
3798                                       /*check_dependency=*/false,
3799                                       /*class_head_p=*/false,
3800                                       declarator_p);
3801             if (cp_parser_parse_definitely (parser))
3802               done = true;
3803           }
3804         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3805         else if (!done && object_scope)
3806           {
3807             cp_parser_parse_tentatively (parser);
3808             parser->scope = object_scope;
3809             parser->object_scope = NULL_TREE;
3810             parser->qualifying_scope = NULL_TREE;
3811             type_decl
3812               = cp_parser_class_name (parser,
3813                                       /*typename_keyword_p=*/false,
3814                                       /*template_keyword_p=*/false,
3815                                       none_type,
3816                                       /*check_dependency=*/false,
3817                                       /*class_head_p=*/false,
3818                                       declarator_p);
3819             if (cp_parser_parse_definitely (parser))
3820               done = true;
3821           }
3822         /* Look in the surrounding context.  */
3823         if (!done)
3824           {
3825             parser->scope = NULL_TREE;
3826             parser->object_scope = NULL_TREE;
3827             parser->qualifying_scope = NULL_TREE;
3828             type_decl
3829               = cp_parser_class_name (parser,
3830                                       /*typename_keyword_p=*/false,
3831                                       /*template_keyword_p=*/false,
3832                                       none_type,
3833                                       /*check_dependency=*/false,
3834                                       /*class_head_p=*/false,
3835                                       declarator_p);
3836           }
3837         /* If an error occurred, assume that the name of the
3838            destructor is the same as the name of the qualifying
3839            class.  That allows us to keep parsing after running
3840            into ill-formed destructor names.  */
3841         if (type_decl == error_mark_node && scope)
3842           return build_nt (BIT_NOT_EXPR, scope);
3843         else if (type_decl == error_mark_node)
3844           return error_mark_node;
3845
3846         /* Check that destructor name and scope match.  */
3847         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3848           {
3849             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3850               error ("declaration of %<~%T%> as member of %qT",
3851                      type_decl, scope);
3852             cp_parser_simulate_error (parser);
3853             return error_mark_node;
3854           }
3855
3856         /* [class.dtor]
3857
3858            A typedef-name that names a class shall not be used as the
3859            identifier in the declarator for a destructor declaration.  */
3860         if (declarator_p
3861             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3862             && !DECL_SELF_REFERENCE_P (type_decl)
3863             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3864           error ("typedef-name %qD used as destructor declarator",
3865                  type_decl);
3866
3867         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3868       }
3869
3870     case CPP_KEYWORD:
3871       if (token->keyword == RID_OPERATOR)
3872         {
3873           tree id;
3874
3875           /* This could be a template-id, so we try that first.  */
3876           cp_parser_parse_tentatively (parser);
3877           /* Try a template-id.  */
3878           id = cp_parser_template_id (parser, template_keyword_p,
3879                                       /*check_dependency_p=*/true,
3880                                       declarator_p);
3881           /* If that worked, we're done.  */
3882           if (cp_parser_parse_definitely (parser))
3883             return id;
3884           /* We still don't know whether we're looking at an
3885              operator-function-id or a conversion-function-id.  */
3886           cp_parser_parse_tentatively (parser);
3887           /* Try an operator-function-id.  */
3888           id = cp_parser_operator_function_id (parser);
3889           /* If that didn't work, try a conversion-function-id.  */
3890           if (!cp_parser_parse_definitely (parser))
3891             id = cp_parser_conversion_function_id (parser);
3892
3893           return id;
3894         }
3895       /* Fall through.  */
3896
3897     default:
3898       if (optional_p)
3899         return NULL_TREE;
3900       cp_parser_error (parser, "expected unqualified-id");
3901       return error_mark_node;
3902     }
3903 }
3904
3905 /* Parse an (optional) nested-name-specifier.
3906
3907    nested-name-specifier:
3908      class-or-namespace-name :: nested-name-specifier [opt]
3909      class-or-namespace-name :: template nested-name-specifier [opt]
3910
3911    PARSER->SCOPE should be set appropriately before this function is
3912    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3913    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3914    in name lookups.
3915
3916    Sets PARSER->SCOPE to the class (TYPE) or namespace
3917    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3918    it unchanged if there is no nested-name-specifier.  Returns the new
3919    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3920
3921    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3922    part of a declaration and/or decl-specifier.  */
3923
3924 static tree
3925 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3926                                      bool typename_keyword_p,
3927                                      bool check_dependency_p,
3928                                      bool type_p,
3929                                      bool is_declaration)
3930 {
3931   bool success = false;
3932   cp_token_position start = 0;
3933   cp_token *token;
3934
3935   /* Remember where the nested-name-specifier starts.  */
3936   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3937     {
3938       start = cp_lexer_token_position (parser->lexer, false);
3939       push_deferring_access_checks (dk_deferred);
3940     }
3941
3942   while (true)
3943     {
3944       tree new_scope;
3945       tree old_scope;
3946       tree saved_qualifying_scope;
3947       bool template_keyword_p;
3948
3949       /* Spot cases that cannot be the beginning of a
3950          nested-name-specifier.  */
3951       token = cp_lexer_peek_token (parser->lexer);
3952
3953       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3954          the already parsed nested-name-specifier.  */
3955       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3956         {
3957           /* Grab the nested-name-specifier and continue the loop.  */
3958           cp_parser_pre_parsed_nested_name_specifier (parser);
3959           /* If we originally encountered this nested-name-specifier
3960              with IS_DECLARATION set to false, we will not have
3961              resolved TYPENAME_TYPEs, so we must do so here.  */
3962           if (is_declaration
3963               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3964             {
3965               new_scope = resolve_typename_type (parser->scope,
3966                                                  /*only_current_p=*/false);
3967               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3968                 parser->scope = new_scope;
3969             }
3970           success = true;
3971           continue;
3972         }
3973
3974       /* Spot cases that cannot be the beginning of a
3975          nested-name-specifier.  On the second and subsequent times
3976          through the loop, we look for the `template' keyword.  */
3977       if (success && token->keyword == RID_TEMPLATE)
3978         ;
3979       /* A template-id can start a nested-name-specifier.  */
3980       else if (token->type == CPP_TEMPLATE_ID)
3981         ;
3982       else
3983         {
3984           /* If the next token is not an identifier, then it is
3985              definitely not a class-or-namespace-name.  */
3986           if (token->type != CPP_NAME)
3987             break;
3988           /* If the following token is neither a `<' (to begin a
3989              template-id), nor a `::', then we are not looking at a
3990              nested-name-specifier.  */
3991           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3992           if (token->type != CPP_SCOPE
3993               && !cp_parser_nth_token_starts_template_argument_list_p
3994                   (parser, 2))
3995             break;
3996         }
3997
3998       /* The nested-name-specifier is optional, so we parse
3999          tentatively.  */
4000       cp_parser_parse_tentatively (parser);
4001
4002       /* Look for the optional `template' keyword, if this isn't the
4003          first time through the loop.  */
4004       if (success)
4005         template_keyword_p = cp_parser_optional_template_keyword (parser);
4006       else
4007         template_keyword_p = false;
4008
4009       /* Save the old scope since the name lookup we are about to do
4010          might destroy it.  */
4011       old_scope = parser->scope;
4012       saved_qualifying_scope = parser->qualifying_scope;
4013       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4014          look up names in "X<T>::I" in order to determine that "Y" is
4015          a template.  So, if we have a typename at this point, we make
4016          an effort to look through it.  */
4017       if (is_declaration
4018           && !typename_keyword_p
4019           && parser->scope
4020           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4021         parser->scope = resolve_typename_type (parser->scope,
4022                                                /*only_current_p=*/false);
4023       /* Parse the qualifying entity.  */
4024       new_scope
4025         = cp_parser_class_or_namespace_name (parser,
4026                                              typename_keyword_p,
4027                                              template_keyword_p,
4028                                              check_dependency_p,
4029                                              type_p,
4030                                              is_declaration);
4031       /* Look for the `::' token.  */
4032       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4033
4034       /* If we found what we wanted, we keep going; otherwise, we're
4035          done.  */
4036       if (!cp_parser_parse_definitely (parser))
4037         {
4038           bool error_p = false;
4039
4040           /* Restore the OLD_SCOPE since it was valid before the
4041              failed attempt at finding the last
4042              class-or-namespace-name.  */
4043           parser->scope = old_scope;
4044           parser->qualifying_scope = saved_qualifying_scope;
4045           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4046             break;
4047           /* If the next token is an identifier, and the one after
4048              that is a `::', then any valid interpretation would have
4049              found a class-or-namespace-name.  */
4050           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4051                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4052                      == CPP_SCOPE)
4053                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4054                      != CPP_COMPL))
4055             {
4056               token = cp_lexer_consume_token (parser->lexer);
4057               if (!error_p)
4058                 {
4059                   if (!token->ambiguous_p)
4060                     {
4061                       tree decl;
4062                       tree ambiguous_decls;
4063
4064                       decl = cp_parser_lookup_name (parser, token->u.value,
4065                                                     none_type,
4066                                                     /*is_template=*/false,
4067                                                     /*is_namespace=*/false,
4068                                                     /*check_dependency=*/true,
4069                                                     &ambiguous_decls);
4070                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4071                         error ("%qD used without template parameters", decl);
4072                       else if (ambiguous_decls)
4073                         {
4074                           error ("reference to %qD is ambiguous",
4075                                  token->u.value);
4076                           print_candidates (ambiguous_decls);
4077                           decl = error_mark_node;
4078                         }
4079                       else
4080                         cp_parser_name_lookup_error
4081                           (parser, token->u.value, decl,
4082                            "is not a class or namespace");
4083                     }
4084                   parser->scope = error_mark_node;
4085                   error_p = true;
4086                   /* Treat this as a successful nested-name-specifier
4087                      due to:
4088
4089                      [basic.lookup.qual]
4090
4091                      If the name found is not a class-name (clause
4092                      _class_) or namespace-name (_namespace.def_), the
4093                      program is ill-formed.  */
4094                   success = true;
4095                 }
4096               cp_lexer_consume_token (parser->lexer);
4097             }
4098           break;
4099         }
4100       /* We've found one valid nested-name-specifier.  */
4101       success = true;
4102       /* Name lookup always gives us a DECL.  */
4103       if (TREE_CODE (new_scope) == TYPE_DECL)
4104         new_scope = TREE_TYPE (new_scope);
4105       /* Uses of "template" must be followed by actual templates.  */
4106       if (template_keyword_p
4107           && !(CLASS_TYPE_P (new_scope)
4108                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4109                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4110                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4111           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4112                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4113                    == TEMPLATE_ID_EXPR)))
4114         pedwarn (TYPE_P (new_scope)
4115                  ? "%qT is not a template"
4116                  : "%qD is not a template",
4117                  new_scope);
4118       /* If it is a class scope, try to complete it; we are about to
4119          be looking up names inside the class.  */
4120       if (TYPE_P (new_scope)
4121           /* Since checking types for dependency can be expensive,
4122              avoid doing it if the type is already complete.  */
4123           && !COMPLETE_TYPE_P (new_scope)
4124           /* Do not try to complete dependent types.  */
4125           && !dependent_type_p (new_scope))
4126         {
4127           new_scope = complete_type (new_scope);
4128           /* If it is a typedef to current class, use the current
4129              class instead, as the typedef won't have any names inside
4130              it yet.  */
4131           if (!COMPLETE_TYPE_P (new_scope)
4132               && currently_open_class (new_scope))
4133             new_scope = TYPE_MAIN_VARIANT (new_scope);
4134         }
4135       /* Make sure we look in the right scope the next time through
4136          the loop.  */
4137       parser->scope = new_scope;
4138     }
4139
4140   /* If parsing tentatively, replace the sequence of tokens that makes
4141      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4142      token.  That way, should we re-parse the token stream, we will
4143      not have to repeat the effort required to do the parse, nor will
4144      we issue duplicate error messages.  */
4145   if (success && start)
4146     {
4147       cp_token *token;
4148
4149       token = cp_lexer_token_at (parser->lexer, start);
4150       /* Reset the contents of the START token.  */
4151       token->type = CPP_NESTED_NAME_SPECIFIER;
4152       /* Retrieve any deferred checks.  Do not pop this access checks yet
4153          so the memory will not be reclaimed during token replacing below.  */
4154       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4155       token->u.tree_check_value->value = parser->scope;
4156       token->u.tree_check_value->checks = get_deferred_access_checks ();
4157       token->u.tree_check_value->qualifying_scope =
4158         parser->qualifying_scope;
4159       token->keyword = RID_MAX;
4160
4161       /* Purge all subsequent tokens.  */
4162       cp_lexer_purge_tokens_after (parser->lexer, start);
4163     }
4164
4165   if (start)
4166     pop_to_parent_deferring_access_checks ();
4167
4168   return success ? parser->scope : NULL_TREE;
4169 }
4170
4171 /* Parse a nested-name-specifier.  See
4172    cp_parser_nested_name_specifier_opt for details.  This function
4173    behaves identically, except that it will an issue an error if no
4174    nested-name-specifier is present.  */
4175
4176 static tree
4177 cp_parser_nested_name_specifier (cp_parser *parser,
4178                                  bool typename_keyword_p,
4179                                  bool check_dependency_p,
4180                                  bool type_p,
4181                                  bool is_declaration)
4182 {
4183   tree scope;
4184
4185   /* Look for the nested-name-specifier.  */
4186   scope = cp_parser_nested_name_specifier_opt (parser,
4187                                                typename_keyword_p,
4188                                                check_dependency_p,
4189                                                type_p,
4190                                                is_declaration);
4191   /* If it was not present, issue an error message.  */
4192   if (!scope)
4193     {
4194       cp_parser_error (parser, "expected nested-name-specifier");
4195       parser->scope = NULL_TREE;
4196     }
4197
4198   return scope;
4199 }
4200
4201 /* Parse a class-or-namespace-name.
4202
4203    class-or-namespace-name:
4204      class-name
4205      namespace-name
4206
4207    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4208    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4209    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4210    TYPE_P is TRUE iff the next name should be taken as a class-name,
4211    even the same name is declared to be another entity in the same
4212    scope.
4213
4214    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4215    specified by the class-or-namespace-name.  If neither is found the
4216    ERROR_MARK_NODE is returned.  */
4217
4218 static tree
4219 cp_parser_class_or_namespace_name (cp_parser *parser,
4220                                    bool typename_keyword_p,
4221                                    bool template_keyword_p,
4222                                    bool check_dependency_p,
4223                                    bool type_p,
4224                                    bool is_declaration)
4225 {
4226   tree saved_scope;
4227   tree saved_qualifying_scope;
4228   tree saved_object_scope;
4229   tree scope;
4230   bool only_class_p;
4231
4232   /* Before we try to parse the class-name, we must save away the
4233      current PARSER->SCOPE since cp_parser_class_name will destroy
4234      it.  */
4235   saved_scope = parser->scope;
4236   saved_qualifying_scope = parser->qualifying_scope;
4237   saved_object_scope = parser->object_scope;
4238   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4239      there is no need to look for a namespace-name.  */
4240   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4241   if (!only_class_p)
4242     cp_parser_parse_tentatively (parser);
4243   scope = cp_parser_class_name (parser,
4244                                 typename_keyword_p,
4245                                 template_keyword_p,
4246                                 type_p ? class_type : none_type,
4247                                 check_dependency_p,
4248                                 /*class_head_p=*/false,
4249                                 is_declaration);
4250   /* If that didn't work, try for a namespace-name.  */
4251   if (!only_class_p && !cp_parser_parse_definitely (parser))
4252     {
4253       /* Restore the saved scope.  */
4254       parser->scope = saved_scope;
4255       parser->qualifying_scope = saved_qualifying_scope;
4256       parser->object_scope = saved_object_scope;
4257       /* If we are not looking at an identifier followed by the scope
4258          resolution operator, then this is not part of a
4259          nested-name-specifier.  (Note that this function is only used
4260          to parse the components of a nested-name-specifier.)  */
4261       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4262           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4263         return error_mark_node;
4264       scope = cp_parser_namespace_name (parser);
4265     }
4266
4267   return scope;
4268 }
4269
4270 /* Parse a postfix-expression.
4271
4272    postfix-expression:
4273      primary-expression
4274      postfix-expression [ expression ]
4275      postfix-expression ( expression-list [opt] )
4276      simple-type-specifier ( expression-list [opt] )
4277      typename :: [opt] nested-name-specifier identifier
4278        ( expression-list [opt] )
4279      typename :: [opt] nested-name-specifier template [opt] template-id
4280        ( expression-list [opt] )
4281      postfix-expression . template [opt] id-expression
4282      postfix-expression -> template [opt] id-expression
4283      postfix-expression . pseudo-destructor-name
4284      postfix-expression -> pseudo-destructor-name
4285      postfix-expression ++
4286      postfix-expression --
4287      dynamic_cast < type-id > ( expression )
4288      static_cast < type-id > ( expression )
4289      reinterpret_cast < type-id > ( expression )
4290      const_cast < type-id > ( expression )
4291      typeid ( expression )
4292      typeid ( type-id )
4293
4294    GNU Extension:
4295
4296    postfix-expression:
4297      ( type-id ) { initializer-list , [opt] }
4298
4299    This extension is a GNU version of the C99 compound-literal
4300    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4301    but they are essentially the same concept.)
4302
4303    If ADDRESS_P is true, the postfix expression is the operand of the
4304    `&' operator.  CAST_P is true if this expression is the target of a
4305    cast.
4306
4307    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4308    class member access expressions [expr.ref].
4309
4310    Returns a representation of the expression.  */
4311
4312 static tree
4313 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4314                               bool member_access_only_p)
4315 {
4316   cp_token *token;
4317   enum rid keyword;
4318   cp_id_kind idk = CP_ID_KIND_NONE;
4319   tree postfix_expression = NULL_TREE;
4320   bool is_member_access = false;
4321
4322   /* Peek at the next token.  */
4323   token = cp_lexer_peek_token (parser->lexer);
4324   /* Some of the productions are determined by keywords.  */
4325   keyword = token->keyword;
4326   switch (keyword)
4327     {
4328     case RID_DYNCAST:
4329     case RID_STATCAST:
4330     case RID_REINTCAST:
4331     case RID_CONSTCAST:
4332       {
4333         tree type;
4334         tree expression;
4335         const char *saved_message;
4336
4337         /* All of these can be handled in the same way from the point
4338            of view of parsing.  Begin by consuming the token
4339            identifying the cast.  */
4340         cp_lexer_consume_token (parser->lexer);
4341
4342         /* New types cannot be defined in the cast.  */
4343         saved_message = parser->type_definition_forbidden_message;
4344         parser->type_definition_forbidden_message
4345           = "types may not be defined in casts";
4346
4347         /* Look for the opening `<'.  */
4348         cp_parser_require (parser, CPP_LESS, "%<<%>");
4349         /* Parse the type to which we are casting.  */
4350         type = cp_parser_type_id (parser);
4351         /* Look for the closing `>'.  */
4352         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4353         /* Restore the old message.  */
4354         parser->type_definition_forbidden_message = saved_message;
4355
4356         /* And the expression which is being cast.  */
4357         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4358         expression = cp_parser_expression (parser, /*cast_p=*/true);
4359         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4360
4361         /* Only type conversions to integral or enumeration types
4362            can be used in constant-expressions.  */
4363         if (!cast_valid_in_integral_constant_expression_p (type)
4364             && (cp_parser_non_integral_constant_expression
4365                 (parser,
4366                  "a cast to a type other than an integral or "
4367                  "enumeration type")))
4368           return error_mark_node;
4369
4370         switch (keyword)
4371           {
4372           case RID_DYNCAST:
4373             postfix_expression
4374               = build_dynamic_cast (type, expression, tf_warning_or_error);
4375             break;
4376           case RID_STATCAST:
4377             postfix_expression
4378               = build_static_cast (type, expression, tf_warning_or_error);
4379             break;
4380           case RID_REINTCAST:
4381             postfix_expression
4382               = build_reinterpret_cast (type, expression, 
4383                                         tf_warning_or_error);
4384             break;
4385           case RID_CONSTCAST:
4386             postfix_expression
4387               = build_const_cast (type, expression, tf_warning_or_error);
4388             break;
4389           default:
4390             gcc_unreachable ();
4391           }
4392       }
4393       break;
4394
4395     case RID_TYPEID:
4396       {
4397         tree type;
4398         const char *saved_message;
4399         bool saved_in_type_id_in_expr_p;
4400
4401         /* Consume the `typeid' token.  */
4402         cp_lexer_consume_token (parser->lexer);
4403         /* Look for the `(' token.  */
4404         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4405         /* Types cannot be defined in a `typeid' expression.  */
4406         saved_message = parser->type_definition_forbidden_message;
4407         parser->type_definition_forbidden_message
4408           = "types may not be defined in a %<typeid%> expression";
4409         /* We can't be sure yet whether we're looking at a type-id or an
4410            expression.  */
4411         cp_parser_parse_tentatively (parser);
4412         /* Try a type-id first.  */
4413         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4414         parser->in_type_id_in_expr_p = true;
4415         type = cp_parser_type_id (parser);
4416         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4417         /* Look for the `)' token.  Otherwise, we can't be sure that
4418            we're not looking at an expression: consider `typeid (int
4419            (3))', for example.  */
4420         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4421         /* If all went well, simply lookup the type-id.  */
4422         if (cp_parser_parse_definitely (parser))
4423           postfix_expression = get_typeid (type);
4424         /* Otherwise, fall back to the expression variant.  */
4425         else
4426           {
4427             tree expression;
4428
4429             /* Look for an expression.  */
4430             expression = cp_parser_expression (parser, /*cast_p=*/false);
4431             /* Compute its typeid.  */
4432             postfix_expression = build_typeid (expression);
4433             /* Look for the `)' token.  */
4434             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4435           }
4436         /* Restore the saved message.  */
4437         parser->type_definition_forbidden_message = saved_message;
4438         /* `typeid' may not appear in an integral constant expression.  */
4439         if (cp_parser_non_integral_constant_expression(parser,
4440                                                        "%<typeid%> operator"))
4441           return error_mark_node;
4442       }
4443       break;
4444
4445     case RID_TYPENAME:
4446       {
4447         tree type;
4448         /* The syntax permitted here is the same permitted for an
4449            elaborated-type-specifier.  */
4450         type = cp_parser_elaborated_type_specifier (parser,
4451                                                     /*is_friend=*/false,
4452                                                     /*is_declaration=*/false);
4453         postfix_expression = cp_parser_functional_cast (parser, type);
4454       }
4455       break;
4456
4457     default:
4458       {
4459         tree type;
4460
4461         /* If the next thing is a simple-type-specifier, we may be
4462            looking at a functional cast.  We could also be looking at
4463            an id-expression.  So, we try the functional cast, and if
4464            that doesn't work we fall back to the primary-expression.  */
4465         cp_parser_parse_tentatively (parser);
4466         /* Look for the simple-type-specifier.  */
4467         type = cp_parser_simple_type_specifier (parser,
4468                                                 /*decl_specs=*/NULL,
4469                                                 CP_PARSER_FLAGS_NONE);
4470         /* Parse the cast itself.  */
4471         if (!cp_parser_error_occurred (parser))
4472           postfix_expression
4473             = cp_parser_functional_cast (parser, type);
4474         /* If that worked, we're done.  */
4475         if (cp_parser_parse_definitely (parser))
4476           break;
4477
4478         /* If the functional-cast didn't work out, try a
4479            compound-literal.  */
4480         if (cp_parser_allow_gnu_extensions_p (parser)
4481             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4482           {
4483             VEC(constructor_elt,gc) *initializer_list = NULL;
4484             bool saved_in_type_id_in_expr_p;
4485
4486             cp_parser_parse_tentatively (parser);
4487             /* Consume the `('.  */
4488             cp_lexer_consume_token (parser->lexer);
4489             /* Parse the type.  */
4490             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4491             parser->in_type_id_in_expr_p = true;
4492             type = cp_parser_type_id (parser);
4493             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4494             /* Look for the `)'.  */
4495             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4496             /* Look for the `{'.  */
4497             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4498             /* If things aren't going well, there's no need to
4499                keep going.  */
4500             if (!cp_parser_error_occurred (parser))
4501               {
4502                 bool non_constant_p;
4503                 /* Parse the initializer-list.  */
4504                 initializer_list
4505                   = cp_parser_initializer_list (parser, &non_constant_p);
4506                 /* Allow a trailing `,'.  */
4507                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4508                   cp_lexer_consume_token (parser->lexer);
4509                 /* Look for the final `}'.  */
4510                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4511               }
4512             /* If that worked, we're definitely looking at a
4513                compound-literal expression.  */
4514             if (cp_parser_parse_definitely (parser))
4515               {
4516                 /* Warn the user that a compound literal is not
4517                    allowed in standard C++.  */
4518                 if (pedantic)
4519                   pedwarn ("ISO C++ forbids compound-literals");
4520                 /* For simplicity, we disallow compound literals in
4521                    constant-expressions.  We could
4522                    allow compound literals of integer type, whose
4523                    initializer was a constant, in constant
4524                    expressions.  Permitting that usage, as a further
4525                    extension, would not change the meaning of any
4526                    currently accepted programs.  (Of course, as
4527                    compound literals are not part of ISO C++, the
4528                    standard has nothing to say.)  */
4529                 if (cp_parser_non_integral_constant_expression 
4530                     (parser, "non-constant compound literals"))
4531                   {
4532                     postfix_expression = error_mark_node;
4533                     break;
4534                   }
4535                 /* Form the representation of the compound-literal.  */
4536                 postfix_expression
4537                   = finish_compound_literal (type, initializer_list);
4538                 break;
4539               }
4540           }
4541
4542         /* It must be a primary-expression.  */
4543         postfix_expression
4544           = cp_parser_primary_expression (parser, address_p, cast_p,
4545                                           /*template_arg_p=*/false,
4546                                           &idk);
4547       }
4548       break;
4549     }
4550
4551   /* Keep looping until the postfix-expression is complete.  */
4552   while (true)
4553     {
4554       if (idk == CP_ID_KIND_UNQUALIFIED
4555           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4556           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4557         /* It is not a Koenig lookup function call.  */
4558         postfix_expression
4559           = unqualified_name_lookup_error (postfix_expression);
4560
4561       /* Peek at the next token.  */
4562       token = cp_lexer_peek_token (parser->lexer);
4563
4564       switch (token->type)
4565         {
4566         case CPP_OPEN_SQUARE:
4567           postfix_expression
4568             = cp_parser_postfix_open_square_expression (parser,
4569                                                         postfix_expression,
4570                                                         false);
4571           idk = CP_ID_KIND_NONE;
4572           is_member_access = false;
4573           break;
4574
4575         case CPP_OPEN_PAREN:
4576           /* postfix-expression ( expression-list [opt] ) */
4577           {
4578             bool koenig_p;
4579             bool is_builtin_constant_p;
4580             bool saved_integral_constant_expression_p = false;
4581             bool saved_non_integral_constant_expression_p = false;
4582             tree args;
4583
4584             is_member_access = false;
4585
4586             is_builtin_constant_p
4587               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4588             if (is_builtin_constant_p)
4589               {
4590                 /* The whole point of __builtin_constant_p is to allow
4591                    non-constant expressions to appear as arguments.  */
4592                 saved_integral_constant_expression_p
4593                   = parser->integral_constant_expression_p;
4594                 saved_non_integral_constant_expression_p
4595                   = parser->non_integral_constant_expression_p;
4596                 parser->integral_constant_expression_p = false;
4597               }
4598             args = (cp_parser_parenthesized_expression_list
4599                     (parser, /*is_attribute_list=*/false,
4600                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4601                      /*non_constant_p=*/NULL));
4602             if (is_builtin_constant_p)
4603               {
4604                 parser->integral_constant_expression_p
4605                   = saved_integral_constant_expression_p;
4606                 parser->non_integral_constant_expression_p
4607                   = saved_non_integral_constant_expression_p;
4608               }
4609
4610             if (args == error_mark_node)
4611               {
4612                 postfix_expression = error_mark_node;
4613                 break;
4614               }
4615
4616             /* Function calls are not permitted in
4617                constant-expressions.  */
4618             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4619                 && cp_parser_non_integral_constant_expression (parser,
4620                                                                "a function call"))
4621               {
4622                 postfix_expression = error_mark_node;
4623                 break;
4624               }
4625
4626             koenig_p = false;
4627             if (idk == CP_ID_KIND_UNQUALIFIED)
4628               {
4629                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4630                   {
4631                     if (args)
4632                       {
4633                         koenig_p = true;
4634                         postfix_expression
4635                           = perform_koenig_lookup (postfix_expression, args);
4636                       }
4637                     else
4638                       postfix_expression
4639                         = unqualified_fn_lookup_error (postfix_expression);
4640                   }
4641                 /* We do not perform argument-dependent lookup if
4642                    normal lookup finds a non-function, in accordance
4643                    with the expected resolution of DR 218.  */
4644                 else if (args && is_overloaded_fn (postfix_expression))
4645                   {
4646                     tree fn = get_first_fn (postfix_expression);
4647
4648                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4649                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4650
4651                     /* Only do argument dependent lookup if regular
4652                        lookup does not find a set of member functions.
4653                        [basic.lookup.koenig]/2a  */
4654                     if (!DECL_FUNCTION_MEMBER_P (fn))
4655                       {
4656                         koenig_p = true;
4657                         postfix_expression
4658                           = perform_koenig_lookup (postfix_expression, args);
4659                       }
4660                   }
4661               }
4662
4663             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4664               {
4665                 tree instance = TREE_OPERAND (postfix_expression, 0);
4666                 tree fn = TREE_OPERAND (postfix_expression, 1);
4667
4668                 if (processing_template_decl
4669                     && (type_dependent_expression_p (instance)
4670                         || (!BASELINK_P (fn)
4671                             && TREE_CODE (fn) != FIELD_DECL)
4672                         || type_dependent_expression_p (fn)
4673                         || any_type_dependent_arguments_p (args)))
4674                   {
4675                     postfix_expression
4676                       = build_nt_call_list (postfix_expression, args);
4677                     break;
4678                   }
4679
4680                 if (BASELINK_P (fn))
4681                   postfix_expression
4682                     = (build_new_method_call
4683                        (instance, fn, args, NULL_TREE,
4684                         (idk == CP_ID_KIND_QUALIFIED
4685                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4686                         /*fn_p=*/NULL,
4687                         tf_warning_or_error));
4688                 else
4689                   postfix_expression
4690                     = finish_call_expr (postfix_expression, args,
4691                                         /*disallow_virtual=*/false,
4692                                         /*koenig_p=*/false,
4693                                         tf_warning_or_error);
4694               }
4695             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4696                      || TREE_CODE (postfix_expression) == MEMBER_REF
4697                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4698               postfix_expression = (build_offset_ref_call_from_tree
4699                                     (postfix_expression, args));
4700             else if (idk == CP_ID_KIND_QUALIFIED)
4701               /* A call to a static class member, or a namespace-scope
4702                  function.  */
4703               postfix_expression
4704                 = finish_call_expr (postfix_expression, args,
4705                                     /*disallow_virtual=*/true,
4706                                     koenig_p,
4707                                     tf_warning_or_error);
4708             else
4709               /* All other function calls.  */
4710               postfix_expression
4711                 = finish_call_expr (postfix_expression, args,
4712                                     /*disallow_virtual=*/false,
4713                                     koenig_p,
4714                                     tf_warning_or_error);
4715
4716             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4717             idk = CP_ID_KIND_NONE;
4718           }
4719           break;
4720
4721         case CPP_DOT:
4722         case CPP_DEREF:
4723           /* postfix-expression . template [opt] id-expression
4724              postfix-expression . pseudo-destructor-name
4725              postfix-expression -> template [opt] id-expression
4726              postfix-expression -> pseudo-destructor-name */
4727
4728           /* Consume the `.' or `->' operator.  */
4729           cp_lexer_consume_token (parser->lexer);
4730
4731           postfix_expression
4732             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4733                                                       postfix_expression,
4734                                                       false, &idk);
4735
4736           is_member_access = true;
4737           break;
4738
4739         case CPP_PLUS_PLUS:
4740           /* postfix-expression ++  */
4741           /* Consume the `++' token.  */
4742           cp_lexer_consume_token (parser->lexer);
4743           /* Generate a representation for the complete expression.  */
4744           postfix_expression
4745             = finish_increment_expr (postfix_expression,
4746                                      POSTINCREMENT_EXPR);
4747           /* Increments may not appear in constant-expressions.  */
4748           if (cp_parser_non_integral_constant_expression (parser,
4749                                                           "an increment"))
4750             postfix_expression = error_mark_node;
4751           idk = CP_ID_KIND_NONE;
4752           is_member_access = false;
4753           break;
4754
4755         case CPP_MINUS_MINUS:
4756           /* postfix-expression -- */
4757           /* Consume the `--' token.  */
4758           cp_lexer_consume_token (parser->lexer);
4759           /* Generate a representation for the complete expression.  */
4760           postfix_expression
4761             = finish_increment_expr (postfix_expression,
4762                                      POSTDECREMENT_EXPR);
4763           /* Decrements may not appear in constant-expressions.  */
4764           if (cp_parser_non_integral_constant_expression (parser,
4765                                                           "a decrement"))
4766             postfix_expression = error_mark_node;
4767           idk = CP_ID_KIND_NONE;
4768           is_member_access = false;
4769           break;
4770
4771         default:
4772           if (member_access_only_p)
4773             return is_member_access? postfix_expression : error_mark_node;
4774           else
4775             return postfix_expression;
4776         }
4777     }
4778
4779   /* We should never get here.  */
4780   gcc_unreachable ();
4781   return error_mark_node;
4782 }
4783
4784 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4785    by cp_parser_builtin_offsetof.  We're looking for
4786
4787      postfix-expression [ expression ]
4788
4789    FOR_OFFSETOF is set if we're being called in that context, which
4790    changes how we deal with integer constant expressions.  */
4791
4792 static tree
4793 cp_parser_postfix_open_square_expression (cp_parser *parser,
4794                                           tree postfix_expression,
4795                                           bool for_offsetof)
4796 {
4797   tree index;
4798
4799   /* Consume the `[' token.  */
4800   cp_lexer_consume_token (parser->lexer);
4801
4802   /* Parse the index expression.  */
4803   /* ??? For offsetof, there is a question of what to allow here.  If
4804      offsetof is not being used in an integral constant expression context,
4805      then we *could* get the right answer by computing the value at runtime.
4806      If we are in an integral constant expression context, then we might
4807      could accept any constant expression; hard to say without analysis.
4808      Rather than open the barn door too wide right away, allow only integer
4809      constant expressions here.  */
4810   if (for_offsetof)
4811     index = cp_parser_constant_expression (parser, false, NULL);
4812   else
4813     index = cp_parser_expression (parser, /*cast_p=*/false);
4814
4815   /* Look for the closing `]'.  */
4816   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4817
4818   /* Build the ARRAY_REF.  */
4819   postfix_expression = grok_array_decl (postfix_expression, index);
4820
4821   /* When not doing offsetof, array references are not permitted in
4822      constant-expressions.  */
4823   if (!for_offsetof
4824       && (cp_parser_non_integral_constant_expression
4825           (parser, "an array reference")))
4826     postfix_expression = error_mark_node;
4827
4828   return postfix_expression;
4829 }
4830
4831 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4832    by cp_parser_builtin_offsetof.  We're looking for
4833
4834      postfix-expression . template [opt] id-expression
4835      postfix-expression . pseudo-destructor-name
4836      postfix-expression -> template [opt] id-expression
4837      postfix-expression -> pseudo-destructor-name
4838
4839    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4840    limits what of the above we'll actually accept, but nevermind.
4841    TOKEN_TYPE is the "." or "->" token, which will already have been
4842    removed from the stream.  */
4843
4844 static tree
4845 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4846                                         enum cpp_ttype token_type,
4847                                         tree postfix_expression,
4848                                         bool for_offsetof, cp_id_kind *idk)
4849 {
4850   tree name;
4851   bool dependent_p;
4852   bool pseudo_destructor_p;
4853   tree scope = NULL_TREE;
4854
4855   /* If this is a `->' operator, dereference the pointer.  */
4856   if (token_type == CPP_DEREF)
4857     postfix_expression = build_x_arrow (postfix_expression);
4858   /* Check to see whether or not the expression is type-dependent.  */
4859   dependent_p = type_dependent_expression_p (postfix_expression);
4860   /* The identifier following the `->' or `.' is not qualified.  */
4861   parser->scope = NULL_TREE;
4862   parser->qualifying_scope = NULL_TREE;
4863   parser->object_scope = NULL_TREE;
4864   *idk = CP_ID_KIND_NONE;
4865   /* Enter the scope corresponding to the type of the object
4866      given by the POSTFIX_EXPRESSION.  */
4867   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4868     {
4869       scope = TREE_TYPE (postfix_expression);
4870       /* According to the standard, no expression should ever have
4871          reference type.  Unfortunately, we do not currently match
4872          the standard in this respect in that our internal representation
4873          of an expression may have reference type even when the standard
4874          says it does not.  Therefore, we have to manually obtain the
4875          underlying type here.  */
4876       scope = non_reference (scope);
4877       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4878       if (scope == unknown_type_node)
4879         {
4880           error ("%qE does not have class type", postfix_expression);
4881           scope = NULL_TREE;
4882         }
4883       else
4884         scope = complete_type_or_else (scope, NULL_TREE);
4885       /* Let the name lookup machinery know that we are processing a
4886          class member access expression.  */
4887       parser->context->object_type = scope;
4888       /* If something went wrong, we want to be able to discern that case,
4889          as opposed to the case where there was no SCOPE due to the type
4890          of expression being dependent.  */
4891       if (!scope)
4892         scope = error_mark_node;
4893       /* If the SCOPE was erroneous, make the various semantic analysis
4894          functions exit quickly -- and without issuing additional error
4895          messages.  */
4896       if (scope == error_mark_node)
4897         postfix_expression = error_mark_node;
4898     }
4899
4900   /* Assume this expression is not a pseudo-destructor access.  */
4901   pseudo_destructor_p = false;
4902
4903   /* If the SCOPE is a scalar type, then, if this is a valid program,
4904      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4905      is type dependent, it can be pseudo-destructor-name or something else.
4906      Try to parse it as pseudo-destructor-name first.  */
4907   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4908     {
4909       tree s;
4910       tree type;
4911
4912       cp_parser_parse_tentatively (parser);
4913       /* Parse the pseudo-destructor-name.  */
4914       s = NULL_TREE;
4915       cp_parser_pseudo_destructor_name (parser, &s, &type);
4916       if (dependent_p
4917           && (cp_parser_error_occurred (parser)
4918               || TREE_CODE (type) != TYPE_DECL
4919               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4920         cp_parser_abort_tentative_parse (parser);
4921       else if (cp_parser_parse_definitely (parser))
4922         {
4923           pseudo_destructor_p = true;
4924           postfix_expression
4925             = finish_pseudo_destructor_expr (postfix_expression,
4926                                              s, TREE_TYPE (type));
4927         }
4928     }
4929
4930   if (!pseudo_destructor_p)
4931     {
4932       /* If the SCOPE is not a scalar type, we are looking at an
4933          ordinary class member access expression, rather than a
4934          pseudo-destructor-name.  */
4935       bool template_p;
4936       /* Parse the id-expression.  */
4937       name = (cp_parser_id_expression
4938               (parser,
4939                cp_parser_optional_template_keyword (parser),
4940                /*check_dependency_p=*/true,
4941                &template_p,
4942                /*declarator_p=*/false,
4943                /*optional_p=*/false));
4944       /* In general, build a SCOPE_REF if the member name is qualified.
4945          However, if the name was not dependent and has already been
4946          resolved; there is no need to build the SCOPE_REF.  For example;
4947
4948              struct X { void f(); };
4949              template <typename T> void f(T* t) { t->X::f(); }
4950
4951          Even though "t" is dependent, "X::f" is not and has been resolved
4952          to a BASELINK; there is no need to include scope information.  */
4953
4954       /* But we do need to remember that there was an explicit scope for
4955          virtual function calls.  */
4956       if (parser->scope)
4957         *idk = CP_ID_KIND_QUALIFIED;
4958
4959       /* If the name is a template-id that names a type, we will get a
4960          TYPE_DECL here.  That is invalid code.  */
4961       if (TREE_CODE (name) == TYPE_DECL)
4962         {
4963           error ("invalid use of %qD", name);
4964           postfix_expression = error_mark_node;
4965         }
4966       else
4967         {
4968           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4969             {
4970               name = build_qualified_name (/*type=*/NULL_TREE,
4971                                            parser->scope,
4972                                            name,
4973                                            template_p);
4974               parser->scope = NULL_TREE;
4975               parser->qualifying_scope = NULL_TREE;
4976               parser->object_scope = NULL_TREE;
4977             }
4978           if (scope && name && BASELINK_P (name))
4979             adjust_result_of_qualified_name_lookup
4980               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4981           postfix_expression
4982             = finish_class_member_access_expr (postfix_expression, name,
4983                                                template_p, 
4984                                                tf_warning_or_error);
4985         }
4986     }
4987
4988   /* We no longer need to look up names in the scope of the object on
4989      the left-hand side of the `.' or `->' operator.  */
4990   parser->context->object_type = NULL_TREE;
4991
4992   /* Outside of offsetof, these operators may not appear in
4993      constant-expressions.  */
4994   if (!for_offsetof
4995       && (cp_parser_non_integral_constant_expression
4996           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
4997     postfix_expression = error_mark_node;
4998
4999   return postfix_expression;
5000 }
5001
5002 /* Parse a parenthesized expression-list.
5003
5004    expression-list:
5005      assignment-expression
5006      expression-list, assignment-expression
5007
5008    attribute-list:
5009      expression-list
5010      identifier
5011      identifier, expression-list
5012
5013    CAST_P is true if this expression is the target of a cast.
5014
5015    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5016    argument pack.
5017
5018    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5019    representation of an assignment-expression.  Note that a TREE_LIST
5020    is returned even if there is only a single expression in the list.
5021    error_mark_node is returned if the ( and or ) are
5022    missing. NULL_TREE is returned on no expressions. The parentheses
5023    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5024    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5025    indicates whether or not all of the expressions in the list were
5026    constant.  */
5027
5028 static tree
5029 cp_parser_parenthesized_expression_list (cp_parser* parser,
5030                                          bool is_attribute_list,
5031                                          bool cast_p,
5032                                          bool allow_expansion_p,
5033                                          bool *non_constant_p)
5034 {
5035   tree expression_list = NULL_TREE;
5036   bool fold_expr_p = is_attribute_list;
5037   tree identifier = NULL_TREE;
5038   bool saved_greater_than_is_operator_p;
5039
5040   /* Assume all the expressions will be constant.  */
5041   if (non_constant_p)
5042     *non_constant_p = false;
5043
5044   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5045     return error_mark_node;
5046
5047   /* Within a parenthesized expression, a `>' token is always
5048      the greater-than operator.  */
5049   saved_greater_than_is_operator_p
5050     = parser->greater_than_is_operator_p;
5051   parser->greater_than_is_operator_p = true;
5052
5053   /* Consume expressions until there are no more.  */
5054   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5055     while (true)
5056       {
5057         tree expr;
5058
5059         /* At the beginning of attribute lists, check to see if the
5060            next token is an identifier.  */
5061         if (is_attribute_list
5062             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5063           {
5064             cp_token *token;
5065
5066             /* Consume the identifier.  */
5067             token = cp_lexer_consume_token (parser->lexer);
5068             /* Save the identifier.  */
5069             identifier = token->u.value;
5070           }
5071         else
5072           {
5073             /* Parse the next assignment-expression.  */
5074             if (non_constant_p)
5075               {
5076                 bool expr_non_constant_p;
5077                 expr = (cp_parser_constant_expression
5078                         (parser, /*allow_non_constant_p=*/true,
5079                          &expr_non_constant_p));
5080                 if (expr_non_constant_p)
5081                   *non_constant_p = true;
5082               }
5083             else
5084               expr = cp_parser_assignment_expression (parser, cast_p);
5085
5086             if (fold_expr_p)
5087               expr = fold_non_dependent_expr (expr);
5088
5089             /* If we have an ellipsis, then this is an expression
5090                expansion.  */
5091             if (allow_expansion_p
5092                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5093               {
5094                 /* Consume the `...'.  */
5095                 cp_lexer_consume_token (parser->lexer);
5096
5097                 /* Build the argument pack.  */
5098                 expr = make_pack_expansion (expr);
5099               }
5100
5101              /* Add it to the list.  We add error_mark_node
5102                 expressions to the list, so that we can still tell if
5103                 the correct form for a parenthesized expression-list
5104                 is found. That gives better errors.  */
5105             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5106
5107             if (expr == error_mark_node)
5108               goto skip_comma;
5109           }
5110
5111         /* After the first item, attribute lists look the same as
5112            expression lists.  */
5113         is_attribute_list = false;
5114
5115       get_comma:;
5116         /* If the next token isn't a `,', then we are done.  */
5117         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5118           break;
5119
5120         /* Otherwise, consume the `,' and keep going.  */
5121         cp_lexer_consume_token (parser->lexer);
5122       }
5123
5124   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5125     {
5126       int ending;
5127
5128     skip_comma:;
5129       /* We try and resync to an unnested comma, as that will give the
5130          user better diagnostics.  */
5131       ending = cp_parser_skip_to_closing_parenthesis (parser,
5132                                                       /*recovering=*/true,
5133                                                       /*or_comma=*/true,
5134                                                       /*consume_paren=*/true);
5135       if (ending < 0)
5136         goto get_comma;
5137       if (!ending)
5138         {
5139           parser->greater_than_is_operator_p
5140             = saved_greater_than_is_operator_p;
5141           return error_mark_node;
5142         }
5143     }
5144
5145   parser->greater_than_is_operator_p
5146     = saved_greater_than_is_operator_p;
5147
5148   /* We built up the list in reverse order so we must reverse it now.  */
5149   expression_list = nreverse (expression_list);
5150   if (identifier)
5151     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5152
5153   return expression_list;
5154 }
5155
5156 /* Parse a pseudo-destructor-name.
5157
5158    pseudo-destructor-name:
5159      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5160      :: [opt] nested-name-specifier template template-id :: ~ type-name
5161      :: [opt] nested-name-specifier [opt] ~ type-name
5162
5163    If either of the first two productions is used, sets *SCOPE to the
5164    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5165    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5166    or ERROR_MARK_NODE if the parse fails.  */
5167
5168 static void
5169 cp_parser_pseudo_destructor_name (cp_parser* parser,
5170                                   tree* scope,
5171                                   tree* type)
5172 {
5173   bool nested_name_specifier_p;
5174
5175   /* Assume that things will not work out.  */
5176   *type = error_mark_node;
5177
5178   /* Look for the optional `::' operator.  */
5179   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5180   /* Look for the optional nested-name-specifier.  */
5181   nested_name_specifier_p
5182     = (cp_parser_nested_name_specifier_opt (parser,
5183                                             /*typename_keyword_p=*/false,
5184                                             /*check_dependency_p=*/true,
5185                                             /*type_p=*/false,
5186                                             /*is_declaration=*/true)
5187        != NULL_TREE);
5188   /* Now, if we saw a nested-name-specifier, we might be doing the
5189      second production.  */
5190   if (nested_name_specifier_p
5191       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5192     {
5193       /* Consume the `template' keyword.  */
5194       cp_lexer_consume_token (parser->lexer);
5195       /* Parse the template-id.  */
5196       cp_parser_template_id (parser,
5197                              /*template_keyword_p=*/true,
5198                              /*check_dependency_p=*/false,
5199                              /*is_declaration=*/true);
5200       /* Look for the `::' token.  */
5201       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5202     }
5203   /* If the next token is not a `~', then there might be some
5204      additional qualification.  */
5205   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5206     {
5207       /* At this point, we're looking for "type-name :: ~".  The type-name
5208          must not be a class-name, since this is a pseudo-destructor.  So,
5209          it must be either an enum-name, or a typedef-name -- both of which
5210          are just identifiers.  So, we peek ahead to check that the "::"
5211          and "~" tokens are present; if they are not, then we can avoid
5212          calling type_name.  */
5213       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5214           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5215           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5216         {
5217           cp_parser_error (parser, "non-scalar type");
5218           return;
5219         }
5220
5221       /* Look for the type-name.  */
5222       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5223       if (*scope == error_mark_node)
5224         return;
5225
5226       /* Look for the `::' token.  */
5227       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5228     }
5229   else
5230     *scope = NULL_TREE;
5231
5232   /* Look for the `~'.  */
5233   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5234   /* Look for the type-name again.  We are not responsible for
5235      checking that it matches the first type-name.  */
5236   *type = cp_parser_nonclass_name (parser);
5237 }
5238
5239 /* Parse a unary-expression.
5240
5241    unary-expression:
5242      postfix-expression
5243      ++ cast-expression
5244      -- cast-expression
5245      unary-operator cast-expression
5246      sizeof unary-expression
5247      sizeof ( type-id )
5248      new-expression
5249      delete-expression
5250
5251    GNU Extensions:
5252
5253    unary-expression:
5254      __extension__ cast-expression
5255      __alignof__ unary-expression
5256      __alignof__ ( type-id )
5257      __real__ cast-expression
5258      __imag__ cast-expression
5259      && identifier
5260
5261    ADDRESS_P is true iff the unary-expression is appearing as the
5262    operand of the `&' operator.   CAST_P is true if this expression is
5263    the target of a cast.
5264
5265    Returns a representation of the expression.  */
5266
5267 static tree
5268 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5269 {
5270   cp_token *token;
5271   enum tree_code unary_operator;
5272
5273   /* Peek at the next token.  */
5274   token = cp_lexer_peek_token (parser->lexer);
5275   /* Some keywords give away the kind of expression.  */
5276   if (token->type == CPP_KEYWORD)
5277     {
5278       enum rid keyword = token->keyword;
5279
5280       switch (keyword)
5281         {
5282         case RID_ALIGNOF:
5283         case RID_SIZEOF:
5284           {
5285             tree operand;
5286             enum tree_code op;
5287
5288             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5289             /* Consume the token.  */
5290             cp_lexer_consume_token (parser->lexer);
5291             /* Parse the operand.  */
5292             operand = cp_parser_sizeof_operand (parser, keyword);
5293
5294             if (TYPE_P (operand))
5295               return cxx_sizeof_or_alignof_type (operand, op, true);
5296             else
5297               return cxx_sizeof_or_alignof_expr (operand, op, true);
5298           }
5299
5300         case RID_NEW:
5301           return cp_parser_new_expression (parser);
5302
5303         case RID_DELETE:
5304           return cp_parser_delete_expression (parser);
5305
5306         case RID_EXTENSION:
5307           {
5308             /* The saved value of the PEDANTIC flag.  */
5309             int saved_pedantic;
5310             tree expr;
5311
5312             /* Save away the PEDANTIC flag.  */
5313             cp_parser_extension_opt (parser, &saved_pedantic);
5314             /* Parse the cast-expression.  */
5315             expr = cp_parser_simple_cast_expression (parser);
5316             /* Restore the PEDANTIC flag.  */
5317             pedantic = saved_pedantic;
5318
5319             return expr;
5320           }
5321
5322         case RID_REALPART:
5323         case RID_IMAGPART:
5324           {
5325             tree expression;
5326
5327             /* Consume the `__real__' or `__imag__' token.  */
5328             cp_lexer_consume_token (parser->lexer);
5329             /* Parse the cast-expression.  */
5330             expression = cp_parser_simple_cast_expression (parser);
5331             /* Create the complete representation.  */
5332             return build_x_unary_op ((keyword == RID_REALPART
5333                                       ? REALPART_EXPR : IMAGPART_EXPR),
5334                                      expression,
5335                                      tf_warning_or_error);
5336           }
5337           break;
5338
5339         default:
5340           break;
5341         }
5342     }
5343
5344   /* Look for the `:: new' and `:: delete', which also signal the
5345      beginning of a new-expression, or delete-expression,
5346      respectively.  If the next token is `::', then it might be one of
5347      these.  */
5348   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5349     {
5350       enum rid keyword;
5351
5352       /* See if the token after the `::' is one of the keywords in
5353          which we're interested.  */
5354       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5355       /* If it's `new', we have a new-expression.  */
5356       if (keyword == RID_NEW)
5357         return cp_parser_new_expression (parser);
5358       /* Similarly, for `delete'.  */
5359       else if (keyword == RID_DELETE)
5360         return cp_parser_delete_expression (parser);
5361     }
5362
5363   /* Look for a unary operator.  */
5364   unary_operator = cp_parser_unary_operator (token);
5365   /* The `++' and `--' operators can be handled similarly, even though
5366      they are not technically unary-operators in the grammar.  */
5367   if (unary_operator == ERROR_MARK)
5368     {
5369       if (token->type == CPP_PLUS_PLUS)
5370         unary_operator = PREINCREMENT_EXPR;
5371       else if (token->type == CPP_MINUS_MINUS)
5372         unary_operator = PREDECREMENT_EXPR;
5373       /* Handle the GNU address-of-label extension.  */
5374       else if (cp_parser_allow_gnu_extensions_p (parser)
5375                && token->type == CPP_AND_AND)
5376         {
5377           tree identifier;
5378           tree expression;
5379
5380           /* Consume the '&&' token.  */
5381           cp_lexer_consume_token (parser->lexer);
5382           /* Look for the identifier.  */
5383           identifier = cp_parser_identifier (parser);
5384           /* Create an expression representing the address.  */
5385           expression = finish_label_address_expr (identifier);
5386           if (cp_parser_non_integral_constant_expression (parser,
5387                                                 "the address of a label"))
5388             expression = error_mark_node;
5389           return expression;
5390         }
5391     }
5392   if (unary_operator != ERROR_MARK)
5393     {
5394       tree cast_expression;
5395       tree expression = error_mark_node;
5396       const char *non_constant_p = NULL;
5397
5398       /* Consume the operator token.  */
5399       token = cp_lexer_consume_token (parser->lexer);
5400       /* Parse the cast-expression.  */
5401       cast_expression
5402         = cp_parser_cast_expression (parser,
5403                                      unary_operator == ADDR_EXPR,
5404                                      /*cast_p=*/false);
5405       /* Now, build an appropriate representation.  */
5406       switch (unary_operator)
5407         {
5408         case INDIRECT_REF:
5409           non_constant_p = "%<*%>";
5410           expression = build_x_indirect_ref (cast_expression, "unary *",
5411                                              tf_warning_or_error);
5412           break;
5413
5414         case ADDR_EXPR:
5415           non_constant_p = "%<&%>";
5416           /* Fall through.  */
5417         case BIT_NOT_EXPR:
5418           expression = build_x_unary_op (unary_operator, cast_expression,
5419                                          tf_warning_or_error);
5420           break;
5421
5422         case PREINCREMENT_EXPR:
5423         case PREDECREMENT_EXPR:
5424           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5425                             ? "%<++%>" : "%<--%>");
5426           /* Fall through.  */
5427         case UNARY_PLUS_EXPR:
5428         case NEGATE_EXPR:
5429         case TRUTH_NOT_EXPR:
5430           expression = finish_unary_op_expr (unary_operator, cast_expression);
5431           break;
5432
5433         default:
5434           gcc_unreachable ();
5435         }
5436
5437       if (non_constant_p
5438           && cp_parser_non_integral_constant_expression (parser,
5439                                                          non_constant_p))
5440         expression = error_mark_node;
5441
5442       return expression;
5443     }
5444
5445   return cp_parser_postfix_expression (parser, address_p, cast_p,
5446                                        /*member_access_only_p=*/false);
5447 }
5448
5449 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5450    unary-operator, the corresponding tree code is returned.  */
5451
5452 static enum tree_code
5453 cp_parser_unary_operator (cp_token* token)
5454 {
5455   switch (token->type)
5456     {
5457     case CPP_MULT:
5458       return INDIRECT_REF;
5459
5460     case CPP_AND:
5461       return ADDR_EXPR;
5462
5463     case CPP_PLUS:
5464       return UNARY_PLUS_EXPR;
5465
5466     case CPP_MINUS:
5467       return NEGATE_EXPR;
5468
5469     case CPP_NOT:
5470       return TRUTH_NOT_EXPR;
5471
5472     case CPP_COMPL:
5473       return BIT_NOT_EXPR;
5474
5475     default:
5476       return ERROR_MARK;
5477     }
5478 }
5479
5480 /* Parse a new-expression.
5481
5482    new-expression:
5483      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5484      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5485
5486    Returns a representation of the expression.  */
5487
5488 static tree
5489 cp_parser_new_expression (cp_parser* parser)
5490 {
5491   bool global_scope_p;
5492   tree placement;
5493   tree type;
5494   tree initializer;
5495   tree nelts;
5496
5497   /* Look for the optional `::' operator.  */
5498   global_scope_p
5499     = (cp_parser_global_scope_opt (parser,
5500                                    /*current_scope_valid_p=*/false)
5501        != NULL_TREE);
5502   /* Look for the `new' operator.  */
5503   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5504   /* There's no easy way to tell a new-placement from the
5505      `( type-id )' construct.  */
5506   cp_parser_parse_tentatively (parser);
5507   /* Look for a new-placement.  */
5508   placement = cp_parser_new_placement (parser);
5509   /* If that didn't work out, there's no new-placement.  */
5510   if (!cp_parser_parse_definitely (parser))
5511     placement = NULL_TREE;
5512
5513   /* If the next token is a `(', then we have a parenthesized
5514      type-id.  */
5515   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5516     {
5517       /* Consume the `('.  */
5518       cp_lexer_consume_token (parser->lexer);
5519       /* Parse the type-id.  */
5520       type = cp_parser_type_id (parser);
5521       /* Look for the closing `)'.  */
5522       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5523       /* There should not be a direct-new-declarator in this production,
5524          but GCC used to allowed this, so we check and emit a sensible error
5525          message for this case.  */
5526       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5527         {
5528           error ("array bound forbidden after parenthesized type-id");
5529           inform ("try removing the parentheses around the type-id");
5530           cp_parser_direct_new_declarator (parser);
5531         }
5532       nelts = NULL_TREE;
5533     }
5534   /* Otherwise, there must be a new-type-id.  */
5535   else
5536     type = cp_parser_new_type_id (parser, &nelts);
5537
5538   /* If the next token is a `(', then we have a new-initializer.  */
5539   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5540     initializer = cp_parser_new_initializer (parser);
5541   else
5542     initializer = NULL_TREE;
5543
5544   /* A new-expression may not appear in an integral constant
5545      expression.  */
5546   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5547     return error_mark_node;
5548
5549   /* Create a representation of the new-expression.  */
5550   return build_new (placement, type, nelts, initializer, global_scope_p,
5551                     tf_warning_or_error);
5552 }
5553
5554 /* Parse a new-placement.
5555
5556    new-placement:
5557      ( expression-list )
5558
5559    Returns the same representation as for an expression-list.  */
5560
5561 static tree
5562 cp_parser_new_placement (cp_parser* parser)
5563 {
5564   tree expression_list;
5565
5566   /* Parse the expression-list.  */
5567   expression_list = (cp_parser_parenthesized_expression_list
5568                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5569                       /*non_constant_p=*/NULL));
5570
5571   return expression_list;
5572 }
5573
5574 /* Parse a new-type-id.
5575
5576    new-type-id:
5577      type-specifier-seq new-declarator [opt]
5578
5579    Returns the TYPE allocated.  If the new-type-id indicates an array
5580    type, *NELTS is set to the number of elements in the last array
5581    bound; the TYPE will not include the last array bound.  */
5582
5583 static tree
5584 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5585 {
5586   cp_decl_specifier_seq type_specifier_seq;
5587   cp_declarator *new_declarator;
5588   cp_declarator *declarator;
5589   cp_declarator *outer_declarator;
5590   const char *saved_message;
5591   tree type;
5592
5593   /* The type-specifier sequence must not contain type definitions.
5594      (It cannot contain declarations of new types either, but if they
5595      are not definitions we will catch that because they are not
5596      complete.)  */
5597   saved_message = parser->type_definition_forbidden_message;
5598   parser->type_definition_forbidden_message
5599     = "types may not be defined in a new-type-id";
5600   /* Parse the type-specifier-seq.  */
5601   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5602                                 &type_specifier_seq);
5603   /* Restore the old message.  */
5604   parser->type_definition_forbidden_message = saved_message;
5605   /* Parse the new-declarator.  */
5606   new_declarator = cp_parser_new_declarator_opt (parser);
5607
5608   /* Determine the number of elements in the last array dimension, if
5609      any.  */
5610   *nelts = NULL_TREE;
5611   /* Skip down to the last array dimension.  */
5612   declarator = new_declarator;
5613   outer_declarator = NULL;
5614   while (declarator && (declarator->kind == cdk_pointer
5615                         || declarator->kind == cdk_ptrmem))
5616     {
5617       outer_declarator = declarator;
5618       declarator = declarator->declarator;
5619     }
5620   while (declarator
5621          && declarator->kind == cdk_array
5622          && declarator->declarator
5623          && declarator->declarator->kind == cdk_array)
5624     {
5625       outer_declarator = declarator;
5626       declarator = declarator->declarator;
5627     }
5628
5629   if (declarator && declarator->kind == cdk_array)
5630     {
5631       *nelts = declarator->u.array.bounds;
5632       if (*nelts == error_mark_node)
5633         *nelts = integer_one_node;
5634
5635       if (outer_declarator)
5636         outer_declarator->declarator = declarator->declarator;
5637       else
5638         new_declarator = NULL;
5639     }
5640
5641   type = groktypename (&type_specifier_seq, new_declarator);
5642   return type;
5643 }
5644
5645 /* Parse an (optional) new-declarator.
5646
5647    new-declarator:
5648      ptr-operator new-declarator [opt]
5649      direct-new-declarator
5650
5651    Returns the declarator.  */
5652
5653 static cp_declarator *
5654 cp_parser_new_declarator_opt (cp_parser* parser)
5655 {
5656   enum tree_code code;
5657   tree type;
5658   cp_cv_quals cv_quals;
5659
5660   /* We don't know if there's a ptr-operator next, or not.  */
5661   cp_parser_parse_tentatively (parser);
5662   /* Look for a ptr-operator.  */
5663   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5664   /* If that worked, look for more new-declarators.  */
5665   if (cp_parser_parse_definitely (parser))
5666     {
5667       cp_declarator *declarator;
5668
5669       /* Parse another optional declarator.  */
5670       declarator = cp_parser_new_declarator_opt (parser);
5671
5672       return cp_parser_make_indirect_declarator
5673         (code, type, cv_quals, declarator);
5674     }
5675
5676   /* If the next token is a `[', there is a direct-new-declarator.  */
5677   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5678     return cp_parser_direct_new_declarator (parser);
5679
5680   return NULL;
5681 }
5682
5683 /* Parse a direct-new-declarator.
5684
5685    direct-new-declarator:
5686      [ expression ]
5687      direct-new-declarator [constant-expression]
5688
5689    */
5690
5691 static cp_declarator *
5692 cp_parser_direct_new_declarator (cp_parser* parser)
5693 {
5694   cp_declarator *declarator = NULL;
5695
5696   while (true)
5697     {
5698       tree expression;
5699
5700       /* Look for the opening `['.  */
5701       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5702       /* The first expression is not required to be constant.  */
5703       if (!declarator)
5704         {
5705           expression = cp_parser_expression (parser, /*cast_p=*/false);
5706           /* The standard requires that the expression have integral
5707              type.  DR 74 adds enumeration types.  We believe that the
5708              real intent is that these expressions be handled like the
5709              expression in a `switch' condition, which also allows
5710              classes with a single conversion to integral or
5711              enumeration type.  */
5712           if (!processing_template_decl)
5713             {
5714               expression
5715                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5716                                               expression,
5717                                               /*complain=*/true);
5718               if (!expression)
5719                 {
5720                   error ("expression in new-declarator must have integral "
5721                          "or enumeration type");
5722                   expression = error_mark_node;
5723                 }
5724             }
5725         }
5726       /* But all the other expressions must be.  */
5727       else
5728         expression
5729           = cp_parser_constant_expression (parser,
5730                                            /*allow_non_constant=*/false,
5731                                            NULL);
5732       /* Look for the closing `]'.  */
5733       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5734
5735       /* Add this bound to the declarator.  */
5736       declarator = make_array_declarator (declarator, expression);
5737
5738       /* If the next token is not a `[', then there are no more
5739          bounds.  */
5740       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5741         break;
5742     }
5743
5744   return declarator;
5745 }
5746
5747 /* Parse a new-initializer.
5748
5749    new-initializer:
5750      ( expression-list [opt] )
5751
5752    Returns a representation of the expression-list.  If there is no
5753    expression-list, VOID_ZERO_NODE is returned.  */
5754
5755 static tree
5756 cp_parser_new_initializer (cp_parser* parser)
5757 {
5758   tree expression_list;
5759
5760   expression_list = (cp_parser_parenthesized_expression_list
5761                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5762                       /*non_constant_p=*/NULL));
5763   if (!expression_list)
5764     expression_list = void_zero_node;
5765
5766   return expression_list;
5767 }
5768
5769 /* Parse a delete-expression.
5770
5771    delete-expression:
5772      :: [opt] delete cast-expression
5773      :: [opt] delete [ ] cast-expression
5774
5775    Returns a representation of the expression.  */
5776
5777 static tree
5778 cp_parser_delete_expression (cp_parser* parser)
5779 {
5780   bool global_scope_p;
5781   bool array_p;
5782   tree expression;
5783
5784   /* Look for the optional `::' operator.  */
5785   global_scope_p
5786     = (cp_parser_global_scope_opt (parser,
5787                                    /*current_scope_valid_p=*/false)
5788        != NULL_TREE);
5789   /* Look for the `delete' keyword.  */
5790   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5791   /* See if the array syntax is in use.  */
5792   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5793     {
5794       /* Consume the `[' token.  */
5795       cp_lexer_consume_token (parser->lexer);
5796       /* Look for the `]' token.  */
5797       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5798       /* Remember that this is the `[]' construct.  */
5799       array_p = true;
5800     }
5801   else
5802     array_p = false;
5803
5804   /* Parse the cast-expression.  */
5805   expression = cp_parser_simple_cast_expression (parser);
5806
5807   /* A delete-expression may not appear in an integral constant
5808      expression.  */
5809   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5810     return error_mark_node;
5811
5812   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5813 }
5814
5815 /* Parse a cast-expression.
5816
5817    cast-expression:
5818      unary-expression
5819      ( type-id ) cast-expression
5820
5821    ADDRESS_P is true iff the unary-expression is appearing as the
5822    operand of the `&' operator.   CAST_P is true if this expression is
5823    the target of a cast.
5824
5825    Returns a representation of the expression.  */
5826
5827 static tree
5828 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5829 {
5830   /* If it's a `(', then we might be looking at a cast.  */
5831   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5832     {
5833       tree type = NULL_TREE;
5834       tree expr = NULL_TREE;
5835       bool compound_literal_p;
5836       const char *saved_message;
5837
5838       /* There's no way to know yet whether or not this is a cast.
5839          For example, `(int (3))' is a unary-expression, while `(int)
5840          3' is a cast.  So, we resort to parsing tentatively.  */
5841       cp_parser_parse_tentatively (parser);
5842       /* Types may not be defined in a cast.  */
5843       saved_message = parser->type_definition_forbidden_message;
5844       parser->type_definition_forbidden_message
5845         = "types may not be defined in casts";
5846       /* Consume the `('.  */
5847       cp_lexer_consume_token (parser->lexer);
5848       /* A very tricky bit is that `(struct S) { 3 }' is a
5849          compound-literal (which we permit in C++ as an extension).
5850          But, that construct is not a cast-expression -- it is a
5851          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5852          is legal; if the compound-literal were a cast-expression,
5853          you'd need an extra set of parentheses.)  But, if we parse
5854          the type-id, and it happens to be a class-specifier, then we
5855          will commit to the parse at that point, because we cannot
5856          undo the action that is done when creating a new class.  So,
5857          then we cannot back up and do a postfix-expression.
5858
5859          Therefore, we scan ahead to the closing `)', and check to see
5860          if the token after the `)' is a `{'.  If so, we are not
5861          looking at a cast-expression.
5862
5863          Save tokens so that we can put them back.  */
5864       cp_lexer_save_tokens (parser->lexer);
5865       /* Skip tokens until the next token is a closing parenthesis.
5866          If we find the closing `)', and the next token is a `{', then
5867          we are looking at a compound-literal.  */
5868       compound_literal_p
5869         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5870                                                   /*consume_paren=*/true)
5871            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5872       /* Roll back the tokens we skipped.  */
5873       cp_lexer_rollback_tokens (parser->lexer);
5874       /* If we were looking at a compound-literal, simulate an error
5875          so that the call to cp_parser_parse_definitely below will
5876          fail.  */
5877       if (compound_literal_p)
5878         cp_parser_simulate_error (parser);
5879       else
5880         {
5881           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5882           parser->in_type_id_in_expr_p = true;
5883           /* Look for the type-id.  */
5884           type = cp_parser_type_id (parser);
5885           /* Look for the closing `)'.  */
5886           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5887           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5888         }
5889
5890       /* Restore the saved message.  */
5891       parser->type_definition_forbidden_message = saved_message;
5892
5893       /* If ok so far, parse the dependent expression. We cannot be
5894          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5895          ctor of T, but looks like a cast to function returning T
5896          without a dependent expression.  */
5897       if (!cp_parser_error_occurred (parser))
5898         expr = cp_parser_cast_expression (parser,
5899                                           /*address_p=*/false,
5900                                           /*cast_p=*/true);
5901
5902       if (cp_parser_parse_definitely (parser))
5903         {
5904           /* Warn about old-style casts, if so requested.  */
5905           if (warn_old_style_cast
5906               && !in_system_header
5907               && !VOID_TYPE_P (type)
5908               && current_lang_name != lang_name_c)
5909             warning (OPT_Wold_style_cast, "use of old-style cast");
5910
5911           /* Only type conversions to integral or enumeration types
5912              can be used in constant-expressions.  */
5913           if (!cast_valid_in_integral_constant_expression_p (type)
5914               && (cp_parser_non_integral_constant_expression
5915                   (parser,
5916                    "a cast to a type other than an integral or "
5917                    "enumeration type")))
5918             return error_mark_node;
5919
5920           /* Perform the cast.  */
5921           expr = build_c_cast (type, expr);
5922           return expr;
5923         }
5924     }
5925
5926   /* If we get here, then it's not a cast, so it must be a
5927      unary-expression.  */
5928   return cp_parser_unary_expression (parser, address_p, cast_p);
5929 }
5930
5931 /* Parse a binary expression of the general form:
5932
5933    pm-expression:
5934      cast-expression
5935      pm-expression .* cast-expression
5936      pm-expression ->* cast-expression
5937
5938    multiplicative-expression:
5939      pm-expression
5940      multiplicative-expression * pm-expression
5941      multiplicative-expression / pm-expression
5942      multiplicative-expression % pm-expression
5943
5944    additive-expression:
5945      multiplicative-expression
5946      additive-expression + multiplicative-expression
5947      additive-expression - multiplicative-expression
5948
5949    shift-expression:
5950      additive-expression
5951      shift-expression << additive-expression
5952      shift-expression >> additive-expression
5953
5954    relational-expression:
5955      shift-expression
5956      relational-expression < shift-expression
5957      relational-expression > shift-expression
5958      relational-expression <= shift-expression
5959      relational-expression >= shift-expression
5960
5961   GNU Extension:
5962
5963    relational-expression:
5964      relational-expression <? shift-expression
5965      relational-expression >? shift-expression
5966
5967    equality-expression:
5968      relational-expression
5969      equality-expression == relational-expression
5970      equality-expression != relational-expression
5971
5972    and-expression:
5973      equality-expression
5974      and-expression & equality-expression
5975
5976    exclusive-or-expression:
5977      and-expression
5978      exclusive-or-expression ^ and-expression
5979
5980    inclusive-or-expression:
5981      exclusive-or-expression
5982      inclusive-or-expression | exclusive-or-expression
5983
5984    logical-and-expression:
5985      inclusive-or-expression
5986      logical-and-expression && inclusive-or-expression
5987
5988    logical-or-expression:
5989      logical-and-expression
5990      logical-or-expression || logical-and-expression
5991
5992    All these are implemented with a single function like:
5993
5994    binary-expression:
5995      simple-cast-expression
5996      binary-expression <token> binary-expression
5997
5998    CAST_P is true if this expression is the target of a cast.
5999
6000    The binops_by_token map is used to get the tree codes for each <token> type.
6001    binary-expressions are associated according to a precedence table.  */
6002
6003 #define TOKEN_PRECEDENCE(token)                              \
6004 (((token->type == CPP_GREATER                                \
6005    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6006   && !parser->greater_than_is_operator_p)                    \
6007  ? PREC_NOT_OPERATOR                                         \
6008  : binops_by_token[token->type].prec)
6009
6010 static tree
6011 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6012                              enum cp_parser_prec prec)
6013 {
6014   cp_parser_expression_stack stack;
6015   cp_parser_expression_stack_entry *sp = &stack[0];
6016   tree lhs, rhs;
6017   cp_token *token;
6018   enum tree_code tree_type, lhs_type, rhs_type;
6019   enum cp_parser_prec new_prec, lookahead_prec;
6020   bool overloaded_p;
6021
6022   /* Parse the first expression.  */
6023   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6024   lhs_type = ERROR_MARK;
6025
6026   for (;;)
6027     {
6028       /* Get an operator token.  */
6029       token = cp_lexer_peek_token (parser->lexer);
6030
6031       if (warn_cxx0x_compat
6032           && token->type == CPP_RSHIFT
6033           && !parser->greater_than_is_operator_p)
6034         {
6035           warning (OPT_Wc__0x_compat, 
6036                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6037                    &token->location);
6038           warning (OPT_Wc__0x_compat, 
6039                    "suggest parentheses around %<>>%> expression");
6040         }
6041
6042       new_prec = TOKEN_PRECEDENCE (token);
6043
6044       /* Popping an entry off the stack means we completed a subexpression:
6045          - either we found a token which is not an operator (`>' where it is not
6046            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6047            will happen repeatedly;
6048          - or, we found an operator which has lower priority.  This is the case
6049            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6050            parsing `3 * 4'.  */
6051       if (new_prec <= prec)
6052         {
6053           if (sp == stack)
6054             break;
6055           else
6056             goto pop;
6057         }
6058
6059      get_rhs:
6060       tree_type = binops_by_token[token->type].tree_type;
6061
6062       /* We used the operator token.  */
6063       cp_lexer_consume_token (parser->lexer);
6064
6065       /* Extract another operand.  It may be the RHS of this expression
6066          or the LHS of a new, higher priority expression.  */
6067       rhs = cp_parser_simple_cast_expression (parser);
6068       rhs_type = ERROR_MARK;
6069
6070       /* Get another operator token.  Look up its precedence to avoid
6071          building a useless (immediately popped) stack entry for common
6072          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6073       token = cp_lexer_peek_token (parser->lexer);
6074       lookahead_prec = TOKEN_PRECEDENCE (token);
6075       if (lookahead_prec > new_prec)
6076         {
6077           /* ... and prepare to parse the RHS of the new, higher priority
6078              expression.  Since precedence levels on the stack are
6079              monotonically increasing, we do not have to care about
6080              stack overflows.  */
6081           sp->prec = prec;
6082           sp->tree_type = tree_type;
6083           sp->lhs = lhs;
6084           sp->lhs_type = lhs_type;
6085           sp++;
6086           lhs = rhs;
6087           lhs_type = rhs_type;
6088           prec = new_prec;
6089           new_prec = lookahead_prec;
6090           goto get_rhs;
6091
6092          pop:
6093           /* If the stack is not empty, we have parsed into LHS the right side
6094              (`4' in the example above) of an expression we had suspended.
6095              We can use the information on the stack to recover the LHS (`3')
6096              from the stack together with the tree code (`MULT_EXPR'), and
6097              the precedence of the higher level subexpression
6098              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6099              which will be used to actually build the additive expression.  */
6100           --sp;
6101           prec = sp->prec;
6102           tree_type = sp->tree_type;
6103           rhs = lhs;
6104           rhs_type = lhs_type;
6105           lhs = sp->lhs;
6106           lhs_type = sp->lhs_type;
6107         }
6108
6109       overloaded_p = false;
6110       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6111                                &overloaded_p, tf_warning_or_error);
6112       lhs_type = tree_type;
6113
6114       /* If the binary operator required the use of an overloaded operator,
6115          then this expression cannot be an integral constant-expression.
6116          An overloaded operator can be used even if both operands are
6117          otherwise permissible in an integral constant-expression if at
6118          least one of the operands is of enumeration type.  */
6119
6120       if (overloaded_p
6121           && (cp_parser_non_integral_constant_expression
6122               (parser, "calls to overloaded operators")))
6123         return error_mark_node;
6124     }
6125
6126   return lhs;
6127 }
6128
6129
6130 /* Parse the `? expression : assignment-expression' part of a
6131    conditional-expression.  The LOGICAL_OR_EXPR is the
6132    logical-or-expression that started the conditional-expression.
6133    Returns a representation of the entire conditional-expression.
6134
6135    This routine is used by cp_parser_assignment_expression.
6136
6137      ? expression : assignment-expression
6138
6139    GNU Extensions:
6140
6141      ? : assignment-expression */
6142
6143 static tree
6144 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6145 {
6146   tree expr;
6147   tree assignment_expr;
6148
6149   /* Consume the `?' token.  */
6150   cp_lexer_consume_token (parser->lexer);
6151   if (cp_parser_allow_gnu_extensions_p (parser)
6152       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6153     /* Implicit true clause.  */
6154     expr = NULL_TREE;
6155   else
6156     /* Parse the expression.  */
6157     expr = cp_parser_expression (parser, /*cast_p=*/false);
6158
6159   /* The next token should be a `:'.  */
6160   cp_parser_require (parser, CPP_COLON, "%<:%>");
6161   /* Parse the assignment-expression.  */
6162   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6163
6164   /* Build the conditional-expression.  */
6165   return build_x_conditional_expr (logical_or_expr,
6166                                    expr,
6167                                    assignment_expr,
6168                                    tf_warning_or_error);
6169 }
6170
6171 /* Parse an assignment-expression.
6172
6173    assignment-expression:
6174      conditional-expression
6175      logical-or-expression assignment-operator assignment_expression
6176      throw-expression
6177
6178    CAST_P is true if this expression is the target of a cast.
6179
6180    Returns a representation for the expression.  */
6181
6182 static tree
6183 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6184 {
6185   tree expr;
6186
6187   /* If the next token is the `throw' keyword, then we're looking at
6188      a throw-expression.  */
6189   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6190     expr = cp_parser_throw_expression (parser);
6191   /* Otherwise, it must be that we are looking at a
6192      logical-or-expression.  */
6193   else
6194     {
6195       /* Parse the binary expressions (logical-or-expression).  */
6196       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6197       /* If the next token is a `?' then we're actually looking at a
6198          conditional-expression.  */
6199       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6200         return cp_parser_question_colon_clause (parser, expr);
6201       else
6202         {
6203           enum tree_code assignment_operator;
6204
6205           /* If it's an assignment-operator, we're using the second
6206              production.  */
6207           assignment_operator
6208             = cp_parser_assignment_operator_opt (parser);
6209           if (assignment_operator != ERROR_MARK)
6210             {
6211               tree rhs;
6212
6213               /* Parse the right-hand side of the assignment.  */
6214               rhs = cp_parser_assignment_expression (parser, cast_p);
6215               /* An assignment may not appear in a
6216                  constant-expression.  */
6217               if (cp_parser_non_integral_constant_expression (parser,
6218                                                               "an assignment"))
6219                 return error_mark_node;
6220               /* Build the assignment expression.  */
6221               expr = build_x_modify_expr (expr,
6222                                           assignment_operator,
6223                                           rhs,
6224                                           tf_warning_or_error);
6225             }
6226         }
6227     }
6228
6229   return expr;
6230 }
6231
6232 /* Parse an (optional) assignment-operator.
6233
6234    assignment-operator: one of
6235      = *= /= %= += -= >>= <<= &= ^= |=
6236
6237    GNU Extension:
6238
6239    assignment-operator: one of
6240      <?= >?=
6241
6242    If the next token is an assignment operator, the corresponding tree
6243    code is returned, and the token is consumed.  For example, for
6244    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6245    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6246    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6247    operator, ERROR_MARK is returned.  */
6248
6249 static enum tree_code
6250 cp_parser_assignment_operator_opt (cp_parser* parser)
6251 {
6252   enum tree_code op;
6253   cp_token *token;
6254
6255   /* Peek at the next toen.  */
6256   token = cp_lexer_peek_token (parser->lexer);
6257
6258   switch (token->type)
6259     {
6260     case CPP_EQ:
6261       op = NOP_EXPR;
6262       break;
6263
6264     case CPP_MULT_EQ:
6265       op = MULT_EXPR;
6266       break;
6267
6268     case CPP_DIV_EQ:
6269       op = TRUNC_DIV_EXPR;
6270       break;
6271
6272     case CPP_MOD_EQ:
6273       op = TRUNC_MOD_EXPR;
6274       break;
6275
6276     case CPP_PLUS_EQ:
6277       op = PLUS_EXPR;
6278       break;
6279
6280     case CPP_MINUS_EQ:
6281       op = MINUS_EXPR;
6282       break;
6283
6284     case CPP_RSHIFT_EQ:
6285       op = RSHIFT_EXPR;
6286       break;
6287
6288     case CPP_LSHIFT_EQ:
6289       op = LSHIFT_EXPR;
6290       break;
6291
6292     case CPP_AND_EQ:
6293       op = BIT_AND_EXPR;
6294       break;
6295
6296     case CPP_XOR_EQ:
6297       op = BIT_XOR_EXPR;
6298       break;
6299
6300     case CPP_OR_EQ:
6301       op = BIT_IOR_EXPR;
6302       break;
6303
6304     default:
6305       /* Nothing else is an assignment operator.  */
6306       op = ERROR_MARK;
6307     }
6308
6309   /* If it was an assignment operator, consume it.  */
6310   if (op != ERROR_MARK)
6311     cp_lexer_consume_token (parser->lexer);
6312
6313   return op;
6314 }
6315
6316 /* Parse an expression.
6317
6318    expression:
6319      assignment-expression
6320      expression , assignment-expression
6321
6322    CAST_P is true if this expression is the target of a cast.
6323
6324    Returns a representation of the expression.  */
6325
6326 static tree
6327 cp_parser_expression (cp_parser* parser, bool cast_p)
6328 {
6329   tree expression = NULL_TREE;
6330
6331   while (true)
6332     {
6333       tree assignment_expression;
6334
6335       /* Parse the next assignment-expression.  */
6336       assignment_expression
6337         = cp_parser_assignment_expression (parser, cast_p);
6338       /* If this is the first assignment-expression, we can just
6339          save it away.  */
6340       if (!expression)
6341         expression = assignment_expression;
6342       else
6343         expression = build_x_compound_expr (expression,
6344                                             assignment_expression,
6345                                             tf_warning_or_error);
6346       /* If the next token is not a comma, then we are done with the
6347          expression.  */
6348       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6349         break;
6350       /* Consume the `,'.  */
6351       cp_lexer_consume_token (parser->lexer);
6352       /* A comma operator cannot appear in a constant-expression.  */
6353       if (cp_parser_non_integral_constant_expression (parser,
6354                                                       "a comma operator"))
6355         expression = error_mark_node;
6356     }
6357
6358   return expression;
6359 }
6360
6361 /* Parse a constant-expression.
6362
6363    constant-expression:
6364      conditional-expression
6365
6366   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6367   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6368   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6369   is false, NON_CONSTANT_P should be NULL.  */
6370
6371 static tree
6372 cp_parser_constant_expression (cp_parser* parser,
6373                                bool allow_non_constant_p,
6374                                bool *non_constant_p)
6375 {
6376   bool saved_integral_constant_expression_p;
6377   bool saved_allow_non_integral_constant_expression_p;
6378   bool saved_non_integral_constant_expression_p;
6379   tree expression;
6380
6381   /* It might seem that we could simply parse the
6382      conditional-expression, and then check to see if it were
6383      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6384      one that the compiler can figure out is constant, possibly after
6385      doing some simplifications or optimizations.  The standard has a
6386      precise definition of constant-expression, and we must honor
6387      that, even though it is somewhat more restrictive.
6388
6389      For example:
6390
6391        int i[(2, 3)];
6392
6393      is not a legal declaration, because `(2, 3)' is not a
6394      constant-expression.  The `,' operator is forbidden in a
6395      constant-expression.  However, GCC's constant-folding machinery
6396      will fold this operation to an INTEGER_CST for `3'.  */
6397
6398   /* Save the old settings.  */
6399   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6400   saved_allow_non_integral_constant_expression_p
6401     = parser->allow_non_integral_constant_expression_p;
6402   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6403   /* We are now parsing a constant-expression.  */
6404   parser->integral_constant_expression_p = true;
6405   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6406   parser->non_integral_constant_expression_p = false;
6407   /* Although the grammar says "conditional-expression", we parse an
6408      "assignment-expression", which also permits "throw-expression"
6409      and the use of assignment operators.  In the case that
6410      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6411      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6412      actually essential that we look for an assignment-expression.
6413      For example, cp_parser_initializer_clauses uses this function to
6414      determine whether a particular assignment-expression is in fact
6415      constant.  */
6416   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6417   /* Restore the old settings.  */
6418   parser->integral_constant_expression_p
6419     = saved_integral_constant_expression_p;
6420   parser->allow_non_integral_constant_expression_p
6421     = saved_allow_non_integral_constant_expression_p;
6422   if (allow_non_constant_p)
6423     *non_constant_p = parser->non_integral_constant_expression_p;
6424   else if (parser->non_integral_constant_expression_p)
6425     expression = error_mark_node;
6426   parser->non_integral_constant_expression_p
6427     = saved_non_integral_constant_expression_p;
6428
6429   return expression;
6430 }
6431
6432 /* Parse __builtin_offsetof.
6433
6434    offsetof-expression:
6435      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6436
6437    offsetof-member-designator:
6438      id-expression
6439      | offsetof-member-designator "." id-expression
6440      | offsetof-member-designator "[" expression "]"  */
6441
6442 static tree
6443 cp_parser_builtin_offsetof (cp_parser *parser)
6444 {
6445   int save_ice_p, save_non_ice_p;
6446   tree type, expr;
6447   cp_id_kind dummy;
6448
6449   /* We're about to accept non-integral-constant things, but will
6450      definitely yield an integral constant expression.  Save and
6451      restore these values around our local parsing.  */
6452   save_ice_p = parser->integral_constant_expression_p;
6453   save_non_ice_p = parser->non_integral_constant_expression_p;
6454
6455   /* Consume the "__builtin_offsetof" token.  */
6456   cp_lexer_consume_token (parser->lexer);
6457   /* Consume the opening `('.  */
6458   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6459   /* Parse the type-id.  */
6460   type = cp_parser_type_id (parser);
6461   /* Look for the `,'.  */
6462   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6463
6464   /* Build the (type *)null that begins the traditional offsetof macro.  */
6465   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6466                             tf_warning_or_error);
6467
6468   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6469   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6470                                                  true, &dummy);
6471   while (true)
6472     {
6473       cp_token *token = cp_lexer_peek_token (parser->lexer);
6474       switch (token->type)
6475         {
6476         case CPP_OPEN_SQUARE:
6477           /* offsetof-member-designator "[" expression "]" */
6478           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6479           break;
6480
6481         case CPP_DOT:
6482           /* offsetof-member-designator "." identifier */
6483           cp_lexer_consume_token (parser->lexer);
6484           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6485                                                          true, &dummy);
6486           break;
6487
6488         case CPP_CLOSE_PAREN:
6489           /* Consume the ")" token.  */
6490           cp_lexer_consume_token (parser->lexer);
6491           goto success;
6492
6493         default:
6494           /* Error.  We know the following require will fail, but
6495              that gives the proper error message.  */
6496           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6497           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6498           expr = error_mark_node;
6499           goto failure;
6500         }
6501     }
6502
6503  success:
6504   /* If we're processing a template, we can't finish the semantics yet.
6505      Otherwise we can fold the entire expression now.  */
6506   if (processing_template_decl)
6507     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6508   else
6509     expr = finish_offsetof (expr);
6510
6511  failure:
6512   parser->integral_constant_expression_p = save_ice_p;
6513   parser->non_integral_constant_expression_p = save_non_ice_p;
6514
6515   return expr;
6516 }
6517
6518 /* Parse a trait expression.  */
6519
6520 static tree
6521 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6522 {
6523   cp_trait_kind kind;
6524   tree type1, type2 = NULL_TREE;
6525   bool binary = false;
6526   cp_decl_specifier_seq decl_specs;
6527
6528   switch (keyword)
6529     {
6530     case RID_HAS_NOTHROW_ASSIGN:
6531       kind = CPTK_HAS_NOTHROW_ASSIGN;
6532       break;
6533     case RID_HAS_NOTHROW_CONSTRUCTOR:
6534       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6535       break;
6536     case RID_HAS_NOTHROW_COPY:
6537       kind = CPTK_HAS_NOTHROW_COPY;
6538       break;
6539     case RID_HAS_TRIVIAL_ASSIGN:
6540       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6541       break;
6542     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6543       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6544       break;
6545     case RID_HAS_TRIVIAL_COPY:
6546       kind = CPTK_HAS_TRIVIAL_COPY;
6547       break;
6548     case RID_HAS_TRIVIAL_DESTRUCTOR:
6549       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6550       break;
6551     case RID_HAS_VIRTUAL_DESTRUCTOR:
6552       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6553       break;
6554     case RID_IS_ABSTRACT:
6555       kind = CPTK_IS_ABSTRACT;
6556       break;
6557     case RID_IS_BASE_OF:
6558       kind = CPTK_IS_BASE_OF;
6559       binary = true;
6560       break;
6561     case RID_IS_CLASS:
6562       kind = CPTK_IS_CLASS;
6563       break;
6564     case RID_IS_CONVERTIBLE_TO:
6565       kind = CPTK_IS_CONVERTIBLE_TO;
6566       binary = true;
6567       break;
6568     case RID_IS_EMPTY:
6569       kind = CPTK_IS_EMPTY;
6570       break;
6571     case RID_IS_ENUM:
6572       kind = CPTK_IS_ENUM;
6573       break;
6574     case RID_IS_POD:
6575       kind = CPTK_IS_POD;
6576       break;
6577     case RID_IS_POLYMORPHIC:
6578       kind = CPTK_IS_POLYMORPHIC;
6579       break;
6580     case RID_IS_UNION:
6581       kind = CPTK_IS_UNION;
6582       break;
6583     default:
6584       gcc_unreachable ();
6585     }
6586
6587   /* Consume the token.  */
6588   cp_lexer_consume_token (parser->lexer);
6589
6590   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6591
6592   type1 = cp_parser_type_id (parser);
6593
6594   if (type1 == error_mark_node)
6595     return error_mark_node;
6596
6597   /* Build a trivial decl-specifier-seq.  */
6598   clear_decl_specs (&decl_specs);
6599   decl_specs.type = type1;
6600
6601   /* Call grokdeclarator to figure out what type this is.  */
6602   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6603                           /*initialized=*/0, /*attrlist=*/NULL);
6604
6605   if (binary)
6606     {
6607       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6608  
6609       type2 = cp_parser_type_id (parser);
6610
6611       if (type2 == error_mark_node)
6612         return error_mark_node;
6613
6614       /* Build a trivial decl-specifier-seq.  */
6615       clear_decl_specs (&decl_specs);
6616       decl_specs.type = type2;
6617
6618       /* Call grokdeclarator to figure out what type this is.  */
6619       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6620                               /*initialized=*/0, /*attrlist=*/NULL);
6621     }
6622
6623   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6624
6625   /* Complete the trait expression, which may mean either processing
6626      the trait expr now or saving it for template instantiation.  */
6627   return finish_trait_expr (kind, type1, type2);
6628 }
6629
6630 /* Statements [gram.stmt.stmt]  */
6631
6632 /* Parse a statement.
6633
6634    statement:
6635      labeled-statement
6636      expression-statement
6637      compound-statement
6638      selection-statement
6639      iteration-statement
6640      jump-statement
6641      declaration-statement
6642      try-block
6643
6644   IN_COMPOUND is true when the statement is nested inside a
6645   cp_parser_compound_statement; this matters for certain pragmas.
6646
6647   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6648   is a (possibly labeled) if statement which is not enclosed in braces
6649   and has an else clause.  This is used to implement -Wparentheses.  */
6650
6651 static void
6652 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6653                      bool in_compound, bool *if_p)
6654 {
6655   tree statement;
6656   cp_token *token;
6657   location_t statement_location;
6658
6659  restart:
6660   if (if_p != NULL)
6661     *if_p = false;
6662   /* There is no statement yet.  */
6663   statement = NULL_TREE;
6664   /* Peek at the next token.  */
6665   token = cp_lexer_peek_token (parser->lexer);
6666   /* Remember the location of the first token in the statement.  */
6667   statement_location = token->location;
6668   /* If this is a keyword, then that will often determine what kind of
6669      statement we have.  */
6670   if (token->type == CPP_KEYWORD)
6671     {
6672       enum rid keyword = token->keyword;
6673
6674       switch (keyword)
6675         {
6676         case RID_CASE:
6677         case RID_DEFAULT:
6678           /* Looks like a labeled-statement with a case label.
6679              Parse the label, and then use tail recursion to parse
6680              the statement.  */
6681           cp_parser_label_for_labeled_statement (parser);
6682           goto restart;
6683
6684         case RID_IF:
6685         case RID_SWITCH:
6686           statement = cp_parser_selection_statement (parser, if_p);
6687           break;
6688
6689         case RID_WHILE:
6690         case RID_DO:
6691         case RID_FOR:
6692           statement = cp_parser_iteration_statement (parser);
6693           break;
6694
6695         case RID_BREAK:
6696         case RID_CONTINUE:
6697         case RID_RETURN:
6698         case RID_GOTO:
6699           statement = cp_parser_jump_statement (parser);
6700           break;
6701
6702           /* Objective-C++ exception-handling constructs.  */
6703         case RID_AT_TRY:
6704         case RID_AT_CATCH:
6705         case RID_AT_FINALLY:
6706         case RID_AT_SYNCHRONIZED:
6707         case RID_AT_THROW:
6708           statement = cp_parser_objc_statement (parser);
6709           break;
6710
6711         case RID_TRY:
6712           statement = cp_parser_try_block (parser);
6713           break;
6714
6715         case RID_NAMESPACE:
6716           /* This must be a namespace alias definition.  */
6717           cp_parser_declaration_statement (parser);
6718           return;
6719           
6720         default:
6721           /* It might be a keyword like `int' that can start a
6722              declaration-statement.  */
6723           break;
6724         }
6725     }
6726   else if (token->type == CPP_NAME)
6727     {
6728       /* If the next token is a `:', then we are looking at a
6729          labeled-statement.  */
6730       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6731       if (token->type == CPP_COLON)
6732         {
6733           /* Looks like a labeled-statement with an ordinary label.
6734              Parse the label, and then use tail recursion to parse
6735              the statement.  */
6736           cp_parser_label_for_labeled_statement (parser);
6737           goto restart;
6738         }
6739     }
6740   /* Anything that starts with a `{' must be a compound-statement.  */
6741   else if (token->type == CPP_OPEN_BRACE)
6742     statement = cp_parser_compound_statement (parser, NULL, false);
6743   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6744      a statement all its own.  */
6745   else if (token->type == CPP_PRAGMA)
6746     {
6747       /* Only certain OpenMP pragmas are attached to statements, and thus
6748          are considered statements themselves.  All others are not.  In
6749          the context of a compound, accept the pragma as a "statement" and
6750          return so that we can check for a close brace.  Otherwise we
6751          require a real statement and must go back and read one.  */
6752       if (in_compound)
6753         cp_parser_pragma (parser, pragma_compound);
6754       else if (!cp_parser_pragma (parser, pragma_stmt))
6755         goto restart;
6756       return;
6757     }
6758   else if (token->type == CPP_EOF)
6759     {
6760       cp_parser_error (parser, "expected statement");
6761       return;
6762     }
6763
6764   /* Everything else must be a declaration-statement or an
6765      expression-statement.  Try for the declaration-statement
6766      first, unless we are looking at a `;', in which case we know that
6767      we have an expression-statement.  */
6768   if (!statement)
6769     {
6770       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6771         {
6772           cp_parser_parse_tentatively (parser);
6773           /* Try to parse the declaration-statement.  */
6774           cp_parser_declaration_statement (parser);
6775           /* If that worked, we're done.  */
6776           if (cp_parser_parse_definitely (parser))
6777             return;
6778         }
6779       /* Look for an expression-statement instead.  */
6780       statement = cp_parser_expression_statement (parser, in_statement_expr);
6781     }
6782
6783   /* Set the line number for the statement.  */
6784   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6785     SET_EXPR_LOCATION (statement, statement_location);
6786 }
6787
6788 /* Parse the label for a labeled-statement, i.e.
6789
6790    identifier :
6791    case constant-expression :
6792    default :
6793
6794    GNU Extension:
6795    case constant-expression ... constant-expression : statement
6796
6797    When a label is parsed without errors, the label is added to the
6798    parse tree by the finish_* functions, so this function doesn't
6799    have to return the label.  */
6800
6801 static void
6802 cp_parser_label_for_labeled_statement (cp_parser* parser)
6803 {
6804   cp_token *token;
6805
6806   /* The next token should be an identifier.  */
6807   token = cp_lexer_peek_token (parser->lexer);
6808   if (token->type != CPP_NAME
6809       && token->type != CPP_KEYWORD)
6810     {
6811       cp_parser_error (parser, "expected labeled-statement");
6812       return;
6813     }
6814
6815   switch (token->keyword)
6816     {
6817     case RID_CASE:
6818       {
6819         tree expr, expr_hi;
6820         cp_token *ellipsis;
6821
6822         /* Consume the `case' token.  */
6823         cp_lexer_consume_token (parser->lexer);
6824         /* Parse the constant-expression.  */
6825         expr = cp_parser_constant_expression (parser,
6826                                               /*allow_non_constant_p=*/false,
6827                                               NULL);
6828
6829         ellipsis = cp_lexer_peek_token (parser->lexer);
6830         if (ellipsis->type == CPP_ELLIPSIS)
6831           {
6832             /* Consume the `...' token.  */
6833             cp_lexer_consume_token (parser->lexer);
6834             expr_hi =
6835               cp_parser_constant_expression (parser,
6836                                              /*allow_non_constant_p=*/false,
6837                                              NULL);
6838             /* We don't need to emit warnings here, as the common code
6839                will do this for us.  */
6840           }
6841         else
6842           expr_hi = NULL_TREE;
6843
6844         if (parser->in_switch_statement_p)
6845           finish_case_label (expr, expr_hi);
6846         else
6847           error ("case label %qE not within a switch statement", expr);
6848       }
6849       break;
6850
6851     case RID_DEFAULT:
6852       /* Consume the `default' token.  */
6853       cp_lexer_consume_token (parser->lexer);
6854
6855       if (parser->in_switch_statement_p)
6856         finish_case_label (NULL_TREE, NULL_TREE);
6857       else
6858         error ("case label not within a switch statement");
6859       break;
6860
6861     default:
6862       /* Anything else must be an ordinary label.  */
6863       finish_label_stmt (cp_parser_identifier (parser));
6864       break;
6865     }
6866
6867   /* Require the `:' token.  */
6868   cp_parser_require (parser, CPP_COLON, "%<:%>");
6869 }
6870
6871 /* Parse an expression-statement.
6872
6873    expression-statement:
6874      expression [opt] ;
6875
6876    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6877    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6878    indicates whether this expression-statement is part of an
6879    expression statement.  */
6880
6881 static tree
6882 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6883 {
6884   tree statement = NULL_TREE;
6885
6886   /* If the next token is a ';', then there is no expression
6887      statement.  */
6888   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6889     statement = cp_parser_expression (parser, /*cast_p=*/false);
6890
6891   /* Consume the final `;'.  */
6892   cp_parser_consume_semicolon_at_end_of_statement (parser);
6893
6894   if (in_statement_expr
6895       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6896     /* This is the final expression statement of a statement
6897        expression.  */
6898     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6899   else if (statement)
6900     statement = finish_expr_stmt (statement);
6901   else
6902     finish_stmt ();
6903
6904   return statement;
6905 }
6906
6907 /* Parse a compound-statement.
6908
6909    compound-statement:
6910      { statement-seq [opt] }
6911
6912    GNU extension:
6913
6914    compound-statement:
6915      { label-declaration-seq [opt] statement-seq [opt] }
6916
6917    label-declaration-seq:
6918      label-declaration
6919      label-declaration-seq label-declaration
6920
6921    Returns a tree representing the statement.  */
6922
6923 static tree
6924 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6925                               bool in_try)
6926 {
6927   tree compound_stmt;
6928
6929   /* Consume the `{'.  */
6930   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
6931     return error_mark_node;
6932   /* Begin the compound-statement.  */
6933   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6934   /* If the next keyword is `__label__' we have a label declaration.  */
6935   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6936     cp_parser_label_declaration (parser);
6937   /* Parse an (optional) statement-seq.  */
6938   cp_parser_statement_seq_opt (parser, in_statement_expr);
6939   /* Finish the compound-statement.  */
6940   finish_compound_stmt (compound_stmt);
6941   /* Consume the `}'.  */
6942   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
6943
6944   return compound_stmt;
6945 }
6946
6947 /* Parse an (optional) statement-seq.
6948
6949    statement-seq:
6950      statement
6951      statement-seq [opt] statement  */
6952
6953 static void
6954 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6955 {
6956   /* Scan statements until there aren't any more.  */
6957   while (true)
6958     {
6959       cp_token *token = cp_lexer_peek_token (parser->lexer);
6960
6961       /* If we're looking at a `}', then we've run out of statements.  */
6962       if (token->type == CPP_CLOSE_BRACE
6963           || token->type == CPP_EOF
6964           || token->type == CPP_PRAGMA_EOL)
6965         break;
6966       
6967       /* If we are in a compound statement and find 'else' then
6968          something went wrong.  */
6969       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6970         {
6971           if (parser->in_statement & IN_IF_STMT) 
6972             break;
6973           else
6974             {
6975               token = cp_lexer_consume_token (parser->lexer);
6976               error ("%<else%> without a previous %<if%>");
6977             }
6978         }
6979
6980       /* Parse the statement.  */
6981       cp_parser_statement (parser, in_statement_expr, true, NULL);
6982     }
6983 }
6984
6985 /* Parse a selection-statement.
6986
6987    selection-statement:
6988      if ( condition ) statement
6989      if ( condition ) statement else statement
6990      switch ( condition ) statement
6991
6992    Returns the new IF_STMT or SWITCH_STMT.
6993
6994    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6995    is a (possibly labeled) if statement which is not enclosed in
6996    braces and has an else clause.  This is used to implement
6997    -Wparentheses.  */
6998
6999 static tree
7000 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7001 {
7002   cp_token *token;
7003   enum rid keyword;
7004
7005   if (if_p != NULL)
7006     *if_p = false;
7007
7008   /* Peek at the next token.  */
7009   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7010
7011   /* See what kind of keyword it is.  */
7012   keyword = token->keyword;
7013   switch (keyword)
7014     {
7015     case RID_IF:
7016     case RID_SWITCH:
7017       {
7018         tree statement;
7019         tree condition;
7020
7021         /* Look for the `('.  */
7022         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7023           {
7024             cp_parser_skip_to_end_of_statement (parser);
7025             return error_mark_node;
7026           }
7027
7028         /* Begin the selection-statement.  */
7029         if (keyword == RID_IF)
7030           statement = begin_if_stmt ();
7031         else
7032           statement = begin_switch_stmt ();
7033
7034         /* Parse the condition.  */
7035         condition = cp_parser_condition (parser);
7036         /* Look for the `)'.  */
7037         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7038           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7039                                                  /*consume_paren=*/true);
7040
7041         if (keyword == RID_IF)
7042           {
7043             bool nested_if;
7044             unsigned char in_statement;
7045
7046             /* Add the condition.  */
7047             finish_if_stmt_cond (condition, statement);
7048
7049             /* Parse the then-clause.  */
7050             in_statement = parser->in_statement;
7051             parser->in_statement |= IN_IF_STMT;
7052             cp_parser_implicitly_scoped_statement (parser, &nested_if);
7053             parser->in_statement = in_statement;
7054
7055             finish_then_clause (statement);
7056
7057             /* If the next token is `else', parse the else-clause.  */
7058             if (cp_lexer_next_token_is_keyword (parser->lexer,
7059                                                 RID_ELSE))
7060               {
7061                 /* Consume the `else' keyword.  */
7062                 cp_lexer_consume_token (parser->lexer);
7063                 begin_else_clause (statement);
7064                 /* Parse the else-clause.  */
7065                 cp_parser_implicitly_scoped_statement (parser, NULL);
7066                 finish_else_clause (statement);
7067
7068                 /* If we are currently parsing a then-clause, then
7069                    IF_P will not be NULL.  We set it to true to
7070                    indicate that this if statement has an else clause.
7071                    This may trigger the Wparentheses warning below
7072                    when we get back up to the parent if statement.  */
7073                 if (if_p != NULL)
7074                   *if_p = true;
7075               }
7076             else
7077               {
7078                 /* This if statement does not have an else clause.  If
7079                    NESTED_IF is true, then the then-clause is an if
7080                    statement which does have an else clause.  We warn
7081                    about the potential ambiguity.  */
7082                 if (nested_if)
7083                   warning (OPT_Wparentheses,
7084                            ("%Hsuggest explicit braces "
7085                             "to avoid ambiguous %<else%>"),
7086                            EXPR_LOCUS (statement));
7087               }
7088
7089             /* Now we're all done with the if-statement.  */
7090             finish_if_stmt (statement);
7091           }
7092         else
7093           {
7094             bool in_switch_statement_p;
7095             unsigned char in_statement;
7096
7097             /* Add the condition.  */
7098             finish_switch_cond (condition, statement);
7099
7100             /* Parse the body of the switch-statement.  */
7101             in_switch_statement_p = parser->in_switch_statement_p;
7102             in_statement = parser->in_statement;
7103             parser->in_switch_statement_p = true;
7104             parser->in_statement |= IN_SWITCH_STMT;
7105             cp_parser_implicitly_scoped_statement (parser, NULL);
7106             parser->in_switch_statement_p = in_switch_statement_p;
7107             parser->in_statement = in_statement;
7108
7109             /* Now we're all done with the switch-statement.  */
7110             finish_switch_stmt (statement);
7111           }
7112
7113         return statement;
7114       }
7115       break;
7116
7117     default:
7118       cp_parser_error (parser, "expected selection-statement");
7119       return error_mark_node;
7120     }
7121 }
7122
7123 /* Parse a condition.
7124
7125    condition:
7126      expression
7127      type-specifier-seq declarator = assignment-expression
7128
7129    GNU Extension:
7130
7131    condition:
7132      type-specifier-seq declarator asm-specification [opt]
7133        attributes [opt] = assignment-expression
7134
7135    Returns the expression that should be tested.  */
7136
7137 static tree
7138 cp_parser_condition (cp_parser* parser)
7139 {
7140   cp_decl_specifier_seq type_specifiers;
7141   const char *saved_message;
7142
7143   /* Try the declaration first.  */
7144   cp_parser_parse_tentatively (parser);
7145   /* New types are not allowed in the type-specifier-seq for a
7146      condition.  */
7147   saved_message = parser->type_definition_forbidden_message;
7148   parser->type_definition_forbidden_message
7149     = "types may not be defined in conditions";
7150   /* Parse the type-specifier-seq.  */
7151   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7152                                 &type_specifiers);
7153   /* Restore the saved message.  */
7154   parser->type_definition_forbidden_message = saved_message;
7155   /* If all is well, we might be looking at a declaration.  */
7156   if (!cp_parser_error_occurred (parser))
7157     {
7158       tree decl;
7159       tree asm_specification;
7160       tree attributes;
7161       cp_declarator *declarator;
7162       tree initializer = NULL_TREE;
7163
7164       /* Parse the declarator.  */
7165       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7166                                          /*ctor_dtor_or_conv_p=*/NULL,
7167                                          /*parenthesized_p=*/NULL,
7168                                          /*member_p=*/false);
7169       /* Parse the attributes.  */
7170       attributes = cp_parser_attributes_opt (parser);
7171       /* Parse the asm-specification.  */
7172       asm_specification = cp_parser_asm_specification_opt (parser);
7173       /* If the next token is not an `=', then we might still be
7174          looking at an expression.  For example:
7175
7176            if (A(a).x)
7177
7178          looks like a decl-specifier-seq and a declarator -- but then
7179          there is no `=', so this is an expression.  */
7180       cp_parser_require (parser, CPP_EQ, "%<=%>");
7181       /* If we did see an `=', then we are looking at a declaration
7182          for sure.  */
7183       if (cp_parser_parse_definitely (parser))
7184         {
7185           tree pushed_scope;
7186           bool non_constant_p;
7187
7188           /* Create the declaration.  */
7189           decl = start_decl (declarator, &type_specifiers,
7190                              /*initialized_p=*/true,
7191                              attributes, /*prefix_attributes=*/NULL_TREE,
7192                              &pushed_scope);
7193           /* Parse the assignment-expression.  */
7194           initializer
7195             = cp_parser_constant_expression (parser,
7196                                              /*allow_non_constant_p=*/true,
7197                                              &non_constant_p);
7198           if (!non_constant_p)
7199             initializer = fold_non_dependent_expr (initializer);
7200
7201           /* Process the initializer.  */
7202           cp_finish_decl (decl,
7203                           initializer, !non_constant_p,
7204                           asm_specification,
7205                           LOOKUP_ONLYCONVERTING);
7206
7207           if (pushed_scope)
7208             pop_scope (pushed_scope);
7209
7210           return convert_from_reference (decl);
7211         }
7212     }
7213   /* If we didn't even get past the declarator successfully, we are
7214      definitely not looking at a declaration.  */
7215   else
7216     cp_parser_abort_tentative_parse (parser);
7217
7218   /* Otherwise, we are looking at an expression.  */
7219   return cp_parser_expression (parser, /*cast_p=*/false);
7220 }
7221
7222 /* We check for a ) immediately followed by ; with no whitespacing
7223    between.  This is used to issue a warning for:
7224
7225      while (...);
7226
7227    and:
7228
7229      for (...);
7230
7231    as the semicolon is probably extraneous.
7232
7233    On parse errors, the next token might not be a ), so do nothing in
7234    that case. */
7235
7236 static void
7237 check_empty_body (cp_parser* parser, const char* type)
7238 {
7239   cp_token *token;
7240   cp_token *close_paren;
7241   expanded_location close_loc;
7242   expanded_location semi_loc;
7243   
7244   close_paren = cp_lexer_peek_token (parser->lexer);
7245   if (close_paren->type != CPP_CLOSE_PAREN)
7246     return;
7247
7248   close_loc = expand_location (close_paren->location);
7249   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7250
7251   if (token->type != CPP_SEMICOLON
7252       || (token->flags & PREV_WHITE))
7253     return;
7254
7255   semi_loc =  expand_location (token->location);
7256   if (close_loc.line == semi_loc.line
7257       && close_loc.column+1 == semi_loc.column)
7258     warning (OPT_Wempty_body,
7259              "suggest a space before %<;%> or explicit braces around empty "
7260              "body in %<%s%> statement",
7261              type);
7262 }
7263
7264 /* Parse an iteration-statement.
7265
7266    iteration-statement:
7267      while ( condition ) statement
7268      do statement while ( expression ) ;
7269      for ( for-init-statement condition [opt] ; expression [opt] )
7270        statement
7271
7272    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7273
7274 static tree
7275 cp_parser_iteration_statement (cp_parser* parser)
7276 {
7277   cp_token *token;
7278   enum rid keyword;
7279   tree statement;
7280   unsigned char in_statement;
7281
7282   /* Peek at the next token.  */
7283   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7284   if (!token)
7285     return error_mark_node;
7286
7287   /* Remember whether or not we are already within an iteration
7288      statement.  */
7289   in_statement = parser->in_statement;
7290
7291   /* See what kind of keyword it is.  */
7292   keyword = token->keyword;
7293   switch (keyword)
7294     {
7295     case RID_WHILE:
7296       {
7297         tree condition;
7298
7299         /* Begin the while-statement.  */
7300         statement = begin_while_stmt ();
7301         /* Look for the `('.  */
7302         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7303         /* Parse the condition.  */
7304         condition = cp_parser_condition (parser);
7305         finish_while_stmt_cond (condition, statement);
7306         check_empty_body (parser, "while");
7307         /* Look for the `)'.  */
7308         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7309         /* Parse the dependent statement.  */
7310         parser->in_statement = IN_ITERATION_STMT;
7311         cp_parser_already_scoped_statement (parser);
7312         parser->in_statement = in_statement;
7313         /* We're done with the while-statement.  */
7314         finish_while_stmt (statement);
7315       }
7316       break;
7317
7318     case RID_DO:
7319       {
7320         tree expression;
7321
7322         /* Begin the do-statement.  */
7323         statement = begin_do_stmt ();
7324         /* Parse the body of the do-statement.  */
7325         parser->in_statement = IN_ITERATION_STMT;
7326         cp_parser_implicitly_scoped_statement (parser, NULL);
7327         parser->in_statement = in_statement;
7328         finish_do_body (statement);
7329         /* Look for the `while' keyword.  */
7330         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7331         /* Look for the `('.  */
7332         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7333         /* Parse the expression.  */
7334         expression = cp_parser_expression (parser, /*cast_p=*/false);
7335         /* We're done with the do-statement.  */
7336         finish_do_stmt (expression, statement);
7337         /* Look for the `)'.  */
7338         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7339         /* Look for the `;'.  */
7340         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7341       }
7342       break;
7343
7344     case RID_FOR:
7345       {
7346         tree condition = NULL_TREE;
7347         tree expression = NULL_TREE;
7348
7349         /* Begin the for-statement.  */
7350         statement = begin_for_stmt ();
7351         /* Look for the `('.  */
7352         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7353         /* Parse the initialization.  */
7354         cp_parser_for_init_statement (parser);
7355         finish_for_init_stmt (statement);
7356
7357         /* If there's a condition, process it.  */
7358         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7359           condition = cp_parser_condition (parser);
7360         finish_for_cond (condition, statement);
7361         /* Look for the `;'.  */
7362         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7363
7364         /* If there's an expression, process it.  */
7365         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7366           expression = cp_parser_expression (parser, /*cast_p=*/false);
7367         finish_for_expr (expression, statement);
7368         check_empty_body (parser, "for");
7369         /* Look for the `)'.  */
7370         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7371
7372         /* Parse the body of the for-statement.  */
7373         parser->in_statement = IN_ITERATION_STMT;
7374         cp_parser_already_scoped_statement (parser);
7375         parser->in_statement = in_statement;
7376
7377         /* We're done with the for-statement.  */
7378         finish_for_stmt (statement);
7379       }
7380       break;
7381
7382     default:
7383       cp_parser_error (parser, "expected iteration-statement");
7384       statement = error_mark_node;
7385       break;
7386     }
7387
7388   return statement;
7389 }
7390
7391 /* Parse a for-init-statement.
7392
7393    for-init-statement:
7394      expression-statement
7395      simple-declaration  */
7396
7397 static void
7398 cp_parser_for_init_statement (cp_parser* parser)
7399 {
7400   /* If the next token is a `;', then we have an empty
7401      expression-statement.  Grammatically, this is also a
7402      simple-declaration, but an invalid one, because it does not
7403      declare anything.  Therefore, if we did not handle this case
7404      specially, we would issue an error message about an invalid
7405      declaration.  */
7406   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7407     {
7408       /* We're going to speculatively look for a declaration, falling back
7409          to an expression, if necessary.  */
7410       cp_parser_parse_tentatively (parser);
7411       /* Parse the declaration.  */
7412       cp_parser_simple_declaration (parser,
7413                                     /*function_definition_allowed_p=*/false);
7414       /* If the tentative parse failed, then we shall need to look for an
7415          expression-statement.  */
7416       if (cp_parser_parse_definitely (parser))
7417         return;
7418     }
7419
7420   cp_parser_expression_statement (parser, false);
7421 }
7422
7423 /* Parse a jump-statement.
7424
7425    jump-statement:
7426      break ;
7427      continue ;
7428      return expression [opt] ;
7429      goto identifier ;
7430
7431    GNU extension:
7432
7433    jump-statement:
7434      goto * expression ;
7435
7436    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7437
7438 static tree
7439 cp_parser_jump_statement (cp_parser* parser)
7440 {
7441   tree statement = error_mark_node;
7442   cp_token *token;
7443   enum rid keyword;
7444   unsigned char in_statement;
7445
7446   /* Peek at the next token.  */
7447   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7448   if (!token)
7449     return error_mark_node;
7450
7451   /* See what kind of keyword it is.  */
7452   keyword = token->keyword;
7453   switch (keyword)
7454     {
7455     case RID_BREAK:
7456       in_statement = parser->in_statement & ~IN_IF_STMT;      
7457       switch (in_statement)
7458         {
7459         case 0:
7460           error ("break statement not within loop or switch");
7461           break;
7462         default:
7463           gcc_assert ((in_statement & IN_SWITCH_STMT)
7464                       || in_statement == IN_ITERATION_STMT);
7465           statement = finish_break_stmt ();
7466           break;
7467         case IN_OMP_BLOCK:
7468           error ("invalid exit from OpenMP structured block");
7469           break;
7470         case IN_OMP_FOR:
7471           error ("break statement used with OpenMP for loop");
7472           break;
7473         }
7474       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7475       break;
7476
7477     case RID_CONTINUE:
7478       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7479         {
7480         case 0:
7481           error ("continue statement not within a loop");
7482           break;
7483         case IN_ITERATION_STMT:
7484         case IN_OMP_FOR:
7485           statement = finish_continue_stmt ();
7486           break;
7487         case IN_OMP_BLOCK:
7488           error ("invalid exit from OpenMP structured block");
7489           break;
7490         default:
7491           gcc_unreachable ();
7492         }
7493       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7494       break;
7495
7496     case RID_RETURN:
7497       {
7498         tree expr;
7499
7500         /* If the next token is a `;', then there is no
7501            expression.  */
7502         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7503           expr = cp_parser_expression (parser, /*cast_p=*/false);
7504         else
7505           expr = NULL_TREE;
7506         /* Build the return-statement.  */
7507         statement = finish_return_stmt (expr);
7508         /* Look for the final `;'.  */
7509         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7510       }
7511       break;
7512
7513     case RID_GOTO:
7514       /* Create the goto-statement.  */
7515       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7516         {
7517           /* Issue a warning about this use of a GNU extension.  */
7518           if (pedantic)
7519             pedwarn ("ISO C++ forbids computed gotos");
7520           /* Consume the '*' token.  */
7521           cp_lexer_consume_token (parser->lexer);
7522           /* Parse the dependent expression.  */
7523           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7524         }
7525       else
7526         finish_goto_stmt (cp_parser_identifier (parser));
7527       /* Look for the final `;'.  */
7528       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7529       break;
7530
7531     default:
7532       cp_parser_error (parser, "expected jump-statement");
7533       break;
7534     }
7535
7536   return statement;
7537 }
7538
7539 /* Parse a declaration-statement.
7540
7541    declaration-statement:
7542      block-declaration  */
7543
7544 static void
7545 cp_parser_declaration_statement (cp_parser* parser)
7546 {
7547   void *p;
7548
7549   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7550   p = obstack_alloc (&declarator_obstack, 0);
7551
7552  /* Parse the block-declaration.  */
7553   cp_parser_block_declaration (parser, /*statement_p=*/true);
7554
7555   /* Free any declarators allocated.  */
7556   obstack_free (&declarator_obstack, p);
7557
7558   /* Finish off the statement.  */
7559   finish_stmt ();
7560 }
7561
7562 /* Some dependent statements (like `if (cond) statement'), are
7563    implicitly in their own scope.  In other words, if the statement is
7564    a single statement (as opposed to a compound-statement), it is
7565    none-the-less treated as if it were enclosed in braces.  Any
7566    declarations appearing in the dependent statement are out of scope
7567    after control passes that point.  This function parses a statement,
7568    but ensures that is in its own scope, even if it is not a
7569    compound-statement.
7570
7571    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7572    is a (possibly labeled) if statement which is not enclosed in
7573    braces and has an else clause.  This is used to implement
7574    -Wparentheses.
7575
7576    Returns the new statement.  */
7577
7578 static tree
7579 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7580 {
7581   tree statement;
7582
7583   if (if_p != NULL)
7584     *if_p = false;
7585
7586   /* Mark if () ; with a special NOP_EXPR.  */
7587   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7588     {
7589       cp_lexer_consume_token (parser->lexer);
7590       statement = add_stmt (build_empty_stmt ());
7591     }
7592   /* if a compound is opened, we simply parse the statement directly.  */
7593   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7594     statement = cp_parser_compound_statement (parser, NULL, false);
7595   /* If the token is not a `{', then we must take special action.  */
7596   else
7597     {
7598       /* Create a compound-statement.  */
7599       statement = begin_compound_stmt (0);
7600       /* Parse the dependent-statement.  */
7601       cp_parser_statement (parser, NULL_TREE, false, if_p);
7602       /* Finish the dummy compound-statement.  */
7603       finish_compound_stmt (statement);
7604     }
7605
7606   /* Return the statement.  */
7607   return statement;
7608 }
7609
7610 /* For some dependent statements (like `while (cond) statement'), we
7611    have already created a scope.  Therefore, even if the dependent
7612    statement is a compound-statement, we do not want to create another
7613    scope.  */
7614
7615 static void
7616 cp_parser_already_scoped_statement (cp_parser* parser)
7617 {
7618   /* If the token is a `{', then we must take special action.  */
7619   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7620     cp_parser_statement (parser, NULL_TREE, false, NULL);
7621   else
7622     {
7623       /* Avoid calling cp_parser_compound_statement, so that we
7624          don't create a new scope.  Do everything else by hand.  */
7625       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7626       cp_parser_statement_seq_opt (parser, NULL_TREE);
7627       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7628     }
7629 }
7630
7631 /* Declarations [gram.dcl.dcl] */
7632
7633 /* Parse an optional declaration-sequence.
7634
7635    declaration-seq:
7636      declaration
7637      declaration-seq declaration  */
7638
7639 static void
7640 cp_parser_declaration_seq_opt (cp_parser* parser)
7641 {
7642   while (true)
7643     {
7644       cp_token *token;
7645
7646       token = cp_lexer_peek_token (parser->lexer);
7647
7648       if (token->type == CPP_CLOSE_BRACE
7649           || token->type == CPP_EOF
7650           || token->type == CPP_PRAGMA_EOL)
7651         break;
7652
7653       if (token->type == CPP_SEMICOLON)
7654         {
7655           /* A declaration consisting of a single semicolon is
7656              invalid.  Allow it unless we're being pedantic.  */
7657           cp_lexer_consume_token (parser->lexer);
7658           if (pedantic && !in_system_header)
7659             pedwarn ("extra %<;%>");
7660           continue;
7661         }
7662
7663       /* If we're entering or exiting a region that's implicitly
7664          extern "C", modify the lang context appropriately.  */
7665       if (!parser->implicit_extern_c && token->implicit_extern_c)
7666         {
7667           push_lang_context (lang_name_c);
7668           parser->implicit_extern_c = true;
7669         }
7670       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7671         {
7672           pop_lang_context ();
7673           parser->implicit_extern_c = false;
7674         }
7675
7676       if (token->type == CPP_PRAGMA)
7677         {
7678           /* A top-level declaration can consist solely of a #pragma.
7679              A nested declaration cannot, so this is done here and not
7680              in cp_parser_declaration.  (A #pragma at block scope is
7681              handled in cp_parser_statement.)  */
7682           cp_parser_pragma (parser, pragma_external);
7683           continue;
7684         }
7685
7686       /* Parse the declaration itself.  */
7687       cp_parser_declaration (parser);
7688     }
7689 }
7690
7691 /* Parse a declaration.
7692
7693    declaration:
7694      block-declaration
7695      function-definition
7696      template-declaration
7697      explicit-instantiation
7698      explicit-specialization
7699      linkage-specification
7700      namespace-definition
7701
7702    GNU extension:
7703
7704    declaration:
7705       __extension__ declaration */
7706
7707 static void
7708 cp_parser_declaration (cp_parser* parser)
7709 {
7710   cp_token token1;
7711   cp_token token2;
7712   int saved_pedantic;
7713   void *p;
7714
7715   /* Check for the `__extension__' keyword.  */
7716   if (cp_parser_extension_opt (parser, &saved_pedantic))
7717     {
7718       /* Parse the qualified declaration.  */
7719       cp_parser_declaration (parser);
7720       /* Restore the PEDANTIC flag.  */
7721       pedantic = saved_pedantic;
7722
7723       return;
7724     }
7725
7726   /* Try to figure out what kind of declaration is present.  */
7727   token1 = *cp_lexer_peek_token (parser->lexer);
7728
7729   if (token1.type != CPP_EOF)
7730     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7731   else
7732     {
7733       token2.type = CPP_EOF;
7734       token2.keyword = RID_MAX;
7735     }
7736
7737   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7738   p = obstack_alloc (&declarator_obstack, 0);
7739
7740   /* If the next token is `extern' and the following token is a string
7741      literal, then we have a linkage specification.  */
7742   if (token1.keyword == RID_EXTERN
7743       && cp_parser_is_string_literal (&token2))
7744     cp_parser_linkage_specification (parser);
7745   /* If the next token is `template', then we have either a template
7746      declaration, an explicit instantiation, or an explicit
7747      specialization.  */
7748   else if (token1.keyword == RID_TEMPLATE)
7749     {
7750       /* `template <>' indicates a template specialization.  */
7751       if (token2.type == CPP_LESS
7752           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7753         cp_parser_explicit_specialization (parser);
7754       /* `template <' indicates a template declaration.  */
7755       else if (token2.type == CPP_LESS)
7756         cp_parser_template_declaration (parser, /*member_p=*/false);
7757       /* Anything else must be an explicit instantiation.  */
7758       else
7759         cp_parser_explicit_instantiation (parser);
7760     }
7761   /* If the next token is `export', then we have a template
7762      declaration.  */
7763   else if (token1.keyword == RID_EXPORT)
7764     cp_parser_template_declaration (parser, /*member_p=*/false);
7765   /* If the next token is `extern', 'static' or 'inline' and the one
7766      after that is `template', we have a GNU extended explicit
7767      instantiation directive.  */
7768   else if (cp_parser_allow_gnu_extensions_p (parser)
7769            && (token1.keyword == RID_EXTERN
7770                || token1.keyword == RID_STATIC
7771                || token1.keyword == RID_INLINE)
7772            && token2.keyword == RID_TEMPLATE)
7773     cp_parser_explicit_instantiation (parser);
7774   /* If the next token is `namespace', check for a named or unnamed
7775      namespace definition.  */
7776   else if (token1.keyword == RID_NAMESPACE
7777            && (/* A named namespace definition.  */
7778                (token2.type == CPP_NAME
7779                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7780                     != CPP_EQ))
7781                /* An unnamed namespace definition.  */
7782                || token2.type == CPP_OPEN_BRACE
7783                || token2.keyword == RID_ATTRIBUTE))
7784     cp_parser_namespace_definition (parser);
7785   /* An inline (associated) namespace definition.  */
7786   else if (token1.keyword == RID_INLINE
7787            && token2.keyword == RID_NAMESPACE)
7788     cp_parser_namespace_definition (parser);
7789   /* Objective-C++ declaration/definition.  */
7790   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7791     cp_parser_objc_declaration (parser);
7792   /* We must have either a block declaration or a function
7793      definition.  */
7794   else
7795     /* Try to parse a block-declaration, or a function-definition.  */
7796     cp_parser_block_declaration (parser, /*statement_p=*/false);
7797
7798   /* Free any declarators allocated.  */
7799   obstack_free (&declarator_obstack, p);
7800 }
7801
7802 /* Parse a block-declaration.
7803
7804    block-declaration:
7805      simple-declaration
7806      asm-definition
7807      namespace-alias-definition
7808      using-declaration
7809      using-directive
7810
7811    GNU Extension:
7812
7813    block-declaration:
7814      __extension__ block-declaration
7815
7816    C++0x Extension:
7817
7818    block-declaration:
7819      static_assert-declaration
7820
7821    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7822    part of a declaration-statement.  */
7823
7824 static void
7825 cp_parser_block_declaration (cp_parser *parser,
7826                              bool      statement_p)
7827 {
7828   cp_token *token1;
7829   int saved_pedantic;
7830
7831   /* Check for the `__extension__' keyword.  */
7832   if (cp_parser_extension_opt (parser, &saved_pedantic))
7833     {
7834       /* Parse the qualified declaration.  */
7835       cp_parser_block_declaration (parser, statement_p);
7836       /* Restore the PEDANTIC flag.  */
7837       pedantic = saved_pedantic;
7838
7839       return;
7840     }
7841
7842   /* Peek at the next token to figure out which kind of declaration is
7843      present.  */
7844   token1 = cp_lexer_peek_token (parser->lexer);
7845
7846   /* If the next keyword is `asm', we have an asm-definition.  */
7847   if (token1->keyword == RID_ASM)
7848     {
7849       if (statement_p)
7850         cp_parser_commit_to_tentative_parse (parser);
7851       cp_parser_asm_definition (parser);
7852     }
7853   /* If the next keyword is `namespace', we have a
7854      namespace-alias-definition.  */
7855   else if (token1->keyword == RID_NAMESPACE)
7856     cp_parser_namespace_alias_definition (parser);
7857   /* If the next keyword is `using', we have either a
7858      using-declaration or a using-directive.  */
7859   else if (token1->keyword == RID_USING)
7860     {
7861       cp_token *token2;
7862
7863       if (statement_p)
7864         cp_parser_commit_to_tentative_parse (parser);
7865       /* If the token after `using' is `namespace', then we have a
7866          using-directive.  */
7867       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7868       if (token2->keyword == RID_NAMESPACE)
7869         cp_parser_using_directive (parser);
7870       /* Otherwise, it's a using-declaration.  */
7871       else
7872         cp_parser_using_declaration (parser,
7873                                      /*access_declaration_p=*/false);
7874     }
7875   /* If the next keyword is `__label__' we have a misplaced label
7876      declaration.  */
7877   else if (token1->keyword == RID_LABEL)
7878     {
7879       cp_lexer_consume_token (parser->lexer);
7880       error ("%<__label__%> not at the beginning of a block");
7881       cp_parser_skip_to_end_of_statement (parser);
7882       /* If the next token is now a `;', consume it.  */
7883       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7884         cp_lexer_consume_token (parser->lexer);
7885     }
7886   /* If the next token is `static_assert' we have a static assertion.  */
7887   else if (token1->keyword == RID_STATIC_ASSERT)
7888     cp_parser_static_assert (parser, /*member_p=*/false);
7889   /* Anything else must be a simple-declaration.  */
7890   else
7891     cp_parser_simple_declaration (parser, !statement_p);
7892 }
7893
7894 /* Parse a simple-declaration.
7895
7896    simple-declaration:
7897      decl-specifier-seq [opt] init-declarator-list [opt] ;
7898
7899    init-declarator-list:
7900      init-declarator
7901      init-declarator-list , init-declarator
7902
7903    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7904    function-definition as a simple-declaration.  */
7905
7906 static void
7907 cp_parser_simple_declaration (cp_parser* parser,
7908                               bool function_definition_allowed_p)
7909 {
7910   cp_decl_specifier_seq decl_specifiers;
7911   int declares_class_or_enum;
7912   bool saw_declarator;
7913
7914   /* Defer access checks until we know what is being declared; the
7915      checks for names appearing in the decl-specifier-seq should be
7916      done as if we were in the scope of the thing being declared.  */
7917   push_deferring_access_checks (dk_deferred);
7918
7919   /* Parse the decl-specifier-seq.  We have to keep track of whether
7920      or not the decl-specifier-seq declares a named class or
7921      enumeration type, since that is the only case in which the
7922      init-declarator-list is allowed to be empty.
7923
7924      [dcl.dcl]
7925
7926      In a simple-declaration, the optional init-declarator-list can be
7927      omitted only when declaring a class or enumeration, that is when
7928      the decl-specifier-seq contains either a class-specifier, an
7929      elaborated-type-specifier, or an enum-specifier.  */
7930   cp_parser_decl_specifier_seq (parser,
7931                                 CP_PARSER_FLAGS_OPTIONAL,
7932                                 &decl_specifiers,
7933                                 &declares_class_or_enum);
7934   /* We no longer need to defer access checks.  */
7935   stop_deferring_access_checks ();
7936
7937   /* In a block scope, a valid declaration must always have a
7938      decl-specifier-seq.  By not trying to parse declarators, we can
7939      resolve the declaration/expression ambiguity more quickly.  */
7940   if (!function_definition_allowed_p
7941       && !decl_specifiers.any_specifiers_p)
7942     {
7943       cp_parser_error (parser, "expected declaration");
7944       goto done;
7945     }
7946
7947   /* If the next two tokens are both identifiers, the code is
7948      erroneous. The usual cause of this situation is code like:
7949
7950        T t;
7951
7952      where "T" should name a type -- but does not.  */
7953   if (!decl_specifiers.type
7954       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7955     {
7956       /* If parsing tentatively, we should commit; we really are
7957          looking at a declaration.  */
7958       cp_parser_commit_to_tentative_parse (parser);
7959       /* Give up.  */
7960       goto done;
7961     }
7962
7963   /* If we have seen at least one decl-specifier, and the next token
7964      is not a parenthesis, then we must be looking at a declaration.
7965      (After "int (" we might be looking at a functional cast.)  */
7966   if (decl_specifiers.any_specifiers_p
7967       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7968     cp_parser_commit_to_tentative_parse (parser);
7969
7970   /* Keep going until we hit the `;' at the end of the simple
7971      declaration.  */
7972   saw_declarator = false;
7973   while (cp_lexer_next_token_is_not (parser->lexer,
7974                                      CPP_SEMICOLON))
7975     {
7976       cp_token *token;
7977       bool function_definition_p;
7978       tree decl;
7979
7980       if (saw_declarator)
7981         {
7982           /* If we are processing next declarator, coma is expected */
7983           token = cp_lexer_peek_token (parser->lexer);
7984           gcc_assert (token->type == CPP_COMMA);
7985           cp_lexer_consume_token (parser->lexer);
7986         }
7987       else
7988         saw_declarator = true;
7989
7990       /* Parse the init-declarator.  */
7991       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7992                                         /*checks=*/NULL,
7993                                         function_definition_allowed_p,
7994                                         /*member_p=*/false,
7995                                         declares_class_or_enum,
7996                                         &function_definition_p);
7997       /* If an error occurred while parsing tentatively, exit quickly.
7998          (That usually happens when in the body of a function; each
7999          statement is treated as a declaration-statement until proven
8000          otherwise.)  */
8001       if (cp_parser_error_occurred (parser))
8002         goto done;
8003       /* Handle function definitions specially.  */
8004       if (function_definition_p)
8005         {
8006           /* If the next token is a `,', then we are probably
8007              processing something like:
8008
8009                void f() {}, *p;
8010
8011              which is erroneous.  */
8012           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8013             error ("mixing declarations and function-definitions is forbidden");
8014           /* Otherwise, we're done with the list of declarators.  */
8015           else
8016             {
8017               pop_deferring_access_checks ();
8018               return;
8019             }
8020         }
8021       /* The next token should be either a `,' or a `;'.  */
8022       token = cp_lexer_peek_token (parser->lexer);
8023       /* If it's a `,', there are more declarators to come.  */
8024       if (token->type == CPP_COMMA)
8025         /* will be consumed next time around */;
8026       /* If it's a `;', we are done.  */
8027       else if (token->type == CPP_SEMICOLON)
8028         break;
8029       /* Anything else is an error.  */
8030       else
8031         {
8032           /* If we have already issued an error message we don't need
8033              to issue another one.  */
8034           if (decl != error_mark_node
8035               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8036             cp_parser_error (parser, "expected %<,%> or %<;%>");
8037           /* Skip tokens until we reach the end of the statement.  */
8038           cp_parser_skip_to_end_of_statement (parser);
8039           /* If the next token is now a `;', consume it.  */
8040           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8041             cp_lexer_consume_token (parser->lexer);
8042           goto done;
8043         }
8044       /* After the first time around, a function-definition is not
8045          allowed -- even if it was OK at first.  For example:
8046
8047            int i, f() {}
8048
8049          is not valid.  */
8050       function_definition_allowed_p = false;
8051     }
8052
8053   /* Issue an error message if no declarators are present, and the
8054      decl-specifier-seq does not itself declare a class or
8055      enumeration.  */
8056   if (!saw_declarator)
8057     {
8058       if (cp_parser_declares_only_class_p (parser))
8059         shadow_tag (&decl_specifiers);
8060       /* Perform any deferred access checks.  */
8061       perform_deferred_access_checks ();
8062     }
8063
8064   /* Consume the `;'.  */
8065   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8066
8067  done:
8068   pop_deferring_access_checks ();
8069 }
8070
8071 /* Parse a decl-specifier-seq.
8072
8073    decl-specifier-seq:
8074      decl-specifier-seq [opt] decl-specifier
8075
8076    decl-specifier:
8077      storage-class-specifier
8078      type-specifier
8079      function-specifier
8080      friend
8081      typedef
8082
8083    GNU Extension:
8084
8085    decl-specifier:
8086      attributes
8087
8088    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8089
8090    The parser flags FLAGS is used to control type-specifier parsing.
8091
8092    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8093    flags:
8094
8095      1: one of the decl-specifiers is an elaborated-type-specifier
8096         (i.e., a type declaration)
8097      2: one of the decl-specifiers is an enum-specifier or a
8098         class-specifier (i.e., a type definition)
8099
8100    */
8101
8102 static void
8103 cp_parser_decl_specifier_seq (cp_parser* parser,
8104                               cp_parser_flags flags,
8105                               cp_decl_specifier_seq *decl_specs,
8106                               int* declares_class_or_enum)
8107 {
8108   bool constructor_possible_p = !parser->in_declarator_p;
8109
8110   /* Clear DECL_SPECS.  */
8111   clear_decl_specs (decl_specs);
8112
8113   /* Assume no class or enumeration type is declared.  */
8114   *declares_class_or_enum = 0;
8115
8116   /* Keep reading specifiers until there are no more to read.  */
8117   while (true)
8118     {
8119       bool constructor_p;
8120       bool found_decl_spec;
8121       cp_token *token;
8122
8123       /* Peek at the next token.  */
8124       token = cp_lexer_peek_token (parser->lexer);
8125       /* Handle attributes.  */
8126       if (token->keyword == RID_ATTRIBUTE)
8127         {
8128           /* Parse the attributes.  */
8129           decl_specs->attributes
8130             = chainon (decl_specs->attributes,
8131                        cp_parser_attributes_opt (parser));
8132           continue;
8133         }
8134       /* Assume we will find a decl-specifier keyword.  */
8135       found_decl_spec = true;
8136       /* If the next token is an appropriate keyword, we can simply
8137          add it to the list.  */
8138       switch (token->keyword)
8139         {
8140           /* decl-specifier:
8141                friend  */
8142         case RID_FRIEND:
8143           if (!at_class_scope_p ())
8144             {
8145               error ("%H%<friend%> used outside of class", &token->location);
8146               cp_lexer_purge_token (parser->lexer);
8147             }
8148           else
8149             {
8150               ++decl_specs->specs[(int) ds_friend];
8151               /* Consume the token.  */
8152               cp_lexer_consume_token (parser->lexer);
8153             }
8154           break;
8155
8156           /* function-specifier:
8157                inline
8158                virtual
8159                explicit  */
8160         case RID_INLINE:
8161         case RID_VIRTUAL:
8162         case RID_EXPLICIT:
8163           cp_parser_function_specifier_opt (parser, decl_specs);
8164           break;
8165
8166           /* decl-specifier:
8167                typedef  */
8168         case RID_TYPEDEF:
8169           ++decl_specs->specs[(int) ds_typedef];
8170           /* Consume the token.  */
8171           cp_lexer_consume_token (parser->lexer);
8172           /* A constructor declarator cannot appear in a typedef.  */
8173           constructor_possible_p = false;
8174           /* The "typedef" keyword can only occur in a declaration; we
8175              may as well commit at this point.  */
8176           cp_parser_commit_to_tentative_parse (parser);
8177
8178           if (decl_specs->storage_class != sc_none)
8179             decl_specs->conflicting_specifiers_p = true;
8180           break;
8181
8182           /* storage-class-specifier:
8183                auto
8184                register
8185                static
8186                extern
8187                mutable
8188
8189              GNU Extension:
8190                thread  */
8191         case RID_AUTO:
8192           /* Consume the token.  */
8193           cp_lexer_consume_token (parser->lexer);
8194
8195           if (cxx_dialect == cxx98) 
8196             {
8197               /* Complain about `auto' as a storage specifier, if
8198                  we're complaining about C++0x compatibility.  */
8199               warning 
8200                 (OPT_Wc__0x_compat, 
8201                  "%<auto%> will change meaning in C++0x; please remove it");
8202
8203               /* Set the storage class anyway.  */
8204               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO);
8205             }
8206           else 
8207             /* We do not yet support the use of `auto' as a
8208                type-specifier.  */
8209             error ("C++0x %<auto%> specifier not supported");
8210           break;
8211
8212         case RID_REGISTER:
8213         case RID_STATIC:
8214         case RID_EXTERN:
8215         case RID_MUTABLE:
8216           /* Consume the token.  */
8217           cp_lexer_consume_token (parser->lexer);
8218           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8219           break;
8220         case RID_THREAD:
8221           /* Consume the token.  */
8222           cp_lexer_consume_token (parser->lexer);
8223           ++decl_specs->specs[(int) ds_thread];
8224           break;
8225
8226         default:
8227           /* We did not yet find a decl-specifier yet.  */
8228           found_decl_spec = false;
8229           break;
8230         }
8231
8232       /* Constructors are a special case.  The `S' in `S()' is not a
8233          decl-specifier; it is the beginning of the declarator.  */
8234       constructor_p
8235         = (!found_decl_spec
8236            && constructor_possible_p
8237            && (cp_parser_constructor_declarator_p
8238                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8239
8240       /* If we don't have a DECL_SPEC yet, then we must be looking at
8241          a type-specifier.  */
8242       if (!found_decl_spec && !constructor_p)
8243         {
8244           int decl_spec_declares_class_or_enum;
8245           bool is_cv_qualifier;
8246           tree type_spec;
8247
8248           type_spec
8249             = cp_parser_type_specifier (parser, flags,
8250                                         decl_specs,
8251                                         /*is_declaration=*/true,
8252                                         &decl_spec_declares_class_or_enum,
8253                                         &is_cv_qualifier);
8254
8255           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8256
8257           /* If this type-specifier referenced a user-defined type
8258              (a typedef, class-name, etc.), then we can't allow any
8259              more such type-specifiers henceforth.
8260
8261              [dcl.spec]
8262
8263              The longest sequence of decl-specifiers that could
8264              possibly be a type name is taken as the
8265              decl-specifier-seq of a declaration.  The sequence shall
8266              be self-consistent as described below.
8267
8268              [dcl.type]
8269
8270              As a general rule, at most one type-specifier is allowed
8271              in the complete decl-specifier-seq of a declaration.  The
8272              only exceptions are the following:
8273
8274              -- const or volatile can be combined with any other
8275                 type-specifier.
8276
8277              -- signed or unsigned can be combined with char, long,
8278                 short, or int.
8279
8280              -- ..
8281
8282              Example:
8283
8284                typedef char* Pc;
8285                void g (const int Pc);
8286
8287              Here, Pc is *not* part of the decl-specifier seq; it's
8288              the declarator.  Therefore, once we see a type-specifier
8289              (other than a cv-qualifier), we forbid any additional
8290              user-defined types.  We *do* still allow things like `int
8291              int' to be considered a decl-specifier-seq, and issue the
8292              error message later.  */
8293           if (type_spec && !is_cv_qualifier)
8294             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8295           /* A constructor declarator cannot follow a type-specifier.  */
8296           if (type_spec)
8297             {
8298               constructor_possible_p = false;
8299               found_decl_spec = true;
8300             }
8301         }
8302
8303       /* If we still do not have a DECL_SPEC, then there are no more
8304          decl-specifiers.  */
8305       if (!found_decl_spec)
8306         break;
8307
8308       decl_specs->any_specifiers_p = true;
8309       /* After we see one decl-specifier, further decl-specifiers are
8310          always optional.  */
8311       flags |= CP_PARSER_FLAGS_OPTIONAL;
8312     }
8313
8314   cp_parser_check_decl_spec (decl_specs);
8315
8316   /* Don't allow a friend specifier with a class definition.  */
8317   if (decl_specs->specs[(int) ds_friend] != 0
8318       && (*declares_class_or_enum & 2))
8319     error ("class definition may not be declared a friend");
8320 }
8321
8322 /* Parse an (optional) storage-class-specifier.
8323
8324    storage-class-specifier:
8325      auto
8326      register
8327      static
8328      extern
8329      mutable
8330
8331    GNU Extension:
8332
8333    storage-class-specifier:
8334      thread
8335
8336    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8337
8338 static tree
8339 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8340 {
8341   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8342     {
8343     case RID_AUTO:
8344       if (cxx_dialect != cxx98)
8345         return NULL_TREE;
8346       /* Fall through for C++98.  */
8347
8348     case RID_REGISTER:
8349     case RID_STATIC:
8350     case RID_EXTERN:
8351     case RID_MUTABLE:
8352     case RID_THREAD:
8353       /* Consume the token.  */
8354       return cp_lexer_consume_token (parser->lexer)->u.value;
8355
8356     default:
8357       return NULL_TREE;
8358     }
8359 }
8360
8361 /* Parse an (optional) function-specifier.
8362
8363    function-specifier:
8364      inline
8365      virtual
8366      explicit
8367
8368    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8369    Updates DECL_SPECS, if it is non-NULL.  */
8370
8371 static tree
8372 cp_parser_function_specifier_opt (cp_parser* parser,
8373                                   cp_decl_specifier_seq *decl_specs)
8374 {
8375   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8376     {
8377     case RID_INLINE:
8378       if (decl_specs)
8379         ++decl_specs->specs[(int) ds_inline];
8380       break;
8381
8382     case RID_VIRTUAL:
8383       /* 14.5.2.3 [temp.mem]
8384
8385          A member function template shall not be virtual.  */
8386       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8387         error ("templates may not be %<virtual%>");
8388       else if (decl_specs)
8389         ++decl_specs->specs[(int) ds_virtual];
8390       break;
8391
8392     case RID_EXPLICIT:
8393       if (decl_specs)
8394         ++decl_specs->specs[(int) ds_explicit];
8395       break;
8396
8397     default:
8398       return NULL_TREE;
8399     }
8400
8401   /* Consume the token.  */
8402   return cp_lexer_consume_token (parser->lexer)->u.value;
8403 }
8404
8405 /* Parse a linkage-specification.
8406
8407    linkage-specification:
8408      extern string-literal { declaration-seq [opt] }
8409      extern string-literal declaration  */
8410
8411 static void
8412 cp_parser_linkage_specification (cp_parser* parser)
8413 {
8414   tree linkage;
8415
8416   /* Look for the `extern' keyword.  */
8417   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8418
8419   /* Look for the string-literal.  */
8420   linkage = cp_parser_string_literal (parser, false, false);
8421
8422   /* Transform the literal into an identifier.  If the literal is a
8423      wide-character string, or contains embedded NULs, then we can't
8424      handle it as the user wants.  */
8425   if (strlen (TREE_STRING_POINTER (linkage))
8426       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8427     {
8428       cp_parser_error (parser, "invalid linkage-specification");
8429       /* Assume C++ linkage.  */
8430       linkage = lang_name_cplusplus;
8431     }
8432   else
8433     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8434
8435   /* We're now using the new linkage.  */
8436   push_lang_context (linkage);
8437
8438   /* If the next token is a `{', then we're using the first
8439      production.  */
8440   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8441     {
8442       /* Consume the `{' token.  */
8443       cp_lexer_consume_token (parser->lexer);
8444       /* Parse the declarations.  */
8445       cp_parser_declaration_seq_opt (parser);
8446       /* Look for the closing `}'.  */
8447       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8448     }
8449   /* Otherwise, there's just one declaration.  */
8450   else
8451     {
8452       bool saved_in_unbraced_linkage_specification_p;
8453
8454       saved_in_unbraced_linkage_specification_p
8455         = parser->in_unbraced_linkage_specification_p;
8456       parser->in_unbraced_linkage_specification_p = true;
8457       cp_parser_declaration (parser);
8458       parser->in_unbraced_linkage_specification_p
8459         = saved_in_unbraced_linkage_specification_p;
8460     }
8461
8462   /* We're done with the linkage-specification.  */
8463   pop_lang_context ();
8464 }
8465
8466 /* Parse a static_assert-declaration.
8467
8468    static_assert-declaration:
8469      static_assert ( constant-expression , string-literal ) ; 
8470
8471    If MEMBER_P, this static_assert is a class member.  */
8472
8473 static void 
8474 cp_parser_static_assert(cp_parser *parser, bool member_p)
8475 {
8476   tree condition;
8477   tree message;
8478   cp_token *token;
8479   location_t saved_loc;
8480
8481   /* Peek at the `static_assert' token so we can keep track of exactly
8482      where the static assertion started.  */
8483   token = cp_lexer_peek_token (parser->lexer);
8484   saved_loc = token->location;
8485
8486   /* Look for the `static_assert' keyword.  */
8487   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8488                                   "%<static_assert%>"))
8489     return;
8490
8491   /*  We know we are in a static assertion; commit to any tentative
8492       parse.  */
8493   if (cp_parser_parsing_tentatively (parser))
8494     cp_parser_commit_to_tentative_parse (parser);
8495
8496   /* Parse the `(' starting the static assertion condition.  */
8497   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8498
8499   /* Parse the constant-expression.  */
8500   condition = 
8501     cp_parser_constant_expression (parser,
8502                                    /*allow_non_constant_p=*/false,
8503                                    /*non_constant_p=*/NULL);
8504
8505   /* Parse the separating `,'.  */
8506   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8507
8508   /* Parse the string-literal message.  */
8509   message = cp_parser_string_literal (parser, 
8510                                       /*translate=*/false,
8511                                       /*wide_ok=*/true);
8512
8513   /* A `)' completes the static assertion.  */
8514   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8515     cp_parser_skip_to_closing_parenthesis (parser, 
8516                                            /*recovering=*/true, 
8517                                            /*or_comma=*/false,
8518                                            /*consume_paren=*/true);
8519
8520   /* A semicolon terminates the declaration.  */
8521   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8522
8523   /* Complete the static assertion, which may mean either processing 
8524      the static assert now or saving it for template instantiation.  */
8525   finish_static_assert (condition, message, saved_loc, member_p);
8526 }
8527
8528 /* Parse a `decltype' type. Returns the type. 
8529
8530    simple-type-specifier:
8531      decltype ( expression )  */
8532
8533 static tree
8534 cp_parser_decltype (cp_parser *parser)
8535 {
8536   tree expr;
8537   bool id_expression_or_member_access_p = false;
8538   const char *saved_message;
8539   bool saved_integral_constant_expression_p;
8540   bool saved_non_integral_constant_expression_p;
8541
8542   /* Look for the `decltype' token.  */
8543   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8544     return error_mark_node;
8545
8546   /* Types cannot be defined in a `decltype' expression.  Save away the
8547      old message.  */
8548   saved_message = parser->type_definition_forbidden_message;
8549
8550   /* And create the new one.  */
8551   parser->type_definition_forbidden_message
8552     = "types may not be defined in %<decltype%> expressions";
8553
8554   /* The restrictions on constant-expressions do not apply inside
8555      decltype expressions.  */
8556   saved_integral_constant_expression_p
8557     = parser->integral_constant_expression_p;
8558   saved_non_integral_constant_expression_p
8559     = parser->non_integral_constant_expression_p;
8560   parser->integral_constant_expression_p = false;
8561
8562   /* Do not actually evaluate the expression.  */
8563   ++skip_evaluation;
8564
8565   /* Parse the opening `('.  */
8566   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8567     return error_mark_node;
8568   
8569   /* First, try parsing an id-expression.  */
8570   cp_parser_parse_tentatively (parser);
8571   expr = cp_parser_id_expression (parser,
8572                                   /*template_keyword_p=*/false,
8573                                   /*check_dependency_p=*/true,
8574                                   /*template_p=*/NULL,
8575                                   /*declarator_p=*/false,
8576                                   /*optional_p=*/false);
8577
8578   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8579     {
8580       bool non_integral_constant_expression_p = false;
8581       tree id_expression = expr;
8582       cp_id_kind idk;
8583       const char *error_msg;
8584
8585       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8586         /* Lookup the name we got back from the id-expression.  */
8587         expr = cp_parser_lookup_name (parser, expr,
8588                                       none_type,
8589                                       /*is_template=*/false,
8590                                       /*is_namespace=*/false,
8591                                       /*check_dependency=*/true,
8592                                       /*ambiguous_decls=*/NULL);
8593
8594       if (expr
8595           && expr != error_mark_node
8596           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8597           && TREE_CODE (expr) != TYPE_DECL
8598           && (TREE_CODE (expr) != BIT_NOT_EXPR
8599               || !TYPE_P (TREE_OPERAND (expr, 0)))
8600           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8601         {
8602           /* Complete lookup of the id-expression.  */
8603           expr = (finish_id_expression
8604                   (id_expression, expr, parser->scope, &idk,
8605                    /*integral_constant_expression_p=*/false,
8606                    /*allow_non_integral_constant_expression_p=*/true,
8607                    &non_integral_constant_expression_p,
8608                    /*template_p=*/false,
8609                    /*done=*/true,
8610                    /*address_p=*/false,
8611                    /*template_arg_p=*/false,
8612                    &error_msg));
8613
8614           if (expr == error_mark_node)
8615             /* We found an id-expression, but it was something that we
8616                should not have found. This is an error, not something
8617                we can recover from, so note that we found an
8618                id-expression and we'll recover as gracefully as
8619                possible.  */
8620             id_expression_or_member_access_p = true;
8621         }
8622
8623       if (expr 
8624           && expr != error_mark_node
8625           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8626         /* We have an id-expression.  */
8627         id_expression_or_member_access_p = true;
8628     }
8629
8630   if (!id_expression_or_member_access_p)
8631     {
8632       /* Abort the id-expression parse.  */
8633       cp_parser_abort_tentative_parse (parser);
8634
8635       /* Parsing tentatively, again.  */
8636       cp_parser_parse_tentatively (parser);
8637
8638       /* Parse a class member access.  */
8639       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8640                                            /*cast_p=*/false,
8641                                            /*member_access_only_p=*/true);
8642
8643       if (expr 
8644           && expr != error_mark_node
8645           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8646         /* We have an id-expression.  */
8647         id_expression_or_member_access_p = true;
8648     }
8649
8650   if (id_expression_or_member_access_p)
8651     /* We have parsed the complete id-expression or member access.  */
8652     cp_parser_parse_definitely (parser);
8653   else
8654     {
8655       /* Abort our attempt to parse an id-expression or member access
8656          expression.  */
8657       cp_parser_abort_tentative_parse (parser);
8658
8659       /* Parse a full expression.  */
8660       expr = cp_parser_expression (parser, /*cast_p=*/false);
8661     }
8662
8663   /* Go back to evaluating expressions.  */
8664   --skip_evaluation;
8665
8666   /* Restore the old message and the integral constant expression
8667      flags.  */
8668   parser->type_definition_forbidden_message = saved_message;
8669   parser->integral_constant_expression_p
8670     = saved_integral_constant_expression_p;
8671   parser->non_integral_constant_expression_p
8672     = saved_non_integral_constant_expression_p;
8673
8674   if (expr == error_mark_node)
8675     {
8676       /* Skip everything up to the closing `)'.  */
8677       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8678                                              /*consume_paren=*/true);
8679       return error_mark_node;
8680     }
8681   
8682   /* Parse to the closing `)'.  */
8683   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8684     {
8685       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8686                                              /*consume_paren=*/true);
8687       return error_mark_node;
8688     }
8689
8690   return finish_decltype_type (expr, id_expression_or_member_access_p);
8691 }
8692
8693 /* Special member functions [gram.special] */
8694
8695 /* Parse a conversion-function-id.
8696
8697    conversion-function-id:
8698      operator conversion-type-id
8699
8700    Returns an IDENTIFIER_NODE representing the operator.  */
8701
8702 static tree
8703 cp_parser_conversion_function_id (cp_parser* parser)
8704 {
8705   tree type;
8706   tree saved_scope;
8707   tree saved_qualifying_scope;
8708   tree saved_object_scope;
8709   tree pushed_scope = NULL_TREE;
8710
8711   /* Look for the `operator' token.  */
8712   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8713     return error_mark_node;
8714   /* When we parse the conversion-type-id, the current scope will be
8715      reset.  However, we need that information in able to look up the
8716      conversion function later, so we save it here.  */
8717   saved_scope = parser->scope;
8718   saved_qualifying_scope = parser->qualifying_scope;
8719   saved_object_scope = parser->object_scope;
8720   /* We must enter the scope of the class so that the names of
8721      entities declared within the class are available in the
8722      conversion-type-id.  For example, consider:
8723
8724        struct S {
8725          typedef int I;
8726          operator I();
8727        };
8728
8729        S::operator I() { ... }
8730
8731      In order to see that `I' is a type-name in the definition, we
8732      must be in the scope of `S'.  */
8733   if (saved_scope)
8734     pushed_scope = push_scope (saved_scope);
8735   /* Parse the conversion-type-id.  */
8736   type = cp_parser_conversion_type_id (parser);
8737   /* Leave the scope of the class, if any.  */
8738   if (pushed_scope)
8739     pop_scope (pushed_scope);
8740   /* Restore the saved scope.  */
8741   parser->scope = saved_scope;
8742   parser->qualifying_scope = saved_qualifying_scope;
8743   parser->object_scope = saved_object_scope;
8744   /* If the TYPE is invalid, indicate failure.  */
8745   if (type == error_mark_node)
8746     return error_mark_node;
8747   return mangle_conv_op_name_for_type (type);
8748 }
8749
8750 /* Parse a conversion-type-id:
8751
8752    conversion-type-id:
8753      type-specifier-seq conversion-declarator [opt]
8754
8755    Returns the TYPE specified.  */
8756
8757 static tree
8758 cp_parser_conversion_type_id (cp_parser* parser)
8759 {
8760   tree attributes;
8761   cp_decl_specifier_seq type_specifiers;
8762   cp_declarator *declarator;
8763   tree type_specified;
8764
8765   /* Parse the attributes.  */
8766   attributes = cp_parser_attributes_opt (parser);
8767   /* Parse the type-specifiers.  */
8768   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8769                                 &type_specifiers);
8770   /* If that didn't work, stop.  */
8771   if (type_specifiers.type == error_mark_node)
8772     return error_mark_node;
8773   /* Parse the conversion-declarator.  */
8774   declarator = cp_parser_conversion_declarator_opt (parser);
8775
8776   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8777                                     /*initialized=*/0, &attributes);
8778   if (attributes)
8779     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8780   return type_specified;
8781 }
8782
8783 /* Parse an (optional) conversion-declarator.
8784
8785    conversion-declarator:
8786      ptr-operator conversion-declarator [opt]
8787
8788    */
8789
8790 static cp_declarator *
8791 cp_parser_conversion_declarator_opt (cp_parser* parser)
8792 {
8793   enum tree_code code;
8794   tree class_type;
8795   cp_cv_quals cv_quals;
8796
8797   /* We don't know if there's a ptr-operator next, or not.  */
8798   cp_parser_parse_tentatively (parser);
8799   /* Try the ptr-operator.  */
8800   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8801   /* If it worked, look for more conversion-declarators.  */
8802   if (cp_parser_parse_definitely (parser))
8803     {
8804       cp_declarator *declarator;
8805
8806       /* Parse another optional declarator.  */
8807       declarator = cp_parser_conversion_declarator_opt (parser);
8808
8809       return cp_parser_make_indirect_declarator
8810         (code, class_type, cv_quals, declarator);
8811    }
8812
8813   return NULL;
8814 }
8815
8816 /* Parse an (optional) ctor-initializer.
8817
8818    ctor-initializer:
8819      : mem-initializer-list
8820
8821    Returns TRUE iff the ctor-initializer was actually present.  */
8822
8823 static bool
8824 cp_parser_ctor_initializer_opt (cp_parser* parser)
8825 {
8826   /* If the next token is not a `:', then there is no
8827      ctor-initializer.  */
8828   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8829     {
8830       /* Do default initialization of any bases and members.  */
8831       if (DECL_CONSTRUCTOR_P (current_function_decl))
8832         finish_mem_initializers (NULL_TREE);
8833
8834       return false;
8835     }
8836
8837   /* Consume the `:' token.  */
8838   cp_lexer_consume_token (parser->lexer);
8839   /* And the mem-initializer-list.  */
8840   cp_parser_mem_initializer_list (parser);
8841
8842   return true;
8843 }
8844
8845 /* Parse a mem-initializer-list.
8846
8847    mem-initializer-list:
8848      mem-initializer ... [opt]
8849      mem-initializer ... [opt] , mem-initializer-list  */
8850
8851 static void
8852 cp_parser_mem_initializer_list (cp_parser* parser)
8853 {
8854   tree mem_initializer_list = NULL_TREE;
8855
8856   /* Let the semantic analysis code know that we are starting the
8857      mem-initializer-list.  */
8858   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8859     error ("only constructors take base initializers");
8860
8861   /* Loop through the list.  */
8862   while (true)
8863     {
8864       tree mem_initializer;
8865
8866       /* Parse the mem-initializer.  */
8867       mem_initializer = cp_parser_mem_initializer (parser);
8868       /* If the next token is a `...', we're expanding member initializers. */
8869       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8870         {
8871           /* Consume the `...'. */
8872           cp_lexer_consume_token (parser->lexer);
8873
8874           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8875              can be expanded but members cannot. */
8876           if (mem_initializer != error_mark_node
8877               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8878             {
8879               error ("cannot expand initializer for member %<%D%>", 
8880                      TREE_PURPOSE (mem_initializer));
8881               mem_initializer = error_mark_node;
8882             }
8883
8884           /* Construct the pack expansion type. */
8885           if (mem_initializer != error_mark_node)
8886             mem_initializer = make_pack_expansion (mem_initializer);
8887         }
8888       /* Add it to the list, unless it was erroneous.  */
8889       if (mem_initializer != error_mark_node)
8890         {
8891           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8892           mem_initializer_list = mem_initializer;
8893         }
8894       /* If the next token is not a `,', we're done.  */
8895       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8896         break;
8897       /* Consume the `,' token.  */
8898       cp_lexer_consume_token (parser->lexer);
8899     }
8900
8901   /* Perform semantic analysis.  */
8902   if (DECL_CONSTRUCTOR_P (current_function_decl))
8903     finish_mem_initializers (mem_initializer_list);
8904 }
8905
8906 /* Parse a mem-initializer.
8907
8908    mem-initializer:
8909      mem-initializer-id ( expression-list [opt] )
8910
8911    GNU extension:
8912
8913    mem-initializer:
8914      ( expression-list [opt] )
8915
8916    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8917    class) or FIELD_DECL (for a non-static data member) to initialize;
8918    the TREE_VALUE is the expression-list.  An empty initialization
8919    list is represented by void_list_node.  */
8920
8921 static tree
8922 cp_parser_mem_initializer (cp_parser* parser)
8923 {
8924   tree mem_initializer_id;
8925   tree expression_list;
8926   tree member;
8927
8928   /* Find out what is being initialized.  */
8929   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8930     {
8931       pedwarn ("anachronistic old-style base class initializer");
8932       mem_initializer_id = NULL_TREE;
8933     }
8934   else
8935     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8936   member = expand_member_init (mem_initializer_id);
8937   if (member && !DECL_P (member))
8938     in_base_initializer = 1;
8939
8940   expression_list
8941     = cp_parser_parenthesized_expression_list (parser, false,
8942                                                /*cast_p=*/false,
8943                                                /*allow_expansion_p=*/true,
8944                                                /*non_constant_p=*/NULL);
8945   if (expression_list == error_mark_node)
8946     return error_mark_node;
8947   if (!expression_list)
8948     expression_list = void_type_node;
8949
8950   in_base_initializer = 0;
8951
8952   return member ? build_tree_list (member, expression_list) : error_mark_node;
8953 }
8954
8955 /* Parse a mem-initializer-id.
8956
8957    mem-initializer-id:
8958      :: [opt] nested-name-specifier [opt] class-name
8959      identifier
8960
8961    Returns a TYPE indicating the class to be initializer for the first
8962    production.  Returns an IDENTIFIER_NODE indicating the data member
8963    to be initialized for the second production.  */
8964
8965 static tree
8966 cp_parser_mem_initializer_id (cp_parser* parser)
8967 {
8968   bool global_scope_p;
8969   bool nested_name_specifier_p;
8970   bool template_p = false;
8971   tree id;
8972
8973   /* `typename' is not allowed in this context ([temp.res]).  */
8974   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8975     {
8976       error ("keyword %<typename%> not allowed in this context (a qualified "
8977              "member initializer is implicitly a type)");
8978       cp_lexer_consume_token (parser->lexer);
8979     }
8980   /* Look for the optional `::' operator.  */
8981   global_scope_p
8982     = (cp_parser_global_scope_opt (parser,
8983                                    /*current_scope_valid_p=*/false)
8984        != NULL_TREE);
8985   /* Look for the optional nested-name-specifier.  The simplest way to
8986      implement:
8987
8988        [temp.res]
8989
8990        The keyword `typename' is not permitted in a base-specifier or
8991        mem-initializer; in these contexts a qualified name that
8992        depends on a template-parameter is implicitly assumed to be a
8993        type name.
8994
8995      is to assume that we have seen the `typename' keyword at this
8996      point.  */
8997   nested_name_specifier_p
8998     = (cp_parser_nested_name_specifier_opt (parser,
8999                                             /*typename_keyword_p=*/true,
9000                                             /*check_dependency_p=*/true,
9001                                             /*type_p=*/true,
9002                                             /*is_declaration=*/true)
9003        != NULL_TREE);
9004   if (nested_name_specifier_p)
9005     template_p = cp_parser_optional_template_keyword (parser);
9006   /* If there is a `::' operator or a nested-name-specifier, then we
9007      are definitely looking for a class-name.  */
9008   if (global_scope_p || nested_name_specifier_p)
9009     return cp_parser_class_name (parser,
9010                                  /*typename_keyword_p=*/true,
9011                                  /*template_keyword_p=*/template_p,
9012                                  none_type,
9013                                  /*check_dependency_p=*/true,
9014                                  /*class_head_p=*/false,
9015                                  /*is_declaration=*/true);
9016   /* Otherwise, we could also be looking for an ordinary identifier.  */
9017   cp_parser_parse_tentatively (parser);
9018   /* Try a class-name.  */
9019   id = cp_parser_class_name (parser,
9020                              /*typename_keyword_p=*/true,
9021                              /*template_keyword_p=*/false,
9022                              none_type,
9023                              /*check_dependency_p=*/true,
9024                              /*class_head_p=*/false,
9025                              /*is_declaration=*/true);
9026   /* If we found one, we're done.  */
9027   if (cp_parser_parse_definitely (parser))
9028     return id;
9029   /* Otherwise, look for an ordinary identifier.  */
9030   return cp_parser_identifier (parser);
9031 }
9032
9033 /* Overloading [gram.over] */
9034
9035 /* Parse an operator-function-id.
9036
9037    operator-function-id:
9038      operator operator
9039
9040    Returns an IDENTIFIER_NODE for the operator which is a
9041    human-readable spelling of the identifier, e.g., `operator +'.  */
9042
9043 static tree
9044 cp_parser_operator_function_id (cp_parser* parser)
9045 {
9046   /* Look for the `operator' keyword.  */
9047   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9048     return error_mark_node;
9049   /* And then the name of the operator itself.  */
9050   return cp_parser_operator (parser);
9051 }
9052
9053 /* Parse an operator.
9054
9055    operator:
9056      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9057      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9058      || ++ -- , ->* -> () []
9059
9060    GNU Extensions:
9061
9062    operator:
9063      <? >? <?= >?=
9064
9065    Returns an IDENTIFIER_NODE for the operator which is a
9066    human-readable spelling of the identifier, e.g., `operator +'.  */
9067
9068 static tree
9069 cp_parser_operator (cp_parser* parser)
9070 {
9071   tree id = NULL_TREE;
9072   cp_token *token;
9073
9074   /* Peek at the next token.  */
9075   token = cp_lexer_peek_token (parser->lexer);
9076   /* Figure out which operator we have.  */
9077   switch (token->type)
9078     {
9079     case CPP_KEYWORD:
9080       {
9081         enum tree_code op;
9082
9083         /* The keyword should be either `new' or `delete'.  */
9084         if (token->keyword == RID_NEW)
9085           op = NEW_EXPR;
9086         else if (token->keyword == RID_DELETE)
9087           op = DELETE_EXPR;
9088         else
9089           break;
9090
9091         /* Consume the `new' or `delete' token.  */
9092         cp_lexer_consume_token (parser->lexer);
9093
9094         /* Peek at the next token.  */
9095         token = cp_lexer_peek_token (parser->lexer);
9096         /* If it's a `[' token then this is the array variant of the
9097            operator.  */
9098         if (token->type == CPP_OPEN_SQUARE)
9099           {
9100             /* Consume the `[' token.  */
9101             cp_lexer_consume_token (parser->lexer);
9102             /* Look for the `]' token.  */
9103             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9104             id = ansi_opname (op == NEW_EXPR
9105                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9106           }
9107         /* Otherwise, we have the non-array variant.  */
9108         else
9109           id = ansi_opname (op);
9110
9111         return id;
9112       }
9113
9114     case CPP_PLUS:
9115       id = ansi_opname (PLUS_EXPR);
9116       break;
9117
9118     case CPP_MINUS:
9119       id = ansi_opname (MINUS_EXPR);
9120       break;
9121
9122     case CPP_MULT:
9123       id = ansi_opname (MULT_EXPR);
9124       break;
9125
9126     case CPP_DIV:
9127       id = ansi_opname (TRUNC_DIV_EXPR);
9128       break;
9129
9130     case CPP_MOD:
9131       id = ansi_opname (TRUNC_MOD_EXPR);
9132       break;
9133
9134     case CPP_XOR:
9135       id = ansi_opname (BIT_XOR_EXPR);
9136       break;
9137
9138     case CPP_AND:
9139       id = ansi_opname (BIT_AND_EXPR);
9140       break;
9141
9142     case CPP_OR:
9143       id = ansi_opname (BIT_IOR_EXPR);
9144       break;
9145
9146     case CPP_COMPL:
9147       id = ansi_opname (BIT_NOT_EXPR);
9148       break;
9149
9150     case CPP_NOT:
9151       id = ansi_opname (TRUTH_NOT_EXPR);
9152       break;
9153
9154     case CPP_EQ:
9155       id = ansi_assopname (NOP_EXPR);
9156       break;
9157
9158     case CPP_LESS:
9159       id = ansi_opname (LT_EXPR);
9160       break;
9161
9162     case CPP_GREATER:
9163       id = ansi_opname (GT_EXPR);
9164       break;
9165
9166     case CPP_PLUS_EQ:
9167       id = ansi_assopname (PLUS_EXPR);
9168       break;
9169
9170     case CPP_MINUS_EQ:
9171       id = ansi_assopname (MINUS_EXPR);
9172       break;
9173
9174     case CPP_MULT_EQ:
9175       id = ansi_assopname (MULT_EXPR);
9176       break;
9177
9178     case CPP_DIV_EQ:
9179       id = ansi_assopname (TRUNC_DIV_EXPR);
9180       break;
9181
9182     case CPP_MOD_EQ:
9183       id = ansi_assopname (TRUNC_MOD_EXPR);
9184       break;
9185
9186     case CPP_XOR_EQ:
9187       id = ansi_assopname (BIT_XOR_EXPR);
9188       break;
9189
9190     case CPP_AND_EQ:
9191       id = ansi_assopname (BIT_AND_EXPR);
9192       break;
9193
9194     case CPP_OR_EQ:
9195       id = ansi_assopname (BIT_IOR_EXPR);
9196       break;
9197
9198     case CPP_LSHIFT:
9199       id = ansi_opname (LSHIFT_EXPR);
9200       break;
9201
9202     case CPP_RSHIFT:
9203       id = ansi_opname (RSHIFT_EXPR);
9204       break;
9205
9206     case CPP_LSHIFT_EQ:
9207       id = ansi_assopname (LSHIFT_EXPR);
9208       break;
9209
9210     case CPP_RSHIFT_EQ:
9211       id = ansi_assopname (RSHIFT_EXPR);
9212       break;
9213
9214     case CPP_EQ_EQ:
9215       id = ansi_opname (EQ_EXPR);
9216       break;
9217
9218     case CPP_NOT_EQ:
9219       id = ansi_opname (NE_EXPR);
9220       break;
9221
9222     case CPP_LESS_EQ:
9223       id = ansi_opname (LE_EXPR);
9224       break;
9225
9226     case CPP_GREATER_EQ:
9227       id = ansi_opname (GE_EXPR);
9228       break;
9229
9230     case CPP_AND_AND:
9231       id = ansi_opname (TRUTH_ANDIF_EXPR);
9232       break;
9233
9234     case CPP_OR_OR:
9235       id = ansi_opname (TRUTH_ORIF_EXPR);
9236       break;
9237
9238     case CPP_PLUS_PLUS:
9239       id = ansi_opname (POSTINCREMENT_EXPR);
9240       break;
9241
9242     case CPP_MINUS_MINUS:
9243       id = ansi_opname (PREDECREMENT_EXPR);
9244       break;
9245
9246     case CPP_COMMA:
9247       id = ansi_opname (COMPOUND_EXPR);
9248       break;
9249
9250     case CPP_DEREF_STAR:
9251       id = ansi_opname (MEMBER_REF);
9252       break;
9253
9254     case CPP_DEREF:
9255       id = ansi_opname (COMPONENT_REF);
9256       break;
9257
9258     case CPP_OPEN_PAREN:
9259       /* Consume the `('.  */
9260       cp_lexer_consume_token (parser->lexer);
9261       /* Look for the matching `)'.  */
9262       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9263       return ansi_opname (CALL_EXPR);
9264
9265     case CPP_OPEN_SQUARE:
9266       /* Consume the `['.  */
9267       cp_lexer_consume_token (parser->lexer);
9268       /* Look for the matching `]'.  */
9269       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9270       return ansi_opname (ARRAY_REF);
9271
9272     default:
9273       /* Anything else is an error.  */
9274       break;
9275     }
9276
9277   /* If we have selected an identifier, we need to consume the
9278      operator token.  */
9279   if (id)
9280     cp_lexer_consume_token (parser->lexer);
9281   /* Otherwise, no valid operator name was present.  */
9282   else
9283     {
9284       cp_parser_error (parser, "expected operator");
9285       id = error_mark_node;
9286     }
9287
9288   return id;
9289 }
9290
9291 /* Parse a template-declaration.
9292
9293    template-declaration:
9294      export [opt] template < template-parameter-list > declaration
9295
9296    If MEMBER_P is TRUE, this template-declaration occurs within a
9297    class-specifier.
9298
9299    The grammar rule given by the standard isn't correct.  What
9300    is really meant is:
9301
9302    template-declaration:
9303      export [opt] template-parameter-list-seq
9304        decl-specifier-seq [opt] init-declarator [opt] ;
9305      export [opt] template-parameter-list-seq
9306        function-definition
9307
9308    template-parameter-list-seq:
9309      template-parameter-list-seq [opt]
9310      template < template-parameter-list >  */
9311
9312 static void
9313 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9314 {
9315   /* Check for `export'.  */
9316   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9317     {
9318       /* Consume the `export' token.  */
9319       cp_lexer_consume_token (parser->lexer);
9320       /* Warn that we do not support `export'.  */
9321       warning (0, "keyword %<export%> not implemented, and will be ignored");
9322     }
9323
9324   cp_parser_template_declaration_after_export (parser, member_p);
9325 }
9326
9327 /* Parse a template-parameter-list.
9328
9329    template-parameter-list:
9330      template-parameter
9331      template-parameter-list , template-parameter
9332
9333    Returns a TREE_LIST.  Each node represents a template parameter.
9334    The nodes are connected via their TREE_CHAINs.  */
9335
9336 static tree
9337 cp_parser_template_parameter_list (cp_parser* parser)
9338 {
9339   tree parameter_list = NULL_TREE;
9340
9341   begin_template_parm_list ();
9342   while (true)
9343     {
9344       tree parameter;
9345       bool is_non_type;
9346       bool is_parameter_pack;
9347
9348       /* Parse the template-parameter.  */
9349       parameter = cp_parser_template_parameter (parser, 
9350                                                 &is_non_type,
9351                                                 &is_parameter_pack);
9352       /* Add it to the list.  */
9353       if (parameter != error_mark_node)
9354         parameter_list = process_template_parm (parameter_list,
9355                                                 parameter,
9356                                                 is_non_type,
9357                                                 is_parameter_pack);
9358       else
9359        {
9360          tree err_parm = build_tree_list (parameter, parameter);
9361          TREE_VALUE (err_parm) = error_mark_node;
9362          parameter_list = chainon (parameter_list, err_parm);
9363        }
9364
9365       /* If the next token is not a `,', we're done.  */
9366       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9367         break;
9368       /* Otherwise, consume the `,' token.  */
9369       cp_lexer_consume_token (parser->lexer);
9370     }
9371
9372   return end_template_parm_list (parameter_list);
9373 }
9374
9375 /* Parse a template-parameter.
9376
9377    template-parameter:
9378      type-parameter
9379      parameter-declaration
9380
9381    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9382    the parameter.  The TREE_PURPOSE is the default value, if any.
9383    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9384    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9385    set to true iff this parameter is a parameter pack. */
9386
9387 static tree
9388 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9389                               bool *is_parameter_pack)
9390 {
9391   cp_token *token;
9392   cp_parameter_declarator *parameter_declarator;
9393   cp_declarator *id_declarator;
9394   tree parm;
9395
9396   /* Assume it is a type parameter or a template parameter.  */
9397   *is_non_type = false;
9398   /* Assume it not a parameter pack. */
9399   *is_parameter_pack = false;
9400   /* Peek at the next token.  */
9401   token = cp_lexer_peek_token (parser->lexer);
9402   /* If it is `class' or `template', we have a type-parameter.  */
9403   if (token->keyword == RID_TEMPLATE)
9404     return cp_parser_type_parameter (parser, is_parameter_pack);
9405   /* If it is `class' or `typename' we do not know yet whether it is a
9406      type parameter or a non-type parameter.  Consider:
9407
9408        template <typename T, typename T::X X> ...
9409
9410      or:
9411
9412        template <class C, class D*> ...
9413
9414      Here, the first parameter is a type parameter, and the second is
9415      a non-type parameter.  We can tell by looking at the token after
9416      the identifier -- if it is a `,', `=', or `>' then we have a type
9417      parameter.  */
9418   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9419     {
9420       /* Peek at the token after `class' or `typename'.  */
9421       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9422       /* If it's an ellipsis, we have a template type parameter
9423          pack. */
9424       if (token->type == CPP_ELLIPSIS)
9425         return cp_parser_type_parameter (parser, is_parameter_pack);
9426       /* If it's an identifier, skip it.  */
9427       if (token->type == CPP_NAME)
9428         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9429       /* Now, see if the token looks like the end of a template
9430          parameter.  */
9431       if (token->type == CPP_COMMA
9432           || token->type == CPP_EQ
9433           || token->type == CPP_GREATER)
9434         return cp_parser_type_parameter (parser, is_parameter_pack);
9435     }
9436
9437   /* Otherwise, it is a non-type parameter.
9438
9439      [temp.param]
9440
9441      When parsing a default template-argument for a non-type
9442      template-parameter, the first non-nested `>' is taken as the end
9443      of the template parameter-list rather than a greater-than
9444      operator.  */
9445   *is_non_type = true;
9446   parameter_declarator
9447      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9448                                         /*parenthesized_p=*/NULL);
9449
9450   /* If the parameter declaration is marked as a parameter pack, set
9451      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9452      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9453      grokdeclarator. */
9454   if (parameter_declarator
9455       && parameter_declarator->declarator
9456       && parameter_declarator->declarator->parameter_pack_p)
9457     {
9458       *is_parameter_pack = true;
9459       parameter_declarator->declarator->parameter_pack_p = false;
9460     }
9461
9462   /* If the next token is an ellipsis, and we don't already have it
9463      marked as a parameter pack, then we have a parameter pack (that
9464      has no declarator).  */
9465   if (!*is_parameter_pack
9466       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9467       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9468     {
9469       /* Consume the `...'.  */
9470       cp_lexer_consume_token (parser->lexer);
9471       maybe_warn_variadic_templates ();
9472       
9473       *is_parameter_pack = true;
9474     }
9475   /* We might end up with a pack expansion as the type of the non-type
9476      template parameter, in which case this is a non-type template
9477      parameter pack.  */
9478   else if (parameter_declarator
9479            && parameter_declarator->decl_specifiers.type
9480            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9481     {
9482       *is_parameter_pack = true;
9483       parameter_declarator->decl_specifiers.type = 
9484         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9485     }
9486
9487   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9488     {
9489       /* Parameter packs cannot have default arguments.  However, a
9490          user may try to do so, so we'll parse them and give an
9491          appropriate diagnostic here.  */
9492
9493       /* Consume the `='.  */
9494       cp_lexer_consume_token (parser->lexer);
9495       
9496       /* Find the name of the parameter pack.  */     
9497       id_declarator = parameter_declarator->declarator;
9498       while (id_declarator && id_declarator->kind != cdk_id)
9499         id_declarator = id_declarator->declarator;
9500       
9501       if (id_declarator && id_declarator->kind == cdk_id)
9502         error ("template parameter pack %qD cannot have a default argument",
9503                id_declarator->u.id.unqualified_name);
9504       else
9505         error ("template parameter pack cannot have a default argument");
9506       
9507       /* Parse the default argument, but throw away the result.  */
9508       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9509     }
9510
9511   parm = grokdeclarator (parameter_declarator->declarator,
9512                          &parameter_declarator->decl_specifiers,
9513                          PARM, /*initialized=*/0,
9514                          /*attrlist=*/NULL);
9515   if (parm == error_mark_node)
9516     return error_mark_node;
9517
9518   return build_tree_list (parameter_declarator->default_argument, parm);
9519 }
9520
9521 /* Parse a type-parameter.
9522
9523    type-parameter:
9524      class identifier [opt]
9525      class identifier [opt] = type-id
9526      typename identifier [opt]
9527      typename identifier [opt] = type-id
9528      template < template-parameter-list > class identifier [opt]
9529      template < template-parameter-list > class identifier [opt]
9530        = id-expression
9531
9532    GNU Extension (variadic templates):
9533
9534    type-parameter:
9535      class ... identifier [opt]
9536      typename ... identifier [opt]
9537
9538    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9539    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9540    the declaration of the parameter.
9541
9542    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9543
9544 static tree
9545 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9546 {
9547   cp_token *token;
9548   tree parameter;
9549
9550   /* Look for a keyword to tell us what kind of parameter this is.  */
9551   token = cp_parser_require (parser, CPP_KEYWORD,
9552                              "%<class%>, %<typename%>, or %<template%>");
9553   if (!token)
9554     return error_mark_node;
9555
9556   switch (token->keyword)
9557     {
9558     case RID_CLASS:
9559     case RID_TYPENAME:
9560       {
9561         tree identifier;
9562         tree default_argument;
9563
9564         /* If the next token is an ellipsis, we have a template
9565            argument pack. */
9566         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9567           {
9568             /* Consume the `...' token. */
9569             cp_lexer_consume_token (parser->lexer);
9570             maybe_warn_variadic_templates ();
9571
9572             *is_parameter_pack = true;
9573           }
9574
9575         /* If the next token is an identifier, then it names the
9576            parameter.  */
9577         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9578           identifier = cp_parser_identifier (parser);
9579         else
9580           identifier = NULL_TREE;
9581
9582         /* Create the parameter.  */
9583         parameter = finish_template_type_parm (class_type_node, identifier);
9584
9585         /* If the next token is an `=', we have a default argument.  */
9586         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9587           {
9588             /* Consume the `=' token.  */
9589             cp_lexer_consume_token (parser->lexer);
9590             /* Parse the default-argument.  */
9591             push_deferring_access_checks (dk_no_deferred);
9592             default_argument = cp_parser_type_id (parser);
9593
9594             /* Template parameter packs cannot have default
9595                arguments. */
9596             if (*is_parameter_pack)
9597               {
9598                 if (identifier)
9599                   error ("template parameter pack %qD cannot have a default argument", 
9600                          identifier);
9601                 else
9602                   error ("template parameter packs cannot have default arguments");
9603                 default_argument = NULL_TREE;
9604               }
9605             pop_deferring_access_checks ();
9606           }
9607         else
9608           default_argument = NULL_TREE;
9609
9610         /* Create the combined representation of the parameter and the
9611            default argument.  */
9612         parameter = build_tree_list (default_argument, parameter);
9613       }
9614       break;
9615
9616     case RID_TEMPLATE:
9617       {
9618         tree parameter_list;
9619         tree identifier;
9620         tree default_argument;
9621
9622         /* Look for the `<'.  */
9623         cp_parser_require (parser, CPP_LESS, "%<<%>");
9624         /* Parse the template-parameter-list.  */
9625         parameter_list = cp_parser_template_parameter_list (parser);
9626         /* Look for the `>'.  */
9627         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9628         /* Look for the `class' keyword.  */
9629         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9630         /* If the next token is an ellipsis, we have a template
9631            argument pack. */
9632         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9633           {
9634             /* Consume the `...' token. */
9635             cp_lexer_consume_token (parser->lexer);
9636             maybe_warn_variadic_templates ();
9637
9638             *is_parameter_pack = true;
9639           }
9640         /* If the next token is an `=', then there is a
9641            default-argument.  If the next token is a `>', we are at
9642            the end of the parameter-list.  If the next token is a `,',
9643            then we are at the end of this parameter.  */
9644         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9645             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9646             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9647           {
9648             identifier = cp_parser_identifier (parser);
9649             /* Treat invalid names as if the parameter were nameless.  */
9650             if (identifier == error_mark_node)
9651               identifier = NULL_TREE;
9652           }
9653         else
9654           identifier = NULL_TREE;
9655
9656         /* Create the template parameter.  */
9657         parameter = finish_template_template_parm (class_type_node,
9658                                                    identifier);
9659
9660         /* If the next token is an `=', then there is a
9661            default-argument.  */
9662         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9663           {
9664             bool is_template;
9665
9666             /* Consume the `='.  */
9667             cp_lexer_consume_token (parser->lexer);
9668             /* Parse the id-expression.  */
9669             push_deferring_access_checks (dk_no_deferred);
9670             default_argument
9671               = cp_parser_id_expression (parser,
9672                                          /*template_keyword_p=*/false,
9673                                          /*check_dependency_p=*/true,
9674                                          /*template_p=*/&is_template,
9675                                          /*declarator_p=*/false,
9676                                          /*optional_p=*/false);
9677             if (TREE_CODE (default_argument) == TYPE_DECL)
9678               /* If the id-expression was a template-id that refers to
9679                  a template-class, we already have the declaration here,
9680                  so no further lookup is needed.  */
9681                  ;
9682             else
9683               /* Look up the name.  */
9684               default_argument
9685                 = cp_parser_lookup_name (parser, default_argument,
9686                                          none_type,
9687                                          /*is_template=*/is_template,
9688                                          /*is_namespace=*/false,
9689                                          /*check_dependency=*/true,
9690                                          /*ambiguous_decls=*/NULL);
9691             /* See if the default argument is valid.  */
9692             default_argument
9693               = check_template_template_default_arg (default_argument);
9694
9695             /* Template parameter packs cannot have default
9696                arguments. */
9697             if (*is_parameter_pack)
9698               {
9699                 if (identifier)
9700                   error ("template parameter pack %qD cannot have a default argument", 
9701                          identifier);
9702                 else
9703                   error ("template parameter packs cannot have default arguments");
9704                 default_argument = NULL_TREE;
9705               }
9706             pop_deferring_access_checks ();
9707           }
9708         else
9709           default_argument = NULL_TREE;
9710
9711         /* Create the combined representation of the parameter and the
9712            default argument.  */
9713         parameter = build_tree_list (default_argument, parameter);
9714       }
9715       break;
9716
9717     default:
9718       gcc_unreachable ();
9719       break;
9720     }
9721
9722   return parameter;
9723 }
9724
9725 /* Parse a template-id.
9726
9727    template-id:
9728      template-name < template-argument-list [opt] >
9729
9730    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9731    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9732    returned.  Otherwise, if the template-name names a function, or set
9733    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9734    names a class, returns a TYPE_DECL for the specialization.
9735
9736    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9737    uninstantiated templates.  */
9738
9739 static tree
9740 cp_parser_template_id (cp_parser *parser,
9741                        bool template_keyword_p,
9742                        bool check_dependency_p,
9743                        bool is_declaration)
9744 {
9745   int i;
9746   tree template;
9747   tree arguments;
9748   tree template_id;
9749   cp_token_position start_of_id = 0;
9750   deferred_access_check *chk;
9751   VEC (deferred_access_check,gc) *access_check;
9752   cp_token *next_token, *next_token_2;
9753   bool is_identifier;
9754
9755   /* If the next token corresponds to a template-id, there is no need
9756      to reparse it.  */
9757   next_token = cp_lexer_peek_token (parser->lexer);
9758   if (next_token->type == CPP_TEMPLATE_ID)
9759     {
9760       struct tree_check *check_value;
9761
9762       /* Get the stored value.  */
9763       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9764       /* Perform any access checks that were deferred.  */
9765       access_check = check_value->checks;
9766       if (access_check)
9767         {
9768           for (i = 0 ;
9769                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9770                ++i)
9771             {
9772               perform_or_defer_access_check (chk->binfo,
9773                                              chk->decl,
9774                                              chk->diag_decl);
9775             }
9776         }
9777       /* Return the stored value.  */
9778       return check_value->value;
9779     }
9780
9781   /* Avoid performing name lookup if there is no possibility of
9782      finding a template-id.  */
9783   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9784       || (next_token->type == CPP_NAME
9785           && !cp_parser_nth_token_starts_template_argument_list_p
9786                (parser, 2)))
9787     {
9788       cp_parser_error (parser, "expected template-id");
9789       return error_mark_node;
9790     }
9791
9792   /* Remember where the template-id starts.  */
9793   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9794     start_of_id = cp_lexer_token_position (parser->lexer, false);
9795
9796   push_deferring_access_checks (dk_deferred);
9797
9798   /* Parse the template-name.  */
9799   is_identifier = false;
9800   template = cp_parser_template_name (parser, template_keyword_p,
9801                                       check_dependency_p,
9802                                       is_declaration,
9803                                       &is_identifier);
9804   if (template == error_mark_node || is_identifier)
9805     {
9806       pop_deferring_access_checks ();
9807       return template;
9808     }
9809
9810   /* If we find the sequence `[:' after a template-name, it's probably
9811      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9812      parse correctly the argument list.  */
9813   next_token = cp_lexer_peek_token (parser->lexer);
9814   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9815   if (next_token->type == CPP_OPEN_SQUARE
9816       && next_token->flags & DIGRAPH
9817       && next_token_2->type == CPP_COLON
9818       && !(next_token_2->flags & PREV_WHITE))
9819     {
9820       cp_parser_parse_tentatively (parser);
9821       /* Change `:' into `::'.  */
9822       next_token_2->type = CPP_SCOPE;
9823       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9824          CPP_LESS.  */
9825       cp_lexer_consume_token (parser->lexer);
9826       /* Parse the arguments.  */
9827       arguments = cp_parser_enclosed_template_argument_list (parser);
9828       if (!cp_parser_parse_definitely (parser))
9829         {
9830           /* If we couldn't parse an argument list, then we revert our changes
9831              and return simply an error. Maybe this is not a template-id
9832              after all.  */
9833           next_token_2->type = CPP_COLON;
9834           cp_parser_error (parser, "expected %<<%>");
9835           pop_deferring_access_checks ();
9836           return error_mark_node;
9837         }
9838       /* Otherwise, emit an error about the invalid digraph, but continue
9839          parsing because we got our argument list.  */
9840       permerror ("%<<::%> cannot begin a template-argument list");
9841       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9842               "between %<<%> and %<::%>");
9843       if (!flag_permissive)
9844         {
9845           static bool hint;
9846           if (!hint)
9847             {
9848               inform ("(if you use -fpermissive G++ will accept your code)");
9849               hint = true;
9850             }
9851         }
9852     }
9853   else
9854     {
9855       /* Look for the `<' that starts the template-argument-list.  */
9856       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
9857         {
9858           pop_deferring_access_checks ();
9859           return error_mark_node;
9860         }
9861       /* Parse the arguments.  */
9862       arguments = cp_parser_enclosed_template_argument_list (parser);
9863     }
9864
9865   /* Build a representation of the specialization.  */
9866   if (TREE_CODE (template) == IDENTIFIER_NODE)
9867     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9868   else if (DECL_CLASS_TEMPLATE_P (template)
9869            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9870     {
9871       bool entering_scope;
9872       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9873          template (rather than some instantiation thereof) only if
9874          is not nested within some other construct.  For example, in
9875          "template <typename T> void f(T) { A<T>::", A<T> is just an
9876          instantiation of A.  */
9877       entering_scope = (template_parm_scope_p ()
9878                         && cp_lexer_next_token_is (parser->lexer,
9879                                                    CPP_SCOPE));
9880       template_id
9881         = finish_template_type (template, arguments, entering_scope);
9882     }
9883   else
9884     {
9885       /* If it's not a class-template or a template-template, it should be
9886          a function-template.  */
9887       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9888                    || TREE_CODE (template) == OVERLOAD
9889                    || BASELINK_P (template)));
9890
9891       template_id = lookup_template_function (template, arguments);
9892     }
9893
9894   /* If parsing tentatively, replace the sequence of tokens that makes
9895      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9896      should we re-parse the token stream, we will not have to repeat
9897      the effort required to do the parse, nor will we issue duplicate
9898      error messages about problems during instantiation of the
9899      template.  */
9900   if (start_of_id)
9901     {
9902       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9903
9904       /* Reset the contents of the START_OF_ID token.  */
9905       token->type = CPP_TEMPLATE_ID;
9906       /* Retrieve any deferred checks.  Do not pop this access checks yet
9907          so the memory will not be reclaimed during token replacing below.  */
9908       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9909       token->u.tree_check_value->value = template_id;
9910       token->u.tree_check_value->checks = get_deferred_access_checks ();
9911       token->keyword = RID_MAX;
9912
9913       /* Purge all subsequent tokens.  */
9914       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9915
9916       /* ??? Can we actually assume that, if template_id ==
9917          error_mark_node, we will have issued a diagnostic to the
9918          user, as opposed to simply marking the tentative parse as
9919          failed?  */
9920       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9921         error ("parse error in template argument list");
9922     }
9923
9924   pop_deferring_access_checks ();
9925   return template_id;
9926 }
9927
9928 /* Parse a template-name.
9929
9930    template-name:
9931      identifier
9932
9933    The standard should actually say:
9934
9935    template-name:
9936      identifier
9937      operator-function-id
9938
9939    A defect report has been filed about this issue.
9940
9941    A conversion-function-id cannot be a template name because they cannot
9942    be part of a template-id. In fact, looking at this code:
9943
9944    a.operator K<int>()
9945
9946    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9947    It is impossible to call a templated conversion-function-id with an
9948    explicit argument list, since the only allowed template parameter is
9949    the type to which it is converting.
9950
9951    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9952    `template' keyword, in a construction like:
9953
9954      T::template f<3>()
9955
9956    In that case `f' is taken to be a template-name, even though there
9957    is no way of knowing for sure.
9958
9959    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9960    name refers to a set of overloaded functions, at least one of which
9961    is a template, or an IDENTIFIER_NODE with the name of the template,
9962    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9963    names are looked up inside uninstantiated templates.  */
9964
9965 static tree
9966 cp_parser_template_name (cp_parser* parser,
9967                          bool template_keyword_p,
9968                          bool check_dependency_p,
9969                          bool is_declaration,
9970                          bool *is_identifier)
9971 {
9972   tree identifier;
9973   tree decl;
9974   tree fns;
9975
9976   /* If the next token is `operator', then we have either an
9977      operator-function-id or a conversion-function-id.  */
9978   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9979     {
9980       /* We don't know whether we're looking at an
9981          operator-function-id or a conversion-function-id.  */
9982       cp_parser_parse_tentatively (parser);
9983       /* Try an operator-function-id.  */
9984       identifier = cp_parser_operator_function_id (parser);
9985       /* If that didn't work, try a conversion-function-id.  */
9986       if (!cp_parser_parse_definitely (parser))
9987         {
9988           cp_parser_error (parser, "expected template-name");
9989           return error_mark_node;
9990         }
9991     }
9992   /* Look for the identifier.  */
9993   else
9994     identifier = cp_parser_identifier (parser);
9995
9996   /* If we didn't find an identifier, we don't have a template-id.  */
9997   if (identifier == error_mark_node)
9998     return error_mark_node;
9999
10000   /* If the name immediately followed the `template' keyword, then it
10001      is a template-name.  However, if the next token is not `<', then
10002      we do not treat it as a template-name, since it is not being used
10003      as part of a template-id.  This enables us to handle constructs
10004      like:
10005
10006        template <typename T> struct S { S(); };
10007        template <typename T> S<T>::S();
10008
10009      correctly.  We would treat `S' as a template -- if it were `S<T>'
10010      -- but we do not if there is no `<'.  */
10011
10012   if (processing_template_decl
10013       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10014     {
10015       /* In a declaration, in a dependent context, we pretend that the
10016          "template" keyword was present in order to improve error
10017          recovery.  For example, given:
10018
10019            template <typename T> void f(T::X<int>);
10020
10021          we want to treat "X<int>" as a template-id.  */
10022       if (is_declaration
10023           && !template_keyword_p
10024           && parser->scope && TYPE_P (parser->scope)
10025           && check_dependency_p
10026           && dependent_type_p (parser->scope)
10027           /* Do not do this for dtors (or ctors), since they never
10028              need the template keyword before their name.  */
10029           && !constructor_name_p (identifier, parser->scope))
10030         {
10031           cp_token_position start = 0;
10032
10033           /* Explain what went wrong.  */
10034           error ("non-template %qD used as template", identifier);
10035           inform ("use %<%T::template %D%> to indicate that it is a template",
10036                   parser->scope, identifier);
10037           /* If parsing tentatively, find the location of the "<" token.  */
10038           if (cp_parser_simulate_error (parser))
10039             start = cp_lexer_token_position (parser->lexer, true);
10040           /* Parse the template arguments so that we can issue error
10041              messages about them.  */
10042           cp_lexer_consume_token (parser->lexer);
10043           cp_parser_enclosed_template_argument_list (parser);
10044           /* Skip tokens until we find a good place from which to
10045              continue parsing.  */
10046           cp_parser_skip_to_closing_parenthesis (parser,
10047                                                  /*recovering=*/true,
10048                                                  /*or_comma=*/true,
10049                                                  /*consume_paren=*/false);
10050           /* If parsing tentatively, permanently remove the
10051              template argument list.  That will prevent duplicate
10052              error messages from being issued about the missing
10053              "template" keyword.  */
10054           if (start)
10055             cp_lexer_purge_tokens_after (parser->lexer, start);
10056           if (is_identifier)
10057             *is_identifier = true;
10058           return identifier;
10059         }
10060
10061       /* If the "template" keyword is present, then there is generally
10062          no point in doing name-lookup, so we just return IDENTIFIER.
10063          But, if the qualifying scope is non-dependent then we can
10064          (and must) do name-lookup normally.  */
10065       if (template_keyword_p
10066           && (!parser->scope
10067               || (TYPE_P (parser->scope)
10068                   && dependent_type_p (parser->scope))))
10069         return identifier;
10070     }
10071
10072   /* Look up the name.  */
10073   decl = cp_parser_lookup_name (parser, identifier,
10074                                 none_type,
10075                                 /*is_template=*/false,
10076                                 /*is_namespace=*/false,
10077                                 check_dependency_p,
10078                                 /*ambiguous_decls=*/NULL);
10079   decl = maybe_get_template_decl_from_type_decl (decl);
10080
10081   /* If DECL is a template, then the name was a template-name.  */
10082   if (TREE_CODE (decl) == TEMPLATE_DECL)
10083     ;
10084   else
10085     {
10086       tree fn = NULL_TREE;
10087
10088       /* The standard does not explicitly indicate whether a name that
10089          names a set of overloaded declarations, some of which are
10090          templates, is a template-name.  However, such a name should
10091          be a template-name; otherwise, there is no way to form a
10092          template-id for the overloaded templates.  */
10093       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10094       if (TREE_CODE (fns) == OVERLOAD)
10095         for (fn = fns; fn; fn = OVL_NEXT (fn))
10096           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10097             break;
10098
10099       if (!fn)
10100         {
10101           /* The name does not name a template.  */
10102           cp_parser_error (parser, "expected template-name");
10103           return error_mark_node;
10104         }
10105     }
10106
10107   /* If DECL is dependent, and refers to a function, then just return
10108      its name; we will look it up again during template instantiation.  */
10109   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10110     {
10111       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10112       if (TYPE_P (scope) && dependent_type_p (scope))
10113         return identifier;
10114     }
10115
10116   return decl;
10117 }
10118
10119 /* Parse a template-argument-list.
10120
10121    template-argument-list:
10122      template-argument ... [opt]
10123      template-argument-list , template-argument ... [opt]
10124
10125    Returns a TREE_VEC containing the arguments.  */
10126
10127 static tree
10128 cp_parser_template_argument_list (cp_parser* parser)
10129 {
10130   tree fixed_args[10];
10131   unsigned n_args = 0;
10132   unsigned alloced = 10;
10133   tree *arg_ary = fixed_args;
10134   tree vec;
10135   bool saved_in_template_argument_list_p;
10136   bool saved_ice_p;
10137   bool saved_non_ice_p;
10138
10139   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10140   parser->in_template_argument_list_p = true;
10141   /* Even if the template-id appears in an integral
10142      constant-expression, the contents of the argument list do
10143      not.  */
10144   saved_ice_p = parser->integral_constant_expression_p;
10145   parser->integral_constant_expression_p = false;
10146   saved_non_ice_p = parser->non_integral_constant_expression_p;
10147   parser->non_integral_constant_expression_p = false;
10148   /* Parse the arguments.  */
10149   do
10150     {
10151       tree argument;
10152
10153       if (n_args)
10154         /* Consume the comma.  */
10155         cp_lexer_consume_token (parser->lexer);
10156
10157       /* Parse the template-argument.  */
10158       argument = cp_parser_template_argument (parser);
10159
10160       /* If the next token is an ellipsis, we're expanding a template
10161          argument pack. */
10162       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10163         {
10164           /* Consume the `...' token. */
10165           cp_lexer_consume_token (parser->lexer);
10166
10167           /* Make the argument into a TYPE_PACK_EXPANSION or
10168              EXPR_PACK_EXPANSION. */
10169           argument = make_pack_expansion (argument);
10170         }
10171
10172       if (n_args == alloced)
10173         {
10174           alloced *= 2;
10175
10176           if (arg_ary == fixed_args)
10177             {
10178               arg_ary = XNEWVEC (tree, alloced);
10179               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10180             }
10181           else
10182             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10183         }
10184       arg_ary[n_args++] = argument;
10185     }
10186   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10187
10188   vec = make_tree_vec (n_args);
10189
10190   while (n_args--)
10191     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10192
10193   if (arg_ary != fixed_args)
10194     free (arg_ary);
10195   parser->non_integral_constant_expression_p = saved_non_ice_p;
10196   parser->integral_constant_expression_p = saved_ice_p;
10197   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10198   return vec;
10199 }
10200
10201 /* Parse a template-argument.
10202
10203    template-argument:
10204      assignment-expression
10205      type-id
10206      id-expression
10207
10208    The representation is that of an assignment-expression, type-id, or
10209    id-expression -- except that the qualified id-expression is
10210    evaluated, so that the value returned is either a DECL or an
10211    OVERLOAD.
10212
10213    Although the standard says "assignment-expression", it forbids
10214    throw-expressions or assignments in the template argument.
10215    Therefore, we use "conditional-expression" instead.  */
10216
10217 static tree
10218 cp_parser_template_argument (cp_parser* parser)
10219 {
10220   tree argument;
10221   bool template_p;
10222   bool address_p;
10223   bool maybe_type_id = false;
10224   cp_token *token;
10225   cp_id_kind idk;
10226
10227   /* There's really no way to know what we're looking at, so we just
10228      try each alternative in order.
10229
10230        [temp.arg]
10231
10232        In a template-argument, an ambiguity between a type-id and an
10233        expression is resolved to a type-id, regardless of the form of
10234        the corresponding template-parameter.
10235
10236      Therefore, we try a type-id first.  */
10237   cp_parser_parse_tentatively (parser);
10238   argument = cp_parser_type_id (parser);
10239   /* If there was no error parsing the type-id but the next token is a '>>',
10240      we probably found a typo for '> >'. But there are type-id which are
10241      also valid expressions. For instance:
10242
10243      struct X { int operator >> (int); };
10244      template <int V> struct Foo {};
10245      Foo<X () >> 5> r;
10246
10247      Here 'X()' is a valid type-id of a function type, but the user just
10248      wanted to write the expression "X() >> 5". Thus, we remember that we
10249      found a valid type-id, but we still try to parse the argument as an
10250      expression to see what happens.  */
10251   if (!cp_parser_error_occurred (parser)
10252       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10253     {
10254       maybe_type_id = true;
10255       cp_parser_abort_tentative_parse (parser);
10256     }
10257   else
10258     {
10259       /* If the next token isn't a `,' or a `>', then this argument wasn't
10260       really finished. This means that the argument is not a valid
10261       type-id.  */
10262       if (!cp_parser_next_token_ends_template_argument_p (parser))
10263         cp_parser_error (parser, "expected template-argument");
10264       /* If that worked, we're done.  */
10265       if (cp_parser_parse_definitely (parser))
10266         return argument;
10267     }
10268   /* We're still not sure what the argument will be.  */
10269   cp_parser_parse_tentatively (parser);
10270   /* Try a template.  */
10271   argument = cp_parser_id_expression (parser,
10272                                       /*template_keyword_p=*/false,
10273                                       /*check_dependency_p=*/true,
10274                                       &template_p,
10275                                       /*declarator_p=*/false,
10276                                       /*optional_p=*/false);
10277   /* If the next token isn't a `,' or a `>', then this argument wasn't
10278      really finished.  */
10279   if (!cp_parser_next_token_ends_template_argument_p (parser))
10280     cp_parser_error (parser, "expected template-argument");
10281   if (!cp_parser_error_occurred (parser))
10282     {
10283       /* Figure out what is being referred to.  If the id-expression
10284          was for a class template specialization, then we will have a
10285          TYPE_DECL at this point.  There is no need to do name lookup
10286          at this point in that case.  */
10287       if (TREE_CODE (argument) != TYPE_DECL)
10288         argument = cp_parser_lookup_name (parser, argument,
10289                                           none_type,
10290                                           /*is_template=*/template_p,
10291                                           /*is_namespace=*/false,
10292                                           /*check_dependency=*/true,
10293                                           /*ambiguous_decls=*/NULL);
10294       if (TREE_CODE (argument) != TEMPLATE_DECL
10295           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10296         cp_parser_error (parser, "expected template-name");
10297     }
10298   if (cp_parser_parse_definitely (parser))
10299     return argument;
10300   /* It must be a non-type argument.  There permitted cases are given
10301      in [temp.arg.nontype]:
10302
10303      -- an integral constant-expression of integral or enumeration
10304         type; or
10305
10306      -- the name of a non-type template-parameter; or
10307
10308      -- the name of an object or function with external linkage...
10309
10310      -- the address of an object or function with external linkage...
10311
10312      -- a pointer to member...  */
10313   /* Look for a non-type template parameter.  */
10314   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10315     {
10316       cp_parser_parse_tentatively (parser);
10317       argument = cp_parser_primary_expression (parser,
10318                                                /*adress_p=*/false,
10319                                                /*cast_p=*/false,
10320                                                /*template_arg_p=*/true,
10321                                                &idk);
10322       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10323           || !cp_parser_next_token_ends_template_argument_p (parser))
10324         cp_parser_simulate_error (parser);
10325       if (cp_parser_parse_definitely (parser))
10326         return argument;
10327     }
10328
10329   /* If the next token is "&", the argument must be the address of an
10330      object or function with external linkage.  */
10331   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10332   if (address_p)
10333     cp_lexer_consume_token (parser->lexer);
10334   /* See if we might have an id-expression.  */
10335   token = cp_lexer_peek_token (parser->lexer);
10336   if (token->type == CPP_NAME
10337       || token->keyword == RID_OPERATOR
10338       || token->type == CPP_SCOPE
10339       || token->type == CPP_TEMPLATE_ID
10340       || token->type == CPP_NESTED_NAME_SPECIFIER)
10341     {
10342       cp_parser_parse_tentatively (parser);
10343       argument = cp_parser_primary_expression (parser,
10344                                                address_p,
10345                                                /*cast_p=*/false,
10346                                                /*template_arg_p=*/true,
10347                                                &idk);
10348       if (cp_parser_error_occurred (parser)
10349           || !cp_parser_next_token_ends_template_argument_p (parser))
10350         cp_parser_abort_tentative_parse (parser);
10351       else
10352         {
10353           if (TREE_CODE (argument) == INDIRECT_REF)
10354             {
10355               gcc_assert (REFERENCE_REF_P (argument));
10356               argument = TREE_OPERAND (argument, 0);
10357             }
10358
10359           if (TREE_CODE (argument) == VAR_DECL)
10360             {
10361               /* A variable without external linkage might still be a
10362                  valid constant-expression, so no error is issued here
10363                  if the external-linkage check fails.  */
10364               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10365                 cp_parser_simulate_error (parser);
10366             }
10367           else if (is_overloaded_fn (argument))
10368             /* All overloaded functions are allowed; if the external
10369                linkage test does not pass, an error will be issued
10370                later.  */
10371             ;
10372           else if (address_p
10373                    && (TREE_CODE (argument) == OFFSET_REF
10374                        || TREE_CODE (argument) == SCOPE_REF))
10375             /* A pointer-to-member.  */
10376             ;
10377           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10378             ;
10379           else
10380             cp_parser_simulate_error (parser);
10381
10382           if (cp_parser_parse_definitely (parser))
10383             {
10384               if (address_p)
10385                 argument = build_x_unary_op (ADDR_EXPR, argument,
10386                                              tf_warning_or_error);
10387               return argument;
10388             }
10389         }
10390     }
10391   /* If the argument started with "&", there are no other valid
10392      alternatives at this point.  */
10393   if (address_p)
10394     {
10395       cp_parser_error (parser, "invalid non-type template argument");
10396       return error_mark_node;
10397     }
10398
10399   /* If the argument wasn't successfully parsed as a type-id followed
10400      by '>>', the argument can only be a constant expression now.
10401      Otherwise, we try parsing the constant-expression tentatively,
10402      because the argument could really be a type-id.  */
10403   if (maybe_type_id)
10404     cp_parser_parse_tentatively (parser);
10405   argument = cp_parser_constant_expression (parser,
10406                                             /*allow_non_constant_p=*/false,
10407                                             /*non_constant_p=*/NULL);
10408   argument = fold_non_dependent_expr (argument);
10409   if (!maybe_type_id)
10410     return argument;
10411   if (!cp_parser_next_token_ends_template_argument_p (parser))
10412     cp_parser_error (parser, "expected template-argument");
10413   if (cp_parser_parse_definitely (parser))
10414     return argument;
10415   /* We did our best to parse the argument as a non type-id, but that
10416      was the only alternative that matched (albeit with a '>' after
10417      it). We can assume it's just a typo from the user, and a
10418      diagnostic will then be issued.  */
10419   return cp_parser_type_id (parser);
10420 }
10421
10422 /* Parse an explicit-instantiation.
10423
10424    explicit-instantiation:
10425      template declaration
10426
10427    Although the standard says `declaration', what it really means is:
10428
10429    explicit-instantiation:
10430      template decl-specifier-seq [opt] declarator [opt] ;
10431
10432    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10433    supposed to be allowed.  A defect report has been filed about this
10434    issue.
10435
10436    GNU Extension:
10437
10438    explicit-instantiation:
10439      storage-class-specifier template
10440        decl-specifier-seq [opt] declarator [opt] ;
10441      function-specifier template
10442        decl-specifier-seq [opt] declarator [opt] ;  */
10443
10444 static void
10445 cp_parser_explicit_instantiation (cp_parser* parser)
10446 {
10447   int declares_class_or_enum;
10448   cp_decl_specifier_seq decl_specifiers;
10449   tree extension_specifier = NULL_TREE;
10450
10451   /* Look for an (optional) storage-class-specifier or
10452      function-specifier.  */
10453   if (cp_parser_allow_gnu_extensions_p (parser))
10454     {
10455       extension_specifier
10456         = cp_parser_storage_class_specifier_opt (parser);
10457       if (!extension_specifier)
10458         extension_specifier
10459           = cp_parser_function_specifier_opt (parser,
10460                                               /*decl_specs=*/NULL);
10461     }
10462
10463   /* Look for the `template' keyword.  */
10464   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10465   /* Let the front end know that we are processing an explicit
10466      instantiation.  */
10467   begin_explicit_instantiation ();
10468   /* [temp.explicit] says that we are supposed to ignore access
10469      control while processing explicit instantiation directives.  */
10470   push_deferring_access_checks (dk_no_check);
10471   /* Parse a decl-specifier-seq.  */
10472   cp_parser_decl_specifier_seq (parser,
10473                                 CP_PARSER_FLAGS_OPTIONAL,
10474                                 &decl_specifiers,
10475                                 &declares_class_or_enum);
10476   /* If there was exactly one decl-specifier, and it declared a class,
10477      and there's no declarator, then we have an explicit type
10478      instantiation.  */
10479   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10480     {
10481       tree type;
10482
10483       type = check_tag_decl (&decl_specifiers);
10484       /* Turn access control back on for names used during
10485          template instantiation.  */
10486       pop_deferring_access_checks ();
10487       if (type)
10488         do_type_instantiation (type, extension_specifier,
10489                                /*complain=*/tf_error);
10490     }
10491   else
10492     {
10493       cp_declarator *declarator;
10494       tree decl;
10495
10496       /* Parse the declarator.  */
10497       declarator
10498         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10499                                 /*ctor_dtor_or_conv_p=*/NULL,
10500                                 /*parenthesized_p=*/NULL,
10501                                 /*member_p=*/false);
10502       if (declares_class_or_enum & 2)
10503         cp_parser_check_for_definition_in_return_type (declarator,
10504                                                        decl_specifiers.type);
10505       if (declarator != cp_error_declarator)
10506         {
10507           decl = grokdeclarator (declarator, &decl_specifiers,
10508                                  NORMAL, 0, &decl_specifiers.attributes);
10509           /* Turn access control back on for names used during
10510              template instantiation.  */
10511           pop_deferring_access_checks ();
10512           /* Do the explicit instantiation.  */
10513           do_decl_instantiation (decl, extension_specifier);
10514         }
10515       else
10516         {
10517           pop_deferring_access_checks ();
10518           /* Skip the body of the explicit instantiation.  */
10519           cp_parser_skip_to_end_of_statement (parser);
10520         }
10521     }
10522   /* We're done with the instantiation.  */
10523   end_explicit_instantiation ();
10524
10525   cp_parser_consume_semicolon_at_end_of_statement (parser);
10526 }
10527
10528 /* Parse an explicit-specialization.
10529
10530    explicit-specialization:
10531      template < > declaration
10532
10533    Although the standard says `declaration', what it really means is:
10534
10535    explicit-specialization:
10536      template <> decl-specifier [opt] init-declarator [opt] ;
10537      template <> function-definition
10538      template <> explicit-specialization
10539      template <> template-declaration  */
10540
10541 static void
10542 cp_parser_explicit_specialization (cp_parser* parser)
10543 {
10544   bool need_lang_pop;
10545   /* Look for the `template' keyword.  */
10546   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10547   /* Look for the `<'.  */
10548   cp_parser_require (parser, CPP_LESS, "%<<%>");
10549   /* Look for the `>'.  */
10550   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10551   /* We have processed another parameter list.  */
10552   ++parser->num_template_parameter_lists;
10553   /* [temp]
10554
10555      A template ... explicit specialization ... shall not have C
10556      linkage.  */
10557   if (current_lang_name == lang_name_c)
10558     {
10559       error ("template specialization with C linkage");
10560       /* Give it C++ linkage to avoid confusing other parts of the
10561          front end.  */
10562       push_lang_context (lang_name_cplusplus);
10563       need_lang_pop = true;
10564     }
10565   else
10566     need_lang_pop = false;
10567   /* Let the front end know that we are beginning a specialization.  */
10568   if (!begin_specialization ())
10569     {
10570       end_specialization ();
10571       cp_parser_skip_to_end_of_block_or_statement (parser);
10572       return;
10573     }
10574
10575   /* If the next keyword is `template', we need to figure out whether
10576      or not we're looking a template-declaration.  */
10577   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10578     {
10579       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10580           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10581         cp_parser_template_declaration_after_export (parser,
10582                                                      /*member_p=*/false);
10583       else
10584         cp_parser_explicit_specialization (parser);
10585     }
10586   else
10587     /* Parse the dependent declaration.  */
10588     cp_parser_single_declaration (parser,
10589                                   /*checks=*/NULL,
10590                                   /*member_p=*/false,
10591                                   /*explicit_specialization_p=*/true,
10592                                   /*friend_p=*/NULL);
10593   /* We're done with the specialization.  */
10594   end_specialization ();
10595   /* For the erroneous case of a template with C linkage, we pushed an
10596      implicit C++ linkage scope; exit that scope now.  */
10597   if (need_lang_pop)
10598     pop_lang_context ();
10599   /* We're done with this parameter list.  */
10600   --parser->num_template_parameter_lists;
10601 }
10602
10603 /* Parse a type-specifier.
10604
10605    type-specifier:
10606      simple-type-specifier
10607      class-specifier
10608      enum-specifier
10609      elaborated-type-specifier
10610      cv-qualifier
10611
10612    GNU Extension:
10613
10614    type-specifier:
10615      __complex__
10616
10617    Returns a representation of the type-specifier.  For a
10618    class-specifier, enum-specifier, or elaborated-type-specifier, a
10619    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10620
10621    The parser flags FLAGS is used to control type-specifier parsing.
10622
10623    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10624    in a decl-specifier-seq.
10625
10626    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10627    class-specifier, enum-specifier, or elaborated-type-specifier, then
10628    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10629    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10630    zero.
10631
10632    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10633    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10634    is set to FALSE.  */
10635
10636 static tree
10637 cp_parser_type_specifier (cp_parser* parser,
10638                           cp_parser_flags flags,
10639                           cp_decl_specifier_seq *decl_specs,
10640                           bool is_declaration,
10641                           int* declares_class_or_enum,
10642                           bool* is_cv_qualifier)
10643 {
10644   tree type_spec = NULL_TREE;
10645   cp_token *token;
10646   enum rid keyword;
10647   cp_decl_spec ds = ds_last;
10648
10649   /* Assume this type-specifier does not declare a new type.  */
10650   if (declares_class_or_enum)
10651     *declares_class_or_enum = 0;
10652   /* And that it does not specify a cv-qualifier.  */
10653   if (is_cv_qualifier)
10654     *is_cv_qualifier = false;
10655   /* Peek at the next token.  */
10656   token = cp_lexer_peek_token (parser->lexer);
10657
10658   /* If we're looking at a keyword, we can use that to guide the
10659      production we choose.  */
10660   keyword = token->keyword;
10661   switch (keyword)
10662     {
10663     case RID_ENUM:
10664       /* Look for the enum-specifier.  */
10665       type_spec = cp_parser_enum_specifier (parser);
10666       /* If that worked, we're done.  */
10667       if (type_spec)
10668         {
10669           if (declares_class_or_enum)
10670             *declares_class_or_enum = 2;
10671           if (decl_specs)
10672             cp_parser_set_decl_spec_type (decl_specs,
10673                                           type_spec,
10674                                           /*user_defined_p=*/true);
10675           return type_spec;
10676         }
10677       else
10678         goto elaborated_type_specifier;
10679
10680       /* Any of these indicate either a class-specifier, or an
10681          elaborated-type-specifier.  */
10682     case RID_CLASS:
10683     case RID_STRUCT:
10684     case RID_UNION:
10685       /* Parse tentatively so that we can back up if we don't find a
10686          class-specifier.  */
10687       cp_parser_parse_tentatively (parser);
10688       /* Look for the class-specifier.  */
10689       type_spec = cp_parser_class_specifier (parser);
10690       /* If that worked, we're done.  */
10691       if (cp_parser_parse_definitely (parser))
10692         {
10693           if (declares_class_or_enum)
10694             *declares_class_or_enum = 2;
10695           if (decl_specs)
10696             cp_parser_set_decl_spec_type (decl_specs,
10697                                           type_spec,
10698                                           /*user_defined_p=*/true);
10699           return type_spec;
10700         }
10701
10702       /* Fall through.  */
10703     elaborated_type_specifier:
10704       /* We're declaring (not defining) a class or enum.  */
10705       if (declares_class_or_enum)
10706         *declares_class_or_enum = 1;
10707
10708       /* Fall through.  */
10709     case RID_TYPENAME:
10710       /* Look for an elaborated-type-specifier.  */
10711       type_spec
10712         = (cp_parser_elaborated_type_specifier
10713            (parser,
10714             decl_specs && decl_specs->specs[(int) ds_friend],
10715             is_declaration));
10716       if (decl_specs)
10717         cp_parser_set_decl_spec_type (decl_specs,
10718                                       type_spec,
10719                                       /*user_defined_p=*/true);
10720       return type_spec;
10721
10722     case RID_CONST:
10723       ds = ds_const;
10724       if (is_cv_qualifier)
10725         *is_cv_qualifier = true;
10726       break;
10727
10728     case RID_VOLATILE:
10729       ds = ds_volatile;
10730       if (is_cv_qualifier)
10731         *is_cv_qualifier = true;
10732       break;
10733
10734     case RID_RESTRICT:
10735       ds = ds_restrict;
10736       if (is_cv_qualifier)
10737         *is_cv_qualifier = true;
10738       break;
10739
10740     case RID_COMPLEX:
10741       /* The `__complex__' keyword is a GNU extension.  */
10742       ds = ds_complex;
10743       break;
10744
10745     default:
10746       break;
10747     }
10748
10749   /* Handle simple keywords.  */
10750   if (ds != ds_last)
10751     {
10752       if (decl_specs)
10753         {
10754           ++decl_specs->specs[(int)ds];
10755           decl_specs->any_specifiers_p = true;
10756         }
10757       return cp_lexer_consume_token (parser->lexer)->u.value;
10758     }
10759
10760   /* If we do not already have a type-specifier, assume we are looking
10761      at a simple-type-specifier.  */
10762   type_spec = cp_parser_simple_type_specifier (parser,
10763                                                decl_specs,
10764                                                flags);
10765
10766   /* If we didn't find a type-specifier, and a type-specifier was not
10767      optional in this context, issue an error message.  */
10768   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10769     {
10770       cp_parser_error (parser, "expected type specifier");
10771       return error_mark_node;
10772     }
10773
10774   return type_spec;
10775 }
10776
10777 /* Parse a simple-type-specifier.
10778
10779    simple-type-specifier:
10780      :: [opt] nested-name-specifier [opt] type-name
10781      :: [opt] nested-name-specifier template template-id
10782      char
10783      wchar_t
10784      bool
10785      short
10786      int
10787      long
10788      signed
10789      unsigned
10790      float
10791      double
10792      void
10793
10794    C++0x Extension:
10795
10796    simple-type-specifier:
10797      auto
10798      decltype ( expression )   
10799      char16_t
10800      char32_t
10801
10802    GNU Extension:
10803
10804    simple-type-specifier:
10805      __typeof__ unary-expression
10806      __typeof__ ( type-id )
10807
10808    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10809    appropriately updated.  */
10810
10811 static tree
10812 cp_parser_simple_type_specifier (cp_parser* parser,
10813                                  cp_decl_specifier_seq *decl_specs,
10814                                  cp_parser_flags flags)
10815 {
10816   tree type = NULL_TREE;
10817   cp_token *token;
10818
10819   /* Peek at the next token.  */
10820   token = cp_lexer_peek_token (parser->lexer);
10821
10822   /* If we're looking at a keyword, things are easy.  */
10823   switch (token->keyword)
10824     {
10825     case RID_CHAR:
10826       if (decl_specs)
10827         decl_specs->explicit_char_p = true;
10828       type = char_type_node;
10829       break;
10830     case RID_CHAR16:
10831       type = char16_type_node;
10832       break;
10833     case RID_CHAR32:
10834       type = char32_type_node;
10835       break;
10836     case RID_WCHAR:
10837       type = wchar_type_node;
10838       break;
10839     case RID_BOOL:
10840       type = boolean_type_node;
10841       break;
10842     case RID_SHORT:
10843       if (decl_specs)
10844         ++decl_specs->specs[(int) ds_short];
10845       type = short_integer_type_node;
10846       break;
10847     case RID_INT:
10848       if (decl_specs)
10849         decl_specs->explicit_int_p = true;
10850       type = integer_type_node;
10851       break;
10852     case RID_LONG:
10853       if (decl_specs)
10854         ++decl_specs->specs[(int) ds_long];
10855       type = long_integer_type_node;
10856       break;
10857     case RID_SIGNED:
10858       if (decl_specs)
10859         ++decl_specs->specs[(int) ds_signed];
10860       type = integer_type_node;
10861       break;
10862     case RID_UNSIGNED:
10863       if (decl_specs)
10864         ++decl_specs->specs[(int) ds_unsigned];
10865       type = unsigned_type_node;
10866       break;
10867     case RID_FLOAT:
10868       type = float_type_node;
10869       break;
10870     case RID_DOUBLE:
10871       type = double_type_node;
10872       break;
10873     case RID_VOID:
10874       type = void_type_node;
10875       break;
10876       
10877     case RID_AUTO:
10878       if (cxx_dialect != cxx98)
10879         {
10880           /* Consume the token.  */
10881           cp_lexer_consume_token (parser->lexer);
10882           /* We do not yet support the use of `auto' as a
10883              type-specifier.  */
10884           error ("C++0x %<auto%> specifier not supported");
10885         }
10886       break;
10887
10888     case RID_DECLTYPE:
10889       /* Parse the `decltype' type.  */
10890       type = cp_parser_decltype (parser);
10891
10892       if (decl_specs)
10893         cp_parser_set_decl_spec_type (decl_specs, type,
10894                                       /*user_defined_p=*/true);
10895
10896       return type;
10897
10898     case RID_TYPEOF:
10899       /* Consume the `typeof' token.  */
10900       cp_lexer_consume_token (parser->lexer);
10901       /* Parse the operand to `typeof'.  */
10902       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10903       /* If it is not already a TYPE, take its type.  */
10904       if (!TYPE_P (type))
10905         type = finish_typeof (type);
10906
10907       if (decl_specs)
10908         cp_parser_set_decl_spec_type (decl_specs, type,
10909                                       /*user_defined_p=*/true);
10910
10911       return type;
10912
10913     default:
10914       break;
10915     }
10916
10917   /* If the type-specifier was for a built-in type, we're done.  */
10918   if (type)
10919     {
10920       tree id;
10921
10922       /* Record the type.  */
10923       if (decl_specs
10924           && (token->keyword != RID_SIGNED
10925               && token->keyword != RID_UNSIGNED
10926               && token->keyword != RID_SHORT
10927               && token->keyword != RID_LONG))
10928         cp_parser_set_decl_spec_type (decl_specs,
10929                                       type,
10930                                       /*user_defined=*/false);
10931       if (decl_specs)
10932         decl_specs->any_specifiers_p = true;
10933
10934       /* Consume the token.  */
10935       id = cp_lexer_consume_token (parser->lexer)->u.value;
10936
10937       /* There is no valid C++ program where a non-template type is
10938          followed by a "<".  That usually indicates that the user thought
10939          that the type was a template.  */
10940       cp_parser_check_for_invalid_template_id (parser, type);
10941
10942       return TYPE_NAME (type);
10943     }
10944
10945   /* The type-specifier must be a user-defined type.  */
10946   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10947     {
10948       bool qualified_p;
10949       bool global_p;
10950
10951       /* Don't gobble tokens or issue error messages if this is an
10952          optional type-specifier.  */
10953       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10954         cp_parser_parse_tentatively (parser);
10955
10956       /* Look for the optional `::' operator.  */
10957       global_p
10958         = (cp_parser_global_scope_opt (parser,
10959                                        /*current_scope_valid_p=*/false)
10960            != NULL_TREE);
10961       /* Look for the nested-name specifier.  */
10962       qualified_p
10963         = (cp_parser_nested_name_specifier_opt (parser,
10964                                                 /*typename_keyword_p=*/false,
10965                                                 /*check_dependency_p=*/true,
10966                                                 /*type_p=*/false,
10967                                                 /*is_declaration=*/false)
10968            != NULL_TREE);
10969       /* If we have seen a nested-name-specifier, and the next token
10970          is `template', then we are using the template-id production.  */
10971       if (parser->scope
10972           && cp_parser_optional_template_keyword (parser))
10973         {
10974           /* Look for the template-id.  */
10975           type = cp_parser_template_id (parser,
10976                                         /*template_keyword_p=*/true,
10977                                         /*check_dependency_p=*/true,
10978                                         /*is_declaration=*/false);
10979           /* If the template-id did not name a type, we are out of
10980              luck.  */
10981           if (TREE_CODE (type) != TYPE_DECL)
10982             {
10983               cp_parser_error (parser, "expected template-id for type");
10984               type = NULL_TREE;
10985             }
10986         }
10987       /* Otherwise, look for a type-name.  */
10988       else
10989         type = cp_parser_type_name (parser);
10990       /* Keep track of all name-lookups performed in class scopes.  */
10991       if (type
10992           && !global_p
10993           && !qualified_p
10994           && TREE_CODE (type) == TYPE_DECL
10995           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10996         maybe_note_name_used_in_class (DECL_NAME (type), type);
10997       /* If it didn't work out, we don't have a TYPE.  */
10998       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10999           && !cp_parser_parse_definitely (parser))
11000         type = NULL_TREE;
11001       if (type && decl_specs)
11002         cp_parser_set_decl_spec_type (decl_specs, type,
11003                                       /*user_defined=*/true);
11004     }
11005
11006   /* If we didn't get a type-name, issue an error message.  */
11007   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11008     {
11009       cp_parser_error (parser, "expected type-name");
11010       return error_mark_node;
11011     }
11012
11013   /* There is no valid C++ program where a non-template type is
11014      followed by a "<".  That usually indicates that the user thought
11015      that the type was a template.  */
11016   if (type && type != error_mark_node)
11017     {
11018       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11019          If it is, then the '<'...'>' enclose protocol names rather than
11020          template arguments, and so everything is fine.  */
11021       if (c_dialect_objc ()
11022           && (objc_is_id (type) || objc_is_class_name (type)))
11023         {
11024           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11025           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11026
11027           /* Clobber the "unqualified" type previously entered into
11028              DECL_SPECS with the new, improved protocol-qualified version.  */
11029           if (decl_specs)
11030             decl_specs->type = qual_type;
11031
11032           return qual_type;
11033         }
11034
11035       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
11036     }
11037
11038   return type;
11039 }
11040
11041 /* Parse a type-name.
11042
11043    type-name:
11044      class-name
11045      enum-name
11046      typedef-name
11047
11048    enum-name:
11049      identifier
11050
11051    typedef-name:
11052      identifier
11053
11054    Returns a TYPE_DECL for the type.  */
11055
11056 static tree
11057 cp_parser_type_name (cp_parser* parser)
11058 {
11059   tree type_decl;
11060
11061   /* We can't know yet whether it is a class-name or not.  */
11062   cp_parser_parse_tentatively (parser);
11063   /* Try a class-name.  */
11064   type_decl = cp_parser_class_name (parser,
11065                                     /*typename_keyword_p=*/false,
11066                                     /*template_keyword_p=*/false,
11067                                     none_type,
11068                                     /*check_dependency_p=*/true,
11069                                     /*class_head_p=*/false,
11070                                     /*is_declaration=*/false);
11071   /* If it's not a class-name, keep looking.  */
11072   if (!cp_parser_parse_definitely (parser))
11073     {
11074       /* It must be a typedef-name or an enum-name.  */
11075       return cp_parser_nonclass_name (parser);
11076     }
11077
11078   return type_decl;
11079 }
11080
11081 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11082
11083    enum-name:
11084      identifier
11085
11086    typedef-name:
11087      identifier
11088
11089    Returns a TYPE_DECL for the type.  */
11090
11091 static tree
11092 cp_parser_nonclass_name (cp_parser* parser)
11093 {
11094   tree type_decl;
11095   tree identifier;
11096
11097   identifier = cp_parser_identifier (parser);
11098   if (identifier == error_mark_node)
11099     return error_mark_node;
11100
11101   /* Look up the type-name.  */
11102   type_decl = cp_parser_lookup_name_simple (parser, identifier);
11103
11104   if (TREE_CODE (type_decl) != TYPE_DECL
11105       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11106     {
11107       /* See if this is an Objective-C type.  */
11108       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11109       tree type = objc_get_protocol_qualified_type (identifier, protos);
11110       if (type)
11111         type_decl = TYPE_NAME (type);
11112     }
11113   
11114   /* Issue an error if we did not find a type-name.  */
11115   if (TREE_CODE (type_decl) != TYPE_DECL)
11116     {
11117       if (!cp_parser_simulate_error (parser))
11118         cp_parser_name_lookup_error (parser, identifier, type_decl,
11119                                      "is not a type");
11120       return error_mark_node;
11121     }
11122   /* Remember that the name was used in the definition of the
11123      current class so that we can check later to see if the
11124      meaning would have been different after the class was
11125      entirely defined.  */
11126   else if (type_decl != error_mark_node
11127            && !parser->scope)
11128     maybe_note_name_used_in_class (identifier, type_decl);
11129   
11130   return type_decl;
11131 }
11132
11133 /* Parse an elaborated-type-specifier.  Note that the grammar given
11134    here incorporates the resolution to DR68.
11135
11136    elaborated-type-specifier:
11137      class-key :: [opt] nested-name-specifier [opt] identifier
11138      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11139      enum :: [opt] nested-name-specifier [opt] identifier
11140      typename :: [opt] nested-name-specifier identifier
11141      typename :: [opt] nested-name-specifier template [opt]
11142        template-id
11143
11144    GNU extension:
11145
11146    elaborated-type-specifier:
11147      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11148      class-key attributes :: [opt] nested-name-specifier [opt]
11149                template [opt] template-id
11150      enum attributes :: [opt] nested-name-specifier [opt] identifier
11151
11152    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11153    declared `friend'.  If IS_DECLARATION is TRUE, then this
11154    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11155    something is being declared.
11156
11157    Returns the TYPE specified.  */
11158
11159 static tree
11160 cp_parser_elaborated_type_specifier (cp_parser* parser,
11161                                      bool is_friend,
11162                                      bool is_declaration)
11163 {
11164   enum tag_types tag_type;
11165   tree identifier;
11166   tree type = NULL_TREE;
11167   tree attributes = NULL_TREE;
11168
11169   /* See if we're looking at the `enum' keyword.  */
11170   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11171     {
11172       /* Consume the `enum' token.  */
11173       cp_lexer_consume_token (parser->lexer);
11174       /* Remember that it's an enumeration type.  */
11175       tag_type = enum_type;
11176       /* Parse the attributes.  */
11177       attributes = cp_parser_attributes_opt (parser);
11178     }
11179   /* Or, it might be `typename'.  */
11180   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11181                                            RID_TYPENAME))
11182     {
11183       /* Consume the `typename' token.  */
11184       cp_lexer_consume_token (parser->lexer);
11185       /* Remember that it's a `typename' type.  */
11186       tag_type = typename_type;
11187       /* The `typename' keyword is only allowed in templates.  */
11188       if (!processing_template_decl)
11189         pedwarn ("using %<typename%> outside of template");
11190     }
11191   /* Otherwise it must be a class-key.  */
11192   else
11193     {
11194       tag_type = cp_parser_class_key (parser);
11195       if (tag_type == none_type)
11196         return error_mark_node;
11197       /* Parse the attributes.  */
11198       attributes = cp_parser_attributes_opt (parser);
11199     }
11200
11201   /* Look for the `::' operator.  */
11202   cp_parser_global_scope_opt (parser,
11203                               /*current_scope_valid_p=*/false);
11204   /* Look for the nested-name-specifier.  */
11205   if (tag_type == typename_type)
11206     {
11207       if (!cp_parser_nested_name_specifier (parser,
11208                                            /*typename_keyword_p=*/true,
11209                                            /*check_dependency_p=*/true,
11210                                            /*type_p=*/true,
11211                                             is_declaration))
11212         return error_mark_node;
11213     }
11214   else
11215     /* Even though `typename' is not present, the proposed resolution
11216        to Core Issue 180 says that in `class A<T>::B', `B' should be
11217        considered a type-name, even if `A<T>' is dependent.  */
11218     cp_parser_nested_name_specifier_opt (parser,
11219                                          /*typename_keyword_p=*/true,
11220                                          /*check_dependency_p=*/true,
11221                                          /*type_p=*/true,
11222                                          is_declaration);
11223  /* For everything but enumeration types, consider a template-id.
11224     For an enumeration type, consider only a plain identifier.  */
11225   if (tag_type != enum_type)
11226     {
11227       bool template_p = false;
11228       tree decl;
11229
11230       /* Allow the `template' keyword.  */
11231       template_p = cp_parser_optional_template_keyword (parser);
11232       /* If we didn't see `template', we don't know if there's a
11233          template-id or not.  */
11234       if (!template_p)
11235         cp_parser_parse_tentatively (parser);
11236       /* Parse the template-id.  */
11237       decl = cp_parser_template_id (parser, template_p,
11238                                     /*check_dependency_p=*/true,
11239                                     is_declaration);
11240       /* If we didn't find a template-id, look for an ordinary
11241          identifier.  */
11242       if (!template_p && !cp_parser_parse_definitely (parser))
11243         ;
11244       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11245          in effect, then we must assume that, upon instantiation, the
11246          template will correspond to a class.  */
11247       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11248                && tag_type == typename_type)
11249         type = make_typename_type (parser->scope, decl,
11250                                    typename_type,
11251                                    /*complain=*/tf_error);
11252       else
11253         type = TREE_TYPE (decl);
11254     }
11255
11256   if (!type)
11257     {
11258       identifier = cp_parser_identifier (parser);
11259
11260       if (identifier == error_mark_node)
11261         {
11262           parser->scope = NULL_TREE;
11263           return error_mark_node;
11264         }
11265
11266       /* For a `typename', we needn't call xref_tag.  */
11267       if (tag_type == typename_type
11268           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11269         return cp_parser_make_typename_type (parser, parser->scope,
11270                                              identifier);
11271       /* Look up a qualified name in the usual way.  */
11272       if (parser->scope)
11273         {
11274           tree decl;
11275           tree ambiguous_decls;
11276
11277           decl = cp_parser_lookup_name (parser, identifier,
11278                                         tag_type,
11279                                         /*is_template=*/false,
11280                                         /*is_namespace=*/false,
11281                                         /*check_dependency=*/true,
11282                                         &ambiguous_decls);
11283
11284           /* If the lookup was ambiguous, an error will already have been
11285              issued.  */
11286           if (ambiguous_decls)
11287             return error_mark_node;
11288
11289           /* If we are parsing friend declaration, DECL may be a
11290              TEMPLATE_DECL tree node here.  However, we need to check
11291              whether this TEMPLATE_DECL results in valid code.  Consider
11292              the following example:
11293
11294                namespace N {
11295                  template <class T> class C {};
11296                }
11297                class X {
11298                  template <class T> friend class N::C; // #1, valid code
11299                };
11300                template <class T> class Y {
11301                  friend class N::C;                    // #2, invalid code
11302                };
11303
11304              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11305              name lookup of `N::C'.  We see that friend declaration must
11306              be template for the code to be valid.  Note that
11307              processing_template_decl does not work here since it is
11308              always 1 for the above two cases.  */
11309
11310           decl = (cp_parser_maybe_treat_template_as_class
11311                   (decl, /*tag_name_p=*/is_friend
11312                          && parser->num_template_parameter_lists));
11313
11314           if (TREE_CODE (decl) != TYPE_DECL)
11315             {
11316               cp_parser_diagnose_invalid_type_name (parser,
11317                                                     parser->scope,
11318                                                     identifier);
11319               return error_mark_node;
11320             }
11321
11322           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11323             {
11324               bool allow_template = (parser->num_template_parameter_lists
11325                                       || DECL_SELF_REFERENCE_P (decl));
11326               type = check_elaborated_type_specifier (tag_type, decl, 
11327                                                       allow_template);
11328
11329               if (type == error_mark_node)
11330                 return error_mark_node;
11331             }
11332
11333           /* Forward declarations of nested types, such as
11334
11335                class C1::C2;
11336                class C1::C2::C3;
11337
11338              are invalid unless all components preceding the final '::'
11339              are complete.  If all enclosing types are complete, these
11340              declarations become merely pointless.
11341
11342              Invalid forward declarations of nested types are errors
11343              caught elsewhere in parsing.  Those that are pointless arrive
11344              here.  */
11345
11346           if (cp_parser_declares_only_class_p (parser)
11347               && !is_friend && !processing_explicit_instantiation)
11348             warning (0, "declaration %qD does not declare anything", decl);
11349
11350           type = TREE_TYPE (decl);
11351         }
11352       else
11353         {
11354           /* An elaborated-type-specifier sometimes introduces a new type and
11355              sometimes names an existing type.  Normally, the rule is that it
11356              introduces a new type only if there is not an existing type of
11357              the same name already in scope.  For example, given:
11358
11359                struct S {};
11360                void f() { struct S s; }
11361
11362              the `struct S' in the body of `f' is the same `struct S' as in
11363              the global scope; the existing definition is used.  However, if
11364              there were no global declaration, this would introduce a new
11365              local class named `S'.
11366
11367              An exception to this rule applies to the following code:
11368
11369                namespace N { struct S; }
11370
11371              Here, the elaborated-type-specifier names a new type
11372              unconditionally; even if there is already an `S' in the
11373              containing scope this declaration names a new type.
11374              This exception only applies if the elaborated-type-specifier
11375              forms the complete declaration:
11376
11377                [class.name]
11378
11379                A declaration consisting solely of `class-key identifier ;' is
11380                either a redeclaration of the name in the current scope or a
11381                forward declaration of the identifier as a class name.  It
11382                introduces the name into the current scope.
11383
11384              We are in this situation precisely when the next token is a `;'.
11385
11386              An exception to the exception is that a `friend' declaration does
11387              *not* name a new type; i.e., given:
11388
11389                struct S { friend struct T; };
11390
11391              `T' is not a new type in the scope of `S'.
11392
11393              Also, `new struct S' or `sizeof (struct S)' never results in the
11394              definition of a new type; a new type can only be declared in a
11395              declaration context.  */
11396
11397           tag_scope ts;
11398           bool template_p;
11399
11400           if (is_friend)
11401             /* Friends have special name lookup rules.  */
11402             ts = ts_within_enclosing_non_class;
11403           else if (is_declaration
11404                    && cp_lexer_next_token_is (parser->lexer,
11405                                               CPP_SEMICOLON))
11406             /* This is a `class-key identifier ;' */
11407             ts = ts_current;
11408           else
11409             ts = ts_global;
11410
11411           template_p =
11412             (parser->num_template_parameter_lists
11413              && (cp_parser_next_token_starts_class_definition_p (parser)
11414                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11415           /* An unqualified name was used to reference this type, so
11416              there were no qualifying templates.  */
11417           if (!cp_parser_check_template_parameters (parser,
11418                                                     /*num_templates=*/0))
11419             return error_mark_node;
11420           type = xref_tag (tag_type, identifier, ts, template_p);
11421         }
11422     }
11423
11424   if (type == error_mark_node)
11425     return error_mark_node;
11426
11427   /* Allow attributes on forward declarations of classes.  */
11428   if (attributes)
11429     {
11430       if (TREE_CODE (type) == TYPENAME_TYPE)
11431         warning (OPT_Wattributes,
11432                  "attributes ignored on uninstantiated type");
11433       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11434                && ! processing_explicit_instantiation)
11435         warning (OPT_Wattributes,
11436                  "attributes ignored on template instantiation");
11437       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11438         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11439       else
11440         warning (OPT_Wattributes,
11441                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11442     }
11443
11444   if (tag_type != enum_type)
11445     cp_parser_check_class_key (tag_type, type);
11446
11447   /* A "<" cannot follow an elaborated type specifier.  If that
11448      happens, the user was probably trying to form a template-id.  */
11449   cp_parser_check_for_invalid_template_id (parser, type);
11450
11451   return type;
11452 }
11453
11454 /* Parse an enum-specifier.
11455
11456    enum-specifier:
11457      enum identifier [opt] { enumerator-list [opt] }
11458
11459    GNU Extensions:
11460      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11461        attributes[opt]
11462
11463    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11464    if the token stream isn't an enum-specifier after all.  */
11465
11466 static tree
11467 cp_parser_enum_specifier (cp_parser* parser)
11468 {
11469   tree identifier;
11470   tree type;
11471   tree attributes;
11472
11473   /* Parse tentatively so that we can back up if we don't find a
11474      enum-specifier.  */
11475   cp_parser_parse_tentatively (parser);
11476
11477   /* Caller guarantees that the current token is 'enum', an identifier
11478      possibly follows, and the token after that is an opening brace.
11479      If we don't have an identifier, fabricate an anonymous name for
11480      the enumeration being defined.  */
11481   cp_lexer_consume_token (parser->lexer);
11482
11483   attributes = cp_parser_attributes_opt (parser);
11484
11485   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11486     identifier = cp_parser_identifier (parser);
11487   else
11488     identifier = make_anon_name ();
11489
11490   /* Look for the `{' but don't consume it yet.  */
11491   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11492     cp_parser_simulate_error (parser);
11493
11494   if (!cp_parser_parse_definitely (parser))
11495     return NULL_TREE;
11496
11497   /* Issue an error message if type-definitions are forbidden here.  */
11498   if (!cp_parser_check_type_definition (parser))
11499     type = error_mark_node;
11500   else
11501     /* Create the new type.  We do this before consuming the opening
11502        brace so the enum will be recorded as being on the line of its
11503        tag (or the 'enum' keyword, if there is no tag).  */
11504     type = start_enum (identifier);
11505   
11506   /* Consume the opening brace.  */
11507   cp_lexer_consume_token (parser->lexer);
11508
11509   if (type == error_mark_node)
11510     {
11511       cp_parser_skip_to_end_of_block_or_statement (parser);
11512       return error_mark_node;
11513     }
11514
11515   /* If the next token is not '}', then there are some enumerators.  */
11516   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11517     cp_parser_enumerator_list (parser, type);
11518
11519   /* Consume the final '}'.  */
11520   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11521
11522   /* Look for trailing attributes to apply to this enumeration, and
11523      apply them if appropriate.  */
11524   if (cp_parser_allow_gnu_extensions_p (parser))
11525     {
11526       tree trailing_attr = cp_parser_attributes_opt (parser);
11527       cplus_decl_attributes (&type,
11528                              trailing_attr,
11529                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11530     }
11531
11532   /* Finish up the enumeration.  */
11533   finish_enum (type);
11534
11535   return type;
11536 }
11537
11538 /* Parse an enumerator-list.  The enumerators all have the indicated
11539    TYPE.
11540
11541    enumerator-list:
11542      enumerator-definition
11543      enumerator-list , enumerator-definition  */
11544
11545 static void
11546 cp_parser_enumerator_list (cp_parser* parser, tree type)
11547 {
11548   while (true)
11549     {
11550       /* Parse an enumerator-definition.  */
11551       cp_parser_enumerator_definition (parser, type);
11552
11553       /* If the next token is not a ',', we've reached the end of
11554          the list.  */
11555       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11556         break;
11557       /* Otherwise, consume the `,' and keep going.  */
11558       cp_lexer_consume_token (parser->lexer);
11559       /* If the next token is a `}', there is a trailing comma.  */
11560       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11561         {
11562           if (pedantic && !in_system_header)
11563             pedwarn ("comma at end of enumerator list");
11564           break;
11565         }
11566     }
11567 }
11568
11569 /* Parse an enumerator-definition.  The enumerator has the indicated
11570    TYPE.
11571
11572    enumerator-definition:
11573      enumerator
11574      enumerator = constant-expression
11575
11576    enumerator:
11577      identifier  */
11578
11579 static void
11580 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11581 {
11582   tree identifier;
11583   tree value;
11584
11585   /* Look for the identifier.  */
11586   identifier = cp_parser_identifier (parser);
11587   if (identifier == error_mark_node)
11588     return;
11589
11590   /* If the next token is an '=', then there is an explicit value.  */
11591   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11592     {
11593       /* Consume the `=' token.  */
11594       cp_lexer_consume_token (parser->lexer);
11595       /* Parse the value.  */
11596       value = cp_parser_constant_expression (parser,
11597                                              /*allow_non_constant_p=*/false,
11598                                              NULL);
11599     }
11600   else
11601     value = NULL_TREE;
11602
11603   /* Create the enumerator.  */
11604   build_enumerator (identifier, value, type);
11605 }
11606
11607 /* Parse a namespace-name.
11608
11609    namespace-name:
11610      original-namespace-name
11611      namespace-alias
11612
11613    Returns the NAMESPACE_DECL for the namespace.  */
11614
11615 static tree
11616 cp_parser_namespace_name (cp_parser* parser)
11617 {
11618   tree identifier;
11619   tree namespace_decl;
11620
11621   /* Get the name of the namespace.  */
11622   identifier = cp_parser_identifier (parser);
11623   if (identifier == error_mark_node)
11624     return error_mark_node;
11625
11626   /* Look up the identifier in the currently active scope.  Look only
11627      for namespaces, due to:
11628
11629        [basic.lookup.udir]
11630
11631        When looking up a namespace-name in a using-directive or alias
11632        definition, only namespace names are considered.
11633
11634      And:
11635
11636        [basic.lookup.qual]
11637
11638        During the lookup of a name preceding the :: scope resolution
11639        operator, object, function, and enumerator names are ignored.
11640
11641      (Note that cp_parser_class_or_namespace_name only calls this
11642      function if the token after the name is the scope resolution
11643      operator.)  */
11644   namespace_decl = cp_parser_lookup_name (parser, identifier,
11645                                           none_type,
11646                                           /*is_template=*/false,
11647                                           /*is_namespace=*/true,
11648                                           /*check_dependency=*/true,
11649                                           /*ambiguous_decls=*/NULL);
11650   /* If it's not a namespace, issue an error.  */
11651   if (namespace_decl == error_mark_node
11652       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11653     {
11654       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11655         error ("%qD is not a namespace-name", identifier);
11656       cp_parser_error (parser, "expected namespace-name");
11657       namespace_decl = error_mark_node;
11658     }
11659
11660   return namespace_decl;
11661 }
11662
11663 /* Parse a namespace-definition.
11664
11665    namespace-definition:
11666      named-namespace-definition
11667      unnamed-namespace-definition
11668
11669    named-namespace-definition:
11670      original-namespace-definition
11671      extension-namespace-definition
11672
11673    original-namespace-definition:
11674      namespace identifier { namespace-body }
11675
11676    extension-namespace-definition:
11677      namespace original-namespace-name { namespace-body }
11678
11679    unnamed-namespace-definition:
11680      namespace { namespace-body } */
11681
11682 static void
11683 cp_parser_namespace_definition (cp_parser* parser)
11684 {
11685   tree identifier, attribs;
11686   bool has_visibility;
11687   bool is_inline;
11688
11689   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11690     {
11691       is_inline = true;
11692       cp_lexer_consume_token (parser->lexer);
11693     }
11694   else
11695     is_inline = false;
11696
11697   /* Look for the `namespace' keyword.  */
11698   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11699
11700   /* Get the name of the namespace.  We do not attempt to distinguish
11701      between an original-namespace-definition and an
11702      extension-namespace-definition at this point.  The semantic
11703      analysis routines are responsible for that.  */
11704   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11705     identifier = cp_parser_identifier (parser);
11706   else
11707     identifier = NULL_TREE;
11708
11709   /* Parse any specified attributes.  */
11710   attribs = cp_parser_attributes_opt (parser);
11711
11712   /* Look for the `{' to start the namespace.  */
11713   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11714   /* Start the namespace.  */
11715   push_namespace (identifier);
11716
11717   /* "inline namespace" is equivalent to a stub namespace definition
11718      followed by a strong using directive.  */
11719   if (is_inline)
11720     {
11721       tree namespace = current_namespace;
11722       /* Set up namespace association.  */
11723       DECL_NAMESPACE_ASSOCIATIONS (namespace)
11724         = tree_cons (CP_DECL_CONTEXT (namespace), NULL_TREE,
11725                      DECL_NAMESPACE_ASSOCIATIONS (namespace));
11726       /* Import the contents of the inline namespace.  */
11727       pop_namespace ();
11728       do_using_directive (namespace);
11729       push_namespace (identifier);
11730     }
11731
11732   has_visibility = handle_namespace_attrs (current_namespace, attribs);
11733
11734   /* Parse the body of the namespace.  */
11735   cp_parser_namespace_body (parser);
11736
11737 #ifdef HANDLE_PRAGMA_VISIBILITY
11738   if (has_visibility)
11739     pop_visibility ();
11740 #endif
11741
11742   /* Finish the namespace.  */
11743   pop_namespace ();
11744   /* Look for the final `}'.  */
11745   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11746 }
11747
11748 /* Parse a namespace-body.
11749
11750    namespace-body:
11751      declaration-seq [opt]  */
11752
11753 static void
11754 cp_parser_namespace_body (cp_parser* parser)
11755 {
11756   cp_parser_declaration_seq_opt (parser);
11757 }
11758
11759 /* Parse a namespace-alias-definition.
11760
11761    namespace-alias-definition:
11762      namespace identifier = qualified-namespace-specifier ;  */
11763
11764 static void
11765 cp_parser_namespace_alias_definition (cp_parser* parser)
11766 {
11767   tree identifier;
11768   tree namespace_specifier;
11769
11770   /* Look for the `namespace' keyword.  */
11771   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11772   /* Look for the identifier.  */
11773   identifier = cp_parser_identifier (parser);
11774   if (identifier == error_mark_node)
11775     return;
11776   /* Look for the `=' token.  */
11777   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11778       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11779     {
11780       error ("%<namespace%> definition is not allowed here");
11781       /* Skip the definition.  */
11782       cp_lexer_consume_token (parser->lexer);
11783       if (cp_parser_skip_to_closing_brace (parser))
11784         cp_lexer_consume_token (parser->lexer);
11785       return;
11786     }
11787   cp_parser_require (parser, CPP_EQ, "%<=%>");
11788   /* Look for the qualified-namespace-specifier.  */
11789   namespace_specifier
11790     = cp_parser_qualified_namespace_specifier (parser);
11791   /* Look for the `;' token.  */
11792   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11793
11794   /* Register the alias in the symbol table.  */
11795   do_namespace_alias (identifier, namespace_specifier);
11796 }
11797
11798 /* Parse a qualified-namespace-specifier.
11799
11800    qualified-namespace-specifier:
11801      :: [opt] nested-name-specifier [opt] namespace-name
11802
11803    Returns a NAMESPACE_DECL corresponding to the specified
11804    namespace.  */
11805
11806 static tree
11807 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11808 {
11809   /* Look for the optional `::'.  */
11810   cp_parser_global_scope_opt (parser,
11811                               /*current_scope_valid_p=*/false);
11812
11813   /* Look for the optional nested-name-specifier.  */
11814   cp_parser_nested_name_specifier_opt (parser,
11815                                        /*typename_keyword_p=*/false,
11816                                        /*check_dependency_p=*/true,
11817                                        /*type_p=*/false,
11818                                        /*is_declaration=*/true);
11819
11820   return cp_parser_namespace_name (parser);
11821 }
11822
11823 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11824    access declaration.
11825
11826    using-declaration:
11827      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11828      using :: unqualified-id ;  
11829
11830    access-declaration:
11831      qualified-id ;  
11832
11833    */
11834
11835 static bool
11836 cp_parser_using_declaration (cp_parser* parser, 
11837                              bool access_declaration_p)
11838 {
11839   cp_token *token;
11840   bool typename_p = false;
11841   bool global_scope_p;
11842   tree decl;
11843   tree identifier;
11844   tree qscope;
11845
11846   if (access_declaration_p)
11847     cp_parser_parse_tentatively (parser);
11848   else
11849     {
11850       /* Look for the `using' keyword.  */
11851       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
11852       
11853       /* Peek at the next token.  */
11854       token = cp_lexer_peek_token (parser->lexer);
11855       /* See if it's `typename'.  */
11856       if (token->keyword == RID_TYPENAME)
11857         {
11858           /* Remember that we've seen it.  */
11859           typename_p = true;
11860           /* Consume the `typename' token.  */
11861           cp_lexer_consume_token (parser->lexer);
11862         }
11863     }
11864
11865   /* Look for the optional global scope qualification.  */
11866   global_scope_p
11867     = (cp_parser_global_scope_opt (parser,
11868                                    /*current_scope_valid_p=*/false)
11869        != NULL_TREE);
11870
11871   /* If we saw `typename', or didn't see `::', then there must be a
11872      nested-name-specifier present.  */
11873   if (typename_p || !global_scope_p)
11874     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11875                                               /*check_dependency_p=*/true,
11876                                               /*type_p=*/false,
11877                                               /*is_declaration=*/true);
11878   /* Otherwise, we could be in either of the two productions.  In that
11879      case, treat the nested-name-specifier as optional.  */
11880   else
11881     qscope = cp_parser_nested_name_specifier_opt (parser,
11882                                                   /*typename_keyword_p=*/false,
11883                                                   /*check_dependency_p=*/true,
11884                                                   /*type_p=*/false,
11885                                                   /*is_declaration=*/true);
11886   if (!qscope)
11887     qscope = global_namespace;
11888
11889   if (access_declaration_p && cp_parser_error_occurred (parser))
11890     /* Something has already gone wrong; there's no need to parse
11891        further.  Since an error has occurred, the return value of
11892        cp_parser_parse_definitely will be false, as required.  */
11893     return cp_parser_parse_definitely (parser);
11894
11895   /* Parse the unqualified-id.  */
11896   identifier = cp_parser_unqualified_id (parser,
11897                                          /*template_keyword_p=*/false,
11898                                          /*check_dependency_p=*/true,
11899                                          /*declarator_p=*/true,
11900                                          /*optional_p=*/false);
11901
11902   if (access_declaration_p)
11903     {
11904       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11905         cp_parser_simulate_error (parser);
11906       if (!cp_parser_parse_definitely (parser))
11907         return false;
11908     }
11909
11910   /* The function we call to handle a using-declaration is different
11911      depending on what scope we are in.  */
11912   if (qscope == error_mark_node || identifier == error_mark_node)
11913     ;
11914   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11915            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11916     /* [namespace.udecl]
11917
11918        A using declaration shall not name a template-id.  */
11919     error ("a template-id may not appear in a using-declaration");
11920   else
11921     {
11922       if (at_class_scope_p ())
11923         {
11924           /* Create the USING_DECL.  */
11925           decl = do_class_using_decl (parser->scope, identifier);
11926
11927           if (check_for_bare_parameter_packs (decl))
11928             return false;
11929           else
11930             /* Add it to the list of members in this class.  */
11931             finish_member_declaration (decl);
11932         }
11933       else
11934         {
11935           decl = cp_parser_lookup_name_simple (parser, identifier);
11936           if (decl == error_mark_node)
11937             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11938           else if (check_for_bare_parameter_packs (decl))
11939             return false;
11940           else if (!at_namespace_scope_p ())
11941             do_local_using_decl (decl, qscope, identifier);
11942           else
11943             do_toplevel_using_decl (decl, qscope, identifier);
11944         }
11945     }
11946
11947   /* Look for the final `;'.  */
11948   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11949   
11950   return true;
11951 }
11952
11953 /* Parse a using-directive.
11954
11955    using-directive:
11956      using namespace :: [opt] nested-name-specifier [opt]
11957        namespace-name ;  */
11958
11959 static void
11960 cp_parser_using_directive (cp_parser* parser)
11961 {
11962   tree namespace_decl;
11963   tree attribs;
11964
11965   /* Look for the `using' keyword.  */
11966   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
11967   /* And the `namespace' keyword.  */
11968   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11969   /* Look for the optional `::' operator.  */
11970   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11971   /* And the optional nested-name-specifier.  */
11972   cp_parser_nested_name_specifier_opt (parser,
11973                                        /*typename_keyword_p=*/false,
11974                                        /*check_dependency_p=*/true,
11975                                        /*type_p=*/false,
11976                                        /*is_declaration=*/true);
11977   /* Get the namespace being used.  */
11978   namespace_decl = cp_parser_namespace_name (parser);
11979   /* And any specified attributes.  */
11980   attribs = cp_parser_attributes_opt (parser);
11981   /* Update the symbol table.  */
11982   parse_using_directive (namespace_decl, attribs);
11983   /* Look for the final `;'.  */
11984   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11985 }
11986
11987 /* Parse an asm-definition.
11988
11989    asm-definition:
11990      asm ( string-literal ) ;
11991
11992    GNU Extension:
11993
11994    asm-definition:
11995      asm volatile [opt] ( string-literal ) ;
11996      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11997      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11998                           : asm-operand-list [opt] ) ;
11999      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12000                           : asm-operand-list [opt]
12001                           : asm-operand-list [opt] ) ;  */
12002
12003 static void
12004 cp_parser_asm_definition (cp_parser* parser)
12005 {
12006   tree string;
12007   tree outputs = NULL_TREE;
12008   tree inputs = NULL_TREE;
12009   tree clobbers = NULL_TREE;
12010   tree asm_stmt;
12011   bool volatile_p = false;
12012   bool extended_p = false;
12013   bool invalid_inputs_p = false;
12014   bool invalid_outputs_p = false;
12015
12016   /* Look for the `asm' keyword.  */
12017   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12018   /* See if the next token is `volatile'.  */
12019   if (cp_parser_allow_gnu_extensions_p (parser)
12020       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12021     {
12022       /* Remember that we saw the `volatile' keyword.  */
12023       volatile_p = true;
12024       /* Consume the token.  */
12025       cp_lexer_consume_token (parser->lexer);
12026     }
12027   /* Look for the opening `('.  */
12028   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12029     return;
12030   /* Look for the string.  */
12031   string = cp_parser_string_literal (parser, false, false);
12032   if (string == error_mark_node)
12033     {
12034       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12035                                              /*consume_paren=*/true);
12036       return;
12037     }
12038
12039   /* If we're allowing GNU extensions, check for the extended assembly
12040      syntax.  Unfortunately, the `:' tokens need not be separated by
12041      a space in C, and so, for compatibility, we tolerate that here
12042      too.  Doing that means that we have to treat the `::' operator as
12043      two `:' tokens.  */
12044   if (cp_parser_allow_gnu_extensions_p (parser)
12045       && parser->in_function_body
12046       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12047           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12048     {
12049       bool inputs_p = false;
12050       bool clobbers_p = false;
12051
12052       /* The extended syntax was used.  */
12053       extended_p = true;
12054
12055       /* Look for outputs.  */
12056       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12057         {
12058           /* Consume the `:'.  */
12059           cp_lexer_consume_token (parser->lexer);
12060           /* Parse the output-operands.  */
12061           if (cp_lexer_next_token_is_not (parser->lexer,
12062                                           CPP_COLON)
12063               && cp_lexer_next_token_is_not (parser->lexer,
12064                                              CPP_SCOPE)
12065               && cp_lexer_next_token_is_not (parser->lexer,
12066                                              CPP_CLOSE_PAREN))
12067             outputs = cp_parser_asm_operand_list (parser);
12068
12069             if (outputs == error_mark_node)
12070               invalid_outputs_p = true;
12071         }
12072       /* If the next token is `::', there are no outputs, and the
12073          next token is the beginning of the inputs.  */
12074       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12075         /* The inputs are coming next.  */
12076         inputs_p = true;
12077
12078       /* Look for inputs.  */
12079       if (inputs_p
12080           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12081         {
12082           /* Consume the `:' or `::'.  */
12083           cp_lexer_consume_token (parser->lexer);
12084           /* Parse the output-operands.  */
12085           if (cp_lexer_next_token_is_not (parser->lexer,
12086                                           CPP_COLON)
12087               && cp_lexer_next_token_is_not (parser->lexer,
12088                                              CPP_CLOSE_PAREN))
12089             inputs = cp_parser_asm_operand_list (parser);
12090
12091             if (inputs == error_mark_node)
12092               invalid_inputs_p = true;
12093         }
12094       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12095         /* The clobbers are coming next.  */
12096         clobbers_p = true;
12097
12098       /* Look for clobbers.  */
12099       if (clobbers_p
12100           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12101         {
12102           /* Consume the `:' or `::'.  */
12103           cp_lexer_consume_token (parser->lexer);
12104           /* Parse the clobbers.  */
12105           if (cp_lexer_next_token_is_not (parser->lexer,
12106                                           CPP_CLOSE_PAREN))
12107             clobbers = cp_parser_asm_clobber_list (parser);
12108         }
12109     }
12110   /* Look for the closing `)'.  */
12111   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12112     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12113                                            /*consume_paren=*/true);
12114   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12115
12116   if (!invalid_inputs_p && !invalid_outputs_p)
12117     {
12118       /* Create the ASM_EXPR.  */
12119       if (parser->in_function_body)
12120         {
12121           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12122                                       inputs, clobbers);
12123           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12124           if (!extended_p)
12125             {
12126               tree temp = asm_stmt;
12127               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12128                 temp = TREE_OPERAND (temp, 0);
12129
12130               ASM_INPUT_P (temp) = 1;
12131             }
12132         }
12133       else
12134         cgraph_add_asm_node (string);
12135     }
12136 }
12137
12138 /* Declarators [gram.dcl.decl] */
12139
12140 /* Parse an init-declarator.
12141
12142    init-declarator:
12143      declarator initializer [opt]
12144
12145    GNU Extension:
12146
12147    init-declarator:
12148      declarator asm-specification [opt] attributes [opt] initializer [opt]
12149
12150    function-definition:
12151      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12152        function-body
12153      decl-specifier-seq [opt] declarator function-try-block
12154
12155    GNU Extension:
12156
12157    function-definition:
12158      __extension__ function-definition
12159
12160    The DECL_SPECIFIERS apply to this declarator.  Returns a
12161    representation of the entity declared.  If MEMBER_P is TRUE, then
12162    this declarator appears in a class scope.  The new DECL created by
12163    this declarator is returned.
12164
12165    The CHECKS are access checks that should be performed once we know
12166    what entity is being declared (and, therefore, what classes have
12167    befriended it).
12168
12169    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12170    for a function-definition here as well.  If the declarator is a
12171    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12172    be TRUE upon return.  By that point, the function-definition will
12173    have been completely parsed.
12174
12175    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12176    is FALSE.  */
12177
12178 static tree
12179 cp_parser_init_declarator (cp_parser* parser,
12180                            cp_decl_specifier_seq *decl_specifiers,
12181                            VEC (deferred_access_check,gc)* checks,
12182                            bool function_definition_allowed_p,
12183                            bool member_p,
12184                            int declares_class_or_enum,
12185                            bool* function_definition_p)
12186 {
12187   cp_token *token;
12188   cp_declarator *declarator;
12189   tree prefix_attributes;
12190   tree attributes;
12191   tree asm_specification;
12192   tree initializer;
12193   tree decl = NULL_TREE;
12194   tree scope;
12195   bool is_initialized;
12196   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12197      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12198      "(...)".  */
12199   enum cpp_ttype initialization_kind;
12200   bool is_parenthesized_init = false;
12201   bool is_non_constant_init;
12202   int ctor_dtor_or_conv_p;
12203   bool friend_p;
12204   tree pushed_scope = NULL;
12205
12206   /* Gather the attributes that were provided with the
12207      decl-specifiers.  */
12208   prefix_attributes = decl_specifiers->attributes;
12209
12210   /* Assume that this is not the declarator for a function
12211      definition.  */
12212   if (function_definition_p)
12213     *function_definition_p = false;
12214
12215   /* Defer access checks while parsing the declarator; we cannot know
12216      what names are accessible until we know what is being
12217      declared.  */
12218   resume_deferring_access_checks ();
12219
12220   /* Parse the declarator.  */
12221   declarator
12222     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12223                             &ctor_dtor_or_conv_p,
12224                             /*parenthesized_p=*/NULL,
12225                             /*member_p=*/false);
12226   /* Gather up the deferred checks.  */
12227   stop_deferring_access_checks ();
12228
12229   /* If the DECLARATOR was erroneous, there's no need to go
12230      further.  */
12231   if (declarator == cp_error_declarator)
12232     return error_mark_node;
12233
12234   /* Check that the number of template-parameter-lists is OK.  */
12235   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12236     return error_mark_node;
12237
12238   if (declares_class_or_enum & 2)
12239     cp_parser_check_for_definition_in_return_type (declarator,
12240                                                    decl_specifiers->type);
12241
12242   /* Figure out what scope the entity declared by the DECLARATOR is
12243      located in.  `grokdeclarator' sometimes changes the scope, so
12244      we compute it now.  */
12245   scope = get_scope_of_declarator (declarator);
12246
12247   /* If we're allowing GNU extensions, look for an asm-specification
12248      and attributes.  */
12249   if (cp_parser_allow_gnu_extensions_p (parser))
12250     {
12251       /* Look for an asm-specification.  */
12252       asm_specification = cp_parser_asm_specification_opt (parser);
12253       /* And attributes.  */
12254       attributes = cp_parser_attributes_opt (parser);
12255     }
12256   else
12257     {
12258       asm_specification = NULL_TREE;
12259       attributes = NULL_TREE;
12260     }
12261
12262   /* Peek at the next token.  */
12263   token = cp_lexer_peek_token (parser->lexer);
12264   /* Check to see if the token indicates the start of a
12265      function-definition.  */
12266   if (cp_parser_token_starts_function_definition_p (token))
12267     {
12268       if (!function_definition_allowed_p)
12269         {
12270           /* If a function-definition should not appear here, issue an
12271              error message.  */
12272           cp_parser_error (parser,
12273                            "a function-definition is not allowed here");
12274           return error_mark_node;
12275         }
12276       else
12277         {
12278           /* Neither attributes nor an asm-specification are allowed
12279              on a function-definition.  */
12280           if (asm_specification)
12281             error ("an asm-specification is not allowed on a function-definition");
12282           if (attributes)
12283             error ("attributes are not allowed on a function-definition");
12284           /* This is a function-definition.  */
12285           *function_definition_p = true;
12286
12287           /* Parse the function definition.  */
12288           if (member_p)
12289             decl = cp_parser_save_member_function_body (parser,
12290                                                         decl_specifiers,
12291                                                         declarator,
12292                                                         prefix_attributes);
12293           else
12294             decl
12295               = (cp_parser_function_definition_from_specifiers_and_declarator
12296                  (parser, decl_specifiers, prefix_attributes, declarator));
12297
12298           return decl;
12299         }
12300     }
12301
12302   /* [dcl.dcl]
12303
12304      Only in function declarations for constructors, destructors, and
12305      type conversions can the decl-specifier-seq be omitted.
12306
12307      We explicitly postpone this check past the point where we handle
12308      function-definitions because we tolerate function-definitions
12309      that are missing their return types in some modes.  */
12310   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12311     {
12312       cp_parser_error (parser,
12313                        "expected constructor, destructor, or type conversion");
12314       return error_mark_node;
12315     }
12316
12317   /* An `=' or an `(' indicates an initializer.  */
12318   if (token->type == CPP_EQ
12319       || token->type == CPP_OPEN_PAREN)
12320     {
12321       is_initialized = true;
12322       initialization_kind = token->type;
12323     }
12324   else
12325     {
12326       /* If the init-declarator isn't initialized and isn't followed by a
12327          `,' or `;', it's not a valid init-declarator.  */
12328       if (token->type != CPP_COMMA
12329           && token->type != CPP_SEMICOLON)
12330         {
12331           cp_parser_error (parser, "expected initializer");
12332           return error_mark_node;
12333         }
12334       is_initialized = false;
12335       initialization_kind = CPP_EOF;
12336     }
12337
12338   /* Because start_decl has side-effects, we should only call it if we
12339      know we're going ahead.  By this point, we know that we cannot
12340      possibly be looking at any other construct.  */
12341   cp_parser_commit_to_tentative_parse (parser);
12342
12343   /* If the decl specifiers were bad, issue an error now that we're
12344      sure this was intended to be a declarator.  Then continue
12345      declaring the variable(s), as int, to try to cut down on further
12346      errors.  */
12347   if (decl_specifiers->any_specifiers_p
12348       && decl_specifiers->type == error_mark_node)
12349     {
12350       cp_parser_error (parser, "invalid type in declaration");
12351       decl_specifiers->type = integer_type_node;
12352     }
12353
12354   /* Check to see whether or not this declaration is a friend.  */
12355   friend_p = cp_parser_friend_p (decl_specifiers);
12356
12357   /* Enter the newly declared entry in the symbol table.  If we're
12358      processing a declaration in a class-specifier, we wait until
12359      after processing the initializer.  */
12360   if (!member_p)
12361     {
12362       if (parser->in_unbraced_linkage_specification_p)
12363         decl_specifiers->storage_class = sc_extern;
12364       decl = start_decl (declarator, decl_specifiers,
12365                          is_initialized, attributes, prefix_attributes,
12366                          &pushed_scope);
12367     }
12368   else if (scope)
12369     /* Enter the SCOPE.  That way unqualified names appearing in the
12370        initializer will be looked up in SCOPE.  */
12371     pushed_scope = push_scope (scope);
12372
12373   /* Perform deferred access control checks, now that we know in which
12374      SCOPE the declared entity resides.  */
12375   if (!member_p && decl)
12376     {
12377       tree saved_current_function_decl = NULL_TREE;
12378
12379       /* If the entity being declared is a function, pretend that we
12380          are in its scope.  If it is a `friend', it may have access to
12381          things that would not otherwise be accessible.  */
12382       if (TREE_CODE (decl) == FUNCTION_DECL)
12383         {
12384           saved_current_function_decl = current_function_decl;
12385           current_function_decl = decl;
12386         }
12387
12388       /* Perform access checks for template parameters.  */
12389       cp_parser_perform_template_parameter_access_checks (checks);
12390
12391       /* Perform the access control checks for the declarator and the
12392          the decl-specifiers.  */
12393       perform_deferred_access_checks ();
12394
12395       /* Restore the saved value.  */
12396       if (TREE_CODE (decl) == FUNCTION_DECL)
12397         current_function_decl = saved_current_function_decl;
12398     }
12399
12400   /* Parse the initializer.  */
12401   initializer = NULL_TREE;
12402   is_parenthesized_init = false;
12403   is_non_constant_init = true;
12404   if (is_initialized)
12405     {
12406       if (function_declarator_p (declarator))
12407         {
12408            if (initialization_kind == CPP_EQ)
12409              initializer = cp_parser_pure_specifier (parser);
12410            else
12411              {
12412                /* If the declaration was erroneous, we don't really
12413                   know what the user intended, so just silently
12414                   consume the initializer.  */
12415                if (decl != error_mark_node)
12416                  error ("initializer provided for function");
12417                cp_parser_skip_to_closing_parenthesis (parser,
12418                                                       /*recovering=*/true,
12419                                                       /*or_comma=*/false,
12420                                                       /*consume_paren=*/true);
12421              }
12422         }
12423       else
12424         initializer = cp_parser_initializer (parser,
12425                                              &is_parenthesized_init,
12426                                              &is_non_constant_init);
12427     }
12428
12429   /* The old parser allows attributes to appear after a parenthesized
12430      initializer.  Mark Mitchell proposed removing this functionality
12431      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12432      attributes -- but ignores them.  */
12433   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12434     if (cp_parser_attributes_opt (parser))
12435       warning (OPT_Wattributes,
12436                "attributes after parenthesized initializer ignored");
12437
12438   /* For an in-class declaration, use `grokfield' to create the
12439      declaration.  */
12440   if (member_p)
12441     {
12442       if (pushed_scope)
12443         {
12444           pop_scope (pushed_scope);
12445           pushed_scope = false;
12446         }
12447       decl = grokfield (declarator, decl_specifiers,
12448                         initializer, !is_non_constant_init,
12449                         /*asmspec=*/NULL_TREE,
12450                         prefix_attributes);
12451       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12452         cp_parser_save_default_args (parser, decl);
12453     }
12454
12455   /* Finish processing the declaration.  But, skip friend
12456      declarations.  */
12457   if (!friend_p && decl && decl != error_mark_node)
12458     {
12459       cp_finish_decl (decl,
12460                       initializer, !is_non_constant_init,
12461                       asm_specification,
12462                       /* If the initializer is in parentheses, then this is
12463                          a direct-initialization, which means that an
12464                          `explicit' constructor is OK.  Otherwise, an
12465                          `explicit' constructor cannot be used.  */
12466                       ((is_parenthesized_init || !is_initialized)
12467                      ? 0 : LOOKUP_ONLYCONVERTING));
12468     }
12469   else if ((cxx_dialect != cxx98) && friend_p
12470            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12471     /* Core issue #226 (C++0x only): A default template-argument
12472        shall not be specified in a friend class template
12473        declaration. */
12474     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12475                              /*is_partial=*/0, /*is_friend_decl=*/1);
12476
12477   if (!friend_p && pushed_scope)
12478     pop_scope (pushed_scope);
12479
12480   return decl;
12481 }
12482
12483 /* Parse a declarator.
12484
12485    declarator:
12486      direct-declarator
12487      ptr-operator declarator
12488
12489    abstract-declarator:
12490      ptr-operator abstract-declarator [opt]
12491      direct-abstract-declarator
12492
12493    GNU Extensions:
12494
12495    declarator:
12496      attributes [opt] direct-declarator
12497      attributes [opt] ptr-operator declarator
12498
12499    abstract-declarator:
12500      attributes [opt] ptr-operator abstract-declarator [opt]
12501      attributes [opt] direct-abstract-declarator
12502
12503    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12504    detect constructor, destructor or conversion operators. It is set
12505    to -1 if the declarator is a name, and +1 if it is a
12506    function. Otherwise it is set to zero. Usually you just want to
12507    test for >0, but internally the negative value is used.
12508
12509    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12510    a decl-specifier-seq unless it declares a constructor, destructor,
12511    or conversion.  It might seem that we could check this condition in
12512    semantic analysis, rather than parsing, but that makes it difficult
12513    to handle something like `f()'.  We want to notice that there are
12514    no decl-specifiers, and therefore realize that this is an
12515    expression, not a declaration.)
12516
12517    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12518    the declarator is a direct-declarator of the form "(...)".
12519
12520    MEMBER_P is true iff this declarator is a member-declarator.  */
12521
12522 static cp_declarator *
12523 cp_parser_declarator (cp_parser* parser,
12524                       cp_parser_declarator_kind dcl_kind,
12525                       int* ctor_dtor_or_conv_p,
12526                       bool* parenthesized_p,
12527                       bool member_p)
12528 {
12529   cp_token *token;
12530   cp_declarator *declarator;
12531   enum tree_code code;
12532   cp_cv_quals cv_quals;
12533   tree class_type;
12534   tree attributes = NULL_TREE;
12535
12536   /* Assume this is not a constructor, destructor, or type-conversion
12537      operator.  */
12538   if (ctor_dtor_or_conv_p)
12539     *ctor_dtor_or_conv_p = 0;
12540
12541   if (cp_parser_allow_gnu_extensions_p (parser))
12542     attributes = cp_parser_attributes_opt (parser);
12543
12544   /* Peek at the next token.  */
12545   token = cp_lexer_peek_token (parser->lexer);
12546
12547   /* Check for the ptr-operator production.  */
12548   cp_parser_parse_tentatively (parser);
12549   /* Parse the ptr-operator.  */
12550   code = cp_parser_ptr_operator (parser,
12551                                  &class_type,
12552                                  &cv_quals);
12553   /* If that worked, then we have a ptr-operator.  */
12554   if (cp_parser_parse_definitely (parser))
12555     {
12556       /* If a ptr-operator was found, then this declarator was not
12557          parenthesized.  */
12558       if (parenthesized_p)
12559         *parenthesized_p = true;
12560       /* The dependent declarator is optional if we are parsing an
12561          abstract-declarator.  */
12562       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12563         cp_parser_parse_tentatively (parser);
12564
12565       /* Parse the dependent declarator.  */
12566       declarator = cp_parser_declarator (parser, dcl_kind,
12567                                          /*ctor_dtor_or_conv_p=*/NULL,
12568                                          /*parenthesized_p=*/NULL,
12569                                          /*member_p=*/false);
12570
12571       /* If we are parsing an abstract-declarator, we must handle the
12572          case where the dependent declarator is absent.  */
12573       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12574           && !cp_parser_parse_definitely (parser))
12575         declarator = NULL;
12576
12577       declarator = cp_parser_make_indirect_declarator
12578         (code, class_type, cv_quals, declarator);
12579     }
12580   /* Everything else is a direct-declarator.  */
12581   else
12582     {
12583       if (parenthesized_p)
12584         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12585                                                    CPP_OPEN_PAREN);
12586       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12587                                                 ctor_dtor_or_conv_p,
12588                                                 member_p);
12589     }
12590
12591   if (attributes && declarator && declarator != cp_error_declarator)
12592     declarator->attributes = attributes;
12593
12594   return declarator;
12595 }
12596
12597 /* Parse a direct-declarator or direct-abstract-declarator.
12598
12599    direct-declarator:
12600      declarator-id
12601      direct-declarator ( parameter-declaration-clause )
12602        cv-qualifier-seq [opt]
12603        exception-specification [opt]
12604      direct-declarator [ constant-expression [opt] ]
12605      ( declarator )
12606
12607    direct-abstract-declarator:
12608      direct-abstract-declarator [opt]
12609        ( parameter-declaration-clause )
12610        cv-qualifier-seq [opt]
12611        exception-specification [opt]
12612      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12613      ( abstract-declarator )
12614
12615    Returns a representation of the declarator.  DCL_KIND is
12616    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12617    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12618    we are parsing a direct-declarator.  It is
12619    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12620    of ambiguity we prefer an abstract declarator, as per
12621    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12622    cp_parser_declarator.  */
12623
12624 static cp_declarator *
12625 cp_parser_direct_declarator (cp_parser* parser,
12626                              cp_parser_declarator_kind dcl_kind,
12627                              int* ctor_dtor_or_conv_p,
12628                              bool member_p)
12629 {
12630   cp_token *token;
12631   cp_declarator *declarator = NULL;
12632   tree scope = NULL_TREE;
12633   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12634   bool saved_in_declarator_p = parser->in_declarator_p;
12635   bool first = true;
12636   tree pushed_scope = NULL_TREE;
12637
12638   while (true)
12639     {
12640       /* Peek at the next token.  */
12641       token = cp_lexer_peek_token (parser->lexer);
12642       if (token->type == CPP_OPEN_PAREN)
12643         {
12644           /* This is either a parameter-declaration-clause, or a
12645              parenthesized declarator. When we know we are parsing a
12646              named declarator, it must be a parenthesized declarator
12647              if FIRST is true. For instance, `(int)' is a
12648              parameter-declaration-clause, with an omitted
12649              direct-abstract-declarator. But `((*))', is a
12650              parenthesized abstract declarator. Finally, when T is a
12651              template parameter `(T)' is a
12652              parameter-declaration-clause, and not a parenthesized
12653              named declarator.
12654
12655              We first try and parse a parameter-declaration-clause,
12656              and then try a nested declarator (if FIRST is true).
12657
12658              It is not an error for it not to be a
12659              parameter-declaration-clause, even when FIRST is
12660              false. Consider,
12661
12662                int i (int);
12663                int i (3);
12664
12665              The first is the declaration of a function while the
12666              second is a the definition of a variable, including its
12667              initializer.
12668
12669              Having seen only the parenthesis, we cannot know which of
12670              these two alternatives should be selected.  Even more
12671              complex are examples like:
12672
12673                int i (int (a));
12674                int i (int (3));
12675
12676              The former is a function-declaration; the latter is a
12677              variable initialization.
12678
12679              Thus again, we try a parameter-declaration-clause, and if
12680              that fails, we back out and return.  */
12681
12682           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12683             {
12684               cp_parameter_declarator *params;
12685               unsigned saved_num_template_parameter_lists;
12686
12687               /* In a member-declarator, the only valid interpretation
12688                  of a parenthesis is the start of a
12689                  parameter-declaration-clause.  (It is invalid to
12690                  initialize a static data member with a parenthesized
12691                  initializer; only the "=" form of initialization is
12692                  permitted.)  */
12693               if (!member_p)
12694                 cp_parser_parse_tentatively (parser);
12695
12696               /* Consume the `('.  */
12697               cp_lexer_consume_token (parser->lexer);
12698               if (first)
12699                 {
12700                   /* If this is going to be an abstract declarator, we're
12701                      in a declarator and we can't have default args.  */
12702                   parser->default_arg_ok_p = false;
12703                   parser->in_declarator_p = true;
12704                 }
12705
12706               /* Inside the function parameter list, surrounding
12707                  template-parameter-lists do not apply.  */
12708               saved_num_template_parameter_lists
12709                 = parser->num_template_parameter_lists;
12710               parser->num_template_parameter_lists = 0;
12711
12712               /* Parse the parameter-declaration-clause.  */
12713               params = cp_parser_parameter_declaration_clause (parser);
12714
12715               parser->num_template_parameter_lists
12716                 = saved_num_template_parameter_lists;
12717
12718               /* If all went well, parse the cv-qualifier-seq and the
12719                  exception-specification.  */
12720               if (member_p || cp_parser_parse_definitely (parser))
12721                 {
12722                   cp_cv_quals cv_quals;
12723                   tree exception_specification;
12724
12725                   if (ctor_dtor_or_conv_p)
12726                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12727                   first = false;
12728                   /* Consume the `)'.  */
12729                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
12730
12731                   /* Parse the cv-qualifier-seq.  */
12732                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12733                   /* And the exception-specification.  */
12734                   exception_specification
12735                     = cp_parser_exception_specification_opt (parser);
12736
12737                   /* Create the function-declarator.  */
12738                   declarator = make_call_declarator (declarator,
12739                                                      params,
12740                                                      cv_quals,
12741                                                      exception_specification);
12742                   /* Any subsequent parameter lists are to do with
12743                      return type, so are not those of the declared
12744                      function.  */
12745                   parser->default_arg_ok_p = false;
12746
12747                   /* Repeat the main loop.  */
12748                   continue;
12749                 }
12750             }
12751
12752           /* If this is the first, we can try a parenthesized
12753              declarator.  */
12754           if (first)
12755             {
12756               bool saved_in_type_id_in_expr_p;
12757
12758               parser->default_arg_ok_p = saved_default_arg_ok_p;
12759               parser->in_declarator_p = saved_in_declarator_p;
12760
12761               /* Consume the `('.  */
12762               cp_lexer_consume_token (parser->lexer);
12763               /* Parse the nested declarator.  */
12764               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12765               parser->in_type_id_in_expr_p = true;
12766               declarator
12767                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12768                                         /*parenthesized_p=*/NULL,
12769                                         member_p);
12770               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12771               first = false;
12772               /* Expect a `)'.  */
12773               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12774                 declarator = cp_error_declarator;
12775               if (declarator == cp_error_declarator)
12776                 break;
12777
12778               goto handle_declarator;
12779             }
12780           /* Otherwise, we must be done.  */
12781           else
12782             break;
12783         }
12784       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12785                && token->type == CPP_OPEN_SQUARE)
12786         {
12787           /* Parse an array-declarator.  */
12788           tree bounds;
12789
12790           if (ctor_dtor_or_conv_p)
12791             *ctor_dtor_or_conv_p = 0;
12792
12793           first = false;
12794           parser->default_arg_ok_p = false;
12795           parser->in_declarator_p = true;
12796           /* Consume the `['.  */
12797           cp_lexer_consume_token (parser->lexer);
12798           /* Peek at the next token.  */
12799           token = cp_lexer_peek_token (parser->lexer);
12800           /* If the next token is `]', then there is no
12801              constant-expression.  */
12802           if (token->type != CPP_CLOSE_SQUARE)
12803             {
12804               bool non_constant_p;
12805
12806               bounds
12807                 = cp_parser_constant_expression (parser,
12808                                                  /*allow_non_constant=*/true,
12809                                                  &non_constant_p);
12810               if (!non_constant_p)
12811                 bounds = fold_non_dependent_expr (bounds);
12812               /* Normally, the array bound must be an integral constant
12813                  expression.  However, as an extension, we allow VLAs
12814                  in function scopes.  */
12815               else if (!parser->in_function_body)
12816                 {
12817                   error ("array bound is not an integer constant");
12818                   bounds = error_mark_node;
12819                 }
12820             }
12821           else
12822             bounds = NULL_TREE;
12823           /* Look for the closing `]'.  */
12824           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
12825             {
12826               declarator = cp_error_declarator;
12827               break;
12828             }
12829
12830           declarator = make_array_declarator (declarator, bounds);
12831         }
12832       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12833         {
12834           tree qualifying_scope;
12835           tree unqualified_name;
12836           special_function_kind sfk;
12837           bool abstract_ok;
12838           bool pack_expansion_p = false;
12839
12840           /* Parse a declarator-id */
12841           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12842           if (abstract_ok)
12843             {
12844               cp_parser_parse_tentatively (parser);
12845
12846               /* If we see an ellipsis, we should be looking at a
12847                  parameter pack. */
12848               if (token->type == CPP_ELLIPSIS)
12849                 {
12850                   /* Consume the `...' */
12851                   cp_lexer_consume_token (parser->lexer);
12852
12853                   pack_expansion_p = true;
12854                 }
12855             }
12856
12857           unqualified_name
12858             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12859           qualifying_scope = parser->scope;
12860           if (abstract_ok)
12861             {
12862               bool okay = false;
12863
12864               if (!unqualified_name && pack_expansion_p)
12865                 {
12866                   /* Check whether an error occurred. */
12867                   okay = !cp_parser_error_occurred (parser);
12868
12869                   /* We already consumed the ellipsis to mark a
12870                      parameter pack, but we have no way to report it,
12871                      so abort the tentative parse. We will be exiting
12872                      immediately anyway. */
12873                   cp_parser_abort_tentative_parse (parser);
12874                 }
12875               else
12876                 okay = cp_parser_parse_definitely (parser);
12877
12878               if (!okay)
12879                 unqualified_name = error_mark_node;
12880               else if (unqualified_name
12881                        && (qualifying_scope
12882                            || (TREE_CODE (unqualified_name)
12883                                != IDENTIFIER_NODE)))
12884                 {
12885                   cp_parser_error (parser, "expected unqualified-id");
12886                   unqualified_name = error_mark_node;
12887                 }
12888             }
12889
12890           if (!unqualified_name)
12891             return NULL;
12892           if (unqualified_name == error_mark_node)
12893             {
12894               declarator = cp_error_declarator;
12895               pack_expansion_p = false;
12896               declarator->parameter_pack_p = false;
12897               break;
12898             }
12899
12900           if (qualifying_scope && at_namespace_scope_p ()
12901               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12902             {
12903               /* In the declaration of a member of a template class
12904                  outside of the class itself, the SCOPE will sometimes
12905                  be a TYPENAME_TYPE.  For example, given:
12906
12907                  template <typename T>
12908                  int S<T>::R::i = 3;
12909
12910                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12911                  this context, we must resolve S<T>::R to an ordinary
12912                  type, rather than a typename type.
12913
12914                  The reason we normally avoid resolving TYPENAME_TYPEs
12915                  is that a specialization of `S' might render
12916                  `S<T>::R' not a type.  However, if `S' is
12917                  specialized, then this `i' will not be used, so there
12918                  is no harm in resolving the types here.  */
12919               tree type;
12920
12921               /* Resolve the TYPENAME_TYPE.  */
12922               type = resolve_typename_type (qualifying_scope,
12923                                             /*only_current_p=*/false);
12924               /* If that failed, the declarator is invalid.  */
12925               if (TREE_CODE (type) == TYPENAME_TYPE)
12926                 error ("%<%T::%E%> is not a type",
12927                        TYPE_CONTEXT (qualifying_scope),
12928                        TYPE_IDENTIFIER (qualifying_scope));
12929               qualifying_scope = type;
12930             }
12931
12932           sfk = sfk_none;
12933
12934           if (unqualified_name)
12935             {
12936               tree class_type;
12937
12938               if (qualifying_scope
12939                   && CLASS_TYPE_P (qualifying_scope))
12940                 class_type = qualifying_scope;
12941               else
12942                 class_type = current_class_type;
12943
12944               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12945                 {
12946                   tree name_type = TREE_TYPE (unqualified_name);
12947                   if (class_type && same_type_p (name_type, class_type))
12948                     {
12949                       if (qualifying_scope
12950                           && CLASSTYPE_USE_TEMPLATE (name_type))
12951                         {
12952                           error ("invalid use of constructor as a template");
12953                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12954                                   "name the constructor in a qualified name",
12955                                   class_type,
12956                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12957                                   class_type, name_type);
12958                           declarator = cp_error_declarator;
12959                           break;
12960                         }
12961                       else
12962                         unqualified_name = constructor_name (class_type);
12963                     }
12964                   else
12965                     {
12966                       /* We do not attempt to print the declarator
12967                          here because we do not have enough
12968                          information about its original syntactic
12969                          form.  */
12970                       cp_parser_error (parser, "invalid declarator");
12971                       declarator = cp_error_declarator;
12972                       break;
12973                     }
12974                 }
12975
12976               if (class_type)
12977                 {
12978                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12979                     sfk = sfk_destructor;
12980                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12981                     sfk = sfk_conversion;
12982                   else if (/* There's no way to declare a constructor
12983                               for an anonymous type, even if the type
12984                               got a name for linkage purposes.  */
12985                            !TYPE_WAS_ANONYMOUS (class_type)
12986                            && constructor_name_p (unqualified_name,
12987                                                   class_type))
12988                     {
12989                       unqualified_name = constructor_name (class_type);
12990                       sfk = sfk_constructor;
12991                     }
12992
12993                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12994                     *ctor_dtor_or_conv_p = -1;
12995                 }
12996             }
12997           declarator = make_id_declarator (qualifying_scope,
12998                                            unqualified_name,
12999                                            sfk);
13000           declarator->id_loc = token->location;
13001           declarator->parameter_pack_p = pack_expansion_p;
13002
13003           if (pack_expansion_p)
13004             maybe_warn_variadic_templates ();
13005
13006         handle_declarator:;
13007           scope = get_scope_of_declarator (declarator);
13008           if (scope)
13009             /* Any names that appear after the declarator-id for a
13010                member are looked up in the containing scope.  */
13011             pushed_scope = push_scope (scope);
13012           parser->in_declarator_p = true;
13013           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13014               || (declarator && declarator->kind == cdk_id))
13015             /* Default args are only allowed on function
13016                declarations.  */
13017             parser->default_arg_ok_p = saved_default_arg_ok_p;
13018           else
13019             parser->default_arg_ok_p = false;
13020
13021           first = false;
13022         }
13023       /* We're done.  */
13024       else
13025         break;
13026     }
13027
13028   /* For an abstract declarator, we might wind up with nothing at this
13029      point.  That's an error; the declarator is not optional.  */
13030   if (!declarator)
13031     cp_parser_error (parser, "expected declarator");
13032
13033   /* If we entered a scope, we must exit it now.  */
13034   if (pushed_scope)
13035     pop_scope (pushed_scope);
13036
13037   parser->default_arg_ok_p = saved_default_arg_ok_p;
13038   parser->in_declarator_p = saved_in_declarator_p;
13039
13040   return declarator;
13041 }
13042
13043 /* Parse a ptr-operator.
13044
13045    ptr-operator:
13046      * cv-qualifier-seq [opt]
13047      &
13048      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13049
13050    GNU Extension:
13051
13052    ptr-operator:
13053      & cv-qualifier-seq [opt]
13054
13055    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13056    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13057    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13058    filled in with the TYPE containing the member.  *CV_QUALS is
13059    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13060    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13061    Note that the tree codes returned by this function have nothing
13062    to do with the types of trees that will be eventually be created
13063    to represent the pointer or reference type being parsed. They are
13064    just constants with suggestive names. */
13065 static enum tree_code
13066 cp_parser_ptr_operator (cp_parser* parser,
13067                         tree* type,
13068                         cp_cv_quals *cv_quals)
13069 {
13070   enum tree_code code = ERROR_MARK;
13071   cp_token *token;
13072
13073   /* Assume that it's not a pointer-to-member.  */
13074   *type = NULL_TREE;
13075   /* And that there are no cv-qualifiers.  */
13076   *cv_quals = TYPE_UNQUALIFIED;
13077
13078   /* Peek at the next token.  */
13079   token = cp_lexer_peek_token (parser->lexer);
13080
13081   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13082   if (token->type == CPP_MULT)
13083     code = INDIRECT_REF;
13084   else if (token->type == CPP_AND)
13085     code = ADDR_EXPR;
13086   else if ((cxx_dialect != cxx98) &&
13087            token->type == CPP_AND_AND) /* C++0x only */
13088     code = NON_LVALUE_EXPR;
13089
13090   if (code != ERROR_MARK)
13091     {
13092       /* Consume the `*', `&' or `&&'.  */
13093       cp_lexer_consume_token (parser->lexer);
13094
13095       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13096          `&', if we are allowing GNU extensions.  (The only qualifier
13097          that can legally appear after `&' is `restrict', but that is
13098          enforced during semantic analysis.  */
13099       if (code == INDIRECT_REF
13100           || cp_parser_allow_gnu_extensions_p (parser))
13101         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13102     }
13103   else
13104     {
13105       /* Try the pointer-to-member case.  */
13106       cp_parser_parse_tentatively (parser);
13107       /* Look for the optional `::' operator.  */
13108       cp_parser_global_scope_opt (parser,
13109                                   /*current_scope_valid_p=*/false);
13110       /* Look for the nested-name specifier.  */
13111       cp_parser_nested_name_specifier (parser,
13112                                        /*typename_keyword_p=*/false,
13113                                        /*check_dependency_p=*/true,
13114                                        /*type_p=*/false,
13115                                        /*is_declaration=*/false);
13116       /* If we found it, and the next token is a `*', then we are
13117          indeed looking at a pointer-to-member operator.  */
13118       if (!cp_parser_error_occurred (parser)
13119           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13120         {
13121           /* Indicate that the `*' operator was used.  */
13122           code = INDIRECT_REF;
13123
13124           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13125             error ("%qD is a namespace", parser->scope);
13126           else
13127             {
13128               /* The type of which the member is a member is given by the
13129                  current SCOPE.  */
13130               *type = parser->scope;
13131               /* The next name will not be qualified.  */
13132               parser->scope = NULL_TREE;
13133               parser->qualifying_scope = NULL_TREE;
13134               parser->object_scope = NULL_TREE;
13135               /* Look for the optional cv-qualifier-seq.  */
13136               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13137             }
13138         }
13139       /* If that didn't work we don't have a ptr-operator.  */
13140       if (!cp_parser_parse_definitely (parser))
13141         cp_parser_error (parser, "expected ptr-operator");
13142     }
13143
13144   return code;
13145 }
13146
13147 /* Parse an (optional) cv-qualifier-seq.
13148
13149    cv-qualifier-seq:
13150      cv-qualifier cv-qualifier-seq [opt]
13151
13152    cv-qualifier:
13153      const
13154      volatile
13155
13156    GNU Extension:
13157
13158    cv-qualifier:
13159      __restrict__
13160
13161    Returns a bitmask representing the cv-qualifiers.  */
13162
13163 static cp_cv_quals
13164 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13165 {
13166   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13167
13168   while (true)
13169     {
13170       cp_token *token;
13171       cp_cv_quals cv_qualifier;
13172
13173       /* Peek at the next token.  */
13174       token = cp_lexer_peek_token (parser->lexer);
13175       /* See if it's a cv-qualifier.  */
13176       switch (token->keyword)
13177         {
13178         case RID_CONST:
13179           cv_qualifier = TYPE_QUAL_CONST;
13180           break;
13181
13182         case RID_VOLATILE:
13183           cv_qualifier = TYPE_QUAL_VOLATILE;
13184           break;
13185
13186         case RID_RESTRICT:
13187           cv_qualifier = TYPE_QUAL_RESTRICT;
13188           break;
13189
13190         default:
13191           cv_qualifier = TYPE_UNQUALIFIED;
13192           break;
13193         }
13194
13195       if (!cv_qualifier)
13196         break;
13197
13198       if (cv_quals & cv_qualifier)
13199         {
13200           error ("duplicate cv-qualifier");
13201           cp_lexer_purge_token (parser->lexer);
13202         }
13203       else
13204         {
13205           cp_lexer_consume_token (parser->lexer);
13206           cv_quals |= cv_qualifier;
13207         }
13208     }
13209
13210   return cv_quals;
13211 }
13212
13213 /* Parse a declarator-id.
13214
13215    declarator-id:
13216      id-expression
13217      :: [opt] nested-name-specifier [opt] type-name
13218
13219    In the `id-expression' case, the value returned is as for
13220    cp_parser_id_expression if the id-expression was an unqualified-id.
13221    If the id-expression was a qualified-id, then a SCOPE_REF is
13222    returned.  The first operand is the scope (either a NAMESPACE_DECL
13223    or TREE_TYPE), but the second is still just a representation of an
13224    unqualified-id.  */
13225
13226 static tree
13227 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13228 {
13229   tree id;
13230   /* The expression must be an id-expression.  Assume that qualified
13231      names are the names of types so that:
13232
13233        template <class T>
13234        int S<T>::R::i = 3;
13235
13236      will work; we must treat `S<T>::R' as the name of a type.
13237      Similarly, assume that qualified names are templates, where
13238      required, so that:
13239
13240        template <class T>
13241        int S<T>::R<T>::i = 3;
13242
13243      will work, too.  */
13244   id = cp_parser_id_expression (parser,
13245                                 /*template_keyword_p=*/false,
13246                                 /*check_dependency_p=*/false,
13247                                 /*template_p=*/NULL,
13248                                 /*declarator_p=*/true,
13249                                 optional_p);
13250   if (id && BASELINK_P (id))
13251     id = BASELINK_FUNCTIONS (id);
13252   return id;
13253 }
13254
13255 /* Parse a type-id.
13256
13257    type-id:
13258      type-specifier-seq abstract-declarator [opt]
13259
13260    Returns the TYPE specified.  */
13261
13262 static tree
13263 cp_parser_type_id (cp_parser* parser)
13264 {
13265   cp_decl_specifier_seq type_specifier_seq;
13266   cp_declarator *abstract_declarator;
13267
13268   /* Parse the type-specifier-seq.  */
13269   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13270                                 &type_specifier_seq);
13271   if (type_specifier_seq.type == error_mark_node)
13272     return error_mark_node;
13273
13274   /* There might or might not be an abstract declarator.  */
13275   cp_parser_parse_tentatively (parser);
13276   /* Look for the declarator.  */
13277   abstract_declarator
13278     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13279                             /*parenthesized_p=*/NULL,
13280                             /*member_p=*/false);
13281   /* Check to see if there really was a declarator.  */
13282   if (!cp_parser_parse_definitely (parser))
13283     abstract_declarator = NULL;
13284
13285   return groktypename (&type_specifier_seq, abstract_declarator);
13286 }
13287
13288 /* Parse a type-specifier-seq.
13289
13290    type-specifier-seq:
13291      type-specifier type-specifier-seq [opt]
13292
13293    GNU extension:
13294
13295    type-specifier-seq:
13296      attributes type-specifier-seq [opt]
13297
13298    If IS_CONDITION is true, we are at the start of a "condition",
13299    e.g., we've just seen "if (".
13300
13301    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13302
13303 static void
13304 cp_parser_type_specifier_seq (cp_parser* parser,
13305                               bool is_condition,
13306                               cp_decl_specifier_seq *type_specifier_seq)
13307 {
13308   bool seen_type_specifier = false;
13309   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13310
13311   /* Clear the TYPE_SPECIFIER_SEQ.  */
13312   clear_decl_specs (type_specifier_seq);
13313
13314   /* Parse the type-specifiers and attributes.  */
13315   while (true)
13316     {
13317       tree type_specifier;
13318       bool is_cv_qualifier;
13319
13320       /* Check for attributes first.  */
13321       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13322         {
13323           type_specifier_seq->attributes =
13324             chainon (type_specifier_seq->attributes,
13325                      cp_parser_attributes_opt (parser));
13326           continue;
13327         }
13328
13329       /* Look for the type-specifier.  */
13330       type_specifier = cp_parser_type_specifier (parser,
13331                                                  flags,
13332                                                  type_specifier_seq,
13333                                                  /*is_declaration=*/false,
13334                                                  NULL,
13335                                                  &is_cv_qualifier);
13336       if (!type_specifier)
13337         {
13338           /* If the first type-specifier could not be found, this is not a
13339              type-specifier-seq at all.  */
13340           if (!seen_type_specifier)
13341             {
13342               cp_parser_error (parser, "expected type-specifier");
13343               type_specifier_seq->type = error_mark_node;
13344               return;
13345             }
13346           /* If subsequent type-specifiers could not be found, the
13347              type-specifier-seq is complete.  */
13348           break;
13349         }
13350
13351       seen_type_specifier = true;
13352       /* The standard says that a condition can be:
13353
13354             type-specifier-seq declarator = assignment-expression
13355
13356          However, given:
13357
13358            struct S {};
13359            if (int S = ...)
13360
13361          we should treat the "S" as a declarator, not as a
13362          type-specifier.  The standard doesn't say that explicitly for
13363          type-specifier-seq, but it does say that for
13364          decl-specifier-seq in an ordinary declaration.  Perhaps it
13365          would be clearer just to allow a decl-specifier-seq here, and
13366          then add a semantic restriction that if any decl-specifiers
13367          that are not type-specifiers appear, the program is invalid.  */
13368       if (is_condition && !is_cv_qualifier)
13369         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13370     }
13371
13372   cp_parser_check_decl_spec (type_specifier_seq);
13373 }
13374
13375 /* Parse a parameter-declaration-clause.
13376
13377    parameter-declaration-clause:
13378      parameter-declaration-list [opt] ... [opt]
13379      parameter-declaration-list , ...
13380
13381    Returns a representation for the parameter declarations.  A return
13382    value of NULL indicates a parameter-declaration-clause consisting
13383    only of an ellipsis.  */
13384
13385 static cp_parameter_declarator *
13386 cp_parser_parameter_declaration_clause (cp_parser* parser)
13387 {
13388   cp_parameter_declarator *parameters;
13389   cp_token *token;
13390   bool ellipsis_p;
13391   bool is_error;
13392
13393   /* Peek at the next token.  */
13394   token = cp_lexer_peek_token (parser->lexer);
13395   /* Check for trivial parameter-declaration-clauses.  */
13396   if (token->type == CPP_ELLIPSIS)
13397     {
13398       /* Consume the `...' token.  */
13399       cp_lexer_consume_token (parser->lexer);
13400       return NULL;
13401     }
13402   else if (token->type == CPP_CLOSE_PAREN)
13403     /* There are no parameters.  */
13404     {
13405 #ifndef NO_IMPLICIT_EXTERN_C
13406       if (in_system_header && current_class_type == NULL
13407           && current_lang_name == lang_name_c)
13408         return NULL;
13409       else
13410 #endif
13411         return no_parameters;
13412     }
13413   /* Check for `(void)', too, which is a special case.  */
13414   else if (token->keyword == RID_VOID
13415            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13416                == CPP_CLOSE_PAREN))
13417     {
13418       /* Consume the `void' token.  */
13419       cp_lexer_consume_token (parser->lexer);
13420       /* There are no parameters.  */
13421       return no_parameters;
13422     }
13423
13424   /* Parse the parameter-declaration-list.  */
13425   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13426   /* If a parse error occurred while parsing the
13427      parameter-declaration-list, then the entire
13428      parameter-declaration-clause is erroneous.  */
13429   if (is_error)
13430     return NULL;
13431
13432   /* Peek at the next token.  */
13433   token = cp_lexer_peek_token (parser->lexer);
13434   /* If it's a `,', the clause should terminate with an ellipsis.  */
13435   if (token->type == CPP_COMMA)
13436     {
13437       /* Consume the `,'.  */
13438       cp_lexer_consume_token (parser->lexer);
13439       /* Expect an ellipsis.  */
13440       ellipsis_p
13441         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13442     }
13443   /* It might also be `...' if the optional trailing `,' was
13444      omitted.  */
13445   else if (token->type == CPP_ELLIPSIS)
13446     {
13447       /* Consume the `...' token.  */
13448       cp_lexer_consume_token (parser->lexer);
13449       /* And remember that we saw it.  */
13450       ellipsis_p = true;
13451     }
13452   else
13453     ellipsis_p = false;
13454
13455   /* Finish the parameter list.  */
13456   if (parameters && ellipsis_p)
13457     parameters->ellipsis_p = true;
13458
13459   return parameters;
13460 }
13461
13462 /* Parse a parameter-declaration-list.
13463
13464    parameter-declaration-list:
13465      parameter-declaration
13466      parameter-declaration-list , parameter-declaration
13467
13468    Returns a representation of the parameter-declaration-list, as for
13469    cp_parser_parameter_declaration_clause.  However, the
13470    `void_list_node' is never appended to the list.  Upon return,
13471    *IS_ERROR will be true iff an error occurred.  */
13472
13473 static cp_parameter_declarator *
13474 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13475 {
13476   cp_parameter_declarator *parameters = NULL;
13477   cp_parameter_declarator **tail = &parameters;
13478   bool saved_in_unbraced_linkage_specification_p;
13479
13480   /* Assume all will go well.  */
13481   *is_error = false;
13482   /* The special considerations that apply to a function within an
13483      unbraced linkage specifications do not apply to the parameters
13484      to the function.  */
13485   saved_in_unbraced_linkage_specification_p 
13486     = parser->in_unbraced_linkage_specification_p;
13487   parser->in_unbraced_linkage_specification_p = false;
13488
13489   /* Look for more parameters.  */
13490   while (true)
13491     {
13492       cp_parameter_declarator *parameter;
13493       bool parenthesized_p;
13494       /* Parse the parameter.  */
13495       parameter
13496         = cp_parser_parameter_declaration (parser,
13497                                            /*template_parm_p=*/false,
13498                                            &parenthesized_p);
13499
13500       /* If a parse error occurred parsing the parameter declaration,
13501          then the entire parameter-declaration-list is erroneous.  */
13502       if (!parameter)
13503         {
13504           *is_error = true;
13505           parameters = NULL;
13506           break;
13507         }
13508       /* Add the new parameter to the list.  */
13509       *tail = parameter;
13510       tail = &parameter->next;
13511
13512       /* Peek at the next token.  */
13513       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13514           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13515           /* These are for Objective-C++ */
13516           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13517           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13518         /* The parameter-declaration-list is complete.  */
13519         break;
13520       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13521         {
13522           cp_token *token;
13523
13524           /* Peek at the next token.  */
13525           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13526           /* If it's an ellipsis, then the list is complete.  */
13527           if (token->type == CPP_ELLIPSIS)
13528             break;
13529           /* Otherwise, there must be more parameters.  Consume the
13530              `,'.  */
13531           cp_lexer_consume_token (parser->lexer);
13532           /* When parsing something like:
13533
13534                 int i(float f, double d)
13535
13536              we can tell after seeing the declaration for "f" that we
13537              are not looking at an initialization of a variable "i",
13538              but rather at the declaration of a function "i".
13539
13540              Due to the fact that the parsing of template arguments
13541              (as specified to a template-id) requires backtracking we
13542              cannot use this technique when inside a template argument
13543              list.  */
13544           if (!parser->in_template_argument_list_p
13545               && !parser->in_type_id_in_expr_p
13546               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13547               /* However, a parameter-declaration of the form
13548                  "foat(f)" (which is a valid declaration of a
13549                  parameter "f") can also be interpreted as an
13550                  expression (the conversion of "f" to "float").  */
13551               && !parenthesized_p)
13552             cp_parser_commit_to_tentative_parse (parser);
13553         }
13554       else
13555         {
13556           cp_parser_error (parser, "expected %<,%> or %<...%>");
13557           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13558             cp_parser_skip_to_closing_parenthesis (parser,
13559                                                    /*recovering=*/true,
13560                                                    /*or_comma=*/false,
13561                                                    /*consume_paren=*/false);
13562           break;
13563         }
13564     }
13565
13566   parser->in_unbraced_linkage_specification_p
13567     = saved_in_unbraced_linkage_specification_p;
13568
13569   return parameters;
13570 }
13571
13572 /* Parse a parameter declaration.
13573
13574    parameter-declaration:
13575      decl-specifier-seq ... [opt] declarator
13576      decl-specifier-seq declarator = assignment-expression
13577      decl-specifier-seq ... [opt] abstract-declarator [opt]
13578      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13579
13580    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13581    declares a template parameter.  (In that case, a non-nested `>'
13582    token encountered during the parsing of the assignment-expression
13583    is not interpreted as a greater-than operator.)
13584
13585    Returns a representation of the parameter, or NULL if an error
13586    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13587    true iff the declarator is of the form "(p)".  */
13588
13589 static cp_parameter_declarator *
13590 cp_parser_parameter_declaration (cp_parser *parser,
13591                                  bool template_parm_p,
13592                                  bool *parenthesized_p)
13593 {
13594   int declares_class_or_enum;
13595   bool greater_than_is_operator_p;
13596   cp_decl_specifier_seq decl_specifiers;
13597   cp_declarator *declarator;
13598   tree default_argument;
13599   cp_token *token;
13600   const char *saved_message;
13601
13602   /* In a template parameter, `>' is not an operator.
13603
13604      [temp.param]
13605
13606      When parsing a default template-argument for a non-type
13607      template-parameter, the first non-nested `>' is taken as the end
13608      of the template parameter-list rather than a greater-than
13609      operator.  */
13610   greater_than_is_operator_p = !template_parm_p;
13611
13612   /* Type definitions may not appear in parameter types.  */
13613   saved_message = parser->type_definition_forbidden_message;
13614   parser->type_definition_forbidden_message
13615     = "types may not be defined in parameter types";
13616
13617   /* Parse the declaration-specifiers.  */
13618   cp_parser_decl_specifier_seq (parser,
13619                                 CP_PARSER_FLAGS_NONE,
13620                                 &decl_specifiers,
13621                                 &declares_class_or_enum);
13622   /* If an error occurred, there's no reason to attempt to parse the
13623      rest of the declaration.  */
13624   if (cp_parser_error_occurred (parser))
13625     {
13626       parser->type_definition_forbidden_message = saved_message;
13627       return NULL;
13628     }
13629
13630   /* Peek at the next token.  */
13631   token = cp_lexer_peek_token (parser->lexer);
13632
13633   /* If the next token is a `)', `,', `=', `>', or `...', then there
13634      is no declarator. However, when variadic templates are enabled,
13635      there may be a declarator following `...'.  */
13636   if (token->type == CPP_CLOSE_PAREN
13637       || token->type == CPP_COMMA
13638       || token->type == CPP_EQ
13639       || token->type == CPP_GREATER)
13640     {
13641       declarator = NULL;
13642       if (parenthesized_p)
13643         *parenthesized_p = false;
13644     }
13645   /* Otherwise, there should be a declarator.  */
13646   else
13647     {
13648       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13649       parser->default_arg_ok_p = false;
13650
13651       /* After seeing a decl-specifier-seq, if the next token is not a
13652          "(", there is no possibility that the code is a valid
13653          expression.  Therefore, if parsing tentatively, we commit at
13654          this point.  */
13655       if (!parser->in_template_argument_list_p
13656           /* In an expression context, having seen:
13657
13658                (int((char ...
13659
13660              we cannot be sure whether we are looking at a
13661              function-type (taking a "char" as a parameter) or a cast
13662              of some object of type "char" to "int".  */
13663           && !parser->in_type_id_in_expr_p
13664           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13665           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13666         cp_parser_commit_to_tentative_parse (parser);
13667       /* Parse the declarator.  */
13668       declarator = cp_parser_declarator (parser,
13669                                          CP_PARSER_DECLARATOR_EITHER,
13670                                          /*ctor_dtor_or_conv_p=*/NULL,
13671                                          parenthesized_p,
13672                                          /*member_p=*/false);
13673       parser->default_arg_ok_p = saved_default_arg_ok_p;
13674       /* After the declarator, allow more attributes.  */
13675       decl_specifiers.attributes
13676         = chainon (decl_specifiers.attributes,
13677                    cp_parser_attributes_opt (parser));
13678     }
13679
13680   /* If the next token is an ellipsis, and we have not seen a
13681      declarator name, and the type of the declarator contains parameter
13682      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13683      a parameter pack expansion expression. Otherwise, leave the
13684      ellipsis for a C-style variadic function. */
13685   token = cp_lexer_peek_token (parser->lexer);
13686   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13687     {
13688       tree type = decl_specifiers.type;
13689
13690       if (type && DECL_P (type))
13691         type = TREE_TYPE (type);
13692
13693       if (type
13694           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13695           && declarator_can_be_parameter_pack (declarator)
13696           && (!declarator || !declarator->parameter_pack_p)
13697           && uses_parameter_packs (type))
13698         {
13699           /* Consume the `...'. */
13700           cp_lexer_consume_token (parser->lexer);
13701           maybe_warn_variadic_templates ();
13702           
13703           /* Build a pack expansion type */
13704           if (declarator)
13705             declarator->parameter_pack_p = true;
13706           else
13707             decl_specifiers.type = make_pack_expansion (type);
13708         }
13709     }
13710
13711   /* The restriction on defining new types applies only to the type
13712      of the parameter, not to the default argument.  */
13713   parser->type_definition_forbidden_message = saved_message;
13714
13715   /* If the next token is `=', then process a default argument.  */
13716   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13717     {
13718       /* Consume the `='.  */
13719       cp_lexer_consume_token (parser->lexer);
13720
13721       /* If we are defining a class, then the tokens that make up the
13722          default argument must be saved and processed later.  */
13723       if (!template_parm_p && at_class_scope_p ()
13724           && TYPE_BEING_DEFINED (current_class_type))
13725         {
13726           unsigned depth = 0;
13727           int maybe_template_id = 0;
13728           cp_token *first_token;
13729           cp_token *token;
13730
13731           /* Add tokens until we have processed the entire default
13732              argument.  We add the range [first_token, token).  */
13733           first_token = cp_lexer_peek_token (parser->lexer);
13734           while (true)
13735             {
13736               bool done = false;
13737
13738               /* Peek at the next token.  */
13739               token = cp_lexer_peek_token (parser->lexer);
13740               /* What we do depends on what token we have.  */
13741               switch (token->type)
13742                 {
13743                   /* In valid code, a default argument must be
13744                      immediately followed by a `,' `)', or `...'.  */
13745                 case CPP_COMMA:
13746                   if (depth == 0 && maybe_template_id)
13747                     {
13748                       /* If we've seen a '<', we might be in a
13749                          template-argument-list.  Until Core issue 325 is
13750                          resolved, we don't know how this situation ought
13751                          to be handled, so try to DTRT.  We check whether
13752                          what comes after the comma is a valid parameter
13753                          declaration list.  If it is, then the comma ends
13754                          the default argument; otherwise the default
13755                          argument continues.  */
13756                       bool error = false;
13757
13758                       /* Set ITALP so cp_parser_parameter_declaration_list
13759                          doesn't decide to commit to this parse.  */
13760                       bool saved_italp = parser->in_template_argument_list_p;
13761                       parser->in_template_argument_list_p = true;
13762
13763                       cp_parser_parse_tentatively (parser);
13764                       cp_lexer_consume_token (parser->lexer);
13765                       cp_parser_parameter_declaration_list (parser, &error);
13766                       if (!cp_parser_error_occurred (parser) && !error)
13767                         done = true;
13768                       cp_parser_abort_tentative_parse (parser);
13769
13770                       parser->in_template_argument_list_p = saved_italp;
13771                       break;
13772                     }
13773                 case CPP_CLOSE_PAREN:
13774                 case CPP_ELLIPSIS:
13775                   /* If we run into a non-nested `;', `}', or `]',
13776                      then the code is invalid -- but the default
13777                      argument is certainly over.  */
13778                 case CPP_SEMICOLON:
13779                 case CPP_CLOSE_BRACE:
13780                 case CPP_CLOSE_SQUARE:
13781                   if (depth == 0)
13782                     done = true;
13783                   /* Update DEPTH, if necessary.  */
13784                   else if (token->type == CPP_CLOSE_PAREN
13785                            || token->type == CPP_CLOSE_BRACE
13786                            || token->type == CPP_CLOSE_SQUARE)
13787                     --depth;
13788                   break;
13789
13790                 case CPP_OPEN_PAREN:
13791                 case CPP_OPEN_SQUARE:
13792                 case CPP_OPEN_BRACE:
13793                   ++depth;
13794                   break;
13795
13796                 case CPP_LESS:
13797                   if (depth == 0)
13798                     /* This might be the comparison operator, or it might
13799                        start a template argument list.  */
13800                     ++maybe_template_id;
13801                   break;
13802
13803                 case CPP_RSHIFT:
13804                   if (cxx_dialect == cxx98)
13805                     break;
13806                   /* Fall through for C++0x, which treats the `>>'
13807                      operator like two `>' tokens in certain
13808                      cases.  */
13809
13810                 case CPP_GREATER:
13811                   if (depth == 0)
13812                     {
13813                       /* This might be an operator, or it might close a
13814                          template argument list.  But if a previous '<'
13815                          started a template argument list, this will have
13816                          closed it, so we can't be in one anymore.  */
13817                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
13818                       if (maybe_template_id < 0)
13819                         maybe_template_id = 0;
13820                     }
13821                   break;
13822
13823                   /* If we run out of tokens, issue an error message.  */
13824                 case CPP_EOF:
13825                 case CPP_PRAGMA_EOL:
13826                   error ("file ends in default argument");
13827                   done = true;
13828                   break;
13829
13830                 case CPP_NAME:
13831                 case CPP_SCOPE:
13832                   /* In these cases, we should look for template-ids.
13833                      For example, if the default argument is
13834                      `X<int, double>()', we need to do name lookup to
13835                      figure out whether or not `X' is a template; if
13836                      so, the `,' does not end the default argument.
13837
13838                      That is not yet done.  */
13839                   break;
13840
13841                 default:
13842                   break;
13843                 }
13844
13845               /* If we've reached the end, stop.  */
13846               if (done)
13847                 break;
13848
13849               /* Add the token to the token block.  */
13850               token = cp_lexer_consume_token (parser->lexer);
13851             }
13852
13853           /* Create a DEFAULT_ARG to represent the unparsed default
13854              argument.  */
13855           default_argument = make_node (DEFAULT_ARG);
13856           DEFARG_TOKENS (default_argument)
13857             = cp_token_cache_new (first_token, token);
13858           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13859         }
13860       /* Outside of a class definition, we can just parse the
13861          assignment-expression.  */
13862       else
13863         default_argument 
13864           = cp_parser_default_argument (parser, template_parm_p);
13865
13866       if (!parser->default_arg_ok_p)
13867         {
13868           if (!flag_pedantic_errors)
13869             warning (0, "deprecated use of default argument for parameter of non-function");
13870           else
13871             {
13872               error ("default arguments are only permitted for function parameters");
13873               default_argument = NULL_TREE;
13874             }
13875         }
13876       else if ((declarator && declarator->parameter_pack_p)
13877                || (decl_specifiers.type
13878                    && PACK_EXPANSION_P (decl_specifiers.type)))
13879         {
13880           const char* kind = template_parm_p? "template " : "";
13881           
13882           /* Find the name of the parameter pack.  */     
13883           cp_declarator *id_declarator = declarator;
13884           while (id_declarator && id_declarator->kind != cdk_id)
13885             id_declarator = id_declarator->declarator;
13886           
13887           if (id_declarator && id_declarator->kind == cdk_id)
13888             error ("%sparameter pack %qD cannot have a default argument",
13889                    kind, id_declarator->u.id.unqualified_name);
13890           else
13891             error ("%sparameter pack cannot have a default argument",
13892                    kind);
13893           
13894           default_argument = NULL_TREE;
13895         }
13896     }
13897   else
13898     default_argument = NULL_TREE;
13899
13900   return make_parameter_declarator (&decl_specifiers,
13901                                     declarator,
13902                                     default_argument);
13903 }
13904
13905 /* Parse a default argument and return it.
13906
13907    TEMPLATE_PARM_P is true if this is a default argument for a
13908    non-type template parameter.  */
13909 static tree
13910 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13911 {
13912   tree default_argument = NULL_TREE;
13913   bool saved_greater_than_is_operator_p;
13914   bool saved_local_variables_forbidden_p;
13915
13916   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13917      set correctly.  */
13918   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13919   parser->greater_than_is_operator_p = !template_parm_p;
13920   /* Local variable names (and the `this' keyword) may not
13921      appear in a default argument.  */
13922   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13923   parser->local_variables_forbidden_p = true;
13924   /* The default argument expression may cause implicitly
13925      defined member functions to be synthesized, which will
13926      result in garbage collection.  We must treat this
13927      situation as if we were within the body of function so as
13928      to avoid collecting live data on the stack.  */
13929   ++function_depth;
13930   /* Parse the assignment-expression.  */
13931   if (template_parm_p)
13932     push_deferring_access_checks (dk_no_deferred);
13933   default_argument
13934     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13935   if (template_parm_p)
13936     pop_deferring_access_checks ();
13937   /* Restore saved state.  */
13938   --function_depth;
13939   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13940   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13941
13942   return default_argument;
13943 }
13944
13945 /* Parse a function-body.
13946
13947    function-body:
13948      compound_statement  */
13949
13950 static void
13951 cp_parser_function_body (cp_parser *parser)
13952 {
13953   cp_parser_compound_statement (parser, NULL, false);
13954 }
13955
13956 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13957    true if a ctor-initializer was present.  */
13958
13959 static bool
13960 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13961 {
13962   tree body;
13963   bool ctor_initializer_p;
13964
13965   /* Begin the function body.  */
13966   body = begin_function_body ();
13967   /* Parse the optional ctor-initializer.  */
13968   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13969   /* Parse the function-body.  */
13970   cp_parser_function_body (parser);
13971   /* Finish the function body.  */
13972   finish_function_body (body);
13973
13974   return ctor_initializer_p;
13975 }
13976
13977 /* Parse an initializer.
13978
13979    initializer:
13980      = initializer-clause
13981      ( expression-list )
13982
13983    Returns an expression representing the initializer.  If no
13984    initializer is present, NULL_TREE is returned.
13985
13986    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13987    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13988    set to FALSE if there is no initializer present.  If there is an
13989    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13990    is set to true; otherwise it is set to false.  */
13991
13992 static tree
13993 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13994                        bool* non_constant_p)
13995 {
13996   cp_token *token;
13997   tree init;
13998
13999   /* Peek at the next token.  */
14000   token = cp_lexer_peek_token (parser->lexer);
14001
14002   /* Let our caller know whether or not this initializer was
14003      parenthesized.  */
14004   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
14005   /* Assume that the initializer is constant.  */
14006   *non_constant_p = false;
14007
14008   if (token->type == CPP_EQ)
14009     {
14010       /* Consume the `='.  */
14011       cp_lexer_consume_token (parser->lexer);
14012       /* Parse the initializer-clause.  */
14013       init = cp_parser_initializer_clause (parser, non_constant_p);
14014     }
14015   else if (token->type == CPP_OPEN_PAREN)
14016     init = cp_parser_parenthesized_expression_list (parser, false,
14017                                                     /*cast_p=*/false,
14018                                                     /*allow_expansion_p=*/true,
14019                                                     non_constant_p);
14020   else
14021     {
14022       /* Anything else is an error.  */
14023       cp_parser_error (parser, "expected initializer");
14024       init = error_mark_node;
14025     }
14026
14027   return init;
14028 }
14029
14030 /* Parse an initializer-clause.
14031
14032    initializer-clause:
14033      assignment-expression
14034      { initializer-list , [opt] }
14035      { }
14036
14037    Returns an expression representing the initializer.
14038
14039    If the `assignment-expression' production is used the value
14040    returned is simply a representation for the expression.
14041
14042    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
14043    the elements of the initializer-list (or NULL, if the last
14044    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14045    NULL_TREE.  There is no way to detect whether or not the optional
14046    trailing `,' was provided.  NON_CONSTANT_P is as for
14047    cp_parser_initializer.  */
14048
14049 static tree
14050 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14051 {
14052   tree initializer;
14053
14054   /* Assume the expression is constant.  */
14055   *non_constant_p = false;
14056
14057   /* If it is not a `{', then we are looking at an
14058      assignment-expression.  */
14059   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14060     {
14061       initializer
14062         = cp_parser_constant_expression (parser,
14063                                         /*allow_non_constant_p=*/true,
14064                                         non_constant_p);
14065       if (!*non_constant_p)
14066         initializer = fold_non_dependent_expr (initializer);
14067     }
14068   else
14069     {
14070       /* Consume the `{' token.  */
14071       cp_lexer_consume_token (parser->lexer);
14072       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14073       initializer = make_node (CONSTRUCTOR);
14074       /* If it's not a `}', then there is a non-trivial initializer.  */
14075       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14076         {
14077           /* Parse the initializer list.  */
14078           CONSTRUCTOR_ELTS (initializer)
14079             = cp_parser_initializer_list (parser, non_constant_p);
14080           /* A trailing `,' token is allowed.  */
14081           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14082             cp_lexer_consume_token (parser->lexer);
14083         }
14084       /* Now, there should be a trailing `}'.  */
14085       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14086     }
14087
14088   return initializer;
14089 }
14090
14091 /* Parse an initializer-list.
14092
14093    initializer-list:
14094      initializer-clause ... [opt]
14095      initializer-list , initializer-clause ... [opt]
14096
14097    GNU Extension:
14098
14099    initializer-list:
14100      identifier : initializer-clause
14101      initializer-list, identifier : initializer-clause
14102
14103    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14104    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14105    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14106    as for cp_parser_initializer.  */
14107
14108 static VEC(constructor_elt,gc) *
14109 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14110 {
14111   VEC(constructor_elt,gc) *v = NULL;
14112
14113   /* Assume all of the expressions are constant.  */
14114   *non_constant_p = false;
14115
14116   /* Parse the rest of the list.  */
14117   while (true)
14118     {
14119       cp_token *token;
14120       tree identifier;
14121       tree initializer;
14122       bool clause_non_constant_p;
14123
14124       /* If the next token is an identifier and the following one is a
14125          colon, we are looking at the GNU designated-initializer
14126          syntax.  */
14127       if (cp_parser_allow_gnu_extensions_p (parser)
14128           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14129           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14130         {
14131           /* Warn the user that they are using an extension.  */
14132           if (pedantic)
14133             pedwarn ("ISO C++ does not allow designated initializers");
14134           /* Consume the identifier.  */
14135           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14136           /* Consume the `:'.  */
14137           cp_lexer_consume_token (parser->lexer);
14138         }
14139       else
14140         identifier = NULL_TREE;
14141
14142       /* Parse the initializer.  */
14143       initializer = cp_parser_initializer_clause (parser,
14144                                                   &clause_non_constant_p);
14145       /* If any clause is non-constant, so is the entire initializer.  */
14146       if (clause_non_constant_p)
14147         *non_constant_p = true;
14148
14149       /* If we have an ellipsis, this is an initializer pack
14150          expansion.  */
14151       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14152         {
14153           /* Consume the `...'.  */
14154           cp_lexer_consume_token (parser->lexer);
14155
14156           /* Turn the initializer into an initializer expansion.  */
14157           initializer = make_pack_expansion (initializer);
14158         }
14159
14160       /* Add it to the vector.  */
14161       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14162
14163       /* If the next token is not a comma, we have reached the end of
14164          the list.  */
14165       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14166         break;
14167
14168       /* Peek at the next token.  */
14169       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14170       /* If the next token is a `}', then we're still done.  An
14171          initializer-clause can have a trailing `,' after the
14172          initializer-list and before the closing `}'.  */
14173       if (token->type == CPP_CLOSE_BRACE)
14174         break;
14175
14176       /* Consume the `,' token.  */
14177       cp_lexer_consume_token (parser->lexer);
14178     }
14179
14180   return v;
14181 }
14182
14183 /* Classes [gram.class] */
14184
14185 /* Parse a class-name.
14186
14187    class-name:
14188      identifier
14189      template-id
14190
14191    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14192    to indicate that names looked up in dependent types should be
14193    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14194    keyword has been used to indicate that the name that appears next
14195    is a template.  TAG_TYPE indicates the explicit tag given before
14196    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14197    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14198    is the class being defined in a class-head.
14199
14200    Returns the TYPE_DECL representing the class.  */
14201
14202 static tree
14203 cp_parser_class_name (cp_parser *parser,
14204                       bool typename_keyword_p,
14205                       bool template_keyword_p,
14206                       enum tag_types tag_type,
14207                       bool check_dependency_p,
14208                       bool class_head_p,
14209                       bool is_declaration)
14210 {
14211   tree decl;
14212   tree scope;
14213   bool typename_p;
14214   cp_token *token;
14215
14216   /* All class-names start with an identifier.  */
14217   token = cp_lexer_peek_token (parser->lexer);
14218   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14219     {
14220       cp_parser_error (parser, "expected class-name");
14221       return error_mark_node;
14222     }
14223
14224   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14225      to a template-id, so we save it here.  */
14226   scope = parser->scope;
14227   if (scope == error_mark_node)
14228     return error_mark_node;
14229
14230   /* Any name names a type if we're following the `typename' keyword
14231      in a qualified name where the enclosing scope is type-dependent.  */
14232   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14233                 && dependent_type_p (scope));
14234   /* Handle the common case (an identifier, but not a template-id)
14235      efficiently.  */
14236   if (token->type == CPP_NAME
14237       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14238     {
14239       cp_token *identifier_token;
14240       tree identifier;
14241       bool ambiguous_p;
14242
14243       /* Look for the identifier.  */
14244       identifier_token = cp_lexer_peek_token (parser->lexer);
14245       ambiguous_p = identifier_token->ambiguous_p;
14246       identifier = cp_parser_identifier (parser);
14247       /* If the next token isn't an identifier, we are certainly not
14248          looking at a class-name.  */
14249       if (identifier == error_mark_node)
14250         decl = error_mark_node;
14251       /* If we know this is a type-name, there's no need to look it
14252          up.  */
14253       else if (typename_p)
14254         decl = identifier;
14255       else
14256         {
14257           tree ambiguous_decls;
14258           /* If we already know that this lookup is ambiguous, then
14259              we've already issued an error message; there's no reason
14260              to check again.  */
14261           if (ambiguous_p)
14262             {
14263               cp_parser_simulate_error (parser);
14264               return error_mark_node;
14265             }
14266           /* If the next token is a `::', then the name must be a type
14267              name.
14268
14269              [basic.lookup.qual]
14270
14271              During the lookup for a name preceding the :: scope
14272              resolution operator, object, function, and enumerator
14273              names are ignored.  */
14274           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14275             tag_type = typename_type;
14276           /* Look up the name.  */
14277           decl = cp_parser_lookup_name (parser, identifier,
14278                                         tag_type,
14279                                         /*is_template=*/false,
14280                                         /*is_namespace=*/false,
14281                                         check_dependency_p,
14282                                         &ambiguous_decls);
14283           if (ambiguous_decls)
14284             {
14285               error ("reference to %qD is ambiguous", identifier);
14286               print_candidates (ambiguous_decls);
14287               if (cp_parser_parsing_tentatively (parser))
14288                 {
14289                   identifier_token->ambiguous_p = true;
14290                   cp_parser_simulate_error (parser);
14291                 }
14292               return error_mark_node;
14293             }
14294         }
14295     }
14296   else
14297     {
14298       /* Try a template-id.  */
14299       decl = cp_parser_template_id (parser, template_keyword_p,
14300                                     check_dependency_p,
14301                                     is_declaration);
14302       if (decl == error_mark_node)
14303         return error_mark_node;
14304     }
14305
14306   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14307
14308   /* If this is a typename, create a TYPENAME_TYPE.  */
14309   if (typename_p && decl != error_mark_node)
14310     {
14311       decl = make_typename_type (scope, decl, typename_type,
14312                                  /*complain=*/tf_error);
14313       if (decl != error_mark_node)
14314         decl = TYPE_NAME (decl);
14315     }
14316
14317   /* Check to see that it is really the name of a class.  */
14318   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14319       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14320       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14321     /* Situations like this:
14322
14323          template <typename T> struct A {
14324            typename T::template X<int>::I i;
14325          };
14326
14327        are problematic.  Is `T::template X<int>' a class-name?  The
14328        standard does not seem to be definitive, but there is no other
14329        valid interpretation of the following `::'.  Therefore, those
14330        names are considered class-names.  */
14331     {
14332       decl = make_typename_type (scope, decl, tag_type, tf_error);
14333       if (decl != error_mark_node)
14334         decl = TYPE_NAME (decl);
14335     }
14336   else if (TREE_CODE (decl) != TYPE_DECL
14337            || TREE_TYPE (decl) == error_mark_node
14338            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14339     decl = error_mark_node;
14340
14341   if (decl == error_mark_node)
14342     cp_parser_error (parser, "expected class-name");
14343
14344   return decl;
14345 }
14346
14347 /* Parse a class-specifier.
14348
14349    class-specifier:
14350      class-head { member-specification [opt] }
14351
14352    Returns the TREE_TYPE representing the class.  */
14353
14354 static tree
14355 cp_parser_class_specifier (cp_parser* parser)
14356 {
14357   cp_token *token;
14358   tree type;
14359   tree attributes = NULL_TREE;
14360   int has_trailing_semicolon;
14361   bool nested_name_specifier_p;
14362   unsigned saved_num_template_parameter_lists;
14363   bool saved_in_function_body;
14364   tree old_scope = NULL_TREE;
14365   tree scope = NULL_TREE;
14366   tree bases;
14367
14368   push_deferring_access_checks (dk_no_deferred);
14369
14370   /* Parse the class-head.  */
14371   type = cp_parser_class_head (parser,
14372                                &nested_name_specifier_p,
14373                                &attributes,
14374                                &bases);
14375   /* If the class-head was a semantic disaster, skip the entire body
14376      of the class.  */
14377   if (!type)
14378     {
14379       cp_parser_skip_to_end_of_block_or_statement (parser);
14380       pop_deferring_access_checks ();
14381       return error_mark_node;
14382     }
14383
14384   /* Look for the `{'.  */
14385   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14386     {
14387       pop_deferring_access_checks ();
14388       return error_mark_node;
14389     }
14390
14391   /* Process the base classes. If they're invalid, skip the 
14392      entire class body.  */
14393   if (!xref_basetypes (type, bases))
14394     {
14395       /* Consuming the closing brace yields better error messages
14396          later on.  */
14397       if (cp_parser_skip_to_closing_brace (parser))
14398         cp_lexer_consume_token (parser->lexer);
14399       pop_deferring_access_checks ();
14400       return error_mark_node;
14401     }
14402
14403   /* Issue an error message if type-definitions are forbidden here.  */
14404   cp_parser_check_type_definition (parser);
14405   /* Remember that we are defining one more class.  */
14406   ++parser->num_classes_being_defined;
14407   /* Inside the class, surrounding template-parameter-lists do not
14408      apply.  */
14409   saved_num_template_parameter_lists
14410     = parser->num_template_parameter_lists;
14411   parser->num_template_parameter_lists = 0;
14412   /* We are not in a function body.  */
14413   saved_in_function_body = parser->in_function_body;
14414   parser->in_function_body = false;
14415
14416   /* Start the class.  */
14417   if (nested_name_specifier_p)
14418     {
14419       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14420       old_scope = push_inner_scope (scope);
14421     }
14422   type = begin_class_definition (type, attributes);
14423
14424   if (type == error_mark_node)
14425     /* If the type is erroneous, skip the entire body of the class.  */
14426     cp_parser_skip_to_closing_brace (parser);
14427   else
14428     /* Parse the member-specification.  */
14429     cp_parser_member_specification_opt (parser);
14430
14431   /* Look for the trailing `}'.  */
14432   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14433   /* We get better error messages by noticing a common problem: a
14434      missing trailing `;'.  */
14435   token = cp_lexer_peek_token (parser->lexer);
14436   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14437   /* Look for trailing attributes to apply to this class.  */
14438   if (cp_parser_allow_gnu_extensions_p (parser))
14439     attributes = cp_parser_attributes_opt (parser);
14440   if (type != error_mark_node)
14441     type = finish_struct (type, attributes);
14442   if (nested_name_specifier_p)
14443     pop_inner_scope (old_scope, scope);
14444   /* If this class is not itself within the scope of another class,
14445      then we need to parse the bodies of all of the queued function
14446      definitions.  Note that the queued functions defined in a class
14447      are not always processed immediately following the
14448      class-specifier for that class.  Consider:
14449
14450        struct A {
14451          struct B { void f() { sizeof (A); } };
14452        };
14453
14454      If `f' were processed before the processing of `A' were
14455      completed, there would be no way to compute the size of `A'.
14456      Note that the nesting we are interested in here is lexical --
14457      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14458      for:
14459
14460        struct A { struct B; };
14461        struct A::B { void f() { } };
14462
14463      there is no need to delay the parsing of `A::B::f'.  */
14464   if (--parser->num_classes_being_defined == 0)
14465     {
14466       tree queue_entry;
14467       tree fn;
14468       tree class_type = NULL_TREE;
14469       tree pushed_scope = NULL_TREE;
14470
14471       /* In a first pass, parse default arguments to the functions.
14472          Then, in a second pass, parse the bodies of the functions.
14473          This two-phased approach handles cases like:
14474
14475             struct S {
14476               void f() { g(); }
14477               void g(int i = 3);
14478             };
14479
14480          */
14481       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14482              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14483            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14484            TREE_PURPOSE (parser->unparsed_functions_queues)
14485              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14486         {
14487           fn = TREE_VALUE (queue_entry);
14488           /* If there are default arguments that have not yet been processed,
14489              take care of them now.  */
14490           if (class_type != TREE_PURPOSE (queue_entry))
14491             {
14492               if (pushed_scope)
14493                 pop_scope (pushed_scope);
14494               class_type = TREE_PURPOSE (queue_entry);
14495               pushed_scope = push_scope (class_type);
14496             }
14497           /* Make sure that any template parameters are in scope.  */
14498           maybe_begin_member_template_processing (fn);
14499           /* Parse the default argument expressions.  */
14500           cp_parser_late_parsing_default_args (parser, fn);
14501           /* Remove any template parameters from the symbol table.  */
14502           maybe_end_member_template_processing ();
14503         }
14504       if (pushed_scope)
14505         pop_scope (pushed_scope);
14506       /* Now parse the body of the functions.  */
14507       for (TREE_VALUE (parser->unparsed_functions_queues)
14508              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14509            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14510            TREE_VALUE (parser->unparsed_functions_queues)
14511              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14512         {
14513           /* Figure out which function we need to process.  */
14514           fn = TREE_VALUE (queue_entry);
14515           /* Parse the function.  */
14516           cp_parser_late_parsing_for_member (parser, fn);
14517         }
14518     }
14519
14520   /* Put back any saved access checks.  */
14521   pop_deferring_access_checks ();
14522
14523   /* Restore saved state.  */
14524   parser->in_function_body = saved_in_function_body;
14525   parser->num_template_parameter_lists
14526     = saved_num_template_parameter_lists;
14527
14528   return type;
14529 }
14530
14531 /* Parse a class-head.
14532
14533    class-head:
14534      class-key identifier [opt] base-clause [opt]
14535      class-key nested-name-specifier identifier base-clause [opt]
14536      class-key nested-name-specifier [opt] template-id
14537        base-clause [opt]
14538
14539    GNU Extensions:
14540      class-key attributes identifier [opt] base-clause [opt]
14541      class-key attributes nested-name-specifier identifier base-clause [opt]
14542      class-key attributes nested-name-specifier [opt] template-id
14543        base-clause [opt]
14544
14545    Upon return BASES is initialized to the list of base classes (or
14546    NULL, if there are none) in the same form returned by
14547    cp_parser_base_clause.
14548
14549    Returns the TYPE of the indicated class.  Sets
14550    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14551    involving a nested-name-specifier was used, and FALSE otherwise.
14552
14553    Returns error_mark_node if this is not a class-head.
14554
14555    Returns NULL_TREE if the class-head is syntactically valid, but
14556    semantically invalid in a way that means we should skip the entire
14557    body of the class.  */
14558
14559 static tree
14560 cp_parser_class_head (cp_parser* parser,
14561                       bool* nested_name_specifier_p,
14562                       tree *attributes_p,
14563                       tree *bases)
14564 {
14565   tree nested_name_specifier;
14566   enum tag_types class_key;
14567   tree id = NULL_TREE;
14568   tree type = NULL_TREE;
14569   tree attributes;
14570   bool template_id_p = false;
14571   bool qualified_p = false;
14572   bool invalid_nested_name_p = false;
14573   bool invalid_explicit_specialization_p = false;
14574   tree pushed_scope = NULL_TREE;
14575   unsigned num_templates;
14576
14577   /* Assume no nested-name-specifier will be present.  */
14578   *nested_name_specifier_p = false;
14579   /* Assume no template parameter lists will be used in defining the
14580      type.  */
14581   num_templates = 0;
14582
14583   *bases = NULL_TREE;
14584
14585   /* Look for the class-key.  */
14586   class_key = cp_parser_class_key (parser);
14587   if (class_key == none_type)
14588     return error_mark_node;
14589
14590   /* Parse the attributes.  */
14591   attributes = cp_parser_attributes_opt (parser);
14592
14593   /* If the next token is `::', that is invalid -- but sometimes
14594      people do try to write:
14595
14596        struct ::S {};
14597
14598      Handle this gracefully by accepting the extra qualifier, and then
14599      issuing an error about it later if this really is a
14600      class-head.  If it turns out just to be an elaborated type
14601      specifier, remain silent.  */
14602   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14603     qualified_p = true;
14604
14605   push_deferring_access_checks (dk_no_check);
14606
14607   /* Determine the name of the class.  Begin by looking for an
14608      optional nested-name-specifier.  */
14609   nested_name_specifier
14610     = cp_parser_nested_name_specifier_opt (parser,
14611                                            /*typename_keyword_p=*/false,
14612                                            /*check_dependency_p=*/false,
14613                                            /*type_p=*/false,
14614                                            /*is_declaration=*/false);
14615   /* If there was a nested-name-specifier, then there *must* be an
14616      identifier.  */
14617   if (nested_name_specifier)
14618     {
14619       /* Although the grammar says `identifier', it really means
14620          `class-name' or `template-name'.  You are only allowed to
14621          define a class that has already been declared with this
14622          syntax.
14623
14624          The proposed resolution for Core Issue 180 says that wherever
14625          you see `class T::X' you should treat `X' as a type-name.
14626
14627          It is OK to define an inaccessible class; for example:
14628
14629            class A { class B; };
14630            class A::B {};
14631
14632          We do not know if we will see a class-name, or a
14633          template-name.  We look for a class-name first, in case the
14634          class-name is a template-id; if we looked for the
14635          template-name first we would stop after the template-name.  */
14636       cp_parser_parse_tentatively (parser);
14637       type = cp_parser_class_name (parser,
14638                                    /*typename_keyword_p=*/false,
14639                                    /*template_keyword_p=*/false,
14640                                    class_type,
14641                                    /*check_dependency_p=*/false,
14642                                    /*class_head_p=*/true,
14643                                    /*is_declaration=*/false);
14644       /* If that didn't work, ignore the nested-name-specifier.  */
14645       if (!cp_parser_parse_definitely (parser))
14646         {
14647           invalid_nested_name_p = true;
14648           id = cp_parser_identifier (parser);
14649           if (id == error_mark_node)
14650             id = NULL_TREE;
14651         }
14652       /* If we could not find a corresponding TYPE, treat this
14653          declaration like an unqualified declaration.  */
14654       if (type == error_mark_node)
14655         nested_name_specifier = NULL_TREE;
14656       /* Otherwise, count the number of templates used in TYPE and its
14657          containing scopes.  */
14658       else
14659         {
14660           tree scope;
14661
14662           for (scope = TREE_TYPE (type);
14663                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14664                scope = (TYPE_P (scope)
14665                         ? TYPE_CONTEXT (scope)
14666                         : DECL_CONTEXT (scope)))
14667             if (TYPE_P (scope)
14668                 && CLASS_TYPE_P (scope)
14669                 && CLASSTYPE_TEMPLATE_INFO (scope)
14670                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14671                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14672               ++num_templates;
14673         }
14674     }
14675   /* Otherwise, the identifier is optional.  */
14676   else
14677     {
14678       /* We don't know whether what comes next is a template-id,
14679          an identifier, or nothing at all.  */
14680       cp_parser_parse_tentatively (parser);
14681       /* Check for a template-id.  */
14682       id = cp_parser_template_id (parser,
14683                                   /*template_keyword_p=*/false,
14684                                   /*check_dependency_p=*/true,
14685                                   /*is_declaration=*/true);
14686       /* If that didn't work, it could still be an identifier.  */
14687       if (!cp_parser_parse_definitely (parser))
14688         {
14689           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14690             id = cp_parser_identifier (parser);
14691           else
14692             id = NULL_TREE;
14693         }
14694       else
14695         {
14696           template_id_p = true;
14697           ++num_templates;
14698         }
14699     }
14700
14701   pop_deferring_access_checks ();
14702
14703   if (id)
14704     cp_parser_check_for_invalid_template_id (parser, id);
14705
14706   /* If it's not a `:' or a `{' then we can't really be looking at a
14707      class-head, since a class-head only appears as part of a
14708      class-specifier.  We have to detect this situation before calling
14709      xref_tag, since that has irreversible side-effects.  */
14710   if (!cp_parser_next_token_starts_class_definition_p (parser))
14711     {
14712       cp_parser_error (parser, "expected %<{%> or %<:%>");
14713       return error_mark_node;
14714     }
14715
14716   /* At this point, we're going ahead with the class-specifier, even
14717      if some other problem occurs.  */
14718   cp_parser_commit_to_tentative_parse (parser);
14719   /* Issue the error about the overly-qualified name now.  */
14720   if (qualified_p)
14721     cp_parser_error (parser,
14722                      "global qualification of class name is invalid");
14723   else if (invalid_nested_name_p)
14724     cp_parser_error (parser,
14725                      "qualified name does not name a class");
14726   else if (nested_name_specifier)
14727     {
14728       tree scope;
14729
14730       /* Reject typedef-names in class heads.  */
14731       if (!DECL_IMPLICIT_TYPEDEF_P (type))
14732         {
14733           error ("invalid class name in declaration of %qD", type);
14734           type = NULL_TREE;
14735           goto done;
14736         }
14737
14738       /* Figure out in what scope the declaration is being placed.  */
14739       scope = current_scope ();
14740       /* If that scope does not contain the scope in which the
14741          class was originally declared, the program is invalid.  */
14742       if (scope && !is_ancestor (scope, nested_name_specifier))
14743         {
14744           if (at_namespace_scope_p ())
14745             error ("declaration of %qD in namespace %qD which does not "
14746                    "enclose %qD", type, scope, nested_name_specifier);
14747           else
14748             error ("declaration of %qD in %qD which does not enclose %qD",
14749                    type, scope, nested_name_specifier);
14750           type = NULL_TREE;
14751           goto done;
14752         }
14753       /* [dcl.meaning]
14754
14755          A declarator-id shall not be qualified exception of the
14756          definition of a ... nested class outside of its class
14757          ... [or] a the definition or explicit instantiation of a
14758          class member of a namespace outside of its namespace.  */
14759       if (scope == nested_name_specifier)
14760         {
14761           pedwarn ("extra qualification ignored");
14762           nested_name_specifier = NULL_TREE;
14763           num_templates = 0;
14764         }
14765     }
14766   /* An explicit-specialization must be preceded by "template <>".  If
14767      it is not, try to recover gracefully.  */
14768   if (at_namespace_scope_p ()
14769       && parser->num_template_parameter_lists == 0
14770       && template_id_p)
14771     {
14772       error ("an explicit specialization must be preceded by %<template <>%>");
14773       invalid_explicit_specialization_p = true;
14774       /* Take the same action that would have been taken by
14775          cp_parser_explicit_specialization.  */
14776       ++parser->num_template_parameter_lists;
14777       begin_specialization ();
14778     }
14779   /* There must be no "return" statements between this point and the
14780      end of this function; set "type "to the correct return value and
14781      use "goto done;" to return.  */
14782   /* Make sure that the right number of template parameters were
14783      present.  */
14784   if (!cp_parser_check_template_parameters (parser, num_templates))
14785     {
14786       /* If something went wrong, there is no point in even trying to
14787          process the class-definition.  */
14788       type = NULL_TREE;
14789       goto done;
14790     }
14791
14792   /* Look up the type.  */
14793   if (template_id_p)
14794     {
14795       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14796           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14797               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14798         {
14799           error ("function template %qD redeclared as a class template", id);
14800           type = error_mark_node;
14801         }
14802       else
14803         {
14804           type = TREE_TYPE (id);
14805           type = maybe_process_partial_specialization (type);
14806         }
14807       if (nested_name_specifier)
14808         pushed_scope = push_scope (nested_name_specifier);
14809     }
14810   else if (nested_name_specifier)
14811     {
14812       tree class_type;
14813
14814       /* Given:
14815
14816             template <typename T> struct S { struct T };
14817             template <typename T> struct S<T>::T { };
14818
14819          we will get a TYPENAME_TYPE when processing the definition of
14820          `S::T'.  We need to resolve it to the actual type before we
14821          try to define it.  */
14822       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14823         {
14824           class_type = resolve_typename_type (TREE_TYPE (type),
14825                                               /*only_current_p=*/false);
14826           if (TREE_CODE (class_type) != TYPENAME_TYPE)
14827             type = TYPE_NAME (class_type);
14828           else
14829             {
14830               cp_parser_error (parser, "could not resolve typename type");
14831               type = error_mark_node;
14832             }
14833         }
14834
14835       if (maybe_process_partial_specialization (TREE_TYPE (type))
14836           == error_mark_node)
14837         {
14838           type = NULL_TREE;
14839           goto done;
14840         }
14841
14842       class_type = current_class_type;
14843       /* Enter the scope indicated by the nested-name-specifier.  */
14844       pushed_scope = push_scope (nested_name_specifier);
14845       /* Get the canonical version of this type.  */
14846       type = TYPE_MAIN_DECL (TREE_TYPE (type));
14847       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14848           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14849         {
14850           type = push_template_decl (type);
14851           if (type == error_mark_node)
14852             {
14853               type = NULL_TREE;
14854               goto done;
14855             }
14856         }
14857
14858       type = TREE_TYPE (type);
14859       *nested_name_specifier_p = true;
14860     }
14861   else      /* The name is not a nested name.  */
14862     {
14863       /* If the class was unnamed, create a dummy name.  */
14864       if (!id)
14865         id = make_anon_name ();
14866       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14867                        parser->num_template_parameter_lists);
14868     }
14869
14870   /* Indicate whether this class was declared as a `class' or as a
14871      `struct'.  */
14872   if (TREE_CODE (type) == RECORD_TYPE)
14873     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14874   cp_parser_check_class_key (class_key, type);
14875
14876   /* If this type was already complete, and we see another definition,
14877      that's an error.  */
14878   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14879     {
14880       error ("redefinition of %q#T", type);
14881       error ("previous definition of %q+#T", type);
14882       type = NULL_TREE;
14883       goto done;
14884     }
14885   else if (type == error_mark_node)
14886     type = NULL_TREE;
14887
14888   /* We will have entered the scope containing the class; the names of
14889      base classes should be looked up in that context.  For example:
14890
14891        struct A { struct B {}; struct C; };
14892        struct A::C : B {};
14893
14894      is valid.  */
14895
14896   /* Get the list of base-classes, if there is one.  */
14897   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14898     *bases = cp_parser_base_clause (parser);
14899
14900  done:
14901   /* Leave the scope given by the nested-name-specifier.  We will
14902      enter the class scope itself while processing the members.  */
14903   if (pushed_scope)
14904     pop_scope (pushed_scope);
14905
14906   if (invalid_explicit_specialization_p)
14907     {
14908       end_specialization ();
14909       --parser->num_template_parameter_lists;
14910     }
14911   *attributes_p = attributes;
14912   return type;
14913 }
14914
14915 /* Parse a class-key.
14916
14917    class-key:
14918      class
14919      struct
14920      union
14921
14922    Returns the kind of class-key specified, or none_type to indicate
14923    error.  */
14924
14925 static enum tag_types
14926 cp_parser_class_key (cp_parser* parser)
14927 {
14928   cp_token *token;
14929   enum tag_types tag_type;
14930
14931   /* Look for the class-key.  */
14932   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14933   if (!token)
14934     return none_type;
14935
14936   /* Check to see if the TOKEN is a class-key.  */
14937   tag_type = cp_parser_token_is_class_key (token);
14938   if (!tag_type)
14939     cp_parser_error (parser, "expected class-key");
14940   return tag_type;
14941 }
14942
14943 /* Parse an (optional) member-specification.
14944
14945    member-specification:
14946      member-declaration member-specification [opt]
14947      access-specifier : member-specification [opt]  */
14948
14949 static void
14950 cp_parser_member_specification_opt (cp_parser* parser)
14951 {
14952   while (true)
14953     {
14954       cp_token *token;
14955       enum rid keyword;
14956
14957       /* Peek at the next token.  */
14958       token = cp_lexer_peek_token (parser->lexer);
14959       /* If it's a `}', or EOF then we've seen all the members.  */
14960       if (token->type == CPP_CLOSE_BRACE
14961           || token->type == CPP_EOF
14962           || token->type == CPP_PRAGMA_EOL)
14963         break;
14964
14965       /* See if this token is a keyword.  */
14966       keyword = token->keyword;
14967       switch (keyword)
14968         {
14969         case RID_PUBLIC:
14970         case RID_PROTECTED:
14971         case RID_PRIVATE:
14972           /* Consume the access-specifier.  */
14973           cp_lexer_consume_token (parser->lexer);
14974           /* Remember which access-specifier is active.  */
14975           current_access_specifier = token->u.value;
14976           /* Look for the `:'.  */
14977           cp_parser_require (parser, CPP_COLON, "%<:%>");
14978           break;
14979
14980         default:
14981           /* Accept #pragmas at class scope.  */
14982           if (token->type == CPP_PRAGMA)
14983             {
14984               cp_parser_pragma (parser, pragma_external);
14985               break;
14986             }
14987
14988           /* Otherwise, the next construction must be a
14989              member-declaration.  */
14990           cp_parser_member_declaration (parser);
14991         }
14992     }
14993 }
14994
14995 /* Parse a member-declaration.
14996
14997    member-declaration:
14998      decl-specifier-seq [opt] member-declarator-list [opt] ;
14999      function-definition ; [opt]
15000      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15001      using-declaration
15002      template-declaration
15003
15004    member-declarator-list:
15005      member-declarator
15006      member-declarator-list , member-declarator
15007
15008    member-declarator:
15009      declarator pure-specifier [opt]
15010      declarator constant-initializer [opt]
15011      identifier [opt] : constant-expression
15012
15013    GNU Extensions:
15014
15015    member-declaration:
15016      __extension__ member-declaration
15017
15018    member-declarator:
15019      declarator attributes [opt] pure-specifier [opt]
15020      declarator attributes [opt] constant-initializer [opt]
15021      identifier [opt] attributes [opt] : constant-expression  
15022
15023    C++0x Extensions:
15024
15025    member-declaration:
15026      static_assert-declaration  */
15027
15028 static void
15029 cp_parser_member_declaration (cp_parser* parser)
15030 {
15031   cp_decl_specifier_seq decl_specifiers;
15032   tree prefix_attributes;
15033   tree decl;
15034   int declares_class_or_enum;
15035   bool friend_p;
15036   cp_token *token;
15037   int saved_pedantic;
15038
15039   /* Check for the `__extension__' keyword.  */
15040   if (cp_parser_extension_opt (parser, &saved_pedantic))
15041     {
15042       /* Recurse.  */
15043       cp_parser_member_declaration (parser);
15044       /* Restore the old value of the PEDANTIC flag.  */
15045       pedantic = saved_pedantic;
15046
15047       return;
15048     }
15049
15050   /* Check for a template-declaration.  */
15051   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15052     {
15053       /* An explicit specialization here is an error condition, and we
15054          expect the specialization handler to detect and report this.  */
15055       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15056           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15057         cp_parser_explicit_specialization (parser);
15058       else
15059         cp_parser_template_declaration (parser, /*member_p=*/true);
15060
15061       return;
15062     }
15063
15064   /* Check for a using-declaration.  */
15065   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15066     {
15067       /* Parse the using-declaration.  */
15068       cp_parser_using_declaration (parser,
15069                                    /*access_declaration_p=*/false);
15070       return;
15071     }
15072
15073   /* Check for @defs.  */
15074   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15075     {
15076       tree ivar, member;
15077       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15078       ivar = ivar_chains;
15079       while (ivar)
15080         {
15081           member = ivar;
15082           ivar = TREE_CHAIN (member);
15083           TREE_CHAIN (member) = NULL_TREE;
15084           finish_member_declaration (member);
15085         }
15086       return;
15087     }
15088
15089   /* If the next token is `static_assert' we have a static assertion.  */
15090   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15091     {
15092       cp_parser_static_assert (parser, /*member_p=*/true);
15093       return;
15094     }
15095
15096   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15097     return;
15098
15099   /* Parse the decl-specifier-seq.  */
15100   cp_parser_decl_specifier_seq (parser,
15101                                 CP_PARSER_FLAGS_OPTIONAL,
15102                                 &decl_specifiers,
15103                                 &declares_class_or_enum);
15104   prefix_attributes = decl_specifiers.attributes;
15105   decl_specifiers.attributes = NULL_TREE;
15106   /* Check for an invalid type-name.  */
15107   if (!decl_specifiers.type
15108       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15109     return;
15110   /* If there is no declarator, then the decl-specifier-seq should
15111      specify a type.  */
15112   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15113     {
15114       /* If there was no decl-specifier-seq, and the next token is a
15115          `;', then we have something like:
15116
15117            struct S { ; };
15118
15119          [class.mem]
15120
15121          Each member-declaration shall declare at least one member
15122          name of the class.  */
15123       if (!decl_specifiers.any_specifiers_p)
15124         {
15125           cp_token *token = cp_lexer_peek_token (parser->lexer);
15126           if (pedantic && !token->in_system_header)
15127             pedwarn ("%Hextra %<;%>", &token->location);
15128         }
15129       else
15130         {
15131           tree type;
15132
15133           /* See if this declaration is a friend.  */
15134           friend_p = cp_parser_friend_p (&decl_specifiers);
15135           /* If there were decl-specifiers, check to see if there was
15136              a class-declaration.  */
15137           type = check_tag_decl (&decl_specifiers);
15138           /* Nested classes have already been added to the class, but
15139              a `friend' needs to be explicitly registered.  */
15140           if (friend_p)
15141             {
15142               /* If the `friend' keyword was present, the friend must
15143                  be introduced with a class-key.  */
15144                if (!declares_class_or_enum)
15145                  error ("a class-key must be used when declaring a friend");
15146                /* In this case:
15147
15148                     template <typename T> struct A {
15149                       friend struct A<T>::B;
15150                     };
15151
15152                   A<T>::B will be represented by a TYPENAME_TYPE, and
15153                   therefore not recognized by check_tag_decl.  */
15154                if (!type
15155                    && decl_specifiers.type
15156                    && TYPE_P (decl_specifiers.type))
15157                  type = decl_specifiers.type;
15158                if (!type || !TYPE_P (type))
15159                  error ("friend declaration does not name a class or "
15160                         "function");
15161                else
15162                  make_friend_class (current_class_type, type,
15163                                     /*complain=*/true);
15164             }
15165           /* If there is no TYPE, an error message will already have
15166              been issued.  */
15167           else if (!type || type == error_mark_node)
15168             ;
15169           /* An anonymous aggregate has to be handled specially; such
15170              a declaration really declares a data member (with a
15171              particular type), as opposed to a nested class.  */
15172           else if (ANON_AGGR_TYPE_P (type))
15173             {
15174               /* Remove constructors and such from TYPE, now that we
15175                  know it is an anonymous aggregate.  */
15176               fixup_anonymous_aggr (type);
15177               /* And make the corresponding data member.  */
15178               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15179               /* Add it to the class.  */
15180               finish_member_declaration (decl);
15181             }
15182           else
15183             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
15184         }
15185     }
15186   else
15187     {
15188       /* See if these declarations will be friends.  */
15189       friend_p = cp_parser_friend_p (&decl_specifiers);
15190
15191       /* Keep going until we hit the `;' at the end of the
15192          declaration.  */
15193       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15194         {
15195           tree attributes = NULL_TREE;
15196           tree first_attribute;
15197
15198           /* Peek at the next token.  */
15199           token = cp_lexer_peek_token (parser->lexer);
15200
15201           /* Check for a bitfield declaration.  */
15202           if (token->type == CPP_COLON
15203               || (token->type == CPP_NAME
15204                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15205                   == CPP_COLON))
15206             {
15207               tree identifier;
15208               tree width;
15209
15210               /* Get the name of the bitfield.  Note that we cannot just
15211                  check TOKEN here because it may have been invalidated by
15212                  the call to cp_lexer_peek_nth_token above.  */
15213               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15214                 identifier = cp_parser_identifier (parser);
15215               else
15216                 identifier = NULL_TREE;
15217
15218               /* Consume the `:' token.  */
15219               cp_lexer_consume_token (parser->lexer);
15220               /* Get the width of the bitfield.  */
15221               width
15222                 = cp_parser_constant_expression (parser,
15223                                                  /*allow_non_constant=*/false,
15224                                                  NULL);
15225
15226               /* Look for attributes that apply to the bitfield.  */
15227               attributes = cp_parser_attributes_opt (parser);
15228               /* Remember which attributes are prefix attributes and
15229                  which are not.  */
15230               first_attribute = attributes;
15231               /* Combine the attributes.  */
15232               attributes = chainon (prefix_attributes, attributes);
15233
15234               /* Create the bitfield declaration.  */
15235               decl = grokbitfield (identifier
15236                                    ? make_id_declarator (NULL_TREE,
15237                                                          identifier,
15238                                                          sfk_none)
15239                                    : NULL,
15240                                    &decl_specifiers,
15241                                    width);
15242               /* Apply the attributes.  */
15243               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15244             }
15245           else
15246             {
15247               cp_declarator *declarator;
15248               tree initializer;
15249               tree asm_specification;
15250               int ctor_dtor_or_conv_p;
15251
15252               /* Parse the declarator.  */
15253               declarator
15254                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15255                                         &ctor_dtor_or_conv_p,
15256                                         /*parenthesized_p=*/NULL,
15257                                         /*member_p=*/true);
15258
15259               /* If something went wrong parsing the declarator, make sure
15260                  that we at least consume some tokens.  */
15261               if (declarator == cp_error_declarator)
15262                 {
15263                   /* Skip to the end of the statement.  */
15264                   cp_parser_skip_to_end_of_statement (parser);
15265                   /* If the next token is not a semicolon, that is
15266                      probably because we just skipped over the body of
15267                      a function.  So, we consume a semicolon if
15268                      present, but do not issue an error message if it
15269                      is not present.  */
15270                   if (cp_lexer_next_token_is (parser->lexer,
15271                                               CPP_SEMICOLON))
15272                     cp_lexer_consume_token (parser->lexer);
15273                   return;
15274                 }
15275
15276               if (declares_class_or_enum & 2)
15277                 cp_parser_check_for_definition_in_return_type
15278                   (declarator, decl_specifiers.type);
15279
15280               /* Look for an asm-specification.  */
15281               asm_specification = cp_parser_asm_specification_opt (parser);
15282               /* Look for attributes that apply to the declaration.  */
15283               attributes = cp_parser_attributes_opt (parser);
15284               /* Remember which attributes are prefix attributes and
15285                  which are not.  */
15286               first_attribute = attributes;
15287               /* Combine the attributes.  */
15288               attributes = chainon (prefix_attributes, attributes);
15289
15290               /* If it's an `=', then we have a constant-initializer or a
15291                  pure-specifier.  It is not correct to parse the
15292                  initializer before registering the member declaration
15293                  since the member declaration should be in scope while
15294                  its initializer is processed.  However, the rest of the
15295                  front end does not yet provide an interface that allows
15296                  us to handle this correctly.  */
15297               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15298                 {
15299                   /* In [class.mem]:
15300
15301                      A pure-specifier shall be used only in the declaration of
15302                      a virtual function.
15303
15304                      A member-declarator can contain a constant-initializer
15305                      only if it declares a static member of integral or
15306                      enumeration type.
15307
15308                      Therefore, if the DECLARATOR is for a function, we look
15309                      for a pure-specifier; otherwise, we look for a
15310                      constant-initializer.  When we call `grokfield', it will
15311                      perform more stringent semantics checks.  */
15312                   if (function_declarator_p (declarator))
15313                     initializer = cp_parser_pure_specifier (parser);
15314                   else
15315                     /* Parse the initializer.  */
15316                     initializer = cp_parser_constant_initializer (parser);
15317                 }
15318               /* Otherwise, there is no initializer.  */
15319               else
15320                 initializer = NULL_TREE;
15321
15322               /* See if we are probably looking at a function
15323                  definition.  We are certainly not looking at a
15324                  member-declarator.  Calling `grokfield' has
15325                  side-effects, so we must not do it unless we are sure
15326                  that we are looking at a member-declarator.  */
15327               if (cp_parser_token_starts_function_definition_p
15328                   (cp_lexer_peek_token (parser->lexer)))
15329                 {
15330                   /* The grammar does not allow a pure-specifier to be
15331                      used when a member function is defined.  (It is
15332                      possible that this fact is an oversight in the
15333                      standard, since a pure function may be defined
15334                      outside of the class-specifier.  */
15335                   if (initializer)
15336                     error ("pure-specifier on function-definition");
15337                   decl = cp_parser_save_member_function_body (parser,
15338                                                               &decl_specifiers,
15339                                                               declarator,
15340                                                               attributes);
15341                   /* If the member was not a friend, declare it here.  */
15342                   if (!friend_p)
15343                     finish_member_declaration (decl);
15344                   /* Peek at the next token.  */
15345                   token = cp_lexer_peek_token (parser->lexer);
15346                   /* If the next token is a semicolon, consume it.  */
15347                   if (token->type == CPP_SEMICOLON)
15348                     cp_lexer_consume_token (parser->lexer);
15349                   return;
15350                 }
15351               else
15352                 /* Create the declaration.  */
15353                 decl = grokfield (declarator, &decl_specifiers,
15354                                   initializer, /*init_const_expr_p=*/true,
15355                                   asm_specification,
15356                                   attributes);
15357             }
15358
15359           /* Reset PREFIX_ATTRIBUTES.  */
15360           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15361             attributes = TREE_CHAIN (attributes);
15362           if (attributes)
15363             TREE_CHAIN (attributes) = NULL_TREE;
15364
15365           /* If there is any qualification still in effect, clear it
15366              now; we will be starting fresh with the next declarator.  */
15367           parser->scope = NULL_TREE;
15368           parser->qualifying_scope = NULL_TREE;
15369           parser->object_scope = NULL_TREE;
15370           /* If it's a `,', then there are more declarators.  */
15371           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15372             cp_lexer_consume_token (parser->lexer);
15373           /* If the next token isn't a `;', then we have a parse error.  */
15374           else if (cp_lexer_next_token_is_not (parser->lexer,
15375                                                CPP_SEMICOLON))
15376             {
15377               cp_parser_error (parser, "expected %<;%>");
15378               /* Skip tokens until we find a `;'.  */
15379               cp_parser_skip_to_end_of_statement (parser);
15380
15381               break;
15382             }
15383
15384           if (decl)
15385             {
15386               /* Add DECL to the list of members.  */
15387               if (!friend_p)
15388                 finish_member_declaration (decl);
15389
15390               if (TREE_CODE (decl) == FUNCTION_DECL)
15391                 cp_parser_save_default_args (parser, decl);
15392             }
15393         }
15394     }
15395
15396   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15397 }
15398
15399 /* Parse a pure-specifier.
15400
15401    pure-specifier:
15402      = 0
15403
15404    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15405    Otherwise, ERROR_MARK_NODE is returned.  */
15406
15407 static tree
15408 cp_parser_pure_specifier (cp_parser* parser)
15409 {
15410   cp_token *token;
15411
15412   /* Look for the `=' token.  */
15413   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15414     return error_mark_node;
15415   /* Look for the `0' token.  */
15416   token = cp_lexer_consume_token (parser->lexer);
15417   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15418   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15419     {
15420       cp_parser_error (parser,
15421                        "invalid pure specifier (only %<= 0%> is allowed)");
15422       cp_parser_skip_to_end_of_statement (parser);
15423       return error_mark_node;
15424     }
15425   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15426     {
15427       error ("templates may not be %<virtual%>");
15428       return error_mark_node;
15429     }
15430
15431   return integer_zero_node;
15432 }
15433
15434 /* Parse a constant-initializer.
15435
15436    constant-initializer:
15437      = constant-expression
15438
15439    Returns a representation of the constant-expression.  */
15440
15441 static tree
15442 cp_parser_constant_initializer (cp_parser* parser)
15443 {
15444   /* Look for the `=' token.  */
15445   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15446     return error_mark_node;
15447
15448   /* It is invalid to write:
15449
15450        struct S { static const int i = { 7 }; };
15451
15452      */
15453   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15454     {
15455       cp_parser_error (parser,
15456                        "a brace-enclosed initializer is not allowed here");
15457       /* Consume the opening brace.  */
15458       cp_lexer_consume_token (parser->lexer);
15459       /* Skip the initializer.  */
15460       cp_parser_skip_to_closing_brace (parser);
15461       /* Look for the trailing `}'.  */
15462       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15463
15464       return error_mark_node;
15465     }
15466
15467   return cp_parser_constant_expression (parser,
15468                                         /*allow_non_constant=*/false,
15469                                         NULL);
15470 }
15471
15472 /* Derived classes [gram.class.derived] */
15473
15474 /* Parse a base-clause.
15475
15476    base-clause:
15477      : base-specifier-list
15478
15479    base-specifier-list:
15480      base-specifier ... [opt]
15481      base-specifier-list , base-specifier ... [opt]
15482
15483    Returns a TREE_LIST representing the base-classes, in the order in
15484    which they were declared.  The representation of each node is as
15485    described by cp_parser_base_specifier.
15486
15487    In the case that no bases are specified, this function will return
15488    NULL_TREE, not ERROR_MARK_NODE.  */
15489
15490 static tree
15491 cp_parser_base_clause (cp_parser* parser)
15492 {
15493   tree bases = NULL_TREE;
15494
15495   /* Look for the `:' that begins the list.  */
15496   cp_parser_require (parser, CPP_COLON, "%<:%>");
15497
15498   /* Scan the base-specifier-list.  */
15499   while (true)
15500     {
15501       cp_token *token;
15502       tree base;
15503       bool pack_expansion_p = false;
15504
15505       /* Look for the base-specifier.  */
15506       base = cp_parser_base_specifier (parser);
15507       /* Look for the (optional) ellipsis. */
15508       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15509         {
15510           /* Consume the `...'. */
15511           cp_lexer_consume_token (parser->lexer);
15512
15513           pack_expansion_p = true;
15514         }
15515
15516       /* Add BASE to the front of the list.  */
15517       if (base != error_mark_node)
15518         {
15519           if (pack_expansion_p)
15520             /* Make this a pack expansion type. */
15521             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15522           
15523
15524           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15525             {
15526               TREE_CHAIN (base) = bases;
15527               bases = base;
15528             }
15529         }
15530       /* Peek at the next token.  */
15531       token = cp_lexer_peek_token (parser->lexer);
15532       /* If it's not a comma, then the list is complete.  */
15533       if (token->type != CPP_COMMA)
15534         break;
15535       /* Consume the `,'.  */
15536       cp_lexer_consume_token (parser->lexer);
15537     }
15538
15539   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15540      base class had a qualified name.  However, the next name that
15541      appears is certainly not qualified.  */
15542   parser->scope = NULL_TREE;
15543   parser->qualifying_scope = NULL_TREE;
15544   parser->object_scope = NULL_TREE;
15545
15546   return nreverse (bases);
15547 }
15548
15549 /* Parse a base-specifier.
15550
15551    base-specifier:
15552      :: [opt] nested-name-specifier [opt] class-name
15553      virtual access-specifier [opt] :: [opt] nested-name-specifier
15554        [opt] class-name
15555      access-specifier virtual [opt] :: [opt] nested-name-specifier
15556        [opt] class-name
15557
15558    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15559    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15560    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15561    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15562
15563 static tree
15564 cp_parser_base_specifier (cp_parser* parser)
15565 {
15566   cp_token *token;
15567   bool done = false;
15568   bool virtual_p = false;
15569   bool duplicate_virtual_error_issued_p = false;
15570   bool duplicate_access_error_issued_p = false;
15571   bool class_scope_p, template_p;
15572   tree access = access_default_node;
15573   tree type;
15574
15575   /* Process the optional `virtual' and `access-specifier'.  */
15576   while (!done)
15577     {
15578       /* Peek at the next token.  */
15579       token = cp_lexer_peek_token (parser->lexer);
15580       /* Process `virtual'.  */
15581       switch (token->keyword)
15582         {
15583         case RID_VIRTUAL:
15584           /* If `virtual' appears more than once, issue an error.  */
15585           if (virtual_p && !duplicate_virtual_error_issued_p)
15586             {
15587               cp_parser_error (parser,
15588                                "%<virtual%> specified more than once in base-specified");
15589               duplicate_virtual_error_issued_p = true;
15590             }
15591
15592           virtual_p = true;
15593
15594           /* Consume the `virtual' token.  */
15595           cp_lexer_consume_token (parser->lexer);
15596
15597           break;
15598
15599         case RID_PUBLIC:
15600         case RID_PROTECTED:
15601         case RID_PRIVATE:
15602           /* If more than one access specifier appears, issue an
15603              error.  */
15604           if (access != access_default_node
15605               && !duplicate_access_error_issued_p)
15606             {
15607               cp_parser_error (parser,
15608                                "more than one access specifier in base-specified");
15609               duplicate_access_error_issued_p = true;
15610             }
15611
15612           access = ridpointers[(int) token->keyword];
15613
15614           /* Consume the access-specifier.  */
15615           cp_lexer_consume_token (parser->lexer);
15616
15617           break;
15618
15619         default:
15620           done = true;
15621           break;
15622         }
15623     }
15624   /* It is not uncommon to see programs mechanically, erroneously, use
15625      the 'typename' keyword to denote (dependent) qualified types
15626      as base classes.  */
15627   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15628     {
15629       if (!processing_template_decl)
15630         error ("keyword %<typename%> not allowed outside of templates");
15631       else
15632         error ("keyword %<typename%> not allowed in this context "
15633                "(the base class is implicitly a type)");
15634       cp_lexer_consume_token (parser->lexer);
15635     }
15636
15637   /* Look for the optional `::' operator.  */
15638   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15639   /* Look for the nested-name-specifier.  The simplest way to
15640      implement:
15641
15642        [temp.res]
15643
15644        The keyword `typename' is not permitted in a base-specifier or
15645        mem-initializer; in these contexts a qualified name that
15646        depends on a template-parameter is implicitly assumed to be a
15647        type name.
15648
15649      is to pretend that we have seen the `typename' keyword at this
15650      point.  */
15651   cp_parser_nested_name_specifier_opt (parser,
15652                                        /*typename_keyword_p=*/true,
15653                                        /*check_dependency_p=*/true,
15654                                        typename_type,
15655                                        /*is_declaration=*/true);
15656   /* If the base class is given by a qualified name, assume that names
15657      we see are type names or templates, as appropriate.  */
15658   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15659   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15660
15661   /* Finally, look for the class-name.  */
15662   type = cp_parser_class_name (parser,
15663                                class_scope_p,
15664                                template_p,
15665                                typename_type,
15666                                /*check_dependency_p=*/true,
15667                                /*class_head_p=*/false,
15668                                /*is_declaration=*/true);
15669
15670   if (type == error_mark_node)
15671     return error_mark_node;
15672
15673   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15674 }
15675
15676 /* Exception handling [gram.exception] */
15677
15678 /* Parse an (optional) exception-specification.
15679
15680    exception-specification:
15681      throw ( type-id-list [opt] )
15682
15683    Returns a TREE_LIST representing the exception-specification.  The
15684    TREE_VALUE of each node is a type.  */
15685
15686 static tree
15687 cp_parser_exception_specification_opt (cp_parser* parser)
15688 {
15689   cp_token *token;
15690   tree type_id_list;
15691
15692   /* Peek at the next token.  */
15693   token = cp_lexer_peek_token (parser->lexer);
15694   /* If it's not `throw', then there's no exception-specification.  */
15695   if (!cp_parser_is_keyword (token, RID_THROW))
15696     return NULL_TREE;
15697
15698   /* Consume the `throw'.  */
15699   cp_lexer_consume_token (parser->lexer);
15700
15701   /* Look for the `('.  */
15702   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15703
15704   /* Peek at the next token.  */
15705   token = cp_lexer_peek_token (parser->lexer);
15706   /* If it's not a `)', then there is a type-id-list.  */
15707   if (token->type != CPP_CLOSE_PAREN)
15708     {
15709       const char *saved_message;
15710
15711       /* Types may not be defined in an exception-specification.  */
15712       saved_message = parser->type_definition_forbidden_message;
15713       parser->type_definition_forbidden_message
15714         = "types may not be defined in an exception-specification";
15715       /* Parse the type-id-list.  */
15716       type_id_list = cp_parser_type_id_list (parser);
15717       /* Restore the saved message.  */
15718       parser->type_definition_forbidden_message = saved_message;
15719     }
15720   else
15721     type_id_list = empty_except_spec;
15722
15723   /* Look for the `)'.  */
15724   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15725
15726   return type_id_list;
15727 }
15728
15729 /* Parse an (optional) type-id-list.
15730
15731    type-id-list:
15732      type-id ... [opt]
15733      type-id-list , type-id ... [opt]
15734
15735    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
15736    in the order that the types were presented.  */
15737
15738 static tree
15739 cp_parser_type_id_list (cp_parser* parser)
15740 {
15741   tree types = NULL_TREE;
15742
15743   while (true)
15744     {
15745       cp_token *token;
15746       tree type;
15747
15748       /* Get the next type-id.  */
15749       type = cp_parser_type_id (parser);
15750       /* Parse the optional ellipsis. */
15751       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15752         {
15753           /* Consume the `...'. */
15754           cp_lexer_consume_token (parser->lexer);
15755
15756           /* Turn the type into a pack expansion expression. */
15757           type = make_pack_expansion (type);
15758         }
15759       /* Add it to the list.  */
15760       types = add_exception_specifier (types, type, /*complain=*/1);
15761       /* Peek at the next token.  */
15762       token = cp_lexer_peek_token (parser->lexer);
15763       /* If it is not a `,', we are done.  */
15764       if (token->type != CPP_COMMA)
15765         break;
15766       /* Consume the `,'.  */
15767       cp_lexer_consume_token (parser->lexer);
15768     }
15769
15770   return nreverse (types);
15771 }
15772
15773 /* Parse a try-block.
15774
15775    try-block:
15776      try compound-statement handler-seq  */
15777
15778 static tree
15779 cp_parser_try_block (cp_parser* parser)
15780 {
15781   tree try_block;
15782
15783   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
15784   try_block = begin_try_block ();
15785   cp_parser_compound_statement (parser, NULL, true);
15786   finish_try_block (try_block);
15787   cp_parser_handler_seq (parser);
15788   finish_handler_sequence (try_block);
15789
15790   return try_block;
15791 }
15792
15793 /* Parse a function-try-block.
15794
15795    function-try-block:
15796      try ctor-initializer [opt] function-body handler-seq  */
15797
15798 static bool
15799 cp_parser_function_try_block (cp_parser* parser)
15800 {
15801   tree compound_stmt;
15802   tree try_block;
15803   bool ctor_initializer_p;
15804
15805   /* Look for the `try' keyword.  */
15806   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
15807     return false;
15808   /* Let the rest of the front end know where we are.  */
15809   try_block = begin_function_try_block (&compound_stmt);
15810   /* Parse the function-body.  */
15811   ctor_initializer_p
15812     = cp_parser_ctor_initializer_opt_and_function_body (parser);
15813   /* We're done with the `try' part.  */
15814   finish_function_try_block (try_block);
15815   /* Parse the handlers.  */
15816   cp_parser_handler_seq (parser);
15817   /* We're done with the handlers.  */
15818   finish_function_handler_sequence (try_block, compound_stmt);
15819
15820   return ctor_initializer_p;
15821 }
15822
15823 /* Parse a handler-seq.
15824
15825    handler-seq:
15826      handler handler-seq [opt]  */
15827
15828 static void
15829 cp_parser_handler_seq (cp_parser* parser)
15830 {
15831   while (true)
15832     {
15833       cp_token *token;
15834
15835       /* Parse the handler.  */
15836       cp_parser_handler (parser);
15837       /* Peek at the next token.  */
15838       token = cp_lexer_peek_token (parser->lexer);
15839       /* If it's not `catch' then there are no more handlers.  */
15840       if (!cp_parser_is_keyword (token, RID_CATCH))
15841         break;
15842     }
15843 }
15844
15845 /* Parse a handler.
15846
15847    handler:
15848      catch ( exception-declaration ) compound-statement  */
15849
15850 static void
15851 cp_parser_handler (cp_parser* parser)
15852 {
15853   tree handler;
15854   tree declaration;
15855
15856   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
15857   handler = begin_handler ();
15858   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15859   declaration = cp_parser_exception_declaration (parser);
15860   finish_handler_parms (declaration, handler);
15861   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15862   cp_parser_compound_statement (parser, NULL, false);
15863   finish_handler (handler);
15864 }
15865
15866 /* Parse an exception-declaration.
15867
15868    exception-declaration:
15869      type-specifier-seq declarator
15870      type-specifier-seq abstract-declarator
15871      type-specifier-seq
15872      ...
15873
15874    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15875    ellipsis variant is used.  */
15876
15877 static tree
15878 cp_parser_exception_declaration (cp_parser* parser)
15879 {
15880   cp_decl_specifier_seq type_specifiers;
15881   cp_declarator *declarator;
15882   const char *saved_message;
15883
15884   /* If it's an ellipsis, it's easy to handle.  */
15885   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15886     {
15887       /* Consume the `...' token.  */
15888       cp_lexer_consume_token (parser->lexer);
15889       return NULL_TREE;
15890     }
15891
15892   /* Types may not be defined in exception-declarations.  */
15893   saved_message = parser->type_definition_forbidden_message;
15894   parser->type_definition_forbidden_message
15895     = "types may not be defined in exception-declarations";
15896
15897   /* Parse the type-specifier-seq.  */
15898   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15899                                 &type_specifiers);
15900   /* If it's a `)', then there is no declarator.  */
15901   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15902     declarator = NULL;
15903   else
15904     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15905                                        /*ctor_dtor_or_conv_p=*/NULL,
15906                                        /*parenthesized_p=*/NULL,
15907                                        /*member_p=*/false);
15908
15909   /* Restore the saved message.  */
15910   parser->type_definition_forbidden_message = saved_message;
15911
15912   if (!type_specifiers.any_specifiers_p)
15913     return error_mark_node;
15914
15915   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15916 }
15917
15918 /* Parse a throw-expression.
15919
15920    throw-expression:
15921      throw assignment-expression [opt]
15922
15923    Returns a THROW_EXPR representing the throw-expression.  */
15924
15925 static tree
15926 cp_parser_throw_expression (cp_parser* parser)
15927 {
15928   tree expression;
15929   cp_token* token;
15930
15931   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
15932   token = cp_lexer_peek_token (parser->lexer);
15933   /* Figure out whether or not there is an assignment-expression
15934      following the "throw" keyword.  */
15935   if (token->type == CPP_COMMA
15936       || token->type == CPP_SEMICOLON
15937       || token->type == CPP_CLOSE_PAREN
15938       || token->type == CPP_CLOSE_SQUARE
15939       || token->type == CPP_CLOSE_BRACE
15940       || token->type == CPP_COLON)
15941     expression = NULL_TREE;
15942   else
15943     expression = cp_parser_assignment_expression (parser,
15944                                                   /*cast_p=*/false);
15945
15946   return build_throw (expression);
15947 }
15948
15949 /* GNU Extensions */
15950
15951 /* Parse an (optional) asm-specification.
15952
15953    asm-specification:
15954      asm ( string-literal )
15955
15956    If the asm-specification is present, returns a STRING_CST
15957    corresponding to the string-literal.  Otherwise, returns
15958    NULL_TREE.  */
15959
15960 static tree
15961 cp_parser_asm_specification_opt (cp_parser* parser)
15962 {
15963   cp_token *token;
15964   tree asm_specification;
15965
15966   /* Peek at the next token.  */
15967   token = cp_lexer_peek_token (parser->lexer);
15968   /* If the next token isn't the `asm' keyword, then there's no
15969      asm-specification.  */
15970   if (!cp_parser_is_keyword (token, RID_ASM))
15971     return NULL_TREE;
15972
15973   /* Consume the `asm' token.  */
15974   cp_lexer_consume_token (parser->lexer);
15975   /* Look for the `('.  */
15976   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15977
15978   /* Look for the string-literal.  */
15979   asm_specification = cp_parser_string_literal (parser, false, false);
15980
15981   /* Look for the `)'.  */
15982   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15983
15984   return asm_specification;
15985 }
15986
15987 /* Parse an asm-operand-list.
15988
15989    asm-operand-list:
15990      asm-operand
15991      asm-operand-list , asm-operand
15992
15993    asm-operand:
15994      string-literal ( expression )
15995      [ string-literal ] string-literal ( expression )
15996
15997    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15998    each node is the expression.  The TREE_PURPOSE is itself a
15999    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16000    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16001    is a STRING_CST for the string literal before the parenthesis. Returns
16002    ERROR_MARK_NODE if any of the operands are invalid.  */
16003
16004 static tree
16005 cp_parser_asm_operand_list (cp_parser* parser)
16006 {
16007   tree asm_operands = NULL_TREE;
16008   bool invalid_operands = false;
16009
16010   while (true)
16011     {
16012       tree string_literal;
16013       tree expression;
16014       tree name;
16015
16016       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16017         {
16018           /* Consume the `[' token.  */
16019           cp_lexer_consume_token (parser->lexer);
16020           /* Read the operand name.  */
16021           name = cp_parser_identifier (parser);
16022           if (name != error_mark_node)
16023             name = build_string (IDENTIFIER_LENGTH (name),
16024                                  IDENTIFIER_POINTER (name));
16025           /* Look for the closing `]'.  */
16026           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16027         }
16028       else
16029         name = NULL_TREE;
16030       /* Look for the string-literal.  */
16031       string_literal = cp_parser_string_literal (parser, false, false);
16032
16033       /* Look for the `('.  */
16034       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16035       /* Parse the expression.  */
16036       expression = cp_parser_expression (parser, /*cast_p=*/false);
16037       /* Look for the `)'.  */
16038       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16039
16040       if (name == error_mark_node 
16041           || string_literal == error_mark_node 
16042           || expression == error_mark_node)
16043         invalid_operands = true;
16044
16045       /* Add this operand to the list.  */
16046       asm_operands = tree_cons (build_tree_list (name, string_literal),
16047                                 expression,
16048                                 asm_operands);
16049       /* If the next token is not a `,', there are no more
16050          operands.  */
16051       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16052         break;
16053       /* Consume the `,'.  */
16054       cp_lexer_consume_token (parser->lexer);
16055     }
16056
16057   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16058 }
16059
16060 /* Parse an asm-clobber-list.
16061
16062    asm-clobber-list:
16063      string-literal
16064      asm-clobber-list , string-literal
16065
16066    Returns a TREE_LIST, indicating the clobbers in the order that they
16067    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16068
16069 static tree
16070 cp_parser_asm_clobber_list (cp_parser* parser)
16071 {
16072   tree clobbers = NULL_TREE;
16073
16074   while (true)
16075     {
16076       tree string_literal;
16077
16078       /* Look for the string literal.  */
16079       string_literal = cp_parser_string_literal (parser, false, false);
16080       /* Add it to the list.  */
16081       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16082       /* If the next token is not a `,', then the list is
16083          complete.  */
16084       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16085         break;
16086       /* Consume the `,' token.  */
16087       cp_lexer_consume_token (parser->lexer);
16088     }
16089
16090   return clobbers;
16091 }
16092
16093 /* Parse an (optional) series of attributes.
16094
16095    attributes:
16096      attributes attribute
16097
16098    attribute:
16099      __attribute__ (( attribute-list [opt] ))
16100
16101    The return value is as for cp_parser_attribute_list.  */
16102
16103 static tree
16104 cp_parser_attributes_opt (cp_parser* parser)
16105 {
16106   tree attributes = NULL_TREE;
16107
16108   while (true)
16109     {
16110       cp_token *token;
16111       tree attribute_list;
16112
16113       /* Peek at the next token.  */
16114       token = cp_lexer_peek_token (parser->lexer);
16115       /* If it's not `__attribute__', then we're done.  */
16116       if (token->keyword != RID_ATTRIBUTE)
16117         break;
16118
16119       /* Consume the `__attribute__' keyword.  */
16120       cp_lexer_consume_token (parser->lexer);
16121       /* Look for the two `(' tokens.  */
16122       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16123       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16124
16125       /* Peek at the next token.  */
16126       token = cp_lexer_peek_token (parser->lexer);
16127       if (token->type != CPP_CLOSE_PAREN)
16128         /* Parse the attribute-list.  */
16129         attribute_list = cp_parser_attribute_list (parser);
16130       else
16131         /* If the next token is a `)', then there is no attribute
16132            list.  */
16133         attribute_list = NULL;
16134
16135       /* Look for the two `)' tokens.  */
16136       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16137       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16138
16139       /* Add these new attributes to the list.  */
16140       attributes = chainon (attributes, attribute_list);
16141     }
16142
16143   return attributes;
16144 }
16145
16146 /* Parse an attribute-list.
16147
16148    attribute-list:
16149      attribute
16150      attribute-list , attribute
16151
16152    attribute:
16153      identifier
16154      identifier ( identifier )
16155      identifier ( identifier , expression-list )
16156      identifier ( expression-list )
16157
16158    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16159    to an attribute.  The TREE_PURPOSE of each node is the identifier
16160    indicating which attribute is in use.  The TREE_VALUE represents
16161    the arguments, if any.  */
16162
16163 static tree
16164 cp_parser_attribute_list (cp_parser* parser)
16165 {
16166   tree attribute_list = NULL_TREE;
16167   bool save_translate_strings_p = parser->translate_strings_p;
16168
16169   parser->translate_strings_p = false;
16170   while (true)
16171     {
16172       cp_token *token;
16173       tree identifier;
16174       tree attribute;
16175
16176       /* Look for the identifier.  We also allow keywords here; for
16177          example `__attribute__ ((const))' is legal.  */
16178       token = cp_lexer_peek_token (parser->lexer);
16179       if (token->type == CPP_NAME
16180           || token->type == CPP_KEYWORD)
16181         {
16182           tree arguments = NULL_TREE;
16183
16184           /* Consume the token.  */
16185           token = cp_lexer_consume_token (parser->lexer);
16186
16187           /* Save away the identifier that indicates which attribute
16188              this is.  */
16189           identifier = token->u.value;
16190           attribute = build_tree_list (identifier, NULL_TREE);
16191
16192           /* Peek at the next token.  */
16193           token = cp_lexer_peek_token (parser->lexer);
16194           /* If it's an `(', then parse the attribute arguments.  */
16195           if (token->type == CPP_OPEN_PAREN)
16196             {
16197               arguments = cp_parser_parenthesized_expression_list
16198                           (parser, true, /*cast_p=*/false,
16199                            /*allow_expansion_p=*/false,
16200                            /*non_constant_p=*/NULL);
16201               /* Save the arguments away.  */
16202               TREE_VALUE (attribute) = arguments;
16203             }
16204
16205           if (arguments != error_mark_node)
16206             {
16207               /* Add this attribute to the list.  */
16208               TREE_CHAIN (attribute) = attribute_list;
16209               attribute_list = attribute;
16210             }
16211
16212           token = cp_lexer_peek_token (parser->lexer);
16213         }
16214       /* Now, look for more attributes.  If the next token isn't a
16215          `,', we're done.  */
16216       if (token->type != CPP_COMMA)
16217         break;
16218
16219       /* Consume the comma and keep going.  */
16220       cp_lexer_consume_token (parser->lexer);
16221     }
16222   parser->translate_strings_p = save_translate_strings_p;
16223
16224   /* We built up the list in reverse order.  */
16225   return nreverse (attribute_list);
16226 }
16227
16228 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16229    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16230    current value of the PEDANTIC flag, regardless of whether or not
16231    the `__extension__' keyword is present.  The caller is responsible
16232    for restoring the value of the PEDANTIC flag.  */
16233
16234 static bool
16235 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16236 {
16237   /* Save the old value of the PEDANTIC flag.  */
16238   *saved_pedantic = pedantic;
16239
16240   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16241     {
16242       /* Consume the `__extension__' token.  */
16243       cp_lexer_consume_token (parser->lexer);
16244       /* We're not being pedantic while the `__extension__' keyword is
16245          in effect.  */
16246       pedantic = 0;
16247
16248       return true;
16249     }
16250
16251   return false;
16252 }
16253
16254 /* Parse a label declaration.
16255
16256    label-declaration:
16257      __label__ label-declarator-seq ;
16258
16259    label-declarator-seq:
16260      identifier , label-declarator-seq
16261      identifier  */
16262
16263 static void
16264 cp_parser_label_declaration (cp_parser* parser)
16265 {
16266   /* Look for the `__label__' keyword.  */
16267   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16268
16269   while (true)
16270     {
16271       tree identifier;
16272
16273       /* Look for an identifier.  */
16274       identifier = cp_parser_identifier (parser);
16275       /* If we failed, stop.  */
16276       if (identifier == error_mark_node)
16277         break;
16278       /* Declare it as a label.  */
16279       finish_label_decl (identifier);
16280       /* If the next token is a `;', stop.  */
16281       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16282         break;
16283       /* Look for the `,' separating the label declarations.  */
16284       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16285     }
16286
16287   /* Look for the final `;'.  */
16288   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16289 }
16290
16291 /* Support Functions */
16292
16293 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16294    NAME should have one of the representations used for an
16295    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16296    is returned.  If PARSER->SCOPE is a dependent type, then a
16297    SCOPE_REF is returned.
16298
16299    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16300    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16301    was formed.  Abstractly, such entities should not be passed to this
16302    function, because they do not need to be looked up, but it is
16303    simpler to check for this special case here, rather than at the
16304    call-sites.
16305
16306    In cases not explicitly covered above, this function returns a
16307    DECL, OVERLOAD, or baselink representing the result of the lookup.
16308    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16309    is returned.
16310
16311    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16312    (e.g., "struct") that was used.  In that case bindings that do not
16313    refer to types are ignored.
16314
16315    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16316    ignored.
16317
16318    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16319    are ignored.
16320
16321    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16322    types.
16323
16324    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16325    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16326    NULL_TREE otherwise.  */
16327
16328 static tree
16329 cp_parser_lookup_name (cp_parser *parser, tree name,
16330                        enum tag_types tag_type,
16331                        bool is_template,
16332                        bool is_namespace,
16333                        bool check_dependency,
16334                        tree *ambiguous_decls)
16335 {
16336   int flags = 0;
16337   tree decl;
16338   tree object_type = parser->context->object_type;
16339
16340   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16341     flags |= LOOKUP_COMPLAIN;
16342
16343   /* Assume that the lookup will be unambiguous.  */
16344   if (ambiguous_decls)
16345     *ambiguous_decls = NULL_TREE;
16346
16347   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16348      no longer valid.  Note that if we are parsing tentatively, and
16349      the parse fails, OBJECT_TYPE will be automatically restored.  */
16350   parser->context->object_type = NULL_TREE;
16351
16352   if (name == error_mark_node)
16353     return error_mark_node;
16354
16355   /* A template-id has already been resolved; there is no lookup to
16356      do.  */
16357   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16358     return name;
16359   if (BASELINK_P (name))
16360     {
16361       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16362                   == TEMPLATE_ID_EXPR);
16363       return name;
16364     }
16365
16366   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16367      it should already have been checked to make sure that the name
16368      used matches the type being destroyed.  */
16369   if (TREE_CODE (name) == BIT_NOT_EXPR)
16370     {
16371       tree type;
16372
16373       /* Figure out to which type this destructor applies.  */
16374       if (parser->scope)
16375         type = parser->scope;
16376       else if (object_type)
16377         type = object_type;
16378       else
16379         type = current_class_type;
16380       /* If that's not a class type, there is no destructor.  */
16381       if (!type || !CLASS_TYPE_P (type))
16382         return error_mark_node;
16383       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16384         lazily_declare_fn (sfk_destructor, type);
16385       if (!CLASSTYPE_DESTRUCTORS (type))
16386           return error_mark_node;
16387       /* If it was a class type, return the destructor.  */
16388       return CLASSTYPE_DESTRUCTORS (type);
16389     }
16390
16391   /* By this point, the NAME should be an ordinary identifier.  If
16392      the id-expression was a qualified name, the qualifying scope is
16393      stored in PARSER->SCOPE at this point.  */
16394   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16395
16396   /* Perform the lookup.  */
16397   if (parser->scope)
16398     {
16399       bool dependent_p;
16400
16401       if (parser->scope == error_mark_node)
16402         return error_mark_node;
16403
16404       /* If the SCOPE is dependent, the lookup must be deferred until
16405          the template is instantiated -- unless we are explicitly
16406          looking up names in uninstantiated templates.  Even then, we
16407          cannot look up the name if the scope is not a class type; it
16408          might, for example, be a template type parameter.  */
16409       dependent_p = (TYPE_P (parser->scope)
16410                      && !(parser->in_declarator_p
16411                           && currently_open_class (parser->scope))
16412                      && dependent_type_p (parser->scope));
16413       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16414            && dependent_p)
16415         {
16416           if (tag_type)
16417             {
16418               tree type;
16419
16420               /* The resolution to Core Issue 180 says that `struct
16421                  A::B' should be considered a type-name, even if `A'
16422                  is dependent.  */
16423               type = make_typename_type (parser->scope, name, tag_type,
16424                                          /*complain=*/tf_error);
16425               decl = TYPE_NAME (type);
16426             }
16427           else if (is_template
16428                    && (cp_parser_next_token_ends_template_argument_p (parser)
16429                        || cp_lexer_next_token_is (parser->lexer,
16430                                                   CPP_CLOSE_PAREN)))
16431             decl = make_unbound_class_template (parser->scope,
16432                                                 name, NULL_TREE,
16433                                                 /*complain=*/tf_error);
16434           else
16435             decl = build_qualified_name (/*type=*/NULL_TREE,
16436                                          parser->scope, name,
16437                                          is_template);
16438         }
16439       else
16440         {
16441           tree pushed_scope = NULL_TREE;
16442
16443           /* If PARSER->SCOPE is a dependent type, then it must be a
16444              class type, and we must not be checking dependencies;
16445              otherwise, we would have processed this lookup above.  So
16446              that PARSER->SCOPE is not considered a dependent base by
16447              lookup_member, we must enter the scope here.  */
16448           if (dependent_p)
16449             pushed_scope = push_scope (parser->scope);
16450           /* If the PARSER->SCOPE is a template specialization, it
16451              may be instantiated during name lookup.  In that case,
16452              errors may be issued.  Even if we rollback the current
16453              tentative parse, those errors are valid.  */
16454           decl = lookup_qualified_name (parser->scope, name,
16455                                         tag_type != none_type,
16456                                         /*complain=*/true);
16457
16458           /* If we have a single function from a using decl, pull it out.  */
16459           if (decl
16460               && TREE_CODE (decl) == OVERLOAD
16461               && !really_overloaded_fn (decl))
16462             decl = OVL_FUNCTION (decl);
16463
16464           if (pushed_scope)
16465             pop_scope (pushed_scope);
16466         }
16467       parser->qualifying_scope = parser->scope;
16468       parser->object_scope = NULL_TREE;
16469     }
16470   else if (object_type)
16471     {
16472       tree object_decl = NULL_TREE;
16473       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16474          OBJECT_TYPE is not a class.  */
16475       if (CLASS_TYPE_P (object_type))
16476         /* If the OBJECT_TYPE is a template specialization, it may
16477            be instantiated during name lookup.  In that case, errors
16478            may be issued.  Even if we rollback the current tentative
16479            parse, those errors are valid.  */
16480         object_decl = lookup_member (object_type,
16481                                      name,
16482                                      /*protect=*/0,
16483                                      tag_type != none_type);
16484       /* Look it up in the enclosing context, too.  */
16485       decl = lookup_name_real (name, tag_type != none_type,
16486                                /*nonclass=*/0,
16487                                /*block_p=*/true, is_namespace, flags);
16488       parser->object_scope = object_type;
16489       parser->qualifying_scope = NULL_TREE;
16490       if (object_decl)
16491         decl = object_decl;
16492     }
16493   else
16494     {
16495       decl = lookup_name_real (name, tag_type != none_type,
16496                                /*nonclass=*/0,
16497                                /*block_p=*/true, is_namespace, flags);
16498       parser->qualifying_scope = NULL_TREE;
16499       parser->object_scope = NULL_TREE;
16500     }
16501
16502   /* If the lookup failed, let our caller know.  */
16503   if (!decl || decl == error_mark_node)
16504     return error_mark_node;
16505
16506   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16507   if (TREE_CODE (decl) == TREE_LIST)
16508     {
16509       if (ambiguous_decls)
16510         *ambiguous_decls = decl;
16511       /* The error message we have to print is too complicated for
16512          cp_parser_error, so we incorporate its actions directly.  */
16513       if (!cp_parser_simulate_error (parser))
16514         {
16515           error ("reference to %qD is ambiguous", name);
16516           print_candidates (decl);
16517         }
16518       return error_mark_node;
16519     }
16520
16521   gcc_assert (DECL_P (decl)
16522               || TREE_CODE (decl) == OVERLOAD
16523               || TREE_CODE (decl) == SCOPE_REF
16524               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16525               || BASELINK_P (decl));
16526
16527   /* If we have resolved the name of a member declaration, check to
16528      see if the declaration is accessible.  When the name resolves to
16529      set of overloaded functions, accessibility is checked when
16530      overload resolution is done.
16531
16532      During an explicit instantiation, access is not checked at all,
16533      as per [temp.explicit].  */
16534   if (DECL_P (decl))
16535     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16536
16537   return decl;
16538 }
16539
16540 /* Like cp_parser_lookup_name, but for use in the typical case where
16541    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16542    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16543
16544 static tree
16545 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16546 {
16547   return cp_parser_lookup_name (parser, name,
16548                                 none_type,
16549                                 /*is_template=*/false,
16550                                 /*is_namespace=*/false,
16551                                 /*check_dependency=*/true,
16552                                 /*ambiguous_decls=*/NULL);
16553 }
16554
16555 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16556    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16557    true, the DECL indicates the class being defined in a class-head,
16558    or declared in an elaborated-type-specifier.
16559
16560    Otherwise, return DECL.  */
16561
16562 static tree
16563 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16564 {
16565   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16566      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16567
16568        struct A {
16569          template <typename T> struct B;
16570        };
16571
16572        template <typename T> struct A::B {};
16573
16574      Similarly, in an elaborated-type-specifier:
16575
16576        namespace N { struct X{}; }
16577
16578        struct A {
16579          template <typename T> friend struct N::X;
16580        };
16581
16582      However, if the DECL refers to a class type, and we are in
16583      the scope of the class, then the name lookup automatically
16584      finds the TYPE_DECL created by build_self_reference rather
16585      than a TEMPLATE_DECL.  For example, in:
16586
16587        template <class T> struct S {
16588          S s;
16589        };
16590
16591      there is no need to handle such case.  */
16592
16593   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16594     return DECL_TEMPLATE_RESULT (decl);
16595
16596   return decl;
16597 }
16598
16599 /* If too many, or too few, template-parameter lists apply to the
16600    declarator, issue an error message.  Returns TRUE if all went well,
16601    and FALSE otherwise.  */
16602
16603 static bool
16604 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16605                                                 cp_declarator *declarator)
16606 {
16607   unsigned num_templates;
16608
16609   /* We haven't seen any classes that involve template parameters yet.  */
16610   num_templates = 0;
16611
16612   switch (declarator->kind)
16613     {
16614     case cdk_id:
16615       if (declarator->u.id.qualifying_scope)
16616         {
16617           tree scope;
16618           tree member;
16619
16620           scope = declarator->u.id.qualifying_scope;
16621           member = declarator->u.id.unqualified_name;
16622
16623           while (scope && CLASS_TYPE_P (scope))
16624             {
16625               /* You're supposed to have one `template <...>'
16626                  for every template class, but you don't need one
16627                  for a full specialization.  For example:
16628
16629                  template <class T> struct S{};
16630                  template <> struct S<int> { void f(); };
16631                  void S<int>::f () {}
16632
16633                  is correct; there shouldn't be a `template <>' for
16634                  the definition of `S<int>::f'.  */
16635               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16636                 /* If SCOPE does not have template information of any
16637                    kind, then it is not a template, nor is it nested
16638                    within a template.  */
16639                 break;
16640               if (explicit_class_specialization_p (scope))
16641                 break;
16642               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16643                 ++num_templates;
16644
16645               scope = TYPE_CONTEXT (scope);
16646             }
16647         }
16648       else if (TREE_CODE (declarator->u.id.unqualified_name)
16649                == TEMPLATE_ID_EXPR)
16650         /* If the DECLARATOR has the form `X<y>' then it uses one
16651            additional level of template parameters.  */
16652         ++num_templates;
16653
16654       return cp_parser_check_template_parameters (parser,
16655                                                   num_templates);
16656
16657     case cdk_function:
16658     case cdk_array:
16659     case cdk_pointer:
16660     case cdk_reference:
16661     case cdk_ptrmem:
16662       return (cp_parser_check_declarator_template_parameters
16663               (parser, declarator->declarator));
16664
16665     case cdk_error:
16666       return true;
16667
16668     default:
16669       gcc_unreachable ();
16670     }
16671   return false;
16672 }
16673
16674 /* NUM_TEMPLATES were used in the current declaration.  If that is
16675    invalid, return FALSE and issue an error messages.  Otherwise,
16676    return TRUE.  */
16677
16678 static bool
16679 cp_parser_check_template_parameters (cp_parser* parser,
16680                                      unsigned num_templates)
16681 {
16682   /* If there are more template classes than parameter lists, we have
16683      something like:
16684
16685        template <class T> void S<T>::R<T>::f ();  */
16686   if (parser->num_template_parameter_lists < num_templates)
16687     {
16688       error ("too few template-parameter-lists");
16689       return false;
16690     }
16691   /* If there are the same number of template classes and parameter
16692      lists, that's OK.  */
16693   if (parser->num_template_parameter_lists == num_templates)
16694     return true;
16695   /* If there are more, but only one more, then we are referring to a
16696      member template.  That's OK too.  */
16697   if (parser->num_template_parameter_lists == num_templates + 1)
16698       return true;
16699   /* Otherwise, there are too many template parameter lists.  We have
16700      something like:
16701
16702      template <class T> template <class U> void S::f();  */
16703   error ("too many template-parameter-lists");
16704   return false;
16705 }
16706
16707 /* Parse an optional `::' token indicating that the following name is
16708    from the global namespace.  If so, PARSER->SCOPE is set to the
16709    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16710    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16711    Returns the new value of PARSER->SCOPE, if the `::' token is
16712    present, and NULL_TREE otherwise.  */
16713
16714 static tree
16715 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16716 {
16717   cp_token *token;
16718
16719   /* Peek at the next token.  */
16720   token = cp_lexer_peek_token (parser->lexer);
16721   /* If we're looking at a `::' token then we're starting from the
16722      global namespace, not our current location.  */
16723   if (token->type == CPP_SCOPE)
16724     {
16725       /* Consume the `::' token.  */
16726       cp_lexer_consume_token (parser->lexer);
16727       /* Set the SCOPE so that we know where to start the lookup.  */
16728       parser->scope = global_namespace;
16729       parser->qualifying_scope = global_namespace;
16730       parser->object_scope = NULL_TREE;
16731
16732       return parser->scope;
16733     }
16734   else if (!current_scope_valid_p)
16735     {
16736       parser->scope = NULL_TREE;
16737       parser->qualifying_scope = NULL_TREE;
16738       parser->object_scope = NULL_TREE;
16739     }
16740
16741   return NULL_TREE;
16742 }
16743
16744 /* Returns TRUE if the upcoming token sequence is the start of a
16745    constructor declarator.  If FRIEND_P is true, the declarator is
16746    preceded by the `friend' specifier.  */
16747
16748 static bool
16749 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16750 {
16751   bool constructor_p;
16752   tree type_decl = NULL_TREE;
16753   bool nested_name_p;
16754   cp_token *next_token;
16755
16756   /* The common case is that this is not a constructor declarator, so
16757      try to avoid doing lots of work if at all possible.  It's not
16758      valid declare a constructor at function scope.  */
16759   if (parser->in_function_body)
16760     return false;
16761   /* And only certain tokens can begin a constructor declarator.  */
16762   next_token = cp_lexer_peek_token (parser->lexer);
16763   if (next_token->type != CPP_NAME
16764       && next_token->type != CPP_SCOPE
16765       && next_token->type != CPP_NESTED_NAME_SPECIFIER
16766       && next_token->type != CPP_TEMPLATE_ID)
16767     return false;
16768
16769   /* Parse tentatively; we are going to roll back all of the tokens
16770      consumed here.  */
16771   cp_parser_parse_tentatively (parser);
16772   /* Assume that we are looking at a constructor declarator.  */
16773   constructor_p = true;
16774
16775   /* Look for the optional `::' operator.  */
16776   cp_parser_global_scope_opt (parser,
16777                               /*current_scope_valid_p=*/false);
16778   /* Look for the nested-name-specifier.  */
16779   nested_name_p
16780     = (cp_parser_nested_name_specifier_opt (parser,
16781                                             /*typename_keyword_p=*/false,
16782                                             /*check_dependency_p=*/false,
16783                                             /*type_p=*/false,
16784                                             /*is_declaration=*/false)
16785        != NULL_TREE);
16786   /* Outside of a class-specifier, there must be a
16787      nested-name-specifier.  */
16788   if (!nested_name_p &&
16789       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16790        || friend_p))
16791     constructor_p = false;
16792   /* If we still think that this might be a constructor-declarator,
16793      look for a class-name.  */
16794   if (constructor_p)
16795     {
16796       /* If we have:
16797
16798            template <typename T> struct S { S(); };
16799            template <typename T> S<T>::S ();
16800
16801          we must recognize that the nested `S' names a class.
16802          Similarly, for:
16803
16804            template <typename T> S<T>::S<T> ();
16805
16806          we must recognize that the nested `S' names a template.  */
16807       type_decl = cp_parser_class_name (parser,
16808                                         /*typename_keyword_p=*/false,
16809                                         /*template_keyword_p=*/false,
16810                                         none_type,
16811                                         /*check_dependency_p=*/false,
16812                                         /*class_head_p=*/false,
16813                                         /*is_declaration=*/false);
16814       /* If there was no class-name, then this is not a constructor.  */
16815       constructor_p = !cp_parser_error_occurred (parser);
16816     }
16817
16818   /* If we're still considering a constructor, we have to see a `(',
16819      to begin the parameter-declaration-clause, followed by either a
16820      `)', an `...', or a decl-specifier.  We need to check for a
16821      type-specifier to avoid being fooled into thinking that:
16822
16823        S::S (f) (int);
16824
16825      is a constructor.  (It is actually a function named `f' that
16826      takes one parameter (of type `int') and returns a value of type
16827      `S::S'.  */
16828   if (constructor_p
16829       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
16830     {
16831       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16832           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16833           /* A parameter declaration begins with a decl-specifier,
16834              which is either the "attribute" keyword, a storage class
16835              specifier, or (usually) a type-specifier.  */
16836           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16837         {
16838           tree type;
16839           tree pushed_scope = NULL_TREE;
16840           unsigned saved_num_template_parameter_lists;
16841
16842           /* Names appearing in the type-specifier should be looked up
16843              in the scope of the class.  */
16844           if (current_class_type)
16845             type = NULL_TREE;
16846           else
16847             {
16848               type = TREE_TYPE (type_decl);
16849               if (TREE_CODE (type) == TYPENAME_TYPE)
16850                 {
16851                   type = resolve_typename_type (type,
16852                                                 /*only_current_p=*/false);
16853                   if (TREE_CODE (type) == TYPENAME_TYPE)
16854                     {
16855                       cp_parser_abort_tentative_parse (parser);
16856                       return false;
16857                     }
16858                 }
16859               pushed_scope = push_scope (type);
16860             }
16861
16862           /* Inside the constructor parameter list, surrounding
16863              template-parameter-lists do not apply.  */
16864           saved_num_template_parameter_lists
16865             = parser->num_template_parameter_lists;
16866           parser->num_template_parameter_lists = 0;
16867
16868           /* Look for the type-specifier.  */
16869           cp_parser_type_specifier (parser,
16870                                     CP_PARSER_FLAGS_NONE,
16871                                     /*decl_specs=*/NULL,
16872                                     /*is_declarator=*/true,
16873                                     /*declares_class_or_enum=*/NULL,
16874                                     /*is_cv_qualifier=*/NULL);
16875
16876           parser->num_template_parameter_lists
16877             = saved_num_template_parameter_lists;
16878
16879           /* Leave the scope of the class.  */
16880           if (pushed_scope)
16881             pop_scope (pushed_scope);
16882
16883           constructor_p = !cp_parser_error_occurred (parser);
16884         }
16885     }
16886   else
16887     constructor_p = false;
16888   /* We did not really want to consume any tokens.  */
16889   cp_parser_abort_tentative_parse (parser);
16890
16891   return constructor_p;
16892 }
16893
16894 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16895    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16896    they must be performed once we are in the scope of the function.
16897
16898    Returns the function defined.  */
16899
16900 static tree
16901 cp_parser_function_definition_from_specifiers_and_declarator
16902   (cp_parser* parser,
16903    cp_decl_specifier_seq *decl_specifiers,
16904    tree attributes,
16905    const cp_declarator *declarator)
16906 {
16907   tree fn;
16908   bool success_p;
16909
16910   /* Begin the function-definition.  */
16911   success_p = start_function (decl_specifiers, declarator, attributes);
16912
16913   /* The things we're about to see are not directly qualified by any
16914      template headers we've seen thus far.  */
16915   reset_specialization ();
16916
16917   /* If there were names looked up in the decl-specifier-seq that we
16918      did not check, check them now.  We must wait until we are in the
16919      scope of the function to perform the checks, since the function
16920      might be a friend.  */
16921   perform_deferred_access_checks ();
16922
16923   if (!success_p)
16924     {
16925       /* Skip the entire function.  */
16926       cp_parser_skip_to_end_of_block_or_statement (parser);
16927       fn = error_mark_node;
16928     }
16929   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16930     {
16931       /* Seen already, skip it.  An error message has already been output.  */
16932       cp_parser_skip_to_end_of_block_or_statement (parser);
16933       fn = current_function_decl;
16934       current_function_decl = NULL_TREE;
16935       /* If this is a function from a class, pop the nested class.  */
16936       if (current_class_name)
16937         pop_nested_class ();
16938     }
16939   else
16940     fn = cp_parser_function_definition_after_declarator (parser,
16941                                                          /*inline_p=*/false);
16942
16943   return fn;
16944 }
16945
16946 /* Parse the part of a function-definition that follows the
16947    declarator.  INLINE_P is TRUE iff this function is an inline
16948    function defined with a class-specifier.
16949
16950    Returns the function defined.  */
16951
16952 static tree
16953 cp_parser_function_definition_after_declarator (cp_parser* parser,
16954                                                 bool inline_p)
16955 {
16956   tree fn;
16957   bool ctor_initializer_p = false;
16958   bool saved_in_unbraced_linkage_specification_p;
16959   bool saved_in_function_body;
16960   unsigned saved_num_template_parameter_lists;
16961
16962   saved_in_function_body = parser->in_function_body;
16963   parser->in_function_body = true;
16964   /* If the next token is `return', then the code may be trying to
16965      make use of the "named return value" extension that G++ used to
16966      support.  */
16967   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16968     {
16969       /* Consume the `return' keyword.  */
16970       cp_lexer_consume_token (parser->lexer);
16971       /* Look for the identifier that indicates what value is to be
16972          returned.  */
16973       cp_parser_identifier (parser);
16974       /* Issue an error message.  */
16975       error ("named return values are no longer supported");
16976       /* Skip tokens until we reach the start of the function body.  */
16977       while (true)
16978         {
16979           cp_token *token = cp_lexer_peek_token (parser->lexer);
16980           if (token->type == CPP_OPEN_BRACE
16981               || token->type == CPP_EOF
16982               || token->type == CPP_PRAGMA_EOL)
16983             break;
16984           cp_lexer_consume_token (parser->lexer);
16985         }
16986     }
16987   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16988      anything declared inside `f'.  */
16989   saved_in_unbraced_linkage_specification_p
16990     = parser->in_unbraced_linkage_specification_p;
16991   parser->in_unbraced_linkage_specification_p = false;
16992   /* Inside the function, surrounding template-parameter-lists do not
16993      apply.  */
16994   saved_num_template_parameter_lists
16995     = parser->num_template_parameter_lists;
16996   parser->num_template_parameter_lists = 0;
16997   /* If the next token is `try', then we are looking at a
16998      function-try-block.  */
16999   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17000     ctor_initializer_p = cp_parser_function_try_block (parser);
17001   /* A function-try-block includes the function-body, so we only do
17002      this next part if we're not processing a function-try-block.  */
17003   else
17004     ctor_initializer_p
17005       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17006
17007   /* Finish the function.  */
17008   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17009                         (inline_p ? 2 : 0));
17010   /* Generate code for it, if necessary.  */
17011   expand_or_defer_fn (fn);
17012   /* Restore the saved values.  */
17013   parser->in_unbraced_linkage_specification_p
17014     = saved_in_unbraced_linkage_specification_p;
17015   parser->num_template_parameter_lists
17016     = saved_num_template_parameter_lists;
17017   parser->in_function_body = saved_in_function_body;
17018
17019   return fn;
17020 }
17021
17022 /* Parse a template-declaration, assuming that the `export' (and
17023    `extern') keywords, if present, has already been scanned.  MEMBER_P
17024    is as for cp_parser_template_declaration.  */
17025
17026 static void
17027 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17028 {
17029   tree decl = NULL_TREE;
17030   VEC (deferred_access_check,gc) *checks;
17031   tree parameter_list;
17032   bool friend_p = false;
17033   bool need_lang_pop;
17034
17035   /* Look for the `template' keyword.  */
17036   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17037     return;
17038
17039   /* And the `<'.  */
17040   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17041     return;
17042   if (at_class_scope_p () && current_function_decl)
17043     {
17044       /* 14.5.2.2 [temp.mem]
17045
17046          A local class shall not have member templates.  */
17047       error ("invalid declaration of member template in local class");
17048       cp_parser_skip_to_end_of_block_or_statement (parser);
17049       return;
17050     }
17051   /* [temp]
17052
17053      A template ... shall not have C linkage.  */
17054   if (current_lang_name == lang_name_c)
17055     {
17056       error ("template with C linkage");
17057       /* Give it C++ linkage to avoid confusing other parts of the
17058          front end.  */
17059       push_lang_context (lang_name_cplusplus);
17060       need_lang_pop = true;
17061     }
17062   else
17063     need_lang_pop = false;
17064
17065   /* We cannot perform access checks on the template parameter
17066      declarations until we know what is being declared, just as we
17067      cannot check the decl-specifier list.  */
17068   push_deferring_access_checks (dk_deferred);
17069
17070   /* If the next token is `>', then we have an invalid
17071      specialization.  Rather than complain about an invalid template
17072      parameter, issue an error message here.  */
17073   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17074     {
17075       cp_parser_error (parser, "invalid explicit specialization");
17076       begin_specialization ();
17077       parameter_list = NULL_TREE;
17078     }
17079   else
17080     /* Parse the template parameters.  */
17081     parameter_list = cp_parser_template_parameter_list (parser);
17082
17083   /* Get the deferred access checks from the parameter list.  These
17084      will be checked once we know what is being declared, as for a
17085      member template the checks must be performed in the scope of the
17086      class containing the member.  */
17087   checks = get_deferred_access_checks ();
17088
17089   /* Look for the `>'.  */
17090   cp_parser_skip_to_end_of_template_parameter_list (parser);
17091   /* We just processed one more parameter list.  */
17092   ++parser->num_template_parameter_lists;
17093   /* If the next token is `template', there are more template
17094      parameters.  */
17095   if (cp_lexer_next_token_is_keyword (parser->lexer,
17096                                       RID_TEMPLATE))
17097     cp_parser_template_declaration_after_export (parser, member_p);
17098   else
17099     {
17100       /* There are no access checks when parsing a template, as we do not
17101          know if a specialization will be a friend.  */
17102       push_deferring_access_checks (dk_no_check);
17103       decl = cp_parser_single_declaration (parser,
17104                                            checks,
17105                                            member_p,
17106                                            /*explicit_specialization_p=*/false,
17107                                            &friend_p);
17108       pop_deferring_access_checks ();
17109
17110       /* If this is a member template declaration, let the front
17111          end know.  */
17112       if (member_p && !friend_p && decl)
17113         {
17114           if (TREE_CODE (decl) == TYPE_DECL)
17115             cp_parser_check_access_in_redeclaration (decl);
17116
17117           decl = finish_member_template_decl (decl);
17118         }
17119       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17120         make_friend_class (current_class_type, TREE_TYPE (decl),
17121                            /*complain=*/true);
17122     }
17123   /* We are done with the current parameter list.  */
17124   --parser->num_template_parameter_lists;
17125
17126   pop_deferring_access_checks ();
17127
17128   /* Finish up.  */
17129   finish_template_decl (parameter_list);
17130
17131   /* Register member declarations.  */
17132   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17133     finish_member_declaration (decl);
17134   /* For the erroneous case of a template with C linkage, we pushed an
17135      implicit C++ linkage scope; exit that scope now.  */
17136   if (need_lang_pop)
17137     pop_lang_context ();
17138   /* If DECL is a function template, we must return to parse it later.
17139      (Even though there is no definition, there might be default
17140      arguments that need handling.)  */
17141   if (member_p && decl
17142       && (TREE_CODE (decl) == FUNCTION_DECL
17143           || DECL_FUNCTION_TEMPLATE_P (decl)))
17144     TREE_VALUE (parser->unparsed_functions_queues)
17145       = tree_cons (NULL_TREE, decl,
17146                    TREE_VALUE (parser->unparsed_functions_queues));
17147 }
17148
17149 /* Perform the deferred access checks from a template-parameter-list.
17150    CHECKS is a TREE_LIST of access checks, as returned by
17151    get_deferred_access_checks.  */
17152
17153 static void
17154 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17155 {
17156   ++processing_template_parmlist;
17157   perform_access_checks (checks);
17158   --processing_template_parmlist;
17159 }
17160
17161 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17162    `function-definition' sequence.  MEMBER_P is true, this declaration
17163    appears in a class scope.
17164
17165    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17166    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17167
17168 static tree
17169 cp_parser_single_declaration (cp_parser* parser,
17170                               VEC (deferred_access_check,gc)* checks,
17171                               bool member_p,
17172                               bool explicit_specialization_p,
17173                               bool* friend_p)
17174 {
17175   int declares_class_or_enum;
17176   tree decl = NULL_TREE;
17177   cp_decl_specifier_seq decl_specifiers;
17178   bool function_definition_p = false;
17179
17180   /* This function is only used when processing a template
17181      declaration.  */
17182   gcc_assert (innermost_scope_kind () == sk_template_parms
17183               || innermost_scope_kind () == sk_template_spec);
17184
17185   /* Defer access checks until we know what is being declared.  */
17186   push_deferring_access_checks (dk_deferred);
17187
17188   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17189      alternative.  */
17190   cp_parser_decl_specifier_seq (parser,
17191                                 CP_PARSER_FLAGS_OPTIONAL,
17192                                 &decl_specifiers,
17193                                 &declares_class_or_enum);
17194   if (friend_p)
17195     *friend_p = cp_parser_friend_p (&decl_specifiers);
17196
17197   /* There are no template typedefs.  */
17198   if (decl_specifiers.specs[(int) ds_typedef])
17199     {
17200       error ("template declaration of %qs", "typedef");
17201       decl = error_mark_node;
17202     }
17203
17204   /* Gather up the access checks that occurred the
17205      decl-specifier-seq.  */
17206   stop_deferring_access_checks ();
17207
17208   /* Check for the declaration of a template class.  */
17209   if (declares_class_or_enum)
17210     {
17211       if (cp_parser_declares_only_class_p (parser))
17212         {
17213           decl = shadow_tag (&decl_specifiers);
17214
17215           /* In this case:
17216
17217                struct C {
17218                  friend template <typename T> struct A<T>::B;
17219                };
17220
17221              A<T>::B will be represented by a TYPENAME_TYPE, and
17222              therefore not recognized by shadow_tag.  */
17223           if (friend_p && *friend_p
17224               && !decl
17225               && decl_specifiers.type
17226               && TYPE_P (decl_specifiers.type))
17227             decl = decl_specifiers.type;
17228
17229           if (decl && decl != error_mark_node)
17230             decl = TYPE_NAME (decl);
17231           else
17232             decl = error_mark_node;
17233
17234           /* Perform access checks for template parameters.  */
17235           cp_parser_perform_template_parameter_access_checks (checks);
17236         }
17237     }
17238   /* If it's not a template class, try for a template function.  If
17239      the next token is a `;', then this declaration does not declare
17240      anything.  But, if there were errors in the decl-specifiers, then
17241      the error might well have come from an attempted class-specifier.
17242      In that case, there's no need to warn about a missing declarator.  */
17243   if (!decl
17244       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17245           || decl_specifiers.type != error_mark_node))
17246     {
17247       decl = cp_parser_init_declarator (parser,
17248                                         &decl_specifiers,
17249                                         checks,
17250                                         /*function_definition_allowed_p=*/true,
17251                                         member_p,
17252                                         declares_class_or_enum,
17253                                         &function_definition_p);
17254
17255     /* 7.1.1-1 [dcl.stc]
17256
17257        A storage-class-specifier shall not be specified in an explicit
17258        specialization...  */
17259     if (decl
17260         && explicit_specialization_p
17261         && decl_specifiers.storage_class != sc_none)
17262       {
17263         error ("explicit template specialization cannot have a storage class");
17264         decl = error_mark_node;
17265       }
17266     }
17267
17268   pop_deferring_access_checks ();
17269
17270   /* Clear any current qualification; whatever comes next is the start
17271      of something new.  */
17272   parser->scope = NULL_TREE;
17273   parser->qualifying_scope = NULL_TREE;
17274   parser->object_scope = NULL_TREE;
17275   /* Look for a trailing `;' after the declaration.  */
17276   if (!function_definition_p
17277       && (decl == error_mark_node
17278           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17279     cp_parser_skip_to_end_of_block_or_statement (parser);
17280
17281   return decl;
17282 }
17283
17284 /* Parse a cast-expression that is not the operand of a unary "&".  */
17285
17286 static tree
17287 cp_parser_simple_cast_expression (cp_parser *parser)
17288 {
17289   return cp_parser_cast_expression (parser, /*address_p=*/false,
17290                                     /*cast_p=*/false);
17291 }
17292
17293 /* Parse a functional cast to TYPE.  Returns an expression
17294    representing the cast.  */
17295
17296 static tree
17297 cp_parser_functional_cast (cp_parser* parser, tree type)
17298 {
17299   tree expression_list;
17300   tree cast;
17301
17302   expression_list
17303     = cp_parser_parenthesized_expression_list (parser, false,
17304                                                /*cast_p=*/true,
17305                                                /*allow_expansion_p=*/true,
17306                                                /*non_constant_p=*/NULL);
17307
17308   cast = build_functional_cast (type, expression_list,
17309                                 tf_warning_or_error);
17310   /* [expr.const]/1: In an integral constant expression "only type
17311      conversions to integral or enumeration type can be used".  */
17312   if (TREE_CODE (type) == TYPE_DECL)
17313     type = TREE_TYPE (type);
17314   if (cast != error_mark_node
17315       && !cast_valid_in_integral_constant_expression_p (type)
17316       && (cp_parser_non_integral_constant_expression
17317           (parser, "a call to a constructor")))
17318     return error_mark_node;
17319   return cast;
17320 }
17321
17322 /* Save the tokens that make up the body of a member function defined
17323    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17324    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17325    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17326    for the member function.  */
17327
17328 static tree
17329 cp_parser_save_member_function_body (cp_parser* parser,
17330                                      cp_decl_specifier_seq *decl_specifiers,
17331                                      cp_declarator *declarator,
17332                                      tree attributes)
17333 {
17334   cp_token *first;
17335   cp_token *last;
17336   tree fn;
17337
17338   /* Create the function-declaration.  */
17339   fn = start_method (decl_specifiers, declarator, attributes);
17340   /* If something went badly wrong, bail out now.  */
17341   if (fn == error_mark_node)
17342     {
17343       /* If there's a function-body, skip it.  */
17344       if (cp_parser_token_starts_function_definition_p
17345           (cp_lexer_peek_token (parser->lexer)))
17346         cp_parser_skip_to_end_of_block_or_statement (parser);
17347       return error_mark_node;
17348     }
17349
17350   /* Remember it, if there default args to post process.  */
17351   cp_parser_save_default_args (parser, fn);
17352
17353   /* Save away the tokens that make up the body of the
17354      function.  */
17355   first = parser->lexer->next_token;
17356   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17357   /* Handle function try blocks.  */
17358   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17359     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17360   last = parser->lexer->next_token;
17361
17362   /* Save away the inline definition; we will process it when the
17363      class is complete.  */
17364   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17365   DECL_PENDING_INLINE_P (fn) = 1;
17366
17367   /* We need to know that this was defined in the class, so that
17368      friend templates are handled correctly.  */
17369   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17370
17371   /* We're done with the inline definition.  */
17372   finish_method (fn);
17373
17374   /* Add FN to the queue of functions to be parsed later.  */
17375   TREE_VALUE (parser->unparsed_functions_queues)
17376     = tree_cons (NULL_TREE, fn,
17377                  TREE_VALUE (parser->unparsed_functions_queues));
17378
17379   return fn;
17380 }
17381
17382 /* Parse a template-argument-list, as well as the trailing ">" (but
17383    not the opening ">").  See cp_parser_template_argument_list for the
17384    return value.  */
17385
17386 static tree
17387 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17388 {
17389   tree arguments;
17390   tree saved_scope;
17391   tree saved_qualifying_scope;
17392   tree saved_object_scope;
17393   bool saved_greater_than_is_operator_p;
17394   bool saved_skip_evaluation;
17395
17396   /* [temp.names]
17397
17398      When parsing a template-id, the first non-nested `>' is taken as
17399      the end of the template-argument-list rather than a greater-than
17400      operator.  */
17401   saved_greater_than_is_operator_p
17402     = parser->greater_than_is_operator_p;
17403   parser->greater_than_is_operator_p = false;
17404   /* Parsing the argument list may modify SCOPE, so we save it
17405      here.  */
17406   saved_scope = parser->scope;
17407   saved_qualifying_scope = parser->qualifying_scope;
17408   saved_object_scope = parser->object_scope;
17409   /* We need to evaluate the template arguments, even though this
17410      template-id may be nested within a "sizeof".  */
17411   saved_skip_evaluation = skip_evaluation;
17412   skip_evaluation = false;
17413   /* Parse the template-argument-list itself.  */
17414   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17415       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17416     arguments = NULL_TREE;
17417   else
17418     arguments = cp_parser_template_argument_list (parser);
17419   /* Look for the `>' that ends the template-argument-list. If we find
17420      a '>>' instead, it's probably just a typo.  */
17421   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17422     {
17423       if (cxx_dialect != cxx98)
17424         {
17425           /* In C++0x, a `>>' in a template argument list or cast
17426              expression is considered to be two separate `>'
17427              tokens. So, change the current token to a `>', but don't
17428              consume it: it will be consumed later when the outer
17429              template argument list (or cast expression) is parsed.
17430              Note that this replacement of `>' for `>>' is necessary
17431              even if we are parsing tentatively: in the tentative
17432              case, after calling
17433              cp_parser_enclosed_template_argument_list we will always
17434              throw away all of the template arguments and the first
17435              closing `>', either because the template argument list
17436              was erroneous or because we are replacing those tokens
17437              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17438              not have been thrown away) is needed either to close an
17439              outer template argument list or to complete a new-style
17440              cast.  */
17441           cp_token *token = cp_lexer_peek_token (parser->lexer);
17442           token->type = CPP_GREATER;
17443         }
17444       else if (!saved_greater_than_is_operator_p)
17445         {
17446           /* If we're in a nested template argument list, the '>>' has
17447             to be a typo for '> >'. We emit the error message, but we
17448             continue parsing and we push a '>' as next token, so that
17449             the argument list will be parsed correctly.  Note that the
17450             global source location is still on the token before the
17451             '>>', so we need to say explicitly where we want it.  */
17452           cp_token *token = cp_lexer_peek_token (parser->lexer);
17453           error ("%H%<>>%> should be %<> >%> "
17454                  "within a nested template argument list",
17455                  &token->location);
17456
17457           token->type = CPP_GREATER;
17458         }
17459       else
17460         {
17461           /* If this is not a nested template argument list, the '>>'
17462             is a typo for '>'. Emit an error message and continue.
17463             Same deal about the token location, but here we can get it
17464             right by consuming the '>>' before issuing the diagnostic.  */
17465           cp_lexer_consume_token (parser->lexer);
17466           error ("spurious %<>>%>, use %<>%> to terminate "
17467                  "a template argument list");
17468         }
17469     }
17470   else
17471     cp_parser_skip_to_end_of_template_parameter_list (parser);
17472   /* The `>' token might be a greater-than operator again now.  */
17473   parser->greater_than_is_operator_p
17474     = saved_greater_than_is_operator_p;
17475   /* Restore the SAVED_SCOPE.  */
17476   parser->scope = saved_scope;
17477   parser->qualifying_scope = saved_qualifying_scope;
17478   parser->object_scope = saved_object_scope;
17479   skip_evaluation = saved_skip_evaluation;
17480
17481   return arguments;
17482 }
17483
17484 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17485    arguments, or the body of the function have not yet been parsed,
17486    parse them now.  */
17487
17488 static void
17489 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17490 {
17491   /* If this member is a template, get the underlying
17492      FUNCTION_DECL.  */
17493   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17494     member_function = DECL_TEMPLATE_RESULT (member_function);
17495
17496   /* There should not be any class definitions in progress at this
17497      point; the bodies of members are only parsed outside of all class
17498      definitions.  */
17499   gcc_assert (parser->num_classes_being_defined == 0);
17500   /* While we're parsing the member functions we might encounter more
17501      classes.  We want to handle them right away, but we don't want
17502      them getting mixed up with functions that are currently in the
17503      queue.  */
17504   parser->unparsed_functions_queues
17505     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17506
17507   /* Make sure that any template parameters are in scope.  */
17508   maybe_begin_member_template_processing (member_function);
17509
17510   /* If the body of the function has not yet been parsed, parse it
17511      now.  */
17512   if (DECL_PENDING_INLINE_P (member_function))
17513     {
17514       tree function_scope;
17515       cp_token_cache *tokens;
17516
17517       /* The function is no longer pending; we are processing it.  */
17518       tokens = DECL_PENDING_INLINE_INFO (member_function);
17519       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17520       DECL_PENDING_INLINE_P (member_function) = 0;
17521
17522       /* If this is a local class, enter the scope of the containing
17523          function.  */
17524       function_scope = current_function_decl;
17525       if (function_scope)
17526         push_function_context ();
17527
17528       /* Push the body of the function onto the lexer stack.  */
17529       cp_parser_push_lexer_for_tokens (parser, tokens);
17530
17531       /* Let the front end know that we going to be defining this
17532          function.  */
17533       start_preparsed_function (member_function, NULL_TREE,
17534                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17535
17536       /* Don't do access checking if it is a templated function.  */
17537       if (processing_template_decl)
17538         push_deferring_access_checks (dk_no_check);
17539
17540       /* Now, parse the body of the function.  */
17541       cp_parser_function_definition_after_declarator (parser,
17542                                                       /*inline_p=*/true);
17543
17544       if (processing_template_decl)
17545         pop_deferring_access_checks ();
17546
17547       /* Leave the scope of the containing function.  */
17548       if (function_scope)
17549         pop_function_context ();
17550       cp_parser_pop_lexer (parser);
17551     }
17552
17553   /* Remove any template parameters from the symbol table.  */
17554   maybe_end_member_template_processing ();
17555
17556   /* Restore the queue.  */
17557   parser->unparsed_functions_queues
17558     = TREE_CHAIN (parser->unparsed_functions_queues);
17559 }
17560
17561 /* If DECL contains any default args, remember it on the unparsed
17562    functions queue.  */
17563
17564 static void
17565 cp_parser_save_default_args (cp_parser* parser, tree decl)
17566 {
17567   tree probe;
17568
17569   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17570        probe;
17571        probe = TREE_CHAIN (probe))
17572     if (TREE_PURPOSE (probe))
17573       {
17574         TREE_PURPOSE (parser->unparsed_functions_queues)
17575           = tree_cons (current_class_type, decl,
17576                        TREE_PURPOSE (parser->unparsed_functions_queues));
17577         break;
17578       }
17579 }
17580
17581 /* FN is a FUNCTION_DECL which may contains a parameter with an
17582    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17583    assumes that the current scope is the scope in which the default
17584    argument should be processed.  */
17585
17586 static void
17587 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17588 {
17589   bool saved_local_variables_forbidden_p;
17590   tree parm;
17591
17592   /* While we're parsing the default args, we might (due to the
17593      statement expression extension) encounter more classes.  We want
17594      to handle them right away, but we don't want them getting mixed
17595      up with default args that are currently in the queue.  */
17596   parser->unparsed_functions_queues
17597     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17598
17599   /* Local variable names (and the `this' keyword) may not appear
17600      in a default argument.  */
17601   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17602   parser->local_variables_forbidden_p = true;
17603
17604   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17605        parm;
17606        parm = TREE_CHAIN (parm))
17607     {
17608       cp_token_cache *tokens;
17609       tree default_arg = TREE_PURPOSE (parm);
17610       tree parsed_arg;
17611       VEC(tree,gc) *insts;
17612       tree copy;
17613       unsigned ix;
17614
17615       if (!default_arg)
17616         continue;
17617
17618       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17619         /* This can happen for a friend declaration for a function
17620            already declared with default arguments.  */
17621         continue;
17622
17623        /* Push the saved tokens for the default argument onto the parser's
17624           lexer stack.  */
17625       tokens = DEFARG_TOKENS (default_arg);
17626       cp_parser_push_lexer_for_tokens (parser, tokens);
17627
17628       /* Parse the assignment-expression.  */
17629       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17630
17631       if (!processing_template_decl)
17632         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17633
17634       TREE_PURPOSE (parm) = parsed_arg;
17635
17636       /* Update any instantiations we've already created.  */
17637       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17638            VEC_iterate (tree, insts, ix, copy); ix++)
17639         TREE_PURPOSE (copy) = parsed_arg;
17640
17641       /* If the token stream has not been completely used up, then
17642          there was extra junk after the end of the default
17643          argument.  */
17644       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17645         cp_parser_error (parser, "expected %<,%>");
17646
17647       /* Revert to the main lexer.  */
17648       cp_parser_pop_lexer (parser);
17649     }
17650
17651   /* Make sure no default arg is missing.  */
17652   check_default_args (fn);
17653
17654   /* Restore the state of local_variables_forbidden_p.  */
17655   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17656
17657   /* Restore the queue.  */
17658   parser->unparsed_functions_queues
17659     = TREE_CHAIN (parser->unparsed_functions_queues);
17660 }
17661
17662 /* Parse the operand of `sizeof' (or a similar operator).  Returns
17663    either a TYPE or an expression, depending on the form of the
17664    input.  The KEYWORD indicates which kind of expression we have
17665    encountered.  */
17666
17667 static tree
17668 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17669 {
17670   tree expr = NULL_TREE;
17671   const char *saved_message;
17672   char *tmp;
17673   bool saved_integral_constant_expression_p;
17674   bool saved_non_integral_constant_expression_p;
17675   bool pack_expansion_p = false;
17676
17677   /* Types cannot be defined in a `sizeof' expression.  Save away the
17678      old message.  */
17679   saved_message = parser->type_definition_forbidden_message;
17680   /* And create the new one.  */
17681   tmp = concat ("types may not be defined in %<",
17682                 IDENTIFIER_POINTER (ridpointers[keyword]),
17683                 "%> expressions", NULL);
17684   parser->type_definition_forbidden_message = tmp;
17685
17686   /* The restrictions on constant-expressions do not apply inside
17687      sizeof expressions.  */
17688   saved_integral_constant_expression_p
17689     = parser->integral_constant_expression_p;
17690   saved_non_integral_constant_expression_p
17691     = parser->non_integral_constant_expression_p;
17692   parser->integral_constant_expression_p = false;
17693
17694   /* If it's a `...', then we are computing the length of a parameter
17695      pack.  */
17696   if (keyword == RID_SIZEOF
17697       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17698     {
17699       /* Consume the `...'.  */
17700       cp_lexer_consume_token (parser->lexer);
17701       maybe_warn_variadic_templates ();
17702
17703       /* Note that this is an expansion.  */
17704       pack_expansion_p = true;
17705     }
17706
17707   /* Do not actually evaluate the expression.  */
17708   ++skip_evaluation;
17709   /* If it's a `(', then we might be looking at the type-id
17710      construction.  */
17711   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17712     {
17713       tree type;
17714       bool saved_in_type_id_in_expr_p;
17715
17716       /* We can't be sure yet whether we're looking at a type-id or an
17717          expression.  */
17718       cp_parser_parse_tentatively (parser);
17719       /* Consume the `('.  */
17720       cp_lexer_consume_token (parser->lexer);
17721       /* Parse the type-id.  */
17722       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17723       parser->in_type_id_in_expr_p = true;
17724       type = cp_parser_type_id (parser);
17725       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17726       /* Now, look for the trailing `)'.  */
17727       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17728       /* If all went well, then we're done.  */
17729       if (cp_parser_parse_definitely (parser))
17730         {
17731           cp_decl_specifier_seq decl_specs;
17732
17733           /* Build a trivial decl-specifier-seq.  */
17734           clear_decl_specs (&decl_specs);
17735           decl_specs.type = type;
17736
17737           /* Call grokdeclarator to figure out what type this is.  */
17738           expr = grokdeclarator (NULL,
17739                                  &decl_specs,
17740                                  TYPENAME,
17741                                  /*initialized=*/0,
17742                                  /*attrlist=*/NULL);
17743         }
17744     }
17745
17746   /* If the type-id production did not work out, then we must be
17747      looking at the unary-expression production.  */
17748   if (!expr)
17749     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17750                                        /*cast_p=*/false);
17751
17752   if (pack_expansion_p)
17753     /* Build a pack expansion. */
17754     expr = make_pack_expansion (expr);
17755
17756   /* Go back to evaluating expressions.  */
17757   --skip_evaluation;
17758
17759   /* Free the message we created.  */
17760   free (tmp);
17761   /* And restore the old one.  */
17762   parser->type_definition_forbidden_message = saved_message;
17763   parser->integral_constant_expression_p
17764     = saved_integral_constant_expression_p;
17765   parser->non_integral_constant_expression_p
17766     = saved_non_integral_constant_expression_p;
17767
17768   return expr;
17769 }
17770
17771 /* If the current declaration has no declarator, return true.  */
17772
17773 static bool
17774 cp_parser_declares_only_class_p (cp_parser *parser)
17775 {
17776   /* If the next token is a `;' or a `,' then there is no
17777      declarator.  */
17778   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17779           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17780 }
17781
17782 /* Update the DECL_SPECS to reflect the storage class indicated by
17783    KEYWORD.  */
17784
17785 static void
17786 cp_parser_set_storage_class (cp_parser *parser,
17787                              cp_decl_specifier_seq *decl_specs,
17788                              enum rid keyword)
17789 {
17790   cp_storage_class storage_class;
17791
17792   if (parser->in_unbraced_linkage_specification_p)
17793     {
17794       error ("invalid use of %qD in linkage specification",
17795              ridpointers[keyword]);
17796       return;
17797     }
17798   else if (decl_specs->storage_class != sc_none)
17799     {
17800       decl_specs->conflicting_specifiers_p = true;
17801       return;
17802     }
17803
17804   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17805       && decl_specs->specs[(int) ds_thread])
17806     {
17807       error ("%<__thread%> before %qD", ridpointers[keyword]);
17808       decl_specs->specs[(int) ds_thread] = 0;
17809     }
17810
17811   switch (keyword)
17812     {
17813     case RID_AUTO:
17814       storage_class = sc_auto;
17815       break;
17816     case RID_REGISTER:
17817       storage_class = sc_register;
17818       break;
17819     case RID_STATIC:
17820       storage_class = sc_static;
17821       break;
17822     case RID_EXTERN:
17823       storage_class = sc_extern;
17824       break;
17825     case RID_MUTABLE:
17826       storage_class = sc_mutable;
17827       break;
17828     default:
17829       gcc_unreachable ();
17830     }
17831   decl_specs->storage_class = storage_class;
17832
17833   /* A storage class specifier cannot be applied alongside a typedef 
17834      specifier. If there is a typedef specifier present then set 
17835      conflicting_specifiers_p which will trigger an error later
17836      on in grokdeclarator. */
17837   if (decl_specs->specs[(int)ds_typedef])
17838     decl_specs->conflicting_specifiers_p = true;
17839 }
17840
17841 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
17842    is true, the type is a user-defined type; otherwise it is a
17843    built-in type specified by a keyword.  */
17844
17845 static void
17846 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17847                               tree type_spec,
17848                               bool user_defined_p)
17849 {
17850   decl_specs->any_specifiers_p = true;
17851
17852   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
17853      (with, for example, in "typedef int wchar_t;") we remember that
17854      this is what happened.  In system headers, we ignore these
17855      declarations so that G++ can work with system headers that are not
17856      C++-safe.  */
17857   if (decl_specs->specs[(int) ds_typedef]
17858       && !user_defined_p
17859       && (type_spec == boolean_type_node
17860           || type_spec == char16_type_node
17861           || type_spec == char32_type_node
17862           || type_spec == wchar_type_node)
17863       && (decl_specs->type
17864           || decl_specs->specs[(int) ds_long]
17865           || decl_specs->specs[(int) ds_short]
17866           || decl_specs->specs[(int) ds_unsigned]
17867           || decl_specs->specs[(int) ds_signed]))
17868     {
17869       decl_specs->redefined_builtin_type = type_spec;
17870       if (!decl_specs->type)
17871         {
17872           decl_specs->type = type_spec;
17873           decl_specs->user_defined_type_p = false;
17874         }
17875     }
17876   else if (decl_specs->type)
17877     decl_specs->multiple_types_p = true;
17878   else
17879     {
17880       decl_specs->type = type_spec;
17881       decl_specs->user_defined_type_p = user_defined_p;
17882       decl_specs->redefined_builtin_type = NULL_TREE;
17883     }
17884 }
17885
17886 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17887    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
17888
17889 static bool
17890 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17891 {
17892   return decl_specifiers->specs[(int) ds_friend] != 0;
17893 }
17894
17895 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
17896    issue an error message indicating that TOKEN_DESC was expected.
17897
17898    Returns the token consumed, if the token had the appropriate type.
17899    Otherwise, returns NULL.  */
17900
17901 static cp_token *
17902 cp_parser_require (cp_parser* parser,
17903                    enum cpp_ttype type,
17904                    const char* token_desc)
17905 {
17906   if (cp_lexer_next_token_is (parser->lexer, type))
17907     return cp_lexer_consume_token (parser->lexer);
17908   else
17909     {
17910       /* Output the MESSAGE -- unless we're parsing tentatively.  */
17911       if (!cp_parser_simulate_error (parser))
17912         {
17913           char *message = concat ("expected ", token_desc, NULL);
17914           cp_parser_error (parser, message);
17915           free (message);
17916         }
17917       return NULL;
17918     }
17919 }
17920
17921 /* An error message is produced if the next token is not '>'.
17922    All further tokens are skipped until the desired token is
17923    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17924
17925 static void
17926 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17927 {
17928   /* Current level of '< ... >'.  */
17929   unsigned level = 0;
17930   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17931   unsigned nesting_depth = 0;
17932
17933   /* Are we ready, yet?  If not, issue error message.  */
17934   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17935     return;
17936
17937   /* Skip tokens until the desired token is found.  */
17938   while (true)
17939     {
17940       /* Peek at the next token.  */
17941       switch (cp_lexer_peek_token (parser->lexer)->type)
17942         {
17943         case CPP_LESS:
17944           if (!nesting_depth)
17945             ++level;
17946           break;
17947
17948         case CPP_RSHIFT:
17949           if (cxx_dialect == cxx98)
17950             /* C++0x views the `>>' operator as two `>' tokens, but
17951                C++98 does not. */
17952             break;
17953           else if (!nesting_depth && level-- == 0)
17954             {
17955               /* We've hit a `>>' where the first `>' closes the
17956                  template argument list, and the second `>' is
17957                  spurious.  Just consume the `>>' and stop; we've
17958                  already produced at least one error.  */
17959               cp_lexer_consume_token (parser->lexer);
17960               return;
17961             }
17962           /* Fall through for C++0x, so we handle the second `>' in
17963              the `>>'.  */
17964
17965         case CPP_GREATER:
17966           if (!nesting_depth && level-- == 0)
17967             {
17968               /* We've reached the token we want, consume it and stop.  */
17969               cp_lexer_consume_token (parser->lexer);
17970               return;
17971             }
17972           break;
17973
17974         case CPP_OPEN_PAREN:
17975         case CPP_OPEN_SQUARE:
17976           ++nesting_depth;
17977           break;
17978
17979         case CPP_CLOSE_PAREN:
17980         case CPP_CLOSE_SQUARE:
17981           if (nesting_depth-- == 0)
17982             return;
17983           break;
17984
17985         case CPP_EOF:
17986         case CPP_PRAGMA_EOL:
17987         case CPP_SEMICOLON:
17988         case CPP_OPEN_BRACE:
17989         case CPP_CLOSE_BRACE:
17990           /* The '>' was probably forgotten, don't look further.  */
17991           return;
17992
17993         default:
17994           break;
17995         }
17996
17997       /* Consume this token.  */
17998       cp_lexer_consume_token (parser->lexer);
17999     }
18000 }
18001
18002 /* If the next token is the indicated keyword, consume it.  Otherwise,
18003    issue an error message indicating that TOKEN_DESC was expected.
18004
18005    Returns the token consumed, if the token had the appropriate type.
18006    Otherwise, returns NULL.  */
18007
18008 static cp_token *
18009 cp_parser_require_keyword (cp_parser* parser,
18010                            enum rid keyword,
18011                            const char* token_desc)
18012 {
18013   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18014
18015   if (token && token->keyword != keyword)
18016     {
18017       dyn_string_t error_msg;
18018
18019       /* Format the error message.  */
18020       error_msg = dyn_string_new (0);
18021       dyn_string_append_cstr (error_msg, "expected ");
18022       dyn_string_append_cstr (error_msg, token_desc);
18023       cp_parser_error (parser, error_msg->s);
18024       dyn_string_delete (error_msg);
18025       return NULL;
18026     }
18027
18028   return token;
18029 }
18030
18031 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18032    function-definition.  */
18033
18034 static bool
18035 cp_parser_token_starts_function_definition_p (cp_token* token)
18036 {
18037   return (/* An ordinary function-body begins with an `{'.  */
18038           token->type == CPP_OPEN_BRACE
18039           /* A ctor-initializer begins with a `:'.  */
18040           || token->type == CPP_COLON
18041           /* A function-try-block begins with `try'.  */
18042           || token->keyword == RID_TRY
18043           /* The named return value extension begins with `return'.  */
18044           || token->keyword == RID_RETURN);
18045 }
18046
18047 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18048    definition.  */
18049
18050 static bool
18051 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18052 {
18053   cp_token *token;
18054
18055   token = cp_lexer_peek_token (parser->lexer);
18056   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18057 }
18058
18059 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18060    C++0x) ending a template-argument.  */
18061
18062 static bool
18063 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18064 {
18065   cp_token *token;
18066
18067   token = cp_lexer_peek_token (parser->lexer);
18068   return (token->type == CPP_COMMA 
18069           || token->type == CPP_GREATER
18070           || token->type == CPP_ELLIPSIS
18071           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18072 }
18073
18074 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18075    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18076
18077 static bool
18078 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18079                                                      size_t n)
18080 {
18081   cp_token *token;
18082
18083   token = cp_lexer_peek_nth_token (parser->lexer, n);
18084   if (token->type == CPP_LESS)
18085     return true;
18086   /* Check for the sequence `<::' in the original code. It would be lexed as
18087      `[:', where `[' is a digraph, and there is no whitespace before
18088      `:'.  */
18089   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18090     {
18091       cp_token *token2;
18092       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18093       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18094         return true;
18095     }
18096   return false;
18097 }
18098
18099 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18100    or none_type otherwise.  */
18101
18102 static enum tag_types
18103 cp_parser_token_is_class_key (cp_token* token)
18104 {
18105   switch (token->keyword)
18106     {
18107     case RID_CLASS:
18108       return class_type;
18109     case RID_STRUCT:
18110       return record_type;
18111     case RID_UNION:
18112       return union_type;
18113
18114     default:
18115       return none_type;
18116     }
18117 }
18118
18119 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18120
18121 static void
18122 cp_parser_check_class_key (enum tag_types class_key, tree type)
18123 {
18124   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18125     pedwarn ("%qs tag used in naming %q#T",
18126             class_key == union_type ? "union"
18127              : class_key == record_type ? "struct" : "class",
18128              type);
18129 }
18130
18131 /* Issue an error message if DECL is redeclared with different
18132    access than its original declaration [class.access.spec/3].
18133    This applies to nested classes and nested class templates.
18134    [class.mem/1].  */
18135
18136 static void
18137 cp_parser_check_access_in_redeclaration (tree decl)
18138 {
18139   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18140     return;
18141
18142   if ((TREE_PRIVATE (decl)
18143        != (current_access_specifier == access_private_node))
18144       || (TREE_PROTECTED (decl)
18145           != (current_access_specifier == access_protected_node)))
18146     error ("%qD redeclared with different access", decl);
18147 }
18148
18149 /* Look for the `template' keyword, as a syntactic disambiguator.
18150    Return TRUE iff it is present, in which case it will be
18151    consumed.  */
18152
18153 static bool
18154 cp_parser_optional_template_keyword (cp_parser *parser)
18155 {
18156   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18157     {
18158       /* The `template' keyword can only be used within templates;
18159          outside templates the parser can always figure out what is a
18160          template and what is not.  */
18161       if (!processing_template_decl)
18162         {
18163           error ("%<template%> (as a disambiguator) is only allowed "
18164                  "within templates");
18165           /* If this part of the token stream is rescanned, the same
18166              error message would be generated.  So, we purge the token
18167              from the stream.  */
18168           cp_lexer_purge_token (parser->lexer);
18169           return false;
18170         }
18171       else
18172         {
18173           /* Consume the `template' keyword.  */
18174           cp_lexer_consume_token (parser->lexer);
18175           return true;
18176         }
18177     }
18178
18179   return false;
18180 }
18181
18182 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18183    set PARSER->SCOPE, and perform other related actions.  */
18184
18185 static void
18186 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18187 {
18188   int i;
18189   struct tree_check *check_value;
18190   deferred_access_check *chk;
18191   VEC (deferred_access_check,gc) *checks;
18192
18193   /* Get the stored value.  */
18194   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18195   /* Perform any access checks that were deferred.  */
18196   checks = check_value->checks;
18197   if (checks)
18198     {
18199       for (i = 0 ;
18200            VEC_iterate (deferred_access_check, checks, i, chk) ;
18201            ++i)
18202         {
18203           perform_or_defer_access_check (chk->binfo,
18204                                          chk->decl,
18205                                          chk->diag_decl);
18206         }
18207     }
18208   /* Set the scope from the stored value.  */
18209   parser->scope = check_value->value;
18210   parser->qualifying_scope = check_value->qualifying_scope;
18211   parser->object_scope = NULL_TREE;
18212 }
18213
18214 /* Consume tokens up through a non-nested END token.  */
18215
18216 static void
18217 cp_parser_cache_group (cp_parser *parser,
18218                        enum cpp_ttype end,
18219                        unsigned depth)
18220 {
18221   while (true)
18222     {
18223       cp_token *token;
18224
18225       /* Abort a parenthesized expression if we encounter a brace.  */
18226       if ((end == CPP_CLOSE_PAREN || depth == 0)
18227           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18228         return;
18229       /* If we've reached the end of the file, stop.  */
18230       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18231           || (end != CPP_PRAGMA_EOL
18232               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18233         return;
18234       /* Consume the next token.  */
18235       token = cp_lexer_consume_token (parser->lexer);
18236       /* See if it starts a new group.  */
18237       if (token->type == CPP_OPEN_BRACE)
18238         {
18239           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18240           if (depth == 0)
18241             return;
18242         }
18243       else if (token->type == CPP_OPEN_PAREN)
18244         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18245       else if (token->type == CPP_PRAGMA)
18246         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18247       else if (token->type == end)
18248         return;
18249     }
18250 }
18251
18252 /* Begin parsing tentatively.  We always save tokens while parsing
18253    tentatively so that if the tentative parsing fails we can restore the
18254    tokens.  */
18255
18256 static void
18257 cp_parser_parse_tentatively (cp_parser* parser)
18258 {
18259   /* Enter a new parsing context.  */
18260   parser->context = cp_parser_context_new (parser->context);
18261   /* Begin saving tokens.  */
18262   cp_lexer_save_tokens (parser->lexer);
18263   /* In order to avoid repetitive access control error messages,
18264      access checks are queued up until we are no longer parsing
18265      tentatively.  */
18266   push_deferring_access_checks (dk_deferred);
18267 }
18268
18269 /* Commit to the currently active tentative parse.  */
18270
18271 static void
18272 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18273 {
18274   cp_parser_context *context;
18275   cp_lexer *lexer;
18276
18277   /* Mark all of the levels as committed.  */
18278   lexer = parser->lexer;
18279   for (context = parser->context; context->next; context = context->next)
18280     {
18281       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18282         break;
18283       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18284       while (!cp_lexer_saving_tokens (lexer))
18285         lexer = lexer->next;
18286       cp_lexer_commit_tokens (lexer);
18287     }
18288 }
18289
18290 /* Abort the currently active tentative parse.  All consumed tokens
18291    will be rolled back, and no diagnostics will be issued.  */
18292
18293 static void
18294 cp_parser_abort_tentative_parse (cp_parser* parser)
18295 {
18296   cp_parser_simulate_error (parser);
18297   /* Now, pretend that we want to see if the construct was
18298      successfully parsed.  */
18299   cp_parser_parse_definitely (parser);
18300 }
18301
18302 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18303    token stream.  Otherwise, commit to the tokens we have consumed.
18304    Returns true if no error occurred; false otherwise.  */
18305
18306 static bool
18307 cp_parser_parse_definitely (cp_parser* parser)
18308 {
18309   bool error_occurred;
18310   cp_parser_context *context;
18311
18312   /* Remember whether or not an error occurred, since we are about to
18313      destroy that information.  */
18314   error_occurred = cp_parser_error_occurred (parser);
18315   /* Remove the topmost context from the stack.  */
18316   context = parser->context;
18317   parser->context = context->next;
18318   /* If no parse errors occurred, commit to the tentative parse.  */
18319   if (!error_occurred)
18320     {
18321       /* Commit to the tokens read tentatively, unless that was
18322          already done.  */
18323       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18324         cp_lexer_commit_tokens (parser->lexer);
18325
18326       pop_to_parent_deferring_access_checks ();
18327     }
18328   /* Otherwise, if errors occurred, roll back our state so that things
18329      are just as they were before we began the tentative parse.  */
18330   else
18331     {
18332       cp_lexer_rollback_tokens (parser->lexer);
18333       pop_deferring_access_checks ();
18334     }
18335   /* Add the context to the front of the free list.  */
18336   context->next = cp_parser_context_free_list;
18337   cp_parser_context_free_list = context;
18338
18339   return !error_occurred;
18340 }
18341
18342 /* Returns true if we are parsing tentatively and are not committed to
18343    this tentative parse.  */
18344
18345 static bool
18346 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18347 {
18348   return (cp_parser_parsing_tentatively (parser)
18349           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18350 }
18351
18352 /* Returns nonzero iff an error has occurred during the most recent
18353    tentative parse.  */
18354
18355 static bool
18356 cp_parser_error_occurred (cp_parser* parser)
18357 {
18358   return (cp_parser_parsing_tentatively (parser)
18359           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18360 }
18361
18362 /* Returns nonzero if GNU extensions are allowed.  */
18363
18364 static bool
18365 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18366 {
18367   return parser->allow_gnu_extensions_p;
18368 }
18369 \f
18370 /* Objective-C++ Productions */
18371
18372
18373 /* Parse an Objective-C expression, which feeds into a primary-expression
18374    above.
18375
18376    objc-expression:
18377      objc-message-expression
18378      objc-string-literal
18379      objc-encode-expression
18380      objc-protocol-expression
18381      objc-selector-expression
18382
18383   Returns a tree representation of the expression.  */
18384
18385 static tree
18386 cp_parser_objc_expression (cp_parser* parser)
18387 {
18388   /* Try to figure out what kind of declaration is present.  */
18389   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18390
18391   switch (kwd->type)
18392     {
18393     case CPP_OPEN_SQUARE:
18394       return cp_parser_objc_message_expression (parser);
18395
18396     case CPP_OBJC_STRING:
18397       kwd = cp_lexer_consume_token (parser->lexer);
18398       return objc_build_string_object (kwd->u.value);
18399
18400     case CPP_KEYWORD:
18401       switch (kwd->keyword)
18402         {
18403         case RID_AT_ENCODE:
18404           return cp_parser_objc_encode_expression (parser);
18405
18406         case RID_AT_PROTOCOL:
18407           return cp_parser_objc_protocol_expression (parser);
18408
18409         case RID_AT_SELECTOR:
18410           return cp_parser_objc_selector_expression (parser);
18411
18412         default:
18413           break;
18414         }
18415     default:
18416       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18417       cp_parser_skip_to_end_of_block_or_statement (parser);
18418     }
18419
18420   return error_mark_node;
18421 }
18422
18423 /* Parse an Objective-C message expression.
18424
18425    objc-message-expression:
18426      [ objc-message-receiver objc-message-args ]
18427
18428    Returns a representation of an Objective-C message.  */
18429
18430 static tree
18431 cp_parser_objc_message_expression (cp_parser* parser)
18432 {
18433   tree receiver, messageargs;
18434
18435   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18436   receiver = cp_parser_objc_message_receiver (parser);
18437   messageargs = cp_parser_objc_message_args (parser);
18438   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18439
18440   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18441 }
18442
18443 /* Parse an objc-message-receiver.
18444
18445    objc-message-receiver:
18446      expression
18447      simple-type-specifier
18448
18449   Returns a representation of the type or expression.  */
18450
18451 static tree
18452 cp_parser_objc_message_receiver (cp_parser* parser)
18453 {
18454   tree rcv;
18455
18456   /* An Objective-C message receiver may be either (1) a type
18457      or (2) an expression.  */
18458   cp_parser_parse_tentatively (parser);
18459   rcv = cp_parser_expression (parser, false);
18460
18461   if (cp_parser_parse_definitely (parser))
18462     return rcv;
18463
18464   rcv = cp_parser_simple_type_specifier (parser,
18465                                          /*decl_specs=*/NULL,
18466                                          CP_PARSER_FLAGS_NONE);
18467
18468   return objc_get_class_reference (rcv);
18469 }
18470
18471 /* Parse the arguments and selectors comprising an Objective-C message.
18472
18473    objc-message-args:
18474      objc-selector
18475      objc-selector-args
18476      objc-selector-args , objc-comma-args
18477
18478    objc-selector-args:
18479      objc-selector [opt] : assignment-expression
18480      objc-selector-args objc-selector [opt] : assignment-expression
18481
18482    objc-comma-args:
18483      assignment-expression
18484      objc-comma-args , assignment-expression
18485
18486    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18487    selector arguments and TREE_VALUE containing a list of comma
18488    arguments.  */
18489
18490 static tree
18491 cp_parser_objc_message_args (cp_parser* parser)
18492 {
18493   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18494   bool maybe_unary_selector_p = true;
18495   cp_token *token = cp_lexer_peek_token (parser->lexer);
18496
18497   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18498     {
18499       tree selector = NULL_TREE, arg;
18500
18501       if (token->type != CPP_COLON)
18502         selector = cp_parser_objc_selector (parser);
18503
18504       /* Detect if we have a unary selector.  */
18505       if (maybe_unary_selector_p
18506           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18507         return build_tree_list (selector, NULL_TREE);
18508
18509       maybe_unary_selector_p = false;
18510       cp_parser_require (parser, CPP_COLON, "%<:%>");
18511       arg = cp_parser_assignment_expression (parser, false);
18512
18513       sel_args
18514         = chainon (sel_args,
18515                    build_tree_list (selector, arg));
18516
18517       token = cp_lexer_peek_token (parser->lexer);
18518     }
18519
18520   /* Handle non-selector arguments, if any. */
18521   while (token->type == CPP_COMMA)
18522     {
18523       tree arg;
18524
18525       cp_lexer_consume_token (parser->lexer);
18526       arg = cp_parser_assignment_expression (parser, false);
18527
18528       addl_args
18529         = chainon (addl_args,
18530                    build_tree_list (NULL_TREE, arg));
18531
18532       token = cp_lexer_peek_token (parser->lexer);
18533     }
18534
18535   return build_tree_list (sel_args, addl_args);
18536 }
18537
18538 /* Parse an Objective-C encode expression.
18539
18540    objc-encode-expression:
18541      @encode objc-typename
18542
18543    Returns an encoded representation of the type argument.  */
18544
18545 static tree
18546 cp_parser_objc_encode_expression (cp_parser* parser)
18547 {
18548   tree type;
18549
18550   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
18551   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18552   type = complete_type (cp_parser_type_id (parser));
18553   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18554
18555   if (!type)
18556     {
18557       error ("%<@encode%> must specify a type as an argument");
18558       return error_mark_node;
18559     }
18560
18561   return objc_build_encode_expr (type);
18562 }
18563
18564 /* Parse an Objective-C @defs expression.  */
18565
18566 static tree
18567 cp_parser_objc_defs_expression (cp_parser *parser)
18568 {
18569   tree name;
18570
18571   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18572   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18573   name = cp_parser_identifier (parser);
18574   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18575
18576   return objc_get_class_ivars (name);
18577 }
18578
18579 /* Parse an Objective-C protocol expression.
18580
18581   objc-protocol-expression:
18582     @protocol ( identifier )
18583
18584   Returns a representation of the protocol expression.  */
18585
18586 static tree
18587 cp_parser_objc_protocol_expression (cp_parser* parser)
18588 {
18589   tree proto;
18590
18591   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18592   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18593   proto = cp_parser_identifier (parser);
18594   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18595
18596   return objc_build_protocol_expr (proto);
18597 }
18598
18599 /* Parse an Objective-C selector expression.
18600
18601    objc-selector-expression:
18602      @selector ( objc-method-signature )
18603
18604    objc-method-signature:
18605      objc-selector
18606      objc-selector-seq
18607
18608    objc-selector-seq:
18609      objc-selector :
18610      objc-selector-seq objc-selector :
18611
18612   Returns a representation of the method selector.  */
18613
18614 static tree
18615 cp_parser_objc_selector_expression (cp_parser* parser)
18616 {
18617   tree sel_seq = NULL_TREE;
18618   bool maybe_unary_selector_p = true;
18619   cp_token *token;
18620
18621   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18622   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18623   token = cp_lexer_peek_token (parser->lexer);
18624
18625   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18626          || token->type == CPP_SCOPE)
18627     {
18628       tree selector = NULL_TREE;
18629
18630       if (token->type != CPP_COLON
18631           || token->type == CPP_SCOPE)
18632         selector = cp_parser_objc_selector (parser);
18633
18634       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18635           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18636         {
18637           /* Detect if we have a unary selector.  */
18638           if (maybe_unary_selector_p)
18639             {
18640               sel_seq = selector;
18641               goto finish_selector;
18642             }
18643           else
18644             {
18645               cp_parser_error (parser, "expected %<:%>");
18646             }
18647         }
18648       maybe_unary_selector_p = false;
18649       token = cp_lexer_consume_token (parser->lexer);
18650
18651       if (token->type == CPP_SCOPE)
18652         {
18653           sel_seq
18654             = chainon (sel_seq,
18655                        build_tree_list (selector, NULL_TREE));
18656           sel_seq
18657             = chainon (sel_seq,
18658                        build_tree_list (NULL_TREE, NULL_TREE));
18659         }
18660       else
18661         sel_seq
18662           = chainon (sel_seq,
18663                      build_tree_list (selector, NULL_TREE));
18664
18665       token = cp_lexer_peek_token (parser->lexer);
18666     }
18667
18668  finish_selector:
18669   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18670
18671   return objc_build_selector_expr (sel_seq);
18672 }
18673
18674 /* Parse a list of identifiers.
18675
18676    objc-identifier-list:
18677      identifier
18678      objc-identifier-list , identifier
18679
18680    Returns a TREE_LIST of identifier nodes.  */
18681
18682 static tree
18683 cp_parser_objc_identifier_list (cp_parser* parser)
18684 {
18685   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18686   cp_token *sep = cp_lexer_peek_token (parser->lexer);
18687
18688   while (sep->type == CPP_COMMA)
18689     {
18690       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18691       list = chainon (list,
18692                       build_tree_list (NULL_TREE,
18693                                        cp_parser_identifier (parser)));
18694       sep = cp_lexer_peek_token (parser->lexer);
18695     }
18696
18697   return list;
18698 }
18699
18700 /* Parse an Objective-C alias declaration.
18701
18702    objc-alias-declaration:
18703      @compatibility_alias identifier identifier ;
18704
18705    This function registers the alias mapping with the Objective-C front end.
18706    It returns nothing.  */
18707
18708 static void
18709 cp_parser_objc_alias_declaration (cp_parser* parser)
18710 {
18711   tree alias, orig;
18712
18713   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
18714   alias = cp_parser_identifier (parser);
18715   orig = cp_parser_identifier (parser);
18716   objc_declare_alias (alias, orig);
18717   cp_parser_consume_semicolon_at_end_of_statement (parser);
18718 }
18719
18720 /* Parse an Objective-C class forward-declaration.
18721
18722    objc-class-declaration:
18723      @class objc-identifier-list ;
18724
18725    The function registers the forward declarations with the Objective-C
18726    front end.  It returns nothing.  */
18727
18728 static void
18729 cp_parser_objc_class_declaration (cp_parser* parser)
18730 {
18731   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
18732   objc_declare_class (cp_parser_objc_identifier_list (parser));
18733   cp_parser_consume_semicolon_at_end_of_statement (parser);
18734 }
18735
18736 /* Parse a list of Objective-C protocol references.
18737
18738    objc-protocol-refs-opt:
18739      objc-protocol-refs [opt]
18740
18741    objc-protocol-refs:
18742      < objc-identifier-list >
18743
18744    Returns a TREE_LIST of identifiers, if any.  */
18745
18746 static tree
18747 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18748 {
18749   tree protorefs = NULL_TREE;
18750
18751   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18752     {
18753       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
18754       protorefs = cp_parser_objc_identifier_list (parser);
18755       cp_parser_require (parser, CPP_GREATER, "%<>%>");
18756     }
18757
18758   return protorefs;
18759 }
18760
18761 /* Parse a Objective-C visibility specification.  */
18762
18763 static void
18764 cp_parser_objc_visibility_spec (cp_parser* parser)
18765 {
18766   cp_token *vis = cp_lexer_peek_token (parser->lexer);
18767
18768   switch (vis->keyword)
18769     {
18770     case RID_AT_PRIVATE:
18771       objc_set_visibility (2);
18772       break;
18773     case RID_AT_PROTECTED:
18774       objc_set_visibility (0);
18775       break;
18776     case RID_AT_PUBLIC:
18777       objc_set_visibility (1);
18778       break;
18779     default:
18780       return;
18781     }
18782
18783   /* Eat '@private'/'@protected'/'@public'.  */
18784   cp_lexer_consume_token (parser->lexer);
18785 }
18786
18787 /* Parse an Objective-C method type.  */
18788
18789 static void
18790 cp_parser_objc_method_type (cp_parser* parser)
18791 {
18792   objc_set_method_type
18793    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18794     ? PLUS_EXPR
18795     : MINUS_EXPR);
18796 }
18797
18798 /* Parse an Objective-C protocol qualifier.  */
18799
18800 static tree
18801 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18802 {
18803   tree quals = NULL_TREE, node;
18804   cp_token *token = cp_lexer_peek_token (parser->lexer);
18805
18806   node = token->u.value;
18807
18808   while (node && TREE_CODE (node) == IDENTIFIER_NODE
18809          && (node == ridpointers [(int) RID_IN]
18810              || node == ridpointers [(int) RID_OUT]
18811              || node == ridpointers [(int) RID_INOUT]
18812              || node == ridpointers [(int) RID_BYCOPY]
18813              || node == ridpointers [(int) RID_BYREF]
18814              || node == ridpointers [(int) RID_ONEWAY]))
18815     {
18816       quals = tree_cons (NULL_TREE, node, quals);
18817       cp_lexer_consume_token (parser->lexer);
18818       token = cp_lexer_peek_token (parser->lexer);
18819       node = token->u.value;
18820     }
18821
18822   return quals;
18823 }
18824
18825 /* Parse an Objective-C typename.  */
18826
18827 static tree
18828 cp_parser_objc_typename (cp_parser* parser)
18829 {
18830   tree typename = NULL_TREE;
18831
18832   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18833     {
18834       tree proto_quals, cp_type = NULL_TREE;
18835
18836       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18837       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18838
18839       /* An ObjC type name may consist of just protocol qualifiers, in which
18840          case the type shall default to 'id'.  */
18841       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18842         cp_type = cp_parser_type_id (parser);
18843
18844       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18845       typename = build_tree_list (proto_quals, cp_type);
18846     }
18847
18848   return typename;
18849 }
18850
18851 /* Check to see if TYPE refers to an Objective-C selector name.  */
18852
18853 static bool
18854 cp_parser_objc_selector_p (enum cpp_ttype type)
18855 {
18856   return (type == CPP_NAME || type == CPP_KEYWORD
18857           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18858           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18859           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18860           || type == CPP_XOR || type == CPP_XOR_EQ);
18861 }
18862
18863 /* Parse an Objective-C selector.  */
18864
18865 static tree
18866 cp_parser_objc_selector (cp_parser* parser)
18867 {
18868   cp_token *token = cp_lexer_consume_token (parser->lexer);
18869
18870   if (!cp_parser_objc_selector_p (token->type))
18871     {
18872       error ("invalid Objective-C++ selector name");
18873       return error_mark_node;
18874     }
18875
18876   /* C++ operator names are allowed to appear in ObjC selectors.  */
18877   switch (token->type)
18878     {
18879     case CPP_AND_AND: return get_identifier ("and");
18880     case CPP_AND_EQ: return get_identifier ("and_eq");
18881     case CPP_AND: return get_identifier ("bitand");
18882     case CPP_OR: return get_identifier ("bitor");
18883     case CPP_COMPL: return get_identifier ("compl");
18884     case CPP_NOT: return get_identifier ("not");
18885     case CPP_NOT_EQ: return get_identifier ("not_eq");
18886     case CPP_OR_OR: return get_identifier ("or");
18887     case CPP_OR_EQ: return get_identifier ("or_eq");
18888     case CPP_XOR: return get_identifier ("xor");
18889     case CPP_XOR_EQ: return get_identifier ("xor_eq");
18890     default: return token->u.value;
18891     }
18892 }
18893
18894 /* Parse an Objective-C params list.  */
18895
18896 static tree
18897 cp_parser_objc_method_keyword_params (cp_parser* parser)
18898 {
18899   tree params = NULL_TREE;
18900   bool maybe_unary_selector_p = true;
18901   cp_token *token = cp_lexer_peek_token (parser->lexer);
18902
18903   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18904     {
18905       tree selector = NULL_TREE, typename, identifier;
18906
18907       if (token->type != CPP_COLON)
18908         selector = cp_parser_objc_selector (parser);
18909
18910       /* Detect if we have a unary selector.  */
18911       if (maybe_unary_selector_p
18912           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18913         return selector;
18914
18915       maybe_unary_selector_p = false;
18916       cp_parser_require (parser, CPP_COLON, "%<:%>");
18917       typename = cp_parser_objc_typename (parser);
18918       identifier = cp_parser_identifier (parser);
18919
18920       params
18921         = chainon (params,
18922                    objc_build_keyword_decl (selector,
18923                                             typename,
18924                                             identifier));
18925
18926       token = cp_lexer_peek_token (parser->lexer);
18927     }
18928
18929   return params;
18930 }
18931
18932 /* Parse the non-keyword Objective-C params.  */
18933
18934 static tree
18935 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18936 {
18937   tree params = make_node (TREE_LIST);
18938   cp_token *token = cp_lexer_peek_token (parser->lexer);
18939   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18940
18941   while (token->type == CPP_COMMA)
18942     {
18943       cp_parameter_declarator *parmdecl;
18944       tree parm;
18945
18946       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18947       token = cp_lexer_peek_token (parser->lexer);
18948
18949       if (token->type == CPP_ELLIPSIS)
18950         {
18951           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18952           *ellipsisp = true;
18953           break;
18954         }
18955
18956       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18957       parm = grokdeclarator (parmdecl->declarator,
18958                              &parmdecl->decl_specifiers,
18959                              PARM, /*initialized=*/0,
18960                              /*attrlist=*/NULL);
18961
18962       chainon (params, build_tree_list (NULL_TREE, parm));
18963       token = cp_lexer_peek_token (parser->lexer);
18964     }
18965
18966   return params;
18967 }
18968
18969 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18970
18971 static void
18972 cp_parser_objc_interstitial_code (cp_parser* parser)
18973 {
18974   cp_token *token = cp_lexer_peek_token (parser->lexer);
18975
18976   /* If the next token is `extern' and the following token is a string
18977      literal, then we have a linkage specification.  */
18978   if (token->keyword == RID_EXTERN
18979       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18980     cp_parser_linkage_specification (parser);
18981   /* Handle #pragma, if any.  */
18982   else if (token->type == CPP_PRAGMA)
18983     cp_parser_pragma (parser, pragma_external);
18984   /* Allow stray semicolons.  */
18985   else if (token->type == CPP_SEMICOLON)
18986     cp_lexer_consume_token (parser->lexer);
18987   /* Finally, try to parse a block-declaration, or a function-definition.  */
18988   else
18989     cp_parser_block_declaration (parser, /*statement_p=*/false);
18990 }
18991
18992 /* Parse a method signature.  */
18993
18994 static tree
18995 cp_parser_objc_method_signature (cp_parser* parser)
18996 {
18997   tree rettype, kwdparms, optparms;
18998   bool ellipsis = false;
18999
19000   cp_parser_objc_method_type (parser);
19001   rettype = cp_parser_objc_typename (parser);
19002   kwdparms = cp_parser_objc_method_keyword_params (parser);
19003   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19004
19005   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19006 }
19007
19008 /* Pars an Objective-C method prototype list.  */
19009
19010 static void
19011 cp_parser_objc_method_prototype_list (cp_parser* parser)
19012 {
19013   cp_token *token = cp_lexer_peek_token (parser->lexer);
19014
19015   while (token->keyword != RID_AT_END)
19016     {
19017       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19018         {
19019           objc_add_method_declaration
19020            (cp_parser_objc_method_signature (parser));
19021           cp_parser_consume_semicolon_at_end_of_statement (parser);
19022         }
19023       else
19024         /* Allow for interspersed non-ObjC++ code.  */
19025         cp_parser_objc_interstitial_code (parser);
19026
19027       token = cp_lexer_peek_token (parser->lexer);
19028     }
19029
19030   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19031   objc_finish_interface ();
19032 }
19033
19034 /* Parse an Objective-C method definition list.  */
19035
19036 static void
19037 cp_parser_objc_method_definition_list (cp_parser* parser)
19038 {
19039   cp_token *token = cp_lexer_peek_token (parser->lexer);
19040
19041   while (token->keyword != RID_AT_END)
19042     {
19043       tree meth;
19044
19045       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19046         {
19047           push_deferring_access_checks (dk_deferred);
19048           objc_start_method_definition
19049            (cp_parser_objc_method_signature (parser));
19050
19051           /* For historical reasons, we accept an optional semicolon.  */
19052           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19053             cp_lexer_consume_token (parser->lexer);
19054
19055           perform_deferred_access_checks ();
19056           stop_deferring_access_checks ();
19057           meth = cp_parser_function_definition_after_declarator (parser,
19058                                                                  false);
19059           pop_deferring_access_checks ();
19060           objc_finish_method_definition (meth);
19061         }
19062       else
19063         /* Allow for interspersed non-ObjC++ code.  */
19064         cp_parser_objc_interstitial_code (parser);
19065
19066       token = cp_lexer_peek_token (parser->lexer);
19067     }
19068
19069   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19070   objc_finish_implementation ();
19071 }
19072
19073 /* Parse Objective-C ivars.  */
19074
19075 static void
19076 cp_parser_objc_class_ivars (cp_parser* parser)
19077 {
19078   cp_token *token = cp_lexer_peek_token (parser->lexer);
19079
19080   if (token->type != CPP_OPEN_BRACE)
19081     return;     /* No ivars specified.  */
19082
19083   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19084   token = cp_lexer_peek_token (parser->lexer);
19085
19086   while (token->type != CPP_CLOSE_BRACE)
19087     {
19088       cp_decl_specifier_seq declspecs;
19089       int decl_class_or_enum_p;
19090       tree prefix_attributes;
19091
19092       cp_parser_objc_visibility_spec (parser);
19093
19094       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19095         break;
19096
19097       cp_parser_decl_specifier_seq (parser,
19098                                     CP_PARSER_FLAGS_OPTIONAL,
19099                                     &declspecs,
19100                                     &decl_class_or_enum_p);
19101       prefix_attributes = declspecs.attributes;
19102       declspecs.attributes = NULL_TREE;
19103
19104       /* Keep going until we hit the `;' at the end of the
19105          declaration.  */
19106       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19107         {
19108           tree width = NULL_TREE, attributes, first_attribute, decl;
19109           cp_declarator *declarator = NULL;
19110           int ctor_dtor_or_conv_p;
19111
19112           /* Check for a (possibly unnamed) bitfield declaration.  */
19113           token = cp_lexer_peek_token (parser->lexer);
19114           if (token->type == CPP_COLON)
19115             goto eat_colon;
19116
19117           if (token->type == CPP_NAME
19118               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19119                   == CPP_COLON))
19120             {
19121               /* Get the name of the bitfield.  */
19122               declarator = make_id_declarator (NULL_TREE,
19123                                                cp_parser_identifier (parser),
19124                                                sfk_none);
19125
19126              eat_colon:
19127               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19128               /* Get the width of the bitfield.  */
19129               width
19130                 = cp_parser_constant_expression (parser,
19131                                                  /*allow_non_constant=*/false,
19132                                                  NULL);
19133             }
19134           else
19135             {
19136               /* Parse the declarator.  */
19137               declarator
19138                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19139                                         &ctor_dtor_or_conv_p,
19140                                         /*parenthesized_p=*/NULL,
19141                                         /*member_p=*/false);
19142             }
19143
19144           /* Look for attributes that apply to the ivar.  */
19145           attributes = cp_parser_attributes_opt (parser);
19146           /* Remember which attributes are prefix attributes and
19147              which are not.  */
19148           first_attribute = attributes;
19149           /* Combine the attributes.  */
19150           attributes = chainon (prefix_attributes, attributes);
19151
19152           if (width)
19153             {
19154               /* Create the bitfield declaration.  */
19155               decl = grokbitfield (declarator, &declspecs, width);
19156               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
19157             }
19158           else
19159             decl = grokfield (declarator, &declspecs,
19160                               NULL_TREE, /*init_const_expr_p=*/false,
19161                               NULL_TREE, attributes);
19162
19163           /* Add the instance variable.  */
19164           objc_add_instance_variable (decl);
19165
19166           /* Reset PREFIX_ATTRIBUTES.  */
19167           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19168             attributes = TREE_CHAIN (attributes);
19169           if (attributes)
19170             TREE_CHAIN (attributes) = NULL_TREE;
19171
19172           token = cp_lexer_peek_token (parser->lexer);
19173
19174           if (token->type == CPP_COMMA)
19175             {
19176               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19177               continue;
19178             }
19179           break;
19180         }
19181
19182       cp_parser_consume_semicolon_at_end_of_statement (parser);
19183       token = cp_lexer_peek_token (parser->lexer);
19184     }
19185
19186   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19187   /* For historical reasons, we accept an optional semicolon.  */
19188   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19189     cp_lexer_consume_token (parser->lexer);
19190 }
19191
19192 /* Parse an Objective-C protocol declaration.  */
19193
19194 static void
19195 cp_parser_objc_protocol_declaration (cp_parser* parser)
19196 {
19197   tree proto, protorefs;
19198   cp_token *tok;
19199
19200   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19201   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19202     {
19203       error ("identifier expected after %<@protocol%>");
19204       goto finish;
19205     }
19206
19207   /* See if we have a forward declaration or a definition.  */
19208   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19209
19210   /* Try a forward declaration first.  */
19211   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19212     {
19213       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19214      finish:
19215       cp_parser_consume_semicolon_at_end_of_statement (parser);
19216     }
19217
19218   /* Ok, we got a full-fledged definition (or at least should).  */
19219   else
19220     {
19221       proto = cp_parser_identifier (parser);
19222       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19223       objc_start_protocol (proto, protorefs);
19224       cp_parser_objc_method_prototype_list (parser);
19225     }
19226 }
19227
19228 /* Parse an Objective-C superclass or category.  */
19229
19230 static void
19231 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19232                                                           tree *categ)
19233 {
19234   cp_token *next = cp_lexer_peek_token (parser->lexer);
19235
19236   *super = *categ = NULL_TREE;
19237   if (next->type == CPP_COLON)
19238     {
19239       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19240       *super = cp_parser_identifier (parser);
19241     }
19242   else if (next->type == CPP_OPEN_PAREN)
19243     {
19244       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19245       *categ = cp_parser_identifier (parser);
19246       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19247     }
19248 }
19249
19250 /* Parse an Objective-C class interface.  */
19251
19252 static void
19253 cp_parser_objc_class_interface (cp_parser* parser)
19254 {
19255   tree name, super, categ, protos;
19256
19257   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19258   name = cp_parser_identifier (parser);
19259   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19260   protos = cp_parser_objc_protocol_refs_opt (parser);
19261
19262   /* We have either a class or a category on our hands.  */
19263   if (categ)
19264     objc_start_category_interface (name, categ, protos);
19265   else
19266     {
19267       objc_start_class_interface (name, super, protos);
19268       /* Handle instance variable declarations, if any.  */
19269       cp_parser_objc_class_ivars (parser);
19270       objc_continue_interface ();
19271     }
19272
19273   cp_parser_objc_method_prototype_list (parser);
19274 }
19275
19276 /* Parse an Objective-C class implementation.  */
19277
19278 static void
19279 cp_parser_objc_class_implementation (cp_parser* parser)
19280 {
19281   tree name, super, categ;
19282
19283   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19284   name = cp_parser_identifier (parser);
19285   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19286
19287   /* We have either a class or a category on our hands.  */
19288   if (categ)
19289     objc_start_category_implementation (name, categ);
19290   else
19291     {
19292       objc_start_class_implementation (name, super);
19293       /* Handle instance variable declarations, if any.  */
19294       cp_parser_objc_class_ivars (parser);
19295       objc_continue_implementation ();
19296     }
19297
19298   cp_parser_objc_method_definition_list (parser);
19299 }
19300
19301 /* Consume the @end token and finish off the implementation.  */
19302
19303 static void
19304 cp_parser_objc_end_implementation (cp_parser* parser)
19305 {
19306   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19307   objc_finish_implementation ();
19308 }
19309
19310 /* Parse an Objective-C declaration.  */
19311
19312 static void
19313 cp_parser_objc_declaration (cp_parser* parser)
19314 {
19315   /* Try to figure out what kind of declaration is present.  */
19316   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19317
19318   switch (kwd->keyword)
19319     {
19320     case RID_AT_ALIAS:
19321       cp_parser_objc_alias_declaration (parser);
19322       break;
19323     case RID_AT_CLASS:
19324       cp_parser_objc_class_declaration (parser);
19325       break;
19326     case RID_AT_PROTOCOL:
19327       cp_parser_objc_protocol_declaration (parser);
19328       break;
19329     case RID_AT_INTERFACE:
19330       cp_parser_objc_class_interface (parser);
19331       break;
19332     case RID_AT_IMPLEMENTATION:
19333       cp_parser_objc_class_implementation (parser);
19334       break;
19335     case RID_AT_END:
19336       cp_parser_objc_end_implementation (parser);
19337       break;
19338     default:
19339       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19340       cp_parser_skip_to_end_of_block_or_statement (parser);
19341     }
19342 }
19343
19344 /* Parse an Objective-C try-catch-finally statement.
19345
19346    objc-try-catch-finally-stmt:
19347      @try compound-statement objc-catch-clause-seq [opt]
19348        objc-finally-clause [opt]
19349
19350    objc-catch-clause-seq:
19351      objc-catch-clause objc-catch-clause-seq [opt]
19352
19353    objc-catch-clause:
19354      @catch ( exception-declaration ) compound-statement
19355
19356    objc-finally-clause
19357      @finally compound-statement
19358
19359    Returns NULL_TREE.  */
19360
19361 static tree
19362 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19363   location_t location;
19364   tree stmt;
19365
19366   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19367   location = cp_lexer_peek_token (parser->lexer)->location;
19368   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19369      node, lest it get absorbed into the surrounding block.  */
19370   stmt = push_stmt_list ();
19371   cp_parser_compound_statement (parser, NULL, false);
19372   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19373
19374   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19375     {
19376       cp_parameter_declarator *parmdecl;
19377       tree parm;
19378
19379       cp_lexer_consume_token (parser->lexer);
19380       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19381       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19382       parm = grokdeclarator (parmdecl->declarator,
19383                              &parmdecl->decl_specifiers,
19384                              PARM, /*initialized=*/0,
19385                              /*attrlist=*/NULL);
19386       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19387       objc_begin_catch_clause (parm);
19388       cp_parser_compound_statement (parser, NULL, false);
19389       objc_finish_catch_clause ();
19390     }
19391
19392   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19393     {
19394       cp_lexer_consume_token (parser->lexer);
19395       location = cp_lexer_peek_token (parser->lexer)->location;
19396       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19397          node, lest it get absorbed into the surrounding block.  */
19398       stmt = push_stmt_list ();
19399       cp_parser_compound_statement (parser, NULL, false);
19400       objc_build_finally_clause (location, pop_stmt_list (stmt));
19401     }
19402
19403   return objc_finish_try_stmt ();
19404 }
19405
19406 /* Parse an Objective-C synchronized statement.
19407
19408    objc-synchronized-stmt:
19409      @synchronized ( expression ) compound-statement
19410
19411    Returns NULL_TREE.  */
19412
19413 static tree
19414 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19415   location_t location;
19416   tree lock, stmt;
19417
19418   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19419
19420   location = cp_lexer_peek_token (parser->lexer)->location;
19421   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19422   lock = cp_parser_expression (parser, false);
19423   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19424
19425   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19426      node, lest it get absorbed into the surrounding block.  */
19427   stmt = push_stmt_list ();
19428   cp_parser_compound_statement (parser, NULL, false);
19429
19430   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19431 }
19432
19433 /* Parse an Objective-C throw statement.
19434
19435    objc-throw-stmt:
19436      @throw assignment-expression [opt] ;
19437
19438    Returns a constructed '@throw' statement.  */
19439
19440 static tree
19441 cp_parser_objc_throw_statement (cp_parser *parser) {
19442   tree expr = NULL_TREE;
19443
19444   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19445
19446   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19447     expr = cp_parser_assignment_expression (parser, false);
19448
19449   cp_parser_consume_semicolon_at_end_of_statement (parser);
19450
19451   return objc_build_throw_stmt (expr);
19452 }
19453
19454 /* Parse an Objective-C statement.  */
19455
19456 static tree
19457 cp_parser_objc_statement (cp_parser * parser) {
19458   /* Try to figure out what kind of declaration is present.  */
19459   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19460
19461   switch (kwd->keyword)
19462     {
19463     case RID_AT_TRY:
19464       return cp_parser_objc_try_catch_finally_statement (parser);
19465     case RID_AT_SYNCHRONIZED:
19466       return cp_parser_objc_synchronized_statement (parser);
19467     case RID_AT_THROW:
19468       return cp_parser_objc_throw_statement (parser);
19469     default:
19470       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19471       cp_parser_skip_to_end_of_block_or_statement (parser);
19472     }
19473
19474   return error_mark_node;
19475 }
19476 \f
19477 /* OpenMP 2.5 parsing routines.  */
19478
19479 /* Returns name of the next clause.
19480    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19481    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19482    returned and the token is consumed.  */
19483
19484 static pragma_omp_clause
19485 cp_parser_omp_clause_name (cp_parser *parser)
19486 {
19487   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19488
19489   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19490     result = PRAGMA_OMP_CLAUSE_IF;
19491   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19492     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19493   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19494     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19495   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19496     {
19497       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19498       const char *p = IDENTIFIER_POINTER (id);
19499
19500       switch (p[0])
19501         {
19502         case 'c':
19503           if (!strcmp ("collapse", p))
19504             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19505           else if (!strcmp ("copyin", p))
19506             result = PRAGMA_OMP_CLAUSE_COPYIN;
19507           else if (!strcmp ("copyprivate", p))
19508             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19509           break;
19510         case 'f':
19511           if (!strcmp ("firstprivate", p))
19512             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19513           break;
19514         case 'l':
19515           if (!strcmp ("lastprivate", p))
19516             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19517           break;
19518         case 'n':
19519           if (!strcmp ("nowait", p))
19520             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19521           else if (!strcmp ("num_threads", p))
19522             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19523           break;
19524         case 'o':
19525           if (!strcmp ("ordered", p))
19526             result = PRAGMA_OMP_CLAUSE_ORDERED;
19527           break;
19528         case 'r':
19529           if (!strcmp ("reduction", p))
19530             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19531           break;
19532         case 's':
19533           if (!strcmp ("schedule", p))
19534             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19535           else if (!strcmp ("shared", p))
19536             result = PRAGMA_OMP_CLAUSE_SHARED;
19537           break;
19538         case 'u':
19539           if (!strcmp ("untied", p))
19540             result = PRAGMA_OMP_CLAUSE_UNTIED;
19541           break;
19542         }
19543     }
19544
19545   if (result != PRAGMA_OMP_CLAUSE_NONE)
19546     cp_lexer_consume_token (parser->lexer);
19547
19548   return result;
19549 }
19550
19551 /* Validate that a clause of the given type does not already exist.  */
19552
19553 static void
19554 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19555 {
19556   tree c;
19557
19558   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19559     if (OMP_CLAUSE_CODE (c) == code)
19560       {
19561         error ("too many %qs clauses", name);
19562         break;
19563       }
19564 }
19565
19566 /* OpenMP 2.5:
19567    variable-list:
19568      identifier
19569      variable-list , identifier
19570
19571    In addition, we match a closing parenthesis.  An opening parenthesis
19572    will have been consumed by the caller.
19573
19574    If KIND is nonzero, create the appropriate node and install the decl
19575    in OMP_CLAUSE_DECL and add the node to the head of the list.
19576
19577    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19578    return the list created.  */
19579
19580 static tree
19581 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19582                                 tree list)
19583 {
19584   while (1)
19585     {
19586       tree name, decl;
19587
19588       name = cp_parser_id_expression (parser, /*template_p=*/false,
19589                                       /*check_dependency_p=*/true,
19590                                       /*template_p=*/NULL,
19591                                       /*declarator_p=*/false,
19592                                       /*optional_p=*/false);
19593       if (name == error_mark_node)
19594         goto skip_comma;
19595
19596       decl = cp_parser_lookup_name_simple (parser, name);
19597       if (decl == error_mark_node)
19598         cp_parser_name_lookup_error (parser, name, decl, NULL);
19599       else if (kind != 0)
19600         {
19601           tree u = build_omp_clause (kind);
19602           OMP_CLAUSE_DECL (u) = decl;
19603           OMP_CLAUSE_CHAIN (u) = list;
19604           list = u;
19605         }
19606       else
19607         list = tree_cons (decl, NULL_TREE, list);
19608
19609     get_comma:
19610       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19611         break;
19612       cp_lexer_consume_token (parser->lexer);
19613     }
19614
19615   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19616     {
19617       int ending;
19618
19619       /* Try to resync to an unnested comma.  Copied from
19620          cp_parser_parenthesized_expression_list.  */
19621     skip_comma:
19622       ending = cp_parser_skip_to_closing_parenthesis (parser,
19623                                                       /*recovering=*/true,
19624                                                       /*or_comma=*/true,
19625                                                       /*consume_paren=*/true);
19626       if (ending < 0)
19627         goto get_comma;
19628     }
19629
19630   return list;
19631 }
19632
19633 /* Similarly, but expect leading and trailing parenthesis.  This is a very
19634    common case for omp clauses.  */
19635
19636 static tree
19637 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19638 {
19639   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19640     return cp_parser_omp_var_list_no_open (parser, kind, list);
19641   return list;
19642 }
19643
19644 /* OpenMP 3.0:
19645    collapse ( constant-expression ) */
19646
19647 static tree
19648 cp_parser_omp_clause_collapse (cp_parser *parser, tree list)
19649 {
19650   tree c, num;
19651   location_t loc;
19652   HOST_WIDE_INT n;
19653
19654   loc = cp_lexer_peek_token (parser->lexer)->location;
19655   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19656     return list;
19657
19658   num = cp_parser_constant_expression (parser, false, NULL);
19659
19660   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19661     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19662                                            /*or_comma=*/false,
19663                                            /*consume_paren=*/true);
19664
19665   if (num == error_mark_node)
19666     return list;
19667   num = fold_non_dependent_expr (num);
19668   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
19669       || !host_integerp (num, 0)
19670       || (n = tree_low_cst (num, 0)) <= 0
19671       || (int) n != n)
19672     {
19673       error ("%Hcollapse argument needs positive constant integer expression", &loc);
19674       return list;
19675     }
19676
19677   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
19678   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
19679   OMP_CLAUSE_CHAIN (c) = list;
19680   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
19681
19682   return c;
19683 }
19684
19685 /* OpenMP 2.5:
19686    default ( shared | none ) */
19687
19688 static tree
19689 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19690 {
19691   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19692   tree c;
19693
19694   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19695     return list;
19696   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19697     {
19698       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19699       const char *p = IDENTIFIER_POINTER (id);
19700
19701       switch (p[0])
19702         {
19703         case 'n':
19704           if (strcmp ("none", p) != 0)
19705             goto invalid_kind;
19706           kind = OMP_CLAUSE_DEFAULT_NONE;
19707           break;
19708
19709         case 's':
19710           if (strcmp ("shared", p) != 0)
19711             goto invalid_kind;
19712           kind = OMP_CLAUSE_DEFAULT_SHARED;
19713           break;
19714
19715         default:
19716           goto invalid_kind;
19717         }
19718
19719       cp_lexer_consume_token (parser->lexer);
19720     }
19721   else
19722     {
19723     invalid_kind:
19724       cp_parser_error (parser, "expected %<none%> or %<shared%>");
19725     }
19726
19727   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19728     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19729                                            /*or_comma=*/false,
19730                                            /*consume_paren=*/true);
19731
19732   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19733     return list;
19734
19735   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19736   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19737   OMP_CLAUSE_CHAIN (c) = list;
19738   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19739
19740   return c;
19741 }
19742
19743 /* OpenMP 2.5:
19744    if ( expression ) */
19745
19746 static tree
19747 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19748 {
19749   tree t, c;
19750
19751   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19752     return list;
19753
19754   t = cp_parser_condition (parser);
19755
19756   if (t == error_mark_node
19757       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19758     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19759                                            /*or_comma=*/false,
19760                                            /*consume_paren=*/true);
19761
19762   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19763
19764   c = build_omp_clause (OMP_CLAUSE_IF);
19765   OMP_CLAUSE_IF_EXPR (c) = t;
19766   OMP_CLAUSE_CHAIN (c) = list;
19767
19768   return c;
19769 }
19770
19771 /* OpenMP 2.5:
19772    nowait */
19773
19774 static tree
19775 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19776 {
19777   tree c;
19778
19779   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19780
19781   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19782   OMP_CLAUSE_CHAIN (c) = list;
19783   return c;
19784 }
19785
19786 /* OpenMP 2.5:
19787    num_threads ( expression ) */
19788
19789 static tree
19790 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19791 {
19792   tree t, c;
19793
19794   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19795     return list;
19796
19797   t = cp_parser_expression (parser, false);
19798
19799   if (t == error_mark_node
19800       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19801     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19802                                            /*or_comma=*/false,
19803                                            /*consume_paren=*/true);
19804
19805   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19806
19807   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19808   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19809   OMP_CLAUSE_CHAIN (c) = list;
19810
19811   return c;
19812 }
19813
19814 /* OpenMP 2.5:
19815    ordered */
19816
19817 static tree
19818 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19819 {
19820   tree c;
19821
19822   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19823
19824   c = build_omp_clause (OMP_CLAUSE_ORDERED);
19825   OMP_CLAUSE_CHAIN (c) = list;
19826   return c;
19827 }
19828
19829 /* OpenMP 2.5:
19830    reduction ( reduction-operator : variable-list )
19831
19832    reduction-operator:
19833      One of: + * - & ^ | && || */
19834
19835 static tree
19836 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19837 {
19838   enum tree_code code;
19839   tree nlist, c;
19840
19841   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19842     return list;
19843
19844   switch (cp_lexer_peek_token (parser->lexer)->type)
19845     {
19846     case CPP_PLUS:
19847       code = PLUS_EXPR;
19848       break;
19849     case CPP_MULT:
19850       code = MULT_EXPR;
19851       break;
19852     case CPP_MINUS:
19853       code = MINUS_EXPR;
19854       break;
19855     case CPP_AND:
19856       code = BIT_AND_EXPR;
19857       break;
19858     case CPP_XOR:
19859       code = BIT_XOR_EXPR;
19860       break;
19861     case CPP_OR:
19862       code = BIT_IOR_EXPR;
19863       break;
19864     case CPP_AND_AND:
19865       code = TRUTH_ANDIF_EXPR;
19866       break;
19867     case CPP_OR_OR:
19868       code = TRUTH_ORIF_EXPR;
19869       break;
19870     default:
19871       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
19872                                "%<|%>, %<&&%>, or %<||%>");
19873     resync_fail:
19874       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19875                                              /*or_comma=*/false,
19876                                              /*consume_paren=*/true);
19877       return list;
19878     }
19879   cp_lexer_consume_token (parser->lexer);
19880
19881   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
19882     goto resync_fail;
19883
19884   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19885   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19886     OMP_CLAUSE_REDUCTION_CODE (c) = code;
19887
19888   return nlist;
19889 }
19890
19891 /* OpenMP 2.5:
19892    schedule ( schedule-kind )
19893    schedule ( schedule-kind , expression )
19894
19895    schedule-kind:
19896      static | dynamic | guided | runtime | auto  */
19897
19898 static tree
19899 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19900 {
19901   tree c, t;
19902
19903   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19904     return list;
19905
19906   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19907
19908   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19909     {
19910       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19911       const char *p = IDENTIFIER_POINTER (id);
19912
19913       switch (p[0])
19914         {
19915         case 'd':
19916           if (strcmp ("dynamic", p) != 0)
19917             goto invalid_kind;
19918           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19919           break;
19920
19921         case 'g':
19922           if (strcmp ("guided", p) != 0)
19923             goto invalid_kind;
19924           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19925           break;
19926
19927         case 'r':
19928           if (strcmp ("runtime", p) != 0)
19929             goto invalid_kind;
19930           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19931           break;
19932
19933         default:
19934           goto invalid_kind;
19935         }
19936     }
19937   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19938     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19939   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
19940     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
19941   else
19942     goto invalid_kind;
19943   cp_lexer_consume_token (parser->lexer);
19944
19945   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19946     {
19947       cp_lexer_consume_token (parser->lexer);
19948
19949       t = cp_parser_assignment_expression (parser, false);
19950
19951       if (t == error_mark_node)
19952         goto resync_fail;
19953       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19954         error ("schedule %<runtime%> does not take "
19955                "a %<chunk_size%> parameter");
19956       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
19957         error ("schedule %<auto%> does not take "
19958                "a %<chunk_size%> parameter");
19959       else
19960         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19961
19962       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19963         goto resync_fail;
19964     }
19965   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
19966     goto resync_fail;
19967
19968   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19969   OMP_CLAUSE_CHAIN (c) = list;
19970   return c;
19971
19972  invalid_kind:
19973   cp_parser_error (parser, "invalid schedule kind");
19974  resync_fail:
19975   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19976                                          /*or_comma=*/false,
19977                                          /*consume_paren=*/true);
19978   return list;
19979 }
19980
19981 /* OpenMP 3.0:
19982    untied */
19983
19984 static tree
19985 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19986 {
19987   tree c;
19988
19989   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
19990
19991   c = build_omp_clause (OMP_CLAUSE_UNTIED);
19992   OMP_CLAUSE_CHAIN (c) = list;
19993   return c;
19994 }
19995
19996 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
19997    is a bitmask in MASK.  Return the list of clauses found; the result
19998    of clause default goes in *pdefault.  */
19999
20000 static tree
20001 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20002                            const char *where, cp_token *pragma_tok)
20003 {
20004   tree clauses = NULL;
20005   bool first = true;
20006
20007   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20008     {
20009       pragma_omp_clause c_kind;
20010       const char *c_name;
20011       tree prev = clauses;
20012
20013       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20014         cp_lexer_consume_token (parser->lexer);
20015
20016       c_kind = cp_parser_omp_clause_name (parser);
20017       first = false;
20018
20019       switch (c_kind)
20020         {
20021         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20022           clauses = cp_parser_omp_clause_collapse (parser, clauses);
20023           c_name = "collapse";
20024           break;
20025         case PRAGMA_OMP_CLAUSE_COPYIN:
20026           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20027           c_name = "copyin";
20028           break;
20029         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20030           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20031                                             clauses);
20032           c_name = "copyprivate";
20033           break;
20034         case PRAGMA_OMP_CLAUSE_DEFAULT:
20035           clauses = cp_parser_omp_clause_default (parser, clauses);
20036           c_name = "default";
20037           break;
20038         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20039           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20040                                             clauses);
20041           c_name = "firstprivate";
20042           break;
20043         case PRAGMA_OMP_CLAUSE_IF:
20044           clauses = cp_parser_omp_clause_if (parser, clauses);
20045           c_name = "if";
20046           break;
20047         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20048           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20049                                             clauses);
20050           c_name = "lastprivate";
20051           break;
20052         case PRAGMA_OMP_CLAUSE_NOWAIT:
20053           clauses = cp_parser_omp_clause_nowait (parser, clauses);
20054           c_name = "nowait";
20055           break;
20056         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20057           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
20058           c_name = "num_threads";
20059           break;
20060         case PRAGMA_OMP_CLAUSE_ORDERED:
20061           clauses = cp_parser_omp_clause_ordered (parser, clauses);
20062           c_name = "ordered";
20063           break;
20064         case PRAGMA_OMP_CLAUSE_PRIVATE:
20065           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20066                                             clauses);
20067           c_name = "private";
20068           break;
20069         case PRAGMA_OMP_CLAUSE_REDUCTION:
20070           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20071           c_name = "reduction";
20072           break;
20073         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20074           clauses = cp_parser_omp_clause_schedule (parser, clauses);
20075           c_name = "schedule";
20076           break;
20077         case PRAGMA_OMP_CLAUSE_SHARED:
20078           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20079                                             clauses);
20080           c_name = "shared";
20081           break;
20082         case PRAGMA_OMP_CLAUSE_UNTIED:
20083           clauses = cp_parser_omp_clause_untied (parser, clauses);
20084           c_name = "nowait";
20085           break;
20086         default:
20087           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20088           goto saw_error;
20089         }
20090
20091       if (((mask >> c_kind) & 1) == 0)
20092         {
20093           /* Remove the invalid clause(s) from the list to avoid
20094              confusing the rest of the compiler.  */
20095           clauses = prev;
20096           error ("%qs is not valid for %qs", c_name, where);
20097         }
20098     }
20099  saw_error:
20100   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20101   return finish_omp_clauses (clauses);
20102 }
20103
20104 /* OpenMP 2.5:
20105    structured-block:
20106      statement
20107
20108    In practice, we're also interested in adding the statement to an
20109    outer node.  So it is convenient if we work around the fact that
20110    cp_parser_statement calls add_stmt.  */
20111
20112 static unsigned
20113 cp_parser_begin_omp_structured_block (cp_parser *parser)
20114 {
20115   unsigned save = parser->in_statement;
20116
20117   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20118      This preserves the "not within loop or switch" style error messages
20119      for nonsense cases like
20120         void foo() {
20121         #pragma omp single
20122           break;
20123         }
20124   */
20125   if (parser->in_statement)
20126     parser->in_statement = IN_OMP_BLOCK;
20127
20128   return save;
20129 }
20130
20131 static void
20132 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20133 {
20134   parser->in_statement = save;
20135 }
20136
20137 static tree
20138 cp_parser_omp_structured_block (cp_parser *parser)
20139 {
20140   tree stmt = begin_omp_structured_block ();
20141   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20142
20143   cp_parser_statement (parser, NULL_TREE, false, NULL);
20144
20145   cp_parser_end_omp_structured_block (parser, save);
20146   return finish_omp_structured_block (stmt);
20147 }
20148
20149 /* OpenMP 2.5:
20150    # pragma omp atomic new-line
20151      expression-stmt
20152
20153    expression-stmt:
20154      x binop= expr | x++ | ++x | x-- | --x
20155    binop:
20156      +, *, -, /, &, ^, |, <<, >>
20157
20158   where x is an lvalue expression with scalar type.  */
20159
20160 static void
20161 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20162 {
20163   tree lhs, rhs;
20164   enum tree_code code;
20165
20166   cp_parser_require_pragma_eol (parser, pragma_tok);
20167
20168   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20169                                     /*cast_p=*/false);
20170   switch (TREE_CODE (lhs))
20171     {
20172     case ERROR_MARK:
20173       goto saw_error;
20174
20175     case PREINCREMENT_EXPR:
20176     case POSTINCREMENT_EXPR:
20177       lhs = TREE_OPERAND (lhs, 0);
20178       code = PLUS_EXPR;
20179       rhs = integer_one_node;
20180       break;
20181
20182     case PREDECREMENT_EXPR:
20183     case POSTDECREMENT_EXPR:
20184       lhs = TREE_OPERAND (lhs, 0);
20185       code = MINUS_EXPR;
20186       rhs = integer_one_node;
20187       break;
20188
20189     default:
20190       switch (cp_lexer_peek_token (parser->lexer)->type)
20191         {
20192         case CPP_MULT_EQ:
20193           code = MULT_EXPR;
20194           break;
20195         case CPP_DIV_EQ:
20196           code = TRUNC_DIV_EXPR;
20197           break;
20198         case CPP_PLUS_EQ:
20199           code = PLUS_EXPR;
20200           break;
20201         case CPP_MINUS_EQ:
20202           code = MINUS_EXPR;
20203           break;
20204         case CPP_LSHIFT_EQ:
20205           code = LSHIFT_EXPR;
20206           break;
20207         case CPP_RSHIFT_EQ:
20208           code = RSHIFT_EXPR;
20209           break;
20210         case CPP_AND_EQ:
20211           code = BIT_AND_EXPR;
20212           break;
20213         case CPP_OR_EQ:
20214           code = BIT_IOR_EXPR;
20215           break;
20216         case CPP_XOR_EQ:
20217           code = BIT_XOR_EXPR;
20218           break;
20219         default:
20220           cp_parser_error (parser,
20221                            "invalid operator for %<#pragma omp atomic%>");
20222           goto saw_error;
20223         }
20224       cp_lexer_consume_token (parser->lexer);
20225
20226       rhs = cp_parser_expression (parser, false);
20227       if (rhs == error_mark_node)
20228         goto saw_error;
20229       break;
20230     }
20231   finish_omp_atomic (code, lhs, rhs);
20232   cp_parser_consume_semicolon_at_end_of_statement (parser);
20233   return;
20234
20235  saw_error:
20236   cp_parser_skip_to_end_of_block_or_statement (parser);
20237 }
20238
20239
20240 /* OpenMP 2.5:
20241    # pragma omp barrier new-line  */
20242
20243 static void
20244 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20245 {
20246   cp_parser_require_pragma_eol (parser, pragma_tok);
20247   finish_omp_barrier ();
20248 }
20249
20250 /* OpenMP 2.5:
20251    # pragma omp critical [(name)] new-line
20252      structured-block  */
20253
20254 static tree
20255 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20256 {
20257   tree stmt, name = NULL;
20258
20259   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20260     {
20261       cp_lexer_consume_token (parser->lexer);
20262
20263       name = cp_parser_identifier (parser);
20264
20265       if (name == error_mark_node
20266           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20267         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20268                                                /*or_comma=*/false,
20269                                                /*consume_paren=*/true);
20270       if (name == error_mark_node)
20271         name = NULL;
20272     }
20273   cp_parser_require_pragma_eol (parser, pragma_tok);
20274
20275   stmt = cp_parser_omp_structured_block (parser);
20276   return c_finish_omp_critical (stmt, name);
20277 }
20278
20279 /* OpenMP 2.5:
20280    # pragma omp flush flush-vars[opt] new-line
20281
20282    flush-vars:
20283      ( variable-list ) */
20284
20285 static void
20286 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20287 {
20288   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20289     (void) cp_parser_omp_var_list (parser, 0, NULL);
20290   cp_parser_require_pragma_eol (parser, pragma_tok);
20291
20292   finish_omp_flush ();
20293 }
20294
20295 /* Helper function, to parse omp for increment expression.  */
20296
20297 static tree
20298 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20299 {
20300   tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20301   enum tree_code op;
20302   cp_token *token;
20303
20304   if (lhs != decl)
20305     {
20306       cp_parser_skip_to_end_of_statement (parser);
20307       return error_mark_node;
20308     }
20309
20310   token = cp_lexer_peek_token (parser->lexer);
20311   op = binops_by_token [token->type].tree_type;
20312   switch (op)
20313     {
20314     case LT_EXPR:
20315     case LE_EXPR:
20316     case GT_EXPR:
20317     case GE_EXPR:
20318       break;
20319     default:
20320       cp_parser_skip_to_end_of_statement (parser);
20321       return error_mark_node;
20322     }
20323
20324   cp_lexer_consume_token (parser->lexer);
20325   rhs = cp_parser_binary_expression (parser, false,
20326                                      PREC_RELATIONAL_EXPRESSION);
20327   if (rhs == error_mark_node
20328       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20329     {
20330       cp_parser_skip_to_end_of_statement (parser);
20331       return error_mark_node;
20332     }
20333
20334   return build2 (op, boolean_type_node, lhs, rhs);
20335 }
20336
20337 /* Helper function, to parse omp for increment expression.  */
20338
20339 static tree
20340 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20341 {
20342   cp_token *token = cp_lexer_peek_token (parser->lexer);
20343   enum tree_code op;
20344   tree lhs, rhs;
20345   cp_id_kind idk;
20346   bool decl_first;
20347
20348   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20349     {
20350       op = (token->type == CPP_PLUS_PLUS
20351             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20352       cp_lexer_consume_token (parser->lexer);
20353       lhs = cp_parser_cast_expression (parser, false, false);
20354       if (lhs != decl)
20355         return error_mark_node;
20356       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20357     }
20358
20359   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20360   if (lhs != decl)
20361     return error_mark_node;
20362
20363   token = cp_lexer_peek_token (parser->lexer);
20364   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20365     {
20366       op = (token->type == CPP_PLUS_PLUS
20367             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20368       cp_lexer_consume_token (parser->lexer);
20369       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20370     }
20371
20372   op = cp_parser_assignment_operator_opt (parser);
20373   if (op == ERROR_MARK)
20374     return error_mark_node;
20375
20376   if (op != NOP_EXPR)
20377     {
20378       rhs = cp_parser_assignment_expression (parser, false);
20379       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20380       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20381     }
20382
20383   lhs = cp_parser_binary_expression (parser, false,
20384                                      PREC_ADDITIVE_EXPRESSION);
20385   token = cp_lexer_peek_token (parser->lexer);
20386   decl_first = lhs == decl;
20387   if (decl_first)
20388     lhs = NULL_TREE;
20389   if (token->type != CPP_PLUS
20390       && token->type != CPP_MINUS)
20391     return error_mark_node;
20392
20393   do
20394     {
20395       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20396       cp_lexer_consume_token (parser->lexer);
20397       rhs = cp_parser_binary_expression (parser, false,
20398                                          PREC_ADDITIVE_EXPRESSION);
20399       token = cp_lexer_peek_token (parser->lexer);
20400       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20401         {
20402           if (lhs == NULL_TREE)
20403             {
20404               if (op == PLUS_EXPR)
20405                 lhs = rhs;
20406               else
20407                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20408             }
20409           else
20410             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20411                                      NULL, tf_warning_or_error);
20412         }
20413     }
20414   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20415
20416   if (!decl_first)
20417     {
20418       if (rhs != decl || op == MINUS_EXPR)
20419         return error_mark_node;
20420       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20421     }
20422   else
20423     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20424
20425   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20426 }
20427
20428 /* Parse the restricted form of the for statment allowed by OpenMP.  */
20429
20430 static tree
20431 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20432 {
20433   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20434   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20435   tree this_pre_body, cl;
20436   location_t loc_first;
20437   bool collapse_err = false;
20438   int i, collapse = 1, nbraces = 0;
20439
20440   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20441     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20442       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20443
20444   gcc_assert (collapse >= 1);
20445
20446   declv = make_tree_vec (collapse);
20447   initv = make_tree_vec (collapse);
20448   condv = make_tree_vec (collapse);
20449   incrv = make_tree_vec (collapse);
20450
20451   loc_first = cp_lexer_peek_token (parser->lexer)->location;
20452
20453   for (i = 0; i < collapse; i++)
20454     {
20455       int bracecount = 0;
20456       bool add_private_clause = false;
20457       location_t loc;
20458
20459       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20460         {
20461           cp_parser_error (parser, "for statement expected");
20462           return NULL;
20463         }
20464       loc = cp_lexer_consume_token (parser->lexer)->location;
20465
20466       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20467         return NULL;
20468
20469       init = decl = real_decl = NULL;
20470       this_pre_body = push_stmt_list ();
20471       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20472         {
20473           cp_decl_specifier_seq type_specifiers;
20474
20475           /* First, try to parse as an initialized declaration.  See
20476              cp_parser_condition, from whence the bulk of this is copied.  */
20477
20478           cp_parser_parse_tentatively (parser);
20479           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20480                                         &type_specifiers);
20481           if (!cp_parser_error_occurred (parser))
20482             {
20483               tree asm_specification, attributes;
20484               cp_declarator *declarator;
20485
20486               declarator = cp_parser_declarator (parser,
20487                                                  CP_PARSER_DECLARATOR_NAMED,
20488                                                  /*ctor_dtor_or_conv_p=*/NULL,
20489                                                  /*parenthesized_p=*/NULL,
20490                                                  /*member_p=*/false);
20491               attributes = cp_parser_attributes_opt (parser);
20492               asm_specification = cp_parser_asm_specification_opt (parser);
20493
20494               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20495                 cp_parser_require (parser, CPP_EQ, "%<=%>");
20496               if (cp_parser_parse_definitely (parser))
20497                 {
20498                   tree pushed_scope;
20499
20500                   decl = start_decl (declarator, &type_specifiers,
20501                                      /*initialized_p=*/false, attributes,
20502                                      /*prefix_attributes=*/NULL_TREE,
20503                                      &pushed_scope);
20504
20505                   if (CLASS_TYPE_P (TREE_TYPE (decl))
20506                       || type_dependent_expression_p (decl))
20507                     {
20508                       bool is_parenthesized_init, is_non_constant_init;
20509
20510                       init = cp_parser_initializer (parser,
20511                                                     &is_parenthesized_init,
20512                                                     &is_non_constant_init);
20513
20514                       cp_finish_decl (decl, init, !is_non_constant_init,
20515                                       asm_specification,
20516                                       LOOKUP_ONLYCONVERTING);
20517                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
20518                         {
20519                           for_block
20520                             = tree_cons (NULL, this_pre_body, for_block);
20521                           init = NULL_TREE;
20522                         }
20523                       else
20524                         init = pop_stmt_list (this_pre_body);
20525                       this_pre_body = NULL_TREE;
20526                     }
20527                   else
20528                     {
20529                       cp_parser_require (parser, CPP_EQ, "%<=%>");
20530                       init = cp_parser_assignment_expression (parser, false);
20531
20532                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20533                         init = error_mark_node;
20534                       else
20535                         cp_finish_decl (decl, NULL_TREE,
20536                                         /*init_const_expr_p=*/false,
20537                                         asm_specification,
20538                                         LOOKUP_ONLYCONVERTING);
20539                     }
20540
20541                   if (pushed_scope)
20542                     pop_scope (pushed_scope);
20543                 }
20544             }
20545           else
20546             cp_parser_abort_tentative_parse (parser);
20547
20548           /* If parsing as an initialized declaration failed, try again as
20549              a simple expression.  */
20550           if (decl == NULL)
20551             {
20552               cp_id_kind idk;
20553               cp_parser_parse_tentatively (parser);
20554               decl = cp_parser_primary_expression (parser, false, false,
20555                                                    false, &idk);
20556               if (!cp_parser_error_occurred (parser)
20557                   && decl
20558                   && DECL_P (decl)
20559                   && CLASS_TYPE_P (TREE_TYPE (decl)))
20560                 {
20561                   tree rhs;
20562
20563                   cp_parser_parse_definitely (parser);
20564                   cp_parser_require (parser, CPP_EQ, "%<=%>");
20565                   rhs = cp_parser_assignment_expression (parser, false);
20566                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
20567                                                          rhs,
20568                                                          tf_warning_or_error));
20569                   add_private_clause = true;
20570                 }
20571               else
20572                 {
20573                   decl = NULL;
20574                   cp_parser_abort_tentative_parse (parser);
20575                   init = cp_parser_expression (parser, false);
20576                   if (init)
20577                     {
20578                       if (TREE_CODE (init) == MODIFY_EXPR
20579                           || TREE_CODE (init) == MODOP_EXPR)
20580                         real_decl = TREE_OPERAND (init, 0);
20581                     }
20582                 }
20583             }
20584         }
20585       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20586       if (this_pre_body)
20587         {
20588           this_pre_body = pop_stmt_list (this_pre_body);
20589           if (pre_body)
20590             {
20591               tree t = pre_body;
20592               pre_body = push_stmt_list ();
20593               add_stmt (t);
20594               add_stmt (this_pre_body);
20595               pre_body = pop_stmt_list (pre_body);
20596             }
20597           else
20598             pre_body = this_pre_body;
20599         }
20600
20601       if (decl)
20602         real_decl = decl;
20603       if (par_clauses != NULL && real_decl != NULL_TREE)
20604         {
20605           tree *c;
20606           for (c = par_clauses; *c ; )
20607             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
20608                 && OMP_CLAUSE_DECL (*c) == real_decl)
20609               {
20610                 error ("%Hiteration variable %qD should not be firstprivate",
20611                        &loc, real_decl);
20612                 *c = OMP_CLAUSE_CHAIN (*c);
20613               }
20614             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
20615                      && OMP_CLAUSE_DECL (*c) == real_decl)
20616               {
20617                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
20618                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
20619                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
20620                 OMP_CLAUSE_DECL (l) = real_decl;
20621                 OMP_CLAUSE_CHAIN (l) = clauses;
20622                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
20623                 clauses = l;
20624                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
20625                 CP_OMP_CLAUSE_INFO (*c) = NULL;
20626                 add_private_clause = false;
20627               }
20628             else
20629               {
20630                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
20631                     && OMP_CLAUSE_DECL (*c) == real_decl)
20632                   add_private_clause = false;
20633                 c = &OMP_CLAUSE_CHAIN (*c);
20634               }
20635         }
20636
20637       if (add_private_clause)
20638         {
20639           tree c;
20640           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20641             {
20642               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
20643                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
20644                   && OMP_CLAUSE_DECL (c) == decl)
20645                 break;
20646               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
20647                        && OMP_CLAUSE_DECL (c) == decl)
20648                 error ("%Hiteration variable %qD should not be firstprivate",
20649                        &loc, decl);
20650               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
20651                        && OMP_CLAUSE_DECL (c) == decl)
20652                 error ("%Hiteration variable %qD should not be reduction",
20653                        &loc, decl);
20654             }
20655           if (c == NULL)
20656             {
20657               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
20658               OMP_CLAUSE_DECL (c) = decl;
20659               c = finish_omp_clauses (c);
20660               if (c)
20661                 {
20662                   OMP_CLAUSE_CHAIN (c) = clauses;
20663                   clauses = c;
20664                 }
20665             }
20666         }
20667
20668       cond = NULL;
20669       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20670         {
20671           /* If decl is an iterator, preserve LHS and RHS of the relational
20672              expr until finish_omp_for.  */
20673           if (decl
20674               && (type_dependent_expression_p (decl)
20675                   || CLASS_TYPE_P (TREE_TYPE (decl))))
20676             cond = cp_parser_omp_for_cond (parser, decl);
20677           else
20678             cond = cp_parser_condition (parser);
20679         }
20680       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20681
20682       incr = NULL;
20683       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20684         {
20685           /* If decl is an iterator, preserve the operator on decl
20686              until finish_omp_for.  */
20687           if (decl
20688               && (type_dependent_expression_p (decl)
20689                   || CLASS_TYPE_P (TREE_TYPE (decl))))
20690             incr = cp_parser_omp_for_incr (parser, decl);
20691           else
20692             incr = cp_parser_expression (parser, false);
20693         }
20694
20695       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20696         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20697                                                /*or_comma=*/false,
20698                                                /*consume_paren=*/true);
20699
20700       TREE_VEC_ELT (declv, i) = decl;
20701       TREE_VEC_ELT (initv, i) = init;
20702       TREE_VEC_ELT (condv, i) = cond;
20703       TREE_VEC_ELT (incrv, i) = incr;
20704
20705       if (i == collapse - 1)
20706         break;
20707
20708       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
20709          in between the collapsed for loops to be still considered perfectly
20710          nested.  Hopefully the final version clarifies this.
20711          For now handle (multiple) {'s and empty statements.  */
20712       cp_parser_parse_tentatively (parser);
20713       do
20714         {
20715           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20716             break;
20717           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20718             {
20719               cp_lexer_consume_token (parser->lexer);
20720               bracecount++;
20721             }
20722           else if (bracecount
20723                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20724             cp_lexer_consume_token (parser->lexer);
20725           else
20726             {
20727               loc = cp_lexer_peek_token (parser->lexer)->location;
20728               error ("%Hnot enough collapsed for loops", &loc);
20729               collapse_err = true;
20730               cp_parser_abort_tentative_parse (parser);
20731               declv = NULL_TREE;
20732               break;
20733             }
20734         }
20735       while (1);
20736
20737       if (declv)
20738         {
20739           cp_parser_parse_definitely (parser);
20740           nbraces += bracecount;
20741         }
20742     }
20743
20744   /* Note that we saved the original contents of this flag when we entered
20745      the structured block, and so we don't need to re-save it here.  */
20746   parser->in_statement = IN_OMP_FOR;
20747
20748   /* Note that the grammar doesn't call for a structured block here,
20749      though the loop as a whole is a structured block.  */
20750   body = push_stmt_list ();
20751   cp_parser_statement (parser, NULL_TREE, false, NULL);
20752   body = pop_stmt_list (body);
20753
20754   if (declv == NULL_TREE)
20755     ret = NULL_TREE;
20756   else
20757     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
20758                           pre_body, clauses);
20759
20760   while (nbraces)
20761     {
20762       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20763         {
20764           cp_lexer_consume_token (parser->lexer);
20765           nbraces--;
20766         }
20767       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20768         cp_lexer_consume_token (parser->lexer);
20769       else
20770         {
20771           if (!collapse_err)
20772             error ("collapsed loops not perfectly nested");
20773           collapse_err = true;
20774           cp_parser_statement_seq_opt (parser, NULL);
20775           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
20776         }
20777     }
20778
20779   while (for_block)
20780     {
20781       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
20782       for_block = TREE_CHAIN (for_block);
20783     }
20784
20785   return ret;
20786 }
20787
20788 /* OpenMP 2.5:
20789    #pragma omp for for-clause[optseq] new-line
20790      for-loop  */
20791
20792 #define OMP_FOR_CLAUSE_MASK                             \
20793         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20794         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20795         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20796         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20797         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
20798         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
20799         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
20800         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
20801
20802 static tree
20803 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20804 {
20805   tree clauses, sb, ret;
20806   unsigned int save;
20807
20808   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20809                                        "#pragma omp for", pragma_tok);
20810
20811   sb = begin_omp_structured_block ();
20812   save = cp_parser_begin_omp_structured_block (parser);
20813
20814   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
20815
20816   cp_parser_end_omp_structured_block (parser, save);
20817   add_stmt (finish_omp_structured_block (sb));
20818
20819   return ret;
20820 }
20821
20822 /* OpenMP 2.5:
20823    # pragma omp master new-line
20824      structured-block  */
20825
20826 static tree
20827 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20828 {
20829   cp_parser_require_pragma_eol (parser, pragma_tok);
20830   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20831 }
20832
20833 /* OpenMP 2.5:
20834    # pragma omp ordered new-line
20835      structured-block  */
20836
20837 static tree
20838 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20839 {
20840   cp_parser_require_pragma_eol (parser, pragma_tok);
20841   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20842 }
20843
20844 /* OpenMP 2.5:
20845
20846    section-scope:
20847      { section-sequence }
20848
20849    section-sequence:
20850      section-directive[opt] structured-block
20851      section-sequence section-directive structured-block  */
20852
20853 static tree
20854 cp_parser_omp_sections_scope (cp_parser *parser)
20855 {
20856   tree stmt, substmt;
20857   bool error_suppress = false;
20858   cp_token *tok;
20859
20860   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
20861     return NULL_TREE;
20862
20863   stmt = push_stmt_list ();
20864
20865   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20866     {
20867       unsigned save;
20868
20869       substmt = begin_omp_structured_block ();
20870       save = cp_parser_begin_omp_structured_block (parser);
20871
20872       while (1)
20873         {
20874           cp_parser_statement (parser, NULL_TREE, false, NULL);
20875
20876           tok = cp_lexer_peek_token (parser->lexer);
20877           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20878             break;
20879           if (tok->type == CPP_CLOSE_BRACE)
20880             break;
20881           if (tok->type == CPP_EOF)
20882             break;
20883         }
20884
20885       cp_parser_end_omp_structured_block (parser, save);
20886       substmt = finish_omp_structured_block (substmt);
20887       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20888       add_stmt (substmt);
20889     }
20890
20891   while (1)
20892     {
20893       tok = cp_lexer_peek_token (parser->lexer);
20894       if (tok->type == CPP_CLOSE_BRACE)
20895         break;
20896       if (tok->type == CPP_EOF)
20897         break;
20898
20899       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20900         {
20901           cp_lexer_consume_token (parser->lexer);
20902           cp_parser_require_pragma_eol (parser, tok);
20903           error_suppress = false;
20904         }
20905       else if (!error_suppress)
20906         {
20907           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20908           error_suppress = true;
20909         }
20910
20911       substmt = cp_parser_omp_structured_block (parser);
20912       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20913       add_stmt (substmt);
20914     }
20915   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
20916
20917   substmt = pop_stmt_list (stmt);
20918
20919   stmt = make_node (OMP_SECTIONS);
20920   TREE_TYPE (stmt) = void_type_node;
20921   OMP_SECTIONS_BODY (stmt) = substmt;
20922
20923   add_stmt (stmt);
20924   return stmt;
20925 }
20926
20927 /* OpenMP 2.5:
20928    # pragma omp sections sections-clause[optseq] newline
20929      sections-scope  */
20930
20931 #define OMP_SECTIONS_CLAUSE_MASK                        \
20932         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20933         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20934         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20935         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20936         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20937
20938 static tree
20939 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20940 {
20941   tree clauses, ret;
20942
20943   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20944                                        "#pragma omp sections", pragma_tok);
20945
20946   ret = cp_parser_omp_sections_scope (parser);
20947   if (ret)
20948     OMP_SECTIONS_CLAUSES (ret) = clauses;
20949
20950   return ret;
20951 }
20952
20953 /* OpenMP 2.5:
20954    # pragma parallel parallel-clause new-line
20955    # pragma parallel for parallel-for-clause new-line
20956    # pragma parallel sections parallel-sections-clause new-line  */
20957
20958 #define OMP_PARALLEL_CLAUSE_MASK                        \
20959         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
20960         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20961         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20962         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
20963         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
20964         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
20965         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20966         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20967
20968 static tree
20969 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20970 {
20971   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20972   const char *p_name = "#pragma omp parallel";
20973   tree stmt, clauses, par_clause, ws_clause, block;
20974   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20975   unsigned int save;
20976
20977   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20978     {
20979       cp_lexer_consume_token (parser->lexer);
20980       p_kind = PRAGMA_OMP_PARALLEL_FOR;
20981       p_name = "#pragma omp parallel for";
20982       mask |= OMP_FOR_CLAUSE_MASK;
20983       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20984     }
20985   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20986     {
20987       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20988       const char *p = IDENTIFIER_POINTER (id);
20989       if (strcmp (p, "sections") == 0)
20990         {
20991           cp_lexer_consume_token (parser->lexer);
20992           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20993           p_name = "#pragma omp parallel sections";
20994           mask |= OMP_SECTIONS_CLAUSE_MASK;
20995           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20996         }
20997     }
20998
20999   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21000   block = begin_omp_parallel ();
21001   save = cp_parser_begin_omp_structured_block (parser);
21002
21003   switch (p_kind)
21004     {
21005     case PRAGMA_OMP_PARALLEL:
21006       cp_parser_statement (parser, NULL_TREE, false, NULL);
21007       par_clause = clauses;
21008       break;
21009
21010     case PRAGMA_OMP_PARALLEL_FOR:
21011       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21012       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21013       break;
21014
21015     case PRAGMA_OMP_PARALLEL_SECTIONS:
21016       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21017       stmt = cp_parser_omp_sections_scope (parser);
21018       if (stmt)
21019         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21020       break;
21021
21022     default:
21023       gcc_unreachable ();
21024     }
21025
21026   cp_parser_end_omp_structured_block (parser, save);
21027   stmt = finish_omp_parallel (par_clause, block);
21028   if (p_kind != PRAGMA_OMP_PARALLEL)
21029     OMP_PARALLEL_COMBINED (stmt) = 1;
21030   return stmt;
21031 }
21032
21033 /* OpenMP 2.5:
21034    # pragma omp single single-clause[optseq] new-line
21035      structured-block  */
21036
21037 #define OMP_SINGLE_CLAUSE_MASK                          \
21038         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21039         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21040         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21041         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21042
21043 static tree
21044 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21045 {
21046   tree stmt = make_node (OMP_SINGLE);
21047   TREE_TYPE (stmt) = void_type_node;
21048
21049   OMP_SINGLE_CLAUSES (stmt)
21050     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21051                                  "#pragma omp single", pragma_tok);
21052   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21053
21054   return add_stmt (stmt);
21055 }
21056
21057 /* OpenMP 3.0:
21058    # pragma omp task task-clause[optseq] new-line
21059      structured-block  */
21060
21061 #define OMP_TASK_CLAUSE_MASK                            \
21062         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21063         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21064         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21065         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21066         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21067         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21068
21069 static tree
21070 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21071 {
21072   tree clauses, block;
21073   unsigned int save;
21074
21075   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21076                                        "#pragma omp task", pragma_tok);
21077   block = begin_omp_task ();
21078   save = cp_parser_begin_omp_structured_block (parser);
21079   cp_parser_statement (parser, NULL_TREE, false, NULL);
21080   cp_parser_end_omp_structured_block (parser, save);
21081   return finish_omp_task (clauses, block);
21082 }
21083
21084 /* OpenMP 3.0:
21085    # pragma omp taskwait new-line  */
21086
21087 static void
21088 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21089 {
21090   cp_parser_require_pragma_eol (parser, pragma_tok);
21091   finish_omp_taskwait ();
21092 }
21093
21094 /* OpenMP 2.5:
21095    # pragma omp threadprivate (variable-list) */
21096
21097 static void
21098 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21099 {
21100   tree vars;
21101
21102   vars = cp_parser_omp_var_list (parser, 0, NULL);
21103   cp_parser_require_pragma_eol (parser, pragma_tok);
21104
21105   finish_omp_threadprivate (vars);
21106 }
21107
21108 /* Main entry point to OpenMP statement pragmas.  */
21109
21110 static void
21111 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21112 {
21113   tree stmt;
21114
21115   switch (pragma_tok->pragma_kind)
21116     {
21117     case PRAGMA_OMP_ATOMIC:
21118       cp_parser_omp_atomic (parser, pragma_tok);
21119       return;
21120     case PRAGMA_OMP_CRITICAL:
21121       stmt = cp_parser_omp_critical (parser, pragma_tok);
21122       break;
21123     case PRAGMA_OMP_FOR:
21124       stmt = cp_parser_omp_for (parser, pragma_tok);
21125       break;
21126     case PRAGMA_OMP_MASTER:
21127       stmt = cp_parser_omp_master (parser, pragma_tok);
21128       break;
21129     case PRAGMA_OMP_ORDERED:
21130       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21131       break;
21132     case PRAGMA_OMP_PARALLEL:
21133       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21134       break;
21135     case PRAGMA_OMP_SECTIONS:
21136       stmt = cp_parser_omp_sections (parser, pragma_tok);
21137       break;
21138     case PRAGMA_OMP_SINGLE:
21139       stmt = cp_parser_omp_single (parser, pragma_tok);
21140       break;
21141     case PRAGMA_OMP_TASK:
21142       stmt = cp_parser_omp_task (parser, pragma_tok);
21143       break;
21144     default:
21145       gcc_unreachable ();
21146     }
21147
21148   if (stmt)
21149     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21150 }
21151 \f
21152 /* The parser.  */
21153
21154 static GTY (()) cp_parser *the_parser;
21155
21156 \f
21157 /* Special handling for the first token or line in the file.  The first
21158    thing in the file might be #pragma GCC pch_preprocess, which loads a
21159    PCH file, which is a GC collection point.  So we need to handle this
21160    first pragma without benefit of an existing lexer structure.
21161
21162    Always returns one token to the caller in *FIRST_TOKEN.  This is
21163    either the true first token of the file, or the first token after
21164    the initial pragma.  */
21165
21166 static void
21167 cp_parser_initial_pragma (cp_token *first_token)
21168 {
21169   tree name = NULL;
21170
21171   cp_lexer_get_preprocessor_token (NULL, first_token);
21172   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21173     return;
21174
21175   cp_lexer_get_preprocessor_token (NULL, first_token);
21176   if (first_token->type == CPP_STRING)
21177     {
21178       name = first_token->u.value;
21179
21180       cp_lexer_get_preprocessor_token (NULL, first_token);
21181       if (first_token->type != CPP_PRAGMA_EOL)
21182         error ("junk at end of %<#pragma GCC pch_preprocess%>");
21183     }
21184   else
21185     error ("expected string literal");
21186
21187   /* Skip to the end of the pragma.  */
21188   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21189     cp_lexer_get_preprocessor_token (NULL, first_token);
21190
21191   /* Now actually load the PCH file.  */
21192   if (name)
21193     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21194
21195   /* Read one more token to return to our caller.  We have to do this
21196      after reading the PCH file in, since its pointers have to be
21197      live.  */
21198   cp_lexer_get_preprocessor_token (NULL, first_token);
21199 }
21200
21201 /* Normal parsing of a pragma token.  Here we can (and must) use the
21202    regular lexer.  */
21203
21204 static bool
21205 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21206 {
21207   cp_token *pragma_tok;
21208   unsigned int id;
21209
21210   pragma_tok = cp_lexer_consume_token (parser->lexer);
21211   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21212   parser->lexer->in_pragma = true;
21213
21214   id = pragma_tok->pragma_kind;
21215   switch (id)
21216     {
21217     case PRAGMA_GCC_PCH_PREPROCESS:
21218       error ("%<#pragma GCC pch_preprocess%> must be first");
21219       break;
21220
21221     case PRAGMA_OMP_BARRIER:
21222       switch (context)
21223         {
21224         case pragma_compound:
21225           cp_parser_omp_barrier (parser, pragma_tok);
21226           return false;
21227         case pragma_stmt:
21228           error ("%<#pragma omp barrier%> may only be "
21229                  "used in compound statements");
21230           break;
21231         default:
21232           goto bad_stmt;
21233         }
21234       break;
21235
21236     case PRAGMA_OMP_FLUSH:
21237       switch (context)
21238         {
21239         case pragma_compound:
21240           cp_parser_omp_flush (parser, pragma_tok);
21241           return false;
21242         case pragma_stmt:
21243           error ("%<#pragma omp flush%> may only be "
21244                  "used in compound statements");
21245           break;
21246         default:
21247           goto bad_stmt;
21248         }
21249       break;
21250
21251     case PRAGMA_OMP_TASKWAIT:
21252       switch (context)
21253         {
21254         case pragma_compound:
21255           cp_parser_omp_taskwait (parser, pragma_tok);
21256           return false;
21257         case pragma_stmt:
21258           error ("%<#pragma omp taskwait%> may only be "
21259                  "used in compound statements");
21260           break;
21261         default:
21262           goto bad_stmt;
21263         }
21264       break;
21265
21266     case PRAGMA_OMP_THREADPRIVATE:
21267       cp_parser_omp_threadprivate (parser, pragma_tok);
21268       return false;
21269
21270     case PRAGMA_OMP_ATOMIC:
21271     case PRAGMA_OMP_CRITICAL:
21272     case PRAGMA_OMP_FOR:
21273     case PRAGMA_OMP_MASTER:
21274     case PRAGMA_OMP_ORDERED:
21275     case PRAGMA_OMP_PARALLEL:
21276     case PRAGMA_OMP_SECTIONS:
21277     case PRAGMA_OMP_SINGLE:
21278     case PRAGMA_OMP_TASK:
21279       if (context == pragma_external)
21280         goto bad_stmt;
21281       cp_parser_omp_construct (parser, pragma_tok);
21282       return true;
21283
21284     case PRAGMA_OMP_SECTION:
21285       error ("%<#pragma omp section%> may only be used in "
21286              "%<#pragma omp sections%> construct");
21287       break;
21288
21289     default:
21290       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21291       c_invoke_pragma_handler (id);
21292       break;
21293
21294     bad_stmt:
21295       cp_parser_error (parser, "expected declaration specifiers");
21296       break;
21297     }
21298
21299   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21300   return false;
21301 }
21302
21303 /* The interface the pragma parsers have to the lexer.  */
21304
21305 enum cpp_ttype
21306 pragma_lex (tree *value)
21307 {
21308   cp_token *tok;
21309   enum cpp_ttype ret;
21310
21311   tok = cp_lexer_peek_token (the_parser->lexer);
21312
21313   ret = tok->type;
21314   *value = tok->u.value;
21315
21316   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21317     ret = CPP_EOF;
21318   else if (ret == CPP_STRING)
21319     *value = cp_parser_string_literal (the_parser, false, false);
21320   else
21321     {
21322       cp_lexer_consume_token (the_parser->lexer);
21323       if (ret == CPP_KEYWORD)
21324         ret = CPP_NAME;
21325     }
21326
21327   return ret;
21328 }
21329
21330 \f
21331 /* External interface.  */
21332
21333 /* Parse one entire translation unit.  */
21334
21335 void
21336 c_parse_file (void)
21337 {
21338   bool error_occurred;
21339   static bool already_called = false;
21340
21341   if (already_called)
21342     {
21343       sorry ("inter-module optimizations not implemented for C++");
21344       return;
21345     }
21346   already_called = true;
21347
21348   the_parser = cp_parser_new ();
21349   push_deferring_access_checks (flag_access_control
21350                                 ? dk_no_deferred : dk_no_check);
21351   error_occurred = cp_parser_translation_unit (the_parser);
21352   the_parser = NULL;
21353 }
21354
21355 #include "gt-cp-parser.h"