OSDN Git Service

PR c++/37806
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009  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 context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The location at which this token was found.  */
81   location_t location;
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 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99 };
100
101 /* The cp_lexer structure represents the C++ lexer.  It is responsible
102    for managing the token stream from the preprocessor and supplying
103    it to the parser.  Tokens are never added to the cp_lexer after
104    it is created.  */
105
106 typedef struct cp_lexer GTY (())
107 {
108   /* The memory allocated for the buffer.  NULL if this lexer does not
109      own the token buffer.  */
110   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
111   /* If the lexer owns the buffer, this is the number of tokens in the
112      buffer.  */
113   size_t buffer_length;
114
115   /* A pointer just past the last available token.  The tokens
116      in this lexer are [buffer, last_token).  */
117   cp_token_position GTY ((skip)) last_token;
118
119   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
120      no more available tokens.  */
121   cp_token_position GTY ((skip)) next_token;
122
123   /* A stack indicating positions at which cp_lexer_save_tokens was
124      called.  The top entry is the most recent position at which we
125      began saving tokens.  If the stack is non-empty, we are saving
126      tokens.  */
127   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
128
129   /* The next lexer in a linked list of lexers.  */
130   struct cp_lexer *next;
131
132   /* True if we should output debugging information.  */
133   bool debugging_p;
134
135   /* True if we're in the context of parsing a pragma, and should not
136      increment past the end-of-line marker.  */
137   bool in_pragma;
138 } cp_lexer;
139
140 /* cp_token_cache is a range of tokens.  There is no need to represent
141    allocate heap memory for it, since tokens are never removed from the
142    lexer's array.  There is also no need for the GC to walk through
143    a cp_token_cache, since everything in here is referenced through
144    a lexer.  */
145
146 typedef struct cp_token_cache GTY(())
147 {
148   /* The beginning of the token range.  */
149   cp_token * GTY((skip)) first;
150
151   /* Points immediately after the last token in the range.  */
152   cp_token * GTY ((skip)) last;
153 } cp_token_cache;
154
155 /* Prototypes.  */
156
157 static cp_lexer *cp_lexer_new_main
158   (void);
159 static cp_lexer *cp_lexer_new_from_tokens
160   (cp_token_cache *tokens);
161 static void cp_lexer_destroy
162   (cp_lexer *);
163 static int cp_lexer_saving_tokens
164   (const cp_lexer *);
165 static cp_token_position cp_lexer_token_position
166   (cp_lexer *, bool);
167 static cp_token *cp_lexer_token_at
168   (cp_lexer *, cp_token_position);
169 static void cp_lexer_get_preprocessor_token
170   (cp_lexer *, cp_token *);
171 static inline cp_token *cp_lexer_peek_token
172   (cp_lexer *);
173 static cp_token *cp_lexer_peek_nth_token
174   (cp_lexer *, size_t);
175 static inline bool cp_lexer_next_token_is
176   (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_not
178   (cp_lexer *, enum cpp_ttype);
179 static bool cp_lexer_next_token_is_keyword
180   (cp_lexer *, enum rid);
181 static cp_token *cp_lexer_consume_token
182   (cp_lexer *);
183 static void cp_lexer_purge_token
184   (cp_lexer *);
185 static void cp_lexer_purge_tokens_after
186   (cp_lexer *, cp_token_position);
187 static void cp_lexer_save_tokens
188   (cp_lexer *);
189 static void cp_lexer_commit_tokens
190   (cp_lexer *);
191 static void cp_lexer_rollback_tokens
192   (cp_lexer *);
193 #ifdef ENABLE_CHECKING
194 static void cp_lexer_print_token
195   (FILE *, cp_token *);
196 static inline bool cp_lexer_debugging_p
197   (cp_lexer *);
198 static void cp_lexer_start_debugging
199   (cp_lexer *) ATTRIBUTE_UNUSED;
200 static void cp_lexer_stop_debugging
201   (cp_lexer *) ATTRIBUTE_UNUSED;
202 #else
203 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
204    about passing NULL to functions that require non-NULL arguments
205    (fputs, fprintf).  It will never be used, so all we need is a value
206    of the right type that's guaranteed not to be NULL.  */
207 #define cp_lexer_debug_stream stdout
208 #define cp_lexer_print_token(str, tok) (void) 0
209 #define cp_lexer_debugging_p(lexer) 0
210 #endif /* ENABLE_CHECKING */
211
212 static cp_token_cache *cp_token_cache_new
213   (cp_token *, cp_token *);
214
215 static void cp_parser_initial_pragma
216   (cp_token *);
217
218 /* Manifest constants.  */
219 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
220 #define CP_SAVED_TOKEN_STACK 5
221
222 /* A token type for keywords, as opposed to ordinary identifiers.  */
223 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
224
225 /* A token type for template-ids.  If a template-id is processed while
226    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
227    the value of the CPP_TEMPLATE_ID is whatever was returned by
228    cp_parser_template_id.  */
229 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
230
231 /* A token type for nested-name-specifiers.  If a
232    nested-name-specifier is processed while parsing tentatively, it is
233    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
234    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
235    cp_parser_nested_name_specifier_opt.  */
236 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
237
238 /* A token type for tokens that are not tokens at all; these are used
239    to represent slots in the array where there used to be a token
240    that has now been deleted.  */
241 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
242
243 /* The number of token types, including C++-specific ones.  */
244 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245
246 /* Variables.  */
247
248 #ifdef ENABLE_CHECKING
249 /* The stream to which debugging output should be written.  */
250 static FILE *cp_lexer_debug_stream;
251 #endif /* ENABLE_CHECKING */
252
253 /* Create a new main C++ lexer, the lexer that gets tokens from the
254    preprocessor.  */
255
256 static cp_lexer *
257 cp_lexer_new_main (void)
258 {
259   cp_token first_token;
260   cp_lexer *lexer;
261   cp_token *pos;
262   size_t alloc;
263   size_t space;
264   cp_token *buffer;
265
266   /* It's possible that parsing the first pragma will load a PCH file,
267      which is a GC collection point.  So we have to do that before
268      allocating any memory.  */
269   cp_parser_initial_pragma (&first_token);
270
271   c_common_no_more_pch ();
272
273   /* Allocate the memory.  */
274   lexer = GGC_CNEW (cp_lexer);
275
276 #ifdef ENABLE_CHECKING
277   /* Initially we are not debugging.  */
278   lexer->debugging_p = false;
279 #endif /* ENABLE_CHECKING */
280   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
281                                    CP_SAVED_TOKEN_STACK);
282
283   /* Create the buffer.  */
284   alloc = CP_LEXER_BUFFER_SIZE;
285   buffer = GGC_NEWVEC (cp_token, alloc);
286
287   /* Put the first token in the buffer.  */
288   space = alloc;
289   pos = buffer;
290   *pos = first_token;
291
292   /* Get the remaining tokens from the preprocessor.  */
293   while (pos->type != CPP_EOF)
294     {
295       pos++;
296       if (!--space)
297         {
298           space = alloc;
299           alloc *= 2;
300           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
301           pos = buffer + space;
302         }
303       cp_lexer_get_preprocessor_token (lexer, pos);
304     }
305   lexer->buffer = buffer;
306   lexer->buffer_length = alloc - space;
307   lexer->last_token = pos;
308   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
309
310   /* Subsequent preprocessor diagnostics should use compiler
311      diagnostic functions to get the compiler source location.  */
312   done_lexing = true;
313
314   gcc_assert (lexer->next_token->type != CPP_PURGED);
315   return lexer;
316 }
317
318 /* Create a new lexer whose token stream is primed with the tokens in
319    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
320
321 static cp_lexer *
322 cp_lexer_new_from_tokens (cp_token_cache *cache)
323 {
324   cp_token *first = cache->first;
325   cp_token *last = cache->last;
326   cp_lexer *lexer = GGC_CNEW (cp_lexer);
327
328   /* We do not own the buffer.  */
329   lexer->buffer = NULL;
330   lexer->buffer_length = 0;
331   lexer->next_token = first == last ? &eof_token : first;
332   lexer->last_token = last;
333
334   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
335                                    CP_SAVED_TOKEN_STACK);
336
337 #ifdef ENABLE_CHECKING
338   /* Initially we are not debugging.  */
339   lexer->debugging_p = false;
340 #endif
341
342   gcc_assert (lexer->next_token->type != CPP_PURGED);
343   return lexer;
344 }
345
346 /* Frees all resources associated with LEXER.  */
347
348 static void
349 cp_lexer_destroy (cp_lexer *lexer)
350 {
351   if (lexer->buffer)
352     ggc_free (lexer->buffer);
353   VEC_free (cp_token_position, heap, lexer->saved_tokens);
354   ggc_free (lexer);
355 }
356
357 /* Returns nonzero if debugging information should be output.  */
358
359 #ifdef ENABLE_CHECKING
360
361 static inline bool
362 cp_lexer_debugging_p (cp_lexer *lexer)
363 {
364   return lexer->debugging_p;
365 }
366
367 #endif /* ENABLE_CHECKING */
368
369 static inline cp_token_position
370 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
371 {
372   gcc_assert (!previous_p || lexer->next_token != &eof_token);
373
374   return lexer->next_token - previous_p;
375 }
376
377 static inline cp_token *
378 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
379 {
380   return pos;
381 }
382
383 /* nonzero if we are presently saving tokens.  */
384
385 static inline int
386 cp_lexer_saving_tokens (const cp_lexer* lexer)
387 {
388   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
389 }
390
391 /* Store the next token from the preprocessor in *TOKEN.  Return true
392    if we reach EOF.  If LEXER is NULL, assume we are handling an
393    initial #pragma pch_preprocess, and thus want the lexer to return
394    processed strings.  */
395
396 static void
397 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
398 {
399   static int is_extern_c = 0;
400
401    /* Get a new token from the preprocessor.  */
402   token->type
403     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
404                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
405   token->keyword = RID_MAX;
406   token->pragma_kind = PRAGMA_NONE;
407
408   /* On some systems, some header files are surrounded by an
409      implicit extern "C" block.  Set a flag in the token if it
410      comes from such a header.  */
411   is_extern_c += pending_lang_change;
412   pending_lang_change = 0;
413   token->implicit_extern_c = is_extern_c > 0;
414
415   /* Check to see if this token is a keyword.  */
416   if (token->type == CPP_NAME)
417     {
418       if (C_IS_RESERVED_WORD (token->u.value))
419         {
420           /* Mark this token as a keyword.  */
421           token->type = CPP_KEYWORD;
422           /* Record which keyword.  */
423           token->keyword = C_RID_CODE (token->u.value);
424           /* Update the value.  Some keywords are mapped to particular
425              entities, rather than simply having the value of the
426              corresponding IDENTIFIER_NODE.  For example, `__const' is
427              mapped to `const'.  */
428           token->u.value = ridpointers[token->keyword];
429         }
430       else
431         {
432           if (warn_cxx0x_compat
433               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
434               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
435             {
436               /* Warn about the C++0x keyword (but still treat it as
437                  an identifier).  */
438               warning (OPT_Wc__0x_compat, 
439                        "identifier %<%s%> will become a keyword in C++0x",
440                        IDENTIFIER_POINTER (token->u.value));
441
442               /* Clear out the C_RID_CODE so we don't warn about this
443                  particular identifier-turned-keyword again.  */
444               C_SET_RID_CODE (token->u.value, RID_MAX);
445             }
446
447           token->ambiguous_p = false;
448           token->keyword = RID_MAX;
449         }
450     }
451   /* Handle Objective-C++ keywords.  */
452   else if (token->type == CPP_AT_NAME)
453     {
454       token->type = CPP_KEYWORD;
455       switch (C_RID_CODE (token->u.value))
456         {
457         /* Map 'class' to '@class', 'private' to '@private', etc.  */
458         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
459         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
460         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
461         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
462         case RID_THROW: token->keyword = RID_AT_THROW; break;
463         case RID_TRY: token->keyword = RID_AT_TRY; break;
464         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
465         default: token->keyword = C_RID_CODE (token->u.value);
466         }
467     }
468   else if (token->type == CPP_PRAGMA)
469     {
470       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
471       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
472       token->u.value = NULL_TREE;
473     }
474 }
475
476 /* Update the globals input_location and the input file stack from TOKEN.  */
477 static inline void
478 cp_lexer_set_source_position_from_token (cp_token *token)
479 {
480   if (token->type != CPP_EOF)
481     {
482       input_location = token->location;
483     }
484 }
485
486 /* Return a pointer to the next token in the token stream, but do not
487    consume it.  */
488
489 static inline cp_token *
490 cp_lexer_peek_token (cp_lexer *lexer)
491 {
492   if (cp_lexer_debugging_p (lexer))
493     {
494       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
495       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
496       putc ('\n', cp_lexer_debug_stream);
497     }
498   return lexer->next_token;
499 }
500
501 /* Return true if the next token has the indicated TYPE.  */
502
503 static inline bool
504 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
505 {
506   return cp_lexer_peek_token (lexer)->type == type;
507 }
508
509 /* Return true if the next token does not have the indicated TYPE.  */
510
511 static inline bool
512 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
513 {
514   return !cp_lexer_next_token_is (lexer, type);
515 }
516
517 /* Return true if the next token is the indicated KEYWORD.  */
518
519 static inline bool
520 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
521 {
522   return cp_lexer_peek_token (lexer)->keyword == keyword;
523 }
524
525 /* Return true if the next token is not the indicated KEYWORD.  */
526
527 static inline bool
528 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
529 {
530   return cp_lexer_peek_token (lexer)->keyword != keyword;
531 }
532
533 /* Return true if the next token is a keyword for a decl-specifier.  */
534
535 static bool
536 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
537 {
538   cp_token *token;
539
540   token = cp_lexer_peek_token (lexer);
541   switch (token->keyword) 
542     {
543       /* auto specifier: storage-class-specifier in C++,
544          simple-type-specifier in C++0x.  */
545     case RID_AUTO:
546       /* Storage classes.  */
547     case RID_REGISTER:
548     case RID_STATIC:
549     case RID_EXTERN:
550     case RID_MUTABLE:
551     case RID_THREAD:
552       /* Elaborated type specifiers.  */
553     case RID_ENUM:
554     case RID_CLASS:
555     case RID_STRUCT:
556     case RID_UNION:
557     case RID_TYPENAME:
558       /* Simple type specifiers.  */
559     case RID_CHAR:
560     case RID_CHAR16:
561     case RID_CHAR32:
562     case RID_WCHAR:
563     case RID_BOOL:
564     case RID_SHORT:
565     case RID_INT:
566     case RID_LONG:
567     case RID_SIGNED:
568     case RID_UNSIGNED:
569     case RID_FLOAT:
570     case RID_DOUBLE:
571     case RID_VOID:
572       /* GNU extensions.  */ 
573     case RID_ATTRIBUTE:
574     case RID_TYPEOF:
575       /* C++0x extensions.  */
576     case RID_DECLTYPE:
577       return true;
578
579     default:
580       return false;
581     }
582 }
583
584 /* Return a pointer to the Nth token in the token stream.  If N is 1,
585    then this is precisely equivalent to cp_lexer_peek_token (except
586    that it is not inline).  One would like to disallow that case, but
587    there is one case (cp_parser_nth_token_starts_template_id) where
588    the caller passes a variable for N and it might be 1.  */
589
590 static cp_token *
591 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
592 {
593   cp_token *token;
594
595   /* N is 1-based, not zero-based.  */
596   gcc_assert (n > 0);
597
598   if (cp_lexer_debugging_p (lexer))
599     fprintf (cp_lexer_debug_stream,
600              "cp_lexer: peeking ahead %ld at token: ", (long)n);
601
602   --n;
603   token = lexer->next_token;
604   gcc_assert (!n || token != &eof_token);
605   while (n != 0)
606     {
607       ++token;
608       if (token == lexer->last_token)
609         {
610           token = &eof_token;
611           break;
612         }
613
614       if (token->type != CPP_PURGED)
615         --n;
616     }
617
618   if (cp_lexer_debugging_p (lexer))
619     {
620       cp_lexer_print_token (cp_lexer_debug_stream, token);
621       putc ('\n', cp_lexer_debug_stream);
622     }
623
624   return token;
625 }
626
627 /* Return the next token, and advance the lexer's next_token pointer
628    to point to the next non-purged token.  */
629
630 static cp_token *
631 cp_lexer_consume_token (cp_lexer* lexer)
632 {
633   cp_token *token = lexer->next_token;
634
635   gcc_assert (token != &eof_token);
636   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
637
638   do
639     {
640       lexer->next_token++;
641       if (lexer->next_token == lexer->last_token)
642         {
643           lexer->next_token = &eof_token;
644           break;
645         }
646
647     }
648   while (lexer->next_token->type == CPP_PURGED);
649
650   cp_lexer_set_source_position_from_token (token);
651
652   /* Provide debugging output.  */
653   if (cp_lexer_debugging_p (lexer))
654     {
655       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
656       cp_lexer_print_token (cp_lexer_debug_stream, token);
657       putc ('\n', cp_lexer_debug_stream);
658     }
659
660   return token;
661 }
662
663 /* Permanently remove the next token from the token stream, and
664    advance the next_token pointer to refer to the next non-purged
665    token.  */
666
667 static void
668 cp_lexer_purge_token (cp_lexer *lexer)
669 {
670   cp_token *tok = lexer->next_token;
671
672   gcc_assert (tok != &eof_token);
673   tok->type = CPP_PURGED;
674   tok->location = UNKNOWN_LOCATION;
675   tok->u.value = NULL_TREE;
676   tok->keyword = RID_MAX;
677
678   do
679     {
680       tok++;
681       if (tok == lexer->last_token)
682         {
683           tok = &eof_token;
684           break;
685         }
686     }
687   while (tok->type == CPP_PURGED);
688   lexer->next_token = tok;
689 }
690
691 /* Permanently remove all tokens after TOK, up to, but not
692    including, the token that will be returned next by
693    cp_lexer_peek_token.  */
694
695 static void
696 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
697 {
698   cp_token *peek = lexer->next_token;
699
700   if (peek == &eof_token)
701     peek = lexer->last_token;
702
703   gcc_assert (tok < peek);
704
705   for ( tok += 1; tok != peek; tok += 1)
706     {
707       tok->type = CPP_PURGED;
708       tok->location = UNKNOWN_LOCATION;
709       tok->u.value = NULL_TREE;
710       tok->keyword = RID_MAX;
711     }
712 }
713
714 /* Begin saving tokens.  All tokens consumed after this point will be
715    preserved.  */
716
717 static void
718 cp_lexer_save_tokens (cp_lexer* lexer)
719 {
720   /* Provide debugging output.  */
721   if (cp_lexer_debugging_p (lexer))
722     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
723
724   VEC_safe_push (cp_token_position, heap,
725                  lexer->saved_tokens, lexer->next_token);
726 }
727
728 /* Commit to the portion of the token stream most recently saved.  */
729
730 static void
731 cp_lexer_commit_tokens (cp_lexer* lexer)
732 {
733   /* Provide debugging output.  */
734   if (cp_lexer_debugging_p (lexer))
735     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
736
737   VEC_pop (cp_token_position, lexer->saved_tokens);
738 }
739
740 /* Return all tokens saved since the last call to cp_lexer_save_tokens
741    to the token stream.  Stop saving tokens.  */
742
743 static void
744 cp_lexer_rollback_tokens (cp_lexer* lexer)
745 {
746   /* Provide debugging output.  */
747   if (cp_lexer_debugging_p (lexer))
748     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
749
750   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
751 }
752
753 /* Print a representation of the TOKEN on the STREAM.  */
754
755 #ifdef ENABLE_CHECKING
756
757 static void
758 cp_lexer_print_token (FILE * stream, cp_token *token)
759 {
760   /* We don't use cpp_type2name here because the parser defines
761      a few tokens of its own.  */
762   static const char *const token_names[] = {
763     /* cpplib-defined token types */
764 #define OP(e, s) #e,
765 #define TK(e, s) #e,
766     TTYPE_TABLE
767 #undef OP
768 #undef TK
769     /* C++ parser token types - see "Manifest constants", above.  */
770     "KEYWORD",
771     "TEMPLATE_ID",
772     "NESTED_NAME_SPECIFIER",
773     "PURGED"
774   };
775
776   /* If we have a name for the token, print it out.  Otherwise, we
777      simply give the numeric code.  */
778   gcc_assert (token->type < ARRAY_SIZE(token_names));
779   fputs (token_names[token->type], stream);
780
781   /* For some tokens, print the associated data.  */
782   switch (token->type)
783     {
784     case CPP_KEYWORD:
785       /* Some keywords have a value that is not an IDENTIFIER_NODE.
786          For example, `struct' is mapped to an INTEGER_CST.  */
787       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
788         break;
789       /* else fall through */
790     case CPP_NAME:
791       fputs (IDENTIFIER_POINTER (token->u.value), stream);
792       break;
793
794     case CPP_STRING:
795     case CPP_STRING16:
796     case CPP_STRING32:
797     case CPP_WSTRING:
798       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
799       break;
800
801     default:
802       break;
803     }
804 }
805
806 /* Start emitting debugging information.  */
807
808 static void
809 cp_lexer_start_debugging (cp_lexer* lexer)
810 {
811   lexer->debugging_p = true;
812 }
813
814 /* Stop emitting debugging information.  */
815
816 static void
817 cp_lexer_stop_debugging (cp_lexer* lexer)
818 {
819   lexer->debugging_p = false;
820 }
821
822 #endif /* ENABLE_CHECKING */
823
824 /* Create a new cp_token_cache, representing a range of tokens.  */
825
826 static cp_token_cache *
827 cp_token_cache_new (cp_token *first, cp_token *last)
828 {
829   cp_token_cache *cache = GGC_NEW (cp_token_cache);
830   cache->first = first;
831   cache->last = last;
832   return cache;
833 }
834
835 \f
836 /* Decl-specifiers.  */
837
838 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
839
840 static void
841 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
842 {
843   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
844 }
845
846 /* Declarators.  */
847
848 /* Nothing other than the parser should be creating declarators;
849    declarators are a semi-syntactic representation of C++ entities.
850    Other parts of the front end that need to create entities (like
851    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
852
853 static cp_declarator *make_call_declarator
854   (cp_declarator *, tree, cp_cv_quals, tree, tree);
855 static cp_declarator *make_array_declarator
856   (cp_declarator *, tree);
857 static cp_declarator *make_pointer_declarator
858   (cp_cv_quals, cp_declarator *);
859 static cp_declarator *make_reference_declarator
860   (cp_cv_quals, cp_declarator *, bool);
861 static cp_parameter_declarator *make_parameter_declarator
862   (cp_decl_specifier_seq *, cp_declarator *, tree);
863 static cp_declarator *make_ptrmem_declarator
864   (cp_cv_quals, tree, cp_declarator *);
865
866 /* An erroneous declarator.  */
867 static cp_declarator *cp_error_declarator;
868
869 /* The obstack on which declarators and related data structures are
870    allocated.  */
871 static struct obstack declarator_obstack;
872
873 /* Alloc BYTES from the declarator memory pool.  */
874
875 static inline void *
876 alloc_declarator (size_t bytes)
877 {
878   return obstack_alloc (&declarator_obstack, bytes);
879 }
880
881 /* Allocate a declarator of the indicated KIND.  Clear fields that are
882    common to all declarators.  */
883
884 static cp_declarator *
885 make_declarator (cp_declarator_kind kind)
886 {
887   cp_declarator *declarator;
888
889   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
890   declarator->kind = kind;
891   declarator->attributes = NULL_TREE;
892   declarator->declarator = NULL;
893   declarator->parameter_pack_p = false;
894
895   return declarator;
896 }
897
898 /* Make a declarator for a generalized identifier.  If
899    QUALIFYING_SCOPE is non-NULL, the identifier is
900    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
902    is, if any.   */
903
904 static cp_declarator *
905 make_id_declarator (tree qualifying_scope, tree unqualified_name,
906                     special_function_kind sfk)
907 {
908   cp_declarator *declarator;
909
910   /* It is valid to write:
911
912        class C { void f(); };
913        typedef C D;
914        void D::f();
915
916      The standard is not clear about whether `typedef const C D' is
917      legal; as of 2002-09-15 the committee is considering that
918      question.  EDG 3.0 allows that syntax.  Therefore, we do as
919      well.  */
920   if (qualifying_scope && TYPE_P (qualifying_scope))
921     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922
923   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926
927   declarator = make_declarator (cdk_id);
928   declarator->u.id.qualifying_scope = qualifying_scope;
929   declarator->u.id.unqualified_name = unqualified_name;
930   declarator->u.id.sfk = sfk;
931   
932   return declarator;
933 }
934
935 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
936    of modifiers such as const or volatile to apply to the pointer
937    type, represented as identifiers.  */
938
939 cp_declarator *
940 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 {
942   cp_declarator *declarator;
943
944   declarator = make_declarator (cdk_pointer);
945   declarator->declarator = target;
946   declarator->u.pointer.qualifiers = cv_qualifiers;
947   declarator->u.pointer.class_type = NULL_TREE;
948   if (target)
949     {
950       declarator->parameter_pack_p = target->parameter_pack_p;
951       target->parameter_pack_p = false;
952     }
953   else
954     declarator->parameter_pack_p = false;
955
956   return declarator;
957 }
958
959 /* Like make_pointer_declarator -- but for references.  */
960
961 cp_declarator *
962 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963                            bool rvalue_ref)
964 {
965   cp_declarator *declarator;
966
967   declarator = make_declarator (cdk_reference);
968   declarator->declarator = target;
969   declarator->u.reference.qualifiers = cv_qualifiers;
970   declarator->u.reference.rvalue_ref = rvalue_ref;
971   if (target)
972     {
973       declarator->parameter_pack_p = target->parameter_pack_p;
974       target->parameter_pack_p = false;
975     }
976   else
977     declarator->parameter_pack_p = false;
978
979   return declarator;
980 }
981
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983    member of CLASS_TYPE.  */
984
985 cp_declarator *
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987                         cp_declarator *pointee)
988 {
989   cp_declarator *declarator;
990
991   declarator = make_declarator (cdk_ptrmem);
992   declarator->declarator = pointee;
993   declarator->u.pointer.qualifiers = cv_qualifiers;
994   declarator->u.pointer.class_type = class_type;
995
996   if (pointee)
997     {
998       declarator->parameter_pack_p = pointee->parameter_pack_p;
999       pointee->parameter_pack_p = false;
1000     }
1001   else
1002     declarator->parameter_pack_p = false;
1003
1004   return declarator;
1005 }
1006
1007 /* Make a declarator for the function given by TARGET, with the
1008    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1009    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1010    indicates what exceptions can be thrown.  */
1011
1012 cp_declarator *
1013 make_call_declarator (cp_declarator *target,
1014                       tree parms,
1015                       cp_cv_quals cv_qualifiers,
1016                       tree exception_specification,
1017                       tree late_return_type)
1018 {
1019   cp_declarator *declarator;
1020
1021   declarator = make_declarator (cdk_function);
1022   declarator->declarator = target;
1023   declarator->u.function.parameters = parms;
1024   declarator->u.function.qualifiers = cv_qualifiers;
1025   declarator->u.function.exception_specification = exception_specification;
1026   declarator->u.function.late_return_type = late_return_type;
1027   if (target)
1028     {
1029       declarator->parameter_pack_p = target->parameter_pack_p;
1030       target->parameter_pack_p = false;
1031     }
1032   else
1033     declarator->parameter_pack_p = false;
1034
1035   return declarator;
1036 }
1037
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039    defined by ELEMENT.  */
1040
1041 cp_declarator *
1042 make_array_declarator (cp_declarator *element, tree bounds)
1043 {
1044   cp_declarator *declarator;
1045
1046   declarator = make_declarator (cdk_array);
1047   declarator->declarator = element;
1048   declarator->u.array.bounds = bounds;
1049   if (element)
1050     {
1051       declarator->parameter_pack_p = element->parameter_pack_p;
1052       element->parameter_pack_p = false;
1053     }
1054   else
1055     declarator->parameter_pack_p = false;
1056
1057   return declarator;
1058 }
1059
1060 /* Determine whether the declarator we've seen so far can be a
1061    parameter pack, when followed by an ellipsis.  */
1062 static bool 
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 {
1065   /* Search for a declarator name, or any other declarator that goes
1066      after the point where the ellipsis could appear in a parameter
1067      pack. If we find any of these, then this declarator can not be
1068      made into a parameter pack.  */
1069   bool found = false;
1070   while (declarator && !found)
1071     {
1072       switch ((int)declarator->kind)
1073         {
1074         case cdk_id:
1075         case cdk_array:
1076           found = true;
1077           break;
1078
1079         case cdk_error:
1080           return true;
1081
1082         default:
1083           declarator = declarator->declarator;
1084           break;
1085         }
1086     }
1087
1088   return !found;
1089 }
1090
1091 cp_parameter_declarator *no_parameters;
1092
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094    DECLARATOR and DEFAULT_ARGUMENT.  */
1095
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098                            cp_declarator *declarator,
1099                            tree default_argument)
1100 {
1101   cp_parameter_declarator *parameter;
1102
1103   parameter = ((cp_parameter_declarator *)
1104                alloc_declarator (sizeof (cp_parameter_declarator)));
1105   parameter->next = NULL;
1106   if (decl_specifiers)
1107     parameter->decl_specifiers = *decl_specifiers;
1108   else
1109     clear_decl_specs (&parameter->decl_specifiers);
1110   parameter->declarator = declarator;
1111   parameter->default_argument = default_argument;
1112   parameter->ellipsis_p = false;
1113
1114   return parameter;
1115 }
1116
1117 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1118
1119 static bool
1120 function_declarator_p (const cp_declarator *declarator)
1121 {
1122   while (declarator)
1123     {
1124       if (declarator->kind == cdk_function
1125           && declarator->declarator->kind == cdk_id)
1126         return true;
1127       if (declarator->kind == cdk_id
1128           || declarator->kind == cdk_error)
1129         return false;
1130       declarator = declarator->declarator;
1131     }
1132   return false;
1133 }
1134  
1135 /* The parser.  */
1136
1137 /* Overview
1138    --------
1139
1140    A cp_parser parses the token stream as specified by the C++
1141    grammar.  Its job is purely parsing, not semantic analysis.  For
1142    example, the parser breaks the token stream into declarators,
1143    expressions, statements, and other similar syntactic constructs.
1144    It does not check that the types of the expressions on either side
1145    of an assignment-statement are compatible, or that a function is
1146    not declared with a parameter of type `void'.
1147
1148    The parser invokes routines elsewhere in the compiler to perform
1149    semantic analysis and to build up the abstract syntax tree for the
1150    code processed.
1151
1152    The parser (and the template instantiation code, which is, in a
1153    way, a close relative of parsing) are the only parts of the
1154    compiler that should be calling push_scope and pop_scope, or
1155    related functions.  The parser (and template instantiation code)
1156    keeps track of what scope is presently active; everything else
1157    should simply honor that.  (The code that generates static
1158    initializers may also need to set the scope, in order to check
1159    access control correctly when emitting the initializers.)
1160
1161    Methodology
1162    -----------
1163
1164    The parser is of the standard recursive-descent variety.  Upcoming
1165    tokens in the token stream are examined in order to determine which
1166    production to use when parsing a non-terminal.  Some C++ constructs
1167    require arbitrary look ahead to disambiguate.  For example, it is
1168    impossible, in the general case, to tell whether a statement is an
1169    expression or declaration without scanning the entire statement.
1170    Therefore, the parser is capable of "parsing tentatively."  When the
1171    parser is not sure what construct comes next, it enters this mode.
1172    Then, while we attempt to parse the construct, the parser queues up
1173    error messages, rather than issuing them immediately, and saves the
1174    tokens it consumes.  If the construct is parsed successfully, the
1175    parser "commits", i.e., it issues any queued error messages and
1176    the tokens that were being preserved are permanently discarded.
1177    If, however, the construct is not parsed successfully, the parser
1178    rolls back its state completely so that it can resume parsing using
1179    a different alternative.
1180
1181    Future Improvements
1182    -------------------
1183
1184    The performance of the parser could probably be improved substantially.
1185    We could often eliminate the need to parse tentatively by looking ahead
1186    a little bit.  In some places, this approach might not entirely eliminate
1187    the need to parse tentatively, but it might still speed up the average
1188    case.  */
1189
1190 /* Flags that are passed to some parsing functions.  These values can
1191    be bitwise-ored together.  */
1192
1193 typedef enum cp_parser_flags
1194 {
1195   /* No flags.  */
1196   CP_PARSER_FLAGS_NONE = 0x0,
1197   /* The construct is optional.  If it is not present, then no error
1198      should be issued.  */
1199   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200   /* When parsing a type-specifier, do not allow user-defined types.  */
1201   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1202 } cp_parser_flags;
1203
1204 /* The different kinds of declarators we want to parse.  */
1205
1206 typedef enum cp_parser_declarator_kind
1207 {
1208   /* We want an abstract declarator.  */
1209   CP_PARSER_DECLARATOR_ABSTRACT,
1210   /* We want a named declarator.  */
1211   CP_PARSER_DECLARATOR_NAMED,
1212   /* We don't mind, but the name must be an unqualified-id.  */
1213   CP_PARSER_DECLARATOR_EITHER
1214 } cp_parser_declarator_kind;
1215
1216 /* The precedence values used to parse binary expressions.  The minimum value
1217    of PREC must be 1, because zero is reserved to quickly discriminate
1218    binary operators from other tokens.  */
1219
1220 enum cp_parser_prec
1221 {
1222   PREC_NOT_OPERATOR,
1223   PREC_LOGICAL_OR_EXPRESSION,
1224   PREC_LOGICAL_AND_EXPRESSION,
1225   PREC_INCLUSIVE_OR_EXPRESSION,
1226   PREC_EXCLUSIVE_OR_EXPRESSION,
1227   PREC_AND_EXPRESSION,
1228   PREC_EQUALITY_EXPRESSION,
1229   PREC_RELATIONAL_EXPRESSION,
1230   PREC_SHIFT_EXPRESSION,
1231   PREC_ADDITIVE_EXPRESSION,
1232   PREC_MULTIPLICATIVE_EXPRESSION,
1233   PREC_PM_EXPRESSION,
1234   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1235 };
1236
1237 /* A mapping from a token type to a corresponding tree node type, with a
1238    precedence value.  */
1239
1240 typedef struct cp_parser_binary_operations_map_node
1241 {
1242   /* The token type.  */
1243   enum cpp_ttype token_type;
1244   /* The corresponding tree code.  */
1245   enum tree_code tree_type;
1246   /* The precedence of this operator.  */
1247   enum cp_parser_prec prec;
1248 } cp_parser_binary_operations_map_node;
1249
1250 /* The status of a tentative parse.  */
1251
1252 typedef enum cp_parser_status_kind
1253 {
1254   /* No errors have occurred.  */
1255   CP_PARSER_STATUS_KIND_NO_ERROR,
1256   /* An error has occurred.  */
1257   CP_PARSER_STATUS_KIND_ERROR,
1258   /* We are committed to this tentative parse, whether or not an error
1259      has occurred.  */
1260   CP_PARSER_STATUS_KIND_COMMITTED
1261 } cp_parser_status_kind;
1262
1263 typedef struct cp_parser_expression_stack_entry
1264 {
1265   /* Left hand side of the binary operation we are currently
1266      parsing.  */
1267   tree lhs;
1268   /* Original tree code for left hand side, if it was a binary
1269      expression itself (used for -Wparentheses).  */
1270   enum tree_code lhs_type;
1271   /* Tree code for the binary operation we are parsing.  */
1272   enum tree_code tree_type;
1273   /* Precedence of the binary operation we are parsing.  */
1274   int prec;
1275 } cp_parser_expression_stack_entry;
1276
1277 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1278    entries because precedence levels on the stack are monotonically
1279    increasing.  */
1280 typedef struct cp_parser_expression_stack_entry
1281   cp_parser_expression_stack[NUM_PREC_VALUES];
1282
1283 /* Context that is saved and restored when parsing tentatively.  */
1284 typedef struct cp_parser_context GTY (())
1285 {
1286   /* If this is a tentative parsing context, the status of the
1287      tentative parse.  */
1288   enum cp_parser_status_kind status;
1289   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1290      that are looked up in this context must be looked up both in the
1291      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292      the context of the containing expression.  */
1293   tree object_type;
1294
1295   /* The next parsing context in the stack.  */
1296   struct cp_parser_context *next;
1297 } cp_parser_context;
1298
1299 /* Prototypes.  */
1300
1301 /* Constructors and destructors.  */
1302
1303 static cp_parser_context *cp_parser_context_new
1304   (cp_parser_context *);
1305
1306 /* Class variables.  */
1307
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1309
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311    Transformed into an associative array (binops_by_token) by
1312    cp_parser_new.  */
1313
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1317
1318   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321
1322   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324
1325   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327
1328   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332
1333   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1335
1336   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1337
1338   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1339
1340   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1343
1344   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1345 };
1346
1347 /* The same as binops, but initialized by cp_parser_new so that
1348    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1349    for speed.  */
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1351
1352 /* Constructors and destructors.  */
1353
1354 /* Construct a new context.  The context below this one on the stack
1355    is given by NEXT.  */
1356
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1359 {
1360   cp_parser_context *context;
1361
1362   /* Allocate the storage.  */
1363   if (cp_parser_context_free_list != NULL)
1364     {
1365       /* Pull the first entry from the free list.  */
1366       context = cp_parser_context_free_list;
1367       cp_parser_context_free_list = context->next;
1368       memset (context, 0, sizeof (*context));
1369     }
1370   else
1371     context = GGC_CNEW (cp_parser_context);
1372
1373   /* No errors have occurred yet in this context.  */
1374   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375   /* If this is not the bottommost context, copy information that we
1376      need from the previous context.  */
1377   if (next)
1378     {
1379       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380          expression, then we are parsing one in this context, too.  */
1381       context->object_type = next->object_type;
1382       /* Thread the stack.  */
1383       context->next = next;
1384     }
1385
1386   return context;
1387 }
1388
1389 /* The cp_parser structure represents the C++ parser.  */
1390
1391 typedef struct cp_parser GTY(())
1392 {
1393   /* The lexer from which we are obtaining tokens.  */
1394   cp_lexer *lexer;
1395
1396   /* The scope in which names should be looked up.  If NULL_TREE, then
1397      we look up names in the scope that is currently open in the
1398      source program.  If non-NULL, this is either a TYPE or
1399      NAMESPACE_DECL for the scope in which we should look.  It can
1400      also be ERROR_MARK, when we've parsed a bogus scope.
1401
1402      This value is not cleared automatically after a name is looked
1403      up, so we must be careful to clear it before starting a new look
1404      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1405      will look up `Z' in the scope of `X', rather than the current
1406      scope.)  Unfortunately, it is difficult to tell when name lookup
1407      is complete, because we sometimes peek at a token, look it up,
1408      and then decide not to consume it.   */
1409   tree scope;
1410
1411   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1412      last lookup took place.  OBJECT_SCOPE is used if an expression
1413      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1414      respectively.  QUALIFYING_SCOPE is used for an expression of the
1415      form "X::Y"; it refers to X.  */
1416   tree object_scope;
1417   tree qualifying_scope;
1418
1419   /* A stack of parsing contexts.  All but the bottom entry on the
1420      stack will be tentative contexts.
1421
1422      We parse tentatively in order to determine which construct is in
1423      use in some situations.  For example, in order to determine
1424      whether a statement is an expression-statement or a
1425      declaration-statement we parse it tentatively as a
1426      declaration-statement.  If that fails, we then reparse the same
1427      token stream as an expression-statement.  */
1428   cp_parser_context *context;
1429
1430   /* True if we are parsing GNU C++.  If this flag is not set, then
1431      GNU extensions are not recognized.  */
1432   bool allow_gnu_extensions_p;
1433
1434   /* TRUE if the `>' token should be interpreted as the greater-than
1435      operator.  FALSE if it is the end of a template-id or
1436      template-parameter-list. In C++0x mode, this flag also applies to
1437      `>>' tokens, which are viewed as two consecutive `>' tokens when
1438      this flag is FALSE.  */
1439   bool greater_than_is_operator_p;
1440
1441   /* TRUE if default arguments are allowed within a parameter list
1442      that starts at this point. FALSE if only a gnu extension makes
1443      them permissible.  */
1444   bool default_arg_ok_p;
1445
1446   /* TRUE if we are parsing an integral constant-expression.  See
1447      [expr.const] for a precise definition.  */
1448   bool integral_constant_expression_p;
1449
1450   /* TRUE if we are parsing an integral constant-expression -- but a
1451      non-constant expression should be permitted as well.  This flag
1452      is used when parsing an array bound so that GNU variable-length
1453      arrays are tolerated.  */
1454   bool allow_non_integral_constant_expression_p;
1455
1456   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1457      been seen that makes the expression non-constant.  */
1458   bool non_integral_constant_expression_p;
1459
1460   /* TRUE if local variable names and `this' are forbidden in the
1461      current context.  */
1462   bool local_variables_forbidden_p;
1463
1464   /* TRUE if the declaration we are parsing is part of a
1465      linkage-specification of the form `extern string-literal
1466      declaration'.  */
1467   bool in_unbraced_linkage_specification_p;
1468
1469   /* TRUE if we are presently parsing a declarator, after the
1470      direct-declarator.  */
1471   bool in_declarator_p;
1472
1473   /* TRUE if we are presently parsing a template-argument-list.  */
1474   bool in_template_argument_list_p;
1475
1476   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1477      to IN_OMP_BLOCK if parsing OpenMP structured block and
1478      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1479      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1480      iteration-statement, OpenMP block or loop within that switch.  */
1481 #define IN_SWITCH_STMT          1
1482 #define IN_ITERATION_STMT       2
1483 #define IN_OMP_BLOCK            4
1484 #define IN_OMP_FOR              8
1485 #define IN_IF_STMT             16
1486   unsigned char in_statement;
1487
1488   /* TRUE if we are presently parsing the body of a switch statement.
1489      Note that this doesn't quite overlap with in_statement above.
1490      The difference relates to giving the right sets of error messages:
1491      "case not in switch" vs "break statement used with OpenMP...".  */
1492   bool in_switch_statement_p;
1493
1494   /* TRUE if we are parsing a type-id in an expression context.  In
1495      such a situation, both "type (expr)" and "type (type)" are valid
1496      alternatives.  */
1497   bool in_type_id_in_expr_p;
1498
1499   /* TRUE if we are currently in a header file where declarations are
1500      implicitly extern "C".  */
1501   bool implicit_extern_c;
1502
1503   /* TRUE if strings in expressions should be translated to the execution
1504      character set.  */
1505   bool translate_strings_p;
1506
1507   /* TRUE if we are presently parsing the body of a function, but not
1508      a local class.  */
1509   bool in_function_body;
1510
1511   /* If non-NULL, then we are parsing a construct where new type
1512      definitions are not permitted.  The string stored here will be
1513      issued as an error message if a type is defined.  */
1514   const char *type_definition_forbidden_message;
1515
1516   /* A list of lists. The outer list is a stack, used for member
1517      functions of local classes. At each level there are two sub-list,
1518      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1519      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1520      TREE_VALUE's. The functions are chained in reverse declaration
1521      order.
1522
1523      The TREE_PURPOSE sublist contains those functions with default
1524      arguments that need post processing, and the TREE_VALUE sublist
1525      contains those functions with definitions that need post
1526      processing.
1527
1528      These lists can only be processed once the outermost class being
1529      defined is complete.  */
1530   tree unparsed_functions_queues;
1531
1532   /* The number of classes whose definitions are currently in
1533      progress.  */
1534   unsigned num_classes_being_defined;
1535
1536   /* The number of template parameter lists that apply directly to the
1537      current declaration.  */
1538   unsigned num_template_parameter_lists;
1539 } cp_parser;
1540
1541 /* Prototypes.  */
1542
1543 /* Constructors and destructors.  */
1544
1545 static cp_parser *cp_parser_new
1546   (void);
1547
1548 /* Routines to parse various constructs.
1549
1550    Those that return `tree' will return the error_mark_node (rather
1551    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1552    Sometimes, they will return an ordinary node if error-recovery was
1553    attempted, even though a parse error occurred.  So, to check
1554    whether or not a parse error occurred, you should always use
1555    cp_parser_error_occurred.  If the construct is optional (indicated
1556    either by an `_opt' in the name of the function that does the
1557    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1558    the construct is not present.  */
1559
1560 /* Lexical conventions [gram.lex]  */
1561
1562 static tree cp_parser_identifier
1563   (cp_parser *);
1564 static tree cp_parser_string_literal
1565   (cp_parser *, bool, bool);
1566
1567 /* Basic concepts [gram.basic]  */
1568
1569 static bool cp_parser_translation_unit
1570   (cp_parser *);
1571
1572 /* Expressions [gram.expr]  */
1573
1574 static tree cp_parser_primary_expression
1575   (cp_parser *, bool, bool, bool, cp_id_kind *);
1576 static tree cp_parser_id_expression
1577   (cp_parser *, bool, bool, bool *, bool, bool);
1578 static tree cp_parser_unqualified_id
1579   (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier_opt
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_qualifying_entity
1585   (cp_parser *, bool, bool, bool, bool, bool);
1586 static tree cp_parser_postfix_expression
1587   (cp_parser *, bool, bool, bool, cp_id_kind *);
1588 static tree cp_parser_postfix_open_square_expression
1589   (cp_parser *, tree, bool);
1590 static tree cp_parser_postfix_dot_deref_expression
1591   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1592 static tree cp_parser_parenthesized_expression_list
1593   (cp_parser *, bool, bool, bool, bool *);
1594 static void cp_parser_pseudo_destructor_name
1595   (cp_parser *, tree *, tree *);
1596 static tree cp_parser_unary_expression
1597   (cp_parser *, bool, bool, cp_id_kind *);
1598 static enum tree_code cp_parser_unary_operator
1599   (cp_token *);
1600 static tree cp_parser_new_expression
1601   (cp_parser *);
1602 static tree cp_parser_new_placement
1603   (cp_parser *);
1604 static tree cp_parser_new_type_id
1605   (cp_parser *, tree *);
1606 static cp_declarator *cp_parser_new_declarator_opt
1607   (cp_parser *);
1608 static cp_declarator *cp_parser_direct_new_declarator
1609   (cp_parser *);
1610 static tree cp_parser_new_initializer
1611   (cp_parser *);
1612 static tree cp_parser_delete_expression
1613   (cp_parser *);
1614 static tree cp_parser_cast_expression
1615   (cp_parser *, bool, bool, cp_id_kind *);
1616 static tree cp_parser_binary_expression
1617   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1618 static tree cp_parser_question_colon_clause
1619   (cp_parser *, tree);
1620 static tree cp_parser_assignment_expression
1621   (cp_parser *, bool, cp_id_kind *);
1622 static enum tree_code cp_parser_assignment_operator_opt
1623   (cp_parser *);
1624 static tree cp_parser_expression
1625   (cp_parser *, bool, cp_id_kind *);
1626 static tree cp_parser_constant_expression
1627   (cp_parser *, bool, bool *);
1628 static tree cp_parser_builtin_offsetof
1629   (cp_parser *);
1630
1631 /* Statements [gram.stmt.stmt]  */
1632
1633 static void cp_parser_statement
1634   (cp_parser *, tree, bool, bool *);
1635 static void cp_parser_label_for_labeled_statement
1636   (cp_parser *);
1637 static tree cp_parser_expression_statement
1638   (cp_parser *, tree);
1639 static tree cp_parser_compound_statement
1640   (cp_parser *, tree, bool);
1641 static void cp_parser_statement_seq_opt
1642   (cp_parser *, tree);
1643 static tree cp_parser_selection_statement
1644   (cp_parser *, bool *);
1645 static tree cp_parser_condition
1646   (cp_parser *);
1647 static tree cp_parser_iteration_statement
1648   (cp_parser *);
1649 static void cp_parser_for_init_statement
1650   (cp_parser *);
1651 static tree cp_parser_jump_statement
1652   (cp_parser *);
1653 static void cp_parser_declaration_statement
1654   (cp_parser *);
1655
1656 static tree cp_parser_implicitly_scoped_statement
1657   (cp_parser *, bool *);
1658 static void cp_parser_already_scoped_statement
1659   (cp_parser *);
1660
1661 /* Declarations [gram.dcl.dcl] */
1662
1663 static void cp_parser_declaration_seq_opt
1664   (cp_parser *);
1665 static void cp_parser_declaration
1666   (cp_parser *);
1667 static void cp_parser_block_declaration
1668   (cp_parser *, bool);
1669 static void cp_parser_simple_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_decl_specifier_seq
1672   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1673 static tree cp_parser_storage_class_specifier_opt
1674   (cp_parser *);
1675 static tree cp_parser_function_specifier_opt
1676   (cp_parser *, cp_decl_specifier_seq *);
1677 static tree cp_parser_type_specifier
1678   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1679    int *, bool *);
1680 static tree cp_parser_simple_type_specifier
1681   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1682 static tree cp_parser_type_name
1683   (cp_parser *);
1684 static tree cp_parser_nonclass_name 
1685   (cp_parser* parser);
1686 static tree cp_parser_elaborated_type_specifier
1687   (cp_parser *, bool, bool);
1688 static tree cp_parser_enum_specifier
1689   (cp_parser *);
1690 static void cp_parser_enumerator_list
1691   (cp_parser *, tree);
1692 static void cp_parser_enumerator_definition
1693   (cp_parser *, tree);
1694 static tree cp_parser_namespace_name
1695   (cp_parser *);
1696 static void cp_parser_namespace_definition
1697   (cp_parser *);
1698 static void cp_parser_namespace_body
1699   (cp_parser *);
1700 static tree cp_parser_qualified_namespace_specifier
1701   (cp_parser *);
1702 static void cp_parser_namespace_alias_definition
1703   (cp_parser *);
1704 static bool cp_parser_using_declaration
1705   (cp_parser *, bool);
1706 static void cp_parser_using_directive
1707   (cp_parser *);
1708 static void cp_parser_asm_definition
1709   (cp_parser *);
1710 static void cp_parser_linkage_specification
1711   (cp_parser *);
1712 static void cp_parser_static_assert
1713   (cp_parser *, bool);
1714 static tree cp_parser_decltype
1715   (cp_parser *);
1716
1717 /* Declarators [gram.dcl.decl] */
1718
1719 static tree cp_parser_init_declarator
1720   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1721 static cp_declarator *cp_parser_declarator
1722   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1723 static cp_declarator *cp_parser_direct_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1725 static enum tree_code cp_parser_ptr_operator
1726   (cp_parser *, tree *, cp_cv_quals *);
1727 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1728   (cp_parser *);
1729 static tree cp_parser_late_return_type_opt
1730   (cp_parser *);
1731 static tree cp_parser_declarator_id
1732   (cp_parser *, bool);
1733 static tree cp_parser_type_id
1734   (cp_parser *);
1735 static tree cp_parser_template_type_arg
1736   (cp_parser *);
1737 static tree cp_parser_type_id_1
1738   (cp_parser *, bool);
1739 static void cp_parser_type_specifier_seq
1740   (cp_parser *, bool, cp_decl_specifier_seq *);
1741 static tree cp_parser_parameter_declaration_clause
1742   (cp_parser *);
1743 static tree cp_parser_parameter_declaration_list
1744   (cp_parser *, bool *);
1745 static cp_parameter_declarator *cp_parser_parameter_declaration
1746   (cp_parser *, bool, bool *);
1747 static tree cp_parser_default_argument 
1748   (cp_parser *, bool);
1749 static void cp_parser_function_body
1750   (cp_parser *);
1751 static tree cp_parser_initializer
1752   (cp_parser *, bool *, bool *);
1753 static tree cp_parser_initializer_clause
1754   (cp_parser *, bool *);
1755 static tree cp_parser_braced_list
1756   (cp_parser*, bool*);
1757 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1758   (cp_parser *, bool *);
1759
1760 static bool cp_parser_ctor_initializer_opt_and_function_body
1761   (cp_parser *);
1762
1763 /* Classes [gram.class] */
1764
1765 static tree cp_parser_class_name
1766   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1767 static tree cp_parser_class_specifier
1768   (cp_parser *);
1769 static tree cp_parser_class_head
1770   (cp_parser *, bool *, tree *, tree *);
1771 static enum tag_types cp_parser_class_key
1772   (cp_parser *);
1773 static void cp_parser_member_specification_opt
1774   (cp_parser *);
1775 static void cp_parser_member_declaration
1776   (cp_parser *);
1777 static tree cp_parser_pure_specifier
1778   (cp_parser *);
1779 static tree cp_parser_constant_initializer
1780   (cp_parser *);
1781
1782 /* Derived classes [gram.class.derived] */
1783
1784 static tree cp_parser_base_clause
1785   (cp_parser *);
1786 static tree cp_parser_base_specifier
1787   (cp_parser *);
1788
1789 /* Special member functions [gram.special] */
1790
1791 static tree cp_parser_conversion_function_id
1792   (cp_parser *);
1793 static tree cp_parser_conversion_type_id
1794   (cp_parser *);
1795 static cp_declarator *cp_parser_conversion_declarator_opt
1796   (cp_parser *);
1797 static bool cp_parser_ctor_initializer_opt
1798   (cp_parser *);
1799 static void cp_parser_mem_initializer_list
1800   (cp_parser *);
1801 static tree cp_parser_mem_initializer
1802   (cp_parser *);
1803 static tree cp_parser_mem_initializer_id
1804   (cp_parser *);
1805
1806 /* Overloading [gram.over] */
1807
1808 static tree cp_parser_operator_function_id
1809   (cp_parser *);
1810 static tree cp_parser_operator
1811   (cp_parser *);
1812
1813 /* Templates [gram.temp] */
1814
1815 static void cp_parser_template_declaration
1816   (cp_parser *, bool);
1817 static tree cp_parser_template_parameter_list
1818   (cp_parser *);
1819 static tree cp_parser_template_parameter
1820   (cp_parser *, bool *, bool *);
1821 static tree cp_parser_type_parameter
1822   (cp_parser *, bool *);
1823 static tree cp_parser_template_id
1824   (cp_parser *, bool, bool, bool);
1825 static tree cp_parser_template_name
1826   (cp_parser *, bool, bool, bool, bool *);
1827 static tree cp_parser_template_argument_list
1828   (cp_parser *);
1829 static tree cp_parser_template_argument
1830   (cp_parser *);
1831 static void cp_parser_explicit_instantiation
1832   (cp_parser *);
1833 static void cp_parser_explicit_specialization
1834   (cp_parser *);
1835
1836 /* Exception handling [gram.exception] */
1837
1838 static tree cp_parser_try_block
1839   (cp_parser *);
1840 static bool cp_parser_function_try_block
1841   (cp_parser *);
1842 static void cp_parser_handler_seq
1843   (cp_parser *);
1844 static void cp_parser_handler
1845   (cp_parser *);
1846 static tree cp_parser_exception_declaration
1847   (cp_parser *);
1848 static tree cp_parser_throw_expression
1849   (cp_parser *);
1850 static tree cp_parser_exception_specification_opt
1851   (cp_parser *);
1852 static tree cp_parser_type_id_list
1853   (cp_parser *);
1854
1855 /* GNU Extensions */
1856
1857 static tree cp_parser_asm_specification_opt
1858   (cp_parser *);
1859 static tree cp_parser_asm_operand_list
1860   (cp_parser *);
1861 static tree cp_parser_asm_clobber_list
1862   (cp_parser *);
1863 static tree cp_parser_attributes_opt
1864   (cp_parser *);
1865 static tree cp_parser_attribute_list
1866   (cp_parser *);
1867 static bool cp_parser_extension_opt
1868   (cp_parser *, int *);
1869 static void cp_parser_label_declaration
1870   (cp_parser *);
1871
1872 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1873 static bool cp_parser_pragma
1874   (cp_parser *, enum pragma_context);
1875
1876 /* Objective-C++ Productions */
1877
1878 static tree cp_parser_objc_message_receiver
1879   (cp_parser *);
1880 static tree cp_parser_objc_message_args
1881   (cp_parser *);
1882 static tree cp_parser_objc_message_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_encode_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_defs_expression
1887   (cp_parser *);
1888 static tree cp_parser_objc_protocol_expression
1889   (cp_parser *);
1890 static tree cp_parser_objc_selector_expression
1891   (cp_parser *);
1892 static tree cp_parser_objc_expression
1893   (cp_parser *);
1894 static bool cp_parser_objc_selector_p
1895   (enum cpp_ttype);
1896 static tree cp_parser_objc_selector
1897   (cp_parser *);
1898 static tree cp_parser_objc_protocol_refs_opt
1899   (cp_parser *);
1900 static void cp_parser_objc_declaration
1901   (cp_parser *);
1902 static tree cp_parser_objc_statement
1903   (cp_parser *);
1904
1905 /* Utility Routines */
1906
1907 static tree cp_parser_lookup_name
1908   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1909 static tree cp_parser_lookup_name_simple
1910   (cp_parser *, tree, location_t);
1911 static tree cp_parser_maybe_treat_template_as_class
1912   (tree, bool);
1913 static bool cp_parser_check_declarator_template_parameters
1914   (cp_parser *, cp_declarator *, location_t);
1915 static bool cp_parser_check_template_parameters
1916   (cp_parser *, unsigned, location_t);
1917 static tree cp_parser_simple_cast_expression
1918   (cp_parser *);
1919 static tree cp_parser_global_scope_opt
1920   (cp_parser *, bool);
1921 static bool cp_parser_constructor_declarator_p
1922   (cp_parser *, bool);
1923 static tree cp_parser_function_definition_from_specifiers_and_declarator
1924   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1925 static tree cp_parser_function_definition_after_declarator
1926   (cp_parser *, bool);
1927 static void cp_parser_template_declaration_after_export
1928   (cp_parser *, bool);
1929 static void cp_parser_perform_template_parameter_access_checks
1930   (VEC (deferred_access_check,gc)*);
1931 static tree cp_parser_single_declaration
1932   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1933 static tree cp_parser_functional_cast
1934   (cp_parser *, tree);
1935 static tree cp_parser_save_member_function_body
1936   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1937 static tree cp_parser_enclosed_template_argument_list
1938   (cp_parser *);
1939 static void cp_parser_save_default_args
1940   (cp_parser *, tree);
1941 static void cp_parser_late_parsing_for_member
1942   (cp_parser *, tree);
1943 static void cp_parser_late_parsing_default_args
1944   (cp_parser *, tree);
1945 static tree cp_parser_sizeof_operand
1946   (cp_parser *, enum rid);
1947 static tree cp_parser_trait_expr
1948   (cp_parser *, enum rid);
1949 static bool cp_parser_declares_only_class_p
1950   (cp_parser *);
1951 static void cp_parser_set_storage_class
1952   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1953 static void cp_parser_set_decl_spec_type
1954   (cp_decl_specifier_seq *, tree, location_t, bool);
1955 static bool cp_parser_friend_p
1956   (const cp_decl_specifier_seq *);
1957 static cp_token *cp_parser_require
1958   (cp_parser *, enum cpp_ttype, const char *);
1959 static cp_token *cp_parser_require_keyword
1960   (cp_parser *, enum rid, const char *);
1961 static bool cp_parser_token_starts_function_definition_p
1962   (cp_token *);
1963 static bool cp_parser_next_token_starts_class_definition_p
1964   (cp_parser *);
1965 static bool cp_parser_next_token_ends_template_argument_p
1966   (cp_parser *);
1967 static bool cp_parser_nth_token_starts_template_argument_list_p
1968   (cp_parser *, size_t);
1969 static enum tag_types cp_parser_token_is_class_key
1970   (cp_token *);
1971 static void cp_parser_check_class_key
1972   (enum tag_types, tree type);
1973 static void cp_parser_check_access_in_redeclaration
1974   (tree type, location_t location);
1975 static bool cp_parser_optional_template_keyword
1976   (cp_parser *);
1977 static void cp_parser_pre_parsed_nested_name_specifier
1978   (cp_parser *);
1979 static bool cp_parser_cache_group
1980   (cp_parser *, enum cpp_ttype, unsigned);
1981 static void cp_parser_parse_tentatively
1982   (cp_parser *);
1983 static void cp_parser_commit_to_tentative_parse
1984   (cp_parser *);
1985 static void cp_parser_abort_tentative_parse
1986   (cp_parser *);
1987 static bool cp_parser_parse_definitely
1988   (cp_parser *);
1989 static inline bool cp_parser_parsing_tentatively
1990   (cp_parser *);
1991 static bool cp_parser_uncommitted_to_tentative_parse_p
1992   (cp_parser *);
1993 static void cp_parser_error
1994   (cp_parser *, const char *);
1995 static void cp_parser_name_lookup_error
1996   (cp_parser *, tree, tree, const char *, location_t);
1997 static bool cp_parser_simulate_error
1998   (cp_parser *);
1999 static bool cp_parser_check_type_definition
2000   (cp_parser *);
2001 static void cp_parser_check_for_definition_in_return_type
2002   (cp_declarator *, tree, location_t type_location);
2003 static void cp_parser_check_for_invalid_template_id
2004   (cp_parser *, tree, location_t location);
2005 static bool cp_parser_non_integral_constant_expression
2006   (cp_parser *, const char *);
2007 static void cp_parser_diagnose_invalid_type_name
2008   (cp_parser *, tree, tree, location_t);
2009 static bool cp_parser_parse_and_diagnose_invalid_type_name
2010   (cp_parser *);
2011 static int cp_parser_skip_to_closing_parenthesis
2012   (cp_parser *, bool, bool, bool);
2013 static void cp_parser_skip_to_end_of_statement
2014   (cp_parser *);
2015 static void cp_parser_consume_semicolon_at_end_of_statement
2016   (cp_parser *);
2017 static void cp_parser_skip_to_end_of_block_or_statement
2018   (cp_parser *);
2019 static bool cp_parser_skip_to_closing_brace
2020   (cp_parser *);
2021 static void cp_parser_skip_to_end_of_template_parameter_list
2022   (cp_parser *);
2023 static void cp_parser_skip_to_pragma_eol
2024   (cp_parser*, cp_token *);
2025 static bool cp_parser_error_occurred
2026   (cp_parser *);
2027 static bool cp_parser_allow_gnu_extensions_p
2028   (cp_parser *);
2029 static bool cp_parser_is_string_literal
2030   (cp_token *);
2031 static bool cp_parser_is_keyword
2032   (cp_token *, enum rid);
2033 static tree cp_parser_make_typename_type
2034   (cp_parser *, tree, tree, location_t location);
2035 static cp_declarator * cp_parser_make_indirect_declarator
2036   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2037
2038 /* Returns nonzero if we are parsing tentatively.  */
2039
2040 static inline bool
2041 cp_parser_parsing_tentatively (cp_parser* parser)
2042 {
2043   return parser->context->next != NULL;
2044 }
2045
2046 /* Returns nonzero if TOKEN is a string literal.  */
2047
2048 static bool
2049 cp_parser_is_string_literal (cp_token* token)
2050 {
2051   return (token->type == CPP_STRING ||
2052           token->type == CPP_STRING16 ||
2053           token->type == CPP_STRING32 ||
2054           token->type == CPP_WSTRING);
2055 }
2056
2057 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2058
2059 static bool
2060 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2061 {
2062   return token->keyword == keyword;
2063 }
2064
2065 /* If not parsing tentatively, issue a diagnostic of the form
2066       FILE:LINE: MESSAGE before TOKEN
2067    where TOKEN is the next token in the input stream.  MESSAGE
2068    (specified by the caller) is usually of the form "expected
2069    OTHER-TOKEN".  */
2070
2071 static void
2072 cp_parser_error (cp_parser* parser, const char* message)
2073 {
2074   if (!cp_parser_simulate_error (parser))
2075     {
2076       cp_token *token = cp_lexer_peek_token (parser->lexer);
2077       /* This diagnostic makes more sense if it is tagged to the line
2078          of the token we just peeked at.  */
2079       cp_lexer_set_source_position_from_token (token);
2080
2081       if (token->type == CPP_PRAGMA)
2082         {
2083           error ("%H%<#pragma%> is not allowed here", &token->location);
2084           cp_parser_skip_to_pragma_eol (parser, token);
2085           return;
2086         }
2087
2088       c_parse_error (message,
2089                      /* Because c_parser_error does not understand
2090                         CPP_KEYWORD, keywords are treated like
2091                         identifiers.  */
2092                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2093                      token->u.value);
2094     }
2095 }
2096
2097 /* Issue an error about name-lookup failing.  NAME is the
2098    IDENTIFIER_NODE DECL is the result of
2099    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2100    the thing that we hoped to find.  */
2101
2102 static void
2103 cp_parser_name_lookup_error (cp_parser* parser,
2104                              tree name,
2105                              tree decl,
2106                              const char* desired,
2107                              location_t location)
2108 {
2109   /* If name lookup completely failed, tell the user that NAME was not
2110      declared.  */
2111   if (decl == error_mark_node)
2112     {
2113       if (parser->scope && parser->scope != global_namespace)
2114         error ("%H%<%E::%E%> has not been declared",
2115                &location, parser->scope, name);
2116       else if (parser->scope == global_namespace)
2117         error ("%H%<::%E%> has not been declared", &location, name);
2118       else if (parser->object_scope
2119                && !CLASS_TYPE_P (parser->object_scope))
2120         error ("%Hrequest for member %qE in non-class type %qT",
2121                &location, name, parser->object_scope);
2122       else if (parser->object_scope)
2123         error ("%H%<%T::%E%> has not been declared",
2124                &location, parser->object_scope, name);
2125       else
2126         error ("%H%qE has not been declared", &location, name);
2127     }
2128   else if (parser->scope && parser->scope != global_namespace)
2129     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2130   else if (parser->scope == global_namespace)
2131     error ("%H%<::%E%> %s", &location, name, desired);
2132   else
2133     error ("%H%qE %s", &location, name, desired);
2134 }
2135
2136 /* If we are parsing tentatively, remember that an error has occurred
2137    during this tentative parse.  Returns true if the error was
2138    simulated; false if a message should be issued by the caller.  */
2139
2140 static bool
2141 cp_parser_simulate_error (cp_parser* parser)
2142 {
2143   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2144     {
2145       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2146       return true;
2147     }
2148   return false;
2149 }
2150
2151 /* Check for repeated decl-specifiers.  */
2152
2153 static void
2154 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2155                            location_t location)
2156 {
2157   cp_decl_spec ds;
2158
2159   for (ds = ds_first; ds != ds_last; ++ds)
2160     {
2161       unsigned count = decl_specs->specs[(int)ds];
2162       if (count < 2)
2163         continue;
2164       /* The "long" specifier is a special case because of "long long".  */
2165       if (ds == ds_long)
2166         {
2167           if (count > 2)
2168             error ("%H%<long long long%> is too long for GCC", &location);
2169           else if (pedantic && !in_system_header && warn_long_long
2170                    && cxx_dialect == cxx98)
2171             pedwarn (location, OPT_Wlong_long, 
2172                      "ISO C++ 1998 does not support %<long long%>");
2173         }
2174       else if (count > 1)
2175         {
2176           static const char *const decl_spec_names[] = {
2177             "signed",
2178             "unsigned",
2179             "short",
2180             "long",
2181             "const",
2182             "volatile",
2183             "restrict",
2184             "inline",
2185             "virtual",
2186             "explicit",
2187             "friend",
2188             "typedef",
2189             "__complex",
2190             "__thread"
2191           };
2192           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2193         }
2194     }
2195 }
2196
2197 /* This function is called when a type is defined.  If type
2198    definitions are forbidden at this point, an error message is
2199    issued.  */
2200
2201 static bool
2202 cp_parser_check_type_definition (cp_parser* parser)
2203 {
2204   /* If types are forbidden here, issue a message.  */
2205   if (parser->type_definition_forbidden_message)
2206     {
2207       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2208          in the message need to be interpreted.  */
2209       error (parser->type_definition_forbidden_message);
2210       return false;
2211     }
2212   return true;
2213 }
2214
2215 /* This function is called when the DECLARATOR is processed.  The TYPE
2216    was a type defined in the decl-specifiers.  If it is invalid to
2217    define a type in the decl-specifiers for DECLARATOR, an error is
2218    issued. TYPE_LOCATION is the location of TYPE and is used
2219    for error reporting.  */
2220
2221 static void
2222 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2223                                                tree type, location_t type_location)
2224 {
2225   /* [dcl.fct] forbids type definitions in return types.
2226      Unfortunately, it's not easy to know whether or not we are
2227      processing a return type until after the fact.  */
2228   while (declarator
2229          && (declarator->kind == cdk_pointer
2230              || declarator->kind == cdk_reference
2231              || declarator->kind == cdk_ptrmem))
2232     declarator = declarator->declarator;
2233   if (declarator
2234       && declarator->kind == cdk_function)
2235     {
2236       error ("%Hnew types may not be defined in a return type", &type_location);
2237       inform (type_location, 
2238               "(perhaps a semicolon is missing after the definition of %qT)",
2239               type);
2240     }
2241 }
2242
2243 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2244    "<" in any valid C++ program.  If the next token is indeed "<",
2245    issue a message warning the user about what appears to be an
2246    invalid attempt to form a template-id. LOCATION is the location
2247    of the type-specifier (TYPE) */
2248
2249 static void
2250 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2251                                          tree type, location_t location)
2252 {
2253   cp_token_position start = 0;
2254
2255   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2256     {
2257       if (TYPE_P (type))
2258         error ("%H%qT is not a template", &location, type);
2259       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2260         error ("%H%qE is not a template", &location, type);
2261       else
2262         error ("%Hinvalid template-id", &location);
2263       /* Remember the location of the invalid "<".  */
2264       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2265         start = cp_lexer_token_position (parser->lexer, true);
2266       /* Consume the "<".  */
2267       cp_lexer_consume_token (parser->lexer);
2268       /* Parse the template arguments.  */
2269       cp_parser_enclosed_template_argument_list (parser);
2270       /* Permanently remove the invalid template arguments so that
2271          this error message is not issued again.  */
2272       if (start)
2273         cp_lexer_purge_tokens_after (parser->lexer, start);
2274     }
2275 }
2276
2277 /* If parsing an integral constant-expression, issue an error message
2278    about the fact that THING appeared and return true.  Otherwise,
2279    return false.  In either case, set
2280    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2281
2282 static bool
2283 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2284                                             const char *thing)
2285 {
2286   parser->non_integral_constant_expression_p = true;
2287   if (parser->integral_constant_expression_p)
2288     {
2289       if (!parser->allow_non_integral_constant_expression_p)
2290         {
2291           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2292              in the message need to be interpreted.  */
2293           char *message = concat (thing,
2294                                   " cannot appear in a constant-expression",
2295                                   NULL);
2296           error (message);
2297           free (message);
2298           return true;
2299         }
2300     }
2301   return false;
2302 }
2303
2304 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2305    qualifying scope (or NULL, if none) for ID.  This function commits
2306    to the current active tentative parse, if any.  (Otherwise, the
2307    problematic construct might be encountered again later, resulting
2308    in duplicate error messages.) LOCATION is the location of ID.  */
2309
2310 static void
2311 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2312                                       tree scope, tree id,
2313                                       location_t location)
2314 {
2315   tree decl, old_scope;
2316   /* Try to lookup the identifier.  */
2317   old_scope = parser->scope;
2318   parser->scope = scope;
2319   decl = cp_parser_lookup_name_simple (parser, id, location);
2320   parser->scope = old_scope;
2321   /* If the lookup found a template-name, it means that the user forgot
2322   to specify an argument list. Emit a useful error message.  */
2323   if (TREE_CODE (decl) == TEMPLATE_DECL)
2324     error ("%Hinvalid use of template-name %qE without an argument list",
2325            &location, decl);
2326   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2327     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2328   else if (TREE_CODE (decl) == TYPE_DECL)
2329     /* Something like 'unsigned A a;'  */
2330     error ("%Hinvalid combination of multiple type-specifiers",
2331            &location);
2332   else if (!parser->scope)
2333     {
2334       /* Issue an error message.  */
2335       error ("%H%qE does not name a type", &location, id);
2336       /* If we're in a template class, it's possible that the user was
2337          referring to a type from a base class.  For example:
2338
2339            template <typename T> struct A { typedef T X; };
2340            template <typename T> struct B : public A<T> { X x; };
2341
2342          The user should have said "typename A<T>::X".  */
2343       if (processing_template_decl && current_class_type
2344           && TYPE_BINFO (current_class_type))
2345         {
2346           tree b;
2347
2348           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2349                b;
2350                b = TREE_CHAIN (b))
2351             {
2352               tree base_type = BINFO_TYPE (b);
2353               if (CLASS_TYPE_P (base_type)
2354                   && dependent_type_p (base_type))
2355                 {
2356                   tree field;
2357                   /* Go from a particular instantiation of the
2358                      template (which will have an empty TYPE_FIELDs),
2359                      to the main version.  */
2360                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2361                   for (field = TYPE_FIELDS (base_type);
2362                        field;
2363                        field = TREE_CHAIN (field))
2364                     if (TREE_CODE (field) == TYPE_DECL
2365                         && DECL_NAME (field) == id)
2366                       {
2367                         inform (location, 
2368                                 "(perhaps %<typename %T::%E%> was intended)",
2369                                 BINFO_TYPE (b), id);
2370                         break;
2371                       }
2372                   if (field)
2373                     break;
2374                 }
2375             }
2376         }
2377     }
2378   /* Here we diagnose qualified-ids where the scope is actually correct,
2379      but the identifier does not resolve to a valid type name.  */
2380   else if (parser->scope != error_mark_node)
2381     {
2382       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2383         error ("%H%qE in namespace %qE does not name a type",
2384                &location, id, parser->scope);
2385       else if (TYPE_P (parser->scope))
2386         error ("%H%qE in class %qT does not name a type",
2387                &location, id, parser->scope);
2388       else
2389         gcc_unreachable ();
2390     }
2391   cp_parser_commit_to_tentative_parse (parser);
2392 }
2393
2394 /* Check for a common situation where a type-name should be present,
2395    but is not, and issue a sensible error message.  Returns true if an
2396    invalid type-name was detected.
2397
2398    The situation handled by this function are variable declarations of the
2399    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2400    Usually, `ID' should name a type, but if we got here it means that it
2401    does not. We try to emit the best possible error message depending on
2402    how exactly the id-expression looks like.  */
2403
2404 static bool
2405 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2406 {
2407   tree id;
2408   cp_token *token = cp_lexer_peek_token (parser->lexer);
2409
2410   cp_parser_parse_tentatively (parser);
2411   id = cp_parser_id_expression (parser,
2412                                 /*template_keyword_p=*/false,
2413                                 /*check_dependency_p=*/true,
2414                                 /*template_p=*/NULL,
2415                                 /*declarator_p=*/true,
2416                                 /*optional_p=*/false);
2417   /* After the id-expression, there should be a plain identifier,
2418      otherwise this is not a simple variable declaration. Also, if
2419      the scope is dependent, we cannot do much.  */
2420   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2421       || (parser->scope && TYPE_P (parser->scope)
2422           && dependent_type_p (parser->scope))
2423       || TREE_CODE (id) == TYPE_DECL)
2424     {
2425       cp_parser_abort_tentative_parse (parser);
2426       return false;
2427     }
2428   if (!cp_parser_parse_definitely (parser))
2429     return false;
2430
2431   /* Emit a diagnostic for the invalid type.  */
2432   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2433                                         id, token->location);
2434   /* Skip to the end of the declaration; there's no point in
2435      trying to process it.  */
2436   cp_parser_skip_to_end_of_block_or_statement (parser);
2437   return true;
2438 }
2439
2440 /* Consume tokens up to, and including, the next non-nested closing `)'.
2441    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2442    are doing error recovery. Returns -1 if OR_COMMA is true and we
2443    found an unnested comma.  */
2444
2445 static int
2446 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2447                                        bool recovering,
2448                                        bool or_comma,
2449                                        bool consume_paren)
2450 {
2451   unsigned paren_depth = 0;
2452   unsigned brace_depth = 0;
2453
2454   if (recovering && !or_comma
2455       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2456     return 0;
2457
2458   while (true)
2459     {
2460       cp_token * token = cp_lexer_peek_token (parser->lexer);
2461
2462       switch (token->type)
2463         {
2464         case CPP_EOF:
2465         case CPP_PRAGMA_EOL:
2466           /* If we've run out of tokens, then there is no closing `)'.  */
2467           return 0;
2468
2469         case CPP_SEMICOLON:
2470           /* This matches the processing in skip_to_end_of_statement.  */
2471           if (!brace_depth)
2472             return 0;
2473           break;
2474
2475         case CPP_OPEN_BRACE:
2476           ++brace_depth;
2477           break;
2478         case CPP_CLOSE_BRACE:
2479           if (!brace_depth--)
2480             return 0;
2481           break;
2482
2483         case CPP_COMMA:
2484           if (recovering && or_comma && !brace_depth && !paren_depth)
2485             return -1;
2486           break;
2487
2488         case CPP_OPEN_PAREN:
2489           if (!brace_depth)
2490             ++paren_depth;
2491           break;
2492
2493         case CPP_CLOSE_PAREN:
2494           if (!brace_depth && !paren_depth--)
2495             {
2496               if (consume_paren)
2497                 cp_lexer_consume_token (parser->lexer);
2498               return 1;
2499             }
2500           break;
2501
2502         default:
2503           break;
2504         }
2505
2506       /* Consume the token.  */
2507       cp_lexer_consume_token (parser->lexer);
2508     }
2509 }
2510
2511 /* Consume tokens until we reach the end of the current statement.
2512    Normally, that will be just before consuming a `;'.  However, if a
2513    non-nested `}' comes first, then we stop before consuming that.  */
2514
2515 static void
2516 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2517 {
2518   unsigned nesting_depth = 0;
2519
2520   while (true)
2521     {
2522       cp_token *token = cp_lexer_peek_token (parser->lexer);
2523
2524       switch (token->type)
2525         {
2526         case CPP_EOF:
2527         case CPP_PRAGMA_EOL:
2528           /* If we've run out of tokens, stop.  */
2529           return;
2530
2531         case CPP_SEMICOLON:
2532           /* If the next token is a `;', we have reached the end of the
2533              statement.  */
2534           if (!nesting_depth)
2535             return;
2536           break;
2537
2538         case CPP_CLOSE_BRACE:
2539           /* If this is a non-nested '}', stop before consuming it.
2540              That way, when confronted with something like:
2541
2542                { 3 + }
2543
2544              we stop before consuming the closing '}', even though we
2545              have not yet reached a `;'.  */
2546           if (nesting_depth == 0)
2547             return;
2548
2549           /* If it is the closing '}' for a block that we have
2550              scanned, stop -- but only after consuming the token.
2551              That way given:
2552
2553                 void f g () { ... }
2554                 typedef int I;
2555
2556              we will stop after the body of the erroneously declared
2557              function, but before consuming the following `typedef'
2558              declaration.  */
2559           if (--nesting_depth == 0)
2560             {
2561               cp_lexer_consume_token (parser->lexer);
2562               return;
2563             }
2564
2565         case CPP_OPEN_BRACE:
2566           ++nesting_depth;
2567           break;
2568
2569         default:
2570           break;
2571         }
2572
2573       /* Consume the token.  */
2574       cp_lexer_consume_token (parser->lexer);
2575     }
2576 }
2577
2578 /* This function is called at the end of a statement or declaration.
2579    If the next token is a semicolon, it is consumed; otherwise, error
2580    recovery is attempted.  */
2581
2582 static void
2583 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2584 {
2585   /* Look for the trailing `;'.  */
2586   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2587     {
2588       /* If there is additional (erroneous) input, skip to the end of
2589          the statement.  */
2590       cp_parser_skip_to_end_of_statement (parser);
2591       /* If the next token is now a `;', consume it.  */
2592       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2593         cp_lexer_consume_token (parser->lexer);
2594     }
2595 }
2596
2597 /* Skip tokens until we have consumed an entire block, or until we
2598    have consumed a non-nested `;'.  */
2599
2600 static void
2601 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2602 {
2603   int nesting_depth = 0;
2604
2605   while (nesting_depth >= 0)
2606     {
2607       cp_token *token = cp_lexer_peek_token (parser->lexer);
2608
2609       switch (token->type)
2610         {
2611         case CPP_EOF:
2612         case CPP_PRAGMA_EOL:
2613           /* If we've run out of tokens, stop.  */
2614           return;
2615
2616         case CPP_SEMICOLON:
2617           /* Stop if this is an unnested ';'. */
2618           if (!nesting_depth)
2619             nesting_depth = -1;
2620           break;
2621
2622         case CPP_CLOSE_BRACE:
2623           /* Stop if this is an unnested '}', or closes the outermost
2624              nesting level.  */
2625           nesting_depth--;
2626           if (!nesting_depth)
2627             nesting_depth = -1;
2628           break;
2629
2630         case CPP_OPEN_BRACE:
2631           /* Nest. */
2632           nesting_depth++;
2633           break;
2634
2635         default:
2636           break;
2637         }
2638
2639       /* Consume the token.  */
2640       cp_lexer_consume_token (parser->lexer);
2641     }
2642 }
2643
2644 /* Skip tokens until a non-nested closing curly brace is the next
2645    token, or there are no more tokens. Return true in the first case,
2646    false otherwise.  */
2647
2648 static bool
2649 cp_parser_skip_to_closing_brace (cp_parser *parser)
2650 {
2651   unsigned nesting_depth = 0;
2652
2653   while (true)
2654     {
2655       cp_token *token = cp_lexer_peek_token (parser->lexer);
2656
2657       switch (token->type)
2658         {
2659         case CPP_EOF:
2660         case CPP_PRAGMA_EOL:
2661           /* If we've run out of tokens, stop.  */
2662           return false;
2663
2664         case CPP_CLOSE_BRACE:
2665           /* If the next token is a non-nested `}', then we have reached
2666              the end of the current block.  */
2667           if (nesting_depth-- == 0)
2668             return true;
2669           break;
2670
2671         case CPP_OPEN_BRACE:
2672           /* If it the next token is a `{', then we are entering a new
2673              block.  Consume the entire block.  */
2674           ++nesting_depth;
2675           break;
2676
2677         default:
2678           break;
2679         }
2680
2681       /* Consume the token.  */
2682       cp_lexer_consume_token (parser->lexer);
2683     }
2684 }
2685
2686 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2687    parameter is the PRAGMA token, allowing us to purge the entire pragma
2688    sequence.  */
2689
2690 static void
2691 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2692 {
2693   cp_token *token;
2694
2695   parser->lexer->in_pragma = false;
2696
2697   do
2698     token = cp_lexer_consume_token (parser->lexer);
2699   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2700
2701   /* Ensure that the pragma is not parsed again.  */
2702   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2703 }
2704
2705 /* Require pragma end of line, resyncing with it as necessary.  The
2706    arguments are as for cp_parser_skip_to_pragma_eol.  */
2707
2708 static void
2709 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2710 {
2711   parser->lexer->in_pragma = false;
2712   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2713     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2714 }
2715
2716 /* This is a simple wrapper around make_typename_type. When the id is
2717    an unresolved identifier node, we can provide a superior diagnostic
2718    using cp_parser_diagnose_invalid_type_name.  */
2719
2720 static tree
2721 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2722                               tree id, location_t id_location)
2723 {
2724   tree result;
2725   if (TREE_CODE (id) == IDENTIFIER_NODE)
2726     {
2727       result = make_typename_type (scope, id, typename_type,
2728                                    /*complain=*/tf_none);
2729       if (result == error_mark_node)
2730         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2731       return result;
2732     }
2733   return make_typename_type (scope, id, typename_type, tf_error);
2734 }
2735
2736 /* This is a wrapper around the
2737    make_{pointer,ptrmem,reference}_declarator functions that decides
2738    which one to call based on the CODE and CLASS_TYPE arguments. The
2739    CODE argument should be one of the values returned by
2740    cp_parser_ptr_operator. */
2741 static cp_declarator *
2742 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2743                                     cp_cv_quals cv_qualifiers,
2744                                     cp_declarator *target)
2745 {
2746   if (code == ERROR_MARK)
2747     return cp_error_declarator;
2748
2749   if (code == INDIRECT_REF)
2750     if (class_type == NULL_TREE)
2751       return make_pointer_declarator (cv_qualifiers, target);
2752     else
2753       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2754   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2755     return make_reference_declarator (cv_qualifiers, target, false);
2756   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2757     return make_reference_declarator (cv_qualifiers, target, true);
2758   gcc_unreachable ();
2759 }
2760
2761 /* Create a new C++ parser.  */
2762
2763 static cp_parser *
2764 cp_parser_new (void)
2765 {
2766   cp_parser *parser;
2767   cp_lexer *lexer;
2768   unsigned i;
2769
2770   /* cp_lexer_new_main is called before calling ggc_alloc because
2771      cp_lexer_new_main might load a PCH file.  */
2772   lexer = cp_lexer_new_main ();
2773
2774   /* Initialize the binops_by_token so that we can get the tree
2775      directly from the token.  */
2776   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2777     binops_by_token[binops[i].token_type] = binops[i];
2778
2779   parser = GGC_CNEW (cp_parser);
2780   parser->lexer = lexer;
2781   parser->context = cp_parser_context_new (NULL);
2782
2783   /* For now, we always accept GNU extensions.  */
2784   parser->allow_gnu_extensions_p = 1;
2785
2786   /* The `>' token is a greater-than operator, not the end of a
2787      template-id.  */
2788   parser->greater_than_is_operator_p = true;
2789
2790   parser->default_arg_ok_p = true;
2791
2792   /* We are not parsing a constant-expression.  */
2793   parser->integral_constant_expression_p = false;
2794   parser->allow_non_integral_constant_expression_p = false;
2795   parser->non_integral_constant_expression_p = false;
2796
2797   /* Local variable names are not forbidden.  */
2798   parser->local_variables_forbidden_p = false;
2799
2800   /* We are not processing an `extern "C"' declaration.  */
2801   parser->in_unbraced_linkage_specification_p = false;
2802
2803   /* We are not processing a declarator.  */
2804   parser->in_declarator_p = false;
2805
2806   /* We are not processing a template-argument-list.  */
2807   parser->in_template_argument_list_p = false;
2808
2809   /* We are not in an iteration statement.  */
2810   parser->in_statement = 0;
2811
2812   /* We are not in a switch statement.  */
2813   parser->in_switch_statement_p = false;
2814
2815   /* We are not parsing a type-id inside an expression.  */
2816   parser->in_type_id_in_expr_p = false;
2817
2818   /* Declarations aren't implicitly extern "C".  */
2819   parser->implicit_extern_c = false;
2820
2821   /* String literals should be translated to the execution character set.  */
2822   parser->translate_strings_p = true;
2823
2824   /* We are not parsing a function body.  */
2825   parser->in_function_body = false;
2826
2827   /* The unparsed function queue is empty.  */
2828   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2829
2830   /* There are no classes being defined.  */
2831   parser->num_classes_being_defined = 0;
2832
2833   /* No template parameters apply.  */
2834   parser->num_template_parameter_lists = 0;
2835
2836   return parser;
2837 }
2838
2839 /* Create a cp_lexer structure which will emit the tokens in CACHE
2840    and push it onto the parser's lexer stack.  This is used for delayed
2841    parsing of in-class method bodies and default arguments, and should
2842    not be confused with tentative parsing.  */
2843 static void
2844 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2845 {
2846   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2847   lexer->next = parser->lexer;
2848   parser->lexer = lexer;
2849
2850   /* Move the current source position to that of the first token in the
2851      new lexer.  */
2852   cp_lexer_set_source_position_from_token (lexer->next_token);
2853 }
2854
2855 /* Pop the top lexer off the parser stack.  This is never used for the
2856    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2857 static void
2858 cp_parser_pop_lexer (cp_parser *parser)
2859 {
2860   cp_lexer *lexer = parser->lexer;
2861   parser->lexer = lexer->next;
2862   cp_lexer_destroy (lexer);
2863
2864   /* Put the current source position back where it was before this
2865      lexer was pushed.  */
2866   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2867 }
2868
2869 /* Lexical conventions [gram.lex]  */
2870
2871 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2872    identifier.  */
2873
2874 static tree
2875 cp_parser_identifier (cp_parser* parser)
2876 {
2877   cp_token *token;
2878
2879   /* Look for the identifier.  */
2880   token = cp_parser_require (parser, CPP_NAME, "identifier");
2881   /* Return the value.  */
2882   return token ? token->u.value : error_mark_node;
2883 }
2884
2885 /* Parse a sequence of adjacent string constants.  Returns a
2886    TREE_STRING representing the combined, nul-terminated string
2887    constant.  If TRANSLATE is true, translate the string to the
2888    execution character set.  If WIDE_OK is true, a wide string is
2889    invalid here.
2890
2891    C++98 [lex.string] says that if a narrow string literal token is
2892    adjacent to a wide string literal token, the behavior is undefined.
2893    However, C99 6.4.5p4 says that this results in a wide string literal.
2894    We follow C99 here, for consistency with the C front end.
2895
2896    This code is largely lifted from lex_string() in c-lex.c.
2897
2898    FUTURE: ObjC++ will need to handle @-strings here.  */
2899 static tree
2900 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2901 {
2902   tree value;
2903   size_t count;
2904   struct obstack str_ob;
2905   cpp_string str, istr, *strs;
2906   cp_token *tok;
2907   enum cpp_ttype type;
2908
2909   tok = cp_lexer_peek_token (parser->lexer);
2910   if (!cp_parser_is_string_literal (tok))
2911     {
2912       cp_parser_error (parser, "expected string-literal");
2913       return error_mark_node;
2914     }
2915
2916   type = tok->type;
2917
2918   /* Try to avoid the overhead of creating and destroying an obstack
2919      for the common case of just one string.  */
2920   if (!cp_parser_is_string_literal
2921       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2922     {
2923       cp_lexer_consume_token (parser->lexer);
2924
2925       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2926       str.len = TREE_STRING_LENGTH (tok->u.value);
2927       count = 1;
2928
2929       strs = &str;
2930     }
2931   else
2932     {
2933       gcc_obstack_init (&str_ob);
2934       count = 0;
2935
2936       do
2937         {
2938           cp_lexer_consume_token (parser->lexer);
2939           count++;
2940           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2941           str.len = TREE_STRING_LENGTH (tok->u.value);
2942
2943           if (type != tok->type)
2944             {
2945               if (type == CPP_STRING)
2946                 type = tok->type;
2947               else if (tok->type != CPP_STRING)
2948                 error ("%Hunsupported non-standard concatenation "
2949                        "of string literals", &tok->location);
2950             }
2951
2952           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2953
2954           tok = cp_lexer_peek_token (parser->lexer);
2955         }
2956       while (cp_parser_is_string_literal (tok));
2957
2958       strs = (cpp_string *) obstack_finish (&str_ob);
2959     }
2960
2961   if (type != CPP_STRING && !wide_ok)
2962     {
2963       cp_parser_error (parser, "a wide string is invalid in this context");
2964       type = CPP_STRING;
2965     }
2966
2967   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2968       (parse_in, strs, count, &istr, type))
2969     {
2970       value = build_string (istr.len, (const char *)istr.text);
2971       free (CONST_CAST (unsigned char *, istr.text));
2972
2973       switch (type)
2974         {
2975         default:
2976         case CPP_STRING:
2977           TREE_TYPE (value) = char_array_type_node;
2978           break;
2979         case CPP_STRING16:
2980           TREE_TYPE (value) = char16_array_type_node;
2981           break;
2982         case CPP_STRING32:
2983           TREE_TYPE (value) = char32_array_type_node;
2984           break;
2985         case CPP_WSTRING:
2986           TREE_TYPE (value) = wchar_array_type_node;
2987           break;
2988         }
2989
2990       value = fix_string_type (value);
2991     }
2992   else
2993     /* cpp_interpret_string has issued an error.  */
2994     value = error_mark_node;
2995
2996   if (count > 1)
2997     obstack_free (&str_ob, 0);
2998
2999   return value;
3000 }
3001
3002
3003 /* Basic concepts [gram.basic]  */
3004
3005 /* Parse a translation-unit.
3006
3007    translation-unit:
3008      declaration-seq [opt]
3009
3010    Returns TRUE if all went well.  */
3011
3012 static bool
3013 cp_parser_translation_unit (cp_parser* parser)
3014 {
3015   /* The address of the first non-permanent object on the declarator
3016      obstack.  */
3017   static void *declarator_obstack_base;
3018
3019   bool success;
3020
3021   /* Create the declarator obstack, if necessary.  */
3022   if (!cp_error_declarator)
3023     {
3024       gcc_obstack_init (&declarator_obstack);
3025       /* Create the error declarator.  */
3026       cp_error_declarator = make_declarator (cdk_error);
3027       /* Create the empty parameter list.  */
3028       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3029       /* Remember where the base of the declarator obstack lies.  */
3030       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3031     }
3032
3033   cp_parser_declaration_seq_opt (parser);
3034
3035   /* If there are no tokens left then all went well.  */
3036   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3037     {
3038       /* Get rid of the token array; we don't need it any more.  */
3039       cp_lexer_destroy (parser->lexer);
3040       parser->lexer = NULL;
3041
3042       /* This file might have been a context that's implicitly extern
3043          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3044       if (parser->implicit_extern_c)
3045         {
3046           pop_lang_context ();
3047           parser->implicit_extern_c = false;
3048         }
3049
3050       /* Finish up.  */
3051       finish_translation_unit ();
3052
3053       success = true;
3054     }
3055   else
3056     {
3057       cp_parser_error (parser, "expected declaration");
3058       success = false;
3059     }
3060
3061   /* Make sure the declarator obstack was fully cleaned up.  */
3062   gcc_assert (obstack_next_free (&declarator_obstack)
3063               == declarator_obstack_base);
3064
3065   /* All went well.  */
3066   return success;
3067 }
3068
3069 /* Expressions [gram.expr] */
3070
3071 /* Parse a primary-expression.
3072
3073    primary-expression:
3074      literal
3075      this
3076      ( expression )
3077      id-expression
3078
3079    GNU Extensions:
3080
3081    primary-expression:
3082      ( compound-statement )
3083      __builtin_va_arg ( assignment-expression , type-id )
3084      __builtin_offsetof ( type-id , offsetof-expression )
3085
3086    C++ Extensions:
3087      __has_nothrow_assign ( type-id )   
3088      __has_nothrow_constructor ( type-id )
3089      __has_nothrow_copy ( type-id )
3090      __has_trivial_assign ( type-id )   
3091      __has_trivial_constructor ( type-id )
3092      __has_trivial_copy ( type-id )
3093      __has_trivial_destructor ( type-id )
3094      __has_virtual_destructor ( type-id )     
3095      __is_abstract ( type-id )
3096      __is_base_of ( type-id , type-id )
3097      __is_class ( type-id )
3098      __is_convertible_to ( type-id , type-id )     
3099      __is_empty ( type-id )
3100      __is_enum ( type-id )
3101      __is_pod ( type-id )
3102      __is_polymorphic ( type-id )
3103      __is_union ( type-id )
3104
3105    Objective-C++ Extension:
3106
3107    primary-expression:
3108      objc-expression
3109
3110    literal:
3111      __null
3112
3113    ADDRESS_P is true iff this expression was immediately preceded by
3114    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3115    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3116    true iff this expression is a template argument.
3117
3118    Returns a representation of the expression.  Upon return, *IDK
3119    indicates what kind of id-expression (if any) was present.  */
3120
3121 static tree
3122 cp_parser_primary_expression (cp_parser *parser,
3123                               bool address_p,
3124                               bool cast_p,
3125                               bool template_arg_p,
3126                               cp_id_kind *idk)
3127 {
3128   cp_token *token = NULL;
3129
3130   /* Assume the primary expression is not an id-expression.  */
3131   *idk = CP_ID_KIND_NONE;
3132
3133   /* Peek at the next token.  */
3134   token = cp_lexer_peek_token (parser->lexer);
3135   switch (token->type)
3136     {
3137       /* literal:
3138            integer-literal
3139            character-literal
3140            floating-literal
3141            string-literal
3142            boolean-literal  */
3143     case CPP_CHAR:
3144     case CPP_CHAR16:
3145     case CPP_CHAR32:
3146     case CPP_WCHAR:
3147     case CPP_NUMBER:
3148       token = cp_lexer_consume_token (parser->lexer);
3149       if (TREE_CODE (token->u.value) == FIXED_CST)
3150         {
3151           error ("%Hfixed-point types not supported in C++",
3152                  &token->location);
3153           return error_mark_node;
3154         }
3155       /* Floating-point literals are only allowed in an integral
3156          constant expression if they are cast to an integral or
3157          enumeration type.  */
3158       if (TREE_CODE (token->u.value) == REAL_CST
3159           && parser->integral_constant_expression_p
3160           && pedantic)
3161         {
3162           /* CAST_P will be set even in invalid code like "int(2.7 +
3163              ...)".   Therefore, we have to check that the next token
3164              is sure to end the cast.  */
3165           if (cast_p)
3166             {
3167               cp_token *next_token;
3168
3169               next_token = cp_lexer_peek_token (parser->lexer);
3170               if (/* The comma at the end of an
3171                      enumerator-definition.  */
3172                   next_token->type != CPP_COMMA
3173                   /* The curly brace at the end of an enum-specifier.  */
3174                   && next_token->type != CPP_CLOSE_BRACE
3175                   /* The end of a statement.  */
3176                   && next_token->type != CPP_SEMICOLON
3177                   /* The end of the cast-expression.  */
3178                   && next_token->type != CPP_CLOSE_PAREN
3179                   /* The end of an array bound.  */
3180                   && next_token->type != CPP_CLOSE_SQUARE
3181                   /* The closing ">" in a template-argument-list.  */
3182                   && (next_token->type != CPP_GREATER
3183                       || parser->greater_than_is_operator_p)
3184                   /* C++0x only: A ">>" treated like two ">" tokens,
3185                      in a template-argument-list.  */
3186                   && (next_token->type != CPP_RSHIFT
3187                       || (cxx_dialect == cxx98)
3188                       || parser->greater_than_is_operator_p))
3189                 cast_p = false;
3190             }
3191
3192           /* If we are within a cast, then the constraint that the
3193              cast is to an integral or enumeration type will be
3194              checked at that point.  If we are not within a cast, then
3195              this code is invalid.  */
3196           if (!cast_p)
3197             cp_parser_non_integral_constant_expression
3198               (parser, "floating-point literal");
3199         }
3200       return token->u.value;
3201
3202     case CPP_STRING:
3203     case CPP_STRING16:
3204     case CPP_STRING32:
3205     case CPP_WSTRING:
3206       /* ??? Should wide strings be allowed when parser->translate_strings_p
3207          is false (i.e. in attributes)?  If not, we can kill the third
3208          argument to cp_parser_string_literal.  */
3209       return cp_parser_string_literal (parser,
3210                                        parser->translate_strings_p,
3211                                        true);
3212
3213     case CPP_OPEN_PAREN:
3214       {
3215         tree expr;
3216         bool saved_greater_than_is_operator_p;
3217
3218         /* Consume the `('.  */
3219         cp_lexer_consume_token (parser->lexer);
3220         /* Within a parenthesized expression, a `>' token is always
3221            the greater-than operator.  */
3222         saved_greater_than_is_operator_p
3223           = parser->greater_than_is_operator_p;
3224         parser->greater_than_is_operator_p = true;
3225         /* If we see `( { ' then we are looking at the beginning of
3226            a GNU statement-expression.  */
3227         if (cp_parser_allow_gnu_extensions_p (parser)
3228             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3229           {
3230             /* Statement-expressions are not allowed by the standard.  */
3231             pedwarn (token->location, OPT_pedantic, 
3232                      "ISO C++ forbids braced-groups within expressions");
3233
3234             /* And they're not allowed outside of a function-body; you
3235                cannot, for example, write:
3236
3237                  int i = ({ int j = 3; j + 1; });
3238
3239                at class or namespace scope.  */
3240             if (!parser->in_function_body
3241                 || parser->in_template_argument_list_p)
3242               {
3243                 error ("%Hstatement-expressions are not allowed outside "
3244                        "functions nor in template-argument lists",
3245                        &token->location);
3246                 cp_parser_skip_to_end_of_block_or_statement (parser);
3247                 expr = error_mark_node;
3248               }
3249             else
3250               {
3251                 /* Start the statement-expression.  */
3252                 expr = begin_stmt_expr ();
3253                 /* Parse the compound-statement.  */
3254                 cp_parser_compound_statement (parser, expr, false);
3255                 /* Finish up.  */
3256                 expr = finish_stmt_expr (expr, false);
3257               }
3258           }
3259         else
3260           {
3261             /* Parse the parenthesized expression.  */
3262             expr = cp_parser_expression (parser, cast_p, idk);
3263             /* Let the front end know that this expression was
3264                enclosed in parentheses. This matters in case, for
3265                example, the expression is of the form `A::B', since
3266                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3267                not.  */
3268             finish_parenthesized_expr (expr);
3269           }
3270         /* The `>' token might be the end of a template-id or
3271            template-parameter-list now.  */
3272         parser->greater_than_is_operator_p
3273           = saved_greater_than_is_operator_p;
3274         /* Consume the `)'.  */
3275         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3276           cp_parser_skip_to_end_of_statement (parser);
3277
3278         return expr;
3279       }
3280
3281     case CPP_KEYWORD:
3282       switch (token->keyword)
3283         {
3284           /* These two are the boolean literals.  */
3285         case RID_TRUE:
3286           cp_lexer_consume_token (parser->lexer);
3287           return boolean_true_node;
3288         case RID_FALSE:
3289           cp_lexer_consume_token (parser->lexer);
3290           return boolean_false_node;
3291
3292           /* The `__null' literal.  */
3293         case RID_NULL:
3294           cp_lexer_consume_token (parser->lexer);
3295           return null_node;
3296
3297           /* Recognize the `this' keyword.  */
3298         case RID_THIS:
3299           cp_lexer_consume_token (parser->lexer);
3300           if (parser->local_variables_forbidden_p)
3301             {
3302               error ("%H%<this%> may not be used in this context",
3303                      &token->location);
3304               return error_mark_node;
3305             }
3306           /* Pointers cannot appear in constant-expressions.  */
3307           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3308             return error_mark_node;
3309           return finish_this_expr ();
3310
3311           /* The `operator' keyword can be the beginning of an
3312              id-expression.  */
3313         case RID_OPERATOR:
3314           goto id_expression;
3315
3316         case RID_FUNCTION_NAME:
3317         case RID_PRETTY_FUNCTION_NAME:
3318         case RID_C99_FUNCTION_NAME:
3319           {
3320             const char *name;
3321
3322             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3323                __func__ are the names of variables -- but they are
3324                treated specially.  Therefore, they are handled here,
3325                rather than relying on the generic id-expression logic
3326                below.  Grammatically, these names are id-expressions.
3327
3328                Consume the token.  */
3329             token = cp_lexer_consume_token (parser->lexer);
3330
3331             switch (token->keyword)
3332               {
3333               case RID_FUNCTION_NAME:
3334                 name = "%<__FUNCTION__%>";
3335                 break;
3336               case RID_PRETTY_FUNCTION_NAME:
3337                 name = "%<__PRETTY_FUNCTION__%>";
3338                 break;
3339               case RID_C99_FUNCTION_NAME:
3340                 name = "%<__func__%>";
3341                 break;
3342               default:
3343                 gcc_unreachable ();
3344               }
3345
3346             if (cp_parser_non_integral_constant_expression (parser, name))
3347               return error_mark_node;
3348
3349             /* Look up the name.  */
3350             return finish_fname (token->u.value);
3351           }
3352
3353         case RID_VA_ARG:
3354           {
3355             tree expression;
3356             tree type;
3357
3358             /* The `__builtin_va_arg' construct is used to handle
3359                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3360             cp_lexer_consume_token (parser->lexer);
3361             /* Look for the opening `('.  */
3362             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3363             /* Now, parse the assignment-expression.  */
3364             expression = cp_parser_assignment_expression (parser,
3365                                                           /*cast_p=*/false, NULL);
3366             /* Look for the `,'.  */
3367             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3368             /* Parse the type-id.  */
3369             type = cp_parser_type_id (parser);
3370             /* Look for the closing `)'.  */
3371             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3372             /* Using `va_arg' in a constant-expression is not
3373                allowed.  */
3374             if (cp_parser_non_integral_constant_expression (parser,
3375                                                             "%<va_arg%>"))
3376               return error_mark_node;
3377             return build_x_va_arg (expression, type);
3378           }
3379
3380         case RID_OFFSETOF:
3381           return cp_parser_builtin_offsetof (parser);
3382
3383         case RID_HAS_NOTHROW_ASSIGN:
3384         case RID_HAS_NOTHROW_CONSTRUCTOR:
3385         case RID_HAS_NOTHROW_COPY:        
3386         case RID_HAS_TRIVIAL_ASSIGN:
3387         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3388         case RID_HAS_TRIVIAL_COPY:        
3389         case RID_HAS_TRIVIAL_DESTRUCTOR:
3390         case RID_HAS_VIRTUAL_DESTRUCTOR:
3391         case RID_IS_ABSTRACT:
3392         case RID_IS_BASE_OF:
3393         case RID_IS_CLASS:
3394         case RID_IS_CONVERTIBLE_TO:
3395         case RID_IS_EMPTY:
3396         case RID_IS_ENUM:
3397         case RID_IS_POD:
3398         case RID_IS_POLYMORPHIC:
3399         case RID_IS_UNION:
3400           return cp_parser_trait_expr (parser, token->keyword);
3401
3402         /* Objective-C++ expressions.  */
3403         case RID_AT_ENCODE:
3404         case RID_AT_PROTOCOL:
3405         case RID_AT_SELECTOR:
3406           return cp_parser_objc_expression (parser);
3407
3408         default:
3409           cp_parser_error (parser, "expected primary-expression");
3410           return error_mark_node;
3411         }
3412
3413       /* An id-expression can start with either an identifier, a
3414          `::' as the beginning of a qualified-id, or the "operator"
3415          keyword.  */
3416     case CPP_NAME:
3417     case CPP_SCOPE:
3418     case CPP_TEMPLATE_ID:
3419     case CPP_NESTED_NAME_SPECIFIER:
3420       {
3421         tree id_expression;
3422         tree decl;
3423         const char *error_msg;
3424         bool template_p;
3425         bool done;
3426         cp_token *id_expr_token;
3427
3428       id_expression:
3429         /* Parse the id-expression.  */
3430         id_expression
3431           = cp_parser_id_expression (parser,
3432                                      /*template_keyword_p=*/false,
3433                                      /*check_dependency_p=*/true,
3434                                      &template_p,
3435                                      /*declarator_p=*/false,
3436                                      /*optional_p=*/false);
3437         if (id_expression == error_mark_node)
3438           return error_mark_node;
3439         id_expr_token = token;
3440         token = cp_lexer_peek_token (parser->lexer);
3441         done = (token->type != CPP_OPEN_SQUARE
3442                 && token->type != CPP_OPEN_PAREN
3443                 && token->type != CPP_DOT
3444                 && token->type != CPP_DEREF
3445                 && token->type != CPP_PLUS_PLUS
3446                 && token->type != CPP_MINUS_MINUS);
3447         /* If we have a template-id, then no further lookup is
3448            required.  If the template-id was for a template-class, we
3449            will sometimes have a TYPE_DECL at this point.  */
3450         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3451                  || TREE_CODE (id_expression) == TYPE_DECL)
3452           decl = id_expression;
3453         /* Look up the name.  */
3454         else
3455           {
3456             tree ambiguous_decls;
3457
3458             decl = cp_parser_lookup_name (parser, id_expression,
3459                                           none_type,
3460                                           template_p,
3461                                           /*is_namespace=*/false,
3462                                           /*check_dependency=*/true,
3463                                           &ambiguous_decls,
3464                                           id_expr_token->location);
3465             /* If the lookup was ambiguous, an error will already have
3466                been issued.  */
3467             if (ambiguous_decls)
3468               return error_mark_node;
3469
3470             /* In Objective-C++, an instance variable (ivar) may be preferred
3471                to whatever cp_parser_lookup_name() found.  */
3472             decl = objc_lookup_ivar (decl, id_expression);
3473
3474             /* If name lookup gives us a SCOPE_REF, then the
3475                qualifying scope was dependent.  */
3476             if (TREE_CODE (decl) == SCOPE_REF)
3477               {
3478                 /* At this point, we do not know if DECL is a valid
3479                    integral constant expression.  We assume that it is
3480                    in fact such an expression, so that code like:
3481
3482                       template <int N> struct A {
3483                         int a[B<N>::i];
3484                       };
3485                      
3486                    is accepted.  At template-instantiation time, we
3487                    will check that B<N>::i is actually a constant.  */
3488                 return decl;
3489               }
3490             /* Check to see if DECL is a local variable in a context
3491                where that is forbidden.  */
3492             if (parser->local_variables_forbidden_p
3493                 && local_variable_p (decl))
3494               {
3495                 /* It might be that we only found DECL because we are
3496                    trying to be generous with pre-ISO scoping rules.
3497                    For example, consider:
3498
3499                      int i;
3500                      void g() {
3501                        for (int i = 0; i < 10; ++i) {}
3502                        extern void f(int j = i);
3503                      }
3504
3505                    Here, name look up will originally find the out
3506                    of scope `i'.  We need to issue a warning message,
3507                    but then use the global `i'.  */
3508                 decl = check_for_out_of_scope_variable (decl);
3509                 if (local_variable_p (decl))
3510                   {
3511                     error ("%Hlocal variable %qD may not appear in this context",
3512                            &id_expr_token->location, decl);
3513                     return error_mark_node;
3514                   }
3515               }
3516           }
3517
3518         decl = (finish_id_expression
3519                 (id_expression, decl, parser->scope,
3520                  idk,
3521                  parser->integral_constant_expression_p,
3522                  parser->allow_non_integral_constant_expression_p,
3523                  &parser->non_integral_constant_expression_p,
3524                  template_p, done, address_p,
3525                  template_arg_p,
3526                  &error_msg,
3527                  id_expr_token->location));
3528         if (error_msg)
3529           cp_parser_error (parser, error_msg);
3530         return decl;
3531       }
3532
3533       /* Anything else is an error.  */
3534     default:
3535       /* ...unless we have an Objective-C++ message or string literal,
3536          that is.  */
3537       if (c_dialect_objc ()
3538           && (token->type == CPP_OPEN_SQUARE
3539               || token->type == CPP_OBJC_STRING))
3540         return cp_parser_objc_expression (parser);
3541
3542       cp_parser_error (parser, "expected primary-expression");
3543       return error_mark_node;
3544     }
3545 }
3546
3547 /* Parse an id-expression.
3548
3549    id-expression:
3550      unqualified-id
3551      qualified-id
3552
3553    qualified-id:
3554      :: [opt] nested-name-specifier template [opt] unqualified-id
3555      :: identifier
3556      :: operator-function-id
3557      :: template-id
3558
3559    Return a representation of the unqualified portion of the
3560    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3561    a `::' or nested-name-specifier.
3562
3563    Often, if the id-expression was a qualified-id, the caller will
3564    want to make a SCOPE_REF to represent the qualified-id.  This
3565    function does not do this in order to avoid wastefully creating
3566    SCOPE_REFs when they are not required.
3567
3568    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3569    `template' keyword.
3570
3571    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3572    uninstantiated templates.
3573
3574    If *TEMPLATE_P is non-NULL, it is set to true iff the
3575    `template' keyword is used to explicitly indicate that the entity
3576    named is a template.
3577
3578    If DECLARATOR_P is true, the id-expression is appearing as part of
3579    a declarator, rather than as part of an expression.  */
3580
3581 static tree
3582 cp_parser_id_expression (cp_parser *parser,
3583                          bool template_keyword_p,
3584                          bool check_dependency_p,
3585                          bool *template_p,
3586                          bool declarator_p,
3587                          bool optional_p)
3588 {
3589   bool global_scope_p;
3590   bool nested_name_specifier_p;
3591
3592   /* Assume the `template' keyword was not used.  */
3593   if (template_p)
3594     *template_p = template_keyword_p;
3595
3596   /* Look for the optional `::' operator.  */
3597   global_scope_p
3598     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3599        != NULL_TREE);
3600   /* Look for the optional nested-name-specifier.  */
3601   nested_name_specifier_p
3602     = (cp_parser_nested_name_specifier_opt (parser,
3603                                             /*typename_keyword_p=*/false,
3604                                             check_dependency_p,
3605                                             /*type_p=*/false,
3606                                             declarator_p)
3607        != NULL_TREE);
3608   /* If there is a nested-name-specifier, then we are looking at
3609      the first qualified-id production.  */
3610   if (nested_name_specifier_p)
3611     {
3612       tree saved_scope;
3613       tree saved_object_scope;
3614       tree saved_qualifying_scope;
3615       tree unqualified_id;
3616       bool is_template;
3617
3618       /* See if the next token is the `template' keyword.  */
3619       if (!template_p)
3620         template_p = &is_template;
3621       *template_p = cp_parser_optional_template_keyword (parser);
3622       /* Name lookup we do during the processing of the
3623          unqualified-id might obliterate SCOPE.  */
3624       saved_scope = parser->scope;
3625       saved_object_scope = parser->object_scope;
3626       saved_qualifying_scope = parser->qualifying_scope;
3627       /* Process the final unqualified-id.  */
3628       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3629                                                  check_dependency_p,
3630                                                  declarator_p,
3631                                                  /*optional_p=*/false);
3632       /* Restore the SAVED_SCOPE for our caller.  */
3633       parser->scope = saved_scope;
3634       parser->object_scope = saved_object_scope;
3635       parser->qualifying_scope = saved_qualifying_scope;
3636
3637       return unqualified_id;
3638     }
3639   /* Otherwise, if we are in global scope, then we are looking at one
3640      of the other qualified-id productions.  */
3641   else if (global_scope_p)
3642     {
3643       cp_token *token;
3644       tree id;
3645
3646       /* Peek at the next token.  */
3647       token = cp_lexer_peek_token (parser->lexer);
3648
3649       /* If it's an identifier, and the next token is not a "<", then
3650          we can avoid the template-id case.  This is an optimization
3651          for this common case.  */
3652       if (token->type == CPP_NAME
3653           && !cp_parser_nth_token_starts_template_argument_list_p
3654                (parser, 2))
3655         return cp_parser_identifier (parser);
3656
3657       cp_parser_parse_tentatively (parser);
3658       /* Try a template-id.  */
3659       id = cp_parser_template_id (parser,
3660                                   /*template_keyword_p=*/false,
3661                                   /*check_dependency_p=*/true,
3662                                   declarator_p);
3663       /* If that worked, we're done.  */
3664       if (cp_parser_parse_definitely (parser))
3665         return id;
3666
3667       /* Peek at the next token.  (Changes in the token buffer may
3668          have invalidated the pointer obtained above.)  */
3669       token = cp_lexer_peek_token (parser->lexer);
3670
3671       switch (token->type)
3672         {
3673         case CPP_NAME:
3674           return cp_parser_identifier (parser);
3675
3676         case CPP_KEYWORD:
3677           if (token->keyword == RID_OPERATOR)
3678             return cp_parser_operator_function_id (parser);
3679           /* Fall through.  */
3680
3681         default:
3682           cp_parser_error (parser, "expected id-expression");
3683           return error_mark_node;
3684         }
3685     }
3686   else
3687     return cp_parser_unqualified_id (parser, template_keyword_p,
3688                                      /*check_dependency_p=*/true,
3689                                      declarator_p,
3690                                      optional_p);
3691 }
3692
3693 /* Parse an unqualified-id.
3694
3695    unqualified-id:
3696      identifier
3697      operator-function-id
3698      conversion-function-id
3699      ~ class-name
3700      template-id
3701
3702    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3703    keyword, in a construct like `A::template ...'.
3704
3705    Returns a representation of unqualified-id.  For the `identifier'
3706    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3707    production a BIT_NOT_EXPR is returned; the operand of the
3708    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3709    other productions, see the documentation accompanying the
3710    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3711    names are looked up in uninstantiated templates.  If DECLARATOR_P
3712    is true, the unqualified-id is appearing as part of a declarator,
3713    rather than as part of an expression.  */
3714
3715 static tree
3716 cp_parser_unqualified_id (cp_parser* parser,
3717                           bool template_keyword_p,
3718                           bool check_dependency_p,
3719                           bool declarator_p,
3720                           bool optional_p)
3721 {
3722   cp_token *token;
3723
3724   /* Peek at the next token.  */
3725   token = cp_lexer_peek_token (parser->lexer);
3726
3727   switch (token->type)
3728     {
3729     case CPP_NAME:
3730       {
3731         tree id;
3732
3733         /* We don't know yet whether or not this will be a
3734            template-id.  */
3735         cp_parser_parse_tentatively (parser);
3736         /* Try a template-id.  */
3737         id = cp_parser_template_id (parser, template_keyword_p,
3738                                     check_dependency_p,
3739                                     declarator_p);
3740         /* If it worked, we're done.  */
3741         if (cp_parser_parse_definitely (parser))
3742           return id;
3743         /* Otherwise, it's an ordinary identifier.  */
3744         return cp_parser_identifier (parser);
3745       }
3746
3747     case CPP_TEMPLATE_ID:
3748       return cp_parser_template_id (parser, template_keyword_p,
3749                                     check_dependency_p,
3750                                     declarator_p);
3751
3752     case CPP_COMPL:
3753       {
3754         tree type_decl;
3755         tree qualifying_scope;
3756         tree object_scope;
3757         tree scope;
3758         bool done;
3759
3760         /* Consume the `~' token.  */
3761         cp_lexer_consume_token (parser->lexer);
3762         /* Parse the class-name.  The standard, as written, seems to
3763            say that:
3764
3765              template <typename T> struct S { ~S (); };
3766              template <typename T> S<T>::~S() {}
3767
3768            is invalid, since `~' must be followed by a class-name, but
3769            `S<T>' is dependent, and so not known to be a class.
3770            That's not right; we need to look in uninstantiated
3771            templates.  A further complication arises from:
3772
3773              template <typename T> void f(T t) {
3774                t.T::~T();
3775              }
3776
3777            Here, it is not possible to look up `T' in the scope of `T'
3778            itself.  We must look in both the current scope, and the
3779            scope of the containing complete expression.
3780
3781            Yet another issue is:
3782
3783              struct S {
3784                int S;
3785                ~S();
3786              };
3787
3788              S::~S() {}
3789
3790            The standard does not seem to say that the `S' in `~S'
3791            should refer to the type `S' and not the data member
3792            `S::S'.  */
3793
3794         /* DR 244 says that we look up the name after the "~" in the
3795            same scope as we looked up the qualifying name.  That idea
3796            isn't fully worked out; it's more complicated than that.  */
3797         scope = parser->scope;
3798         object_scope = parser->object_scope;
3799         qualifying_scope = parser->qualifying_scope;
3800
3801         /* Check for invalid scopes.  */
3802         if (scope == error_mark_node)
3803           {
3804             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3805               cp_lexer_consume_token (parser->lexer);
3806             return error_mark_node;
3807           }
3808         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3809           {
3810             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3811               error ("%Hscope %qT before %<~%> is not a class-name",
3812                      &token->location, scope);
3813             cp_parser_simulate_error (parser);
3814             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3815               cp_lexer_consume_token (parser->lexer);
3816             return error_mark_node;
3817           }
3818         gcc_assert (!scope || TYPE_P (scope));
3819
3820         /* If the name is of the form "X::~X" it's OK.  */
3821         token = cp_lexer_peek_token (parser->lexer);
3822         if (scope
3823             && token->type == CPP_NAME
3824             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3825                 == CPP_OPEN_PAREN)
3826             && constructor_name_p (token->u.value, scope))
3827           {
3828             cp_lexer_consume_token (parser->lexer);
3829             return build_nt (BIT_NOT_EXPR, scope);
3830           }
3831
3832         /* If there was an explicit qualification (S::~T), first look
3833            in the scope given by the qualification (i.e., S).  */
3834         done = false;
3835         type_decl = NULL_TREE;
3836         if (scope)
3837           {
3838             cp_parser_parse_tentatively (parser);
3839             type_decl = cp_parser_class_name (parser,
3840                                               /*typename_keyword_p=*/false,
3841                                               /*template_keyword_p=*/false,
3842                                               none_type,
3843                                               /*check_dependency=*/false,
3844                                               /*class_head_p=*/false,
3845                                               declarator_p);
3846             if (cp_parser_parse_definitely (parser))
3847               done = true;
3848           }
3849         /* In "N::S::~S", look in "N" as well.  */
3850         if (!done && scope && qualifying_scope)
3851           {
3852             cp_parser_parse_tentatively (parser);
3853             parser->scope = qualifying_scope;
3854             parser->object_scope = NULL_TREE;
3855             parser->qualifying_scope = NULL_TREE;
3856             type_decl
3857               = cp_parser_class_name (parser,
3858                                       /*typename_keyword_p=*/false,
3859                                       /*template_keyword_p=*/false,
3860                                       none_type,
3861                                       /*check_dependency=*/false,
3862                                       /*class_head_p=*/false,
3863                                       declarator_p);
3864             if (cp_parser_parse_definitely (parser))
3865               done = true;
3866           }
3867         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3868         else if (!done && object_scope)
3869           {
3870             cp_parser_parse_tentatively (parser);
3871             parser->scope = object_scope;
3872             parser->object_scope = NULL_TREE;
3873             parser->qualifying_scope = NULL_TREE;
3874             type_decl
3875               = cp_parser_class_name (parser,
3876                                       /*typename_keyword_p=*/false,
3877                                       /*template_keyword_p=*/false,
3878                                       none_type,
3879                                       /*check_dependency=*/false,
3880                                       /*class_head_p=*/false,
3881                                       declarator_p);
3882             if (cp_parser_parse_definitely (parser))
3883               done = true;
3884           }
3885         /* Look in the surrounding context.  */
3886         if (!done)
3887           {
3888             parser->scope = NULL_TREE;
3889             parser->object_scope = NULL_TREE;
3890             parser->qualifying_scope = NULL_TREE;
3891             if (processing_template_decl)
3892               cp_parser_parse_tentatively (parser);
3893             type_decl
3894               = cp_parser_class_name (parser,
3895                                       /*typename_keyword_p=*/false,
3896                                       /*template_keyword_p=*/false,
3897                                       none_type,
3898                                       /*check_dependency=*/false,
3899                                       /*class_head_p=*/false,
3900                                       declarator_p);
3901             if (processing_template_decl
3902                 && ! cp_parser_parse_definitely (parser))
3903               {
3904                 /* We couldn't find a type with this name, so just accept
3905                    it and check for a match at instantiation time.  */
3906                 type_decl = cp_parser_identifier (parser);
3907                 if (type_decl != error_mark_node)
3908                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3909                 return type_decl;
3910               }
3911           }
3912         /* If an error occurred, assume that the name of the
3913            destructor is the same as the name of the qualifying
3914            class.  That allows us to keep parsing after running
3915            into ill-formed destructor names.  */
3916         if (type_decl == error_mark_node && scope)
3917           return build_nt (BIT_NOT_EXPR, scope);
3918         else if (type_decl == error_mark_node)
3919           return error_mark_node;
3920
3921         /* Check that destructor name and scope match.  */
3922         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3923           {
3924             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3925               error ("%Hdeclaration of %<~%T%> as member of %qT",
3926                      &token->location, type_decl, scope);
3927             cp_parser_simulate_error (parser);
3928             return error_mark_node;
3929           }
3930
3931         /* [class.dtor]
3932
3933            A typedef-name that names a class shall not be used as the
3934            identifier in the declarator for a destructor declaration.  */
3935         if (declarator_p
3936             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3937             && !DECL_SELF_REFERENCE_P (type_decl)
3938             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3939           error ("%Htypedef-name %qD used as destructor declarator",
3940                  &token->location, type_decl);
3941
3942         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3943       }
3944
3945     case CPP_KEYWORD:
3946       if (token->keyword == RID_OPERATOR)
3947         {
3948           tree id;
3949
3950           /* This could be a template-id, so we try that first.  */
3951           cp_parser_parse_tentatively (parser);
3952           /* Try a template-id.  */
3953           id = cp_parser_template_id (parser, template_keyword_p,
3954                                       /*check_dependency_p=*/true,
3955                                       declarator_p);
3956           /* If that worked, we're done.  */
3957           if (cp_parser_parse_definitely (parser))
3958             return id;
3959           /* We still don't know whether we're looking at an
3960              operator-function-id or a conversion-function-id.  */
3961           cp_parser_parse_tentatively (parser);
3962           /* Try an operator-function-id.  */
3963           id = cp_parser_operator_function_id (parser);
3964           /* If that didn't work, try a conversion-function-id.  */
3965           if (!cp_parser_parse_definitely (parser))
3966             id = cp_parser_conversion_function_id (parser);
3967
3968           return id;
3969         }
3970       /* Fall through.  */
3971
3972     default:
3973       if (optional_p)
3974         return NULL_TREE;
3975       cp_parser_error (parser, "expected unqualified-id");
3976       return error_mark_node;
3977     }
3978 }
3979
3980 /* Parse an (optional) nested-name-specifier.
3981
3982    nested-name-specifier: [C++98]
3983      class-or-namespace-name :: nested-name-specifier [opt]
3984      class-or-namespace-name :: template nested-name-specifier [opt]
3985
3986    nested-name-specifier: [C++0x]
3987      type-name ::
3988      namespace-name ::
3989      nested-name-specifier identifier ::
3990      nested-name-specifier template [opt] simple-template-id ::
3991
3992    PARSER->SCOPE should be set appropriately before this function is
3993    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3994    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3995    in name lookups.
3996
3997    Sets PARSER->SCOPE to the class (TYPE) or namespace
3998    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3999    it unchanged if there is no nested-name-specifier.  Returns the new
4000    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4001
4002    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4003    part of a declaration and/or decl-specifier.  */
4004
4005 static tree
4006 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4007                                      bool typename_keyword_p,
4008                                      bool check_dependency_p,
4009                                      bool type_p,
4010                                      bool is_declaration)
4011 {
4012   bool success = false;
4013   cp_token_position start = 0;
4014   cp_token *token;
4015
4016   /* Remember where the nested-name-specifier starts.  */
4017   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4018     {
4019       start = cp_lexer_token_position (parser->lexer, false);
4020       push_deferring_access_checks (dk_deferred);
4021     }
4022
4023   while (true)
4024     {
4025       tree new_scope;
4026       tree old_scope;
4027       tree saved_qualifying_scope;
4028       bool template_keyword_p;
4029
4030       /* Spot cases that cannot be the beginning of a
4031          nested-name-specifier.  */
4032       token = cp_lexer_peek_token (parser->lexer);
4033
4034       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4035          the already parsed nested-name-specifier.  */
4036       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4037         {
4038           /* Grab the nested-name-specifier and continue the loop.  */
4039           cp_parser_pre_parsed_nested_name_specifier (parser);
4040           /* If we originally encountered this nested-name-specifier
4041              with IS_DECLARATION set to false, we will not have
4042              resolved TYPENAME_TYPEs, so we must do so here.  */
4043           if (is_declaration
4044               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4045             {
4046               new_scope = resolve_typename_type (parser->scope,
4047                                                  /*only_current_p=*/false);
4048               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4049                 parser->scope = new_scope;
4050             }
4051           success = true;
4052           continue;
4053         }
4054
4055       /* Spot cases that cannot be the beginning of a
4056          nested-name-specifier.  On the second and subsequent times
4057          through the loop, we look for the `template' keyword.  */
4058       if (success && token->keyword == RID_TEMPLATE)
4059         ;
4060       /* A template-id can start a nested-name-specifier.  */
4061       else if (token->type == CPP_TEMPLATE_ID)
4062         ;
4063       else
4064         {
4065           /* If the next token is not an identifier, then it is
4066              definitely not a type-name or namespace-name.  */
4067           if (token->type != CPP_NAME)
4068             break;
4069           /* If the following token is neither a `<' (to begin a
4070              template-id), nor a `::', then we are not looking at a
4071              nested-name-specifier.  */
4072           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4073           if (token->type != CPP_SCOPE
4074               && !cp_parser_nth_token_starts_template_argument_list_p
4075                   (parser, 2))
4076             break;
4077         }
4078
4079       /* The nested-name-specifier is optional, so we parse
4080          tentatively.  */
4081       cp_parser_parse_tentatively (parser);
4082
4083       /* Look for the optional `template' keyword, if this isn't the
4084          first time through the loop.  */
4085       if (success)
4086         template_keyword_p = cp_parser_optional_template_keyword (parser);
4087       else
4088         template_keyword_p = false;
4089
4090       /* Save the old scope since the name lookup we are about to do
4091          might destroy it.  */
4092       old_scope = parser->scope;
4093       saved_qualifying_scope = parser->qualifying_scope;
4094       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4095          look up names in "X<T>::I" in order to determine that "Y" is
4096          a template.  So, if we have a typename at this point, we make
4097          an effort to look through it.  */
4098       if (is_declaration
4099           && !typename_keyword_p
4100           && parser->scope
4101           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4102         parser->scope = resolve_typename_type (parser->scope,
4103                                                /*only_current_p=*/false);
4104       /* Parse the qualifying entity.  */
4105       new_scope
4106         = cp_parser_qualifying_entity (parser,
4107                                        typename_keyword_p,
4108                                        template_keyword_p,
4109                                        check_dependency_p,
4110                                        type_p,
4111                                        is_declaration);
4112       /* Look for the `::' token.  */
4113       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4114
4115       /* If we found what we wanted, we keep going; otherwise, we're
4116          done.  */
4117       if (!cp_parser_parse_definitely (parser))
4118         {
4119           bool error_p = false;
4120
4121           /* Restore the OLD_SCOPE since it was valid before the
4122              failed attempt at finding the last
4123              class-or-namespace-name.  */
4124           parser->scope = old_scope;
4125           parser->qualifying_scope = saved_qualifying_scope;
4126           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4127             break;
4128           /* If the next token is an identifier, and the one after
4129              that is a `::', then any valid interpretation would have
4130              found a class-or-namespace-name.  */
4131           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4132                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4133                      == CPP_SCOPE)
4134                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4135                      != CPP_COMPL))
4136             {
4137               token = cp_lexer_consume_token (parser->lexer);
4138               if (!error_p)
4139                 {
4140                   if (!token->ambiguous_p)
4141                     {
4142                       tree decl;
4143                       tree ambiguous_decls;
4144
4145                       decl = cp_parser_lookup_name (parser, token->u.value,
4146                                                     none_type,
4147                                                     /*is_template=*/false,
4148                                                     /*is_namespace=*/false,
4149                                                     /*check_dependency=*/true,
4150                                                     &ambiguous_decls,
4151                                                     token->location);
4152                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4153                         error ("%H%qD used without template parameters",
4154                                &token->location, decl);
4155                       else if (ambiguous_decls)
4156                         {
4157                           error ("%Hreference to %qD is ambiguous",
4158                                  &token->location, token->u.value);
4159                           print_candidates (ambiguous_decls);
4160                           decl = error_mark_node;
4161                         }
4162                       else
4163                         {
4164                           const char* msg = "is not a class or namespace";
4165                           if (cxx_dialect != cxx98)
4166                             msg = "is not a class, namespace, or enumeration";
4167                           cp_parser_name_lookup_error
4168                             (parser, token->u.value, decl, msg,
4169                              token->location);
4170                         }
4171                     }
4172                   parser->scope = error_mark_node;
4173                   error_p = true;
4174                   /* Treat this as a successful nested-name-specifier
4175                      due to:
4176
4177                      [basic.lookup.qual]
4178
4179                      If the name found is not a class-name (clause
4180                      _class_) or namespace-name (_namespace.def_), the
4181                      program is ill-formed.  */
4182                   success = true;
4183                 }
4184               cp_lexer_consume_token (parser->lexer);
4185             }
4186           break;
4187         }
4188       /* We've found one valid nested-name-specifier.  */
4189       success = true;
4190       /* Name lookup always gives us a DECL.  */
4191       if (TREE_CODE (new_scope) == TYPE_DECL)
4192         new_scope = TREE_TYPE (new_scope);
4193       /* Uses of "template" must be followed by actual templates.  */
4194       if (template_keyword_p
4195           && !(CLASS_TYPE_P (new_scope)
4196                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4197                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4198                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4199           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4200                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4201                    == TEMPLATE_ID_EXPR)))
4202         permerror (input_location, TYPE_P (new_scope)
4203                    ? "%qT is not a template"
4204                    : "%qD is not a template",
4205                    new_scope);
4206       /* If it is a class scope, try to complete it; we are about to
4207          be looking up names inside the class.  */
4208       if (TYPE_P (new_scope)
4209           /* Since checking types for dependency can be expensive,
4210              avoid doing it if the type is already complete.  */
4211           && !COMPLETE_TYPE_P (new_scope)
4212           /* Do not try to complete dependent types.  */
4213           && !dependent_type_p (new_scope))
4214         {
4215           new_scope = complete_type (new_scope);
4216           /* If it is a typedef to current class, use the current
4217              class instead, as the typedef won't have any names inside
4218              it yet.  */
4219           if (!COMPLETE_TYPE_P (new_scope)
4220               && currently_open_class (new_scope))
4221             new_scope = TYPE_MAIN_VARIANT (new_scope);
4222         }
4223       /* Make sure we look in the right scope the next time through
4224          the loop.  */
4225       parser->scope = new_scope;
4226     }
4227
4228   /* If parsing tentatively, replace the sequence of tokens that makes
4229      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4230      token.  That way, should we re-parse the token stream, we will
4231      not have to repeat the effort required to do the parse, nor will
4232      we issue duplicate error messages.  */
4233   if (success && start)
4234     {
4235       cp_token *token;
4236
4237       token = cp_lexer_token_at (parser->lexer, start);
4238       /* Reset the contents of the START token.  */
4239       token->type = CPP_NESTED_NAME_SPECIFIER;
4240       /* Retrieve any deferred checks.  Do not pop this access checks yet
4241          so the memory will not be reclaimed during token replacing below.  */
4242       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4243       token->u.tree_check_value->value = parser->scope;
4244       token->u.tree_check_value->checks = get_deferred_access_checks ();
4245       token->u.tree_check_value->qualifying_scope =
4246         parser->qualifying_scope;
4247       token->keyword = RID_MAX;
4248
4249       /* Purge all subsequent tokens.  */
4250       cp_lexer_purge_tokens_after (parser->lexer, start);
4251     }
4252
4253   if (start)
4254     pop_to_parent_deferring_access_checks ();
4255
4256   return success ? parser->scope : NULL_TREE;
4257 }
4258
4259 /* Parse a nested-name-specifier.  See
4260    cp_parser_nested_name_specifier_opt for details.  This function
4261    behaves identically, except that it will an issue an error if no
4262    nested-name-specifier is present.  */
4263
4264 static tree
4265 cp_parser_nested_name_specifier (cp_parser *parser,
4266                                  bool typename_keyword_p,
4267                                  bool check_dependency_p,
4268                                  bool type_p,
4269                                  bool is_declaration)
4270 {
4271   tree scope;
4272
4273   /* Look for the nested-name-specifier.  */
4274   scope = cp_parser_nested_name_specifier_opt (parser,
4275                                                typename_keyword_p,
4276                                                check_dependency_p,
4277                                                type_p,
4278                                                is_declaration);
4279   /* If it was not present, issue an error message.  */
4280   if (!scope)
4281     {
4282       cp_parser_error (parser, "expected nested-name-specifier");
4283       parser->scope = NULL_TREE;
4284     }
4285
4286   return scope;
4287 }
4288
4289 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4290    this is either a class-name or a namespace-name (which corresponds
4291    to the class-or-namespace-name production in the grammar). For
4292    C++0x, it can also be a type-name that refers to an enumeration
4293    type.
4294
4295    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4296    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4297    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4298    TYPE_P is TRUE iff the next name should be taken as a class-name,
4299    even the same name is declared to be another entity in the same
4300    scope.
4301
4302    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4303    specified by the class-or-namespace-name.  If neither is found the
4304    ERROR_MARK_NODE is returned.  */
4305
4306 static tree
4307 cp_parser_qualifying_entity (cp_parser *parser,
4308                              bool typename_keyword_p,
4309                              bool template_keyword_p,
4310                              bool check_dependency_p,
4311                              bool type_p,
4312                              bool is_declaration)
4313 {
4314   tree saved_scope;
4315   tree saved_qualifying_scope;
4316   tree saved_object_scope;
4317   tree scope;
4318   bool only_class_p;
4319   bool successful_parse_p;
4320
4321   /* Before we try to parse the class-name, we must save away the
4322      current PARSER->SCOPE since cp_parser_class_name will destroy
4323      it.  */
4324   saved_scope = parser->scope;
4325   saved_qualifying_scope = parser->qualifying_scope;
4326   saved_object_scope = parser->object_scope;
4327   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4328      there is no need to look for a namespace-name.  */
4329   only_class_p = template_keyword_p 
4330     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4331   if (!only_class_p)
4332     cp_parser_parse_tentatively (parser);
4333   scope = cp_parser_class_name (parser,
4334                                 typename_keyword_p,
4335                                 template_keyword_p,
4336                                 type_p ? class_type : none_type,
4337                                 check_dependency_p,
4338                                 /*class_head_p=*/false,
4339                                 is_declaration);
4340   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4341   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4342   if (!only_class_p 
4343       && cxx_dialect != cxx98
4344       && !successful_parse_p)
4345     {
4346       /* Restore the saved scope.  */
4347       parser->scope = saved_scope;
4348       parser->qualifying_scope = saved_qualifying_scope;
4349       parser->object_scope = saved_object_scope;
4350
4351       /* Parse tentatively.  */
4352       cp_parser_parse_tentatively (parser);
4353      
4354       /* Parse a typedef-name or enum-name.  */
4355       scope = cp_parser_nonclass_name (parser);
4356       successful_parse_p = cp_parser_parse_definitely (parser);
4357     }
4358   /* If that didn't work, try for a namespace-name.  */
4359   if (!only_class_p && !successful_parse_p)
4360     {
4361       /* Restore the saved scope.  */
4362       parser->scope = saved_scope;
4363       parser->qualifying_scope = saved_qualifying_scope;
4364       parser->object_scope = saved_object_scope;
4365       /* If we are not looking at an identifier followed by the scope
4366          resolution operator, then this is not part of a
4367          nested-name-specifier.  (Note that this function is only used
4368          to parse the components of a nested-name-specifier.)  */
4369       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4370           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4371         return error_mark_node;
4372       scope = cp_parser_namespace_name (parser);
4373     }
4374
4375   return scope;
4376 }
4377
4378 /* Parse a postfix-expression.
4379
4380    postfix-expression:
4381      primary-expression
4382      postfix-expression [ expression ]
4383      postfix-expression ( expression-list [opt] )
4384      simple-type-specifier ( expression-list [opt] )
4385      typename :: [opt] nested-name-specifier identifier
4386        ( expression-list [opt] )
4387      typename :: [opt] nested-name-specifier template [opt] template-id
4388        ( expression-list [opt] )
4389      postfix-expression . template [opt] id-expression
4390      postfix-expression -> template [opt] id-expression
4391      postfix-expression . pseudo-destructor-name
4392      postfix-expression -> pseudo-destructor-name
4393      postfix-expression ++
4394      postfix-expression --
4395      dynamic_cast < type-id > ( expression )
4396      static_cast < type-id > ( expression )
4397      reinterpret_cast < type-id > ( expression )
4398      const_cast < type-id > ( expression )
4399      typeid ( expression )
4400      typeid ( type-id )
4401
4402    GNU Extension:
4403
4404    postfix-expression:
4405      ( type-id ) { initializer-list , [opt] }
4406
4407    This extension is a GNU version of the C99 compound-literal
4408    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4409    but they are essentially the same concept.)
4410
4411    If ADDRESS_P is true, the postfix expression is the operand of the
4412    `&' operator.  CAST_P is true if this expression is the target of a
4413    cast.
4414
4415    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4416    class member access expressions [expr.ref].
4417
4418    Returns a representation of the expression.  */
4419
4420 static tree
4421 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4422                               bool member_access_only_p,
4423                               cp_id_kind * pidk_return)
4424 {
4425   cp_token *token;
4426   enum rid keyword;
4427   cp_id_kind idk = CP_ID_KIND_NONE;
4428   tree postfix_expression = NULL_TREE;
4429   bool is_member_access = false;
4430
4431   /* Peek at the next token.  */
4432   token = cp_lexer_peek_token (parser->lexer);
4433   /* Some of the productions are determined by keywords.  */
4434   keyword = token->keyword;
4435   switch (keyword)
4436     {
4437     case RID_DYNCAST:
4438     case RID_STATCAST:
4439     case RID_REINTCAST:
4440     case RID_CONSTCAST:
4441       {
4442         tree type;
4443         tree expression;
4444         const char *saved_message;
4445
4446         /* All of these can be handled in the same way from the point
4447            of view of parsing.  Begin by consuming the token
4448            identifying the cast.  */
4449         cp_lexer_consume_token (parser->lexer);
4450
4451         /* New types cannot be defined in the cast.  */
4452         saved_message = parser->type_definition_forbidden_message;
4453         parser->type_definition_forbidden_message
4454           = "types may not be defined in casts";
4455
4456         /* Look for the opening `<'.  */
4457         cp_parser_require (parser, CPP_LESS, "%<<%>");
4458         /* Parse the type to which we are casting.  */
4459         type = cp_parser_type_id (parser);
4460         /* Look for the closing `>'.  */
4461         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4462         /* Restore the old message.  */
4463         parser->type_definition_forbidden_message = saved_message;
4464
4465         /* And the expression which is being cast.  */
4466         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4467         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4468         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4469
4470         /* Only type conversions to integral or enumeration types
4471            can be used in constant-expressions.  */
4472         if (!cast_valid_in_integral_constant_expression_p (type)
4473             && (cp_parser_non_integral_constant_expression
4474                 (parser,
4475                  "a cast to a type other than an integral or "
4476                  "enumeration type")))
4477           return error_mark_node;
4478
4479         switch (keyword)
4480           {
4481           case RID_DYNCAST:
4482             postfix_expression
4483               = build_dynamic_cast (type, expression, tf_warning_or_error);
4484             break;
4485           case RID_STATCAST:
4486             postfix_expression
4487               = build_static_cast (type, expression, tf_warning_or_error);
4488             break;
4489           case RID_REINTCAST:
4490             postfix_expression
4491               = build_reinterpret_cast (type, expression, 
4492                                         tf_warning_or_error);
4493             break;
4494           case RID_CONSTCAST:
4495             postfix_expression
4496               = build_const_cast (type, expression, tf_warning_or_error);
4497             break;
4498           default:
4499             gcc_unreachable ();
4500           }
4501       }
4502       break;
4503
4504     case RID_TYPEID:
4505       {
4506         tree type;
4507         const char *saved_message;
4508         bool saved_in_type_id_in_expr_p;
4509
4510         /* Consume the `typeid' token.  */
4511         cp_lexer_consume_token (parser->lexer);
4512         /* Look for the `(' token.  */
4513         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4514         /* Types cannot be defined in a `typeid' expression.  */
4515         saved_message = parser->type_definition_forbidden_message;
4516         parser->type_definition_forbidden_message
4517           = "types may not be defined in a %<typeid%> expression";
4518         /* We can't be sure yet whether we're looking at a type-id or an
4519            expression.  */
4520         cp_parser_parse_tentatively (parser);
4521         /* Try a type-id first.  */
4522         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4523         parser->in_type_id_in_expr_p = true;
4524         type = cp_parser_type_id (parser);
4525         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4526         /* Look for the `)' token.  Otherwise, we can't be sure that
4527            we're not looking at an expression: consider `typeid (int
4528            (3))', for example.  */
4529         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4530         /* If all went well, simply lookup the type-id.  */
4531         if (cp_parser_parse_definitely (parser))
4532           postfix_expression = get_typeid (type);
4533         /* Otherwise, fall back to the expression variant.  */
4534         else
4535           {
4536             tree expression;
4537
4538             /* Look for an expression.  */
4539             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4540             /* Compute its typeid.  */
4541             postfix_expression = build_typeid (expression);
4542             /* Look for the `)' token.  */
4543             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4544           }
4545         /* Restore the saved message.  */
4546         parser->type_definition_forbidden_message = saved_message;
4547         /* `typeid' may not appear in an integral constant expression.  */
4548         if (cp_parser_non_integral_constant_expression(parser,
4549                                                        "%<typeid%> operator"))
4550           return error_mark_node;
4551       }
4552       break;
4553
4554     case RID_TYPENAME:
4555       {
4556         tree type;
4557         /* The syntax permitted here is the same permitted for an
4558            elaborated-type-specifier.  */
4559         type = cp_parser_elaborated_type_specifier (parser,
4560                                                     /*is_friend=*/false,
4561                                                     /*is_declaration=*/false);
4562         postfix_expression = cp_parser_functional_cast (parser, type);
4563       }
4564       break;
4565
4566     default:
4567       {
4568         tree type;
4569
4570         /* If the next thing is a simple-type-specifier, we may be
4571            looking at a functional cast.  We could also be looking at
4572            an id-expression.  So, we try the functional cast, and if
4573            that doesn't work we fall back to the primary-expression.  */
4574         cp_parser_parse_tentatively (parser);
4575         /* Look for the simple-type-specifier.  */
4576         type = cp_parser_simple_type_specifier (parser,
4577                                                 /*decl_specs=*/NULL,
4578                                                 CP_PARSER_FLAGS_NONE);
4579         /* Parse the cast itself.  */
4580         if (!cp_parser_error_occurred (parser))
4581           postfix_expression
4582             = cp_parser_functional_cast (parser, type);
4583         /* If that worked, we're done.  */
4584         if (cp_parser_parse_definitely (parser))
4585           break;
4586
4587         /* If the functional-cast didn't work out, try a
4588            compound-literal.  */
4589         if (cp_parser_allow_gnu_extensions_p (parser)
4590             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4591           {
4592             VEC(constructor_elt,gc) *initializer_list = NULL;
4593             bool saved_in_type_id_in_expr_p;
4594
4595             cp_parser_parse_tentatively (parser);
4596             /* Consume the `('.  */
4597             cp_lexer_consume_token (parser->lexer);
4598             /* Parse the type.  */
4599             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4600             parser->in_type_id_in_expr_p = true;
4601             type = cp_parser_type_id (parser);
4602             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4603             /* Look for the `)'.  */
4604             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4605             /* Look for the `{'.  */
4606             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4607             /* If things aren't going well, there's no need to
4608                keep going.  */
4609             if (!cp_parser_error_occurred (parser))
4610               {
4611                 bool non_constant_p;
4612                 /* Parse the initializer-list.  */
4613                 initializer_list
4614                   = cp_parser_initializer_list (parser, &non_constant_p);
4615                 /* Allow a trailing `,'.  */
4616                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4617                   cp_lexer_consume_token (parser->lexer);
4618                 /* Look for the final `}'.  */
4619                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4620               }
4621             /* If that worked, we're definitely looking at a
4622                compound-literal expression.  */
4623             if (cp_parser_parse_definitely (parser))
4624               {
4625                 /* Warn the user that a compound literal is not
4626                    allowed in standard C++.  */
4627                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4628                 /* For simplicity, we disallow compound literals in
4629                    constant-expressions.  We could
4630                    allow compound literals of integer type, whose
4631                    initializer was a constant, in constant
4632                    expressions.  Permitting that usage, as a further
4633                    extension, would not change the meaning of any
4634                    currently accepted programs.  (Of course, as
4635                    compound literals are not part of ISO C++, the
4636                    standard has nothing to say.)  */
4637                 if (cp_parser_non_integral_constant_expression 
4638                     (parser, "non-constant compound literals"))
4639                   {
4640                     postfix_expression = error_mark_node;
4641                     break;
4642                   }
4643                 /* Form the representation of the compound-literal.  */
4644                 postfix_expression
4645                   = (finish_compound_literal
4646                      (type, build_constructor (init_list_type_node,
4647                                                initializer_list)));
4648                 break;
4649               }
4650           }
4651
4652         /* It must be a primary-expression.  */
4653         postfix_expression
4654           = cp_parser_primary_expression (parser, address_p, cast_p,
4655                                           /*template_arg_p=*/false,
4656                                           &idk);
4657       }
4658       break;
4659     }
4660
4661   /* Keep looping until the postfix-expression is complete.  */
4662   while (true)
4663     {
4664       if (idk == CP_ID_KIND_UNQUALIFIED
4665           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4666           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4667         /* It is not a Koenig lookup function call.  */
4668         postfix_expression
4669           = unqualified_name_lookup_error (postfix_expression);
4670
4671       /* Peek at the next token.  */
4672       token = cp_lexer_peek_token (parser->lexer);
4673
4674       switch (token->type)
4675         {
4676         case CPP_OPEN_SQUARE:
4677           postfix_expression
4678             = cp_parser_postfix_open_square_expression (parser,
4679                                                         postfix_expression,
4680                                                         false);
4681           idk = CP_ID_KIND_NONE;
4682           is_member_access = false;
4683           break;
4684
4685         case CPP_OPEN_PAREN:
4686           /* postfix-expression ( expression-list [opt] ) */
4687           {
4688             bool koenig_p;
4689             bool is_builtin_constant_p;
4690             bool saved_integral_constant_expression_p = false;
4691             bool saved_non_integral_constant_expression_p = false;
4692             tree args;
4693
4694             is_member_access = false;
4695
4696             is_builtin_constant_p
4697               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4698             if (is_builtin_constant_p)
4699               {
4700                 /* The whole point of __builtin_constant_p is to allow
4701                    non-constant expressions to appear as arguments.  */
4702                 saved_integral_constant_expression_p
4703                   = parser->integral_constant_expression_p;
4704                 saved_non_integral_constant_expression_p
4705                   = parser->non_integral_constant_expression_p;
4706                 parser->integral_constant_expression_p = false;
4707               }
4708             args = (cp_parser_parenthesized_expression_list
4709                     (parser, /*is_attribute_list=*/false,
4710                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4711                      /*non_constant_p=*/NULL));
4712             if (is_builtin_constant_p)
4713               {
4714                 parser->integral_constant_expression_p
4715                   = saved_integral_constant_expression_p;
4716                 parser->non_integral_constant_expression_p
4717                   = saved_non_integral_constant_expression_p;
4718               }
4719
4720             if (args == error_mark_node)
4721               {
4722                 postfix_expression = error_mark_node;
4723                 break;
4724               }
4725
4726             /* Function calls are not permitted in
4727                constant-expressions.  */
4728             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4729                 && cp_parser_non_integral_constant_expression (parser,
4730                                                                "a function call"))
4731               {
4732                 postfix_expression = error_mark_node;
4733                 break;
4734               }
4735
4736             koenig_p = false;
4737             if (idk == CP_ID_KIND_UNQUALIFIED
4738                 || idk == CP_ID_KIND_TEMPLATE_ID)
4739               {
4740                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4741                   {
4742                     if (args)
4743                       {
4744                         koenig_p = true;
4745                         if (!any_type_dependent_arguments_p (args))
4746                           postfix_expression
4747                             = perform_koenig_lookup (postfix_expression, args);
4748                       }
4749                     else
4750                       postfix_expression
4751                         = unqualified_fn_lookup_error (postfix_expression);
4752                   }
4753                 /* We do not perform argument-dependent lookup if
4754                    normal lookup finds a non-function, in accordance
4755                    with the expected resolution of DR 218.  */
4756                 else if (args && is_overloaded_fn (postfix_expression))
4757                   {
4758                     tree fn = get_first_fn (postfix_expression);
4759
4760                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4761                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4762
4763                     /* Only do argument dependent lookup if regular
4764                        lookup does not find a set of member functions.
4765                        [basic.lookup.koenig]/2a  */
4766                     if (!DECL_FUNCTION_MEMBER_P (fn))
4767                       {
4768                         koenig_p = true;
4769                         if (!any_type_dependent_arguments_p (args))
4770                           postfix_expression
4771                             = perform_koenig_lookup (postfix_expression, args);
4772                       }
4773                   }
4774               }
4775
4776             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4777               {
4778                 tree instance = TREE_OPERAND (postfix_expression, 0);
4779                 tree fn = TREE_OPERAND (postfix_expression, 1);
4780
4781                 if (processing_template_decl
4782                     && (type_dependent_expression_p (instance)
4783                         || (!BASELINK_P (fn)
4784                             && TREE_CODE (fn) != FIELD_DECL)
4785                         || type_dependent_expression_p (fn)
4786                         || any_type_dependent_arguments_p (args)))
4787                   {
4788                     postfix_expression
4789                       = build_nt_call_list (postfix_expression, args);
4790                     break;
4791                   }
4792
4793                 if (BASELINK_P (fn))
4794                   {
4795                   postfix_expression
4796                     = (build_new_method_call
4797                        (instance, fn, args, NULL_TREE,
4798                         (idk == CP_ID_KIND_QUALIFIED
4799                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4800                         /*fn_p=*/NULL,
4801                         tf_warning_or_error));
4802                   }
4803                 else
4804                   postfix_expression
4805                     = finish_call_expr (postfix_expression, args,
4806                                         /*disallow_virtual=*/false,
4807                                         /*koenig_p=*/false,
4808                                         tf_warning_or_error);
4809               }
4810             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4811                      || TREE_CODE (postfix_expression) == MEMBER_REF
4812                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4813               postfix_expression = (build_offset_ref_call_from_tree
4814                                     (postfix_expression, args));
4815             else if (idk == CP_ID_KIND_QUALIFIED)
4816               /* A call to a static class member, or a namespace-scope
4817                  function.  */
4818               postfix_expression
4819                 = finish_call_expr (postfix_expression, args,
4820                                     /*disallow_virtual=*/true,
4821                                     koenig_p,
4822                                     tf_warning_or_error);
4823             else
4824               /* All other function calls.  */
4825               postfix_expression
4826                 = finish_call_expr (postfix_expression, args,
4827                                     /*disallow_virtual=*/false,
4828                                     koenig_p,
4829                                     tf_warning_or_error);
4830
4831             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4832             idk = CP_ID_KIND_NONE;
4833           }
4834           break;
4835
4836         case CPP_DOT:
4837         case CPP_DEREF:
4838           /* postfix-expression . template [opt] id-expression
4839              postfix-expression . pseudo-destructor-name
4840              postfix-expression -> template [opt] id-expression
4841              postfix-expression -> pseudo-destructor-name */
4842
4843           /* Consume the `.' or `->' operator.  */
4844           cp_lexer_consume_token (parser->lexer);
4845
4846           postfix_expression
4847             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4848                                                       postfix_expression,
4849                                                       false, &idk,
4850                                                       token->location);
4851
4852           is_member_access = true;
4853           break;
4854
4855         case CPP_PLUS_PLUS:
4856           /* postfix-expression ++  */
4857           /* Consume the `++' token.  */
4858           cp_lexer_consume_token (parser->lexer);
4859           /* Generate a representation for the complete expression.  */
4860           postfix_expression
4861             = finish_increment_expr (postfix_expression,
4862                                      POSTINCREMENT_EXPR);
4863           /* Increments may not appear in constant-expressions.  */
4864           if (cp_parser_non_integral_constant_expression (parser,
4865                                                           "an increment"))
4866             postfix_expression = error_mark_node;
4867           idk = CP_ID_KIND_NONE;
4868           is_member_access = false;
4869           break;
4870
4871         case CPP_MINUS_MINUS:
4872           /* postfix-expression -- */
4873           /* Consume the `--' token.  */
4874           cp_lexer_consume_token (parser->lexer);
4875           /* Generate a representation for the complete expression.  */
4876           postfix_expression
4877             = finish_increment_expr (postfix_expression,
4878                                      POSTDECREMENT_EXPR);
4879           /* Decrements may not appear in constant-expressions.  */
4880           if (cp_parser_non_integral_constant_expression (parser,
4881                                                           "a decrement"))
4882             postfix_expression = error_mark_node;
4883           idk = CP_ID_KIND_NONE;
4884           is_member_access = false;
4885           break;
4886
4887         default:
4888           if (pidk_return != NULL)
4889             * pidk_return = idk;
4890           if (member_access_only_p)
4891             return is_member_access? postfix_expression : error_mark_node;
4892           else
4893             return postfix_expression;
4894         }
4895     }
4896
4897   /* We should never get here.  */
4898   gcc_unreachable ();
4899   return error_mark_node;
4900 }
4901
4902 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4903    by cp_parser_builtin_offsetof.  We're looking for
4904
4905      postfix-expression [ expression ]
4906
4907    FOR_OFFSETOF is set if we're being called in that context, which
4908    changes how we deal with integer constant expressions.  */
4909
4910 static tree
4911 cp_parser_postfix_open_square_expression (cp_parser *parser,
4912                                           tree postfix_expression,
4913                                           bool for_offsetof)
4914 {
4915   tree index;
4916
4917   /* Consume the `[' token.  */
4918   cp_lexer_consume_token (parser->lexer);
4919
4920   /* Parse the index expression.  */
4921   /* ??? For offsetof, there is a question of what to allow here.  If
4922      offsetof is not being used in an integral constant expression context,
4923      then we *could* get the right answer by computing the value at runtime.
4924      If we are in an integral constant expression context, then we might
4925      could accept any constant expression; hard to say without analysis.
4926      Rather than open the barn door too wide right away, allow only integer
4927      constant expressions here.  */
4928   if (for_offsetof)
4929     index = cp_parser_constant_expression (parser, false, NULL);
4930   else
4931     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4932
4933   /* Look for the closing `]'.  */
4934   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4935
4936   /* Build the ARRAY_REF.  */
4937   postfix_expression = grok_array_decl (postfix_expression, index);
4938
4939   /* When not doing offsetof, array references are not permitted in
4940      constant-expressions.  */
4941   if (!for_offsetof
4942       && (cp_parser_non_integral_constant_expression
4943           (parser, "an array reference")))
4944     postfix_expression = error_mark_node;
4945
4946   return postfix_expression;
4947 }
4948
4949 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4950    by cp_parser_builtin_offsetof.  We're looking for
4951
4952      postfix-expression . template [opt] id-expression
4953      postfix-expression . pseudo-destructor-name
4954      postfix-expression -> template [opt] id-expression
4955      postfix-expression -> pseudo-destructor-name
4956
4957    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4958    limits what of the above we'll actually accept, but nevermind.
4959    TOKEN_TYPE is the "." or "->" token, which will already have been
4960    removed from the stream.  */
4961
4962 static tree
4963 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4964                                         enum cpp_ttype token_type,
4965                                         tree postfix_expression,
4966                                         bool for_offsetof, cp_id_kind *idk,
4967                                         location_t location)
4968 {
4969   tree name;
4970   bool dependent_p;
4971   bool pseudo_destructor_p;
4972   tree scope = NULL_TREE;
4973
4974   /* If this is a `->' operator, dereference the pointer.  */
4975   if (token_type == CPP_DEREF)
4976     postfix_expression = build_x_arrow (postfix_expression);
4977   /* Check to see whether or not the expression is type-dependent.  */
4978   dependent_p = type_dependent_expression_p (postfix_expression);
4979   /* The identifier following the `->' or `.' is not qualified.  */
4980   parser->scope = NULL_TREE;
4981   parser->qualifying_scope = NULL_TREE;
4982   parser->object_scope = NULL_TREE;
4983   *idk = CP_ID_KIND_NONE;
4984
4985   /* Enter the scope corresponding to the type of the object
4986      given by the POSTFIX_EXPRESSION.  */
4987   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4988     {
4989       scope = TREE_TYPE (postfix_expression);
4990       /* According to the standard, no expression should ever have
4991          reference type.  Unfortunately, we do not currently match
4992          the standard in this respect in that our internal representation
4993          of an expression may have reference type even when the standard
4994          says it does not.  Therefore, we have to manually obtain the
4995          underlying type here.  */
4996       scope = non_reference (scope);
4997       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4998       if (scope == unknown_type_node)
4999         {
5000           error ("%H%qE does not have class type", &location, postfix_expression);
5001           scope = NULL_TREE;
5002         }
5003       else
5004         scope = complete_type_or_else (scope, NULL_TREE);
5005       /* Let the name lookup machinery know that we are processing a
5006          class member access expression.  */
5007       parser->context->object_type = scope;
5008       /* If something went wrong, we want to be able to discern that case,
5009          as opposed to the case where there was no SCOPE due to the type
5010          of expression being dependent.  */
5011       if (!scope)
5012         scope = error_mark_node;
5013       /* If the SCOPE was erroneous, make the various semantic analysis
5014          functions exit quickly -- and without issuing additional error
5015          messages.  */
5016       if (scope == error_mark_node)
5017         postfix_expression = error_mark_node;
5018     }
5019
5020   /* Assume this expression is not a pseudo-destructor access.  */
5021   pseudo_destructor_p = false;
5022
5023   /* If the SCOPE is a scalar type, then, if this is a valid program,
5024      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5025      is type dependent, it can be pseudo-destructor-name or something else.
5026      Try to parse it as pseudo-destructor-name first.  */
5027   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5028     {
5029       tree s;
5030       tree type;
5031
5032       cp_parser_parse_tentatively (parser);
5033       /* Parse the pseudo-destructor-name.  */
5034       s = NULL_TREE;
5035       cp_parser_pseudo_destructor_name (parser, &s, &type);
5036       if (dependent_p
5037           && (cp_parser_error_occurred (parser)
5038               || TREE_CODE (type) != TYPE_DECL
5039               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5040         cp_parser_abort_tentative_parse (parser);
5041       else if (cp_parser_parse_definitely (parser))
5042         {
5043           pseudo_destructor_p = true;
5044           postfix_expression
5045             = finish_pseudo_destructor_expr (postfix_expression,
5046                                              s, TREE_TYPE (type));
5047         }
5048     }
5049
5050   if (!pseudo_destructor_p)
5051     {
5052       /* If the SCOPE is not a scalar type, we are looking at an
5053          ordinary class member access expression, rather than a
5054          pseudo-destructor-name.  */
5055       bool template_p;
5056       cp_token *token = cp_lexer_peek_token (parser->lexer);
5057       /* Parse the id-expression.  */
5058       name = (cp_parser_id_expression
5059               (parser,
5060                cp_parser_optional_template_keyword (parser),
5061                /*check_dependency_p=*/true,
5062                &template_p,
5063                /*declarator_p=*/false,
5064                /*optional_p=*/false));
5065       /* In general, build a SCOPE_REF if the member name is qualified.
5066          However, if the name was not dependent and has already been
5067          resolved; there is no need to build the SCOPE_REF.  For example;
5068
5069              struct X { void f(); };
5070              template <typename T> void f(T* t) { t->X::f(); }
5071
5072          Even though "t" is dependent, "X::f" is not and has been resolved
5073          to a BASELINK; there is no need to include scope information.  */
5074
5075       /* But we do need to remember that there was an explicit scope for
5076          virtual function calls.  */
5077       if (parser->scope)
5078         *idk = CP_ID_KIND_QUALIFIED;
5079
5080       /* If the name is a template-id that names a type, we will get a
5081          TYPE_DECL here.  That is invalid code.  */
5082       if (TREE_CODE (name) == TYPE_DECL)
5083         {
5084           error ("%Hinvalid use of %qD", &token->location, name);
5085           postfix_expression = error_mark_node;
5086         }
5087       else
5088         {
5089           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5090             {
5091               name = build_qualified_name (/*type=*/NULL_TREE,
5092                                            parser->scope,
5093                                            name,
5094                                            template_p);
5095               parser->scope = NULL_TREE;
5096               parser->qualifying_scope = NULL_TREE;
5097               parser->object_scope = NULL_TREE;
5098             }
5099           if (scope && name && BASELINK_P (name))
5100             adjust_result_of_qualified_name_lookup
5101               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5102           postfix_expression
5103             = finish_class_member_access_expr (postfix_expression, name,
5104                                                template_p, 
5105                                                tf_warning_or_error);
5106         }
5107     }
5108
5109   /* We no longer need to look up names in the scope of the object on
5110      the left-hand side of the `.' or `->' operator.  */
5111   parser->context->object_type = NULL_TREE;
5112
5113   /* Outside of offsetof, these operators may not appear in
5114      constant-expressions.  */
5115   if (!for_offsetof
5116       && (cp_parser_non_integral_constant_expression
5117           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5118     postfix_expression = error_mark_node;
5119
5120   return postfix_expression;
5121 }
5122
5123 /* Parse a parenthesized expression-list.
5124
5125    expression-list:
5126      assignment-expression
5127      expression-list, assignment-expression
5128
5129    attribute-list:
5130      expression-list
5131      identifier
5132      identifier, expression-list
5133
5134    CAST_P is true if this expression is the target of a cast.
5135
5136    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5137    argument pack.
5138
5139    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5140    representation of an assignment-expression.  Note that a TREE_LIST
5141    is returned even if there is only a single expression in the list.
5142    error_mark_node is returned if the ( and or ) are
5143    missing. NULL_TREE is returned on no expressions. The parentheses
5144    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5145    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5146    indicates whether or not all of the expressions in the list were
5147    constant.  */
5148
5149 static tree
5150 cp_parser_parenthesized_expression_list (cp_parser* parser,
5151                                          bool is_attribute_list,
5152                                          bool cast_p,
5153                                          bool allow_expansion_p,
5154                                          bool *non_constant_p)
5155 {
5156   tree expression_list = NULL_TREE;
5157   bool fold_expr_p = is_attribute_list;
5158   tree identifier = NULL_TREE;
5159   bool saved_greater_than_is_operator_p;
5160
5161   /* Assume all the expressions will be constant.  */
5162   if (non_constant_p)
5163     *non_constant_p = false;
5164
5165   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5166     return error_mark_node;
5167
5168   /* Within a parenthesized expression, a `>' token is always
5169      the greater-than operator.  */
5170   saved_greater_than_is_operator_p
5171     = parser->greater_than_is_operator_p;
5172   parser->greater_than_is_operator_p = true;
5173
5174   /* Consume expressions until there are no more.  */
5175   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5176     while (true)
5177       {
5178         tree expr;
5179
5180         /* At the beginning of attribute lists, check to see if the
5181            next token is an identifier.  */
5182         if (is_attribute_list
5183             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5184           {
5185             cp_token *token;
5186
5187             /* Consume the identifier.  */
5188             token = cp_lexer_consume_token (parser->lexer);
5189             /* Save the identifier.  */
5190             identifier = token->u.value;
5191           }
5192         else
5193           {
5194             bool expr_non_constant_p;
5195
5196             /* Parse the next assignment-expression.  */
5197             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5198               {
5199                 /* A braced-init-list.  */
5200                 maybe_warn_cpp0x ("extended initializer lists");
5201                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5202                 if (non_constant_p && expr_non_constant_p)
5203                   *non_constant_p = true;
5204               }
5205             else if (non_constant_p)
5206               {
5207                 expr = (cp_parser_constant_expression
5208                         (parser, /*allow_non_constant_p=*/true,
5209                          &expr_non_constant_p));
5210                 if (expr_non_constant_p)
5211                   *non_constant_p = true;
5212               }
5213             else
5214               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5215
5216             if (fold_expr_p)
5217               expr = fold_non_dependent_expr (expr);
5218
5219             /* If we have an ellipsis, then this is an expression
5220                expansion.  */
5221             if (allow_expansion_p
5222                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5223               {
5224                 /* Consume the `...'.  */
5225                 cp_lexer_consume_token (parser->lexer);
5226
5227                 /* Build the argument pack.  */
5228                 expr = make_pack_expansion (expr);
5229               }
5230
5231              /* Add it to the list.  We add error_mark_node
5232                 expressions to the list, so that we can still tell if
5233                 the correct form for a parenthesized expression-list
5234                 is found. That gives better errors.  */
5235             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5236
5237             if (expr == error_mark_node)
5238               goto skip_comma;
5239           }
5240
5241         /* After the first item, attribute lists look the same as
5242            expression lists.  */
5243         is_attribute_list = false;
5244
5245       get_comma:;
5246         /* If the next token isn't a `,', then we are done.  */
5247         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5248           break;
5249
5250         /* Otherwise, consume the `,' and keep going.  */
5251         cp_lexer_consume_token (parser->lexer);
5252       }
5253
5254   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5255     {
5256       int ending;
5257
5258     skip_comma:;
5259       /* We try and resync to an unnested comma, as that will give the
5260          user better diagnostics.  */
5261       ending = cp_parser_skip_to_closing_parenthesis (parser,
5262                                                       /*recovering=*/true,
5263                                                       /*or_comma=*/true,
5264                                                       /*consume_paren=*/true);
5265       if (ending < 0)
5266         goto get_comma;
5267       if (!ending)
5268         {
5269           parser->greater_than_is_operator_p
5270             = saved_greater_than_is_operator_p;
5271           return error_mark_node;
5272         }
5273     }
5274
5275   parser->greater_than_is_operator_p
5276     = saved_greater_than_is_operator_p;
5277
5278   /* We built up the list in reverse order so we must reverse it now.  */
5279   expression_list = nreverse (expression_list);
5280   if (identifier)
5281     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5282
5283   return expression_list;
5284 }
5285
5286 /* Parse a pseudo-destructor-name.
5287
5288    pseudo-destructor-name:
5289      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5290      :: [opt] nested-name-specifier template template-id :: ~ type-name
5291      :: [opt] nested-name-specifier [opt] ~ type-name
5292
5293    If either of the first two productions is used, sets *SCOPE to the
5294    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5295    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5296    or ERROR_MARK_NODE if the parse fails.  */
5297
5298 static void
5299 cp_parser_pseudo_destructor_name (cp_parser* parser,
5300                                   tree* scope,
5301                                   tree* type)
5302 {
5303   bool nested_name_specifier_p;
5304
5305   /* Assume that things will not work out.  */
5306   *type = error_mark_node;
5307
5308   /* Look for the optional `::' operator.  */
5309   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5310   /* Look for the optional nested-name-specifier.  */
5311   nested_name_specifier_p
5312     = (cp_parser_nested_name_specifier_opt (parser,
5313                                             /*typename_keyword_p=*/false,
5314                                             /*check_dependency_p=*/true,
5315                                             /*type_p=*/false,
5316                                             /*is_declaration=*/false)
5317        != NULL_TREE);
5318   /* Now, if we saw a nested-name-specifier, we might be doing the
5319      second production.  */
5320   if (nested_name_specifier_p
5321       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5322     {
5323       /* Consume the `template' keyword.  */
5324       cp_lexer_consume_token (parser->lexer);
5325       /* Parse the template-id.  */
5326       cp_parser_template_id (parser,
5327                              /*template_keyword_p=*/true,
5328                              /*check_dependency_p=*/false,
5329                              /*is_declaration=*/true);
5330       /* Look for the `::' token.  */
5331       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5332     }
5333   /* If the next token is not a `~', then there might be some
5334      additional qualification.  */
5335   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5336     {
5337       /* At this point, we're looking for "type-name :: ~".  The type-name
5338          must not be a class-name, since this is a pseudo-destructor.  So,
5339          it must be either an enum-name, or a typedef-name -- both of which
5340          are just identifiers.  So, we peek ahead to check that the "::"
5341          and "~" tokens are present; if they are not, then we can avoid
5342          calling type_name.  */
5343       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5344           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5345           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5346         {
5347           cp_parser_error (parser, "non-scalar type");
5348           return;
5349         }
5350
5351       /* Look for the type-name.  */
5352       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5353       if (*scope == error_mark_node)
5354         return;
5355
5356       /* Look for the `::' token.  */
5357       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5358     }
5359   else
5360     *scope = NULL_TREE;
5361
5362   /* Look for the `~'.  */
5363   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5364   /* Look for the type-name again.  We are not responsible for
5365      checking that it matches the first type-name.  */
5366   *type = cp_parser_nonclass_name (parser);
5367 }
5368
5369 /* Parse a unary-expression.
5370
5371    unary-expression:
5372      postfix-expression
5373      ++ cast-expression
5374      -- cast-expression
5375      unary-operator cast-expression
5376      sizeof unary-expression
5377      sizeof ( type-id )
5378      new-expression
5379      delete-expression
5380
5381    GNU Extensions:
5382
5383    unary-expression:
5384      __extension__ cast-expression
5385      __alignof__ unary-expression
5386      __alignof__ ( type-id )
5387      __real__ cast-expression
5388      __imag__ cast-expression
5389      && identifier
5390
5391    ADDRESS_P is true iff the unary-expression is appearing as the
5392    operand of the `&' operator.   CAST_P is true if this expression is
5393    the target of a cast.
5394
5395    Returns a representation of the expression.  */
5396
5397 static tree
5398 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5399                             cp_id_kind * pidk)
5400 {
5401   cp_token *token;
5402   enum tree_code unary_operator;
5403
5404   /* Peek at the next token.  */
5405   token = cp_lexer_peek_token (parser->lexer);
5406   /* Some keywords give away the kind of expression.  */
5407   if (token->type == CPP_KEYWORD)
5408     {
5409       enum rid keyword = token->keyword;
5410
5411       switch (keyword)
5412         {
5413         case RID_ALIGNOF:
5414         case RID_SIZEOF:
5415           {
5416             tree operand;
5417             enum tree_code op;
5418
5419             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5420             /* Consume the token.  */
5421             cp_lexer_consume_token (parser->lexer);
5422             /* Parse the operand.  */
5423             operand = cp_parser_sizeof_operand (parser, keyword);
5424
5425             if (TYPE_P (operand))
5426               return cxx_sizeof_or_alignof_type (operand, op, true);
5427             else
5428               return cxx_sizeof_or_alignof_expr (operand, op, true);
5429           }
5430
5431         case RID_NEW:
5432           return cp_parser_new_expression (parser);
5433
5434         case RID_DELETE:
5435           return cp_parser_delete_expression (parser);
5436
5437         case RID_EXTENSION:
5438           {
5439             /* The saved value of the PEDANTIC flag.  */
5440             int saved_pedantic;
5441             tree expr;
5442
5443             /* Save away the PEDANTIC flag.  */
5444             cp_parser_extension_opt (parser, &saved_pedantic);
5445             /* Parse the cast-expression.  */
5446             expr = cp_parser_simple_cast_expression (parser);
5447             /* Restore the PEDANTIC flag.  */
5448             pedantic = saved_pedantic;
5449
5450             return expr;
5451           }
5452
5453         case RID_REALPART:
5454         case RID_IMAGPART:
5455           {
5456             tree expression;
5457
5458             /* Consume the `__real__' or `__imag__' token.  */
5459             cp_lexer_consume_token (parser->lexer);
5460             /* Parse the cast-expression.  */
5461             expression = cp_parser_simple_cast_expression (parser);
5462             /* Create the complete representation.  */
5463             return build_x_unary_op ((keyword == RID_REALPART
5464                                       ? REALPART_EXPR : IMAGPART_EXPR),
5465                                      expression,
5466                                      tf_warning_or_error);
5467           }
5468           break;
5469
5470         default:
5471           break;
5472         }
5473     }
5474
5475   /* Look for the `:: new' and `:: delete', which also signal the
5476      beginning of a new-expression, or delete-expression,
5477      respectively.  If the next token is `::', then it might be one of
5478      these.  */
5479   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5480     {
5481       enum rid keyword;
5482
5483       /* See if the token after the `::' is one of the keywords in
5484          which we're interested.  */
5485       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5486       /* If it's `new', we have a new-expression.  */
5487       if (keyword == RID_NEW)
5488         return cp_parser_new_expression (parser);
5489       /* Similarly, for `delete'.  */
5490       else if (keyword == RID_DELETE)
5491         return cp_parser_delete_expression (parser);
5492     }
5493
5494   /* Look for a unary operator.  */
5495   unary_operator = cp_parser_unary_operator (token);
5496   /* The `++' and `--' operators can be handled similarly, even though
5497      they are not technically unary-operators in the grammar.  */
5498   if (unary_operator == ERROR_MARK)
5499     {
5500       if (token->type == CPP_PLUS_PLUS)
5501         unary_operator = PREINCREMENT_EXPR;
5502       else if (token->type == CPP_MINUS_MINUS)
5503         unary_operator = PREDECREMENT_EXPR;
5504       /* Handle the GNU address-of-label extension.  */
5505       else if (cp_parser_allow_gnu_extensions_p (parser)
5506                && token->type == CPP_AND_AND)
5507         {
5508           tree identifier;
5509           tree expression;
5510           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5511
5512           /* Consume the '&&' token.  */
5513           cp_lexer_consume_token (parser->lexer);
5514           /* Look for the identifier.  */
5515           identifier = cp_parser_identifier (parser);
5516           /* Create an expression representing the address.  */
5517           expression = finish_label_address_expr (identifier, loc);
5518           if (cp_parser_non_integral_constant_expression (parser,
5519                                                 "the address of a label"))
5520             expression = error_mark_node;
5521           return expression;
5522         }
5523     }
5524   if (unary_operator != ERROR_MARK)
5525     {
5526       tree cast_expression;
5527       tree expression = error_mark_node;
5528       const char *non_constant_p = NULL;
5529
5530       /* Consume the operator token.  */
5531       token = cp_lexer_consume_token (parser->lexer);
5532       /* Parse the cast-expression.  */
5533       cast_expression
5534         = cp_parser_cast_expression (parser,
5535                                      unary_operator == ADDR_EXPR,
5536                                      /*cast_p=*/false, pidk);
5537       /* Now, build an appropriate representation.  */
5538       switch (unary_operator)
5539         {
5540         case INDIRECT_REF:
5541           non_constant_p = "%<*%>";
5542           expression = build_x_indirect_ref (cast_expression, "unary *",
5543                                              tf_warning_or_error);
5544           break;
5545
5546         case ADDR_EXPR:
5547           non_constant_p = "%<&%>";
5548           /* Fall through.  */
5549         case BIT_NOT_EXPR:
5550           expression = build_x_unary_op (unary_operator, cast_expression,
5551                                          tf_warning_or_error);
5552           break;
5553
5554         case PREINCREMENT_EXPR:
5555         case PREDECREMENT_EXPR:
5556           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5557                             ? "%<++%>" : "%<--%>");
5558           /* Fall through.  */
5559         case UNARY_PLUS_EXPR:
5560         case NEGATE_EXPR:
5561         case TRUTH_NOT_EXPR:
5562           expression = finish_unary_op_expr (unary_operator, cast_expression);
5563           break;
5564
5565         default:
5566           gcc_unreachable ();
5567         }
5568
5569       if (non_constant_p
5570           && cp_parser_non_integral_constant_expression (parser,
5571                                                          non_constant_p))
5572         expression = error_mark_node;
5573
5574       return expression;
5575     }
5576
5577   return cp_parser_postfix_expression (parser, address_p, cast_p,
5578                                        /*member_access_only_p=*/false,
5579                                        pidk);
5580 }
5581
5582 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5583    unary-operator, the corresponding tree code is returned.  */
5584
5585 static enum tree_code
5586 cp_parser_unary_operator (cp_token* token)
5587 {
5588   switch (token->type)
5589     {
5590     case CPP_MULT:
5591       return INDIRECT_REF;
5592
5593     case CPP_AND:
5594       return ADDR_EXPR;
5595
5596     case CPP_PLUS:
5597       return UNARY_PLUS_EXPR;
5598
5599     case CPP_MINUS:
5600       return NEGATE_EXPR;
5601
5602     case CPP_NOT:
5603       return TRUTH_NOT_EXPR;
5604
5605     case CPP_COMPL:
5606       return BIT_NOT_EXPR;
5607
5608     default:
5609       return ERROR_MARK;
5610     }
5611 }
5612
5613 /* Parse a new-expression.
5614
5615    new-expression:
5616      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5617      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5618
5619    Returns a representation of the expression.  */
5620
5621 static tree
5622 cp_parser_new_expression (cp_parser* parser)
5623 {
5624   bool global_scope_p;
5625   tree placement;
5626   tree type;
5627   tree initializer;
5628   tree nelts;
5629
5630   /* Look for the optional `::' operator.  */
5631   global_scope_p
5632     = (cp_parser_global_scope_opt (parser,
5633                                    /*current_scope_valid_p=*/false)
5634        != NULL_TREE);
5635   /* Look for the `new' operator.  */
5636   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5637   /* There's no easy way to tell a new-placement from the
5638      `( type-id )' construct.  */
5639   cp_parser_parse_tentatively (parser);
5640   /* Look for a new-placement.  */
5641   placement = cp_parser_new_placement (parser);
5642   /* If that didn't work out, there's no new-placement.  */
5643   if (!cp_parser_parse_definitely (parser))
5644     placement = NULL_TREE;
5645
5646   /* If the next token is a `(', then we have a parenthesized
5647      type-id.  */
5648   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5649     {
5650       cp_token *token;
5651       /* Consume the `('.  */
5652       cp_lexer_consume_token (parser->lexer);
5653       /* Parse the type-id.  */
5654       type = cp_parser_type_id (parser);
5655       /* Look for the closing `)'.  */
5656       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5657       token = cp_lexer_peek_token (parser->lexer);
5658       /* There should not be a direct-new-declarator in this production,
5659          but GCC used to allowed this, so we check and emit a sensible error
5660          message for this case.  */
5661       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5662         {
5663           error ("%Harray bound forbidden after parenthesized type-id",
5664                  &token->location);
5665           inform (token->location, 
5666                   "try removing the parentheses around the type-id");
5667           cp_parser_direct_new_declarator (parser);
5668         }
5669       nelts = NULL_TREE;
5670     }
5671   /* Otherwise, there must be a new-type-id.  */
5672   else
5673     type = cp_parser_new_type_id (parser, &nelts);
5674
5675   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5676   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5677       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5678     initializer = cp_parser_new_initializer (parser);
5679   else
5680     initializer = NULL_TREE;
5681
5682   /* A new-expression may not appear in an integral constant
5683      expression.  */
5684   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5685     return error_mark_node;
5686
5687   /* Create a representation of the new-expression.  */
5688   return build_new (placement, type, nelts, initializer, global_scope_p,
5689                     tf_warning_or_error);
5690 }
5691
5692 /* Parse a new-placement.
5693
5694    new-placement:
5695      ( expression-list )
5696
5697    Returns the same representation as for an expression-list.  */
5698
5699 static tree
5700 cp_parser_new_placement (cp_parser* parser)
5701 {
5702   tree expression_list;
5703
5704   /* Parse the expression-list.  */
5705   expression_list = (cp_parser_parenthesized_expression_list
5706                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5707                       /*non_constant_p=*/NULL));
5708
5709   return expression_list;
5710 }
5711
5712 /* Parse a new-type-id.
5713
5714    new-type-id:
5715      type-specifier-seq new-declarator [opt]
5716
5717    Returns the TYPE allocated.  If the new-type-id indicates an array
5718    type, *NELTS is set to the number of elements in the last array
5719    bound; the TYPE will not include the last array bound.  */
5720
5721 static tree
5722 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5723 {
5724   cp_decl_specifier_seq type_specifier_seq;
5725   cp_declarator *new_declarator;
5726   cp_declarator *declarator;
5727   cp_declarator *outer_declarator;
5728   const char *saved_message;
5729   tree type;
5730
5731   /* The type-specifier sequence must not contain type definitions.
5732      (It cannot contain declarations of new types either, but if they
5733      are not definitions we will catch that because they are not
5734      complete.)  */
5735   saved_message = parser->type_definition_forbidden_message;
5736   parser->type_definition_forbidden_message
5737     = "types may not be defined in a new-type-id";
5738   /* Parse the type-specifier-seq.  */
5739   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5740                                 &type_specifier_seq);
5741   /* Restore the old message.  */
5742   parser->type_definition_forbidden_message = saved_message;
5743   /* Parse the new-declarator.  */
5744   new_declarator = cp_parser_new_declarator_opt (parser);
5745
5746   /* Determine the number of elements in the last array dimension, if
5747      any.  */
5748   *nelts = NULL_TREE;
5749   /* Skip down to the last array dimension.  */
5750   declarator = new_declarator;
5751   outer_declarator = NULL;
5752   while (declarator && (declarator->kind == cdk_pointer
5753                         || declarator->kind == cdk_ptrmem))
5754     {
5755       outer_declarator = declarator;
5756       declarator = declarator->declarator;
5757     }
5758   while (declarator
5759          && declarator->kind == cdk_array
5760          && declarator->declarator
5761          && declarator->declarator->kind == cdk_array)
5762     {
5763       outer_declarator = declarator;
5764       declarator = declarator->declarator;
5765     }
5766
5767   if (declarator && declarator->kind == cdk_array)
5768     {
5769       *nelts = declarator->u.array.bounds;
5770       if (*nelts == error_mark_node)
5771         *nelts = integer_one_node;
5772
5773       if (outer_declarator)
5774         outer_declarator->declarator = declarator->declarator;
5775       else
5776         new_declarator = NULL;
5777     }
5778
5779   type = groktypename (&type_specifier_seq, new_declarator, false);
5780   return type;
5781 }
5782
5783 /* Parse an (optional) new-declarator.
5784
5785    new-declarator:
5786      ptr-operator new-declarator [opt]
5787      direct-new-declarator
5788
5789    Returns the declarator.  */
5790
5791 static cp_declarator *
5792 cp_parser_new_declarator_opt (cp_parser* parser)
5793 {
5794   enum tree_code code;
5795   tree type;
5796   cp_cv_quals cv_quals;
5797
5798   /* We don't know if there's a ptr-operator next, or not.  */
5799   cp_parser_parse_tentatively (parser);
5800   /* Look for a ptr-operator.  */
5801   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5802   /* If that worked, look for more new-declarators.  */
5803   if (cp_parser_parse_definitely (parser))
5804     {
5805       cp_declarator *declarator;
5806
5807       /* Parse another optional declarator.  */
5808       declarator = cp_parser_new_declarator_opt (parser);
5809
5810       return cp_parser_make_indirect_declarator
5811         (code, type, cv_quals, declarator);
5812     }
5813
5814   /* If the next token is a `[', there is a direct-new-declarator.  */
5815   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5816     return cp_parser_direct_new_declarator (parser);
5817
5818   return NULL;
5819 }
5820
5821 /* Parse a direct-new-declarator.
5822
5823    direct-new-declarator:
5824      [ expression ]
5825      direct-new-declarator [constant-expression]
5826
5827    */
5828
5829 static cp_declarator *
5830 cp_parser_direct_new_declarator (cp_parser* parser)
5831 {
5832   cp_declarator *declarator = NULL;
5833
5834   while (true)
5835     {
5836       tree expression;
5837
5838       /* Look for the opening `['.  */
5839       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5840       /* The first expression is not required to be constant.  */
5841       if (!declarator)
5842         {
5843           cp_token *token = cp_lexer_peek_token (parser->lexer);
5844           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5845           /* The standard requires that the expression have integral
5846              type.  DR 74 adds enumeration types.  We believe that the
5847              real intent is that these expressions be handled like the
5848              expression in a `switch' condition, which also allows
5849              classes with a single conversion to integral or
5850              enumeration type.  */
5851           if (!processing_template_decl)
5852             {
5853               expression
5854                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5855                                               expression,
5856                                               /*complain=*/true);
5857               if (!expression)
5858                 {
5859                   error ("%Hexpression in new-declarator must have integral "
5860                          "or enumeration type", &token->location);
5861                   expression = error_mark_node;
5862                 }
5863             }
5864         }
5865       /* But all the other expressions must be.  */
5866       else
5867         expression
5868           = cp_parser_constant_expression (parser,
5869                                            /*allow_non_constant=*/false,
5870                                            NULL);
5871       /* Look for the closing `]'.  */
5872       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5873
5874       /* Add this bound to the declarator.  */
5875       declarator = make_array_declarator (declarator, expression);
5876
5877       /* If the next token is not a `[', then there are no more
5878          bounds.  */
5879       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5880         break;
5881     }
5882
5883   return declarator;
5884 }
5885
5886 /* Parse a new-initializer.
5887
5888    new-initializer:
5889      ( expression-list [opt] )
5890      braced-init-list
5891
5892    Returns a representation of the expression-list.  If there is no
5893    expression-list, VOID_ZERO_NODE is returned.  */
5894
5895 static tree
5896 cp_parser_new_initializer (cp_parser* parser)
5897 {
5898   tree expression_list;
5899
5900   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5901     {
5902       bool expr_non_constant_p;
5903       maybe_warn_cpp0x ("extended initializer lists");
5904       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5905       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5906       expression_list = build_tree_list (NULL_TREE, expression_list);
5907     }
5908   else
5909     expression_list = (cp_parser_parenthesized_expression_list
5910                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5911                         /*non_constant_p=*/NULL));
5912   if (!expression_list)
5913     expression_list = void_zero_node;
5914
5915   return expression_list;
5916 }
5917
5918 /* Parse a delete-expression.
5919
5920    delete-expression:
5921      :: [opt] delete cast-expression
5922      :: [opt] delete [ ] cast-expression
5923
5924    Returns a representation of the expression.  */
5925
5926 static tree
5927 cp_parser_delete_expression (cp_parser* parser)
5928 {
5929   bool global_scope_p;
5930   bool array_p;
5931   tree expression;
5932
5933   /* Look for the optional `::' operator.  */
5934   global_scope_p
5935     = (cp_parser_global_scope_opt (parser,
5936                                    /*current_scope_valid_p=*/false)
5937        != NULL_TREE);
5938   /* Look for the `delete' keyword.  */
5939   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5940   /* See if the array syntax is in use.  */
5941   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5942     {
5943       /* Consume the `[' token.  */
5944       cp_lexer_consume_token (parser->lexer);
5945       /* Look for the `]' token.  */
5946       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5947       /* Remember that this is the `[]' construct.  */
5948       array_p = true;
5949     }
5950   else
5951     array_p = false;
5952
5953   /* Parse the cast-expression.  */
5954   expression = cp_parser_simple_cast_expression (parser);
5955
5956   /* A delete-expression may not appear in an integral constant
5957      expression.  */
5958   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5959     return error_mark_node;
5960
5961   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5962 }
5963
5964 /* Returns true if TOKEN may start a cast-expression and false
5965    otherwise.  */
5966
5967 static bool
5968 cp_parser_token_starts_cast_expression (cp_token *token)
5969 {
5970   switch (token->type)
5971     {
5972     case CPP_COMMA:
5973     case CPP_SEMICOLON:
5974     case CPP_QUERY:
5975     case CPP_COLON:
5976     case CPP_CLOSE_SQUARE:
5977     case CPP_CLOSE_PAREN:
5978     case CPP_CLOSE_BRACE:
5979     case CPP_DOT:
5980     case CPP_DOT_STAR:
5981     case CPP_DEREF:
5982     case CPP_DEREF_STAR:
5983     case CPP_DIV:
5984     case CPP_MOD:
5985     case CPP_LSHIFT:
5986     case CPP_RSHIFT:
5987     case CPP_LESS:
5988     case CPP_GREATER:
5989     case CPP_LESS_EQ:
5990     case CPP_GREATER_EQ:
5991     case CPP_EQ_EQ:
5992     case CPP_NOT_EQ:
5993     case CPP_EQ:
5994     case CPP_MULT_EQ:
5995     case CPP_DIV_EQ:
5996     case CPP_MOD_EQ:
5997     case CPP_PLUS_EQ:
5998     case CPP_MINUS_EQ:
5999     case CPP_RSHIFT_EQ:
6000     case CPP_LSHIFT_EQ:
6001     case CPP_AND_EQ:
6002     case CPP_XOR_EQ:
6003     case CPP_OR_EQ:
6004     case CPP_XOR:
6005     case CPP_OR:
6006     case CPP_OR_OR:
6007     case CPP_EOF:
6008       return false;
6009
6010       /* '[' may start a primary-expression in obj-c++.  */
6011     case CPP_OPEN_SQUARE:
6012       return c_dialect_objc ();
6013
6014     default:
6015       return true;
6016     }
6017 }
6018
6019 /* Parse a cast-expression.
6020
6021    cast-expression:
6022      unary-expression
6023      ( type-id ) cast-expression
6024
6025    ADDRESS_P is true iff the unary-expression is appearing as the
6026    operand of the `&' operator.   CAST_P is true if this expression is
6027    the target of a cast.
6028
6029    Returns a representation of the expression.  */
6030
6031 static tree
6032 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6033                            cp_id_kind * pidk)
6034 {
6035   /* If it's a `(', then we might be looking at a cast.  */
6036   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6037     {
6038       tree type = NULL_TREE;
6039       tree expr = NULL_TREE;
6040       bool compound_literal_p;
6041       const char *saved_message;
6042
6043       /* There's no way to know yet whether or not this is a cast.
6044          For example, `(int (3))' is a unary-expression, while `(int)
6045          3' is a cast.  So, we resort to parsing tentatively.  */
6046       cp_parser_parse_tentatively (parser);
6047       /* Types may not be defined in a cast.  */
6048       saved_message = parser->type_definition_forbidden_message;
6049       parser->type_definition_forbidden_message
6050         = "types may not be defined in casts";
6051       /* Consume the `('.  */
6052       cp_lexer_consume_token (parser->lexer);
6053       /* A very tricky bit is that `(struct S) { 3 }' is a
6054          compound-literal (which we permit in C++ as an extension).
6055          But, that construct is not a cast-expression -- it is a
6056          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6057          is legal; if the compound-literal were a cast-expression,
6058          you'd need an extra set of parentheses.)  But, if we parse
6059          the type-id, and it happens to be a class-specifier, then we
6060          will commit to the parse at that point, because we cannot
6061          undo the action that is done when creating a new class.  So,
6062          then we cannot back up and do a postfix-expression.
6063
6064          Therefore, we scan ahead to the closing `)', and check to see
6065          if the token after the `)' is a `{'.  If so, we are not
6066          looking at a cast-expression.
6067
6068          Save tokens so that we can put them back.  */
6069       cp_lexer_save_tokens (parser->lexer);
6070       /* Skip tokens until the next token is a closing parenthesis.
6071          If we find the closing `)', and the next token is a `{', then
6072          we are looking at a compound-literal.  */
6073       compound_literal_p
6074         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6075                                                   /*consume_paren=*/true)
6076            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6077       /* Roll back the tokens we skipped.  */
6078       cp_lexer_rollback_tokens (parser->lexer);
6079       /* If we were looking at a compound-literal, simulate an error
6080          so that the call to cp_parser_parse_definitely below will
6081          fail.  */
6082       if (compound_literal_p)
6083         cp_parser_simulate_error (parser);
6084       else
6085         {
6086           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6087           parser->in_type_id_in_expr_p = true;
6088           /* Look for the type-id.  */
6089           type = cp_parser_type_id (parser);
6090           /* Look for the closing `)'.  */
6091           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6092           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6093         }
6094
6095       /* Restore the saved message.  */
6096       parser->type_definition_forbidden_message = saved_message;
6097
6098       /* At this point this can only be either a cast or a
6099          parenthesized ctor such as `(T ())' that looks like a cast to
6100          function returning T.  */
6101       if (!cp_parser_error_occurred (parser)
6102           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6103                                                      (parser->lexer)))
6104         {
6105           cp_parser_parse_definitely (parser);
6106           expr = cp_parser_cast_expression (parser,
6107                                             /*address_p=*/false,
6108                                             /*cast_p=*/true, pidk);
6109
6110           /* Warn about old-style casts, if so requested.  */
6111           if (warn_old_style_cast
6112               && !in_system_header
6113               && !VOID_TYPE_P (type)
6114               && current_lang_name != lang_name_c)
6115             warning (OPT_Wold_style_cast, "use of old-style cast");
6116
6117           /* Only type conversions to integral or enumeration types
6118              can be used in constant-expressions.  */
6119           if (!cast_valid_in_integral_constant_expression_p (type)
6120               && (cp_parser_non_integral_constant_expression
6121                   (parser,
6122                    "a cast to a type other than an integral or "
6123                    "enumeration type")))
6124             return error_mark_node;
6125
6126           /* Perform the cast.  */
6127           expr = build_c_cast (type, expr);
6128           return expr;
6129         }
6130       else 
6131         cp_parser_abort_tentative_parse (parser);
6132     }
6133
6134   /* If we get here, then it's not a cast, so it must be a
6135      unary-expression.  */
6136   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6137 }
6138
6139 /* Parse a binary expression of the general form:
6140
6141    pm-expression:
6142      cast-expression
6143      pm-expression .* cast-expression
6144      pm-expression ->* cast-expression
6145
6146    multiplicative-expression:
6147      pm-expression
6148      multiplicative-expression * pm-expression
6149      multiplicative-expression / pm-expression
6150      multiplicative-expression % pm-expression
6151
6152    additive-expression:
6153      multiplicative-expression
6154      additive-expression + multiplicative-expression
6155      additive-expression - multiplicative-expression
6156
6157    shift-expression:
6158      additive-expression
6159      shift-expression << additive-expression
6160      shift-expression >> additive-expression
6161
6162    relational-expression:
6163      shift-expression
6164      relational-expression < shift-expression
6165      relational-expression > shift-expression
6166      relational-expression <= shift-expression
6167      relational-expression >= shift-expression
6168
6169   GNU Extension:
6170
6171    relational-expression:
6172      relational-expression <? shift-expression
6173      relational-expression >? shift-expression
6174
6175    equality-expression:
6176      relational-expression
6177      equality-expression == relational-expression
6178      equality-expression != relational-expression
6179
6180    and-expression:
6181      equality-expression
6182      and-expression & equality-expression
6183
6184    exclusive-or-expression:
6185      and-expression
6186      exclusive-or-expression ^ and-expression
6187
6188    inclusive-or-expression:
6189      exclusive-or-expression
6190      inclusive-or-expression | exclusive-or-expression
6191
6192    logical-and-expression:
6193      inclusive-or-expression
6194      logical-and-expression && inclusive-or-expression
6195
6196    logical-or-expression:
6197      logical-and-expression
6198      logical-or-expression || logical-and-expression
6199
6200    All these are implemented with a single function like:
6201
6202    binary-expression:
6203      simple-cast-expression
6204      binary-expression <token> binary-expression
6205
6206    CAST_P is true if this expression is the target of a cast.
6207
6208    The binops_by_token map is used to get the tree codes for each <token> type.
6209    binary-expressions are associated according to a precedence table.  */
6210
6211 #define TOKEN_PRECEDENCE(token)                              \
6212 (((token->type == CPP_GREATER                                \
6213    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6214   && !parser->greater_than_is_operator_p)                    \
6215  ? PREC_NOT_OPERATOR                                         \
6216  : binops_by_token[token->type].prec)
6217
6218 static tree
6219 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6220                              bool no_toplevel_fold_p,
6221                              enum cp_parser_prec prec,
6222                              cp_id_kind * pidk)
6223 {
6224   cp_parser_expression_stack stack;
6225   cp_parser_expression_stack_entry *sp = &stack[0];
6226   tree lhs, rhs;
6227   cp_token *token;
6228   enum tree_code tree_type, lhs_type, rhs_type;
6229   enum cp_parser_prec new_prec, lookahead_prec;
6230   bool overloaded_p;
6231
6232   /* Parse the first expression.  */
6233   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6234   lhs_type = ERROR_MARK;
6235
6236   for (;;)
6237     {
6238       /* Get an operator token.  */
6239       token = cp_lexer_peek_token (parser->lexer);
6240
6241       if (warn_cxx0x_compat
6242           && token->type == CPP_RSHIFT
6243           && !parser->greater_than_is_operator_p)
6244         {
6245           warning (OPT_Wc__0x_compat, 
6246                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6247                    &token->location);
6248           warning (OPT_Wc__0x_compat, 
6249                    "suggest parentheses around %<>>%> expression");
6250         }
6251
6252       new_prec = TOKEN_PRECEDENCE (token);
6253
6254       /* Popping an entry off the stack means we completed a subexpression:
6255          - either we found a token which is not an operator (`>' where it is not
6256            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6257            will happen repeatedly;
6258          - or, we found an operator which has lower priority.  This is the case
6259            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6260            parsing `3 * 4'.  */
6261       if (new_prec <= prec)
6262         {
6263           if (sp == stack)
6264             break;
6265           else
6266             goto pop;
6267         }
6268
6269      get_rhs:
6270       tree_type = binops_by_token[token->type].tree_type;
6271
6272       /* We used the operator token.  */
6273       cp_lexer_consume_token (parser->lexer);
6274
6275       /* Extract another operand.  It may be the RHS of this expression
6276          or the LHS of a new, higher priority expression.  */
6277       rhs = cp_parser_simple_cast_expression (parser);
6278       rhs_type = ERROR_MARK;
6279
6280       /* Get another operator token.  Look up its precedence to avoid
6281          building a useless (immediately popped) stack entry for common
6282          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6283       token = cp_lexer_peek_token (parser->lexer);
6284       lookahead_prec = TOKEN_PRECEDENCE (token);
6285       if (lookahead_prec > new_prec)
6286         {
6287           /* ... and prepare to parse the RHS of the new, higher priority
6288              expression.  Since precedence levels on the stack are
6289              monotonically increasing, we do not have to care about
6290              stack overflows.  */
6291           sp->prec = prec;
6292           sp->tree_type = tree_type;
6293           sp->lhs = lhs;
6294           sp->lhs_type = lhs_type;
6295           sp++;
6296           lhs = rhs;
6297           lhs_type = rhs_type;
6298           prec = new_prec;
6299           new_prec = lookahead_prec;
6300           goto get_rhs;
6301
6302          pop:
6303           lookahead_prec = new_prec;
6304           /* If the stack is not empty, we have parsed into LHS the right side
6305              (`4' in the example above) of an expression we had suspended.
6306              We can use the information on the stack to recover the LHS (`3')
6307              from the stack together with the tree code (`MULT_EXPR'), and
6308              the precedence of the higher level subexpression
6309              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6310              which will be used to actually build the additive expression.  */
6311           --sp;
6312           prec = sp->prec;
6313           tree_type = sp->tree_type;
6314           rhs = lhs;
6315           rhs_type = lhs_type;
6316           lhs = sp->lhs;
6317           lhs_type = sp->lhs_type;
6318         }
6319
6320       overloaded_p = false;
6321       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6322          ERROR_MARK for everything that is not a binary expression.
6323          This makes warn_about_parentheses miss some warnings that
6324          involve unary operators.  For unary expressions we should
6325          pass the correct tree_code unless the unary expression was
6326          surrounded by parentheses.
6327       */
6328       if (no_toplevel_fold_p
6329           && lookahead_prec <= prec
6330           && sp == stack
6331           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6332         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6333       else
6334         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6335                                  &overloaded_p, tf_warning_or_error);
6336       lhs_type = tree_type;
6337
6338       /* If the binary operator required the use of an overloaded operator,
6339          then this expression cannot be an integral constant-expression.
6340          An overloaded operator can be used even if both operands are
6341          otherwise permissible in an integral constant-expression if at
6342          least one of the operands is of enumeration type.  */
6343
6344       if (overloaded_p
6345           && (cp_parser_non_integral_constant_expression
6346               (parser, "calls to overloaded operators")))
6347         return error_mark_node;
6348     }
6349
6350   return lhs;
6351 }
6352
6353
6354 /* Parse the `? expression : assignment-expression' part of a
6355    conditional-expression.  The LOGICAL_OR_EXPR is the
6356    logical-or-expression that started the conditional-expression.
6357    Returns a representation of the entire conditional-expression.
6358
6359    This routine is used by cp_parser_assignment_expression.
6360
6361      ? expression : assignment-expression
6362
6363    GNU Extensions:
6364
6365      ? : assignment-expression */
6366
6367 static tree
6368 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6369 {
6370   tree expr;
6371   tree assignment_expr;
6372
6373   /* Consume the `?' token.  */
6374   cp_lexer_consume_token (parser->lexer);
6375   if (cp_parser_allow_gnu_extensions_p (parser)
6376       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6377     /* Implicit true clause.  */
6378     expr = NULL_TREE;
6379   else
6380     /* Parse the expression.  */
6381     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6382
6383   /* The next token should be a `:'.  */
6384   cp_parser_require (parser, CPP_COLON, "%<:%>");
6385   /* Parse the assignment-expression.  */
6386   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6387
6388   /* Build the conditional-expression.  */
6389   return build_x_conditional_expr (logical_or_expr,
6390                                    expr,
6391                                    assignment_expr,
6392                                    tf_warning_or_error);
6393 }
6394
6395 /* Parse an assignment-expression.
6396
6397    assignment-expression:
6398      conditional-expression
6399      logical-or-expression assignment-operator assignment_expression
6400      throw-expression
6401
6402    CAST_P is true if this expression is the target of a cast.
6403
6404    Returns a representation for the expression.  */
6405
6406 static tree
6407 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6408                                  cp_id_kind * pidk)
6409 {
6410   tree expr;
6411
6412   /* If the next token is the `throw' keyword, then we're looking at
6413      a throw-expression.  */
6414   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6415     expr = cp_parser_throw_expression (parser);
6416   /* Otherwise, it must be that we are looking at a
6417      logical-or-expression.  */
6418   else
6419     {
6420       /* Parse the binary expressions (logical-or-expression).  */
6421       expr = cp_parser_binary_expression (parser, cast_p, false,
6422                                           PREC_NOT_OPERATOR, pidk);
6423       /* If the next token is a `?' then we're actually looking at a
6424          conditional-expression.  */
6425       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6426         return cp_parser_question_colon_clause (parser, expr);
6427       else
6428         {
6429           enum tree_code assignment_operator;
6430
6431           /* If it's an assignment-operator, we're using the second
6432              production.  */
6433           assignment_operator
6434             = cp_parser_assignment_operator_opt (parser);
6435           if (assignment_operator != ERROR_MARK)
6436             {
6437               bool non_constant_p;
6438
6439               /* Parse the right-hand side of the assignment.  */
6440               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6441
6442               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6443                 maybe_warn_cpp0x ("extended initializer lists");
6444
6445               /* An assignment may not appear in a
6446                  constant-expression.  */
6447               if (cp_parser_non_integral_constant_expression (parser,
6448                                                               "an assignment"))
6449                 return error_mark_node;
6450               /* Build the assignment expression.  */
6451               expr = build_x_modify_expr (expr,
6452                                           assignment_operator,
6453                                           rhs,
6454                                           tf_warning_or_error);
6455             }
6456         }
6457     }
6458
6459   return expr;
6460 }
6461
6462 /* Parse an (optional) assignment-operator.
6463
6464    assignment-operator: one of
6465      = *= /= %= += -= >>= <<= &= ^= |=
6466
6467    GNU Extension:
6468
6469    assignment-operator: one of
6470      <?= >?=
6471
6472    If the next token is an assignment operator, the corresponding tree
6473    code is returned, and the token is consumed.  For example, for
6474    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6475    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6476    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6477    operator, ERROR_MARK is returned.  */
6478
6479 static enum tree_code
6480 cp_parser_assignment_operator_opt (cp_parser* parser)
6481 {
6482   enum tree_code op;
6483   cp_token *token;
6484
6485   /* Peek at the next token.  */
6486   token = cp_lexer_peek_token (parser->lexer);
6487
6488   switch (token->type)
6489     {
6490     case CPP_EQ:
6491       op = NOP_EXPR;
6492       break;
6493
6494     case CPP_MULT_EQ:
6495       op = MULT_EXPR;
6496       break;
6497
6498     case CPP_DIV_EQ:
6499       op = TRUNC_DIV_EXPR;
6500       break;
6501
6502     case CPP_MOD_EQ:
6503       op = TRUNC_MOD_EXPR;
6504       break;
6505
6506     case CPP_PLUS_EQ:
6507       op = PLUS_EXPR;
6508       break;
6509
6510     case CPP_MINUS_EQ:
6511       op = MINUS_EXPR;
6512       break;
6513
6514     case CPP_RSHIFT_EQ:
6515       op = RSHIFT_EXPR;
6516       break;
6517
6518     case CPP_LSHIFT_EQ:
6519       op = LSHIFT_EXPR;
6520       break;
6521
6522     case CPP_AND_EQ:
6523       op = BIT_AND_EXPR;
6524       break;
6525
6526     case CPP_XOR_EQ:
6527       op = BIT_XOR_EXPR;
6528       break;
6529
6530     case CPP_OR_EQ:
6531       op = BIT_IOR_EXPR;
6532       break;
6533
6534     default:
6535       /* Nothing else is an assignment operator.  */
6536       op = ERROR_MARK;
6537     }
6538
6539   /* If it was an assignment operator, consume it.  */
6540   if (op != ERROR_MARK)
6541     cp_lexer_consume_token (parser->lexer);
6542
6543   return op;
6544 }
6545
6546 /* Parse an expression.
6547
6548    expression:
6549      assignment-expression
6550      expression , assignment-expression
6551
6552    CAST_P is true if this expression is the target of a cast.
6553
6554    Returns a representation of the expression.  */
6555
6556 static tree
6557 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6558 {
6559   tree expression = NULL_TREE;
6560
6561   while (true)
6562     {
6563       tree assignment_expression;
6564
6565       /* Parse the next assignment-expression.  */
6566       assignment_expression
6567         = cp_parser_assignment_expression (parser, cast_p, pidk);
6568       /* If this is the first assignment-expression, we can just
6569          save it away.  */
6570       if (!expression)
6571         expression = assignment_expression;
6572       else
6573         expression = build_x_compound_expr (expression,
6574                                             assignment_expression,
6575                                             tf_warning_or_error);
6576       /* If the next token is not a comma, then we are done with the
6577          expression.  */
6578       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6579         break;
6580       /* Consume the `,'.  */
6581       cp_lexer_consume_token (parser->lexer);
6582       /* A comma operator cannot appear in a constant-expression.  */
6583       if (cp_parser_non_integral_constant_expression (parser,
6584                                                       "a comma operator"))
6585         expression = error_mark_node;
6586     }
6587
6588   return expression;
6589 }
6590
6591 /* Parse a constant-expression.
6592
6593    constant-expression:
6594      conditional-expression
6595
6596   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6597   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6598   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6599   is false, NON_CONSTANT_P should be NULL.  */
6600
6601 static tree
6602 cp_parser_constant_expression (cp_parser* parser,
6603                                bool allow_non_constant_p,
6604                                bool *non_constant_p)
6605 {
6606   bool saved_integral_constant_expression_p;
6607   bool saved_allow_non_integral_constant_expression_p;
6608   bool saved_non_integral_constant_expression_p;
6609   tree expression;
6610
6611   /* It might seem that we could simply parse the
6612      conditional-expression, and then check to see if it were
6613      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6614      one that the compiler can figure out is constant, possibly after
6615      doing some simplifications or optimizations.  The standard has a
6616      precise definition of constant-expression, and we must honor
6617      that, even though it is somewhat more restrictive.
6618
6619      For example:
6620
6621        int i[(2, 3)];
6622
6623      is not a legal declaration, because `(2, 3)' is not a
6624      constant-expression.  The `,' operator is forbidden in a
6625      constant-expression.  However, GCC's constant-folding machinery
6626      will fold this operation to an INTEGER_CST for `3'.  */
6627
6628   /* Save the old settings.  */
6629   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6630   saved_allow_non_integral_constant_expression_p
6631     = parser->allow_non_integral_constant_expression_p;
6632   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6633   /* We are now parsing a constant-expression.  */
6634   parser->integral_constant_expression_p = true;
6635   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6636   parser->non_integral_constant_expression_p = false;
6637   /* Although the grammar says "conditional-expression", we parse an
6638      "assignment-expression", which also permits "throw-expression"
6639      and the use of assignment operators.  In the case that
6640      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6641      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6642      actually essential that we look for an assignment-expression.
6643      For example, cp_parser_initializer_clauses uses this function to
6644      determine whether a particular assignment-expression is in fact
6645      constant.  */
6646   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6647   /* Restore the old settings.  */
6648   parser->integral_constant_expression_p
6649     = saved_integral_constant_expression_p;
6650   parser->allow_non_integral_constant_expression_p
6651     = saved_allow_non_integral_constant_expression_p;
6652   if (allow_non_constant_p)
6653     *non_constant_p = parser->non_integral_constant_expression_p;
6654   else if (parser->non_integral_constant_expression_p)
6655     expression = error_mark_node;
6656   parser->non_integral_constant_expression_p
6657     = saved_non_integral_constant_expression_p;
6658
6659   return expression;
6660 }
6661
6662 /* Parse __builtin_offsetof.
6663
6664    offsetof-expression:
6665      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6666
6667    offsetof-member-designator:
6668      id-expression
6669      | offsetof-member-designator "." id-expression
6670      | offsetof-member-designator "[" expression "]"
6671      | offsetof-member-designator "->" id-expression  */
6672
6673 static tree
6674 cp_parser_builtin_offsetof (cp_parser *parser)
6675 {
6676   int save_ice_p, save_non_ice_p;
6677   tree type, expr;
6678   cp_id_kind dummy;
6679   cp_token *token;
6680
6681   /* We're about to accept non-integral-constant things, but will
6682      definitely yield an integral constant expression.  Save and
6683      restore these values around our local parsing.  */
6684   save_ice_p = parser->integral_constant_expression_p;
6685   save_non_ice_p = parser->non_integral_constant_expression_p;
6686
6687   /* Consume the "__builtin_offsetof" token.  */
6688   cp_lexer_consume_token (parser->lexer);
6689   /* Consume the opening `('.  */
6690   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6691   /* Parse the type-id.  */
6692   type = cp_parser_type_id (parser);
6693   /* Look for the `,'.  */
6694   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6695   token = cp_lexer_peek_token (parser->lexer);
6696
6697   /* Build the (type *)null that begins the traditional offsetof macro.  */
6698   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6699                             tf_warning_or_error);
6700
6701   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6702   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6703                                                  true, &dummy, token->location);
6704   while (true)
6705     {
6706       token = cp_lexer_peek_token (parser->lexer);
6707       switch (token->type)
6708         {
6709         case CPP_OPEN_SQUARE:
6710           /* offsetof-member-designator "[" expression "]" */
6711           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6712           break;
6713
6714         case CPP_DEREF:
6715           /* offsetof-member-designator "->" identifier */
6716           expr = grok_array_decl (expr, integer_zero_node);
6717           /* FALLTHRU */
6718
6719         case CPP_DOT:
6720           /* offsetof-member-designator "." identifier */
6721           cp_lexer_consume_token (parser->lexer);
6722           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6723                                                          expr, true, &dummy,
6724                                                          token->location);
6725           break;
6726
6727         case CPP_CLOSE_PAREN:
6728           /* Consume the ")" token.  */
6729           cp_lexer_consume_token (parser->lexer);
6730           goto success;
6731
6732         default:
6733           /* Error.  We know the following require will fail, but
6734              that gives the proper error message.  */
6735           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6736           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6737           expr = error_mark_node;
6738           goto failure;
6739         }
6740     }
6741
6742  success:
6743   /* If we're processing a template, we can't finish the semantics yet.
6744      Otherwise we can fold the entire expression now.  */
6745   if (processing_template_decl)
6746     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6747   else
6748     expr = finish_offsetof (expr);
6749
6750  failure:
6751   parser->integral_constant_expression_p = save_ice_p;
6752   parser->non_integral_constant_expression_p = save_non_ice_p;
6753
6754   return expr;
6755 }
6756
6757 /* Parse a trait expression.  */
6758
6759 static tree
6760 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6761 {
6762   cp_trait_kind kind;
6763   tree type1, type2 = NULL_TREE;
6764   bool binary = false;
6765   cp_decl_specifier_seq decl_specs;
6766
6767   switch (keyword)
6768     {
6769     case RID_HAS_NOTHROW_ASSIGN:
6770       kind = CPTK_HAS_NOTHROW_ASSIGN;
6771       break;
6772     case RID_HAS_NOTHROW_CONSTRUCTOR:
6773       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6774       break;
6775     case RID_HAS_NOTHROW_COPY:
6776       kind = CPTK_HAS_NOTHROW_COPY;
6777       break;
6778     case RID_HAS_TRIVIAL_ASSIGN:
6779       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6780       break;
6781     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6782       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6783       break;
6784     case RID_HAS_TRIVIAL_COPY:
6785       kind = CPTK_HAS_TRIVIAL_COPY;
6786       break;
6787     case RID_HAS_TRIVIAL_DESTRUCTOR:
6788       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6789       break;
6790     case RID_HAS_VIRTUAL_DESTRUCTOR:
6791       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6792       break;
6793     case RID_IS_ABSTRACT:
6794       kind = CPTK_IS_ABSTRACT;
6795       break;
6796     case RID_IS_BASE_OF:
6797       kind = CPTK_IS_BASE_OF;
6798       binary = true;
6799       break;
6800     case RID_IS_CLASS:
6801       kind = CPTK_IS_CLASS;
6802       break;
6803     case RID_IS_CONVERTIBLE_TO:
6804       kind = CPTK_IS_CONVERTIBLE_TO;
6805       binary = true;
6806       break;
6807     case RID_IS_EMPTY:
6808       kind = CPTK_IS_EMPTY;
6809       break;
6810     case RID_IS_ENUM:
6811       kind = CPTK_IS_ENUM;
6812       break;
6813     case RID_IS_POD:
6814       kind = CPTK_IS_POD;
6815       break;
6816     case RID_IS_POLYMORPHIC:
6817       kind = CPTK_IS_POLYMORPHIC;
6818       break;
6819     case RID_IS_UNION:
6820       kind = CPTK_IS_UNION;
6821       break;
6822     default:
6823       gcc_unreachable ();
6824     }
6825
6826   /* Consume the token.  */
6827   cp_lexer_consume_token (parser->lexer);
6828
6829   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6830
6831   type1 = cp_parser_type_id (parser);
6832
6833   if (type1 == error_mark_node)
6834     return error_mark_node;
6835
6836   /* Build a trivial decl-specifier-seq.  */
6837   clear_decl_specs (&decl_specs);
6838   decl_specs.type = type1;
6839
6840   /* Call grokdeclarator to figure out what type this is.  */
6841   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6842                           /*initialized=*/0, /*attrlist=*/NULL);
6843
6844   if (binary)
6845     {
6846       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6847  
6848       type2 = cp_parser_type_id (parser);
6849
6850       if (type2 == error_mark_node)
6851         return error_mark_node;
6852
6853       /* Build a trivial decl-specifier-seq.  */
6854       clear_decl_specs (&decl_specs);
6855       decl_specs.type = type2;
6856
6857       /* Call grokdeclarator to figure out what type this is.  */
6858       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6859                               /*initialized=*/0, /*attrlist=*/NULL);
6860     }
6861
6862   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6863
6864   /* Complete the trait expression, which may mean either processing
6865      the trait expr now or saving it for template instantiation.  */
6866   return finish_trait_expr (kind, type1, type2);
6867 }
6868
6869 /* Statements [gram.stmt.stmt]  */
6870
6871 /* Parse a statement.
6872
6873    statement:
6874      labeled-statement
6875      expression-statement
6876      compound-statement
6877      selection-statement
6878      iteration-statement
6879      jump-statement
6880      declaration-statement
6881      try-block
6882
6883   IN_COMPOUND is true when the statement is nested inside a
6884   cp_parser_compound_statement; this matters for certain pragmas.
6885
6886   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6887   is a (possibly labeled) if statement which is not enclosed in braces
6888   and has an else clause.  This is used to implement -Wparentheses.  */
6889
6890 static void
6891 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6892                      bool in_compound, bool *if_p)
6893 {
6894   tree statement;
6895   cp_token *token;
6896   location_t statement_location;
6897
6898  restart:
6899   if (if_p != NULL)
6900     *if_p = false;
6901   /* There is no statement yet.  */
6902   statement = NULL_TREE;
6903   /* Peek at the next token.  */
6904   token = cp_lexer_peek_token (parser->lexer);
6905   /* Remember the location of the first token in the statement.  */
6906   statement_location = token->location;
6907   /* If this is a keyword, then that will often determine what kind of
6908      statement we have.  */
6909   if (token->type == CPP_KEYWORD)
6910     {
6911       enum rid keyword = token->keyword;
6912
6913       switch (keyword)
6914         {
6915         case RID_CASE:
6916         case RID_DEFAULT:
6917           /* Looks like a labeled-statement with a case label.
6918              Parse the label, and then use tail recursion to parse
6919              the statement.  */
6920           cp_parser_label_for_labeled_statement (parser);
6921           goto restart;
6922
6923         case RID_IF:
6924         case RID_SWITCH:
6925           statement = cp_parser_selection_statement (parser, if_p);
6926           break;
6927
6928         case RID_WHILE:
6929         case RID_DO:
6930         case RID_FOR:
6931           statement = cp_parser_iteration_statement (parser);
6932           break;
6933
6934         case RID_BREAK:
6935         case RID_CONTINUE:
6936         case RID_RETURN:
6937         case RID_GOTO:
6938           statement = cp_parser_jump_statement (parser);
6939           break;
6940
6941           /* Objective-C++ exception-handling constructs.  */
6942         case RID_AT_TRY:
6943         case RID_AT_CATCH:
6944         case RID_AT_FINALLY:
6945         case RID_AT_SYNCHRONIZED:
6946         case RID_AT_THROW:
6947           statement = cp_parser_objc_statement (parser);
6948           break;
6949
6950         case RID_TRY:
6951           statement = cp_parser_try_block (parser);
6952           break;
6953
6954         case RID_NAMESPACE:
6955           /* This must be a namespace alias definition.  */
6956           cp_parser_declaration_statement (parser);
6957           return;
6958           
6959         default:
6960           /* It might be a keyword like `int' that can start a
6961              declaration-statement.  */
6962           break;
6963         }
6964     }
6965   else if (token->type == CPP_NAME)
6966     {
6967       /* If the next token is a `:', then we are looking at a
6968          labeled-statement.  */
6969       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6970       if (token->type == CPP_COLON)
6971         {
6972           /* Looks like a labeled-statement with an ordinary label.
6973              Parse the label, and then use tail recursion to parse
6974              the statement.  */
6975           cp_parser_label_for_labeled_statement (parser);
6976           goto restart;
6977         }
6978     }
6979   /* Anything that starts with a `{' must be a compound-statement.  */
6980   else if (token->type == CPP_OPEN_BRACE)
6981     statement = cp_parser_compound_statement (parser, NULL, false);
6982   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6983      a statement all its own.  */
6984   else if (token->type == CPP_PRAGMA)
6985     {
6986       /* Only certain OpenMP pragmas are attached to statements, and thus
6987          are considered statements themselves.  All others are not.  In
6988          the context of a compound, accept the pragma as a "statement" and
6989          return so that we can check for a close brace.  Otherwise we
6990          require a real statement and must go back and read one.  */
6991       if (in_compound)
6992         cp_parser_pragma (parser, pragma_compound);
6993       else if (!cp_parser_pragma (parser, pragma_stmt))
6994         goto restart;
6995       return;
6996     }
6997   else if (token->type == CPP_EOF)
6998     {
6999       cp_parser_error (parser, "expected statement");
7000       return;
7001     }
7002
7003   /* Everything else must be a declaration-statement or an
7004      expression-statement.  Try for the declaration-statement
7005      first, unless we are looking at a `;', in which case we know that
7006      we have an expression-statement.  */
7007   if (!statement)
7008     {
7009       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7010         {
7011           cp_parser_parse_tentatively (parser);
7012           /* Try to parse the declaration-statement.  */
7013           cp_parser_declaration_statement (parser);
7014           /* If that worked, we're done.  */
7015           if (cp_parser_parse_definitely (parser))
7016             return;
7017         }
7018       /* Look for an expression-statement instead.  */
7019       statement = cp_parser_expression_statement (parser, in_statement_expr);
7020     }
7021
7022   /* Set the line number for the statement.  */
7023   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7024     SET_EXPR_LOCATION (statement, statement_location);
7025 }
7026
7027 /* Parse the label for a labeled-statement, i.e.
7028
7029    identifier :
7030    case constant-expression :
7031    default :
7032
7033    GNU Extension:
7034    case constant-expression ... constant-expression : statement
7035
7036    When a label is parsed without errors, the label is added to the
7037    parse tree by the finish_* functions, so this function doesn't
7038    have to return the label.  */
7039
7040 static void
7041 cp_parser_label_for_labeled_statement (cp_parser* parser)
7042 {
7043   cp_token *token;
7044
7045   /* The next token should be an identifier.  */
7046   token = cp_lexer_peek_token (parser->lexer);
7047   if (token->type != CPP_NAME
7048       && token->type != CPP_KEYWORD)
7049     {
7050       cp_parser_error (parser, "expected labeled-statement");
7051       return;
7052     }
7053
7054   switch (token->keyword)
7055     {
7056     case RID_CASE:
7057       {
7058         tree expr, expr_hi;
7059         cp_token *ellipsis;
7060
7061         /* Consume the `case' token.  */
7062         cp_lexer_consume_token (parser->lexer);
7063         /* Parse the constant-expression.  */
7064         expr = cp_parser_constant_expression (parser,
7065                                               /*allow_non_constant_p=*/false,
7066                                               NULL);
7067
7068         ellipsis = cp_lexer_peek_token (parser->lexer);
7069         if (ellipsis->type == CPP_ELLIPSIS)
7070           {
7071             /* Consume the `...' token.  */
7072             cp_lexer_consume_token (parser->lexer);
7073             expr_hi =
7074               cp_parser_constant_expression (parser,
7075                                              /*allow_non_constant_p=*/false,
7076                                              NULL);
7077             /* We don't need to emit warnings here, as the common code
7078                will do this for us.  */
7079           }
7080         else
7081           expr_hi = NULL_TREE;
7082
7083         if (parser->in_switch_statement_p)
7084           finish_case_label (expr, expr_hi);
7085         else
7086           error ("%Hcase label %qE not within a switch statement",
7087                  &token->location, expr);
7088       }
7089       break;
7090
7091     case RID_DEFAULT:
7092       /* Consume the `default' token.  */
7093       cp_lexer_consume_token (parser->lexer);
7094
7095       if (parser->in_switch_statement_p)
7096         finish_case_label (NULL_TREE, NULL_TREE);
7097       else
7098         error ("%Hcase label not within a switch statement", &token->location);
7099       break;
7100
7101     default:
7102       /* Anything else must be an ordinary label.  */
7103       finish_label_stmt (cp_parser_identifier (parser));
7104       break;
7105     }
7106
7107   /* Require the `:' token.  */
7108   cp_parser_require (parser, CPP_COLON, "%<:%>");
7109 }
7110
7111 /* Parse an expression-statement.
7112
7113    expression-statement:
7114      expression [opt] ;
7115
7116    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7117    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7118    indicates whether this expression-statement is part of an
7119    expression statement.  */
7120
7121 static tree
7122 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7123 {
7124   tree statement = NULL_TREE;
7125
7126   /* If the next token is a ';', then there is no expression
7127      statement.  */
7128   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7129     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7130
7131   /* Consume the final `;'.  */
7132   cp_parser_consume_semicolon_at_end_of_statement (parser);
7133
7134   if (in_statement_expr
7135       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7136     /* This is the final expression statement of a statement
7137        expression.  */
7138     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7139   else if (statement)
7140     statement = finish_expr_stmt (statement);
7141   else
7142     finish_stmt ();
7143
7144   return statement;
7145 }
7146
7147 /* Parse a compound-statement.
7148
7149    compound-statement:
7150      { statement-seq [opt] }
7151
7152    GNU extension:
7153
7154    compound-statement:
7155      { label-declaration-seq [opt] statement-seq [opt] }
7156
7157    label-declaration-seq:
7158      label-declaration
7159      label-declaration-seq label-declaration
7160
7161    Returns a tree representing the statement.  */
7162
7163 static tree
7164 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7165                               bool in_try)
7166 {
7167   tree compound_stmt;
7168
7169   /* Consume the `{'.  */
7170   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7171     return error_mark_node;
7172   /* Begin the compound-statement.  */
7173   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7174   /* If the next keyword is `__label__' we have a label declaration.  */
7175   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7176     cp_parser_label_declaration (parser);
7177   /* Parse an (optional) statement-seq.  */
7178   cp_parser_statement_seq_opt (parser, in_statement_expr);
7179   /* Finish the compound-statement.  */
7180   finish_compound_stmt (compound_stmt);
7181   /* Consume the `}'.  */
7182   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7183
7184   return compound_stmt;
7185 }
7186
7187 /* Parse an (optional) statement-seq.
7188
7189    statement-seq:
7190      statement
7191      statement-seq [opt] statement  */
7192
7193 static void
7194 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7195 {
7196   /* Scan statements until there aren't any more.  */
7197   while (true)
7198     {
7199       cp_token *token = cp_lexer_peek_token (parser->lexer);
7200
7201       /* If we're looking at a `}', then we've run out of statements.  */
7202       if (token->type == CPP_CLOSE_BRACE
7203           || token->type == CPP_EOF
7204           || token->type == CPP_PRAGMA_EOL)
7205         break;
7206       
7207       /* If we are in a compound statement and find 'else' then
7208          something went wrong.  */
7209       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7210         {
7211           if (parser->in_statement & IN_IF_STMT) 
7212             break;
7213           else
7214             {
7215               token = cp_lexer_consume_token (parser->lexer);
7216               error ("%H%<else%> without a previous %<if%>", &token->location);
7217             }
7218         }
7219
7220       /* Parse the statement.  */
7221       cp_parser_statement (parser, in_statement_expr, true, NULL);
7222     }
7223 }
7224
7225 /* Parse a selection-statement.
7226
7227    selection-statement:
7228      if ( condition ) statement
7229      if ( condition ) statement else statement
7230      switch ( condition ) statement
7231
7232    Returns the new IF_STMT or SWITCH_STMT.
7233
7234    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7235    is a (possibly labeled) if statement which is not enclosed in
7236    braces and has an else clause.  This is used to implement
7237    -Wparentheses.  */
7238
7239 static tree
7240 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7241 {
7242   cp_token *token;
7243   enum rid keyword;
7244
7245   if (if_p != NULL)
7246     *if_p = false;
7247
7248   /* Peek at the next token.  */
7249   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7250
7251   /* See what kind of keyword it is.  */
7252   keyword = token->keyword;
7253   switch (keyword)
7254     {
7255     case RID_IF:
7256     case RID_SWITCH:
7257       {
7258         tree statement;
7259         tree condition;
7260
7261         /* Look for the `('.  */
7262         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7263           {
7264             cp_parser_skip_to_end_of_statement (parser);
7265             return error_mark_node;
7266           }
7267
7268         /* Begin the selection-statement.  */
7269         if (keyword == RID_IF)
7270           statement = begin_if_stmt ();
7271         else
7272           statement = begin_switch_stmt ();
7273
7274         /* Parse the condition.  */
7275         condition = cp_parser_condition (parser);
7276         /* Look for the `)'.  */
7277         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7278           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7279                                                  /*consume_paren=*/true);
7280
7281         if (keyword == RID_IF)
7282           {
7283             bool nested_if;
7284             unsigned char in_statement;
7285
7286             /* Add the condition.  */
7287             finish_if_stmt_cond (condition, statement);
7288
7289             /* Parse the then-clause.  */
7290             in_statement = parser->in_statement;
7291             parser->in_statement |= IN_IF_STMT;
7292             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7293               {
7294                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7295                 add_stmt (build_empty_stmt ());
7296                 cp_lexer_consume_token (parser->lexer);
7297                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7298                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7299                               "empty body in an %<if%> statement");
7300                 nested_if = false;
7301               }
7302             else
7303               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7304             parser->in_statement = in_statement;
7305
7306             finish_then_clause (statement);
7307
7308             /* If the next token is `else', parse the else-clause.  */
7309             if (cp_lexer_next_token_is_keyword (parser->lexer,
7310                                                 RID_ELSE))
7311               {
7312                 /* Consume the `else' keyword.  */
7313                 cp_lexer_consume_token (parser->lexer);
7314                 begin_else_clause (statement);
7315                 /* Parse the else-clause.  */
7316                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7317                   {
7318                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7319                                 OPT_Wempty_body, "suggest braces around "
7320                                 "empty body in an %<else%> statement");
7321                     add_stmt (build_empty_stmt ());
7322                     cp_lexer_consume_token (parser->lexer);
7323                   }
7324                 else
7325                   cp_parser_implicitly_scoped_statement (parser, NULL);
7326
7327                 finish_else_clause (statement);
7328
7329                 /* If we are currently parsing a then-clause, then
7330                    IF_P will not be NULL.  We set it to true to
7331                    indicate that this if statement has an else clause.
7332                    This may trigger the Wparentheses warning below
7333                    when we get back up to the parent if statement.  */
7334                 if (if_p != NULL)
7335                   *if_p = true;
7336               }
7337             else
7338               {
7339                 /* This if statement does not have an else clause.  If
7340                    NESTED_IF is true, then the then-clause is an if
7341                    statement which does have an else clause.  We warn
7342                    about the potential ambiguity.  */
7343                 if (nested_if)
7344                   warning (OPT_Wparentheses,
7345                            ("%Hsuggest explicit braces "
7346                             "to avoid ambiguous %<else%>"),
7347                            EXPR_LOCUS (statement));
7348               }
7349
7350             /* Now we're all done with the if-statement.  */
7351             finish_if_stmt (statement);
7352           }
7353         else
7354           {
7355             bool in_switch_statement_p;
7356             unsigned char in_statement;
7357
7358             /* Add the condition.  */
7359             finish_switch_cond (condition, statement);
7360
7361             /* Parse the body of the switch-statement.  */
7362             in_switch_statement_p = parser->in_switch_statement_p;
7363             in_statement = parser->in_statement;
7364             parser->in_switch_statement_p = true;
7365             parser->in_statement |= IN_SWITCH_STMT;
7366             cp_parser_implicitly_scoped_statement (parser, NULL);
7367             parser->in_switch_statement_p = in_switch_statement_p;
7368             parser->in_statement = in_statement;
7369
7370             /* Now we're all done with the switch-statement.  */
7371             finish_switch_stmt (statement);
7372           }
7373
7374         return statement;
7375       }
7376       break;
7377
7378     default:
7379       cp_parser_error (parser, "expected selection-statement");
7380       return error_mark_node;
7381     }
7382 }
7383
7384 /* Parse a condition.
7385
7386    condition:
7387      expression
7388      type-specifier-seq declarator = initializer-clause
7389      type-specifier-seq declarator braced-init-list
7390
7391    GNU Extension:
7392
7393    condition:
7394      type-specifier-seq declarator asm-specification [opt]
7395        attributes [opt] = assignment-expression
7396
7397    Returns the expression that should be tested.  */
7398
7399 static tree
7400 cp_parser_condition (cp_parser* parser)
7401 {
7402   cp_decl_specifier_seq type_specifiers;
7403   const char *saved_message;
7404
7405   /* Try the declaration first.  */
7406   cp_parser_parse_tentatively (parser);
7407   /* New types are not allowed in the type-specifier-seq for a
7408      condition.  */
7409   saved_message = parser->type_definition_forbidden_message;
7410   parser->type_definition_forbidden_message
7411     = "types may not be defined in conditions";
7412   /* Parse the type-specifier-seq.  */
7413   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7414                                 &type_specifiers);
7415   /* Restore the saved message.  */
7416   parser->type_definition_forbidden_message = saved_message;
7417   /* If all is well, we might be looking at a declaration.  */
7418   if (!cp_parser_error_occurred (parser))
7419     {
7420       tree decl;
7421       tree asm_specification;
7422       tree attributes;
7423       cp_declarator *declarator;
7424       tree initializer = NULL_TREE;
7425
7426       /* Parse the declarator.  */
7427       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7428                                          /*ctor_dtor_or_conv_p=*/NULL,
7429                                          /*parenthesized_p=*/NULL,
7430                                          /*member_p=*/false);
7431       /* Parse the attributes.  */
7432       attributes = cp_parser_attributes_opt (parser);
7433       /* Parse the asm-specification.  */
7434       asm_specification = cp_parser_asm_specification_opt (parser);
7435       /* If the next token is not an `=' or '{', then we might still be
7436          looking at an expression.  For example:
7437
7438            if (A(a).x)
7439
7440          looks like a decl-specifier-seq and a declarator -- but then
7441          there is no `=', so this is an expression.  */
7442       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7443           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7444         cp_parser_simulate_error (parser);
7445         
7446       /* If we did see an `=' or '{', then we are looking at a declaration
7447          for sure.  */
7448       if (cp_parser_parse_definitely (parser))
7449         {
7450           tree pushed_scope;
7451           bool non_constant_p;
7452           bool flags = LOOKUP_ONLYCONVERTING;
7453
7454           /* Create the declaration.  */
7455           decl = start_decl (declarator, &type_specifiers,
7456                              /*initialized_p=*/true,
7457                              attributes, /*prefix_attributes=*/NULL_TREE,
7458                              &pushed_scope);
7459
7460           /* Parse the initializer.  */
7461           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7462             {
7463               initializer = cp_parser_braced_list (parser, &non_constant_p);
7464               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7465               flags = 0;
7466             }
7467           else
7468             {
7469               /* Consume the `='.  */
7470               cp_parser_require (parser, CPP_EQ, "%<=%>");
7471               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7472             }
7473           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7474             maybe_warn_cpp0x ("extended initializer lists");
7475
7476           if (!non_constant_p)
7477             initializer = fold_non_dependent_expr (initializer);
7478
7479           /* Process the initializer.  */
7480           cp_finish_decl (decl,
7481                           initializer, !non_constant_p,
7482                           asm_specification,
7483                           flags);
7484
7485           if (pushed_scope)
7486             pop_scope (pushed_scope);
7487
7488           return convert_from_reference (decl);
7489         }
7490     }
7491   /* If we didn't even get past the declarator successfully, we are
7492      definitely not looking at a declaration.  */
7493   else
7494     cp_parser_abort_tentative_parse (parser);
7495
7496   /* Otherwise, we are looking at an expression.  */
7497   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7498 }
7499
7500 /* Parse an iteration-statement.
7501
7502    iteration-statement:
7503      while ( condition ) statement
7504      do statement while ( expression ) ;
7505      for ( for-init-statement condition [opt] ; expression [opt] )
7506        statement
7507
7508    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7509
7510 static tree
7511 cp_parser_iteration_statement (cp_parser* parser)
7512 {
7513   cp_token *token;
7514   enum rid keyword;
7515   tree statement;
7516   unsigned char in_statement;
7517
7518   /* Peek at the next token.  */
7519   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7520   if (!token)
7521     return error_mark_node;
7522
7523   /* Remember whether or not we are already within an iteration
7524      statement.  */
7525   in_statement = parser->in_statement;
7526
7527   /* See what kind of keyword it is.  */
7528   keyword = token->keyword;
7529   switch (keyword)
7530     {
7531     case RID_WHILE:
7532       {
7533         tree condition;
7534
7535         /* Begin the while-statement.  */
7536         statement = begin_while_stmt ();
7537         /* Look for the `('.  */
7538         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7539         /* Parse the condition.  */
7540         condition = cp_parser_condition (parser);
7541         finish_while_stmt_cond (condition, statement);
7542         /* Look for the `)'.  */
7543         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7544         /* Parse the dependent statement.  */
7545         parser->in_statement = IN_ITERATION_STMT;
7546         cp_parser_already_scoped_statement (parser);
7547         parser->in_statement = in_statement;
7548         /* We're done with the while-statement.  */
7549         finish_while_stmt (statement);
7550       }
7551       break;
7552
7553     case RID_DO:
7554       {
7555         tree expression;
7556
7557         /* Begin the do-statement.  */
7558         statement = begin_do_stmt ();
7559         /* Parse the body of the do-statement.  */
7560         parser->in_statement = IN_ITERATION_STMT;
7561         cp_parser_implicitly_scoped_statement (parser, NULL);
7562         parser->in_statement = in_statement;
7563         finish_do_body (statement);
7564         /* Look for the `while' keyword.  */
7565         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7566         /* Look for the `('.  */
7567         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7568         /* Parse the expression.  */
7569         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7570         /* We're done with the do-statement.  */
7571         finish_do_stmt (expression, statement);
7572         /* Look for the `)'.  */
7573         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7574         /* Look for the `;'.  */
7575         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7576       }
7577       break;
7578
7579     case RID_FOR:
7580       {
7581         tree condition = NULL_TREE;
7582         tree expression = NULL_TREE;
7583
7584         /* Begin the for-statement.  */
7585         statement = begin_for_stmt ();
7586         /* Look for the `('.  */
7587         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7588         /* Parse the initialization.  */
7589         cp_parser_for_init_statement (parser);
7590         finish_for_init_stmt (statement);
7591
7592         /* If there's a condition, process it.  */
7593         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7594           condition = cp_parser_condition (parser);
7595         finish_for_cond (condition, statement);
7596         /* Look for the `;'.  */
7597         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7598
7599         /* If there's an expression, process it.  */
7600         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7601           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7602         finish_for_expr (expression, statement);
7603         /* Look for the `)'.  */
7604         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7605
7606         /* Parse the body of the for-statement.  */
7607         parser->in_statement = IN_ITERATION_STMT;
7608         cp_parser_already_scoped_statement (parser);
7609         parser->in_statement = in_statement;
7610
7611         /* We're done with the for-statement.  */
7612         finish_for_stmt (statement);
7613       }
7614       break;
7615
7616     default:
7617       cp_parser_error (parser, "expected iteration-statement");
7618       statement = error_mark_node;
7619       break;
7620     }
7621
7622   return statement;
7623 }
7624
7625 /* Parse a for-init-statement.
7626
7627    for-init-statement:
7628      expression-statement
7629      simple-declaration  */
7630
7631 static void
7632 cp_parser_for_init_statement (cp_parser* parser)
7633 {
7634   /* If the next token is a `;', then we have an empty
7635      expression-statement.  Grammatically, this is also a
7636      simple-declaration, but an invalid one, because it does not
7637      declare anything.  Therefore, if we did not handle this case
7638      specially, we would issue an error message about an invalid
7639      declaration.  */
7640   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7641     {
7642       /* We're going to speculatively look for a declaration, falling back
7643          to an expression, if necessary.  */
7644       cp_parser_parse_tentatively (parser);
7645       /* Parse the declaration.  */
7646       cp_parser_simple_declaration (parser,
7647                                     /*function_definition_allowed_p=*/false);
7648       /* If the tentative parse failed, then we shall need to look for an
7649          expression-statement.  */
7650       if (cp_parser_parse_definitely (parser))
7651         return;
7652     }
7653
7654   cp_parser_expression_statement (parser, false);
7655 }
7656
7657 /* Parse a jump-statement.
7658
7659    jump-statement:
7660      break ;
7661      continue ;
7662      return expression [opt] ;
7663      return braced-init-list ;
7664      goto identifier ;
7665
7666    GNU extension:
7667
7668    jump-statement:
7669      goto * expression ;
7670
7671    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7672
7673 static tree
7674 cp_parser_jump_statement (cp_parser* parser)
7675 {
7676   tree statement = error_mark_node;
7677   cp_token *token;
7678   enum rid keyword;
7679   unsigned char in_statement;
7680
7681   /* Peek at the next token.  */
7682   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7683   if (!token)
7684     return error_mark_node;
7685
7686   /* See what kind of keyword it is.  */
7687   keyword = token->keyword;
7688   switch (keyword)
7689     {
7690     case RID_BREAK:
7691       in_statement = parser->in_statement & ~IN_IF_STMT;      
7692       switch (in_statement)
7693         {
7694         case 0:
7695           error ("%Hbreak statement not within loop or switch", &token->location);
7696           break;
7697         default:
7698           gcc_assert ((in_statement & IN_SWITCH_STMT)
7699                       || in_statement == IN_ITERATION_STMT);
7700           statement = finish_break_stmt ();
7701           break;
7702         case IN_OMP_BLOCK:
7703           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7704           break;
7705         case IN_OMP_FOR:
7706           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7707           break;
7708         }
7709       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7710       break;
7711
7712     case RID_CONTINUE:
7713       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7714         {
7715         case 0:
7716           error ("%Hcontinue statement not within a loop", &token->location);
7717           break;
7718         case IN_ITERATION_STMT:
7719         case IN_OMP_FOR:
7720           statement = finish_continue_stmt ();
7721           break;
7722         case IN_OMP_BLOCK:
7723           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7724           break;
7725         default:
7726           gcc_unreachable ();
7727         }
7728       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7729       break;
7730
7731     case RID_RETURN:
7732       {
7733         tree expr;
7734         bool expr_non_constant_p;
7735
7736         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7737           {
7738             maybe_warn_cpp0x ("extended initializer lists");
7739             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7740           }
7741         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7742           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7743         else
7744           /* If the next token is a `;', then there is no
7745              expression.  */
7746           expr = NULL_TREE;
7747         /* Build the return-statement.  */
7748         statement = finish_return_stmt (expr);
7749         /* Look for the final `;'.  */
7750         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7751       }
7752       break;
7753
7754     case RID_GOTO:
7755       /* Create the goto-statement.  */
7756       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7757         {
7758           /* Issue a warning about this use of a GNU extension.  */
7759           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7760           /* Consume the '*' token.  */
7761           cp_lexer_consume_token (parser->lexer);
7762           /* Parse the dependent expression.  */
7763           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7764         }
7765       else
7766         finish_goto_stmt (cp_parser_identifier (parser));
7767       /* Look for the final `;'.  */
7768       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7769       break;
7770
7771     default:
7772       cp_parser_error (parser, "expected jump-statement");
7773       break;
7774     }
7775
7776   return statement;
7777 }
7778
7779 /* Parse a declaration-statement.
7780
7781    declaration-statement:
7782      block-declaration  */
7783
7784 static void
7785 cp_parser_declaration_statement (cp_parser* parser)
7786 {
7787   void *p;
7788
7789   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7790   p = obstack_alloc (&declarator_obstack, 0);
7791
7792  /* Parse the block-declaration.  */
7793   cp_parser_block_declaration (parser, /*statement_p=*/true);
7794
7795   /* Free any declarators allocated.  */
7796   obstack_free (&declarator_obstack, p);
7797
7798   /* Finish off the statement.  */
7799   finish_stmt ();
7800 }
7801
7802 /* Some dependent statements (like `if (cond) statement'), are
7803    implicitly in their own scope.  In other words, if the statement is
7804    a single statement (as opposed to a compound-statement), it is
7805    none-the-less treated as if it were enclosed in braces.  Any
7806    declarations appearing in the dependent statement are out of scope
7807    after control passes that point.  This function parses a statement,
7808    but ensures that is in its own scope, even if it is not a
7809    compound-statement.
7810
7811    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7812    is a (possibly labeled) if statement which is not enclosed in
7813    braces and has an else clause.  This is used to implement
7814    -Wparentheses.
7815
7816    Returns the new statement.  */
7817
7818 static tree
7819 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7820 {
7821   tree statement;
7822
7823   if (if_p != NULL)
7824     *if_p = false;
7825
7826   /* Mark if () ; with a special NOP_EXPR.  */
7827   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7828     {
7829       cp_lexer_consume_token (parser->lexer);
7830       statement = add_stmt (build_empty_stmt ());
7831     }
7832   /* if a compound is opened, we simply parse the statement directly.  */
7833   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7834     statement = cp_parser_compound_statement (parser, NULL, false);
7835   /* If the token is not a `{', then we must take special action.  */
7836   else
7837     {
7838       /* Create a compound-statement.  */
7839       statement = begin_compound_stmt (0);
7840       /* Parse the dependent-statement.  */
7841       cp_parser_statement (parser, NULL_TREE, false, if_p);
7842       /* Finish the dummy compound-statement.  */
7843       finish_compound_stmt (statement);
7844     }
7845
7846   /* Return the statement.  */
7847   return statement;
7848 }
7849
7850 /* For some dependent statements (like `while (cond) statement'), we
7851    have already created a scope.  Therefore, even if the dependent
7852    statement is a compound-statement, we do not want to create another
7853    scope.  */
7854
7855 static void
7856 cp_parser_already_scoped_statement (cp_parser* parser)
7857 {
7858   /* If the token is a `{', then we must take special action.  */
7859   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7860     cp_parser_statement (parser, NULL_TREE, false, NULL);
7861   else
7862     {
7863       /* Avoid calling cp_parser_compound_statement, so that we
7864          don't create a new scope.  Do everything else by hand.  */
7865       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7866       /* If the next keyword is `__label__' we have a label declaration.  */
7867       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7868         cp_parser_label_declaration (parser);
7869       /* Parse an (optional) statement-seq.  */
7870       cp_parser_statement_seq_opt (parser, NULL_TREE);
7871       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7872     }
7873 }
7874
7875 /* Declarations [gram.dcl.dcl] */
7876
7877 /* Parse an optional declaration-sequence.
7878
7879    declaration-seq:
7880      declaration
7881      declaration-seq declaration  */
7882
7883 static void
7884 cp_parser_declaration_seq_opt (cp_parser* parser)
7885 {
7886   while (true)
7887     {
7888       cp_token *token;
7889
7890       token = cp_lexer_peek_token (parser->lexer);
7891
7892       if (token->type == CPP_CLOSE_BRACE
7893           || token->type == CPP_EOF
7894           || token->type == CPP_PRAGMA_EOL)
7895         break;
7896
7897       if (token->type == CPP_SEMICOLON)
7898         {
7899           /* A declaration consisting of a single semicolon is
7900              invalid.  Allow it unless we're being pedantic.  */
7901           cp_lexer_consume_token (parser->lexer);
7902           if (!in_system_header)
7903             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7904           continue;
7905         }
7906
7907       /* If we're entering or exiting a region that's implicitly
7908          extern "C", modify the lang context appropriately.  */
7909       if (!parser->implicit_extern_c && token->implicit_extern_c)
7910         {
7911           push_lang_context (lang_name_c);
7912           parser->implicit_extern_c = true;
7913         }
7914       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7915         {
7916           pop_lang_context ();
7917           parser->implicit_extern_c = false;
7918         }
7919
7920       if (token->type == CPP_PRAGMA)
7921         {
7922           /* A top-level declaration can consist solely of a #pragma.
7923              A nested declaration cannot, so this is done here and not
7924              in cp_parser_declaration.  (A #pragma at block scope is
7925              handled in cp_parser_statement.)  */
7926           cp_parser_pragma (parser, pragma_external);
7927           continue;
7928         }
7929
7930       /* Parse the declaration itself.  */
7931       cp_parser_declaration (parser);
7932     }
7933 }
7934
7935 /* Parse a declaration.
7936
7937    declaration:
7938      block-declaration
7939      function-definition
7940      template-declaration
7941      explicit-instantiation
7942      explicit-specialization
7943      linkage-specification
7944      namespace-definition
7945
7946    GNU extension:
7947
7948    declaration:
7949       __extension__ declaration */
7950
7951 static void
7952 cp_parser_declaration (cp_parser* parser)
7953 {
7954   cp_token token1;
7955   cp_token token2;
7956   int saved_pedantic;
7957   void *p;
7958
7959   /* Check for the `__extension__' keyword.  */
7960   if (cp_parser_extension_opt (parser, &saved_pedantic))
7961     {
7962       /* Parse the qualified declaration.  */
7963       cp_parser_declaration (parser);
7964       /* Restore the PEDANTIC flag.  */
7965       pedantic = saved_pedantic;
7966
7967       return;
7968     }
7969
7970   /* Try to figure out what kind of declaration is present.  */
7971   token1 = *cp_lexer_peek_token (parser->lexer);
7972
7973   if (token1.type != CPP_EOF)
7974     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7975   else
7976     {
7977       token2.type = CPP_EOF;
7978       token2.keyword = RID_MAX;
7979     }
7980
7981   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7982   p = obstack_alloc (&declarator_obstack, 0);
7983
7984   /* If the next token is `extern' and the following token is a string
7985      literal, then we have a linkage specification.  */
7986   if (token1.keyword == RID_EXTERN
7987       && cp_parser_is_string_literal (&token2))
7988     cp_parser_linkage_specification (parser);
7989   /* If the next token is `template', then we have either a template
7990      declaration, an explicit instantiation, or an explicit
7991      specialization.  */
7992   else if (token1.keyword == RID_TEMPLATE)
7993     {
7994       /* `template <>' indicates a template specialization.  */
7995       if (token2.type == CPP_LESS
7996           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7997         cp_parser_explicit_specialization (parser);
7998       /* `template <' indicates a template declaration.  */
7999       else if (token2.type == CPP_LESS)
8000         cp_parser_template_declaration (parser, /*member_p=*/false);
8001       /* Anything else must be an explicit instantiation.  */
8002       else
8003         cp_parser_explicit_instantiation (parser);
8004     }
8005   /* If the next token is `export', then we have a template
8006      declaration.  */
8007   else if (token1.keyword == RID_EXPORT)
8008     cp_parser_template_declaration (parser, /*member_p=*/false);
8009   /* If the next token is `extern', 'static' or 'inline' and the one
8010      after that is `template', we have a GNU extended explicit
8011      instantiation directive.  */
8012   else if (cp_parser_allow_gnu_extensions_p (parser)
8013            && (token1.keyword == RID_EXTERN
8014                || token1.keyword == RID_STATIC
8015                || token1.keyword == RID_INLINE)
8016            && token2.keyword == RID_TEMPLATE)
8017     cp_parser_explicit_instantiation (parser);
8018   /* If the next token is `namespace', check for a named or unnamed
8019      namespace definition.  */
8020   else if (token1.keyword == RID_NAMESPACE
8021            && (/* A named namespace definition.  */
8022                (token2.type == CPP_NAME
8023                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8024                     != CPP_EQ))
8025                /* An unnamed namespace definition.  */
8026                || token2.type == CPP_OPEN_BRACE
8027                || token2.keyword == RID_ATTRIBUTE))
8028     cp_parser_namespace_definition (parser);
8029   /* An inline (associated) namespace definition.  */
8030   else if (token1.keyword == RID_INLINE
8031            && token2.keyword == RID_NAMESPACE)
8032     cp_parser_namespace_definition (parser);
8033   /* Objective-C++ declaration/definition.  */
8034   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8035     cp_parser_objc_declaration (parser);
8036   /* We must have either a block declaration or a function
8037      definition.  */
8038   else
8039     /* Try to parse a block-declaration, or a function-definition.  */
8040     cp_parser_block_declaration (parser, /*statement_p=*/false);
8041
8042   /* Free any declarators allocated.  */
8043   obstack_free (&declarator_obstack, p);
8044 }
8045
8046 /* Parse a block-declaration.
8047
8048    block-declaration:
8049      simple-declaration
8050      asm-definition
8051      namespace-alias-definition
8052      using-declaration
8053      using-directive
8054
8055    GNU Extension:
8056
8057    block-declaration:
8058      __extension__ block-declaration
8059
8060    C++0x Extension:
8061
8062    block-declaration:
8063      static_assert-declaration
8064
8065    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8066    part of a declaration-statement.  */
8067
8068 static void
8069 cp_parser_block_declaration (cp_parser *parser,
8070                              bool      statement_p)
8071 {
8072   cp_token *token1;
8073   int saved_pedantic;
8074
8075   /* Check for the `__extension__' keyword.  */
8076   if (cp_parser_extension_opt (parser, &saved_pedantic))
8077     {
8078       /* Parse the qualified declaration.  */
8079       cp_parser_block_declaration (parser, statement_p);
8080       /* Restore the PEDANTIC flag.  */
8081       pedantic = saved_pedantic;
8082
8083       return;
8084     }
8085
8086   /* Peek at the next token to figure out which kind of declaration is
8087      present.  */
8088   token1 = cp_lexer_peek_token (parser->lexer);
8089
8090   /* If the next keyword is `asm', we have an asm-definition.  */
8091   if (token1->keyword == RID_ASM)
8092     {
8093       if (statement_p)
8094         cp_parser_commit_to_tentative_parse (parser);
8095       cp_parser_asm_definition (parser);
8096     }
8097   /* If the next keyword is `namespace', we have a
8098      namespace-alias-definition.  */
8099   else if (token1->keyword == RID_NAMESPACE)
8100     cp_parser_namespace_alias_definition (parser);
8101   /* If the next keyword is `using', we have either a
8102      using-declaration or a using-directive.  */
8103   else if (token1->keyword == RID_USING)
8104     {
8105       cp_token *token2;
8106
8107       if (statement_p)
8108         cp_parser_commit_to_tentative_parse (parser);
8109       /* If the token after `using' is `namespace', then we have a
8110          using-directive.  */
8111       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8112       if (token2->keyword == RID_NAMESPACE)
8113         cp_parser_using_directive (parser);
8114       /* Otherwise, it's a using-declaration.  */
8115       else
8116         cp_parser_using_declaration (parser,
8117                                      /*access_declaration_p=*/false);
8118     }
8119   /* If the next keyword is `__label__' we have a misplaced label
8120      declaration.  */
8121   else if (token1->keyword == RID_LABEL)
8122     {
8123       cp_lexer_consume_token (parser->lexer);
8124       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8125       cp_parser_skip_to_end_of_statement (parser);
8126       /* If the next token is now a `;', consume it.  */
8127       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8128         cp_lexer_consume_token (parser->lexer);
8129     }
8130   /* If the next token is `static_assert' we have a static assertion.  */
8131   else if (token1->keyword == RID_STATIC_ASSERT)
8132     cp_parser_static_assert (parser, /*member_p=*/false);
8133   /* Anything else must be a simple-declaration.  */
8134   else
8135     cp_parser_simple_declaration (parser, !statement_p);
8136 }
8137
8138 /* Parse a simple-declaration.
8139
8140    simple-declaration:
8141      decl-specifier-seq [opt] init-declarator-list [opt] ;
8142
8143    init-declarator-list:
8144      init-declarator
8145      init-declarator-list , init-declarator
8146
8147    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8148    function-definition as a simple-declaration.  */
8149
8150 static void
8151 cp_parser_simple_declaration (cp_parser* parser,
8152                               bool function_definition_allowed_p)
8153 {
8154   cp_decl_specifier_seq decl_specifiers;
8155   int declares_class_or_enum;
8156   bool saw_declarator;
8157
8158   /* Defer access checks until we know what is being declared; the
8159      checks for names appearing in the decl-specifier-seq should be
8160      done as if we were in the scope of the thing being declared.  */
8161   push_deferring_access_checks (dk_deferred);
8162
8163   /* Parse the decl-specifier-seq.  We have to keep track of whether
8164      or not the decl-specifier-seq declares a named class or
8165      enumeration type, since that is the only case in which the
8166      init-declarator-list is allowed to be empty.
8167
8168      [dcl.dcl]
8169
8170      In a simple-declaration, the optional init-declarator-list can be
8171      omitted only when declaring a class or enumeration, that is when
8172      the decl-specifier-seq contains either a class-specifier, an
8173      elaborated-type-specifier, or an enum-specifier.  */
8174   cp_parser_decl_specifier_seq (parser,
8175                                 CP_PARSER_FLAGS_OPTIONAL,
8176                                 &decl_specifiers,
8177                                 &declares_class_or_enum);
8178   /* We no longer need to defer access checks.  */
8179   stop_deferring_access_checks ();
8180
8181   /* In a block scope, a valid declaration must always have a
8182      decl-specifier-seq.  By not trying to parse declarators, we can
8183      resolve the declaration/expression ambiguity more quickly.  */
8184   if (!function_definition_allowed_p
8185       && !decl_specifiers.any_specifiers_p)
8186     {
8187       cp_parser_error (parser, "expected declaration");
8188       goto done;
8189     }
8190
8191   /* If the next two tokens are both identifiers, the code is
8192      erroneous. The usual cause of this situation is code like:
8193
8194        T t;
8195
8196      where "T" should name a type -- but does not.  */
8197   if (!decl_specifiers.type
8198       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8199     {
8200       /* If parsing tentatively, we should commit; we really are
8201          looking at a declaration.  */
8202       cp_parser_commit_to_tentative_parse (parser);
8203       /* Give up.  */
8204       goto done;
8205     }
8206
8207   /* If we have seen at least one decl-specifier, and the next token
8208      is not a parenthesis, then we must be looking at a declaration.
8209      (After "int (" we might be looking at a functional cast.)  */
8210   if (decl_specifiers.any_specifiers_p
8211       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8212       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8213       && !cp_parser_error_occurred (parser))
8214     cp_parser_commit_to_tentative_parse (parser);
8215
8216   /* Keep going until we hit the `;' at the end of the simple
8217      declaration.  */
8218   saw_declarator = false;
8219   while (cp_lexer_next_token_is_not (parser->lexer,
8220                                      CPP_SEMICOLON))
8221     {
8222       cp_token *token;
8223       bool function_definition_p;
8224       tree decl;
8225
8226       if (saw_declarator)
8227         {
8228           /* If we are processing next declarator, coma is expected */
8229           token = cp_lexer_peek_token (parser->lexer);
8230           gcc_assert (token->type == CPP_COMMA);
8231           cp_lexer_consume_token (parser->lexer);
8232         }
8233       else
8234         saw_declarator = true;
8235
8236       /* Parse the init-declarator.  */
8237       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8238                                         /*checks=*/NULL,
8239                                         function_definition_allowed_p,
8240                                         /*member_p=*/false,
8241                                         declares_class_or_enum,
8242                                         &function_definition_p);
8243       /* If an error occurred while parsing tentatively, exit quickly.
8244          (That usually happens when in the body of a function; each
8245          statement is treated as a declaration-statement until proven
8246          otherwise.)  */
8247       if (cp_parser_error_occurred (parser))
8248         goto done;
8249       /* Handle function definitions specially.  */
8250       if (function_definition_p)
8251         {
8252           /* If the next token is a `,', then we are probably
8253              processing something like:
8254
8255                void f() {}, *p;
8256
8257              which is erroneous.  */
8258           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8259             {
8260               cp_token *token = cp_lexer_peek_token (parser->lexer);
8261               error ("%Hmixing declarations and function-definitions is forbidden",
8262                      &token->location);
8263             }
8264           /* Otherwise, we're done with the list of declarators.  */
8265           else
8266             {
8267               pop_deferring_access_checks ();
8268               return;
8269             }
8270         }
8271       /* The next token should be either a `,' or a `;'.  */
8272       token = cp_lexer_peek_token (parser->lexer);
8273       /* If it's a `,', there are more declarators to come.  */
8274       if (token->type == CPP_COMMA)
8275         /* will be consumed next time around */;
8276       /* If it's a `;', we are done.  */
8277       else if (token->type == CPP_SEMICOLON)
8278         break;
8279       /* Anything else is an error.  */
8280       else
8281         {
8282           /* If we have already issued an error message we don't need
8283              to issue another one.  */
8284           if (decl != error_mark_node
8285               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8286             cp_parser_error (parser, "expected %<,%> or %<;%>");
8287           /* Skip tokens until we reach the end of the statement.  */
8288           cp_parser_skip_to_end_of_statement (parser);
8289           /* If the next token is now a `;', consume it.  */
8290           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8291             cp_lexer_consume_token (parser->lexer);
8292           goto done;
8293         }
8294       /* After the first time around, a function-definition is not
8295          allowed -- even if it was OK at first.  For example:
8296
8297            int i, f() {}
8298
8299          is not valid.  */
8300       function_definition_allowed_p = false;
8301     }
8302
8303   /* Issue an error message if no declarators are present, and the
8304      decl-specifier-seq does not itself declare a class or
8305      enumeration.  */
8306   if (!saw_declarator)
8307     {
8308       if (cp_parser_declares_only_class_p (parser))
8309         shadow_tag (&decl_specifiers);
8310       /* Perform any deferred access checks.  */
8311       perform_deferred_access_checks ();
8312     }
8313
8314   /* Consume the `;'.  */
8315   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8316
8317  done:
8318   pop_deferring_access_checks ();
8319 }
8320
8321 /* Parse a decl-specifier-seq.
8322
8323    decl-specifier-seq:
8324      decl-specifier-seq [opt] decl-specifier
8325
8326    decl-specifier:
8327      storage-class-specifier
8328      type-specifier
8329      function-specifier
8330      friend
8331      typedef
8332
8333    GNU Extension:
8334
8335    decl-specifier:
8336      attributes
8337
8338    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8339
8340    The parser flags FLAGS is used to control type-specifier parsing.
8341
8342    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8343    flags:
8344
8345      1: one of the decl-specifiers is an elaborated-type-specifier
8346         (i.e., a type declaration)
8347      2: one of the decl-specifiers is an enum-specifier or a
8348         class-specifier (i.e., a type definition)
8349
8350    */
8351
8352 static void
8353 cp_parser_decl_specifier_seq (cp_parser* parser,
8354                               cp_parser_flags flags,
8355                               cp_decl_specifier_seq *decl_specs,
8356                               int* declares_class_or_enum)
8357 {
8358   bool constructor_possible_p = !parser->in_declarator_p;
8359   cp_token *start_token = NULL;
8360
8361   /* Clear DECL_SPECS.  */
8362   clear_decl_specs (decl_specs);
8363
8364   /* Assume no class or enumeration type is declared.  */
8365   *declares_class_or_enum = 0;
8366
8367   /* Keep reading specifiers until there are no more to read.  */
8368   while (true)
8369     {
8370       bool constructor_p;
8371       bool found_decl_spec;
8372       cp_token *token;
8373
8374       /* Peek at the next token.  */
8375       token = cp_lexer_peek_token (parser->lexer);
8376
8377       /* Save the first token of the decl spec list for error
8378          reporting.  */
8379       if (!start_token)
8380         start_token = token;
8381       /* Handle attributes.  */
8382       if (token->keyword == RID_ATTRIBUTE)
8383         {
8384           /* Parse the attributes.  */
8385           decl_specs->attributes
8386             = chainon (decl_specs->attributes,
8387                        cp_parser_attributes_opt (parser));
8388           continue;
8389         }
8390       /* Assume we will find a decl-specifier keyword.  */
8391       found_decl_spec = true;
8392       /* If the next token is an appropriate keyword, we can simply
8393          add it to the list.  */
8394       switch (token->keyword)
8395         {
8396           /* decl-specifier:
8397                friend  */
8398         case RID_FRIEND:
8399           if (!at_class_scope_p ())
8400             {
8401               error ("%H%<friend%> used outside of class", &token->location);
8402               cp_lexer_purge_token (parser->lexer);
8403             }
8404           else
8405             {
8406               ++decl_specs->specs[(int) ds_friend];
8407               /* Consume the token.  */
8408               cp_lexer_consume_token (parser->lexer);
8409             }
8410           break;
8411
8412           /* function-specifier:
8413                inline
8414                virtual
8415                explicit  */
8416         case RID_INLINE:
8417         case RID_VIRTUAL:
8418         case RID_EXPLICIT:
8419           cp_parser_function_specifier_opt (parser, decl_specs);
8420           break;
8421
8422           /* decl-specifier:
8423                typedef  */
8424         case RID_TYPEDEF:
8425           ++decl_specs->specs[(int) ds_typedef];
8426           /* Consume the token.  */
8427           cp_lexer_consume_token (parser->lexer);
8428           /* A constructor declarator cannot appear in a typedef.  */
8429           constructor_possible_p = false;
8430           /* The "typedef" keyword can only occur in a declaration; we
8431              may as well commit at this point.  */
8432           cp_parser_commit_to_tentative_parse (parser);
8433
8434           if (decl_specs->storage_class != sc_none)
8435             decl_specs->conflicting_specifiers_p = true;
8436           break;
8437
8438           /* storage-class-specifier:
8439                auto
8440                register
8441                static
8442                extern
8443                mutable
8444
8445              GNU Extension:
8446                thread  */
8447         case RID_AUTO:
8448           if (cxx_dialect == cxx98) 
8449             {
8450               /* Consume the token.  */
8451               cp_lexer_consume_token (parser->lexer);
8452
8453               /* Complain about `auto' as a storage specifier, if
8454                  we're complaining about C++0x compatibility.  */
8455               warning 
8456                 (OPT_Wc__0x_compat, 
8457                  "%H%<auto%> will change meaning in C++0x; please remove it",
8458                  &token->location);
8459
8460               /* Set the storage class anyway.  */
8461               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8462                                            token->location);
8463             }
8464           else
8465             /* C++0x auto type-specifier.  */
8466             found_decl_spec = false;
8467           break;
8468
8469         case RID_REGISTER:
8470         case RID_STATIC:
8471         case RID_EXTERN:
8472         case RID_MUTABLE:
8473           /* Consume the token.  */
8474           cp_lexer_consume_token (parser->lexer);
8475           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8476                                        token->location);
8477           break;
8478         case RID_THREAD:
8479           /* Consume the token.  */
8480           cp_lexer_consume_token (parser->lexer);
8481           ++decl_specs->specs[(int) ds_thread];
8482           break;
8483
8484         default:
8485           /* We did not yet find a decl-specifier yet.  */
8486           found_decl_spec = false;
8487           break;
8488         }
8489
8490       /* Constructors are a special case.  The `S' in `S()' is not a
8491          decl-specifier; it is the beginning of the declarator.  */
8492       constructor_p
8493         = (!found_decl_spec
8494            && constructor_possible_p
8495            && (cp_parser_constructor_declarator_p
8496                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8497
8498       /* If we don't have a DECL_SPEC yet, then we must be looking at
8499          a type-specifier.  */
8500       if (!found_decl_spec && !constructor_p)
8501         {
8502           int decl_spec_declares_class_or_enum;
8503           bool is_cv_qualifier;
8504           tree type_spec;
8505
8506           type_spec
8507             = cp_parser_type_specifier (parser, flags,
8508                                         decl_specs,
8509                                         /*is_declaration=*/true,
8510                                         &decl_spec_declares_class_or_enum,
8511                                         &is_cv_qualifier);
8512           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8513
8514           /* If this type-specifier referenced a user-defined type
8515              (a typedef, class-name, etc.), then we can't allow any
8516              more such type-specifiers henceforth.
8517
8518              [dcl.spec]
8519
8520              The longest sequence of decl-specifiers that could
8521              possibly be a type name is taken as the
8522              decl-specifier-seq of a declaration.  The sequence shall
8523              be self-consistent as described below.
8524
8525              [dcl.type]
8526
8527              As a general rule, at most one type-specifier is allowed
8528              in the complete decl-specifier-seq of a declaration.  The
8529              only exceptions are the following:
8530
8531              -- const or volatile can be combined with any other
8532                 type-specifier.
8533
8534              -- signed or unsigned can be combined with char, long,
8535                 short, or int.
8536
8537              -- ..
8538
8539              Example:
8540
8541                typedef char* Pc;
8542                void g (const int Pc);
8543
8544              Here, Pc is *not* part of the decl-specifier seq; it's
8545              the declarator.  Therefore, once we see a type-specifier
8546              (other than a cv-qualifier), we forbid any additional
8547              user-defined types.  We *do* still allow things like `int
8548              int' to be considered a decl-specifier-seq, and issue the
8549              error message later.  */
8550           if (type_spec && !is_cv_qualifier)
8551             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8552           /* A constructor declarator cannot follow a type-specifier.  */
8553           if (type_spec)
8554             {
8555               constructor_possible_p = false;
8556               found_decl_spec = true;
8557             }
8558         }
8559
8560       /* If we still do not have a DECL_SPEC, then there are no more
8561          decl-specifiers.  */
8562       if (!found_decl_spec)
8563         break;
8564
8565       decl_specs->any_specifiers_p = true;
8566       /* After we see one decl-specifier, further decl-specifiers are
8567          always optional.  */
8568       flags |= CP_PARSER_FLAGS_OPTIONAL;
8569     }
8570
8571   cp_parser_check_decl_spec (decl_specs, start_token->location);
8572
8573   /* Don't allow a friend specifier with a class definition.  */
8574   if (decl_specs->specs[(int) ds_friend] != 0
8575       && (*declares_class_or_enum & 2))
8576     error ("%Hclass definition may not be declared a friend",
8577             &start_token->location);
8578 }
8579
8580 /* Parse an (optional) storage-class-specifier.
8581
8582    storage-class-specifier:
8583      auto
8584      register
8585      static
8586      extern
8587      mutable
8588
8589    GNU Extension:
8590
8591    storage-class-specifier:
8592      thread
8593
8594    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8595
8596 static tree
8597 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8598 {
8599   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8600     {
8601     case RID_AUTO:
8602       if (cxx_dialect != cxx98)
8603         return NULL_TREE;
8604       /* Fall through for C++98.  */
8605
8606     case RID_REGISTER:
8607     case RID_STATIC:
8608     case RID_EXTERN:
8609     case RID_MUTABLE:
8610     case RID_THREAD:
8611       /* Consume the token.  */
8612       return cp_lexer_consume_token (parser->lexer)->u.value;
8613
8614     default:
8615       return NULL_TREE;
8616     }
8617 }
8618
8619 /* Parse an (optional) function-specifier.
8620
8621    function-specifier:
8622      inline
8623      virtual
8624      explicit
8625
8626    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8627    Updates DECL_SPECS, if it is non-NULL.  */
8628
8629 static tree
8630 cp_parser_function_specifier_opt (cp_parser* parser,
8631                                   cp_decl_specifier_seq *decl_specs)
8632 {
8633   cp_token *token = cp_lexer_peek_token (parser->lexer);
8634   switch (token->keyword)
8635     {
8636     case RID_INLINE:
8637       if (decl_specs)
8638         ++decl_specs->specs[(int) ds_inline];
8639       break;
8640
8641     case RID_VIRTUAL:
8642       /* 14.5.2.3 [temp.mem]
8643
8644          A member function template shall not be virtual.  */
8645       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8646         error ("%Htemplates may not be %<virtual%>", &token->location);
8647       else if (decl_specs)
8648         ++decl_specs->specs[(int) ds_virtual];
8649       break;
8650
8651     case RID_EXPLICIT:
8652       if (decl_specs)
8653         ++decl_specs->specs[(int) ds_explicit];
8654       break;
8655
8656     default:
8657       return NULL_TREE;
8658     }
8659
8660   /* Consume the token.  */
8661   return cp_lexer_consume_token (parser->lexer)->u.value;
8662 }
8663
8664 /* Parse a linkage-specification.
8665
8666    linkage-specification:
8667      extern string-literal { declaration-seq [opt] }
8668      extern string-literal declaration  */
8669
8670 static void
8671 cp_parser_linkage_specification (cp_parser* parser)
8672 {
8673   tree linkage;
8674
8675   /* Look for the `extern' keyword.  */
8676   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8677
8678   /* Look for the string-literal.  */
8679   linkage = cp_parser_string_literal (parser, false, false);
8680
8681   /* Transform the literal into an identifier.  If the literal is a
8682      wide-character string, or contains embedded NULs, then we can't
8683      handle it as the user wants.  */
8684   if (strlen (TREE_STRING_POINTER (linkage))
8685       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8686     {
8687       cp_parser_error (parser, "invalid linkage-specification");
8688       /* Assume C++ linkage.  */
8689       linkage = lang_name_cplusplus;
8690     }
8691   else
8692     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8693
8694   /* We're now using the new linkage.  */
8695   push_lang_context (linkage);
8696
8697   /* If the next token is a `{', then we're using the first
8698      production.  */
8699   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8700     {
8701       /* Consume the `{' token.  */
8702       cp_lexer_consume_token (parser->lexer);
8703       /* Parse the declarations.  */
8704       cp_parser_declaration_seq_opt (parser);
8705       /* Look for the closing `}'.  */
8706       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8707     }
8708   /* Otherwise, there's just one declaration.  */
8709   else
8710     {
8711       bool saved_in_unbraced_linkage_specification_p;
8712
8713       saved_in_unbraced_linkage_specification_p
8714         = parser->in_unbraced_linkage_specification_p;
8715       parser->in_unbraced_linkage_specification_p = true;
8716       cp_parser_declaration (parser);
8717       parser->in_unbraced_linkage_specification_p
8718         = saved_in_unbraced_linkage_specification_p;
8719     }
8720
8721   /* We're done with the linkage-specification.  */
8722   pop_lang_context ();
8723 }
8724
8725 /* Parse a static_assert-declaration.
8726
8727    static_assert-declaration:
8728      static_assert ( constant-expression , string-literal ) ; 
8729
8730    If MEMBER_P, this static_assert is a class member.  */
8731
8732 static void 
8733 cp_parser_static_assert(cp_parser *parser, bool member_p)
8734 {
8735   tree condition;
8736   tree message;
8737   cp_token *token;
8738   location_t saved_loc;
8739
8740   /* Peek at the `static_assert' token so we can keep track of exactly
8741      where the static assertion started.  */
8742   token = cp_lexer_peek_token (parser->lexer);
8743   saved_loc = token->location;
8744
8745   /* Look for the `static_assert' keyword.  */
8746   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8747                                   "%<static_assert%>"))
8748     return;
8749
8750   /*  We know we are in a static assertion; commit to any tentative
8751       parse.  */
8752   if (cp_parser_parsing_tentatively (parser))
8753     cp_parser_commit_to_tentative_parse (parser);
8754
8755   /* Parse the `(' starting the static assertion condition.  */
8756   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8757
8758   /* Parse the constant-expression.  */
8759   condition = 
8760     cp_parser_constant_expression (parser,
8761                                    /*allow_non_constant_p=*/false,
8762                                    /*non_constant_p=*/NULL);
8763
8764   /* Parse the separating `,'.  */
8765   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8766
8767   /* Parse the string-literal message.  */
8768   message = cp_parser_string_literal (parser, 
8769                                       /*translate=*/false,
8770                                       /*wide_ok=*/true);
8771
8772   /* A `)' completes the static assertion.  */
8773   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8774     cp_parser_skip_to_closing_parenthesis (parser, 
8775                                            /*recovering=*/true, 
8776                                            /*or_comma=*/false,
8777                                            /*consume_paren=*/true);
8778
8779   /* A semicolon terminates the declaration.  */
8780   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8781
8782   /* Complete the static assertion, which may mean either processing 
8783      the static assert now or saving it for template instantiation.  */
8784   finish_static_assert (condition, message, saved_loc, member_p);
8785 }
8786
8787 /* Parse a `decltype' type. Returns the type. 
8788
8789    simple-type-specifier:
8790      decltype ( expression )  */
8791
8792 static tree
8793 cp_parser_decltype (cp_parser *parser)
8794 {
8795   tree expr;
8796   bool id_expression_or_member_access_p = false;
8797   const char *saved_message;
8798   bool saved_integral_constant_expression_p;
8799   bool saved_non_integral_constant_expression_p;
8800   cp_token *id_expr_start_token;
8801
8802   /* Look for the `decltype' token.  */
8803   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8804     return error_mark_node;
8805
8806   /* Types cannot be defined in a `decltype' expression.  Save away the
8807      old message.  */
8808   saved_message = parser->type_definition_forbidden_message;
8809
8810   /* And create the new one.  */
8811   parser->type_definition_forbidden_message
8812     = "types may not be defined in %<decltype%> expressions";
8813
8814   /* The restrictions on constant-expressions do not apply inside
8815      decltype expressions.  */
8816   saved_integral_constant_expression_p
8817     = parser->integral_constant_expression_p;
8818   saved_non_integral_constant_expression_p
8819     = parser->non_integral_constant_expression_p;
8820   parser->integral_constant_expression_p = false;
8821
8822   /* Do not actually evaluate the expression.  */
8823   ++skip_evaluation;
8824
8825   /* Parse the opening `('.  */
8826   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8827     return error_mark_node;
8828   
8829   /* First, try parsing an id-expression.  */
8830   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8831   cp_parser_parse_tentatively (parser);
8832   expr = cp_parser_id_expression (parser,
8833                                   /*template_keyword_p=*/false,
8834                                   /*check_dependency_p=*/true,
8835                                   /*template_p=*/NULL,
8836                                   /*declarator_p=*/false,
8837                                   /*optional_p=*/false);
8838
8839   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8840     {
8841       bool non_integral_constant_expression_p = false;
8842       tree id_expression = expr;
8843       cp_id_kind idk;
8844       const char *error_msg;
8845
8846       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8847         /* Lookup the name we got back from the id-expression.  */
8848         expr = cp_parser_lookup_name (parser, expr,
8849                                       none_type,
8850                                       /*is_template=*/false,
8851                                       /*is_namespace=*/false,
8852                                       /*check_dependency=*/true,
8853                                       /*ambiguous_decls=*/NULL,
8854                                       id_expr_start_token->location);
8855
8856       if (expr
8857           && expr != error_mark_node
8858           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8859           && TREE_CODE (expr) != TYPE_DECL
8860           && (TREE_CODE (expr) != BIT_NOT_EXPR
8861               || !TYPE_P (TREE_OPERAND (expr, 0)))
8862           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8863         {
8864           /* Complete lookup of the id-expression.  */
8865           expr = (finish_id_expression
8866                   (id_expression, expr, parser->scope, &idk,
8867                    /*integral_constant_expression_p=*/false,
8868                    /*allow_non_integral_constant_expression_p=*/true,
8869                    &non_integral_constant_expression_p,
8870                    /*template_p=*/false,
8871                    /*done=*/true,
8872                    /*address_p=*/false,
8873                    /*template_arg_p=*/false,
8874                    &error_msg,
8875                    id_expr_start_token->location));
8876
8877           if (expr == error_mark_node)
8878             /* We found an id-expression, but it was something that we
8879                should not have found. This is an error, not something
8880                we can recover from, so note that we found an
8881                id-expression and we'll recover as gracefully as
8882                possible.  */
8883             id_expression_or_member_access_p = true;
8884         }
8885
8886       if (expr 
8887           && expr != error_mark_node
8888           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8889         /* We have an id-expression.  */
8890         id_expression_or_member_access_p = true;
8891     }
8892
8893   if (!id_expression_or_member_access_p)
8894     {
8895       /* Abort the id-expression parse.  */
8896       cp_parser_abort_tentative_parse (parser);
8897
8898       /* Parsing tentatively, again.  */
8899       cp_parser_parse_tentatively (parser);
8900
8901       /* Parse a class member access.  */
8902       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8903                                            /*cast_p=*/false,
8904                                            /*member_access_only_p=*/true, NULL);
8905
8906       if (expr 
8907           && expr != error_mark_node
8908           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8909         /* We have an id-expression.  */
8910         id_expression_or_member_access_p = true;
8911     }
8912
8913   if (id_expression_or_member_access_p)
8914     /* We have parsed the complete id-expression or member access.  */
8915     cp_parser_parse_definitely (parser);
8916   else
8917     {
8918       /* Abort our attempt to parse an id-expression or member access
8919          expression.  */
8920       cp_parser_abort_tentative_parse (parser);
8921
8922       /* Parse a full expression.  */
8923       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8924     }
8925
8926   /* Go back to evaluating expressions.  */
8927   --skip_evaluation;
8928
8929   /* Restore the old message and the integral constant expression
8930      flags.  */
8931   parser->type_definition_forbidden_message = saved_message;
8932   parser->integral_constant_expression_p
8933     = saved_integral_constant_expression_p;
8934   parser->non_integral_constant_expression_p
8935     = saved_non_integral_constant_expression_p;
8936
8937   if (expr == error_mark_node)
8938     {
8939       /* Skip everything up to the closing `)'.  */
8940       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8941                                              /*consume_paren=*/true);
8942       return error_mark_node;
8943     }
8944   
8945   /* Parse to the closing `)'.  */
8946   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8947     {
8948       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8949                                              /*consume_paren=*/true);
8950       return error_mark_node;
8951     }
8952
8953   return finish_decltype_type (expr, id_expression_or_member_access_p);
8954 }
8955
8956 /* Special member functions [gram.special] */
8957
8958 /* Parse a conversion-function-id.
8959
8960    conversion-function-id:
8961      operator conversion-type-id
8962
8963    Returns an IDENTIFIER_NODE representing the operator.  */
8964
8965 static tree
8966 cp_parser_conversion_function_id (cp_parser* parser)
8967 {
8968   tree type;
8969   tree saved_scope;
8970   tree saved_qualifying_scope;
8971   tree saved_object_scope;
8972   tree pushed_scope = NULL_TREE;
8973
8974   /* Look for the `operator' token.  */
8975   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8976     return error_mark_node;
8977   /* When we parse the conversion-type-id, the current scope will be
8978      reset.  However, we need that information in able to look up the
8979      conversion function later, so we save it here.  */
8980   saved_scope = parser->scope;
8981   saved_qualifying_scope = parser->qualifying_scope;
8982   saved_object_scope = parser->object_scope;
8983   /* We must enter the scope of the class so that the names of
8984      entities declared within the class are available in the
8985      conversion-type-id.  For example, consider:
8986
8987        struct S {
8988          typedef int I;
8989          operator I();
8990        };
8991
8992        S::operator I() { ... }
8993
8994      In order to see that `I' is a type-name in the definition, we
8995      must be in the scope of `S'.  */
8996   if (saved_scope)
8997     pushed_scope = push_scope (saved_scope);
8998   /* Parse the conversion-type-id.  */
8999   type = cp_parser_conversion_type_id (parser);
9000   /* Leave the scope of the class, if any.  */
9001   if (pushed_scope)
9002     pop_scope (pushed_scope);
9003   /* Restore the saved scope.  */
9004   parser->scope = saved_scope;
9005   parser->qualifying_scope = saved_qualifying_scope;
9006   parser->object_scope = saved_object_scope;
9007   /* If the TYPE is invalid, indicate failure.  */
9008   if (type == error_mark_node)
9009     return error_mark_node;
9010   return mangle_conv_op_name_for_type (type);
9011 }
9012
9013 /* Parse a conversion-type-id:
9014
9015    conversion-type-id:
9016      type-specifier-seq conversion-declarator [opt]
9017
9018    Returns the TYPE specified.  */
9019
9020 static tree
9021 cp_parser_conversion_type_id (cp_parser* parser)
9022 {
9023   tree attributes;
9024   cp_decl_specifier_seq type_specifiers;
9025   cp_declarator *declarator;
9026   tree type_specified;
9027
9028   /* Parse the attributes.  */
9029   attributes = cp_parser_attributes_opt (parser);
9030   /* Parse the type-specifiers.  */
9031   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9032                                 &type_specifiers);
9033   /* If that didn't work, stop.  */
9034   if (type_specifiers.type == error_mark_node)
9035     return error_mark_node;
9036   /* Parse the conversion-declarator.  */
9037   declarator = cp_parser_conversion_declarator_opt (parser);
9038
9039   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9040                                     /*initialized=*/0, &attributes);
9041   if (attributes)
9042     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9043
9044   /* Don't give this error when parsing tentatively.  This happens to
9045      work because we always parse this definitively once.  */
9046   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9047       && type_uses_auto (type_specified))
9048     {
9049       error ("invalid use of %<auto%> in conversion operator");
9050       return error_mark_node;
9051     }
9052
9053   return type_specified;
9054 }
9055
9056 /* Parse an (optional) conversion-declarator.
9057
9058    conversion-declarator:
9059      ptr-operator conversion-declarator [opt]
9060
9061    */
9062
9063 static cp_declarator *
9064 cp_parser_conversion_declarator_opt (cp_parser* parser)
9065 {
9066   enum tree_code code;
9067   tree class_type;
9068   cp_cv_quals cv_quals;
9069
9070   /* We don't know if there's a ptr-operator next, or not.  */
9071   cp_parser_parse_tentatively (parser);
9072   /* Try the ptr-operator.  */
9073   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9074   /* If it worked, look for more conversion-declarators.  */
9075   if (cp_parser_parse_definitely (parser))
9076     {
9077       cp_declarator *declarator;
9078
9079       /* Parse another optional declarator.  */
9080       declarator = cp_parser_conversion_declarator_opt (parser);
9081
9082       return cp_parser_make_indirect_declarator
9083         (code, class_type, cv_quals, declarator);
9084    }
9085
9086   return NULL;
9087 }
9088
9089 /* Parse an (optional) ctor-initializer.
9090
9091    ctor-initializer:
9092      : mem-initializer-list
9093
9094    Returns TRUE iff the ctor-initializer was actually present.  */
9095
9096 static bool
9097 cp_parser_ctor_initializer_opt (cp_parser* parser)
9098 {
9099   /* If the next token is not a `:', then there is no
9100      ctor-initializer.  */
9101   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9102     {
9103       /* Do default initialization of any bases and members.  */
9104       if (DECL_CONSTRUCTOR_P (current_function_decl))
9105         finish_mem_initializers (NULL_TREE);
9106
9107       return false;
9108     }
9109
9110   /* Consume the `:' token.  */
9111   cp_lexer_consume_token (parser->lexer);
9112   /* And the mem-initializer-list.  */
9113   cp_parser_mem_initializer_list (parser);
9114
9115   return true;
9116 }
9117
9118 /* Parse a mem-initializer-list.
9119
9120    mem-initializer-list:
9121      mem-initializer ... [opt]
9122      mem-initializer ... [opt] , mem-initializer-list  */
9123
9124 static void
9125 cp_parser_mem_initializer_list (cp_parser* parser)
9126 {
9127   tree mem_initializer_list = NULL_TREE;
9128   cp_token *token = cp_lexer_peek_token (parser->lexer);
9129
9130   /* Let the semantic analysis code know that we are starting the
9131      mem-initializer-list.  */
9132   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9133     error ("%Honly constructors take base initializers",
9134            &token->location);
9135
9136   /* Loop through the list.  */
9137   while (true)
9138     {
9139       tree mem_initializer;
9140
9141       token = cp_lexer_peek_token (parser->lexer);
9142       /* Parse the mem-initializer.  */
9143       mem_initializer = cp_parser_mem_initializer (parser);
9144       /* If the next token is a `...', we're expanding member initializers. */
9145       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9146         {
9147           /* Consume the `...'. */
9148           cp_lexer_consume_token (parser->lexer);
9149
9150           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9151              can be expanded but members cannot. */
9152           if (mem_initializer != error_mark_node
9153               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9154             {
9155               error ("%Hcannot expand initializer for member %<%D%>",
9156                      &token->location, TREE_PURPOSE (mem_initializer));
9157               mem_initializer = error_mark_node;
9158             }
9159
9160           /* Construct the pack expansion type. */
9161           if (mem_initializer != error_mark_node)
9162             mem_initializer = make_pack_expansion (mem_initializer);
9163         }
9164       /* Add it to the list, unless it was erroneous.  */
9165       if (mem_initializer != error_mark_node)
9166         {
9167           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9168           mem_initializer_list = mem_initializer;
9169         }
9170       /* If the next token is not a `,', we're done.  */
9171       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9172         break;
9173       /* Consume the `,' token.  */
9174       cp_lexer_consume_token (parser->lexer);
9175     }
9176
9177   /* Perform semantic analysis.  */
9178   if (DECL_CONSTRUCTOR_P (current_function_decl))
9179     finish_mem_initializers (mem_initializer_list);
9180 }
9181
9182 /* Parse a mem-initializer.
9183
9184    mem-initializer:
9185      mem-initializer-id ( expression-list [opt] )
9186      mem-initializer-id braced-init-list
9187
9188    GNU extension:
9189
9190    mem-initializer:
9191      ( expression-list [opt] )
9192
9193    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9194    class) or FIELD_DECL (for a non-static data member) to initialize;
9195    the TREE_VALUE is the expression-list.  An empty initialization
9196    list is represented by void_list_node.  */
9197
9198 static tree
9199 cp_parser_mem_initializer (cp_parser* parser)
9200 {
9201   tree mem_initializer_id;
9202   tree expression_list;
9203   tree member;
9204   cp_token *token = cp_lexer_peek_token (parser->lexer);
9205
9206   /* Find out what is being initialized.  */
9207   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9208     {
9209       permerror (token->location,
9210                  "anachronistic old-style base class initializer");
9211       mem_initializer_id = NULL_TREE;
9212     }
9213   else
9214     {
9215       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9216       if (mem_initializer_id == error_mark_node)
9217         return mem_initializer_id;
9218     }
9219   member = expand_member_init (mem_initializer_id);
9220   if (member && !DECL_P (member))
9221     in_base_initializer = 1;
9222
9223   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9224     {
9225       bool expr_non_constant_p;
9226       maybe_warn_cpp0x ("extended initializer lists");
9227       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9228       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9229       expression_list = build_tree_list (NULL_TREE, expression_list);
9230     }
9231   else
9232     expression_list
9233       = cp_parser_parenthesized_expression_list (parser, false,
9234                                                  /*cast_p=*/false,
9235                                                  /*allow_expansion_p=*/true,
9236                                                  /*non_constant_p=*/NULL);
9237   if (expression_list == error_mark_node)
9238     return error_mark_node;
9239   if (!expression_list)
9240     expression_list = void_type_node;
9241
9242   in_base_initializer = 0;
9243
9244   return member ? build_tree_list (member, expression_list) : error_mark_node;
9245 }
9246
9247 /* Parse a mem-initializer-id.
9248
9249    mem-initializer-id:
9250      :: [opt] nested-name-specifier [opt] class-name
9251      identifier
9252
9253    Returns a TYPE indicating the class to be initializer for the first
9254    production.  Returns an IDENTIFIER_NODE indicating the data member
9255    to be initialized for the second production.  */
9256
9257 static tree
9258 cp_parser_mem_initializer_id (cp_parser* parser)
9259 {
9260   bool global_scope_p;
9261   bool nested_name_specifier_p;
9262   bool template_p = false;
9263   tree id;
9264
9265   cp_token *token = cp_lexer_peek_token (parser->lexer);
9266
9267   /* `typename' is not allowed in this context ([temp.res]).  */
9268   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9269     {
9270       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9271              "member initializer is implicitly a type)",
9272              &token->location);
9273       cp_lexer_consume_token (parser->lexer);
9274     }
9275   /* Look for the optional `::' operator.  */
9276   global_scope_p
9277     = (cp_parser_global_scope_opt (parser,
9278                                    /*current_scope_valid_p=*/false)
9279        != NULL_TREE);
9280   /* Look for the optional nested-name-specifier.  The simplest way to
9281      implement:
9282
9283        [temp.res]
9284
9285        The keyword `typename' is not permitted in a base-specifier or
9286        mem-initializer; in these contexts a qualified name that
9287        depends on a template-parameter is implicitly assumed to be a
9288        type name.
9289
9290      is to assume that we have seen the `typename' keyword at this
9291      point.  */
9292   nested_name_specifier_p
9293     = (cp_parser_nested_name_specifier_opt (parser,
9294                                             /*typename_keyword_p=*/true,
9295                                             /*check_dependency_p=*/true,
9296                                             /*type_p=*/true,
9297                                             /*is_declaration=*/true)
9298        != NULL_TREE);
9299   if (nested_name_specifier_p)
9300     template_p = cp_parser_optional_template_keyword (parser);
9301   /* If there is a `::' operator or a nested-name-specifier, then we
9302      are definitely looking for a class-name.  */
9303   if (global_scope_p || nested_name_specifier_p)
9304     return cp_parser_class_name (parser,
9305                                  /*typename_keyword_p=*/true,
9306                                  /*template_keyword_p=*/template_p,
9307                                  none_type,
9308                                  /*check_dependency_p=*/true,
9309                                  /*class_head_p=*/false,
9310                                  /*is_declaration=*/true);
9311   /* Otherwise, we could also be looking for an ordinary identifier.  */
9312   cp_parser_parse_tentatively (parser);
9313   /* Try a class-name.  */
9314   id = cp_parser_class_name (parser,
9315                              /*typename_keyword_p=*/true,
9316                              /*template_keyword_p=*/false,
9317                              none_type,
9318                              /*check_dependency_p=*/true,
9319                              /*class_head_p=*/false,
9320                              /*is_declaration=*/true);
9321   /* If we found one, we're done.  */
9322   if (cp_parser_parse_definitely (parser))
9323     return id;
9324   /* Otherwise, look for an ordinary identifier.  */
9325   return cp_parser_identifier (parser);
9326 }
9327
9328 /* Overloading [gram.over] */
9329
9330 /* Parse an operator-function-id.
9331
9332    operator-function-id:
9333      operator operator
9334
9335    Returns an IDENTIFIER_NODE for the operator which is a
9336    human-readable spelling of the identifier, e.g., `operator +'.  */
9337
9338 static tree
9339 cp_parser_operator_function_id (cp_parser* parser)
9340 {
9341   /* Look for the `operator' keyword.  */
9342   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9343     return error_mark_node;
9344   /* And then the name of the operator itself.  */
9345   return cp_parser_operator (parser);
9346 }
9347
9348 /* Parse an operator.
9349
9350    operator:
9351      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9352      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9353      || ++ -- , ->* -> () []
9354
9355    GNU Extensions:
9356
9357    operator:
9358      <? >? <?= >?=
9359
9360    Returns an IDENTIFIER_NODE for the operator which is a
9361    human-readable spelling of the identifier, e.g., `operator +'.  */
9362
9363 static tree
9364 cp_parser_operator (cp_parser* parser)
9365 {
9366   tree id = NULL_TREE;
9367   cp_token *token;
9368
9369   /* Peek at the next token.  */
9370   token = cp_lexer_peek_token (parser->lexer);
9371   /* Figure out which operator we have.  */
9372   switch (token->type)
9373     {
9374     case CPP_KEYWORD:
9375       {
9376         enum tree_code op;
9377
9378         /* The keyword should be either `new' or `delete'.  */
9379         if (token->keyword == RID_NEW)
9380           op = NEW_EXPR;
9381         else if (token->keyword == RID_DELETE)
9382           op = DELETE_EXPR;
9383         else
9384           break;
9385
9386         /* Consume the `new' or `delete' token.  */
9387         cp_lexer_consume_token (parser->lexer);
9388
9389         /* Peek at the next token.  */
9390         token = cp_lexer_peek_token (parser->lexer);
9391         /* If it's a `[' token then this is the array variant of the
9392            operator.  */
9393         if (token->type == CPP_OPEN_SQUARE)
9394           {
9395             /* Consume the `[' token.  */
9396             cp_lexer_consume_token (parser->lexer);
9397             /* Look for the `]' token.  */
9398             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9399             id = ansi_opname (op == NEW_EXPR
9400                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9401           }
9402         /* Otherwise, we have the non-array variant.  */
9403         else
9404           id = ansi_opname (op);
9405
9406         return id;
9407       }
9408
9409     case CPP_PLUS:
9410       id = ansi_opname (PLUS_EXPR);
9411       break;
9412
9413     case CPP_MINUS:
9414       id = ansi_opname (MINUS_EXPR);
9415       break;
9416
9417     case CPP_MULT:
9418       id = ansi_opname (MULT_EXPR);
9419       break;
9420
9421     case CPP_DIV:
9422       id = ansi_opname (TRUNC_DIV_EXPR);
9423       break;
9424
9425     case CPP_MOD:
9426       id = ansi_opname (TRUNC_MOD_EXPR);
9427       break;
9428
9429     case CPP_XOR:
9430       id = ansi_opname (BIT_XOR_EXPR);
9431       break;
9432
9433     case CPP_AND:
9434       id = ansi_opname (BIT_AND_EXPR);
9435       break;
9436
9437     case CPP_OR:
9438       id = ansi_opname (BIT_IOR_EXPR);
9439       break;
9440
9441     case CPP_COMPL:
9442       id = ansi_opname (BIT_NOT_EXPR);
9443       break;
9444
9445     case CPP_NOT:
9446       id = ansi_opname (TRUTH_NOT_EXPR);
9447       break;
9448
9449     case CPP_EQ:
9450       id = ansi_assopname (NOP_EXPR);
9451       break;
9452
9453     case CPP_LESS:
9454       id = ansi_opname (LT_EXPR);
9455       break;
9456
9457     case CPP_GREATER:
9458       id = ansi_opname (GT_EXPR);
9459       break;
9460
9461     case CPP_PLUS_EQ:
9462       id = ansi_assopname (PLUS_EXPR);
9463       break;
9464
9465     case CPP_MINUS_EQ:
9466       id = ansi_assopname (MINUS_EXPR);
9467       break;
9468
9469     case CPP_MULT_EQ:
9470       id = ansi_assopname (MULT_EXPR);
9471       break;
9472
9473     case CPP_DIV_EQ:
9474       id = ansi_assopname (TRUNC_DIV_EXPR);
9475       break;
9476
9477     case CPP_MOD_EQ:
9478       id = ansi_assopname (TRUNC_MOD_EXPR);
9479       break;
9480
9481     case CPP_XOR_EQ:
9482       id = ansi_assopname (BIT_XOR_EXPR);
9483       break;
9484
9485     case CPP_AND_EQ:
9486       id = ansi_assopname (BIT_AND_EXPR);
9487       break;
9488
9489     case CPP_OR_EQ:
9490       id = ansi_assopname (BIT_IOR_EXPR);
9491       break;
9492
9493     case CPP_LSHIFT:
9494       id = ansi_opname (LSHIFT_EXPR);
9495       break;
9496
9497     case CPP_RSHIFT:
9498       id = ansi_opname (RSHIFT_EXPR);
9499       break;
9500
9501     case CPP_LSHIFT_EQ:
9502       id = ansi_assopname (LSHIFT_EXPR);
9503       break;
9504
9505     case CPP_RSHIFT_EQ:
9506       id = ansi_assopname (RSHIFT_EXPR);
9507       break;
9508
9509     case CPP_EQ_EQ:
9510       id = ansi_opname (EQ_EXPR);
9511       break;
9512
9513     case CPP_NOT_EQ:
9514       id = ansi_opname (NE_EXPR);
9515       break;
9516
9517     case CPP_LESS_EQ:
9518       id = ansi_opname (LE_EXPR);
9519       break;
9520
9521     case CPP_GREATER_EQ:
9522       id = ansi_opname (GE_EXPR);
9523       break;
9524
9525     case CPP_AND_AND:
9526       id = ansi_opname (TRUTH_ANDIF_EXPR);
9527       break;
9528
9529     case CPP_OR_OR:
9530       id = ansi_opname (TRUTH_ORIF_EXPR);
9531       break;
9532
9533     case CPP_PLUS_PLUS:
9534       id = ansi_opname (POSTINCREMENT_EXPR);
9535       break;
9536
9537     case CPP_MINUS_MINUS:
9538       id = ansi_opname (PREDECREMENT_EXPR);
9539       break;
9540
9541     case CPP_COMMA:
9542       id = ansi_opname (COMPOUND_EXPR);
9543       break;
9544
9545     case CPP_DEREF_STAR:
9546       id = ansi_opname (MEMBER_REF);
9547       break;
9548
9549     case CPP_DEREF:
9550       id = ansi_opname (COMPONENT_REF);
9551       break;
9552
9553     case CPP_OPEN_PAREN:
9554       /* Consume the `('.  */
9555       cp_lexer_consume_token (parser->lexer);
9556       /* Look for the matching `)'.  */
9557       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9558       return ansi_opname (CALL_EXPR);
9559
9560     case CPP_OPEN_SQUARE:
9561       /* Consume the `['.  */
9562       cp_lexer_consume_token (parser->lexer);
9563       /* Look for the matching `]'.  */
9564       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9565       return ansi_opname (ARRAY_REF);
9566
9567     default:
9568       /* Anything else is an error.  */
9569       break;
9570     }
9571
9572   /* If we have selected an identifier, we need to consume the
9573      operator token.  */
9574   if (id)
9575     cp_lexer_consume_token (parser->lexer);
9576   /* Otherwise, no valid operator name was present.  */
9577   else
9578     {
9579       cp_parser_error (parser, "expected operator");
9580       id = error_mark_node;
9581     }
9582
9583   return id;
9584 }
9585
9586 /* Parse a template-declaration.
9587
9588    template-declaration:
9589      export [opt] template < template-parameter-list > declaration
9590
9591    If MEMBER_P is TRUE, this template-declaration occurs within a
9592    class-specifier.
9593
9594    The grammar rule given by the standard isn't correct.  What
9595    is really meant is:
9596
9597    template-declaration:
9598      export [opt] template-parameter-list-seq
9599        decl-specifier-seq [opt] init-declarator [opt] ;
9600      export [opt] template-parameter-list-seq
9601        function-definition
9602
9603    template-parameter-list-seq:
9604      template-parameter-list-seq [opt]
9605      template < template-parameter-list >  */
9606
9607 static void
9608 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9609 {
9610   /* Check for `export'.  */
9611   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9612     {
9613       /* Consume the `export' token.  */
9614       cp_lexer_consume_token (parser->lexer);
9615       /* Warn that we do not support `export'.  */
9616       warning (0, "keyword %<export%> not implemented, and will be ignored");
9617     }
9618
9619   cp_parser_template_declaration_after_export (parser, member_p);
9620 }
9621
9622 /* Parse a template-parameter-list.
9623
9624    template-parameter-list:
9625      template-parameter
9626      template-parameter-list , template-parameter
9627
9628    Returns a TREE_LIST.  Each node represents a template parameter.
9629    The nodes are connected via their TREE_CHAINs.  */
9630
9631 static tree
9632 cp_parser_template_parameter_list (cp_parser* parser)
9633 {
9634   tree parameter_list = NULL_TREE;
9635
9636   begin_template_parm_list ();
9637   while (true)
9638     {
9639       tree parameter;
9640       bool is_non_type;
9641       bool is_parameter_pack;
9642
9643       /* Parse the template-parameter.  */
9644       parameter = cp_parser_template_parameter (parser, 
9645                                                 &is_non_type,
9646                                                 &is_parameter_pack);
9647       /* Add it to the list.  */
9648       if (parameter != error_mark_node)
9649         parameter_list = process_template_parm (parameter_list,
9650                                                 parameter,
9651                                                 is_non_type,
9652                                                 is_parameter_pack);
9653       else
9654        {
9655          tree err_parm = build_tree_list (parameter, parameter);
9656          TREE_VALUE (err_parm) = error_mark_node;
9657          parameter_list = chainon (parameter_list, err_parm);
9658        }
9659
9660       /* If the next token is not a `,', we're done.  */
9661       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9662         break;
9663       /* Otherwise, consume the `,' token.  */
9664       cp_lexer_consume_token (parser->lexer);
9665     }
9666
9667   return end_template_parm_list (parameter_list);
9668 }
9669
9670 /* Parse a template-parameter.
9671
9672    template-parameter:
9673      type-parameter
9674      parameter-declaration
9675
9676    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9677    the parameter.  The TREE_PURPOSE is the default value, if any.
9678    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9679    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9680    set to true iff this parameter is a parameter pack. */
9681
9682 static tree
9683 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9684                               bool *is_parameter_pack)
9685 {
9686   cp_token *token;
9687   cp_parameter_declarator *parameter_declarator;
9688   cp_declarator *id_declarator;
9689   tree parm;
9690
9691   /* Assume it is a type parameter or a template parameter.  */
9692   *is_non_type = false;
9693   /* Assume it not a parameter pack. */
9694   *is_parameter_pack = false;
9695   /* Peek at the next token.  */
9696   token = cp_lexer_peek_token (parser->lexer);
9697   /* If it is `class' or `template', we have a type-parameter.  */
9698   if (token->keyword == RID_TEMPLATE)
9699     return cp_parser_type_parameter (parser, is_parameter_pack);
9700   /* If it is `class' or `typename' we do not know yet whether it is a
9701      type parameter or a non-type parameter.  Consider:
9702
9703        template <typename T, typename T::X X> ...
9704
9705      or:
9706
9707        template <class C, class D*> ...
9708
9709      Here, the first parameter is a type parameter, and the second is
9710      a non-type parameter.  We can tell by looking at the token after
9711      the identifier -- if it is a `,', `=', or `>' then we have a type
9712      parameter.  */
9713   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9714     {
9715       /* Peek at the token after `class' or `typename'.  */
9716       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9717       /* If it's an ellipsis, we have a template type parameter
9718          pack. */
9719       if (token->type == CPP_ELLIPSIS)
9720         return cp_parser_type_parameter (parser, is_parameter_pack);
9721       /* If it's an identifier, skip it.  */
9722       if (token->type == CPP_NAME)
9723         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9724       /* Now, see if the token looks like the end of a template
9725          parameter.  */
9726       if (token->type == CPP_COMMA
9727           || token->type == CPP_EQ
9728           || token->type == CPP_GREATER)
9729         return cp_parser_type_parameter (parser, is_parameter_pack);
9730     }
9731
9732   /* Otherwise, it is a non-type parameter.
9733
9734      [temp.param]
9735
9736      When parsing a default template-argument for a non-type
9737      template-parameter, the first non-nested `>' is taken as the end
9738      of the template parameter-list rather than a greater-than
9739      operator.  */
9740   *is_non_type = true;
9741   parameter_declarator
9742      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9743                                         /*parenthesized_p=*/NULL);
9744
9745   /* If the parameter declaration is marked as a parameter pack, set
9746      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9747      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9748      grokdeclarator. */
9749   if (parameter_declarator
9750       && parameter_declarator->declarator
9751       && parameter_declarator->declarator->parameter_pack_p)
9752     {
9753       *is_parameter_pack = true;
9754       parameter_declarator->declarator->parameter_pack_p = false;
9755     }
9756
9757   /* If the next token is an ellipsis, and we don't already have it
9758      marked as a parameter pack, then we have a parameter pack (that
9759      has no declarator).  */
9760   if (!*is_parameter_pack
9761       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9762       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9763     {
9764       /* Consume the `...'.  */
9765       cp_lexer_consume_token (parser->lexer);
9766       maybe_warn_variadic_templates ();
9767       
9768       *is_parameter_pack = true;
9769     }
9770   /* We might end up with a pack expansion as the type of the non-type
9771      template parameter, in which case this is a non-type template
9772      parameter pack.  */
9773   else if (parameter_declarator
9774            && parameter_declarator->decl_specifiers.type
9775            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9776     {
9777       *is_parameter_pack = true;
9778       parameter_declarator->decl_specifiers.type = 
9779         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9780     }
9781
9782   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9783     {
9784       /* Parameter packs cannot have default arguments.  However, a
9785          user may try to do so, so we'll parse them and give an
9786          appropriate diagnostic here.  */
9787
9788       /* Consume the `='.  */
9789       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9790       cp_lexer_consume_token (parser->lexer);
9791       
9792       /* Find the name of the parameter pack.  */     
9793       id_declarator = parameter_declarator->declarator;
9794       while (id_declarator && id_declarator->kind != cdk_id)
9795         id_declarator = id_declarator->declarator;
9796       
9797       if (id_declarator && id_declarator->kind == cdk_id)
9798         error ("%Htemplate parameter pack %qD cannot have a default argument",
9799                &start_token->location, id_declarator->u.id.unqualified_name);
9800       else
9801         error ("%Htemplate parameter pack cannot have a default argument",
9802                &start_token->location);
9803       
9804       /* Parse the default argument, but throw away the result.  */
9805       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9806     }
9807
9808   parm = grokdeclarator (parameter_declarator->declarator,
9809                          &parameter_declarator->decl_specifiers,
9810                          PARM, /*initialized=*/0,
9811                          /*attrlist=*/NULL);
9812   if (parm == error_mark_node)
9813     return error_mark_node;
9814
9815   return build_tree_list (parameter_declarator->default_argument, parm);
9816 }
9817
9818 /* Parse a type-parameter.
9819
9820    type-parameter:
9821      class identifier [opt]
9822      class identifier [opt] = type-id
9823      typename identifier [opt]
9824      typename identifier [opt] = type-id
9825      template < template-parameter-list > class identifier [opt]
9826      template < template-parameter-list > class identifier [opt]
9827        = id-expression
9828
9829    GNU Extension (variadic templates):
9830
9831    type-parameter:
9832      class ... identifier [opt]
9833      typename ... identifier [opt]
9834
9835    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9836    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9837    the declaration of the parameter.
9838
9839    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9840
9841 static tree
9842 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9843 {
9844   cp_token *token;
9845   tree parameter;
9846
9847   /* Look for a keyword to tell us what kind of parameter this is.  */
9848   token = cp_parser_require (parser, CPP_KEYWORD,
9849                              "%<class%>, %<typename%>, or %<template%>");
9850   if (!token)
9851     return error_mark_node;
9852
9853   switch (token->keyword)
9854     {
9855     case RID_CLASS:
9856     case RID_TYPENAME:
9857       {
9858         tree identifier;
9859         tree default_argument;
9860
9861         /* If the next token is an ellipsis, we have a template
9862            argument pack. */
9863         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9864           {
9865             /* Consume the `...' token. */
9866             cp_lexer_consume_token (parser->lexer);
9867             maybe_warn_variadic_templates ();
9868
9869             *is_parameter_pack = true;
9870           }
9871
9872         /* If the next token is an identifier, then it names the
9873            parameter.  */
9874         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9875           identifier = cp_parser_identifier (parser);
9876         else
9877           identifier = NULL_TREE;
9878
9879         /* Create the parameter.  */
9880         parameter = finish_template_type_parm (class_type_node, identifier);
9881
9882         /* If the next token is an `=', we have a default argument.  */
9883         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9884           {
9885             /* Consume the `=' token.  */
9886             cp_lexer_consume_token (parser->lexer);
9887             /* Parse the default-argument.  */
9888             push_deferring_access_checks (dk_no_deferred);
9889             default_argument = cp_parser_type_id (parser);
9890
9891             /* Template parameter packs cannot have default
9892                arguments. */
9893             if (*is_parameter_pack)
9894               {
9895                 if (identifier)
9896                   error ("%Htemplate parameter pack %qD cannot have a "
9897                          "default argument", &token->location, identifier);
9898                 else
9899                   error ("%Htemplate parameter packs cannot have "
9900                          "default arguments", &token->location);
9901                 default_argument = NULL_TREE;
9902               }
9903             pop_deferring_access_checks ();
9904           }
9905         else
9906           default_argument = NULL_TREE;
9907
9908         /* Create the combined representation of the parameter and the
9909            default argument.  */
9910         parameter = build_tree_list (default_argument, parameter);
9911       }
9912       break;
9913
9914     case RID_TEMPLATE:
9915       {
9916         tree parameter_list;
9917         tree identifier;
9918         tree default_argument;
9919
9920         /* Look for the `<'.  */
9921         cp_parser_require (parser, CPP_LESS, "%<<%>");
9922         /* Parse the template-parameter-list.  */
9923         parameter_list = cp_parser_template_parameter_list (parser);
9924         /* Look for the `>'.  */
9925         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9926         /* Look for the `class' keyword.  */
9927         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9928         /* If the next token is an ellipsis, we have a template
9929            argument pack. */
9930         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9931           {
9932             /* Consume the `...' token. */
9933             cp_lexer_consume_token (parser->lexer);
9934             maybe_warn_variadic_templates ();
9935
9936             *is_parameter_pack = true;
9937           }
9938         /* If the next token is an `=', then there is a
9939            default-argument.  If the next token is a `>', we are at
9940            the end of the parameter-list.  If the next token is a `,',
9941            then we are at the end of this parameter.  */
9942         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9943             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9944             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9945           {
9946             identifier = cp_parser_identifier (parser);
9947             /* Treat invalid names as if the parameter were nameless.  */
9948             if (identifier == error_mark_node)
9949               identifier = NULL_TREE;
9950           }
9951         else
9952           identifier = NULL_TREE;
9953
9954         /* Create the template parameter.  */
9955         parameter = finish_template_template_parm (class_type_node,
9956                                                    identifier);
9957
9958         /* If the next token is an `=', then there is a
9959            default-argument.  */
9960         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9961           {
9962             bool is_template;
9963
9964             /* Consume the `='.  */
9965             cp_lexer_consume_token (parser->lexer);
9966             /* Parse the id-expression.  */
9967             push_deferring_access_checks (dk_no_deferred);
9968             /* save token before parsing the id-expression, for error
9969                reporting */
9970             token = cp_lexer_peek_token (parser->lexer);
9971             default_argument
9972               = cp_parser_id_expression (parser,
9973                                          /*template_keyword_p=*/false,
9974                                          /*check_dependency_p=*/true,
9975                                          /*template_p=*/&is_template,
9976                                          /*declarator_p=*/false,
9977                                          /*optional_p=*/false);
9978             if (TREE_CODE (default_argument) == TYPE_DECL)
9979               /* If the id-expression was a template-id that refers to
9980                  a template-class, we already have the declaration here,
9981                  so no further lookup is needed.  */
9982                  ;
9983             else
9984               /* Look up the name.  */
9985               default_argument
9986                 = cp_parser_lookup_name (parser, default_argument,
9987                                          none_type,
9988                                          /*is_template=*/is_template,
9989                                          /*is_namespace=*/false,
9990                                          /*check_dependency=*/true,
9991                                          /*ambiguous_decls=*/NULL,
9992                                          token->location);
9993             /* See if the default argument is valid.  */
9994             default_argument
9995               = check_template_template_default_arg (default_argument);
9996
9997             /* Template parameter packs cannot have default
9998                arguments. */
9999             if (*is_parameter_pack)
10000               {
10001                 if (identifier)
10002                   error ("%Htemplate parameter pack %qD cannot "
10003                          "have a default argument",
10004                          &token->location, identifier);
10005                 else
10006                   error ("%Htemplate parameter packs cannot "
10007                          "have default arguments",
10008                          &token->location);
10009                 default_argument = NULL_TREE;
10010               }
10011             pop_deferring_access_checks ();
10012           }
10013         else
10014           default_argument = NULL_TREE;
10015
10016         /* Create the combined representation of the parameter and the
10017            default argument.  */
10018         parameter = build_tree_list (default_argument, parameter);
10019       }
10020       break;
10021
10022     default:
10023       gcc_unreachable ();
10024       break;
10025     }
10026
10027   return parameter;
10028 }
10029
10030 /* Parse a template-id.
10031
10032    template-id:
10033      template-name < template-argument-list [opt] >
10034
10035    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10036    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10037    returned.  Otherwise, if the template-name names a function, or set
10038    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10039    names a class, returns a TYPE_DECL for the specialization.
10040
10041    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10042    uninstantiated templates.  */
10043
10044 static tree
10045 cp_parser_template_id (cp_parser *parser,
10046                        bool template_keyword_p,
10047                        bool check_dependency_p,
10048                        bool is_declaration)
10049 {
10050   int i;
10051   tree templ;
10052   tree arguments;
10053   tree template_id;
10054   cp_token_position start_of_id = 0;
10055   deferred_access_check *chk;
10056   VEC (deferred_access_check,gc) *access_check;
10057   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10058   bool is_identifier;
10059
10060   /* If the next token corresponds to a template-id, there is no need
10061      to reparse it.  */
10062   next_token = cp_lexer_peek_token (parser->lexer);
10063   if (next_token->type == CPP_TEMPLATE_ID)
10064     {
10065       struct tree_check *check_value;
10066
10067       /* Get the stored value.  */
10068       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10069       /* Perform any access checks that were deferred.  */
10070       access_check = check_value->checks;
10071       if (access_check)
10072         {
10073           for (i = 0 ;
10074                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10075                ++i)
10076             {
10077               perform_or_defer_access_check (chk->binfo,
10078                                              chk->decl,
10079                                              chk->diag_decl);
10080             }
10081         }
10082       /* Return the stored value.  */
10083       return check_value->value;
10084     }
10085
10086   /* Avoid performing name lookup if there is no possibility of
10087      finding a template-id.  */
10088   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10089       || (next_token->type == CPP_NAME
10090           && !cp_parser_nth_token_starts_template_argument_list_p
10091                (parser, 2)))
10092     {
10093       cp_parser_error (parser, "expected template-id");
10094       return error_mark_node;
10095     }
10096
10097   /* Remember where the template-id starts.  */
10098   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10099     start_of_id = cp_lexer_token_position (parser->lexer, false);
10100
10101   push_deferring_access_checks (dk_deferred);
10102
10103   /* Parse the template-name.  */
10104   is_identifier = false;
10105   token = cp_lexer_peek_token (parser->lexer);
10106   templ = cp_parser_template_name (parser, template_keyword_p,
10107                                    check_dependency_p,
10108                                    is_declaration,
10109                                    &is_identifier);
10110   if (templ == error_mark_node || is_identifier)
10111     {
10112       pop_deferring_access_checks ();
10113       return templ;
10114     }
10115
10116   /* If we find the sequence `[:' after a template-name, it's probably
10117      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10118      parse correctly the argument list.  */
10119   next_token = cp_lexer_peek_token (parser->lexer);
10120   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10121   if (next_token->type == CPP_OPEN_SQUARE
10122       && next_token->flags & DIGRAPH
10123       && next_token_2->type == CPP_COLON
10124       && !(next_token_2->flags & PREV_WHITE))
10125     {
10126       cp_parser_parse_tentatively (parser);
10127       /* Change `:' into `::'.  */
10128       next_token_2->type = CPP_SCOPE;
10129       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10130          CPP_LESS.  */
10131       cp_lexer_consume_token (parser->lexer);
10132
10133       /* Parse the arguments.  */
10134       arguments = cp_parser_enclosed_template_argument_list (parser);
10135       if (!cp_parser_parse_definitely (parser))
10136         {
10137           /* If we couldn't parse an argument list, then we revert our changes
10138              and return simply an error. Maybe this is not a template-id
10139              after all.  */
10140           next_token_2->type = CPP_COLON;
10141           cp_parser_error (parser, "expected %<<%>");
10142           pop_deferring_access_checks ();
10143           return error_mark_node;
10144         }
10145       /* Otherwise, emit an error about the invalid digraph, but continue
10146          parsing because we got our argument list.  */
10147       if (permerror (next_token->location,
10148                      "%<<::%> cannot begin a template-argument list"))
10149         {
10150           static bool hint = false;
10151           inform (next_token->location,
10152                   "%<<:%> is an alternate spelling for %<[%>."
10153                   " Insert whitespace between %<<%> and %<::%>");
10154           if (!hint && !flag_permissive)
10155             {
10156               inform (next_token->location, "(if you use %<-fpermissive%>"
10157                       " G++ will accept your code)");
10158               hint = true;
10159             }
10160         }
10161     }
10162   else
10163     {
10164       /* Look for the `<' that starts the template-argument-list.  */
10165       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10166         {
10167           pop_deferring_access_checks ();
10168           return error_mark_node;
10169         }
10170       /* Parse the arguments.  */
10171       arguments = cp_parser_enclosed_template_argument_list (parser);
10172     }
10173
10174   /* Build a representation of the specialization.  */
10175   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10176     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10177   else if (DECL_CLASS_TEMPLATE_P (templ)
10178            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10179     {
10180       bool entering_scope;
10181       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10182          template (rather than some instantiation thereof) only if
10183          is not nested within some other construct.  For example, in
10184          "template <typename T> void f(T) { A<T>::", A<T> is just an
10185          instantiation of A.  */
10186       entering_scope = (template_parm_scope_p ()
10187                         && cp_lexer_next_token_is (parser->lexer,
10188                                                    CPP_SCOPE));
10189       template_id
10190         = finish_template_type (templ, arguments, entering_scope);
10191     }
10192   else
10193     {
10194       /* If it's not a class-template or a template-template, it should be
10195          a function-template.  */
10196       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10197                    || TREE_CODE (templ) == OVERLOAD
10198                    || BASELINK_P (templ)));
10199
10200       template_id = lookup_template_function (templ, arguments);
10201     }
10202
10203   /* If parsing tentatively, replace the sequence of tokens that makes
10204      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10205      should we re-parse the token stream, we will not have to repeat
10206      the effort required to do the parse, nor will we issue duplicate
10207      error messages about problems during instantiation of the
10208      template.  */
10209   if (start_of_id)
10210     {
10211       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10212
10213       /* Reset the contents of the START_OF_ID token.  */
10214       token->type = CPP_TEMPLATE_ID;
10215       /* Retrieve any deferred checks.  Do not pop this access checks yet
10216          so the memory will not be reclaimed during token replacing below.  */
10217       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10218       token->u.tree_check_value->value = template_id;
10219       token->u.tree_check_value->checks = get_deferred_access_checks ();
10220       token->keyword = RID_MAX;
10221
10222       /* Purge all subsequent tokens.  */
10223       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10224
10225       /* ??? Can we actually assume that, if template_id ==
10226          error_mark_node, we will have issued a diagnostic to the
10227          user, as opposed to simply marking the tentative parse as
10228          failed?  */
10229       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10230         error ("%Hparse error in template argument list",
10231                &token->location);
10232     }
10233
10234   pop_deferring_access_checks ();
10235   return template_id;
10236 }
10237
10238 /* Parse a template-name.
10239
10240    template-name:
10241      identifier
10242
10243    The standard should actually say:
10244
10245    template-name:
10246      identifier
10247      operator-function-id
10248
10249    A defect report has been filed about this issue.
10250
10251    A conversion-function-id cannot be a template name because they cannot
10252    be part of a template-id. In fact, looking at this code:
10253
10254    a.operator K<int>()
10255
10256    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10257    It is impossible to call a templated conversion-function-id with an
10258    explicit argument list, since the only allowed template parameter is
10259    the type to which it is converting.
10260
10261    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10262    `template' keyword, in a construction like:
10263
10264      T::template f<3>()
10265
10266    In that case `f' is taken to be a template-name, even though there
10267    is no way of knowing for sure.
10268
10269    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10270    name refers to a set of overloaded functions, at least one of which
10271    is a template, or an IDENTIFIER_NODE with the name of the template,
10272    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10273    names are looked up inside uninstantiated templates.  */
10274
10275 static tree
10276 cp_parser_template_name (cp_parser* parser,
10277                          bool template_keyword_p,
10278                          bool check_dependency_p,
10279                          bool is_declaration,
10280                          bool *is_identifier)
10281 {
10282   tree identifier;
10283   tree decl;
10284   tree fns;
10285   cp_token *token = cp_lexer_peek_token (parser->lexer);
10286
10287   /* If the next token is `operator', then we have either an
10288      operator-function-id or a conversion-function-id.  */
10289   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10290     {
10291       /* We don't know whether we're looking at an
10292          operator-function-id or a conversion-function-id.  */
10293       cp_parser_parse_tentatively (parser);
10294       /* Try an operator-function-id.  */
10295       identifier = cp_parser_operator_function_id (parser);
10296       /* If that didn't work, try a conversion-function-id.  */
10297       if (!cp_parser_parse_definitely (parser))
10298         {
10299           cp_parser_error (parser, "expected template-name");
10300           return error_mark_node;
10301         }
10302     }
10303   /* Look for the identifier.  */
10304   else
10305     identifier = cp_parser_identifier (parser);
10306
10307   /* If we didn't find an identifier, we don't have a template-id.  */
10308   if (identifier == error_mark_node)
10309     return error_mark_node;
10310
10311   /* If the name immediately followed the `template' keyword, then it
10312      is a template-name.  However, if the next token is not `<', then
10313      we do not treat it as a template-name, since it is not being used
10314      as part of a template-id.  This enables us to handle constructs
10315      like:
10316
10317        template <typename T> struct S { S(); };
10318        template <typename T> S<T>::S();
10319
10320      correctly.  We would treat `S' as a template -- if it were `S<T>'
10321      -- but we do not if there is no `<'.  */
10322
10323   if (processing_template_decl
10324       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10325     {
10326       /* In a declaration, in a dependent context, we pretend that the
10327          "template" keyword was present in order to improve error
10328          recovery.  For example, given:
10329
10330            template <typename T> void f(T::X<int>);
10331
10332          we want to treat "X<int>" as a template-id.  */
10333       if (is_declaration
10334           && !template_keyword_p
10335           && parser->scope && TYPE_P (parser->scope)
10336           && check_dependency_p
10337           && dependent_scope_p (parser->scope)
10338           /* Do not do this for dtors (or ctors), since they never
10339              need the template keyword before their name.  */
10340           && !constructor_name_p (identifier, parser->scope))
10341         {
10342           cp_token_position start = 0;
10343
10344           /* Explain what went wrong.  */
10345           error ("%Hnon-template %qD used as template",
10346                  &token->location, identifier);
10347           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10348                   parser->scope, identifier);
10349           /* If parsing tentatively, find the location of the "<" token.  */
10350           if (cp_parser_simulate_error (parser))
10351             start = cp_lexer_token_position (parser->lexer, true);
10352           /* Parse the template arguments so that we can issue error
10353              messages about them.  */
10354           cp_lexer_consume_token (parser->lexer);
10355           cp_parser_enclosed_template_argument_list (parser);
10356           /* Skip tokens until we find a good place from which to
10357              continue parsing.  */
10358           cp_parser_skip_to_closing_parenthesis (parser,
10359                                                  /*recovering=*/true,
10360                                                  /*or_comma=*/true,
10361                                                  /*consume_paren=*/false);
10362           /* If parsing tentatively, permanently remove the
10363              template argument list.  That will prevent duplicate
10364              error messages from being issued about the missing
10365              "template" keyword.  */
10366           if (start)
10367             cp_lexer_purge_tokens_after (parser->lexer, start);
10368           if (is_identifier)
10369             *is_identifier = true;
10370           return identifier;
10371         }
10372
10373       /* If the "template" keyword is present, then there is generally
10374          no point in doing name-lookup, so we just return IDENTIFIER.
10375          But, if the qualifying scope is non-dependent then we can
10376          (and must) do name-lookup normally.  */
10377       if (template_keyword_p
10378           && (!parser->scope
10379               || (TYPE_P (parser->scope)
10380                   && dependent_type_p (parser->scope))))
10381         return identifier;
10382     }
10383
10384   /* Look up the name.  */
10385   decl = cp_parser_lookup_name (parser, identifier,
10386                                 none_type,
10387                                 /*is_template=*/false,
10388                                 /*is_namespace=*/false,
10389                                 check_dependency_p,
10390                                 /*ambiguous_decls=*/NULL,
10391                                 token->location);
10392   decl = maybe_get_template_decl_from_type_decl (decl);
10393
10394   /* If DECL is a template, then the name was a template-name.  */
10395   if (TREE_CODE (decl) == TEMPLATE_DECL)
10396     ;
10397   else
10398     {
10399       tree fn = NULL_TREE;
10400
10401       /* The standard does not explicitly indicate whether a name that
10402          names a set of overloaded declarations, some of which are
10403          templates, is a template-name.  However, such a name should
10404          be a template-name; otherwise, there is no way to form a
10405          template-id for the overloaded templates.  */
10406       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10407       if (TREE_CODE (fns) == OVERLOAD)
10408         for (fn = fns; fn; fn = OVL_NEXT (fn))
10409           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10410             break;
10411
10412       if (!fn)
10413         {
10414           /* The name does not name a template.  */
10415           cp_parser_error (parser, "expected template-name");
10416           return error_mark_node;
10417         }
10418     }
10419
10420   /* If DECL is dependent, and refers to a function, then just return
10421      its name; we will look it up again during template instantiation.  */
10422   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10423     {
10424       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10425       if (TYPE_P (scope) && dependent_type_p (scope))
10426         return identifier;
10427     }
10428
10429   return decl;
10430 }
10431
10432 /* Parse a template-argument-list.
10433
10434    template-argument-list:
10435      template-argument ... [opt]
10436      template-argument-list , template-argument ... [opt]
10437
10438    Returns a TREE_VEC containing the arguments.  */
10439
10440 static tree
10441 cp_parser_template_argument_list (cp_parser* parser)
10442 {
10443   tree fixed_args[10];
10444   unsigned n_args = 0;
10445   unsigned alloced = 10;
10446   tree *arg_ary = fixed_args;
10447   tree vec;
10448   bool saved_in_template_argument_list_p;
10449   bool saved_ice_p;
10450   bool saved_non_ice_p;
10451
10452   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10453   parser->in_template_argument_list_p = true;
10454   /* Even if the template-id appears in an integral
10455      constant-expression, the contents of the argument list do
10456      not.  */
10457   saved_ice_p = parser->integral_constant_expression_p;
10458   parser->integral_constant_expression_p = false;
10459   saved_non_ice_p = parser->non_integral_constant_expression_p;
10460   parser->non_integral_constant_expression_p = false;
10461   /* Parse the arguments.  */
10462   do
10463     {
10464       tree argument;
10465
10466       if (n_args)
10467         /* Consume the comma.  */
10468         cp_lexer_consume_token (parser->lexer);
10469
10470       /* Parse the template-argument.  */
10471       argument = cp_parser_template_argument (parser);
10472
10473       /* If the next token is an ellipsis, we're expanding a template
10474          argument pack. */
10475       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10476         {
10477           /* Consume the `...' token. */
10478           cp_lexer_consume_token (parser->lexer);
10479
10480           /* Make the argument into a TYPE_PACK_EXPANSION or
10481              EXPR_PACK_EXPANSION. */
10482           argument = make_pack_expansion (argument);
10483         }
10484
10485       if (n_args == alloced)
10486         {
10487           alloced *= 2;
10488
10489           if (arg_ary == fixed_args)
10490             {
10491               arg_ary = XNEWVEC (tree, alloced);
10492               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10493             }
10494           else
10495             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10496         }
10497       arg_ary[n_args++] = argument;
10498     }
10499   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10500
10501   vec = make_tree_vec (n_args);
10502
10503   while (n_args--)
10504     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10505
10506   if (arg_ary != fixed_args)
10507     free (arg_ary);
10508   parser->non_integral_constant_expression_p = saved_non_ice_p;
10509   parser->integral_constant_expression_p = saved_ice_p;
10510   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10511   return vec;
10512 }
10513
10514 /* Parse a template-argument.
10515
10516    template-argument:
10517      assignment-expression
10518      type-id
10519      id-expression
10520
10521    The representation is that of an assignment-expression, type-id, or
10522    id-expression -- except that the qualified id-expression is
10523    evaluated, so that the value returned is either a DECL or an
10524    OVERLOAD.
10525
10526    Although the standard says "assignment-expression", it forbids
10527    throw-expressions or assignments in the template argument.
10528    Therefore, we use "conditional-expression" instead.  */
10529
10530 static tree
10531 cp_parser_template_argument (cp_parser* parser)
10532 {
10533   tree argument;
10534   bool template_p;
10535   bool address_p;
10536   bool maybe_type_id = false;
10537   cp_token *token = NULL, *argument_start_token = NULL;
10538   cp_id_kind idk;
10539
10540   /* There's really no way to know what we're looking at, so we just
10541      try each alternative in order.
10542
10543        [temp.arg]
10544
10545        In a template-argument, an ambiguity between a type-id and an
10546        expression is resolved to a type-id, regardless of the form of
10547        the corresponding template-parameter.
10548
10549      Therefore, we try a type-id first.  */
10550   cp_parser_parse_tentatively (parser);
10551   argument = cp_parser_template_type_arg (parser);
10552   /* If there was no error parsing the type-id but the next token is a
10553      '>>', our behavior depends on which dialect of C++ we're
10554      parsing. In C++98, we probably found a typo for '> >'. But there
10555      are type-id which are also valid expressions. For instance:
10556
10557      struct X { int operator >> (int); };
10558      template <int V> struct Foo {};
10559      Foo<X () >> 5> r;
10560
10561      Here 'X()' is a valid type-id of a function type, but the user just
10562      wanted to write the expression "X() >> 5". Thus, we remember that we
10563      found a valid type-id, but we still try to parse the argument as an
10564      expression to see what happens. 
10565
10566      In C++0x, the '>>' will be considered two separate '>'
10567      tokens.  */
10568   if (!cp_parser_error_occurred (parser)
10569       && cxx_dialect == cxx98
10570       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10571     {
10572       maybe_type_id = true;
10573       cp_parser_abort_tentative_parse (parser);
10574     }
10575   else
10576     {
10577       /* If the next token isn't a `,' or a `>', then this argument wasn't
10578       really finished. This means that the argument is not a valid
10579       type-id.  */
10580       if (!cp_parser_next_token_ends_template_argument_p (parser))
10581         cp_parser_error (parser, "expected template-argument");
10582       /* If that worked, we're done.  */
10583       if (cp_parser_parse_definitely (parser))
10584         return argument;
10585     }
10586   /* We're still not sure what the argument will be.  */
10587   cp_parser_parse_tentatively (parser);
10588   /* Try a template.  */
10589   argument_start_token = cp_lexer_peek_token (parser->lexer);
10590   argument = cp_parser_id_expression (parser,
10591                                       /*template_keyword_p=*/false,
10592                                       /*check_dependency_p=*/true,
10593                                       &template_p,
10594                                       /*declarator_p=*/false,
10595                                       /*optional_p=*/false);
10596   /* If the next token isn't a `,' or a `>', then this argument wasn't
10597      really finished.  */
10598   if (!cp_parser_next_token_ends_template_argument_p (parser))
10599     cp_parser_error (parser, "expected template-argument");
10600   if (!cp_parser_error_occurred (parser))
10601     {
10602       /* Figure out what is being referred to.  If the id-expression
10603          was for a class template specialization, then we will have a
10604          TYPE_DECL at this point.  There is no need to do name lookup
10605          at this point in that case.  */
10606       if (TREE_CODE (argument) != TYPE_DECL)
10607         argument = cp_parser_lookup_name (parser, argument,
10608                                           none_type,
10609                                           /*is_template=*/template_p,
10610                                           /*is_namespace=*/false,
10611                                           /*check_dependency=*/true,
10612                                           /*ambiguous_decls=*/NULL,
10613                                           argument_start_token->location);
10614       if (TREE_CODE (argument) != TEMPLATE_DECL
10615           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10616         cp_parser_error (parser, "expected template-name");
10617     }
10618   if (cp_parser_parse_definitely (parser))
10619     return argument;
10620   /* It must be a non-type argument.  There permitted cases are given
10621      in [temp.arg.nontype]:
10622
10623      -- an integral constant-expression of integral or enumeration
10624         type; or
10625
10626      -- the name of a non-type template-parameter; or
10627
10628      -- the name of an object or function with external linkage...
10629
10630      -- the address of an object or function with external linkage...
10631
10632      -- a pointer to member...  */
10633   /* Look for a non-type template parameter.  */
10634   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10635     {
10636       cp_parser_parse_tentatively (parser);
10637       argument = cp_parser_primary_expression (parser,
10638                                                /*address_p=*/false,
10639                                                /*cast_p=*/false,
10640                                                /*template_arg_p=*/true,
10641                                                &idk);
10642       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10643           || !cp_parser_next_token_ends_template_argument_p (parser))
10644         cp_parser_simulate_error (parser);
10645       if (cp_parser_parse_definitely (parser))
10646         return argument;
10647     }
10648
10649   /* If the next token is "&", the argument must be the address of an
10650      object or function with external linkage.  */
10651   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10652   if (address_p)
10653     cp_lexer_consume_token (parser->lexer);
10654   /* See if we might have an id-expression.  */
10655   token = cp_lexer_peek_token (parser->lexer);
10656   if (token->type == CPP_NAME
10657       || token->keyword == RID_OPERATOR
10658       || token->type == CPP_SCOPE
10659       || token->type == CPP_TEMPLATE_ID
10660       || token->type == CPP_NESTED_NAME_SPECIFIER)
10661     {
10662       cp_parser_parse_tentatively (parser);
10663       argument = cp_parser_primary_expression (parser,
10664                                                address_p,
10665                                                /*cast_p=*/false,
10666                                                /*template_arg_p=*/true,
10667                                                &idk);
10668       if (cp_parser_error_occurred (parser)
10669           || !cp_parser_next_token_ends_template_argument_p (parser))
10670         cp_parser_abort_tentative_parse (parser);
10671       else
10672         {
10673           if (TREE_CODE (argument) == INDIRECT_REF)
10674             {
10675               gcc_assert (REFERENCE_REF_P (argument));
10676               argument = TREE_OPERAND (argument, 0);
10677             }
10678
10679           if (TREE_CODE (argument) == VAR_DECL)
10680             {
10681               /* A variable without external linkage might still be a
10682                  valid constant-expression, so no error is issued here
10683                  if the external-linkage check fails.  */
10684               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10685                 cp_parser_simulate_error (parser);
10686             }
10687           else if (is_overloaded_fn (argument))
10688             /* All overloaded functions are allowed; if the external
10689                linkage test does not pass, an error will be issued
10690                later.  */
10691             ;
10692           else if (address_p
10693                    && (TREE_CODE (argument) == OFFSET_REF
10694                        || TREE_CODE (argument) == SCOPE_REF))
10695             /* A pointer-to-member.  */
10696             ;
10697           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10698             ;
10699           else
10700             cp_parser_simulate_error (parser);
10701
10702           if (cp_parser_parse_definitely (parser))
10703             {
10704               if (address_p)
10705                 argument = build_x_unary_op (ADDR_EXPR, argument,
10706                                              tf_warning_or_error);
10707               return argument;
10708             }
10709         }
10710     }
10711   /* If the argument started with "&", there are no other valid
10712      alternatives at this point.  */
10713   if (address_p)
10714     {
10715       cp_parser_error (parser, "invalid non-type template argument");
10716       return error_mark_node;
10717     }
10718
10719   /* If the argument wasn't successfully parsed as a type-id followed
10720      by '>>', the argument can only be a constant expression now.
10721      Otherwise, we try parsing the constant-expression tentatively,
10722      because the argument could really be a type-id.  */
10723   if (maybe_type_id)
10724     cp_parser_parse_tentatively (parser);
10725   argument = cp_parser_constant_expression (parser,
10726                                             /*allow_non_constant_p=*/false,
10727                                             /*non_constant_p=*/NULL);
10728   argument = fold_non_dependent_expr (argument);
10729   if (!maybe_type_id)
10730     return argument;
10731   if (!cp_parser_next_token_ends_template_argument_p (parser))
10732     cp_parser_error (parser, "expected template-argument");
10733   if (cp_parser_parse_definitely (parser))
10734     return argument;
10735   /* We did our best to parse the argument as a non type-id, but that
10736      was the only alternative that matched (albeit with a '>' after
10737      it). We can assume it's just a typo from the user, and a
10738      diagnostic will then be issued.  */
10739   return cp_parser_template_type_arg (parser);
10740 }
10741
10742 /* Parse an explicit-instantiation.
10743
10744    explicit-instantiation:
10745      template declaration
10746
10747    Although the standard says `declaration', what it really means is:
10748
10749    explicit-instantiation:
10750      template decl-specifier-seq [opt] declarator [opt] ;
10751
10752    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10753    supposed to be allowed.  A defect report has been filed about this
10754    issue.
10755
10756    GNU Extension:
10757
10758    explicit-instantiation:
10759      storage-class-specifier template
10760        decl-specifier-seq [opt] declarator [opt] ;
10761      function-specifier template
10762        decl-specifier-seq [opt] declarator [opt] ;  */
10763
10764 static void
10765 cp_parser_explicit_instantiation (cp_parser* parser)
10766 {
10767   int declares_class_or_enum;
10768   cp_decl_specifier_seq decl_specifiers;
10769   tree extension_specifier = NULL_TREE;
10770   cp_token *token;
10771
10772   /* Look for an (optional) storage-class-specifier or
10773      function-specifier.  */
10774   if (cp_parser_allow_gnu_extensions_p (parser))
10775     {
10776       extension_specifier
10777         = cp_parser_storage_class_specifier_opt (parser);
10778       if (!extension_specifier)
10779         extension_specifier
10780           = cp_parser_function_specifier_opt (parser,
10781                                               /*decl_specs=*/NULL);
10782     }
10783
10784   /* Look for the `template' keyword.  */
10785   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10786   /* Let the front end know that we are processing an explicit
10787      instantiation.  */
10788   begin_explicit_instantiation ();
10789   /* [temp.explicit] says that we are supposed to ignore access
10790      control while processing explicit instantiation directives.  */
10791   push_deferring_access_checks (dk_no_check);
10792   /* Parse a decl-specifier-seq.  */
10793   token = cp_lexer_peek_token (parser->lexer);
10794   cp_parser_decl_specifier_seq (parser,
10795                                 CP_PARSER_FLAGS_OPTIONAL,
10796                                 &decl_specifiers,
10797                                 &declares_class_or_enum);
10798   /* If there was exactly one decl-specifier, and it declared a class,
10799      and there's no declarator, then we have an explicit type
10800      instantiation.  */
10801   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10802     {
10803       tree type;
10804
10805       type = check_tag_decl (&decl_specifiers);
10806       /* Turn access control back on for names used during
10807          template instantiation.  */
10808       pop_deferring_access_checks ();
10809       if (type)
10810         do_type_instantiation (type, extension_specifier,
10811                                /*complain=*/tf_error);
10812     }
10813   else
10814     {
10815       cp_declarator *declarator;
10816       tree decl;
10817
10818       /* Parse the declarator.  */
10819       declarator
10820         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10821                                 /*ctor_dtor_or_conv_p=*/NULL,
10822                                 /*parenthesized_p=*/NULL,
10823                                 /*member_p=*/false);
10824       if (declares_class_or_enum & 2)
10825         cp_parser_check_for_definition_in_return_type (declarator,
10826                                                        decl_specifiers.type,
10827                                                        decl_specifiers.type_location);
10828       if (declarator != cp_error_declarator)
10829         {
10830           decl = grokdeclarator (declarator, &decl_specifiers,
10831                                  NORMAL, 0, &decl_specifiers.attributes);
10832           /* Turn access control back on for names used during
10833              template instantiation.  */
10834           pop_deferring_access_checks ();
10835           /* Do the explicit instantiation.  */
10836           do_decl_instantiation (decl, extension_specifier);
10837         }
10838       else
10839         {
10840           pop_deferring_access_checks ();
10841           /* Skip the body of the explicit instantiation.  */
10842           cp_parser_skip_to_end_of_statement (parser);
10843         }
10844     }
10845   /* We're done with the instantiation.  */
10846   end_explicit_instantiation ();
10847
10848   cp_parser_consume_semicolon_at_end_of_statement (parser);
10849 }
10850
10851 /* Parse an explicit-specialization.
10852
10853    explicit-specialization:
10854      template < > declaration
10855
10856    Although the standard says `declaration', what it really means is:
10857
10858    explicit-specialization:
10859      template <> decl-specifier [opt] init-declarator [opt] ;
10860      template <> function-definition
10861      template <> explicit-specialization
10862      template <> template-declaration  */
10863
10864 static void
10865 cp_parser_explicit_specialization (cp_parser* parser)
10866 {
10867   bool need_lang_pop;
10868   cp_token *token = cp_lexer_peek_token (parser->lexer);
10869
10870   /* Look for the `template' keyword.  */
10871   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10872   /* Look for the `<'.  */
10873   cp_parser_require (parser, CPP_LESS, "%<<%>");
10874   /* Look for the `>'.  */
10875   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10876   /* We have processed another parameter list.  */
10877   ++parser->num_template_parameter_lists;
10878   /* [temp]
10879
10880      A template ... explicit specialization ... shall not have C
10881      linkage.  */
10882   if (current_lang_name == lang_name_c)
10883     {
10884       error ("%Htemplate specialization with C linkage", &token->location);
10885       /* Give it C++ linkage to avoid confusing other parts of the
10886          front end.  */
10887       push_lang_context (lang_name_cplusplus);
10888       need_lang_pop = true;
10889     }
10890   else
10891     need_lang_pop = false;
10892   /* Let the front end know that we are beginning a specialization.  */
10893   if (!begin_specialization ())
10894     {
10895       end_specialization ();
10896       return;
10897     }
10898
10899   /* If the next keyword is `template', we need to figure out whether
10900      or not we're looking a template-declaration.  */
10901   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10902     {
10903       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10904           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10905         cp_parser_template_declaration_after_export (parser,
10906                                                      /*member_p=*/false);
10907       else
10908         cp_parser_explicit_specialization (parser);
10909     }
10910   else
10911     /* Parse the dependent declaration.  */
10912     cp_parser_single_declaration (parser,
10913                                   /*checks=*/NULL,
10914                                   /*member_p=*/false,
10915                                   /*explicit_specialization_p=*/true,
10916                                   /*friend_p=*/NULL);
10917   /* We're done with the specialization.  */
10918   end_specialization ();
10919   /* For the erroneous case of a template with C linkage, we pushed an
10920      implicit C++ linkage scope; exit that scope now.  */
10921   if (need_lang_pop)
10922     pop_lang_context ();
10923   /* We're done with this parameter list.  */
10924   --parser->num_template_parameter_lists;
10925 }
10926
10927 /* Parse a type-specifier.
10928
10929    type-specifier:
10930      simple-type-specifier
10931      class-specifier
10932      enum-specifier
10933      elaborated-type-specifier
10934      cv-qualifier
10935
10936    GNU Extension:
10937
10938    type-specifier:
10939      __complex__
10940
10941    Returns a representation of the type-specifier.  For a
10942    class-specifier, enum-specifier, or elaborated-type-specifier, a
10943    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10944
10945    The parser flags FLAGS is used to control type-specifier parsing.
10946
10947    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10948    in a decl-specifier-seq.
10949
10950    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10951    class-specifier, enum-specifier, or elaborated-type-specifier, then
10952    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10953    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10954    zero.
10955
10956    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10957    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10958    is set to FALSE.  */
10959
10960 static tree
10961 cp_parser_type_specifier (cp_parser* parser,
10962                           cp_parser_flags flags,
10963                           cp_decl_specifier_seq *decl_specs,
10964                           bool is_declaration,
10965                           int* declares_class_or_enum,
10966                           bool* is_cv_qualifier)
10967 {
10968   tree type_spec = NULL_TREE;
10969   cp_token *token;
10970   enum rid keyword;
10971   cp_decl_spec ds = ds_last;
10972
10973   /* Assume this type-specifier does not declare a new type.  */
10974   if (declares_class_or_enum)
10975     *declares_class_or_enum = 0;
10976   /* And that it does not specify a cv-qualifier.  */
10977   if (is_cv_qualifier)
10978     *is_cv_qualifier = false;
10979   /* Peek at the next token.  */
10980   token = cp_lexer_peek_token (parser->lexer);
10981
10982   /* If we're looking at a keyword, we can use that to guide the
10983      production we choose.  */
10984   keyword = token->keyword;
10985   switch (keyword)
10986     {
10987     case RID_ENUM:
10988       /* Look for the enum-specifier.  */
10989       type_spec = cp_parser_enum_specifier (parser);
10990       /* If that worked, we're done.  */
10991       if (type_spec)
10992         {
10993           if (declares_class_or_enum)
10994             *declares_class_or_enum = 2;
10995           if (decl_specs)
10996             cp_parser_set_decl_spec_type (decl_specs,
10997                                           type_spec,
10998                                           token->location,
10999                                           /*user_defined_p=*/true);
11000           return type_spec;
11001         }
11002       else
11003         goto elaborated_type_specifier;
11004
11005       /* Any of these indicate either a class-specifier, or an
11006          elaborated-type-specifier.  */
11007     case RID_CLASS:
11008     case RID_STRUCT:
11009     case RID_UNION:
11010       /* Parse tentatively so that we can back up if we don't find a
11011          class-specifier.  */
11012       cp_parser_parse_tentatively (parser);
11013       /* Look for the class-specifier.  */
11014       type_spec = cp_parser_class_specifier (parser);
11015       /* If that worked, we're done.  */
11016       if (cp_parser_parse_definitely (parser))
11017         {
11018           if (declares_class_or_enum)
11019             *declares_class_or_enum = 2;
11020           if (decl_specs)
11021             cp_parser_set_decl_spec_type (decl_specs,
11022                                           type_spec,
11023                                           token->location,
11024                                           /*user_defined_p=*/true);
11025           return type_spec;
11026         }
11027
11028       /* Fall through.  */
11029     elaborated_type_specifier:
11030       /* We're declaring (not defining) a class or enum.  */
11031       if (declares_class_or_enum)
11032         *declares_class_or_enum = 1;
11033
11034       /* Fall through.  */
11035     case RID_TYPENAME:
11036       /* Look for an elaborated-type-specifier.  */
11037       type_spec
11038         = (cp_parser_elaborated_type_specifier
11039            (parser,
11040             decl_specs && decl_specs->specs[(int) ds_friend],
11041             is_declaration));
11042       if (decl_specs)
11043         cp_parser_set_decl_spec_type (decl_specs,
11044                                       type_spec,
11045                                       token->location,
11046                                       /*user_defined_p=*/true);
11047       return type_spec;
11048
11049     case RID_CONST:
11050       ds = ds_const;
11051       if (is_cv_qualifier)
11052         *is_cv_qualifier = true;
11053       break;
11054
11055     case RID_VOLATILE:
11056       ds = ds_volatile;
11057       if (is_cv_qualifier)
11058         *is_cv_qualifier = true;
11059       break;
11060
11061     case RID_RESTRICT:
11062       ds = ds_restrict;
11063       if (is_cv_qualifier)
11064         *is_cv_qualifier = true;
11065       break;
11066
11067     case RID_COMPLEX:
11068       /* The `__complex__' keyword is a GNU extension.  */
11069       ds = ds_complex;
11070       break;
11071
11072     default:
11073       break;
11074     }
11075
11076   /* Handle simple keywords.  */
11077   if (ds != ds_last)
11078     {
11079       if (decl_specs)
11080         {
11081           ++decl_specs->specs[(int)ds];
11082           decl_specs->any_specifiers_p = true;
11083         }
11084       return cp_lexer_consume_token (parser->lexer)->u.value;
11085     }
11086
11087   /* If we do not already have a type-specifier, assume we are looking
11088      at a simple-type-specifier.  */
11089   type_spec = cp_parser_simple_type_specifier (parser,
11090                                                decl_specs,
11091                                                flags);
11092
11093   /* If we didn't find a type-specifier, and a type-specifier was not
11094      optional in this context, issue an error message.  */
11095   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11096     {
11097       cp_parser_error (parser, "expected type specifier");
11098       return error_mark_node;
11099     }
11100
11101   return type_spec;
11102 }
11103
11104 /* Parse a simple-type-specifier.
11105
11106    simple-type-specifier:
11107      :: [opt] nested-name-specifier [opt] type-name
11108      :: [opt] nested-name-specifier template template-id
11109      char
11110      wchar_t
11111      bool
11112      short
11113      int
11114      long
11115      signed
11116      unsigned
11117      float
11118      double
11119      void
11120
11121    C++0x Extension:
11122
11123    simple-type-specifier:
11124      auto
11125      decltype ( expression )   
11126      char16_t
11127      char32_t
11128
11129    GNU Extension:
11130
11131    simple-type-specifier:
11132      __typeof__ unary-expression
11133      __typeof__ ( type-id )
11134
11135    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11136    appropriately updated.  */
11137
11138 static tree
11139 cp_parser_simple_type_specifier (cp_parser* parser,
11140                                  cp_decl_specifier_seq *decl_specs,
11141                                  cp_parser_flags flags)
11142 {
11143   tree type = NULL_TREE;
11144   cp_token *token;
11145
11146   /* Peek at the next token.  */
11147   token = cp_lexer_peek_token (parser->lexer);
11148
11149   /* If we're looking at a keyword, things are easy.  */
11150   switch (token->keyword)
11151     {
11152     case RID_CHAR:
11153       if (decl_specs)
11154         decl_specs->explicit_char_p = true;
11155       type = char_type_node;
11156       break;
11157     case RID_CHAR16:
11158       type = char16_type_node;
11159       break;
11160     case RID_CHAR32:
11161       type = char32_type_node;
11162       break;
11163     case RID_WCHAR:
11164       type = wchar_type_node;
11165       break;
11166     case RID_BOOL:
11167       type = boolean_type_node;
11168       break;
11169     case RID_SHORT:
11170       if (decl_specs)
11171         ++decl_specs->specs[(int) ds_short];
11172       type = short_integer_type_node;
11173       break;
11174     case RID_INT:
11175       if (decl_specs)
11176         decl_specs->explicit_int_p = true;
11177       type = integer_type_node;
11178       break;
11179     case RID_LONG:
11180       if (decl_specs)
11181         ++decl_specs->specs[(int) ds_long];
11182       type = long_integer_type_node;
11183       break;
11184     case RID_SIGNED:
11185       if (decl_specs)
11186         ++decl_specs->specs[(int) ds_signed];
11187       type = integer_type_node;
11188       break;
11189     case RID_UNSIGNED:
11190       if (decl_specs)
11191         ++decl_specs->specs[(int) ds_unsigned];
11192       type = unsigned_type_node;
11193       break;
11194     case RID_FLOAT:
11195       type = float_type_node;
11196       break;
11197     case RID_DOUBLE:
11198       type = double_type_node;
11199       break;
11200     case RID_VOID:
11201       type = void_type_node;
11202       break;
11203       
11204     case RID_AUTO:
11205       maybe_warn_cpp0x ("C++0x auto");
11206       type = make_auto ();
11207       break;
11208
11209     case RID_DECLTYPE:
11210       /* Parse the `decltype' type.  */
11211       type = cp_parser_decltype (parser);
11212
11213       if (decl_specs)
11214         cp_parser_set_decl_spec_type (decl_specs, type,
11215                                       token->location,
11216                                       /*user_defined_p=*/true);
11217
11218       return type;
11219
11220     case RID_TYPEOF:
11221       /* Consume the `typeof' token.  */
11222       cp_lexer_consume_token (parser->lexer);
11223       /* Parse the operand to `typeof'.  */
11224       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11225       /* If it is not already a TYPE, take its type.  */
11226       if (!TYPE_P (type))
11227         type = finish_typeof (type);
11228
11229       if (decl_specs)
11230         cp_parser_set_decl_spec_type (decl_specs, type,
11231                                       token->location,
11232                                       /*user_defined_p=*/true);
11233
11234       return type;
11235
11236     default:
11237       break;
11238     }
11239
11240   /* If the type-specifier was for a built-in type, we're done.  */
11241   if (type)
11242     {
11243       tree id;
11244
11245       /* Record the type.  */
11246       if (decl_specs
11247           && (token->keyword != RID_SIGNED
11248               && token->keyword != RID_UNSIGNED
11249               && token->keyword != RID_SHORT
11250               && token->keyword != RID_LONG))
11251         cp_parser_set_decl_spec_type (decl_specs,
11252                                       type,
11253                                       token->location,
11254                                       /*user_defined=*/false);
11255       if (decl_specs)
11256         decl_specs->any_specifiers_p = true;
11257
11258       /* Consume the token.  */
11259       id = cp_lexer_consume_token (parser->lexer)->u.value;
11260
11261       /* There is no valid C++ program where a non-template type is
11262          followed by a "<".  That usually indicates that the user thought
11263          that the type was a template.  */
11264       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11265
11266       return TYPE_NAME (type);
11267     }
11268
11269   /* The type-specifier must be a user-defined type.  */
11270   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11271     {
11272       bool qualified_p;
11273       bool global_p;
11274
11275       /* Don't gobble tokens or issue error messages if this is an
11276          optional type-specifier.  */
11277       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11278         cp_parser_parse_tentatively (parser);
11279
11280       /* Look for the optional `::' operator.  */
11281       global_p
11282         = (cp_parser_global_scope_opt (parser,
11283                                        /*current_scope_valid_p=*/false)
11284            != NULL_TREE);
11285       /* Look for the nested-name specifier.  */
11286       qualified_p
11287         = (cp_parser_nested_name_specifier_opt (parser,
11288                                                 /*typename_keyword_p=*/false,
11289                                                 /*check_dependency_p=*/true,
11290                                                 /*type_p=*/false,
11291                                                 /*is_declaration=*/false)
11292            != NULL_TREE);
11293       token = cp_lexer_peek_token (parser->lexer);
11294       /* If we have seen a nested-name-specifier, and the next token
11295          is `template', then we are using the template-id production.  */
11296       if (parser->scope
11297           && cp_parser_optional_template_keyword (parser))
11298         {
11299           /* Look for the template-id.  */
11300           type = cp_parser_template_id (parser,
11301                                         /*template_keyword_p=*/true,
11302                                         /*check_dependency_p=*/true,
11303                                         /*is_declaration=*/false);
11304           /* If the template-id did not name a type, we are out of
11305              luck.  */
11306           if (TREE_CODE (type) != TYPE_DECL)
11307             {
11308               cp_parser_error (parser, "expected template-id for type");
11309               type = NULL_TREE;
11310             }
11311         }
11312       /* Otherwise, look for a type-name.  */
11313       else
11314         type = cp_parser_type_name (parser);
11315       /* Keep track of all name-lookups performed in class scopes.  */
11316       if (type
11317           && !global_p
11318           && !qualified_p
11319           && TREE_CODE (type) == TYPE_DECL
11320           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11321         maybe_note_name_used_in_class (DECL_NAME (type), type);
11322       /* If it didn't work out, we don't have a TYPE.  */
11323       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11324           && !cp_parser_parse_definitely (parser))
11325         type = NULL_TREE;
11326       if (type && decl_specs)
11327         cp_parser_set_decl_spec_type (decl_specs, type,
11328                                       token->location,
11329                                       /*user_defined=*/true);
11330     }
11331
11332   /* If we didn't get a type-name, issue an error message.  */
11333   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11334     {
11335       cp_parser_error (parser, "expected type-name");
11336       return error_mark_node;
11337     }
11338
11339   /* There is no valid C++ program where a non-template type is
11340      followed by a "<".  That usually indicates that the user thought
11341      that the type was a template.  */
11342   if (type && type != error_mark_node)
11343     {
11344       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11345          If it is, then the '<'...'>' enclose protocol names rather than
11346          template arguments, and so everything is fine.  */
11347       if (c_dialect_objc ()
11348           && (objc_is_id (type) || objc_is_class_name (type)))
11349         {
11350           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11351           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11352
11353           /* Clobber the "unqualified" type previously entered into
11354              DECL_SPECS with the new, improved protocol-qualified version.  */
11355           if (decl_specs)
11356             decl_specs->type = qual_type;
11357
11358           return qual_type;
11359         }
11360
11361       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11362                                                token->location);
11363     }
11364
11365   return type;
11366 }
11367
11368 /* Parse a type-name.
11369
11370    type-name:
11371      class-name
11372      enum-name
11373      typedef-name
11374
11375    enum-name:
11376      identifier
11377
11378    typedef-name:
11379      identifier
11380
11381    Returns a TYPE_DECL for the type.  */
11382
11383 static tree
11384 cp_parser_type_name (cp_parser* parser)
11385 {
11386   tree type_decl;
11387
11388   /* We can't know yet whether it is a class-name or not.  */
11389   cp_parser_parse_tentatively (parser);
11390   /* Try a class-name.  */
11391   type_decl = cp_parser_class_name (parser,
11392                                     /*typename_keyword_p=*/false,
11393                                     /*template_keyword_p=*/false,
11394                                     none_type,
11395                                     /*check_dependency_p=*/true,
11396                                     /*class_head_p=*/false,
11397                                     /*is_declaration=*/false);
11398   /* If it's not a class-name, keep looking.  */
11399   if (!cp_parser_parse_definitely (parser))
11400     {
11401       /* It must be a typedef-name or an enum-name.  */
11402       return cp_parser_nonclass_name (parser);
11403     }
11404
11405   return type_decl;
11406 }
11407
11408 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11409
11410    enum-name:
11411      identifier
11412
11413    typedef-name:
11414      identifier
11415
11416    Returns a TYPE_DECL for the type.  */
11417
11418 static tree
11419 cp_parser_nonclass_name (cp_parser* parser)
11420 {
11421   tree type_decl;
11422   tree identifier;
11423
11424   cp_token *token = cp_lexer_peek_token (parser->lexer);
11425   identifier = cp_parser_identifier (parser);
11426   if (identifier == error_mark_node)
11427     return error_mark_node;
11428
11429   /* Look up the type-name.  */
11430   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11431
11432   if (TREE_CODE (type_decl) != TYPE_DECL
11433       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11434     {
11435       /* See if this is an Objective-C type.  */
11436       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11437       tree type = objc_get_protocol_qualified_type (identifier, protos);
11438       if (type)
11439         type_decl = TYPE_NAME (type);
11440     }
11441   
11442   /* Issue an error if we did not find a type-name.  */
11443   if (TREE_CODE (type_decl) != TYPE_DECL)
11444     {
11445       if (!cp_parser_simulate_error (parser))
11446         cp_parser_name_lookup_error (parser, identifier, type_decl,
11447                                      "is not a type", token->location);
11448       return error_mark_node;
11449     }
11450   /* Remember that the name was used in the definition of the
11451      current class so that we can check later to see if the
11452      meaning would have been different after the class was
11453      entirely defined.  */
11454   else if (type_decl != error_mark_node
11455            && !parser->scope)
11456     maybe_note_name_used_in_class (identifier, type_decl);
11457   
11458   return type_decl;
11459 }
11460
11461 /* Parse an elaborated-type-specifier.  Note that the grammar given
11462    here incorporates the resolution to DR68.
11463
11464    elaborated-type-specifier:
11465      class-key :: [opt] nested-name-specifier [opt] identifier
11466      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11467      enum-key :: [opt] nested-name-specifier [opt] identifier
11468      typename :: [opt] nested-name-specifier identifier
11469      typename :: [opt] nested-name-specifier template [opt]
11470        template-id
11471
11472    GNU extension:
11473
11474    elaborated-type-specifier:
11475      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11476      class-key attributes :: [opt] nested-name-specifier [opt]
11477                template [opt] template-id
11478      enum attributes :: [opt] nested-name-specifier [opt] identifier
11479
11480    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11481    declared `friend'.  If IS_DECLARATION is TRUE, then this
11482    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11483    something is being declared.
11484
11485    Returns the TYPE specified.  */
11486
11487 static tree
11488 cp_parser_elaborated_type_specifier (cp_parser* parser,
11489                                      bool is_friend,
11490                                      bool is_declaration)
11491 {
11492   enum tag_types tag_type;
11493   tree identifier;
11494   tree type = NULL_TREE;
11495   tree attributes = NULL_TREE;
11496   cp_token *token = NULL;
11497
11498   /* See if we're looking at the `enum' keyword.  */
11499   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11500     {
11501       /* Consume the `enum' token.  */
11502       cp_lexer_consume_token (parser->lexer);
11503       /* Remember that it's an enumeration type.  */
11504       tag_type = enum_type;
11505       /* Parse the optional `struct' or `class' key (for C++0x scoped
11506          enums).  */
11507       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11508           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11509         {
11510           if (cxx_dialect == cxx98)
11511             maybe_warn_cpp0x ("scoped enums");
11512
11513           /* Consume the `struct' or `class'.  */
11514           cp_lexer_consume_token (parser->lexer);
11515         }
11516       /* Parse the attributes.  */
11517       attributes = cp_parser_attributes_opt (parser);
11518     }
11519   /* Or, it might be `typename'.  */
11520   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11521                                            RID_TYPENAME))
11522     {
11523       /* Consume the `typename' token.  */
11524       cp_lexer_consume_token (parser->lexer);
11525       /* Remember that it's a `typename' type.  */
11526       tag_type = typename_type;
11527       /* The `typename' keyword is only allowed in templates.  */
11528       if (!processing_template_decl)
11529         permerror (input_location, "using %<typename%> outside of template");
11530     }
11531   /* Otherwise it must be a class-key.  */
11532   else
11533     {
11534       tag_type = cp_parser_class_key (parser);
11535       if (tag_type == none_type)
11536         return error_mark_node;
11537       /* Parse the attributes.  */
11538       attributes = cp_parser_attributes_opt (parser);
11539     }
11540
11541   /* Look for the `::' operator.  */
11542   cp_parser_global_scope_opt (parser,
11543                               /*current_scope_valid_p=*/false);
11544   /* Look for the nested-name-specifier.  */
11545   if (tag_type == typename_type)
11546     {
11547       if (!cp_parser_nested_name_specifier (parser,
11548                                            /*typename_keyword_p=*/true,
11549                                            /*check_dependency_p=*/true,
11550                                            /*type_p=*/true,
11551                                             is_declaration))
11552         return error_mark_node;
11553     }
11554   else
11555     /* Even though `typename' is not present, the proposed resolution
11556        to Core Issue 180 says that in `class A<T>::B', `B' should be
11557        considered a type-name, even if `A<T>' is dependent.  */
11558     cp_parser_nested_name_specifier_opt (parser,
11559                                          /*typename_keyword_p=*/true,
11560                                          /*check_dependency_p=*/true,
11561                                          /*type_p=*/true,
11562                                          is_declaration);
11563  /* For everything but enumeration types, consider a template-id.
11564     For an enumeration type, consider only a plain identifier.  */
11565   if (tag_type != enum_type)
11566     {
11567       bool template_p = false;
11568       tree decl;
11569
11570       /* Allow the `template' keyword.  */
11571       template_p = cp_parser_optional_template_keyword (parser);
11572       /* If we didn't see `template', we don't know if there's a
11573          template-id or not.  */
11574       if (!template_p)
11575         cp_parser_parse_tentatively (parser);
11576       /* Parse the template-id.  */
11577       token = cp_lexer_peek_token (parser->lexer);
11578       decl = cp_parser_template_id (parser, template_p,
11579                                     /*check_dependency_p=*/true,
11580                                     is_declaration);
11581       /* If we didn't find a template-id, look for an ordinary
11582          identifier.  */
11583       if (!template_p && !cp_parser_parse_definitely (parser))
11584         ;
11585       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11586          in effect, then we must assume that, upon instantiation, the
11587          template will correspond to a class.  */
11588       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11589                && tag_type == typename_type)
11590         type = make_typename_type (parser->scope, decl,
11591                                    typename_type,
11592                                    /*complain=*/tf_error);
11593       /* If the `typename' keyword is in effect and DECL is not a type
11594          decl. Then type is non existant.   */
11595       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11596         type = NULL_TREE; 
11597       else 
11598         type = TREE_TYPE (decl);
11599     }
11600
11601   if (!type)
11602     {
11603       token = cp_lexer_peek_token (parser->lexer);
11604       identifier = cp_parser_identifier (parser);
11605
11606       if (identifier == error_mark_node)
11607         {
11608           parser->scope = NULL_TREE;
11609           return error_mark_node;
11610         }
11611
11612       /* For a `typename', we needn't call xref_tag.  */
11613       if (tag_type == typename_type
11614           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11615         return cp_parser_make_typename_type (parser, parser->scope,
11616                                              identifier,
11617                                              token->location);
11618       /* Look up a qualified name in the usual way.  */
11619       if (parser->scope)
11620         {
11621           tree decl;
11622           tree ambiguous_decls;
11623
11624           decl = cp_parser_lookup_name (parser, identifier,
11625                                         tag_type,
11626                                         /*is_template=*/false,
11627                                         /*is_namespace=*/false,
11628                                         /*check_dependency=*/true,
11629                                         &ambiguous_decls,
11630                                         token->location);
11631
11632           /* If the lookup was ambiguous, an error will already have been
11633              issued.  */
11634           if (ambiguous_decls)
11635             return error_mark_node;
11636
11637           /* If we are parsing friend declaration, DECL may be a
11638              TEMPLATE_DECL tree node here.  However, we need to check
11639              whether this TEMPLATE_DECL results in valid code.  Consider
11640              the following example:
11641
11642                namespace N {
11643                  template <class T> class C {};
11644                }
11645                class X {
11646                  template <class T> friend class N::C; // #1, valid code
11647                };
11648                template <class T> class Y {
11649                  friend class N::C;                    // #2, invalid code
11650                };
11651
11652              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11653              name lookup of `N::C'.  We see that friend declaration must
11654              be template for the code to be valid.  Note that
11655              processing_template_decl does not work here since it is
11656              always 1 for the above two cases.  */
11657
11658           decl = (cp_parser_maybe_treat_template_as_class
11659                   (decl, /*tag_name_p=*/is_friend
11660                          && parser->num_template_parameter_lists));
11661
11662           if (TREE_CODE (decl) != TYPE_DECL)
11663             {
11664               cp_parser_diagnose_invalid_type_name (parser,
11665                                                     parser->scope,
11666                                                     identifier,
11667                                                     token->location);
11668               return error_mark_node;
11669             }
11670
11671           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11672             {
11673               bool allow_template = (parser->num_template_parameter_lists
11674                                       || DECL_SELF_REFERENCE_P (decl));
11675               type = check_elaborated_type_specifier (tag_type, decl, 
11676                                                       allow_template);
11677
11678               if (type == error_mark_node)
11679                 return error_mark_node;
11680             }
11681
11682           /* Forward declarations of nested types, such as
11683
11684                class C1::C2;
11685                class C1::C2::C3;
11686
11687              are invalid unless all components preceding the final '::'
11688              are complete.  If all enclosing types are complete, these
11689              declarations become merely pointless.
11690
11691              Invalid forward declarations of nested types are errors
11692              caught elsewhere in parsing.  Those that are pointless arrive
11693              here.  */
11694
11695           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11696               && !is_friend && !processing_explicit_instantiation)
11697             warning (0, "declaration %qD does not declare anything", decl);
11698
11699           type = TREE_TYPE (decl);
11700         }
11701       else
11702         {
11703           /* An elaborated-type-specifier sometimes introduces a new type and
11704              sometimes names an existing type.  Normally, the rule is that it
11705              introduces a new type only if there is not an existing type of
11706              the same name already in scope.  For example, given:
11707
11708                struct S {};
11709                void f() { struct S s; }
11710
11711              the `struct S' in the body of `f' is the same `struct S' as in
11712              the global scope; the existing definition is used.  However, if
11713              there were no global declaration, this would introduce a new
11714              local class named `S'.
11715
11716              An exception to this rule applies to the following code:
11717
11718                namespace N { struct S; }
11719
11720              Here, the elaborated-type-specifier names a new type
11721              unconditionally; even if there is already an `S' in the
11722              containing scope this declaration names a new type.
11723              This exception only applies if the elaborated-type-specifier
11724              forms the complete declaration:
11725
11726                [class.name]
11727
11728                A declaration consisting solely of `class-key identifier ;' is
11729                either a redeclaration of the name in the current scope or a
11730                forward declaration of the identifier as a class name.  It
11731                introduces the name into the current scope.
11732
11733              We are in this situation precisely when the next token is a `;'.
11734
11735              An exception to the exception is that a `friend' declaration does
11736              *not* name a new type; i.e., given:
11737
11738                struct S { friend struct T; };
11739
11740              `T' is not a new type in the scope of `S'.
11741
11742              Also, `new struct S' or `sizeof (struct S)' never results in the
11743              definition of a new type; a new type can only be declared in a
11744              declaration context.  */
11745
11746           tag_scope ts;
11747           bool template_p;
11748
11749           if (is_friend)
11750             /* Friends have special name lookup rules.  */
11751             ts = ts_within_enclosing_non_class;
11752           else if (is_declaration
11753                    && cp_lexer_next_token_is (parser->lexer,
11754                                               CPP_SEMICOLON))
11755             /* This is a `class-key identifier ;' */
11756             ts = ts_current;
11757           else
11758             ts = ts_global;
11759
11760           template_p =
11761             (parser->num_template_parameter_lists
11762              && (cp_parser_next_token_starts_class_definition_p (parser)
11763                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11764           /* An unqualified name was used to reference this type, so
11765              there were no qualifying templates.  */
11766           if (!cp_parser_check_template_parameters (parser,
11767                                                     /*num_templates=*/0,
11768                                                     token->location))
11769             return error_mark_node;
11770           type = xref_tag (tag_type, identifier, ts, template_p);
11771         }
11772     }
11773
11774   if (type == error_mark_node)
11775     return error_mark_node;
11776
11777   /* Allow attributes on forward declarations of classes.  */
11778   if (attributes)
11779     {
11780       if (TREE_CODE (type) == TYPENAME_TYPE)
11781         warning (OPT_Wattributes,
11782                  "attributes ignored on uninstantiated type");
11783       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11784                && ! processing_explicit_instantiation)
11785         warning (OPT_Wattributes,
11786                  "attributes ignored on template instantiation");
11787       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11788         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11789       else
11790         warning (OPT_Wattributes,
11791                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11792     }
11793
11794   if (tag_type != enum_type)
11795     cp_parser_check_class_key (tag_type, type);
11796
11797   /* A "<" cannot follow an elaborated type specifier.  If that
11798      happens, the user was probably trying to form a template-id.  */
11799   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11800
11801   return type;
11802 }
11803
11804 /* Parse an enum-specifier.
11805
11806    enum-specifier:
11807      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11808
11809    enum-key:
11810      enum
11811      enum class   [C++0x]
11812      enum struct  [C++0x]
11813
11814    enum-base:   [C++0x]
11815      : type-specifier-seq
11816
11817    GNU Extensions:
11818      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11819        { enumerator-list [opt] }attributes[opt]
11820
11821    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11822    if the token stream isn't an enum-specifier after all.  */
11823
11824 static tree
11825 cp_parser_enum_specifier (cp_parser* parser)
11826 {
11827   tree identifier;
11828   tree type;
11829   tree attributes;
11830   bool scoped_enum_p = false;
11831   bool has_underlying_type = false;
11832   tree underlying_type = NULL_TREE;
11833
11834   /* Parse tentatively so that we can back up if we don't find a
11835      enum-specifier.  */
11836   cp_parser_parse_tentatively (parser);
11837
11838   /* Caller guarantees that the current token is 'enum', an identifier
11839      possibly follows, and the token after that is an opening brace.
11840      If we don't have an identifier, fabricate an anonymous name for
11841      the enumeration being defined.  */
11842   cp_lexer_consume_token (parser->lexer);
11843
11844   /* Parse the "class" or "struct", which indicates a scoped
11845      enumeration type in C++0x.  */
11846   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11847       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11848     {
11849       if (cxx_dialect == cxx98)
11850         maybe_warn_cpp0x ("scoped enums");
11851
11852       /* Consume the `struct' or `class' token.  */
11853       cp_lexer_consume_token (parser->lexer);
11854
11855       scoped_enum_p = true;
11856     }
11857
11858   attributes = cp_parser_attributes_opt (parser);
11859
11860   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11861     identifier = cp_parser_identifier (parser);
11862   else
11863     identifier = make_anon_name ();
11864
11865   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11866   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11867     {
11868       cp_decl_specifier_seq type_specifiers;
11869
11870       /* At this point this is surely not elaborated type specifier.  */
11871       if (!cp_parser_parse_definitely (parser))
11872         return NULL_TREE;
11873
11874       if (cxx_dialect == cxx98)
11875         maybe_warn_cpp0x ("scoped enums");
11876
11877       /* Consume the `:'.  */
11878       cp_lexer_consume_token (parser->lexer);
11879
11880       has_underlying_type = true;
11881
11882       /* Parse the type-specifier-seq.  */
11883       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11884                                     &type_specifiers);
11885
11886       /* If that didn't work, stop.  */
11887       if (type_specifiers.type != error_mark_node)
11888         {
11889           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11890                                             /*initialized=*/0, NULL);
11891           if (underlying_type == error_mark_node)
11892             underlying_type = NULL_TREE;
11893         }
11894     }
11895
11896   /* Look for the `{' but don't consume it yet.  */
11897   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11898     {
11899       cp_parser_error (parser, "expected %<{%>");
11900       if (has_underlying_type)
11901         return NULL_TREE;
11902     }
11903
11904   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11905     return NULL_TREE;
11906
11907   /* Issue an error message if type-definitions are forbidden here.  */
11908   if (!cp_parser_check_type_definition (parser))
11909     type = error_mark_node;
11910   else
11911     /* Create the new type.  We do this before consuming the opening
11912        brace so the enum will be recorded as being on the line of its
11913        tag (or the 'enum' keyword, if there is no tag).  */
11914     type = start_enum (identifier, underlying_type, scoped_enum_p);
11915   
11916   /* Consume the opening brace.  */
11917   cp_lexer_consume_token (parser->lexer);
11918
11919   if (type == error_mark_node)
11920     {
11921       cp_parser_skip_to_end_of_block_or_statement (parser);
11922       return error_mark_node;
11923     }
11924
11925   /* If the next token is not '}', then there are some enumerators.  */
11926   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11927     cp_parser_enumerator_list (parser, type);
11928
11929   /* Consume the final '}'.  */
11930   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11931
11932   /* Look for trailing attributes to apply to this enumeration, and
11933      apply them if appropriate.  */
11934   if (cp_parser_allow_gnu_extensions_p (parser))
11935     {
11936       tree trailing_attr = cp_parser_attributes_opt (parser);
11937       trailing_attr = chainon (trailing_attr, attributes);
11938       cplus_decl_attributes (&type,
11939                              trailing_attr,
11940                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11941     }
11942
11943   /* Finish up the enumeration.  */
11944   finish_enum (type);
11945
11946   return type;
11947 }
11948
11949 /* Parse an enumerator-list.  The enumerators all have the indicated
11950    TYPE.
11951
11952    enumerator-list:
11953      enumerator-definition
11954      enumerator-list , enumerator-definition  */
11955
11956 static void
11957 cp_parser_enumerator_list (cp_parser* parser, tree type)
11958 {
11959   while (true)
11960     {
11961       /* Parse an enumerator-definition.  */
11962       cp_parser_enumerator_definition (parser, type);
11963
11964       /* If the next token is not a ',', we've reached the end of
11965          the list.  */
11966       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11967         break;
11968       /* Otherwise, consume the `,' and keep going.  */
11969       cp_lexer_consume_token (parser->lexer);
11970       /* If the next token is a `}', there is a trailing comma.  */
11971       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11972         {
11973           if (!in_system_header)
11974             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11975           break;
11976         }
11977     }
11978 }
11979
11980 /* Parse an enumerator-definition.  The enumerator has the indicated
11981    TYPE.
11982
11983    enumerator-definition:
11984      enumerator
11985      enumerator = constant-expression
11986
11987    enumerator:
11988      identifier  */
11989
11990 static void
11991 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11992 {
11993   tree identifier;
11994   tree value;
11995
11996   /* Look for the identifier.  */
11997   identifier = cp_parser_identifier (parser);
11998   if (identifier == error_mark_node)
11999     return;
12000
12001   /* If the next token is an '=', then there is an explicit value.  */
12002   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12003     {
12004       /* Consume the `=' token.  */
12005       cp_lexer_consume_token (parser->lexer);
12006       /* Parse the value.  */
12007       value = cp_parser_constant_expression (parser,
12008                                              /*allow_non_constant_p=*/false,
12009                                              NULL);
12010     }
12011   else
12012     value = NULL_TREE;
12013
12014   /* Create the enumerator.  */
12015   build_enumerator (identifier, value, type);
12016 }
12017
12018 /* Parse a namespace-name.
12019
12020    namespace-name:
12021      original-namespace-name
12022      namespace-alias
12023
12024    Returns the NAMESPACE_DECL for the namespace.  */
12025
12026 static tree
12027 cp_parser_namespace_name (cp_parser* parser)
12028 {
12029   tree identifier;
12030   tree namespace_decl;
12031
12032   cp_token *token = cp_lexer_peek_token (parser->lexer);
12033
12034   /* Get the name of the namespace.  */
12035   identifier = cp_parser_identifier (parser);
12036   if (identifier == error_mark_node)
12037     return error_mark_node;
12038
12039   /* Look up the identifier in the currently active scope.  Look only
12040      for namespaces, due to:
12041
12042        [basic.lookup.udir]
12043
12044        When looking up a namespace-name in a using-directive or alias
12045        definition, only namespace names are considered.
12046
12047      And:
12048
12049        [basic.lookup.qual]
12050
12051        During the lookup of a name preceding the :: scope resolution
12052        operator, object, function, and enumerator names are ignored.
12053
12054      (Note that cp_parser_qualifying_entity only calls this
12055      function if the token after the name is the scope resolution
12056      operator.)  */
12057   namespace_decl = cp_parser_lookup_name (parser, identifier,
12058                                           none_type,
12059                                           /*is_template=*/false,
12060                                           /*is_namespace=*/true,
12061                                           /*check_dependency=*/true,
12062                                           /*ambiguous_decls=*/NULL,
12063                                           token->location);
12064   /* If it's not a namespace, issue an error.  */
12065   if (namespace_decl == error_mark_node
12066       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12067     {
12068       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12069         error ("%H%qD is not a namespace-name", &token->location, identifier);
12070       cp_parser_error (parser, "expected namespace-name");
12071       namespace_decl = error_mark_node;
12072     }
12073
12074   return namespace_decl;
12075 }
12076
12077 /* Parse a namespace-definition.
12078
12079    namespace-definition:
12080      named-namespace-definition
12081      unnamed-namespace-definition
12082
12083    named-namespace-definition:
12084      original-namespace-definition
12085      extension-namespace-definition
12086
12087    original-namespace-definition:
12088      namespace identifier { namespace-body }
12089
12090    extension-namespace-definition:
12091      namespace original-namespace-name { namespace-body }
12092
12093    unnamed-namespace-definition:
12094      namespace { namespace-body } */
12095
12096 static void
12097 cp_parser_namespace_definition (cp_parser* parser)
12098 {
12099   tree identifier, attribs;
12100   bool has_visibility;
12101   bool is_inline;
12102
12103   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12104     {
12105       is_inline = true;
12106       cp_lexer_consume_token (parser->lexer);
12107     }
12108   else
12109     is_inline = false;
12110
12111   /* Look for the `namespace' keyword.  */
12112   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12113
12114   /* Get the name of the namespace.  We do not attempt to distinguish
12115      between an original-namespace-definition and an
12116      extension-namespace-definition at this point.  The semantic
12117      analysis routines are responsible for that.  */
12118   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12119     identifier = cp_parser_identifier (parser);
12120   else
12121     identifier = NULL_TREE;
12122
12123   /* Parse any specified attributes.  */
12124   attribs = cp_parser_attributes_opt (parser);
12125
12126   /* Look for the `{' to start the namespace.  */
12127   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12128   /* Start the namespace.  */
12129   push_namespace (identifier);
12130
12131   /* "inline namespace" is equivalent to a stub namespace definition
12132      followed by a strong using directive.  */
12133   if (is_inline)
12134     {
12135       tree name_space = current_namespace;
12136       /* Set up namespace association.  */
12137       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12138         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12139                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12140       /* Import the contents of the inline namespace.  */
12141       pop_namespace ();
12142       do_using_directive (name_space);
12143       push_namespace (identifier);
12144     }
12145
12146   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12147
12148   /* Parse the body of the namespace.  */
12149   cp_parser_namespace_body (parser);
12150
12151 #ifdef HANDLE_PRAGMA_VISIBILITY
12152   if (has_visibility)
12153     pop_visibility ();
12154 #endif
12155
12156   /* Finish the namespace.  */
12157   pop_namespace ();
12158   /* Look for the final `}'.  */
12159   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12160 }
12161
12162 /* Parse a namespace-body.
12163
12164    namespace-body:
12165      declaration-seq [opt]  */
12166
12167 static void
12168 cp_parser_namespace_body (cp_parser* parser)
12169 {
12170   cp_parser_declaration_seq_opt (parser);
12171 }
12172
12173 /* Parse a namespace-alias-definition.
12174
12175    namespace-alias-definition:
12176      namespace identifier = qualified-namespace-specifier ;  */
12177
12178 static void
12179 cp_parser_namespace_alias_definition (cp_parser* parser)
12180 {
12181   tree identifier;
12182   tree namespace_specifier;
12183
12184   cp_token *token = cp_lexer_peek_token (parser->lexer);
12185
12186   /* Look for the `namespace' keyword.  */
12187   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12188   /* Look for the identifier.  */
12189   identifier = cp_parser_identifier (parser);
12190   if (identifier == error_mark_node)
12191     return;
12192   /* Look for the `=' token.  */
12193   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12194       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12195     {
12196       error ("%H%<namespace%> definition is not allowed here", &token->location);
12197       /* Skip the definition.  */
12198       cp_lexer_consume_token (parser->lexer);
12199       if (cp_parser_skip_to_closing_brace (parser))
12200         cp_lexer_consume_token (parser->lexer);
12201       return;
12202     }
12203   cp_parser_require (parser, CPP_EQ, "%<=%>");
12204   /* Look for the qualified-namespace-specifier.  */
12205   namespace_specifier
12206     = cp_parser_qualified_namespace_specifier (parser);
12207   /* Look for the `;' token.  */
12208   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12209
12210   /* Register the alias in the symbol table.  */
12211   do_namespace_alias (identifier, namespace_specifier);
12212 }
12213
12214 /* Parse a qualified-namespace-specifier.
12215
12216    qualified-namespace-specifier:
12217      :: [opt] nested-name-specifier [opt] namespace-name
12218
12219    Returns a NAMESPACE_DECL corresponding to the specified
12220    namespace.  */
12221
12222 static tree
12223 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12224 {
12225   /* Look for the optional `::'.  */
12226   cp_parser_global_scope_opt (parser,
12227                               /*current_scope_valid_p=*/false);
12228
12229   /* Look for the optional nested-name-specifier.  */
12230   cp_parser_nested_name_specifier_opt (parser,
12231                                        /*typename_keyword_p=*/false,
12232                                        /*check_dependency_p=*/true,
12233                                        /*type_p=*/false,
12234                                        /*is_declaration=*/true);
12235
12236   return cp_parser_namespace_name (parser);
12237 }
12238
12239 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12240    access declaration.
12241
12242    using-declaration:
12243      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12244      using :: unqualified-id ;  
12245
12246    access-declaration:
12247      qualified-id ;  
12248
12249    */
12250
12251 static bool
12252 cp_parser_using_declaration (cp_parser* parser, 
12253                              bool access_declaration_p)
12254 {
12255   cp_token *token;
12256   bool typename_p = false;
12257   bool global_scope_p;
12258   tree decl;
12259   tree identifier;
12260   tree qscope;
12261
12262   if (access_declaration_p)
12263     cp_parser_parse_tentatively (parser);
12264   else
12265     {
12266       /* Look for the `using' keyword.  */
12267       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12268       
12269       /* Peek at the next token.  */
12270       token = cp_lexer_peek_token (parser->lexer);
12271       /* See if it's `typename'.  */
12272       if (token->keyword == RID_TYPENAME)
12273         {
12274           /* Remember that we've seen it.  */
12275           typename_p = true;
12276           /* Consume the `typename' token.  */
12277           cp_lexer_consume_token (parser->lexer);
12278         }
12279     }
12280
12281   /* Look for the optional global scope qualification.  */
12282   global_scope_p
12283     = (cp_parser_global_scope_opt (parser,
12284                                    /*current_scope_valid_p=*/false)
12285        != NULL_TREE);
12286
12287   /* If we saw `typename', or didn't see `::', then there must be a
12288      nested-name-specifier present.  */
12289   if (typename_p || !global_scope_p)
12290     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12291                                               /*check_dependency_p=*/true,
12292                                               /*type_p=*/false,
12293                                               /*is_declaration=*/true);
12294   /* Otherwise, we could be in either of the two productions.  In that
12295      case, treat the nested-name-specifier as optional.  */
12296   else
12297     qscope = cp_parser_nested_name_specifier_opt (parser,
12298                                                   /*typename_keyword_p=*/false,
12299                                                   /*check_dependency_p=*/true,
12300                                                   /*type_p=*/false,
12301                                                   /*is_declaration=*/true);
12302   if (!qscope)
12303     qscope = global_namespace;
12304
12305   if (access_declaration_p && cp_parser_error_occurred (parser))
12306     /* Something has already gone wrong; there's no need to parse
12307        further.  Since an error has occurred, the return value of
12308        cp_parser_parse_definitely will be false, as required.  */
12309     return cp_parser_parse_definitely (parser);
12310
12311   token = cp_lexer_peek_token (parser->lexer);
12312   /* Parse the unqualified-id.  */
12313   identifier = cp_parser_unqualified_id (parser,
12314                                          /*template_keyword_p=*/false,
12315                                          /*check_dependency_p=*/true,
12316                                          /*declarator_p=*/true,
12317                                          /*optional_p=*/false);
12318
12319   if (access_declaration_p)
12320     {
12321       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12322         cp_parser_simulate_error (parser);
12323       if (!cp_parser_parse_definitely (parser))
12324         return false;
12325     }
12326
12327   /* The function we call to handle a using-declaration is different
12328      depending on what scope we are in.  */
12329   if (qscope == error_mark_node || identifier == error_mark_node)
12330     ;
12331   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12332            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12333     /* [namespace.udecl]
12334
12335        A using declaration shall not name a template-id.  */
12336     error ("%Ha template-id may not appear in a using-declaration",
12337             &token->location);
12338   else
12339     {
12340       if (at_class_scope_p ())
12341         {
12342           /* Create the USING_DECL.  */
12343           decl = do_class_using_decl (parser->scope, identifier);
12344
12345           if (check_for_bare_parameter_packs (decl))
12346             return false;
12347           else
12348             /* Add it to the list of members in this class.  */
12349             finish_member_declaration (decl);
12350         }
12351       else
12352         {
12353           decl = cp_parser_lookup_name_simple (parser,
12354                                                identifier,
12355                                                token->location);
12356           if (decl == error_mark_node)
12357             cp_parser_name_lookup_error (parser, identifier,
12358                                          decl, NULL,
12359                                          token->location);
12360           else if (check_for_bare_parameter_packs (decl))
12361             return false;
12362           else if (!at_namespace_scope_p ())
12363             do_local_using_decl (decl, qscope, identifier);
12364           else
12365             do_toplevel_using_decl (decl, qscope, identifier);
12366         }
12367     }
12368
12369   /* Look for the final `;'.  */
12370   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12371   
12372   return true;
12373 }
12374
12375 /* Parse a using-directive.
12376
12377    using-directive:
12378      using namespace :: [opt] nested-name-specifier [opt]
12379        namespace-name ;  */
12380
12381 static void
12382 cp_parser_using_directive (cp_parser* parser)
12383 {
12384   tree namespace_decl;
12385   tree attribs;
12386
12387   /* Look for the `using' keyword.  */
12388   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12389   /* And the `namespace' keyword.  */
12390   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12391   /* Look for the optional `::' operator.  */
12392   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12393   /* And the optional nested-name-specifier.  */
12394   cp_parser_nested_name_specifier_opt (parser,
12395                                        /*typename_keyword_p=*/false,
12396                                        /*check_dependency_p=*/true,
12397                                        /*type_p=*/false,
12398                                        /*is_declaration=*/true);
12399   /* Get the namespace being used.  */
12400   namespace_decl = cp_parser_namespace_name (parser);
12401   /* And any specified attributes.  */
12402   attribs = cp_parser_attributes_opt (parser);
12403   /* Update the symbol table.  */
12404   parse_using_directive (namespace_decl, attribs);
12405   /* Look for the final `;'.  */
12406   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12407 }
12408
12409 /* Parse an asm-definition.
12410
12411    asm-definition:
12412      asm ( string-literal ) ;
12413
12414    GNU Extension:
12415
12416    asm-definition:
12417      asm volatile [opt] ( string-literal ) ;
12418      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12419      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12420                           : asm-operand-list [opt] ) ;
12421      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12422                           : asm-operand-list [opt]
12423                           : asm-operand-list [opt] ) ;  */
12424
12425 static void
12426 cp_parser_asm_definition (cp_parser* parser)
12427 {
12428   tree string;
12429   tree outputs = NULL_TREE;
12430   tree inputs = NULL_TREE;
12431   tree clobbers = NULL_TREE;
12432   tree asm_stmt;
12433   bool volatile_p = false;
12434   bool extended_p = false;
12435   bool invalid_inputs_p = false;
12436   bool invalid_outputs_p = false;
12437
12438   /* Look for the `asm' keyword.  */
12439   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12440   /* See if the next token is `volatile'.  */
12441   if (cp_parser_allow_gnu_extensions_p (parser)
12442       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12443     {
12444       /* Remember that we saw the `volatile' keyword.  */
12445       volatile_p = true;
12446       /* Consume the token.  */
12447       cp_lexer_consume_token (parser->lexer);
12448     }
12449   /* Look for the opening `('.  */
12450   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12451     return;
12452   /* Look for the string.  */
12453   string = cp_parser_string_literal (parser, false, false);
12454   if (string == error_mark_node)
12455     {
12456       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12457                                              /*consume_paren=*/true);
12458       return;
12459     }
12460
12461   /* If we're allowing GNU extensions, check for the extended assembly
12462      syntax.  Unfortunately, the `:' tokens need not be separated by
12463      a space in C, and so, for compatibility, we tolerate that here
12464      too.  Doing that means that we have to treat the `::' operator as
12465      two `:' tokens.  */
12466   if (cp_parser_allow_gnu_extensions_p (parser)
12467       && parser->in_function_body
12468       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12469           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12470     {
12471       bool inputs_p = false;
12472       bool clobbers_p = false;
12473
12474       /* The extended syntax was used.  */
12475       extended_p = true;
12476
12477       /* Look for outputs.  */
12478       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12479         {
12480           /* Consume the `:'.  */
12481           cp_lexer_consume_token (parser->lexer);
12482           /* Parse the output-operands.  */
12483           if (cp_lexer_next_token_is_not (parser->lexer,
12484                                           CPP_COLON)
12485               && cp_lexer_next_token_is_not (parser->lexer,
12486                                              CPP_SCOPE)
12487               && cp_lexer_next_token_is_not (parser->lexer,
12488                                              CPP_CLOSE_PAREN))
12489             outputs = cp_parser_asm_operand_list (parser);
12490
12491             if (outputs == error_mark_node)
12492               invalid_outputs_p = true;
12493         }
12494       /* If the next token is `::', there are no outputs, and the
12495          next token is the beginning of the inputs.  */
12496       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12497         /* The inputs are coming next.  */
12498         inputs_p = true;
12499
12500       /* Look for inputs.  */
12501       if (inputs_p
12502           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12503         {
12504           /* Consume the `:' or `::'.  */
12505           cp_lexer_consume_token (parser->lexer);
12506           /* Parse the output-operands.  */
12507           if (cp_lexer_next_token_is_not (parser->lexer,
12508                                           CPP_COLON)
12509               && cp_lexer_next_token_is_not (parser->lexer,
12510                                              CPP_CLOSE_PAREN))
12511             inputs = cp_parser_asm_operand_list (parser);
12512
12513             if (inputs == error_mark_node)
12514               invalid_inputs_p = true;
12515         }
12516       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12517         /* The clobbers are coming next.  */
12518         clobbers_p = true;
12519
12520       /* Look for clobbers.  */
12521       if (clobbers_p
12522           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12523         {
12524           /* Consume the `:' or `::'.  */
12525           cp_lexer_consume_token (parser->lexer);
12526           /* Parse the clobbers.  */
12527           if (cp_lexer_next_token_is_not (parser->lexer,
12528                                           CPP_CLOSE_PAREN))
12529             clobbers = cp_parser_asm_clobber_list (parser);
12530         }
12531     }
12532   /* Look for the closing `)'.  */
12533   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12534     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12535                                            /*consume_paren=*/true);
12536   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12537
12538   if (!invalid_inputs_p && !invalid_outputs_p)
12539     {
12540       /* Create the ASM_EXPR.  */
12541       if (parser->in_function_body)
12542         {
12543           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12544                                       inputs, clobbers);
12545           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12546           if (!extended_p)
12547             {
12548               tree temp = asm_stmt;
12549               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12550                 temp = TREE_OPERAND (temp, 0);
12551
12552               ASM_INPUT_P (temp) = 1;
12553             }
12554         }
12555       else
12556         cgraph_add_asm_node (string);
12557     }
12558 }
12559
12560 /* Declarators [gram.dcl.decl] */
12561
12562 /* Parse an init-declarator.
12563
12564    init-declarator:
12565      declarator initializer [opt]
12566
12567    GNU Extension:
12568
12569    init-declarator:
12570      declarator asm-specification [opt] attributes [opt] initializer [opt]
12571
12572    function-definition:
12573      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12574        function-body
12575      decl-specifier-seq [opt] declarator function-try-block
12576
12577    GNU Extension:
12578
12579    function-definition:
12580      __extension__ function-definition
12581
12582    The DECL_SPECIFIERS apply to this declarator.  Returns a
12583    representation of the entity declared.  If MEMBER_P is TRUE, then
12584    this declarator appears in a class scope.  The new DECL created by
12585    this declarator is returned.
12586
12587    The CHECKS are access checks that should be performed once we know
12588    what entity is being declared (and, therefore, what classes have
12589    befriended it).
12590
12591    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12592    for a function-definition here as well.  If the declarator is a
12593    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12594    be TRUE upon return.  By that point, the function-definition will
12595    have been completely parsed.
12596
12597    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12598    is FALSE.  */
12599
12600 static tree
12601 cp_parser_init_declarator (cp_parser* parser,
12602                            cp_decl_specifier_seq *decl_specifiers,
12603                            VEC (deferred_access_check,gc)* checks,
12604                            bool function_definition_allowed_p,
12605                            bool member_p,
12606                            int declares_class_or_enum,
12607                            bool* function_definition_p)
12608 {
12609   cp_token *token = NULL, *asm_spec_start_token = NULL,
12610            *attributes_start_token = NULL;
12611   cp_declarator *declarator;
12612   tree prefix_attributes;
12613   tree attributes;
12614   tree asm_specification;
12615   tree initializer;
12616   tree decl = NULL_TREE;
12617   tree scope;
12618   int is_initialized;
12619   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12620      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12621      "(...)".  */
12622   enum cpp_ttype initialization_kind;
12623   bool is_direct_init = false;
12624   bool is_non_constant_init;
12625   int ctor_dtor_or_conv_p;
12626   bool friend_p;
12627   tree pushed_scope = NULL;
12628
12629   /* Gather the attributes that were provided with the
12630      decl-specifiers.  */
12631   prefix_attributes = decl_specifiers->attributes;
12632
12633   /* Assume that this is not the declarator for a function
12634      definition.  */
12635   if (function_definition_p)
12636     *function_definition_p = false;
12637
12638   /* Defer access checks while parsing the declarator; we cannot know
12639      what names are accessible until we know what is being
12640      declared.  */
12641   resume_deferring_access_checks ();
12642
12643   /* Parse the declarator.  */
12644   token = cp_lexer_peek_token (parser->lexer);
12645   declarator
12646     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12647                             &ctor_dtor_or_conv_p,
12648                             /*parenthesized_p=*/NULL,
12649                             /*member_p=*/false);
12650   /* Gather up the deferred checks.  */
12651   stop_deferring_access_checks ();
12652
12653   /* If the DECLARATOR was erroneous, there's no need to go
12654      further.  */
12655   if (declarator == cp_error_declarator)
12656     return error_mark_node;
12657
12658   /* Check that the number of template-parameter-lists is OK.  */
12659   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12660                                                        token->location))
12661     return error_mark_node;
12662
12663   if (declares_class_or_enum & 2)
12664     cp_parser_check_for_definition_in_return_type (declarator,
12665                                                    decl_specifiers->type,
12666                                                    decl_specifiers->type_location);
12667
12668   /* Figure out what scope the entity declared by the DECLARATOR is
12669      located in.  `grokdeclarator' sometimes changes the scope, so
12670      we compute it now.  */
12671   scope = get_scope_of_declarator (declarator);
12672
12673   /* If we're allowing GNU extensions, look for an asm-specification
12674      and attributes.  */
12675   if (cp_parser_allow_gnu_extensions_p (parser))
12676     {
12677       /* Look for an asm-specification.  */
12678       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12679       asm_specification = cp_parser_asm_specification_opt (parser);
12680       /* And attributes.  */
12681       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12682       attributes = cp_parser_attributes_opt (parser);
12683     }
12684   else
12685     {
12686       asm_specification = NULL_TREE;
12687       attributes = NULL_TREE;
12688     }
12689
12690   /* Peek at the next token.  */
12691   token = cp_lexer_peek_token (parser->lexer);
12692   /* Check to see if the token indicates the start of a
12693      function-definition.  */
12694   if (function_declarator_p (declarator)
12695       && cp_parser_token_starts_function_definition_p (token))
12696     {
12697       if (!function_definition_allowed_p)
12698         {
12699           /* If a function-definition should not appear here, issue an
12700              error message.  */
12701           cp_parser_error (parser,
12702                            "a function-definition is not allowed here");
12703           return error_mark_node;
12704         }
12705       else
12706         {
12707           location_t func_brace_location
12708             = cp_lexer_peek_token (parser->lexer)->location;
12709
12710           /* Neither attributes nor an asm-specification are allowed
12711              on a function-definition.  */
12712           if (asm_specification)
12713             error ("%Han asm-specification is not allowed "
12714                    "on a function-definition",
12715                    &asm_spec_start_token->location);
12716           if (attributes)
12717             error ("%Hattributes are not allowed on a function-definition",
12718                    &attributes_start_token->location);
12719           /* This is a function-definition.  */
12720           *function_definition_p = true;
12721
12722           /* Parse the function definition.  */
12723           if (member_p)
12724             decl = cp_parser_save_member_function_body (parser,
12725                                                         decl_specifiers,
12726                                                         declarator,
12727                                                         prefix_attributes);
12728           else
12729             decl
12730               = (cp_parser_function_definition_from_specifiers_and_declarator
12731                  (parser, decl_specifiers, prefix_attributes, declarator));
12732
12733           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12734             {
12735               /* This is where the prologue starts...  */
12736               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12737                 = func_brace_location;
12738             }
12739
12740           return decl;
12741         }
12742     }
12743
12744   /* [dcl.dcl]
12745
12746      Only in function declarations for constructors, destructors, and
12747      type conversions can the decl-specifier-seq be omitted.
12748
12749      We explicitly postpone this check past the point where we handle
12750      function-definitions because we tolerate function-definitions
12751      that are missing their return types in some modes.  */
12752   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12753     {
12754       cp_parser_error (parser,
12755                        "expected constructor, destructor, or type conversion");
12756       return error_mark_node;
12757     }
12758
12759   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12760   if (token->type == CPP_EQ
12761       || token->type == CPP_OPEN_PAREN
12762       || token->type == CPP_OPEN_BRACE)
12763     {
12764       is_initialized = SD_INITIALIZED;
12765       initialization_kind = token->type;
12766
12767       if (token->type == CPP_EQ
12768           && function_declarator_p (declarator))
12769         {
12770           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12771           if (t2->keyword == RID_DEFAULT)
12772             is_initialized = SD_DEFAULTED;
12773           else if (t2->keyword == RID_DELETE)
12774             is_initialized = SD_DELETED;
12775         }
12776     }
12777   else
12778     {
12779       /* If the init-declarator isn't initialized and isn't followed by a
12780          `,' or `;', it's not a valid init-declarator.  */
12781       if (token->type != CPP_COMMA
12782           && token->type != CPP_SEMICOLON)
12783         {
12784           cp_parser_error (parser, "expected initializer");
12785           return error_mark_node;
12786         }
12787       is_initialized = SD_UNINITIALIZED;
12788       initialization_kind = CPP_EOF;
12789     }
12790
12791   /* Because start_decl has side-effects, we should only call it if we
12792      know we're going ahead.  By this point, we know that we cannot
12793      possibly be looking at any other construct.  */
12794   cp_parser_commit_to_tentative_parse (parser);
12795
12796   /* If the decl specifiers were bad, issue an error now that we're
12797      sure this was intended to be a declarator.  Then continue
12798      declaring the variable(s), as int, to try to cut down on further
12799      errors.  */
12800   if (decl_specifiers->any_specifiers_p
12801       && decl_specifiers->type == error_mark_node)
12802     {
12803       cp_parser_error (parser, "invalid type in declaration");
12804       decl_specifiers->type = integer_type_node;
12805     }
12806
12807   /* Check to see whether or not this declaration is a friend.  */
12808   friend_p = cp_parser_friend_p (decl_specifiers);
12809
12810   /* Enter the newly declared entry in the symbol table.  If we're
12811      processing a declaration in a class-specifier, we wait until
12812      after processing the initializer.  */
12813   if (!member_p)
12814     {
12815       if (parser->in_unbraced_linkage_specification_p)
12816         decl_specifiers->storage_class = sc_extern;
12817       decl = start_decl (declarator, decl_specifiers,
12818                          is_initialized, attributes, prefix_attributes,
12819                          &pushed_scope);
12820     }
12821   else if (scope)
12822     /* Enter the SCOPE.  That way unqualified names appearing in the
12823        initializer will be looked up in SCOPE.  */
12824     pushed_scope = push_scope (scope);
12825
12826   /* Perform deferred access control checks, now that we know in which
12827      SCOPE the declared entity resides.  */
12828   if (!member_p && decl)
12829     {
12830       tree saved_current_function_decl = NULL_TREE;
12831
12832       /* If the entity being declared is a function, pretend that we
12833          are in its scope.  If it is a `friend', it may have access to
12834          things that would not otherwise be accessible.  */
12835       if (TREE_CODE (decl) == FUNCTION_DECL)
12836         {
12837           saved_current_function_decl = current_function_decl;
12838           current_function_decl = decl;
12839         }
12840
12841       /* Perform access checks for template parameters.  */
12842       cp_parser_perform_template_parameter_access_checks (checks);
12843
12844       /* Perform the access control checks for the declarator and the
12845          decl-specifiers.  */
12846       perform_deferred_access_checks ();
12847
12848       /* Restore the saved value.  */
12849       if (TREE_CODE (decl) == FUNCTION_DECL)
12850         current_function_decl = saved_current_function_decl;
12851     }
12852
12853   /* Parse the initializer.  */
12854   initializer = NULL_TREE;
12855   is_direct_init = false;
12856   is_non_constant_init = true;
12857   if (is_initialized)
12858     {
12859       if (function_declarator_p (declarator))
12860         {
12861           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12862            if (initialization_kind == CPP_EQ)
12863              initializer = cp_parser_pure_specifier (parser);
12864            else
12865              {
12866                /* If the declaration was erroneous, we don't really
12867                   know what the user intended, so just silently
12868                   consume the initializer.  */
12869                if (decl != error_mark_node)
12870                  error ("%Hinitializer provided for function",
12871                         &initializer_start_token->location);
12872                cp_parser_skip_to_closing_parenthesis (parser,
12873                                                       /*recovering=*/true,
12874                                                       /*or_comma=*/false,
12875                                                       /*consume_paren=*/true);
12876              }
12877         }
12878       else
12879         initializer = cp_parser_initializer (parser,
12880                                              &is_direct_init,
12881                                              &is_non_constant_init);
12882     }
12883
12884   /* The old parser allows attributes to appear after a parenthesized
12885      initializer.  Mark Mitchell proposed removing this functionality
12886      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12887      attributes -- but ignores them.  */
12888   if (cp_parser_allow_gnu_extensions_p (parser)
12889       && initialization_kind == CPP_OPEN_PAREN)
12890     if (cp_parser_attributes_opt (parser))
12891       warning (OPT_Wattributes,
12892                "attributes after parenthesized initializer ignored");
12893
12894   /* For an in-class declaration, use `grokfield' to create the
12895      declaration.  */
12896   if (member_p)
12897     {
12898       if (pushed_scope)
12899         {
12900           pop_scope (pushed_scope);
12901           pushed_scope = false;
12902         }
12903       decl = grokfield (declarator, decl_specifiers,
12904                         initializer, !is_non_constant_init,
12905                         /*asmspec=*/NULL_TREE,
12906                         prefix_attributes);
12907       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12908         cp_parser_save_default_args (parser, decl);
12909     }
12910
12911   /* Finish processing the declaration.  But, skip friend
12912      declarations.  */
12913   if (!friend_p && decl && decl != error_mark_node)
12914     {
12915       cp_finish_decl (decl,
12916                       initializer, !is_non_constant_init,
12917                       asm_specification,
12918                       /* If the initializer is in parentheses, then this is
12919                          a direct-initialization, which means that an
12920                          `explicit' constructor is OK.  Otherwise, an
12921                          `explicit' constructor cannot be used.  */
12922                       ((is_direct_init || !is_initialized)
12923                        ? 0 : LOOKUP_ONLYCONVERTING));
12924     }
12925   else if ((cxx_dialect != cxx98) && friend_p
12926            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12927     /* Core issue #226 (C++0x only): A default template-argument
12928        shall not be specified in a friend class template
12929        declaration. */
12930     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12931                              /*is_partial=*/0, /*is_friend_decl=*/1);
12932
12933   if (!friend_p && pushed_scope)
12934     pop_scope (pushed_scope);
12935
12936   return decl;
12937 }
12938
12939 /* Parse a declarator.
12940
12941    declarator:
12942      direct-declarator
12943      ptr-operator declarator
12944
12945    abstract-declarator:
12946      ptr-operator abstract-declarator [opt]
12947      direct-abstract-declarator
12948
12949    GNU Extensions:
12950
12951    declarator:
12952      attributes [opt] direct-declarator
12953      attributes [opt] ptr-operator declarator
12954
12955    abstract-declarator:
12956      attributes [opt] ptr-operator abstract-declarator [opt]
12957      attributes [opt] direct-abstract-declarator
12958
12959    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12960    detect constructor, destructor or conversion operators. It is set
12961    to -1 if the declarator is a name, and +1 if it is a
12962    function. Otherwise it is set to zero. Usually you just want to
12963    test for >0, but internally the negative value is used.
12964
12965    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12966    a decl-specifier-seq unless it declares a constructor, destructor,
12967    or conversion.  It might seem that we could check this condition in
12968    semantic analysis, rather than parsing, but that makes it difficult
12969    to handle something like `f()'.  We want to notice that there are
12970    no decl-specifiers, and therefore realize that this is an
12971    expression, not a declaration.)
12972
12973    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12974    the declarator is a direct-declarator of the form "(...)".
12975
12976    MEMBER_P is true iff this declarator is a member-declarator.  */
12977
12978 static cp_declarator *
12979 cp_parser_declarator (cp_parser* parser,
12980                       cp_parser_declarator_kind dcl_kind,
12981                       int* ctor_dtor_or_conv_p,
12982                       bool* parenthesized_p,
12983                       bool member_p)
12984 {
12985   cp_token *token;
12986   cp_declarator *declarator;
12987   enum tree_code code;
12988   cp_cv_quals cv_quals;
12989   tree class_type;
12990   tree attributes = NULL_TREE;
12991
12992   /* Assume this is not a constructor, destructor, or type-conversion
12993      operator.  */
12994   if (ctor_dtor_or_conv_p)
12995     *ctor_dtor_or_conv_p = 0;
12996
12997   if (cp_parser_allow_gnu_extensions_p (parser))
12998     attributes = cp_parser_attributes_opt (parser);
12999
13000   /* Peek at the next token.  */
13001   token = cp_lexer_peek_token (parser->lexer);
13002
13003   /* Check for the ptr-operator production.  */
13004   cp_parser_parse_tentatively (parser);
13005   /* Parse the ptr-operator.  */
13006   code = cp_parser_ptr_operator (parser,
13007                                  &class_type,
13008                                  &cv_quals);
13009   /* If that worked, then we have a ptr-operator.  */
13010   if (cp_parser_parse_definitely (parser))
13011     {
13012       /* If a ptr-operator was found, then this declarator was not
13013          parenthesized.  */
13014       if (parenthesized_p)
13015         *parenthesized_p = true;
13016       /* The dependent declarator is optional if we are parsing an
13017          abstract-declarator.  */
13018       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13019         cp_parser_parse_tentatively (parser);
13020
13021       /* Parse the dependent declarator.  */
13022       declarator = cp_parser_declarator (parser, dcl_kind,
13023                                          /*ctor_dtor_or_conv_p=*/NULL,
13024                                          /*parenthesized_p=*/NULL,
13025                                          /*member_p=*/false);
13026
13027       /* If we are parsing an abstract-declarator, we must handle the
13028          case where the dependent declarator is absent.  */
13029       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13030           && !cp_parser_parse_definitely (parser))
13031         declarator = NULL;
13032
13033       declarator = cp_parser_make_indirect_declarator
13034         (code, class_type, cv_quals, declarator);
13035     }
13036   /* Everything else is a direct-declarator.  */
13037   else
13038     {
13039       if (parenthesized_p)
13040         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13041                                                    CPP_OPEN_PAREN);
13042       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13043                                                 ctor_dtor_or_conv_p,
13044                                                 member_p);
13045     }
13046
13047   if (attributes && declarator && declarator != cp_error_declarator)
13048     declarator->attributes = attributes;
13049
13050   return declarator;
13051 }
13052
13053 /* Parse a direct-declarator or direct-abstract-declarator.
13054
13055    direct-declarator:
13056      declarator-id
13057      direct-declarator ( parameter-declaration-clause )
13058        cv-qualifier-seq [opt]
13059        exception-specification [opt]
13060      direct-declarator [ constant-expression [opt] ]
13061      ( declarator )
13062
13063    direct-abstract-declarator:
13064      direct-abstract-declarator [opt]
13065        ( parameter-declaration-clause )
13066        cv-qualifier-seq [opt]
13067        exception-specification [opt]
13068      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13069      ( abstract-declarator )
13070
13071    Returns a representation of the declarator.  DCL_KIND is
13072    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13073    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13074    we are parsing a direct-declarator.  It is
13075    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13076    of ambiguity we prefer an abstract declarator, as per
13077    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13078    cp_parser_declarator.  */
13079
13080 static cp_declarator *
13081 cp_parser_direct_declarator (cp_parser* parser,
13082                              cp_parser_declarator_kind dcl_kind,
13083                              int* ctor_dtor_or_conv_p,
13084                              bool member_p)
13085 {
13086   cp_token *token;
13087   cp_declarator *declarator = NULL;
13088   tree scope = NULL_TREE;
13089   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13090   bool saved_in_declarator_p = parser->in_declarator_p;
13091   bool first = true;
13092   tree pushed_scope = NULL_TREE;
13093
13094   while (true)
13095     {
13096       /* Peek at the next token.  */
13097       token = cp_lexer_peek_token (parser->lexer);
13098       if (token->type == CPP_OPEN_PAREN)
13099         {
13100           /* This is either a parameter-declaration-clause, or a
13101              parenthesized declarator. When we know we are parsing a
13102              named declarator, it must be a parenthesized declarator
13103              if FIRST is true. For instance, `(int)' is a
13104              parameter-declaration-clause, with an omitted
13105              direct-abstract-declarator. But `((*))', is a
13106              parenthesized abstract declarator. Finally, when T is a
13107              template parameter `(T)' is a
13108              parameter-declaration-clause, and not a parenthesized
13109              named declarator.
13110
13111              We first try and parse a parameter-declaration-clause,
13112              and then try a nested declarator (if FIRST is true).
13113
13114              It is not an error for it not to be a
13115              parameter-declaration-clause, even when FIRST is
13116              false. Consider,
13117
13118                int i (int);
13119                int i (3);
13120
13121              The first is the declaration of a function while the
13122              second is the definition of a variable, including its
13123              initializer.
13124
13125              Having seen only the parenthesis, we cannot know which of
13126              these two alternatives should be selected.  Even more
13127              complex are examples like:
13128
13129                int i (int (a));
13130                int i (int (3));
13131
13132              The former is a function-declaration; the latter is a
13133              variable initialization.
13134
13135              Thus again, we try a parameter-declaration-clause, and if
13136              that fails, we back out and return.  */
13137
13138           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13139             {
13140               tree params;
13141               unsigned saved_num_template_parameter_lists;
13142               bool is_declarator = false;
13143               tree t;
13144
13145               /* In a member-declarator, the only valid interpretation
13146                  of a parenthesis is the start of a
13147                  parameter-declaration-clause.  (It is invalid to
13148                  initialize a static data member with a parenthesized
13149                  initializer; only the "=" form of initialization is
13150                  permitted.)  */
13151               if (!member_p)
13152                 cp_parser_parse_tentatively (parser);
13153
13154               /* Consume the `('.  */
13155               cp_lexer_consume_token (parser->lexer);
13156               if (first)
13157                 {
13158                   /* If this is going to be an abstract declarator, we're
13159                      in a declarator and we can't have default args.  */
13160                   parser->default_arg_ok_p = false;
13161                   parser->in_declarator_p = true;
13162                 }
13163
13164               /* Inside the function parameter list, surrounding
13165                  template-parameter-lists do not apply.  */
13166               saved_num_template_parameter_lists
13167                 = parser->num_template_parameter_lists;
13168               parser->num_template_parameter_lists = 0;
13169
13170               begin_scope (sk_function_parms, NULL_TREE);
13171
13172               /* Parse the parameter-declaration-clause.  */
13173               params = cp_parser_parameter_declaration_clause (parser);
13174
13175               parser->num_template_parameter_lists
13176                 = saved_num_template_parameter_lists;
13177
13178               /* If all went well, parse the cv-qualifier-seq and the
13179                  exception-specification.  */
13180               if (member_p || cp_parser_parse_definitely (parser))
13181                 {
13182                   cp_cv_quals cv_quals;
13183                   tree exception_specification;
13184                   tree late_return;
13185
13186                   is_declarator = true;
13187
13188                   if (ctor_dtor_or_conv_p)
13189                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13190                   first = false;
13191                   /* Consume the `)'.  */
13192                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13193
13194                   /* Parse the cv-qualifier-seq.  */
13195                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13196                   /* And the exception-specification.  */
13197                   exception_specification
13198                     = cp_parser_exception_specification_opt (parser);
13199
13200                   late_return
13201                     = cp_parser_late_return_type_opt (parser);
13202
13203                   /* Create the function-declarator.  */
13204                   declarator = make_call_declarator (declarator,
13205                                                      params,
13206                                                      cv_quals,
13207                                                      exception_specification,
13208                                                      late_return);
13209                   /* Any subsequent parameter lists are to do with
13210                      return type, so are not those of the declared
13211                      function.  */
13212                   parser->default_arg_ok_p = false;
13213                 }
13214
13215               /* Remove the function parms from scope.  */
13216               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13217                 pop_binding (DECL_NAME (t), t);
13218               leave_scope();
13219
13220               if (is_declarator)
13221                 /* Repeat the main loop.  */
13222                 continue;
13223             }
13224
13225           /* If this is the first, we can try a parenthesized
13226              declarator.  */
13227           if (first)
13228             {
13229               bool saved_in_type_id_in_expr_p;
13230
13231               parser->default_arg_ok_p = saved_default_arg_ok_p;
13232               parser->in_declarator_p = saved_in_declarator_p;
13233
13234               /* Consume the `('.  */
13235               cp_lexer_consume_token (parser->lexer);
13236               /* Parse the nested declarator.  */
13237               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13238               parser->in_type_id_in_expr_p = true;
13239               declarator
13240                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13241                                         /*parenthesized_p=*/NULL,
13242                                         member_p);
13243               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13244               first = false;
13245               /* Expect a `)'.  */
13246               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13247                 declarator = cp_error_declarator;
13248               if (declarator == cp_error_declarator)
13249                 break;
13250
13251               goto handle_declarator;
13252             }
13253           /* Otherwise, we must be done.  */
13254           else
13255             break;
13256         }
13257       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13258                && token->type == CPP_OPEN_SQUARE)
13259         {
13260           /* Parse an array-declarator.  */
13261           tree bounds;
13262
13263           if (ctor_dtor_or_conv_p)
13264             *ctor_dtor_or_conv_p = 0;
13265
13266           first = false;
13267           parser->default_arg_ok_p = false;
13268           parser->in_declarator_p = true;
13269           /* Consume the `['.  */
13270           cp_lexer_consume_token (parser->lexer);
13271           /* Peek at the next token.  */
13272           token = cp_lexer_peek_token (parser->lexer);
13273           /* If the next token is `]', then there is no
13274              constant-expression.  */
13275           if (token->type != CPP_CLOSE_SQUARE)
13276             {
13277               bool non_constant_p;
13278
13279               bounds
13280                 = cp_parser_constant_expression (parser,
13281                                                  /*allow_non_constant=*/true,
13282                                                  &non_constant_p);
13283               if (!non_constant_p)
13284                 bounds = fold_non_dependent_expr (bounds);
13285               else if (processing_template_decl)
13286                 {
13287                   /* Remember this wasn't a constant-expression.  */
13288                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13289                   TREE_SIDE_EFFECTS (bounds) = 1;
13290                 }
13291
13292               /* Normally, the array bound must be an integral constant
13293                  expression.  However, as an extension, we allow VLAs
13294                  in function scopes.  */
13295               else if (!parser->in_function_body)
13296                 {
13297                   error ("%Harray bound is not an integer constant",
13298                          &token->location);
13299                   bounds = error_mark_node;
13300                 }
13301             }
13302           else
13303             bounds = NULL_TREE;
13304           /* Look for the closing `]'.  */
13305           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13306             {
13307               declarator = cp_error_declarator;
13308               break;
13309             }
13310
13311           declarator = make_array_declarator (declarator, bounds);
13312         }
13313       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13314         {
13315           tree qualifying_scope;
13316           tree unqualified_name;
13317           special_function_kind sfk;
13318           bool abstract_ok;
13319           bool pack_expansion_p = false;
13320           cp_token *declarator_id_start_token;
13321
13322           /* Parse a declarator-id */
13323           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13324           if (abstract_ok)
13325             {
13326               cp_parser_parse_tentatively (parser);
13327
13328               /* If we see an ellipsis, we should be looking at a
13329                  parameter pack. */
13330               if (token->type == CPP_ELLIPSIS)
13331                 {
13332                   /* Consume the `...' */
13333                   cp_lexer_consume_token (parser->lexer);
13334
13335                   pack_expansion_p = true;
13336                 }
13337             }
13338
13339           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13340           unqualified_name
13341             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13342           qualifying_scope = parser->scope;
13343           if (abstract_ok)
13344             {
13345               bool okay = false;
13346
13347               if (!unqualified_name && pack_expansion_p)
13348                 {
13349                   /* Check whether an error occurred. */
13350                   okay = !cp_parser_error_occurred (parser);
13351
13352                   /* We already consumed the ellipsis to mark a
13353                      parameter pack, but we have no way to report it,
13354                      so abort the tentative parse. We will be exiting
13355                      immediately anyway. */
13356                   cp_parser_abort_tentative_parse (parser);
13357                 }
13358               else
13359                 okay = cp_parser_parse_definitely (parser);
13360
13361               if (!okay)
13362                 unqualified_name = error_mark_node;
13363               else if (unqualified_name
13364                        && (qualifying_scope
13365                            || (TREE_CODE (unqualified_name)
13366                                != IDENTIFIER_NODE)))
13367                 {
13368                   cp_parser_error (parser, "expected unqualified-id");
13369                   unqualified_name = error_mark_node;
13370                 }
13371             }
13372
13373           if (!unqualified_name)
13374             return NULL;
13375           if (unqualified_name == error_mark_node)
13376             {
13377               declarator = cp_error_declarator;
13378               pack_expansion_p = false;
13379               declarator->parameter_pack_p = false;
13380               break;
13381             }
13382
13383           if (qualifying_scope && at_namespace_scope_p ()
13384               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13385             {
13386               /* In the declaration of a member of a template class
13387                  outside of the class itself, the SCOPE will sometimes
13388                  be a TYPENAME_TYPE.  For example, given:
13389
13390                  template <typename T>
13391                  int S<T>::R::i = 3;
13392
13393                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13394                  this context, we must resolve S<T>::R to an ordinary
13395                  type, rather than a typename type.
13396
13397                  The reason we normally avoid resolving TYPENAME_TYPEs
13398                  is that a specialization of `S' might render
13399                  `S<T>::R' not a type.  However, if `S' is
13400                  specialized, then this `i' will not be used, so there
13401                  is no harm in resolving the types here.  */
13402               tree type;
13403
13404               /* Resolve the TYPENAME_TYPE.  */
13405               type = resolve_typename_type (qualifying_scope,
13406                                             /*only_current_p=*/false);
13407               /* If that failed, the declarator is invalid.  */
13408               if (TREE_CODE (type) == TYPENAME_TYPE)
13409                 error ("%H%<%T::%E%> is not a type",
13410                        &declarator_id_start_token->location,
13411                        TYPE_CONTEXT (qualifying_scope),
13412                        TYPE_IDENTIFIER (qualifying_scope));
13413               qualifying_scope = type;
13414             }
13415
13416           sfk = sfk_none;
13417
13418           if (unqualified_name)
13419             {
13420               tree class_type;
13421
13422               if (qualifying_scope
13423                   && CLASS_TYPE_P (qualifying_scope))
13424                 class_type = qualifying_scope;
13425               else
13426                 class_type = current_class_type;
13427
13428               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13429                 {
13430                   tree name_type = TREE_TYPE (unqualified_name);
13431                   if (class_type && same_type_p (name_type, class_type))
13432                     {
13433                       if (qualifying_scope
13434                           && CLASSTYPE_USE_TEMPLATE (name_type))
13435                         {
13436                           error ("%Hinvalid use of constructor as a template",
13437                                  &declarator_id_start_token->location);
13438                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13439                                   "name the constructor in a qualified name",
13440                                   class_type,
13441                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13442                                   class_type, name_type);
13443                           declarator = cp_error_declarator;
13444                           break;
13445                         }
13446                       else
13447                         unqualified_name = constructor_name (class_type);
13448                     }
13449                   else
13450                     {
13451                       /* We do not attempt to print the declarator
13452                          here because we do not have enough
13453                          information about its original syntactic
13454                          form.  */
13455                       cp_parser_error (parser, "invalid declarator");
13456                       declarator = cp_error_declarator;
13457                       break;
13458                     }
13459                 }
13460
13461               if (class_type)
13462                 {
13463                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13464                     sfk = sfk_destructor;
13465                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13466                     sfk = sfk_conversion;
13467                   else if (/* There's no way to declare a constructor
13468                               for an anonymous type, even if the type
13469                               got a name for linkage purposes.  */
13470                            !TYPE_WAS_ANONYMOUS (class_type)
13471                            && constructor_name_p (unqualified_name,
13472                                                   class_type))
13473                     {
13474                       unqualified_name = constructor_name (class_type);
13475                       sfk = sfk_constructor;
13476                     }
13477
13478                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13479                     *ctor_dtor_or_conv_p = -1;
13480                 }
13481             }
13482           declarator = make_id_declarator (qualifying_scope,
13483                                            unqualified_name,
13484                                            sfk);
13485           declarator->id_loc = token->location;
13486           declarator->parameter_pack_p = pack_expansion_p;
13487
13488           if (pack_expansion_p)
13489             maybe_warn_variadic_templates ();
13490
13491         handle_declarator:;
13492           scope = get_scope_of_declarator (declarator);
13493           if (scope)
13494             /* Any names that appear after the declarator-id for a
13495                member are looked up in the containing scope.  */
13496             pushed_scope = push_scope (scope);
13497           parser->in_declarator_p = true;
13498           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13499               || (declarator && declarator->kind == cdk_id))
13500             /* Default args are only allowed on function
13501                declarations.  */
13502             parser->default_arg_ok_p = saved_default_arg_ok_p;
13503           else
13504             parser->default_arg_ok_p = false;
13505
13506           first = false;
13507         }
13508       /* We're done.  */
13509       else
13510         break;
13511     }
13512
13513   /* For an abstract declarator, we might wind up with nothing at this
13514      point.  That's an error; the declarator is not optional.  */
13515   if (!declarator)
13516     cp_parser_error (parser, "expected declarator");
13517
13518   /* If we entered a scope, we must exit it now.  */
13519   if (pushed_scope)
13520     pop_scope (pushed_scope);
13521
13522   parser->default_arg_ok_p = saved_default_arg_ok_p;
13523   parser->in_declarator_p = saved_in_declarator_p;
13524
13525   return declarator;
13526 }
13527
13528 /* Parse a ptr-operator.
13529
13530    ptr-operator:
13531      * cv-qualifier-seq [opt]
13532      &
13533      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13534
13535    GNU Extension:
13536
13537    ptr-operator:
13538      & cv-qualifier-seq [opt]
13539
13540    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13541    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13542    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13543    filled in with the TYPE containing the member.  *CV_QUALS is
13544    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13545    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13546    Note that the tree codes returned by this function have nothing
13547    to do with the types of trees that will be eventually be created
13548    to represent the pointer or reference type being parsed. They are
13549    just constants with suggestive names. */
13550 static enum tree_code
13551 cp_parser_ptr_operator (cp_parser* parser,
13552                         tree* type,
13553                         cp_cv_quals *cv_quals)
13554 {
13555   enum tree_code code = ERROR_MARK;
13556   cp_token *token;
13557
13558   /* Assume that it's not a pointer-to-member.  */
13559   *type = NULL_TREE;
13560   /* And that there are no cv-qualifiers.  */
13561   *cv_quals = TYPE_UNQUALIFIED;
13562
13563   /* Peek at the next token.  */
13564   token = cp_lexer_peek_token (parser->lexer);
13565
13566   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13567   if (token->type == CPP_MULT)
13568     code = INDIRECT_REF;
13569   else if (token->type == CPP_AND)
13570     code = ADDR_EXPR;
13571   else if ((cxx_dialect != cxx98) &&
13572            token->type == CPP_AND_AND) /* C++0x only */
13573     code = NON_LVALUE_EXPR;
13574
13575   if (code != ERROR_MARK)
13576     {
13577       /* Consume the `*', `&' or `&&'.  */
13578       cp_lexer_consume_token (parser->lexer);
13579
13580       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13581          `&', if we are allowing GNU extensions.  (The only qualifier
13582          that can legally appear after `&' is `restrict', but that is
13583          enforced during semantic analysis.  */
13584       if (code == INDIRECT_REF
13585           || cp_parser_allow_gnu_extensions_p (parser))
13586         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13587     }
13588   else
13589     {
13590       /* Try the pointer-to-member case.  */
13591       cp_parser_parse_tentatively (parser);
13592       /* Look for the optional `::' operator.  */
13593       cp_parser_global_scope_opt (parser,
13594                                   /*current_scope_valid_p=*/false);
13595       /* Look for the nested-name specifier.  */
13596       token = cp_lexer_peek_token (parser->lexer);
13597       cp_parser_nested_name_specifier (parser,
13598                                        /*typename_keyword_p=*/false,
13599                                        /*check_dependency_p=*/true,
13600                                        /*type_p=*/false,
13601                                        /*is_declaration=*/false);
13602       /* If we found it, and the next token is a `*', then we are
13603          indeed looking at a pointer-to-member operator.  */
13604       if (!cp_parser_error_occurred (parser)
13605           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13606         {
13607           /* Indicate that the `*' operator was used.  */
13608           code = INDIRECT_REF;
13609
13610           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13611             error ("%H%qD is a namespace", &token->location, parser->scope);
13612           else
13613             {
13614               /* The type of which the member is a member is given by the
13615                  current SCOPE.  */
13616               *type = parser->scope;
13617               /* The next name will not be qualified.  */
13618               parser->scope = NULL_TREE;
13619               parser->qualifying_scope = NULL_TREE;
13620               parser->object_scope = NULL_TREE;
13621               /* Look for the optional cv-qualifier-seq.  */
13622               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13623             }
13624         }
13625       /* If that didn't work we don't have a ptr-operator.  */
13626       if (!cp_parser_parse_definitely (parser))
13627         cp_parser_error (parser, "expected ptr-operator");
13628     }
13629
13630   return code;
13631 }
13632
13633 /* Parse an (optional) cv-qualifier-seq.
13634
13635    cv-qualifier-seq:
13636      cv-qualifier cv-qualifier-seq [opt]
13637
13638    cv-qualifier:
13639      const
13640      volatile
13641
13642    GNU Extension:
13643
13644    cv-qualifier:
13645      __restrict__
13646
13647    Returns a bitmask representing the cv-qualifiers.  */
13648
13649 static cp_cv_quals
13650 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13651 {
13652   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13653
13654   while (true)
13655     {
13656       cp_token *token;
13657       cp_cv_quals cv_qualifier;
13658
13659       /* Peek at the next token.  */
13660       token = cp_lexer_peek_token (parser->lexer);
13661       /* See if it's a cv-qualifier.  */
13662       switch (token->keyword)
13663         {
13664         case RID_CONST:
13665           cv_qualifier = TYPE_QUAL_CONST;
13666           break;
13667
13668         case RID_VOLATILE:
13669           cv_qualifier = TYPE_QUAL_VOLATILE;
13670           break;
13671
13672         case RID_RESTRICT:
13673           cv_qualifier = TYPE_QUAL_RESTRICT;
13674           break;
13675
13676         default:
13677           cv_qualifier = TYPE_UNQUALIFIED;
13678           break;
13679         }
13680
13681       if (!cv_qualifier)
13682         break;
13683
13684       if (cv_quals & cv_qualifier)
13685         {
13686           error ("%Hduplicate cv-qualifier", &token->location);
13687           cp_lexer_purge_token (parser->lexer);
13688         }
13689       else
13690         {
13691           cp_lexer_consume_token (parser->lexer);
13692           cv_quals |= cv_qualifier;
13693         }
13694     }
13695
13696   return cv_quals;
13697 }
13698
13699 /* Parse a late-specified return type, if any.  This is not a separate
13700    non-terminal, but part of a function declarator, which looks like
13701
13702    -> type-id
13703
13704    Returns the type indicated by the type-id.  */
13705
13706 static tree
13707 cp_parser_late_return_type_opt (cp_parser* parser)
13708 {
13709   cp_token *token;
13710
13711   /* Peek at the next token.  */
13712   token = cp_lexer_peek_token (parser->lexer);
13713   /* A late-specified return type is indicated by an initial '->'. */
13714   if (token->type != CPP_DEREF)
13715     return NULL_TREE;
13716
13717   /* Consume the ->.  */
13718   cp_lexer_consume_token (parser->lexer);
13719
13720   return cp_parser_type_id (parser);
13721 }
13722
13723 /* Parse a declarator-id.
13724
13725    declarator-id:
13726      id-expression
13727      :: [opt] nested-name-specifier [opt] type-name
13728
13729    In the `id-expression' case, the value returned is as for
13730    cp_parser_id_expression if the id-expression was an unqualified-id.
13731    If the id-expression was a qualified-id, then a SCOPE_REF is
13732    returned.  The first operand is the scope (either a NAMESPACE_DECL
13733    or TREE_TYPE), but the second is still just a representation of an
13734    unqualified-id.  */
13735
13736 static tree
13737 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13738 {
13739   tree id;
13740   /* The expression must be an id-expression.  Assume that qualified
13741      names are the names of types so that:
13742
13743        template <class T>
13744        int S<T>::R::i = 3;
13745
13746      will work; we must treat `S<T>::R' as the name of a type.
13747      Similarly, assume that qualified names are templates, where
13748      required, so that:
13749
13750        template <class T>
13751        int S<T>::R<T>::i = 3;
13752
13753      will work, too.  */
13754   id = cp_parser_id_expression (parser,
13755                                 /*template_keyword_p=*/false,
13756                                 /*check_dependency_p=*/false,
13757                                 /*template_p=*/NULL,
13758                                 /*declarator_p=*/true,
13759                                 optional_p);
13760   if (id && BASELINK_P (id))
13761     id = BASELINK_FUNCTIONS (id);
13762   return id;
13763 }
13764
13765 /* Parse a type-id.
13766
13767    type-id:
13768      type-specifier-seq abstract-declarator [opt]
13769
13770    Returns the TYPE specified.  */
13771
13772 static tree
13773 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13774 {
13775   cp_decl_specifier_seq type_specifier_seq;
13776   cp_declarator *abstract_declarator;
13777
13778   /* Parse the type-specifier-seq.  */
13779   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13780                                 &type_specifier_seq);
13781   if (type_specifier_seq.type == error_mark_node)
13782     return error_mark_node;
13783
13784   /* There might or might not be an abstract declarator.  */
13785   cp_parser_parse_tentatively (parser);
13786   /* Look for the declarator.  */
13787   abstract_declarator
13788     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13789                             /*parenthesized_p=*/NULL,
13790                             /*member_p=*/false);
13791   /* Check to see if there really was a declarator.  */
13792   if (!cp_parser_parse_definitely (parser))
13793     abstract_declarator = NULL;
13794
13795   if (type_specifier_seq.type
13796       && type_uses_auto (type_specifier_seq.type))
13797     {
13798       error ("invalid use of %<auto%>");
13799       return error_mark_node;
13800     }
13801   
13802   return groktypename (&type_specifier_seq, abstract_declarator,
13803                        is_template_arg);
13804 }
13805
13806 static tree cp_parser_type_id (cp_parser *parser)
13807 {
13808   return cp_parser_type_id_1 (parser, false);
13809 }
13810
13811 static tree cp_parser_template_type_arg (cp_parser *parser)
13812 {
13813   return cp_parser_type_id_1 (parser, true);
13814 }
13815
13816 /* Parse a type-specifier-seq.
13817
13818    type-specifier-seq:
13819      type-specifier type-specifier-seq [opt]
13820
13821    GNU extension:
13822
13823    type-specifier-seq:
13824      attributes type-specifier-seq [opt]
13825
13826    If IS_CONDITION is true, we are at the start of a "condition",
13827    e.g., we've just seen "if (".
13828
13829    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13830
13831 static void
13832 cp_parser_type_specifier_seq (cp_parser* parser,
13833                               bool is_condition,
13834                               cp_decl_specifier_seq *type_specifier_seq)
13835 {
13836   bool seen_type_specifier = false;
13837   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13838   cp_token *start_token = NULL;
13839
13840   /* Clear the TYPE_SPECIFIER_SEQ.  */
13841   clear_decl_specs (type_specifier_seq);
13842
13843   /* Parse the type-specifiers and attributes.  */
13844   while (true)
13845     {
13846       tree type_specifier;
13847       bool is_cv_qualifier;
13848
13849       /* Check for attributes first.  */
13850       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13851         {
13852           type_specifier_seq->attributes =
13853             chainon (type_specifier_seq->attributes,
13854                      cp_parser_attributes_opt (parser));
13855           continue;
13856         }
13857
13858       /* record the token of the beginning of the type specifier seq,
13859          for error reporting purposes*/
13860      if (!start_token)
13861        start_token = cp_lexer_peek_token (parser->lexer);
13862
13863       /* Look for the type-specifier.  */
13864       type_specifier = cp_parser_type_specifier (parser,
13865                                                  flags,
13866                                                  type_specifier_seq,
13867                                                  /*is_declaration=*/false,
13868                                                  NULL,
13869                                                  &is_cv_qualifier);
13870       if (!type_specifier)
13871         {
13872           /* If the first type-specifier could not be found, this is not a
13873              type-specifier-seq at all.  */
13874           if (!seen_type_specifier)
13875             {
13876               cp_parser_error (parser, "expected type-specifier");
13877               type_specifier_seq->type = error_mark_node;
13878               return;
13879             }
13880           /* If subsequent type-specifiers could not be found, the
13881              type-specifier-seq is complete.  */
13882           break;
13883         }
13884
13885       seen_type_specifier = true;
13886       /* The standard says that a condition can be:
13887
13888             type-specifier-seq declarator = assignment-expression
13889
13890          However, given:
13891
13892            struct S {};
13893            if (int S = ...)
13894
13895          we should treat the "S" as a declarator, not as a
13896          type-specifier.  The standard doesn't say that explicitly for
13897          type-specifier-seq, but it does say that for
13898          decl-specifier-seq in an ordinary declaration.  Perhaps it
13899          would be clearer just to allow a decl-specifier-seq here, and
13900          then add a semantic restriction that if any decl-specifiers
13901          that are not type-specifiers appear, the program is invalid.  */
13902       if (is_condition && !is_cv_qualifier)
13903         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13904     }
13905
13906   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13907 }
13908
13909 /* Parse a parameter-declaration-clause.
13910
13911    parameter-declaration-clause:
13912      parameter-declaration-list [opt] ... [opt]
13913      parameter-declaration-list , ...
13914
13915    Returns a representation for the parameter declarations.  A return
13916    value of NULL indicates a parameter-declaration-clause consisting
13917    only of an ellipsis.  */
13918
13919 static tree
13920 cp_parser_parameter_declaration_clause (cp_parser* parser)
13921 {
13922   tree parameters;
13923   cp_token *token;
13924   bool ellipsis_p;
13925   bool is_error;
13926
13927   /* Peek at the next token.  */
13928   token = cp_lexer_peek_token (parser->lexer);
13929   /* Check for trivial parameter-declaration-clauses.  */
13930   if (token->type == CPP_ELLIPSIS)
13931     {
13932       /* Consume the `...' token.  */
13933       cp_lexer_consume_token (parser->lexer);
13934       return NULL_TREE;
13935     }
13936   else if (token->type == CPP_CLOSE_PAREN)
13937     /* There are no parameters.  */
13938     {
13939 #ifndef NO_IMPLICIT_EXTERN_C
13940       if (in_system_header && current_class_type == NULL
13941           && current_lang_name == lang_name_c)
13942         return NULL_TREE;
13943       else
13944 #endif
13945         return void_list_node;
13946     }
13947   /* Check for `(void)', too, which is a special case.  */
13948   else if (token->keyword == RID_VOID
13949            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13950                == CPP_CLOSE_PAREN))
13951     {
13952       /* Consume the `void' token.  */
13953       cp_lexer_consume_token (parser->lexer);
13954       /* There are no parameters.  */
13955       return void_list_node;
13956     }
13957
13958   /* Parse the parameter-declaration-list.  */
13959   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13960   /* If a parse error occurred while parsing the
13961      parameter-declaration-list, then the entire
13962      parameter-declaration-clause is erroneous.  */
13963   if (is_error)
13964     return NULL;
13965
13966   /* Peek at the next token.  */
13967   token = cp_lexer_peek_token (parser->lexer);
13968   /* If it's a `,', the clause should terminate with an ellipsis.  */
13969   if (token->type == CPP_COMMA)
13970     {
13971       /* Consume the `,'.  */
13972       cp_lexer_consume_token (parser->lexer);
13973       /* Expect an ellipsis.  */
13974       ellipsis_p
13975         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13976     }
13977   /* It might also be `...' if the optional trailing `,' was
13978      omitted.  */
13979   else if (token->type == CPP_ELLIPSIS)
13980     {
13981       /* Consume the `...' token.  */
13982       cp_lexer_consume_token (parser->lexer);
13983       /* And remember that we saw it.  */
13984       ellipsis_p = true;
13985     }
13986   else
13987     ellipsis_p = false;
13988
13989   /* Finish the parameter list.  */
13990   if (!ellipsis_p)
13991     parameters = chainon (parameters, void_list_node);
13992
13993   return parameters;
13994 }
13995
13996 /* Parse a parameter-declaration-list.
13997
13998    parameter-declaration-list:
13999      parameter-declaration
14000      parameter-declaration-list , parameter-declaration
14001
14002    Returns a representation of the parameter-declaration-list, as for
14003    cp_parser_parameter_declaration_clause.  However, the
14004    `void_list_node' is never appended to the list.  Upon return,
14005    *IS_ERROR will be true iff an error occurred.  */
14006
14007 static tree
14008 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14009 {
14010   tree parameters = NULL_TREE;
14011   tree *tail = &parameters; 
14012   bool saved_in_unbraced_linkage_specification_p;
14013
14014   /* Assume all will go well.  */
14015   *is_error = false;
14016   /* The special considerations that apply to a function within an
14017      unbraced linkage specifications do not apply to the parameters
14018      to the function.  */
14019   saved_in_unbraced_linkage_specification_p 
14020     = parser->in_unbraced_linkage_specification_p;
14021   parser->in_unbraced_linkage_specification_p = false;
14022
14023   /* Look for more parameters.  */
14024   while (true)
14025     {
14026       cp_parameter_declarator *parameter;
14027       tree decl = error_mark_node;
14028       bool parenthesized_p;
14029       /* Parse the parameter.  */
14030       parameter
14031         = cp_parser_parameter_declaration (parser,
14032                                            /*template_parm_p=*/false,
14033                                            &parenthesized_p);
14034
14035       /* We don't know yet if the enclosing context is deprecated, so wait
14036          and warn in grokparms if appropriate.  */
14037       deprecated_state = DEPRECATED_SUPPRESS;
14038
14039       if (parameter)
14040         decl = grokdeclarator (parameter->declarator,
14041                                &parameter->decl_specifiers,
14042                                PARM,
14043                                parameter->default_argument != NULL_TREE,
14044                                &parameter->decl_specifiers.attributes);
14045
14046       deprecated_state = DEPRECATED_NORMAL;
14047
14048       /* If a parse error occurred parsing the parameter declaration,
14049          then the entire parameter-declaration-list is erroneous.  */
14050       if (decl == error_mark_node)
14051         {
14052           *is_error = true;
14053           parameters = error_mark_node;
14054           break;
14055         }
14056
14057       if (parameter->decl_specifiers.attributes)
14058         cplus_decl_attributes (&decl,
14059                                parameter->decl_specifiers.attributes,
14060                                0);
14061       if (DECL_NAME (decl))
14062         decl = pushdecl (decl);
14063
14064       /* Add the new parameter to the list.  */
14065       *tail = build_tree_list (parameter->default_argument, decl);
14066       tail = &TREE_CHAIN (*tail);
14067
14068       /* Peek at the next token.  */
14069       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14070           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14071           /* These are for Objective-C++ */
14072           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14073           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14074         /* The parameter-declaration-list is complete.  */
14075         break;
14076       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14077         {
14078           cp_token *token;
14079
14080           /* Peek at the next token.  */
14081           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14082           /* If it's an ellipsis, then the list is complete.  */
14083           if (token->type == CPP_ELLIPSIS)
14084             break;
14085           /* Otherwise, there must be more parameters.  Consume the
14086              `,'.  */
14087           cp_lexer_consume_token (parser->lexer);
14088           /* When parsing something like:
14089
14090                 int i(float f, double d)
14091
14092              we can tell after seeing the declaration for "f" that we
14093              are not looking at an initialization of a variable "i",
14094              but rather at the declaration of a function "i".
14095
14096              Due to the fact that the parsing of template arguments
14097              (as specified to a template-id) requires backtracking we
14098              cannot use this technique when inside a template argument
14099              list.  */
14100           if (!parser->in_template_argument_list_p
14101               && !parser->in_type_id_in_expr_p
14102               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14103               /* However, a parameter-declaration of the form
14104                  "foat(f)" (which is a valid declaration of a
14105                  parameter "f") can also be interpreted as an
14106                  expression (the conversion of "f" to "float").  */
14107               && !parenthesized_p)
14108             cp_parser_commit_to_tentative_parse (parser);
14109         }
14110       else
14111         {
14112           cp_parser_error (parser, "expected %<,%> or %<...%>");
14113           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14114             cp_parser_skip_to_closing_parenthesis (parser,
14115                                                    /*recovering=*/true,
14116                                                    /*or_comma=*/false,
14117                                                    /*consume_paren=*/false);
14118           break;
14119         }
14120     }
14121
14122   parser->in_unbraced_linkage_specification_p
14123     = saved_in_unbraced_linkage_specification_p;
14124
14125   return parameters;
14126 }
14127
14128 /* Parse a parameter declaration.
14129
14130    parameter-declaration:
14131      decl-specifier-seq ... [opt] declarator
14132      decl-specifier-seq declarator = assignment-expression
14133      decl-specifier-seq ... [opt] abstract-declarator [opt]
14134      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14135
14136    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14137    declares a template parameter.  (In that case, a non-nested `>'
14138    token encountered during the parsing of the assignment-expression
14139    is not interpreted as a greater-than operator.)
14140
14141    Returns a representation of the parameter, or NULL if an error
14142    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14143    true iff the declarator is of the form "(p)".  */
14144
14145 static cp_parameter_declarator *
14146 cp_parser_parameter_declaration (cp_parser *parser,
14147                                  bool template_parm_p,
14148                                  bool *parenthesized_p)
14149 {
14150   int declares_class_or_enum;
14151   bool greater_than_is_operator_p;
14152   cp_decl_specifier_seq decl_specifiers;
14153   cp_declarator *declarator;
14154   tree default_argument;
14155   cp_token *token = NULL, *declarator_token_start = NULL;
14156   const char *saved_message;
14157
14158   /* In a template parameter, `>' is not an operator.
14159
14160      [temp.param]
14161
14162      When parsing a default template-argument for a non-type
14163      template-parameter, the first non-nested `>' is taken as the end
14164      of the template parameter-list rather than a greater-than
14165      operator.  */
14166   greater_than_is_operator_p = !template_parm_p;
14167
14168   /* Type definitions may not appear in parameter types.  */
14169   saved_message = parser->type_definition_forbidden_message;
14170   parser->type_definition_forbidden_message
14171     = "types may not be defined in parameter types";
14172
14173   /* Parse the declaration-specifiers.  */
14174   cp_parser_decl_specifier_seq (parser,
14175                                 CP_PARSER_FLAGS_NONE,
14176                                 &decl_specifiers,
14177                                 &declares_class_or_enum);
14178   /* If an error occurred, there's no reason to attempt to parse the
14179      rest of the declaration.  */
14180   if (cp_parser_error_occurred (parser))
14181     {
14182       parser->type_definition_forbidden_message = saved_message;
14183       return NULL;
14184     }
14185
14186   /* Peek at the next token.  */
14187   token = cp_lexer_peek_token (parser->lexer);
14188
14189   /* If the next token is a `)', `,', `=', `>', or `...', then there
14190      is no declarator. However, when variadic templates are enabled,
14191      there may be a declarator following `...'.  */
14192   if (token->type == CPP_CLOSE_PAREN
14193       || token->type == CPP_COMMA
14194       || token->type == CPP_EQ
14195       || token->type == CPP_GREATER)
14196     {
14197       declarator = NULL;
14198       if (parenthesized_p)
14199         *parenthesized_p = false;
14200     }
14201   /* Otherwise, there should be a declarator.  */
14202   else
14203     {
14204       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14205       parser->default_arg_ok_p = false;
14206
14207       /* After seeing a decl-specifier-seq, if the next token is not a
14208          "(", there is no possibility that the code is a valid
14209          expression.  Therefore, if parsing tentatively, we commit at
14210          this point.  */
14211       if (!parser->in_template_argument_list_p
14212           /* In an expression context, having seen:
14213
14214                (int((char ...
14215
14216              we cannot be sure whether we are looking at a
14217              function-type (taking a "char" as a parameter) or a cast
14218              of some object of type "char" to "int".  */
14219           && !parser->in_type_id_in_expr_p
14220           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14221           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14222         cp_parser_commit_to_tentative_parse (parser);
14223       /* Parse the declarator.  */
14224       declarator_token_start = token;
14225       declarator = cp_parser_declarator (parser,
14226                                          CP_PARSER_DECLARATOR_EITHER,
14227                                          /*ctor_dtor_or_conv_p=*/NULL,
14228                                          parenthesized_p,
14229                                          /*member_p=*/false);
14230       parser->default_arg_ok_p = saved_default_arg_ok_p;
14231       /* After the declarator, allow more attributes.  */
14232       decl_specifiers.attributes
14233         = chainon (decl_specifiers.attributes,
14234                    cp_parser_attributes_opt (parser));
14235     }
14236
14237   /* If the next token is an ellipsis, and we have not seen a
14238      declarator name, and the type of the declarator contains parameter
14239      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14240      a parameter pack expansion expression. Otherwise, leave the
14241      ellipsis for a C-style variadic function. */
14242   token = cp_lexer_peek_token (parser->lexer);
14243   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14244     {
14245       tree type = decl_specifiers.type;
14246
14247       if (type && DECL_P (type))
14248         type = TREE_TYPE (type);
14249
14250       if (type
14251           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14252           && declarator_can_be_parameter_pack (declarator)
14253           && (!declarator || !declarator->parameter_pack_p)
14254           && uses_parameter_packs (type))
14255         {
14256           /* Consume the `...'. */
14257           cp_lexer_consume_token (parser->lexer);
14258           maybe_warn_variadic_templates ();
14259           
14260           /* Build a pack expansion type */
14261           if (declarator)
14262             declarator->parameter_pack_p = true;
14263           else
14264             decl_specifiers.type = make_pack_expansion (type);
14265         }
14266     }
14267
14268   /* The restriction on defining new types applies only to the type
14269      of the parameter, not to the default argument.  */
14270   parser->type_definition_forbidden_message = saved_message;
14271
14272   /* If the next token is `=', then process a default argument.  */
14273   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14274     {
14275       /* Consume the `='.  */
14276       cp_lexer_consume_token (parser->lexer);
14277
14278       /* If we are defining a class, then the tokens that make up the
14279          default argument must be saved and processed later.  */
14280       if (!template_parm_p && at_class_scope_p ()
14281           && TYPE_BEING_DEFINED (current_class_type))
14282         {
14283           unsigned depth = 0;
14284           int maybe_template_id = 0;
14285           cp_token *first_token;
14286           cp_token *token;
14287
14288           /* Add tokens until we have processed the entire default
14289              argument.  We add the range [first_token, token).  */
14290           first_token = cp_lexer_peek_token (parser->lexer);
14291           while (true)
14292             {
14293               bool done = false;
14294
14295               /* Peek at the next token.  */
14296               token = cp_lexer_peek_token (parser->lexer);
14297               /* What we do depends on what token we have.  */
14298               switch (token->type)
14299                 {
14300                   /* In valid code, a default argument must be
14301                      immediately followed by a `,' `)', or `...'.  */
14302                 case CPP_COMMA:
14303                   if (depth == 0 && maybe_template_id)
14304                     {
14305                       /* If we've seen a '<', we might be in a
14306                          template-argument-list.  Until Core issue 325 is
14307                          resolved, we don't know how this situation ought
14308                          to be handled, so try to DTRT.  We check whether
14309                          what comes after the comma is a valid parameter
14310                          declaration list.  If it is, then the comma ends
14311                          the default argument; otherwise the default
14312                          argument continues.  */
14313                       bool error = false;
14314
14315                       /* Set ITALP so cp_parser_parameter_declaration_list
14316                          doesn't decide to commit to this parse.  */
14317                       bool saved_italp = parser->in_template_argument_list_p;
14318                       parser->in_template_argument_list_p = true;
14319
14320                       cp_parser_parse_tentatively (parser);
14321                       cp_lexer_consume_token (parser->lexer);
14322                       cp_parser_parameter_declaration_list (parser, &error);
14323                       if (!cp_parser_error_occurred (parser) && !error)
14324                         done = true;
14325                       cp_parser_abort_tentative_parse (parser);
14326
14327                       parser->in_template_argument_list_p = saved_italp;
14328                       break;
14329                     }
14330                 case CPP_CLOSE_PAREN:
14331                 case CPP_ELLIPSIS:
14332                   /* If we run into a non-nested `;', `}', or `]',
14333                      then the code is invalid -- but the default
14334                      argument is certainly over.  */
14335                 case CPP_SEMICOLON:
14336                 case CPP_CLOSE_BRACE:
14337                 case CPP_CLOSE_SQUARE:
14338                   if (depth == 0)
14339                     done = true;
14340                   /* Update DEPTH, if necessary.  */
14341                   else if (token->type == CPP_CLOSE_PAREN
14342                            || token->type == CPP_CLOSE_BRACE
14343                            || token->type == CPP_CLOSE_SQUARE)
14344                     --depth;
14345                   break;
14346
14347                 case CPP_OPEN_PAREN:
14348                 case CPP_OPEN_SQUARE:
14349                 case CPP_OPEN_BRACE:
14350                   ++depth;
14351                   break;
14352
14353                 case CPP_LESS:
14354                   if (depth == 0)
14355                     /* This might be the comparison operator, or it might
14356                        start a template argument list.  */
14357                     ++maybe_template_id;
14358                   break;
14359
14360                 case CPP_RSHIFT:
14361                   if (cxx_dialect == cxx98)
14362                     break;
14363                   /* Fall through for C++0x, which treats the `>>'
14364                      operator like two `>' tokens in certain
14365                      cases.  */
14366
14367                 case CPP_GREATER:
14368                   if (depth == 0)
14369                     {
14370                       /* This might be an operator, or it might close a
14371                          template argument list.  But if a previous '<'
14372                          started a template argument list, this will have
14373                          closed it, so we can't be in one anymore.  */
14374                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14375                       if (maybe_template_id < 0)
14376                         maybe_template_id = 0;
14377                     }
14378                   break;
14379
14380                   /* If we run out of tokens, issue an error message.  */
14381                 case CPP_EOF:
14382                 case CPP_PRAGMA_EOL:
14383                   error ("%Hfile ends in default argument", &token->location);
14384                   done = true;
14385                   break;
14386
14387                 case CPP_NAME:
14388                 case CPP_SCOPE:
14389                   /* In these cases, we should look for template-ids.
14390                      For example, if the default argument is
14391                      `X<int, double>()', we need to do name lookup to
14392                      figure out whether or not `X' is a template; if
14393                      so, the `,' does not end the default argument.
14394
14395                      That is not yet done.  */
14396                   break;
14397
14398                 default:
14399                   break;
14400                 }
14401
14402               /* If we've reached the end, stop.  */
14403               if (done)
14404                 break;
14405
14406               /* Add the token to the token block.  */
14407               token = cp_lexer_consume_token (parser->lexer);
14408             }
14409
14410           /* Create a DEFAULT_ARG to represent the unparsed default
14411              argument.  */
14412           default_argument = make_node (DEFAULT_ARG);
14413           DEFARG_TOKENS (default_argument)
14414             = cp_token_cache_new (first_token, token);
14415           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14416         }
14417       /* Outside of a class definition, we can just parse the
14418          assignment-expression.  */
14419       else
14420         {
14421           token = cp_lexer_peek_token (parser->lexer);
14422           default_argument 
14423             = cp_parser_default_argument (parser, template_parm_p);
14424         }
14425
14426       if (!parser->default_arg_ok_p)
14427         {
14428           if (flag_permissive)
14429             warning (0, "deprecated use of default argument for parameter of non-function");
14430           else
14431             {
14432               error ("%Hdefault arguments are only "
14433                      "permitted for function parameters",
14434                      &token->location);
14435               default_argument = NULL_TREE;
14436             }
14437         }
14438       else if ((declarator && declarator->parameter_pack_p)
14439                || (decl_specifiers.type
14440                    && PACK_EXPANSION_P (decl_specifiers.type)))
14441         {
14442           const char* kind = template_parm_p? "template " : "";
14443           
14444           /* Find the name of the parameter pack.  */     
14445           cp_declarator *id_declarator = declarator;
14446           while (id_declarator && id_declarator->kind != cdk_id)
14447             id_declarator = id_declarator->declarator;
14448           
14449           if (id_declarator && id_declarator->kind == cdk_id)
14450             error ("%H%sparameter pack %qD cannot have a default argument",
14451                    &declarator_token_start->location,
14452                    kind, id_declarator->u.id.unqualified_name);
14453           else
14454             error ("%H%sparameter pack cannot have a default argument",
14455                    &declarator_token_start->location, kind);
14456           
14457           default_argument = NULL_TREE;
14458         }
14459     }
14460   else
14461     default_argument = NULL_TREE;
14462
14463   return make_parameter_declarator (&decl_specifiers,
14464                                     declarator,
14465                                     default_argument);
14466 }
14467
14468 /* Parse a default argument and return it.
14469
14470    TEMPLATE_PARM_P is true if this is a default argument for a
14471    non-type template parameter.  */
14472 static tree
14473 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14474 {
14475   tree default_argument = NULL_TREE;
14476   bool saved_greater_than_is_operator_p;
14477   bool saved_local_variables_forbidden_p;
14478
14479   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14480      set correctly.  */
14481   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14482   parser->greater_than_is_operator_p = !template_parm_p;
14483   /* Local variable names (and the `this' keyword) may not
14484      appear in a default argument.  */
14485   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14486   parser->local_variables_forbidden_p = true;
14487   /* The default argument expression may cause implicitly
14488      defined member functions to be synthesized, which will
14489      result in garbage collection.  We must treat this
14490      situation as if we were within the body of function so as
14491      to avoid collecting live data on the stack.  */
14492   ++function_depth;
14493   /* Parse the assignment-expression.  */
14494   if (template_parm_p)
14495     push_deferring_access_checks (dk_no_deferred);
14496   default_argument
14497     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14498   if (template_parm_p)
14499     pop_deferring_access_checks ();
14500   /* Restore saved state.  */
14501   --function_depth;
14502   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14503   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14504
14505   return default_argument;
14506 }
14507
14508 /* Parse a function-body.
14509
14510    function-body:
14511      compound_statement  */
14512
14513 static void
14514 cp_parser_function_body (cp_parser *parser)
14515 {
14516   cp_parser_compound_statement (parser, NULL, false);
14517 }
14518
14519 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14520    true if a ctor-initializer was present.  */
14521
14522 static bool
14523 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14524 {
14525   tree body;
14526   bool ctor_initializer_p;
14527
14528   /* Begin the function body.  */
14529   body = begin_function_body ();
14530   /* Parse the optional ctor-initializer.  */
14531   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14532   /* Parse the function-body.  */
14533   cp_parser_function_body (parser);
14534   /* Finish the function body.  */
14535   finish_function_body (body);
14536
14537   return ctor_initializer_p;
14538 }
14539
14540 /* Parse an initializer.
14541
14542    initializer:
14543      = initializer-clause
14544      ( expression-list )
14545
14546    Returns an expression representing the initializer.  If no
14547    initializer is present, NULL_TREE is returned.
14548
14549    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14550    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14551    set to TRUE if there is no initializer present.  If there is an
14552    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14553    is set to true; otherwise it is set to false.  */
14554
14555 static tree
14556 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14557                        bool* non_constant_p)
14558 {
14559   cp_token *token;
14560   tree init;
14561
14562   /* Peek at the next token.  */
14563   token = cp_lexer_peek_token (parser->lexer);
14564
14565   /* Let our caller know whether or not this initializer was
14566      parenthesized.  */
14567   *is_direct_init = (token->type != CPP_EQ);
14568   /* Assume that the initializer is constant.  */
14569   *non_constant_p = false;
14570
14571   if (token->type == CPP_EQ)
14572     {
14573       /* Consume the `='.  */
14574       cp_lexer_consume_token (parser->lexer);
14575       /* Parse the initializer-clause.  */
14576       init = cp_parser_initializer_clause (parser, non_constant_p);
14577     }
14578   else if (token->type == CPP_OPEN_PAREN)
14579     init = cp_parser_parenthesized_expression_list (parser, false,
14580                                                     /*cast_p=*/false,
14581                                                     /*allow_expansion_p=*/true,
14582                                                     non_constant_p);
14583   else if (token->type == CPP_OPEN_BRACE)
14584     {
14585       maybe_warn_cpp0x ("extended initializer lists");
14586       init = cp_parser_braced_list (parser, non_constant_p);
14587       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14588     }
14589   else
14590     {
14591       /* Anything else is an error.  */
14592       cp_parser_error (parser, "expected initializer");
14593       init = error_mark_node;
14594     }
14595
14596   return init;
14597 }
14598
14599 /* Parse an initializer-clause.
14600
14601    initializer-clause:
14602      assignment-expression
14603      braced-init-list
14604
14605    Returns an expression representing the initializer.
14606
14607    If the `assignment-expression' production is used the value
14608    returned is simply a representation for the expression.
14609
14610    Otherwise, calls cp_parser_braced_list.  */
14611
14612 static tree
14613 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14614 {
14615   tree initializer;
14616
14617   /* Assume the expression is constant.  */
14618   *non_constant_p = false;
14619
14620   /* If it is not a `{', then we are looking at an
14621      assignment-expression.  */
14622   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14623     {
14624       initializer
14625         = cp_parser_constant_expression (parser,
14626                                         /*allow_non_constant_p=*/true,
14627                                         non_constant_p);
14628       if (!*non_constant_p)
14629         initializer = fold_non_dependent_expr (initializer);
14630     }
14631   else
14632     initializer = cp_parser_braced_list (parser, non_constant_p);
14633
14634   return initializer;
14635 }
14636
14637 /* Parse a brace-enclosed initializer list.
14638
14639    braced-init-list:
14640      { initializer-list , [opt] }
14641      { }
14642
14643    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14644    the elements of the initializer-list (or NULL, if the last
14645    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14646    NULL_TREE.  There is no way to detect whether or not the optional
14647    trailing `,' was provided.  NON_CONSTANT_P is as for
14648    cp_parser_initializer.  */     
14649
14650 static tree
14651 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14652 {
14653   tree initializer;
14654
14655   /* Consume the `{' token.  */
14656   cp_lexer_consume_token (parser->lexer);
14657   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14658   initializer = make_node (CONSTRUCTOR);
14659   /* If it's not a `}', then there is a non-trivial initializer.  */
14660   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14661     {
14662       /* Parse the initializer list.  */
14663       CONSTRUCTOR_ELTS (initializer)
14664         = cp_parser_initializer_list (parser, non_constant_p);
14665       /* A trailing `,' token is allowed.  */
14666       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14667         cp_lexer_consume_token (parser->lexer);
14668     }
14669   /* Now, there should be a trailing `}'.  */
14670   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14671   TREE_TYPE (initializer) = init_list_type_node;
14672   return initializer;
14673 }
14674
14675 /* Parse an initializer-list.
14676
14677    initializer-list:
14678      initializer-clause ... [opt]
14679      initializer-list , initializer-clause ... [opt]
14680
14681    GNU Extension:
14682
14683    initializer-list:
14684      identifier : initializer-clause
14685      initializer-list, identifier : initializer-clause
14686
14687    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14688    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14689    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14690    as for cp_parser_initializer.  */
14691
14692 static VEC(constructor_elt,gc) *
14693 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14694 {
14695   VEC(constructor_elt,gc) *v = NULL;
14696
14697   /* Assume all of the expressions are constant.  */
14698   *non_constant_p = false;
14699
14700   /* Parse the rest of the list.  */
14701   while (true)
14702     {
14703       cp_token *token;
14704       tree identifier;
14705       tree initializer;
14706       bool clause_non_constant_p;
14707
14708       /* If the next token is an identifier and the following one is a
14709          colon, we are looking at the GNU designated-initializer
14710          syntax.  */
14711       if (cp_parser_allow_gnu_extensions_p (parser)
14712           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14713           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14714         {
14715           /* Warn the user that they are using an extension.  */
14716           pedwarn (input_location, OPT_pedantic, 
14717                    "ISO C++ does not allow designated initializers");
14718           /* Consume the identifier.  */
14719           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14720           /* Consume the `:'.  */
14721           cp_lexer_consume_token (parser->lexer);
14722         }
14723       else
14724         identifier = NULL_TREE;
14725
14726       /* Parse the initializer.  */
14727       initializer = cp_parser_initializer_clause (parser,
14728                                                   &clause_non_constant_p);
14729       /* If any clause is non-constant, so is the entire initializer.  */
14730       if (clause_non_constant_p)
14731         *non_constant_p = true;
14732
14733       /* If we have an ellipsis, this is an initializer pack
14734          expansion.  */
14735       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14736         {
14737           /* Consume the `...'.  */
14738           cp_lexer_consume_token (parser->lexer);
14739
14740           /* Turn the initializer into an initializer expansion.  */
14741           initializer = make_pack_expansion (initializer);
14742         }
14743
14744       /* Add it to the vector.  */
14745       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14746
14747       /* If the next token is not a comma, we have reached the end of
14748          the list.  */
14749       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14750         break;
14751
14752       /* Peek at the next token.  */
14753       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14754       /* If the next token is a `}', then we're still done.  An
14755          initializer-clause can have a trailing `,' after the
14756          initializer-list and before the closing `}'.  */
14757       if (token->type == CPP_CLOSE_BRACE)
14758         break;
14759
14760       /* Consume the `,' token.  */
14761       cp_lexer_consume_token (parser->lexer);
14762     }
14763
14764   return v;
14765 }
14766
14767 /* Classes [gram.class] */
14768
14769 /* Parse a class-name.
14770
14771    class-name:
14772      identifier
14773      template-id
14774
14775    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14776    to indicate that names looked up in dependent types should be
14777    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14778    keyword has been used to indicate that the name that appears next
14779    is a template.  TAG_TYPE indicates the explicit tag given before
14780    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14781    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14782    is the class being defined in a class-head.
14783
14784    Returns the TYPE_DECL representing the class.  */
14785
14786 static tree
14787 cp_parser_class_name (cp_parser *parser,
14788                       bool typename_keyword_p,
14789                       bool template_keyword_p,
14790                       enum tag_types tag_type,
14791                       bool check_dependency_p,
14792                       bool class_head_p,
14793                       bool is_declaration)
14794 {
14795   tree decl;
14796   tree scope;
14797   bool typename_p;
14798   cp_token *token;
14799   tree identifier = NULL_TREE;
14800
14801   /* All class-names start with an identifier.  */
14802   token = cp_lexer_peek_token (parser->lexer);
14803   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14804     {
14805       cp_parser_error (parser, "expected class-name");
14806       return error_mark_node;
14807     }
14808
14809   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14810      to a template-id, so we save it here.  */
14811   scope = parser->scope;
14812   if (scope == error_mark_node)
14813     return error_mark_node;
14814
14815   /* Any name names a type if we're following the `typename' keyword
14816      in a qualified name where the enclosing scope is type-dependent.  */
14817   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14818                 && dependent_type_p (scope));
14819   /* Handle the common case (an identifier, but not a template-id)
14820      efficiently.  */
14821   if (token->type == CPP_NAME
14822       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14823     {
14824       cp_token *identifier_token;
14825       bool ambiguous_p;
14826
14827       /* Look for the identifier.  */
14828       identifier_token = cp_lexer_peek_token (parser->lexer);
14829       ambiguous_p = identifier_token->ambiguous_p;
14830       identifier = cp_parser_identifier (parser);
14831       /* If the next token isn't an identifier, we are certainly not
14832          looking at a class-name.  */
14833       if (identifier == error_mark_node)
14834         decl = error_mark_node;
14835       /* If we know this is a type-name, there's no need to look it
14836          up.  */
14837       else if (typename_p)
14838         decl = identifier;
14839       else
14840         {
14841           tree ambiguous_decls;
14842           /* If we already know that this lookup is ambiguous, then
14843              we've already issued an error message; there's no reason
14844              to check again.  */
14845           if (ambiguous_p)
14846             {
14847               cp_parser_simulate_error (parser);
14848               return error_mark_node;
14849             }
14850           /* If the next token is a `::', then the name must be a type
14851              name.
14852
14853              [basic.lookup.qual]
14854
14855              During the lookup for a name preceding the :: scope
14856              resolution operator, object, function, and enumerator
14857              names are ignored.  */
14858           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14859             tag_type = typename_type;
14860           /* Look up the name.  */
14861           decl = cp_parser_lookup_name (parser, identifier,
14862                                         tag_type,
14863                                         /*is_template=*/false,
14864                                         /*is_namespace=*/false,
14865                                         check_dependency_p,
14866                                         &ambiguous_decls,
14867                                         identifier_token->location);
14868           if (ambiguous_decls)
14869             {
14870               error ("%Hreference to %qD is ambiguous",
14871                      &identifier_token->location, identifier);
14872               print_candidates (ambiguous_decls);
14873               if (cp_parser_parsing_tentatively (parser))
14874                 {
14875                   identifier_token->ambiguous_p = true;
14876                   cp_parser_simulate_error (parser);
14877                 }
14878               return error_mark_node;
14879             }
14880         }
14881     }
14882   else
14883     {
14884       /* Try a template-id.  */
14885       decl = cp_parser_template_id (parser, template_keyword_p,
14886                                     check_dependency_p,
14887                                     is_declaration);
14888       if (decl == error_mark_node)
14889         return error_mark_node;
14890     }
14891
14892   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14893
14894   /* If this is a typename, create a TYPENAME_TYPE.  */
14895   if (typename_p && decl != error_mark_node)
14896     {
14897       decl = make_typename_type (scope, decl, typename_type,
14898                                  /*complain=*/tf_error);
14899       if (decl != error_mark_node)
14900         decl = TYPE_NAME (decl);
14901     }
14902
14903   /* Check to see that it is really the name of a class.  */
14904   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14905       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14906       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14907     /* Situations like this:
14908
14909          template <typename T> struct A {
14910            typename T::template X<int>::I i;
14911          };
14912
14913        are problematic.  Is `T::template X<int>' a class-name?  The
14914        standard does not seem to be definitive, but there is no other
14915        valid interpretation of the following `::'.  Therefore, those
14916        names are considered class-names.  */
14917     {
14918       decl = make_typename_type (scope, decl, tag_type, tf_error);
14919       if (decl != error_mark_node)
14920         decl = TYPE_NAME (decl);
14921     }
14922   else if (TREE_CODE (decl) != TYPE_DECL
14923            || TREE_TYPE (decl) == error_mark_node
14924            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14925     decl = error_mark_node;
14926
14927   if (decl == error_mark_node)
14928     cp_parser_error (parser, "expected class-name");
14929   else if (identifier && !parser->scope)
14930     maybe_note_name_used_in_class (identifier, decl);
14931
14932   return decl;
14933 }
14934
14935 /* Parse a class-specifier.
14936
14937    class-specifier:
14938      class-head { member-specification [opt] }
14939
14940    Returns the TREE_TYPE representing the class.  */
14941
14942 static tree
14943 cp_parser_class_specifier (cp_parser* parser)
14944 {
14945   cp_token *token;
14946   tree type;
14947   tree attributes = NULL_TREE;
14948   int has_trailing_semicolon;
14949   bool nested_name_specifier_p;
14950   unsigned saved_num_template_parameter_lists;
14951   bool saved_in_function_body;
14952   bool saved_in_unbraced_linkage_specification_p;
14953   tree old_scope = NULL_TREE;
14954   tree scope = NULL_TREE;
14955   tree bases;
14956
14957   push_deferring_access_checks (dk_no_deferred);
14958
14959   /* Parse the class-head.  */
14960   type = cp_parser_class_head (parser,
14961                                &nested_name_specifier_p,
14962                                &attributes,
14963                                &bases);
14964   /* If the class-head was a semantic disaster, skip the entire body
14965      of the class.  */
14966   if (!type)
14967     {
14968       cp_parser_skip_to_end_of_block_or_statement (parser);
14969       pop_deferring_access_checks ();
14970       return error_mark_node;
14971     }
14972
14973   /* Look for the `{'.  */
14974   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14975     {
14976       pop_deferring_access_checks ();
14977       return error_mark_node;
14978     }
14979
14980   /* Process the base classes. If they're invalid, skip the 
14981      entire class body.  */
14982   if (!xref_basetypes (type, bases))
14983     {
14984       /* Consuming the closing brace yields better error messages
14985          later on.  */
14986       if (cp_parser_skip_to_closing_brace (parser))
14987         cp_lexer_consume_token (parser->lexer);
14988       pop_deferring_access_checks ();
14989       return error_mark_node;
14990     }
14991
14992   /* Issue an error message if type-definitions are forbidden here.  */
14993   cp_parser_check_type_definition (parser);
14994   /* Remember that we are defining one more class.  */
14995   ++parser->num_classes_being_defined;
14996   /* Inside the class, surrounding template-parameter-lists do not
14997      apply.  */
14998   saved_num_template_parameter_lists
14999     = parser->num_template_parameter_lists;
15000   parser->num_template_parameter_lists = 0;
15001   /* We are not in a function body.  */
15002   saved_in_function_body = parser->in_function_body;
15003   parser->in_function_body = false;
15004   /* We are not immediately inside an extern "lang" block.  */
15005   saved_in_unbraced_linkage_specification_p
15006     = parser->in_unbraced_linkage_specification_p;
15007   parser->in_unbraced_linkage_specification_p = false;
15008
15009   /* Start the class.  */
15010   if (nested_name_specifier_p)
15011     {
15012       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15013       old_scope = push_inner_scope (scope);
15014     }
15015   type = begin_class_definition (type, attributes);
15016
15017   if (type == error_mark_node)
15018     /* If the type is erroneous, skip the entire body of the class.  */
15019     cp_parser_skip_to_closing_brace (parser);
15020   else
15021     /* Parse the member-specification.  */
15022     cp_parser_member_specification_opt (parser);
15023
15024   /* Look for the trailing `}'.  */
15025   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15026   /* We get better error messages by noticing a common problem: a
15027      missing trailing `;'.  */
15028   token = cp_lexer_peek_token (parser->lexer);
15029   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
15030   /* Look for trailing attributes to apply to this class.  */
15031   if (cp_parser_allow_gnu_extensions_p (parser))
15032     attributes = cp_parser_attributes_opt (parser);
15033   if (type != error_mark_node)
15034     type = finish_struct (type, attributes);
15035   if (nested_name_specifier_p)
15036     pop_inner_scope (old_scope, scope);
15037   /* If this class is not itself within the scope of another class,
15038      then we need to parse the bodies of all of the queued function
15039      definitions.  Note that the queued functions defined in a class
15040      are not always processed immediately following the
15041      class-specifier for that class.  Consider:
15042
15043        struct A {
15044          struct B { void f() { sizeof (A); } };
15045        };
15046
15047      If `f' were processed before the processing of `A' were
15048      completed, there would be no way to compute the size of `A'.
15049      Note that the nesting we are interested in here is lexical --
15050      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15051      for:
15052
15053        struct A { struct B; };
15054        struct A::B { void f() { } };
15055
15056      there is no need to delay the parsing of `A::B::f'.  */
15057   if (--parser->num_classes_being_defined == 0)
15058     {
15059       tree queue_entry;
15060       tree fn;
15061       tree class_type = NULL_TREE;
15062       tree pushed_scope = NULL_TREE;
15063
15064       /* In a first pass, parse default arguments to the functions.
15065          Then, in a second pass, parse the bodies of the functions.
15066          This two-phased approach handles cases like:
15067
15068             struct S {
15069               void f() { g(); }
15070               void g(int i = 3);
15071             };
15072
15073          */
15074       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15075              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15076            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15077            TREE_PURPOSE (parser->unparsed_functions_queues)
15078              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15079         {
15080           fn = TREE_VALUE (queue_entry);
15081           /* If there are default arguments that have not yet been processed,
15082              take care of them now.  */
15083           if (class_type != TREE_PURPOSE (queue_entry))
15084             {
15085               if (pushed_scope)
15086                 pop_scope (pushed_scope);
15087               class_type = TREE_PURPOSE (queue_entry);
15088               pushed_scope = push_scope (class_type);
15089             }
15090           /* Make sure that any template parameters are in scope.  */
15091           maybe_begin_member_template_processing (fn);
15092           /* Parse the default argument expressions.  */
15093           cp_parser_late_parsing_default_args (parser, fn);
15094           /* Remove any template parameters from the symbol table.  */
15095           maybe_end_member_template_processing ();
15096         }
15097       if (pushed_scope)
15098         pop_scope (pushed_scope);
15099       /* Now parse the body of the functions.  */
15100       for (TREE_VALUE (parser->unparsed_functions_queues)
15101              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15102            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15103            TREE_VALUE (parser->unparsed_functions_queues)
15104              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15105         {
15106           /* Figure out which function we need to process.  */
15107           fn = TREE_VALUE (queue_entry);
15108           /* Parse the function.  */
15109           cp_parser_late_parsing_for_member (parser, fn);
15110         }
15111     }
15112
15113   /* Put back any saved access checks.  */
15114   pop_deferring_access_checks ();
15115
15116   /* Restore saved state.  */
15117   parser->in_function_body = saved_in_function_body;
15118   parser->num_template_parameter_lists
15119     = saved_num_template_parameter_lists;
15120   parser->in_unbraced_linkage_specification_p
15121     = saved_in_unbraced_linkage_specification_p;
15122
15123   return type;
15124 }
15125
15126 /* Parse a class-head.
15127
15128    class-head:
15129      class-key identifier [opt] base-clause [opt]
15130      class-key nested-name-specifier identifier base-clause [opt]
15131      class-key nested-name-specifier [opt] template-id
15132        base-clause [opt]
15133
15134    GNU Extensions:
15135      class-key attributes identifier [opt] base-clause [opt]
15136      class-key attributes nested-name-specifier identifier base-clause [opt]
15137      class-key attributes nested-name-specifier [opt] template-id
15138        base-clause [opt]
15139
15140    Upon return BASES is initialized to the list of base classes (or
15141    NULL, if there are none) in the same form returned by
15142    cp_parser_base_clause.
15143
15144    Returns the TYPE of the indicated class.  Sets
15145    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15146    involving a nested-name-specifier was used, and FALSE otherwise.
15147
15148    Returns error_mark_node if this is not a class-head.
15149
15150    Returns NULL_TREE if the class-head is syntactically valid, but
15151    semantically invalid in a way that means we should skip the entire
15152    body of the class.  */
15153
15154 static tree
15155 cp_parser_class_head (cp_parser* parser,
15156                       bool* nested_name_specifier_p,
15157                       tree *attributes_p,
15158                       tree *bases)
15159 {
15160   tree nested_name_specifier;
15161   enum tag_types class_key;
15162   tree id = NULL_TREE;
15163   tree type = NULL_TREE;
15164   tree attributes;
15165   bool template_id_p = false;
15166   bool qualified_p = false;
15167   bool invalid_nested_name_p = false;
15168   bool invalid_explicit_specialization_p = false;
15169   tree pushed_scope = NULL_TREE;
15170   unsigned num_templates;
15171   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15172   /* Assume no nested-name-specifier will be present.  */
15173   *nested_name_specifier_p = false;
15174   /* Assume no template parameter lists will be used in defining the
15175      type.  */
15176   num_templates = 0;
15177
15178   *bases = NULL_TREE;
15179
15180   /* Look for the class-key.  */
15181   class_key = cp_parser_class_key (parser);
15182   if (class_key == none_type)
15183     return error_mark_node;
15184
15185   /* Parse the attributes.  */
15186   attributes = cp_parser_attributes_opt (parser);
15187
15188   /* If the next token is `::', that is invalid -- but sometimes
15189      people do try to write:
15190
15191        struct ::S {};
15192
15193      Handle this gracefully by accepting the extra qualifier, and then
15194      issuing an error about it later if this really is a
15195      class-head.  If it turns out just to be an elaborated type
15196      specifier, remain silent.  */
15197   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15198     qualified_p = true;
15199
15200   push_deferring_access_checks (dk_no_check);
15201
15202   /* Determine the name of the class.  Begin by looking for an
15203      optional nested-name-specifier.  */
15204   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15205   nested_name_specifier
15206     = cp_parser_nested_name_specifier_opt (parser,
15207                                            /*typename_keyword_p=*/false,
15208                                            /*check_dependency_p=*/false,
15209                                            /*type_p=*/false,
15210                                            /*is_declaration=*/false);
15211   /* If there was a nested-name-specifier, then there *must* be an
15212      identifier.  */
15213   if (nested_name_specifier)
15214     {
15215       type_start_token = cp_lexer_peek_token (parser->lexer);
15216       /* Although the grammar says `identifier', it really means
15217          `class-name' or `template-name'.  You are only allowed to
15218          define a class that has already been declared with this
15219          syntax.
15220
15221          The proposed resolution for Core Issue 180 says that wherever
15222          you see `class T::X' you should treat `X' as a type-name.
15223
15224          It is OK to define an inaccessible class; for example:
15225
15226            class A { class B; };
15227            class A::B {};
15228
15229          We do not know if we will see a class-name, or a
15230          template-name.  We look for a class-name first, in case the
15231          class-name is a template-id; if we looked for the
15232          template-name first we would stop after the template-name.  */
15233       cp_parser_parse_tentatively (parser);
15234       type = cp_parser_class_name (parser,
15235                                    /*typename_keyword_p=*/false,
15236                                    /*template_keyword_p=*/false,
15237                                    class_type,
15238                                    /*check_dependency_p=*/false,
15239                                    /*class_head_p=*/true,
15240                                    /*is_declaration=*/false);
15241       /* If that didn't work, ignore the nested-name-specifier.  */
15242       if (!cp_parser_parse_definitely (parser))
15243         {
15244           invalid_nested_name_p = true;
15245           type_start_token = cp_lexer_peek_token (parser->lexer);
15246           id = cp_parser_identifier (parser);
15247           if (id == error_mark_node)
15248             id = NULL_TREE;
15249         }
15250       /* If we could not find a corresponding TYPE, treat this
15251          declaration like an unqualified declaration.  */
15252       if (type == error_mark_node)
15253         nested_name_specifier = NULL_TREE;
15254       /* Otherwise, count the number of templates used in TYPE and its
15255          containing scopes.  */
15256       else
15257         {
15258           tree scope;
15259
15260           for (scope = TREE_TYPE (type);
15261                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15262                scope = (TYPE_P (scope)
15263                         ? TYPE_CONTEXT (scope)
15264                         : DECL_CONTEXT (scope)))
15265             if (TYPE_P (scope)
15266                 && CLASS_TYPE_P (scope)
15267                 && CLASSTYPE_TEMPLATE_INFO (scope)
15268                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15269                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15270               ++num_templates;
15271         }
15272     }
15273   /* Otherwise, the identifier is optional.  */
15274   else
15275     {
15276       /* We don't know whether what comes next is a template-id,
15277          an identifier, or nothing at all.  */
15278       cp_parser_parse_tentatively (parser);
15279       /* Check for a template-id.  */
15280       type_start_token = cp_lexer_peek_token (parser->lexer);
15281       id = cp_parser_template_id (parser,
15282                                   /*template_keyword_p=*/false,
15283                                   /*check_dependency_p=*/true,
15284                                   /*is_declaration=*/true);
15285       /* If that didn't work, it could still be an identifier.  */
15286       if (!cp_parser_parse_definitely (parser))
15287         {
15288           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15289             {
15290               type_start_token = cp_lexer_peek_token (parser->lexer);
15291               id = cp_parser_identifier (parser);
15292             }
15293           else
15294             id = NULL_TREE;
15295         }
15296       else
15297         {
15298           template_id_p = true;
15299           ++num_templates;
15300         }
15301     }
15302
15303   pop_deferring_access_checks ();
15304
15305   if (id)
15306     cp_parser_check_for_invalid_template_id (parser, id,
15307                                              type_start_token->location);
15308
15309   /* If it's not a `:' or a `{' then we can't really be looking at a
15310      class-head, since a class-head only appears as part of a
15311      class-specifier.  We have to detect this situation before calling
15312      xref_tag, since that has irreversible side-effects.  */
15313   if (!cp_parser_next_token_starts_class_definition_p (parser))
15314     {
15315       cp_parser_error (parser, "expected %<{%> or %<:%>");
15316       return error_mark_node;
15317     }
15318
15319   /* At this point, we're going ahead with the class-specifier, even
15320      if some other problem occurs.  */
15321   cp_parser_commit_to_tentative_parse (parser);
15322   /* Issue the error about the overly-qualified name now.  */
15323   if (qualified_p)
15324     {
15325       cp_parser_error (parser,
15326                        "global qualification of class name is invalid");
15327       return error_mark_node;
15328     }
15329   else if (invalid_nested_name_p)
15330     {
15331       cp_parser_error (parser,
15332                        "qualified name does not name a class");
15333       return error_mark_node;
15334     }
15335   else if (nested_name_specifier)
15336     {
15337       tree scope;
15338
15339       /* Reject typedef-names in class heads.  */
15340       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15341         {
15342           error ("%Hinvalid class name in declaration of %qD",
15343                  &type_start_token->location, type);
15344           type = NULL_TREE;
15345           goto done;
15346         }
15347
15348       /* Figure out in what scope the declaration is being placed.  */
15349       scope = current_scope ();
15350       /* If that scope does not contain the scope in which the
15351          class was originally declared, the program is invalid.  */
15352       if (scope && !is_ancestor (scope, nested_name_specifier))
15353         {
15354           if (at_namespace_scope_p ())
15355             error ("%Hdeclaration of %qD in namespace %qD which does not "
15356                    "enclose %qD",
15357                    &type_start_token->location,
15358                    type, scope, nested_name_specifier);
15359           else
15360             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15361                    &type_start_token->location,
15362                    type, scope, nested_name_specifier);
15363           type = NULL_TREE;
15364           goto done;
15365         }
15366       /* [dcl.meaning]
15367
15368          A declarator-id shall not be qualified except for the
15369          definition of a ... nested class outside of its class
15370          ... [or] the definition or explicit instantiation of a
15371          class member of a namespace outside of its namespace.  */
15372       if (scope == nested_name_specifier)
15373         {
15374           permerror (input_location, "%Hextra qualification not allowed",
15375                      &nested_name_specifier_token_start->location);
15376           nested_name_specifier = NULL_TREE;
15377           num_templates = 0;
15378         }
15379     }
15380   /* An explicit-specialization must be preceded by "template <>".  If
15381      it is not, try to recover gracefully.  */
15382   if (at_namespace_scope_p ()
15383       && parser->num_template_parameter_lists == 0
15384       && template_id_p)
15385     {
15386       error ("%Han explicit specialization must be preceded by %<template <>%>",
15387              &type_start_token->location);
15388       invalid_explicit_specialization_p = true;
15389       /* Take the same action that would have been taken by
15390          cp_parser_explicit_specialization.  */
15391       ++parser->num_template_parameter_lists;
15392       begin_specialization ();
15393     }
15394   /* There must be no "return" statements between this point and the
15395      end of this function; set "type "to the correct return value and
15396      use "goto done;" to return.  */
15397   /* Make sure that the right number of template parameters were
15398      present.  */
15399   if (!cp_parser_check_template_parameters (parser, num_templates,
15400                                             type_start_token->location))
15401     {
15402       /* If something went wrong, there is no point in even trying to
15403          process the class-definition.  */
15404       type = NULL_TREE;
15405       goto done;
15406     }
15407
15408   /* Look up the type.  */
15409   if (template_id_p)
15410     {
15411       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15412           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15413               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15414         {
15415           error ("%Hfunction template %qD redeclared as a class template",
15416                  &type_start_token->location, id);
15417           type = error_mark_node;
15418         }
15419       else
15420         {
15421           type = TREE_TYPE (id);
15422           type = maybe_process_partial_specialization (type);
15423         }
15424       if (nested_name_specifier)
15425         pushed_scope = push_scope (nested_name_specifier);
15426     }
15427   else if (nested_name_specifier)
15428     {
15429       tree class_type;
15430
15431       /* Given:
15432
15433             template <typename T> struct S { struct T };
15434             template <typename T> struct S<T>::T { };
15435
15436          we will get a TYPENAME_TYPE when processing the definition of
15437          `S::T'.  We need to resolve it to the actual type before we
15438          try to define it.  */
15439       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15440         {
15441           class_type = resolve_typename_type (TREE_TYPE (type),
15442                                               /*only_current_p=*/false);
15443           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15444             type = TYPE_NAME (class_type);
15445           else
15446             {
15447               cp_parser_error (parser, "could not resolve typename type");
15448               type = error_mark_node;
15449             }
15450         }
15451
15452       if (maybe_process_partial_specialization (TREE_TYPE (type))
15453           == error_mark_node)
15454         {
15455           type = NULL_TREE;
15456           goto done;
15457         }
15458
15459       class_type = current_class_type;
15460       /* Enter the scope indicated by the nested-name-specifier.  */
15461       pushed_scope = push_scope (nested_name_specifier);
15462       /* Get the canonical version of this type.  */
15463       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15464       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15465           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15466         {
15467           type = push_template_decl (type);
15468           if (type == error_mark_node)
15469             {
15470               type = NULL_TREE;
15471               goto done;
15472             }
15473         }
15474
15475       type = TREE_TYPE (type);
15476       *nested_name_specifier_p = true;
15477     }
15478   else      /* The name is not a nested name.  */
15479     {
15480       /* If the class was unnamed, create a dummy name.  */
15481       if (!id)
15482         id = make_anon_name ();
15483       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15484                        parser->num_template_parameter_lists);
15485     }
15486
15487   /* Indicate whether this class was declared as a `class' or as a
15488      `struct'.  */
15489   if (TREE_CODE (type) == RECORD_TYPE)
15490     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15491   cp_parser_check_class_key (class_key, type);
15492
15493   /* If this type was already complete, and we see another definition,
15494      that's an error.  */
15495   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15496     {
15497       error ("%Hredefinition of %q#T",
15498              &type_start_token->location, type);
15499       error ("%Hprevious definition of %q+#T",
15500              &type_start_token->location, type);
15501       type = NULL_TREE;
15502       goto done;
15503     }
15504   else if (type == error_mark_node)
15505     type = NULL_TREE;
15506
15507   /* We will have entered the scope containing the class; the names of
15508      base classes should be looked up in that context.  For example:
15509
15510        struct A { struct B {}; struct C; };
15511        struct A::C : B {};
15512
15513      is valid.  */
15514
15515   /* Get the list of base-classes, if there is one.  */
15516   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15517     *bases = cp_parser_base_clause (parser);
15518
15519  done:
15520   /* Leave the scope given by the nested-name-specifier.  We will
15521      enter the class scope itself while processing the members.  */
15522   if (pushed_scope)
15523     pop_scope (pushed_scope);
15524
15525   if (invalid_explicit_specialization_p)
15526     {
15527       end_specialization ();
15528       --parser->num_template_parameter_lists;
15529     }
15530   *attributes_p = attributes;
15531   return type;
15532 }
15533
15534 /* Parse a class-key.
15535
15536    class-key:
15537      class
15538      struct
15539      union
15540
15541    Returns the kind of class-key specified, or none_type to indicate
15542    error.  */
15543
15544 static enum tag_types
15545 cp_parser_class_key (cp_parser* parser)
15546 {
15547   cp_token *token;
15548   enum tag_types tag_type;
15549
15550   /* Look for the class-key.  */
15551   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15552   if (!token)
15553     return none_type;
15554
15555   /* Check to see if the TOKEN is a class-key.  */
15556   tag_type = cp_parser_token_is_class_key (token);
15557   if (!tag_type)
15558     cp_parser_error (parser, "expected class-key");
15559   return tag_type;
15560 }
15561
15562 /* Parse an (optional) member-specification.
15563
15564    member-specification:
15565      member-declaration member-specification [opt]
15566      access-specifier : member-specification [opt]  */
15567
15568 static void
15569 cp_parser_member_specification_opt (cp_parser* parser)
15570 {
15571   while (true)
15572     {
15573       cp_token *token;
15574       enum rid keyword;
15575
15576       /* Peek at the next token.  */
15577       token = cp_lexer_peek_token (parser->lexer);
15578       /* If it's a `}', or EOF then we've seen all the members.  */
15579       if (token->type == CPP_CLOSE_BRACE
15580           || token->type == CPP_EOF
15581           || token->type == CPP_PRAGMA_EOL)
15582         break;
15583
15584       /* See if this token is a keyword.  */
15585       keyword = token->keyword;
15586       switch (keyword)
15587         {
15588         case RID_PUBLIC:
15589         case RID_PROTECTED:
15590         case RID_PRIVATE:
15591           /* Consume the access-specifier.  */
15592           cp_lexer_consume_token (parser->lexer);
15593           /* Remember which access-specifier is active.  */
15594           current_access_specifier = token->u.value;
15595           /* Look for the `:'.  */
15596           cp_parser_require (parser, CPP_COLON, "%<:%>");
15597           break;
15598
15599         default:
15600           /* Accept #pragmas at class scope.  */
15601           if (token->type == CPP_PRAGMA)
15602             {
15603               cp_parser_pragma (parser, pragma_external);
15604               break;
15605             }
15606
15607           /* Otherwise, the next construction must be a
15608              member-declaration.  */
15609           cp_parser_member_declaration (parser);
15610         }
15611     }
15612 }
15613
15614 /* Parse a member-declaration.
15615
15616    member-declaration:
15617      decl-specifier-seq [opt] member-declarator-list [opt] ;
15618      function-definition ; [opt]
15619      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15620      using-declaration
15621      template-declaration
15622
15623    member-declarator-list:
15624      member-declarator
15625      member-declarator-list , member-declarator
15626
15627    member-declarator:
15628      declarator pure-specifier [opt]
15629      declarator constant-initializer [opt]
15630      identifier [opt] : constant-expression
15631
15632    GNU Extensions:
15633
15634    member-declaration:
15635      __extension__ member-declaration
15636
15637    member-declarator:
15638      declarator attributes [opt] pure-specifier [opt]
15639      declarator attributes [opt] constant-initializer [opt]
15640      identifier [opt] attributes [opt] : constant-expression  
15641
15642    C++0x Extensions:
15643
15644    member-declaration:
15645      static_assert-declaration  */
15646
15647 static void
15648 cp_parser_member_declaration (cp_parser* parser)
15649 {
15650   cp_decl_specifier_seq decl_specifiers;
15651   tree prefix_attributes;
15652   tree decl;
15653   int declares_class_or_enum;
15654   bool friend_p;
15655   cp_token *token = NULL;
15656   cp_token *decl_spec_token_start = NULL;
15657   cp_token *initializer_token_start = NULL;
15658   int saved_pedantic;
15659
15660   /* Check for the `__extension__' keyword.  */
15661   if (cp_parser_extension_opt (parser, &saved_pedantic))
15662     {
15663       /* Recurse.  */
15664       cp_parser_member_declaration (parser);
15665       /* Restore the old value of the PEDANTIC flag.  */
15666       pedantic = saved_pedantic;
15667
15668       return;
15669     }
15670
15671   /* Check for a template-declaration.  */
15672   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15673     {
15674       /* An explicit specialization here is an error condition, and we
15675          expect the specialization handler to detect and report this.  */
15676       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15677           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15678         cp_parser_explicit_specialization (parser);
15679       else
15680         cp_parser_template_declaration (parser, /*member_p=*/true);
15681
15682       return;
15683     }
15684
15685   /* Check for a using-declaration.  */
15686   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15687     {
15688       /* Parse the using-declaration.  */
15689       cp_parser_using_declaration (parser,
15690                                    /*access_declaration_p=*/false);
15691       return;
15692     }
15693
15694   /* Check for @defs.  */
15695   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15696     {
15697       tree ivar, member;
15698       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15699       ivar = ivar_chains;
15700       while (ivar)
15701         {
15702           member = ivar;
15703           ivar = TREE_CHAIN (member);
15704           TREE_CHAIN (member) = NULL_TREE;
15705           finish_member_declaration (member);
15706         }
15707       return;
15708     }
15709
15710   /* If the next token is `static_assert' we have a static assertion.  */
15711   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15712     {
15713       cp_parser_static_assert (parser, /*member_p=*/true);
15714       return;
15715     }
15716
15717   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15718     return;
15719
15720   /* Parse the decl-specifier-seq.  */
15721   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15722   cp_parser_decl_specifier_seq (parser,
15723                                 CP_PARSER_FLAGS_OPTIONAL,
15724                                 &decl_specifiers,
15725                                 &declares_class_or_enum);
15726   prefix_attributes = decl_specifiers.attributes;
15727   decl_specifiers.attributes = NULL_TREE;
15728   /* Check for an invalid type-name.  */
15729   if (!decl_specifiers.type
15730       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15731     return;
15732   /* If there is no declarator, then the decl-specifier-seq should
15733      specify a type.  */
15734   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15735     {
15736       /* If there was no decl-specifier-seq, and the next token is a
15737          `;', then we have something like:
15738
15739            struct S { ; };
15740
15741          [class.mem]
15742
15743          Each member-declaration shall declare at least one member
15744          name of the class.  */
15745       if (!decl_specifiers.any_specifiers_p)
15746         {
15747           cp_token *token = cp_lexer_peek_token (parser->lexer);
15748           if (!in_system_header_at (token->location))
15749             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15750         }
15751       else
15752         {
15753           tree type;
15754
15755           /* See if this declaration is a friend.  */
15756           friend_p = cp_parser_friend_p (&decl_specifiers);
15757           /* If there were decl-specifiers, check to see if there was
15758              a class-declaration.  */
15759           type = check_tag_decl (&decl_specifiers);
15760           /* Nested classes have already been added to the class, but
15761              a `friend' needs to be explicitly registered.  */
15762           if (friend_p)
15763             {
15764               /* If the `friend' keyword was present, the friend must
15765                  be introduced with a class-key.  */
15766                if (!declares_class_or_enum)
15767                  error ("%Ha class-key must be used when declaring a friend",
15768                         &decl_spec_token_start->location);
15769                /* In this case:
15770
15771                     template <typename T> struct A {
15772                       friend struct A<T>::B;
15773                     };
15774
15775                   A<T>::B will be represented by a TYPENAME_TYPE, and
15776                   therefore not recognized by check_tag_decl.  */
15777                if (!type
15778                    && decl_specifiers.type
15779                    && TYPE_P (decl_specifiers.type))
15780                  type = decl_specifiers.type;
15781                if (!type || !TYPE_P (type))
15782                  error ("%Hfriend declaration does not name a class or "
15783                         "function", &decl_spec_token_start->location);
15784                else
15785                  make_friend_class (current_class_type, type,
15786                                     /*complain=*/true);
15787             }
15788           /* If there is no TYPE, an error message will already have
15789              been issued.  */
15790           else if (!type || type == error_mark_node)
15791             ;
15792           /* An anonymous aggregate has to be handled specially; such
15793              a declaration really declares a data member (with a
15794              particular type), as opposed to a nested class.  */
15795           else if (ANON_AGGR_TYPE_P (type))
15796             {
15797               /* Remove constructors and such from TYPE, now that we
15798                  know it is an anonymous aggregate.  */
15799               fixup_anonymous_aggr (type);
15800               /* And make the corresponding data member.  */
15801               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15802               /* Add it to the class.  */
15803               finish_member_declaration (decl);
15804             }
15805           else
15806             cp_parser_check_access_in_redeclaration
15807                                               (TYPE_NAME (type),
15808                                                decl_spec_token_start->location);
15809         }
15810     }
15811   else
15812     {
15813       /* See if these declarations will be friends.  */
15814       friend_p = cp_parser_friend_p (&decl_specifiers);
15815
15816       /* Keep going until we hit the `;' at the end of the
15817          declaration.  */
15818       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15819         {
15820           tree attributes = NULL_TREE;
15821           tree first_attribute;
15822
15823           /* Peek at the next token.  */
15824           token = cp_lexer_peek_token (parser->lexer);
15825
15826           /* Check for a bitfield declaration.  */
15827           if (token->type == CPP_COLON
15828               || (token->type == CPP_NAME
15829                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15830                   == CPP_COLON))
15831             {
15832               tree identifier;
15833               tree width;
15834
15835               /* Get the name of the bitfield.  Note that we cannot just
15836                  check TOKEN here because it may have been invalidated by
15837                  the call to cp_lexer_peek_nth_token above.  */
15838               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15839                 identifier = cp_parser_identifier (parser);
15840               else
15841                 identifier = NULL_TREE;
15842
15843               /* Consume the `:' token.  */
15844               cp_lexer_consume_token (parser->lexer);
15845               /* Get the width of the bitfield.  */
15846               width
15847                 = cp_parser_constant_expression (parser,
15848                                                  /*allow_non_constant=*/false,
15849                                                  NULL);
15850
15851               /* Look for attributes that apply to the bitfield.  */
15852               attributes = cp_parser_attributes_opt (parser);
15853               /* Remember which attributes are prefix attributes and
15854                  which are not.  */
15855               first_attribute = attributes;
15856               /* Combine the attributes.  */
15857               attributes = chainon (prefix_attributes, attributes);
15858
15859               /* Create the bitfield declaration.  */
15860               decl = grokbitfield (identifier
15861                                    ? make_id_declarator (NULL_TREE,
15862                                                          identifier,
15863                                                          sfk_none)
15864                                    : NULL,
15865                                    &decl_specifiers,
15866                                    width,
15867                                    attributes);
15868             }
15869           else
15870             {
15871               cp_declarator *declarator;
15872               tree initializer;
15873               tree asm_specification;
15874               int ctor_dtor_or_conv_p;
15875
15876               /* Parse the declarator.  */
15877               declarator
15878                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15879                                         &ctor_dtor_or_conv_p,
15880                                         /*parenthesized_p=*/NULL,
15881                                         /*member_p=*/true);
15882
15883               /* If something went wrong parsing the declarator, make sure
15884                  that we at least consume some tokens.  */
15885               if (declarator == cp_error_declarator)
15886                 {
15887                   /* Skip to the end of the statement.  */
15888                   cp_parser_skip_to_end_of_statement (parser);
15889                   /* If the next token is not a semicolon, that is
15890                      probably because we just skipped over the body of
15891                      a function.  So, we consume a semicolon if
15892                      present, but do not issue an error message if it
15893                      is not present.  */
15894                   if (cp_lexer_next_token_is (parser->lexer,
15895                                               CPP_SEMICOLON))
15896                     cp_lexer_consume_token (parser->lexer);
15897                   return;
15898                 }
15899
15900               if (declares_class_or_enum & 2)
15901                 cp_parser_check_for_definition_in_return_type
15902                                             (declarator, decl_specifiers.type,
15903                                              decl_specifiers.type_location);
15904
15905               /* Look for an asm-specification.  */
15906               asm_specification = cp_parser_asm_specification_opt (parser);
15907               /* Look for attributes that apply to the declaration.  */
15908               attributes = cp_parser_attributes_opt (parser);
15909               /* Remember which attributes are prefix attributes and
15910                  which are not.  */
15911               first_attribute = attributes;
15912               /* Combine the attributes.  */
15913               attributes = chainon (prefix_attributes, attributes);
15914
15915               /* If it's an `=', then we have a constant-initializer or a
15916                  pure-specifier.  It is not correct to parse the
15917                  initializer before registering the member declaration
15918                  since the member declaration should be in scope while
15919                  its initializer is processed.  However, the rest of the
15920                  front end does not yet provide an interface that allows
15921                  us to handle this correctly.  */
15922               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15923                 {
15924                   /* In [class.mem]:
15925
15926                      A pure-specifier shall be used only in the declaration of
15927                      a virtual function.
15928
15929                      A member-declarator can contain a constant-initializer
15930                      only if it declares a static member of integral or
15931                      enumeration type.
15932
15933                      Therefore, if the DECLARATOR is for a function, we look
15934                      for a pure-specifier; otherwise, we look for a
15935                      constant-initializer.  When we call `grokfield', it will
15936                      perform more stringent semantics checks.  */
15937                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15938                   if (function_declarator_p (declarator))
15939                     initializer = cp_parser_pure_specifier (parser);
15940                   else
15941                     /* Parse the initializer.  */
15942                     initializer = cp_parser_constant_initializer (parser);
15943                 }
15944               /* Otherwise, there is no initializer.  */
15945               else
15946                 initializer = NULL_TREE;
15947
15948               /* See if we are probably looking at a function
15949                  definition.  We are certainly not looking at a
15950                  member-declarator.  Calling `grokfield' has
15951                  side-effects, so we must not do it unless we are sure
15952                  that we are looking at a member-declarator.  */
15953               if (cp_parser_token_starts_function_definition_p
15954                   (cp_lexer_peek_token (parser->lexer)))
15955                 {
15956                   /* The grammar does not allow a pure-specifier to be
15957                      used when a member function is defined.  (It is
15958                      possible that this fact is an oversight in the
15959                      standard, since a pure function may be defined
15960                      outside of the class-specifier.  */
15961                   if (initializer)
15962                     error ("%Hpure-specifier on function-definition",
15963                            &initializer_token_start->location);
15964                   decl = cp_parser_save_member_function_body (parser,
15965                                                               &decl_specifiers,
15966                                                               declarator,
15967                                                               attributes);
15968                   /* If the member was not a friend, declare it here.  */
15969                   if (!friend_p)
15970                     finish_member_declaration (decl);
15971                   /* Peek at the next token.  */
15972                   token = cp_lexer_peek_token (parser->lexer);
15973                   /* If the next token is a semicolon, consume it.  */
15974                   if (token->type == CPP_SEMICOLON)
15975                     cp_lexer_consume_token (parser->lexer);
15976                   return;
15977                 }
15978               else
15979                 if (declarator->kind == cdk_function)
15980                   declarator->id_loc = token->location;
15981                 /* Create the declaration.  */
15982                 decl = grokfield (declarator, &decl_specifiers,
15983                                   initializer, /*init_const_expr_p=*/true,
15984                                   asm_specification,
15985                                   attributes);
15986             }
15987
15988           /* Reset PREFIX_ATTRIBUTES.  */
15989           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15990             attributes = TREE_CHAIN (attributes);
15991           if (attributes)
15992             TREE_CHAIN (attributes) = NULL_TREE;
15993
15994           /* If there is any qualification still in effect, clear it
15995              now; we will be starting fresh with the next declarator.  */
15996           parser->scope = NULL_TREE;
15997           parser->qualifying_scope = NULL_TREE;
15998           parser->object_scope = NULL_TREE;
15999           /* If it's a `,', then there are more declarators.  */
16000           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16001             cp_lexer_consume_token (parser->lexer);
16002           /* If the next token isn't a `;', then we have a parse error.  */
16003           else if (cp_lexer_next_token_is_not (parser->lexer,
16004                                                CPP_SEMICOLON))
16005             {
16006               cp_parser_error (parser, "expected %<;%>");
16007               /* Skip tokens until we find a `;'.  */
16008               cp_parser_skip_to_end_of_statement (parser);
16009
16010               break;
16011             }
16012
16013           if (decl)
16014             {
16015               /* Add DECL to the list of members.  */
16016               if (!friend_p)
16017                 finish_member_declaration (decl);
16018
16019               if (TREE_CODE (decl) == FUNCTION_DECL)
16020                 cp_parser_save_default_args (parser, decl);
16021             }
16022         }
16023     }
16024
16025   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16026 }
16027
16028 /* Parse a pure-specifier.
16029
16030    pure-specifier:
16031      = 0
16032
16033    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16034    Otherwise, ERROR_MARK_NODE is returned.  */
16035
16036 static tree
16037 cp_parser_pure_specifier (cp_parser* parser)
16038 {
16039   cp_token *token;
16040
16041   /* Look for the `=' token.  */
16042   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16043     return error_mark_node;
16044   /* Look for the `0' token.  */
16045   token = cp_lexer_peek_token (parser->lexer);
16046
16047   if (token->type == CPP_EOF
16048       || token->type == CPP_PRAGMA_EOL)
16049     return error_mark_node;
16050
16051   cp_lexer_consume_token (parser->lexer);
16052
16053   /* Accept = default or = delete in c++0x mode.  */
16054   if (token->keyword == RID_DEFAULT
16055       || token->keyword == RID_DELETE)
16056     {
16057       maybe_warn_cpp0x ("defaulted and deleted functions");
16058       return token->u.value;
16059     }
16060
16061   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16062   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16063     {
16064       cp_parser_error (parser,
16065                        "invalid pure specifier (only %<= 0%> is allowed)");
16066       cp_parser_skip_to_end_of_statement (parser);
16067       return error_mark_node;
16068     }
16069   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16070     {
16071       error ("%Htemplates may not be %<virtual%>", &token->location);
16072       return error_mark_node;
16073     }
16074
16075   return integer_zero_node;
16076 }
16077
16078 /* Parse a constant-initializer.
16079
16080    constant-initializer:
16081      = constant-expression
16082
16083    Returns a representation of the constant-expression.  */
16084
16085 static tree
16086 cp_parser_constant_initializer (cp_parser* parser)
16087 {
16088   /* Look for the `=' token.  */
16089   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16090     return error_mark_node;
16091
16092   /* It is invalid to write:
16093
16094        struct S { static const int i = { 7 }; };
16095
16096      */
16097   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16098     {
16099       cp_parser_error (parser,
16100                        "a brace-enclosed initializer is not allowed here");
16101       /* Consume the opening brace.  */
16102       cp_lexer_consume_token (parser->lexer);
16103       /* Skip the initializer.  */
16104       cp_parser_skip_to_closing_brace (parser);
16105       /* Look for the trailing `}'.  */
16106       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16107
16108       return error_mark_node;
16109     }
16110
16111   return cp_parser_constant_expression (parser,
16112                                         /*allow_non_constant=*/false,
16113                                         NULL);
16114 }
16115
16116 /* Derived classes [gram.class.derived] */
16117
16118 /* Parse a base-clause.
16119
16120    base-clause:
16121      : base-specifier-list
16122
16123    base-specifier-list:
16124      base-specifier ... [opt]
16125      base-specifier-list , base-specifier ... [opt]
16126
16127    Returns a TREE_LIST representing the base-classes, in the order in
16128    which they were declared.  The representation of each node is as
16129    described by cp_parser_base_specifier.
16130
16131    In the case that no bases are specified, this function will return
16132    NULL_TREE, not ERROR_MARK_NODE.  */
16133
16134 static tree
16135 cp_parser_base_clause (cp_parser* parser)
16136 {
16137   tree bases = NULL_TREE;
16138
16139   /* Look for the `:' that begins the list.  */
16140   cp_parser_require (parser, CPP_COLON, "%<:%>");
16141
16142   /* Scan the base-specifier-list.  */
16143   while (true)
16144     {
16145       cp_token *token;
16146       tree base;
16147       bool pack_expansion_p = false;
16148
16149       /* Look for the base-specifier.  */
16150       base = cp_parser_base_specifier (parser);
16151       /* Look for the (optional) ellipsis. */
16152       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16153         {
16154           /* Consume the `...'. */
16155           cp_lexer_consume_token (parser->lexer);
16156
16157           pack_expansion_p = true;
16158         }
16159
16160       /* Add BASE to the front of the list.  */
16161       if (base != error_mark_node)
16162         {
16163           if (pack_expansion_p)
16164             /* Make this a pack expansion type. */
16165             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16166           
16167
16168           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16169             {
16170               TREE_CHAIN (base) = bases;
16171               bases = base;
16172             }
16173         }
16174       /* Peek at the next token.  */
16175       token = cp_lexer_peek_token (parser->lexer);
16176       /* If it's not a comma, then the list is complete.  */
16177       if (token->type != CPP_COMMA)
16178         break;
16179       /* Consume the `,'.  */
16180       cp_lexer_consume_token (parser->lexer);
16181     }
16182
16183   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16184      base class had a qualified name.  However, the next name that
16185      appears is certainly not qualified.  */
16186   parser->scope = NULL_TREE;
16187   parser->qualifying_scope = NULL_TREE;
16188   parser->object_scope = NULL_TREE;
16189
16190   return nreverse (bases);
16191 }
16192
16193 /* Parse a base-specifier.
16194
16195    base-specifier:
16196      :: [opt] nested-name-specifier [opt] class-name
16197      virtual access-specifier [opt] :: [opt] nested-name-specifier
16198        [opt] class-name
16199      access-specifier virtual [opt] :: [opt] nested-name-specifier
16200        [opt] class-name
16201
16202    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16203    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16204    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16205    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16206
16207 static tree
16208 cp_parser_base_specifier (cp_parser* parser)
16209 {
16210   cp_token *token;
16211   bool done = false;
16212   bool virtual_p = false;
16213   bool duplicate_virtual_error_issued_p = false;
16214   bool duplicate_access_error_issued_p = false;
16215   bool class_scope_p, template_p;
16216   tree access = access_default_node;
16217   tree type;
16218
16219   /* Process the optional `virtual' and `access-specifier'.  */
16220   while (!done)
16221     {
16222       /* Peek at the next token.  */
16223       token = cp_lexer_peek_token (parser->lexer);
16224       /* Process `virtual'.  */
16225       switch (token->keyword)
16226         {
16227         case RID_VIRTUAL:
16228           /* If `virtual' appears more than once, issue an error.  */
16229           if (virtual_p && !duplicate_virtual_error_issued_p)
16230             {
16231               cp_parser_error (parser,
16232                                "%<virtual%> specified more than once in base-specified");
16233               duplicate_virtual_error_issued_p = true;
16234             }
16235
16236           virtual_p = true;
16237
16238           /* Consume the `virtual' token.  */
16239           cp_lexer_consume_token (parser->lexer);
16240
16241           break;
16242
16243         case RID_PUBLIC:
16244         case RID_PROTECTED:
16245         case RID_PRIVATE:
16246           /* If more than one access specifier appears, issue an
16247              error.  */
16248           if (access != access_default_node
16249               && !duplicate_access_error_issued_p)
16250             {
16251               cp_parser_error (parser,
16252                                "more than one access specifier in base-specified");
16253               duplicate_access_error_issued_p = true;
16254             }
16255
16256           access = ridpointers[(int) token->keyword];
16257
16258           /* Consume the access-specifier.  */
16259           cp_lexer_consume_token (parser->lexer);
16260
16261           break;
16262
16263         default:
16264           done = true;
16265           break;
16266         }
16267     }
16268   /* It is not uncommon to see programs mechanically, erroneously, use
16269      the 'typename' keyword to denote (dependent) qualified types
16270      as base classes.  */
16271   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16272     {
16273       token = cp_lexer_peek_token (parser->lexer);
16274       if (!processing_template_decl)
16275         error ("%Hkeyword %<typename%> not allowed outside of templates",
16276                &token->location);
16277       else
16278         error ("%Hkeyword %<typename%> not allowed in this context "
16279                "(the base class is implicitly a type)",
16280                &token->location);
16281       cp_lexer_consume_token (parser->lexer);
16282     }
16283
16284   /* Look for the optional `::' operator.  */
16285   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16286   /* Look for the nested-name-specifier.  The simplest way to
16287      implement:
16288
16289        [temp.res]
16290
16291        The keyword `typename' is not permitted in a base-specifier or
16292        mem-initializer; in these contexts a qualified name that
16293        depends on a template-parameter is implicitly assumed to be a
16294        type name.
16295
16296      is to pretend that we have seen the `typename' keyword at this
16297      point.  */
16298   cp_parser_nested_name_specifier_opt (parser,
16299                                        /*typename_keyword_p=*/true,
16300                                        /*check_dependency_p=*/true,
16301                                        typename_type,
16302                                        /*is_declaration=*/true);
16303   /* If the base class is given by a qualified name, assume that names
16304      we see are type names or templates, as appropriate.  */
16305   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16306   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16307
16308   /* Finally, look for the class-name.  */
16309   type = cp_parser_class_name (parser,
16310                                class_scope_p,
16311                                template_p,
16312                                typename_type,
16313                                /*check_dependency_p=*/true,
16314                                /*class_head_p=*/false,
16315                                /*is_declaration=*/true);
16316
16317   if (type == error_mark_node)
16318     return error_mark_node;
16319
16320   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16321 }
16322
16323 /* Exception handling [gram.exception] */
16324
16325 /* Parse an (optional) exception-specification.
16326
16327    exception-specification:
16328      throw ( type-id-list [opt] )
16329
16330    Returns a TREE_LIST representing the exception-specification.  The
16331    TREE_VALUE of each node is a type.  */
16332
16333 static tree
16334 cp_parser_exception_specification_opt (cp_parser* parser)
16335 {
16336   cp_token *token;
16337   tree type_id_list;
16338
16339   /* Peek at the next token.  */
16340   token = cp_lexer_peek_token (parser->lexer);
16341   /* If it's not `throw', then there's no exception-specification.  */
16342   if (!cp_parser_is_keyword (token, RID_THROW))
16343     return NULL_TREE;
16344
16345   /* Consume the `throw'.  */
16346   cp_lexer_consume_token (parser->lexer);
16347
16348   /* Look for the `('.  */
16349   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16350
16351   /* Peek at the next token.  */
16352   token = cp_lexer_peek_token (parser->lexer);
16353   /* If it's not a `)', then there is a type-id-list.  */
16354   if (token->type != CPP_CLOSE_PAREN)
16355     {
16356       const char *saved_message;
16357
16358       /* Types may not be defined in an exception-specification.  */
16359       saved_message = parser->type_definition_forbidden_message;
16360       parser->type_definition_forbidden_message
16361         = "types may not be defined in an exception-specification";
16362       /* Parse the type-id-list.  */
16363       type_id_list = cp_parser_type_id_list (parser);
16364       /* Restore the saved message.  */
16365       parser->type_definition_forbidden_message = saved_message;
16366     }
16367   else
16368     type_id_list = empty_except_spec;
16369
16370   /* Look for the `)'.  */
16371   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16372
16373   return type_id_list;
16374 }
16375
16376 /* Parse an (optional) type-id-list.
16377
16378    type-id-list:
16379      type-id ... [opt]
16380      type-id-list , type-id ... [opt]
16381
16382    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16383    in the order that the types were presented.  */
16384
16385 static tree
16386 cp_parser_type_id_list (cp_parser* parser)
16387 {
16388   tree types = NULL_TREE;
16389
16390   while (true)
16391     {
16392       cp_token *token;
16393       tree type;
16394
16395       /* Get the next type-id.  */
16396       type = cp_parser_type_id (parser);
16397       /* Parse the optional ellipsis. */
16398       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16399         {
16400           /* Consume the `...'. */
16401           cp_lexer_consume_token (parser->lexer);
16402
16403           /* Turn the type into a pack expansion expression. */
16404           type = make_pack_expansion (type);
16405         }
16406       /* Add it to the list.  */
16407       types = add_exception_specifier (types, type, /*complain=*/1);
16408       /* Peek at the next token.  */
16409       token = cp_lexer_peek_token (parser->lexer);
16410       /* If it is not a `,', we are done.  */
16411       if (token->type != CPP_COMMA)
16412         break;
16413       /* Consume the `,'.  */
16414       cp_lexer_consume_token (parser->lexer);
16415     }
16416
16417   return nreverse (types);
16418 }
16419
16420 /* Parse a try-block.
16421
16422    try-block:
16423      try compound-statement handler-seq  */
16424
16425 static tree
16426 cp_parser_try_block (cp_parser* parser)
16427 {
16428   tree try_block;
16429
16430   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16431   try_block = begin_try_block ();
16432   cp_parser_compound_statement (parser, NULL, true);
16433   finish_try_block (try_block);
16434   cp_parser_handler_seq (parser);
16435   finish_handler_sequence (try_block);
16436
16437   return try_block;
16438 }
16439
16440 /* Parse a function-try-block.
16441
16442    function-try-block:
16443      try ctor-initializer [opt] function-body handler-seq  */
16444
16445 static bool
16446 cp_parser_function_try_block (cp_parser* parser)
16447 {
16448   tree compound_stmt;
16449   tree try_block;
16450   bool ctor_initializer_p;
16451
16452   /* Look for the `try' keyword.  */
16453   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16454     return false;
16455   /* Let the rest of the front end know where we are.  */
16456   try_block = begin_function_try_block (&compound_stmt);
16457   /* Parse the function-body.  */
16458   ctor_initializer_p
16459     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16460   /* We're done with the `try' part.  */
16461   finish_function_try_block (try_block);
16462   /* Parse the handlers.  */
16463   cp_parser_handler_seq (parser);
16464   /* We're done with the handlers.  */
16465   finish_function_handler_sequence (try_block, compound_stmt);
16466
16467   return ctor_initializer_p;
16468 }
16469
16470 /* Parse a handler-seq.
16471
16472    handler-seq:
16473      handler handler-seq [opt]  */
16474
16475 static void
16476 cp_parser_handler_seq (cp_parser* parser)
16477 {
16478   while (true)
16479     {
16480       cp_token *token;
16481
16482       /* Parse the handler.  */
16483       cp_parser_handler (parser);
16484       /* Peek at the next token.  */
16485       token = cp_lexer_peek_token (parser->lexer);
16486       /* If it's not `catch' then there are no more handlers.  */
16487       if (!cp_parser_is_keyword (token, RID_CATCH))
16488         break;
16489     }
16490 }
16491
16492 /* Parse a handler.
16493
16494    handler:
16495      catch ( exception-declaration ) compound-statement  */
16496
16497 static void
16498 cp_parser_handler (cp_parser* parser)
16499 {
16500   tree handler;
16501   tree declaration;
16502
16503   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16504   handler = begin_handler ();
16505   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16506   declaration = cp_parser_exception_declaration (parser);
16507   finish_handler_parms (declaration, handler);
16508   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16509   cp_parser_compound_statement (parser, NULL, false);
16510   finish_handler (handler);
16511 }
16512
16513 /* Parse an exception-declaration.
16514
16515    exception-declaration:
16516      type-specifier-seq declarator
16517      type-specifier-seq abstract-declarator
16518      type-specifier-seq
16519      ...
16520
16521    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16522    ellipsis variant is used.  */
16523
16524 static tree
16525 cp_parser_exception_declaration (cp_parser* parser)
16526 {
16527   cp_decl_specifier_seq type_specifiers;
16528   cp_declarator *declarator;
16529   const char *saved_message;
16530
16531   /* If it's an ellipsis, it's easy to handle.  */
16532   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16533     {
16534       /* Consume the `...' token.  */
16535       cp_lexer_consume_token (parser->lexer);
16536       return NULL_TREE;
16537     }
16538
16539   /* Types may not be defined in exception-declarations.  */
16540   saved_message = parser->type_definition_forbidden_message;
16541   parser->type_definition_forbidden_message
16542     = "types may not be defined in exception-declarations";
16543
16544   /* Parse the type-specifier-seq.  */
16545   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16546                                 &type_specifiers);
16547   /* If it's a `)', then there is no declarator.  */
16548   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16549     declarator = NULL;
16550   else
16551     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16552                                        /*ctor_dtor_or_conv_p=*/NULL,
16553                                        /*parenthesized_p=*/NULL,
16554                                        /*member_p=*/false);
16555
16556   /* Restore the saved message.  */
16557   parser->type_definition_forbidden_message = saved_message;
16558
16559   if (!type_specifiers.any_specifiers_p)
16560     return error_mark_node;
16561
16562   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16563 }
16564
16565 /* Parse a throw-expression.
16566
16567    throw-expression:
16568      throw assignment-expression [opt]
16569
16570    Returns a THROW_EXPR representing the throw-expression.  */
16571
16572 static tree
16573 cp_parser_throw_expression (cp_parser* parser)
16574 {
16575   tree expression;
16576   cp_token* token;
16577
16578   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16579   token = cp_lexer_peek_token (parser->lexer);
16580   /* Figure out whether or not there is an assignment-expression
16581      following the "throw" keyword.  */
16582   if (token->type == CPP_COMMA
16583       || token->type == CPP_SEMICOLON
16584       || token->type == CPP_CLOSE_PAREN
16585       || token->type == CPP_CLOSE_SQUARE
16586       || token->type == CPP_CLOSE_BRACE
16587       || token->type == CPP_COLON)
16588     expression = NULL_TREE;
16589   else
16590     expression = cp_parser_assignment_expression (parser,
16591                                                   /*cast_p=*/false, NULL);
16592
16593   return build_throw (expression);
16594 }
16595
16596 /* GNU Extensions */
16597
16598 /* Parse an (optional) asm-specification.
16599
16600    asm-specification:
16601      asm ( string-literal )
16602
16603    If the asm-specification is present, returns a STRING_CST
16604    corresponding to the string-literal.  Otherwise, returns
16605    NULL_TREE.  */
16606
16607 static tree
16608 cp_parser_asm_specification_opt (cp_parser* parser)
16609 {
16610   cp_token *token;
16611   tree asm_specification;
16612
16613   /* Peek at the next token.  */
16614   token = cp_lexer_peek_token (parser->lexer);
16615   /* If the next token isn't the `asm' keyword, then there's no
16616      asm-specification.  */
16617   if (!cp_parser_is_keyword (token, RID_ASM))
16618     return NULL_TREE;
16619
16620   /* Consume the `asm' token.  */
16621   cp_lexer_consume_token (parser->lexer);
16622   /* Look for the `('.  */
16623   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16624
16625   /* Look for the string-literal.  */
16626   asm_specification = cp_parser_string_literal (parser, false, false);
16627
16628   /* Look for the `)'.  */
16629   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16630
16631   return asm_specification;
16632 }
16633
16634 /* Parse an asm-operand-list.
16635
16636    asm-operand-list:
16637      asm-operand
16638      asm-operand-list , asm-operand
16639
16640    asm-operand:
16641      string-literal ( expression )
16642      [ string-literal ] string-literal ( expression )
16643
16644    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16645    each node is the expression.  The TREE_PURPOSE is itself a
16646    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16647    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16648    is a STRING_CST for the string literal before the parenthesis. Returns
16649    ERROR_MARK_NODE if any of the operands are invalid.  */
16650
16651 static tree
16652 cp_parser_asm_operand_list (cp_parser* parser)
16653 {
16654   tree asm_operands = NULL_TREE;
16655   bool invalid_operands = false;
16656
16657   while (true)
16658     {
16659       tree string_literal;
16660       tree expression;
16661       tree name;
16662
16663       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16664         {
16665           /* Consume the `[' token.  */
16666           cp_lexer_consume_token (parser->lexer);
16667           /* Read the operand name.  */
16668           name = cp_parser_identifier (parser);
16669           if (name != error_mark_node)
16670             name = build_string (IDENTIFIER_LENGTH (name),
16671                                  IDENTIFIER_POINTER (name));
16672           /* Look for the closing `]'.  */
16673           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16674         }
16675       else
16676         name = NULL_TREE;
16677       /* Look for the string-literal.  */
16678       string_literal = cp_parser_string_literal (parser, false, false);
16679
16680       /* Look for the `('.  */
16681       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16682       /* Parse the expression.  */
16683       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16684       /* Look for the `)'.  */
16685       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16686
16687       if (name == error_mark_node 
16688           || string_literal == error_mark_node 
16689           || expression == error_mark_node)
16690         invalid_operands = true;
16691
16692       /* Add this operand to the list.  */
16693       asm_operands = tree_cons (build_tree_list (name, string_literal),
16694                                 expression,
16695                                 asm_operands);
16696       /* If the next token is not a `,', there are no more
16697          operands.  */
16698       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16699         break;
16700       /* Consume the `,'.  */
16701       cp_lexer_consume_token (parser->lexer);
16702     }
16703
16704   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16705 }
16706
16707 /* Parse an asm-clobber-list.
16708
16709    asm-clobber-list:
16710      string-literal
16711      asm-clobber-list , string-literal
16712
16713    Returns a TREE_LIST, indicating the clobbers in the order that they
16714    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16715
16716 static tree
16717 cp_parser_asm_clobber_list (cp_parser* parser)
16718 {
16719   tree clobbers = NULL_TREE;
16720
16721   while (true)
16722     {
16723       tree string_literal;
16724
16725       /* Look for the string literal.  */
16726       string_literal = cp_parser_string_literal (parser, false, false);
16727       /* Add it to the list.  */
16728       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16729       /* If the next token is not a `,', then the list is
16730          complete.  */
16731       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16732         break;
16733       /* Consume the `,' token.  */
16734       cp_lexer_consume_token (parser->lexer);
16735     }
16736
16737   return clobbers;
16738 }
16739
16740 /* Parse an (optional) series of attributes.
16741
16742    attributes:
16743      attributes attribute
16744
16745    attribute:
16746      __attribute__ (( attribute-list [opt] ))
16747
16748    The return value is as for cp_parser_attribute_list.  */
16749
16750 static tree
16751 cp_parser_attributes_opt (cp_parser* parser)
16752 {
16753   tree attributes = NULL_TREE;
16754
16755   while (true)
16756     {
16757       cp_token *token;
16758       tree attribute_list;
16759
16760       /* Peek at the next token.  */
16761       token = cp_lexer_peek_token (parser->lexer);
16762       /* If it's not `__attribute__', then we're done.  */
16763       if (token->keyword != RID_ATTRIBUTE)
16764         break;
16765
16766       /* Consume the `__attribute__' keyword.  */
16767       cp_lexer_consume_token (parser->lexer);
16768       /* Look for the two `(' tokens.  */
16769       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16770       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16771
16772       /* Peek at the next token.  */
16773       token = cp_lexer_peek_token (parser->lexer);
16774       if (token->type != CPP_CLOSE_PAREN)
16775         /* Parse the attribute-list.  */
16776         attribute_list = cp_parser_attribute_list (parser);
16777       else
16778         /* If the next token is a `)', then there is no attribute
16779            list.  */
16780         attribute_list = NULL;
16781
16782       /* Look for the two `)' tokens.  */
16783       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16784       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16785
16786       /* Add these new attributes to the list.  */
16787       attributes = chainon (attributes, attribute_list);
16788     }
16789
16790   return attributes;
16791 }
16792
16793 /* Parse an attribute-list.
16794
16795    attribute-list:
16796      attribute
16797      attribute-list , attribute
16798
16799    attribute:
16800      identifier
16801      identifier ( identifier )
16802      identifier ( identifier , expression-list )
16803      identifier ( expression-list )
16804
16805    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16806    to an attribute.  The TREE_PURPOSE of each node is the identifier
16807    indicating which attribute is in use.  The TREE_VALUE represents
16808    the arguments, if any.  */
16809
16810 static tree
16811 cp_parser_attribute_list (cp_parser* parser)
16812 {
16813   tree attribute_list = NULL_TREE;
16814   bool save_translate_strings_p = parser->translate_strings_p;
16815
16816   parser->translate_strings_p = false;
16817   while (true)
16818     {
16819       cp_token *token;
16820       tree identifier;
16821       tree attribute;
16822
16823       /* Look for the identifier.  We also allow keywords here; for
16824          example `__attribute__ ((const))' is legal.  */
16825       token = cp_lexer_peek_token (parser->lexer);
16826       if (token->type == CPP_NAME
16827           || token->type == CPP_KEYWORD)
16828         {
16829           tree arguments = NULL_TREE;
16830
16831           /* Consume the token.  */
16832           token = cp_lexer_consume_token (parser->lexer);
16833
16834           /* Save away the identifier that indicates which attribute
16835              this is.  */
16836           identifier = token->u.value;
16837           attribute = build_tree_list (identifier, NULL_TREE);
16838
16839           /* Peek at the next token.  */
16840           token = cp_lexer_peek_token (parser->lexer);
16841           /* If it's an `(', then parse the attribute arguments.  */
16842           if (token->type == CPP_OPEN_PAREN)
16843             {
16844               arguments = cp_parser_parenthesized_expression_list
16845                           (parser, true, /*cast_p=*/false,
16846                            /*allow_expansion_p=*/false,
16847                            /*non_constant_p=*/NULL);
16848               /* Save the arguments away.  */
16849               TREE_VALUE (attribute) = arguments;
16850             }
16851
16852           if (arguments != error_mark_node)
16853             {
16854               /* Add this attribute to the list.  */
16855               TREE_CHAIN (attribute) = attribute_list;
16856               attribute_list = attribute;
16857             }
16858
16859           token = cp_lexer_peek_token (parser->lexer);
16860         }
16861       /* Now, look for more attributes.  If the next token isn't a
16862          `,', we're done.  */
16863       if (token->type != CPP_COMMA)
16864         break;
16865
16866       /* Consume the comma and keep going.  */
16867       cp_lexer_consume_token (parser->lexer);
16868     }
16869   parser->translate_strings_p = save_translate_strings_p;
16870
16871   /* We built up the list in reverse order.  */
16872   return nreverse (attribute_list);
16873 }
16874
16875 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16876    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16877    current value of the PEDANTIC flag, regardless of whether or not
16878    the `__extension__' keyword is present.  The caller is responsible
16879    for restoring the value of the PEDANTIC flag.  */
16880
16881 static bool
16882 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16883 {
16884   /* Save the old value of the PEDANTIC flag.  */
16885   *saved_pedantic = pedantic;
16886
16887   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16888     {
16889       /* Consume the `__extension__' token.  */
16890       cp_lexer_consume_token (parser->lexer);
16891       /* We're not being pedantic while the `__extension__' keyword is
16892          in effect.  */
16893       pedantic = 0;
16894
16895       return true;
16896     }
16897
16898   return false;
16899 }
16900
16901 /* Parse a label declaration.
16902
16903    label-declaration:
16904      __label__ label-declarator-seq ;
16905
16906    label-declarator-seq:
16907      identifier , label-declarator-seq
16908      identifier  */
16909
16910 static void
16911 cp_parser_label_declaration (cp_parser* parser)
16912 {
16913   /* Look for the `__label__' keyword.  */
16914   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16915
16916   while (true)
16917     {
16918       tree identifier;
16919
16920       /* Look for an identifier.  */
16921       identifier = cp_parser_identifier (parser);
16922       /* If we failed, stop.  */
16923       if (identifier == error_mark_node)
16924         break;
16925       /* Declare it as a label.  */
16926       finish_label_decl (identifier);
16927       /* If the next token is a `;', stop.  */
16928       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16929         break;
16930       /* Look for the `,' separating the label declarations.  */
16931       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16932     }
16933
16934   /* Look for the final `;'.  */
16935   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16936 }
16937
16938 /* Support Functions */
16939
16940 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16941    NAME should have one of the representations used for an
16942    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16943    is returned.  If PARSER->SCOPE is a dependent type, then a
16944    SCOPE_REF is returned.
16945
16946    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16947    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16948    was formed.  Abstractly, such entities should not be passed to this
16949    function, because they do not need to be looked up, but it is
16950    simpler to check for this special case here, rather than at the
16951    call-sites.
16952
16953    In cases not explicitly covered above, this function returns a
16954    DECL, OVERLOAD, or baselink representing the result of the lookup.
16955    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16956    is returned.
16957
16958    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16959    (e.g., "struct") that was used.  In that case bindings that do not
16960    refer to types are ignored.
16961
16962    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16963    ignored.
16964
16965    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16966    are ignored.
16967
16968    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16969    types.
16970
16971    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16972    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16973    NULL_TREE otherwise.  */
16974
16975 static tree
16976 cp_parser_lookup_name (cp_parser *parser, tree name,
16977                        enum tag_types tag_type,
16978                        bool is_template,
16979                        bool is_namespace,
16980                        bool check_dependency,
16981                        tree *ambiguous_decls,
16982                        location_t name_location)
16983 {
16984   int flags = 0;
16985   tree decl;
16986   tree object_type = parser->context->object_type;
16987
16988   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16989     flags |= LOOKUP_COMPLAIN;
16990
16991   /* Assume that the lookup will be unambiguous.  */
16992   if (ambiguous_decls)
16993     *ambiguous_decls = NULL_TREE;
16994
16995   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16996      no longer valid.  Note that if we are parsing tentatively, and
16997      the parse fails, OBJECT_TYPE will be automatically restored.  */
16998   parser->context->object_type = NULL_TREE;
16999
17000   if (name == error_mark_node)
17001     return error_mark_node;
17002
17003   /* A template-id has already been resolved; there is no lookup to
17004      do.  */
17005   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17006     return name;
17007   if (BASELINK_P (name))
17008     {
17009       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17010                   == TEMPLATE_ID_EXPR);
17011       return name;
17012     }
17013
17014   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17015      it should already have been checked to make sure that the name
17016      used matches the type being destroyed.  */
17017   if (TREE_CODE (name) == BIT_NOT_EXPR)
17018     {
17019       tree type;
17020
17021       /* Figure out to which type this destructor applies.  */
17022       if (parser->scope)
17023         type = parser->scope;
17024       else if (object_type)
17025         type = object_type;
17026       else
17027         type = current_class_type;
17028       /* If that's not a class type, there is no destructor.  */
17029       if (!type || !CLASS_TYPE_P (type))
17030         return error_mark_node;
17031       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17032         lazily_declare_fn (sfk_destructor, type);
17033       if (!CLASSTYPE_DESTRUCTORS (type))
17034           return error_mark_node;
17035       /* If it was a class type, return the destructor.  */
17036       return CLASSTYPE_DESTRUCTORS (type);
17037     }
17038
17039   /* By this point, the NAME should be an ordinary identifier.  If
17040      the id-expression was a qualified name, the qualifying scope is
17041      stored in PARSER->SCOPE at this point.  */
17042   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17043
17044   /* Perform the lookup.  */
17045   if (parser->scope)
17046     {
17047       bool dependent_p;
17048
17049       if (parser->scope == error_mark_node)
17050         return error_mark_node;
17051
17052       /* If the SCOPE is dependent, the lookup must be deferred until
17053          the template is instantiated -- unless we are explicitly
17054          looking up names in uninstantiated templates.  Even then, we
17055          cannot look up the name if the scope is not a class type; it
17056          might, for example, be a template type parameter.  */
17057       dependent_p = (TYPE_P (parser->scope)
17058                      && dependent_scope_p (parser->scope));
17059       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17060           && dependent_p)
17061         /* Defer lookup.  */
17062         decl = error_mark_node;
17063       else
17064         {
17065           tree pushed_scope = NULL_TREE;
17066
17067           /* If PARSER->SCOPE is a dependent type, then it must be a
17068              class type, and we must not be checking dependencies;
17069              otherwise, we would have processed this lookup above.  So
17070              that PARSER->SCOPE is not considered a dependent base by
17071              lookup_member, we must enter the scope here.  */
17072           if (dependent_p)
17073             pushed_scope = push_scope (parser->scope);
17074           /* If the PARSER->SCOPE is a template specialization, it
17075              may be instantiated during name lookup.  In that case,
17076              errors may be issued.  Even if we rollback the current
17077              tentative parse, those errors are valid.  */
17078           decl = lookup_qualified_name (parser->scope, name,
17079                                         tag_type != none_type,
17080                                         /*complain=*/true);
17081
17082           /* If we have a single function from a using decl, pull it out.  */
17083           if (TREE_CODE (decl) == OVERLOAD
17084               && !really_overloaded_fn (decl))
17085             decl = OVL_FUNCTION (decl);
17086
17087           if (pushed_scope)
17088             pop_scope (pushed_scope);
17089         }
17090
17091       /* If the scope is a dependent type and either we deferred lookup or
17092          we did lookup but didn't find the name, rememeber the name.  */
17093       if (decl == error_mark_node && TYPE_P (parser->scope)
17094           && dependent_type_p (parser->scope))
17095         {
17096           if (tag_type)
17097             {
17098               tree type;
17099
17100               /* The resolution to Core Issue 180 says that `struct
17101                  A::B' should be considered a type-name, even if `A'
17102                  is dependent.  */
17103               type = make_typename_type (parser->scope, name, tag_type,
17104                                          /*complain=*/tf_error);
17105               decl = TYPE_NAME (type);
17106             }
17107           else if (is_template
17108                    && (cp_parser_next_token_ends_template_argument_p (parser)
17109                        || cp_lexer_next_token_is (parser->lexer,
17110                                                   CPP_CLOSE_PAREN)))
17111             decl = make_unbound_class_template (parser->scope,
17112                                                 name, NULL_TREE,
17113                                                 /*complain=*/tf_error);
17114           else
17115             decl = build_qualified_name (/*type=*/NULL_TREE,
17116                                          parser->scope, name,
17117                                          is_template);
17118         }
17119       parser->qualifying_scope = parser->scope;
17120       parser->object_scope = NULL_TREE;
17121     }
17122   else if (object_type)
17123     {
17124       tree object_decl = NULL_TREE;
17125       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17126          OBJECT_TYPE is not a class.  */
17127       if (CLASS_TYPE_P (object_type))
17128         /* If the OBJECT_TYPE is a template specialization, it may
17129            be instantiated during name lookup.  In that case, errors
17130            may be issued.  Even if we rollback the current tentative
17131            parse, those errors are valid.  */
17132         object_decl = lookup_member (object_type,
17133                                      name,
17134                                      /*protect=*/0,
17135                                      tag_type != none_type);
17136       /* Look it up in the enclosing context, too.  */
17137       decl = lookup_name_real (name, tag_type != none_type,
17138                                /*nonclass=*/0,
17139                                /*block_p=*/true, is_namespace, flags);
17140       parser->object_scope = object_type;
17141       parser->qualifying_scope = NULL_TREE;
17142       if (object_decl)
17143         decl = object_decl;
17144     }
17145   else
17146     {
17147       decl = lookup_name_real (name, tag_type != none_type,
17148                                /*nonclass=*/0,
17149                                /*block_p=*/true, is_namespace, flags);
17150       parser->qualifying_scope = NULL_TREE;
17151       parser->object_scope = NULL_TREE;
17152     }
17153
17154   /* If the lookup failed, let our caller know.  */
17155   if (!decl || decl == error_mark_node)
17156     return error_mark_node;
17157
17158   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17159   if (TREE_CODE (decl) == TREE_LIST)
17160     {
17161       if (ambiguous_decls)
17162         *ambiguous_decls = decl;
17163       /* The error message we have to print is too complicated for
17164          cp_parser_error, so we incorporate its actions directly.  */
17165       if (!cp_parser_simulate_error (parser))
17166         {
17167           error ("%Hreference to %qD is ambiguous",
17168                  &name_location, name);
17169           print_candidates (decl);
17170         }
17171       return error_mark_node;
17172     }
17173
17174   gcc_assert (DECL_P (decl)
17175               || TREE_CODE (decl) == OVERLOAD
17176               || TREE_CODE (decl) == SCOPE_REF
17177               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17178               || BASELINK_P (decl));
17179
17180   /* If we have resolved the name of a member declaration, check to
17181      see if the declaration is accessible.  When the name resolves to
17182      set of overloaded functions, accessibility is checked when
17183      overload resolution is done.
17184
17185      During an explicit instantiation, access is not checked at all,
17186      as per [temp.explicit].  */
17187   if (DECL_P (decl))
17188     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17189
17190   return decl;
17191 }
17192
17193 /* Like cp_parser_lookup_name, but for use in the typical case where
17194    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17195    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17196
17197 static tree
17198 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17199 {
17200   return cp_parser_lookup_name (parser, name,
17201                                 none_type,
17202                                 /*is_template=*/false,
17203                                 /*is_namespace=*/false,
17204                                 /*check_dependency=*/true,
17205                                 /*ambiguous_decls=*/NULL,
17206                                 location);
17207 }
17208
17209 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17210    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17211    true, the DECL indicates the class being defined in a class-head,
17212    or declared in an elaborated-type-specifier.
17213
17214    Otherwise, return DECL.  */
17215
17216 static tree
17217 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17218 {
17219   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17220      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17221
17222        struct A {
17223          template <typename T> struct B;
17224        };
17225
17226        template <typename T> struct A::B {};
17227
17228      Similarly, in an elaborated-type-specifier:
17229
17230        namespace N { struct X{}; }
17231
17232        struct A {
17233          template <typename T> friend struct N::X;
17234        };
17235
17236      However, if the DECL refers to a class type, and we are in
17237      the scope of the class, then the name lookup automatically
17238      finds the TYPE_DECL created by build_self_reference rather
17239      than a TEMPLATE_DECL.  For example, in:
17240
17241        template <class T> struct S {
17242          S s;
17243        };
17244
17245      there is no need to handle such case.  */
17246
17247   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17248     return DECL_TEMPLATE_RESULT (decl);
17249
17250   return decl;
17251 }
17252
17253 /* If too many, or too few, template-parameter lists apply to the
17254    declarator, issue an error message.  Returns TRUE if all went well,
17255    and FALSE otherwise.  */
17256
17257 static bool
17258 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17259                                                 cp_declarator *declarator,
17260                                                 location_t declarator_location)
17261 {
17262   unsigned num_templates;
17263
17264   /* We haven't seen any classes that involve template parameters yet.  */
17265   num_templates = 0;
17266
17267   switch (declarator->kind)
17268     {
17269     case cdk_id:
17270       if (declarator->u.id.qualifying_scope)
17271         {
17272           tree scope;
17273           tree member;
17274
17275           scope = declarator->u.id.qualifying_scope;
17276           member = declarator->u.id.unqualified_name;
17277
17278           while (scope && CLASS_TYPE_P (scope))
17279             {
17280               /* You're supposed to have one `template <...>'
17281                  for every template class, but you don't need one
17282                  for a full specialization.  For example:
17283
17284                  template <class T> struct S{};
17285                  template <> struct S<int> { void f(); };
17286                  void S<int>::f () {}
17287
17288                  is correct; there shouldn't be a `template <>' for
17289                  the definition of `S<int>::f'.  */
17290               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17291                 /* If SCOPE does not have template information of any
17292                    kind, then it is not a template, nor is it nested
17293                    within a template.  */
17294                 break;
17295               if (explicit_class_specialization_p (scope))
17296                 break;
17297               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17298                 ++num_templates;
17299
17300               scope = TYPE_CONTEXT (scope);
17301             }
17302         }
17303       else if (TREE_CODE (declarator->u.id.unqualified_name)
17304                == TEMPLATE_ID_EXPR)
17305         /* If the DECLARATOR has the form `X<y>' then it uses one
17306            additional level of template parameters.  */
17307         ++num_templates;
17308
17309       return cp_parser_check_template_parameters (parser,
17310                                                   num_templates,
17311                                                   declarator_location);
17312
17313     case cdk_function:
17314     case cdk_array:
17315     case cdk_pointer:
17316     case cdk_reference:
17317     case cdk_ptrmem:
17318       return (cp_parser_check_declarator_template_parameters
17319               (parser, declarator->declarator, declarator_location));
17320
17321     case cdk_error:
17322       return true;
17323
17324     default:
17325       gcc_unreachable ();
17326     }
17327   return false;
17328 }
17329
17330 /* NUM_TEMPLATES were used in the current declaration.  If that is
17331    invalid, return FALSE and issue an error messages.  Otherwise,
17332    return TRUE.  */
17333
17334 static bool
17335 cp_parser_check_template_parameters (cp_parser* parser,
17336                                      unsigned num_templates,
17337                                      location_t location)
17338 {
17339   /* If there are more template classes than parameter lists, we have
17340      something like:
17341
17342        template <class T> void S<T>::R<T>::f ();  */
17343   if (parser->num_template_parameter_lists < num_templates)
17344     {
17345       error ("%Htoo few template-parameter-lists", &location);
17346       return false;
17347     }
17348   /* If there are the same number of template classes and parameter
17349      lists, that's OK.  */
17350   if (parser->num_template_parameter_lists == num_templates)
17351     return true;
17352   /* If there are more, but only one more, then we are referring to a
17353      member template.  That's OK too.  */
17354   if (parser->num_template_parameter_lists == num_templates + 1)
17355       return true;
17356   /* Otherwise, there are too many template parameter lists.  We have
17357      something like:
17358
17359      template <class T> template <class U> void S::f();  */
17360   error ("%Htoo many template-parameter-lists", &location);
17361   return false;
17362 }
17363
17364 /* Parse an optional `::' token indicating that the following name is
17365    from the global namespace.  If so, PARSER->SCOPE is set to the
17366    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17367    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17368    Returns the new value of PARSER->SCOPE, if the `::' token is
17369    present, and NULL_TREE otherwise.  */
17370
17371 static tree
17372 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17373 {
17374   cp_token *token;
17375
17376   /* Peek at the next token.  */
17377   token = cp_lexer_peek_token (parser->lexer);
17378   /* If we're looking at a `::' token then we're starting from the
17379      global namespace, not our current location.  */
17380   if (token->type == CPP_SCOPE)
17381     {
17382       /* Consume the `::' token.  */
17383       cp_lexer_consume_token (parser->lexer);
17384       /* Set the SCOPE so that we know where to start the lookup.  */
17385       parser->scope = global_namespace;
17386       parser->qualifying_scope = global_namespace;
17387       parser->object_scope = NULL_TREE;
17388
17389       return parser->scope;
17390     }
17391   else if (!current_scope_valid_p)
17392     {
17393       parser->scope = NULL_TREE;
17394       parser->qualifying_scope = NULL_TREE;
17395       parser->object_scope = NULL_TREE;
17396     }
17397
17398   return NULL_TREE;
17399 }
17400
17401 /* Returns TRUE if the upcoming token sequence is the start of a
17402    constructor declarator.  If FRIEND_P is true, the declarator is
17403    preceded by the `friend' specifier.  */
17404
17405 static bool
17406 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17407 {
17408   bool constructor_p;
17409   tree type_decl = NULL_TREE;
17410   bool nested_name_p;
17411   cp_token *next_token;
17412
17413   /* The common case is that this is not a constructor declarator, so
17414      try to avoid doing lots of work if at all possible.  It's not
17415      valid declare a constructor at function scope.  */
17416   if (parser->in_function_body)
17417     return false;
17418   /* And only certain tokens can begin a constructor declarator.  */
17419   next_token = cp_lexer_peek_token (parser->lexer);
17420   if (next_token->type != CPP_NAME
17421       && next_token->type != CPP_SCOPE
17422       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17423       && next_token->type != CPP_TEMPLATE_ID)
17424     return false;
17425
17426   /* Parse tentatively; we are going to roll back all of the tokens
17427      consumed here.  */
17428   cp_parser_parse_tentatively (parser);
17429   /* Assume that we are looking at a constructor declarator.  */
17430   constructor_p = true;
17431
17432   /* Look for the optional `::' operator.  */
17433   cp_parser_global_scope_opt (parser,
17434                               /*current_scope_valid_p=*/false);
17435   /* Look for the nested-name-specifier.  */
17436   nested_name_p
17437     = (cp_parser_nested_name_specifier_opt (parser,
17438                                             /*typename_keyword_p=*/false,
17439                                             /*check_dependency_p=*/false,
17440                                             /*type_p=*/false,
17441                                             /*is_declaration=*/false)
17442        != NULL_TREE);
17443   /* Outside of a class-specifier, there must be a
17444      nested-name-specifier.  */
17445   if (!nested_name_p &&
17446       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17447        || friend_p))
17448     constructor_p = false;
17449   /* If we still think that this might be a constructor-declarator,
17450      look for a class-name.  */
17451   if (constructor_p)
17452     {
17453       /* If we have:
17454
17455            template <typename T> struct S { S(); };
17456            template <typename T> S<T>::S ();
17457
17458          we must recognize that the nested `S' names a class.
17459          Similarly, for:
17460
17461            template <typename T> S<T>::S<T> ();
17462
17463          we must recognize that the nested `S' names a template.  */
17464       type_decl = cp_parser_class_name (parser,
17465                                         /*typename_keyword_p=*/false,
17466                                         /*template_keyword_p=*/false,
17467                                         none_type,
17468                                         /*check_dependency_p=*/false,
17469                                         /*class_head_p=*/false,
17470                                         /*is_declaration=*/false);
17471       /* If there was no class-name, then this is not a constructor.  */
17472       constructor_p = !cp_parser_error_occurred (parser);
17473     }
17474
17475   /* If we're still considering a constructor, we have to see a `(',
17476      to begin the parameter-declaration-clause, followed by either a
17477      `)', an `...', or a decl-specifier.  We need to check for a
17478      type-specifier to avoid being fooled into thinking that:
17479
17480        S::S (f) (int);
17481
17482      is a constructor.  (It is actually a function named `f' that
17483      takes one parameter (of type `int') and returns a value of type
17484      `S::S'.  */
17485   if (constructor_p
17486       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17487     {
17488       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17489           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17490           /* A parameter declaration begins with a decl-specifier,
17491              which is either the "attribute" keyword, a storage class
17492              specifier, or (usually) a type-specifier.  */
17493           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17494         {
17495           tree type;
17496           tree pushed_scope = NULL_TREE;
17497           unsigned saved_num_template_parameter_lists;
17498
17499           /* Names appearing in the type-specifier should be looked up
17500              in the scope of the class.  */
17501           if (current_class_type)
17502             type = NULL_TREE;
17503           else
17504             {
17505               type = TREE_TYPE (type_decl);
17506               if (TREE_CODE (type) == TYPENAME_TYPE)
17507                 {
17508                   type = resolve_typename_type (type,
17509                                                 /*only_current_p=*/false);
17510                   if (TREE_CODE (type) == TYPENAME_TYPE)
17511                     {
17512                       cp_parser_abort_tentative_parse (parser);
17513                       return false;
17514                     }
17515                 }
17516               pushed_scope = push_scope (type);
17517             }
17518
17519           /* Inside the constructor parameter list, surrounding
17520              template-parameter-lists do not apply.  */
17521           saved_num_template_parameter_lists
17522             = parser->num_template_parameter_lists;
17523           parser->num_template_parameter_lists = 0;
17524
17525           /* Look for the type-specifier.  */
17526           cp_parser_type_specifier (parser,
17527                                     CP_PARSER_FLAGS_NONE,
17528                                     /*decl_specs=*/NULL,
17529                                     /*is_declarator=*/true,
17530                                     /*declares_class_or_enum=*/NULL,
17531                                     /*is_cv_qualifier=*/NULL);
17532
17533           parser->num_template_parameter_lists
17534             = saved_num_template_parameter_lists;
17535
17536           /* Leave the scope of the class.  */
17537           if (pushed_scope)
17538             pop_scope (pushed_scope);
17539
17540           constructor_p = !cp_parser_error_occurred (parser);
17541         }
17542     }
17543   else
17544     constructor_p = false;
17545   /* We did not really want to consume any tokens.  */
17546   cp_parser_abort_tentative_parse (parser);
17547
17548   return constructor_p;
17549 }
17550
17551 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17552    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17553    they must be performed once we are in the scope of the function.
17554
17555    Returns the function defined.  */
17556
17557 static tree
17558 cp_parser_function_definition_from_specifiers_and_declarator
17559   (cp_parser* parser,
17560    cp_decl_specifier_seq *decl_specifiers,
17561    tree attributes,
17562    const cp_declarator *declarator)
17563 {
17564   tree fn;
17565   bool success_p;
17566
17567   /* Begin the function-definition.  */
17568   success_p = start_function (decl_specifiers, declarator, attributes);
17569
17570   /* The things we're about to see are not directly qualified by any
17571      template headers we've seen thus far.  */
17572   reset_specialization ();
17573
17574   /* If there were names looked up in the decl-specifier-seq that we
17575      did not check, check them now.  We must wait until we are in the
17576      scope of the function to perform the checks, since the function
17577      might be a friend.  */
17578   perform_deferred_access_checks ();
17579
17580   if (!success_p)
17581     {
17582       /* Skip the entire function.  */
17583       cp_parser_skip_to_end_of_block_or_statement (parser);
17584       fn = error_mark_node;
17585     }
17586   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17587     {
17588       /* Seen already, skip it.  An error message has already been output.  */
17589       cp_parser_skip_to_end_of_block_or_statement (parser);
17590       fn = current_function_decl;
17591       current_function_decl = NULL_TREE;
17592       /* If this is a function from a class, pop the nested class.  */
17593       if (current_class_name)
17594         pop_nested_class ();
17595     }
17596   else
17597     fn = cp_parser_function_definition_after_declarator (parser,
17598                                                          /*inline_p=*/false);
17599
17600   return fn;
17601 }
17602
17603 /* Parse the part of a function-definition that follows the
17604    declarator.  INLINE_P is TRUE iff this function is an inline
17605    function defined with a class-specifier.
17606
17607    Returns the function defined.  */
17608
17609 static tree
17610 cp_parser_function_definition_after_declarator (cp_parser* parser,
17611                                                 bool inline_p)
17612 {
17613   tree fn;
17614   bool ctor_initializer_p = false;
17615   bool saved_in_unbraced_linkage_specification_p;
17616   bool saved_in_function_body;
17617   unsigned saved_num_template_parameter_lists;
17618   cp_token *token;
17619
17620   saved_in_function_body = parser->in_function_body;
17621   parser->in_function_body = true;
17622   /* If the next token is `return', then the code may be trying to
17623      make use of the "named return value" extension that G++ used to
17624      support.  */
17625   token = cp_lexer_peek_token (parser->lexer);
17626   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17627     {
17628       /* Consume the `return' keyword.  */
17629       cp_lexer_consume_token (parser->lexer);
17630       /* Look for the identifier that indicates what value is to be
17631          returned.  */
17632       cp_parser_identifier (parser);
17633       /* Issue an error message.  */
17634       error ("%Hnamed return values are no longer supported",
17635              &token->location);
17636       /* Skip tokens until we reach the start of the function body.  */
17637       while (true)
17638         {
17639           cp_token *token = cp_lexer_peek_token (parser->lexer);
17640           if (token->type == CPP_OPEN_BRACE
17641               || token->type == CPP_EOF
17642               || token->type == CPP_PRAGMA_EOL)
17643             break;
17644           cp_lexer_consume_token (parser->lexer);
17645         }
17646     }
17647   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17648      anything declared inside `f'.  */
17649   saved_in_unbraced_linkage_specification_p
17650     = parser->in_unbraced_linkage_specification_p;
17651   parser->in_unbraced_linkage_specification_p = false;
17652   /* Inside the function, surrounding template-parameter-lists do not
17653      apply.  */
17654   saved_num_template_parameter_lists
17655     = parser->num_template_parameter_lists;
17656   parser->num_template_parameter_lists = 0;
17657   /* If the next token is `try', then we are looking at a
17658      function-try-block.  */
17659   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17660     ctor_initializer_p = cp_parser_function_try_block (parser);
17661   /* A function-try-block includes the function-body, so we only do
17662      this next part if we're not processing a function-try-block.  */
17663   else
17664     ctor_initializer_p
17665       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17666
17667   /* Finish the function.  */
17668   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17669                         (inline_p ? 2 : 0));
17670   /* Generate code for it, if necessary.  */
17671   expand_or_defer_fn (fn);
17672   /* Restore the saved values.  */
17673   parser->in_unbraced_linkage_specification_p
17674     = saved_in_unbraced_linkage_specification_p;
17675   parser->num_template_parameter_lists
17676     = saved_num_template_parameter_lists;
17677   parser->in_function_body = saved_in_function_body;
17678
17679   return fn;
17680 }
17681
17682 /* Parse a template-declaration, assuming that the `export' (and
17683    `extern') keywords, if present, has already been scanned.  MEMBER_P
17684    is as for cp_parser_template_declaration.  */
17685
17686 static void
17687 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17688 {
17689   tree decl = NULL_TREE;
17690   VEC (deferred_access_check,gc) *checks;
17691   tree parameter_list;
17692   bool friend_p = false;
17693   bool need_lang_pop;
17694   cp_token *token;
17695
17696   /* Look for the `template' keyword.  */
17697   token = cp_lexer_peek_token (parser->lexer);
17698   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17699     return;
17700
17701   /* And the `<'.  */
17702   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17703     return;
17704   if (at_class_scope_p () && current_function_decl)
17705     {
17706       /* 14.5.2.2 [temp.mem]
17707
17708          A local class shall not have member templates.  */
17709       error ("%Hinvalid declaration of member template in local class",
17710              &token->location);
17711       cp_parser_skip_to_end_of_block_or_statement (parser);
17712       return;
17713     }
17714   /* [temp]
17715
17716      A template ... shall not have C linkage.  */
17717   if (current_lang_name == lang_name_c)
17718     {
17719       error ("%Htemplate with C linkage", &token->location);
17720       /* Give it C++ linkage to avoid confusing other parts of the
17721          front end.  */
17722       push_lang_context (lang_name_cplusplus);
17723       need_lang_pop = true;
17724     }
17725   else
17726     need_lang_pop = false;
17727
17728   /* We cannot perform access checks on the template parameter
17729      declarations until we know what is being declared, just as we
17730      cannot check the decl-specifier list.  */
17731   push_deferring_access_checks (dk_deferred);
17732
17733   /* If the next token is `>', then we have an invalid
17734      specialization.  Rather than complain about an invalid template
17735      parameter, issue an error message here.  */
17736   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17737     {
17738       cp_parser_error (parser, "invalid explicit specialization");
17739       begin_specialization ();
17740       parameter_list = NULL_TREE;
17741     }
17742   else
17743     /* Parse the template parameters.  */
17744     parameter_list = cp_parser_template_parameter_list (parser);
17745
17746   /* Get the deferred access checks from the parameter list.  These
17747      will be checked once we know what is being declared, as for a
17748      member template the checks must be performed in the scope of the
17749      class containing the member.  */
17750   checks = get_deferred_access_checks ();
17751
17752   /* Look for the `>'.  */
17753   cp_parser_skip_to_end_of_template_parameter_list (parser);
17754   /* We just processed one more parameter list.  */
17755   ++parser->num_template_parameter_lists;
17756   /* If the next token is `template', there are more template
17757      parameters.  */
17758   if (cp_lexer_next_token_is_keyword (parser->lexer,
17759                                       RID_TEMPLATE))
17760     cp_parser_template_declaration_after_export (parser, member_p);
17761   else
17762     {
17763       /* There are no access checks when parsing a template, as we do not
17764          know if a specialization will be a friend.  */
17765       push_deferring_access_checks (dk_no_check);
17766       token = cp_lexer_peek_token (parser->lexer);
17767       decl = cp_parser_single_declaration (parser,
17768                                            checks,
17769                                            member_p,
17770                                            /*explicit_specialization_p=*/false,
17771                                            &friend_p);
17772       pop_deferring_access_checks ();
17773
17774       /* If this is a member template declaration, let the front
17775          end know.  */
17776       if (member_p && !friend_p && decl)
17777         {
17778           if (TREE_CODE (decl) == TYPE_DECL)
17779             cp_parser_check_access_in_redeclaration (decl, token->location);
17780
17781           decl = finish_member_template_decl (decl);
17782         }
17783       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17784         make_friend_class (current_class_type, TREE_TYPE (decl),
17785                            /*complain=*/true);
17786     }
17787   /* We are done with the current parameter list.  */
17788   --parser->num_template_parameter_lists;
17789
17790   pop_deferring_access_checks ();
17791
17792   /* Finish up.  */
17793   finish_template_decl (parameter_list);
17794
17795   /* Register member declarations.  */
17796   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17797     finish_member_declaration (decl);
17798   /* For the erroneous case of a template with C linkage, we pushed an
17799      implicit C++ linkage scope; exit that scope now.  */
17800   if (need_lang_pop)
17801     pop_lang_context ();
17802   /* If DECL is a function template, we must return to parse it later.
17803      (Even though there is no definition, there might be default
17804      arguments that need handling.)  */
17805   if (member_p && decl
17806       && (TREE_CODE (decl) == FUNCTION_DECL
17807           || DECL_FUNCTION_TEMPLATE_P (decl)))
17808     TREE_VALUE (parser->unparsed_functions_queues)
17809       = tree_cons (NULL_TREE, decl,
17810                    TREE_VALUE (parser->unparsed_functions_queues));
17811 }
17812
17813 /* Perform the deferred access checks from a template-parameter-list.
17814    CHECKS is a TREE_LIST of access checks, as returned by
17815    get_deferred_access_checks.  */
17816
17817 static void
17818 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17819 {
17820   ++processing_template_parmlist;
17821   perform_access_checks (checks);
17822   --processing_template_parmlist;
17823 }
17824
17825 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17826    `function-definition' sequence.  MEMBER_P is true, this declaration
17827    appears in a class scope.
17828
17829    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17830    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17831
17832 static tree
17833 cp_parser_single_declaration (cp_parser* parser,
17834                               VEC (deferred_access_check,gc)* checks,
17835                               bool member_p,
17836                               bool explicit_specialization_p,
17837                               bool* friend_p)
17838 {
17839   int declares_class_or_enum;
17840   tree decl = NULL_TREE;
17841   cp_decl_specifier_seq decl_specifiers;
17842   bool function_definition_p = false;
17843   cp_token *decl_spec_token_start;
17844
17845   /* This function is only used when processing a template
17846      declaration.  */
17847   gcc_assert (innermost_scope_kind () == sk_template_parms
17848               || innermost_scope_kind () == sk_template_spec);
17849
17850   /* Defer access checks until we know what is being declared.  */
17851   push_deferring_access_checks (dk_deferred);
17852
17853   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17854      alternative.  */
17855   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17856   cp_parser_decl_specifier_seq (parser,
17857                                 CP_PARSER_FLAGS_OPTIONAL,
17858                                 &decl_specifiers,
17859                                 &declares_class_or_enum);
17860   if (friend_p)
17861     *friend_p = cp_parser_friend_p (&decl_specifiers);
17862
17863   /* There are no template typedefs.  */
17864   if (decl_specifiers.specs[(int) ds_typedef])
17865     {
17866       error ("%Htemplate declaration of %qs",
17867              &decl_spec_token_start->location, "typedef");
17868       decl = error_mark_node;
17869     }
17870
17871   /* Gather up the access checks that occurred the
17872      decl-specifier-seq.  */
17873   stop_deferring_access_checks ();
17874
17875   /* Check for the declaration of a template class.  */
17876   if (declares_class_or_enum)
17877     {
17878       if (cp_parser_declares_only_class_p (parser))
17879         {
17880           decl = shadow_tag (&decl_specifiers);
17881
17882           /* In this case:
17883
17884                struct C {
17885                  friend template <typename T> struct A<T>::B;
17886                };
17887
17888              A<T>::B will be represented by a TYPENAME_TYPE, and
17889              therefore not recognized by shadow_tag.  */
17890           if (friend_p && *friend_p
17891               && !decl
17892               && decl_specifiers.type
17893               && TYPE_P (decl_specifiers.type))
17894             decl = decl_specifiers.type;
17895
17896           if (decl && decl != error_mark_node)
17897             decl = TYPE_NAME (decl);
17898           else
17899             decl = error_mark_node;
17900
17901           /* Perform access checks for template parameters.  */
17902           cp_parser_perform_template_parameter_access_checks (checks);
17903         }
17904     }
17905   /* If it's not a template class, try for a template function.  If
17906      the next token is a `;', then this declaration does not declare
17907      anything.  But, if there were errors in the decl-specifiers, then
17908      the error might well have come from an attempted class-specifier.
17909      In that case, there's no need to warn about a missing declarator.  */
17910   if (!decl
17911       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17912           || decl_specifiers.type != error_mark_node))
17913     {
17914       decl = cp_parser_init_declarator (parser,
17915                                         &decl_specifiers,
17916                                         checks,
17917                                         /*function_definition_allowed_p=*/true,
17918                                         member_p,
17919                                         declares_class_or_enum,
17920                                         &function_definition_p);
17921
17922     /* 7.1.1-1 [dcl.stc]
17923
17924        A storage-class-specifier shall not be specified in an explicit
17925        specialization...  */
17926     if (decl
17927         && explicit_specialization_p
17928         && decl_specifiers.storage_class != sc_none)
17929       {
17930         error ("%Hexplicit template specialization cannot have a storage class",
17931                &decl_spec_token_start->location);
17932         decl = error_mark_node;
17933       }
17934     }
17935
17936   pop_deferring_access_checks ();
17937
17938   /* Clear any current qualification; whatever comes next is the start
17939      of something new.  */
17940   parser->scope = NULL_TREE;
17941   parser->qualifying_scope = NULL_TREE;
17942   parser->object_scope = NULL_TREE;
17943   /* Look for a trailing `;' after the declaration.  */
17944   if (!function_definition_p
17945       && (decl == error_mark_node
17946           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17947     cp_parser_skip_to_end_of_block_or_statement (parser);
17948
17949   return decl;
17950 }
17951
17952 /* Parse a cast-expression that is not the operand of a unary "&".  */
17953
17954 static tree
17955 cp_parser_simple_cast_expression (cp_parser *parser)
17956 {
17957   return cp_parser_cast_expression (parser, /*address_p=*/false,
17958                                     /*cast_p=*/false, NULL);
17959 }
17960
17961 /* Parse a functional cast to TYPE.  Returns an expression
17962    representing the cast.  */
17963
17964 static tree
17965 cp_parser_functional_cast (cp_parser* parser, tree type)
17966 {
17967   tree expression_list;
17968   tree cast;
17969   bool nonconst_p;
17970
17971   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17972     {
17973       maybe_warn_cpp0x ("extended initializer lists");
17974       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17975       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17976       if (TREE_CODE (type) == TYPE_DECL)
17977         type = TREE_TYPE (type);
17978       return finish_compound_literal (type, expression_list);
17979     }
17980
17981   expression_list
17982     = cp_parser_parenthesized_expression_list (parser, false,
17983                                                /*cast_p=*/true,
17984                                                /*allow_expansion_p=*/true,
17985                                                /*non_constant_p=*/NULL);
17986
17987   cast = build_functional_cast (type, expression_list,
17988                                 tf_warning_or_error);
17989   /* [expr.const]/1: In an integral constant expression "only type
17990      conversions to integral or enumeration type can be used".  */
17991   if (TREE_CODE (type) == TYPE_DECL)
17992     type = TREE_TYPE (type);
17993   if (cast != error_mark_node
17994       && !cast_valid_in_integral_constant_expression_p (type)
17995       && (cp_parser_non_integral_constant_expression
17996           (parser, "a call to a constructor")))
17997     return error_mark_node;
17998   return cast;
17999 }
18000
18001 /* Save the tokens that make up the body of a member function defined
18002    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18003    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18004    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18005    for the member function.  */
18006
18007 static tree
18008 cp_parser_save_member_function_body (cp_parser* parser,
18009                                      cp_decl_specifier_seq *decl_specifiers,
18010                                      cp_declarator *declarator,
18011                                      tree attributes)
18012 {
18013   cp_token *first;
18014   cp_token *last;
18015   tree fn;
18016
18017   /* Create the function-declaration.  */
18018   fn = start_method (decl_specifiers, declarator, attributes);
18019   /* If something went badly wrong, bail out now.  */
18020   if (fn == error_mark_node)
18021     {
18022       /* If there's a function-body, skip it.  */
18023       if (cp_parser_token_starts_function_definition_p
18024           (cp_lexer_peek_token (parser->lexer)))
18025         cp_parser_skip_to_end_of_block_or_statement (parser);
18026       return error_mark_node;
18027     }
18028
18029   /* Remember it, if there default args to post process.  */
18030   cp_parser_save_default_args (parser, fn);
18031
18032   /* Save away the tokens that make up the body of the
18033      function.  */
18034   first = parser->lexer->next_token;
18035   /* We can have braced-init-list mem-initializers before the fn body.  */
18036   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18037     {
18038       cp_lexer_consume_token (parser->lexer);
18039       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18040              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18041         {
18042           /* cache_group will stop after an un-nested { } pair, too.  */
18043           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18044             break;
18045
18046           /* variadic mem-inits have ... after the ')'.  */
18047           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18048             cp_lexer_consume_token (parser->lexer);
18049         }
18050     }
18051   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18052   /* Handle function try blocks.  */
18053   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18054     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18055   last = parser->lexer->next_token;
18056
18057   /* Save away the inline definition; we will process it when the
18058      class is complete.  */
18059   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18060   DECL_PENDING_INLINE_P (fn) = 1;
18061
18062   /* We need to know that this was defined in the class, so that
18063      friend templates are handled correctly.  */
18064   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18065
18066   /* We're done with the inline definition.  */
18067   finish_method (fn);
18068
18069   /* Add FN to the queue of functions to be parsed later.  */
18070   TREE_VALUE (parser->unparsed_functions_queues)
18071     = tree_cons (NULL_TREE, fn,
18072                  TREE_VALUE (parser->unparsed_functions_queues));
18073
18074   return fn;
18075 }
18076
18077 /* Parse a template-argument-list, as well as the trailing ">" (but
18078    not the opening ">").  See cp_parser_template_argument_list for the
18079    return value.  */
18080
18081 static tree
18082 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18083 {
18084   tree arguments;
18085   tree saved_scope;
18086   tree saved_qualifying_scope;
18087   tree saved_object_scope;
18088   bool saved_greater_than_is_operator_p;
18089   bool saved_skip_evaluation;
18090
18091   /* [temp.names]
18092
18093      When parsing a template-id, the first non-nested `>' is taken as
18094      the end of the template-argument-list rather than a greater-than
18095      operator.  */
18096   saved_greater_than_is_operator_p
18097     = parser->greater_than_is_operator_p;
18098   parser->greater_than_is_operator_p = false;
18099   /* Parsing the argument list may modify SCOPE, so we save it
18100      here.  */
18101   saved_scope = parser->scope;
18102   saved_qualifying_scope = parser->qualifying_scope;
18103   saved_object_scope = parser->object_scope;
18104   /* We need to evaluate the template arguments, even though this
18105      template-id may be nested within a "sizeof".  */
18106   saved_skip_evaluation = skip_evaluation;
18107   skip_evaluation = false;
18108   /* Parse the template-argument-list itself.  */
18109   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18110       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18111     arguments = NULL_TREE;
18112   else
18113     arguments = cp_parser_template_argument_list (parser);
18114   /* Look for the `>' that ends the template-argument-list. If we find
18115      a '>>' instead, it's probably just a typo.  */
18116   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18117     {
18118       if (cxx_dialect != cxx98)
18119         {
18120           /* In C++0x, a `>>' in a template argument list or cast
18121              expression is considered to be two separate `>'
18122              tokens. So, change the current token to a `>', but don't
18123              consume it: it will be consumed later when the outer
18124              template argument list (or cast expression) is parsed.
18125              Note that this replacement of `>' for `>>' is necessary
18126              even if we are parsing tentatively: in the tentative
18127              case, after calling
18128              cp_parser_enclosed_template_argument_list we will always
18129              throw away all of the template arguments and the first
18130              closing `>', either because the template argument list
18131              was erroneous or because we are replacing those tokens
18132              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18133              not have been thrown away) is needed either to close an
18134              outer template argument list or to complete a new-style
18135              cast.  */
18136           cp_token *token = cp_lexer_peek_token (parser->lexer);
18137           token->type = CPP_GREATER;
18138         }
18139       else if (!saved_greater_than_is_operator_p)
18140         {
18141           /* If we're in a nested template argument list, the '>>' has
18142             to be a typo for '> >'. We emit the error message, but we
18143             continue parsing and we push a '>' as next token, so that
18144             the argument list will be parsed correctly.  Note that the
18145             global source location is still on the token before the
18146             '>>', so we need to say explicitly where we want it.  */
18147           cp_token *token = cp_lexer_peek_token (parser->lexer);
18148           error ("%H%<>>%> should be %<> >%> "
18149                  "within a nested template argument list",
18150                  &token->location);
18151
18152           token->type = CPP_GREATER;
18153         }
18154       else
18155         {
18156           /* If this is not a nested template argument list, the '>>'
18157             is a typo for '>'. Emit an error message and continue.
18158             Same deal about the token location, but here we can get it
18159             right by consuming the '>>' before issuing the diagnostic.  */
18160           cp_token *token = cp_lexer_consume_token (parser->lexer);
18161           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18162                  "a template argument list", &token->location);
18163         }
18164     }
18165   else
18166     cp_parser_skip_to_end_of_template_parameter_list (parser);
18167   /* The `>' token might be a greater-than operator again now.  */
18168   parser->greater_than_is_operator_p
18169     = saved_greater_than_is_operator_p;
18170   /* Restore the SAVED_SCOPE.  */
18171   parser->scope = saved_scope;
18172   parser->qualifying_scope = saved_qualifying_scope;
18173   parser->object_scope = saved_object_scope;
18174   skip_evaluation = saved_skip_evaluation;
18175
18176   return arguments;
18177 }
18178
18179 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18180    arguments, or the body of the function have not yet been parsed,
18181    parse them now.  */
18182
18183 static void
18184 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18185 {
18186   /* If this member is a template, get the underlying
18187      FUNCTION_DECL.  */
18188   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18189     member_function = DECL_TEMPLATE_RESULT (member_function);
18190
18191   /* There should not be any class definitions in progress at this
18192      point; the bodies of members are only parsed outside of all class
18193      definitions.  */
18194   gcc_assert (parser->num_classes_being_defined == 0);
18195   /* While we're parsing the member functions we might encounter more
18196      classes.  We want to handle them right away, but we don't want
18197      them getting mixed up with functions that are currently in the
18198      queue.  */
18199   parser->unparsed_functions_queues
18200     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18201
18202   /* Make sure that any template parameters are in scope.  */
18203   maybe_begin_member_template_processing (member_function);
18204
18205   /* If the body of the function has not yet been parsed, parse it
18206      now.  */
18207   if (DECL_PENDING_INLINE_P (member_function))
18208     {
18209       tree function_scope;
18210       cp_token_cache *tokens;
18211
18212       /* The function is no longer pending; we are processing it.  */
18213       tokens = DECL_PENDING_INLINE_INFO (member_function);
18214       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18215       DECL_PENDING_INLINE_P (member_function) = 0;
18216
18217       /* If this is a local class, enter the scope of the containing
18218          function.  */
18219       function_scope = current_function_decl;
18220       if (function_scope)
18221         push_function_context ();
18222
18223       /* Push the body of the function onto the lexer stack.  */
18224       cp_parser_push_lexer_for_tokens (parser, tokens);
18225
18226       /* Let the front end know that we going to be defining this
18227          function.  */
18228       start_preparsed_function (member_function, NULL_TREE,
18229                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18230
18231       /* Don't do access checking if it is a templated function.  */
18232       if (processing_template_decl)
18233         push_deferring_access_checks (dk_no_check);
18234
18235       /* Now, parse the body of the function.  */
18236       cp_parser_function_definition_after_declarator (parser,
18237                                                       /*inline_p=*/true);
18238
18239       if (processing_template_decl)
18240         pop_deferring_access_checks ();
18241
18242       /* Leave the scope of the containing function.  */
18243       if (function_scope)
18244         pop_function_context ();
18245       cp_parser_pop_lexer (parser);
18246     }
18247
18248   /* Remove any template parameters from the symbol table.  */
18249   maybe_end_member_template_processing ();
18250
18251   /* Restore the queue.  */
18252   parser->unparsed_functions_queues
18253     = TREE_CHAIN (parser->unparsed_functions_queues);
18254 }
18255
18256 /* If DECL contains any default args, remember it on the unparsed
18257    functions queue.  */
18258
18259 static void
18260 cp_parser_save_default_args (cp_parser* parser, tree decl)
18261 {
18262   tree probe;
18263
18264   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18265        probe;
18266        probe = TREE_CHAIN (probe))
18267     if (TREE_PURPOSE (probe))
18268       {
18269         TREE_PURPOSE (parser->unparsed_functions_queues)
18270           = tree_cons (current_class_type, decl,
18271                        TREE_PURPOSE (parser->unparsed_functions_queues));
18272         break;
18273       }
18274 }
18275
18276 /* FN is a FUNCTION_DECL which may contains a parameter with an
18277    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18278    assumes that the current scope is the scope in which the default
18279    argument should be processed.  */
18280
18281 static void
18282 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18283 {
18284   bool saved_local_variables_forbidden_p;
18285   tree parm;
18286
18287   /* While we're parsing the default args, we might (due to the
18288      statement expression extension) encounter more classes.  We want
18289      to handle them right away, but we don't want them getting mixed
18290      up with default args that are currently in the queue.  */
18291   parser->unparsed_functions_queues
18292     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18293
18294   /* Local variable names (and the `this' keyword) may not appear
18295      in a default argument.  */
18296   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18297   parser->local_variables_forbidden_p = true;
18298
18299   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18300        parm;
18301        parm = TREE_CHAIN (parm))
18302     {
18303       cp_token_cache *tokens;
18304       tree default_arg = TREE_PURPOSE (parm);
18305       tree parsed_arg;
18306       VEC(tree,gc) *insts;
18307       tree copy;
18308       unsigned ix;
18309
18310       if (!default_arg)
18311         continue;
18312
18313       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18314         /* This can happen for a friend declaration for a function
18315            already declared with default arguments.  */
18316         continue;
18317
18318        /* Push the saved tokens for the default argument onto the parser's
18319           lexer stack.  */
18320       tokens = DEFARG_TOKENS (default_arg);
18321       cp_parser_push_lexer_for_tokens (parser, tokens);
18322
18323       /* Parse the assignment-expression.  */
18324       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18325       if (parsed_arg == error_mark_node)
18326         {
18327           cp_parser_pop_lexer (parser);
18328           continue;
18329         }
18330
18331       if (!processing_template_decl)
18332         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18333
18334       TREE_PURPOSE (parm) = parsed_arg;
18335
18336       /* Update any instantiations we've already created.  */
18337       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18338            VEC_iterate (tree, insts, ix, copy); ix++)
18339         TREE_PURPOSE (copy) = parsed_arg;
18340
18341       /* If the token stream has not been completely used up, then
18342          there was extra junk after the end of the default
18343          argument.  */
18344       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18345         cp_parser_error (parser, "expected %<,%>");
18346
18347       /* Revert to the main lexer.  */
18348       cp_parser_pop_lexer (parser);
18349     }
18350
18351   /* Make sure no default arg is missing.  */
18352   check_default_args (fn);
18353
18354   /* Restore the state of local_variables_forbidden_p.  */
18355   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18356
18357   /* Restore the queue.  */
18358   parser->unparsed_functions_queues
18359     = TREE_CHAIN (parser->unparsed_functions_queues);
18360 }
18361
18362 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18363    either a TYPE or an expression, depending on the form of the
18364    input.  The KEYWORD indicates which kind of expression we have
18365    encountered.  */
18366
18367 static tree
18368 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18369 {
18370   tree expr = NULL_TREE;
18371   const char *saved_message;
18372   char *tmp;
18373   bool saved_integral_constant_expression_p;
18374   bool saved_non_integral_constant_expression_p;
18375   bool pack_expansion_p = false;
18376
18377   /* Types cannot be defined in a `sizeof' expression.  Save away the
18378      old message.  */
18379   saved_message = parser->type_definition_forbidden_message;
18380   /* And create the new one.  */
18381   tmp = concat ("types may not be defined in %<",
18382                 IDENTIFIER_POINTER (ridpointers[keyword]),
18383                 "%> expressions", NULL);
18384   parser->type_definition_forbidden_message = tmp;
18385
18386   /* The restrictions on constant-expressions do not apply inside
18387      sizeof expressions.  */
18388   saved_integral_constant_expression_p
18389     = parser->integral_constant_expression_p;
18390   saved_non_integral_constant_expression_p
18391     = parser->non_integral_constant_expression_p;
18392   parser->integral_constant_expression_p = false;
18393
18394   /* If it's a `...', then we are computing the length of a parameter
18395      pack.  */
18396   if (keyword == RID_SIZEOF
18397       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18398     {
18399       /* Consume the `...'.  */
18400       cp_lexer_consume_token (parser->lexer);
18401       maybe_warn_variadic_templates ();
18402
18403       /* Note that this is an expansion.  */
18404       pack_expansion_p = true;
18405     }
18406
18407   /* Do not actually evaluate the expression.  */
18408   ++skip_evaluation;
18409   /* If it's a `(', then we might be looking at the type-id
18410      construction.  */
18411   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18412     {
18413       tree type;
18414       bool saved_in_type_id_in_expr_p;
18415
18416       /* We can't be sure yet whether we're looking at a type-id or an
18417          expression.  */
18418       cp_parser_parse_tentatively (parser);
18419       /* Consume the `('.  */
18420       cp_lexer_consume_token (parser->lexer);
18421       /* Parse the type-id.  */
18422       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18423       parser->in_type_id_in_expr_p = true;
18424       type = cp_parser_type_id (parser);
18425       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18426       /* Now, look for the trailing `)'.  */
18427       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18428       /* If all went well, then we're done.  */
18429       if (cp_parser_parse_definitely (parser))
18430         {
18431           cp_decl_specifier_seq decl_specs;
18432
18433           /* Build a trivial decl-specifier-seq.  */
18434           clear_decl_specs (&decl_specs);
18435           decl_specs.type = type;
18436
18437           /* Call grokdeclarator to figure out what type this is.  */
18438           expr = grokdeclarator (NULL,
18439                                  &decl_specs,
18440                                  TYPENAME,
18441                                  /*initialized=*/0,
18442                                  /*attrlist=*/NULL);
18443         }
18444     }
18445
18446   /* If the type-id production did not work out, then we must be
18447      looking at the unary-expression production.  */
18448   if (!expr)
18449     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18450                                        /*cast_p=*/false, NULL);
18451
18452   if (pack_expansion_p)
18453     /* Build a pack expansion. */
18454     expr = make_pack_expansion (expr);
18455
18456   /* Go back to evaluating expressions.  */
18457   --skip_evaluation;
18458
18459   /* Free the message we created.  */
18460   free (tmp);
18461   /* And restore the old one.  */
18462   parser->type_definition_forbidden_message = saved_message;
18463   parser->integral_constant_expression_p
18464     = saved_integral_constant_expression_p;
18465   parser->non_integral_constant_expression_p
18466     = saved_non_integral_constant_expression_p;
18467
18468   return expr;
18469 }
18470
18471 /* If the current declaration has no declarator, return true.  */
18472
18473 static bool
18474 cp_parser_declares_only_class_p (cp_parser *parser)
18475 {
18476   /* If the next token is a `;' or a `,' then there is no
18477      declarator.  */
18478   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18479           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18480 }
18481
18482 /* Update the DECL_SPECS to reflect the storage class indicated by
18483    KEYWORD.  */
18484
18485 static void
18486 cp_parser_set_storage_class (cp_parser *parser,
18487                              cp_decl_specifier_seq *decl_specs,
18488                              enum rid keyword,
18489                              location_t location)
18490 {
18491   cp_storage_class storage_class;
18492
18493   if (parser->in_unbraced_linkage_specification_p)
18494     {
18495       error ("%Hinvalid use of %qD in linkage specification",
18496              &location, ridpointers[keyword]);
18497       return;
18498     }
18499   else if (decl_specs->storage_class != sc_none)
18500     {
18501       decl_specs->conflicting_specifiers_p = true;
18502       return;
18503     }
18504
18505   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18506       && decl_specs->specs[(int) ds_thread])
18507     {
18508       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18509       decl_specs->specs[(int) ds_thread] = 0;
18510     }
18511
18512   switch (keyword)
18513     {
18514     case RID_AUTO:
18515       storage_class = sc_auto;
18516       break;
18517     case RID_REGISTER:
18518       storage_class = sc_register;
18519       break;
18520     case RID_STATIC:
18521       storage_class = sc_static;
18522       break;
18523     case RID_EXTERN:
18524       storage_class = sc_extern;
18525       break;
18526     case RID_MUTABLE:
18527       storage_class = sc_mutable;
18528       break;
18529     default:
18530       gcc_unreachable ();
18531     }
18532   decl_specs->storage_class = storage_class;
18533
18534   /* A storage class specifier cannot be applied alongside a typedef 
18535      specifier. If there is a typedef specifier present then set 
18536      conflicting_specifiers_p which will trigger an error later
18537      on in grokdeclarator. */
18538   if (decl_specs->specs[(int)ds_typedef])
18539     decl_specs->conflicting_specifiers_p = true;
18540 }
18541
18542 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18543    is true, the type is a user-defined type; otherwise it is a
18544    built-in type specified by a keyword.  */
18545
18546 static void
18547 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18548                               tree type_spec,
18549                               location_t location,
18550                               bool user_defined_p)
18551 {
18552   decl_specs->any_specifiers_p = true;
18553
18554   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18555      (with, for example, in "typedef int wchar_t;") we remember that
18556      this is what happened.  In system headers, we ignore these
18557      declarations so that G++ can work with system headers that are not
18558      C++-safe.  */
18559   if (decl_specs->specs[(int) ds_typedef]
18560       && !user_defined_p
18561       && (type_spec == boolean_type_node
18562           || type_spec == char16_type_node
18563           || type_spec == char32_type_node
18564           || type_spec == wchar_type_node)
18565       && (decl_specs->type
18566           || decl_specs->specs[(int) ds_long]
18567           || decl_specs->specs[(int) ds_short]
18568           || decl_specs->specs[(int) ds_unsigned]
18569           || decl_specs->specs[(int) ds_signed]))
18570     {
18571       decl_specs->redefined_builtin_type = type_spec;
18572       if (!decl_specs->type)
18573         {
18574           decl_specs->type = type_spec;
18575           decl_specs->user_defined_type_p = false;
18576           decl_specs->type_location = location;
18577         }
18578     }
18579   else if (decl_specs->type)
18580     decl_specs->multiple_types_p = true;
18581   else
18582     {
18583       decl_specs->type = type_spec;
18584       decl_specs->user_defined_type_p = user_defined_p;
18585       decl_specs->redefined_builtin_type = NULL_TREE;
18586       decl_specs->type_location = location;
18587     }
18588 }
18589
18590 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18591    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18592
18593 static bool
18594 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18595 {
18596   return decl_specifiers->specs[(int) ds_friend] != 0;
18597 }
18598
18599 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18600    issue an error message indicating that TOKEN_DESC was expected.
18601
18602    Returns the token consumed, if the token had the appropriate type.
18603    Otherwise, returns NULL.  */
18604
18605 static cp_token *
18606 cp_parser_require (cp_parser* parser,
18607                    enum cpp_ttype type,
18608                    const char* token_desc)
18609 {
18610   if (cp_lexer_next_token_is (parser->lexer, type))
18611     return cp_lexer_consume_token (parser->lexer);
18612   else
18613     {
18614       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18615       if (!cp_parser_simulate_error (parser))
18616         {
18617           char *message = concat ("expected ", token_desc, NULL);
18618           cp_parser_error (parser, message);
18619           free (message);
18620         }
18621       return NULL;
18622     }
18623 }
18624
18625 /* An error message is produced if the next token is not '>'.
18626    All further tokens are skipped until the desired token is
18627    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18628
18629 static void
18630 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18631 {
18632   /* Current level of '< ... >'.  */
18633   unsigned level = 0;
18634   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18635   unsigned nesting_depth = 0;
18636
18637   /* Are we ready, yet?  If not, issue error message.  */
18638   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18639     return;
18640
18641   /* Skip tokens until the desired token is found.  */
18642   while (true)
18643     {
18644       /* Peek at the next token.  */
18645       switch (cp_lexer_peek_token (parser->lexer)->type)
18646         {
18647         case CPP_LESS:
18648           if (!nesting_depth)
18649             ++level;
18650           break;
18651
18652         case CPP_RSHIFT:
18653           if (cxx_dialect == cxx98)
18654             /* C++0x views the `>>' operator as two `>' tokens, but
18655                C++98 does not. */
18656             break;
18657           else if (!nesting_depth && level-- == 0)
18658             {
18659               /* We've hit a `>>' where the first `>' closes the
18660                  template argument list, and the second `>' is
18661                  spurious.  Just consume the `>>' and stop; we've
18662                  already produced at least one error.  */
18663               cp_lexer_consume_token (parser->lexer);
18664               return;
18665             }
18666           /* Fall through for C++0x, so we handle the second `>' in
18667              the `>>'.  */
18668
18669         case CPP_GREATER:
18670           if (!nesting_depth && level-- == 0)
18671             {
18672               /* We've reached the token we want, consume it and stop.  */
18673               cp_lexer_consume_token (parser->lexer);
18674               return;
18675             }
18676           break;
18677
18678         case CPP_OPEN_PAREN:
18679         case CPP_OPEN_SQUARE:
18680           ++nesting_depth;
18681           break;
18682
18683         case CPP_CLOSE_PAREN:
18684         case CPP_CLOSE_SQUARE:
18685           if (nesting_depth-- == 0)
18686             return;
18687           break;
18688
18689         case CPP_EOF:
18690         case CPP_PRAGMA_EOL:
18691         case CPP_SEMICOLON:
18692         case CPP_OPEN_BRACE:
18693         case CPP_CLOSE_BRACE:
18694           /* The '>' was probably forgotten, don't look further.  */
18695           return;
18696
18697         default:
18698           break;
18699         }
18700
18701       /* Consume this token.  */
18702       cp_lexer_consume_token (parser->lexer);
18703     }
18704 }
18705
18706 /* If the next token is the indicated keyword, consume it.  Otherwise,
18707    issue an error message indicating that TOKEN_DESC was expected.
18708
18709    Returns the token consumed, if the token had the appropriate type.
18710    Otherwise, returns NULL.  */
18711
18712 static cp_token *
18713 cp_parser_require_keyword (cp_parser* parser,
18714                            enum rid keyword,
18715                            const char* token_desc)
18716 {
18717   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18718
18719   if (token && token->keyword != keyword)
18720     {
18721       dyn_string_t error_msg;
18722
18723       /* Format the error message.  */
18724       error_msg = dyn_string_new (0);
18725       dyn_string_append_cstr (error_msg, "expected ");
18726       dyn_string_append_cstr (error_msg, token_desc);
18727       cp_parser_error (parser, error_msg->s);
18728       dyn_string_delete (error_msg);
18729       return NULL;
18730     }
18731
18732   return token;
18733 }
18734
18735 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18736    function-definition.  */
18737
18738 static bool
18739 cp_parser_token_starts_function_definition_p (cp_token* token)
18740 {
18741   return (/* An ordinary function-body begins with an `{'.  */
18742           token->type == CPP_OPEN_BRACE
18743           /* A ctor-initializer begins with a `:'.  */
18744           || token->type == CPP_COLON
18745           /* A function-try-block begins with `try'.  */
18746           || token->keyword == RID_TRY
18747           /* The named return value extension begins with `return'.  */
18748           || token->keyword == RID_RETURN);
18749 }
18750
18751 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18752    definition.  */
18753
18754 static bool
18755 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18756 {
18757   cp_token *token;
18758
18759   token = cp_lexer_peek_token (parser->lexer);
18760   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18761 }
18762
18763 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18764    C++0x) ending a template-argument.  */
18765
18766 static bool
18767 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18768 {
18769   cp_token *token;
18770
18771   token = cp_lexer_peek_token (parser->lexer);
18772   return (token->type == CPP_COMMA 
18773           || token->type == CPP_GREATER
18774           || token->type == CPP_ELLIPSIS
18775           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18776 }
18777
18778 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18779    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18780
18781 static bool
18782 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18783                                                      size_t n)
18784 {
18785   cp_token *token;
18786
18787   token = cp_lexer_peek_nth_token (parser->lexer, n);
18788   if (token->type == CPP_LESS)
18789     return true;
18790   /* Check for the sequence `<::' in the original code. It would be lexed as
18791      `[:', where `[' is a digraph, and there is no whitespace before
18792      `:'.  */
18793   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18794     {
18795       cp_token *token2;
18796       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18797       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18798         return true;
18799     }
18800   return false;
18801 }
18802
18803 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18804    or none_type otherwise.  */
18805
18806 static enum tag_types
18807 cp_parser_token_is_class_key (cp_token* token)
18808 {
18809   switch (token->keyword)
18810     {
18811     case RID_CLASS:
18812       return class_type;
18813     case RID_STRUCT:
18814       return record_type;
18815     case RID_UNION:
18816       return union_type;
18817
18818     default:
18819       return none_type;
18820     }
18821 }
18822
18823 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18824
18825 static void
18826 cp_parser_check_class_key (enum tag_types class_key, tree type)
18827 {
18828   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18829     permerror (input_location, "%qs tag used in naming %q#T",
18830             class_key == union_type ? "union"
18831              : class_key == record_type ? "struct" : "class",
18832              type);
18833 }
18834
18835 /* Issue an error message if DECL is redeclared with different
18836    access than its original declaration [class.access.spec/3].
18837    This applies to nested classes and nested class templates.
18838    [class.mem/1].  */
18839
18840 static void
18841 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18842 {
18843   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18844     return;
18845
18846   if ((TREE_PRIVATE (decl)
18847        != (current_access_specifier == access_private_node))
18848       || (TREE_PROTECTED (decl)
18849           != (current_access_specifier == access_protected_node)))
18850     error ("%H%qD redeclared with different access", &location, decl);
18851 }
18852
18853 /* Look for the `template' keyword, as a syntactic disambiguator.
18854    Return TRUE iff it is present, in which case it will be
18855    consumed.  */
18856
18857 static bool
18858 cp_parser_optional_template_keyword (cp_parser *parser)
18859 {
18860   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18861     {
18862       /* The `template' keyword can only be used within templates;
18863          outside templates the parser can always figure out what is a
18864          template and what is not.  */
18865       if (!processing_template_decl)
18866         {
18867           cp_token *token = cp_lexer_peek_token (parser->lexer);
18868           error ("%H%<template%> (as a disambiguator) is only allowed "
18869                  "within templates", &token->location);
18870           /* If this part of the token stream is rescanned, the same
18871              error message would be generated.  So, we purge the token
18872              from the stream.  */
18873           cp_lexer_purge_token (parser->lexer);
18874           return false;
18875         }
18876       else
18877         {
18878           /* Consume the `template' keyword.  */
18879           cp_lexer_consume_token (parser->lexer);
18880           return true;
18881         }
18882     }
18883
18884   return false;
18885 }
18886
18887 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18888    set PARSER->SCOPE, and perform other related actions.  */
18889
18890 static void
18891 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18892 {
18893   int i;
18894   struct tree_check *check_value;
18895   deferred_access_check *chk;
18896   VEC (deferred_access_check,gc) *checks;
18897
18898   /* Get the stored value.  */
18899   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18900   /* Perform any access checks that were deferred.  */
18901   checks = check_value->checks;
18902   if (checks)
18903     {
18904       for (i = 0 ;
18905            VEC_iterate (deferred_access_check, checks, i, chk) ;
18906            ++i)
18907         {
18908           perform_or_defer_access_check (chk->binfo,
18909                                          chk->decl,
18910                                          chk->diag_decl);
18911         }
18912     }
18913   /* Set the scope from the stored value.  */
18914   parser->scope = check_value->value;
18915   parser->qualifying_scope = check_value->qualifying_scope;
18916   parser->object_scope = NULL_TREE;
18917 }
18918
18919 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18920    encounter the end of a block before what we were looking for.  */
18921
18922 static bool
18923 cp_parser_cache_group (cp_parser *parser,
18924                        enum cpp_ttype end,
18925                        unsigned depth)
18926 {
18927   while (true)
18928     {
18929       cp_token *token = cp_lexer_peek_token (parser->lexer);
18930
18931       /* Abort a parenthesized expression if we encounter a semicolon.  */
18932       if ((end == CPP_CLOSE_PAREN || depth == 0)
18933           && token->type == CPP_SEMICOLON)
18934         return true;
18935       /* If we've reached the end of the file, stop.  */
18936       if (token->type == CPP_EOF
18937           || (end != CPP_PRAGMA_EOL
18938               && token->type == CPP_PRAGMA_EOL))
18939         return true;
18940       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18941         /* We've hit the end of an enclosing block, so there's been some
18942            kind of syntax error.  */
18943         return true;
18944
18945       /* Consume the token.  */
18946       cp_lexer_consume_token (parser->lexer);
18947       /* See if it starts a new group.  */
18948       if (token->type == CPP_OPEN_BRACE)
18949         {
18950           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18951           /* In theory this should probably check end == '}', but
18952              cp_parser_save_member_function_body needs it to exit
18953              after either '}' or ')' when called with ')'.  */
18954           if (depth == 0)
18955             return false;
18956         }
18957       else if (token->type == CPP_OPEN_PAREN)
18958         {
18959           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18960           if (depth == 0 && end == CPP_CLOSE_PAREN)
18961             return false;
18962         }
18963       else if (token->type == CPP_PRAGMA)
18964         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18965       else if (token->type == end)
18966         return false;
18967     }
18968 }
18969
18970 /* Begin parsing tentatively.  We always save tokens while parsing
18971    tentatively so that if the tentative parsing fails we can restore the
18972    tokens.  */
18973
18974 static void
18975 cp_parser_parse_tentatively (cp_parser* parser)
18976 {
18977   /* Enter a new parsing context.  */
18978   parser->context = cp_parser_context_new (parser->context);
18979   /* Begin saving tokens.  */
18980   cp_lexer_save_tokens (parser->lexer);
18981   /* In order to avoid repetitive access control error messages,
18982      access checks are queued up until we are no longer parsing
18983      tentatively.  */
18984   push_deferring_access_checks (dk_deferred);
18985 }
18986
18987 /* Commit to the currently active tentative parse.  */
18988
18989 static void
18990 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18991 {
18992   cp_parser_context *context;
18993   cp_lexer *lexer;
18994
18995   /* Mark all of the levels as committed.  */
18996   lexer = parser->lexer;
18997   for (context = parser->context; context->next; context = context->next)
18998     {
18999       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19000         break;
19001       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19002       while (!cp_lexer_saving_tokens (lexer))
19003         lexer = lexer->next;
19004       cp_lexer_commit_tokens (lexer);
19005     }
19006 }
19007
19008 /* Abort the currently active tentative parse.  All consumed tokens
19009    will be rolled back, and no diagnostics will be issued.  */
19010
19011 static void
19012 cp_parser_abort_tentative_parse (cp_parser* parser)
19013 {
19014   cp_parser_simulate_error (parser);
19015   /* Now, pretend that we want to see if the construct was
19016      successfully parsed.  */
19017   cp_parser_parse_definitely (parser);
19018 }
19019
19020 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19021    token stream.  Otherwise, commit to the tokens we have consumed.
19022    Returns true if no error occurred; false otherwise.  */
19023
19024 static bool
19025 cp_parser_parse_definitely (cp_parser* parser)
19026 {
19027   bool error_occurred;
19028   cp_parser_context *context;
19029
19030   /* Remember whether or not an error occurred, since we are about to
19031      destroy that information.  */
19032   error_occurred = cp_parser_error_occurred (parser);
19033   /* Remove the topmost context from the stack.  */
19034   context = parser->context;
19035   parser->context = context->next;
19036   /* If no parse errors occurred, commit to the tentative parse.  */
19037   if (!error_occurred)
19038     {
19039       /* Commit to the tokens read tentatively, unless that was
19040          already done.  */
19041       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19042         cp_lexer_commit_tokens (parser->lexer);
19043
19044       pop_to_parent_deferring_access_checks ();
19045     }
19046   /* Otherwise, if errors occurred, roll back our state so that things
19047      are just as they were before we began the tentative parse.  */
19048   else
19049     {
19050       cp_lexer_rollback_tokens (parser->lexer);
19051       pop_deferring_access_checks ();
19052     }
19053   /* Add the context to the front of the free list.  */
19054   context->next = cp_parser_context_free_list;
19055   cp_parser_context_free_list = context;
19056
19057   return !error_occurred;
19058 }
19059
19060 /* Returns true if we are parsing tentatively and are not committed to
19061    this tentative parse.  */
19062
19063 static bool
19064 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19065 {
19066   return (cp_parser_parsing_tentatively (parser)
19067           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19068 }
19069
19070 /* Returns nonzero iff an error has occurred during the most recent
19071    tentative parse.  */
19072
19073 static bool
19074 cp_parser_error_occurred (cp_parser* parser)
19075 {
19076   return (cp_parser_parsing_tentatively (parser)
19077           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19078 }
19079
19080 /* Returns nonzero if GNU extensions are allowed.  */
19081
19082 static bool
19083 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19084 {
19085   return parser->allow_gnu_extensions_p;
19086 }
19087 \f
19088 /* Objective-C++ Productions */
19089
19090
19091 /* Parse an Objective-C expression, which feeds into a primary-expression
19092    above.
19093
19094    objc-expression:
19095      objc-message-expression
19096      objc-string-literal
19097      objc-encode-expression
19098      objc-protocol-expression
19099      objc-selector-expression
19100
19101   Returns a tree representation of the expression.  */
19102
19103 static tree
19104 cp_parser_objc_expression (cp_parser* parser)
19105 {
19106   /* Try to figure out what kind of declaration is present.  */
19107   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19108
19109   switch (kwd->type)
19110     {
19111     case CPP_OPEN_SQUARE:
19112       return cp_parser_objc_message_expression (parser);
19113
19114     case CPP_OBJC_STRING:
19115       kwd = cp_lexer_consume_token (parser->lexer);
19116       return objc_build_string_object (kwd->u.value);
19117
19118     case CPP_KEYWORD:
19119       switch (kwd->keyword)
19120         {
19121         case RID_AT_ENCODE:
19122           return cp_parser_objc_encode_expression (parser);
19123
19124         case RID_AT_PROTOCOL:
19125           return cp_parser_objc_protocol_expression (parser);
19126
19127         case RID_AT_SELECTOR:
19128           return cp_parser_objc_selector_expression (parser);
19129
19130         default:
19131           break;
19132         }
19133     default:
19134       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19135              &kwd->location, kwd->u.value);
19136       cp_parser_skip_to_end_of_block_or_statement (parser);
19137     }
19138
19139   return error_mark_node;
19140 }
19141
19142 /* Parse an Objective-C message expression.
19143
19144    objc-message-expression:
19145      [ objc-message-receiver objc-message-args ]
19146
19147    Returns a representation of an Objective-C message.  */
19148
19149 static tree
19150 cp_parser_objc_message_expression (cp_parser* parser)
19151 {
19152   tree receiver, messageargs;
19153
19154   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19155   receiver = cp_parser_objc_message_receiver (parser);
19156   messageargs = cp_parser_objc_message_args (parser);
19157   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19158
19159   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19160 }
19161
19162 /* Parse an objc-message-receiver.
19163
19164    objc-message-receiver:
19165      expression
19166      simple-type-specifier
19167
19168   Returns a representation of the type or expression.  */
19169
19170 static tree
19171 cp_parser_objc_message_receiver (cp_parser* parser)
19172 {
19173   tree rcv;
19174
19175   /* An Objective-C message receiver may be either (1) a type
19176      or (2) an expression.  */
19177   cp_parser_parse_tentatively (parser);
19178   rcv = cp_parser_expression (parser, false, NULL);
19179
19180   if (cp_parser_parse_definitely (parser))
19181     return rcv;
19182
19183   rcv = cp_parser_simple_type_specifier (parser,
19184                                          /*decl_specs=*/NULL,
19185                                          CP_PARSER_FLAGS_NONE);
19186
19187   return objc_get_class_reference (rcv);
19188 }
19189
19190 /* Parse the arguments and selectors comprising an Objective-C message.
19191
19192    objc-message-args:
19193      objc-selector
19194      objc-selector-args
19195      objc-selector-args , objc-comma-args
19196
19197    objc-selector-args:
19198      objc-selector [opt] : assignment-expression
19199      objc-selector-args objc-selector [opt] : assignment-expression
19200
19201    objc-comma-args:
19202      assignment-expression
19203      objc-comma-args , assignment-expression
19204
19205    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19206    selector arguments and TREE_VALUE containing a list of comma
19207    arguments.  */
19208
19209 static tree
19210 cp_parser_objc_message_args (cp_parser* parser)
19211 {
19212   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19213   bool maybe_unary_selector_p = true;
19214   cp_token *token = cp_lexer_peek_token (parser->lexer);
19215
19216   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19217     {
19218       tree selector = NULL_TREE, arg;
19219
19220       if (token->type != CPP_COLON)
19221         selector = cp_parser_objc_selector (parser);
19222
19223       /* Detect if we have a unary selector.  */
19224       if (maybe_unary_selector_p
19225           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19226         return build_tree_list (selector, NULL_TREE);
19227
19228       maybe_unary_selector_p = false;
19229       cp_parser_require (parser, CPP_COLON, "%<:%>");
19230       arg = cp_parser_assignment_expression (parser, false, NULL);
19231
19232       sel_args
19233         = chainon (sel_args,
19234                    build_tree_list (selector, arg));
19235
19236       token = cp_lexer_peek_token (parser->lexer);
19237     }
19238
19239   /* Handle non-selector arguments, if any. */
19240   while (token->type == CPP_COMMA)
19241     {
19242       tree arg;
19243
19244       cp_lexer_consume_token (parser->lexer);
19245       arg = cp_parser_assignment_expression (parser, false, NULL);
19246
19247       addl_args
19248         = chainon (addl_args,
19249                    build_tree_list (NULL_TREE, arg));
19250
19251       token = cp_lexer_peek_token (parser->lexer);
19252     }
19253
19254   return build_tree_list (sel_args, addl_args);
19255 }
19256
19257 /* Parse an Objective-C encode expression.
19258
19259    objc-encode-expression:
19260      @encode objc-typename
19261
19262    Returns an encoded representation of the type argument.  */
19263
19264 static tree
19265 cp_parser_objc_encode_expression (cp_parser* parser)
19266 {
19267   tree type;
19268   cp_token *token;
19269
19270   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19271   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19272   token = cp_lexer_peek_token (parser->lexer);
19273   type = complete_type (cp_parser_type_id (parser));
19274   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19275
19276   if (!type)
19277     {
19278       error ("%H%<@encode%> must specify a type as an argument",
19279              &token->location);
19280       return error_mark_node;
19281     }
19282
19283   return objc_build_encode_expr (type);
19284 }
19285
19286 /* Parse an Objective-C @defs expression.  */
19287
19288 static tree
19289 cp_parser_objc_defs_expression (cp_parser *parser)
19290 {
19291   tree name;
19292
19293   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19294   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19295   name = cp_parser_identifier (parser);
19296   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19297
19298   return objc_get_class_ivars (name);
19299 }
19300
19301 /* Parse an Objective-C protocol expression.
19302
19303   objc-protocol-expression:
19304     @protocol ( identifier )
19305
19306   Returns a representation of the protocol expression.  */
19307
19308 static tree
19309 cp_parser_objc_protocol_expression (cp_parser* parser)
19310 {
19311   tree proto;
19312
19313   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19314   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19315   proto = cp_parser_identifier (parser);
19316   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19317
19318   return objc_build_protocol_expr (proto);
19319 }
19320
19321 /* Parse an Objective-C selector expression.
19322
19323    objc-selector-expression:
19324      @selector ( objc-method-signature )
19325
19326    objc-method-signature:
19327      objc-selector
19328      objc-selector-seq
19329
19330    objc-selector-seq:
19331      objc-selector :
19332      objc-selector-seq objc-selector :
19333
19334   Returns a representation of the method selector.  */
19335
19336 static tree
19337 cp_parser_objc_selector_expression (cp_parser* parser)
19338 {
19339   tree sel_seq = NULL_TREE;
19340   bool maybe_unary_selector_p = true;
19341   cp_token *token;
19342
19343   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19344   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19345   token = cp_lexer_peek_token (parser->lexer);
19346
19347   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19348          || token->type == CPP_SCOPE)
19349     {
19350       tree selector = NULL_TREE;
19351
19352       if (token->type != CPP_COLON
19353           || token->type == CPP_SCOPE)
19354         selector = cp_parser_objc_selector (parser);
19355
19356       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19357           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19358         {
19359           /* Detect if we have a unary selector.  */
19360           if (maybe_unary_selector_p)
19361             {
19362               sel_seq = selector;
19363               goto finish_selector;
19364             }
19365           else
19366             {
19367               cp_parser_error (parser, "expected %<:%>");
19368             }
19369         }
19370       maybe_unary_selector_p = false;
19371       token = cp_lexer_consume_token (parser->lexer);
19372
19373       if (token->type == CPP_SCOPE)
19374         {
19375           sel_seq
19376             = chainon (sel_seq,
19377                        build_tree_list (selector, NULL_TREE));
19378           sel_seq
19379             = chainon (sel_seq,
19380                        build_tree_list (NULL_TREE, NULL_TREE));
19381         }
19382       else
19383         sel_seq
19384           = chainon (sel_seq,
19385                      build_tree_list (selector, NULL_TREE));
19386
19387       token = cp_lexer_peek_token (parser->lexer);
19388     }
19389
19390  finish_selector:
19391   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19392
19393   return objc_build_selector_expr (sel_seq);
19394 }
19395
19396 /* Parse a list of identifiers.
19397
19398    objc-identifier-list:
19399      identifier
19400      objc-identifier-list , identifier
19401
19402    Returns a TREE_LIST of identifier nodes.  */
19403
19404 static tree
19405 cp_parser_objc_identifier_list (cp_parser* parser)
19406 {
19407   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19408   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19409
19410   while (sep->type == CPP_COMMA)
19411     {
19412       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19413       list = chainon (list,
19414                       build_tree_list (NULL_TREE,
19415                                        cp_parser_identifier (parser)));
19416       sep = cp_lexer_peek_token (parser->lexer);
19417     }
19418
19419   return list;
19420 }
19421
19422 /* Parse an Objective-C alias declaration.
19423
19424    objc-alias-declaration:
19425      @compatibility_alias identifier identifier ;
19426
19427    This function registers the alias mapping with the Objective-C front end.
19428    It returns nothing.  */
19429
19430 static void
19431 cp_parser_objc_alias_declaration (cp_parser* parser)
19432 {
19433   tree alias, orig;
19434
19435   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19436   alias = cp_parser_identifier (parser);
19437   orig = cp_parser_identifier (parser);
19438   objc_declare_alias (alias, orig);
19439   cp_parser_consume_semicolon_at_end_of_statement (parser);
19440 }
19441
19442 /* Parse an Objective-C class forward-declaration.
19443
19444    objc-class-declaration:
19445      @class objc-identifier-list ;
19446
19447    The function registers the forward declarations with the Objective-C
19448    front end.  It returns nothing.  */
19449
19450 static void
19451 cp_parser_objc_class_declaration (cp_parser* parser)
19452 {
19453   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19454   objc_declare_class (cp_parser_objc_identifier_list (parser));
19455   cp_parser_consume_semicolon_at_end_of_statement (parser);
19456 }
19457
19458 /* Parse a list of Objective-C protocol references.
19459
19460    objc-protocol-refs-opt:
19461      objc-protocol-refs [opt]
19462
19463    objc-protocol-refs:
19464      < objc-identifier-list >
19465
19466    Returns a TREE_LIST of identifiers, if any.  */
19467
19468 static tree
19469 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19470 {
19471   tree protorefs = NULL_TREE;
19472
19473   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19474     {
19475       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19476       protorefs = cp_parser_objc_identifier_list (parser);
19477       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19478     }
19479
19480   return protorefs;
19481 }
19482
19483 /* Parse a Objective-C visibility specification.  */
19484
19485 static void
19486 cp_parser_objc_visibility_spec (cp_parser* parser)
19487 {
19488   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19489
19490   switch (vis->keyword)
19491     {
19492     case RID_AT_PRIVATE:
19493       objc_set_visibility (2);
19494       break;
19495     case RID_AT_PROTECTED:
19496       objc_set_visibility (0);
19497       break;
19498     case RID_AT_PUBLIC:
19499       objc_set_visibility (1);
19500       break;
19501     default:
19502       return;
19503     }
19504
19505   /* Eat '@private'/'@protected'/'@public'.  */
19506   cp_lexer_consume_token (parser->lexer);
19507 }
19508
19509 /* Parse an Objective-C method type.  */
19510
19511 static void
19512 cp_parser_objc_method_type (cp_parser* parser)
19513 {
19514   objc_set_method_type
19515    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19516     ? PLUS_EXPR
19517     : MINUS_EXPR);
19518 }
19519
19520 /* Parse an Objective-C protocol qualifier.  */
19521
19522 static tree
19523 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19524 {
19525   tree quals = NULL_TREE, node;
19526   cp_token *token = cp_lexer_peek_token (parser->lexer);
19527
19528   node = token->u.value;
19529
19530   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19531          && (node == ridpointers [(int) RID_IN]
19532              || node == ridpointers [(int) RID_OUT]
19533              || node == ridpointers [(int) RID_INOUT]
19534              || node == ridpointers [(int) RID_BYCOPY]
19535              || node == ridpointers [(int) RID_BYREF]
19536              || node == ridpointers [(int) RID_ONEWAY]))
19537     {
19538       quals = tree_cons (NULL_TREE, node, quals);
19539       cp_lexer_consume_token (parser->lexer);
19540       token = cp_lexer_peek_token (parser->lexer);
19541       node = token->u.value;
19542     }
19543
19544   return quals;
19545 }
19546
19547 /* Parse an Objective-C typename.  */
19548
19549 static tree
19550 cp_parser_objc_typename (cp_parser* parser)
19551 {
19552   tree type_name = NULL_TREE;
19553
19554   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19555     {
19556       tree proto_quals, cp_type = NULL_TREE;
19557
19558       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19559       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19560
19561       /* An ObjC type name may consist of just protocol qualifiers, in which
19562          case the type shall default to 'id'.  */
19563       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19564         cp_type = cp_parser_type_id (parser);
19565
19566       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19567       type_name = build_tree_list (proto_quals, cp_type);
19568     }
19569
19570   return type_name;
19571 }
19572
19573 /* Check to see if TYPE refers to an Objective-C selector name.  */
19574
19575 static bool
19576 cp_parser_objc_selector_p (enum cpp_ttype type)
19577 {
19578   return (type == CPP_NAME || type == CPP_KEYWORD
19579           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19580           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19581           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19582           || type == CPP_XOR || type == CPP_XOR_EQ);
19583 }
19584
19585 /* Parse an Objective-C selector.  */
19586
19587 static tree
19588 cp_parser_objc_selector (cp_parser* parser)
19589 {
19590   cp_token *token = cp_lexer_consume_token (parser->lexer);
19591
19592   if (!cp_parser_objc_selector_p (token->type))
19593     {
19594       error ("%Hinvalid Objective-C++ selector name", &token->location);
19595       return error_mark_node;
19596     }
19597
19598   /* C++ operator names are allowed to appear in ObjC selectors.  */
19599   switch (token->type)
19600     {
19601     case CPP_AND_AND: return get_identifier ("and");
19602     case CPP_AND_EQ: return get_identifier ("and_eq");
19603     case CPP_AND: return get_identifier ("bitand");
19604     case CPP_OR: return get_identifier ("bitor");
19605     case CPP_COMPL: return get_identifier ("compl");
19606     case CPP_NOT: return get_identifier ("not");
19607     case CPP_NOT_EQ: return get_identifier ("not_eq");
19608     case CPP_OR_OR: return get_identifier ("or");
19609     case CPP_OR_EQ: return get_identifier ("or_eq");
19610     case CPP_XOR: return get_identifier ("xor");
19611     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19612     default: return token->u.value;
19613     }
19614 }
19615
19616 /* Parse an Objective-C params list.  */
19617
19618 static tree
19619 cp_parser_objc_method_keyword_params (cp_parser* parser)
19620 {
19621   tree params = NULL_TREE;
19622   bool maybe_unary_selector_p = true;
19623   cp_token *token = cp_lexer_peek_token (parser->lexer);
19624
19625   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19626     {
19627       tree selector = NULL_TREE, type_name, identifier;
19628
19629       if (token->type != CPP_COLON)
19630         selector = cp_parser_objc_selector (parser);
19631
19632       /* Detect if we have a unary selector.  */
19633       if (maybe_unary_selector_p
19634           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19635         return selector;
19636
19637       maybe_unary_selector_p = false;
19638       cp_parser_require (parser, CPP_COLON, "%<:%>");
19639       type_name = cp_parser_objc_typename (parser);
19640       identifier = cp_parser_identifier (parser);
19641
19642       params
19643         = chainon (params,
19644                    objc_build_keyword_decl (selector,
19645                                             type_name,
19646                                             identifier));
19647
19648       token = cp_lexer_peek_token (parser->lexer);
19649     }
19650
19651   return params;
19652 }
19653
19654 /* Parse the non-keyword Objective-C params.  */
19655
19656 static tree
19657 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19658 {
19659   tree params = make_node (TREE_LIST);
19660   cp_token *token = cp_lexer_peek_token (parser->lexer);
19661   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19662
19663   while (token->type == CPP_COMMA)
19664     {
19665       cp_parameter_declarator *parmdecl;
19666       tree parm;
19667
19668       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19669       token = cp_lexer_peek_token (parser->lexer);
19670
19671       if (token->type == CPP_ELLIPSIS)
19672         {
19673           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19674           *ellipsisp = true;
19675           break;
19676         }
19677
19678       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19679       parm = grokdeclarator (parmdecl->declarator,
19680                              &parmdecl->decl_specifiers,
19681                              PARM, /*initialized=*/0,
19682                              /*attrlist=*/NULL);
19683
19684       chainon (params, build_tree_list (NULL_TREE, parm));
19685       token = cp_lexer_peek_token (parser->lexer);
19686     }
19687
19688   return params;
19689 }
19690
19691 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19692
19693 static void
19694 cp_parser_objc_interstitial_code (cp_parser* parser)
19695 {
19696   cp_token *token = cp_lexer_peek_token (parser->lexer);
19697
19698   /* If the next token is `extern' and the following token is a string
19699      literal, then we have a linkage specification.  */
19700   if (token->keyword == RID_EXTERN
19701       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19702     cp_parser_linkage_specification (parser);
19703   /* Handle #pragma, if any.  */
19704   else if (token->type == CPP_PRAGMA)
19705     cp_parser_pragma (parser, pragma_external);
19706   /* Allow stray semicolons.  */
19707   else if (token->type == CPP_SEMICOLON)
19708     cp_lexer_consume_token (parser->lexer);
19709   /* Finally, try to parse a block-declaration, or a function-definition.  */
19710   else
19711     cp_parser_block_declaration (parser, /*statement_p=*/false);
19712 }
19713
19714 /* Parse a method signature.  */
19715
19716 static tree
19717 cp_parser_objc_method_signature (cp_parser* parser)
19718 {
19719   tree rettype, kwdparms, optparms;
19720   bool ellipsis = false;
19721
19722   cp_parser_objc_method_type (parser);
19723   rettype = cp_parser_objc_typename (parser);
19724   kwdparms = cp_parser_objc_method_keyword_params (parser);
19725   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19726
19727   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19728 }
19729
19730 /* Pars an Objective-C method prototype list.  */
19731
19732 static void
19733 cp_parser_objc_method_prototype_list (cp_parser* parser)
19734 {
19735   cp_token *token = cp_lexer_peek_token (parser->lexer);
19736
19737   while (token->keyword != RID_AT_END)
19738     {
19739       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19740         {
19741           objc_add_method_declaration
19742            (cp_parser_objc_method_signature (parser));
19743           cp_parser_consume_semicolon_at_end_of_statement (parser);
19744         }
19745       else
19746         /* Allow for interspersed non-ObjC++ code.  */
19747         cp_parser_objc_interstitial_code (parser);
19748
19749       token = cp_lexer_peek_token (parser->lexer);
19750     }
19751
19752   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19753   objc_finish_interface ();
19754 }
19755
19756 /* Parse an Objective-C method definition list.  */
19757
19758 static void
19759 cp_parser_objc_method_definition_list (cp_parser* parser)
19760 {
19761   cp_token *token = cp_lexer_peek_token (parser->lexer);
19762
19763   while (token->keyword != RID_AT_END)
19764     {
19765       tree meth;
19766
19767       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19768         {
19769           push_deferring_access_checks (dk_deferred);
19770           objc_start_method_definition
19771            (cp_parser_objc_method_signature (parser));
19772
19773           /* For historical reasons, we accept an optional semicolon.  */
19774           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19775             cp_lexer_consume_token (parser->lexer);
19776
19777           perform_deferred_access_checks ();
19778           stop_deferring_access_checks ();
19779           meth = cp_parser_function_definition_after_declarator (parser,
19780                                                                  false);
19781           pop_deferring_access_checks ();
19782           objc_finish_method_definition (meth);
19783         }
19784       else
19785         /* Allow for interspersed non-ObjC++ code.  */
19786         cp_parser_objc_interstitial_code (parser);
19787
19788       token = cp_lexer_peek_token (parser->lexer);
19789     }
19790
19791   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19792   objc_finish_implementation ();
19793 }
19794
19795 /* Parse Objective-C ivars.  */
19796
19797 static void
19798 cp_parser_objc_class_ivars (cp_parser* parser)
19799 {
19800   cp_token *token = cp_lexer_peek_token (parser->lexer);
19801
19802   if (token->type != CPP_OPEN_BRACE)
19803     return;     /* No ivars specified.  */
19804
19805   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19806   token = cp_lexer_peek_token (parser->lexer);
19807
19808   while (token->type != CPP_CLOSE_BRACE)
19809     {
19810       cp_decl_specifier_seq declspecs;
19811       int decl_class_or_enum_p;
19812       tree prefix_attributes;
19813
19814       cp_parser_objc_visibility_spec (parser);
19815
19816       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19817         break;
19818
19819       cp_parser_decl_specifier_seq (parser,
19820                                     CP_PARSER_FLAGS_OPTIONAL,
19821                                     &declspecs,
19822                                     &decl_class_or_enum_p);
19823       prefix_attributes = declspecs.attributes;
19824       declspecs.attributes = NULL_TREE;
19825
19826       /* Keep going until we hit the `;' at the end of the
19827          declaration.  */
19828       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19829         {
19830           tree width = NULL_TREE, attributes, first_attribute, decl;
19831           cp_declarator *declarator = NULL;
19832           int ctor_dtor_or_conv_p;
19833
19834           /* Check for a (possibly unnamed) bitfield declaration.  */
19835           token = cp_lexer_peek_token (parser->lexer);
19836           if (token->type == CPP_COLON)
19837             goto eat_colon;
19838
19839           if (token->type == CPP_NAME
19840               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19841                   == CPP_COLON))
19842             {
19843               /* Get the name of the bitfield.  */
19844               declarator = make_id_declarator (NULL_TREE,
19845                                                cp_parser_identifier (parser),
19846                                                sfk_none);
19847
19848              eat_colon:
19849               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19850               /* Get the width of the bitfield.  */
19851               width
19852                 = cp_parser_constant_expression (parser,
19853                                                  /*allow_non_constant=*/false,
19854                                                  NULL);
19855             }
19856           else
19857             {
19858               /* Parse the declarator.  */
19859               declarator
19860                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19861                                         &ctor_dtor_or_conv_p,
19862                                         /*parenthesized_p=*/NULL,
19863                                         /*member_p=*/false);
19864             }
19865
19866           /* Look for attributes that apply to the ivar.  */
19867           attributes = cp_parser_attributes_opt (parser);
19868           /* Remember which attributes are prefix attributes and
19869              which are not.  */
19870           first_attribute = attributes;
19871           /* Combine the attributes.  */
19872           attributes = chainon (prefix_attributes, attributes);
19873
19874           if (width)
19875               /* Create the bitfield declaration.  */
19876               decl = grokbitfield (declarator, &declspecs,
19877                                    width,
19878                                    attributes);
19879           else
19880             decl = grokfield (declarator, &declspecs,
19881                               NULL_TREE, /*init_const_expr_p=*/false,
19882                               NULL_TREE, attributes);
19883
19884           /* Add the instance variable.  */
19885           objc_add_instance_variable (decl);
19886
19887           /* Reset PREFIX_ATTRIBUTES.  */
19888           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19889             attributes = TREE_CHAIN (attributes);
19890           if (attributes)
19891             TREE_CHAIN (attributes) = NULL_TREE;
19892
19893           token = cp_lexer_peek_token (parser->lexer);
19894
19895           if (token->type == CPP_COMMA)
19896             {
19897               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19898               continue;
19899             }
19900           break;
19901         }
19902
19903       cp_parser_consume_semicolon_at_end_of_statement (parser);
19904       token = cp_lexer_peek_token (parser->lexer);
19905     }
19906
19907   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19908   /* For historical reasons, we accept an optional semicolon.  */
19909   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19910     cp_lexer_consume_token (parser->lexer);
19911 }
19912
19913 /* Parse an Objective-C protocol declaration.  */
19914
19915 static void
19916 cp_parser_objc_protocol_declaration (cp_parser* parser)
19917 {
19918   tree proto, protorefs;
19919   cp_token *tok;
19920
19921   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19922   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19923     {
19924       tok = cp_lexer_peek_token (parser->lexer);
19925       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19926       goto finish;
19927     }
19928
19929   /* See if we have a forward declaration or a definition.  */
19930   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19931
19932   /* Try a forward declaration first.  */
19933   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19934     {
19935       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19936      finish:
19937       cp_parser_consume_semicolon_at_end_of_statement (parser);
19938     }
19939
19940   /* Ok, we got a full-fledged definition (or at least should).  */
19941   else
19942     {
19943       proto = cp_parser_identifier (parser);
19944       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19945       objc_start_protocol (proto, protorefs);
19946       cp_parser_objc_method_prototype_list (parser);
19947     }
19948 }
19949
19950 /* Parse an Objective-C superclass or category.  */
19951
19952 static void
19953 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19954                                                           tree *categ)
19955 {
19956   cp_token *next = cp_lexer_peek_token (parser->lexer);
19957
19958   *super = *categ = NULL_TREE;
19959   if (next->type == CPP_COLON)
19960     {
19961       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19962       *super = cp_parser_identifier (parser);
19963     }
19964   else if (next->type == CPP_OPEN_PAREN)
19965     {
19966       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19967       *categ = cp_parser_identifier (parser);
19968       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19969     }
19970 }
19971
19972 /* Parse an Objective-C class interface.  */
19973
19974 static void
19975 cp_parser_objc_class_interface (cp_parser* parser)
19976 {
19977   tree name, super, categ, protos;
19978
19979   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19980   name = cp_parser_identifier (parser);
19981   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19982   protos = cp_parser_objc_protocol_refs_opt (parser);
19983
19984   /* We have either a class or a category on our hands.  */
19985   if (categ)
19986     objc_start_category_interface (name, categ, protos);
19987   else
19988     {
19989       objc_start_class_interface (name, super, protos);
19990       /* Handle instance variable declarations, if any.  */
19991       cp_parser_objc_class_ivars (parser);
19992       objc_continue_interface ();
19993     }
19994
19995   cp_parser_objc_method_prototype_list (parser);
19996 }
19997
19998 /* Parse an Objective-C class implementation.  */
19999
20000 static void
20001 cp_parser_objc_class_implementation (cp_parser* parser)
20002 {
20003   tree name, super, categ;
20004
20005   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20006   name = cp_parser_identifier (parser);
20007   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20008
20009   /* We have either a class or a category on our hands.  */
20010   if (categ)
20011     objc_start_category_implementation (name, categ);
20012   else
20013     {
20014       objc_start_class_implementation (name, super);
20015       /* Handle instance variable declarations, if any.  */
20016       cp_parser_objc_class_ivars (parser);
20017       objc_continue_implementation ();
20018     }
20019
20020   cp_parser_objc_method_definition_list (parser);
20021 }
20022
20023 /* Consume the @end token and finish off the implementation.  */
20024
20025 static void
20026 cp_parser_objc_end_implementation (cp_parser* parser)
20027 {
20028   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20029   objc_finish_implementation ();
20030 }
20031
20032 /* Parse an Objective-C declaration.  */
20033
20034 static void
20035 cp_parser_objc_declaration (cp_parser* parser)
20036 {
20037   /* Try to figure out what kind of declaration is present.  */
20038   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20039
20040   switch (kwd->keyword)
20041     {
20042     case RID_AT_ALIAS:
20043       cp_parser_objc_alias_declaration (parser);
20044       break;
20045     case RID_AT_CLASS:
20046       cp_parser_objc_class_declaration (parser);
20047       break;
20048     case RID_AT_PROTOCOL:
20049       cp_parser_objc_protocol_declaration (parser);
20050       break;
20051     case RID_AT_INTERFACE:
20052       cp_parser_objc_class_interface (parser);
20053       break;
20054     case RID_AT_IMPLEMENTATION:
20055       cp_parser_objc_class_implementation (parser);
20056       break;
20057     case RID_AT_END:
20058       cp_parser_objc_end_implementation (parser);
20059       break;
20060     default:
20061       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20062              &kwd->location, kwd->u.value);
20063       cp_parser_skip_to_end_of_block_or_statement (parser);
20064     }
20065 }
20066
20067 /* Parse an Objective-C try-catch-finally statement.
20068
20069    objc-try-catch-finally-stmt:
20070      @try compound-statement objc-catch-clause-seq [opt]
20071        objc-finally-clause [opt]
20072
20073    objc-catch-clause-seq:
20074      objc-catch-clause objc-catch-clause-seq [opt]
20075
20076    objc-catch-clause:
20077      @catch ( exception-declaration ) compound-statement
20078
20079    objc-finally-clause
20080      @finally compound-statement
20081
20082    Returns NULL_TREE.  */
20083
20084 static tree
20085 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20086   location_t location;
20087   tree stmt;
20088
20089   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20090   location = cp_lexer_peek_token (parser->lexer)->location;
20091   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20092      node, lest it get absorbed into the surrounding block.  */
20093   stmt = push_stmt_list ();
20094   cp_parser_compound_statement (parser, NULL, false);
20095   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20096
20097   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20098     {
20099       cp_parameter_declarator *parmdecl;
20100       tree parm;
20101
20102       cp_lexer_consume_token (parser->lexer);
20103       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20104       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20105       parm = grokdeclarator (parmdecl->declarator,
20106                              &parmdecl->decl_specifiers,
20107                              PARM, /*initialized=*/0,
20108                              /*attrlist=*/NULL);
20109       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20110       objc_begin_catch_clause (parm);
20111       cp_parser_compound_statement (parser, NULL, false);
20112       objc_finish_catch_clause ();
20113     }
20114
20115   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20116     {
20117       cp_lexer_consume_token (parser->lexer);
20118       location = cp_lexer_peek_token (parser->lexer)->location;
20119       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20120          node, lest it get absorbed into the surrounding block.  */
20121       stmt = push_stmt_list ();
20122       cp_parser_compound_statement (parser, NULL, false);
20123       objc_build_finally_clause (location, pop_stmt_list (stmt));
20124     }
20125
20126   return objc_finish_try_stmt ();
20127 }
20128
20129 /* Parse an Objective-C synchronized statement.
20130
20131    objc-synchronized-stmt:
20132      @synchronized ( expression ) compound-statement
20133
20134    Returns NULL_TREE.  */
20135
20136 static tree
20137 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20138   location_t location;
20139   tree lock, stmt;
20140
20141   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20142
20143   location = cp_lexer_peek_token (parser->lexer)->location;
20144   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20145   lock = cp_parser_expression (parser, false, NULL);
20146   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20147
20148   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20149      node, lest it get absorbed into the surrounding block.  */
20150   stmt = push_stmt_list ();
20151   cp_parser_compound_statement (parser, NULL, false);
20152
20153   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20154 }
20155
20156 /* Parse an Objective-C throw statement.
20157
20158    objc-throw-stmt:
20159      @throw assignment-expression [opt] ;
20160
20161    Returns a constructed '@throw' statement.  */
20162
20163 static tree
20164 cp_parser_objc_throw_statement (cp_parser *parser) {
20165   tree expr = NULL_TREE;
20166
20167   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20168
20169   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20170     expr = cp_parser_assignment_expression (parser, false, NULL);
20171
20172   cp_parser_consume_semicolon_at_end_of_statement (parser);
20173
20174   return objc_build_throw_stmt (expr);
20175 }
20176
20177 /* Parse an Objective-C statement.  */
20178
20179 static tree
20180 cp_parser_objc_statement (cp_parser * parser) {
20181   /* Try to figure out what kind of declaration is present.  */
20182   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20183
20184   switch (kwd->keyword)
20185     {
20186     case RID_AT_TRY:
20187       return cp_parser_objc_try_catch_finally_statement (parser);
20188     case RID_AT_SYNCHRONIZED:
20189       return cp_parser_objc_synchronized_statement (parser);
20190     case RID_AT_THROW:
20191       return cp_parser_objc_throw_statement (parser);
20192     default:
20193       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20194              &kwd->location, kwd->u.value);
20195       cp_parser_skip_to_end_of_block_or_statement (parser);
20196     }
20197
20198   return error_mark_node;
20199 }
20200 \f
20201 /* OpenMP 2.5 parsing routines.  */
20202
20203 /* Returns name of the next clause.
20204    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20205    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20206    returned and the token is consumed.  */
20207
20208 static pragma_omp_clause
20209 cp_parser_omp_clause_name (cp_parser *parser)
20210 {
20211   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20212
20213   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20214     result = PRAGMA_OMP_CLAUSE_IF;
20215   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20216     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20217   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20218     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20219   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20220     {
20221       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20222       const char *p = IDENTIFIER_POINTER (id);
20223
20224       switch (p[0])
20225         {
20226         case 'c':
20227           if (!strcmp ("collapse", p))
20228             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20229           else if (!strcmp ("copyin", p))
20230             result = PRAGMA_OMP_CLAUSE_COPYIN;
20231           else if (!strcmp ("copyprivate", p))
20232             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20233           break;
20234         case 'f':
20235           if (!strcmp ("firstprivate", p))
20236             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20237           break;
20238         case 'l':
20239           if (!strcmp ("lastprivate", p))
20240             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20241           break;
20242         case 'n':
20243           if (!strcmp ("nowait", p))
20244             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20245           else if (!strcmp ("num_threads", p))
20246             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20247           break;
20248         case 'o':
20249           if (!strcmp ("ordered", p))
20250             result = PRAGMA_OMP_CLAUSE_ORDERED;
20251           break;
20252         case 'r':
20253           if (!strcmp ("reduction", p))
20254             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20255           break;
20256         case 's':
20257           if (!strcmp ("schedule", p))
20258             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20259           else if (!strcmp ("shared", p))
20260             result = PRAGMA_OMP_CLAUSE_SHARED;
20261           break;
20262         case 'u':
20263           if (!strcmp ("untied", p))
20264             result = PRAGMA_OMP_CLAUSE_UNTIED;
20265           break;
20266         }
20267     }
20268
20269   if (result != PRAGMA_OMP_CLAUSE_NONE)
20270     cp_lexer_consume_token (parser->lexer);
20271
20272   return result;
20273 }
20274
20275 /* Validate that a clause of the given type does not already exist.  */
20276
20277 static void
20278 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20279                            const char *name, location_t location)
20280 {
20281   tree c;
20282
20283   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20284     if (OMP_CLAUSE_CODE (c) == code)
20285       {
20286         error ("%Htoo many %qs clauses", &location, name);
20287         break;
20288       }
20289 }
20290
20291 /* OpenMP 2.5:
20292    variable-list:
20293      identifier
20294      variable-list , identifier
20295
20296    In addition, we match a closing parenthesis.  An opening parenthesis
20297    will have been consumed by the caller.
20298
20299    If KIND is nonzero, create the appropriate node and install the decl
20300    in OMP_CLAUSE_DECL and add the node to the head of the list.
20301
20302    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20303    return the list created.  */
20304
20305 static tree
20306 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20307                                 tree list)
20308 {
20309   cp_token *token;
20310   while (1)
20311     {
20312       tree name, decl;
20313
20314       token = cp_lexer_peek_token (parser->lexer);
20315       name = cp_parser_id_expression (parser, /*template_p=*/false,
20316                                       /*check_dependency_p=*/true,
20317                                       /*template_p=*/NULL,
20318                                       /*declarator_p=*/false,
20319                                       /*optional_p=*/false);
20320       if (name == error_mark_node)
20321         goto skip_comma;
20322
20323       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20324       if (decl == error_mark_node)
20325         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20326       else if (kind != 0)
20327         {
20328           tree u = build_omp_clause (kind);
20329           OMP_CLAUSE_DECL (u) = decl;
20330           OMP_CLAUSE_CHAIN (u) = list;
20331           list = u;
20332         }
20333       else
20334         list = tree_cons (decl, NULL_TREE, list);
20335
20336     get_comma:
20337       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20338         break;
20339       cp_lexer_consume_token (parser->lexer);
20340     }
20341
20342   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20343     {
20344       int ending;
20345
20346       /* Try to resync to an unnested comma.  Copied from
20347          cp_parser_parenthesized_expression_list.  */
20348     skip_comma:
20349       ending = cp_parser_skip_to_closing_parenthesis (parser,
20350                                                       /*recovering=*/true,
20351                                                       /*or_comma=*/true,
20352                                                       /*consume_paren=*/true);
20353       if (ending < 0)
20354         goto get_comma;
20355     }
20356
20357   return list;
20358 }
20359
20360 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20361    common case for omp clauses.  */
20362
20363 static tree
20364 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20365 {
20366   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20367     return cp_parser_omp_var_list_no_open (parser, kind, list);
20368   return list;
20369 }
20370
20371 /* OpenMP 3.0:
20372    collapse ( constant-expression ) */
20373
20374 static tree
20375 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20376 {
20377   tree c, num;
20378   location_t loc;
20379   HOST_WIDE_INT n;
20380
20381   loc = cp_lexer_peek_token (parser->lexer)->location;
20382   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20383     return list;
20384
20385   num = cp_parser_constant_expression (parser, false, NULL);
20386
20387   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20388     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20389                                            /*or_comma=*/false,
20390                                            /*consume_paren=*/true);
20391
20392   if (num == error_mark_node)
20393     return list;
20394   num = fold_non_dependent_expr (num);
20395   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20396       || !host_integerp (num, 0)
20397       || (n = tree_low_cst (num, 0)) <= 0
20398       || (int) n != n)
20399     {
20400       error ("%Hcollapse argument needs positive constant integer expression",
20401              &loc);
20402       return list;
20403     }
20404
20405   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20406   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20407   OMP_CLAUSE_CHAIN (c) = list;
20408   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20409
20410   return c;
20411 }
20412
20413 /* OpenMP 2.5:
20414    default ( shared | none ) */
20415
20416 static tree
20417 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20418 {
20419   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20420   tree c;
20421
20422   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20423     return list;
20424   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20425     {
20426       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20427       const char *p = IDENTIFIER_POINTER (id);
20428
20429       switch (p[0])
20430         {
20431         case 'n':
20432           if (strcmp ("none", p) != 0)
20433             goto invalid_kind;
20434           kind = OMP_CLAUSE_DEFAULT_NONE;
20435           break;
20436
20437         case 's':
20438           if (strcmp ("shared", p) != 0)
20439             goto invalid_kind;
20440           kind = OMP_CLAUSE_DEFAULT_SHARED;
20441           break;
20442
20443         default:
20444           goto invalid_kind;
20445         }
20446
20447       cp_lexer_consume_token (parser->lexer);
20448     }
20449   else
20450     {
20451     invalid_kind:
20452       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20453     }
20454
20455   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20456     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20457                                            /*or_comma=*/false,
20458                                            /*consume_paren=*/true);
20459
20460   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20461     return list;
20462
20463   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20464   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20465   OMP_CLAUSE_CHAIN (c) = list;
20466   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20467
20468   return c;
20469 }
20470
20471 /* OpenMP 2.5:
20472    if ( expression ) */
20473
20474 static tree
20475 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20476 {
20477   tree t, c;
20478
20479   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20480     return list;
20481
20482   t = cp_parser_condition (parser);
20483
20484   if (t == error_mark_node
20485       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20486     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20487                                            /*or_comma=*/false,
20488                                            /*consume_paren=*/true);
20489
20490   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20491
20492   c = build_omp_clause (OMP_CLAUSE_IF);
20493   OMP_CLAUSE_IF_EXPR (c) = t;
20494   OMP_CLAUSE_CHAIN (c) = list;
20495
20496   return c;
20497 }
20498
20499 /* OpenMP 2.5:
20500    nowait */
20501
20502 static tree
20503 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20504                              tree list, location_t location)
20505 {
20506   tree c;
20507
20508   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20509
20510   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20511   OMP_CLAUSE_CHAIN (c) = list;
20512   return c;
20513 }
20514
20515 /* OpenMP 2.5:
20516    num_threads ( expression ) */
20517
20518 static tree
20519 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20520                                   location_t location)
20521 {
20522   tree t, c;
20523
20524   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20525     return list;
20526
20527   t = cp_parser_expression (parser, false, NULL);
20528
20529   if (t == error_mark_node
20530       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20531     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20532                                            /*or_comma=*/false,
20533                                            /*consume_paren=*/true);
20534
20535   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20536                              "num_threads", location);
20537
20538   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20539   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20540   OMP_CLAUSE_CHAIN (c) = list;
20541
20542   return c;
20543 }
20544
20545 /* OpenMP 2.5:
20546    ordered */
20547
20548 static tree
20549 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20550                               tree list, location_t location)
20551 {
20552   tree c;
20553
20554   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20555                              "ordered", location);
20556
20557   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20558   OMP_CLAUSE_CHAIN (c) = list;
20559   return c;
20560 }
20561
20562 /* OpenMP 2.5:
20563    reduction ( reduction-operator : variable-list )
20564
20565    reduction-operator:
20566      One of: + * - & ^ | && || */
20567
20568 static tree
20569 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20570 {
20571   enum tree_code code;
20572   tree nlist, c;
20573
20574   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20575     return list;
20576
20577   switch (cp_lexer_peek_token (parser->lexer)->type)
20578     {
20579     case CPP_PLUS:
20580       code = PLUS_EXPR;
20581       break;
20582     case CPP_MULT:
20583       code = MULT_EXPR;
20584       break;
20585     case CPP_MINUS:
20586       code = MINUS_EXPR;
20587       break;
20588     case CPP_AND:
20589       code = BIT_AND_EXPR;
20590       break;
20591     case CPP_XOR:
20592       code = BIT_XOR_EXPR;
20593       break;
20594     case CPP_OR:
20595       code = BIT_IOR_EXPR;
20596       break;
20597     case CPP_AND_AND:
20598       code = TRUTH_ANDIF_EXPR;
20599       break;
20600     case CPP_OR_OR:
20601       code = TRUTH_ORIF_EXPR;
20602       break;
20603     default:
20604       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20605                                "%<|%>, %<&&%>, or %<||%>");
20606     resync_fail:
20607       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20608                                              /*or_comma=*/false,
20609                                              /*consume_paren=*/true);
20610       return list;
20611     }
20612   cp_lexer_consume_token (parser->lexer);
20613
20614   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20615     goto resync_fail;
20616
20617   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20618   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20619     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20620
20621   return nlist;
20622 }
20623
20624 /* OpenMP 2.5:
20625    schedule ( schedule-kind )
20626    schedule ( schedule-kind , expression )
20627
20628    schedule-kind:
20629      static | dynamic | guided | runtime | auto  */
20630
20631 static tree
20632 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20633 {
20634   tree c, t;
20635
20636   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20637     return list;
20638
20639   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20640
20641   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20642     {
20643       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20644       const char *p = IDENTIFIER_POINTER (id);
20645
20646       switch (p[0])
20647         {
20648         case 'd':
20649           if (strcmp ("dynamic", p) != 0)
20650             goto invalid_kind;
20651           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20652           break;
20653
20654         case 'g':
20655           if (strcmp ("guided", p) != 0)
20656             goto invalid_kind;
20657           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20658           break;
20659
20660         case 'r':
20661           if (strcmp ("runtime", p) != 0)
20662             goto invalid_kind;
20663           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20664           break;
20665
20666         default:
20667           goto invalid_kind;
20668         }
20669     }
20670   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20671     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20672   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20673     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20674   else
20675     goto invalid_kind;
20676   cp_lexer_consume_token (parser->lexer);
20677
20678   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20679     {
20680       cp_token *token;
20681       cp_lexer_consume_token (parser->lexer);
20682
20683       token = cp_lexer_peek_token (parser->lexer);
20684       t = cp_parser_assignment_expression (parser, false, NULL);
20685
20686       if (t == error_mark_node)
20687         goto resync_fail;
20688       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20689         error ("%Hschedule %<runtime%> does not take "
20690                "a %<chunk_size%> parameter", &token->location);
20691       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20692         error ("%Hschedule %<auto%> does not take "
20693                "a %<chunk_size%> parameter", &token->location);
20694       else
20695         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20696
20697       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20698         goto resync_fail;
20699     }
20700   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20701     goto resync_fail;
20702
20703   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20704   OMP_CLAUSE_CHAIN (c) = list;
20705   return c;
20706
20707  invalid_kind:
20708   cp_parser_error (parser, "invalid schedule kind");
20709  resync_fail:
20710   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20711                                          /*or_comma=*/false,
20712                                          /*consume_paren=*/true);
20713   return list;
20714 }
20715
20716 /* OpenMP 3.0:
20717    untied */
20718
20719 static tree
20720 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20721                              tree list, location_t location)
20722 {
20723   tree c;
20724
20725   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20726
20727   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20728   OMP_CLAUSE_CHAIN (c) = list;
20729   return c;
20730 }
20731
20732 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20733    is a bitmask in MASK.  Return the list of clauses found; the result
20734    of clause default goes in *pdefault.  */
20735
20736 static tree
20737 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20738                            const char *where, cp_token *pragma_tok)
20739 {
20740   tree clauses = NULL;
20741   bool first = true;
20742   cp_token *token = NULL;
20743
20744   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20745     {
20746       pragma_omp_clause c_kind;
20747       const char *c_name;
20748       tree prev = clauses;
20749
20750       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20751         cp_lexer_consume_token (parser->lexer);
20752
20753       token = cp_lexer_peek_token (parser->lexer);
20754       c_kind = cp_parser_omp_clause_name (parser);
20755       first = false;
20756
20757       switch (c_kind)
20758         {
20759         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20760           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20761                                                    token->location);
20762           c_name = "collapse";
20763           break;
20764         case PRAGMA_OMP_CLAUSE_COPYIN:
20765           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20766           c_name = "copyin";
20767           break;
20768         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20769           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20770                                             clauses);
20771           c_name = "copyprivate";
20772           break;
20773         case PRAGMA_OMP_CLAUSE_DEFAULT:
20774           clauses = cp_parser_omp_clause_default (parser, clauses,
20775                                                   token->location);
20776           c_name = "default";
20777           break;
20778         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20779           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20780                                             clauses);
20781           c_name = "firstprivate";
20782           break;
20783         case PRAGMA_OMP_CLAUSE_IF:
20784           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20785           c_name = "if";
20786           break;
20787         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20788           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20789                                             clauses);
20790           c_name = "lastprivate";
20791           break;
20792         case PRAGMA_OMP_CLAUSE_NOWAIT:
20793           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20794           c_name = "nowait";
20795           break;
20796         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20797           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20798                                                       token->location);
20799           c_name = "num_threads";
20800           break;
20801         case PRAGMA_OMP_CLAUSE_ORDERED:
20802           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20803                                                   token->location);
20804           c_name = "ordered";
20805           break;
20806         case PRAGMA_OMP_CLAUSE_PRIVATE:
20807           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20808                                             clauses);
20809           c_name = "private";
20810           break;
20811         case PRAGMA_OMP_CLAUSE_REDUCTION:
20812           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20813           c_name = "reduction";
20814           break;
20815         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20816           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20817                                                    token->location);
20818           c_name = "schedule";
20819           break;
20820         case PRAGMA_OMP_CLAUSE_SHARED:
20821           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20822                                             clauses);
20823           c_name = "shared";
20824           break;
20825         case PRAGMA_OMP_CLAUSE_UNTIED:
20826           clauses = cp_parser_omp_clause_untied (parser, clauses,
20827                                                  token->location);
20828           c_name = "nowait";
20829           break;
20830         default:
20831           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20832           goto saw_error;
20833         }
20834
20835       if (((mask >> c_kind) & 1) == 0)
20836         {
20837           /* Remove the invalid clause(s) from the list to avoid
20838              confusing the rest of the compiler.  */
20839           clauses = prev;
20840           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20841         }
20842     }
20843  saw_error:
20844   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20845   return finish_omp_clauses (clauses);
20846 }
20847
20848 /* OpenMP 2.5:
20849    structured-block:
20850      statement
20851
20852    In practice, we're also interested in adding the statement to an
20853    outer node.  So it is convenient if we work around the fact that
20854    cp_parser_statement calls add_stmt.  */
20855
20856 static unsigned
20857 cp_parser_begin_omp_structured_block (cp_parser *parser)
20858 {
20859   unsigned save = parser->in_statement;
20860
20861   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20862      This preserves the "not within loop or switch" style error messages
20863      for nonsense cases like
20864         void foo() {
20865         #pragma omp single
20866           break;
20867         }
20868   */
20869   if (parser->in_statement)
20870     parser->in_statement = IN_OMP_BLOCK;
20871
20872   return save;
20873 }
20874
20875 static void
20876 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20877 {
20878   parser->in_statement = save;
20879 }
20880
20881 static tree
20882 cp_parser_omp_structured_block (cp_parser *parser)
20883 {
20884   tree stmt = begin_omp_structured_block ();
20885   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20886
20887   cp_parser_statement (parser, NULL_TREE, false, NULL);
20888
20889   cp_parser_end_omp_structured_block (parser, save);
20890   return finish_omp_structured_block (stmt);
20891 }
20892
20893 /* OpenMP 2.5:
20894    # pragma omp atomic new-line
20895      expression-stmt
20896
20897    expression-stmt:
20898      x binop= expr | x++ | ++x | x-- | --x
20899    binop:
20900      +, *, -, /, &, ^, |, <<, >>
20901
20902   where x is an lvalue expression with scalar type.  */
20903
20904 static void
20905 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20906 {
20907   tree lhs, rhs;
20908   enum tree_code code;
20909
20910   cp_parser_require_pragma_eol (parser, pragma_tok);
20911
20912   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20913                                     /*cast_p=*/false, NULL);
20914   switch (TREE_CODE (lhs))
20915     {
20916     case ERROR_MARK:
20917       goto saw_error;
20918
20919     case PREINCREMENT_EXPR:
20920     case POSTINCREMENT_EXPR:
20921       lhs = TREE_OPERAND (lhs, 0);
20922       code = PLUS_EXPR;
20923       rhs = integer_one_node;
20924       break;
20925
20926     case PREDECREMENT_EXPR:
20927     case POSTDECREMENT_EXPR:
20928       lhs = TREE_OPERAND (lhs, 0);
20929       code = MINUS_EXPR;
20930       rhs = integer_one_node;
20931       break;
20932
20933     default:
20934       switch (cp_lexer_peek_token (parser->lexer)->type)
20935         {
20936         case CPP_MULT_EQ:
20937           code = MULT_EXPR;
20938           break;
20939         case CPP_DIV_EQ:
20940           code = TRUNC_DIV_EXPR;
20941           break;
20942         case CPP_PLUS_EQ:
20943           code = PLUS_EXPR;
20944           break;
20945         case CPP_MINUS_EQ:
20946           code = MINUS_EXPR;
20947           break;
20948         case CPP_LSHIFT_EQ:
20949           code = LSHIFT_EXPR;
20950           break;
20951         case CPP_RSHIFT_EQ:
20952           code = RSHIFT_EXPR;
20953           break;
20954         case CPP_AND_EQ:
20955           code = BIT_AND_EXPR;
20956           break;
20957         case CPP_OR_EQ:
20958           code = BIT_IOR_EXPR;
20959           break;
20960         case CPP_XOR_EQ:
20961           code = BIT_XOR_EXPR;
20962           break;
20963         default:
20964           cp_parser_error (parser,
20965                            "invalid operator for %<#pragma omp atomic%>");
20966           goto saw_error;
20967         }
20968       cp_lexer_consume_token (parser->lexer);
20969
20970       rhs = cp_parser_expression (parser, false, NULL);
20971       if (rhs == error_mark_node)
20972         goto saw_error;
20973       break;
20974     }
20975   finish_omp_atomic (code, lhs, rhs);
20976   cp_parser_consume_semicolon_at_end_of_statement (parser);
20977   return;
20978
20979  saw_error:
20980   cp_parser_skip_to_end_of_block_or_statement (parser);
20981 }
20982
20983
20984 /* OpenMP 2.5:
20985    # pragma omp barrier new-line  */
20986
20987 static void
20988 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20989 {
20990   cp_parser_require_pragma_eol (parser, pragma_tok);
20991   finish_omp_barrier ();
20992 }
20993
20994 /* OpenMP 2.5:
20995    # pragma omp critical [(name)] new-line
20996      structured-block  */
20997
20998 static tree
20999 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21000 {
21001   tree stmt, name = NULL;
21002
21003   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21004     {
21005       cp_lexer_consume_token (parser->lexer);
21006
21007       name = cp_parser_identifier (parser);
21008
21009       if (name == error_mark_node
21010           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21011         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21012                                                /*or_comma=*/false,
21013                                                /*consume_paren=*/true);
21014       if (name == error_mark_node)
21015         name = NULL;
21016     }
21017   cp_parser_require_pragma_eol (parser, pragma_tok);
21018
21019   stmt = cp_parser_omp_structured_block (parser);
21020   return c_finish_omp_critical (stmt, name);
21021 }
21022
21023 /* OpenMP 2.5:
21024    # pragma omp flush flush-vars[opt] new-line
21025
21026    flush-vars:
21027      ( variable-list ) */
21028
21029 static void
21030 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21031 {
21032   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21033     (void) cp_parser_omp_var_list (parser, 0, NULL);
21034   cp_parser_require_pragma_eol (parser, pragma_tok);
21035
21036   finish_omp_flush ();
21037 }
21038
21039 /* Helper function, to parse omp for increment expression.  */
21040
21041 static tree
21042 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21043 {
21044   tree cond = cp_parser_binary_expression (parser, false, true,
21045                                            PREC_NOT_OPERATOR, NULL);
21046   bool overloaded_p;
21047
21048   if (cond == error_mark_node
21049       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21050     {
21051       cp_parser_skip_to_end_of_statement (parser);
21052       return error_mark_node;
21053     }
21054
21055   switch (TREE_CODE (cond))
21056     {
21057     case GT_EXPR:
21058     case GE_EXPR:
21059     case LT_EXPR:
21060     case LE_EXPR:
21061       break;
21062     default:
21063       return error_mark_node;
21064     }
21065
21066   /* If decl is an iterator, preserve LHS and RHS of the relational
21067      expr until finish_omp_for.  */
21068   if (decl
21069       && (type_dependent_expression_p (decl)
21070           || CLASS_TYPE_P (TREE_TYPE (decl))))
21071     return cond;
21072
21073   return build_x_binary_op (TREE_CODE (cond),
21074                             TREE_OPERAND (cond, 0), ERROR_MARK,
21075                             TREE_OPERAND (cond, 1), ERROR_MARK,
21076                             &overloaded_p, tf_warning_or_error);
21077 }
21078
21079 /* Helper function, to parse omp for increment expression.  */
21080
21081 static tree
21082 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21083 {
21084   cp_token *token = cp_lexer_peek_token (parser->lexer);
21085   enum tree_code op;
21086   tree lhs, rhs;
21087   cp_id_kind idk;
21088   bool decl_first;
21089
21090   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21091     {
21092       op = (token->type == CPP_PLUS_PLUS
21093             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21094       cp_lexer_consume_token (parser->lexer);
21095       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21096       if (lhs != decl)
21097         return error_mark_node;
21098       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21099     }
21100
21101   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21102   if (lhs != decl)
21103     return error_mark_node;
21104
21105   token = cp_lexer_peek_token (parser->lexer);
21106   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21107     {
21108       op = (token->type == CPP_PLUS_PLUS
21109             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21110       cp_lexer_consume_token (parser->lexer);
21111       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21112     }
21113
21114   op = cp_parser_assignment_operator_opt (parser);
21115   if (op == ERROR_MARK)
21116     return error_mark_node;
21117
21118   if (op != NOP_EXPR)
21119     {
21120       rhs = cp_parser_assignment_expression (parser, false, NULL);
21121       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21122       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21123     }
21124
21125   lhs = cp_parser_binary_expression (parser, false, false,
21126                                      PREC_ADDITIVE_EXPRESSION, NULL);
21127   token = cp_lexer_peek_token (parser->lexer);
21128   decl_first = lhs == decl;
21129   if (decl_first)
21130     lhs = NULL_TREE;
21131   if (token->type != CPP_PLUS
21132       && token->type != CPP_MINUS)
21133     return error_mark_node;
21134
21135   do
21136     {
21137       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21138       cp_lexer_consume_token (parser->lexer);
21139       rhs = cp_parser_binary_expression (parser, false, false,
21140                                          PREC_ADDITIVE_EXPRESSION, NULL);
21141       token = cp_lexer_peek_token (parser->lexer);
21142       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21143         {
21144           if (lhs == NULL_TREE)
21145             {
21146               if (op == PLUS_EXPR)
21147                 lhs = rhs;
21148               else
21149                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21150             }
21151           else
21152             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21153                                      NULL, tf_warning_or_error);
21154         }
21155     }
21156   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21157
21158   if (!decl_first)
21159     {
21160       if (rhs != decl || op == MINUS_EXPR)
21161         return error_mark_node;
21162       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21163     }
21164   else
21165     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21166
21167   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21168 }
21169
21170 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21171
21172 static tree
21173 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21174 {
21175   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21176   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21177   tree this_pre_body, cl;
21178   location_t loc_first;
21179   bool collapse_err = false;
21180   int i, collapse = 1, nbraces = 0;
21181
21182   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21183     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21184       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21185
21186   gcc_assert (collapse >= 1);
21187
21188   declv = make_tree_vec (collapse);
21189   initv = make_tree_vec (collapse);
21190   condv = make_tree_vec (collapse);
21191   incrv = make_tree_vec (collapse);
21192
21193   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21194
21195   for (i = 0; i < collapse; i++)
21196     {
21197       int bracecount = 0;
21198       bool add_private_clause = false;
21199       location_t loc;
21200
21201       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21202         {
21203           cp_parser_error (parser, "for statement expected");
21204           return NULL;
21205         }
21206       loc = cp_lexer_consume_token (parser->lexer)->location;
21207
21208       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21209         return NULL;
21210
21211       init = decl = real_decl = NULL;
21212       this_pre_body = push_stmt_list ();
21213       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21214         {
21215           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21216
21217              init-expr:
21218                        var = lb
21219                        integer-type var = lb
21220                        random-access-iterator-type var = lb
21221                        pointer-type var = lb
21222           */
21223           cp_decl_specifier_seq type_specifiers;
21224
21225           /* First, try to parse as an initialized declaration.  See
21226              cp_parser_condition, from whence the bulk of this is copied.  */
21227
21228           cp_parser_parse_tentatively (parser);
21229           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21230                                         &type_specifiers);
21231           if (cp_parser_parse_definitely (parser))
21232             {
21233               /* If parsing a type specifier seq succeeded, then this
21234                  MUST be a initialized declaration.  */
21235               tree asm_specification, attributes;
21236               cp_declarator *declarator;
21237
21238               declarator = cp_parser_declarator (parser,
21239                                                  CP_PARSER_DECLARATOR_NAMED,
21240                                                  /*ctor_dtor_or_conv_p=*/NULL,
21241                                                  /*parenthesized_p=*/NULL,
21242                                                  /*member_p=*/false);
21243               attributes = cp_parser_attributes_opt (parser);
21244               asm_specification = cp_parser_asm_specification_opt (parser);
21245
21246               if (declarator == cp_error_declarator) 
21247                 cp_parser_skip_to_end_of_statement (parser);
21248
21249               else 
21250                 {
21251                   tree pushed_scope, auto_node;
21252
21253                   decl = start_decl (declarator, &type_specifiers,
21254                                      SD_INITIALIZED, attributes,
21255                                      /*prefix_attributes=*/NULL_TREE,
21256                                      &pushed_scope);
21257
21258                   auto_node = type_uses_auto (TREE_TYPE (decl));
21259                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21260                     {
21261                       if (cp_lexer_next_token_is (parser->lexer, 
21262                                                   CPP_OPEN_PAREN))
21263                         error ("parenthesized initialization is not allowed in "
21264                                "OpenMP %<for%> loop");
21265                       else
21266                         /* Trigger an error.  */
21267                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21268
21269                       init = error_mark_node;
21270                       cp_parser_skip_to_end_of_statement (parser);
21271                     }
21272                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21273                            || type_dependent_expression_p (decl)
21274                            || auto_node)
21275                     {
21276                       bool is_direct_init, is_non_constant_init;
21277
21278                       init = cp_parser_initializer (parser,
21279                                                     &is_direct_init,
21280                                                     &is_non_constant_init);
21281
21282                       if (auto_node && describable_type (init))
21283                         {
21284                           TREE_TYPE (decl)
21285                             = do_auto_deduction (TREE_TYPE (decl), init,
21286                                                  auto_node);
21287
21288                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21289                               && !type_dependent_expression_p (decl))
21290                             goto non_class;
21291                         }
21292                       
21293                       cp_finish_decl (decl, init, !is_non_constant_init,
21294                                       asm_specification,
21295                                       LOOKUP_ONLYCONVERTING);
21296                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21297                         {
21298                           for_block
21299                             = tree_cons (NULL, this_pre_body, for_block);
21300                           init = NULL_TREE;
21301                         }
21302                       else
21303                         init = pop_stmt_list (this_pre_body);
21304                       this_pre_body = NULL_TREE;
21305                     }
21306                   else
21307                     {
21308                       /* Consume '='.  */
21309                       cp_lexer_consume_token (parser->lexer);
21310                       init = cp_parser_assignment_expression (parser, false, NULL);
21311
21312                     non_class:
21313                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21314                         init = error_mark_node;
21315                       else
21316                         cp_finish_decl (decl, NULL_TREE,
21317                                         /*init_const_expr_p=*/false,
21318                                         asm_specification,
21319                                         LOOKUP_ONLYCONVERTING);
21320                     }
21321
21322                   if (pushed_scope)
21323                     pop_scope (pushed_scope);
21324                 }
21325             }
21326           else 
21327             {
21328               cp_id_kind idk;
21329               /* If parsing a type specifier sequence failed, then
21330                  this MUST be a simple expression.  */
21331               cp_parser_parse_tentatively (parser);
21332               decl = cp_parser_primary_expression (parser, false, false,
21333                                                    false, &idk);
21334               if (!cp_parser_error_occurred (parser)
21335                   && decl
21336                   && DECL_P (decl)
21337                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21338                 {
21339                   tree rhs;
21340
21341                   cp_parser_parse_definitely (parser);
21342                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21343                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21344                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21345                                                          rhs,
21346                                                          tf_warning_or_error));
21347                   add_private_clause = true;
21348                 }
21349               else
21350                 {
21351                   decl = NULL;
21352                   cp_parser_abort_tentative_parse (parser);
21353                   init = cp_parser_expression (parser, false, NULL);
21354                   if (init)
21355                     {
21356                       if (TREE_CODE (init) == MODIFY_EXPR
21357                           || TREE_CODE (init) == MODOP_EXPR)
21358                         real_decl = TREE_OPERAND (init, 0);
21359                     }
21360                 }
21361             }
21362         }
21363       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21364       if (this_pre_body)
21365         {
21366           this_pre_body = pop_stmt_list (this_pre_body);
21367           if (pre_body)
21368             {
21369               tree t = pre_body;
21370               pre_body = push_stmt_list ();
21371               add_stmt (t);
21372               add_stmt (this_pre_body);
21373               pre_body = pop_stmt_list (pre_body);
21374             }
21375           else
21376             pre_body = this_pre_body;
21377         }
21378
21379       if (decl)
21380         real_decl = decl;
21381       if (par_clauses != NULL && real_decl != NULL_TREE)
21382         {
21383           tree *c;
21384           for (c = par_clauses; *c ; )
21385             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21386                 && OMP_CLAUSE_DECL (*c) == real_decl)
21387               {
21388                 error ("%Hiteration variable %qD should not be firstprivate",
21389                        &loc, real_decl);
21390                 *c = OMP_CLAUSE_CHAIN (*c);
21391               }
21392             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21393                      && OMP_CLAUSE_DECL (*c) == real_decl)
21394               {
21395                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21396                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21397                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21398                 OMP_CLAUSE_DECL (l) = real_decl;
21399                 OMP_CLAUSE_CHAIN (l) = clauses;
21400                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21401                 clauses = l;
21402                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21403                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21404                 add_private_clause = false;
21405               }
21406             else
21407               {
21408                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21409                     && OMP_CLAUSE_DECL (*c) == real_decl)
21410                   add_private_clause = false;
21411                 c = &OMP_CLAUSE_CHAIN (*c);
21412               }
21413         }
21414
21415       if (add_private_clause)
21416         {
21417           tree c;
21418           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21419             {
21420               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21421                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21422                   && OMP_CLAUSE_DECL (c) == decl)
21423                 break;
21424               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21425                        && OMP_CLAUSE_DECL (c) == decl)
21426                 error ("%Hiteration variable %qD should not be firstprivate",
21427                        &loc, decl);
21428               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21429                        && OMP_CLAUSE_DECL (c) == decl)
21430                 error ("%Hiteration variable %qD should not be reduction",
21431                        &loc, decl);
21432             }
21433           if (c == NULL)
21434             {
21435               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21436               OMP_CLAUSE_DECL (c) = decl;
21437               c = finish_omp_clauses (c);
21438               if (c)
21439                 {
21440                   OMP_CLAUSE_CHAIN (c) = clauses;
21441                   clauses = c;
21442                 }
21443             }
21444         }
21445
21446       cond = NULL;
21447       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21448         cond = cp_parser_omp_for_cond (parser, decl);
21449       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21450
21451       incr = NULL;
21452       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21453         {
21454           /* If decl is an iterator, preserve the operator on decl
21455              until finish_omp_for.  */
21456           if (decl
21457               && (type_dependent_expression_p (decl)
21458                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21459             incr = cp_parser_omp_for_incr (parser, decl);
21460           else
21461             incr = cp_parser_expression (parser, false, NULL);
21462         }
21463
21464       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21465         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21466                                                /*or_comma=*/false,
21467                                                /*consume_paren=*/true);
21468
21469       TREE_VEC_ELT (declv, i) = decl;
21470       TREE_VEC_ELT (initv, i) = init;
21471       TREE_VEC_ELT (condv, i) = cond;
21472       TREE_VEC_ELT (incrv, i) = incr;
21473
21474       if (i == collapse - 1)
21475         break;
21476
21477       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21478          in between the collapsed for loops to be still considered perfectly
21479          nested.  Hopefully the final version clarifies this.
21480          For now handle (multiple) {'s and empty statements.  */
21481       cp_parser_parse_tentatively (parser);
21482       do
21483         {
21484           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21485             break;
21486           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21487             {
21488               cp_lexer_consume_token (parser->lexer);
21489               bracecount++;
21490             }
21491           else if (bracecount
21492                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21493             cp_lexer_consume_token (parser->lexer);
21494           else
21495             {
21496               loc = cp_lexer_peek_token (parser->lexer)->location;
21497               error ("%Hnot enough collapsed for loops", &loc);
21498               collapse_err = true;
21499               cp_parser_abort_tentative_parse (parser);
21500               declv = NULL_TREE;
21501               break;
21502             }
21503         }
21504       while (1);
21505
21506       if (declv)
21507         {
21508           cp_parser_parse_definitely (parser);
21509           nbraces += bracecount;
21510         }
21511     }
21512
21513   /* Note that we saved the original contents of this flag when we entered
21514      the structured block, and so we don't need to re-save it here.  */
21515   parser->in_statement = IN_OMP_FOR;
21516
21517   /* Note that the grammar doesn't call for a structured block here,
21518      though the loop as a whole is a structured block.  */
21519   body = push_stmt_list ();
21520   cp_parser_statement (parser, NULL_TREE, false, NULL);
21521   body = pop_stmt_list (body);
21522
21523   if (declv == NULL_TREE)
21524     ret = NULL_TREE;
21525   else
21526     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21527                           pre_body, clauses);
21528
21529   while (nbraces)
21530     {
21531       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21532         {
21533           cp_lexer_consume_token (parser->lexer);
21534           nbraces--;
21535         }
21536       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21537         cp_lexer_consume_token (parser->lexer);
21538       else
21539         {
21540           if (!collapse_err)
21541             {
21542               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21543               error ("%Hcollapsed loops not perfectly nested", &loc);
21544             }
21545           collapse_err = true;
21546           cp_parser_statement_seq_opt (parser, NULL);
21547           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21548         }
21549     }
21550
21551   while (for_block)
21552     {
21553       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21554       for_block = TREE_CHAIN (for_block);
21555     }
21556
21557   return ret;
21558 }
21559
21560 /* OpenMP 2.5:
21561    #pragma omp for for-clause[optseq] new-line
21562      for-loop  */
21563
21564 #define OMP_FOR_CLAUSE_MASK                             \
21565         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21566         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21567         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21568         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21569         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21570         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21571         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21572         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21573
21574 static tree
21575 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21576 {
21577   tree clauses, sb, ret;
21578   unsigned int save;
21579
21580   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21581                                        "#pragma omp for", pragma_tok);
21582
21583   sb = begin_omp_structured_block ();
21584   save = cp_parser_begin_omp_structured_block (parser);
21585
21586   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21587
21588   cp_parser_end_omp_structured_block (parser, save);
21589   add_stmt (finish_omp_structured_block (sb));
21590
21591   return ret;
21592 }
21593
21594 /* OpenMP 2.5:
21595    # pragma omp master new-line
21596      structured-block  */
21597
21598 static tree
21599 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21600 {
21601   cp_parser_require_pragma_eol (parser, pragma_tok);
21602   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21603 }
21604
21605 /* OpenMP 2.5:
21606    # pragma omp ordered new-line
21607      structured-block  */
21608
21609 static tree
21610 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21611 {
21612   cp_parser_require_pragma_eol (parser, pragma_tok);
21613   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21614 }
21615
21616 /* OpenMP 2.5:
21617
21618    section-scope:
21619      { section-sequence }
21620
21621    section-sequence:
21622      section-directive[opt] structured-block
21623      section-sequence section-directive structured-block  */
21624
21625 static tree
21626 cp_parser_omp_sections_scope (cp_parser *parser)
21627 {
21628   tree stmt, substmt;
21629   bool error_suppress = false;
21630   cp_token *tok;
21631
21632   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21633     return NULL_TREE;
21634
21635   stmt = push_stmt_list ();
21636
21637   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21638     {
21639       unsigned save;
21640
21641       substmt = begin_omp_structured_block ();
21642       save = cp_parser_begin_omp_structured_block (parser);
21643
21644       while (1)
21645         {
21646           cp_parser_statement (parser, NULL_TREE, false, NULL);
21647
21648           tok = cp_lexer_peek_token (parser->lexer);
21649           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21650             break;
21651           if (tok->type == CPP_CLOSE_BRACE)
21652             break;
21653           if (tok->type == CPP_EOF)
21654             break;
21655         }
21656
21657       cp_parser_end_omp_structured_block (parser, save);
21658       substmt = finish_omp_structured_block (substmt);
21659       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21660       add_stmt (substmt);
21661     }
21662
21663   while (1)
21664     {
21665       tok = cp_lexer_peek_token (parser->lexer);
21666       if (tok->type == CPP_CLOSE_BRACE)
21667         break;
21668       if (tok->type == CPP_EOF)
21669         break;
21670
21671       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21672         {
21673           cp_lexer_consume_token (parser->lexer);
21674           cp_parser_require_pragma_eol (parser, tok);
21675           error_suppress = false;
21676         }
21677       else if (!error_suppress)
21678         {
21679           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21680           error_suppress = true;
21681         }
21682
21683       substmt = cp_parser_omp_structured_block (parser);
21684       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21685       add_stmt (substmt);
21686     }
21687   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21688
21689   substmt = pop_stmt_list (stmt);
21690
21691   stmt = make_node (OMP_SECTIONS);
21692   TREE_TYPE (stmt) = void_type_node;
21693   OMP_SECTIONS_BODY (stmt) = substmt;
21694
21695   add_stmt (stmt);
21696   return stmt;
21697 }
21698
21699 /* OpenMP 2.5:
21700    # pragma omp sections sections-clause[optseq] newline
21701      sections-scope  */
21702
21703 #define OMP_SECTIONS_CLAUSE_MASK                        \
21704         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21705         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21706         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21707         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21708         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21709
21710 static tree
21711 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21712 {
21713   tree clauses, ret;
21714
21715   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21716                                        "#pragma omp sections", pragma_tok);
21717
21718   ret = cp_parser_omp_sections_scope (parser);
21719   if (ret)
21720     OMP_SECTIONS_CLAUSES (ret) = clauses;
21721
21722   return ret;
21723 }
21724
21725 /* OpenMP 2.5:
21726    # pragma parallel parallel-clause new-line
21727    # pragma parallel for parallel-for-clause new-line
21728    # pragma parallel sections parallel-sections-clause new-line  */
21729
21730 #define OMP_PARALLEL_CLAUSE_MASK                        \
21731         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21732         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21733         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21734         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21735         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21736         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21737         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21738         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21739
21740 static tree
21741 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21742 {
21743   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21744   const char *p_name = "#pragma omp parallel";
21745   tree stmt, clauses, par_clause, ws_clause, block;
21746   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21747   unsigned int save;
21748
21749   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21750     {
21751       cp_lexer_consume_token (parser->lexer);
21752       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21753       p_name = "#pragma omp parallel for";
21754       mask |= OMP_FOR_CLAUSE_MASK;
21755       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21756     }
21757   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21758     {
21759       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21760       const char *p = IDENTIFIER_POINTER (id);
21761       if (strcmp (p, "sections") == 0)
21762         {
21763           cp_lexer_consume_token (parser->lexer);
21764           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21765           p_name = "#pragma omp parallel sections";
21766           mask |= OMP_SECTIONS_CLAUSE_MASK;
21767           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21768         }
21769     }
21770
21771   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21772   block = begin_omp_parallel ();
21773   save = cp_parser_begin_omp_structured_block (parser);
21774
21775   switch (p_kind)
21776     {
21777     case PRAGMA_OMP_PARALLEL:
21778       cp_parser_statement (parser, NULL_TREE, false, NULL);
21779       par_clause = clauses;
21780       break;
21781
21782     case PRAGMA_OMP_PARALLEL_FOR:
21783       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21784       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21785       break;
21786
21787     case PRAGMA_OMP_PARALLEL_SECTIONS:
21788       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21789       stmt = cp_parser_omp_sections_scope (parser);
21790       if (stmt)
21791         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21792       break;
21793
21794     default:
21795       gcc_unreachable ();
21796     }
21797
21798   cp_parser_end_omp_structured_block (parser, save);
21799   stmt = finish_omp_parallel (par_clause, block);
21800   if (p_kind != PRAGMA_OMP_PARALLEL)
21801     OMP_PARALLEL_COMBINED (stmt) = 1;
21802   return stmt;
21803 }
21804
21805 /* OpenMP 2.5:
21806    # pragma omp single single-clause[optseq] new-line
21807      structured-block  */
21808
21809 #define OMP_SINGLE_CLAUSE_MASK                          \
21810         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21811         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21812         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21813         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21814
21815 static tree
21816 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21817 {
21818   tree stmt = make_node (OMP_SINGLE);
21819   TREE_TYPE (stmt) = void_type_node;
21820
21821   OMP_SINGLE_CLAUSES (stmt)
21822     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21823                                  "#pragma omp single", pragma_tok);
21824   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21825
21826   return add_stmt (stmt);
21827 }
21828
21829 /* OpenMP 3.0:
21830    # pragma omp task task-clause[optseq] new-line
21831      structured-block  */
21832
21833 #define OMP_TASK_CLAUSE_MASK                            \
21834         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21835         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21836         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21837         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21838         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21839         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21840
21841 static tree
21842 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21843 {
21844   tree clauses, block;
21845   unsigned int save;
21846
21847   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21848                                        "#pragma omp task", pragma_tok);
21849   block = begin_omp_task ();
21850   save = cp_parser_begin_omp_structured_block (parser);
21851   cp_parser_statement (parser, NULL_TREE, false, NULL);
21852   cp_parser_end_omp_structured_block (parser, save);
21853   return finish_omp_task (clauses, block);
21854 }
21855
21856 /* OpenMP 3.0:
21857    # pragma omp taskwait new-line  */
21858
21859 static void
21860 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21861 {
21862   cp_parser_require_pragma_eol (parser, pragma_tok);
21863   finish_omp_taskwait ();
21864 }
21865
21866 /* OpenMP 2.5:
21867    # pragma omp threadprivate (variable-list) */
21868
21869 static void
21870 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21871 {
21872   tree vars;
21873
21874   vars = cp_parser_omp_var_list (parser, 0, NULL);
21875   cp_parser_require_pragma_eol (parser, pragma_tok);
21876
21877   finish_omp_threadprivate (vars);
21878 }
21879
21880 /* Main entry point to OpenMP statement pragmas.  */
21881
21882 static void
21883 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21884 {
21885   tree stmt;
21886
21887   switch (pragma_tok->pragma_kind)
21888     {
21889     case PRAGMA_OMP_ATOMIC:
21890       cp_parser_omp_atomic (parser, pragma_tok);
21891       return;
21892     case PRAGMA_OMP_CRITICAL:
21893       stmt = cp_parser_omp_critical (parser, pragma_tok);
21894       break;
21895     case PRAGMA_OMP_FOR:
21896       stmt = cp_parser_omp_for (parser, pragma_tok);
21897       break;
21898     case PRAGMA_OMP_MASTER:
21899       stmt = cp_parser_omp_master (parser, pragma_tok);
21900       break;
21901     case PRAGMA_OMP_ORDERED:
21902       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21903       break;
21904     case PRAGMA_OMP_PARALLEL:
21905       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21906       break;
21907     case PRAGMA_OMP_SECTIONS:
21908       stmt = cp_parser_omp_sections (parser, pragma_tok);
21909       break;
21910     case PRAGMA_OMP_SINGLE:
21911       stmt = cp_parser_omp_single (parser, pragma_tok);
21912       break;
21913     case PRAGMA_OMP_TASK:
21914       stmt = cp_parser_omp_task (parser, pragma_tok);
21915       break;
21916     default:
21917       gcc_unreachable ();
21918     }
21919
21920   if (stmt)
21921     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21922 }
21923 \f
21924 /* The parser.  */
21925
21926 static GTY (()) cp_parser *the_parser;
21927
21928 \f
21929 /* Special handling for the first token or line in the file.  The first
21930    thing in the file might be #pragma GCC pch_preprocess, which loads a
21931    PCH file, which is a GC collection point.  So we need to handle this
21932    first pragma without benefit of an existing lexer structure.
21933
21934    Always returns one token to the caller in *FIRST_TOKEN.  This is
21935    either the true first token of the file, or the first token after
21936    the initial pragma.  */
21937
21938 static void
21939 cp_parser_initial_pragma (cp_token *first_token)
21940 {
21941   tree name = NULL;
21942
21943   cp_lexer_get_preprocessor_token (NULL, first_token);
21944   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21945     return;
21946
21947   cp_lexer_get_preprocessor_token (NULL, first_token);
21948   if (first_token->type == CPP_STRING)
21949     {
21950       name = first_token->u.value;
21951
21952       cp_lexer_get_preprocessor_token (NULL, first_token);
21953       if (first_token->type != CPP_PRAGMA_EOL)
21954         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21955                &first_token->location);
21956     }
21957   else
21958     error ("%Hexpected string literal", &first_token->location);
21959
21960   /* Skip to the end of the pragma.  */
21961   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21962     cp_lexer_get_preprocessor_token (NULL, first_token);
21963
21964   /* Now actually load the PCH file.  */
21965   if (name)
21966     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21967
21968   /* Read one more token to return to our caller.  We have to do this
21969      after reading the PCH file in, since its pointers have to be
21970      live.  */
21971   cp_lexer_get_preprocessor_token (NULL, first_token);
21972 }
21973
21974 /* Normal parsing of a pragma token.  Here we can (and must) use the
21975    regular lexer.  */
21976
21977 static bool
21978 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21979 {
21980   cp_token *pragma_tok;
21981   unsigned int id;
21982
21983   pragma_tok = cp_lexer_consume_token (parser->lexer);
21984   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21985   parser->lexer->in_pragma = true;
21986
21987   id = pragma_tok->pragma_kind;
21988   switch (id)
21989     {
21990     case PRAGMA_GCC_PCH_PREPROCESS:
21991       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21992              &pragma_tok->location);
21993       break;
21994
21995     case PRAGMA_OMP_BARRIER:
21996       switch (context)
21997         {
21998         case pragma_compound:
21999           cp_parser_omp_barrier (parser, pragma_tok);
22000           return false;
22001         case pragma_stmt:
22002           error ("%H%<#pragma omp barrier%> may only be "
22003                  "used in compound statements", &pragma_tok->location);
22004           break;
22005         default:
22006           goto bad_stmt;
22007         }
22008       break;
22009
22010     case PRAGMA_OMP_FLUSH:
22011       switch (context)
22012         {
22013         case pragma_compound:
22014           cp_parser_omp_flush (parser, pragma_tok);
22015           return false;
22016         case pragma_stmt:
22017           error ("%H%<#pragma omp flush%> may only be "
22018                  "used in compound statements", &pragma_tok->location);
22019           break;
22020         default:
22021           goto bad_stmt;
22022         }
22023       break;
22024
22025     case PRAGMA_OMP_TASKWAIT:
22026       switch (context)
22027         {
22028         case pragma_compound:
22029           cp_parser_omp_taskwait (parser, pragma_tok);
22030           return false;
22031         case pragma_stmt:
22032           error ("%H%<#pragma omp taskwait%> may only be "
22033                  "used in compound statements",
22034                  &pragma_tok->location);
22035           break;
22036         default:
22037           goto bad_stmt;
22038         }
22039       break;
22040
22041     case PRAGMA_OMP_THREADPRIVATE:
22042       cp_parser_omp_threadprivate (parser, pragma_tok);
22043       return false;
22044
22045     case PRAGMA_OMP_ATOMIC:
22046     case PRAGMA_OMP_CRITICAL:
22047     case PRAGMA_OMP_FOR:
22048     case PRAGMA_OMP_MASTER:
22049     case PRAGMA_OMP_ORDERED:
22050     case PRAGMA_OMP_PARALLEL:
22051     case PRAGMA_OMP_SECTIONS:
22052     case PRAGMA_OMP_SINGLE:
22053     case PRAGMA_OMP_TASK:
22054       if (context == pragma_external)
22055         goto bad_stmt;
22056       cp_parser_omp_construct (parser, pragma_tok);
22057       return true;
22058
22059     case PRAGMA_OMP_SECTION:
22060       error ("%H%<#pragma omp section%> may only be used in "
22061              "%<#pragma omp sections%> construct", &pragma_tok->location);
22062       break;
22063
22064     default:
22065       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22066       c_invoke_pragma_handler (id);
22067       break;
22068
22069     bad_stmt:
22070       cp_parser_error (parser, "expected declaration specifiers");
22071       break;
22072     }
22073
22074   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22075   return false;
22076 }
22077
22078 /* The interface the pragma parsers have to the lexer.  */
22079
22080 enum cpp_ttype
22081 pragma_lex (tree *value)
22082 {
22083   cp_token *tok;
22084   enum cpp_ttype ret;
22085
22086   tok = cp_lexer_peek_token (the_parser->lexer);
22087
22088   ret = tok->type;
22089   *value = tok->u.value;
22090
22091   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22092     ret = CPP_EOF;
22093   else if (ret == CPP_STRING)
22094     *value = cp_parser_string_literal (the_parser, false, false);
22095   else
22096     {
22097       cp_lexer_consume_token (the_parser->lexer);
22098       if (ret == CPP_KEYWORD)
22099         ret = CPP_NAME;
22100     }
22101
22102   return ret;
22103 }
22104
22105 \f
22106 /* External interface.  */
22107
22108 /* Parse one entire translation unit.  */
22109
22110 void
22111 c_parse_file (void)
22112 {
22113   bool error_occurred;
22114   static bool already_called = false;
22115
22116   if (already_called)
22117     {
22118       sorry ("inter-module optimizations not implemented for C++");
22119       return;
22120     }
22121   already_called = true;
22122
22123   the_parser = cp_parser_new ();
22124   push_deferring_access_checks (flag_access_control
22125                                 ? dk_no_deferred : dk_no_check);
22126   error_occurred = cp_parser_translation_unit (the_parser);
22127   the_parser = NULL;
22128 }
22129
22130 #include "gt-cp-parser.h"