OSDN Git Service

gcc/cp/
[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, cp_declarator *);
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 < 0)
2627             return;
2628           if (!nesting_depth)
2629             nesting_depth = -1;
2630           break;
2631
2632         case CPP_OPEN_BRACE:
2633           /* Nest. */
2634           nesting_depth++;
2635           break;
2636
2637         default:
2638           break;
2639         }
2640
2641       /* Consume the token.  */
2642       cp_lexer_consume_token (parser->lexer);
2643     }
2644 }
2645
2646 /* Skip tokens until a non-nested closing curly brace is the next
2647    token, or there are no more tokens. Return true in the first case,
2648    false otherwise.  */
2649
2650 static bool
2651 cp_parser_skip_to_closing_brace (cp_parser *parser)
2652 {
2653   unsigned nesting_depth = 0;
2654
2655   while (true)
2656     {
2657       cp_token *token = cp_lexer_peek_token (parser->lexer);
2658
2659       switch (token->type)
2660         {
2661         case CPP_EOF:
2662         case CPP_PRAGMA_EOL:
2663           /* If we've run out of tokens, stop.  */
2664           return false;
2665
2666         case CPP_CLOSE_BRACE:
2667           /* If the next token is a non-nested `}', then we have reached
2668              the end of the current block.  */
2669           if (nesting_depth-- == 0)
2670             return true;
2671           break;
2672
2673         case CPP_OPEN_BRACE:
2674           /* If it the next token is a `{', then we are entering a new
2675              block.  Consume the entire block.  */
2676           ++nesting_depth;
2677           break;
2678
2679         default:
2680           break;
2681         }
2682
2683       /* Consume the token.  */
2684       cp_lexer_consume_token (parser->lexer);
2685     }
2686 }
2687
2688 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2689    parameter is the PRAGMA token, allowing us to purge the entire pragma
2690    sequence.  */
2691
2692 static void
2693 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2694 {
2695   cp_token *token;
2696
2697   parser->lexer->in_pragma = false;
2698
2699   do
2700     token = cp_lexer_consume_token (parser->lexer);
2701   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2702
2703   /* Ensure that the pragma is not parsed again.  */
2704   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2705 }
2706
2707 /* Require pragma end of line, resyncing with it as necessary.  The
2708    arguments are as for cp_parser_skip_to_pragma_eol.  */
2709
2710 static void
2711 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2712 {
2713   parser->lexer->in_pragma = false;
2714   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2715     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2716 }
2717
2718 /* This is a simple wrapper around make_typename_type. When the id is
2719    an unresolved identifier node, we can provide a superior diagnostic
2720    using cp_parser_diagnose_invalid_type_name.  */
2721
2722 static tree
2723 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2724                               tree id, location_t id_location)
2725 {
2726   tree result;
2727   if (TREE_CODE (id) == IDENTIFIER_NODE)
2728     {
2729       result = make_typename_type (scope, id, typename_type,
2730                                    /*complain=*/tf_none);
2731       if (result == error_mark_node)
2732         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2733       return result;
2734     }
2735   return make_typename_type (scope, id, typename_type, tf_error);
2736 }
2737
2738 /* This is a wrapper around the
2739    make_{pointer,ptrmem,reference}_declarator functions that decides
2740    which one to call based on the CODE and CLASS_TYPE arguments. The
2741    CODE argument should be one of the values returned by
2742    cp_parser_ptr_operator. */
2743 static cp_declarator *
2744 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2745                                     cp_cv_quals cv_qualifiers,
2746                                     cp_declarator *target)
2747 {
2748   if (code == ERROR_MARK)
2749     return cp_error_declarator;
2750
2751   if (code == INDIRECT_REF)
2752     if (class_type == NULL_TREE)
2753       return make_pointer_declarator (cv_qualifiers, target);
2754     else
2755       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2756   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2757     return make_reference_declarator (cv_qualifiers, target, false);
2758   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2759     return make_reference_declarator (cv_qualifiers, target, true);
2760   gcc_unreachable ();
2761 }
2762
2763 /* Create a new C++ parser.  */
2764
2765 static cp_parser *
2766 cp_parser_new (void)
2767 {
2768   cp_parser *parser;
2769   cp_lexer *lexer;
2770   unsigned i;
2771
2772   /* cp_lexer_new_main is called before calling ggc_alloc because
2773      cp_lexer_new_main might load a PCH file.  */
2774   lexer = cp_lexer_new_main ();
2775
2776   /* Initialize the binops_by_token so that we can get the tree
2777      directly from the token.  */
2778   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2779     binops_by_token[binops[i].token_type] = binops[i];
2780
2781   parser = GGC_CNEW (cp_parser);
2782   parser->lexer = lexer;
2783   parser->context = cp_parser_context_new (NULL);
2784
2785   /* For now, we always accept GNU extensions.  */
2786   parser->allow_gnu_extensions_p = 1;
2787
2788   /* The `>' token is a greater-than operator, not the end of a
2789      template-id.  */
2790   parser->greater_than_is_operator_p = true;
2791
2792   parser->default_arg_ok_p = true;
2793
2794   /* We are not parsing a constant-expression.  */
2795   parser->integral_constant_expression_p = false;
2796   parser->allow_non_integral_constant_expression_p = false;
2797   parser->non_integral_constant_expression_p = false;
2798
2799   /* Local variable names are not forbidden.  */
2800   parser->local_variables_forbidden_p = false;
2801
2802   /* We are not processing an `extern "C"' declaration.  */
2803   parser->in_unbraced_linkage_specification_p = false;
2804
2805   /* We are not processing a declarator.  */
2806   parser->in_declarator_p = false;
2807
2808   /* We are not processing a template-argument-list.  */
2809   parser->in_template_argument_list_p = false;
2810
2811   /* We are not in an iteration statement.  */
2812   parser->in_statement = 0;
2813
2814   /* We are not in a switch statement.  */
2815   parser->in_switch_statement_p = false;
2816
2817   /* We are not parsing a type-id inside an expression.  */
2818   parser->in_type_id_in_expr_p = false;
2819
2820   /* Declarations aren't implicitly extern "C".  */
2821   parser->implicit_extern_c = false;
2822
2823   /* String literals should be translated to the execution character set.  */
2824   parser->translate_strings_p = true;
2825
2826   /* We are not parsing a function body.  */
2827   parser->in_function_body = false;
2828
2829   /* The unparsed function queue is empty.  */
2830   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2831
2832   /* There are no classes being defined.  */
2833   parser->num_classes_being_defined = 0;
2834
2835   /* No template parameters apply.  */
2836   parser->num_template_parameter_lists = 0;
2837
2838   return parser;
2839 }
2840
2841 /* Create a cp_lexer structure which will emit the tokens in CACHE
2842    and push it onto the parser's lexer stack.  This is used for delayed
2843    parsing of in-class method bodies and default arguments, and should
2844    not be confused with tentative parsing.  */
2845 static void
2846 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2847 {
2848   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2849   lexer->next = parser->lexer;
2850   parser->lexer = lexer;
2851
2852   /* Move the current source position to that of the first token in the
2853      new lexer.  */
2854   cp_lexer_set_source_position_from_token (lexer->next_token);
2855 }
2856
2857 /* Pop the top lexer off the parser stack.  This is never used for the
2858    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2859 static void
2860 cp_parser_pop_lexer (cp_parser *parser)
2861 {
2862   cp_lexer *lexer = parser->lexer;
2863   parser->lexer = lexer->next;
2864   cp_lexer_destroy (lexer);
2865
2866   /* Put the current source position back where it was before this
2867      lexer was pushed.  */
2868   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2869 }
2870
2871 /* Lexical conventions [gram.lex]  */
2872
2873 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2874    identifier.  */
2875
2876 static tree
2877 cp_parser_identifier (cp_parser* parser)
2878 {
2879   cp_token *token;
2880
2881   /* Look for the identifier.  */
2882   token = cp_parser_require (parser, CPP_NAME, "identifier");
2883   /* Return the value.  */
2884   return token ? token->u.value : error_mark_node;
2885 }
2886
2887 /* Parse a sequence of adjacent string constants.  Returns a
2888    TREE_STRING representing the combined, nul-terminated string
2889    constant.  If TRANSLATE is true, translate the string to the
2890    execution character set.  If WIDE_OK is true, a wide string is
2891    invalid here.
2892
2893    C++98 [lex.string] says that if a narrow string literal token is
2894    adjacent to a wide string literal token, the behavior is undefined.
2895    However, C99 6.4.5p4 says that this results in a wide string literal.
2896    We follow C99 here, for consistency with the C front end.
2897
2898    This code is largely lifted from lex_string() in c-lex.c.
2899
2900    FUTURE: ObjC++ will need to handle @-strings here.  */
2901 static tree
2902 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2903 {
2904   tree value;
2905   size_t count;
2906   struct obstack str_ob;
2907   cpp_string str, istr, *strs;
2908   cp_token *tok;
2909   enum cpp_ttype type;
2910
2911   tok = cp_lexer_peek_token (parser->lexer);
2912   if (!cp_parser_is_string_literal (tok))
2913     {
2914       cp_parser_error (parser, "expected string-literal");
2915       return error_mark_node;
2916     }
2917
2918   type = tok->type;
2919
2920   /* Try to avoid the overhead of creating and destroying an obstack
2921      for the common case of just one string.  */
2922   if (!cp_parser_is_string_literal
2923       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2924     {
2925       cp_lexer_consume_token (parser->lexer);
2926
2927       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2928       str.len = TREE_STRING_LENGTH (tok->u.value);
2929       count = 1;
2930
2931       strs = &str;
2932     }
2933   else
2934     {
2935       gcc_obstack_init (&str_ob);
2936       count = 0;
2937
2938       do
2939         {
2940           cp_lexer_consume_token (parser->lexer);
2941           count++;
2942           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2943           str.len = TREE_STRING_LENGTH (tok->u.value);
2944
2945           if (type != tok->type)
2946             {
2947               if (type == CPP_STRING)
2948                 type = tok->type;
2949               else if (tok->type != CPP_STRING)
2950                 error ("%Hunsupported non-standard concatenation "
2951                        "of string literals", &tok->location);
2952             }
2953
2954           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2955
2956           tok = cp_lexer_peek_token (parser->lexer);
2957         }
2958       while (cp_parser_is_string_literal (tok));
2959
2960       strs = (cpp_string *) obstack_finish (&str_ob);
2961     }
2962
2963   if (type != CPP_STRING && !wide_ok)
2964     {
2965       cp_parser_error (parser, "a wide string is invalid in this context");
2966       type = CPP_STRING;
2967     }
2968
2969   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2970       (parse_in, strs, count, &istr, type))
2971     {
2972       value = build_string (istr.len, (const char *)istr.text);
2973       free (CONST_CAST (unsigned char *, istr.text));
2974
2975       switch (type)
2976         {
2977         default:
2978         case CPP_STRING:
2979           TREE_TYPE (value) = char_array_type_node;
2980           break;
2981         case CPP_STRING16:
2982           TREE_TYPE (value) = char16_array_type_node;
2983           break;
2984         case CPP_STRING32:
2985           TREE_TYPE (value) = char32_array_type_node;
2986           break;
2987         case CPP_WSTRING:
2988           TREE_TYPE (value) = wchar_array_type_node;
2989           break;
2990         }
2991
2992       value = fix_string_type (value);
2993     }
2994   else
2995     /* cpp_interpret_string has issued an error.  */
2996     value = error_mark_node;
2997
2998   if (count > 1)
2999     obstack_free (&str_ob, 0);
3000
3001   return value;
3002 }
3003
3004
3005 /* Basic concepts [gram.basic]  */
3006
3007 /* Parse a translation-unit.
3008
3009    translation-unit:
3010      declaration-seq [opt]
3011
3012    Returns TRUE if all went well.  */
3013
3014 static bool
3015 cp_parser_translation_unit (cp_parser* parser)
3016 {
3017   /* The address of the first non-permanent object on the declarator
3018      obstack.  */
3019   static void *declarator_obstack_base;
3020
3021   bool success;
3022
3023   /* Create the declarator obstack, if necessary.  */
3024   if (!cp_error_declarator)
3025     {
3026       gcc_obstack_init (&declarator_obstack);
3027       /* Create the error declarator.  */
3028       cp_error_declarator = make_declarator (cdk_error);
3029       /* Create the empty parameter list.  */
3030       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3031       /* Remember where the base of the declarator obstack lies.  */
3032       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3033     }
3034
3035   cp_parser_declaration_seq_opt (parser);
3036
3037   /* If there are no tokens left then all went well.  */
3038   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3039     {
3040       /* Get rid of the token array; we don't need it any more.  */
3041       cp_lexer_destroy (parser->lexer);
3042       parser->lexer = NULL;
3043
3044       /* This file might have been a context that's implicitly extern
3045          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3046       if (parser->implicit_extern_c)
3047         {
3048           pop_lang_context ();
3049           parser->implicit_extern_c = false;
3050         }
3051
3052       /* Finish up.  */
3053       finish_translation_unit ();
3054
3055       success = true;
3056     }
3057   else
3058     {
3059       cp_parser_error (parser, "expected declaration");
3060       success = false;
3061     }
3062
3063   /* Make sure the declarator obstack was fully cleaned up.  */
3064   gcc_assert (obstack_next_free (&declarator_obstack)
3065               == declarator_obstack_base);
3066
3067   /* All went well.  */
3068   return success;
3069 }
3070
3071 /* Expressions [gram.expr] */
3072
3073 /* Parse a primary-expression.
3074
3075    primary-expression:
3076      literal
3077      this
3078      ( expression )
3079      id-expression
3080
3081    GNU Extensions:
3082
3083    primary-expression:
3084      ( compound-statement )
3085      __builtin_va_arg ( assignment-expression , type-id )
3086      __builtin_offsetof ( type-id , offsetof-expression )
3087
3088    C++ Extensions:
3089      __has_nothrow_assign ( type-id )   
3090      __has_nothrow_constructor ( type-id )
3091      __has_nothrow_copy ( type-id )
3092      __has_trivial_assign ( type-id )   
3093      __has_trivial_constructor ( type-id )
3094      __has_trivial_copy ( type-id )
3095      __has_trivial_destructor ( type-id )
3096      __has_virtual_destructor ( type-id )     
3097      __is_abstract ( type-id )
3098      __is_base_of ( type-id , type-id )
3099      __is_class ( type-id )
3100      __is_convertible_to ( type-id , type-id )     
3101      __is_empty ( type-id )
3102      __is_enum ( type-id )
3103      __is_pod ( type-id )
3104      __is_polymorphic ( type-id )
3105      __is_union ( type-id )
3106
3107    Objective-C++ Extension:
3108
3109    primary-expression:
3110      objc-expression
3111
3112    literal:
3113      __null
3114
3115    ADDRESS_P is true iff this expression was immediately preceded by
3116    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3117    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3118    true iff this expression is a template argument.
3119
3120    Returns a representation of the expression.  Upon return, *IDK
3121    indicates what kind of id-expression (if any) was present.  */
3122
3123 static tree
3124 cp_parser_primary_expression (cp_parser *parser,
3125                               bool address_p,
3126                               bool cast_p,
3127                               bool template_arg_p,
3128                               cp_id_kind *idk)
3129 {
3130   cp_token *token = NULL;
3131
3132   /* Assume the primary expression is not an id-expression.  */
3133   *idk = CP_ID_KIND_NONE;
3134
3135   /* Peek at the next token.  */
3136   token = cp_lexer_peek_token (parser->lexer);
3137   switch (token->type)
3138     {
3139       /* literal:
3140            integer-literal
3141            character-literal
3142            floating-literal
3143            string-literal
3144            boolean-literal  */
3145     case CPP_CHAR:
3146     case CPP_CHAR16:
3147     case CPP_CHAR32:
3148     case CPP_WCHAR:
3149     case CPP_NUMBER:
3150       token = cp_lexer_consume_token (parser->lexer);
3151       if (TREE_CODE (token->u.value) == FIXED_CST)
3152         {
3153           error ("%Hfixed-point types not supported in C++",
3154                  &token->location);
3155           return error_mark_node;
3156         }
3157       /* Floating-point literals are only allowed in an integral
3158          constant expression if they are cast to an integral or
3159          enumeration type.  */
3160       if (TREE_CODE (token->u.value) == REAL_CST
3161           && parser->integral_constant_expression_p
3162           && pedantic)
3163         {
3164           /* CAST_P will be set even in invalid code like "int(2.7 +
3165              ...)".   Therefore, we have to check that the next token
3166              is sure to end the cast.  */
3167           if (cast_p)
3168             {
3169               cp_token *next_token;
3170
3171               next_token = cp_lexer_peek_token (parser->lexer);
3172               if (/* The comma at the end of an
3173                      enumerator-definition.  */
3174                   next_token->type != CPP_COMMA
3175                   /* The curly brace at the end of an enum-specifier.  */
3176                   && next_token->type != CPP_CLOSE_BRACE
3177                   /* The end of a statement.  */
3178                   && next_token->type != CPP_SEMICOLON
3179                   /* The end of the cast-expression.  */
3180                   && next_token->type != CPP_CLOSE_PAREN
3181                   /* The end of an array bound.  */
3182                   && next_token->type != CPP_CLOSE_SQUARE
3183                   /* The closing ">" in a template-argument-list.  */
3184                   && (next_token->type != CPP_GREATER
3185                       || parser->greater_than_is_operator_p)
3186                   /* C++0x only: A ">>" treated like two ">" tokens,
3187                      in a template-argument-list.  */
3188                   && (next_token->type != CPP_RSHIFT
3189                       || (cxx_dialect == cxx98)
3190                       || parser->greater_than_is_operator_p))
3191                 cast_p = false;
3192             }
3193
3194           /* If we are within a cast, then the constraint that the
3195              cast is to an integral or enumeration type will be
3196              checked at that point.  If we are not within a cast, then
3197              this code is invalid.  */
3198           if (!cast_p)
3199             cp_parser_non_integral_constant_expression
3200               (parser, "floating-point literal");
3201         }
3202       return token->u.value;
3203
3204     case CPP_STRING:
3205     case CPP_STRING16:
3206     case CPP_STRING32:
3207     case CPP_WSTRING:
3208       /* ??? Should wide strings be allowed when parser->translate_strings_p
3209          is false (i.e. in attributes)?  If not, we can kill the third
3210          argument to cp_parser_string_literal.  */
3211       return cp_parser_string_literal (parser,
3212                                        parser->translate_strings_p,
3213                                        true);
3214
3215     case CPP_OPEN_PAREN:
3216       {
3217         tree expr;
3218         bool saved_greater_than_is_operator_p;
3219
3220         /* Consume the `('.  */
3221         cp_lexer_consume_token (parser->lexer);
3222         /* Within a parenthesized expression, a `>' token is always
3223            the greater-than operator.  */
3224         saved_greater_than_is_operator_p
3225           = parser->greater_than_is_operator_p;
3226         parser->greater_than_is_operator_p = true;
3227         /* If we see `( { ' then we are looking at the beginning of
3228            a GNU statement-expression.  */
3229         if (cp_parser_allow_gnu_extensions_p (parser)
3230             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3231           {
3232             /* Statement-expressions are not allowed by the standard.  */
3233             pedwarn (token->location, OPT_pedantic, 
3234                      "ISO C++ forbids braced-groups within expressions");
3235
3236             /* And they're not allowed outside of a function-body; you
3237                cannot, for example, write:
3238
3239                  int i = ({ int j = 3; j + 1; });
3240
3241                at class or namespace scope.  */
3242             if (!parser->in_function_body
3243                 || parser->in_template_argument_list_p)
3244               {
3245                 error ("%Hstatement-expressions are not allowed outside "
3246                        "functions nor in template-argument lists",
3247                        &token->location);
3248                 cp_parser_skip_to_end_of_block_or_statement (parser);
3249                 expr = error_mark_node;
3250               }
3251             else
3252               {
3253                 /* Start the statement-expression.  */
3254                 expr = begin_stmt_expr ();
3255                 /* Parse the compound-statement.  */
3256                 cp_parser_compound_statement (parser, expr, false);
3257                 /* Finish up.  */
3258                 expr = finish_stmt_expr (expr, false);
3259               }
3260           }
3261         else
3262           {
3263             /* Parse the parenthesized expression.  */
3264             expr = cp_parser_expression (parser, cast_p, idk);
3265             /* Let the front end know that this expression was
3266                enclosed in parentheses. This matters in case, for
3267                example, the expression is of the form `A::B', since
3268                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3269                not.  */
3270             finish_parenthesized_expr (expr);
3271           }
3272         /* The `>' token might be the end of a template-id or
3273            template-parameter-list now.  */
3274         parser->greater_than_is_operator_p
3275           = saved_greater_than_is_operator_p;
3276         /* Consume the `)'.  */
3277         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3278           cp_parser_skip_to_end_of_statement (parser);
3279
3280         return expr;
3281       }
3282
3283     case CPP_KEYWORD:
3284       switch (token->keyword)
3285         {
3286           /* These two are the boolean literals.  */
3287         case RID_TRUE:
3288           cp_lexer_consume_token (parser->lexer);
3289           return boolean_true_node;
3290         case RID_FALSE:
3291           cp_lexer_consume_token (parser->lexer);
3292           return boolean_false_node;
3293
3294           /* The `__null' literal.  */
3295         case RID_NULL:
3296           cp_lexer_consume_token (parser->lexer);
3297           return null_node;
3298
3299           /* Recognize the `this' keyword.  */
3300         case RID_THIS:
3301           cp_lexer_consume_token (parser->lexer);
3302           if (parser->local_variables_forbidden_p)
3303             {
3304               error ("%H%<this%> may not be used in this context",
3305                      &token->location);
3306               return error_mark_node;
3307             }
3308           /* Pointers cannot appear in constant-expressions.  */
3309           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3310             return error_mark_node;
3311           return finish_this_expr ();
3312
3313           /* The `operator' keyword can be the beginning of an
3314              id-expression.  */
3315         case RID_OPERATOR:
3316           goto id_expression;
3317
3318         case RID_FUNCTION_NAME:
3319         case RID_PRETTY_FUNCTION_NAME:
3320         case RID_C99_FUNCTION_NAME:
3321           {
3322             const char *name;
3323
3324             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3325                __func__ are the names of variables -- but they are
3326                treated specially.  Therefore, they are handled here,
3327                rather than relying on the generic id-expression logic
3328                below.  Grammatically, these names are id-expressions.
3329
3330                Consume the token.  */
3331             token = cp_lexer_consume_token (parser->lexer);
3332
3333             switch (token->keyword)
3334               {
3335               case RID_FUNCTION_NAME:
3336                 name = "%<__FUNCTION__%>";
3337                 break;
3338               case RID_PRETTY_FUNCTION_NAME:
3339                 name = "%<__PRETTY_FUNCTION__%>";
3340                 break;
3341               case RID_C99_FUNCTION_NAME:
3342                 name = "%<__func__%>";
3343                 break;
3344               default:
3345                 gcc_unreachable ();
3346               }
3347
3348             if (cp_parser_non_integral_constant_expression (parser, name))
3349               return error_mark_node;
3350
3351             /* Look up the name.  */
3352             return finish_fname (token->u.value);
3353           }
3354
3355         case RID_VA_ARG:
3356           {
3357             tree expression;
3358             tree type;
3359
3360             /* The `__builtin_va_arg' construct is used to handle
3361                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3362             cp_lexer_consume_token (parser->lexer);
3363             /* Look for the opening `('.  */
3364             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3365             /* Now, parse the assignment-expression.  */
3366             expression = cp_parser_assignment_expression (parser,
3367                                                           /*cast_p=*/false, NULL);
3368             /* Look for the `,'.  */
3369             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3370             /* Parse the type-id.  */
3371             type = cp_parser_type_id (parser);
3372             /* Look for the closing `)'.  */
3373             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3374             /* Using `va_arg' in a constant-expression is not
3375                allowed.  */
3376             if (cp_parser_non_integral_constant_expression (parser,
3377                                                             "%<va_arg%>"))
3378               return error_mark_node;
3379             return build_x_va_arg (expression, type);
3380           }
3381
3382         case RID_OFFSETOF:
3383           return cp_parser_builtin_offsetof (parser);
3384
3385         case RID_HAS_NOTHROW_ASSIGN:
3386         case RID_HAS_NOTHROW_CONSTRUCTOR:
3387         case RID_HAS_NOTHROW_COPY:        
3388         case RID_HAS_TRIVIAL_ASSIGN:
3389         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3390         case RID_HAS_TRIVIAL_COPY:        
3391         case RID_HAS_TRIVIAL_DESTRUCTOR:
3392         case RID_HAS_VIRTUAL_DESTRUCTOR:
3393         case RID_IS_ABSTRACT:
3394         case RID_IS_BASE_OF:
3395         case RID_IS_CLASS:
3396         case RID_IS_CONVERTIBLE_TO:
3397         case RID_IS_EMPTY:
3398         case RID_IS_ENUM:
3399         case RID_IS_POD:
3400         case RID_IS_POLYMORPHIC:
3401         case RID_IS_UNION:
3402           return cp_parser_trait_expr (parser, token->keyword);
3403
3404         /* Objective-C++ expressions.  */
3405         case RID_AT_ENCODE:
3406         case RID_AT_PROTOCOL:
3407         case RID_AT_SELECTOR:
3408           return cp_parser_objc_expression (parser);
3409
3410         default:
3411           cp_parser_error (parser, "expected primary-expression");
3412           return error_mark_node;
3413         }
3414
3415       /* An id-expression can start with either an identifier, a
3416          `::' as the beginning of a qualified-id, or the "operator"
3417          keyword.  */
3418     case CPP_NAME:
3419     case CPP_SCOPE:
3420     case CPP_TEMPLATE_ID:
3421     case CPP_NESTED_NAME_SPECIFIER:
3422       {
3423         tree id_expression;
3424         tree decl;
3425         const char *error_msg;
3426         bool template_p;
3427         bool done;
3428         cp_token *id_expr_token;
3429
3430       id_expression:
3431         /* Parse the id-expression.  */
3432         id_expression
3433           = cp_parser_id_expression (parser,
3434                                      /*template_keyword_p=*/false,
3435                                      /*check_dependency_p=*/true,
3436                                      &template_p,
3437                                      /*declarator_p=*/false,
3438                                      /*optional_p=*/false);
3439         if (id_expression == error_mark_node)
3440           return error_mark_node;
3441         id_expr_token = token;
3442         token = cp_lexer_peek_token (parser->lexer);
3443         done = (token->type != CPP_OPEN_SQUARE
3444                 && token->type != CPP_OPEN_PAREN
3445                 && token->type != CPP_DOT
3446                 && token->type != CPP_DEREF
3447                 && token->type != CPP_PLUS_PLUS
3448                 && token->type != CPP_MINUS_MINUS);
3449         /* If we have a template-id, then no further lookup is
3450            required.  If the template-id was for a template-class, we
3451            will sometimes have a TYPE_DECL at this point.  */
3452         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3453                  || TREE_CODE (id_expression) == TYPE_DECL)
3454           decl = id_expression;
3455         /* Look up the name.  */
3456         else
3457           {
3458             tree ambiguous_decls;
3459
3460             decl = cp_parser_lookup_name (parser, id_expression,
3461                                           none_type,
3462                                           template_p,
3463                                           /*is_namespace=*/false,
3464                                           /*check_dependency=*/true,
3465                                           &ambiguous_decls,
3466                                           id_expr_token->location);
3467             /* If the lookup was ambiguous, an error will already have
3468                been issued.  */
3469             if (ambiguous_decls)
3470               return error_mark_node;
3471
3472             /* In Objective-C++, an instance variable (ivar) may be preferred
3473                to whatever cp_parser_lookup_name() found.  */
3474             decl = objc_lookup_ivar (decl, id_expression);
3475
3476             /* If name lookup gives us a SCOPE_REF, then the
3477                qualifying scope was dependent.  */
3478             if (TREE_CODE (decl) == SCOPE_REF)
3479               {
3480                 /* At this point, we do not know if DECL is a valid
3481                    integral constant expression.  We assume that it is
3482                    in fact such an expression, so that code like:
3483
3484                       template <int N> struct A {
3485                         int a[B<N>::i];
3486                       };
3487                      
3488                    is accepted.  At template-instantiation time, we
3489                    will check that B<N>::i is actually a constant.  */
3490                 return decl;
3491               }
3492             /* Check to see if DECL is a local variable in a context
3493                where that is forbidden.  */
3494             if (parser->local_variables_forbidden_p
3495                 && local_variable_p (decl))
3496               {
3497                 /* It might be that we only found DECL because we are
3498                    trying to be generous with pre-ISO scoping rules.
3499                    For example, consider:
3500
3501                      int i;
3502                      void g() {
3503                        for (int i = 0; i < 10; ++i) {}
3504                        extern void f(int j = i);
3505                      }
3506
3507                    Here, name look up will originally find the out
3508                    of scope `i'.  We need to issue a warning message,
3509                    but then use the global `i'.  */
3510                 decl = check_for_out_of_scope_variable (decl);
3511                 if (local_variable_p (decl))
3512                   {
3513                     error ("%Hlocal variable %qD may not appear in this context",
3514                            &id_expr_token->location, decl);
3515                     return error_mark_node;
3516                   }
3517               }
3518           }
3519
3520         decl = (finish_id_expression
3521                 (id_expression, decl, parser->scope,
3522                  idk,
3523                  parser->integral_constant_expression_p,
3524                  parser->allow_non_integral_constant_expression_p,
3525                  &parser->non_integral_constant_expression_p,
3526                  template_p, done, address_p,
3527                  template_arg_p,
3528                  &error_msg,
3529                  id_expr_token->location));
3530         if (error_msg)
3531           cp_parser_error (parser, error_msg);
3532         return decl;
3533       }
3534
3535       /* Anything else is an error.  */
3536     default:
3537       /* ...unless we have an Objective-C++ message or string literal,
3538          that is.  */
3539       if (c_dialect_objc ()
3540           && (token->type == CPP_OPEN_SQUARE
3541               || token->type == CPP_OBJC_STRING))
3542         return cp_parser_objc_expression (parser);
3543
3544       cp_parser_error (parser, "expected primary-expression");
3545       return error_mark_node;
3546     }
3547 }
3548
3549 /* Parse an id-expression.
3550
3551    id-expression:
3552      unqualified-id
3553      qualified-id
3554
3555    qualified-id:
3556      :: [opt] nested-name-specifier template [opt] unqualified-id
3557      :: identifier
3558      :: operator-function-id
3559      :: template-id
3560
3561    Return a representation of the unqualified portion of the
3562    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3563    a `::' or nested-name-specifier.
3564
3565    Often, if the id-expression was a qualified-id, the caller will
3566    want to make a SCOPE_REF to represent the qualified-id.  This
3567    function does not do this in order to avoid wastefully creating
3568    SCOPE_REFs when they are not required.
3569
3570    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3571    `template' keyword.
3572
3573    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3574    uninstantiated templates.
3575
3576    If *TEMPLATE_P is non-NULL, it is set to true iff the
3577    `template' keyword is used to explicitly indicate that the entity
3578    named is a template.
3579
3580    If DECLARATOR_P is true, the id-expression is appearing as part of
3581    a declarator, rather than as part of an expression.  */
3582
3583 static tree
3584 cp_parser_id_expression (cp_parser *parser,
3585                          bool template_keyword_p,
3586                          bool check_dependency_p,
3587                          bool *template_p,
3588                          bool declarator_p,
3589                          bool optional_p)
3590 {
3591   bool global_scope_p;
3592   bool nested_name_specifier_p;
3593
3594   /* Assume the `template' keyword was not used.  */
3595   if (template_p)
3596     *template_p = template_keyword_p;
3597
3598   /* Look for the optional `::' operator.  */
3599   global_scope_p
3600     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3601        != NULL_TREE);
3602   /* Look for the optional nested-name-specifier.  */
3603   nested_name_specifier_p
3604     = (cp_parser_nested_name_specifier_opt (parser,
3605                                             /*typename_keyword_p=*/false,
3606                                             check_dependency_p,
3607                                             /*type_p=*/false,
3608                                             declarator_p)
3609        != NULL_TREE);
3610   /* If there is a nested-name-specifier, then we are looking at
3611      the first qualified-id production.  */
3612   if (nested_name_specifier_p)
3613     {
3614       tree saved_scope;
3615       tree saved_object_scope;
3616       tree saved_qualifying_scope;
3617       tree unqualified_id;
3618       bool is_template;
3619
3620       /* See if the next token is the `template' keyword.  */
3621       if (!template_p)
3622         template_p = &is_template;
3623       *template_p = cp_parser_optional_template_keyword (parser);
3624       /* Name lookup we do during the processing of the
3625          unqualified-id might obliterate SCOPE.  */
3626       saved_scope = parser->scope;
3627       saved_object_scope = parser->object_scope;
3628       saved_qualifying_scope = parser->qualifying_scope;
3629       /* Process the final unqualified-id.  */
3630       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3631                                                  check_dependency_p,
3632                                                  declarator_p,
3633                                                  /*optional_p=*/false);
3634       /* Restore the SAVED_SCOPE for our caller.  */
3635       parser->scope = saved_scope;
3636       parser->object_scope = saved_object_scope;
3637       parser->qualifying_scope = saved_qualifying_scope;
3638
3639       return unqualified_id;
3640     }
3641   /* Otherwise, if we are in global scope, then we are looking at one
3642      of the other qualified-id productions.  */
3643   else if (global_scope_p)
3644     {
3645       cp_token *token;
3646       tree id;
3647
3648       /* Peek at the next token.  */
3649       token = cp_lexer_peek_token (parser->lexer);
3650
3651       /* If it's an identifier, and the next token is not a "<", then
3652          we can avoid the template-id case.  This is an optimization
3653          for this common case.  */
3654       if (token->type == CPP_NAME
3655           && !cp_parser_nth_token_starts_template_argument_list_p
3656                (parser, 2))
3657         return cp_parser_identifier (parser);
3658
3659       cp_parser_parse_tentatively (parser);
3660       /* Try a template-id.  */
3661       id = cp_parser_template_id (parser,
3662                                   /*template_keyword_p=*/false,
3663                                   /*check_dependency_p=*/true,
3664                                   declarator_p);
3665       /* If that worked, we're done.  */
3666       if (cp_parser_parse_definitely (parser))
3667         return id;
3668
3669       /* Peek at the next token.  (Changes in the token buffer may
3670          have invalidated the pointer obtained above.)  */
3671       token = cp_lexer_peek_token (parser->lexer);
3672
3673       switch (token->type)
3674         {
3675         case CPP_NAME:
3676           return cp_parser_identifier (parser);
3677
3678         case CPP_KEYWORD:
3679           if (token->keyword == RID_OPERATOR)
3680             return cp_parser_operator_function_id (parser);
3681           /* Fall through.  */
3682
3683         default:
3684           cp_parser_error (parser, "expected id-expression");
3685           return error_mark_node;
3686         }
3687     }
3688   else
3689     return cp_parser_unqualified_id (parser, template_keyword_p,
3690                                      /*check_dependency_p=*/true,
3691                                      declarator_p,
3692                                      optional_p);
3693 }
3694
3695 /* Parse an unqualified-id.
3696
3697    unqualified-id:
3698      identifier
3699      operator-function-id
3700      conversion-function-id
3701      ~ class-name
3702      template-id
3703
3704    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3705    keyword, in a construct like `A::template ...'.
3706
3707    Returns a representation of unqualified-id.  For the `identifier'
3708    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3709    production a BIT_NOT_EXPR is returned; the operand of the
3710    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3711    other productions, see the documentation accompanying the
3712    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3713    names are looked up in uninstantiated templates.  If DECLARATOR_P
3714    is true, the unqualified-id is appearing as part of a declarator,
3715    rather than as part of an expression.  */
3716
3717 static tree
3718 cp_parser_unqualified_id (cp_parser* parser,
3719                           bool template_keyword_p,
3720                           bool check_dependency_p,
3721                           bool declarator_p,
3722                           bool optional_p)
3723 {
3724   cp_token *token;
3725
3726   /* Peek at the next token.  */
3727   token = cp_lexer_peek_token (parser->lexer);
3728
3729   switch (token->type)
3730     {
3731     case CPP_NAME:
3732       {
3733         tree id;
3734
3735         /* We don't know yet whether or not this will be a
3736            template-id.  */
3737         cp_parser_parse_tentatively (parser);
3738         /* Try a template-id.  */
3739         id = cp_parser_template_id (parser, template_keyword_p,
3740                                     check_dependency_p,
3741                                     declarator_p);
3742         /* If it worked, we're done.  */
3743         if (cp_parser_parse_definitely (parser))
3744           return id;
3745         /* Otherwise, it's an ordinary identifier.  */
3746         return cp_parser_identifier (parser);
3747       }
3748
3749     case CPP_TEMPLATE_ID:
3750       return cp_parser_template_id (parser, template_keyword_p,
3751                                     check_dependency_p,
3752                                     declarator_p);
3753
3754     case CPP_COMPL:
3755       {
3756         tree type_decl;
3757         tree qualifying_scope;
3758         tree object_scope;
3759         tree scope;
3760         bool done;
3761
3762         /* Consume the `~' token.  */
3763         cp_lexer_consume_token (parser->lexer);
3764         /* Parse the class-name.  The standard, as written, seems to
3765            say that:
3766
3767              template <typename T> struct S { ~S (); };
3768              template <typename T> S<T>::~S() {}
3769
3770            is invalid, since `~' must be followed by a class-name, but
3771            `S<T>' is dependent, and so not known to be a class.
3772            That's not right; we need to look in uninstantiated
3773            templates.  A further complication arises from:
3774
3775              template <typename T> void f(T t) {
3776                t.T::~T();
3777              }
3778
3779            Here, it is not possible to look up `T' in the scope of `T'
3780            itself.  We must look in both the current scope, and the
3781            scope of the containing complete expression.
3782
3783            Yet another issue is:
3784
3785              struct S {
3786                int S;
3787                ~S();
3788              };
3789
3790              S::~S() {}
3791
3792            The standard does not seem to say that the `S' in `~S'
3793            should refer to the type `S' and not the data member
3794            `S::S'.  */
3795
3796         /* DR 244 says that we look up the name after the "~" in the
3797            same scope as we looked up the qualifying name.  That idea
3798            isn't fully worked out; it's more complicated than that.  */
3799         scope = parser->scope;
3800         object_scope = parser->object_scope;
3801         qualifying_scope = parser->qualifying_scope;
3802
3803         /* Check for invalid scopes.  */
3804         if (scope == error_mark_node)
3805           {
3806             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3807               cp_lexer_consume_token (parser->lexer);
3808             return error_mark_node;
3809           }
3810         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3811           {
3812             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3813               error ("%Hscope %qT before %<~%> is not a class-name",
3814                      &token->location, scope);
3815             cp_parser_simulate_error (parser);
3816             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3817               cp_lexer_consume_token (parser->lexer);
3818             return error_mark_node;
3819           }
3820         gcc_assert (!scope || TYPE_P (scope));
3821
3822         /* If the name is of the form "X::~X" it's OK.  */
3823         token = cp_lexer_peek_token (parser->lexer);
3824         if (scope
3825             && token->type == CPP_NAME
3826             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3827                 == CPP_OPEN_PAREN)
3828             && constructor_name_p (token->u.value, scope))
3829           {
3830             cp_lexer_consume_token (parser->lexer);
3831             return build_nt (BIT_NOT_EXPR, scope);
3832           }
3833
3834         /* If there was an explicit qualification (S::~T), first look
3835            in the scope given by the qualification (i.e., S).  */
3836         done = false;
3837         type_decl = NULL_TREE;
3838         if (scope)
3839           {
3840             cp_parser_parse_tentatively (parser);
3841             type_decl = cp_parser_class_name (parser,
3842                                               /*typename_keyword_p=*/false,
3843                                               /*template_keyword_p=*/false,
3844                                               none_type,
3845                                               /*check_dependency=*/false,
3846                                               /*class_head_p=*/false,
3847                                               declarator_p);
3848             if (cp_parser_parse_definitely (parser))
3849               done = true;
3850           }
3851         /* In "N::S::~S", look in "N" as well.  */
3852         if (!done && scope && qualifying_scope)
3853           {
3854             cp_parser_parse_tentatively (parser);
3855             parser->scope = qualifying_scope;
3856             parser->object_scope = NULL_TREE;
3857             parser->qualifying_scope = NULL_TREE;
3858             type_decl
3859               = cp_parser_class_name (parser,
3860                                       /*typename_keyword_p=*/false,
3861                                       /*template_keyword_p=*/false,
3862                                       none_type,
3863                                       /*check_dependency=*/false,
3864                                       /*class_head_p=*/false,
3865                                       declarator_p);
3866             if (cp_parser_parse_definitely (parser))
3867               done = true;
3868           }
3869         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3870         else if (!done && object_scope)
3871           {
3872             cp_parser_parse_tentatively (parser);
3873             parser->scope = object_scope;
3874             parser->object_scope = NULL_TREE;
3875             parser->qualifying_scope = NULL_TREE;
3876             type_decl
3877               = cp_parser_class_name (parser,
3878                                       /*typename_keyword_p=*/false,
3879                                       /*template_keyword_p=*/false,
3880                                       none_type,
3881                                       /*check_dependency=*/false,
3882                                       /*class_head_p=*/false,
3883                                       declarator_p);
3884             if (cp_parser_parse_definitely (parser))
3885               done = true;
3886           }
3887         /* Look in the surrounding context.  */
3888         if (!done)
3889           {
3890             parser->scope = NULL_TREE;
3891             parser->object_scope = NULL_TREE;
3892             parser->qualifying_scope = NULL_TREE;
3893             if (processing_template_decl)
3894               cp_parser_parse_tentatively (parser);
3895             type_decl
3896               = cp_parser_class_name (parser,
3897                                       /*typename_keyword_p=*/false,
3898                                       /*template_keyword_p=*/false,
3899                                       none_type,
3900                                       /*check_dependency=*/false,
3901                                       /*class_head_p=*/false,
3902                                       declarator_p);
3903             if (processing_template_decl
3904                 && ! cp_parser_parse_definitely (parser))
3905               {
3906                 /* We couldn't find a type with this name, so just accept
3907                    it and check for a match at instantiation time.  */
3908                 type_decl = cp_parser_identifier (parser);
3909                 if (type_decl != error_mark_node)
3910                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3911                 return type_decl;
3912               }
3913           }
3914         /* If an error occurred, assume that the name of the
3915            destructor is the same as the name of the qualifying
3916            class.  That allows us to keep parsing after running
3917            into ill-formed destructor names.  */
3918         if (type_decl == error_mark_node && scope)
3919           return build_nt (BIT_NOT_EXPR, scope);
3920         else if (type_decl == error_mark_node)
3921           return error_mark_node;
3922
3923         /* Check that destructor name and scope match.  */
3924         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3925           {
3926             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3927               error ("%Hdeclaration of %<~%T%> as member of %qT",
3928                      &token->location, type_decl, scope);
3929             cp_parser_simulate_error (parser);
3930             return error_mark_node;
3931           }
3932
3933         /* [class.dtor]
3934
3935            A typedef-name that names a class shall not be used as the
3936            identifier in the declarator for a destructor declaration.  */
3937         if (declarator_p
3938             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3939             && !DECL_SELF_REFERENCE_P (type_decl)
3940             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3941           error ("%Htypedef-name %qD used as destructor declarator",
3942                  &token->location, type_decl);
3943
3944         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3945       }
3946
3947     case CPP_KEYWORD:
3948       if (token->keyword == RID_OPERATOR)
3949         {
3950           tree id;
3951
3952           /* This could be a template-id, so we try that first.  */
3953           cp_parser_parse_tentatively (parser);
3954           /* Try a template-id.  */
3955           id = cp_parser_template_id (parser, template_keyword_p,
3956                                       /*check_dependency_p=*/true,
3957                                       declarator_p);
3958           /* If that worked, we're done.  */
3959           if (cp_parser_parse_definitely (parser))
3960             return id;
3961           /* We still don't know whether we're looking at an
3962              operator-function-id or a conversion-function-id.  */
3963           cp_parser_parse_tentatively (parser);
3964           /* Try an operator-function-id.  */
3965           id = cp_parser_operator_function_id (parser);
3966           /* If that didn't work, try a conversion-function-id.  */
3967           if (!cp_parser_parse_definitely (parser))
3968             id = cp_parser_conversion_function_id (parser);
3969
3970           return id;
3971         }
3972       /* Fall through.  */
3973
3974     default:
3975       if (optional_p)
3976         return NULL_TREE;
3977       cp_parser_error (parser, "expected unqualified-id");
3978       return error_mark_node;
3979     }
3980 }
3981
3982 /* Parse an (optional) nested-name-specifier.
3983
3984    nested-name-specifier: [C++98]
3985      class-or-namespace-name :: nested-name-specifier [opt]
3986      class-or-namespace-name :: template nested-name-specifier [opt]
3987
3988    nested-name-specifier: [C++0x]
3989      type-name ::
3990      namespace-name ::
3991      nested-name-specifier identifier ::
3992      nested-name-specifier template [opt] simple-template-id ::
3993
3994    PARSER->SCOPE should be set appropriately before this function is
3995    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3996    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3997    in name lookups.
3998
3999    Sets PARSER->SCOPE to the class (TYPE) or namespace
4000    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4001    it unchanged if there is no nested-name-specifier.  Returns the new
4002    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4003
4004    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4005    part of a declaration and/or decl-specifier.  */
4006
4007 static tree
4008 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4009                                      bool typename_keyword_p,
4010                                      bool check_dependency_p,
4011                                      bool type_p,
4012                                      bool is_declaration)
4013 {
4014   bool success = false;
4015   cp_token_position start = 0;
4016   cp_token *token;
4017
4018   /* Remember where the nested-name-specifier starts.  */
4019   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4020     {
4021       start = cp_lexer_token_position (parser->lexer, false);
4022       push_deferring_access_checks (dk_deferred);
4023     }
4024
4025   while (true)
4026     {
4027       tree new_scope;
4028       tree old_scope;
4029       tree saved_qualifying_scope;
4030       bool template_keyword_p;
4031
4032       /* Spot cases that cannot be the beginning of a
4033          nested-name-specifier.  */
4034       token = cp_lexer_peek_token (parser->lexer);
4035
4036       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4037          the already parsed nested-name-specifier.  */
4038       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4039         {
4040           /* Grab the nested-name-specifier and continue the loop.  */
4041           cp_parser_pre_parsed_nested_name_specifier (parser);
4042           /* If we originally encountered this nested-name-specifier
4043              with IS_DECLARATION set to false, we will not have
4044              resolved TYPENAME_TYPEs, so we must do so here.  */
4045           if (is_declaration
4046               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4047             {
4048               new_scope = resolve_typename_type (parser->scope,
4049                                                  /*only_current_p=*/false);
4050               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4051                 parser->scope = new_scope;
4052             }
4053           success = true;
4054           continue;
4055         }
4056
4057       /* Spot cases that cannot be the beginning of a
4058          nested-name-specifier.  On the second and subsequent times
4059          through the loop, we look for the `template' keyword.  */
4060       if (success && token->keyword == RID_TEMPLATE)
4061         ;
4062       /* A template-id can start a nested-name-specifier.  */
4063       else if (token->type == CPP_TEMPLATE_ID)
4064         ;
4065       else
4066         {
4067           /* If the next token is not an identifier, then it is
4068              definitely not a type-name or namespace-name.  */
4069           if (token->type != CPP_NAME)
4070             break;
4071           /* If the following token is neither a `<' (to begin a
4072              template-id), nor a `::', then we are not looking at a
4073              nested-name-specifier.  */
4074           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4075           if (token->type != CPP_SCOPE
4076               && !cp_parser_nth_token_starts_template_argument_list_p
4077                   (parser, 2))
4078             break;
4079         }
4080
4081       /* The nested-name-specifier is optional, so we parse
4082          tentatively.  */
4083       cp_parser_parse_tentatively (parser);
4084
4085       /* Look for the optional `template' keyword, if this isn't the
4086          first time through the loop.  */
4087       if (success)
4088         template_keyword_p = cp_parser_optional_template_keyword (parser);
4089       else
4090         template_keyword_p = false;
4091
4092       /* Save the old scope since the name lookup we are about to do
4093          might destroy it.  */
4094       old_scope = parser->scope;
4095       saved_qualifying_scope = parser->qualifying_scope;
4096       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4097          look up names in "X<T>::I" in order to determine that "Y" is
4098          a template.  So, if we have a typename at this point, we make
4099          an effort to look through it.  */
4100       if (is_declaration
4101           && !typename_keyword_p
4102           && parser->scope
4103           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4104         parser->scope = resolve_typename_type (parser->scope,
4105                                                /*only_current_p=*/false);
4106       /* Parse the qualifying entity.  */
4107       new_scope
4108         = cp_parser_qualifying_entity (parser,
4109                                        typename_keyword_p,
4110                                        template_keyword_p,
4111                                        check_dependency_p,
4112                                        type_p,
4113                                        is_declaration);
4114       /* Look for the `::' token.  */
4115       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4116
4117       /* If we found what we wanted, we keep going; otherwise, we're
4118          done.  */
4119       if (!cp_parser_parse_definitely (parser))
4120         {
4121           bool error_p = false;
4122
4123           /* Restore the OLD_SCOPE since it was valid before the
4124              failed attempt at finding the last
4125              class-or-namespace-name.  */
4126           parser->scope = old_scope;
4127           parser->qualifying_scope = saved_qualifying_scope;
4128           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4129             break;
4130           /* If the next token is an identifier, and the one after
4131              that is a `::', then any valid interpretation would have
4132              found a class-or-namespace-name.  */
4133           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4134                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4135                      == CPP_SCOPE)
4136                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4137                      != CPP_COMPL))
4138             {
4139               token = cp_lexer_consume_token (parser->lexer);
4140               if (!error_p)
4141                 {
4142                   if (!token->ambiguous_p)
4143                     {
4144                       tree decl;
4145                       tree ambiguous_decls;
4146
4147                       decl = cp_parser_lookup_name (parser, token->u.value,
4148                                                     none_type,
4149                                                     /*is_template=*/false,
4150                                                     /*is_namespace=*/false,
4151                                                     /*check_dependency=*/true,
4152                                                     &ambiguous_decls,
4153                                                     token->location);
4154                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4155                         error ("%H%qD used without template parameters",
4156                                &token->location, decl);
4157                       else if (ambiguous_decls)
4158                         {
4159                           error ("%Hreference to %qD is ambiguous",
4160                                  &token->location, token->u.value);
4161                           print_candidates (ambiguous_decls);
4162                           decl = error_mark_node;
4163                         }
4164                       else
4165                         {
4166                           const char* msg = "is not a class or namespace";
4167                           if (cxx_dialect != cxx98)
4168                             msg = "is not a class, namespace, or enumeration";
4169                           cp_parser_name_lookup_error
4170                             (parser, token->u.value, decl, msg,
4171                              token->location);
4172                         }
4173                     }
4174                   parser->scope = error_mark_node;
4175                   error_p = true;
4176                   /* Treat this as a successful nested-name-specifier
4177                      due to:
4178
4179                      [basic.lookup.qual]
4180
4181                      If the name found is not a class-name (clause
4182                      _class_) or namespace-name (_namespace.def_), the
4183                      program is ill-formed.  */
4184                   success = true;
4185                 }
4186               cp_lexer_consume_token (parser->lexer);
4187             }
4188           break;
4189         }
4190       /* We've found one valid nested-name-specifier.  */
4191       success = true;
4192       /* Name lookup always gives us a DECL.  */
4193       if (TREE_CODE (new_scope) == TYPE_DECL)
4194         new_scope = TREE_TYPE (new_scope);
4195       /* Uses of "template" must be followed by actual templates.  */
4196       if (template_keyword_p
4197           && !(CLASS_TYPE_P (new_scope)
4198                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4199                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4200                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4201           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4202                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4203                    == TEMPLATE_ID_EXPR)))
4204         permerror (input_location, TYPE_P (new_scope)
4205                    ? "%qT is not a template"
4206                    : "%qD is not a template",
4207                    new_scope);
4208       /* If it is a class scope, try to complete it; we are about to
4209          be looking up names inside the class.  */
4210       if (TYPE_P (new_scope)
4211           /* Since checking types for dependency can be expensive,
4212              avoid doing it if the type is already complete.  */
4213           && !COMPLETE_TYPE_P (new_scope)
4214           /* Do not try to complete dependent types.  */
4215           && !dependent_type_p (new_scope))
4216         {
4217           new_scope = complete_type (new_scope);
4218           /* If it is a typedef to current class, use the current
4219              class instead, as the typedef won't have any names inside
4220              it yet.  */
4221           if (!COMPLETE_TYPE_P (new_scope)
4222               && currently_open_class (new_scope))
4223             new_scope = TYPE_MAIN_VARIANT (new_scope);
4224         }
4225       /* Make sure we look in the right scope the next time through
4226          the loop.  */
4227       parser->scope = new_scope;
4228     }
4229
4230   /* If parsing tentatively, replace the sequence of tokens that makes
4231      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4232      token.  That way, should we re-parse the token stream, we will
4233      not have to repeat the effort required to do the parse, nor will
4234      we issue duplicate error messages.  */
4235   if (success && start)
4236     {
4237       cp_token *token;
4238
4239       token = cp_lexer_token_at (parser->lexer, start);
4240       /* Reset the contents of the START token.  */
4241       token->type = CPP_NESTED_NAME_SPECIFIER;
4242       /* Retrieve any deferred checks.  Do not pop this access checks yet
4243          so the memory will not be reclaimed during token replacing below.  */
4244       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4245       token->u.tree_check_value->value = parser->scope;
4246       token->u.tree_check_value->checks = get_deferred_access_checks ();
4247       token->u.tree_check_value->qualifying_scope =
4248         parser->qualifying_scope;
4249       token->keyword = RID_MAX;
4250
4251       /* Purge all subsequent tokens.  */
4252       cp_lexer_purge_tokens_after (parser->lexer, start);
4253     }
4254
4255   if (start)
4256     pop_to_parent_deferring_access_checks ();
4257
4258   return success ? parser->scope : NULL_TREE;
4259 }
4260
4261 /* Parse a nested-name-specifier.  See
4262    cp_parser_nested_name_specifier_opt for details.  This function
4263    behaves identically, except that it will an issue an error if no
4264    nested-name-specifier is present.  */
4265
4266 static tree
4267 cp_parser_nested_name_specifier (cp_parser *parser,
4268                                  bool typename_keyword_p,
4269                                  bool check_dependency_p,
4270                                  bool type_p,
4271                                  bool is_declaration)
4272 {
4273   tree scope;
4274
4275   /* Look for the nested-name-specifier.  */
4276   scope = cp_parser_nested_name_specifier_opt (parser,
4277                                                typename_keyword_p,
4278                                                check_dependency_p,
4279                                                type_p,
4280                                                is_declaration);
4281   /* If it was not present, issue an error message.  */
4282   if (!scope)
4283     {
4284       cp_parser_error (parser, "expected nested-name-specifier");
4285       parser->scope = NULL_TREE;
4286     }
4287
4288   return scope;
4289 }
4290
4291 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4292    this is either a class-name or a namespace-name (which corresponds
4293    to the class-or-namespace-name production in the grammar). For
4294    C++0x, it can also be a type-name that refers to an enumeration
4295    type.
4296
4297    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4298    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4299    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4300    TYPE_P is TRUE iff the next name should be taken as a class-name,
4301    even the same name is declared to be another entity in the same
4302    scope.
4303
4304    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4305    specified by the class-or-namespace-name.  If neither is found the
4306    ERROR_MARK_NODE is returned.  */
4307
4308 static tree
4309 cp_parser_qualifying_entity (cp_parser *parser,
4310                              bool typename_keyword_p,
4311                              bool template_keyword_p,
4312                              bool check_dependency_p,
4313                              bool type_p,
4314                              bool is_declaration)
4315 {
4316   tree saved_scope;
4317   tree saved_qualifying_scope;
4318   tree saved_object_scope;
4319   tree scope;
4320   bool only_class_p;
4321   bool successful_parse_p;
4322
4323   /* Before we try to parse the class-name, we must save away the
4324      current PARSER->SCOPE since cp_parser_class_name will destroy
4325      it.  */
4326   saved_scope = parser->scope;
4327   saved_qualifying_scope = parser->qualifying_scope;
4328   saved_object_scope = parser->object_scope;
4329   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4330      there is no need to look for a namespace-name.  */
4331   only_class_p = template_keyword_p 
4332     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4333   if (!only_class_p)
4334     cp_parser_parse_tentatively (parser);
4335   scope = cp_parser_class_name (parser,
4336                                 typename_keyword_p,
4337                                 template_keyword_p,
4338                                 type_p ? class_type : none_type,
4339                                 check_dependency_p,
4340                                 /*class_head_p=*/false,
4341                                 is_declaration);
4342   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4343   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4344   if (!only_class_p 
4345       && cxx_dialect != cxx98
4346       && !successful_parse_p)
4347     {
4348       /* Restore the saved scope.  */
4349       parser->scope = saved_scope;
4350       parser->qualifying_scope = saved_qualifying_scope;
4351       parser->object_scope = saved_object_scope;
4352
4353       /* Parse tentatively.  */
4354       cp_parser_parse_tentatively (parser);
4355      
4356       /* Parse a typedef-name or enum-name.  */
4357       scope = cp_parser_nonclass_name (parser);
4358       successful_parse_p = cp_parser_parse_definitely (parser);
4359     }
4360   /* If that didn't work, try for a namespace-name.  */
4361   if (!only_class_p && !successful_parse_p)
4362     {
4363       /* Restore the saved scope.  */
4364       parser->scope = saved_scope;
4365       parser->qualifying_scope = saved_qualifying_scope;
4366       parser->object_scope = saved_object_scope;
4367       /* If we are not looking at an identifier followed by the scope
4368          resolution operator, then this is not part of a
4369          nested-name-specifier.  (Note that this function is only used
4370          to parse the components of a nested-name-specifier.)  */
4371       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4372           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4373         return error_mark_node;
4374       scope = cp_parser_namespace_name (parser);
4375     }
4376
4377   return scope;
4378 }
4379
4380 /* Parse a postfix-expression.
4381
4382    postfix-expression:
4383      primary-expression
4384      postfix-expression [ expression ]
4385      postfix-expression ( expression-list [opt] )
4386      simple-type-specifier ( expression-list [opt] )
4387      typename :: [opt] nested-name-specifier identifier
4388        ( expression-list [opt] )
4389      typename :: [opt] nested-name-specifier template [opt] template-id
4390        ( expression-list [opt] )
4391      postfix-expression . template [opt] id-expression
4392      postfix-expression -> template [opt] id-expression
4393      postfix-expression . pseudo-destructor-name
4394      postfix-expression -> pseudo-destructor-name
4395      postfix-expression ++
4396      postfix-expression --
4397      dynamic_cast < type-id > ( expression )
4398      static_cast < type-id > ( expression )
4399      reinterpret_cast < type-id > ( expression )
4400      const_cast < type-id > ( expression )
4401      typeid ( expression )
4402      typeid ( type-id )
4403
4404    GNU Extension:
4405
4406    postfix-expression:
4407      ( type-id ) { initializer-list , [opt] }
4408
4409    This extension is a GNU version of the C99 compound-literal
4410    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4411    but they are essentially the same concept.)
4412
4413    If ADDRESS_P is true, the postfix expression is the operand of the
4414    `&' operator.  CAST_P is true if this expression is the target of a
4415    cast.
4416
4417    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4418    class member access expressions [expr.ref].
4419
4420    Returns a representation of the expression.  */
4421
4422 static tree
4423 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4424                               bool member_access_only_p,
4425                               cp_id_kind * pidk_return)
4426 {
4427   cp_token *token;
4428   enum rid keyword;
4429   cp_id_kind idk = CP_ID_KIND_NONE;
4430   tree postfix_expression = NULL_TREE;
4431   bool is_member_access = false;
4432
4433   /* Peek at the next token.  */
4434   token = cp_lexer_peek_token (parser->lexer);
4435   /* Some of the productions are determined by keywords.  */
4436   keyword = token->keyword;
4437   switch (keyword)
4438     {
4439     case RID_DYNCAST:
4440     case RID_STATCAST:
4441     case RID_REINTCAST:
4442     case RID_CONSTCAST:
4443       {
4444         tree type;
4445         tree expression;
4446         const char *saved_message;
4447
4448         /* All of these can be handled in the same way from the point
4449            of view of parsing.  Begin by consuming the token
4450            identifying the cast.  */
4451         cp_lexer_consume_token (parser->lexer);
4452
4453         /* New types cannot be defined in the cast.  */
4454         saved_message = parser->type_definition_forbidden_message;
4455         parser->type_definition_forbidden_message
4456           = "types may not be defined in casts";
4457
4458         /* Look for the opening `<'.  */
4459         cp_parser_require (parser, CPP_LESS, "%<<%>");
4460         /* Parse the type to which we are casting.  */
4461         type = cp_parser_type_id (parser);
4462         /* Look for the closing `>'.  */
4463         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4464         /* Restore the old message.  */
4465         parser->type_definition_forbidden_message = saved_message;
4466
4467         /* And the expression which is being cast.  */
4468         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4469         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4470         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4471
4472         /* Only type conversions to integral or enumeration types
4473            can be used in constant-expressions.  */
4474         if (!cast_valid_in_integral_constant_expression_p (type)
4475             && (cp_parser_non_integral_constant_expression
4476                 (parser,
4477                  "a cast to a type other than an integral or "
4478                  "enumeration type")))
4479           return error_mark_node;
4480
4481         switch (keyword)
4482           {
4483           case RID_DYNCAST:
4484             postfix_expression
4485               = build_dynamic_cast (type, expression, tf_warning_or_error);
4486             break;
4487           case RID_STATCAST:
4488             postfix_expression
4489               = build_static_cast (type, expression, tf_warning_or_error);
4490             break;
4491           case RID_REINTCAST:
4492             postfix_expression
4493               = build_reinterpret_cast (type, expression, 
4494                                         tf_warning_or_error);
4495             break;
4496           case RID_CONSTCAST:
4497             postfix_expression
4498               = build_const_cast (type, expression, tf_warning_or_error);
4499             break;
4500           default:
4501             gcc_unreachable ();
4502           }
4503       }
4504       break;
4505
4506     case RID_TYPEID:
4507       {
4508         tree type;
4509         const char *saved_message;
4510         bool saved_in_type_id_in_expr_p;
4511
4512         /* Consume the `typeid' token.  */
4513         cp_lexer_consume_token (parser->lexer);
4514         /* Look for the `(' token.  */
4515         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4516         /* Types cannot be defined in a `typeid' expression.  */
4517         saved_message = parser->type_definition_forbidden_message;
4518         parser->type_definition_forbidden_message
4519           = "types may not be defined in a %<typeid%> expression";
4520         /* We can't be sure yet whether we're looking at a type-id or an
4521            expression.  */
4522         cp_parser_parse_tentatively (parser);
4523         /* Try a type-id first.  */
4524         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4525         parser->in_type_id_in_expr_p = true;
4526         type = cp_parser_type_id (parser);
4527         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4528         /* Look for the `)' token.  Otherwise, we can't be sure that
4529            we're not looking at an expression: consider `typeid (int
4530            (3))', for example.  */
4531         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4532         /* If all went well, simply lookup the type-id.  */
4533         if (cp_parser_parse_definitely (parser))
4534           postfix_expression = get_typeid (type);
4535         /* Otherwise, fall back to the expression variant.  */
4536         else
4537           {
4538             tree expression;
4539
4540             /* Look for an expression.  */
4541             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4542             /* Compute its typeid.  */
4543             postfix_expression = build_typeid (expression);
4544             /* Look for the `)' token.  */
4545             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4546           }
4547         /* Restore the saved message.  */
4548         parser->type_definition_forbidden_message = saved_message;
4549         /* `typeid' may not appear in an integral constant expression.  */
4550         if (cp_parser_non_integral_constant_expression(parser,
4551                                                        "%<typeid%> operator"))
4552           return error_mark_node;
4553       }
4554       break;
4555
4556     case RID_TYPENAME:
4557       {
4558         tree type;
4559         /* The syntax permitted here is the same permitted for an
4560            elaborated-type-specifier.  */
4561         type = cp_parser_elaborated_type_specifier (parser,
4562                                                     /*is_friend=*/false,
4563                                                     /*is_declaration=*/false);
4564         postfix_expression = cp_parser_functional_cast (parser, type);
4565       }
4566       break;
4567
4568     default:
4569       {
4570         tree type;
4571
4572         /* If the next thing is a simple-type-specifier, we may be
4573            looking at a functional cast.  We could also be looking at
4574            an id-expression.  So, we try the functional cast, and if
4575            that doesn't work we fall back to the primary-expression.  */
4576         cp_parser_parse_tentatively (parser);
4577         /* Look for the simple-type-specifier.  */
4578         type = cp_parser_simple_type_specifier (parser,
4579                                                 /*decl_specs=*/NULL,
4580                                                 CP_PARSER_FLAGS_NONE);
4581         /* Parse the cast itself.  */
4582         if (!cp_parser_error_occurred (parser))
4583           postfix_expression
4584             = cp_parser_functional_cast (parser, type);
4585         /* If that worked, we're done.  */
4586         if (cp_parser_parse_definitely (parser))
4587           break;
4588
4589         /* If the functional-cast didn't work out, try a
4590            compound-literal.  */
4591         if (cp_parser_allow_gnu_extensions_p (parser)
4592             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4593           {
4594             VEC(constructor_elt,gc) *initializer_list = NULL;
4595             bool saved_in_type_id_in_expr_p;
4596
4597             cp_parser_parse_tentatively (parser);
4598             /* Consume the `('.  */
4599             cp_lexer_consume_token (parser->lexer);
4600             /* Parse the type.  */
4601             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4602             parser->in_type_id_in_expr_p = true;
4603             type = cp_parser_type_id (parser);
4604             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4605             /* Look for the `)'.  */
4606             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4607             /* Look for the `{'.  */
4608             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4609             /* If things aren't going well, there's no need to
4610                keep going.  */
4611             if (!cp_parser_error_occurred (parser))
4612               {
4613                 bool non_constant_p;
4614                 /* Parse the initializer-list.  */
4615                 initializer_list
4616                   = cp_parser_initializer_list (parser, &non_constant_p);
4617                 /* Allow a trailing `,'.  */
4618                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4619                   cp_lexer_consume_token (parser->lexer);
4620                 /* Look for the final `}'.  */
4621                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4622               }
4623             /* If that worked, we're definitely looking at a
4624                compound-literal expression.  */
4625             if (cp_parser_parse_definitely (parser))
4626               {
4627                 /* Warn the user that a compound literal is not
4628                    allowed in standard C++.  */
4629                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4630                 /* For simplicity, we disallow compound literals in
4631                    constant-expressions.  We could
4632                    allow compound literals of integer type, whose
4633                    initializer was a constant, in constant
4634                    expressions.  Permitting that usage, as a further
4635                    extension, would not change the meaning of any
4636                    currently accepted programs.  (Of course, as
4637                    compound literals are not part of ISO C++, the
4638                    standard has nothing to say.)  */
4639                 if (cp_parser_non_integral_constant_expression 
4640                     (parser, "non-constant compound literals"))
4641                   {
4642                     postfix_expression = error_mark_node;
4643                     break;
4644                   }
4645                 /* Form the representation of the compound-literal.  */
4646                 postfix_expression
4647                   = (finish_compound_literal
4648                      (type, build_constructor (init_list_type_node,
4649                                                initializer_list)));
4650                 break;
4651               }
4652           }
4653
4654         /* It must be a primary-expression.  */
4655         postfix_expression
4656           = cp_parser_primary_expression (parser, address_p, cast_p,
4657                                           /*template_arg_p=*/false,
4658                                           &idk);
4659       }
4660       break;
4661     }
4662
4663   /* Keep looping until the postfix-expression is complete.  */
4664   while (true)
4665     {
4666       if (idk == CP_ID_KIND_UNQUALIFIED
4667           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4668           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4669         /* It is not a Koenig lookup function call.  */
4670         postfix_expression
4671           = unqualified_name_lookup_error (postfix_expression);
4672
4673       /* Peek at the next token.  */
4674       token = cp_lexer_peek_token (parser->lexer);
4675
4676       switch (token->type)
4677         {
4678         case CPP_OPEN_SQUARE:
4679           postfix_expression
4680             = cp_parser_postfix_open_square_expression (parser,
4681                                                         postfix_expression,
4682                                                         false);
4683           idk = CP_ID_KIND_NONE;
4684           is_member_access = false;
4685           break;
4686
4687         case CPP_OPEN_PAREN:
4688           /* postfix-expression ( expression-list [opt] ) */
4689           {
4690             bool koenig_p;
4691             bool is_builtin_constant_p;
4692             bool saved_integral_constant_expression_p = false;
4693             bool saved_non_integral_constant_expression_p = false;
4694             tree args;
4695
4696             is_member_access = false;
4697
4698             is_builtin_constant_p
4699               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4700             if (is_builtin_constant_p)
4701               {
4702                 /* The whole point of __builtin_constant_p is to allow
4703                    non-constant expressions to appear as arguments.  */
4704                 saved_integral_constant_expression_p
4705                   = parser->integral_constant_expression_p;
4706                 saved_non_integral_constant_expression_p
4707                   = parser->non_integral_constant_expression_p;
4708                 parser->integral_constant_expression_p = false;
4709               }
4710             args = (cp_parser_parenthesized_expression_list
4711                     (parser, /*is_attribute_list=*/false,
4712                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4713                      /*non_constant_p=*/NULL));
4714             if (is_builtin_constant_p)
4715               {
4716                 parser->integral_constant_expression_p
4717                   = saved_integral_constant_expression_p;
4718                 parser->non_integral_constant_expression_p
4719                   = saved_non_integral_constant_expression_p;
4720               }
4721
4722             if (args == error_mark_node)
4723               {
4724                 postfix_expression = error_mark_node;
4725                 break;
4726               }
4727
4728             /* Function calls are not permitted in
4729                constant-expressions.  */
4730             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4731                 && cp_parser_non_integral_constant_expression (parser,
4732                                                                "a function call"))
4733               {
4734                 postfix_expression = error_mark_node;
4735                 break;
4736               }
4737
4738             koenig_p = false;
4739             if (idk == CP_ID_KIND_UNQUALIFIED
4740                 || idk == CP_ID_KIND_TEMPLATE_ID)
4741               {
4742                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4743                   {
4744                     if (args)
4745                       {
4746                         koenig_p = true;
4747                         if (!any_type_dependent_arguments_p (args))
4748                           postfix_expression
4749                             = perform_koenig_lookup (postfix_expression, args);
4750                       }
4751                     else
4752                       postfix_expression
4753                         = unqualified_fn_lookup_error (postfix_expression);
4754                   }
4755                 /* We do not perform argument-dependent lookup if
4756                    normal lookup finds a non-function, in accordance
4757                    with the expected resolution of DR 218.  */
4758                 else if (args && is_overloaded_fn (postfix_expression))
4759                   {
4760                     tree fn = get_first_fn (postfix_expression);
4761
4762                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4763                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4764
4765                     /* Only do argument dependent lookup if regular
4766                        lookup does not find a set of member functions.
4767                        [basic.lookup.koenig]/2a  */
4768                     if (!DECL_FUNCTION_MEMBER_P (fn))
4769                       {
4770                         koenig_p = true;
4771                         if (!any_type_dependent_arguments_p (args))
4772                           postfix_expression
4773                             = perform_koenig_lookup (postfix_expression, args);
4774                       }
4775                   }
4776               }
4777
4778             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4779               {
4780                 tree instance = TREE_OPERAND (postfix_expression, 0);
4781                 tree fn = TREE_OPERAND (postfix_expression, 1);
4782
4783                 if (processing_template_decl
4784                     && (type_dependent_expression_p (instance)
4785                         || (!BASELINK_P (fn)
4786                             && TREE_CODE (fn) != FIELD_DECL)
4787                         || type_dependent_expression_p (fn)
4788                         || any_type_dependent_arguments_p (args)))
4789                   {
4790                     postfix_expression
4791                       = build_nt_call_list (postfix_expression, args);
4792                     break;
4793                   }
4794
4795                 if (BASELINK_P (fn))
4796                   {
4797                   postfix_expression
4798                     = (build_new_method_call
4799                        (instance, fn, args, NULL_TREE,
4800                         (idk == CP_ID_KIND_QUALIFIED
4801                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4802                         /*fn_p=*/NULL,
4803                         tf_warning_or_error));
4804                   }
4805                 else
4806                   postfix_expression
4807                     = finish_call_expr (postfix_expression, args,
4808                                         /*disallow_virtual=*/false,
4809                                         /*koenig_p=*/false,
4810                                         tf_warning_or_error);
4811               }
4812             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4813                      || TREE_CODE (postfix_expression) == MEMBER_REF
4814                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4815               postfix_expression = (build_offset_ref_call_from_tree
4816                                     (postfix_expression, args));
4817             else if (idk == CP_ID_KIND_QUALIFIED)
4818               /* A call to a static class member, or a namespace-scope
4819                  function.  */
4820               postfix_expression
4821                 = finish_call_expr (postfix_expression, args,
4822                                     /*disallow_virtual=*/true,
4823                                     koenig_p,
4824                                     tf_warning_or_error);
4825             else
4826               /* All other function calls.  */
4827               postfix_expression
4828                 = finish_call_expr (postfix_expression, args,
4829                                     /*disallow_virtual=*/false,
4830                                     koenig_p,
4831                                     tf_warning_or_error);
4832
4833             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4834             idk = CP_ID_KIND_NONE;
4835           }
4836           break;
4837
4838         case CPP_DOT:
4839         case CPP_DEREF:
4840           /* postfix-expression . template [opt] id-expression
4841              postfix-expression . pseudo-destructor-name
4842              postfix-expression -> template [opt] id-expression
4843              postfix-expression -> pseudo-destructor-name */
4844
4845           /* Consume the `.' or `->' operator.  */
4846           cp_lexer_consume_token (parser->lexer);
4847
4848           postfix_expression
4849             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4850                                                       postfix_expression,
4851                                                       false, &idk,
4852                                                       token->location);
4853
4854           is_member_access = true;
4855           break;
4856
4857         case CPP_PLUS_PLUS:
4858           /* postfix-expression ++  */
4859           /* Consume the `++' token.  */
4860           cp_lexer_consume_token (parser->lexer);
4861           /* Generate a representation for the complete expression.  */
4862           postfix_expression
4863             = finish_increment_expr (postfix_expression,
4864                                      POSTINCREMENT_EXPR);
4865           /* Increments may not appear in constant-expressions.  */
4866           if (cp_parser_non_integral_constant_expression (parser,
4867                                                           "an increment"))
4868             postfix_expression = error_mark_node;
4869           idk = CP_ID_KIND_NONE;
4870           is_member_access = false;
4871           break;
4872
4873         case CPP_MINUS_MINUS:
4874           /* postfix-expression -- */
4875           /* Consume the `--' token.  */
4876           cp_lexer_consume_token (parser->lexer);
4877           /* Generate a representation for the complete expression.  */
4878           postfix_expression
4879             = finish_increment_expr (postfix_expression,
4880                                      POSTDECREMENT_EXPR);
4881           /* Decrements may not appear in constant-expressions.  */
4882           if (cp_parser_non_integral_constant_expression (parser,
4883                                                           "a decrement"))
4884             postfix_expression = error_mark_node;
4885           idk = CP_ID_KIND_NONE;
4886           is_member_access = false;
4887           break;
4888
4889         default:
4890           if (pidk_return != NULL)
4891             * pidk_return = idk;
4892           if (member_access_only_p)
4893             return is_member_access? postfix_expression : error_mark_node;
4894           else
4895             return postfix_expression;
4896         }
4897     }
4898
4899   /* We should never get here.  */
4900   gcc_unreachable ();
4901   return error_mark_node;
4902 }
4903
4904 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4905    by cp_parser_builtin_offsetof.  We're looking for
4906
4907      postfix-expression [ expression ]
4908
4909    FOR_OFFSETOF is set if we're being called in that context, which
4910    changes how we deal with integer constant expressions.  */
4911
4912 static tree
4913 cp_parser_postfix_open_square_expression (cp_parser *parser,
4914                                           tree postfix_expression,
4915                                           bool for_offsetof)
4916 {
4917   tree index;
4918
4919   /* Consume the `[' token.  */
4920   cp_lexer_consume_token (parser->lexer);
4921
4922   /* Parse the index expression.  */
4923   /* ??? For offsetof, there is a question of what to allow here.  If
4924      offsetof is not being used in an integral constant expression context,
4925      then we *could* get the right answer by computing the value at runtime.
4926      If we are in an integral constant expression context, then we might
4927      could accept any constant expression; hard to say without analysis.
4928      Rather than open the barn door too wide right away, allow only integer
4929      constant expressions here.  */
4930   if (for_offsetof)
4931     index = cp_parser_constant_expression (parser, false, NULL);
4932   else
4933     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4934
4935   /* Look for the closing `]'.  */
4936   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4937
4938   /* Build the ARRAY_REF.  */
4939   postfix_expression = grok_array_decl (postfix_expression, index);
4940
4941   /* When not doing offsetof, array references are not permitted in
4942      constant-expressions.  */
4943   if (!for_offsetof
4944       && (cp_parser_non_integral_constant_expression
4945           (parser, "an array reference")))
4946     postfix_expression = error_mark_node;
4947
4948   return postfix_expression;
4949 }
4950
4951 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4952    by cp_parser_builtin_offsetof.  We're looking for
4953
4954      postfix-expression . template [opt] id-expression
4955      postfix-expression . pseudo-destructor-name
4956      postfix-expression -> template [opt] id-expression
4957      postfix-expression -> pseudo-destructor-name
4958
4959    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4960    limits what of the above we'll actually accept, but nevermind.
4961    TOKEN_TYPE is the "." or "->" token, which will already have been
4962    removed from the stream.  */
4963
4964 static tree
4965 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4966                                         enum cpp_ttype token_type,
4967                                         tree postfix_expression,
4968                                         bool for_offsetof, cp_id_kind *idk,
4969                                         location_t location)
4970 {
4971   tree name;
4972   bool dependent_p;
4973   bool pseudo_destructor_p;
4974   tree scope = NULL_TREE;
4975
4976   /* If this is a `->' operator, dereference the pointer.  */
4977   if (token_type == CPP_DEREF)
4978     postfix_expression = build_x_arrow (postfix_expression);
4979   /* Check to see whether or not the expression is type-dependent.  */
4980   dependent_p = type_dependent_expression_p (postfix_expression);
4981   /* The identifier following the `->' or `.' is not qualified.  */
4982   parser->scope = NULL_TREE;
4983   parser->qualifying_scope = NULL_TREE;
4984   parser->object_scope = NULL_TREE;
4985   *idk = CP_ID_KIND_NONE;
4986
4987   /* Enter the scope corresponding to the type of the object
4988      given by the POSTFIX_EXPRESSION.  */
4989   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4990     {
4991       scope = TREE_TYPE (postfix_expression);
4992       /* According to the standard, no expression should ever have
4993          reference type.  Unfortunately, we do not currently match
4994          the standard in this respect in that our internal representation
4995          of an expression may have reference type even when the standard
4996          says it does not.  Therefore, we have to manually obtain the
4997          underlying type here.  */
4998       scope = non_reference (scope);
4999       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5000       if (scope == unknown_type_node)
5001         {
5002           error ("%H%qE does not have class type", &location, postfix_expression);
5003           scope = NULL_TREE;
5004         }
5005       else
5006         scope = complete_type_or_else (scope, NULL_TREE);
5007       /* Let the name lookup machinery know that we are processing a
5008          class member access expression.  */
5009       parser->context->object_type = scope;
5010       /* If something went wrong, we want to be able to discern that case,
5011          as opposed to the case where there was no SCOPE due to the type
5012          of expression being dependent.  */
5013       if (!scope)
5014         scope = error_mark_node;
5015       /* If the SCOPE was erroneous, make the various semantic analysis
5016          functions exit quickly -- and without issuing additional error
5017          messages.  */
5018       if (scope == error_mark_node)
5019         postfix_expression = error_mark_node;
5020     }
5021
5022   /* Assume this expression is not a pseudo-destructor access.  */
5023   pseudo_destructor_p = false;
5024
5025   /* If the SCOPE is a scalar type, then, if this is a valid program,
5026      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5027      is type dependent, it can be pseudo-destructor-name or something else.
5028      Try to parse it as pseudo-destructor-name first.  */
5029   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5030     {
5031       tree s;
5032       tree type;
5033
5034       cp_parser_parse_tentatively (parser);
5035       /* Parse the pseudo-destructor-name.  */
5036       s = NULL_TREE;
5037       cp_parser_pseudo_destructor_name (parser, &s, &type);
5038       if (dependent_p
5039           && (cp_parser_error_occurred (parser)
5040               || TREE_CODE (type) != TYPE_DECL
5041               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5042         cp_parser_abort_tentative_parse (parser);
5043       else if (cp_parser_parse_definitely (parser))
5044         {
5045           pseudo_destructor_p = true;
5046           postfix_expression
5047             = finish_pseudo_destructor_expr (postfix_expression,
5048                                              s, TREE_TYPE (type));
5049         }
5050     }
5051
5052   if (!pseudo_destructor_p)
5053     {
5054       /* If the SCOPE is not a scalar type, we are looking at an
5055          ordinary class member access expression, rather than a
5056          pseudo-destructor-name.  */
5057       bool template_p;
5058       cp_token *token = cp_lexer_peek_token (parser->lexer);
5059       /* Parse the id-expression.  */
5060       name = (cp_parser_id_expression
5061               (parser,
5062                cp_parser_optional_template_keyword (parser),
5063                /*check_dependency_p=*/true,
5064                &template_p,
5065                /*declarator_p=*/false,
5066                /*optional_p=*/false));
5067       /* In general, build a SCOPE_REF if the member name is qualified.
5068          However, if the name was not dependent and has already been
5069          resolved; there is no need to build the SCOPE_REF.  For example;
5070
5071              struct X { void f(); };
5072              template <typename T> void f(T* t) { t->X::f(); }
5073
5074          Even though "t" is dependent, "X::f" is not and has been resolved
5075          to a BASELINK; there is no need to include scope information.  */
5076
5077       /* But we do need to remember that there was an explicit scope for
5078          virtual function calls.  */
5079       if (parser->scope)
5080         *idk = CP_ID_KIND_QUALIFIED;
5081
5082       /* If the name is a template-id that names a type, we will get a
5083          TYPE_DECL here.  That is invalid code.  */
5084       if (TREE_CODE (name) == TYPE_DECL)
5085         {
5086           error ("%Hinvalid use of %qD", &token->location, name);
5087           postfix_expression = error_mark_node;
5088         }
5089       else
5090         {
5091           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5092             {
5093               name = build_qualified_name (/*type=*/NULL_TREE,
5094                                            parser->scope,
5095                                            name,
5096                                            template_p);
5097               parser->scope = NULL_TREE;
5098               parser->qualifying_scope = NULL_TREE;
5099               parser->object_scope = NULL_TREE;
5100             }
5101           if (scope && name && BASELINK_P (name))
5102             adjust_result_of_qualified_name_lookup
5103               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5104           postfix_expression
5105             = finish_class_member_access_expr (postfix_expression, name,
5106                                                template_p, 
5107                                                tf_warning_or_error);
5108         }
5109     }
5110
5111   /* We no longer need to look up names in the scope of the object on
5112      the left-hand side of the `.' or `->' operator.  */
5113   parser->context->object_type = NULL_TREE;
5114
5115   /* Outside of offsetof, these operators may not appear in
5116      constant-expressions.  */
5117   if (!for_offsetof
5118       && (cp_parser_non_integral_constant_expression
5119           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5120     postfix_expression = error_mark_node;
5121
5122   return postfix_expression;
5123 }
5124
5125 /* Parse a parenthesized expression-list.
5126
5127    expression-list:
5128      assignment-expression
5129      expression-list, assignment-expression
5130
5131    attribute-list:
5132      expression-list
5133      identifier
5134      identifier, expression-list
5135
5136    CAST_P is true if this expression is the target of a cast.
5137
5138    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5139    argument pack.
5140
5141    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5142    representation of an assignment-expression.  Note that a TREE_LIST
5143    is returned even if there is only a single expression in the list.
5144    error_mark_node is returned if the ( and or ) are
5145    missing. NULL_TREE is returned on no expressions. The parentheses
5146    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5147    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5148    indicates whether or not all of the expressions in the list were
5149    constant.  */
5150
5151 static tree
5152 cp_parser_parenthesized_expression_list (cp_parser* parser,
5153                                          bool is_attribute_list,
5154                                          bool cast_p,
5155                                          bool allow_expansion_p,
5156                                          bool *non_constant_p)
5157 {
5158   tree expression_list = NULL_TREE;
5159   bool fold_expr_p = is_attribute_list;
5160   tree identifier = NULL_TREE;
5161   bool saved_greater_than_is_operator_p;
5162
5163   /* Assume all the expressions will be constant.  */
5164   if (non_constant_p)
5165     *non_constant_p = false;
5166
5167   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5168     return error_mark_node;
5169
5170   /* Within a parenthesized expression, a `>' token is always
5171      the greater-than operator.  */
5172   saved_greater_than_is_operator_p
5173     = parser->greater_than_is_operator_p;
5174   parser->greater_than_is_operator_p = true;
5175
5176   /* Consume expressions until there are no more.  */
5177   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5178     while (true)
5179       {
5180         tree expr;
5181
5182         /* At the beginning of attribute lists, check to see if the
5183            next token is an identifier.  */
5184         if (is_attribute_list
5185             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5186           {
5187             cp_token *token;
5188
5189             /* Consume the identifier.  */
5190             token = cp_lexer_consume_token (parser->lexer);
5191             /* Save the identifier.  */
5192             identifier = token->u.value;
5193           }
5194         else
5195           {
5196             bool expr_non_constant_p;
5197
5198             /* Parse the next assignment-expression.  */
5199             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5200               {
5201                 /* A braced-init-list.  */
5202                 maybe_warn_cpp0x ("extended initializer lists");
5203                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5204                 if (non_constant_p && expr_non_constant_p)
5205                   *non_constant_p = true;
5206               }
5207             else if (non_constant_p)
5208               {
5209                 expr = (cp_parser_constant_expression
5210                         (parser, /*allow_non_constant_p=*/true,
5211                          &expr_non_constant_p));
5212                 if (expr_non_constant_p)
5213                   *non_constant_p = true;
5214               }
5215             else
5216               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5217
5218             if (fold_expr_p)
5219               expr = fold_non_dependent_expr (expr);
5220
5221             /* If we have an ellipsis, then this is an expression
5222                expansion.  */
5223             if (allow_expansion_p
5224                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5225               {
5226                 /* Consume the `...'.  */
5227                 cp_lexer_consume_token (parser->lexer);
5228
5229                 /* Build the argument pack.  */
5230                 expr = make_pack_expansion (expr);
5231               }
5232
5233              /* Add it to the list.  We add error_mark_node
5234                 expressions to the list, so that we can still tell if
5235                 the correct form for a parenthesized expression-list
5236                 is found. That gives better errors.  */
5237             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5238
5239             if (expr == error_mark_node)
5240               goto skip_comma;
5241           }
5242
5243         /* After the first item, attribute lists look the same as
5244            expression lists.  */
5245         is_attribute_list = false;
5246
5247       get_comma:;
5248         /* If the next token isn't a `,', then we are done.  */
5249         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5250           break;
5251
5252         /* Otherwise, consume the `,' and keep going.  */
5253         cp_lexer_consume_token (parser->lexer);
5254       }
5255
5256   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5257     {
5258       int ending;
5259
5260     skip_comma:;
5261       /* We try and resync to an unnested comma, as that will give the
5262          user better diagnostics.  */
5263       ending = cp_parser_skip_to_closing_parenthesis (parser,
5264                                                       /*recovering=*/true,
5265                                                       /*or_comma=*/true,
5266                                                       /*consume_paren=*/true);
5267       if (ending < 0)
5268         goto get_comma;
5269       if (!ending)
5270         {
5271           parser->greater_than_is_operator_p
5272             = saved_greater_than_is_operator_p;
5273           return error_mark_node;
5274         }
5275     }
5276
5277   parser->greater_than_is_operator_p
5278     = saved_greater_than_is_operator_p;
5279
5280   /* We built up the list in reverse order so we must reverse it now.  */
5281   expression_list = nreverse (expression_list);
5282   if (identifier)
5283     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5284
5285   return expression_list;
5286 }
5287
5288 /* Parse a pseudo-destructor-name.
5289
5290    pseudo-destructor-name:
5291      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5292      :: [opt] nested-name-specifier template template-id :: ~ type-name
5293      :: [opt] nested-name-specifier [opt] ~ type-name
5294
5295    If either of the first two productions is used, sets *SCOPE to the
5296    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5297    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5298    or ERROR_MARK_NODE if the parse fails.  */
5299
5300 static void
5301 cp_parser_pseudo_destructor_name (cp_parser* parser,
5302                                   tree* scope,
5303                                   tree* type)
5304 {
5305   bool nested_name_specifier_p;
5306
5307   /* Assume that things will not work out.  */
5308   *type = error_mark_node;
5309
5310   /* Look for the optional `::' operator.  */
5311   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5312   /* Look for the optional nested-name-specifier.  */
5313   nested_name_specifier_p
5314     = (cp_parser_nested_name_specifier_opt (parser,
5315                                             /*typename_keyword_p=*/false,
5316                                             /*check_dependency_p=*/true,
5317                                             /*type_p=*/false,
5318                                             /*is_declaration=*/false)
5319        != NULL_TREE);
5320   /* Now, if we saw a nested-name-specifier, we might be doing the
5321      second production.  */
5322   if (nested_name_specifier_p
5323       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5324     {
5325       /* Consume the `template' keyword.  */
5326       cp_lexer_consume_token (parser->lexer);
5327       /* Parse the template-id.  */
5328       cp_parser_template_id (parser,
5329                              /*template_keyword_p=*/true,
5330                              /*check_dependency_p=*/false,
5331                              /*is_declaration=*/true);
5332       /* Look for the `::' token.  */
5333       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5334     }
5335   /* If the next token is not a `~', then there might be some
5336      additional qualification.  */
5337   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5338     {
5339       /* At this point, we're looking for "type-name :: ~".  The type-name
5340          must not be a class-name, since this is a pseudo-destructor.  So,
5341          it must be either an enum-name, or a typedef-name -- both of which
5342          are just identifiers.  So, we peek ahead to check that the "::"
5343          and "~" tokens are present; if they are not, then we can avoid
5344          calling type_name.  */
5345       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5346           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5347           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5348         {
5349           cp_parser_error (parser, "non-scalar type");
5350           return;
5351         }
5352
5353       /* Look for the type-name.  */
5354       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5355       if (*scope == error_mark_node)
5356         return;
5357
5358       /* Look for the `::' token.  */
5359       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5360     }
5361   else
5362     *scope = NULL_TREE;
5363
5364   /* Look for the `~'.  */
5365   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5366   /* Look for the type-name again.  We are not responsible for
5367      checking that it matches the first type-name.  */
5368   *type = cp_parser_nonclass_name (parser);
5369 }
5370
5371 /* Parse a unary-expression.
5372
5373    unary-expression:
5374      postfix-expression
5375      ++ cast-expression
5376      -- cast-expression
5377      unary-operator cast-expression
5378      sizeof unary-expression
5379      sizeof ( type-id )
5380      new-expression
5381      delete-expression
5382
5383    GNU Extensions:
5384
5385    unary-expression:
5386      __extension__ cast-expression
5387      __alignof__ unary-expression
5388      __alignof__ ( type-id )
5389      __real__ cast-expression
5390      __imag__ cast-expression
5391      && identifier
5392
5393    ADDRESS_P is true iff the unary-expression is appearing as the
5394    operand of the `&' operator.   CAST_P is true if this expression is
5395    the target of a cast.
5396
5397    Returns a representation of the expression.  */
5398
5399 static tree
5400 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5401                             cp_id_kind * pidk)
5402 {
5403   cp_token *token;
5404   enum tree_code unary_operator;
5405
5406   /* Peek at the next token.  */
5407   token = cp_lexer_peek_token (parser->lexer);
5408   /* Some keywords give away the kind of expression.  */
5409   if (token->type == CPP_KEYWORD)
5410     {
5411       enum rid keyword = token->keyword;
5412
5413       switch (keyword)
5414         {
5415         case RID_ALIGNOF:
5416         case RID_SIZEOF:
5417           {
5418             tree operand;
5419             enum tree_code op;
5420
5421             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5422             /* Consume the token.  */
5423             cp_lexer_consume_token (parser->lexer);
5424             /* Parse the operand.  */
5425             operand = cp_parser_sizeof_operand (parser, keyword);
5426
5427             if (TYPE_P (operand))
5428               return cxx_sizeof_or_alignof_type (operand, op, true);
5429             else
5430               return cxx_sizeof_or_alignof_expr (operand, op, true);
5431           }
5432
5433         case RID_NEW:
5434           return cp_parser_new_expression (parser);
5435
5436         case RID_DELETE:
5437           return cp_parser_delete_expression (parser);
5438
5439         case RID_EXTENSION:
5440           {
5441             /* The saved value of the PEDANTIC flag.  */
5442             int saved_pedantic;
5443             tree expr;
5444
5445             /* Save away the PEDANTIC flag.  */
5446             cp_parser_extension_opt (parser, &saved_pedantic);
5447             /* Parse the cast-expression.  */
5448             expr = cp_parser_simple_cast_expression (parser);
5449             /* Restore the PEDANTIC flag.  */
5450             pedantic = saved_pedantic;
5451
5452             return expr;
5453           }
5454
5455         case RID_REALPART:
5456         case RID_IMAGPART:
5457           {
5458             tree expression;
5459
5460             /* Consume the `__real__' or `__imag__' token.  */
5461             cp_lexer_consume_token (parser->lexer);
5462             /* Parse the cast-expression.  */
5463             expression = cp_parser_simple_cast_expression (parser);
5464             /* Create the complete representation.  */
5465             return build_x_unary_op ((keyword == RID_REALPART
5466                                       ? REALPART_EXPR : IMAGPART_EXPR),
5467                                      expression,
5468                                      tf_warning_or_error);
5469           }
5470           break;
5471
5472         default:
5473           break;
5474         }
5475     }
5476
5477   /* Look for the `:: new' and `:: delete', which also signal the
5478      beginning of a new-expression, or delete-expression,
5479      respectively.  If the next token is `::', then it might be one of
5480      these.  */
5481   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5482     {
5483       enum rid keyword;
5484
5485       /* See if the token after the `::' is one of the keywords in
5486          which we're interested.  */
5487       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5488       /* If it's `new', we have a new-expression.  */
5489       if (keyword == RID_NEW)
5490         return cp_parser_new_expression (parser);
5491       /* Similarly, for `delete'.  */
5492       else if (keyword == RID_DELETE)
5493         return cp_parser_delete_expression (parser);
5494     }
5495
5496   /* Look for a unary operator.  */
5497   unary_operator = cp_parser_unary_operator (token);
5498   /* The `++' and `--' operators can be handled similarly, even though
5499      they are not technically unary-operators in the grammar.  */
5500   if (unary_operator == ERROR_MARK)
5501     {
5502       if (token->type == CPP_PLUS_PLUS)
5503         unary_operator = PREINCREMENT_EXPR;
5504       else if (token->type == CPP_MINUS_MINUS)
5505         unary_operator = PREDECREMENT_EXPR;
5506       /* Handle the GNU address-of-label extension.  */
5507       else if (cp_parser_allow_gnu_extensions_p (parser)
5508                && token->type == CPP_AND_AND)
5509         {
5510           tree identifier;
5511           tree expression;
5512           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5513
5514           /* Consume the '&&' token.  */
5515           cp_lexer_consume_token (parser->lexer);
5516           /* Look for the identifier.  */
5517           identifier = cp_parser_identifier (parser);
5518           /* Create an expression representing the address.  */
5519           expression = finish_label_address_expr (identifier, loc);
5520           if (cp_parser_non_integral_constant_expression (parser,
5521                                                 "the address of a label"))
5522             expression = error_mark_node;
5523           return expression;
5524         }
5525     }
5526   if (unary_operator != ERROR_MARK)
5527     {
5528       tree cast_expression;
5529       tree expression = error_mark_node;
5530       const char *non_constant_p = NULL;
5531
5532       /* Consume the operator token.  */
5533       token = cp_lexer_consume_token (parser->lexer);
5534       /* Parse the cast-expression.  */
5535       cast_expression
5536         = cp_parser_cast_expression (parser,
5537                                      unary_operator == ADDR_EXPR,
5538                                      /*cast_p=*/false, pidk);
5539       /* Now, build an appropriate representation.  */
5540       switch (unary_operator)
5541         {
5542         case INDIRECT_REF:
5543           non_constant_p = "%<*%>";
5544           expression = build_x_indirect_ref (cast_expression, "unary *",
5545                                              tf_warning_or_error);
5546           break;
5547
5548         case ADDR_EXPR:
5549           non_constant_p = "%<&%>";
5550           /* Fall through.  */
5551         case BIT_NOT_EXPR:
5552           expression = build_x_unary_op (unary_operator, cast_expression,
5553                                          tf_warning_or_error);
5554           break;
5555
5556         case PREINCREMENT_EXPR:
5557         case PREDECREMENT_EXPR:
5558           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5559                             ? "%<++%>" : "%<--%>");
5560           /* Fall through.  */
5561         case UNARY_PLUS_EXPR:
5562         case NEGATE_EXPR:
5563         case TRUTH_NOT_EXPR:
5564           expression = finish_unary_op_expr (unary_operator, cast_expression);
5565           break;
5566
5567         default:
5568           gcc_unreachable ();
5569         }
5570
5571       if (non_constant_p
5572           && cp_parser_non_integral_constant_expression (parser,
5573                                                          non_constant_p))
5574         expression = error_mark_node;
5575
5576       return expression;
5577     }
5578
5579   return cp_parser_postfix_expression (parser, address_p, cast_p,
5580                                        /*member_access_only_p=*/false,
5581                                        pidk);
5582 }
5583
5584 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5585    unary-operator, the corresponding tree code is returned.  */
5586
5587 static enum tree_code
5588 cp_parser_unary_operator (cp_token* token)
5589 {
5590   switch (token->type)
5591     {
5592     case CPP_MULT:
5593       return INDIRECT_REF;
5594
5595     case CPP_AND:
5596       return ADDR_EXPR;
5597
5598     case CPP_PLUS:
5599       return UNARY_PLUS_EXPR;
5600
5601     case CPP_MINUS:
5602       return NEGATE_EXPR;
5603
5604     case CPP_NOT:
5605       return TRUTH_NOT_EXPR;
5606
5607     case CPP_COMPL:
5608       return BIT_NOT_EXPR;
5609
5610     default:
5611       return ERROR_MARK;
5612     }
5613 }
5614
5615 /* Parse a new-expression.
5616
5617    new-expression:
5618      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5619      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5620
5621    Returns a representation of the expression.  */
5622
5623 static tree
5624 cp_parser_new_expression (cp_parser* parser)
5625 {
5626   bool global_scope_p;
5627   tree placement;
5628   tree type;
5629   tree initializer;
5630   tree nelts;
5631
5632   /* Look for the optional `::' operator.  */
5633   global_scope_p
5634     = (cp_parser_global_scope_opt (parser,
5635                                    /*current_scope_valid_p=*/false)
5636        != NULL_TREE);
5637   /* Look for the `new' operator.  */
5638   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5639   /* There's no easy way to tell a new-placement from the
5640      `( type-id )' construct.  */
5641   cp_parser_parse_tentatively (parser);
5642   /* Look for a new-placement.  */
5643   placement = cp_parser_new_placement (parser);
5644   /* If that didn't work out, there's no new-placement.  */
5645   if (!cp_parser_parse_definitely (parser))
5646     placement = NULL_TREE;
5647
5648   /* If the next token is a `(', then we have a parenthesized
5649      type-id.  */
5650   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5651     {
5652       cp_token *token;
5653       /* Consume the `('.  */
5654       cp_lexer_consume_token (parser->lexer);
5655       /* Parse the type-id.  */
5656       type = cp_parser_type_id (parser);
5657       /* Look for the closing `)'.  */
5658       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5659       token = cp_lexer_peek_token (parser->lexer);
5660       /* There should not be a direct-new-declarator in this production,
5661          but GCC used to allowed this, so we check and emit a sensible error
5662          message for this case.  */
5663       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5664         {
5665           error ("%Harray bound forbidden after parenthesized type-id",
5666                  &token->location);
5667           inform (token->location, 
5668                   "try removing the parentheses around the type-id");
5669           cp_parser_direct_new_declarator (parser);
5670         }
5671       nelts = NULL_TREE;
5672     }
5673   /* Otherwise, there must be a new-type-id.  */
5674   else
5675     type = cp_parser_new_type_id (parser, &nelts);
5676
5677   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5678   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5679       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5680     initializer = cp_parser_new_initializer (parser);
5681   else
5682     initializer = NULL_TREE;
5683
5684   /* A new-expression may not appear in an integral constant
5685      expression.  */
5686   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5687     return error_mark_node;
5688
5689   /* Create a representation of the new-expression.  */
5690   return build_new (placement, type, nelts, initializer, global_scope_p,
5691                     tf_warning_or_error);
5692 }
5693
5694 /* Parse a new-placement.
5695
5696    new-placement:
5697      ( expression-list )
5698
5699    Returns the same representation as for an expression-list.  */
5700
5701 static tree
5702 cp_parser_new_placement (cp_parser* parser)
5703 {
5704   tree expression_list;
5705
5706   /* Parse the expression-list.  */
5707   expression_list = (cp_parser_parenthesized_expression_list
5708                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5709                       /*non_constant_p=*/NULL));
5710
5711   return expression_list;
5712 }
5713
5714 /* Parse a new-type-id.
5715
5716    new-type-id:
5717      type-specifier-seq new-declarator [opt]
5718
5719    Returns the TYPE allocated.  If the new-type-id indicates an array
5720    type, *NELTS is set to the number of elements in the last array
5721    bound; the TYPE will not include the last array bound.  */
5722
5723 static tree
5724 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5725 {
5726   cp_decl_specifier_seq type_specifier_seq;
5727   cp_declarator *new_declarator;
5728   cp_declarator *declarator;
5729   cp_declarator *outer_declarator;
5730   const char *saved_message;
5731   tree type;
5732
5733   /* The type-specifier sequence must not contain type definitions.
5734      (It cannot contain declarations of new types either, but if they
5735      are not definitions we will catch that because they are not
5736      complete.)  */
5737   saved_message = parser->type_definition_forbidden_message;
5738   parser->type_definition_forbidden_message
5739     = "types may not be defined in a new-type-id";
5740   /* Parse the type-specifier-seq.  */
5741   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5742                                 &type_specifier_seq);
5743   /* Restore the old message.  */
5744   parser->type_definition_forbidden_message = saved_message;
5745   /* Parse the new-declarator.  */
5746   new_declarator = cp_parser_new_declarator_opt (parser);
5747
5748   /* Determine the number of elements in the last array dimension, if
5749      any.  */
5750   *nelts = NULL_TREE;
5751   /* Skip down to the last array dimension.  */
5752   declarator = new_declarator;
5753   outer_declarator = NULL;
5754   while (declarator && (declarator->kind == cdk_pointer
5755                         || declarator->kind == cdk_ptrmem))
5756     {
5757       outer_declarator = declarator;
5758       declarator = declarator->declarator;
5759     }
5760   while (declarator
5761          && declarator->kind == cdk_array
5762          && declarator->declarator
5763          && declarator->declarator->kind == cdk_array)
5764     {
5765       outer_declarator = declarator;
5766       declarator = declarator->declarator;
5767     }
5768
5769   if (declarator && declarator->kind == cdk_array)
5770     {
5771       *nelts = declarator->u.array.bounds;
5772       if (*nelts == error_mark_node)
5773         *nelts = integer_one_node;
5774
5775       if (outer_declarator)
5776         outer_declarator->declarator = declarator->declarator;
5777       else
5778         new_declarator = NULL;
5779     }
5780
5781   type = groktypename (&type_specifier_seq, new_declarator, false);
5782   return type;
5783 }
5784
5785 /* Parse an (optional) new-declarator.
5786
5787    new-declarator:
5788      ptr-operator new-declarator [opt]
5789      direct-new-declarator
5790
5791    Returns the declarator.  */
5792
5793 static cp_declarator *
5794 cp_parser_new_declarator_opt (cp_parser* parser)
5795 {
5796   enum tree_code code;
5797   tree type;
5798   cp_cv_quals cv_quals;
5799
5800   /* We don't know if there's a ptr-operator next, or not.  */
5801   cp_parser_parse_tentatively (parser);
5802   /* Look for a ptr-operator.  */
5803   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5804   /* If that worked, look for more new-declarators.  */
5805   if (cp_parser_parse_definitely (parser))
5806     {
5807       cp_declarator *declarator;
5808
5809       /* Parse another optional declarator.  */
5810       declarator = cp_parser_new_declarator_opt (parser);
5811
5812       return cp_parser_make_indirect_declarator
5813         (code, type, cv_quals, declarator);
5814     }
5815
5816   /* If the next token is a `[', there is a direct-new-declarator.  */
5817   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5818     return cp_parser_direct_new_declarator (parser);
5819
5820   return NULL;
5821 }
5822
5823 /* Parse a direct-new-declarator.
5824
5825    direct-new-declarator:
5826      [ expression ]
5827      direct-new-declarator [constant-expression]
5828
5829    */
5830
5831 static cp_declarator *
5832 cp_parser_direct_new_declarator (cp_parser* parser)
5833 {
5834   cp_declarator *declarator = NULL;
5835
5836   while (true)
5837     {
5838       tree expression;
5839
5840       /* Look for the opening `['.  */
5841       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5842       /* The first expression is not required to be constant.  */
5843       if (!declarator)
5844         {
5845           cp_token *token = cp_lexer_peek_token (parser->lexer);
5846           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5847           /* The standard requires that the expression have integral
5848              type.  DR 74 adds enumeration types.  We believe that the
5849              real intent is that these expressions be handled like the
5850              expression in a `switch' condition, which also allows
5851              classes with a single conversion to integral or
5852              enumeration type.  */
5853           if (!processing_template_decl)
5854             {
5855               expression
5856                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5857                                               expression,
5858                                               /*complain=*/true);
5859               if (!expression)
5860                 {
5861                   error ("%Hexpression in new-declarator must have integral "
5862                          "or enumeration type", &token->location);
5863                   expression = error_mark_node;
5864                 }
5865             }
5866         }
5867       /* But all the other expressions must be.  */
5868       else
5869         expression
5870           = cp_parser_constant_expression (parser,
5871                                            /*allow_non_constant=*/false,
5872                                            NULL);
5873       /* Look for the closing `]'.  */
5874       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5875
5876       /* Add this bound to the declarator.  */
5877       declarator = make_array_declarator (declarator, expression);
5878
5879       /* If the next token is not a `[', then there are no more
5880          bounds.  */
5881       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5882         break;
5883     }
5884
5885   return declarator;
5886 }
5887
5888 /* Parse a new-initializer.
5889
5890    new-initializer:
5891      ( expression-list [opt] )
5892      braced-init-list
5893
5894    Returns a representation of the expression-list.  If there is no
5895    expression-list, VOID_ZERO_NODE is returned.  */
5896
5897 static tree
5898 cp_parser_new_initializer (cp_parser* parser)
5899 {
5900   tree expression_list;
5901
5902   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5903     {
5904       bool expr_non_constant_p;
5905       maybe_warn_cpp0x ("extended initializer lists");
5906       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5907       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5908       expression_list = build_tree_list (NULL_TREE, expression_list);
5909     }
5910   else
5911     expression_list = (cp_parser_parenthesized_expression_list
5912                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5913                         /*non_constant_p=*/NULL));
5914   if (!expression_list)
5915     expression_list = void_zero_node;
5916
5917   return expression_list;
5918 }
5919
5920 /* Parse a delete-expression.
5921
5922    delete-expression:
5923      :: [opt] delete cast-expression
5924      :: [opt] delete [ ] cast-expression
5925
5926    Returns a representation of the expression.  */
5927
5928 static tree
5929 cp_parser_delete_expression (cp_parser* parser)
5930 {
5931   bool global_scope_p;
5932   bool array_p;
5933   tree expression;
5934
5935   /* Look for the optional `::' operator.  */
5936   global_scope_p
5937     = (cp_parser_global_scope_opt (parser,
5938                                    /*current_scope_valid_p=*/false)
5939        != NULL_TREE);
5940   /* Look for the `delete' keyword.  */
5941   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5942   /* See if the array syntax is in use.  */
5943   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5944     {
5945       /* Consume the `[' token.  */
5946       cp_lexer_consume_token (parser->lexer);
5947       /* Look for the `]' token.  */
5948       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5949       /* Remember that this is the `[]' construct.  */
5950       array_p = true;
5951     }
5952   else
5953     array_p = false;
5954
5955   /* Parse the cast-expression.  */
5956   expression = cp_parser_simple_cast_expression (parser);
5957
5958   /* A delete-expression may not appear in an integral constant
5959      expression.  */
5960   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5961     return error_mark_node;
5962
5963   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5964 }
5965
5966 /* Returns true if TOKEN may start a cast-expression and false
5967    otherwise.  */
5968
5969 static bool
5970 cp_parser_token_starts_cast_expression (cp_token *token)
5971 {
5972   switch (token->type)
5973     {
5974     case CPP_COMMA:
5975     case CPP_SEMICOLON:
5976     case CPP_QUERY:
5977     case CPP_COLON:
5978     case CPP_CLOSE_SQUARE:
5979     case CPP_CLOSE_PAREN:
5980     case CPP_CLOSE_BRACE:
5981     case CPP_DOT:
5982     case CPP_DOT_STAR:
5983     case CPP_DEREF:
5984     case CPP_DEREF_STAR:
5985     case CPP_DIV:
5986     case CPP_MOD:
5987     case CPP_LSHIFT:
5988     case CPP_RSHIFT:
5989     case CPP_LESS:
5990     case CPP_GREATER:
5991     case CPP_LESS_EQ:
5992     case CPP_GREATER_EQ:
5993     case CPP_EQ_EQ:
5994     case CPP_NOT_EQ:
5995     case CPP_EQ:
5996     case CPP_MULT_EQ:
5997     case CPP_DIV_EQ:
5998     case CPP_MOD_EQ:
5999     case CPP_PLUS_EQ:
6000     case CPP_MINUS_EQ:
6001     case CPP_RSHIFT_EQ:
6002     case CPP_LSHIFT_EQ:
6003     case CPP_AND_EQ:
6004     case CPP_XOR_EQ:
6005     case CPP_OR_EQ:
6006     case CPP_XOR:
6007     case CPP_OR:
6008     case CPP_OR_OR:
6009     case CPP_EOF:
6010       return false;
6011
6012       /* '[' may start a primary-expression in obj-c++.  */
6013     case CPP_OPEN_SQUARE:
6014       return c_dialect_objc ();
6015
6016     default:
6017       return true;
6018     }
6019 }
6020
6021 /* Parse a cast-expression.
6022
6023    cast-expression:
6024      unary-expression
6025      ( type-id ) cast-expression
6026
6027    ADDRESS_P is true iff the unary-expression is appearing as the
6028    operand of the `&' operator.   CAST_P is true if this expression is
6029    the target of a cast.
6030
6031    Returns a representation of the expression.  */
6032
6033 static tree
6034 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6035                            cp_id_kind * pidk)
6036 {
6037   /* If it's a `(', then we might be looking at a cast.  */
6038   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6039     {
6040       tree type = NULL_TREE;
6041       tree expr = NULL_TREE;
6042       bool compound_literal_p;
6043       const char *saved_message;
6044
6045       /* There's no way to know yet whether or not this is a cast.
6046          For example, `(int (3))' is a unary-expression, while `(int)
6047          3' is a cast.  So, we resort to parsing tentatively.  */
6048       cp_parser_parse_tentatively (parser);
6049       /* Types may not be defined in a cast.  */
6050       saved_message = parser->type_definition_forbidden_message;
6051       parser->type_definition_forbidden_message
6052         = "types may not be defined in casts";
6053       /* Consume the `('.  */
6054       cp_lexer_consume_token (parser->lexer);
6055       /* A very tricky bit is that `(struct S) { 3 }' is a
6056          compound-literal (which we permit in C++ as an extension).
6057          But, that construct is not a cast-expression -- it is a
6058          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6059          is legal; if the compound-literal were a cast-expression,
6060          you'd need an extra set of parentheses.)  But, if we parse
6061          the type-id, and it happens to be a class-specifier, then we
6062          will commit to the parse at that point, because we cannot
6063          undo the action that is done when creating a new class.  So,
6064          then we cannot back up and do a postfix-expression.
6065
6066          Therefore, we scan ahead to the closing `)', and check to see
6067          if the token after the `)' is a `{'.  If so, we are not
6068          looking at a cast-expression.
6069
6070          Save tokens so that we can put them back.  */
6071       cp_lexer_save_tokens (parser->lexer);
6072       /* Skip tokens until the next token is a closing parenthesis.
6073          If we find the closing `)', and the next token is a `{', then
6074          we are looking at a compound-literal.  */
6075       compound_literal_p
6076         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6077                                                   /*consume_paren=*/true)
6078            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6079       /* Roll back the tokens we skipped.  */
6080       cp_lexer_rollback_tokens (parser->lexer);
6081       /* If we were looking at a compound-literal, simulate an error
6082          so that the call to cp_parser_parse_definitely below will
6083          fail.  */
6084       if (compound_literal_p)
6085         cp_parser_simulate_error (parser);
6086       else
6087         {
6088           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6089           parser->in_type_id_in_expr_p = true;
6090           /* Look for the type-id.  */
6091           type = cp_parser_type_id (parser);
6092           /* Look for the closing `)'.  */
6093           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6094           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6095         }
6096
6097       /* Restore the saved message.  */
6098       parser->type_definition_forbidden_message = saved_message;
6099
6100       /* At this point this can only be either a cast or a
6101          parenthesized ctor such as `(T ())' that looks like a cast to
6102          function returning T.  */
6103       if (!cp_parser_error_occurred (parser)
6104           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6105                                                      (parser->lexer)))
6106         {
6107           cp_parser_parse_definitely (parser);
6108           expr = cp_parser_cast_expression (parser,
6109                                             /*address_p=*/false,
6110                                             /*cast_p=*/true, pidk);
6111
6112           /* Warn about old-style casts, if so requested.  */
6113           if (warn_old_style_cast
6114               && !in_system_header
6115               && !VOID_TYPE_P (type)
6116               && current_lang_name != lang_name_c)
6117             warning (OPT_Wold_style_cast, "use of old-style cast");
6118
6119           /* Only type conversions to integral or enumeration types
6120              can be used in constant-expressions.  */
6121           if (!cast_valid_in_integral_constant_expression_p (type)
6122               && (cp_parser_non_integral_constant_expression
6123                   (parser,
6124                    "a cast to a type other than an integral or "
6125                    "enumeration type")))
6126             return error_mark_node;
6127
6128           /* Perform the cast.  */
6129           expr = build_c_cast (type, expr);
6130           return expr;
6131         }
6132       else 
6133         cp_parser_abort_tentative_parse (parser);
6134     }
6135
6136   /* If we get here, then it's not a cast, so it must be a
6137      unary-expression.  */
6138   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6139 }
6140
6141 /* Parse a binary expression of the general form:
6142
6143    pm-expression:
6144      cast-expression
6145      pm-expression .* cast-expression
6146      pm-expression ->* cast-expression
6147
6148    multiplicative-expression:
6149      pm-expression
6150      multiplicative-expression * pm-expression
6151      multiplicative-expression / pm-expression
6152      multiplicative-expression % pm-expression
6153
6154    additive-expression:
6155      multiplicative-expression
6156      additive-expression + multiplicative-expression
6157      additive-expression - multiplicative-expression
6158
6159    shift-expression:
6160      additive-expression
6161      shift-expression << additive-expression
6162      shift-expression >> additive-expression
6163
6164    relational-expression:
6165      shift-expression
6166      relational-expression < shift-expression
6167      relational-expression > shift-expression
6168      relational-expression <= shift-expression
6169      relational-expression >= shift-expression
6170
6171   GNU Extension:
6172
6173    relational-expression:
6174      relational-expression <? shift-expression
6175      relational-expression >? shift-expression
6176
6177    equality-expression:
6178      relational-expression
6179      equality-expression == relational-expression
6180      equality-expression != relational-expression
6181
6182    and-expression:
6183      equality-expression
6184      and-expression & equality-expression
6185
6186    exclusive-or-expression:
6187      and-expression
6188      exclusive-or-expression ^ and-expression
6189
6190    inclusive-or-expression:
6191      exclusive-or-expression
6192      inclusive-or-expression | exclusive-or-expression
6193
6194    logical-and-expression:
6195      inclusive-or-expression
6196      logical-and-expression && inclusive-or-expression
6197
6198    logical-or-expression:
6199      logical-and-expression
6200      logical-or-expression || logical-and-expression
6201
6202    All these are implemented with a single function like:
6203
6204    binary-expression:
6205      simple-cast-expression
6206      binary-expression <token> binary-expression
6207
6208    CAST_P is true if this expression is the target of a cast.
6209
6210    The binops_by_token map is used to get the tree codes for each <token> type.
6211    binary-expressions are associated according to a precedence table.  */
6212
6213 #define TOKEN_PRECEDENCE(token)                              \
6214 (((token->type == CPP_GREATER                                \
6215    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6216   && !parser->greater_than_is_operator_p)                    \
6217  ? PREC_NOT_OPERATOR                                         \
6218  : binops_by_token[token->type].prec)
6219
6220 static tree
6221 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6222                              bool no_toplevel_fold_p,
6223                              enum cp_parser_prec prec,
6224                              cp_id_kind * pidk)
6225 {
6226   cp_parser_expression_stack stack;
6227   cp_parser_expression_stack_entry *sp = &stack[0];
6228   tree lhs, rhs;
6229   cp_token *token;
6230   enum tree_code tree_type, lhs_type, rhs_type;
6231   enum cp_parser_prec new_prec, lookahead_prec;
6232   bool overloaded_p;
6233
6234   /* Parse the first expression.  */
6235   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6236   lhs_type = ERROR_MARK;
6237
6238   for (;;)
6239     {
6240       /* Get an operator token.  */
6241       token = cp_lexer_peek_token (parser->lexer);
6242
6243       if (warn_cxx0x_compat
6244           && token->type == CPP_RSHIFT
6245           && !parser->greater_than_is_operator_p)
6246         {
6247           warning (OPT_Wc__0x_compat, 
6248                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6249                    &token->location);
6250           warning (OPT_Wc__0x_compat, 
6251                    "suggest parentheses around %<>>%> expression");
6252         }
6253
6254       new_prec = TOKEN_PRECEDENCE (token);
6255
6256       /* Popping an entry off the stack means we completed a subexpression:
6257          - either we found a token which is not an operator (`>' where it is not
6258            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6259            will happen repeatedly;
6260          - or, we found an operator which has lower priority.  This is the case
6261            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6262            parsing `3 * 4'.  */
6263       if (new_prec <= prec)
6264         {
6265           if (sp == stack)
6266             break;
6267           else
6268             goto pop;
6269         }
6270
6271      get_rhs:
6272       tree_type = binops_by_token[token->type].tree_type;
6273
6274       /* We used the operator token.  */
6275       cp_lexer_consume_token (parser->lexer);
6276
6277       /* Extract another operand.  It may be the RHS of this expression
6278          or the LHS of a new, higher priority expression.  */
6279       rhs = cp_parser_simple_cast_expression (parser);
6280       rhs_type = ERROR_MARK;
6281
6282       /* Get another operator token.  Look up its precedence to avoid
6283          building a useless (immediately popped) stack entry for common
6284          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6285       token = cp_lexer_peek_token (parser->lexer);
6286       lookahead_prec = TOKEN_PRECEDENCE (token);
6287       if (lookahead_prec > new_prec)
6288         {
6289           /* ... and prepare to parse the RHS of the new, higher priority
6290              expression.  Since precedence levels on the stack are
6291              monotonically increasing, we do not have to care about
6292              stack overflows.  */
6293           sp->prec = prec;
6294           sp->tree_type = tree_type;
6295           sp->lhs = lhs;
6296           sp->lhs_type = lhs_type;
6297           sp++;
6298           lhs = rhs;
6299           lhs_type = rhs_type;
6300           prec = new_prec;
6301           new_prec = lookahead_prec;
6302           goto get_rhs;
6303
6304          pop:
6305           lookahead_prec = new_prec;
6306           /* If the stack is not empty, we have parsed into LHS the right side
6307              (`4' in the example above) of an expression we had suspended.
6308              We can use the information on the stack to recover the LHS (`3')
6309              from the stack together with the tree code (`MULT_EXPR'), and
6310              the precedence of the higher level subexpression
6311              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6312              which will be used to actually build the additive expression.  */
6313           --sp;
6314           prec = sp->prec;
6315           tree_type = sp->tree_type;
6316           rhs = lhs;
6317           rhs_type = lhs_type;
6318           lhs = sp->lhs;
6319           lhs_type = sp->lhs_type;
6320         }
6321
6322       overloaded_p = false;
6323       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6324          ERROR_MARK for everything that is not a binary expression.
6325          This makes warn_about_parentheses miss some warnings that
6326          involve unary operators.  For unary expressions we should
6327          pass the correct tree_code unless the unary expression was
6328          surrounded by parentheses.
6329       */
6330       if (no_toplevel_fold_p
6331           && lookahead_prec <= prec
6332           && sp == stack
6333           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6334         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6335       else
6336         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6337                                  &overloaded_p, tf_warning_or_error);
6338       lhs_type = tree_type;
6339
6340       /* If the binary operator required the use of an overloaded operator,
6341          then this expression cannot be an integral constant-expression.
6342          An overloaded operator can be used even if both operands are
6343          otherwise permissible in an integral constant-expression if at
6344          least one of the operands is of enumeration type.  */
6345
6346       if (overloaded_p
6347           && (cp_parser_non_integral_constant_expression
6348               (parser, "calls to overloaded operators")))
6349         return error_mark_node;
6350     }
6351
6352   return lhs;
6353 }
6354
6355
6356 /* Parse the `? expression : assignment-expression' part of a
6357    conditional-expression.  The LOGICAL_OR_EXPR is the
6358    logical-or-expression that started the conditional-expression.
6359    Returns a representation of the entire conditional-expression.
6360
6361    This routine is used by cp_parser_assignment_expression.
6362
6363      ? expression : assignment-expression
6364
6365    GNU Extensions:
6366
6367      ? : assignment-expression */
6368
6369 static tree
6370 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6371 {
6372   tree expr;
6373   tree assignment_expr;
6374
6375   /* Consume the `?' token.  */
6376   cp_lexer_consume_token (parser->lexer);
6377   if (cp_parser_allow_gnu_extensions_p (parser)
6378       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6379     /* Implicit true clause.  */
6380     expr = NULL_TREE;
6381   else
6382     /* Parse the expression.  */
6383     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6384
6385   /* The next token should be a `:'.  */
6386   cp_parser_require (parser, CPP_COLON, "%<:%>");
6387   /* Parse the assignment-expression.  */
6388   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6389
6390   /* Build the conditional-expression.  */
6391   return build_x_conditional_expr (logical_or_expr,
6392                                    expr,
6393                                    assignment_expr,
6394                                    tf_warning_or_error);
6395 }
6396
6397 /* Parse an assignment-expression.
6398
6399    assignment-expression:
6400      conditional-expression
6401      logical-or-expression assignment-operator assignment_expression
6402      throw-expression
6403
6404    CAST_P is true if this expression is the target of a cast.
6405
6406    Returns a representation for the expression.  */
6407
6408 static tree
6409 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6410                                  cp_id_kind * pidk)
6411 {
6412   tree expr;
6413
6414   /* If the next token is the `throw' keyword, then we're looking at
6415      a throw-expression.  */
6416   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6417     expr = cp_parser_throw_expression (parser);
6418   /* Otherwise, it must be that we are looking at a
6419      logical-or-expression.  */
6420   else
6421     {
6422       /* Parse the binary expressions (logical-or-expression).  */
6423       expr = cp_parser_binary_expression (parser, cast_p, false,
6424                                           PREC_NOT_OPERATOR, pidk);
6425       /* If the next token is a `?' then we're actually looking at a
6426          conditional-expression.  */
6427       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6428         return cp_parser_question_colon_clause (parser, expr);
6429       else
6430         {
6431           enum tree_code assignment_operator;
6432
6433           /* If it's an assignment-operator, we're using the second
6434              production.  */
6435           assignment_operator
6436             = cp_parser_assignment_operator_opt (parser);
6437           if (assignment_operator != ERROR_MARK)
6438             {
6439               bool non_constant_p;
6440
6441               /* Parse the right-hand side of the assignment.  */
6442               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6443
6444               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6445                 maybe_warn_cpp0x ("extended initializer lists");
6446
6447               /* An assignment may not appear in a
6448                  constant-expression.  */
6449               if (cp_parser_non_integral_constant_expression (parser,
6450                                                               "an assignment"))
6451                 return error_mark_node;
6452               /* Build the assignment expression.  */
6453               expr = build_x_modify_expr (expr,
6454                                           assignment_operator,
6455                                           rhs,
6456                                           tf_warning_or_error);
6457             }
6458         }
6459     }
6460
6461   return expr;
6462 }
6463
6464 /* Parse an (optional) assignment-operator.
6465
6466    assignment-operator: one of
6467      = *= /= %= += -= >>= <<= &= ^= |=
6468
6469    GNU Extension:
6470
6471    assignment-operator: one of
6472      <?= >?=
6473
6474    If the next token is an assignment operator, the corresponding tree
6475    code is returned, and the token is consumed.  For example, for
6476    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6477    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6478    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6479    operator, ERROR_MARK is returned.  */
6480
6481 static enum tree_code
6482 cp_parser_assignment_operator_opt (cp_parser* parser)
6483 {
6484   enum tree_code op;
6485   cp_token *token;
6486
6487   /* Peek at the next token.  */
6488   token = cp_lexer_peek_token (parser->lexer);
6489
6490   switch (token->type)
6491     {
6492     case CPP_EQ:
6493       op = NOP_EXPR;
6494       break;
6495
6496     case CPP_MULT_EQ:
6497       op = MULT_EXPR;
6498       break;
6499
6500     case CPP_DIV_EQ:
6501       op = TRUNC_DIV_EXPR;
6502       break;
6503
6504     case CPP_MOD_EQ:
6505       op = TRUNC_MOD_EXPR;
6506       break;
6507
6508     case CPP_PLUS_EQ:
6509       op = PLUS_EXPR;
6510       break;
6511
6512     case CPP_MINUS_EQ:
6513       op = MINUS_EXPR;
6514       break;
6515
6516     case CPP_RSHIFT_EQ:
6517       op = RSHIFT_EXPR;
6518       break;
6519
6520     case CPP_LSHIFT_EQ:
6521       op = LSHIFT_EXPR;
6522       break;
6523
6524     case CPP_AND_EQ:
6525       op = BIT_AND_EXPR;
6526       break;
6527
6528     case CPP_XOR_EQ:
6529       op = BIT_XOR_EXPR;
6530       break;
6531
6532     case CPP_OR_EQ:
6533       op = BIT_IOR_EXPR;
6534       break;
6535
6536     default:
6537       /* Nothing else is an assignment operator.  */
6538       op = ERROR_MARK;
6539     }
6540
6541   /* If it was an assignment operator, consume it.  */
6542   if (op != ERROR_MARK)
6543     cp_lexer_consume_token (parser->lexer);
6544
6545   return op;
6546 }
6547
6548 /* Parse an expression.
6549
6550    expression:
6551      assignment-expression
6552      expression , assignment-expression
6553
6554    CAST_P is true if this expression is the target of a cast.
6555
6556    Returns a representation of the expression.  */
6557
6558 static tree
6559 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6560 {
6561   tree expression = NULL_TREE;
6562
6563   while (true)
6564     {
6565       tree assignment_expression;
6566
6567       /* Parse the next assignment-expression.  */
6568       assignment_expression
6569         = cp_parser_assignment_expression (parser, cast_p, pidk);
6570       /* If this is the first assignment-expression, we can just
6571          save it away.  */
6572       if (!expression)
6573         expression = assignment_expression;
6574       else
6575         expression = build_x_compound_expr (expression,
6576                                             assignment_expression,
6577                                             tf_warning_or_error);
6578       /* If the next token is not a comma, then we are done with the
6579          expression.  */
6580       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6581         break;
6582       /* Consume the `,'.  */
6583       cp_lexer_consume_token (parser->lexer);
6584       /* A comma operator cannot appear in a constant-expression.  */
6585       if (cp_parser_non_integral_constant_expression (parser,
6586                                                       "a comma operator"))
6587         expression = error_mark_node;
6588     }
6589
6590   return expression;
6591 }
6592
6593 /* Parse a constant-expression.
6594
6595    constant-expression:
6596      conditional-expression
6597
6598   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6599   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6600   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6601   is false, NON_CONSTANT_P should be NULL.  */
6602
6603 static tree
6604 cp_parser_constant_expression (cp_parser* parser,
6605                                bool allow_non_constant_p,
6606                                bool *non_constant_p)
6607 {
6608   bool saved_integral_constant_expression_p;
6609   bool saved_allow_non_integral_constant_expression_p;
6610   bool saved_non_integral_constant_expression_p;
6611   tree expression;
6612
6613   /* It might seem that we could simply parse the
6614      conditional-expression, and then check to see if it were
6615      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6616      one that the compiler can figure out is constant, possibly after
6617      doing some simplifications or optimizations.  The standard has a
6618      precise definition of constant-expression, and we must honor
6619      that, even though it is somewhat more restrictive.
6620
6621      For example:
6622
6623        int i[(2, 3)];
6624
6625      is not a legal declaration, because `(2, 3)' is not a
6626      constant-expression.  The `,' operator is forbidden in a
6627      constant-expression.  However, GCC's constant-folding machinery
6628      will fold this operation to an INTEGER_CST for `3'.  */
6629
6630   /* Save the old settings.  */
6631   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6632   saved_allow_non_integral_constant_expression_p
6633     = parser->allow_non_integral_constant_expression_p;
6634   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6635   /* We are now parsing a constant-expression.  */
6636   parser->integral_constant_expression_p = true;
6637   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6638   parser->non_integral_constant_expression_p = false;
6639   /* Although the grammar says "conditional-expression", we parse an
6640      "assignment-expression", which also permits "throw-expression"
6641      and the use of assignment operators.  In the case that
6642      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6643      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6644      actually essential that we look for an assignment-expression.
6645      For example, cp_parser_initializer_clauses uses this function to
6646      determine whether a particular assignment-expression is in fact
6647      constant.  */
6648   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6649   /* Restore the old settings.  */
6650   parser->integral_constant_expression_p
6651     = saved_integral_constant_expression_p;
6652   parser->allow_non_integral_constant_expression_p
6653     = saved_allow_non_integral_constant_expression_p;
6654   if (allow_non_constant_p)
6655     *non_constant_p = parser->non_integral_constant_expression_p;
6656   else if (parser->non_integral_constant_expression_p)
6657     expression = error_mark_node;
6658   parser->non_integral_constant_expression_p
6659     = saved_non_integral_constant_expression_p;
6660
6661   return expression;
6662 }
6663
6664 /* Parse __builtin_offsetof.
6665
6666    offsetof-expression:
6667      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6668
6669    offsetof-member-designator:
6670      id-expression
6671      | offsetof-member-designator "." id-expression
6672      | offsetof-member-designator "[" expression "]"
6673      | offsetof-member-designator "->" id-expression  */
6674
6675 static tree
6676 cp_parser_builtin_offsetof (cp_parser *parser)
6677 {
6678   int save_ice_p, save_non_ice_p;
6679   tree type, expr;
6680   cp_id_kind dummy;
6681   cp_token *token;
6682
6683   /* We're about to accept non-integral-constant things, but will
6684      definitely yield an integral constant expression.  Save and
6685      restore these values around our local parsing.  */
6686   save_ice_p = parser->integral_constant_expression_p;
6687   save_non_ice_p = parser->non_integral_constant_expression_p;
6688
6689   /* Consume the "__builtin_offsetof" token.  */
6690   cp_lexer_consume_token (parser->lexer);
6691   /* Consume the opening `('.  */
6692   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6693   /* Parse the type-id.  */
6694   type = cp_parser_type_id (parser);
6695   /* Look for the `,'.  */
6696   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6697   token = cp_lexer_peek_token (parser->lexer);
6698
6699   /* Build the (type *)null that begins the traditional offsetof macro.  */
6700   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6701                             tf_warning_or_error);
6702
6703   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6704   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6705                                                  true, &dummy, token->location);
6706   while (true)
6707     {
6708       token = cp_lexer_peek_token (parser->lexer);
6709       switch (token->type)
6710         {
6711         case CPP_OPEN_SQUARE:
6712           /* offsetof-member-designator "[" expression "]" */
6713           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6714           break;
6715
6716         case CPP_DEREF:
6717           /* offsetof-member-designator "->" identifier */
6718           expr = grok_array_decl (expr, integer_zero_node);
6719           /* FALLTHRU */
6720
6721         case CPP_DOT:
6722           /* offsetof-member-designator "." identifier */
6723           cp_lexer_consume_token (parser->lexer);
6724           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6725                                                          expr, true, &dummy,
6726                                                          token->location);
6727           break;
6728
6729         case CPP_CLOSE_PAREN:
6730           /* Consume the ")" token.  */
6731           cp_lexer_consume_token (parser->lexer);
6732           goto success;
6733
6734         default:
6735           /* Error.  We know the following require will fail, but
6736              that gives the proper error message.  */
6737           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6738           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6739           expr = error_mark_node;
6740           goto failure;
6741         }
6742     }
6743
6744  success:
6745   /* If we're processing a template, we can't finish the semantics yet.
6746      Otherwise we can fold the entire expression now.  */
6747   if (processing_template_decl)
6748     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6749   else
6750     expr = finish_offsetof (expr);
6751
6752  failure:
6753   parser->integral_constant_expression_p = save_ice_p;
6754   parser->non_integral_constant_expression_p = save_non_ice_p;
6755
6756   return expr;
6757 }
6758
6759 /* Parse a trait expression.  */
6760
6761 static tree
6762 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6763 {
6764   cp_trait_kind kind;
6765   tree type1, type2 = NULL_TREE;
6766   bool binary = false;
6767   cp_decl_specifier_seq decl_specs;
6768
6769   switch (keyword)
6770     {
6771     case RID_HAS_NOTHROW_ASSIGN:
6772       kind = CPTK_HAS_NOTHROW_ASSIGN;
6773       break;
6774     case RID_HAS_NOTHROW_CONSTRUCTOR:
6775       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6776       break;
6777     case RID_HAS_NOTHROW_COPY:
6778       kind = CPTK_HAS_NOTHROW_COPY;
6779       break;
6780     case RID_HAS_TRIVIAL_ASSIGN:
6781       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6782       break;
6783     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6784       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6785       break;
6786     case RID_HAS_TRIVIAL_COPY:
6787       kind = CPTK_HAS_TRIVIAL_COPY;
6788       break;
6789     case RID_HAS_TRIVIAL_DESTRUCTOR:
6790       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6791       break;
6792     case RID_HAS_VIRTUAL_DESTRUCTOR:
6793       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6794       break;
6795     case RID_IS_ABSTRACT:
6796       kind = CPTK_IS_ABSTRACT;
6797       break;
6798     case RID_IS_BASE_OF:
6799       kind = CPTK_IS_BASE_OF;
6800       binary = true;
6801       break;
6802     case RID_IS_CLASS:
6803       kind = CPTK_IS_CLASS;
6804       break;
6805     case RID_IS_CONVERTIBLE_TO:
6806       kind = CPTK_IS_CONVERTIBLE_TO;
6807       binary = true;
6808       break;
6809     case RID_IS_EMPTY:
6810       kind = CPTK_IS_EMPTY;
6811       break;
6812     case RID_IS_ENUM:
6813       kind = CPTK_IS_ENUM;
6814       break;
6815     case RID_IS_POD:
6816       kind = CPTK_IS_POD;
6817       break;
6818     case RID_IS_POLYMORPHIC:
6819       kind = CPTK_IS_POLYMORPHIC;
6820       break;
6821     case RID_IS_UNION:
6822       kind = CPTK_IS_UNION;
6823       break;
6824     default:
6825       gcc_unreachable ();
6826     }
6827
6828   /* Consume the token.  */
6829   cp_lexer_consume_token (parser->lexer);
6830
6831   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6832
6833   type1 = cp_parser_type_id (parser);
6834
6835   if (type1 == error_mark_node)
6836     return error_mark_node;
6837
6838   /* Build a trivial decl-specifier-seq.  */
6839   clear_decl_specs (&decl_specs);
6840   decl_specs.type = type1;
6841
6842   /* Call grokdeclarator to figure out what type this is.  */
6843   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6844                           /*initialized=*/0, /*attrlist=*/NULL);
6845
6846   if (binary)
6847     {
6848       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6849  
6850       type2 = cp_parser_type_id (parser);
6851
6852       if (type2 == error_mark_node)
6853         return error_mark_node;
6854
6855       /* Build a trivial decl-specifier-seq.  */
6856       clear_decl_specs (&decl_specs);
6857       decl_specs.type = type2;
6858
6859       /* Call grokdeclarator to figure out what type this is.  */
6860       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6861                               /*initialized=*/0, /*attrlist=*/NULL);
6862     }
6863
6864   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6865
6866   /* Complete the trait expression, which may mean either processing
6867      the trait expr now or saving it for template instantiation.  */
6868   return finish_trait_expr (kind, type1, type2);
6869 }
6870
6871 /* Statements [gram.stmt.stmt]  */
6872
6873 /* Parse a statement.
6874
6875    statement:
6876      labeled-statement
6877      expression-statement
6878      compound-statement
6879      selection-statement
6880      iteration-statement
6881      jump-statement
6882      declaration-statement
6883      try-block
6884
6885   IN_COMPOUND is true when the statement is nested inside a
6886   cp_parser_compound_statement; this matters for certain pragmas.
6887
6888   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6889   is a (possibly labeled) if statement which is not enclosed in braces
6890   and has an else clause.  This is used to implement -Wparentheses.  */
6891
6892 static void
6893 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6894                      bool in_compound, bool *if_p)
6895 {
6896   tree statement;
6897   cp_token *token;
6898   location_t statement_location;
6899
6900  restart:
6901   if (if_p != NULL)
6902     *if_p = false;
6903   /* There is no statement yet.  */
6904   statement = NULL_TREE;
6905   /* Peek at the next token.  */
6906   token = cp_lexer_peek_token (parser->lexer);
6907   /* Remember the location of the first token in the statement.  */
6908   statement_location = token->location;
6909   /* If this is a keyword, then that will often determine what kind of
6910      statement we have.  */
6911   if (token->type == CPP_KEYWORD)
6912     {
6913       enum rid keyword = token->keyword;
6914
6915       switch (keyword)
6916         {
6917         case RID_CASE:
6918         case RID_DEFAULT:
6919           /* Looks like a labeled-statement with a case label.
6920              Parse the label, and then use tail recursion to parse
6921              the statement.  */
6922           cp_parser_label_for_labeled_statement (parser);
6923           goto restart;
6924
6925         case RID_IF:
6926         case RID_SWITCH:
6927           statement = cp_parser_selection_statement (parser, if_p);
6928           break;
6929
6930         case RID_WHILE:
6931         case RID_DO:
6932         case RID_FOR:
6933           statement = cp_parser_iteration_statement (parser);
6934           break;
6935
6936         case RID_BREAK:
6937         case RID_CONTINUE:
6938         case RID_RETURN:
6939         case RID_GOTO:
6940           statement = cp_parser_jump_statement (parser);
6941           break;
6942
6943           /* Objective-C++ exception-handling constructs.  */
6944         case RID_AT_TRY:
6945         case RID_AT_CATCH:
6946         case RID_AT_FINALLY:
6947         case RID_AT_SYNCHRONIZED:
6948         case RID_AT_THROW:
6949           statement = cp_parser_objc_statement (parser);
6950           break;
6951
6952         case RID_TRY:
6953           statement = cp_parser_try_block (parser);
6954           break;
6955
6956         case RID_NAMESPACE:
6957           /* This must be a namespace alias definition.  */
6958           cp_parser_declaration_statement (parser);
6959           return;
6960           
6961         default:
6962           /* It might be a keyword like `int' that can start a
6963              declaration-statement.  */
6964           break;
6965         }
6966     }
6967   else if (token->type == CPP_NAME)
6968     {
6969       /* If the next token is a `:', then we are looking at a
6970          labeled-statement.  */
6971       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6972       if (token->type == CPP_COLON)
6973         {
6974           /* Looks like a labeled-statement with an ordinary label.
6975              Parse the label, and then use tail recursion to parse
6976              the statement.  */
6977           cp_parser_label_for_labeled_statement (parser);
6978           goto restart;
6979         }
6980     }
6981   /* Anything that starts with a `{' must be a compound-statement.  */
6982   else if (token->type == CPP_OPEN_BRACE)
6983     statement = cp_parser_compound_statement (parser, NULL, false);
6984   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6985      a statement all its own.  */
6986   else if (token->type == CPP_PRAGMA)
6987     {
6988       /* Only certain OpenMP pragmas are attached to statements, and thus
6989          are considered statements themselves.  All others are not.  In
6990          the context of a compound, accept the pragma as a "statement" and
6991          return so that we can check for a close brace.  Otherwise we
6992          require a real statement and must go back and read one.  */
6993       if (in_compound)
6994         cp_parser_pragma (parser, pragma_compound);
6995       else if (!cp_parser_pragma (parser, pragma_stmt))
6996         goto restart;
6997       return;
6998     }
6999   else if (token->type == CPP_EOF)
7000     {
7001       cp_parser_error (parser, "expected statement");
7002       return;
7003     }
7004
7005   /* Everything else must be a declaration-statement or an
7006      expression-statement.  Try for the declaration-statement
7007      first, unless we are looking at a `;', in which case we know that
7008      we have an expression-statement.  */
7009   if (!statement)
7010     {
7011       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7012         {
7013           cp_parser_parse_tentatively (parser);
7014           /* Try to parse the declaration-statement.  */
7015           cp_parser_declaration_statement (parser);
7016           /* If that worked, we're done.  */
7017           if (cp_parser_parse_definitely (parser))
7018             return;
7019         }
7020       /* Look for an expression-statement instead.  */
7021       statement = cp_parser_expression_statement (parser, in_statement_expr);
7022     }
7023
7024   /* Set the line number for the statement.  */
7025   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7026     SET_EXPR_LOCATION (statement, statement_location);
7027 }
7028
7029 /* Parse the label for a labeled-statement, i.e.
7030
7031    identifier :
7032    case constant-expression :
7033    default :
7034
7035    GNU Extension:
7036    case constant-expression ... constant-expression : statement
7037
7038    When a label is parsed without errors, the label is added to the
7039    parse tree by the finish_* functions, so this function doesn't
7040    have to return the label.  */
7041
7042 static void
7043 cp_parser_label_for_labeled_statement (cp_parser* parser)
7044 {
7045   cp_token *token;
7046
7047   /* The next token should be an identifier.  */
7048   token = cp_lexer_peek_token (parser->lexer);
7049   if (token->type != CPP_NAME
7050       && token->type != CPP_KEYWORD)
7051     {
7052       cp_parser_error (parser, "expected labeled-statement");
7053       return;
7054     }
7055
7056   switch (token->keyword)
7057     {
7058     case RID_CASE:
7059       {
7060         tree expr, expr_hi;
7061         cp_token *ellipsis;
7062
7063         /* Consume the `case' token.  */
7064         cp_lexer_consume_token (parser->lexer);
7065         /* Parse the constant-expression.  */
7066         expr = cp_parser_constant_expression (parser,
7067                                               /*allow_non_constant_p=*/false,
7068                                               NULL);
7069
7070         ellipsis = cp_lexer_peek_token (parser->lexer);
7071         if (ellipsis->type == CPP_ELLIPSIS)
7072           {
7073             /* Consume the `...' token.  */
7074             cp_lexer_consume_token (parser->lexer);
7075             expr_hi =
7076               cp_parser_constant_expression (parser,
7077                                              /*allow_non_constant_p=*/false,
7078                                              NULL);
7079             /* We don't need to emit warnings here, as the common code
7080                will do this for us.  */
7081           }
7082         else
7083           expr_hi = NULL_TREE;
7084
7085         if (parser->in_switch_statement_p)
7086           finish_case_label (expr, expr_hi);
7087         else
7088           error ("%Hcase label %qE not within a switch statement",
7089                  &token->location, expr);
7090       }
7091       break;
7092
7093     case RID_DEFAULT:
7094       /* Consume the `default' token.  */
7095       cp_lexer_consume_token (parser->lexer);
7096
7097       if (parser->in_switch_statement_p)
7098         finish_case_label (NULL_TREE, NULL_TREE);
7099       else
7100         error ("%Hcase label not within a switch statement", &token->location);
7101       break;
7102
7103     default:
7104       /* Anything else must be an ordinary label.  */
7105       finish_label_stmt (cp_parser_identifier (parser));
7106       break;
7107     }
7108
7109   /* Require the `:' token.  */
7110   cp_parser_require (parser, CPP_COLON, "%<:%>");
7111 }
7112
7113 /* Parse an expression-statement.
7114
7115    expression-statement:
7116      expression [opt] ;
7117
7118    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7119    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7120    indicates whether this expression-statement is part of an
7121    expression statement.  */
7122
7123 static tree
7124 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7125 {
7126   tree statement = NULL_TREE;
7127
7128   /* If the next token is a ';', then there is no expression
7129      statement.  */
7130   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7131     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7132
7133   /* Consume the final `;'.  */
7134   cp_parser_consume_semicolon_at_end_of_statement (parser);
7135
7136   if (in_statement_expr
7137       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7138     /* This is the final expression statement of a statement
7139        expression.  */
7140     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7141   else if (statement)
7142     statement = finish_expr_stmt (statement);
7143   else
7144     finish_stmt ();
7145
7146   return statement;
7147 }
7148
7149 /* Parse a compound-statement.
7150
7151    compound-statement:
7152      { statement-seq [opt] }
7153
7154    GNU extension:
7155
7156    compound-statement:
7157      { label-declaration-seq [opt] statement-seq [opt] }
7158
7159    label-declaration-seq:
7160      label-declaration
7161      label-declaration-seq label-declaration
7162
7163    Returns a tree representing the statement.  */
7164
7165 static tree
7166 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7167                               bool in_try)
7168 {
7169   tree compound_stmt;
7170
7171   /* Consume the `{'.  */
7172   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7173     return error_mark_node;
7174   /* Begin the compound-statement.  */
7175   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7176   /* If the next keyword is `__label__' we have a label declaration.  */
7177   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7178     cp_parser_label_declaration (parser);
7179   /* Parse an (optional) statement-seq.  */
7180   cp_parser_statement_seq_opt (parser, in_statement_expr);
7181   /* Finish the compound-statement.  */
7182   finish_compound_stmt (compound_stmt);
7183   /* Consume the `}'.  */
7184   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7185
7186   return compound_stmt;
7187 }
7188
7189 /* Parse an (optional) statement-seq.
7190
7191    statement-seq:
7192      statement
7193      statement-seq [opt] statement  */
7194
7195 static void
7196 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7197 {
7198   /* Scan statements until there aren't any more.  */
7199   while (true)
7200     {
7201       cp_token *token = cp_lexer_peek_token (parser->lexer);
7202
7203       /* If we're looking at a `}', then we've run out of statements.  */
7204       if (token->type == CPP_CLOSE_BRACE
7205           || token->type == CPP_EOF
7206           || token->type == CPP_PRAGMA_EOL)
7207         break;
7208       
7209       /* If we are in a compound statement and find 'else' then
7210          something went wrong.  */
7211       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7212         {
7213           if (parser->in_statement & IN_IF_STMT) 
7214             break;
7215           else
7216             {
7217               token = cp_lexer_consume_token (parser->lexer);
7218               error ("%H%<else%> without a previous %<if%>", &token->location);
7219             }
7220         }
7221
7222       /* Parse the statement.  */
7223       cp_parser_statement (parser, in_statement_expr, true, NULL);
7224     }
7225 }
7226
7227 /* Parse a selection-statement.
7228
7229    selection-statement:
7230      if ( condition ) statement
7231      if ( condition ) statement else statement
7232      switch ( condition ) statement
7233
7234    Returns the new IF_STMT or SWITCH_STMT.
7235
7236    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7237    is a (possibly labeled) if statement which is not enclosed in
7238    braces and has an else clause.  This is used to implement
7239    -Wparentheses.  */
7240
7241 static tree
7242 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7243 {
7244   cp_token *token;
7245   enum rid keyword;
7246
7247   if (if_p != NULL)
7248     *if_p = false;
7249
7250   /* Peek at the next token.  */
7251   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7252
7253   /* See what kind of keyword it is.  */
7254   keyword = token->keyword;
7255   switch (keyword)
7256     {
7257     case RID_IF:
7258     case RID_SWITCH:
7259       {
7260         tree statement;
7261         tree condition;
7262
7263         /* Look for the `('.  */
7264         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7265           {
7266             cp_parser_skip_to_end_of_statement (parser);
7267             return error_mark_node;
7268           }
7269
7270         /* Begin the selection-statement.  */
7271         if (keyword == RID_IF)
7272           statement = begin_if_stmt ();
7273         else
7274           statement = begin_switch_stmt ();
7275
7276         /* Parse the condition.  */
7277         condition = cp_parser_condition (parser);
7278         /* Look for the `)'.  */
7279         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7280           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7281                                                  /*consume_paren=*/true);
7282
7283         if (keyword == RID_IF)
7284           {
7285             bool nested_if;
7286             unsigned char in_statement;
7287
7288             /* Add the condition.  */
7289             finish_if_stmt_cond (condition, statement);
7290
7291             /* Parse the then-clause.  */
7292             in_statement = parser->in_statement;
7293             parser->in_statement |= IN_IF_STMT;
7294             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7295               {
7296                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7297                 add_stmt (build_empty_stmt ());
7298                 cp_lexer_consume_token (parser->lexer);
7299                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7300                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7301                               "empty body in an %<if%> statement");
7302                 nested_if = false;
7303               }
7304             else
7305               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7306             parser->in_statement = in_statement;
7307
7308             finish_then_clause (statement);
7309
7310             /* If the next token is `else', parse the else-clause.  */
7311             if (cp_lexer_next_token_is_keyword (parser->lexer,
7312                                                 RID_ELSE))
7313               {
7314                 /* Consume the `else' keyword.  */
7315                 cp_lexer_consume_token (parser->lexer);
7316                 begin_else_clause (statement);
7317                 /* Parse the else-clause.  */
7318                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7319                   {
7320                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7321                                 OPT_Wempty_body, "suggest braces around "
7322                                 "empty body in an %<else%> statement");
7323                     add_stmt (build_empty_stmt ());
7324                     cp_lexer_consume_token (parser->lexer);
7325                   }
7326                 else
7327                   cp_parser_implicitly_scoped_statement (parser, NULL);
7328
7329                 finish_else_clause (statement);
7330
7331                 /* If we are currently parsing a then-clause, then
7332                    IF_P will not be NULL.  We set it to true to
7333                    indicate that this if statement has an else clause.
7334                    This may trigger the Wparentheses warning below
7335                    when we get back up to the parent if statement.  */
7336                 if (if_p != NULL)
7337                   *if_p = true;
7338               }
7339             else
7340               {
7341                 /* This if statement does not have an else clause.  If
7342                    NESTED_IF is true, then the then-clause is an if
7343                    statement which does have an else clause.  We warn
7344                    about the potential ambiguity.  */
7345                 if (nested_if)
7346                   warning (OPT_Wparentheses,
7347                            ("%Hsuggest explicit braces "
7348                             "to avoid ambiguous %<else%>"),
7349                            EXPR_LOCUS (statement));
7350               }
7351
7352             /* Now we're all done with the if-statement.  */
7353             finish_if_stmt (statement);
7354           }
7355         else
7356           {
7357             bool in_switch_statement_p;
7358             unsigned char in_statement;
7359
7360             /* Add the condition.  */
7361             finish_switch_cond (condition, statement);
7362
7363             /* Parse the body of the switch-statement.  */
7364             in_switch_statement_p = parser->in_switch_statement_p;
7365             in_statement = parser->in_statement;
7366             parser->in_switch_statement_p = true;
7367             parser->in_statement |= IN_SWITCH_STMT;
7368             cp_parser_implicitly_scoped_statement (parser, NULL);
7369             parser->in_switch_statement_p = in_switch_statement_p;
7370             parser->in_statement = in_statement;
7371
7372             /* Now we're all done with the switch-statement.  */
7373             finish_switch_stmt (statement);
7374           }
7375
7376         return statement;
7377       }
7378       break;
7379
7380     default:
7381       cp_parser_error (parser, "expected selection-statement");
7382       return error_mark_node;
7383     }
7384 }
7385
7386 /* Parse a condition.
7387
7388    condition:
7389      expression
7390      type-specifier-seq declarator = initializer-clause
7391      type-specifier-seq declarator braced-init-list
7392
7393    GNU Extension:
7394
7395    condition:
7396      type-specifier-seq declarator asm-specification [opt]
7397        attributes [opt] = assignment-expression
7398
7399    Returns the expression that should be tested.  */
7400
7401 static tree
7402 cp_parser_condition (cp_parser* parser)
7403 {
7404   cp_decl_specifier_seq type_specifiers;
7405   const char *saved_message;
7406
7407   /* Try the declaration first.  */
7408   cp_parser_parse_tentatively (parser);
7409   /* New types are not allowed in the type-specifier-seq for a
7410      condition.  */
7411   saved_message = parser->type_definition_forbidden_message;
7412   parser->type_definition_forbidden_message
7413     = "types may not be defined in conditions";
7414   /* Parse the type-specifier-seq.  */
7415   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7416                                 &type_specifiers);
7417   /* Restore the saved message.  */
7418   parser->type_definition_forbidden_message = saved_message;
7419   /* If all is well, we might be looking at a declaration.  */
7420   if (!cp_parser_error_occurred (parser))
7421     {
7422       tree decl;
7423       tree asm_specification;
7424       tree attributes;
7425       cp_declarator *declarator;
7426       tree initializer = NULL_TREE;
7427
7428       /* Parse the declarator.  */
7429       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7430                                          /*ctor_dtor_or_conv_p=*/NULL,
7431                                          /*parenthesized_p=*/NULL,
7432                                          /*member_p=*/false);
7433       /* Parse the attributes.  */
7434       attributes = cp_parser_attributes_opt (parser);
7435       /* Parse the asm-specification.  */
7436       asm_specification = cp_parser_asm_specification_opt (parser);
7437       /* If the next token is not an `=' or '{', then we might still be
7438          looking at an expression.  For example:
7439
7440            if (A(a).x)
7441
7442          looks like a decl-specifier-seq and a declarator -- but then
7443          there is no `=', so this is an expression.  */
7444       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7445           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7446         cp_parser_simulate_error (parser);
7447         
7448       /* If we did see an `=' or '{', then we are looking at a declaration
7449          for sure.  */
7450       if (cp_parser_parse_definitely (parser))
7451         {
7452           tree pushed_scope;
7453           bool non_constant_p;
7454           bool flags = LOOKUP_ONLYCONVERTING;
7455
7456           /* Create the declaration.  */
7457           decl = start_decl (declarator, &type_specifiers,
7458                              /*initialized_p=*/true,
7459                              attributes, /*prefix_attributes=*/NULL_TREE,
7460                              &pushed_scope);
7461
7462           /* Parse the initializer.  */
7463           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7464             {
7465               initializer = cp_parser_braced_list (parser, &non_constant_p);
7466               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7467               flags = 0;
7468             }
7469           else
7470             {
7471               /* Consume the `='.  */
7472               cp_parser_require (parser, CPP_EQ, "%<=%>");
7473               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7474             }
7475           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7476             maybe_warn_cpp0x ("extended initializer lists");
7477
7478           if (!non_constant_p)
7479             initializer = fold_non_dependent_expr (initializer);
7480
7481           /* Process the initializer.  */
7482           cp_finish_decl (decl,
7483                           initializer, !non_constant_p,
7484                           asm_specification,
7485                           flags);
7486
7487           if (pushed_scope)
7488             pop_scope (pushed_scope);
7489
7490           return convert_from_reference (decl);
7491         }
7492     }
7493   /* If we didn't even get past the declarator successfully, we are
7494      definitely not looking at a declaration.  */
7495   else
7496     cp_parser_abort_tentative_parse (parser);
7497
7498   /* Otherwise, we are looking at an expression.  */
7499   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7500 }
7501
7502 /* Parse an iteration-statement.
7503
7504    iteration-statement:
7505      while ( condition ) statement
7506      do statement while ( expression ) ;
7507      for ( for-init-statement condition [opt] ; expression [opt] )
7508        statement
7509
7510    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7511
7512 static tree
7513 cp_parser_iteration_statement (cp_parser* parser)
7514 {
7515   cp_token *token;
7516   enum rid keyword;
7517   tree statement;
7518   unsigned char in_statement;
7519
7520   /* Peek at the next token.  */
7521   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7522   if (!token)
7523     return error_mark_node;
7524
7525   /* Remember whether or not we are already within an iteration
7526      statement.  */
7527   in_statement = parser->in_statement;
7528
7529   /* See what kind of keyword it is.  */
7530   keyword = token->keyword;
7531   switch (keyword)
7532     {
7533     case RID_WHILE:
7534       {
7535         tree condition;
7536
7537         /* Begin the while-statement.  */
7538         statement = begin_while_stmt ();
7539         /* Look for the `('.  */
7540         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7541         /* Parse the condition.  */
7542         condition = cp_parser_condition (parser);
7543         finish_while_stmt_cond (condition, statement);
7544         /* Look for the `)'.  */
7545         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7546         /* Parse the dependent statement.  */
7547         parser->in_statement = IN_ITERATION_STMT;
7548         cp_parser_already_scoped_statement (parser);
7549         parser->in_statement = in_statement;
7550         /* We're done with the while-statement.  */
7551         finish_while_stmt (statement);
7552       }
7553       break;
7554
7555     case RID_DO:
7556       {
7557         tree expression;
7558
7559         /* Begin the do-statement.  */
7560         statement = begin_do_stmt ();
7561         /* Parse the body of the do-statement.  */
7562         parser->in_statement = IN_ITERATION_STMT;
7563         cp_parser_implicitly_scoped_statement (parser, NULL);
7564         parser->in_statement = in_statement;
7565         finish_do_body (statement);
7566         /* Look for the `while' keyword.  */
7567         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7568         /* Look for the `('.  */
7569         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7570         /* Parse the expression.  */
7571         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7572         /* We're done with the do-statement.  */
7573         finish_do_stmt (expression, statement);
7574         /* Look for the `)'.  */
7575         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7576         /* Look for the `;'.  */
7577         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7578       }
7579       break;
7580
7581     case RID_FOR:
7582       {
7583         tree condition = NULL_TREE;
7584         tree expression = NULL_TREE;
7585
7586         /* Begin the for-statement.  */
7587         statement = begin_for_stmt ();
7588         /* Look for the `('.  */
7589         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7590         /* Parse the initialization.  */
7591         cp_parser_for_init_statement (parser);
7592         finish_for_init_stmt (statement);
7593
7594         /* If there's a condition, process it.  */
7595         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7596           condition = cp_parser_condition (parser);
7597         finish_for_cond (condition, statement);
7598         /* Look for the `;'.  */
7599         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7600
7601         /* If there's an expression, process it.  */
7602         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7603           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7604         finish_for_expr (expression, statement);
7605         /* Look for the `)'.  */
7606         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7607
7608         /* Parse the body of the for-statement.  */
7609         parser->in_statement = IN_ITERATION_STMT;
7610         cp_parser_already_scoped_statement (parser);
7611         parser->in_statement = in_statement;
7612
7613         /* We're done with the for-statement.  */
7614         finish_for_stmt (statement);
7615       }
7616       break;
7617
7618     default:
7619       cp_parser_error (parser, "expected iteration-statement");
7620       statement = error_mark_node;
7621       break;
7622     }
7623
7624   return statement;
7625 }
7626
7627 /* Parse a for-init-statement.
7628
7629    for-init-statement:
7630      expression-statement
7631      simple-declaration  */
7632
7633 static void
7634 cp_parser_for_init_statement (cp_parser* parser)
7635 {
7636   /* If the next token is a `;', then we have an empty
7637      expression-statement.  Grammatically, this is also a
7638      simple-declaration, but an invalid one, because it does not
7639      declare anything.  Therefore, if we did not handle this case
7640      specially, we would issue an error message about an invalid
7641      declaration.  */
7642   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7643     {
7644       /* We're going to speculatively look for a declaration, falling back
7645          to an expression, if necessary.  */
7646       cp_parser_parse_tentatively (parser);
7647       /* Parse the declaration.  */
7648       cp_parser_simple_declaration (parser,
7649                                     /*function_definition_allowed_p=*/false);
7650       /* If the tentative parse failed, then we shall need to look for an
7651          expression-statement.  */
7652       if (cp_parser_parse_definitely (parser))
7653         return;
7654     }
7655
7656   cp_parser_expression_statement (parser, false);
7657 }
7658
7659 /* Parse a jump-statement.
7660
7661    jump-statement:
7662      break ;
7663      continue ;
7664      return expression [opt] ;
7665      return braced-init-list ;
7666      goto identifier ;
7667
7668    GNU extension:
7669
7670    jump-statement:
7671      goto * expression ;
7672
7673    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7674
7675 static tree
7676 cp_parser_jump_statement (cp_parser* parser)
7677 {
7678   tree statement = error_mark_node;
7679   cp_token *token;
7680   enum rid keyword;
7681   unsigned char in_statement;
7682
7683   /* Peek at the next token.  */
7684   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7685   if (!token)
7686     return error_mark_node;
7687
7688   /* See what kind of keyword it is.  */
7689   keyword = token->keyword;
7690   switch (keyword)
7691     {
7692     case RID_BREAK:
7693       in_statement = parser->in_statement & ~IN_IF_STMT;      
7694       switch (in_statement)
7695         {
7696         case 0:
7697           error ("%Hbreak statement not within loop or switch", &token->location);
7698           break;
7699         default:
7700           gcc_assert ((in_statement & IN_SWITCH_STMT)
7701                       || in_statement == IN_ITERATION_STMT);
7702           statement = finish_break_stmt ();
7703           break;
7704         case IN_OMP_BLOCK:
7705           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7706           break;
7707         case IN_OMP_FOR:
7708           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7709           break;
7710         }
7711       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7712       break;
7713
7714     case RID_CONTINUE:
7715       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7716         {
7717         case 0:
7718           error ("%Hcontinue statement not within a loop", &token->location);
7719           break;
7720         case IN_ITERATION_STMT:
7721         case IN_OMP_FOR:
7722           statement = finish_continue_stmt ();
7723           break;
7724         case IN_OMP_BLOCK:
7725           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7726           break;
7727         default:
7728           gcc_unreachable ();
7729         }
7730       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7731       break;
7732
7733     case RID_RETURN:
7734       {
7735         tree expr;
7736         bool expr_non_constant_p;
7737
7738         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7739           {
7740             maybe_warn_cpp0x ("extended initializer lists");
7741             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7742           }
7743         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7744           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7745         else
7746           /* If the next token is a `;', then there is no
7747              expression.  */
7748           expr = NULL_TREE;
7749         /* Build the return-statement.  */
7750         statement = finish_return_stmt (expr);
7751         /* Look for the final `;'.  */
7752         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7753       }
7754       break;
7755
7756     case RID_GOTO:
7757       /* Create the goto-statement.  */
7758       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7759         {
7760           /* Issue a warning about this use of a GNU extension.  */
7761           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7762           /* Consume the '*' token.  */
7763           cp_lexer_consume_token (parser->lexer);
7764           /* Parse the dependent expression.  */
7765           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7766         }
7767       else
7768         finish_goto_stmt (cp_parser_identifier (parser));
7769       /* Look for the final `;'.  */
7770       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7771       break;
7772
7773     default:
7774       cp_parser_error (parser, "expected jump-statement");
7775       break;
7776     }
7777
7778   return statement;
7779 }
7780
7781 /* Parse a declaration-statement.
7782
7783    declaration-statement:
7784      block-declaration  */
7785
7786 static void
7787 cp_parser_declaration_statement (cp_parser* parser)
7788 {
7789   void *p;
7790
7791   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7792   p = obstack_alloc (&declarator_obstack, 0);
7793
7794  /* Parse the block-declaration.  */
7795   cp_parser_block_declaration (parser, /*statement_p=*/true);
7796
7797   /* Free any declarators allocated.  */
7798   obstack_free (&declarator_obstack, p);
7799
7800   /* Finish off the statement.  */
7801   finish_stmt ();
7802 }
7803
7804 /* Some dependent statements (like `if (cond) statement'), are
7805    implicitly in their own scope.  In other words, if the statement is
7806    a single statement (as opposed to a compound-statement), it is
7807    none-the-less treated as if it were enclosed in braces.  Any
7808    declarations appearing in the dependent statement are out of scope
7809    after control passes that point.  This function parses a statement,
7810    but ensures that is in its own scope, even if it is not a
7811    compound-statement.
7812
7813    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7814    is a (possibly labeled) if statement which is not enclosed in
7815    braces and has an else clause.  This is used to implement
7816    -Wparentheses.
7817
7818    Returns the new statement.  */
7819
7820 static tree
7821 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7822 {
7823   tree statement;
7824
7825   if (if_p != NULL)
7826     *if_p = false;
7827
7828   /* Mark if () ; with a special NOP_EXPR.  */
7829   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7830     {
7831       cp_lexer_consume_token (parser->lexer);
7832       statement = add_stmt (build_empty_stmt ());
7833     }
7834   /* if a compound is opened, we simply parse the statement directly.  */
7835   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7836     statement = cp_parser_compound_statement (parser, NULL, false);
7837   /* If the token is not a `{', then we must take special action.  */
7838   else
7839     {
7840       /* Create a compound-statement.  */
7841       statement = begin_compound_stmt (0);
7842       /* Parse the dependent-statement.  */
7843       cp_parser_statement (parser, NULL_TREE, false, if_p);
7844       /* Finish the dummy compound-statement.  */
7845       finish_compound_stmt (statement);
7846     }
7847
7848   /* Return the statement.  */
7849   return statement;
7850 }
7851
7852 /* For some dependent statements (like `while (cond) statement'), we
7853    have already created a scope.  Therefore, even if the dependent
7854    statement is a compound-statement, we do not want to create another
7855    scope.  */
7856
7857 static void
7858 cp_parser_already_scoped_statement (cp_parser* parser)
7859 {
7860   /* If the token is a `{', then we must take special action.  */
7861   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7862     cp_parser_statement (parser, NULL_TREE, false, NULL);
7863   else
7864     {
7865       /* Avoid calling cp_parser_compound_statement, so that we
7866          don't create a new scope.  Do everything else by hand.  */
7867       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7868       /* If the next keyword is `__label__' we have a label declaration.  */
7869       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7870         cp_parser_label_declaration (parser);
7871       /* Parse an (optional) statement-seq.  */
7872       cp_parser_statement_seq_opt (parser, NULL_TREE);
7873       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7874     }
7875 }
7876
7877 /* Declarations [gram.dcl.dcl] */
7878
7879 /* Parse an optional declaration-sequence.
7880
7881    declaration-seq:
7882      declaration
7883      declaration-seq declaration  */
7884
7885 static void
7886 cp_parser_declaration_seq_opt (cp_parser* parser)
7887 {
7888   while (true)
7889     {
7890       cp_token *token;
7891
7892       token = cp_lexer_peek_token (parser->lexer);
7893
7894       if (token->type == CPP_CLOSE_BRACE
7895           || token->type == CPP_EOF
7896           || token->type == CPP_PRAGMA_EOL)
7897         break;
7898
7899       if (token->type == CPP_SEMICOLON)
7900         {
7901           /* A declaration consisting of a single semicolon is
7902              invalid.  Allow it unless we're being pedantic.  */
7903           cp_lexer_consume_token (parser->lexer);
7904           if (!in_system_header)
7905             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7906           continue;
7907         }
7908
7909       /* If we're entering or exiting a region that's implicitly
7910          extern "C", modify the lang context appropriately.  */
7911       if (!parser->implicit_extern_c && token->implicit_extern_c)
7912         {
7913           push_lang_context (lang_name_c);
7914           parser->implicit_extern_c = true;
7915         }
7916       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7917         {
7918           pop_lang_context ();
7919           parser->implicit_extern_c = false;
7920         }
7921
7922       if (token->type == CPP_PRAGMA)
7923         {
7924           /* A top-level declaration can consist solely of a #pragma.
7925              A nested declaration cannot, so this is done here and not
7926              in cp_parser_declaration.  (A #pragma at block scope is
7927              handled in cp_parser_statement.)  */
7928           cp_parser_pragma (parser, pragma_external);
7929           continue;
7930         }
7931
7932       /* Parse the declaration itself.  */
7933       cp_parser_declaration (parser);
7934     }
7935 }
7936
7937 /* Parse a declaration.
7938
7939    declaration:
7940      block-declaration
7941      function-definition
7942      template-declaration
7943      explicit-instantiation
7944      explicit-specialization
7945      linkage-specification
7946      namespace-definition
7947
7948    GNU extension:
7949
7950    declaration:
7951       __extension__ declaration */
7952
7953 static void
7954 cp_parser_declaration (cp_parser* parser)
7955 {
7956   cp_token token1;
7957   cp_token token2;
7958   int saved_pedantic;
7959   void *p;
7960
7961   /* Check for the `__extension__' keyword.  */
7962   if (cp_parser_extension_opt (parser, &saved_pedantic))
7963     {
7964       /* Parse the qualified declaration.  */
7965       cp_parser_declaration (parser);
7966       /* Restore the PEDANTIC flag.  */
7967       pedantic = saved_pedantic;
7968
7969       return;
7970     }
7971
7972   /* Try to figure out what kind of declaration is present.  */
7973   token1 = *cp_lexer_peek_token (parser->lexer);
7974
7975   if (token1.type != CPP_EOF)
7976     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7977   else
7978     {
7979       token2.type = CPP_EOF;
7980       token2.keyword = RID_MAX;
7981     }
7982
7983   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7984   p = obstack_alloc (&declarator_obstack, 0);
7985
7986   /* If the next token is `extern' and the following token is a string
7987      literal, then we have a linkage specification.  */
7988   if (token1.keyword == RID_EXTERN
7989       && cp_parser_is_string_literal (&token2))
7990     cp_parser_linkage_specification (parser);
7991   /* If the next token is `template', then we have either a template
7992      declaration, an explicit instantiation, or an explicit
7993      specialization.  */
7994   else if (token1.keyword == RID_TEMPLATE)
7995     {
7996       /* `template <>' indicates a template specialization.  */
7997       if (token2.type == CPP_LESS
7998           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7999         cp_parser_explicit_specialization (parser);
8000       /* `template <' indicates a template declaration.  */
8001       else if (token2.type == CPP_LESS)
8002         cp_parser_template_declaration (parser, /*member_p=*/false);
8003       /* Anything else must be an explicit instantiation.  */
8004       else
8005         cp_parser_explicit_instantiation (parser);
8006     }
8007   /* If the next token is `export', then we have a template
8008      declaration.  */
8009   else if (token1.keyword == RID_EXPORT)
8010     cp_parser_template_declaration (parser, /*member_p=*/false);
8011   /* If the next token is `extern', 'static' or 'inline' and the one
8012      after that is `template', we have a GNU extended explicit
8013      instantiation directive.  */
8014   else if (cp_parser_allow_gnu_extensions_p (parser)
8015            && (token1.keyword == RID_EXTERN
8016                || token1.keyword == RID_STATIC
8017                || token1.keyword == RID_INLINE)
8018            && token2.keyword == RID_TEMPLATE)
8019     cp_parser_explicit_instantiation (parser);
8020   /* If the next token is `namespace', check for a named or unnamed
8021      namespace definition.  */
8022   else if (token1.keyword == RID_NAMESPACE
8023            && (/* A named namespace definition.  */
8024                (token2.type == CPP_NAME
8025                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8026                     != CPP_EQ))
8027                /* An unnamed namespace definition.  */
8028                || token2.type == CPP_OPEN_BRACE
8029                || token2.keyword == RID_ATTRIBUTE))
8030     cp_parser_namespace_definition (parser);
8031   /* An inline (associated) namespace definition.  */
8032   else if (token1.keyword == RID_INLINE
8033            && token2.keyword == RID_NAMESPACE)
8034     cp_parser_namespace_definition (parser);
8035   /* Objective-C++ declaration/definition.  */
8036   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8037     cp_parser_objc_declaration (parser);
8038   /* We must have either a block declaration or a function
8039      definition.  */
8040   else
8041     /* Try to parse a block-declaration, or a function-definition.  */
8042     cp_parser_block_declaration (parser, /*statement_p=*/false);
8043
8044   /* Free any declarators allocated.  */
8045   obstack_free (&declarator_obstack, p);
8046 }
8047
8048 /* Parse a block-declaration.
8049
8050    block-declaration:
8051      simple-declaration
8052      asm-definition
8053      namespace-alias-definition
8054      using-declaration
8055      using-directive
8056
8057    GNU Extension:
8058
8059    block-declaration:
8060      __extension__ block-declaration
8061
8062    C++0x Extension:
8063
8064    block-declaration:
8065      static_assert-declaration
8066
8067    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8068    part of a declaration-statement.  */
8069
8070 static void
8071 cp_parser_block_declaration (cp_parser *parser,
8072                              bool      statement_p)
8073 {
8074   cp_token *token1;
8075   int saved_pedantic;
8076
8077   /* Check for the `__extension__' keyword.  */
8078   if (cp_parser_extension_opt (parser, &saved_pedantic))
8079     {
8080       /* Parse the qualified declaration.  */
8081       cp_parser_block_declaration (parser, statement_p);
8082       /* Restore the PEDANTIC flag.  */
8083       pedantic = saved_pedantic;
8084
8085       return;
8086     }
8087
8088   /* Peek at the next token to figure out which kind of declaration is
8089      present.  */
8090   token1 = cp_lexer_peek_token (parser->lexer);
8091
8092   /* If the next keyword is `asm', we have an asm-definition.  */
8093   if (token1->keyword == RID_ASM)
8094     {
8095       if (statement_p)
8096         cp_parser_commit_to_tentative_parse (parser);
8097       cp_parser_asm_definition (parser);
8098     }
8099   /* If the next keyword is `namespace', we have a
8100      namespace-alias-definition.  */
8101   else if (token1->keyword == RID_NAMESPACE)
8102     cp_parser_namespace_alias_definition (parser);
8103   /* If the next keyword is `using', we have either a
8104      using-declaration or a using-directive.  */
8105   else if (token1->keyword == RID_USING)
8106     {
8107       cp_token *token2;
8108
8109       if (statement_p)
8110         cp_parser_commit_to_tentative_parse (parser);
8111       /* If the token after `using' is `namespace', then we have a
8112          using-directive.  */
8113       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8114       if (token2->keyword == RID_NAMESPACE)
8115         cp_parser_using_directive (parser);
8116       /* Otherwise, it's a using-declaration.  */
8117       else
8118         cp_parser_using_declaration (parser,
8119                                      /*access_declaration_p=*/false);
8120     }
8121   /* If the next keyword is `__label__' we have a misplaced label
8122      declaration.  */
8123   else if (token1->keyword == RID_LABEL)
8124     {
8125       cp_lexer_consume_token (parser->lexer);
8126       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8127       cp_parser_skip_to_end_of_statement (parser);
8128       /* If the next token is now a `;', consume it.  */
8129       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8130         cp_lexer_consume_token (parser->lexer);
8131     }
8132   /* If the next token is `static_assert' we have a static assertion.  */
8133   else if (token1->keyword == RID_STATIC_ASSERT)
8134     cp_parser_static_assert (parser, /*member_p=*/false);
8135   /* Anything else must be a simple-declaration.  */
8136   else
8137     cp_parser_simple_declaration (parser, !statement_p);
8138 }
8139
8140 /* Parse a simple-declaration.
8141
8142    simple-declaration:
8143      decl-specifier-seq [opt] init-declarator-list [opt] ;
8144
8145    init-declarator-list:
8146      init-declarator
8147      init-declarator-list , init-declarator
8148
8149    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8150    function-definition as a simple-declaration.  */
8151
8152 static void
8153 cp_parser_simple_declaration (cp_parser* parser,
8154                               bool function_definition_allowed_p)
8155 {
8156   cp_decl_specifier_seq decl_specifiers;
8157   int declares_class_or_enum;
8158   bool saw_declarator;
8159
8160   /* Defer access checks until we know what is being declared; the
8161      checks for names appearing in the decl-specifier-seq should be
8162      done as if we were in the scope of the thing being declared.  */
8163   push_deferring_access_checks (dk_deferred);
8164
8165   /* Parse the decl-specifier-seq.  We have to keep track of whether
8166      or not the decl-specifier-seq declares a named class or
8167      enumeration type, since that is the only case in which the
8168      init-declarator-list is allowed to be empty.
8169
8170      [dcl.dcl]
8171
8172      In a simple-declaration, the optional init-declarator-list can be
8173      omitted only when declaring a class or enumeration, that is when
8174      the decl-specifier-seq contains either a class-specifier, an
8175      elaborated-type-specifier, or an enum-specifier.  */
8176   cp_parser_decl_specifier_seq (parser,
8177                                 CP_PARSER_FLAGS_OPTIONAL,
8178                                 &decl_specifiers,
8179                                 &declares_class_or_enum);
8180   /* We no longer need to defer access checks.  */
8181   stop_deferring_access_checks ();
8182
8183   /* In a block scope, a valid declaration must always have a
8184      decl-specifier-seq.  By not trying to parse declarators, we can
8185      resolve the declaration/expression ambiguity more quickly.  */
8186   if (!function_definition_allowed_p
8187       && !decl_specifiers.any_specifiers_p)
8188     {
8189       cp_parser_error (parser, "expected declaration");
8190       goto done;
8191     }
8192
8193   /* If the next two tokens are both identifiers, the code is
8194      erroneous. The usual cause of this situation is code like:
8195
8196        T t;
8197
8198      where "T" should name a type -- but does not.  */
8199   if (!decl_specifiers.type
8200       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8201     {
8202       /* If parsing tentatively, we should commit; we really are
8203          looking at a declaration.  */
8204       cp_parser_commit_to_tentative_parse (parser);
8205       /* Give up.  */
8206       goto done;
8207     }
8208
8209   /* If we have seen at least one decl-specifier, and the next token
8210      is not a parenthesis, then we must be looking at a declaration.
8211      (After "int (" we might be looking at a functional cast.)  */
8212   if (decl_specifiers.any_specifiers_p
8213       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8214       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8215       && !cp_parser_error_occurred (parser))
8216     cp_parser_commit_to_tentative_parse (parser);
8217
8218   /* Keep going until we hit the `;' at the end of the simple
8219      declaration.  */
8220   saw_declarator = false;
8221   while (cp_lexer_next_token_is_not (parser->lexer,
8222                                      CPP_SEMICOLON))
8223     {
8224       cp_token *token;
8225       bool function_definition_p;
8226       tree decl;
8227
8228       if (saw_declarator)
8229         {
8230           /* If we are processing next declarator, coma is expected */
8231           token = cp_lexer_peek_token (parser->lexer);
8232           gcc_assert (token->type == CPP_COMMA);
8233           cp_lexer_consume_token (parser->lexer);
8234         }
8235       else
8236         saw_declarator = true;
8237
8238       /* Parse the init-declarator.  */
8239       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8240                                         /*checks=*/NULL,
8241                                         function_definition_allowed_p,
8242                                         /*member_p=*/false,
8243                                         declares_class_or_enum,
8244                                         &function_definition_p);
8245       /* If an error occurred while parsing tentatively, exit quickly.
8246          (That usually happens when in the body of a function; each
8247          statement is treated as a declaration-statement until proven
8248          otherwise.)  */
8249       if (cp_parser_error_occurred (parser))
8250         goto done;
8251       /* Handle function definitions specially.  */
8252       if (function_definition_p)
8253         {
8254           /* If the next token is a `,', then we are probably
8255              processing something like:
8256
8257                void f() {}, *p;
8258
8259              which is erroneous.  */
8260           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8261             {
8262               cp_token *token = cp_lexer_peek_token (parser->lexer);
8263               error ("%Hmixing declarations and function-definitions is forbidden",
8264                      &token->location);
8265             }
8266           /* Otherwise, we're done with the list of declarators.  */
8267           else
8268             {
8269               pop_deferring_access_checks ();
8270               return;
8271             }
8272         }
8273       /* The next token should be either a `,' or a `;'.  */
8274       token = cp_lexer_peek_token (parser->lexer);
8275       /* If it's a `,', there are more declarators to come.  */
8276       if (token->type == CPP_COMMA)
8277         /* will be consumed next time around */;
8278       /* If it's a `;', we are done.  */
8279       else if (token->type == CPP_SEMICOLON)
8280         break;
8281       /* Anything else is an error.  */
8282       else
8283         {
8284           /* If we have already issued an error message we don't need
8285              to issue another one.  */
8286           if (decl != error_mark_node
8287               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8288             cp_parser_error (parser, "expected %<,%> or %<;%>");
8289           /* Skip tokens until we reach the end of the statement.  */
8290           cp_parser_skip_to_end_of_statement (parser);
8291           /* If the next token is now a `;', consume it.  */
8292           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8293             cp_lexer_consume_token (parser->lexer);
8294           goto done;
8295         }
8296       /* After the first time around, a function-definition is not
8297          allowed -- even if it was OK at first.  For example:
8298
8299            int i, f() {}
8300
8301          is not valid.  */
8302       function_definition_allowed_p = false;
8303     }
8304
8305   /* Issue an error message if no declarators are present, and the
8306      decl-specifier-seq does not itself declare a class or
8307      enumeration.  */
8308   if (!saw_declarator)
8309     {
8310       if (cp_parser_declares_only_class_p (parser))
8311         shadow_tag (&decl_specifiers);
8312       /* Perform any deferred access checks.  */
8313       perform_deferred_access_checks ();
8314     }
8315
8316   /* Consume the `;'.  */
8317   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8318
8319  done:
8320   pop_deferring_access_checks ();
8321 }
8322
8323 /* Parse a decl-specifier-seq.
8324
8325    decl-specifier-seq:
8326      decl-specifier-seq [opt] decl-specifier
8327
8328    decl-specifier:
8329      storage-class-specifier
8330      type-specifier
8331      function-specifier
8332      friend
8333      typedef
8334
8335    GNU Extension:
8336
8337    decl-specifier:
8338      attributes
8339
8340    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8341
8342    The parser flags FLAGS is used to control type-specifier parsing.
8343
8344    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8345    flags:
8346
8347      1: one of the decl-specifiers is an elaborated-type-specifier
8348         (i.e., a type declaration)
8349      2: one of the decl-specifiers is an enum-specifier or a
8350         class-specifier (i.e., a type definition)
8351
8352    */
8353
8354 static void
8355 cp_parser_decl_specifier_seq (cp_parser* parser,
8356                               cp_parser_flags flags,
8357                               cp_decl_specifier_seq *decl_specs,
8358                               int* declares_class_or_enum)
8359 {
8360   bool constructor_possible_p = !parser->in_declarator_p;
8361   cp_token *start_token = NULL;
8362
8363   /* Clear DECL_SPECS.  */
8364   clear_decl_specs (decl_specs);
8365
8366   /* Assume no class or enumeration type is declared.  */
8367   *declares_class_or_enum = 0;
8368
8369   /* Keep reading specifiers until there are no more to read.  */
8370   while (true)
8371     {
8372       bool constructor_p;
8373       bool found_decl_spec;
8374       cp_token *token;
8375
8376       /* Peek at the next token.  */
8377       token = cp_lexer_peek_token (parser->lexer);
8378
8379       /* Save the first token of the decl spec list for error
8380          reporting.  */
8381       if (!start_token)
8382         start_token = token;
8383       /* Handle attributes.  */
8384       if (token->keyword == RID_ATTRIBUTE)
8385         {
8386           /* Parse the attributes.  */
8387           decl_specs->attributes
8388             = chainon (decl_specs->attributes,
8389                        cp_parser_attributes_opt (parser));
8390           continue;
8391         }
8392       /* Assume we will find a decl-specifier keyword.  */
8393       found_decl_spec = true;
8394       /* If the next token is an appropriate keyword, we can simply
8395          add it to the list.  */
8396       switch (token->keyword)
8397         {
8398           /* decl-specifier:
8399                friend  */
8400         case RID_FRIEND:
8401           if (!at_class_scope_p ())
8402             {
8403               error ("%H%<friend%> used outside of class", &token->location);
8404               cp_lexer_purge_token (parser->lexer);
8405             }
8406           else
8407             {
8408               ++decl_specs->specs[(int) ds_friend];
8409               /* Consume the token.  */
8410               cp_lexer_consume_token (parser->lexer);
8411             }
8412           break;
8413
8414           /* function-specifier:
8415                inline
8416                virtual
8417                explicit  */
8418         case RID_INLINE:
8419         case RID_VIRTUAL:
8420         case RID_EXPLICIT:
8421           cp_parser_function_specifier_opt (parser, decl_specs);
8422           break;
8423
8424           /* decl-specifier:
8425                typedef  */
8426         case RID_TYPEDEF:
8427           ++decl_specs->specs[(int) ds_typedef];
8428           /* Consume the token.  */
8429           cp_lexer_consume_token (parser->lexer);
8430           /* A constructor declarator cannot appear in a typedef.  */
8431           constructor_possible_p = false;
8432           /* The "typedef" keyword can only occur in a declaration; we
8433              may as well commit at this point.  */
8434           cp_parser_commit_to_tentative_parse (parser);
8435
8436           if (decl_specs->storage_class != sc_none)
8437             decl_specs->conflicting_specifiers_p = true;
8438           break;
8439
8440           /* storage-class-specifier:
8441                auto
8442                register
8443                static
8444                extern
8445                mutable
8446
8447              GNU Extension:
8448                thread  */
8449         case RID_AUTO:
8450           if (cxx_dialect == cxx98) 
8451             {
8452               /* Consume the token.  */
8453               cp_lexer_consume_token (parser->lexer);
8454
8455               /* Complain about `auto' as a storage specifier, if
8456                  we're complaining about C++0x compatibility.  */
8457               warning 
8458                 (OPT_Wc__0x_compat, 
8459                  "%H%<auto%> will change meaning in C++0x; please remove it",
8460                  &token->location);
8461
8462               /* Set the storage class anyway.  */
8463               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8464                                            token->location);
8465             }
8466           else
8467             /* C++0x auto type-specifier.  */
8468             found_decl_spec = false;
8469           break;
8470
8471         case RID_REGISTER:
8472         case RID_STATIC:
8473         case RID_EXTERN:
8474         case RID_MUTABLE:
8475           /* Consume the token.  */
8476           cp_lexer_consume_token (parser->lexer);
8477           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8478                                        token->location);
8479           break;
8480         case RID_THREAD:
8481           /* Consume the token.  */
8482           cp_lexer_consume_token (parser->lexer);
8483           ++decl_specs->specs[(int) ds_thread];
8484           break;
8485
8486         default:
8487           /* We did not yet find a decl-specifier yet.  */
8488           found_decl_spec = false;
8489           break;
8490         }
8491
8492       /* Constructors are a special case.  The `S' in `S()' is not a
8493          decl-specifier; it is the beginning of the declarator.  */
8494       constructor_p
8495         = (!found_decl_spec
8496            && constructor_possible_p
8497            && (cp_parser_constructor_declarator_p
8498                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8499
8500       /* If we don't have a DECL_SPEC yet, then we must be looking at
8501          a type-specifier.  */
8502       if (!found_decl_spec && !constructor_p)
8503         {
8504           int decl_spec_declares_class_or_enum;
8505           bool is_cv_qualifier;
8506           tree type_spec;
8507
8508           type_spec
8509             = cp_parser_type_specifier (parser, flags,
8510                                         decl_specs,
8511                                         /*is_declaration=*/true,
8512                                         &decl_spec_declares_class_or_enum,
8513                                         &is_cv_qualifier);
8514           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8515
8516           /* If this type-specifier referenced a user-defined type
8517              (a typedef, class-name, etc.), then we can't allow any
8518              more such type-specifiers henceforth.
8519
8520              [dcl.spec]
8521
8522              The longest sequence of decl-specifiers that could
8523              possibly be a type name is taken as the
8524              decl-specifier-seq of a declaration.  The sequence shall
8525              be self-consistent as described below.
8526
8527              [dcl.type]
8528
8529              As a general rule, at most one type-specifier is allowed
8530              in the complete decl-specifier-seq of a declaration.  The
8531              only exceptions are the following:
8532
8533              -- const or volatile can be combined with any other
8534                 type-specifier.
8535
8536              -- signed or unsigned can be combined with char, long,
8537                 short, or int.
8538
8539              -- ..
8540
8541              Example:
8542
8543                typedef char* Pc;
8544                void g (const int Pc);
8545
8546              Here, Pc is *not* part of the decl-specifier seq; it's
8547              the declarator.  Therefore, once we see a type-specifier
8548              (other than a cv-qualifier), we forbid any additional
8549              user-defined types.  We *do* still allow things like `int
8550              int' to be considered a decl-specifier-seq, and issue the
8551              error message later.  */
8552           if (type_spec && !is_cv_qualifier)
8553             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8554           /* A constructor declarator cannot follow a type-specifier.  */
8555           if (type_spec)
8556             {
8557               constructor_possible_p = false;
8558               found_decl_spec = true;
8559             }
8560         }
8561
8562       /* If we still do not have a DECL_SPEC, then there are no more
8563          decl-specifiers.  */
8564       if (!found_decl_spec)
8565         break;
8566
8567       decl_specs->any_specifiers_p = true;
8568       /* After we see one decl-specifier, further decl-specifiers are
8569          always optional.  */
8570       flags |= CP_PARSER_FLAGS_OPTIONAL;
8571     }
8572
8573   cp_parser_check_decl_spec (decl_specs, start_token->location);
8574
8575   /* Don't allow a friend specifier with a class definition.  */
8576   if (decl_specs->specs[(int) ds_friend] != 0
8577       && (*declares_class_or_enum & 2))
8578     error ("%Hclass definition may not be declared a friend",
8579             &start_token->location);
8580 }
8581
8582 /* Parse an (optional) storage-class-specifier.
8583
8584    storage-class-specifier:
8585      auto
8586      register
8587      static
8588      extern
8589      mutable
8590
8591    GNU Extension:
8592
8593    storage-class-specifier:
8594      thread
8595
8596    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8597
8598 static tree
8599 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8600 {
8601   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8602     {
8603     case RID_AUTO:
8604       if (cxx_dialect != cxx98)
8605         return NULL_TREE;
8606       /* Fall through for C++98.  */
8607
8608     case RID_REGISTER:
8609     case RID_STATIC:
8610     case RID_EXTERN:
8611     case RID_MUTABLE:
8612     case RID_THREAD:
8613       /* Consume the token.  */
8614       return cp_lexer_consume_token (parser->lexer)->u.value;
8615
8616     default:
8617       return NULL_TREE;
8618     }
8619 }
8620
8621 /* Parse an (optional) function-specifier.
8622
8623    function-specifier:
8624      inline
8625      virtual
8626      explicit
8627
8628    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8629    Updates DECL_SPECS, if it is non-NULL.  */
8630
8631 static tree
8632 cp_parser_function_specifier_opt (cp_parser* parser,
8633                                   cp_decl_specifier_seq *decl_specs)
8634 {
8635   cp_token *token = cp_lexer_peek_token (parser->lexer);
8636   switch (token->keyword)
8637     {
8638     case RID_INLINE:
8639       if (decl_specs)
8640         ++decl_specs->specs[(int) ds_inline];
8641       break;
8642
8643     case RID_VIRTUAL:
8644       /* 14.5.2.3 [temp.mem]
8645
8646          A member function template shall not be virtual.  */
8647       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8648         error ("%Htemplates may not be %<virtual%>", &token->location);
8649       else if (decl_specs)
8650         ++decl_specs->specs[(int) ds_virtual];
8651       break;
8652
8653     case RID_EXPLICIT:
8654       if (decl_specs)
8655         ++decl_specs->specs[(int) ds_explicit];
8656       break;
8657
8658     default:
8659       return NULL_TREE;
8660     }
8661
8662   /* Consume the token.  */
8663   return cp_lexer_consume_token (parser->lexer)->u.value;
8664 }
8665
8666 /* Parse a linkage-specification.
8667
8668    linkage-specification:
8669      extern string-literal { declaration-seq [opt] }
8670      extern string-literal declaration  */
8671
8672 static void
8673 cp_parser_linkage_specification (cp_parser* parser)
8674 {
8675   tree linkage;
8676
8677   /* Look for the `extern' keyword.  */
8678   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8679
8680   /* Look for the string-literal.  */
8681   linkage = cp_parser_string_literal (parser, false, false);
8682
8683   /* Transform the literal into an identifier.  If the literal is a
8684      wide-character string, or contains embedded NULs, then we can't
8685      handle it as the user wants.  */
8686   if (strlen (TREE_STRING_POINTER (linkage))
8687       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8688     {
8689       cp_parser_error (parser, "invalid linkage-specification");
8690       /* Assume C++ linkage.  */
8691       linkage = lang_name_cplusplus;
8692     }
8693   else
8694     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8695
8696   /* We're now using the new linkage.  */
8697   push_lang_context (linkage);
8698
8699   /* If the next token is a `{', then we're using the first
8700      production.  */
8701   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8702     {
8703       /* Consume the `{' token.  */
8704       cp_lexer_consume_token (parser->lexer);
8705       /* Parse the declarations.  */
8706       cp_parser_declaration_seq_opt (parser);
8707       /* Look for the closing `}'.  */
8708       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8709     }
8710   /* Otherwise, there's just one declaration.  */
8711   else
8712     {
8713       bool saved_in_unbraced_linkage_specification_p;
8714
8715       saved_in_unbraced_linkage_specification_p
8716         = parser->in_unbraced_linkage_specification_p;
8717       parser->in_unbraced_linkage_specification_p = true;
8718       cp_parser_declaration (parser);
8719       parser->in_unbraced_linkage_specification_p
8720         = saved_in_unbraced_linkage_specification_p;
8721     }
8722
8723   /* We're done with the linkage-specification.  */
8724   pop_lang_context ();
8725 }
8726
8727 /* Parse a static_assert-declaration.
8728
8729    static_assert-declaration:
8730      static_assert ( constant-expression , string-literal ) ; 
8731
8732    If MEMBER_P, this static_assert is a class member.  */
8733
8734 static void 
8735 cp_parser_static_assert(cp_parser *parser, bool member_p)
8736 {
8737   tree condition;
8738   tree message;
8739   cp_token *token;
8740   location_t saved_loc;
8741
8742   /* Peek at the `static_assert' token so we can keep track of exactly
8743      where the static assertion started.  */
8744   token = cp_lexer_peek_token (parser->lexer);
8745   saved_loc = token->location;
8746
8747   /* Look for the `static_assert' keyword.  */
8748   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8749                                   "%<static_assert%>"))
8750     return;
8751
8752   /*  We know we are in a static assertion; commit to any tentative
8753       parse.  */
8754   if (cp_parser_parsing_tentatively (parser))
8755     cp_parser_commit_to_tentative_parse (parser);
8756
8757   /* Parse the `(' starting the static assertion condition.  */
8758   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8759
8760   /* Parse the constant-expression.  */
8761   condition = 
8762     cp_parser_constant_expression (parser,
8763                                    /*allow_non_constant_p=*/false,
8764                                    /*non_constant_p=*/NULL);
8765
8766   /* Parse the separating `,'.  */
8767   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8768
8769   /* Parse the string-literal message.  */
8770   message = cp_parser_string_literal (parser, 
8771                                       /*translate=*/false,
8772                                       /*wide_ok=*/true);
8773
8774   /* A `)' completes the static assertion.  */
8775   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8776     cp_parser_skip_to_closing_parenthesis (parser, 
8777                                            /*recovering=*/true, 
8778                                            /*or_comma=*/false,
8779                                            /*consume_paren=*/true);
8780
8781   /* A semicolon terminates the declaration.  */
8782   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8783
8784   /* Complete the static assertion, which may mean either processing 
8785      the static assert now or saving it for template instantiation.  */
8786   finish_static_assert (condition, message, saved_loc, member_p);
8787 }
8788
8789 /* Parse a `decltype' type. Returns the type. 
8790
8791    simple-type-specifier:
8792      decltype ( expression )  */
8793
8794 static tree
8795 cp_parser_decltype (cp_parser *parser)
8796 {
8797   tree expr;
8798   bool id_expression_or_member_access_p = false;
8799   const char *saved_message;
8800   bool saved_integral_constant_expression_p;
8801   bool saved_non_integral_constant_expression_p;
8802   cp_token *id_expr_start_token;
8803
8804   /* Look for the `decltype' token.  */
8805   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8806     return error_mark_node;
8807
8808   /* Types cannot be defined in a `decltype' expression.  Save away the
8809      old message.  */
8810   saved_message = parser->type_definition_forbidden_message;
8811
8812   /* And create the new one.  */
8813   parser->type_definition_forbidden_message
8814     = "types may not be defined in %<decltype%> expressions";
8815
8816   /* The restrictions on constant-expressions do not apply inside
8817      decltype expressions.  */
8818   saved_integral_constant_expression_p
8819     = parser->integral_constant_expression_p;
8820   saved_non_integral_constant_expression_p
8821     = parser->non_integral_constant_expression_p;
8822   parser->integral_constant_expression_p = false;
8823
8824   /* Do not actually evaluate the expression.  */
8825   ++skip_evaluation;
8826
8827   /* Parse the opening `('.  */
8828   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8829     return error_mark_node;
8830   
8831   /* First, try parsing an id-expression.  */
8832   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8833   cp_parser_parse_tentatively (parser);
8834   expr = cp_parser_id_expression (parser,
8835                                   /*template_keyword_p=*/false,
8836                                   /*check_dependency_p=*/true,
8837                                   /*template_p=*/NULL,
8838                                   /*declarator_p=*/false,
8839                                   /*optional_p=*/false);
8840
8841   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8842     {
8843       bool non_integral_constant_expression_p = false;
8844       tree id_expression = expr;
8845       cp_id_kind idk;
8846       const char *error_msg;
8847
8848       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8849         /* Lookup the name we got back from the id-expression.  */
8850         expr = cp_parser_lookup_name (parser, expr,
8851                                       none_type,
8852                                       /*is_template=*/false,
8853                                       /*is_namespace=*/false,
8854                                       /*check_dependency=*/true,
8855                                       /*ambiguous_decls=*/NULL,
8856                                       id_expr_start_token->location);
8857
8858       if (expr
8859           && expr != error_mark_node
8860           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8861           && TREE_CODE (expr) != TYPE_DECL
8862           && (TREE_CODE (expr) != BIT_NOT_EXPR
8863               || !TYPE_P (TREE_OPERAND (expr, 0)))
8864           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8865         {
8866           /* Complete lookup of the id-expression.  */
8867           expr = (finish_id_expression
8868                   (id_expression, expr, parser->scope, &idk,
8869                    /*integral_constant_expression_p=*/false,
8870                    /*allow_non_integral_constant_expression_p=*/true,
8871                    &non_integral_constant_expression_p,
8872                    /*template_p=*/false,
8873                    /*done=*/true,
8874                    /*address_p=*/false,
8875                    /*template_arg_p=*/false,
8876                    &error_msg,
8877                    id_expr_start_token->location));
8878
8879           if (expr == error_mark_node)
8880             /* We found an id-expression, but it was something that we
8881                should not have found. This is an error, not something
8882                we can recover from, so note that we found an
8883                id-expression and we'll recover as gracefully as
8884                possible.  */
8885             id_expression_or_member_access_p = true;
8886         }
8887
8888       if (expr 
8889           && expr != error_mark_node
8890           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8891         /* We have an id-expression.  */
8892         id_expression_or_member_access_p = true;
8893     }
8894
8895   if (!id_expression_or_member_access_p)
8896     {
8897       /* Abort the id-expression parse.  */
8898       cp_parser_abort_tentative_parse (parser);
8899
8900       /* Parsing tentatively, again.  */
8901       cp_parser_parse_tentatively (parser);
8902
8903       /* Parse a class member access.  */
8904       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8905                                            /*cast_p=*/false,
8906                                            /*member_access_only_p=*/true, NULL);
8907
8908       if (expr 
8909           && expr != error_mark_node
8910           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8911         /* We have an id-expression.  */
8912         id_expression_or_member_access_p = true;
8913     }
8914
8915   if (id_expression_or_member_access_p)
8916     /* We have parsed the complete id-expression or member access.  */
8917     cp_parser_parse_definitely (parser);
8918   else
8919     {
8920       /* Abort our attempt to parse an id-expression or member access
8921          expression.  */
8922       cp_parser_abort_tentative_parse (parser);
8923
8924       /* Parse a full expression.  */
8925       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8926     }
8927
8928   /* Go back to evaluating expressions.  */
8929   --skip_evaluation;
8930
8931   /* Restore the old message and the integral constant expression
8932      flags.  */
8933   parser->type_definition_forbidden_message = saved_message;
8934   parser->integral_constant_expression_p
8935     = saved_integral_constant_expression_p;
8936   parser->non_integral_constant_expression_p
8937     = saved_non_integral_constant_expression_p;
8938
8939   if (expr == error_mark_node)
8940     {
8941       /* Skip everything up to the closing `)'.  */
8942       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8943                                              /*consume_paren=*/true);
8944       return error_mark_node;
8945     }
8946   
8947   /* Parse to the closing `)'.  */
8948   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8949     {
8950       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8951                                              /*consume_paren=*/true);
8952       return error_mark_node;
8953     }
8954
8955   return finish_decltype_type (expr, id_expression_or_member_access_p);
8956 }
8957
8958 /* Special member functions [gram.special] */
8959
8960 /* Parse a conversion-function-id.
8961
8962    conversion-function-id:
8963      operator conversion-type-id
8964
8965    Returns an IDENTIFIER_NODE representing the operator.  */
8966
8967 static tree
8968 cp_parser_conversion_function_id (cp_parser* parser)
8969 {
8970   tree type;
8971   tree saved_scope;
8972   tree saved_qualifying_scope;
8973   tree saved_object_scope;
8974   tree pushed_scope = NULL_TREE;
8975
8976   /* Look for the `operator' token.  */
8977   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8978     return error_mark_node;
8979   /* When we parse the conversion-type-id, the current scope will be
8980      reset.  However, we need that information in able to look up the
8981      conversion function later, so we save it here.  */
8982   saved_scope = parser->scope;
8983   saved_qualifying_scope = parser->qualifying_scope;
8984   saved_object_scope = parser->object_scope;
8985   /* We must enter the scope of the class so that the names of
8986      entities declared within the class are available in the
8987      conversion-type-id.  For example, consider:
8988
8989        struct S {
8990          typedef int I;
8991          operator I();
8992        };
8993
8994        S::operator I() { ... }
8995
8996      In order to see that `I' is a type-name in the definition, we
8997      must be in the scope of `S'.  */
8998   if (saved_scope)
8999     pushed_scope = push_scope (saved_scope);
9000   /* Parse the conversion-type-id.  */
9001   type = cp_parser_conversion_type_id (parser);
9002   /* Leave the scope of the class, if any.  */
9003   if (pushed_scope)
9004     pop_scope (pushed_scope);
9005   /* Restore the saved scope.  */
9006   parser->scope = saved_scope;
9007   parser->qualifying_scope = saved_qualifying_scope;
9008   parser->object_scope = saved_object_scope;
9009   /* If the TYPE is invalid, indicate failure.  */
9010   if (type == error_mark_node)
9011     return error_mark_node;
9012   return mangle_conv_op_name_for_type (type);
9013 }
9014
9015 /* Parse a conversion-type-id:
9016
9017    conversion-type-id:
9018      type-specifier-seq conversion-declarator [opt]
9019
9020    Returns the TYPE specified.  */
9021
9022 static tree
9023 cp_parser_conversion_type_id (cp_parser* parser)
9024 {
9025   tree attributes;
9026   cp_decl_specifier_seq type_specifiers;
9027   cp_declarator *declarator;
9028   tree type_specified;
9029
9030   /* Parse the attributes.  */
9031   attributes = cp_parser_attributes_opt (parser);
9032   /* Parse the type-specifiers.  */
9033   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9034                                 &type_specifiers);
9035   /* If that didn't work, stop.  */
9036   if (type_specifiers.type == error_mark_node)
9037     return error_mark_node;
9038   /* Parse the conversion-declarator.  */
9039   declarator = cp_parser_conversion_declarator_opt (parser);
9040
9041   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9042                                     /*initialized=*/0, &attributes);
9043   if (attributes)
9044     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9045
9046   /* Don't give this error when parsing tentatively.  This happens to
9047      work because we always parse this definitively once.  */
9048   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9049       && type_uses_auto (type_specified))
9050     {
9051       error ("invalid use of %<auto%> in conversion operator");
9052       return error_mark_node;
9053     }
9054
9055   return type_specified;
9056 }
9057
9058 /* Parse an (optional) conversion-declarator.
9059
9060    conversion-declarator:
9061      ptr-operator conversion-declarator [opt]
9062
9063    */
9064
9065 static cp_declarator *
9066 cp_parser_conversion_declarator_opt (cp_parser* parser)
9067 {
9068   enum tree_code code;
9069   tree class_type;
9070   cp_cv_quals cv_quals;
9071
9072   /* We don't know if there's a ptr-operator next, or not.  */
9073   cp_parser_parse_tentatively (parser);
9074   /* Try the ptr-operator.  */
9075   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9076   /* If it worked, look for more conversion-declarators.  */
9077   if (cp_parser_parse_definitely (parser))
9078     {
9079       cp_declarator *declarator;
9080
9081       /* Parse another optional declarator.  */
9082       declarator = cp_parser_conversion_declarator_opt (parser);
9083
9084       return cp_parser_make_indirect_declarator
9085         (code, class_type, cv_quals, declarator);
9086    }
9087
9088   return NULL;
9089 }
9090
9091 /* Parse an (optional) ctor-initializer.
9092
9093    ctor-initializer:
9094      : mem-initializer-list
9095
9096    Returns TRUE iff the ctor-initializer was actually present.  */
9097
9098 static bool
9099 cp_parser_ctor_initializer_opt (cp_parser* parser)
9100 {
9101   /* If the next token is not a `:', then there is no
9102      ctor-initializer.  */
9103   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9104     {
9105       /* Do default initialization of any bases and members.  */
9106       if (DECL_CONSTRUCTOR_P (current_function_decl))
9107         finish_mem_initializers (NULL_TREE);
9108
9109       return false;
9110     }
9111
9112   /* Consume the `:' token.  */
9113   cp_lexer_consume_token (parser->lexer);
9114   /* And the mem-initializer-list.  */
9115   cp_parser_mem_initializer_list (parser);
9116
9117   return true;
9118 }
9119
9120 /* Parse a mem-initializer-list.
9121
9122    mem-initializer-list:
9123      mem-initializer ... [opt]
9124      mem-initializer ... [opt] , mem-initializer-list  */
9125
9126 static void
9127 cp_parser_mem_initializer_list (cp_parser* parser)
9128 {
9129   tree mem_initializer_list = NULL_TREE;
9130   cp_token *token = cp_lexer_peek_token (parser->lexer);
9131
9132   /* Let the semantic analysis code know that we are starting the
9133      mem-initializer-list.  */
9134   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9135     error ("%Honly constructors take base initializers",
9136            &token->location);
9137
9138   /* Loop through the list.  */
9139   while (true)
9140     {
9141       tree mem_initializer;
9142
9143       token = cp_lexer_peek_token (parser->lexer);
9144       /* Parse the mem-initializer.  */
9145       mem_initializer = cp_parser_mem_initializer (parser);
9146       /* If the next token is a `...', we're expanding member initializers. */
9147       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9148         {
9149           /* Consume the `...'. */
9150           cp_lexer_consume_token (parser->lexer);
9151
9152           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9153              can be expanded but members cannot. */
9154           if (mem_initializer != error_mark_node
9155               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9156             {
9157               error ("%Hcannot expand initializer for member %<%D%>",
9158                      &token->location, TREE_PURPOSE (mem_initializer));
9159               mem_initializer = error_mark_node;
9160             }
9161
9162           /* Construct the pack expansion type. */
9163           if (mem_initializer != error_mark_node)
9164             mem_initializer = make_pack_expansion (mem_initializer);
9165         }
9166       /* Add it to the list, unless it was erroneous.  */
9167       if (mem_initializer != error_mark_node)
9168         {
9169           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9170           mem_initializer_list = mem_initializer;
9171         }
9172       /* If the next token is not a `,', we're done.  */
9173       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9174         break;
9175       /* Consume the `,' token.  */
9176       cp_lexer_consume_token (parser->lexer);
9177     }
9178
9179   /* Perform semantic analysis.  */
9180   if (DECL_CONSTRUCTOR_P (current_function_decl))
9181     finish_mem_initializers (mem_initializer_list);
9182 }
9183
9184 /* Parse a mem-initializer.
9185
9186    mem-initializer:
9187      mem-initializer-id ( expression-list [opt] )
9188      mem-initializer-id braced-init-list
9189
9190    GNU extension:
9191
9192    mem-initializer:
9193      ( expression-list [opt] )
9194
9195    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9196    class) or FIELD_DECL (for a non-static data member) to initialize;
9197    the TREE_VALUE is the expression-list.  An empty initialization
9198    list is represented by void_list_node.  */
9199
9200 static tree
9201 cp_parser_mem_initializer (cp_parser* parser)
9202 {
9203   tree mem_initializer_id;
9204   tree expression_list;
9205   tree member;
9206   cp_token *token = cp_lexer_peek_token (parser->lexer);
9207
9208   /* Find out what is being initialized.  */
9209   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9210     {
9211       permerror (token->location,
9212                  "anachronistic old-style base class initializer");
9213       mem_initializer_id = NULL_TREE;
9214     }
9215   else
9216     {
9217       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9218       if (mem_initializer_id == error_mark_node)
9219         return mem_initializer_id;
9220     }
9221   member = expand_member_init (mem_initializer_id);
9222   if (member && !DECL_P (member))
9223     in_base_initializer = 1;
9224
9225   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9226     {
9227       bool expr_non_constant_p;
9228       maybe_warn_cpp0x ("extended initializer lists");
9229       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9230       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9231       expression_list = build_tree_list (NULL_TREE, expression_list);
9232     }
9233   else
9234     expression_list
9235       = cp_parser_parenthesized_expression_list (parser, false,
9236                                                  /*cast_p=*/false,
9237                                                  /*allow_expansion_p=*/true,
9238                                                  /*non_constant_p=*/NULL);
9239   if (expression_list == error_mark_node)
9240     return error_mark_node;
9241   if (!expression_list)
9242     expression_list = void_type_node;
9243
9244   in_base_initializer = 0;
9245
9246   return member ? build_tree_list (member, expression_list) : error_mark_node;
9247 }
9248
9249 /* Parse a mem-initializer-id.
9250
9251    mem-initializer-id:
9252      :: [opt] nested-name-specifier [opt] class-name
9253      identifier
9254
9255    Returns a TYPE indicating the class to be initializer for the first
9256    production.  Returns an IDENTIFIER_NODE indicating the data member
9257    to be initialized for the second production.  */
9258
9259 static tree
9260 cp_parser_mem_initializer_id (cp_parser* parser)
9261 {
9262   bool global_scope_p;
9263   bool nested_name_specifier_p;
9264   bool template_p = false;
9265   tree id;
9266
9267   cp_token *token = cp_lexer_peek_token (parser->lexer);
9268
9269   /* `typename' is not allowed in this context ([temp.res]).  */
9270   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9271     {
9272       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9273              "member initializer is implicitly a type)",
9274              &token->location);
9275       cp_lexer_consume_token (parser->lexer);
9276     }
9277   /* Look for the optional `::' operator.  */
9278   global_scope_p
9279     = (cp_parser_global_scope_opt (parser,
9280                                    /*current_scope_valid_p=*/false)
9281        != NULL_TREE);
9282   /* Look for the optional nested-name-specifier.  The simplest way to
9283      implement:
9284
9285        [temp.res]
9286
9287        The keyword `typename' is not permitted in a base-specifier or
9288        mem-initializer; in these contexts a qualified name that
9289        depends on a template-parameter is implicitly assumed to be a
9290        type name.
9291
9292      is to assume that we have seen the `typename' keyword at this
9293      point.  */
9294   nested_name_specifier_p
9295     = (cp_parser_nested_name_specifier_opt (parser,
9296                                             /*typename_keyword_p=*/true,
9297                                             /*check_dependency_p=*/true,
9298                                             /*type_p=*/true,
9299                                             /*is_declaration=*/true)
9300        != NULL_TREE);
9301   if (nested_name_specifier_p)
9302     template_p = cp_parser_optional_template_keyword (parser);
9303   /* If there is a `::' operator or a nested-name-specifier, then we
9304      are definitely looking for a class-name.  */
9305   if (global_scope_p || nested_name_specifier_p)
9306     return cp_parser_class_name (parser,
9307                                  /*typename_keyword_p=*/true,
9308                                  /*template_keyword_p=*/template_p,
9309                                  none_type,
9310                                  /*check_dependency_p=*/true,
9311                                  /*class_head_p=*/false,
9312                                  /*is_declaration=*/true);
9313   /* Otherwise, we could also be looking for an ordinary identifier.  */
9314   cp_parser_parse_tentatively (parser);
9315   /* Try a class-name.  */
9316   id = cp_parser_class_name (parser,
9317                              /*typename_keyword_p=*/true,
9318                              /*template_keyword_p=*/false,
9319                              none_type,
9320                              /*check_dependency_p=*/true,
9321                              /*class_head_p=*/false,
9322                              /*is_declaration=*/true);
9323   /* If we found one, we're done.  */
9324   if (cp_parser_parse_definitely (parser))
9325     return id;
9326   /* Otherwise, look for an ordinary identifier.  */
9327   return cp_parser_identifier (parser);
9328 }
9329
9330 /* Overloading [gram.over] */
9331
9332 /* Parse an operator-function-id.
9333
9334    operator-function-id:
9335      operator operator
9336
9337    Returns an IDENTIFIER_NODE for the operator which is a
9338    human-readable spelling of the identifier, e.g., `operator +'.  */
9339
9340 static tree
9341 cp_parser_operator_function_id (cp_parser* parser)
9342 {
9343   /* Look for the `operator' keyword.  */
9344   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9345     return error_mark_node;
9346   /* And then the name of the operator itself.  */
9347   return cp_parser_operator (parser);
9348 }
9349
9350 /* Parse an operator.
9351
9352    operator:
9353      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9354      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9355      || ++ -- , ->* -> () []
9356
9357    GNU Extensions:
9358
9359    operator:
9360      <? >? <?= >?=
9361
9362    Returns an IDENTIFIER_NODE for the operator which is a
9363    human-readable spelling of the identifier, e.g., `operator +'.  */
9364
9365 static tree
9366 cp_parser_operator (cp_parser* parser)
9367 {
9368   tree id = NULL_TREE;
9369   cp_token *token;
9370
9371   /* Peek at the next token.  */
9372   token = cp_lexer_peek_token (parser->lexer);
9373   /* Figure out which operator we have.  */
9374   switch (token->type)
9375     {
9376     case CPP_KEYWORD:
9377       {
9378         enum tree_code op;
9379
9380         /* The keyword should be either `new' or `delete'.  */
9381         if (token->keyword == RID_NEW)
9382           op = NEW_EXPR;
9383         else if (token->keyword == RID_DELETE)
9384           op = DELETE_EXPR;
9385         else
9386           break;
9387
9388         /* Consume the `new' or `delete' token.  */
9389         cp_lexer_consume_token (parser->lexer);
9390
9391         /* Peek at the next token.  */
9392         token = cp_lexer_peek_token (parser->lexer);
9393         /* If it's a `[' token then this is the array variant of the
9394            operator.  */
9395         if (token->type == CPP_OPEN_SQUARE)
9396           {
9397             /* Consume the `[' token.  */
9398             cp_lexer_consume_token (parser->lexer);
9399             /* Look for the `]' token.  */
9400             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9401             id = ansi_opname (op == NEW_EXPR
9402                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9403           }
9404         /* Otherwise, we have the non-array variant.  */
9405         else
9406           id = ansi_opname (op);
9407
9408         return id;
9409       }
9410
9411     case CPP_PLUS:
9412       id = ansi_opname (PLUS_EXPR);
9413       break;
9414
9415     case CPP_MINUS:
9416       id = ansi_opname (MINUS_EXPR);
9417       break;
9418
9419     case CPP_MULT:
9420       id = ansi_opname (MULT_EXPR);
9421       break;
9422
9423     case CPP_DIV:
9424       id = ansi_opname (TRUNC_DIV_EXPR);
9425       break;
9426
9427     case CPP_MOD:
9428       id = ansi_opname (TRUNC_MOD_EXPR);
9429       break;
9430
9431     case CPP_XOR:
9432       id = ansi_opname (BIT_XOR_EXPR);
9433       break;
9434
9435     case CPP_AND:
9436       id = ansi_opname (BIT_AND_EXPR);
9437       break;
9438
9439     case CPP_OR:
9440       id = ansi_opname (BIT_IOR_EXPR);
9441       break;
9442
9443     case CPP_COMPL:
9444       id = ansi_opname (BIT_NOT_EXPR);
9445       break;
9446
9447     case CPP_NOT:
9448       id = ansi_opname (TRUTH_NOT_EXPR);
9449       break;
9450
9451     case CPP_EQ:
9452       id = ansi_assopname (NOP_EXPR);
9453       break;
9454
9455     case CPP_LESS:
9456       id = ansi_opname (LT_EXPR);
9457       break;
9458
9459     case CPP_GREATER:
9460       id = ansi_opname (GT_EXPR);
9461       break;
9462
9463     case CPP_PLUS_EQ:
9464       id = ansi_assopname (PLUS_EXPR);
9465       break;
9466
9467     case CPP_MINUS_EQ:
9468       id = ansi_assopname (MINUS_EXPR);
9469       break;
9470
9471     case CPP_MULT_EQ:
9472       id = ansi_assopname (MULT_EXPR);
9473       break;
9474
9475     case CPP_DIV_EQ:
9476       id = ansi_assopname (TRUNC_DIV_EXPR);
9477       break;
9478
9479     case CPP_MOD_EQ:
9480       id = ansi_assopname (TRUNC_MOD_EXPR);
9481       break;
9482
9483     case CPP_XOR_EQ:
9484       id = ansi_assopname (BIT_XOR_EXPR);
9485       break;
9486
9487     case CPP_AND_EQ:
9488       id = ansi_assopname (BIT_AND_EXPR);
9489       break;
9490
9491     case CPP_OR_EQ:
9492       id = ansi_assopname (BIT_IOR_EXPR);
9493       break;
9494
9495     case CPP_LSHIFT:
9496       id = ansi_opname (LSHIFT_EXPR);
9497       break;
9498
9499     case CPP_RSHIFT:
9500       id = ansi_opname (RSHIFT_EXPR);
9501       break;
9502
9503     case CPP_LSHIFT_EQ:
9504       id = ansi_assopname (LSHIFT_EXPR);
9505       break;
9506
9507     case CPP_RSHIFT_EQ:
9508       id = ansi_assopname (RSHIFT_EXPR);
9509       break;
9510
9511     case CPP_EQ_EQ:
9512       id = ansi_opname (EQ_EXPR);
9513       break;
9514
9515     case CPP_NOT_EQ:
9516       id = ansi_opname (NE_EXPR);
9517       break;
9518
9519     case CPP_LESS_EQ:
9520       id = ansi_opname (LE_EXPR);
9521       break;
9522
9523     case CPP_GREATER_EQ:
9524       id = ansi_opname (GE_EXPR);
9525       break;
9526
9527     case CPP_AND_AND:
9528       id = ansi_opname (TRUTH_ANDIF_EXPR);
9529       break;
9530
9531     case CPP_OR_OR:
9532       id = ansi_opname (TRUTH_ORIF_EXPR);
9533       break;
9534
9535     case CPP_PLUS_PLUS:
9536       id = ansi_opname (POSTINCREMENT_EXPR);
9537       break;
9538
9539     case CPP_MINUS_MINUS:
9540       id = ansi_opname (PREDECREMENT_EXPR);
9541       break;
9542
9543     case CPP_COMMA:
9544       id = ansi_opname (COMPOUND_EXPR);
9545       break;
9546
9547     case CPP_DEREF_STAR:
9548       id = ansi_opname (MEMBER_REF);
9549       break;
9550
9551     case CPP_DEREF:
9552       id = ansi_opname (COMPONENT_REF);
9553       break;
9554
9555     case CPP_OPEN_PAREN:
9556       /* Consume the `('.  */
9557       cp_lexer_consume_token (parser->lexer);
9558       /* Look for the matching `)'.  */
9559       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9560       return ansi_opname (CALL_EXPR);
9561
9562     case CPP_OPEN_SQUARE:
9563       /* Consume the `['.  */
9564       cp_lexer_consume_token (parser->lexer);
9565       /* Look for the matching `]'.  */
9566       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9567       return ansi_opname (ARRAY_REF);
9568
9569     default:
9570       /* Anything else is an error.  */
9571       break;
9572     }
9573
9574   /* If we have selected an identifier, we need to consume the
9575      operator token.  */
9576   if (id)
9577     cp_lexer_consume_token (parser->lexer);
9578   /* Otherwise, no valid operator name was present.  */
9579   else
9580     {
9581       cp_parser_error (parser, "expected operator");
9582       id = error_mark_node;
9583     }
9584
9585   return id;
9586 }
9587
9588 /* Parse a template-declaration.
9589
9590    template-declaration:
9591      export [opt] template < template-parameter-list > declaration
9592
9593    If MEMBER_P is TRUE, this template-declaration occurs within a
9594    class-specifier.
9595
9596    The grammar rule given by the standard isn't correct.  What
9597    is really meant is:
9598
9599    template-declaration:
9600      export [opt] template-parameter-list-seq
9601        decl-specifier-seq [opt] init-declarator [opt] ;
9602      export [opt] template-parameter-list-seq
9603        function-definition
9604
9605    template-parameter-list-seq:
9606      template-parameter-list-seq [opt]
9607      template < template-parameter-list >  */
9608
9609 static void
9610 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9611 {
9612   /* Check for `export'.  */
9613   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9614     {
9615       /* Consume the `export' token.  */
9616       cp_lexer_consume_token (parser->lexer);
9617       /* Warn that we do not support `export'.  */
9618       warning (0, "keyword %<export%> not implemented, and will be ignored");
9619     }
9620
9621   cp_parser_template_declaration_after_export (parser, member_p);
9622 }
9623
9624 /* Parse a template-parameter-list.
9625
9626    template-parameter-list:
9627      template-parameter
9628      template-parameter-list , template-parameter
9629
9630    Returns a TREE_LIST.  Each node represents a template parameter.
9631    The nodes are connected via their TREE_CHAINs.  */
9632
9633 static tree
9634 cp_parser_template_parameter_list (cp_parser* parser)
9635 {
9636   tree parameter_list = NULL_TREE;
9637
9638   begin_template_parm_list ();
9639   while (true)
9640     {
9641       tree parameter;
9642       bool is_non_type;
9643       bool is_parameter_pack;
9644
9645       /* Parse the template-parameter.  */
9646       parameter = cp_parser_template_parameter (parser, 
9647                                                 &is_non_type,
9648                                                 &is_parameter_pack);
9649       /* Add it to the list.  */
9650       if (parameter != error_mark_node)
9651         parameter_list = process_template_parm (parameter_list,
9652                                                 parameter,
9653                                                 is_non_type,
9654                                                 is_parameter_pack);
9655       else
9656        {
9657          tree err_parm = build_tree_list (parameter, parameter);
9658          TREE_VALUE (err_parm) = error_mark_node;
9659          parameter_list = chainon (parameter_list, err_parm);
9660        }
9661
9662       /* If the next token is not a `,', we're done.  */
9663       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9664         break;
9665       /* Otherwise, consume the `,' token.  */
9666       cp_lexer_consume_token (parser->lexer);
9667     }
9668
9669   return end_template_parm_list (parameter_list);
9670 }
9671
9672 /* Parse a template-parameter.
9673
9674    template-parameter:
9675      type-parameter
9676      parameter-declaration
9677
9678    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9679    the parameter.  The TREE_PURPOSE is the default value, if any.
9680    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9681    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9682    set to true iff this parameter is a parameter pack. */
9683
9684 static tree
9685 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9686                               bool *is_parameter_pack)
9687 {
9688   cp_token *token;
9689   cp_parameter_declarator *parameter_declarator;
9690   cp_declarator *id_declarator;
9691   tree parm;
9692
9693   /* Assume it is a type parameter or a template parameter.  */
9694   *is_non_type = false;
9695   /* Assume it not a parameter pack. */
9696   *is_parameter_pack = false;
9697   /* Peek at the next token.  */
9698   token = cp_lexer_peek_token (parser->lexer);
9699   /* If it is `class' or `template', we have a type-parameter.  */
9700   if (token->keyword == RID_TEMPLATE)
9701     return cp_parser_type_parameter (parser, is_parameter_pack);
9702   /* If it is `class' or `typename' we do not know yet whether it is a
9703      type parameter or a non-type parameter.  Consider:
9704
9705        template <typename T, typename T::X X> ...
9706
9707      or:
9708
9709        template <class C, class D*> ...
9710
9711      Here, the first parameter is a type parameter, and the second is
9712      a non-type parameter.  We can tell by looking at the token after
9713      the identifier -- if it is a `,', `=', or `>' then we have a type
9714      parameter.  */
9715   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9716     {
9717       /* Peek at the token after `class' or `typename'.  */
9718       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9719       /* If it's an ellipsis, we have a template type parameter
9720          pack. */
9721       if (token->type == CPP_ELLIPSIS)
9722         return cp_parser_type_parameter (parser, is_parameter_pack);
9723       /* If it's an identifier, skip it.  */
9724       if (token->type == CPP_NAME)
9725         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9726       /* Now, see if the token looks like the end of a template
9727          parameter.  */
9728       if (token->type == CPP_COMMA
9729           || token->type == CPP_EQ
9730           || token->type == CPP_GREATER)
9731         return cp_parser_type_parameter (parser, is_parameter_pack);
9732     }
9733
9734   /* Otherwise, it is a non-type parameter.
9735
9736      [temp.param]
9737
9738      When parsing a default template-argument for a non-type
9739      template-parameter, the first non-nested `>' is taken as the end
9740      of the template parameter-list rather than a greater-than
9741      operator.  */
9742   *is_non_type = true;
9743   parameter_declarator
9744      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9745                                         /*parenthesized_p=*/NULL);
9746
9747   /* If the parameter declaration is marked as a parameter pack, set
9748      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9749      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9750      grokdeclarator. */
9751   if (parameter_declarator
9752       && parameter_declarator->declarator
9753       && parameter_declarator->declarator->parameter_pack_p)
9754     {
9755       *is_parameter_pack = true;
9756       parameter_declarator->declarator->parameter_pack_p = false;
9757     }
9758
9759   /* If the next token is an ellipsis, and we don't already have it
9760      marked as a parameter pack, then we have a parameter pack (that
9761      has no declarator).  */
9762   if (!*is_parameter_pack
9763       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9764       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9765     {
9766       /* Consume the `...'.  */
9767       cp_lexer_consume_token (parser->lexer);
9768       maybe_warn_variadic_templates ();
9769       
9770       *is_parameter_pack = true;
9771     }
9772   /* We might end up with a pack expansion as the type of the non-type
9773      template parameter, in which case this is a non-type template
9774      parameter pack.  */
9775   else if (parameter_declarator
9776            && parameter_declarator->decl_specifiers.type
9777            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9778     {
9779       *is_parameter_pack = true;
9780       parameter_declarator->decl_specifiers.type = 
9781         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9782     }
9783
9784   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9785     {
9786       /* Parameter packs cannot have default arguments.  However, a
9787          user may try to do so, so we'll parse them and give an
9788          appropriate diagnostic here.  */
9789
9790       /* Consume the `='.  */
9791       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9792       cp_lexer_consume_token (parser->lexer);
9793       
9794       /* Find the name of the parameter pack.  */     
9795       id_declarator = parameter_declarator->declarator;
9796       while (id_declarator && id_declarator->kind != cdk_id)
9797         id_declarator = id_declarator->declarator;
9798       
9799       if (id_declarator && id_declarator->kind == cdk_id)
9800         error ("%Htemplate parameter pack %qD cannot have a default argument",
9801                &start_token->location, id_declarator->u.id.unqualified_name);
9802       else
9803         error ("%Htemplate parameter pack cannot have a default argument",
9804                &start_token->location);
9805       
9806       /* Parse the default argument, but throw away the result.  */
9807       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9808     }
9809
9810   parm = grokdeclarator (parameter_declarator->declarator,
9811                          &parameter_declarator->decl_specifiers,
9812                          PARM, /*initialized=*/0,
9813                          /*attrlist=*/NULL);
9814   if (parm == error_mark_node)
9815     return error_mark_node;
9816
9817   return build_tree_list (parameter_declarator->default_argument, parm);
9818 }
9819
9820 /* Parse a type-parameter.
9821
9822    type-parameter:
9823      class identifier [opt]
9824      class identifier [opt] = type-id
9825      typename identifier [opt]
9826      typename identifier [opt] = type-id
9827      template < template-parameter-list > class identifier [opt]
9828      template < template-parameter-list > class identifier [opt]
9829        = id-expression
9830
9831    GNU Extension (variadic templates):
9832
9833    type-parameter:
9834      class ... identifier [opt]
9835      typename ... identifier [opt]
9836
9837    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9838    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9839    the declaration of the parameter.
9840
9841    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9842
9843 static tree
9844 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9845 {
9846   cp_token *token;
9847   tree parameter;
9848
9849   /* Look for a keyword to tell us what kind of parameter this is.  */
9850   token = cp_parser_require (parser, CPP_KEYWORD,
9851                              "%<class%>, %<typename%>, or %<template%>");
9852   if (!token)
9853     return error_mark_node;
9854
9855   switch (token->keyword)
9856     {
9857     case RID_CLASS:
9858     case RID_TYPENAME:
9859       {
9860         tree identifier;
9861         tree default_argument;
9862
9863         /* If the next token is an ellipsis, we have a template
9864            argument pack. */
9865         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9866           {
9867             /* Consume the `...' token. */
9868             cp_lexer_consume_token (parser->lexer);
9869             maybe_warn_variadic_templates ();
9870
9871             *is_parameter_pack = true;
9872           }
9873
9874         /* If the next token is an identifier, then it names the
9875            parameter.  */
9876         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9877           identifier = cp_parser_identifier (parser);
9878         else
9879           identifier = NULL_TREE;
9880
9881         /* Create the parameter.  */
9882         parameter = finish_template_type_parm (class_type_node, identifier);
9883
9884         /* If the next token is an `=', we have a default argument.  */
9885         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9886           {
9887             /* Consume the `=' token.  */
9888             cp_lexer_consume_token (parser->lexer);
9889             /* Parse the default-argument.  */
9890             push_deferring_access_checks (dk_no_deferred);
9891             default_argument = cp_parser_type_id (parser);
9892
9893             /* Template parameter packs cannot have default
9894                arguments. */
9895             if (*is_parameter_pack)
9896               {
9897                 if (identifier)
9898                   error ("%Htemplate parameter pack %qD cannot have a "
9899                          "default argument", &token->location, identifier);
9900                 else
9901                   error ("%Htemplate parameter packs cannot have "
9902                          "default arguments", &token->location);
9903                 default_argument = NULL_TREE;
9904               }
9905             pop_deferring_access_checks ();
9906           }
9907         else
9908           default_argument = NULL_TREE;
9909
9910         /* Create the combined representation of the parameter and the
9911            default argument.  */
9912         parameter = build_tree_list (default_argument, parameter);
9913       }
9914       break;
9915
9916     case RID_TEMPLATE:
9917       {
9918         tree parameter_list;
9919         tree identifier;
9920         tree default_argument;
9921
9922         /* Look for the `<'.  */
9923         cp_parser_require (parser, CPP_LESS, "%<<%>");
9924         /* Parse the template-parameter-list.  */
9925         parameter_list = cp_parser_template_parameter_list (parser);
9926         /* Look for the `>'.  */
9927         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9928         /* Look for the `class' keyword.  */
9929         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9930         /* If the next token is an ellipsis, we have a template
9931            argument pack. */
9932         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9933           {
9934             /* Consume the `...' token. */
9935             cp_lexer_consume_token (parser->lexer);
9936             maybe_warn_variadic_templates ();
9937
9938             *is_parameter_pack = true;
9939           }
9940         /* If the next token is an `=', then there is a
9941            default-argument.  If the next token is a `>', we are at
9942            the end of the parameter-list.  If the next token is a `,',
9943            then we are at the end of this parameter.  */
9944         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9945             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9946             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9947           {
9948             identifier = cp_parser_identifier (parser);
9949             /* Treat invalid names as if the parameter were nameless.  */
9950             if (identifier == error_mark_node)
9951               identifier = NULL_TREE;
9952           }
9953         else
9954           identifier = NULL_TREE;
9955
9956         /* Create the template parameter.  */
9957         parameter = finish_template_template_parm (class_type_node,
9958                                                    identifier);
9959
9960         /* If the next token is an `=', then there is a
9961            default-argument.  */
9962         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9963           {
9964             bool is_template;
9965
9966             /* Consume the `='.  */
9967             cp_lexer_consume_token (parser->lexer);
9968             /* Parse the id-expression.  */
9969             push_deferring_access_checks (dk_no_deferred);
9970             /* save token before parsing the id-expression, for error
9971                reporting */
9972             token = cp_lexer_peek_token (parser->lexer);
9973             default_argument
9974               = cp_parser_id_expression (parser,
9975                                          /*template_keyword_p=*/false,
9976                                          /*check_dependency_p=*/true,
9977                                          /*template_p=*/&is_template,
9978                                          /*declarator_p=*/false,
9979                                          /*optional_p=*/false);
9980             if (TREE_CODE (default_argument) == TYPE_DECL)
9981               /* If the id-expression was a template-id that refers to
9982                  a template-class, we already have the declaration here,
9983                  so no further lookup is needed.  */
9984                  ;
9985             else
9986               /* Look up the name.  */
9987               default_argument
9988                 = cp_parser_lookup_name (parser, default_argument,
9989                                          none_type,
9990                                          /*is_template=*/is_template,
9991                                          /*is_namespace=*/false,
9992                                          /*check_dependency=*/true,
9993                                          /*ambiguous_decls=*/NULL,
9994                                          token->location);
9995             /* See if the default argument is valid.  */
9996             default_argument
9997               = check_template_template_default_arg (default_argument);
9998
9999             /* Template parameter packs cannot have default
10000                arguments. */
10001             if (*is_parameter_pack)
10002               {
10003                 if (identifier)
10004                   error ("%Htemplate parameter pack %qD cannot "
10005                          "have a default argument",
10006                          &token->location, identifier);
10007                 else
10008                   error ("%Htemplate parameter packs cannot "
10009                          "have default arguments",
10010                          &token->location);
10011                 default_argument = NULL_TREE;
10012               }
10013             pop_deferring_access_checks ();
10014           }
10015         else
10016           default_argument = NULL_TREE;
10017
10018         /* Create the combined representation of the parameter and the
10019            default argument.  */
10020         parameter = build_tree_list (default_argument, parameter);
10021       }
10022       break;
10023
10024     default:
10025       gcc_unreachable ();
10026       break;
10027     }
10028
10029   return parameter;
10030 }
10031
10032 /* Parse a template-id.
10033
10034    template-id:
10035      template-name < template-argument-list [opt] >
10036
10037    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10038    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10039    returned.  Otherwise, if the template-name names a function, or set
10040    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10041    names a class, returns a TYPE_DECL for the specialization.
10042
10043    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10044    uninstantiated templates.  */
10045
10046 static tree
10047 cp_parser_template_id (cp_parser *parser,
10048                        bool template_keyword_p,
10049                        bool check_dependency_p,
10050                        bool is_declaration)
10051 {
10052   int i;
10053   tree templ;
10054   tree arguments;
10055   tree template_id;
10056   cp_token_position start_of_id = 0;
10057   deferred_access_check *chk;
10058   VEC (deferred_access_check,gc) *access_check;
10059   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10060   bool is_identifier;
10061
10062   /* If the next token corresponds to a template-id, there is no need
10063      to reparse it.  */
10064   next_token = cp_lexer_peek_token (parser->lexer);
10065   if (next_token->type == CPP_TEMPLATE_ID)
10066     {
10067       struct tree_check *check_value;
10068
10069       /* Get the stored value.  */
10070       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10071       /* Perform any access checks that were deferred.  */
10072       access_check = check_value->checks;
10073       if (access_check)
10074         {
10075           for (i = 0 ;
10076                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10077                ++i)
10078             {
10079               perform_or_defer_access_check (chk->binfo,
10080                                              chk->decl,
10081                                              chk->diag_decl);
10082             }
10083         }
10084       /* Return the stored value.  */
10085       return check_value->value;
10086     }
10087
10088   /* Avoid performing name lookup if there is no possibility of
10089      finding a template-id.  */
10090   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10091       || (next_token->type == CPP_NAME
10092           && !cp_parser_nth_token_starts_template_argument_list_p
10093                (parser, 2)))
10094     {
10095       cp_parser_error (parser, "expected template-id");
10096       return error_mark_node;
10097     }
10098
10099   /* Remember where the template-id starts.  */
10100   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10101     start_of_id = cp_lexer_token_position (parser->lexer, false);
10102
10103   push_deferring_access_checks (dk_deferred);
10104
10105   /* Parse the template-name.  */
10106   is_identifier = false;
10107   token = cp_lexer_peek_token (parser->lexer);
10108   templ = cp_parser_template_name (parser, template_keyword_p,
10109                                    check_dependency_p,
10110                                    is_declaration,
10111                                    &is_identifier);
10112   if (templ == error_mark_node || is_identifier)
10113     {
10114       pop_deferring_access_checks ();
10115       return templ;
10116     }
10117
10118   /* If we find the sequence `[:' after a template-name, it's probably
10119      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10120      parse correctly the argument list.  */
10121   next_token = cp_lexer_peek_token (parser->lexer);
10122   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10123   if (next_token->type == CPP_OPEN_SQUARE
10124       && next_token->flags & DIGRAPH
10125       && next_token_2->type == CPP_COLON
10126       && !(next_token_2->flags & PREV_WHITE))
10127     {
10128       cp_parser_parse_tentatively (parser);
10129       /* Change `:' into `::'.  */
10130       next_token_2->type = CPP_SCOPE;
10131       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10132          CPP_LESS.  */
10133       cp_lexer_consume_token (parser->lexer);
10134
10135       /* Parse the arguments.  */
10136       arguments = cp_parser_enclosed_template_argument_list (parser);
10137       if (!cp_parser_parse_definitely (parser))
10138         {
10139           /* If we couldn't parse an argument list, then we revert our changes
10140              and return simply an error. Maybe this is not a template-id
10141              after all.  */
10142           next_token_2->type = CPP_COLON;
10143           cp_parser_error (parser, "expected %<<%>");
10144           pop_deferring_access_checks ();
10145           return error_mark_node;
10146         }
10147       /* Otherwise, emit an error about the invalid digraph, but continue
10148          parsing because we got our argument list.  */
10149       if (permerror (next_token->location,
10150                      "%<<::%> cannot begin a template-argument list"))
10151         {
10152           static bool hint = false;
10153           inform (next_token->location,
10154                   "%<<:%> is an alternate spelling for %<[%>."
10155                   " Insert whitespace between %<<%> and %<::%>");
10156           if (!hint && !flag_permissive)
10157             {
10158               inform (next_token->location, "(if you use %<-fpermissive%>"
10159                       " G++ will accept your code)");
10160               hint = true;
10161             }
10162         }
10163     }
10164   else
10165     {
10166       /* Look for the `<' that starts the template-argument-list.  */
10167       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10168         {
10169           pop_deferring_access_checks ();
10170           return error_mark_node;
10171         }
10172       /* Parse the arguments.  */
10173       arguments = cp_parser_enclosed_template_argument_list (parser);
10174     }
10175
10176   /* Build a representation of the specialization.  */
10177   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10178     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10179   else if (DECL_CLASS_TEMPLATE_P (templ)
10180            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10181     {
10182       bool entering_scope;
10183       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10184          template (rather than some instantiation thereof) only if
10185          is not nested within some other construct.  For example, in
10186          "template <typename T> void f(T) { A<T>::", A<T> is just an
10187          instantiation of A.  */
10188       entering_scope = (template_parm_scope_p ()
10189                         && cp_lexer_next_token_is (parser->lexer,
10190                                                    CPP_SCOPE));
10191       template_id
10192         = finish_template_type (templ, arguments, entering_scope);
10193     }
10194   else
10195     {
10196       /* If it's not a class-template or a template-template, it should be
10197          a function-template.  */
10198       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10199                    || TREE_CODE (templ) == OVERLOAD
10200                    || BASELINK_P (templ)));
10201
10202       template_id = lookup_template_function (templ, arguments);
10203     }
10204
10205   /* If parsing tentatively, replace the sequence of tokens that makes
10206      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10207      should we re-parse the token stream, we will not have to repeat
10208      the effort required to do the parse, nor will we issue duplicate
10209      error messages about problems during instantiation of the
10210      template.  */
10211   if (start_of_id)
10212     {
10213       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10214
10215       /* Reset the contents of the START_OF_ID token.  */
10216       token->type = CPP_TEMPLATE_ID;
10217       /* Retrieve any deferred checks.  Do not pop this access checks yet
10218          so the memory will not be reclaimed during token replacing below.  */
10219       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10220       token->u.tree_check_value->value = template_id;
10221       token->u.tree_check_value->checks = get_deferred_access_checks ();
10222       token->keyword = RID_MAX;
10223
10224       /* Purge all subsequent tokens.  */
10225       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10226
10227       /* ??? Can we actually assume that, if template_id ==
10228          error_mark_node, we will have issued a diagnostic to the
10229          user, as opposed to simply marking the tentative parse as
10230          failed?  */
10231       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10232         error ("%Hparse error in template argument list",
10233                &token->location);
10234     }
10235
10236   pop_deferring_access_checks ();
10237   return template_id;
10238 }
10239
10240 /* Parse a template-name.
10241
10242    template-name:
10243      identifier
10244
10245    The standard should actually say:
10246
10247    template-name:
10248      identifier
10249      operator-function-id
10250
10251    A defect report has been filed about this issue.
10252
10253    A conversion-function-id cannot be a template name because they cannot
10254    be part of a template-id. In fact, looking at this code:
10255
10256    a.operator K<int>()
10257
10258    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10259    It is impossible to call a templated conversion-function-id with an
10260    explicit argument list, since the only allowed template parameter is
10261    the type to which it is converting.
10262
10263    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10264    `template' keyword, in a construction like:
10265
10266      T::template f<3>()
10267
10268    In that case `f' is taken to be a template-name, even though there
10269    is no way of knowing for sure.
10270
10271    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10272    name refers to a set of overloaded functions, at least one of which
10273    is a template, or an IDENTIFIER_NODE with the name of the template,
10274    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10275    names are looked up inside uninstantiated templates.  */
10276
10277 static tree
10278 cp_parser_template_name (cp_parser* parser,
10279                          bool template_keyword_p,
10280                          bool check_dependency_p,
10281                          bool is_declaration,
10282                          bool *is_identifier)
10283 {
10284   tree identifier;
10285   tree decl;
10286   tree fns;
10287   cp_token *token = cp_lexer_peek_token (parser->lexer);
10288
10289   /* If the next token is `operator', then we have either an
10290      operator-function-id or a conversion-function-id.  */
10291   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10292     {
10293       /* We don't know whether we're looking at an
10294          operator-function-id or a conversion-function-id.  */
10295       cp_parser_parse_tentatively (parser);
10296       /* Try an operator-function-id.  */
10297       identifier = cp_parser_operator_function_id (parser);
10298       /* If that didn't work, try a conversion-function-id.  */
10299       if (!cp_parser_parse_definitely (parser))
10300         {
10301           cp_parser_error (parser, "expected template-name");
10302           return error_mark_node;
10303         }
10304     }
10305   /* Look for the identifier.  */
10306   else
10307     identifier = cp_parser_identifier (parser);
10308
10309   /* If we didn't find an identifier, we don't have a template-id.  */
10310   if (identifier == error_mark_node)
10311     return error_mark_node;
10312
10313   /* If the name immediately followed the `template' keyword, then it
10314      is a template-name.  However, if the next token is not `<', then
10315      we do not treat it as a template-name, since it is not being used
10316      as part of a template-id.  This enables us to handle constructs
10317      like:
10318
10319        template <typename T> struct S { S(); };
10320        template <typename T> S<T>::S();
10321
10322      correctly.  We would treat `S' as a template -- if it were `S<T>'
10323      -- but we do not if there is no `<'.  */
10324
10325   if (processing_template_decl
10326       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10327     {
10328       /* In a declaration, in a dependent context, we pretend that the
10329          "template" keyword was present in order to improve error
10330          recovery.  For example, given:
10331
10332            template <typename T> void f(T::X<int>);
10333
10334          we want to treat "X<int>" as a template-id.  */
10335       if (is_declaration
10336           && !template_keyword_p
10337           && parser->scope && TYPE_P (parser->scope)
10338           && check_dependency_p
10339           && dependent_scope_p (parser->scope)
10340           /* Do not do this for dtors (or ctors), since they never
10341              need the template keyword before their name.  */
10342           && !constructor_name_p (identifier, parser->scope))
10343         {
10344           cp_token_position start = 0;
10345
10346           /* Explain what went wrong.  */
10347           error ("%Hnon-template %qD used as template",
10348                  &token->location, identifier);
10349           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10350                   parser->scope, identifier);
10351           /* If parsing tentatively, find the location of the "<" token.  */
10352           if (cp_parser_simulate_error (parser))
10353             start = cp_lexer_token_position (parser->lexer, true);
10354           /* Parse the template arguments so that we can issue error
10355              messages about them.  */
10356           cp_lexer_consume_token (parser->lexer);
10357           cp_parser_enclosed_template_argument_list (parser);
10358           /* Skip tokens until we find a good place from which to
10359              continue parsing.  */
10360           cp_parser_skip_to_closing_parenthesis (parser,
10361                                                  /*recovering=*/true,
10362                                                  /*or_comma=*/true,
10363                                                  /*consume_paren=*/false);
10364           /* If parsing tentatively, permanently remove the
10365              template argument list.  That will prevent duplicate
10366              error messages from being issued about the missing
10367              "template" keyword.  */
10368           if (start)
10369             cp_lexer_purge_tokens_after (parser->lexer, start);
10370           if (is_identifier)
10371             *is_identifier = true;
10372           return identifier;
10373         }
10374
10375       /* If the "template" keyword is present, then there is generally
10376          no point in doing name-lookup, so we just return IDENTIFIER.
10377          But, if the qualifying scope is non-dependent then we can
10378          (and must) do name-lookup normally.  */
10379       if (template_keyword_p
10380           && (!parser->scope
10381               || (TYPE_P (parser->scope)
10382                   && dependent_type_p (parser->scope))))
10383         return identifier;
10384     }
10385
10386   /* Look up the name.  */
10387   decl = cp_parser_lookup_name (parser, identifier,
10388                                 none_type,
10389                                 /*is_template=*/false,
10390                                 /*is_namespace=*/false,
10391                                 check_dependency_p,
10392                                 /*ambiguous_decls=*/NULL,
10393                                 token->location);
10394   decl = maybe_get_template_decl_from_type_decl (decl);
10395
10396   /* If DECL is a template, then the name was a template-name.  */
10397   if (TREE_CODE (decl) == TEMPLATE_DECL)
10398     ;
10399   else
10400     {
10401       tree fn = NULL_TREE;
10402
10403       /* The standard does not explicitly indicate whether a name that
10404          names a set of overloaded declarations, some of which are
10405          templates, is a template-name.  However, such a name should
10406          be a template-name; otherwise, there is no way to form a
10407          template-id for the overloaded templates.  */
10408       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10409       if (TREE_CODE (fns) == OVERLOAD)
10410         for (fn = fns; fn; fn = OVL_NEXT (fn))
10411           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10412             break;
10413
10414       if (!fn)
10415         {
10416           /* The name does not name a template.  */
10417           cp_parser_error (parser, "expected template-name");
10418           return error_mark_node;
10419         }
10420     }
10421
10422   /* If DECL is dependent, and refers to a function, then just return
10423      its name; we will look it up again during template instantiation.  */
10424   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10425     {
10426       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10427       if (TYPE_P (scope) && dependent_type_p (scope))
10428         return identifier;
10429     }
10430
10431   return decl;
10432 }
10433
10434 /* Parse a template-argument-list.
10435
10436    template-argument-list:
10437      template-argument ... [opt]
10438      template-argument-list , template-argument ... [opt]
10439
10440    Returns a TREE_VEC containing the arguments.  */
10441
10442 static tree
10443 cp_parser_template_argument_list (cp_parser* parser)
10444 {
10445   tree fixed_args[10];
10446   unsigned n_args = 0;
10447   unsigned alloced = 10;
10448   tree *arg_ary = fixed_args;
10449   tree vec;
10450   bool saved_in_template_argument_list_p;
10451   bool saved_ice_p;
10452   bool saved_non_ice_p;
10453
10454   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10455   parser->in_template_argument_list_p = true;
10456   /* Even if the template-id appears in an integral
10457      constant-expression, the contents of the argument list do
10458      not.  */
10459   saved_ice_p = parser->integral_constant_expression_p;
10460   parser->integral_constant_expression_p = false;
10461   saved_non_ice_p = parser->non_integral_constant_expression_p;
10462   parser->non_integral_constant_expression_p = false;
10463   /* Parse the arguments.  */
10464   do
10465     {
10466       tree argument;
10467
10468       if (n_args)
10469         /* Consume the comma.  */
10470         cp_lexer_consume_token (parser->lexer);
10471
10472       /* Parse the template-argument.  */
10473       argument = cp_parser_template_argument (parser);
10474
10475       /* If the next token is an ellipsis, we're expanding a template
10476          argument pack. */
10477       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10478         {
10479           /* Consume the `...' token. */
10480           cp_lexer_consume_token (parser->lexer);
10481
10482           /* Make the argument into a TYPE_PACK_EXPANSION or
10483              EXPR_PACK_EXPANSION. */
10484           argument = make_pack_expansion (argument);
10485         }
10486
10487       if (n_args == alloced)
10488         {
10489           alloced *= 2;
10490
10491           if (arg_ary == fixed_args)
10492             {
10493               arg_ary = XNEWVEC (tree, alloced);
10494               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10495             }
10496           else
10497             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10498         }
10499       arg_ary[n_args++] = argument;
10500     }
10501   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10502
10503   vec = make_tree_vec (n_args);
10504
10505   while (n_args--)
10506     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10507
10508   if (arg_ary != fixed_args)
10509     free (arg_ary);
10510   parser->non_integral_constant_expression_p = saved_non_ice_p;
10511   parser->integral_constant_expression_p = saved_ice_p;
10512   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10513   return vec;
10514 }
10515
10516 /* Parse a template-argument.
10517
10518    template-argument:
10519      assignment-expression
10520      type-id
10521      id-expression
10522
10523    The representation is that of an assignment-expression, type-id, or
10524    id-expression -- except that the qualified id-expression is
10525    evaluated, so that the value returned is either a DECL or an
10526    OVERLOAD.
10527
10528    Although the standard says "assignment-expression", it forbids
10529    throw-expressions or assignments in the template argument.
10530    Therefore, we use "conditional-expression" instead.  */
10531
10532 static tree
10533 cp_parser_template_argument (cp_parser* parser)
10534 {
10535   tree argument;
10536   bool template_p;
10537   bool address_p;
10538   bool maybe_type_id = false;
10539   cp_token *token = NULL, *argument_start_token = NULL;
10540   cp_id_kind idk;
10541
10542   /* There's really no way to know what we're looking at, so we just
10543      try each alternative in order.
10544
10545        [temp.arg]
10546
10547        In a template-argument, an ambiguity between a type-id and an
10548        expression is resolved to a type-id, regardless of the form of
10549        the corresponding template-parameter.
10550
10551      Therefore, we try a type-id first.  */
10552   cp_parser_parse_tentatively (parser);
10553   argument = cp_parser_template_type_arg (parser);
10554   /* If there was no error parsing the type-id but the next token is a
10555      '>>', our behavior depends on which dialect of C++ we're
10556      parsing. In C++98, we probably found a typo for '> >'. But there
10557      are type-id which are also valid expressions. For instance:
10558
10559      struct X { int operator >> (int); };
10560      template <int V> struct Foo {};
10561      Foo<X () >> 5> r;
10562
10563      Here 'X()' is a valid type-id of a function type, but the user just
10564      wanted to write the expression "X() >> 5". Thus, we remember that we
10565      found a valid type-id, but we still try to parse the argument as an
10566      expression to see what happens. 
10567
10568      In C++0x, the '>>' will be considered two separate '>'
10569      tokens.  */
10570   if (!cp_parser_error_occurred (parser)
10571       && cxx_dialect == cxx98
10572       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10573     {
10574       maybe_type_id = true;
10575       cp_parser_abort_tentative_parse (parser);
10576     }
10577   else
10578     {
10579       /* If the next token isn't a `,' or a `>', then this argument wasn't
10580       really finished. This means that the argument is not a valid
10581       type-id.  */
10582       if (!cp_parser_next_token_ends_template_argument_p (parser))
10583         cp_parser_error (parser, "expected template-argument");
10584       /* If that worked, we're done.  */
10585       if (cp_parser_parse_definitely (parser))
10586         return argument;
10587     }
10588   /* We're still not sure what the argument will be.  */
10589   cp_parser_parse_tentatively (parser);
10590   /* Try a template.  */
10591   argument_start_token = cp_lexer_peek_token (parser->lexer);
10592   argument = cp_parser_id_expression (parser,
10593                                       /*template_keyword_p=*/false,
10594                                       /*check_dependency_p=*/true,
10595                                       &template_p,
10596                                       /*declarator_p=*/false,
10597                                       /*optional_p=*/false);
10598   /* If the next token isn't a `,' or a `>', then this argument wasn't
10599      really finished.  */
10600   if (!cp_parser_next_token_ends_template_argument_p (parser))
10601     cp_parser_error (parser, "expected template-argument");
10602   if (!cp_parser_error_occurred (parser))
10603     {
10604       /* Figure out what is being referred to.  If the id-expression
10605          was for a class template specialization, then we will have a
10606          TYPE_DECL at this point.  There is no need to do name lookup
10607          at this point in that case.  */
10608       if (TREE_CODE (argument) != TYPE_DECL)
10609         argument = cp_parser_lookup_name (parser, argument,
10610                                           none_type,
10611                                           /*is_template=*/template_p,
10612                                           /*is_namespace=*/false,
10613                                           /*check_dependency=*/true,
10614                                           /*ambiguous_decls=*/NULL,
10615                                           argument_start_token->location);
10616       if (TREE_CODE (argument) != TEMPLATE_DECL
10617           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10618         cp_parser_error (parser, "expected template-name");
10619     }
10620   if (cp_parser_parse_definitely (parser))
10621     return argument;
10622   /* It must be a non-type argument.  There permitted cases are given
10623      in [temp.arg.nontype]:
10624
10625      -- an integral constant-expression of integral or enumeration
10626         type; or
10627
10628      -- the name of a non-type template-parameter; or
10629
10630      -- the name of an object or function with external linkage...
10631
10632      -- the address of an object or function with external linkage...
10633
10634      -- a pointer to member...  */
10635   /* Look for a non-type template parameter.  */
10636   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10637     {
10638       cp_parser_parse_tentatively (parser);
10639       argument = cp_parser_primary_expression (parser,
10640                                                /*address_p=*/false,
10641                                                /*cast_p=*/false,
10642                                                /*template_arg_p=*/true,
10643                                                &idk);
10644       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10645           || !cp_parser_next_token_ends_template_argument_p (parser))
10646         cp_parser_simulate_error (parser);
10647       if (cp_parser_parse_definitely (parser))
10648         return argument;
10649     }
10650
10651   /* If the next token is "&", the argument must be the address of an
10652      object or function with external linkage.  */
10653   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10654   if (address_p)
10655     cp_lexer_consume_token (parser->lexer);
10656   /* See if we might have an id-expression.  */
10657   token = cp_lexer_peek_token (parser->lexer);
10658   if (token->type == CPP_NAME
10659       || token->keyword == RID_OPERATOR
10660       || token->type == CPP_SCOPE
10661       || token->type == CPP_TEMPLATE_ID
10662       || token->type == CPP_NESTED_NAME_SPECIFIER)
10663     {
10664       cp_parser_parse_tentatively (parser);
10665       argument = cp_parser_primary_expression (parser,
10666                                                address_p,
10667                                                /*cast_p=*/false,
10668                                                /*template_arg_p=*/true,
10669                                                &idk);
10670       if (cp_parser_error_occurred (parser)
10671           || !cp_parser_next_token_ends_template_argument_p (parser))
10672         cp_parser_abort_tentative_parse (parser);
10673       else
10674         {
10675           if (TREE_CODE (argument) == INDIRECT_REF)
10676             {
10677               gcc_assert (REFERENCE_REF_P (argument));
10678               argument = TREE_OPERAND (argument, 0);
10679             }
10680
10681           if (TREE_CODE (argument) == VAR_DECL)
10682             {
10683               /* A variable without external linkage might still be a
10684                  valid constant-expression, so no error is issued here
10685                  if the external-linkage check fails.  */
10686               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10687                 cp_parser_simulate_error (parser);
10688             }
10689           else if (is_overloaded_fn (argument))
10690             /* All overloaded functions are allowed; if the external
10691                linkage test does not pass, an error will be issued
10692                later.  */
10693             ;
10694           else if (address_p
10695                    && (TREE_CODE (argument) == OFFSET_REF
10696                        || TREE_CODE (argument) == SCOPE_REF))
10697             /* A pointer-to-member.  */
10698             ;
10699           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10700             ;
10701           else
10702             cp_parser_simulate_error (parser);
10703
10704           if (cp_parser_parse_definitely (parser))
10705             {
10706               if (address_p)
10707                 argument = build_x_unary_op (ADDR_EXPR, argument,
10708                                              tf_warning_or_error);
10709               return argument;
10710             }
10711         }
10712     }
10713   /* If the argument started with "&", there are no other valid
10714      alternatives at this point.  */
10715   if (address_p)
10716     {
10717       cp_parser_error (parser, "invalid non-type template argument");
10718       return error_mark_node;
10719     }
10720
10721   /* If the argument wasn't successfully parsed as a type-id followed
10722      by '>>', the argument can only be a constant expression now.
10723      Otherwise, we try parsing the constant-expression tentatively,
10724      because the argument could really be a type-id.  */
10725   if (maybe_type_id)
10726     cp_parser_parse_tentatively (parser);
10727   argument = cp_parser_constant_expression (parser,
10728                                             /*allow_non_constant_p=*/false,
10729                                             /*non_constant_p=*/NULL);
10730   argument = fold_non_dependent_expr (argument);
10731   if (!maybe_type_id)
10732     return argument;
10733   if (!cp_parser_next_token_ends_template_argument_p (parser))
10734     cp_parser_error (parser, "expected template-argument");
10735   if (cp_parser_parse_definitely (parser))
10736     return argument;
10737   /* We did our best to parse the argument as a non type-id, but that
10738      was the only alternative that matched (albeit with a '>' after
10739      it). We can assume it's just a typo from the user, and a
10740      diagnostic will then be issued.  */
10741   return cp_parser_template_type_arg (parser);
10742 }
10743
10744 /* Parse an explicit-instantiation.
10745
10746    explicit-instantiation:
10747      template declaration
10748
10749    Although the standard says `declaration', what it really means is:
10750
10751    explicit-instantiation:
10752      template decl-specifier-seq [opt] declarator [opt] ;
10753
10754    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10755    supposed to be allowed.  A defect report has been filed about this
10756    issue.
10757
10758    GNU Extension:
10759
10760    explicit-instantiation:
10761      storage-class-specifier template
10762        decl-specifier-seq [opt] declarator [opt] ;
10763      function-specifier template
10764        decl-specifier-seq [opt] declarator [opt] ;  */
10765
10766 static void
10767 cp_parser_explicit_instantiation (cp_parser* parser)
10768 {
10769   int declares_class_or_enum;
10770   cp_decl_specifier_seq decl_specifiers;
10771   tree extension_specifier = NULL_TREE;
10772   cp_token *token;
10773
10774   /* Look for an (optional) storage-class-specifier or
10775      function-specifier.  */
10776   if (cp_parser_allow_gnu_extensions_p (parser))
10777     {
10778       extension_specifier
10779         = cp_parser_storage_class_specifier_opt (parser);
10780       if (!extension_specifier)
10781         extension_specifier
10782           = cp_parser_function_specifier_opt (parser,
10783                                               /*decl_specs=*/NULL);
10784     }
10785
10786   /* Look for the `template' keyword.  */
10787   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10788   /* Let the front end know that we are processing an explicit
10789      instantiation.  */
10790   begin_explicit_instantiation ();
10791   /* [temp.explicit] says that we are supposed to ignore access
10792      control while processing explicit instantiation directives.  */
10793   push_deferring_access_checks (dk_no_check);
10794   /* Parse a decl-specifier-seq.  */
10795   token = cp_lexer_peek_token (parser->lexer);
10796   cp_parser_decl_specifier_seq (parser,
10797                                 CP_PARSER_FLAGS_OPTIONAL,
10798                                 &decl_specifiers,
10799                                 &declares_class_or_enum);
10800   /* If there was exactly one decl-specifier, and it declared a class,
10801      and there's no declarator, then we have an explicit type
10802      instantiation.  */
10803   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10804     {
10805       tree type;
10806
10807       type = check_tag_decl (&decl_specifiers);
10808       /* Turn access control back on for names used during
10809          template instantiation.  */
10810       pop_deferring_access_checks ();
10811       if (type)
10812         do_type_instantiation (type, extension_specifier,
10813                                /*complain=*/tf_error);
10814     }
10815   else
10816     {
10817       cp_declarator *declarator;
10818       tree decl;
10819
10820       /* Parse the declarator.  */
10821       declarator
10822         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10823                                 /*ctor_dtor_or_conv_p=*/NULL,
10824                                 /*parenthesized_p=*/NULL,
10825                                 /*member_p=*/false);
10826       if (declares_class_or_enum & 2)
10827         cp_parser_check_for_definition_in_return_type (declarator,
10828                                                        decl_specifiers.type,
10829                                                        decl_specifiers.type_location);
10830       if (declarator != cp_error_declarator)
10831         {
10832           decl = grokdeclarator (declarator, &decl_specifiers,
10833                                  NORMAL, 0, &decl_specifiers.attributes);
10834           /* Turn access control back on for names used during
10835              template instantiation.  */
10836           pop_deferring_access_checks ();
10837           /* Do the explicit instantiation.  */
10838           do_decl_instantiation (decl, extension_specifier);
10839         }
10840       else
10841         {
10842           pop_deferring_access_checks ();
10843           /* Skip the body of the explicit instantiation.  */
10844           cp_parser_skip_to_end_of_statement (parser);
10845         }
10846     }
10847   /* We're done with the instantiation.  */
10848   end_explicit_instantiation ();
10849
10850   cp_parser_consume_semicolon_at_end_of_statement (parser);
10851 }
10852
10853 /* Parse an explicit-specialization.
10854
10855    explicit-specialization:
10856      template < > declaration
10857
10858    Although the standard says `declaration', what it really means is:
10859
10860    explicit-specialization:
10861      template <> decl-specifier [opt] init-declarator [opt] ;
10862      template <> function-definition
10863      template <> explicit-specialization
10864      template <> template-declaration  */
10865
10866 static void
10867 cp_parser_explicit_specialization (cp_parser* parser)
10868 {
10869   bool need_lang_pop;
10870   cp_token *token = cp_lexer_peek_token (parser->lexer);
10871
10872   /* Look for the `template' keyword.  */
10873   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10874   /* Look for the `<'.  */
10875   cp_parser_require (parser, CPP_LESS, "%<<%>");
10876   /* Look for the `>'.  */
10877   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10878   /* We have processed another parameter list.  */
10879   ++parser->num_template_parameter_lists;
10880   /* [temp]
10881
10882      A template ... explicit specialization ... shall not have C
10883      linkage.  */
10884   if (current_lang_name == lang_name_c)
10885     {
10886       error ("%Htemplate specialization with C linkage", &token->location);
10887       /* Give it C++ linkage to avoid confusing other parts of the
10888          front end.  */
10889       push_lang_context (lang_name_cplusplus);
10890       need_lang_pop = true;
10891     }
10892   else
10893     need_lang_pop = false;
10894   /* Let the front end know that we are beginning a specialization.  */
10895   if (!begin_specialization ())
10896     {
10897       end_specialization ();
10898       return;
10899     }
10900
10901   /* If the next keyword is `template', we need to figure out whether
10902      or not we're looking a template-declaration.  */
10903   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10904     {
10905       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10906           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10907         cp_parser_template_declaration_after_export (parser,
10908                                                      /*member_p=*/false);
10909       else
10910         cp_parser_explicit_specialization (parser);
10911     }
10912   else
10913     /* Parse the dependent declaration.  */
10914     cp_parser_single_declaration (parser,
10915                                   /*checks=*/NULL,
10916                                   /*member_p=*/false,
10917                                   /*explicit_specialization_p=*/true,
10918                                   /*friend_p=*/NULL);
10919   /* We're done with the specialization.  */
10920   end_specialization ();
10921   /* For the erroneous case of a template with C linkage, we pushed an
10922      implicit C++ linkage scope; exit that scope now.  */
10923   if (need_lang_pop)
10924     pop_lang_context ();
10925   /* We're done with this parameter list.  */
10926   --parser->num_template_parameter_lists;
10927 }
10928
10929 /* Parse a type-specifier.
10930
10931    type-specifier:
10932      simple-type-specifier
10933      class-specifier
10934      enum-specifier
10935      elaborated-type-specifier
10936      cv-qualifier
10937
10938    GNU Extension:
10939
10940    type-specifier:
10941      __complex__
10942
10943    Returns a representation of the type-specifier.  For a
10944    class-specifier, enum-specifier, or elaborated-type-specifier, a
10945    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10946
10947    The parser flags FLAGS is used to control type-specifier parsing.
10948
10949    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10950    in a decl-specifier-seq.
10951
10952    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10953    class-specifier, enum-specifier, or elaborated-type-specifier, then
10954    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10955    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10956    zero.
10957
10958    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10959    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10960    is set to FALSE.  */
10961
10962 static tree
10963 cp_parser_type_specifier (cp_parser* parser,
10964                           cp_parser_flags flags,
10965                           cp_decl_specifier_seq *decl_specs,
10966                           bool is_declaration,
10967                           int* declares_class_or_enum,
10968                           bool* is_cv_qualifier)
10969 {
10970   tree type_spec = NULL_TREE;
10971   cp_token *token;
10972   enum rid keyword;
10973   cp_decl_spec ds = ds_last;
10974
10975   /* Assume this type-specifier does not declare a new type.  */
10976   if (declares_class_or_enum)
10977     *declares_class_or_enum = 0;
10978   /* And that it does not specify a cv-qualifier.  */
10979   if (is_cv_qualifier)
10980     *is_cv_qualifier = false;
10981   /* Peek at the next token.  */
10982   token = cp_lexer_peek_token (parser->lexer);
10983
10984   /* If we're looking at a keyword, we can use that to guide the
10985      production we choose.  */
10986   keyword = token->keyword;
10987   switch (keyword)
10988     {
10989     case RID_ENUM:
10990       /* Look for the enum-specifier.  */
10991       type_spec = cp_parser_enum_specifier (parser);
10992       /* If that worked, we're done.  */
10993       if (type_spec)
10994         {
10995           if (declares_class_or_enum)
10996             *declares_class_or_enum = 2;
10997           if (decl_specs)
10998             cp_parser_set_decl_spec_type (decl_specs,
10999                                           type_spec,
11000                                           token->location,
11001                                           /*user_defined_p=*/true);
11002           return type_spec;
11003         }
11004       else
11005         goto elaborated_type_specifier;
11006
11007       /* Any of these indicate either a class-specifier, or an
11008          elaborated-type-specifier.  */
11009     case RID_CLASS:
11010     case RID_STRUCT:
11011     case RID_UNION:
11012       /* Parse tentatively so that we can back up if we don't find a
11013          class-specifier.  */
11014       cp_parser_parse_tentatively (parser);
11015       /* Look for the class-specifier.  */
11016       type_spec = cp_parser_class_specifier (parser);
11017       /* If that worked, we're done.  */
11018       if (cp_parser_parse_definitely (parser))
11019         {
11020           if (declares_class_or_enum)
11021             *declares_class_or_enum = 2;
11022           if (decl_specs)
11023             cp_parser_set_decl_spec_type (decl_specs,
11024                                           type_spec,
11025                                           token->location,
11026                                           /*user_defined_p=*/true);
11027           return type_spec;
11028         }
11029
11030       /* Fall through.  */
11031     elaborated_type_specifier:
11032       /* We're declaring (not defining) a class or enum.  */
11033       if (declares_class_or_enum)
11034         *declares_class_or_enum = 1;
11035
11036       /* Fall through.  */
11037     case RID_TYPENAME:
11038       /* Look for an elaborated-type-specifier.  */
11039       type_spec
11040         = (cp_parser_elaborated_type_specifier
11041            (parser,
11042             decl_specs && decl_specs->specs[(int) ds_friend],
11043             is_declaration));
11044       if (decl_specs)
11045         cp_parser_set_decl_spec_type (decl_specs,
11046                                       type_spec,
11047                                       token->location,
11048                                       /*user_defined_p=*/true);
11049       return type_spec;
11050
11051     case RID_CONST:
11052       ds = ds_const;
11053       if (is_cv_qualifier)
11054         *is_cv_qualifier = true;
11055       break;
11056
11057     case RID_VOLATILE:
11058       ds = ds_volatile;
11059       if (is_cv_qualifier)
11060         *is_cv_qualifier = true;
11061       break;
11062
11063     case RID_RESTRICT:
11064       ds = ds_restrict;
11065       if (is_cv_qualifier)
11066         *is_cv_qualifier = true;
11067       break;
11068
11069     case RID_COMPLEX:
11070       /* The `__complex__' keyword is a GNU extension.  */
11071       ds = ds_complex;
11072       break;
11073
11074     default:
11075       break;
11076     }
11077
11078   /* Handle simple keywords.  */
11079   if (ds != ds_last)
11080     {
11081       if (decl_specs)
11082         {
11083           ++decl_specs->specs[(int)ds];
11084           decl_specs->any_specifiers_p = true;
11085         }
11086       return cp_lexer_consume_token (parser->lexer)->u.value;
11087     }
11088
11089   /* If we do not already have a type-specifier, assume we are looking
11090      at a simple-type-specifier.  */
11091   type_spec = cp_parser_simple_type_specifier (parser,
11092                                                decl_specs,
11093                                                flags);
11094
11095   /* If we didn't find a type-specifier, and a type-specifier was not
11096      optional in this context, issue an error message.  */
11097   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11098     {
11099       cp_parser_error (parser, "expected type specifier");
11100       return error_mark_node;
11101     }
11102
11103   return type_spec;
11104 }
11105
11106 /* Parse a simple-type-specifier.
11107
11108    simple-type-specifier:
11109      :: [opt] nested-name-specifier [opt] type-name
11110      :: [opt] nested-name-specifier template template-id
11111      char
11112      wchar_t
11113      bool
11114      short
11115      int
11116      long
11117      signed
11118      unsigned
11119      float
11120      double
11121      void
11122
11123    C++0x Extension:
11124
11125    simple-type-specifier:
11126      auto
11127      decltype ( expression )   
11128      char16_t
11129      char32_t
11130
11131    GNU Extension:
11132
11133    simple-type-specifier:
11134      __typeof__ unary-expression
11135      __typeof__ ( type-id )
11136
11137    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11138    appropriately updated.  */
11139
11140 static tree
11141 cp_parser_simple_type_specifier (cp_parser* parser,
11142                                  cp_decl_specifier_seq *decl_specs,
11143                                  cp_parser_flags flags)
11144 {
11145   tree type = NULL_TREE;
11146   cp_token *token;
11147
11148   /* Peek at the next token.  */
11149   token = cp_lexer_peek_token (parser->lexer);
11150
11151   /* If we're looking at a keyword, things are easy.  */
11152   switch (token->keyword)
11153     {
11154     case RID_CHAR:
11155       if (decl_specs)
11156         decl_specs->explicit_char_p = true;
11157       type = char_type_node;
11158       break;
11159     case RID_CHAR16:
11160       type = char16_type_node;
11161       break;
11162     case RID_CHAR32:
11163       type = char32_type_node;
11164       break;
11165     case RID_WCHAR:
11166       type = wchar_type_node;
11167       break;
11168     case RID_BOOL:
11169       type = boolean_type_node;
11170       break;
11171     case RID_SHORT:
11172       if (decl_specs)
11173         ++decl_specs->specs[(int) ds_short];
11174       type = short_integer_type_node;
11175       break;
11176     case RID_INT:
11177       if (decl_specs)
11178         decl_specs->explicit_int_p = true;
11179       type = integer_type_node;
11180       break;
11181     case RID_LONG:
11182       if (decl_specs)
11183         ++decl_specs->specs[(int) ds_long];
11184       type = long_integer_type_node;
11185       break;
11186     case RID_SIGNED:
11187       if (decl_specs)
11188         ++decl_specs->specs[(int) ds_signed];
11189       type = integer_type_node;
11190       break;
11191     case RID_UNSIGNED:
11192       if (decl_specs)
11193         ++decl_specs->specs[(int) ds_unsigned];
11194       type = unsigned_type_node;
11195       break;
11196     case RID_FLOAT:
11197       type = float_type_node;
11198       break;
11199     case RID_DOUBLE:
11200       type = double_type_node;
11201       break;
11202     case RID_VOID:
11203       type = void_type_node;
11204       break;
11205       
11206     case RID_AUTO:
11207       maybe_warn_cpp0x ("C++0x auto");
11208       type = make_auto ();
11209       break;
11210
11211     case RID_DECLTYPE:
11212       /* Parse the `decltype' type.  */
11213       type = cp_parser_decltype (parser);
11214
11215       if (decl_specs)
11216         cp_parser_set_decl_spec_type (decl_specs, type,
11217                                       token->location,
11218                                       /*user_defined_p=*/true);
11219
11220       return type;
11221
11222     case RID_TYPEOF:
11223       /* Consume the `typeof' token.  */
11224       cp_lexer_consume_token (parser->lexer);
11225       /* Parse the operand to `typeof'.  */
11226       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11227       /* If it is not already a TYPE, take its type.  */
11228       if (!TYPE_P (type))
11229         type = finish_typeof (type);
11230
11231       if (decl_specs)
11232         cp_parser_set_decl_spec_type (decl_specs, type,
11233                                       token->location,
11234                                       /*user_defined_p=*/true);
11235
11236       return type;
11237
11238     default:
11239       break;
11240     }
11241
11242   /* If the type-specifier was for a built-in type, we're done.  */
11243   if (type)
11244     {
11245       tree id;
11246
11247       /* Record the type.  */
11248       if (decl_specs
11249           && (token->keyword != RID_SIGNED
11250               && token->keyword != RID_UNSIGNED
11251               && token->keyword != RID_SHORT
11252               && token->keyword != RID_LONG))
11253         cp_parser_set_decl_spec_type (decl_specs,
11254                                       type,
11255                                       token->location,
11256                                       /*user_defined=*/false);
11257       if (decl_specs)
11258         decl_specs->any_specifiers_p = true;
11259
11260       /* Consume the token.  */
11261       id = cp_lexer_consume_token (parser->lexer)->u.value;
11262
11263       /* There is no valid C++ program where a non-template type is
11264          followed by a "<".  That usually indicates that the user thought
11265          that the type was a template.  */
11266       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11267
11268       return TYPE_NAME (type);
11269     }
11270
11271   /* The type-specifier must be a user-defined type.  */
11272   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11273     {
11274       bool qualified_p;
11275       bool global_p;
11276
11277       /* Don't gobble tokens or issue error messages if this is an
11278          optional type-specifier.  */
11279       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11280         cp_parser_parse_tentatively (parser);
11281
11282       /* Look for the optional `::' operator.  */
11283       global_p
11284         = (cp_parser_global_scope_opt (parser,
11285                                        /*current_scope_valid_p=*/false)
11286            != NULL_TREE);
11287       /* Look for the nested-name specifier.  */
11288       qualified_p
11289         = (cp_parser_nested_name_specifier_opt (parser,
11290                                                 /*typename_keyword_p=*/false,
11291                                                 /*check_dependency_p=*/true,
11292                                                 /*type_p=*/false,
11293                                                 /*is_declaration=*/false)
11294            != NULL_TREE);
11295       token = cp_lexer_peek_token (parser->lexer);
11296       /* If we have seen a nested-name-specifier, and the next token
11297          is `template', then we are using the template-id production.  */
11298       if (parser->scope
11299           && cp_parser_optional_template_keyword (parser))
11300         {
11301           /* Look for the template-id.  */
11302           type = cp_parser_template_id (parser,
11303                                         /*template_keyword_p=*/true,
11304                                         /*check_dependency_p=*/true,
11305                                         /*is_declaration=*/false);
11306           /* If the template-id did not name a type, we are out of
11307              luck.  */
11308           if (TREE_CODE (type) != TYPE_DECL)
11309             {
11310               cp_parser_error (parser, "expected template-id for type");
11311               type = NULL_TREE;
11312             }
11313         }
11314       /* Otherwise, look for a type-name.  */
11315       else
11316         type = cp_parser_type_name (parser);
11317       /* Keep track of all name-lookups performed in class scopes.  */
11318       if (type
11319           && !global_p
11320           && !qualified_p
11321           && TREE_CODE (type) == TYPE_DECL
11322           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11323         maybe_note_name_used_in_class (DECL_NAME (type), type);
11324       /* If it didn't work out, we don't have a TYPE.  */
11325       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11326           && !cp_parser_parse_definitely (parser))
11327         type = NULL_TREE;
11328       if (type && decl_specs)
11329         cp_parser_set_decl_spec_type (decl_specs, type,
11330                                       token->location,
11331                                       /*user_defined=*/true);
11332     }
11333
11334   /* If we didn't get a type-name, issue an error message.  */
11335   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11336     {
11337       cp_parser_error (parser, "expected type-name");
11338       return error_mark_node;
11339     }
11340
11341   /* There is no valid C++ program where a non-template type is
11342      followed by a "<".  That usually indicates that the user thought
11343      that the type was a template.  */
11344   if (type && type != error_mark_node)
11345     {
11346       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11347          If it is, then the '<'...'>' enclose protocol names rather than
11348          template arguments, and so everything is fine.  */
11349       if (c_dialect_objc ()
11350           && (objc_is_id (type) || objc_is_class_name (type)))
11351         {
11352           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11353           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11354
11355           /* Clobber the "unqualified" type previously entered into
11356              DECL_SPECS with the new, improved protocol-qualified version.  */
11357           if (decl_specs)
11358             decl_specs->type = qual_type;
11359
11360           return qual_type;
11361         }
11362
11363       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11364                                                token->location);
11365     }
11366
11367   return type;
11368 }
11369
11370 /* Parse a type-name.
11371
11372    type-name:
11373      class-name
11374      enum-name
11375      typedef-name
11376
11377    enum-name:
11378      identifier
11379
11380    typedef-name:
11381      identifier
11382
11383    Returns a TYPE_DECL for the type.  */
11384
11385 static tree
11386 cp_parser_type_name (cp_parser* parser)
11387 {
11388   tree type_decl;
11389
11390   /* We can't know yet whether it is a class-name or not.  */
11391   cp_parser_parse_tentatively (parser);
11392   /* Try a class-name.  */
11393   type_decl = cp_parser_class_name (parser,
11394                                     /*typename_keyword_p=*/false,
11395                                     /*template_keyword_p=*/false,
11396                                     none_type,
11397                                     /*check_dependency_p=*/true,
11398                                     /*class_head_p=*/false,
11399                                     /*is_declaration=*/false);
11400   /* If it's not a class-name, keep looking.  */
11401   if (!cp_parser_parse_definitely (parser))
11402     {
11403       /* It must be a typedef-name or an enum-name.  */
11404       return cp_parser_nonclass_name (parser);
11405     }
11406
11407   return type_decl;
11408 }
11409
11410 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11411
11412    enum-name:
11413      identifier
11414
11415    typedef-name:
11416      identifier
11417
11418    Returns a TYPE_DECL for the type.  */
11419
11420 static tree
11421 cp_parser_nonclass_name (cp_parser* parser)
11422 {
11423   tree type_decl;
11424   tree identifier;
11425
11426   cp_token *token = cp_lexer_peek_token (parser->lexer);
11427   identifier = cp_parser_identifier (parser);
11428   if (identifier == error_mark_node)
11429     return error_mark_node;
11430
11431   /* Look up the type-name.  */
11432   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11433
11434   if (TREE_CODE (type_decl) != TYPE_DECL
11435       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11436     {
11437       /* See if this is an Objective-C type.  */
11438       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11439       tree type = objc_get_protocol_qualified_type (identifier, protos);
11440       if (type)
11441         type_decl = TYPE_NAME (type);
11442     }
11443   
11444   /* Issue an error if we did not find a type-name.  */
11445   if (TREE_CODE (type_decl) != TYPE_DECL)
11446     {
11447       if (!cp_parser_simulate_error (parser))
11448         cp_parser_name_lookup_error (parser, identifier, type_decl,
11449                                      "is not a type", token->location);
11450       return error_mark_node;
11451     }
11452   /* Remember that the name was used in the definition of the
11453      current class so that we can check later to see if the
11454      meaning would have been different after the class was
11455      entirely defined.  */
11456   else if (type_decl != error_mark_node
11457            && !parser->scope)
11458     maybe_note_name_used_in_class (identifier, type_decl);
11459   
11460   return type_decl;
11461 }
11462
11463 /* Parse an elaborated-type-specifier.  Note that the grammar given
11464    here incorporates the resolution to DR68.
11465
11466    elaborated-type-specifier:
11467      class-key :: [opt] nested-name-specifier [opt] identifier
11468      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11469      enum-key :: [opt] nested-name-specifier [opt] identifier
11470      typename :: [opt] nested-name-specifier identifier
11471      typename :: [opt] nested-name-specifier template [opt]
11472        template-id
11473
11474    GNU extension:
11475
11476    elaborated-type-specifier:
11477      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11478      class-key attributes :: [opt] nested-name-specifier [opt]
11479                template [opt] template-id
11480      enum attributes :: [opt] nested-name-specifier [opt] identifier
11481
11482    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11483    declared `friend'.  If IS_DECLARATION is TRUE, then this
11484    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11485    something is being declared.
11486
11487    Returns the TYPE specified.  */
11488
11489 static tree
11490 cp_parser_elaborated_type_specifier (cp_parser* parser,
11491                                      bool is_friend,
11492                                      bool is_declaration)
11493 {
11494   enum tag_types tag_type;
11495   tree identifier;
11496   tree type = NULL_TREE;
11497   tree attributes = NULL_TREE;
11498   cp_token *token = NULL;
11499
11500   /* See if we're looking at the `enum' keyword.  */
11501   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11502     {
11503       /* Consume the `enum' token.  */
11504       cp_lexer_consume_token (parser->lexer);
11505       /* Remember that it's an enumeration type.  */
11506       tag_type = enum_type;
11507       /* Parse the optional `struct' or `class' key (for C++0x scoped
11508          enums).  */
11509       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11510           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11511         {
11512           if (cxx_dialect == cxx98)
11513             maybe_warn_cpp0x ("scoped enums");
11514
11515           /* Consume the `struct' or `class'.  */
11516           cp_lexer_consume_token (parser->lexer);
11517         }
11518       /* Parse the attributes.  */
11519       attributes = cp_parser_attributes_opt (parser);
11520     }
11521   /* Or, it might be `typename'.  */
11522   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11523                                            RID_TYPENAME))
11524     {
11525       /* Consume the `typename' token.  */
11526       cp_lexer_consume_token (parser->lexer);
11527       /* Remember that it's a `typename' type.  */
11528       tag_type = typename_type;
11529       /* The `typename' keyword is only allowed in templates.  */
11530       if (!processing_template_decl)
11531         permerror (input_location, "using %<typename%> outside of template");
11532     }
11533   /* Otherwise it must be a class-key.  */
11534   else
11535     {
11536       tag_type = cp_parser_class_key (parser);
11537       if (tag_type == none_type)
11538         return error_mark_node;
11539       /* Parse the attributes.  */
11540       attributes = cp_parser_attributes_opt (parser);
11541     }
11542
11543   /* Look for the `::' operator.  */
11544   cp_parser_global_scope_opt (parser,
11545                               /*current_scope_valid_p=*/false);
11546   /* Look for the nested-name-specifier.  */
11547   if (tag_type == typename_type)
11548     {
11549       if (!cp_parser_nested_name_specifier (parser,
11550                                            /*typename_keyword_p=*/true,
11551                                            /*check_dependency_p=*/true,
11552                                            /*type_p=*/true,
11553                                             is_declaration))
11554         return error_mark_node;
11555     }
11556   else
11557     /* Even though `typename' is not present, the proposed resolution
11558        to Core Issue 180 says that in `class A<T>::B', `B' should be
11559        considered a type-name, even if `A<T>' is dependent.  */
11560     cp_parser_nested_name_specifier_opt (parser,
11561                                          /*typename_keyword_p=*/true,
11562                                          /*check_dependency_p=*/true,
11563                                          /*type_p=*/true,
11564                                          is_declaration);
11565  /* For everything but enumeration types, consider a template-id.
11566     For an enumeration type, consider only a plain identifier.  */
11567   if (tag_type != enum_type)
11568     {
11569       bool template_p = false;
11570       tree decl;
11571
11572       /* Allow the `template' keyword.  */
11573       template_p = cp_parser_optional_template_keyword (parser);
11574       /* If we didn't see `template', we don't know if there's a
11575          template-id or not.  */
11576       if (!template_p)
11577         cp_parser_parse_tentatively (parser);
11578       /* Parse the template-id.  */
11579       token = cp_lexer_peek_token (parser->lexer);
11580       decl = cp_parser_template_id (parser, template_p,
11581                                     /*check_dependency_p=*/true,
11582                                     is_declaration);
11583       /* If we didn't find a template-id, look for an ordinary
11584          identifier.  */
11585       if (!template_p && !cp_parser_parse_definitely (parser))
11586         ;
11587       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11588          in effect, then we must assume that, upon instantiation, the
11589          template will correspond to a class.  */
11590       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11591                && tag_type == typename_type)
11592         type = make_typename_type (parser->scope, decl,
11593                                    typename_type,
11594                                    /*complain=*/tf_error);
11595       /* If the `typename' keyword is in effect and DECL is not a type
11596          decl. Then type is non existant.   */
11597       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11598         type = NULL_TREE; 
11599       else 
11600         type = TREE_TYPE (decl);
11601     }
11602
11603   if (!type)
11604     {
11605       token = cp_lexer_peek_token (parser->lexer);
11606       identifier = cp_parser_identifier (parser);
11607
11608       if (identifier == error_mark_node)
11609         {
11610           parser->scope = NULL_TREE;
11611           return error_mark_node;
11612         }
11613
11614       /* For a `typename', we needn't call xref_tag.  */
11615       if (tag_type == typename_type
11616           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11617         return cp_parser_make_typename_type (parser, parser->scope,
11618                                              identifier,
11619                                              token->location);
11620       /* Look up a qualified name in the usual way.  */
11621       if (parser->scope)
11622         {
11623           tree decl;
11624           tree ambiguous_decls;
11625
11626           decl = cp_parser_lookup_name (parser, identifier,
11627                                         tag_type,
11628                                         /*is_template=*/false,
11629                                         /*is_namespace=*/false,
11630                                         /*check_dependency=*/true,
11631                                         &ambiguous_decls,
11632                                         token->location);
11633
11634           /* If the lookup was ambiguous, an error will already have been
11635              issued.  */
11636           if (ambiguous_decls)
11637             return error_mark_node;
11638
11639           /* If we are parsing friend declaration, DECL may be a
11640              TEMPLATE_DECL tree node here.  However, we need to check
11641              whether this TEMPLATE_DECL results in valid code.  Consider
11642              the following example:
11643
11644                namespace N {
11645                  template <class T> class C {};
11646                }
11647                class X {
11648                  template <class T> friend class N::C; // #1, valid code
11649                };
11650                template <class T> class Y {
11651                  friend class N::C;                    // #2, invalid code
11652                };
11653
11654              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11655              name lookup of `N::C'.  We see that friend declaration must
11656              be template for the code to be valid.  Note that
11657              processing_template_decl does not work here since it is
11658              always 1 for the above two cases.  */
11659
11660           decl = (cp_parser_maybe_treat_template_as_class
11661                   (decl, /*tag_name_p=*/is_friend
11662                          && parser->num_template_parameter_lists));
11663
11664           if (TREE_CODE (decl) != TYPE_DECL)
11665             {
11666               cp_parser_diagnose_invalid_type_name (parser,
11667                                                     parser->scope,
11668                                                     identifier,
11669                                                     token->location);
11670               return error_mark_node;
11671             }
11672
11673           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11674             {
11675               bool allow_template = (parser->num_template_parameter_lists
11676                                       || DECL_SELF_REFERENCE_P (decl));
11677               type = check_elaborated_type_specifier (tag_type, decl, 
11678                                                       allow_template);
11679
11680               if (type == error_mark_node)
11681                 return error_mark_node;
11682             }
11683
11684           /* Forward declarations of nested types, such as
11685
11686                class C1::C2;
11687                class C1::C2::C3;
11688
11689              are invalid unless all components preceding the final '::'
11690              are complete.  If all enclosing types are complete, these
11691              declarations become merely pointless.
11692
11693              Invalid forward declarations of nested types are errors
11694              caught elsewhere in parsing.  Those that are pointless arrive
11695              here.  */
11696
11697           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11698               && !is_friend && !processing_explicit_instantiation)
11699             warning (0, "declaration %qD does not declare anything", decl);
11700
11701           type = TREE_TYPE (decl);
11702         }
11703       else
11704         {
11705           /* An elaborated-type-specifier sometimes introduces a new type and
11706              sometimes names an existing type.  Normally, the rule is that it
11707              introduces a new type only if there is not an existing type of
11708              the same name already in scope.  For example, given:
11709
11710                struct S {};
11711                void f() { struct S s; }
11712
11713              the `struct S' in the body of `f' is the same `struct S' as in
11714              the global scope; the existing definition is used.  However, if
11715              there were no global declaration, this would introduce a new
11716              local class named `S'.
11717
11718              An exception to this rule applies to the following code:
11719
11720                namespace N { struct S; }
11721
11722              Here, the elaborated-type-specifier names a new type
11723              unconditionally; even if there is already an `S' in the
11724              containing scope this declaration names a new type.
11725              This exception only applies if the elaborated-type-specifier
11726              forms the complete declaration:
11727
11728                [class.name]
11729
11730                A declaration consisting solely of `class-key identifier ;' is
11731                either a redeclaration of the name in the current scope or a
11732                forward declaration of the identifier as a class name.  It
11733                introduces the name into the current scope.
11734
11735              We are in this situation precisely when the next token is a `;'.
11736
11737              An exception to the exception is that a `friend' declaration does
11738              *not* name a new type; i.e., given:
11739
11740                struct S { friend struct T; };
11741
11742              `T' is not a new type in the scope of `S'.
11743
11744              Also, `new struct S' or `sizeof (struct S)' never results in the
11745              definition of a new type; a new type can only be declared in a
11746              declaration context.  */
11747
11748           tag_scope ts;
11749           bool template_p;
11750
11751           if (is_friend)
11752             /* Friends have special name lookup rules.  */
11753             ts = ts_within_enclosing_non_class;
11754           else if (is_declaration
11755                    && cp_lexer_next_token_is (parser->lexer,
11756                                               CPP_SEMICOLON))
11757             /* This is a `class-key identifier ;' */
11758             ts = ts_current;
11759           else
11760             ts = ts_global;
11761
11762           template_p =
11763             (parser->num_template_parameter_lists
11764              && (cp_parser_next_token_starts_class_definition_p (parser)
11765                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11766           /* An unqualified name was used to reference this type, so
11767              there were no qualifying templates.  */
11768           if (!cp_parser_check_template_parameters (parser,
11769                                                     /*num_templates=*/0,
11770                                                     token->location,
11771                                                     /*declarator=*/NULL))
11772             return error_mark_node;
11773           type = xref_tag (tag_type, identifier, ts, template_p);
11774         }
11775     }
11776
11777   if (type == error_mark_node)
11778     return error_mark_node;
11779
11780   /* Allow attributes on forward declarations of classes.  */
11781   if (attributes)
11782     {
11783       if (TREE_CODE (type) == TYPENAME_TYPE)
11784         warning (OPT_Wattributes,
11785                  "attributes ignored on uninstantiated type");
11786       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11787                && ! processing_explicit_instantiation)
11788         warning (OPT_Wattributes,
11789                  "attributes ignored on template instantiation");
11790       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11791         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11792       else
11793         warning (OPT_Wattributes,
11794                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11795     }
11796
11797   if (tag_type != enum_type)
11798     cp_parser_check_class_key (tag_type, type);
11799
11800   /* A "<" cannot follow an elaborated type specifier.  If that
11801      happens, the user was probably trying to form a template-id.  */
11802   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11803
11804   return type;
11805 }
11806
11807 /* Parse an enum-specifier.
11808
11809    enum-specifier:
11810      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11811
11812    enum-key:
11813      enum
11814      enum class   [C++0x]
11815      enum struct  [C++0x]
11816
11817    enum-base:   [C++0x]
11818      : type-specifier-seq
11819
11820    GNU Extensions:
11821      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11822        { enumerator-list [opt] }attributes[opt]
11823
11824    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11825    if the token stream isn't an enum-specifier after all.  */
11826
11827 static tree
11828 cp_parser_enum_specifier (cp_parser* parser)
11829 {
11830   tree identifier;
11831   tree type;
11832   tree attributes;
11833   bool scoped_enum_p = false;
11834   bool has_underlying_type = false;
11835   tree underlying_type = NULL_TREE;
11836
11837   /* Parse tentatively so that we can back up if we don't find a
11838      enum-specifier.  */
11839   cp_parser_parse_tentatively (parser);
11840
11841   /* Caller guarantees that the current token is 'enum', an identifier
11842      possibly follows, and the token after that is an opening brace.
11843      If we don't have an identifier, fabricate an anonymous name for
11844      the enumeration being defined.  */
11845   cp_lexer_consume_token (parser->lexer);
11846
11847   /* Parse the "class" or "struct", which indicates a scoped
11848      enumeration type in C++0x.  */
11849   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11850       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11851     {
11852       if (cxx_dialect == cxx98)
11853         maybe_warn_cpp0x ("scoped enums");
11854
11855       /* Consume the `struct' or `class' token.  */
11856       cp_lexer_consume_token (parser->lexer);
11857
11858       scoped_enum_p = true;
11859     }
11860
11861   attributes = cp_parser_attributes_opt (parser);
11862
11863   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11864     identifier = cp_parser_identifier (parser);
11865   else
11866     identifier = make_anon_name ();
11867
11868   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11869   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11870     {
11871       cp_decl_specifier_seq type_specifiers;
11872
11873       /* At this point this is surely not elaborated type specifier.  */
11874       if (!cp_parser_parse_definitely (parser))
11875         return NULL_TREE;
11876
11877       if (cxx_dialect == cxx98)
11878         maybe_warn_cpp0x ("scoped enums");
11879
11880       /* Consume the `:'.  */
11881       cp_lexer_consume_token (parser->lexer);
11882
11883       has_underlying_type = true;
11884
11885       /* Parse the type-specifier-seq.  */
11886       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11887                                     &type_specifiers);
11888
11889       /* If that didn't work, stop.  */
11890       if (type_specifiers.type != error_mark_node)
11891         {
11892           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11893                                             /*initialized=*/0, NULL);
11894           if (underlying_type == error_mark_node)
11895             underlying_type = NULL_TREE;
11896         }
11897     }
11898
11899   /* Look for the `{' but don't consume it yet.  */
11900   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11901     {
11902       cp_parser_error (parser, "expected %<{%>");
11903       if (has_underlying_type)
11904         return NULL_TREE;
11905     }
11906
11907   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11908     return NULL_TREE;
11909
11910   /* Issue an error message if type-definitions are forbidden here.  */
11911   if (!cp_parser_check_type_definition (parser))
11912     type = error_mark_node;
11913   else
11914     /* Create the new type.  We do this before consuming the opening
11915        brace so the enum will be recorded as being on the line of its
11916        tag (or the 'enum' keyword, if there is no tag).  */
11917     type = start_enum (identifier, underlying_type, scoped_enum_p);
11918   
11919   /* Consume the opening brace.  */
11920   cp_lexer_consume_token (parser->lexer);
11921
11922   if (type == error_mark_node)
11923     {
11924       cp_parser_skip_to_end_of_block_or_statement (parser);
11925       return error_mark_node;
11926     }
11927
11928   /* If the next token is not '}', then there are some enumerators.  */
11929   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11930     cp_parser_enumerator_list (parser, type);
11931
11932   /* Consume the final '}'.  */
11933   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11934
11935   /* Look for trailing attributes to apply to this enumeration, and
11936      apply them if appropriate.  */
11937   if (cp_parser_allow_gnu_extensions_p (parser))
11938     {
11939       tree trailing_attr = cp_parser_attributes_opt (parser);
11940       trailing_attr = chainon (trailing_attr, attributes);
11941       cplus_decl_attributes (&type,
11942                              trailing_attr,
11943                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11944     }
11945
11946   /* Finish up the enumeration.  */
11947   finish_enum (type);
11948
11949   return type;
11950 }
11951
11952 /* Parse an enumerator-list.  The enumerators all have the indicated
11953    TYPE.
11954
11955    enumerator-list:
11956      enumerator-definition
11957      enumerator-list , enumerator-definition  */
11958
11959 static void
11960 cp_parser_enumerator_list (cp_parser* parser, tree type)
11961 {
11962   while (true)
11963     {
11964       /* Parse an enumerator-definition.  */
11965       cp_parser_enumerator_definition (parser, type);
11966
11967       /* If the next token is not a ',', we've reached the end of
11968          the list.  */
11969       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11970         break;
11971       /* Otherwise, consume the `,' and keep going.  */
11972       cp_lexer_consume_token (parser->lexer);
11973       /* If the next token is a `}', there is a trailing comma.  */
11974       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11975         {
11976           if (!in_system_header)
11977             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11978           break;
11979         }
11980     }
11981 }
11982
11983 /* Parse an enumerator-definition.  The enumerator has the indicated
11984    TYPE.
11985
11986    enumerator-definition:
11987      enumerator
11988      enumerator = constant-expression
11989
11990    enumerator:
11991      identifier  */
11992
11993 static void
11994 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11995 {
11996   tree identifier;
11997   tree value;
11998
11999   /* Look for the identifier.  */
12000   identifier = cp_parser_identifier (parser);
12001   if (identifier == error_mark_node)
12002     return;
12003
12004   /* If the next token is an '=', then there is an explicit value.  */
12005   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12006     {
12007       /* Consume the `=' token.  */
12008       cp_lexer_consume_token (parser->lexer);
12009       /* Parse the value.  */
12010       value = cp_parser_constant_expression (parser,
12011                                              /*allow_non_constant_p=*/false,
12012                                              NULL);
12013     }
12014   else
12015     value = NULL_TREE;
12016
12017   /* If we are processing a template, make sure the initializer of the
12018      enumerator doesn't contain any bare template parameter pack.  */
12019   if (check_for_bare_parameter_packs (value))
12020     value = error_mark_node;
12021
12022   /* Create the enumerator.  */
12023   build_enumerator (identifier, value, type);
12024 }
12025
12026 /* Parse a namespace-name.
12027
12028    namespace-name:
12029      original-namespace-name
12030      namespace-alias
12031
12032    Returns the NAMESPACE_DECL for the namespace.  */
12033
12034 static tree
12035 cp_parser_namespace_name (cp_parser* parser)
12036 {
12037   tree identifier;
12038   tree namespace_decl;
12039
12040   cp_token *token = cp_lexer_peek_token (parser->lexer);
12041
12042   /* Get the name of the namespace.  */
12043   identifier = cp_parser_identifier (parser);
12044   if (identifier == error_mark_node)
12045     return error_mark_node;
12046
12047   /* Look up the identifier in the currently active scope.  Look only
12048      for namespaces, due to:
12049
12050        [basic.lookup.udir]
12051
12052        When looking up a namespace-name in a using-directive or alias
12053        definition, only namespace names are considered.
12054
12055      And:
12056
12057        [basic.lookup.qual]
12058
12059        During the lookup of a name preceding the :: scope resolution
12060        operator, object, function, and enumerator names are ignored.
12061
12062      (Note that cp_parser_qualifying_entity only calls this
12063      function if the token after the name is the scope resolution
12064      operator.)  */
12065   namespace_decl = cp_parser_lookup_name (parser, identifier,
12066                                           none_type,
12067                                           /*is_template=*/false,
12068                                           /*is_namespace=*/true,
12069                                           /*check_dependency=*/true,
12070                                           /*ambiguous_decls=*/NULL,
12071                                           token->location);
12072   /* If it's not a namespace, issue an error.  */
12073   if (namespace_decl == error_mark_node
12074       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12075     {
12076       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12077         error ("%H%qD is not a namespace-name", &token->location, identifier);
12078       cp_parser_error (parser, "expected namespace-name");
12079       namespace_decl = error_mark_node;
12080     }
12081
12082   return namespace_decl;
12083 }
12084
12085 /* Parse a namespace-definition.
12086
12087    namespace-definition:
12088      named-namespace-definition
12089      unnamed-namespace-definition
12090
12091    named-namespace-definition:
12092      original-namespace-definition
12093      extension-namespace-definition
12094
12095    original-namespace-definition:
12096      namespace identifier { namespace-body }
12097
12098    extension-namespace-definition:
12099      namespace original-namespace-name { namespace-body }
12100
12101    unnamed-namespace-definition:
12102      namespace { namespace-body } */
12103
12104 static void
12105 cp_parser_namespace_definition (cp_parser* parser)
12106 {
12107   tree identifier, attribs;
12108   bool has_visibility;
12109   bool is_inline;
12110
12111   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12112     {
12113       is_inline = true;
12114       cp_lexer_consume_token (parser->lexer);
12115     }
12116   else
12117     is_inline = false;
12118
12119   /* Look for the `namespace' keyword.  */
12120   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12121
12122   /* Get the name of the namespace.  We do not attempt to distinguish
12123      between an original-namespace-definition and an
12124      extension-namespace-definition at this point.  The semantic
12125      analysis routines are responsible for that.  */
12126   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12127     identifier = cp_parser_identifier (parser);
12128   else
12129     identifier = NULL_TREE;
12130
12131   /* Parse any specified attributes.  */
12132   attribs = cp_parser_attributes_opt (parser);
12133
12134   /* Look for the `{' to start the namespace.  */
12135   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12136   /* Start the namespace.  */
12137   push_namespace (identifier);
12138
12139   /* "inline namespace" is equivalent to a stub namespace definition
12140      followed by a strong using directive.  */
12141   if (is_inline)
12142     {
12143       tree name_space = current_namespace;
12144       /* Set up namespace association.  */
12145       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12146         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12147                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12148       /* Import the contents of the inline namespace.  */
12149       pop_namespace ();
12150       do_using_directive (name_space);
12151       push_namespace (identifier);
12152     }
12153
12154   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12155
12156   /* Parse the body of the namespace.  */
12157   cp_parser_namespace_body (parser);
12158
12159 #ifdef HANDLE_PRAGMA_VISIBILITY
12160   if (has_visibility)
12161     pop_visibility ();
12162 #endif
12163
12164   /* Finish the namespace.  */
12165   pop_namespace ();
12166   /* Look for the final `}'.  */
12167   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12168 }
12169
12170 /* Parse a namespace-body.
12171
12172    namespace-body:
12173      declaration-seq [opt]  */
12174
12175 static void
12176 cp_parser_namespace_body (cp_parser* parser)
12177 {
12178   cp_parser_declaration_seq_opt (parser);
12179 }
12180
12181 /* Parse a namespace-alias-definition.
12182
12183    namespace-alias-definition:
12184      namespace identifier = qualified-namespace-specifier ;  */
12185
12186 static void
12187 cp_parser_namespace_alias_definition (cp_parser* parser)
12188 {
12189   tree identifier;
12190   tree namespace_specifier;
12191
12192   cp_token *token = cp_lexer_peek_token (parser->lexer);
12193
12194   /* Look for the `namespace' keyword.  */
12195   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12196   /* Look for the identifier.  */
12197   identifier = cp_parser_identifier (parser);
12198   if (identifier == error_mark_node)
12199     return;
12200   /* Look for the `=' token.  */
12201   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12202       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12203     {
12204       error ("%H%<namespace%> definition is not allowed here", &token->location);
12205       /* Skip the definition.  */
12206       cp_lexer_consume_token (parser->lexer);
12207       if (cp_parser_skip_to_closing_brace (parser))
12208         cp_lexer_consume_token (parser->lexer);
12209       return;
12210     }
12211   cp_parser_require (parser, CPP_EQ, "%<=%>");
12212   /* Look for the qualified-namespace-specifier.  */
12213   namespace_specifier
12214     = cp_parser_qualified_namespace_specifier (parser);
12215   /* Look for the `;' token.  */
12216   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12217
12218   /* Register the alias in the symbol table.  */
12219   do_namespace_alias (identifier, namespace_specifier);
12220 }
12221
12222 /* Parse a qualified-namespace-specifier.
12223
12224    qualified-namespace-specifier:
12225      :: [opt] nested-name-specifier [opt] namespace-name
12226
12227    Returns a NAMESPACE_DECL corresponding to the specified
12228    namespace.  */
12229
12230 static tree
12231 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12232 {
12233   /* Look for the optional `::'.  */
12234   cp_parser_global_scope_opt (parser,
12235                               /*current_scope_valid_p=*/false);
12236
12237   /* Look for the optional nested-name-specifier.  */
12238   cp_parser_nested_name_specifier_opt (parser,
12239                                        /*typename_keyword_p=*/false,
12240                                        /*check_dependency_p=*/true,
12241                                        /*type_p=*/false,
12242                                        /*is_declaration=*/true);
12243
12244   return cp_parser_namespace_name (parser);
12245 }
12246
12247 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12248    access declaration.
12249
12250    using-declaration:
12251      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12252      using :: unqualified-id ;  
12253
12254    access-declaration:
12255      qualified-id ;  
12256
12257    */
12258
12259 static bool
12260 cp_parser_using_declaration (cp_parser* parser, 
12261                              bool access_declaration_p)
12262 {
12263   cp_token *token;
12264   bool typename_p = false;
12265   bool global_scope_p;
12266   tree decl;
12267   tree identifier;
12268   tree qscope;
12269
12270   if (access_declaration_p)
12271     cp_parser_parse_tentatively (parser);
12272   else
12273     {
12274       /* Look for the `using' keyword.  */
12275       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12276       
12277       /* Peek at the next token.  */
12278       token = cp_lexer_peek_token (parser->lexer);
12279       /* See if it's `typename'.  */
12280       if (token->keyword == RID_TYPENAME)
12281         {
12282           /* Remember that we've seen it.  */
12283           typename_p = true;
12284           /* Consume the `typename' token.  */
12285           cp_lexer_consume_token (parser->lexer);
12286         }
12287     }
12288
12289   /* Look for the optional global scope qualification.  */
12290   global_scope_p
12291     = (cp_parser_global_scope_opt (parser,
12292                                    /*current_scope_valid_p=*/false)
12293        != NULL_TREE);
12294
12295   /* If we saw `typename', or didn't see `::', then there must be a
12296      nested-name-specifier present.  */
12297   if (typename_p || !global_scope_p)
12298     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12299                                               /*check_dependency_p=*/true,
12300                                               /*type_p=*/false,
12301                                               /*is_declaration=*/true);
12302   /* Otherwise, we could be in either of the two productions.  In that
12303      case, treat the nested-name-specifier as optional.  */
12304   else
12305     qscope = cp_parser_nested_name_specifier_opt (parser,
12306                                                   /*typename_keyword_p=*/false,
12307                                                   /*check_dependency_p=*/true,
12308                                                   /*type_p=*/false,
12309                                                   /*is_declaration=*/true);
12310   if (!qscope)
12311     qscope = global_namespace;
12312
12313   if (access_declaration_p && cp_parser_error_occurred (parser))
12314     /* Something has already gone wrong; there's no need to parse
12315        further.  Since an error has occurred, the return value of
12316        cp_parser_parse_definitely will be false, as required.  */
12317     return cp_parser_parse_definitely (parser);
12318
12319   token = cp_lexer_peek_token (parser->lexer);
12320   /* Parse the unqualified-id.  */
12321   identifier = cp_parser_unqualified_id (parser,
12322                                          /*template_keyword_p=*/false,
12323                                          /*check_dependency_p=*/true,
12324                                          /*declarator_p=*/true,
12325                                          /*optional_p=*/false);
12326
12327   if (access_declaration_p)
12328     {
12329       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12330         cp_parser_simulate_error (parser);
12331       if (!cp_parser_parse_definitely (parser))
12332         return false;
12333     }
12334
12335   /* The function we call to handle a using-declaration is different
12336      depending on what scope we are in.  */
12337   if (qscope == error_mark_node || identifier == error_mark_node)
12338     ;
12339   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12340            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12341     /* [namespace.udecl]
12342
12343        A using declaration shall not name a template-id.  */
12344     error ("%Ha template-id may not appear in a using-declaration",
12345             &token->location);
12346   else
12347     {
12348       if (at_class_scope_p ())
12349         {
12350           /* Create the USING_DECL.  */
12351           decl = do_class_using_decl (parser->scope, identifier);
12352
12353           if (check_for_bare_parameter_packs (decl))
12354             return false;
12355           else
12356             /* Add it to the list of members in this class.  */
12357             finish_member_declaration (decl);
12358         }
12359       else
12360         {
12361           decl = cp_parser_lookup_name_simple (parser,
12362                                                identifier,
12363                                                token->location);
12364           if (decl == error_mark_node)
12365             cp_parser_name_lookup_error (parser, identifier,
12366                                          decl, NULL,
12367                                          token->location);
12368           else if (check_for_bare_parameter_packs (decl))
12369             return false;
12370           else if (!at_namespace_scope_p ())
12371             do_local_using_decl (decl, qscope, identifier);
12372           else
12373             do_toplevel_using_decl (decl, qscope, identifier);
12374         }
12375     }
12376
12377   /* Look for the final `;'.  */
12378   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12379   
12380   return true;
12381 }
12382
12383 /* Parse a using-directive.
12384
12385    using-directive:
12386      using namespace :: [opt] nested-name-specifier [opt]
12387        namespace-name ;  */
12388
12389 static void
12390 cp_parser_using_directive (cp_parser* parser)
12391 {
12392   tree namespace_decl;
12393   tree attribs;
12394
12395   /* Look for the `using' keyword.  */
12396   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12397   /* And the `namespace' keyword.  */
12398   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12399   /* Look for the optional `::' operator.  */
12400   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12401   /* And the optional nested-name-specifier.  */
12402   cp_parser_nested_name_specifier_opt (parser,
12403                                        /*typename_keyword_p=*/false,
12404                                        /*check_dependency_p=*/true,
12405                                        /*type_p=*/false,
12406                                        /*is_declaration=*/true);
12407   /* Get the namespace being used.  */
12408   namespace_decl = cp_parser_namespace_name (parser);
12409   /* And any specified attributes.  */
12410   attribs = cp_parser_attributes_opt (parser);
12411   /* Update the symbol table.  */
12412   parse_using_directive (namespace_decl, attribs);
12413   /* Look for the final `;'.  */
12414   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12415 }
12416
12417 /* Parse an asm-definition.
12418
12419    asm-definition:
12420      asm ( string-literal ) ;
12421
12422    GNU Extension:
12423
12424    asm-definition:
12425      asm volatile [opt] ( string-literal ) ;
12426      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12427      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12428                           : asm-operand-list [opt] ) ;
12429      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12430                           : asm-operand-list [opt]
12431                           : asm-operand-list [opt] ) ;  */
12432
12433 static void
12434 cp_parser_asm_definition (cp_parser* parser)
12435 {
12436   tree string;
12437   tree outputs = NULL_TREE;
12438   tree inputs = NULL_TREE;
12439   tree clobbers = NULL_TREE;
12440   tree asm_stmt;
12441   bool volatile_p = false;
12442   bool extended_p = false;
12443   bool invalid_inputs_p = false;
12444   bool invalid_outputs_p = false;
12445
12446   /* Look for the `asm' keyword.  */
12447   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12448   /* See if the next token is `volatile'.  */
12449   if (cp_parser_allow_gnu_extensions_p (parser)
12450       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12451     {
12452       /* Remember that we saw the `volatile' keyword.  */
12453       volatile_p = true;
12454       /* Consume the token.  */
12455       cp_lexer_consume_token (parser->lexer);
12456     }
12457   /* Look for the opening `('.  */
12458   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12459     return;
12460   /* Look for the string.  */
12461   string = cp_parser_string_literal (parser, false, false);
12462   if (string == error_mark_node)
12463     {
12464       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12465                                              /*consume_paren=*/true);
12466       return;
12467     }
12468
12469   /* If we're allowing GNU extensions, check for the extended assembly
12470      syntax.  Unfortunately, the `:' tokens need not be separated by
12471      a space in C, and so, for compatibility, we tolerate that here
12472      too.  Doing that means that we have to treat the `::' operator as
12473      two `:' tokens.  */
12474   if (cp_parser_allow_gnu_extensions_p (parser)
12475       && parser->in_function_body
12476       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12477           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12478     {
12479       bool inputs_p = false;
12480       bool clobbers_p = false;
12481
12482       /* The extended syntax was used.  */
12483       extended_p = true;
12484
12485       /* Look for outputs.  */
12486       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12487         {
12488           /* Consume the `:'.  */
12489           cp_lexer_consume_token (parser->lexer);
12490           /* Parse the output-operands.  */
12491           if (cp_lexer_next_token_is_not (parser->lexer,
12492                                           CPP_COLON)
12493               && cp_lexer_next_token_is_not (parser->lexer,
12494                                              CPP_SCOPE)
12495               && cp_lexer_next_token_is_not (parser->lexer,
12496                                              CPP_CLOSE_PAREN))
12497             outputs = cp_parser_asm_operand_list (parser);
12498
12499             if (outputs == error_mark_node)
12500               invalid_outputs_p = true;
12501         }
12502       /* If the next token is `::', there are no outputs, and the
12503          next token is the beginning of the inputs.  */
12504       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12505         /* The inputs are coming next.  */
12506         inputs_p = true;
12507
12508       /* Look for inputs.  */
12509       if (inputs_p
12510           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12511         {
12512           /* Consume the `:' or `::'.  */
12513           cp_lexer_consume_token (parser->lexer);
12514           /* Parse the output-operands.  */
12515           if (cp_lexer_next_token_is_not (parser->lexer,
12516                                           CPP_COLON)
12517               && cp_lexer_next_token_is_not (parser->lexer,
12518                                              CPP_CLOSE_PAREN))
12519             inputs = cp_parser_asm_operand_list (parser);
12520
12521             if (inputs == error_mark_node)
12522               invalid_inputs_p = true;
12523         }
12524       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12525         /* The clobbers are coming next.  */
12526         clobbers_p = true;
12527
12528       /* Look for clobbers.  */
12529       if (clobbers_p
12530           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12531         {
12532           /* Consume the `:' or `::'.  */
12533           cp_lexer_consume_token (parser->lexer);
12534           /* Parse the clobbers.  */
12535           if (cp_lexer_next_token_is_not (parser->lexer,
12536                                           CPP_CLOSE_PAREN))
12537             clobbers = cp_parser_asm_clobber_list (parser);
12538         }
12539     }
12540   /* Look for the closing `)'.  */
12541   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12542     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12543                                            /*consume_paren=*/true);
12544   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12545
12546   if (!invalid_inputs_p && !invalid_outputs_p)
12547     {
12548       /* Create the ASM_EXPR.  */
12549       if (parser->in_function_body)
12550         {
12551           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12552                                       inputs, clobbers);
12553           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12554           if (!extended_p)
12555             {
12556               tree temp = asm_stmt;
12557               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12558                 temp = TREE_OPERAND (temp, 0);
12559
12560               ASM_INPUT_P (temp) = 1;
12561             }
12562         }
12563       else
12564         cgraph_add_asm_node (string);
12565     }
12566 }
12567
12568 /* Declarators [gram.dcl.decl] */
12569
12570 /* Parse an init-declarator.
12571
12572    init-declarator:
12573      declarator initializer [opt]
12574
12575    GNU Extension:
12576
12577    init-declarator:
12578      declarator asm-specification [opt] attributes [opt] initializer [opt]
12579
12580    function-definition:
12581      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12582        function-body
12583      decl-specifier-seq [opt] declarator function-try-block
12584
12585    GNU Extension:
12586
12587    function-definition:
12588      __extension__ function-definition
12589
12590    The DECL_SPECIFIERS apply to this declarator.  Returns a
12591    representation of the entity declared.  If MEMBER_P is TRUE, then
12592    this declarator appears in a class scope.  The new DECL created by
12593    this declarator is returned.
12594
12595    The CHECKS are access checks that should be performed once we know
12596    what entity is being declared (and, therefore, what classes have
12597    befriended it).
12598
12599    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12600    for a function-definition here as well.  If the declarator is a
12601    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12602    be TRUE upon return.  By that point, the function-definition will
12603    have been completely parsed.
12604
12605    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12606    is FALSE.  */
12607
12608 static tree
12609 cp_parser_init_declarator (cp_parser* parser,
12610                            cp_decl_specifier_seq *decl_specifiers,
12611                            VEC (deferred_access_check,gc)* checks,
12612                            bool function_definition_allowed_p,
12613                            bool member_p,
12614                            int declares_class_or_enum,
12615                            bool* function_definition_p)
12616 {
12617   cp_token *token = NULL, *asm_spec_start_token = NULL,
12618            *attributes_start_token = NULL;
12619   cp_declarator *declarator;
12620   tree prefix_attributes;
12621   tree attributes;
12622   tree asm_specification;
12623   tree initializer;
12624   tree decl = NULL_TREE;
12625   tree scope;
12626   int is_initialized;
12627   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12628      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12629      "(...)".  */
12630   enum cpp_ttype initialization_kind;
12631   bool is_direct_init = false;
12632   bool is_non_constant_init;
12633   int ctor_dtor_or_conv_p;
12634   bool friend_p;
12635   tree pushed_scope = NULL;
12636
12637   /* Gather the attributes that were provided with the
12638      decl-specifiers.  */
12639   prefix_attributes = decl_specifiers->attributes;
12640
12641   /* Assume that this is not the declarator for a function
12642      definition.  */
12643   if (function_definition_p)
12644     *function_definition_p = false;
12645
12646   /* Defer access checks while parsing the declarator; we cannot know
12647      what names are accessible until we know what is being
12648      declared.  */
12649   resume_deferring_access_checks ();
12650
12651   /* Parse the declarator.  */
12652   token = cp_lexer_peek_token (parser->lexer);
12653   declarator
12654     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12655                             &ctor_dtor_or_conv_p,
12656                             /*parenthesized_p=*/NULL,
12657                             /*member_p=*/false);
12658   /* Gather up the deferred checks.  */
12659   stop_deferring_access_checks ();
12660
12661   /* If the DECLARATOR was erroneous, there's no need to go
12662      further.  */
12663   if (declarator == cp_error_declarator)
12664     return error_mark_node;
12665
12666   /* Check that the number of template-parameter-lists is OK.  */
12667   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12668                                                        token->location))
12669     return error_mark_node;
12670
12671   if (declares_class_or_enum & 2)
12672     cp_parser_check_for_definition_in_return_type (declarator,
12673                                                    decl_specifiers->type,
12674                                                    decl_specifiers->type_location);
12675
12676   /* Figure out what scope the entity declared by the DECLARATOR is
12677      located in.  `grokdeclarator' sometimes changes the scope, so
12678      we compute it now.  */
12679   scope = get_scope_of_declarator (declarator);
12680
12681   /* If we're allowing GNU extensions, look for an asm-specification
12682      and attributes.  */
12683   if (cp_parser_allow_gnu_extensions_p (parser))
12684     {
12685       /* Look for an asm-specification.  */
12686       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12687       asm_specification = cp_parser_asm_specification_opt (parser);
12688       /* And attributes.  */
12689       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12690       attributes = cp_parser_attributes_opt (parser);
12691     }
12692   else
12693     {
12694       asm_specification = NULL_TREE;
12695       attributes = NULL_TREE;
12696     }
12697
12698   /* Peek at the next token.  */
12699   token = cp_lexer_peek_token (parser->lexer);
12700   /* Check to see if the token indicates the start of a
12701      function-definition.  */
12702   if (function_declarator_p (declarator)
12703       && cp_parser_token_starts_function_definition_p (token))
12704     {
12705       if (!function_definition_allowed_p)
12706         {
12707           /* If a function-definition should not appear here, issue an
12708              error message.  */
12709           cp_parser_error (parser,
12710                            "a function-definition is not allowed here");
12711           return error_mark_node;
12712         }
12713       else
12714         {
12715           location_t func_brace_location
12716             = cp_lexer_peek_token (parser->lexer)->location;
12717
12718           /* Neither attributes nor an asm-specification are allowed
12719              on a function-definition.  */
12720           if (asm_specification)
12721             error ("%Han asm-specification is not allowed "
12722                    "on a function-definition",
12723                    &asm_spec_start_token->location);
12724           if (attributes)
12725             error ("%Hattributes are not allowed on a function-definition",
12726                    &attributes_start_token->location);
12727           /* This is a function-definition.  */
12728           *function_definition_p = true;
12729
12730           /* Parse the function definition.  */
12731           if (member_p)
12732             decl = cp_parser_save_member_function_body (parser,
12733                                                         decl_specifiers,
12734                                                         declarator,
12735                                                         prefix_attributes);
12736           else
12737             decl
12738               = (cp_parser_function_definition_from_specifiers_and_declarator
12739                  (parser, decl_specifiers, prefix_attributes, declarator));
12740
12741           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12742             {
12743               /* This is where the prologue starts...  */
12744               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12745                 = func_brace_location;
12746             }
12747
12748           return decl;
12749         }
12750     }
12751
12752   /* [dcl.dcl]
12753
12754      Only in function declarations for constructors, destructors, and
12755      type conversions can the decl-specifier-seq be omitted.
12756
12757      We explicitly postpone this check past the point where we handle
12758      function-definitions because we tolerate function-definitions
12759      that are missing their return types in some modes.  */
12760   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12761     {
12762       cp_parser_error (parser,
12763                        "expected constructor, destructor, or type conversion");
12764       return error_mark_node;
12765     }
12766
12767   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12768   if (token->type == CPP_EQ
12769       || token->type == CPP_OPEN_PAREN
12770       || token->type == CPP_OPEN_BRACE)
12771     {
12772       is_initialized = SD_INITIALIZED;
12773       initialization_kind = token->type;
12774
12775       if (token->type == CPP_EQ
12776           && function_declarator_p (declarator))
12777         {
12778           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12779           if (t2->keyword == RID_DEFAULT)
12780             is_initialized = SD_DEFAULTED;
12781           else if (t2->keyword == RID_DELETE)
12782             is_initialized = SD_DELETED;
12783         }
12784     }
12785   else
12786     {
12787       /* If the init-declarator isn't initialized and isn't followed by a
12788          `,' or `;', it's not a valid init-declarator.  */
12789       if (token->type != CPP_COMMA
12790           && token->type != CPP_SEMICOLON)
12791         {
12792           cp_parser_error (parser, "expected initializer");
12793           return error_mark_node;
12794         }
12795       is_initialized = SD_UNINITIALIZED;
12796       initialization_kind = CPP_EOF;
12797     }
12798
12799   /* Because start_decl has side-effects, we should only call it if we
12800      know we're going ahead.  By this point, we know that we cannot
12801      possibly be looking at any other construct.  */
12802   cp_parser_commit_to_tentative_parse (parser);
12803
12804   /* If the decl specifiers were bad, issue an error now that we're
12805      sure this was intended to be a declarator.  Then continue
12806      declaring the variable(s), as int, to try to cut down on further
12807      errors.  */
12808   if (decl_specifiers->any_specifiers_p
12809       && decl_specifiers->type == error_mark_node)
12810     {
12811       cp_parser_error (parser, "invalid type in declaration");
12812       decl_specifiers->type = integer_type_node;
12813     }
12814
12815   /* Check to see whether or not this declaration is a friend.  */
12816   friend_p = cp_parser_friend_p (decl_specifiers);
12817
12818   /* Enter the newly declared entry in the symbol table.  If we're
12819      processing a declaration in a class-specifier, we wait until
12820      after processing the initializer.  */
12821   if (!member_p)
12822     {
12823       if (parser->in_unbraced_linkage_specification_p)
12824         decl_specifiers->storage_class = sc_extern;
12825       decl = start_decl (declarator, decl_specifiers,
12826                          is_initialized, attributes, prefix_attributes,
12827                          &pushed_scope);
12828     }
12829   else if (scope)
12830     /* Enter the SCOPE.  That way unqualified names appearing in the
12831        initializer will be looked up in SCOPE.  */
12832     pushed_scope = push_scope (scope);
12833
12834   /* Perform deferred access control checks, now that we know in which
12835      SCOPE the declared entity resides.  */
12836   if (!member_p && decl)
12837     {
12838       tree saved_current_function_decl = NULL_TREE;
12839
12840       /* If the entity being declared is a function, pretend that we
12841          are in its scope.  If it is a `friend', it may have access to
12842          things that would not otherwise be accessible.  */
12843       if (TREE_CODE (decl) == FUNCTION_DECL)
12844         {
12845           saved_current_function_decl = current_function_decl;
12846           current_function_decl = decl;
12847         }
12848
12849       /* Perform access checks for template parameters.  */
12850       cp_parser_perform_template_parameter_access_checks (checks);
12851
12852       /* Perform the access control checks for the declarator and the
12853          decl-specifiers.  */
12854       perform_deferred_access_checks ();
12855
12856       /* Restore the saved value.  */
12857       if (TREE_CODE (decl) == FUNCTION_DECL)
12858         current_function_decl = saved_current_function_decl;
12859     }
12860
12861   /* Parse the initializer.  */
12862   initializer = NULL_TREE;
12863   is_direct_init = false;
12864   is_non_constant_init = true;
12865   if (is_initialized)
12866     {
12867       if (function_declarator_p (declarator))
12868         {
12869           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12870            if (initialization_kind == CPP_EQ)
12871              initializer = cp_parser_pure_specifier (parser);
12872            else
12873              {
12874                /* If the declaration was erroneous, we don't really
12875                   know what the user intended, so just silently
12876                   consume the initializer.  */
12877                if (decl != error_mark_node)
12878                  error ("%Hinitializer provided for function",
12879                         &initializer_start_token->location);
12880                cp_parser_skip_to_closing_parenthesis (parser,
12881                                                       /*recovering=*/true,
12882                                                       /*or_comma=*/false,
12883                                                       /*consume_paren=*/true);
12884              }
12885         }
12886       else
12887         initializer = cp_parser_initializer (parser,
12888                                              &is_direct_init,
12889                                              &is_non_constant_init);
12890     }
12891
12892   /* The old parser allows attributes to appear after a parenthesized
12893      initializer.  Mark Mitchell proposed removing this functionality
12894      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12895      attributes -- but ignores them.  */
12896   if (cp_parser_allow_gnu_extensions_p (parser)
12897       && initialization_kind == CPP_OPEN_PAREN)
12898     if (cp_parser_attributes_opt (parser))
12899       warning (OPT_Wattributes,
12900                "attributes after parenthesized initializer ignored");
12901
12902   /* For an in-class declaration, use `grokfield' to create the
12903      declaration.  */
12904   if (member_p)
12905     {
12906       if (pushed_scope)
12907         {
12908           pop_scope (pushed_scope);
12909           pushed_scope = false;
12910         }
12911       decl = grokfield (declarator, decl_specifiers,
12912                         initializer, !is_non_constant_init,
12913                         /*asmspec=*/NULL_TREE,
12914                         prefix_attributes);
12915       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12916         cp_parser_save_default_args (parser, decl);
12917     }
12918
12919   /* Finish processing the declaration.  But, skip friend
12920      declarations.  */
12921   if (!friend_p && decl && decl != error_mark_node)
12922     {
12923       cp_finish_decl (decl,
12924                       initializer, !is_non_constant_init,
12925                       asm_specification,
12926                       /* If the initializer is in parentheses, then this is
12927                          a direct-initialization, which means that an
12928                          `explicit' constructor is OK.  Otherwise, an
12929                          `explicit' constructor cannot be used.  */
12930                       ((is_direct_init || !is_initialized)
12931                        ? 0 : LOOKUP_ONLYCONVERTING));
12932     }
12933   else if ((cxx_dialect != cxx98) && friend_p
12934            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12935     /* Core issue #226 (C++0x only): A default template-argument
12936        shall not be specified in a friend class template
12937        declaration. */
12938     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12939                              /*is_partial=*/0, /*is_friend_decl=*/1);
12940
12941   if (!friend_p && pushed_scope)
12942     pop_scope (pushed_scope);
12943
12944   return decl;
12945 }
12946
12947 /* Parse a declarator.
12948
12949    declarator:
12950      direct-declarator
12951      ptr-operator declarator
12952
12953    abstract-declarator:
12954      ptr-operator abstract-declarator [opt]
12955      direct-abstract-declarator
12956
12957    GNU Extensions:
12958
12959    declarator:
12960      attributes [opt] direct-declarator
12961      attributes [opt] ptr-operator declarator
12962
12963    abstract-declarator:
12964      attributes [opt] ptr-operator abstract-declarator [opt]
12965      attributes [opt] direct-abstract-declarator
12966
12967    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12968    detect constructor, destructor or conversion operators. It is set
12969    to -1 if the declarator is a name, and +1 if it is a
12970    function. Otherwise it is set to zero. Usually you just want to
12971    test for >0, but internally the negative value is used.
12972
12973    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12974    a decl-specifier-seq unless it declares a constructor, destructor,
12975    or conversion.  It might seem that we could check this condition in
12976    semantic analysis, rather than parsing, but that makes it difficult
12977    to handle something like `f()'.  We want to notice that there are
12978    no decl-specifiers, and therefore realize that this is an
12979    expression, not a declaration.)
12980
12981    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12982    the declarator is a direct-declarator of the form "(...)".
12983
12984    MEMBER_P is true iff this declarator is a member-declarator.  */
12985
12986 static cp_declarator *
12987 cp_parser_declarator (cp_parser* parser,
12988                       cp_parser_declarator_kind dcl_kind,
12989                       int* ctor_dtor_or_conv_p,
12990                       bool* parenthesized_p,
12991                       bool member_p)
12992 {
12993   cp_token *token;
12994   cp_declarator *declarator;
12995   enum tree_code code;
12996   cp_cv_quals cv_quals;
12997   tree class_type;
12998   tree attributes = NULL_TREE;
12999
13000   /* Assume this is not a constructor, destructor, or type-conversion
13001      operator.  */
13002   if (ctor_dtor_or_conv_p)
13003     *ctor_dtor_or_conv_p = 0;
13004
13005   if (cp_parser_allow_gnu_extensions_p (parser))
13006     attributes = cp_parser_attributes_opt (parser);
13007
13008   /* Peek at the next token.  */
13009   token = cp_lexer_peek_token (parser->lexer);
13010
13011   /* Check for the ptr-operator production.  */
13012   cp_parser_parse_tentatively (parser);
13013   /* Parse the ptr-operator.  */
13014   code = cp_parser_ptr_operator (parser,
13015                                  &class_type,
13016                                  &cv_quals);
13017   /* If that worked, then we have a ptr-operator.  */
13018   if (cp_parser_parse_definitely (parser))
13019     {
13020       /* If a ptr-operator was found, then this declarator was not
13021          parenthesized.  */
13022       if (parenthesized_p)
13023         *parenthesized_p = true;
13024       /* The dependent declarator is optional if we are parsing an
13025          abstract-declarator.  */
13026       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13027         cp_parser_parse_tentatively (parser);
13028
13029       /* Parse the dependent declarator.  */
13030       declarator = cp_parser_declarator (parser, dcl_kind,
13031                                          /*ctor_dtor_or_conv_p=*/NULL,
13032                                          /*parenthesized_p=*/NULL,
13033                                          /*member_p=*/false);
13034
13035       /* If we are parsing an abstract-declarator, we must handle the
13036          case where the dependent declarator is absent.  */
13037       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13038           && !cp_parser_parse_definitely (parser))
13039         declarator = NULL;
13040
13041       declarator = cp_parser_make_indirect_declarator
13042         (code, class_type, cv_quals, declarator);
13043     }
13044   /* Everything else is a direct-declarator.  */
13045   else
13046     {
13047       if (parenthesized_p)
13048         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13049                                                    CPP_OPEN_PAREN);
13050       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13051                                                 ctor_dtor_or_conv_p,
13052                                                 member_p);
13053     }
13054
13055   if (attributes && declarator && declarator != cp_error_declarator)
13056     declarator->attributes = attributes;
13057
13058   return declarator;
13059 }
13060
13061 /* Parse a direct-declarator or direct-abstract-declarator.
13062
13063    direct-declarator:
13064      declarator-id
13065      direct-declarator ( parameter-declaration-clause )
13066        cv-qualifier-seq [opt]
13067        exception-specification [opt]
13068      direct-declarator [ constant-expression [opt] ]
13069      ( declarator )
13070
13071    direct-abstract-declarator:
13072      direct-abstract-declarator [opt]
13073        ( parameter-declaration-clause )
13074        cv-qualifier-seq [opt]
13075        exception-specification [opt]
13076      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13077      ( abstract-declarator )
13078
13079    Returns a representation of the declarator.  DCL_KIND is
13080    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13081    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13082    we are parsing a direct-declarator.  It is
13083    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13084    of ambiguity we prefer an abstract declarator, as per
13085    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13086    cp_parser_declarator.  */
13087
13088 static cp_declarator *
13089 cp_parser_direct_declarator (cp_parser* parser,
13090                              cp_parser_declarator_kind dcl_kind,
13091                              int* ctor_dtor_or_conv_p,
13092                              bool member_p)
13093 {
13094   cp_token *token;
13095   cp_declarator *declarator = NULL;
13096   tree scope = NULL_TREE;
13097   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13098   bool saved_in_declarator_p = parser->in_declarator_p;
13099   bool first = true;
13100   tree pushed_scope = NULL_TREE;
13101
13102   while (true)
13103     {
13104       /* Peek at the next token.  */
13105       token = cp_lexer_peek_token (parser->lexer);
13106       if (token->type == CPP_OPEN_PAREN)
13107         {
13108           /* This is either a parameter-declaration-clause, or a
13109              parenthesized declarator. When we know we are parsing a
13110              named declarator, it must be a parenthesized declarator
13111              if FIRST is true. For instance, `(int)' is a
13112              parameter-declaration-clause, with an omitted
13113              direct-abstract-declarator. But `((*))', is a
13114              parenthesized abstract declarator. Finally, when T is a
13115              template parameter `(T)' is a
13116              parameter-declaration-clause, and not a parenthesized
13117              named declarator.
13118
13119              We first try and parse a parameter-declaration-clause,
13120              and then try a nested declarator (if FIRST is true).
13121
13122              It is not an error for it not to be a
13123              parameter-declaration-clause, even when FIRST is
13124              false. Consider,
13125
13126                int i (int);
13127                int i (3);
13128
13129              The first is the declaration of a function while the
13130              second is the definition of a variable, including its
13131              initializer.
13132
13133              Having seen only the parenthesis, we cannot know which of
13134              these two alternatives should be selected.  Even more
13135              complex are examples like:
13136
13137                int i (int (a));
13138                int i (int (3));
13139
13140              The former is a function-declaration; the latter is a
13141              variable initialization.
13142
13143              Thus again, we try a parameter-declaration-clause, and if
13144              that fails, we back out and return.  */
13145
13146           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13147             {
13148               tree params;
13149               unsigned saved_num_template_parameter_lists;
13150               bool is_declarator = false;
13151               tree t;
13152
13153               /* In a member-declarator, the only valid interpretation
13154                  of a parenthesis is the start of a
13155                  parameter-declaration-clause.  (It is invalid to
13156                  initialize a static data member with a parenthesized
13157                  initializer; only the "=" form of initialization is
13158                  permitted.)  */
13159               if (!member_p)
13160                 cp_parser_parse_tentatively (parser);
13161
13162               /* Consume the `('.  */
13163               cp_lexer_consume_token (parser->lexer);
13164               if (first)
13165                 {
13166                   /* If this is going to be an abstract declarator, we're
13167                      in a declarator and we can't have default args.  */
13168                   parser->default_arg_ok_p = false;
13169                   parser->in_declarator_p = true;
13170                 }
13171
13172               /* Inside the function parameter list, surrounding
13173                  template-parameter-lists do not apply.  */
13174               saved_num_template_parameter_lists
13175                 = parser->num_template_parameter_lists;
13176               parser->num_template_parameter_lists = 0;
13177
13178               begin_scope (sk_function_parms, NULL_TREE);
13179
13180               /* Parse the parameter-declaration-clause.  */
13181               params = cp_parser_parameter_declaration_clause (parser);
13182
13183               parser->num_template_parameter_lists
13184                 = saved_num_template_parameter_lists;
13185
13186               /* If all went well, parse the cv-qualifier-seq and the
13187                  exception-specification.  */
13188               if (member_p || cp_parser_parse_definitely (parser))
13189                 {
13190                   cp_cv_quals cv_quals;
13191                   tree exception_specification;
13192                   tree late_return;
13193
13194                   is_declarator = true;
13195
13196                   if (ctor_dtor_or_conv_p)
13197                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13198                   first = false;
13199                   /* Consume the `)'.  */
13200                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13201
13202                   /* Parse the cv-qualifier-seq.  */
13203                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13204                   /* And the exception-specification.  */
13205                   exception_specification
13206                     = cp_parser_exception_specification_opt (parser);
13207
13208                   late_return
13209                     = cp_parser_late_return_type_opt (parser);
13210
13211                   /* Create the function-declarator.  */
13212                   declarator = make_call_declarator (declarator,
13213                                                      params,
13214                                                      cv_quals,
13215                                                      exception_specification,
13216                                                      late_return);
13217                   /* Any subsequent parameter lists are to do with
13218                      return type, so are not those of the declared
13219                      function.  */
13220                   parser->default_arg_ok_p = false;
13221                 }
13222
13223               /* Remove the function parms from scope.  */
13224               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13225                 pop_binding (DECL_NAME (t), t);
13226               leave_scope();
13227
13228               if (is_declarator)
13229                 /* Repeat the main loop.  */
13230                 continue;
13231             }
13232
13233           /* If this is the first, we can try a parenthesized
13234              declarator.  */
13235           if (first)
13236             {
13237               bool saved_in_type_id_in_expr_p;
13238
13239               parser->default_arg_ok_p = saved_default_arg_ok_p;
13240               parser->in_declarator_p = saved_in_declarator_p;
13241
13242               /* Consume the `('.  */
13243               cp_lexer_consume_token (parser->lexer);
13244               /* Parse the nested declarator.  */
13245               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13246               parser->in_type_id_in_expr_p = true;
13247               declarator
13248                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13249                                         /*parenthesized_p=*/NULL,
13250                                         member_p);
13251               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13252               first = false;
13253               /* Expect a `)'.  */
13254               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13255                 declarator = cp_error_declarator;
13256               if (declarator == cp_error_declarator)
13257                 break;
13258
13259               goto handle_declarator;
13260             }
13261           /* Otherwise, we must be done.  */
13262           else
13263             break;
13264         }
13265       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13266                && token->type == CPP_OPEN_SQUARE)
13267         {
13268           /* Parse an array-declarator.  */
13269           tree bounds;
13270
13271           if (ctor_dtor_or_conv_p)
13272             *ctor_dtor_or_conv_p = 0;
13273
13274           first = false;
13275           parser->default_arg_ok_p = false;
13276           parser->in_declarator_p = true;
13277           /* Consume the `['.  */
13278           cp_lexer_consume_token (parser->lexer);
13279           /* Peek at the next token.  */
13280           token = cp_lexer_peek_token (parser->lexer);
13281           /* If the next token is `]', then there is no
13282              constant-expression.  */
13283           if (token->type != CPP_CLOSE_SQUARE)
13284             {
13285               bool non_constant_p;
13286
13287               bounds
13288                 = cp_parser_constant_expression (parser,
13289                                                  /*allow_non_constant=*/true,
13290                                                  &non_constant_p);
13291               if (!non_constant_p)
13292                 bounds = fold_non_dependent_expr (bounds);
13293               else if (processing_template_decl)
13294                 {
13295                   /* Remember this wasn't a constant-expression.  */
13296                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13297                   TREE_SIDE_EFFECTS (bounds) = 1;
13298                 }
13299
13300               /* Normally, the array bound must be an integral constant
13301                  expression.  However, as an extension, we allow VLAs
13302                  in function scopes.  */
13303               else if (!parser->in_function_body)
13304                 {
13305                   error ("%Harray bound is not an integer constant",
13306                          &token->location);
13307                   bounds = error_mark_node;
13308                 }
13309             }
13310           else
13311             bounds = NULL_TREE;
13312           /* Look for the closing `]'.  */
13313           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13314             {
13315               declarator = cp_error_declarator;
13316               break;
13317             }
13318
13319           declarator = make_array_declarator (declarator, bounds);
13320         }
13321       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13322         {
13323           tree qualifying_scope;
13324           tree unqualified_name;
13325           special_function_kind sfk;
13326           bool abstract_ok;
13327           bool pack_expansion_p = false;
13328           cp_token *declarator_id_start_token;
13329
13330           /* Parse a declarator-id */
13331           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13332           if (abstract_ok)
13333             {
13334               cp_parser_parse_tentatively (parser);
13335
13336               /* If we see an ellipsis, we should be looking at a
13337                  parameter pack. */
13338               if (token->type == CPP_ELLIPSIS)
13339                 {
13340                   /* Consume the `...' */
13341                   cp_lexer_consume_token (parser->lexer);
13342
13343                   pack_expansion_p = true;
13344                 }
13345             }
13346
13347           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13348           unqualified_name
13349             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13350           qualifying_scope = parser->scope;
13351           if (abstract_ok)
13352             {
13353               bool okay = false;
13354
13355               if (!unqualified_name && pack_expansion_p)
13356                 {
13357                   /* Check whether an error occurred. */
13358                   okay = !cp_parser_error_occurred (parser);
13359
13360                   /* We already consumed the ellipsis to mark a
13361                      parameter pack, but we have no way to report it,
13362                      so abort the tentative parse. We will be exiting
13363                      immediately anyway. */
13364                   cp_parser_abort_tentative_parse (parser);
13365                 }
13366               else
13367                 okay = cp_parser_parse_definitely (parser);
13368
13369               if (!okay)
13370                 unqualified_name = error_mark_node;
13371               else if (unqualified_name
13372                        && (qualifying_scope
13373                            || (TREE_CODE (unqualified_name)
13374                                != IDENTIFIER_NODE)))
13375                 {
13376                   cp_parser_error (parser, "expected unqualified-id");
13377                   unqualified_name = error_mark_node;
13378                 }
13379             }
13380
13381           if (!unqualified_name)
13382             return NULL;
13383           if (unqualified_name == error_mark_node)
13384             {
13385               declarator = cp_error_declarator;
13386               pack_expansion_p = false;
13387               declarator->parameter_pack_p = false;
13388               break;
13389             }
13390
13391           if (qualifying_scope && at_namespace_scope_p ()
13392               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13393             {
13394               /* In the declaration of a member of a template class
13395                  outside of the class itself, the SCOPE will sometimes
13396                  be a TYPENAME_TYPE.  For example, given:
13397
13398                  template <typename T>
13399                  int S<T>::R::i = 3;
13400
13401                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13402                  this context, we must resolve S<T>::R to an ordinary
13403                  type, rather than a typename type.
13404
13405                  The reason we normally avoid resolving TYPENAME_TYPEs
13406                  is that a specialization of `S' might render
13407                  `S<T>::R' not a type.  However, if `S' is
13408                  specialized, then this `i' will not be used, so there
13409                  is no harm in resolving the types here.  */
13410               tree type;
13411
13412               /* Resolve the TYPENAME_TYPE.  */
13413               type = resolve_typename_type (qualifying_scope,
13414                                             /*only_current_p=*/false);
13415               /* If that failed, the declarator is invalid.  */
13416               if (TREE_CODE (type) == TYPENAME_TYPE)
13417                 error ("%H%<%T::%E%> is not a type",
13418                        &declarator_id_start_token->location,
13419                        TYPE_CONTEXT (qualifying_scope),
13420                        TYPE_IDENTIFIER (qualifying_scope));
13421               qualifying_scope = type;
13422             }
13423
13424           sfk = sfk_none;
13425
13426           if (unqualified_name)
13427             {
13428               tree class_type;
13429
13430               if (qualifying_scope
13431                   && CLASS_TYPE_P (qualifying_scope))
13432                 class_type = qualifying_scope;
13433               else
13434                 class_type = current_class_type;
13435
13436               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13437                 {
13438                   tree name_type = TREE_TYPE (unqualified_name);
13439                   if (class_type && same_type_p (name_type, class_type))
13440                     {
13441                       if (qualifying_scope
13442                           && CLASSTYPE_USE_TEMPLATE (name_type))
13443                         {
13444                           error ("%Hinvalid use of constructor as a template",
13445                                  &declarator_id_start_token->location);
13446                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13447                                   "name the constructor in a qualified name",
13448                                   class_type,
13449                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13450                                   class_type, name_type);
13451                           declarator = cp_error_declarator;
13452                           break;
13453                         }
13454                       else
13455                         unqualified_name = constructor_name (class_type);
13456                     }
13457                   else
13458                     {
13459                       /* We do not attempt to print the declarator
13460                          here because we do not have enough
13461                          information about its original syntactic
13462                          form.  */
13463                       cp_parser_error (parser, "invalid declarator");
13464                       declarator = cp_error_declarator;
13465                       break;
13466                     }
13467                 }
13468
13469               if (class_type)
13470                 {
13471                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13472                     sfk = sfk_destructor;
13473                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13474                     sfk = sfk_conversion;
13475                   else if (/* There's no way to declare a constructor
13476                               for an anonymous type, even if the type
13477                               got a name for linkage purposes.  */
13478                            !TYPE_WAS_ANONYMOUS (class_type)
13479                            && constructor_name_p (unqualified_name,
13480                                                   class_type))
13481                     {
13482                       unqualified_name = constructor_name (class_type);
13483                       sfk = sfk_constructor;
13484                     }
13485
13486                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13487                     *ctor_dtor_or_conv_p = -1;
13488                 }
13489             }
13490           declarator = make_id_declarator (qualifying_scope,
13491                                            unqualified_name,
13492                                            sfk);
13493           declarator->id_loc = token->location;
13494           declarator->parameter_pack_p = pack_expansion_p;
13495
13496           if (pack_expansion_p)
13497             maybe_warn_variadic_templates ();
13498
13499         handle_declarator:;
13500           scope = get_scope_of_declarator (declarator);
13501           if (scope)
13502             /* Any names that appear after the declarator-id for a
13503                member are looked up in the containing scope.  */
13504             pushed_scope = push_scope (scope);
13505           parser->in_declarator_p = true;
13506           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13507               || (declarator && declarator->kind == cdk_id))
13508             /* Default args are only allowed on function
13509                declarations.  */
13510             parser->default_arg_ok_p = saved_default_arg_ok_p;
13511           else
13512             parser->default_arg_ok_p = false;
13513
13514           first = false;
13515         }
13516       /* We're done.  */
13517       else
13518         break;
13519     }
13520
13521   /* For an abstract declarator, we might wind up with nothing at this
13522      point.  That's an error; the declarator is not optional.  */
13523   if (!declarator)
13524     cp_parser_error (parser, "expected declarator");
13525
13526   /* If we entered a scope, we must exit it now.  */
13527   if (pushed_scope)
13528     pop_scope (pushed_scope);
13529
13530   parser->default_arg_ok_p = saved_default_arg_ok_p;
13531   parser->in_declarator_p = saved_in_declarator_p;
13532
13533   return declarator;
13534 }
13535
13536 /* Parse a ptr-operator.
13537
13538    ptr-operator:
13539      * cv-qualifier-seq [opt]
13540      &
13541      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13542
13543    GNU Extension:
13544
13545    ptr-operator:
13546      & cv-qualifier-seq [opt]
13547
13548    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13549    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13550    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13551    filled in with the TYPE containing the member.  *CV_QUALS is
13552    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13553    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13554    Note that the tree codes returned by this function have nothing
13555    to do with the types of trees that will be eventually be created
13556    to represent the pointer or reference type being parsed. They are
13557    just constants with suggestive names. */
13558 static enum tree_code
13559 cp_parser_ptr_operator (cp_parser* parser,
13560                         tree* type,
13561                         cp_cv_quals *cv_quals)
13562 {
13563   enum tree_code code = ERROR_MARK;
13564   cp_token *token;
13565
13566   /* Assume that it's not a pointer-to-member.  */
13567   *type = NULL_TREE;
13568   /* And that there are no cv-qualifiers.  */
13569   *cv_quals = TYPE_UNQUALIFIED;
13570
13571   /* Peek at the next token.  */
13572   token = cp_lexer_peek_token (parser->lexer);
13573
13574   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13575   if (token->type == CPP_MULT)
13576     code = INDIRECT_REF;
13577   else if (token->type == CPP_AND)
13578     code = ADDR_EXPR;
13579   else if ((cxx_dialect != cxx98) &&
13580            token->type == CPP_AND_AND) /* C++0x only */
13581     code = NON_LVALUE_EXPR;
13582
13583   if (code != ERROR_MARK)
13584     {
13585       /* Consume the `*', `&' or `&&'.  */
13586       cp_lexer_consume_token (parser->lexer);
13587
13588       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13589          `&', if we are allowing GNU extensions.  (The only qualifier
13590          that can legally appear after `&' is `restrict', but that is
13591          enforced during semantic analysis.  */
13592       if (code == INDIRECT_REF
13593           || cp_parser_allow_gnu_extensions_p (parser))
13594         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13595     }
13596   else
13597     {
13598       /* Try the pointer-to-member case.  */
13599       cp_parser_parse_tentatively (parser);
13600       /* Look for the optional `::' operator.  */
13601       cp_parser_global_scope_opt (parser,
13602                                   /*current_scope_valid_p=*/false);
13603       /* Look for the nested-name specifier.  */
13604       token = cp_lexer_peek_token (parser->lexer);
13605       cp_parser_nested_name_specifier (parser,
13606                                        /*typename_keyword_p=*/false,
13607                                        /*check_dependency_p=*/true,
13608                                        /*type_p=*/false,
13609                                        /*is_declaration=*/false);
13610       /* If we found it, and the next token is a `*', then we are
13611          indeed looking at a pointer-to-member operator.  */
13612       if (!cp_parser_error_occurred (parser)
13613           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13614         {
13615           /* Indicate that the `*' operator was used.  */
13616           code = INDIRECT_REF;
13617
13618           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13619             error ("%H%qD is a namespace", &token->location, parser->scope);
13620           else
13621             {
13622               /* The type of which the member is a member is given by the
13623                  current SCOPE.  */
13624               *type = parser->scope;
13625               /* The next name will not be qualified.  */
13626               parser->scope = NULL_TREE;
13627               parser->qualifying_scope = NULL_TREE;
13628               parser->object_scope = NULL_TREE;
13629               /* Look for the optional cv-qualifier-seq.  */
13630               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13631             }
13632         }
13633       /* If that didn't work we don't have a ptr-operator.  */
13634       if (!cp_parser_parse_definitely (parser))
13635         cp_parser_error (parser, "expected ptr-operator");
13636     }
13637
13638   return code;
13639 }
13640
13641 /* Parse an (optional) cv-qualifier-seq.
13642
13643    cv-qualifier-seq:
13644      cv-qualifier cv-qualifier-seq [opt]
13645
13646    cv-qualifier:
13647      const
13648      volatile
13649
13650    GNU Extension:
13651
13652    cv-qualifier:
13653      __restrict__
13654
13655    Returns a bitmask representing the cv-qualifiers.  */
13656
13657 static cp_cv_quals
13658 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13659 {
13660   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13661
13662   while (true)
13663     {
13664       cp_token *token;
13665       cp_cv_quals cv_qualifier;
13666
13667       /* Peek at the next token.  */
13668       token = cp_lexer_peek_token (parser->lexer);
13669       /* See if it's a cv-qualifier.  */
13670       switch (token->keyword)
13671         {
13672         case RID_CONST:
13673           cv_qualifier = TYPE_QUAL_CONST;
13674           break;
13675
13676         case RID_VOLATILE:
13677           cv_qualifier = TYPE_QUAL_VOLATILE;
13678           break;
13679
13680         case RID_RESTRICT:
13681           cv_qualifier = TYPE_QUAL_RESTRICT;
13682           break;
13683
13684         default:
13685           cv_qualifier = TYPE_UNQUALIFIED;
13686           break;
13687         }
13688
13689       if (!cv_qualifier)
13690         break;
13691
13692       if (cv_quals & cv_qualifier)
13693         {
13694           error ("%Hduplicate cv-qualifier", &token->location);
13695           cp_lexer_purge_token (parser->lexer);
13696         }
13697       else
13698         {
13699           cp_lexer_consume_token (parser->lexer);
13700           cv_quals |= cv_qualifier;
13701         }
13702     }
13703
13704   return cv_quals;
13705 }
13706
13707 /* Parse a late-specified return type, if any.  This is not a separate
13708    non-terminal, but part of a function declarator, which looks like
13709
13710    -> type-id
13711
13712    Returns the type indicated by the type-id.  */
13713
13714 static tree
13715 cp_parser_late_return_type_opt (cp_parser* parser)
13716 {
13717   cp_token *token;
13718
13719   /* Peek at the next token.  */
13720   token = cp_lexer_peek_token (parser->lexer);
13721   /* A late-specified return type is indicated by an initial '->'. */
13722   if (token->type != CPP_DEREF)
13723     return NULL_TREE;
13724
13725   /* Consume the ->.  */
13726   cp_lexer_consume_token (parser->lexer);
13727
13728   return cp_parser_type_id (parser);
13729 }
13730
13731 /* Parse a declarator-id.
13732
13733    declarator-id:
13734      id-expression
13735      :: [opt] nested-name-specifier [opt] type-name
13736
13737    In the `id-expression' case, the value returned is as for
13738    cp_parser_id_expression if the id-expression was an unqualified-id.
13739    If the id-expression was a qualified-id, then a SCOPE_REF is
13740    returned.  The first operand is the scope (either a NAMESPACE_DECL
13741    or TREE_TYPE), but the second is still just a representation of an
13742    unqualified-id.  */
13743
13744 static tree
13745 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13746 {
13747   tree id;
13748   /* The expression must be an id-expression.  Assume that qualified
13749      names are the names of types so that:
13750
13751        template <class T>
13752        int S<T>::R::i = 3;
13753
13754      will work; we must treat `S<T>::R' as the name of a type.
13755      Similarly, assume that qualified names are templates, where
13756      required, so that:
13757
13758        template <class T>
13759        int S<T>::R<T>::i = 3;
13760
13761      will work, too.  */
13762   id = cp_parser_id_expression (parser,
13763                                 /*template_keyword_p=*/false,
13764                                 /*check_dependency_p=*/false,
13765                                 /*template_p=*/NULL,
13766                                 /*declarator_p=*/true,
13767                                 optional_p);
13768   if (id && BASELINK_P (id))
13769     id = BASELINK_FUNCTIONS (id);
13770   return id;
13771 }
13772
13773 /* Parse a type-id.
13774
13775    type-id:
13776      type-specifier-seq abstract-declarator [opt]
13777
13778    Returns the TYPE specified.  */
13779
13780 static tree
13781 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13782 {
13783   cp_decl_specifier_seq type_specifier_seq;
13784   cp_declarator *abstract_declarator;
13785
13786   /* Parse the type-specifier-seq.  */
13787   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13788                                 &type_specifier_seq);
13789   if (type_specifier_seq.type == error_mark_node)
13790     return error_mark_node;
13791
13792   /* There might or might not be an abstract declarator.  */
13793   cp_parser_parse_tentatively (parser);
13794   /* Look for the declarator.  */
13795   abstract_declarator
13796     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13797                             /*parenthesized_p=*/NULL,
13798                             /*member_p=*/false);
13799   /* Check to see if there really was a declarator.  */
13800   if (!cp_parser_parse_definitely (parser))
13801     abstract_declarator = NULL;
13802
13803   if (type_specifier_seq.type
13804       && type_uses_auto (type_specifier_seq.type))
13805     {
13806       error ("invalid use of %<auto%>");
13807       return error_mark_node;
13808     }
13809   
13810   return groktypename (&type_specifier_seq, abstract_declarator,
13811                        is_template_arg);
13812 }
13813
13814 static tree cp_parser_type_id (cp_parser *parser)
13815 {
13816   return cp_parser_type_id_1 (parser, false);
13817 }
13818
13819 static tree cp_parser_template_type_arg (cp_parser *parser)
13820 {
13821   return cp_parser_type_id_1 (parser, true);
13822 }
13823
13824 /* Parse a type-specifier-seq.
13825
13826    type-specifier-seq:
13827      type-specifier type-specifier-seq [opt]
13828
13829    GNU extension:
13830
13831    type-specifier-seq:
13832      attributes type-specifier-seq [opt]
13833
13834    If IS_CONDITION is true, we are at the start of a "condition",
13835    e.g., we've just seen "if (".
13836
13837    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13838
13839 static void
13840 cp_parser_type_specifier_seq (cp_parser* parser,
13841                               bool is_condition,
13842                               cp_decl_specifier_seq *type_specifier_seq)
13843 {
13844   bool seen_type_specifier = false;
13845   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13846   cp_token *start_token = NULL;
13847
13848   /* Clear the TYPE_SPECIFIER_SEQ.  */
13849   clear_decl_specs (type_specifier_seq);
13850
13851   /* Parse the type-specifiers and attributes.  */
13852   while (true)
13853     {
13854       tree type_specifier;
13855       bool is_cv_qualifier;
13856
13857       /* Check for attributes first.  */
13858       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13859         {
13860           type_specifier_seq->attributes =
13861             chainon (type_specifier_seq->attributes,
13862                      cp_parser_attributes_opt (parser));
13863           continue;
13864         }
13865
13866       /* record the token of the beginning of the type specifier seq,
13867          for error reporting purposes*/
13868      if (!start_token)
13869        start_token = cp_lexer_peek_token (parser->lexer);
13870
13871       /* Look for the type-specifier.  */
13872       type_specifier = cp_parser_type_specifier (parser,
13873                                                  flags,
13874                                                  type_specifier_seq,
13875                                                  /*is_declaration=*/false,
13876                                                  NULL,
13877                                                  &is_cv_qualifier);
13878       if (!type_specifier)
13879         {
13880           /* If the first type-specifier could not be found, this is not a
13881              type-specifier-seq at all.  */
13882           if (!seen_type_specifier)
13883             {
13884               cp_parser_error (parser, "expected type-specifier");
13885               type_specifier_seq->type = error_mark_node;
13886               return;
13887             }
13888           /* If subsequent type-specifiers could not be found, the
13889              type-specifier-seq is complete.  */
13890           break;
13891         }
13892
13893       seen_type_specifier = true;
13894       /* The standard says that a condition can be:
13895
13896             type-specifier-seq declarator = assignment-expression
13897
13898          However, given:
13899
13900            struct S {};
13901            if (int S = ...)
13902
13903          we should treat the "S" as a declarator, not as a
13904          type-specifier.  The standard doesn't say that explicitly for
13905          type-specifier-seq, but it does say that for
13906          decl-specifier-seq in an ordinary declaration.  Perhaps it
13907          would be clearer just to allow a decl-specifier-seq here, and
13908          then add a semantic restriction that if any decl-specifiers
13909          that are not type-specifiers appear, the program is invalid.  */
13910       if (is_condition && !is_cv_qualifier)
13911         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13912     }
13913
13914   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13915 }
13916
13917 /* Parse a parameter-declaration-clause.
13918
13919    parameter-declaration-clause:
13920      parameter-declaration-list [opt] ... [opt]
13921      parameter-declaration-list , ...
13922
13923    Returns a representation for the parameter declarations.  A return
13924    value of NULL indicates a parameter-declaration-clause consisting
13925    only of an ellipsis.  */
13926
13927 static tree
13928 cp_parser_parameter_declaration_clause (cp_parser* parser)
13929 {
13930   tree parameters;
13931   cp_token *token;
13932   bool ellipsis_p;
13933   bool is_error;
13934
13935   /* Peek at the next token.  */
13936   token = cp_lexer_peek_token (parser->lexer);
13937   /* Check for trivial parameter-declaration-clauses.  */
13938   if (token->type == CPP_ELLIPSIS)
13939     {
13940       /* Consume the `...' token.  */
13941       cp_lexer_consume_token (parser->lexer);
13942       return NULL_TREE;
13943     }
13944   else if (token->type == CPP_CLOSE_PAREN)
13945     /* There are no parameters.  */
13946     {
13947 #ifndef NO_IMPLICIT_EXTERN_C
13948       if (in_system_header && current_class_type == NULL
13949           && current_lang_name == lang_name_c)
13950         return NULL_TREE;
13951       else
13952 #endif
13953         return void_list_node;
13954     }
13955   /* Check for `(void)', too, which is a special case.  */
13956   else if (token->keyword == RID_VOID
13957            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13958                == CPP_CLOSE_PAREN))
13959     {
13960       /* Consume the `void' token.  */
13961       cp_lexer_consume_token (parser->lexer);
13962       /* There are no parameters.  */
13963       return void_list_node;
13964     }
13965
13966   /* Parse the parameter-declaration-list.  */
13967   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13968   /* If a parse error occurred while parsing the
13969      parameter-declaration-list, then the entire
13970      parameter-declaration-clause is erroneous.  */
13971   if (is_error)
13972     return NULL;
13973
13974   /* Peek at the next token.  */
13975   token = cp_lexer_peek_token (parser->lexer);
13976   /* If it's a `,', the clause should terminate with an ellipsis.  */
13977   if (token->type == CPP_COMMA)
13978     {
13979       /* Consume the `,'.  */
13980       cp_lexer_consume_token (parser->lexer);
13981       /* Expect an ellipsis.  */
13982       ellipsis_p
13983         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13984     }
13985   /* It might also be `...' if the optional trailing `,' was
13986      omitted.  */
13987   else if (token->type == CPP_ELLIPSIS)
13988     {
13989       /* Consume the `...' token.  */
13990       cp_lexer_consume_token (parser->lexer);
13991       /* And remember that we saw it.  */
13992       ellipsis_p = true;
13993     }
13994   else
13995     ellipsis_p = false;
13996
13997   /* Finish the parameter list.  */
13998   if (!ellipsis_p)
13999     parameters = chainon (parameters, void_list_node);
14000
14001   return parameters;
14002 }
14003
14004 /* Parse a parameter-declaration-list.
14005
14006    parameter-declaration-list:
14007      parameter-declaration
14008      parameter-declaration-list , parameter-declaration
14009
14010    Returns a representation of the parameter-declaration-list, as for
14011    cp_parser_parameter_declaration_clause.  However, the
14012    `void_list_node' is never appended to the list.  Upon return,
14013    *IS_ERROR will be true iff an error occurred.  */
14014
14015 static tree
14016 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14017 {
14018   tree parameters = NULL_TREE;
14019   tree *tail = &parameters; 
14020   bool saved_in_unbraced_linkage_specification_p;
14021
14022   /* Assume all will go well.  */
14023   *is_error = false;
14024   /* The special considerations that apply to a function within an
14025      unbraced linkage specifications do not apply to the parameters
14026      to the function.  */
14027   saved_in_unbraced_linkage_specification_p 
14028     = parser->in_unbraced_linkage_specification_p;
14029   parser->in_unbraced_linkage_specification_p = false;
14030
14031   /* Look for more parameters.  */
14032   while (true)
14033     {
14034       cp_parameter_declarator *parameter;
14035       tree decl = error_mark_node;
14036       bool parenthesized_p;
14037       /* Parse the parameter.  */
14038       parameter
14039         = cp_parser_parameter_declaration (parser,
14040                                            /*template_parm_p=*/false,
14041                                            &parenthesized_p);
14042
14043       /* We don't know yet if the enclosing context is deprecated, so wait
14044          and warn in grokparms if appropriate.  */
14045       deprecated_state = DEPRECATED_SUPPRESS;
14046
14047       if (parameter)
14048         decl = grokdeclarator (parameter->declarator,
14049                                &parameter->decl_specifiers,
14050                                PARM,
14051                                parameter->default_argument != NULL_TREE,
14052                                &parameter->decl_specifiers.attributes);
14053
14054       deprecated_state = DEPRECATED_NORMAL;
14055
14056       /* If a parse error occurred parsing the parameter declaration,
14057          then the entire parameter-declaration-list is erroneous.  */
14058       if (decl == error_mark_node)
14059         {
14060           *is_error = true;
14061           parameters = error_mark_node;
14062           break;
14063         }
14064
14065       if (parameter->decl_specifiers.attributes)
14066         cplus_decl_attributes (&decl,
14067                                parameter->decl_specifiers.attributes,
14068                                0);
14069       if (DECL_NAME (decl))
14070         decl = pushdecl (decl);
14071
14072       /* Add the new parameter to the list.  */
14073       *tail = build_tree_list (parameter->default_argument, decl);
14074       tail = &TREE_CHAIN (*tail);
14075
14076       /* Peek at the next token.  */
14077       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14078           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14079           /* These are for Objective-C++ */
14080           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14081           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14082         /* The parameter-declaration-list is complete.  */
14083         break;
14084       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14085         {
14086           cp_token *token;
14087
14088           /* Peek at the next token.  */
14089           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14090           /* If it's an ellipsis, then the list is complete.  */
14091           if (token->type == CPP_ELLIPSIS)
14092             break;
14093           /* Otherwise, there must be more parameters.  Consume the
14094              `,'.  */
14095           cp_lexer_consume_token (parser->lexer);
14096           /* When parsing something like:
14097
14098                 int i(float f, double d)
14099
14100              we can tell after seeing the declaration for "f" that we
14101              are not looking at an initialization of a variable "i",
14102              but rather at the declaration of a function "i".
14103
14104              Due to the fact that the parsing of template arguments
14105              (as specified to a template-id) requires backtracking we
14106              cannot use this technique when inside a template argument
14107              list.  */
14108           if (!parser->in_template_argument_list_p
14109               && !parser->in_type_id_in_expr_p
14110               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14111               /* However, a parameter-declaration of the form
14112                  "foat(f)" (which is a valid declaration of a
14113                  parameter "f") can also be interpreted as an
14114                  expression (the conversion of "f" to "float").  */
14115               && !parenthesized_p)
14116             cp_parser_commit_to_tentative_parse (parser);
14117         }
14118       else
14119         {
14120           cp_parser_error (parser, "expected %<,%> or %<...%>");
14121           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14122             cp_parser_skip_to_closing_parenthesis (parser,
14123                                                    /*recovering=*/true,
14124                                                    /*or_comma=*/false,
14125                                                    /*consume_paren=*/false);
14126           break;
14127         }
14128     }
14129
14130   parser->in_unbraced_linkage_specification_p
14131     = saved_in_unbraced_linkage_specification_p;
14132
14133   return parameters;
14134 }
14135
14136 /* Parse a parameter declaration.
14137
14138    parameter-declaration:
14139      decl-specifier-seq ... [opt] declarator
14140      decl-specifier-seq declarator = assignment-expression
14141      decl-specifier-seq ... [opt] abstract-declarator [opt]
14142      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14143
14144    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14145    declares a template parameter.  (In that case, a non-nested `>'
14146    token encountered during the parsing of the assignment-expression
14147    is not interpreted as a greater-than operator.)
14148
14149    Returns a representation of the parameter, or NULL if an error
14150    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14151    true iff the declarator is of the form "(p)".  */
14152
14153 static cp_parameter_declarator *
14154 cp_parser_parameter_declaration (cp_parser *parser,
14155                                  bool template_parm_p,
14156                                  bool *parenthesized_p)
14157 {
14158   int declares_class_or_enum;
14159   bool greater_than_is_operator_p;
14160   cp_decl_specifier_seq decl_specifiers;
14161   cp_declarator *declarator;
14162   tree default_argument;
14163   cp_token *token = NULL, *declarator_token_start = NULL;
14164   const char *saved_message;
14165
14166   /* In a template parameter, `>' is not an operator.
14167
14168      [temp.param]
14169
14170      When parsing a default template-argument for a non-type
14171      template-parameter, the first non-nested `>' is taken as the end
14172      of the template parameter-list rather than a greater-than
14173      operator.  */
14174   greater_than_is_operator_p = !template_parm_p;
14175
14176   /* Type definitions may not appear in parameter types.  */
14177   saved_message = parser->type_definition_forbidden_message;
14178   parser->type_definition_forbidden_message
14179     = "types may not be defined in parameter types";
14180
14181   /* Parse the declaration-specifiers.  */
14182   cp_parser_decl_specifier_seq (parser,
14183                                 CP_PARSER_FLAGS_NONE,
14184                                 &decl_specifiers,
14185                                 &declares_class_or_enum);
14186   /* If an error occurred, there's no reason to attempt to parse the
14187      rest of the declaration.  */
14188   if (cp_parser_error_occurred (parser))
14189     {
14190       parser->type_definition_forbidden_message = saved_message;
14191       return NULL;
14192     }
14193
14194   /* Peek at the next token.  */
14195   token = cp_lexer_peek_token (parser->lexer);
14196
14197   /* If the next token is a `)', `,', `=', `>', or `...', then there
14198      is no declarator. However, when variadic templates are enabled,
14199      there may be a declarator following `...'.  */
14200   if (token->type == CPP_CLOSE_PAREN
14201       || token->type == CPP_COMMA
14202       || token->type == CPP_EQ
14203       || token->type == CPP_GREATER)
14204     {
14205       declarator = NULL;
14206       if (parenthesized_p)
14207         *parenthesized_p = false;
14208     }
14209   /* Otherwise, there should be a declarator.  */
14210   else
14211     {
14212       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14213       parser->default_arg_ok_p = false;
14214
14215       /* After seeing a decl-specifier-seq, if the next token is not a
14216          "(", there is no possibility that the code is a valid
14217          expression.  Therefore, if parsing tentatively, we commit at
14218          this point.  */
14219       if (!parser->in_template_argument_list_p
14220           /* In an expression context, having seen:
14221
14222                (int((char ...
14223
14224              we cannot be sure whether we are looking at a
14225              function-type (taking a "char" as a parameter) or a cast
14226              of some object of type "char" to "int".  */
14227           && !parser->in_type_id_in_expr_p
14228           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14229           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14230         cp_parser_commit_to_tentative_parse (parser);
14231       /* Parse the declarator.  */
14232       declarator_token_start = token;
14233       declarator = cp_parser_declarator (parser,
14234                                          CP_PARSER_DECLARATOR_EITHER,
14235                                          /*ctor_dtor_or_conv_p=*/NULL,
14236                                          parenthesized_p,
14237                                          /*member_p=*/false);
14238       parser->default_arg_ok_p = saved_default_arg_ok_p;
14239       /* After the declarator, allow more attributes.  */
14240       decl_specifiers.attributes
14241         = chainon (decl_specifiers.attributes,
14242                    cp_parser_attributes_opt (parser));
14243     }
14244
14245   /* If the next token is an ellipsis, and we have not seen a
14246      declarator name, and the type of the declarator contains parameter
14247      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14248      a parameter pack expansion expression. Otherwise, leave the
14249      ellipsis for a C-style variadic function. */
14250   token = cp_lexer_peek_token (parser->lexer);
14251   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14252     {
14253       tree type = decl_specifiers.type;
14254
14255       if (type && DECL_P (type))
14256         type = TREE_TYPE (type);
14257
14258       if (type
14259           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14260           && declarator_can_be_parameter_pack (declarator)
14261           && (!declarator || !declarator->parameter_pack_p)
14262           && uses_parameter_packs (type))
14263         {
14264           /* Consume the `...'. */
14265           cp_lexer_consume_token (parser->lexer);
14266           maybe_warn_variadic_templates ();
14267           
14268           /* Build a pack expansion type */
14269           if (declarator)
14270             declarator->parameter_pack_p = true;
14271           else
14272             decl_specifiers.type = make_pack_expansion (type);
14273         }
14274     }
14275
14276   /* The restriction on defining new types applies only to the type
14277      of the parameter, not to the default argument.  */
14278   parser->type_definition_forbidden_message = saved_message;
14279
14280   /* If the next token is `=', then process a default argument.  */
14281   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14282     {
14283       /* Consume the `='.  */
14284       cp_lexer_consume_token (parser->lexer);
14285
14286       /* If we are defining a class, then the tokens that make up the
14287          default argument must be saved and processed later.  */
14288       if (!template_parm_p && at_class_scope_p ()
14289           && TYPE_BEING_DEFINED (current_class_type))
14290         {
14291           unsigned depth = 0;
14292           int maybe_template_id = 0;
14293           cp_token *first_token;
14294           cp_token *token;
14295
14296           /* Add tokens until we have processed the entire default
14297              argument.  We add the range [first_token, token).  */
14298           first_token = cp_lexer_peek_token (parser->lexer);
14299           while (true)
14300             {
14301               bool done = false;
14302
14303               /* Peek at the next token.  */
14304               token = cp_lexer_peek_token (parser->lexer);
14305               /* What we do depends on what token we have.  */
14306               switch (token->type)
14307                 {
14308                   /* In valid code, a default argument must be
14309                      immediately followed by a `,' `)', or `...'.  */
14310                 case CPP_COMMA:
14311                   if (depth == 0 && maybe_template_id)
14312                     {
14313                       /* If we've seen a '<', we might be in a
14314                          template-argument-list.  Until Core issue 325 is
14315                          resolved, we don't know how this situation ought
14316                          to be handled, so try to DTRT.  We check whether
14317                          what comes after the comma is a valid parameter
14318                          declaration list.  If it is, then the comma ends
14319                          the default argument; otherwise the default
14320                          argument continues.  */
14321                       bool error = false;
14322
14323                       /* Set ITALP so cp_parser_parameter_declaration_list
14324                          doesn't decide to commit to this parse.  */
14325                       bool saved_italp = parser->in_template_argument_list_p;
14326                       parser->in_template_argument_list_p = true;
14327
14328                       cp_parser_parse_tentatively (parser);
14329                       cp_lexer_consume_token (parser->lexer);
14330                       cp_parser_parameter_declaration_list (parser, &error);
14331                       if (!cp_parser_error_occurred (parser) && !error)
14332                         done = true;
14333                       cp_parser_abort_tentative_parse (parser);
14334
14335                       parser->in_template_argument_list_p = saved_italp;
14336                       break;
14337                     }
14338                 case CPP_CLOSE_PAREN:
14339                 case CPP_ELLIPSIS:
14340                   /* If we run into a non-nested `;', `}', or `]',
14341                      then the code is invalid -- but the default
14342                      argument is certainly over.  */
14343                 case CPP_SEMICOLON:
14344                 case CPP_CLOSE_BRACE:
14345                 case CPP_CLOSE_SQUARE:
14346                   if (depth == 0)
14347                     done = true;
14348                   /* Update DEPTH, if necessary.  */
14349                   else if (token->type == CPP_CLOSE_PAREN
14350                            || token->type == CPP_CLOSE_BRACE
14351                            || token->type == CPP_CLOSE_SQUARE)
14352                     --depth;
14353                   break;
14354
14355                 case CPP_OPEN_PAREN:
14356                 case CPP_OPEN_SQUARE:
14357                 case CPP_OPEN_BRACE:
14358                   ++depth;
14359                   break;
14360
14361                 case CPP_LESS:
14362                   if (depth == 0)
14363                     /* This might be the comparison operator, or it might
14364                        start a template argument list.  */
14365                     ++maybe_template_id;
14366                   break;
14367
14368                 case CPP_RSHIFT:
14369                   if (cxx_dialect == cxx98)
14370                     break;
14371                   /* Fall through for C++0x, which treats the `>>'
14372                      operator like two `>' tokens in certain
14373                      cases.  */
14374
14375                 case CPP_GREATER:
14376                   if (depth == 0)
14377                     {
14378                       /* This might be an operator, or it might close a
14379                          template argument list.  But if a previous '<'
14380                          started a template argument list, this will have
14381                          closed it, so we can't be in one anymore.  */
14382                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14383                       if (maybe_template_id < 0)
14384                         maybe_template_id = 0;
14385                     }
14386                   break;
14387
14388                   /* If we run out of tokens, issue an error message.  */
14389                 case CPP_EOF:
14390                 case CPP_PRAGMA_EOL:
14391                   error ("%Hfile ends in default argument", &token->location);
14392                   done = true;
14393                   break;
14394
14395                 case CPP_NAME:
14396                 case CPP_SCOPE:
14397                   /* In these cases, we should look for template-ids.
14398                      For example, if the default argument is
14399                      `X<int, double>()', we need to do name lookup to
14400                      figure out whether or not `X' is a template; if
14401                      so, the `,' does not end the default argument.
14402
14403                      That is not yet done.  */
14404                   break;
14405
14406                 default:
14407                   break;
14408                 }
14409
14410               /* If we've reached the end, stop.  */
14411               if (done)
14412                 break;
14413
14414               /* Add the token to the token block.  */
14415               token = cp_lexer_consume_token (parser->lexer);
14416             }
14417
14418           /* Create a DEFAULT_ARG to represent the unparsed default
14419              argument.  */
14420           default_argument = make_node (DEFAULT_ARG);
14421           DEFARG_TOKENS (default_argument)
14422             = cp_token_cache_new (first_token, token);
14423           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14424         }
14425       /* Outside of a class definition, we can just parse the
14426          assignment-expression.  */
14427       else
14428         {
14429           token = cp_lexer_peek_token (parser->lexer);
14430           default_argument 
14431             = cp_parser_default_argument (parser, template_parm_p);
14432         }
14433
14434       if (!parser->default_arg_ok_p)
14435         {
14436           if (flag_permissive)
14437             warning (0, "deprecated use of default argument for parameter of non-function");
14438           else
14439             {
14440               error ("%Hdefault arguments are only "
14441                      "permitted for function parameters",
14442                      &token->location);
14443               default_argument = NULL_TREE;
14444             }
14445         }
14446       else if ((declarator && declarator->parameter_pack_p)
14447                || (decl_specifiers.type
14448                    && PACK_EXPANSION_P (decl_specifiers.type)))
14449         {
14450           const char* kind = template_parm_p? "template " : "";
14451           
14452           /* Find the name of the parameter pack.  */     
14453           cp_declarator *id_declarator = declarator;
14454           while (id_declarator && id_declarator->kind != cdk_id)
14455             id_declarator = id_declarator->declarator;
14456           
14457           if (id_declarator && id_declarator->kind == cdk_id)
14458             error ("%H%sparameter pack %qD cannot have a default argument",
14459                    &declarator_token_start->location,
14460                    kind, id_declarator->u.id.unqualified_name);
14461           else
14462             error ("%H%sparameter pack cannot have a default argument",
14463                    &declarator_token_start->location, kind);
14464           
14465           default_argument = NULL_TREE;
14466         }
14467     }
14468   else
14469     default_argument = NULL_TREE;
14470
14471   return make_parameter_declarator (&decl_specifiers,
14472                                     declarator,
14473                                     default_argument);
14474 }
14475
14476 /* Parse a default argument and return it.
14477
14478    TEMPLATE_PARM_P is true if this is a default argument for a
14479    non-type template parameter.  */
14480 static tree
14481 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14482 {
14483   tree default_argument = NULL_TREE;
14484   bool saved_greater_than_is_operator_p;
14485   bool saved_local_variables_forbidden_p;
14486
14487   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14488      set correctly.  */
14489   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14490   parser->greater_than_is_operator_p = !template_parm_p;
14491   /* Local variable names (and the `this' keyword) may not
14492      appear in a default argument.  */
14493   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14494   parser->local_variables_forbidden_p = true;
14495   /* The default argument expression may cause implicitly
14496      defined member functions to be synthesized, which will
14497      result in garbage collection.  We must treat this
14498      situation as if we were within the body of function so as
14499      to avoid collecting live data on the stack.  */
14500   ++function_depth;
14501   /* Parse the assignment-expression.  */
14502   if (template_parm_p)
14503     push_deferring_access_checks (dk_no_deferred);
14504   default_argument
14505     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14506   if (template_parm_p)
14507     pop_deferring_access_checks ();
14508   /* Restore saved state.  */
14509   --function_depth;
14510   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14511   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14512
14513   return default_argument;
14514 }
14515
14516 /* Parse a function-body.
14517
14518    function-body:
14519      compound_statement  */
14520
14521 static void
14522 cp_parser_function_body (cp_parser *parser)
14523 {
14524   cp_parser_compound_statement (parser, NULL, false);
14525 }
14526
14527 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14528    true if a ctor-initializer was present.  */
14529
14530 static bool
14531 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14532 {
14533   tree body;
14534   bool ctor_initializer_p;
14535
14536   /* Begin the function body.  */
14537   body = begin_function_body ();
14538   /* Parse the optional ctor-initializer.  */
14539   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14540   /* Parse the function-body.  */
14541   cp_parser_function_body (parser);
14542   /* Finish the function body.  */
14543   finish_function_body (body);
14544
14545   return ctor_initializer_p;
14546 }
14547
14548 /* Parse an initializer.
14549
14550    initializer:
14551      = initializer-clause
14552      ( expression-list )
14553
14554    Returns an expression representing the initializer.  If no
14555    initializer is present, NULL_TREE is returned.
14556
14557    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14558    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14559    set to TRUE if there is no initializer present.  If there is an
14560    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14561    is set to true; otherwise it is set to false.  */
14562
14563 static tree
14564 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14565                        bool* non_constant_p)
14566 {
14567   cp_token *token;
14568   tree init;
14569
14570   /* Peek at the next token.  */
14571   token = cp_lexer_peek_token (parser->lexer);
14572
14573   /* Let our caller know whether or not this initializer was
14574      parenthesized.  */
14575   *is_direct_init = (token->type != CPP_EQ);
14576   /* Assume that the initializer is constant.  */
14577   *non_constant_p = false;
14578
14579   if (token->type == CPP_EQ)
14580     {
14581       /* Consume the `='.  */
14582       cp_lexer_consume_token (parser->lexer);
14583       /* Parse the initializer-clause.  */
14584       init = cp_parser_initializer_clause (parser, non_constant_p);
14585     }
14586   else if (token->type == CPP_OPEN_PAREN)
14587     init = cp_parser_parenthesized_expression_list (parser, false,
14588                                                     /*cast_p=*/false,
14589                                                     /*allow_expansion_p=*/true,
14590                                                     non_constant_p);
14591   else if (token->type == CPP_OPEN_BRACE)
14592     {
14593       maybe_warn_cpp0x ("extended initializer lists");
14594       init = cp_parser_braced_list (parser, non_constant_p);
14595       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14596     }
14597   else
14598     {
14599       /* Anything else is an error.  */
14600       cp_parser_error (parser, "expected initializer");
14601       init = error_mark_node;
14602     }
14603
14604   return init;
14605 }
14606
14607 /* Parse an initializer-clause.
14608
14609    initializer-clause:
14610      assignment-expression
14611      braced-init-list
14612
14613    Returns an expression representing the initializer.
14614
14615    If the `assignment-expression' production is used the value
14616    returned is simply a representation for the expression.
14617
14618    Otherwise, calls cp_parser_braced_list.  */
14619
14620 static tree
14621 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14622 {
14623   tree initializer;
14624
14625   /* Assume the expression is constant.  */
14626   *non_constant_p = false;
14627
14628   /* If it is not a `{', then we are looking at an
14629      assignment-expression.  */
14630   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14631     {
14632       initializer
14633         = cp_parser_constant_expression (parser,
14634                                         /*allow_non_constant_p=*/true,
14635                                         non_constant_p);
14636       if (!*non_constant_p)
14637         initializer = fold_non_dependent_expr (initializer);
14638     }
14639   else
14640     initializer = cp_parser_braced_list (parser, non_constant_p);
14641
14642   return initializer;
14643 }
14644
14645 /* Parse a brace-enclosed initializer list.
14646
14647    braced-init-list:
14648      { initializer-list , [opt] }
14649      { }
14650
14651    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14652    the elements of the initializer-list (or NULL, if the last
14653    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14654    NULL_TREE.  There is no way to detect whether or not the optional
14655    trailing `,' was provided.  NON_CONSTANT_P is as for
14656    cp_parser_initializer.  */     
14657
14658 static tree
14659 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14660 {
14661   tree initializer;
14662
14663   /* Consume the `{' token.  */
14664   cp_lexer_consume_token (parser->lexer);
14665   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14666   initializer = make_node (CONSTRUCTOR);
14667   /* If it's not a `}', then there is a non-trivial initializer.  */
14668   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14669     {
14670       /* Parse the initializer list.  */
14671       CONSTRUCTOR_ELTS (initializer)
14672         = cp_parser_initializer_list (parser, non_constant_p);
14673       /* A trailing `,' token is allowed.  */
14674       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14675         cp_lexer_consume_token (parser->lexer);
14676     }
14677   /* Now, there should be a trailing `}'.  */
14678   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14679   TREE_TYPE (initializer) = init_list_type_node;
14680   return initializer;
14681 }
14682
14683 /* Parse an initializer-list.
14684
14685    initializer-list:
14686      initializer-clause ... [opt]
14687      initializer-list , initializer-clause ... [opt]
14688
14689    GNU Extension:
14690
14691    initializer-list:
14692      identifier : initializer-clause
14693      initializer-list, identifier : initializer-clause
14694
14695    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14696    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14697    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14698    as for cp_parser_initializer.  */
14699
14700 static VEC(constructor_elt,gc) *
14701 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14702 {
14703   VEC(constructor_elt,gc) *v = NULL;
14704
14705   /* Assume all of the expressions are constant.  */
14706   *non_constant_p = false;
14707
14708   /* Parse the rest of the list.  */
14709   while (true)
14710     {
14711       cp_token *token;
14712       tree identifier;
14713       tree initializer;
14714       bool clause_non_constant_p;
14715
14716       /* If the next token is an identifier and the following one is a
14717          colon, we are looking at the GNU designated-initializer
14718          syntax.  */
14719       if (cp_parser_allow_gnu_extensions_p (parser)
14720           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14721           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14722         {
14723           /* Warn the user that they are using an extension.  */
14724           pedwarn (input_location, OPT_pedantic, 
14725                    "ISO C++ does not allow designated initializers");
14726           /* Consume the identifier.  */
14727           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14728           /* Consume the `:'.  */
14729           cp_lexer_consume_token (parser->lexer);
14730         }
14731       else
14732         identifier = NULL_TREE;
14733
14734       /* Parse the initializer.  */
14735       initializer = cp_parser_initializer_clause (parser,
14736                                                   &clause_non_constant_p);
14737       /* If any clause is non-constant, so is the entire initializer.  */
14738       if (clause_non_constant_p)
14739         *non_constant_p = true;
14740
14741       /* If we have an ellipsis, this is an initializer pack
14742          expansion.  */
14743       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14744         {
14745           /* Consume the `...'.  */
14746           cp_lexer_consume_token (parser->lexer);
14747
14748           /* Turn the initializer into an initializer expansion.  */
14749           initializer = make_pack_expansion (initializer);
14750         }
14751
14752       /* Add it to the vector.  */
14753       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14754
14755       /* If the next token is not a comma, we have reached the end of
14756          the list.  */
14757       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14758         break;
14759
14760       /* Peek at the next token.  */
14761       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14762       /* If the next token is a `}', then we're still done.  An
14763          initializer-clause can have a trailing `,' after the
14764          initializer-list and before the closing `}'.  */
14765       if (token->type == CPP_CLOSE_BRACE)
14766         break;
14767
14768       /* Consume the `,' token.  */
14769       cp_lexer_consume_token (parser->lexer);
14770     }
14771
14772   return v;
14773 }
14774
14775 /* Classes [gram.class] */
14776
14777 /* Parse a class-name.
14778
14779    class-name:
14780      identifier
14781      template-id
14782
14783    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14784    to indicate that names looked up in dependent types should be
14785    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14786    keyword has been used to indicate that the name that appears next
14787    is a template.  TAG_TYPE indicates the explicit tag given before
14788    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14789    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14790    is the class being defined in a class-head.
14791
14792    Returns the TYPE_DECL representing the class.  */
14793
14794 static tree
14795 cp_parser_class_name (cp_parser *parser,
14796                       bool typename_keyword_p,
14797                       bool template_keyword_p,
14798                       enum tag_types tag_type,
14799                       bool check_dependency_p,
14800                       bool class_head_p,
14801                       bool is_declaration)
14802 {
14803   tree decl;
14804   tree scope;
14805   bool typename_p;
14806   cp_token *token;
14807   tree identifier = NULL_TREE;
14808
14809   /* All class-names start with an identifier.  */
14810   token = cp_lexer_peek_token (parser->lexer);
14811   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14812     {
14813       cp_parser_error (parser, "expected class-name");
14814       return error_mark_node;
14815     }
14816
14817   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14818      to a template-id, so we save it here.  */
14819   scope = parser->scope;
14820   if (scope == error_mark_node)
14821     return error_mark_node;
14822
14823   /* Any name names a type if we're following the `typename' keyword
14824      in a qualified name where the enclosing scope is type-dependent.  */
14825   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14826                 && dependent_type_p (scope));
14827   /* Handle the common case (an identifier, but not a template-id)
14828      efficiently.  */
14829   if (token->type == CPP_NAME
14830       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14831     {
14832       cp_token *identifier_token;
14833       bool ambiguous_p;
14834
14835       /* Look for the identifier.  */
14836       identifier_token = cp_lexer_peek_token (parser->lexer);
14837       ambiguous_p = identifier_token->ambiguous_p;
14838       identifier = cp_parser_identifier (parser);
14839       /* If the next token isn't an identifier, we are certainly not
14840          looking at a class-name.  */
14841       if (identifier == error_mark_node)
14842         decl = error_mark_node;
14843       /* If we know this is a type-name, there's no need to look it
14844          up.  */
14845       else if (typename_p)
14846         decl = identifier;
14847       else
14848         {
14849           tree ambiguous_decls;
14850           /* If we already know that this lookup is ambiguous, then
14851              we've already issued an error message; there's no reason
14852              to check again.  */
14853           if (ambiguous_p)
14854             {
14855               cp_parser_simulate_error (parser);
14856               return error_mark_node;
14857             }
14858           /* If the next token is a `::', then the name must be a type
14859              name.
14860
14861              [basic.lookup.qual]
14862
14863              During the lookup for a name preceding the :: scope
14864              resolution operator, object, function, and enumerator
14865              names are ignored.  */
14866           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14867             tag_type = typename_type;
14868           /* Look up the name.  */
14869           decl = cp_parser_lookup_name (parser, identifier,
14870                                         tag_type,
14871                                         /*is_template=*/false,
14872                                         /*is_namespace=*/false,
14873                                         check_dependency_p,
14874                                         &ambiguous_decls,
14875                                         identifier_token->location);
14876           if (ambiguous_decls)
14877             {
14878               error ("%Hreference to %qD is ambiguous",
14879                      &identifier_token->location, identifier);
14880               print_candidates (ambiguous_decls);
14881               if (cp_parser_parsing_tentatively (parser))
14882                 {
14883                   identifier_token->ambiguous_p = true;
14884                   cp_parser_simulate_error (parser);
14885                 }
14886               return error_mark_node;
14887             }
14888         }
14889     }
14890   else
14891     {
14892       /* Try a template-id.  */
14893       decl = cp_parser_template_id (parser, template_keyword_p,
14894                                     check_dependency_p,
14895                                     is_declaration);
14896       if (decl == error_mark_node)
14897         return error_mark_node;
14898     }
14899
14900   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14901
14902   /* If this is a typename, create a TYPENAME_TYPE.  */
14903   if (typename_p && decl != error_mark_node)
14904     {
14905       decl = make_typename_type (scope, decl, typename_type,
14906                                  /*complain=*/tf_error);
14907       if (decl != error_mark_node)
14908         decl = TYPE_NAME (decl);
14909     }
14910
14911   /* Check to see that it is really the name of a class.  */
14912   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14913       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14914       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14915     /* Situations like this:
14916
14917          template <typename T> struct A {
14918            typename T::template X<int>::I i;
14919          };
14920
14921        are problematic.  Is `T::template X<int>' a class-name?  The
14922        standard does not seem to be definitive, but there is no other
14923        valid interpretation of the following `::'.  Therefore, those
14924        names are considered class-names.  */
14925     {
14926       decl = make_typename_type (scope, decl, tag_type, tf_error);
14927       if (decl != error_mark_node)
14928         decl = TYPE_NAME (decl);
14929     }
14930   else if (TREE_CODE (decl) != TYPE_DECL
14931            || TREE_TYPE (decl) == error_mark_node
14932            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14933     decl = error_mark_node;
14934
14935   if (decl == error_mark_node)
14936     cp_parser_error (parser, "expected class-name");
14937   else if (identifier && !parser->scope)
14938     maybe_note_name_used_in_class (identifier, decl);
14939
14940   return decl;
14941 }
14942
14943 /* Parse a class-specifier.
14944
14945    class-specifier:
14946      class-head { member-specification [opt] }
14947
14948    Returns the TREE_TYPE representing the class.  */
14949
14950 static tree
14951 cp_parser_class_specifier (cp_parser* parser)
14952 {
14953   tree type;
14954   tree attributes = NULL_TREE;
14955   bool nested_name_specifier_p;
14956   unsigned saved_num_template_parameter_lists;
14957   bool saved_in_function_body;
14958   bool saved_in_unbraced_linkage_specification_p;
14959   tree old_scope = NULL_TREE;
14960   tree scope = NULL_TREE;
14961   tree bases;
14962
14963   push_deferring_access_checks (dk_no_deferred);
14964
14965   /* Parse the class-head.  */
14966   type = cp_parser_class_head (parser,
14967                                &nested_name_specifier_p,
14968                                &attributes,
14969                                &bases);
14970   /* If the class-head was a semantic disaster, skip the entire body
14971      of the class.  */
14972   if (!type)
14973     {
14974       cp_parser_skip_to_end_of_block_or_statement (parser);
14975       pop_deferring_access_checks ();
14976       return error_mark_node;
14977     }
14978
14979   /* Look for the `{'.  */
14980   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14981     {
14982       pop_deferring_access_checks ();
14983       return error_mark_node;
14984     }
14985
14986   /* Process the base classes. If they're invalid, skip the 
14987      entire class body.  */
14988   if (!xref_basetypes (type, bases))
14989     {
14990       /* Consuming the closing brace yields better error messages
14991          later on.  */
14992       if (cp_parser_skip_to_closing_brace (parser))
14993         cp_lexer_consume_token (parser->lexer);
14994       pop_deferring_access_checks ();
14995       return error_mark_node;
14996     }
14997
14998   /* Issue an error message if type-definitions are forbidden here.  */
14999   cp_parser_check_type_definition (parser);
15000   /* Remember that we are defining one more class.  */
15001   ++parser->num_classes_being_defined;
15002   /* Inside the class, surrounding template-parameter-lists do not
15003      apply.  */
15004   saved_num_template_parameter_lists
15005     = parser->num_template_parameter_lists;
15006   parser->num_template_parameter_lists = 0;
15007   /* We are not in a function body.  */
15008   saved_in_function_body = parser->in_function_body;
15009   parser->in_function_body = false;
15010   /* We are not immediately inside an extern "lang" block.  */
15011   saved_in_unbraced_linkage_specification_p
15012     = parser->in_unbraced_linkage_specification_p;
15013   parser->in_unbraced_linkage_specification_p = false;
15014
15015   /* Start the class.  */
15016   if (nested_name_specifier_p)
15017     {
15018       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15019       old_scope = push_inner_scope (scope);
15020     }
15021   type = begin_class_definition (type, attributes);
15022
15023   if (type == error_mark_node)
15024     /* If the type is erroneous, skip the entire body of the class.  */
15025     cp_parser_skip_to_closing_brace (parser);
15026   else
15027     /* Parse the member-specification.  */
15028     cp_parser_member_specification_opt (parser);
15029
15030   /* Look for the trailing `}'.  */
15031   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15032   /* Look for trailing attributes to apply to this class.  */
15033   if (cp_parser_allow_gnu_extensions_p (parser))
15034     attributes = cp_parser_attributes_opt (parser);
15035   if (type != error_mark_node)
15036     type = finish_struct (type, attributes);
15037   if (nested_name_specifier_p)
15038     pop_inner_scope (old_scope, scope);
15039   /* If this class is not itself within the scope of another class,
15040      then we need to parse the bodies of all of the queued function
15041      definitions.  Note that the queued functions defined in a class
15042      are not always processed immediately following the
15043      class-specifier for that class.  Consider:
15044
15045        struct A {
15046          struct B { void f() { sizeof (A); } };
15047        };
15048
15049      If `f' were processed before the processing of `A' were
15050      completed, there would be no way to compute the size of `A'.
15051      Note that the nesting we are interested in here is lexical --
15052      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15053      for:
15054
15055        struct A { struct B; };
15056        struct A::B { void f() { } };
15057
15058      there is no need to delay the parsing of `A::B::f'.  */
15059   if (--parser->num_classes_being_defined == 0)
15060     {
15061       tree queue_entry;
15062       tree fn;
15063       tree class_type = NULL_TREE;
15064       tree pushed_scope = NULL_TREE;
15065
15066       /* In a first pass, parse default arguments to the functions.
15067          Then, in a second pass, parse the bodies of the functions.
15068          This two-phased approach handles cases like:
15069
15070             struct S {
15071               void f() { g(); }
15072               void g(int i = 3);
15073             };
15074
15075          */
15076       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15077              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15078            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15079            TREE_PURPOSE (parser->unparsed_functions_queues)
15080              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15081         {
15082           fn = TREE_VALUE (queue_entry);
15083           /* If there are default arguments that have not yet been processed,
15084              take care of them now.  */
15085           if (class_type != TREE_PURPOSE (queue_entry))
15086             {
15087               if (pushed_scope)
15088                 pop_scope (pushed_scope);
15089               class_type = TREE_PURPOSE (queue_entry);
15090               pushed_scope = push_scope (class_type);
15091             }
15092           /* Make sure that any template parameters are in scope.  */
15093           maybe_begin_member_template_processing (fn);
15094           /* Parse the default argument expressions.  */
15095           cp_parser_late_parsing_default_args (parser, fn);
15096           /* Remove any template parameters from the symbol table.  */
15097           maybe_end_member_template_processing ();
15098         }
15099       if (pushed_scope)
15100         pop_scope (pushed_scope);
15101       /* Now parse the body of the functions.  */
15102       for (TREE_VALUE (parser->unparsed_functions_queues)
15103              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15104            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15105            TREE_VALUE (parser->unparsed_functions_queues)
15106              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15107         {
15108           /* Figure out which function we need to process.  */
15109           fn = TREE_VALUE (queue_entry);
15110           /* Parse the function.  */
15111           cp_parser_late_parsing_for_member (parser, fn);
15112         }
15113     }
15114
15115   /* Put back any saved access checks.  */
15116   pop_deferring_access_checks ();
15117
15118   /* Restore saved state.  */
15119   parser->in_function_body = saved_in_function_body;
15120   parser->num_template_parameter_lists
15121     = saved_num_template_parameter_lists;
15122   parser->in_unbraced_linkage_specification_p
15123     = saved_in_unbraced_linkage_specification_p;
15124
15125   return type;
15126 }
15127
15128 /* Parse a class-head.
15129
15130    class-head:
15131      class-key identifier [opt] base-clause [opt]
15132      class-key nested-name-specifier identifier base-clause [opt]
15133      class-key nested-name-specifier [opt] template-id
15134        base-clause [opt]
15135
15136    GNU Extensions:
15137      class-key attributes identifier [opt] base-clause [opt]
15138      class-key attributes nested-name-specifier identifier base-clause [opt]
15139      class-key attributes nested-name-specifier [opt] template-id
15140        base-clause [opt]
15141
15142    Upon return BASES is initialized to the list of base classes (or
15143    NULL, if there are none) in the same form returned by
15144    cp_parser_base_clause.
15145
15146    Returns the TYPE of the indicated class.  Sets
15147    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15148    involving a nested-name-specifier was used, and FALSE otherwise.
15149
15150    Returns error_mark_node if this is not a class-head.
15151
15152    Returns NULL_TREE if the class-head is syntactically valid, but
15153    semantically invalid in a way that means we should skip the entire
15154    body of the class.  */
15155
15156 static tree
15157 cp_parser_class_head (cp_parser* parser,
15158                       bool* nested_name_specifier_p,
15159                       tree *attributes_p,
15160                       tree *bases)
15161 {
15162   tree nested_name_specifier;
15163   enum tag_types class_key;
15164   tree id = NULL_TREE;
15165   tree type = NULL_TREE;
15166   tree attributes;
15167   bool template_id_p = false;
15168   bool qualified_p = false;
15169   bool invalid_nested_name_p = false;
15170   bool invalid_explicit_specialization_p = false;
15171   tree pushed_scope = NULL_TREE;
15172   unsigned num_templates;
15173   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15174   /* Assume no nested-name-specifier will be present.  */
15175   *nested_name_specifier_p = false;
15176   /* Assume no template parameter lists will be used in defining the
15177      type.  */
15178   num_templates = 0;
15179
15180   *bases = NULL_TREE;
15181
15182   /* Look for the class-key.  */
15183   class_key = cp_parser_class_key (parser);
15184   if (class_key == none_type)
15185     return error_mark_node;
15186
15187   /* Parse the attributes.  */
15188   attributes = cp_parser_attributes_opt (parser);
15189
15190   /* If the next token is `::', that is invalid -- but sometimes
15191      people do try to write:
15192
15193        struct ::S {};
15194
15195      Handle this gracefully by accepting the extra qualifier, and then
15196      issuing an error about it later if this really is a
15197      class-head.  If it turns out just to be an elaborated type
15198      specifier, remain silent.  */
15199   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15200     qualified_p = true;
15201
15202   push_deferring_access_checks (dk_no_check);
15203
15204   /* Determine the name of the class.  Begin by looking for an
15205      optional nested-name-specifier.  */
15206   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15207   nested_name_specifier
15208     = cp_parser_nested_name_specifier_opt (parser,
15209                                            /*typename_keyword_p=*/false,
15210                                            /*check_dependency_p=*/false,
15211                                            /*type_p=*/false,
15212                                            /*is_declaration=*/false);
15213   /* If there was a nested-name-specifier, then there *must* be an
15214      identifier.  */
15215   if (nested_name_specifier)
15216     {
15217       type_start_token = cp_lexer_peek_token (parser->lexer);
15218       /* Although the grammar says `identifier', it really means
15219          `class-name' or `template-name'.  You are only allowed to
15220          define a class that has already been declared with this
15221          syntax.
15222
15223          The proposed resolution for Core Issue 180 says that wherever
15224          you see `class T::X' you should treat `X' as a type-name.
15225
15226          It is OK to define an inaccessible class; for example:
15227
15228            class A { class B; };
15229            class A::B {};
15230
15231          We do not know if we will see a class-name, or a
15232          template-name.  We look for a class-name first, in case the
15233          class-name is a template-id; if we looked for the
15234          template-name first we would stop after the template-name.  */
15235       cp_parser_parse_tentatively (parser);
15236       type = cp_parser_class_name (parser,
15237                                    /*typename_keyword_p=*/false,
15238                                    /*template_keyword_p=*/false,
15239                                    class_type,
15240                                    /*check_dependency_p=*/false,
15241                                    /*class_head_p=*/true,
15242                                    /*is_declaration=*/false);
15243       /* If that didn't work, ignore the nested-name-specifier.  */
15244       if (!cp_parser_parse_definitely (parser))
15245         {
15246           invalid_nested_name_p = true;
15247           type_start_token = cp_lexer_peek_token (parser->lexer);
15248           id = cp_parser_identifier (parser);
15249           if (id == error_mark_node)
15250             id = NULL_TREE;
15251         }
15252       /* If we could not find a corresponding TYPE, treat this
15253          declaration like an unqualified declaration.  */
15254       if (type == error_mark_node)
15255         nested_name_specifier = NULL_TREE;
15256       /* Otherwise, count the number of templates used in TYPE and its
15257          containing scopes.  */
15258       else
15259         {
15260           tree scope;
15261
15262           for (scope = TREE_TYPE (type);
15263                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15264                scope = (TYPE_P (scope)
15265                         ? TYPE_CONTEXT (scope)
15266                         : DECL_CONTEXT (scope)))
15267             if (TYPE_P (scope)
15268                 && CLASS_TYPE_P (scope)
15269                 && CLASSTYPE_TEMPLATE_INFO (scope)
15270                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15271                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15272               ++num_templates;
15273         }
15274     }
15275   /* Otherwise, the identifier is optional.  */
15276   else
15277     {
15278       /* We don't know whether what comes next is a template-id,
15279          an identifier, or nothing at all.  */
15280       cp_parser_parse_tentatively (parser);
15281       /* Check for a template-id.  */
15282       type_start_token = cp_lexer_peek_token (parser->lexer);
15283       id = cp_parser_template_id (parser,
15284                                   /*template_keyword_p=*/false,
15285                                   /*check_dependency_p=*/true,
15286                                   /*is_declaration=*/true);
15287       /* If that didn't work, it could still be an identifier.  */
15288       if (!cp_parser_parse_definitely (parser))
15289         {
15290           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15291             {
15292               type_start_token = cp_lexer_peek_token (parser->lexer);
15293               id = cp_parser_identifier (parser);
15294             }
15295           else
15296             id = NULL_TREE;
15297         }
15298       else
15299         {
15300           template_id_p = true;
15301           ++num_templates;
15302         }
15303     }
15304
15305   pop_deferring_access_checks ();
15306
15307   if (id)
15308     cp_parser_check_for_invalid_template_id (parser, id,
15309                                              type_start_token->location);
15310
15311   /* If it's not a `:' or a `{' then we can't really be looking at a
15312      class-head, since a class-head only appears as part of a
15313      class-specifier.  We have to detect this situation before calling
15314      xref_tag, since that has irreversible side-effects.  */
15315   if (!cp_parser_next_token_starts_class_definition_p (parser))
15316     {
15317       cp_parser_error (parser, "expected %<{%> or %<:%>");
15318       return error_mark_node;
15319     }
15320
15321   /* At this point, we're going ahead with the class-specifier, even
15322      if some other problem occurs.  */
15323   cp_parser_commit_to_tentative_parse (parser);
15324   /* Issue the error about the overly-qualified name now.  */
15325   if (qualified_p)
15326     {
15327       cp_parser_error (parser,
15328                        "global qualification of class name is invalid");
15329       return error_mark_node;
15330     }
15331   else if (invalid_nested_name_p)
15332     {
15333       cp_parser_error (parser,
15334                        "qualified name does not name a class");
15335       return error_mark_node;
15336     }
15337   else if (nested_name_specifier)
15338     {
15339       tree scope;
15340
15341       /* Reject typedef-names in class heads.  */
15342       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15343         {
15344           error ("%Hinvalid class name in declaration of %qD",
15345                  &type_start_token->location, type);
15346           type = NULL_TREE;
15347           goto done;
15348         }
15349
15350       /* Figure out in what scope the declaration is being placed.  */
15351       scope = current_scope ();
15352       /* If that scope does not contain the scope in which the
15353          class was originally declared, the program is invalid.  */
15354       if (scope && !is_ancestor (scope, nested_name_specifier))
15355         {
15356           if (at_namespace_scope_p ())
15357             error ("%Hdeclaration of %qD in namespace %qD which does not "
15358                    "enclose %qD",
15359                    &type_start_token->location,
15360                    type, scope, nested_name_specifier);
15361           else
15362             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15363                    &type_start_token->location,
15364                    type, scope, nested_name_specifier);
15365           type = NULL_TREE;
15366           goto done;
15367         }
15368       /* [dcl.meaning]
15369
15370          A declarator-id shall not be qualified except for the
15371          definition of a ... nested class outside of its class
15372          ... [or] the definition or explicit instantiation of a
15373          class member of a namespace outside of its namespace.  */
15374       if (scope == nested_name_specifier)
15375         {
15376           permerror (input_location, "%Hextra qualification not allowed",
15377                      &nested_name_specifier_token_start->location);
15378           nested_name_specifier = NULL_TREE;
15379           num_templates = 0;
15380         }
15381     }
15382   /* An explicit-specialization must be preceded by "template <>".  If
15383      it is not, try to recover gracefully.  */
15384   if (at_namespace_scope_p ()
15385       && parser->num_template_parameter_lists == 0
15386       && template_id_p)
15387     {
15388       error ("%Han explicit specialization must be preceded by %<template <>%>",
15389              &type_start_token->location);
15390       invalid_explicit_specialization_p = true;
15391       /* Take the same action that would have been taken by
15392          cp_parser_explicit_specialization.  */
15393       ++parser->num_template_parameter_lists;
15394       begin_specialization ();
15395     }
15396   /* There must be no "return" statements between this point and the
15397      end of this function; set "type "to the correct return value and
15398      use "goto done;" to return.  */
15399   /* Make sure that the right number of template parameters were
15400      present.  */
15401   if (!cp_parser_check_template_parameters (parser, num_templates,
15402                                             type_start_token->location,
15403                                             /*declarator=*/NULL))
15404     {
15405       /* If something went wrong, there is no point in even trying to
15406          process the class-definition.  */
15407       type = NULL_TREE;
15408       goto done;
15409     }
15410
15411   /* Look up the type.  */
15412   if (template_id_p)
15413     {
15414       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15415           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15416               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15417         {
15418           error ("%Hfunction template %qD redeclared as a class template",
15419                  &type_start_token->location, id);
15420           type = error_mark_node;
15421         }
15422       else
15423         {
15424           type = TREE_TYPE (id);
15425           type = maybe_process_partial_specialization (type);
15426         }
15427       if (nested_name_specifier)
15428         pushed_scope = push_scope (nested_name_specifier);
15429     }
15430   else if (nested_name_specifier)
15431     {
15432       tree class_type;
15433
15434       /* Given:
15435
15436             template <typename T> struct S { struct T };
15437             template <typename T> struct S<T>::T { };
15438
15439          we will get a TYPENAME_TYPE when processing the definition of
15440          `S::T'.  We need to resolve it to the actual type before we
15441          try to define it.  */
15442       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15443         {
15444           class_type = resolve_typename_type (TREE_TYPE (type),
15445                                               /*only_current_p=*/false);
15446           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15447             type = TYPE_NAME (class_type);
15448           else
15449             {
15450               cp_parser_error (parser, "could not resolve typename type");
15451               type = error_mark_node;
15452             }
15453         }
15454
15455       if (maybe_process_partial_specialization (TREE_TYPE (type))
15456           == error_mark_node)
15457         {
15458           type = NULL_TREE;
15459           goto done;
15460         }
15461
15462       class_type = current_class_type;
15463       /* Enter the scope indicated by the nested-name-specifier.  */
15464       pushed_scope = push_scope (nested_name_specifier);
15465       /* Get the canonical version of this type.  */
15466       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15467       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15468           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15469         {
15470           type = push_template_decl (type);
15471           if (type == error_mark_node)
15472             {
15473               type = NULL_TREE;
15474               goto done;
15475             }
15476         }
15477
15478       type = TREE_TYPE (type);
15479       *nested_name_specifier_p = true;
15480     }
15481   else      /* The name is not a nested name.  */
15482     {
15483       /* If the class was unnamed, create a dummy name.  */
15484       if (!id)
15485         id = make_anon_name ();
15486       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15487                        parser->num_template_parameter_lists);
15488     }
15489
15490   /* Indicate whether this class was declared as a `class' or as a
15491      `struct'.  */
15492   if (TREE_CODE (type) == RECORD_TYPE)
15493     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15494   cp_parser_check_class_key (class_key, type);
15495
15496   /* If this type was already complete, and we see another definition,
15497      that's an error.  */
15498   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15499     {
15500       error ("%Hredefinition of %q#T",
15501              &type_start_token->location, type);
15502       error ("%Hprevious definition of %q+#T",
15503              &type_start_token->location, type);
15504       type = NULL_TREE;
15505       goto done;
15506     }
15507   else if (type == error_mark_node)
15508     type = NULL_TREE;
15509
15510   /* We will have entered the scope containing the class; the names of
15511      base classes should be looked up in that context.  For example:
15512
15513        struct A { struct B {}; struct C; };
15514        struct A::C : B {};
15515
15516      is valid.  */
15517
15518   /* Get the list of base-classes, if there is one.  */
15519   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15520     *bases = cp_parser_base_clause (parser);
15521
15522  done:
15523   /* Leave the scope given by the nested-name-specifier.  We will
15524      enter the class scope itself while processing the members.  */
15525   if (pushed_scope)
15526     pop_scope (pushed_scope);
15527
15528   if (invalid_explicit_specialization_p)
15529     {
15530       end_specialization ();
15531       --parser->num_template_parameter_lists;
15532     }
15533   *attributes_p = attributes;
15534   return type;
15535 }
15536
15537 /* Parse a class-key.
15538
15539    class-key:
15540      class
15541      struct
15542      union
15543
15544    Returns the kind of class-key specified, or none_type to indicate
15545    error.  */
15546
15547 static enum tag_types
15548 cp_parser_class_key (cp_parser* parser)
15549 {
15550   cp_token *token;
15551   enum tag_types tag_type;
15552
15553   /* Look for the class-key.  */
15554   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15555   if (!token)
15556     return none_type;
15557
15558   /* Check to see if the TOKEN is a class-key.  */
15559   tag_type = cp_parser_token_is_class_key (token);
15560   if (!tag_type)
15561     cp_parser_error (parser, "expected class-key");
15562   return tag_type;
15563 }
15564
15565 /* Parse an (optional) member-specification.
15566
15567    member-specification:
15568      member-declaration member-specification [opt]
15569      access-specifier : member-specification [opt]  */
15570
15571 static void
15572 cp_parser_member_specification_opt (cp_parser* parser)
15573 {
15574   while (true)
15575     {
15576       cp_token *token;
15577       enum rid keyword;
15578
15579       /* Peek at the next token.  */
15580       token = cp_lexer_peek_token (parser->lexer);
15581       /* If it's a `}', or EOF then we've seen all the members.  */
15582       if (token->type == CPP_CLOSE_BRACE
15583           || token->type == CPP_EOF
15584           || token->type == CPP_PRAGMA_EOL)
15585         break;
15586
15587       /* See if this token is a keyword.  */
15588       keyword = token->keyword;
15589       switch (keyword)
15590         {
15591         case RID_PUBLIC:
15592         case RID_PROTECTED:
15593         case RID_PRIVATE:
15594           /* Consume the access-specifier.  */
15595           cp_lexer_consume_token (parser->lexer);
15596           /* Remember which access-specifier is active.  */
15597           current_access_specifier = token->u.value;
15598           /* Look for the `:'.  */
15599           cp_parser_require (parser, CPP_COLON, "%<:%>");
15600           break;
15601
15602         default:
15603           /* Accept #pragmas at class scope.  */
15604           if (token->type == CPP_PRAGMA)
15605             {
15606               cp_parser_pragma (parser, pragma_external);
15607               break;
15608             }
15609
15610           /* Otherwise, the next construction must be a
15611              member-declaration.  */
15612           cp_parser_member_declaration (parser);
15613         }
15614     }
15615 }
15616
15617 /* Parse a member-declaration.
15618
15619    member-declaration:
15620      decl-specifier-seq [opt] member-declarator-list [opt] ;
15621      function-definition ; [opt]
15622      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15623      using-declaration
15624      template-declaration
15625
15626    member-declarator-list:
15627      member-declarator
15628      member-declarator-list , member-declarator
15629
15630    member-declarator:
15631      declarator pure-specifier [opt]
15632      declarator constant-initializer [opt]
15633      identifier [opt] : constant-expression
15634
15635    GNU Extensions:
15636
15637    member-declaration:
15638      __extension__ member-declaration
15639
15640    member-declarator:
15641      declarator attributes [opt] pure-specifier [opt]
15642      declarator attributes [opt] constant-initializer [opt]
15643      identifier [opt] attributes [opt] : constant-expression  
15644
15645    C++0x Extensions:
15646
15647    member-declaration:
15648      static_assert-declaration  */
15649
15650 static void
15651 cp_parser_member_declaration (cp_parser* parser)
15652 {
15653   cp_decl_specifier_seq decl_specifiers;
15654   tree prefix_attributes;
15655   tree decl;
15656   int declares_class_or_enum;
15657   bool friend_p;
15658   cp_token *token = NULL;
15659   cp_token *decl_spec_token_start = NULL;
15660   cp_token *initializer_token_start = NULL;
15661   int saved_pedantic;
15662
15663   /* Check for the `__extension__' keyword.  */
15664   if (cp_parser_extension_opt (parser, &saved_pedantic))
15665     {
15666       /* Recurse.  */
15667       cp_parser_member_declaration (parser);
15668       /* Restore the old value of the PEDANTIC flag.  */
15669       pedantic = saved_pedantic;
15670
15671       return;
15672     }
15673
15674   /* Check for a template-declaration.  */
15675   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15676     {
15677       /* An explicit specialization here is an error condition, and we
15678          expect the specialization handler to detect and report this.  */
15679       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15680           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15681         cp_parser_explicit_specialization (parser);
15682       else
15683         cp_parser_template_declaration (parser, /*member_p=*/true);
15684
15685       return;
15686     }
15687
15688   /* Check for a using-declaration.  */
15689   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15690     {
15691       /* Parse the using-declaration.  */
15692       cp_parser_using_declaration (parser,
15693                                    /*access_declaration_p=*/false);
15694       return;
15695     }
15696
15697   /* Check for @defs.  */
15698   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15699     {
15700       tree ivar, member;
15701       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15702       ivar = ivar_chains;
15703       while (ivar)
15704         {
15705           member = ivar;
15706           ivar = TREE_CHAIN (member);
15707           TREE_CHAIN (member) = NULL_TREE;
15708           finish_member_declaration (member);
15709         }
15710       return;
15711     }
15712
15713   /* If the next token is `static_assert' we have a static assertion.  */
15714   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15715     {
15716       cp_parser_static_assert (parser, /*member_p=*/true);
15717       return;
15718     }
15719
15720   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15721     return;
15722
15723   /* Parse the decl-specifier-seq.  */
15724   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15725   cp_parser_decl_specifier_seq (parser,
15726                                 CP_PARSER_FLAGS_OPTIONAL,
15727                                 &decl_specifiers,
15728                                 &declares_class_or_enum);
15729   prefix_attributes = decl_specifiers.attributes;
15730   decl_specifiers.attributes = NULL_TREE;
15731   /* Check for an invalid type-name.  */
15732   if (!decl_specifiers.type
15733       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15734     return;
15735   /* If there is no declarator, then the decl-specifier-seq should
15736      specify a type.  */
15737   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15738     {
15739       /* If there was no decl-specifier-seq, and the next token is a
15740          `;', then we have something like:
15741
15742            struct S { ; };
15743
15744          [class.mem]
15745
15746          Each member-declaration shall declare at least one member
15747          name of the class.  */
15748       if (!decl_specifiers.any_specifiers_p)
15749         {
15750           cp_token *token = cp_lexer_peek_token (parser->lexer);
15751           if (!in_system_header_at (token->location))
15752             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15753         }
15754       else
15755         {
15756           tree type;
15757
15758           /* See if this declaration is a friend.  */
15759           friend_p = cp_parser_friend_p (&decl_specifiers);
15760           /* If there were decl-specifiers, check to see if there was
15761              a class-declaration.  */
15762           type = check_tag_decl (&decl_specifiers);
15763           /* Nested classes have already been added to the class, but
15764              a `friend' needs to be explicitly registered.  */
15765           if (friend_p)
15766             {
15767               /* If the `friend' keyword was present, the friend must
15768                  be introduced with a class-key.  */
15769                if (!declares_class_or_enum)
15770                  error ("%Ha class-key must be used when declaring a friend",
15771                         &decl_spec_token_start->location);
15772                /* In this case:
15773
15774                     template <typename T> struct A {
15775                       friend struct A<T>::B;
15776                     };
15777
15778                   A<T>::B will be represented by a TYPENAME_TYPE, and
15779                   therefore not recognized by check_tag_decl.  */
15780                if (!type
15781                    && decl_specifiers.type
15782                    && TYPE_P (decl_specifiers.type))
15783                  type = decl_specifiers.type;
15784                if (!type || !TYPE_P (type))
15785                  error ("%Hfriend declaration does not name a class or "
15786                         "function", &decl_spec_token_start->location);
15787                else
15788                  make_friend_class (current_class_type, type,
15789                                     /*complain=*/true);
15790             }
15791           /* If there is no TYPE, an error message will already have
15792              been issued.  */
15793           else if (!type || type == error_mark_node)
15794             ;
15795           /* An anonymous aggregate has to be handled specially; such
15796              a declaration really declares a data member (with a
15797              particular type), as opposed to a nested class.  */
15798           else if (ANON_AGGR_TYPE_P (type))
15799             {
15800               /* Remove constructors and such from TYPE, now that we
15801                  know it is an anonymous aggregate.  */
15802               fixup_anonymous_aggr (type);
15803               /* And make the corresponding data member.  */
15804               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15805               /* Add it to the class.  */
15806               finish_member_declaration (decl);
15807             }
15808           else
15809             cp_parser_check_access_in_redeclaration
15810                                               (TYPE_NAME (type),
15811                                                decl_spec_token_start->location);
15812         }
15813     }
15814   else
15815     {
15816       /* See if these declarations will be friends.  */
15817       friend_p = cp_parser_friend_p (&decl_specifiers);
15818
15819       /* Keep going until we hit the `;' at the end of the
15820          declaration.  */
15821       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15822         {
15823           tree attributes = NULL_TREE;
15824           tree first_attribute;
15825
15826           /* Peek at the next token.  */
15827           token = cp_lexer_peek_token (parser->lexer);
15828
15829           /* Check for a bitfield declaration.  */
15830           if (token->type == CPP_COLON
15831               || (token->type == CPP_NAME
15832                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15833                   == CPP_COLON))
15834             {
15835               tree identifier;
15836               tree width;
15837
15838               /* Get the name of the bitfield.  Note that we cannot just
15839                  check TOKEN here because it may have been invalidated by
15840                  the call to cp_lexer_peek_nth_token above.  */
15841               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15842                 identifier = cp_parser_identifier (parser);
15843               else
15844                 identifier = NULL_TREE;
15845
15846               /* Consume the `:' token.  */
15847               cp_lexer_consume_token (parser->lexer);
15848               /* Get the width of the bitfield.  */
15849               width
15850                 = cp_parser_constant_expression (parser,
15851                                                  /*allow_non_constant=*/false,
15852                                                  NULL);
15853
15854               /* Look for attributes that apply to the bitfield.  */
15855               attributes = cp_parser_attributes_opt (parser);
15856               /* Remember which attributes are prefix attributes and
15857                  which are not.  */
15858               first_attribute = attributes;
15859               /* Combine the attributes.  */
15860               attributes = chainon (prefix_attributes, attributes);
15861
15862               /* Create the bitfield declaration.  */
15863               decl = grokbitfield (identifier
15864                                    ? make_id_declarator (NULL_TREE,
15865                                                          identifier,
15866                                                          sfk_none)
15867                                    : NULL,
15868                                    &decl_specifiers,
15869                                    width,
15870                                    attributes);
15871             }
15872           else
15873             {
15874               cp_declarator *declarator;
15875               tree initializer;
15876               tree asm_specification;
15877               int ctor_dtor_or_conv_p;
15878
15879               /* Parse the declarator.  */
15880               declarator
15881                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15882                                         &ctor_dtor_or_conv_p,
15883                                         /*parenthesized_p=*/NULL,
15884                                         /*member_p=*/true);
15885
15886               /* If something went wrong parsing the declarator, make sure
15887                  that we at least consume some tokens.  */
15888               if (declarator == cp_error_declarator)
15889                 {
15890                   /* Skip to the end of the statement.  */
15891                   cp_parser_skip_to_end_of_statement (parser);
15892                   /* If the next token is not a semicolon, that is
15893                      probably because we just skipped over the body of
15894                      a function.  So, we consume a semicolon if
15895                      present, but do not issue an error message if it
15896                      is not present.  */
15897                   if (cp_lexer_next_token_is (parser->lexer,
15898                                               CPP_SEMICOLON))
15899                     cp_lexer_consume_token (parser->lexer);
15900                   return;
15901                 }
15902
15903               if (declares_class_or_enum & 2)
15904                 cp_parser_check_for_definition_in_return_type
15905                                             (declarator, decl_specifiers.type,
15906                                              decl_specifiers.type_location);
15907
15908               /* Look for an asm-specification.  */
15909               asm_specification = cp_parser_asm_specification_opt (parser);
15910               /* Look for attributes that apply to the declaration.  */
15911               attributes = cp_parser_attributes_opt (parser);
15912               /* Remember which attributes are prefix attributes and
15913                  which are not.  */
15914               first_attribute = attributes;
15915               /* Combine the attributes.  */
15916               attributes = chainon (prefix_attributes, attributes);
15917
15918               /* If it's an `=', then we have a constant-initializer or a
15919                  pure-specifier.  It is not correct to parse the
15920                  initializer before registering the member declaration
15921                  since the member declaration should be in scope while
15922                  its initializer is processed.  However, the rest of the
15923                  front end does not yet provide an interface that allows
15924                  us to handle this correctly.  */
15925               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15926                 {
15927                   /* In [class.mem]:
15928
15929                      A pure-specifier shall be used only in the declaration of
15930                      a virtual function.
15931
15932                      A member-declarator can contain a constant-initializer
15933                      only if it declares a static member of integral or
15934                      enumeration type.
15935
15936                      Therefore, if the DECLARATOR is for a function, we look
15937                      for a pure-specifier; otherwise, we look for a
15938                      constant-initializer.  When we call `grokfield', it will
15939                      perform more stringent semantics checks.  */
15940                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15941                   if (function_declarator_p (declarator))
15942                     initializer = cp_parser_pure_specifier (parser);
15943                   else
15944                     /* Parse the initializer.  */
15945                     initializer = cp_parser_constant_initializer (parser);
15946                 }
15947               /* Otherwise, there is no initializer.  */
15948               else
15949                 initializer = NULL_TREE;
15950
15951               /* See if we are probably looking at a function
15952                  definition.  We are certainly not looking at a
15953                  member-declarator.  Calling `grokfield' has
15954                  side-effects, so we must not do it unless we are sure
15955                  that we are looking at a member-declarator.  */
15956               if (cp_parser_token_starts_function_definition_p
15957                   (cp_lexer_peek_token (parser->lexer)))
15958                 {
15959                   /* The grammar does not allow a pure-specifier to be
15960                      used when a member function is defined.  (It is
15961                      possible that this fact is an oversight in the
15962                      standard, since a pure function may be defined
15963                      outside of the class-specifier.  */
15964                   if (initializer)
15965                     error ("%Hpure-specifier on function-definition",
15966                            &initializer_token_start->location);
15967                   decl = cp_parser_save_member_function_body (parser,
15968                                                               &decl_specifiers,
15969                                                               declarator,
15970                                                               attributes);
15971                   /* If the member was not a friend, declare it here.  */
15972                   if (!friend_p)
15973                     finish_member_declaration (decl);
15974                   /* Peek at the next token.  */
15975                   token = cp_lexer_peek_token (parser->lexer);
15976                   /* If the next token is a semicolon, consume it.  */
15977                   if (token->type == CPP_SEMICOLON)
15978                     cp_lexer_consume_token (parser->lexer);
15979                   return;
15980                 }
15981               else
15982                 if (declarator->kind == cdk_function)
15983                   declarator->id_loc = token->location;
15984                 /* Create the declaration.  */
15985                 decl = grokfield (declarator, &decl_specifiers,
15986                                   initializer, /*init_const_expr_p=*/true,
15987                                   asm_specification,
15988                                   attributes);
15989             }
15990
15991           /* Reset PREFIX_ATTRIBUTES.  */
15992           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15993             attributes = TREE_CHAIN (attributes);
15994           if (attributes)
15995             TREE_CHAIN (attributes) = NULL_TREE;
15996
15997           /* If there is any qualification still in effect, clear it
15998              now; we will be starting fresh with the next declarator.  */
15999           parser->scope = NULL_TREE;
16000           parser->qualifying_scope = NULL_TREE;
16001           parser->object_scope = NULL_TREE;
16002           /* If it's a `,', then there are more declarators.  */
16003           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16004             cp_lexer_consume_token (parser->lexer);
16005           /* If the next token isn't a `;', then we have a parse error.  */
16006           else if (cp_lexer_next_token_is_not (parser->lexer,
16007                                                CPP_SEMICOLON))
16008             {
16009               cp_parser_error (parser, "expected %<;%>");
16010               /* Skip tokens until we find a `;'.  */
16011               cp_parser_skip_to_end_of_statement (parser);
16012
16013               break;
16014             }
16015
16016           if (decl)
16017             {
16018               /* Add DECL to the list of members.  */
16019               if (!friend_p)
16020                 finish_member_declaration (decl);
16021
16022               if (TREE_CODE (decl) == FUNCTION_DECL)
16023                 cp_parser_save_default_args (parser, decl);
16024             }
16025         }
16026     }
16027
16028   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16029 }
16030
16031 /* Parse a pure-specifier.
16032
16033    pure-specifier:
16034      = 0
16035
16036    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16037    Otherwise, ERROR_MARK_NODE is returned.  */
16038
16039 static tree
16040 cp_parser_pure_specifier (cp_parser* parser)
16041 {
16042   cp_token *token;
16043
16044   /* Look for the `=' token.  */
16045   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16046     return error_mark_node;
16047   /* Look for the `0' token.  */
16048   token = cp_lexer_peek_token (parser->lexer);
16049
16050   if (token->type == CPP_EOF
16051       || token->type == CPP_PRAGMA_EOL)
16052     return error_mark_node;
16053
16054   cp_lexer_consume_token (parser->lexer);
16055
16056   /* Accept = default or = delete in c++0x mode.  */
16057   if (token->keyword == RID_DEFAULT
16058       || token->keyword == RID_DELETE)
16059     {
16060       maybe_warn_cpp0x ("defaulted and deleted functions");
16061       return token->u.value;
16062     }
16063
16064   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16065   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16066     {
16067       cp_parser_error (parser,
16068                        "invalid pure specifier (only %<= 0%> is allowed)");
16069       cp_parser_skip_to_end_of_statement (parser);
16070       return error_mark_node;
16071     }
16072   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16073     {
16074       error ("%Htemplates may not be %<virtual%>", &token->location);
16075       return error_mark_node;
16076     }
16077
16078   return integer_zero_node;
16079 }
16080
16081 /* Parse a constant-initializer.
16082
16083    constant-initializer:
16084      = constant-expression
16085
16086    Returns a representation of the constant-expression.  */
16087
16088 static tree
16089 cp_parser_constant_initializer (cp_parser* parser)
16090 {
16091   /* Look for the `=' token.  */
16092   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16093     return error_mark_node;
16094
16095   /* It is invalid to write:
16096
16097        struct S { static const int i = { 7 }; };
16098
16099      */
16100   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16101     {
16102       cp_parser_error (parser,
16103                        "a brace-enclosed initializer is not allowed here");
16104       /* Consume the opening brace.  */
16105       cp_lexer_consume_token (parser->lexer);
16106       /* Skip the initializer.  */
16107       cp_parser_skip_to_closing_brace (parser);
16108       /* Look for the trailing `}'.  */
16109       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16110
16111       return error_mark_node;
16112     }
16113
16114   return cp_parser_constant_expression (parser,
16115                                         /*allow_non_constant=*/false,
16116                                         NULL);
16117 }
16118
16119 /* Derived classes [gram.class.derived] */
16120
16121 /* Parse a base-clause.
16122
16123    base-clause:
16124      : base-specifier-list
16125
16126    base-specifier-list:
16127      base-specifier ... [opt]
16128      base-specifier-list , base-specifier ... [opt]
16129
16130    Returns a TREE_LIST representing the base-classes, in the order in
16131    which they were declared.  The representation of each node is as
16132    described by cp_parser_base_specifier.
16133
16134    In the case that no bases are specified, this function will return
16135    NULL_TREE, not ERROR_MARK_NODE.  */
16136
16137 static tree
16138 cp_parser_base_clause (cp_parser* parser)
16139 {
16140   tree bases = NULL_TREE;
16141
16142   /* Look for the `:' that begins the list.  */
16143   cp_parser_require (parser, CPP_COLON, "%<:%>");
16144
16145   /* Scan the base-specifier-list.  */
16146   while (true)
16147     {
16148       cp_token *token;
16149       tree base;
16150       bool pack_expansion_p = false;
16151
16152       /* Look for the base-specifier.  */
16153       base = cp_parser_base_specifier (parser);
16154       /* Look for the (optional) ellipsis. */
16155       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16156         {
16157           /* Consume the `...'. */
16158           cp_lexer_consume_token (parser->lexer);
16159
16160           pack_expansion_p = true;
16161         }
16162
16163       /* Add BASE to the front of the list.  */
16164       if (base != error_mark_node)
16165         {
16166           if (pack_expansion_p)
16167             /* Make this a pack expansion type. */
16168             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16169           
16170
16171           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16172             {
16173               TREE_CHAIN (base) = bases;
16174               bases = base;
16175             }
16176         }
16177       /* Peek at the next token.  */
16178       token = cp_lexer_peek_token (parser->lexer);
16179       /* If it's not a comma, then the list is complete.  */
16180       if (token->type != CPP_COMMA)
16181         break;
16182       /* Consume the `,'.  */
16183       cp_lexer_consume_token (parser->lexer);
16184     }
16185
16186   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16187      base class had a qualified name.  However, the next name that
16188      appears is certainly not qualified.  */
16189   parser->scope = NULL_TREE;
16190   parser->qualifying_scope = NULL_TREE;
16191   parser->object_scope = NULL_TREE;
16192
16193   return nreverse (bases);
16194 }
16195
16196 /* Parse a base-specifier.
16197
16198    base-specifier:
16199      :: [opt] nested-name-specifier [opt] class-name
16200      virtual access-specifier [opt] :: [opt] nested-name-specifier
16201        [opt] class-name
16202      access-specifier virtual [opt] :: [opt] nested-name-specifier
16203        [opt] class-name
16204
16205    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16206    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16207    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16208    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16209
16210 static tree
16211 cp_parser_base_specifier (cp_parser* parser)
16212 {
16213   cp_token *token;
16214   bool done = false;
16215   bool virtual_p = false;
16216   bool duplicate_virtual_error_issued_p = false;
16217   bool duplicate_access_error_issued_p = false;
16218   bool class_scope_p, template_p;
16219   tree access = access_default_node;
16220   tree type;
16221
16222   /* Process the optional `virtual' and `access-specifier'.  */
16223   while (!done)
16224     {
16225       /* Peek at the next token.  */
16226       token = cp_lexer_peek_token (parser->lexer);
16227       /* Process `virtual'.  */
16228       switch (token->keyword)
16229         {
16230         case RID_VIRTUAL:
16231           /* If `virtual' appears more than once, issue an error.  */
16232           if (virtual_p && !duplicate_virtual_error_issued_p)
16233             {
16234               cp_parser_error (parser,
16235                                "%<virtual%> specified more than once in base-specified");
16236               duplicate_virtual_error_issued_p = true;
16237             }
16238
16239           virtual_p = true;
16240
16241           /* Consume the `virtual' token.  */
16242           cp_lexer_consume_token (parser->lexer);
16243
16244           break;
16245
16246         case RID_PUBLIC:
16247         case RID_PROTECTED:
16248         case RID_PRIVATE:
16249           /* If more than one access specifier appears, issue an
16250              error.  */
16251           if (access != access_default_node
16252               && !duplicate_access_error_issued_p)
16253             {
16254               cp_parser_error (parser,
16255                                "more than one access specifier in base-specified");
16256               duplicate_access_error_issued_p = true;
16257             }
16258
16259           access = ridpointers[(int) token->keyword];
16260
16261           /* Consume the access-specifier.  */
16262           cp_lexer_consume_token (parser->lexer);
16263
16264           break;
16265
16266         default:
16267           done = true;
16268           break;
16269         }
16270     }
16271   /* It is not uncommon to see programs mechanically, erroneously, use
16272      the 'typename' keyword to denote (dependent) qualified types
16273      as base classes.  */
16274   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16275     {
16276       token = cp_lexer_peek_token (parser->lexer);
16277       if (!processing_template_decl)
16278         error ("%Hkeyword %<typename%> not allowed outside of templates",
16279                &token->location);
16280       else
16281         error ("%Hkeyword %<typename%> not allowed in this context "
16282                "(the base class is implicitly a type)",
16283                &token->location);
16284       cp_lexer_consume_token (parser->lexer);
16285     }
16286
16287   /* Look for the optional `::' operator.  */
16288   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16289   /* Look for the nested-name-specifier.  The simplest way to
16290      implement:
16291
16292        [temp.res]
16293
16294        The keyword `typename' is not permitted in a base-specifier or
16295        mem-initializer; in these contexts a qualified name that
16296        depends on a template-parameter is implicitly assumed to be a
16297        type name.
16298
16299      is to pretend that we have seen the `typename' keyword at this
16300      point.  */
16301   cp_parser_nested_name_specifier_opt (parser,
16302                                        /*typename_keyword_p=*/true,
16303                                        /*check_dependency_p=*/true,
16304                                        typename_type,
16305                                        /*is_declaration=*/true);
16306   /* If the base class is given by a qualified name, assume that names
16307      we see are type names or templates, as appropriate.  */
16308   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16309   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16310
16311   /* Finally, look for the class-name.  */
16312   type = cp_parser_class_name (parser,
16313                                class_scope_p,
16314                                template_p,
16315                                typename_type,
16316                                /*check_dependency_p=*/true,
16317                                /*class_head_p=*/false,
16318                                /*is_declaration=*/true);
16319
16320   if (type == error_mark_node)
16321     return error_mark_node;
16322
16323   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16324 }
16325
16326 /* Exception handling [gram.exception] */
16327
16328 /* Parse an (optional) exception-specification.
16329
16330    exception-specification:
16331      throw ( type-id-list [opt] )
16332
16333    Returns a TREE_LIST representing the exception-specification.  The
16334    TREE_VALUE of each node is a type.  */
16335
16336 static tree
16337 cp_parser_exception_specification_opt (cp_parser* parser)
16338 {
16339   cp_token *token;
16340   tree type_id_list;
16341
16342   /* Peek at the next token.  */
16343   token = cp_lexer_peek_token (parser->lexer);
16344   /* If it's not `throw', then there's no exception-specification.  */
16345   if (!cp_parser_is_keyword (token, RID_THROW))
16346     return NULL_TREE;
16347
16348   /* Consume the `throw'.  */
16349   cp_lexer_consume_token (parser->lexer);
16350
16351   /* Look for the `('.  */
16352   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16353
16354   /* Peek at the next token.  */
16355   token = cp_lexer_peek_token (parser->lexer);
16356   /* If it's not a `)', then there is a type-id-list.  */
16357   if (token->type != CPP_CLOSE_PAREN)
16358     {
16359       const char *saved_message;
16360
16361       /* Types may not be defined in an exception-specification.  */
16362       saved_message = parser->type_definition_forbidden_message;
16363       parser->type_definition_forbidden_message
16364         = "types may not be defined in an exception-specification";
16365       /* Parse the type-id-list.  */
16366       type_id_list = cp_parser_type_id_list (parser);
16367       /* Restore the saved message.  */
16368       parser->type_definition_forbidden_message = saved_message;
16369     }
16370   else
16371     type_id_list = empty_except_spec;
16372
16373   /* Look for the `)'.  */
16374   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16375
16376   return type_id_list;
16377 }
16378
16379 /* Parse an (optional) type-id-list.
16380
16381    type-id-list:
16382      type-id ... [opt]
16383      type-id-list , type-id ... [opt]
16384
16385    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16386    in the order that the types were presented.  */
16387
16388 static tree
16389 cp_parser_type_id_list (cp_parser* parser)
16390 {
16391   tree types = NULL_TREE;
16392
16393   while (true)
16394     {
16395       cp_token *token;
16396       tree type;
16397
16398       /* Get the next type-id.  */
16399       type = cp_parser_type_id (parser);
16400       /* Parse the optional ellipsis. */
16401       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16402         {
16403           /* Consume the `...'. */
16404           cp_lexer_consume_token (parser->lexer);
16405
16406           /* Turn the type into a pack expansion expression. */
16407           type = make_pack_expansion (type);
16408         }
16409       /* Add it to the list.  */
16410       types = add_exception_specifier (types, type, /*complain=*/1);
16411       /* Peek at the next token.  */
16412       token = cp_lexer_peek_token (parser->lexer);
16413       /* If it is not a `,', we are done.  */
16414       if (token->type != CPP_COMMA)
16415         break;
16416       /* Consume the `,'.  */
16417       cp_lexer_consume_token (parser->lexer);
16418     }
16419
16420   return nreverse (types);
16421 }
16422
16423 /* Parse a try-block.
16424
16425    try-block:
16426      try compound-statement handler-seq  */
16427
16428 static tree
16429 cp_parser_try_block (cp_parser* parser)
16430 {
16431   tree try_block;
16432
16433   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16434   try_block = begin_try_block ();
16435   cp_parser_compound_statement (parser, NULL, true);
16436   finish_try_block (try_block);
16437   cp_parser_handler_seq (parser);
16438   finish_handler_sequence (try_block);
16439
16440   return try_block;
16441 }
16442
16443 /* Parse a function-try-block.
16444
16445    function-try-block:
16446      try ctor-initializer [opt] function-body handler-seq  */
16447
16448 static bool
16449 cp_parser_function_try_block (cp_parser* parser)
16450 {
16451   tree compound_stmt;
16452   tree try_block;
16453   bool ctor_initializer_p;
16454
16455   /* Look for the `try' keyword.  */
16456   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16457     return false;
16458   /* Let the rest of the front end know where we are.  */
16459   try_block = begin_function_try_block (&compound_stmt);
16460   /* Parse the function-body.  */
16461   ctor_initializer_p
16462     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16463   /* We're done with the `try' part.  */
16464   finish_function_try_block (try_block);
16465   /* Parse the handlers.  */
16466   cp_parser_handler_seq (parser);
16467   /* We're done with the handlers.  */
16468   finish_function_handler_sequence (try_block, compound_stmt);
16469
16470   return ctor_initializer_p;
16471 }
16472
16473 /* Parse a handler-seq.
16474
16475    handler-seq:
16476      handler handler-seq [opt]  */
16477
16478 static void
16479 cp_parser_handler_seq (cp_parser* parser)
16480 {
16481   while (true)
16482     {
16483       cp_token *token;
16484
16485       /* Parse the handler.  */
16486       cp_parser_handler (parser);
16487       /* Peek at the next token.  */
16488       token = cp_lexer_peek_token (parser->lexer);
16489       /* If it's not `catch' then there are no more handlers.  */
16490       if (!cp_parser_is_keyword (token, RID_CATCH))
16491         break;
16492     }
16493 }
16494
16495 /* Parse a handler.
16496
16497    handler:
16498      catch ( exception-declaration ) compound-statement  */
16499
16500 static void
16501 cp_parser_handler (cp_parser* parser)
16502 {
16503   tree handler;
16504   tree declaration;
16505
16506   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16507   handler = begin_handler ();
16508   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16509   declaration = cp_parser_exception_declaration (parser);
16510   finish_handler_parms (declaration, handler);
16511   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16512   cp_parser_compound_statement (parser, NULL, false);
16513   finish_handler (handler);
16514 }
16515
16516 /* Parse an exception-declaration.
16517
16518    exception-declaration:
16519      type-specifier-seq declarator
16520      type-specifier-seq abstract-declarator
16521      type-specifier-seq
16522      ...
16523
16524    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16525    ellipsis variant is used.  */
16526
16527 static tree
16528 cp_parser_exception_declaration (cp_parser* parser)
16529 {
16530   cp_decl_specifier_seq type_specifiers;
16531   cp_declarator *declarator;
16532   const char *saved_message;
16533
16534   /* If it's an ellipsis, it's easy to handle.  */
16535   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16536     {
16537       /* Consume the `...' token.  */
16538       cp_lexer_consume_token (parser->lexer);
16539       return NULL_TREE;
16540     }
16541
16542   /* Types may not be defined in exception-declarations.  */
16543   saved_message = parser->type_definition_forbidden_message;
16544   parser->type_definition_forbidden_message
16545     = "types may not be defined in exception-declarations";
16546
16547   /* Parse the type-specifier-seq.  */
16548   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16549                                 &type_specifiers);
16550   /* If it's a `)', then there is no declarator.  */
16551   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16552     declarator = NULL;
16553   else
16554     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16555                                        /*ctor_dtor_or_conv_p=*/NULL,
16556                                        /*parenthesized_p=*/NULL,
16557                                        /*member_p=*/false);
16558
16559   /* Restore the saved message.  */
16560   parser->type_definition_forbidden_message = saved_message;
16561
16562   if (!type_specifiers.any_specifiers_p)
16563     return error_mark_node;
16564
16565   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16566 }
16567
16568 /* Parse a throw-expression.
16569
16570    throw-expression:
16571      throw assignment-expression [opt]
16572
16573    Returns a THROW_EXPR representing the throw-expression.  */
16574
16575 static tree
16576 cp_parser_throw_expression (cp_parser* parser)
16577 {
16578   tree expression;
16579   cp_token* token;
16580
16581   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16582   token = cp_lexer_peek_token (parser->lexer);
16583   /* Figure out whether or not there is an assignment-expression
16584      following the "throw" keyword.  */
16585   if (token->type == CPP_COMMA
16586       || token->type == CPP_SEMICOLON
16587       || token->type == CPP_CLOSE_PAREN
16588       || token->type == CPP_CLOSE_SQUARE
16589       || token->type == CPP_CLOSE_BRACE
16590       || token->type == CPP_COLON)
16591     expression = NULL_TREE;
16592   else
16593     expression = cp_parser_assignment_expression (parser,
16594                                                   /*cast_p=*/false, NULL);
16595
16596   return build_throw (expression);
16597 }
16598
16599 /* GNU Extensions */
16600
16601 /* Parse an (optional) asm-specification.
16602
16603    asm-specification:
16604      asm ( string-literal )
16605
16606    If the asm-specification is present, returns a STRING_CST
16607    corresponding to the string-literal.  Otherwise, returns
16608    NULL_TREE.  */
16609
16610 static tree
16611 cp_parser_asm_specification_opt (cp_parser* parser)
16612 {
16613   cp_token *token;
16614   tree asm_specification;
16615
16616   /* Peek at the next token.  */
16617   token = cp_lexer_peek_token (parser->lexer);
16618   /* If the next token isn't the `asm' keyword, then there's no
16619      asm-specification.  */
16620   if (!cp_parser_is_keyword (token, RID_ASM))
16621     return NULL_TREE;
16622
16623   /* Consume the `asm' token.  */
16624   cp_lexer_consume_token (parser->lexer);
16625   /* Look for the `('.  */
16626   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16627
16628   /* Look for the string-literal.  */
16629   asm_specification = cp_parser_string_literal (parser, false, false);
16630
16631   /* Look for the `)'.  */
16632   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16633
16634   return asm_specification;
16635 }
16636
16637 /* Parse an asm-operand-list.
16638
16639    asm-operand-list:
16640      asm-operand
16641      asm-operand-list , asm-operand
16642
16643    asm-operand:
16644      string-literal ( expression )
16645      [ string-literal ] string-literal ( expression )
16646
16647    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16648    each node is the expression.  The TREE_PURPOSE is itself a
16649    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16650    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16651    is a STRING_CST for the string literal before the parenthesis. Returns
16652    ERROR_MARK_NODE if any of the operands are invalid.  */
16653
16654 static tree
16655 cp_parser_asm_operand_list (cp_parser* parser)
16656 {
16657   tree asm_operands = NULL_TREE;
16658   bool invalid_operands = false;
16659
16660   while (true)
16661     {
16662       tree string_literal;
16663       tree expression;
16664       tree name;
16665
16666       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16667         {
16668           /* Consume the `[' token.  */
16669           cp_lexer_consume_token (parser->lexer);
16670           /* Read the operand name.  */
16671           name = cp_parser_identifier (parser);
16672           if (name != error_mark_node)
16673             name = build_string (IDENTIFIER_LENGTH (name),
16674                                  IDENTIFIER_POINTER (name));
16675           /* Look for the closing `]'.  */
16676           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16677         }
16678       else
16679         name = NULL_TREE;
16680       /* Look for the string-literal.  */
16681       string_literal = cp_parser_string_literal (parser, false, false);
16682
16683       /* Look for the `('.  */
16684       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16685       /* Parse the expression.  */
16686       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16687       /* Look for the `)'.  */
16688       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16689
16690       if (name == error_mark_node 
16691           || string_literal == error_mark_node 
16692           || expression == error_mark_node)
16693         invalid_operands = true;
16694
16695       /* Add this operand to the list.  */
16696       asm_operands = tree_cons (build_tree_list (name, string_literal),
16697                                 expression,
16698                                 asm_operands);
16699       /* If the next token is not a `,', there are no more
16700          operands.  */
16701       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16702         break;
16703       /* Consume the `,'.  */
16704       cp_lexer_consume_token (parser->lexer);
16705     }
16706
16707   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16708 }
16709
16710 /* Parse an asm-clobber-list.
16711
16712    asm-clobber-list:
16713      string-literal
16714      asm-clobber-list , string-literal
16715
16716    Returns a TREE_LIST, indicating the clobbers in the order that they
16717    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16718
16719 static tree
16720 cp_parser_asm_clobber_list (cp_parser* parser)
16721 {
16722   tree clobbers = NULL_TREE;
16723
16724   while (true)
16725     {
16726       tree string_literal;
16727
16728       /* Look for the string literal.  */
16729       string_literal = cp_parser_string_literal (parser, false, false);
16730       /* Add it to the list.  */
16731       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16732       /* If the next token is not a `,', then the list is
16733          complete.  */
16734       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16735         break;
16736       /* Consume the `,' token.  */
16737       cp_lexer_consume_token (parser->lexer);
16738     }
16739
16740   return clobbers;
16741 }
16742
16743 /* Parse an (optional) series of attributes.
16744
16745    attributes:
16746      attributes attribute
16747
16748    attribute:
16749      __attribute__ (( attribute-list [opt] ))
16750
16751    The return value is as for cp_parser_attribute_list.  */
16752
16753 static tree
16754 cp_parser_attributes_opt (cp_parser* parser)
16755 {
16756   tree attributes = NULL_TREE;
16757
16758   while (true)
16759     {
16760       cp_token *token;
16761       tree attribute_list;
16762
16763       /* Peek at the next token.  */
16764       token = cp_lexer_peek_token (parser->lexer);
16765       /* If it's not `__attribute__', then we're done.  */
16766       if (token->keyword != RID_ATTRIBUTE)
16767         break;
16768
16769       /* Consume the `__attribute__' keyword.  */
16770       cp_lexer_consume_token (parser->lexer);
16771       /* Look for the two `(' tokens.  */
16772       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16773       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16774
16775       /* Peek at the next token.  */
16776       token = cp_lexer_peek_token (parser->lexer);
16777       if (token->type != CPP_CLOSE_PAREN)
16778         /* Parse the attribute-list.  */
16779         attribute_list = cp_parser_attribute_list (parser);
16780       else
16781         /* If the next token is a `)', then there is no attribute
16782            list.  */
16783         attribute_list = NULL;
16784
16785       /* Look for the two `)' tokens.  */
16786       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16787       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16788
16789       /* Add these new attributes to the list.  */
16790       attributes = chainon (attributes, attribute_list);
16791     }
16792
16793   return attributes;
16794 }
16795
16796 /* Parse an attribute-list.
16797
16798    attribute-list:
16799      attribute
16800      attribute-list , attribute
16801
16802    attribute:
16803      identifier
16804      identifier ( identifier )
16805      identifier ( identifier , expression-list )
16806      identifier ( expression-list )
16807
16808    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16809    to an attribute.  The TREE_PURPOSE of each node is the identifier
16810    indicating which attribute is in use.  The TREE_VALUE represents
16811    the arguments, if any.  */
16812
16813 static tree
16814 cp_parser_attribute_list (cp_parser* parser)
16815 {
16816   tree attribute_list = NULL_TREE;
16817   bool save_translate_strings_p = parser->translate_strings_p;
16818
16819   parser->translate_strings_p = false;
16820   while (true)
16821     {
16822       cp_token *token;
16823       tree identifier;
16824       tree attribute;
16825
16826       /* Look for the identifier.  We also allow keywords here; for
16827          example `__attribute__ ((const))' is legal.  */
16828       token = cp_lexer_peek_token (parser->lexer);
16829       if (token->type == CPP_NAME
16830           || token->type == CPP_KEYWORD)
16831         {
16832           tree arguments = NULL_TREE;
16833
16834           /* Consume the token.  */
16835           token = cp_lexer_consume_token (parser->lexer);
16836
16837           /* Save away the identifier that indicates which attribute
16838              this is.  */
16839           identifier = token->u.value;
16840           attribute = build_tree_list (identifier, NULL_TREE);
16841
16842           /* Peek at the next token.  */
16843           token = cp_lexer_peek_token (parser->lexer);
16844           /* If it's an `(', then parse the attribute arguments.  */
16845           if (token->type == CPP_OPEN_PAREN)
16846             {
16847               arguments = cp_parser_parenthesized_expression_list
16848                           (parser, true, /*cast_p=*/false,
16849                            /*allow_expansion_p=*/false,
16850                            /*non_constant_p=*/NULL);
16851               /* Save the arguments away.  */
16852               TREE_VALUE (attribute) = arguments;
16853             }
16854
16855           if (arguments != error_mark_node)
16856             {
16857               /* Add this attribute to the list.  */
16858               TREE_CHAIN (attribute) = attribute_list;
16859               attribute_list = attribute;
16860             }
16861
16862           token = cp_lexer_peek_token (parser->lexer);
16863         }
16864       /* Now, look for more attributes.  If the next token isn't a
16865          `,', we're done.  */
16866       if (token->type != CPP_COMMA)
16867         break;
16868
16869       /* Consume the comma and keep going.  */
16870       cp_lexer_consume_token (parser->lexer);
16871     }
16872   parser->translate_strings_p = save_translate_strings_p;
16873
16874   /* We built up the list in reverse order.  */
16875   return nreverse (attribute_list);
16876 }
16877
16878 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16879    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16880    current value of the PEDANTIC flag, regardless of whether or not
16881    the `__extension__' keyword is present.  The caller is responsible
16882    for restoring the value of the PEDANTIC flag.  */
16883
16884 static bool
16885 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16886 {
16887   /* Save the old value of the PEDANTIC flag.  */
16888   *saved_pedantic = pedantic;
16889
16890   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16891     {
16892       /* Consume the `__extension__' token.  */
16893       cp_lexer_consume_token (parser->lexer);
16894       /* We're not being pedantic while the `__extension__' keyword is
16895          in effect.  */
16896       pedantic = 0;
16897
16898       return true;
16899     }
16900
16901   return false;
16902 }
16903
16904 /* Parse a label declaration.
16905
16906    label-declaration:
16907      __label__ label-declarator-seq ;
16908
16909    label-declarator-seq:
16910      identifier , label-declarator-seq
16911      identifier  */
16912
16913 static void
16914 cp_parser_label_declaration (cp_parser* parser)
16915 {
16916   /* Look for the `__label__' keyword.  */
16917   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16918
16919   while (true)
16920     {
16921       tree identifier;
16922
16923       /* Look for an identifier.  */
16924       identifier = cp_parser_identifier (parser);
16925       /* If we failed, stop.  */
16926       if (identifier == error_mark_node)
16927         break;
16928       /* Declare it as a label.  */
16929       finish_label_decl (identifier);
16930       /* If the next token is a `;', stop.  */
16931       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16932         break;
16933       /* Look for the `,' separating the label declarations.  */
16934       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16935     }
16936
16937   /* Look for the final `;'.  */
16938   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16939 }
16940
16941 /* Support Functions */
16942
16943 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16944    NAME should have one of the representations used for an
16945    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16946    is returned.  If PARSER->SCOPE is a dependent type, then a
16947    SCOPE_REF is returned.
16948
16949    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16950    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16951    was formed.  Abstractly, such entities should not be passed to this
16952    function, because they do not need to be looked up, but it is
16953    simpler to check for this special case here, rather than at the
16954    call-sites.
16955
16956    In cases not explicitly covered above, this function returns a
16957    DECL, OVERLOAD, or baselink representing the result of the lookup.
16958    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16959    is returned.
16960
16961    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16962    (e.g., "struct") that was used.  In that case bindings that do not
16963    refer to types are ignored.
16964
16965    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16966    ignored.
16967
16968    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16969    are ignored.
16970
16971    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16972    types.
16973
16974    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16975    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16976    NULL_TREE otherwise.  */
16977
16978 static tree
16979 cp_parser_lookup_name (cp_parser *parser, tree name,
16980                        enum tag_types tag_type,
16981                        bool is_template,
16982                        bool is_namespace,
16983                        bool check_dependency,
16984                        tree *ambiguous_decls,
16985                        location_t name_location)
16986 {
16987   int flags = 0;
16988   tree decl;
16989   tree object_type = parser->context->object_type;
16990
16991   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16992     flags |= LOOKUP_COMPLAIN;
16993
16994   /* Assume that the lookup will be unambiguous.  */
16995   if (ambiguous_decls)
16996     *ambiguous_decls = NULL_TREE;
16997
16998   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16999      no longer valid.  Note that if we are parsing tentatively, and
17000      the parse fails, OBJECT_TYPE will be automatically restored.  */
17001   parser->context->object_type = NULL_TREE;
17002
17003   if (name == error_mark_node)
17004     return error_mark_node;
17005
17006   /* A template-id has already been resolved; there is no lookup to
17007      do.  */
17008   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17009     return name;
17010   if (BASELINK_P (name))
17011     {
17012       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17013                   == TEMPLATE_ID_EXPR);
17014       return name;
17015     }
17016
17017   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17018      it should already have been checked to make sure that the name
17019      used matches the type being destroyed.  */
17020   if (TREE_CODE (name) == BIT_NOT_EXPR)
17021     {
17022       tree type;
17023
17024       /* Figure out to which type this destructor applies.  */
17025       if (parser->scope)
17026         type = parser->scope;
17027       else if (object_type)
17028         type = object_type;
17029       else
17030         type = current_class_type;
17031       /* If that's not a class type, there is no destructor.  */
17032       if (!type || !CLASS_TYPE_P (type))
17033         return error_mark_node;
17034       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17035         lazily_declare_fn (sfk_destructor, type);
17036       if (!CLASSTYPE_DESTRUCTORS (type))
17037           return error_mark_node;
17038       /* If it was a class type, return the destructor.  */
17039       return CLASSTYPE_DESTRUCTORS (type);
17040     }
17041
17042   /* By this point, the NAME should be an ordinary identifier.  If
17043      the id-expression was a qualified name, the qualifying scope is
17044      stored in PARSER->SCOPE at this point.  */
17045   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17046
17047   /* Perform the lookup.  */
17048   if (parser->scope)
17049     {
17050       bool dependent_p;
17051
17052       if (parser->scope == error_mark_node)
17053         return error_mark_node;
17054
17055       /* If the SCOPE is dependent, the lookup must be deferred until
17056          the template is instantiated -- unless we are explicitly
17057          looking up names in uninstantiated templates.  Even then, we
17058          cannot look up the name if the scope is not a class type; it
17059          might, for example, be a template type parameter.  */
17060       dependent_p = (TYPE_P (parser->scope)
17061                      && dependent_scope_p (parser->scope));
17062       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17063           && dependent_p)
17064         /* Defer lookup.  */
17065         decl = error_mark_node;
17066       else
17067         {
17068           tree pushed_scope = NULL_TREE;
17069
17070           /* If PARSER->SCOPE is a dependent type, then it must be a
17071              class type, and we must not be checking dependencies;
17072              otherwise, we would have processed this lookup above.  So
17073              that PARSER->SCOPE is not considered a dependent base by
17074              lookup_member, we must enter the scope here.  */
17075           if (dependent_p)
17076             pushed_scope = push_scope (parser->scope);
17077           /* If the PARSER->SCOPE is a template specialization, it
17078              may be instantiated during name lookup.  In that case,
17079              errors may be issued.  Even if we rollback the current
17080              tentative parse, those errors are valid.  */
17081           decl = lookup_qualified_name (parser->scope, name,
17082                                         tag_type != none_type,
17083                                         /*complain=*/true);
17084
17085           /* If we have a single function from a using decl, pull it out.  */
17086           if (TREE_CODE (decl) == OVERLOAD
17087               && !really_overloaded_fn (decl))
17088             decl = OVL_FUNCTION (decl);
17089
17090           if (pushed_scope)
17091             pop_scope (pushed_scope);
17092         }
17093
17094       /* If the scope is a dependent type and either we deferred lookup or
17095          we did lookup but didn't find the name, rememeber the name.  */
17096       if (decl == error_mark_node && TYPE_P (parser->scope)
17097           && dependent_type_p (parser->scope))
17098         {
17099           if (tag_type)
17100             {
17101               tree type;
17102
17103               /* The resolution to Core Issue 180 says that `struct
17104                  A::B' should be considered a type-name, even if `A'
17105                  is dependent.  */
17106               type = make_typename_type (parser->scope, name, tag_type,
17107                                          /*complain=*/tf_error);
17108               decl = TYPE_NAME (type);
17109             }
17110           else if (is_template
17111                    && (cp_parser_next_token_ends_template_argument_p (parser)
17112                        || cp_lexer_next_token_is (parser->lexer,
17113                                                   CPP_CLOSE_PAREN)))
17114             decl = make_unbound_class_template (parser->scope,
17115                                                 name, NULL_TREE,
17116                                                 /*complain=*/tf_error);
17117           else
17118             decl = build_qualified_name (/*type=*/NULL_TREE,
17119                                          parser->scope, name,
17120                                          is_template);
17121         }
17122       parser->qualifying_scope = parser->scope;
17123       parser->object_scope = NULL_TREE;
17124     }
17125   else if (object_type)
17126     {
17127       tree object_decl = NULL_TREE;
17128       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17129          OBJECT_TYPE is not a class.  */
17130       if (CLASS_TYPE_P (object_type))
17131         /* If the OBJECT_TYPE is a template specialization, it may
17132            be instantiated during name lookup.  In that case, errors
17133            may be issued.  Even if we rollback the current tentative
17134            parse, those errors are valid.  */
17135         object_decl = lookup_member (object_type,
17136                                      name,
17137                                      /*protect=*/0,
17138                                      tag_type != none_type);
17139       /* Look it up in the enclosing context, too.  */
17140       decl = lookup_name_real (name, tag_type != none_type,
17141                                /*nonclass=*/0,
17142                                /*block_p=*/true, is_namespace, flags);
17143       parser->object_scope = object_type;
17144       parser->qualifying_scope = NULL_TREE;
17145       if (object_decl)
17146         decl = object_decl;
17147     }
17148   else
17149     {
17150       decl = lookup_name_real (name, tag_type != none_type,
17151                                /*nonclass=*/0,
17152                                /*block_p=*/true, is_namespace, flags);
17153       parser->qualifying_scope = NULL_TREE;
17154       parser->object_scope = NULL_TREE;
17155     }
17156
17157   /* If the lookup failed, let our caller know.  */
17158   if (!decl || decl == error_mark_node)
17159     return error_mark_node;
17160
17161   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17162   if (TREE_CODE (decl) == TREE_LIST)
17163     {
17164       if (ambiguous_decls)
17165         *ambiguous_decls = decl;
17166       /* The error message we have to print is too complicated for
17167          cp_parser_error, so we incorporate its actions directly.  */
17168       if (!cp_parser_simulate_error (parser))
17169         {
17170           error ("%Hreference to %qD is ambiguous",
17171                  &name_location, name);
17172           print_candidates (decl);
17173         }
17174       return error_mark_node;
17175     }
17176
17177   gcc_assert (DECL_P (decl)
17178               || TREE_CODE (decl) == OVERLOAD
17179               || TREE_CODE (decl) == SCOPE_REF
17180               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17181               || BASELINK_P (decl));
17182
17183   /* If we have resolved the name of a member declaration, check to
17184      see if the declaration is accessible.  When the name resolves to
17185      set of overloaded functions, accessibility is checked when
17186      overload resolution is done.
17187
17188      During an explicit instantiation, access is not checked at all,
17189      as per [temp.explicit].  */
17190   if (DECL_P (decl))
17191     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17192
17193   return decl;
17194 }
17195
17196 /* Like cp_parser_lookup_name, but for use in the typical case where
17197    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17198    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17199
17200 static tree
17201 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17202 {
17203   return cp_parser_lookup_name (parser, name,
17204                                 none_type,
17205                                 /*is_template=*/false,
17206                                 /*is_namespace=*/false,
17207                                 /*check_dependency=*/true,
17208                                 /*ambiguous_decls=*/NULL,
17209                                 location);
17210 }
17211
17212 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17213    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17214    true, the DECL indicates the class being defined in a class-head,
17215    or declared in an elaborated-type-specifier.
17216
17217    Otherwise, return DECL.  */
17218
17219 static tree
17220 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17221 {
17222   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17223      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17224
17225        struct A {
17226          template <typename T> struct B;
17227        };
17228
17229        template <typename T> struct A::B {};
17230
17231      Similarly, in an elaborated-type-specifier:
17232
17233        namespace N { struct X{}; }
17234
17235        struct A {
17236          template <typename T> friend struct N::X;
17237        };
17238
17239      However, if the DECL refers to a class type, and we are in
17240      the scope of the class, then the name lookup automatically
17241      finds the TYPE_DECL created by build_self_reference rather
17242      than a TEMPLATE_DECL.  For example, in:
17243
17244        template <class T> struct S {
17245          S s;
17246        };
17247
17248      there is no need to handle such case.  */
17249
17250   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17251     return DECL_TEMPLATE_RESULT (decl);
17252
17253   return decl;
17254 }
17255
17256 /* If too many, or too few, template-parameter lists apply to the
17257    declarator, issue an error message.  Returns TRUE if all went well,
17258    and FALSE otherwise.  */
17259
17260 static bool
17261 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17262                                                 cp_declarator *declarator,
17263                                                 location_t declarator_location)
17264 {
17265   unsigned num_templates;
17266
17267   /* We haven't seen any classes that involve template parameters yet.  */
17268   num_templates = 0;
17269
17270   switch (declarator->kind)
17271     {
17272     case cdk_id:
17273       if (declarator->u.id.qualifying_scope)
17274         {
17275           tree scope;
17276           tree member;
17277
17278           scope = declarator->u.id.qualifying_scope;
17279           member = declarator->u.id.unqualified_name;
17280
17281           while (scope && CLASS_TYPE_P (scope))
17282             {
17283               /* You're supposed to have one `template <...>'
17284                  for every template class, but you don't need one
17285                  for a full specialization.  For example:
17286
17287                  template <class T> struct S{};
17288                  template <> struct S<int> { void f(); };
17289                  void S<int>::f () {}
17290
17291                  is correct; there shouldn't be a `template <>' for
17292                  the definition of `S<int>::f'.  */
17293               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17294                 /* If SCOPE does not have template information of any
17295                    kind, then it is not a template, nor is it nested
17296                    within a template.  */
17297                 break;
17298               if (explicit_class_specialization_p (scope))
17299                 break;
17300               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17301                 ++num_templates;
17302
17303               scope = TYPE_CONTEXT (scope);
17304             }
17305         }
17306       else if (TREE_CODE (declarator->u.id.unqualified_name)
17307                == TEMPLATE_ID_EXPR)
17308         /* If the DECLARATOR has the form `X<y>' then it uses one
17309            additional level of template parameters.  */
17310         ++num_templates;
17311
17312       return cp_parser_check_template_parameters 
17313         (parser, num_templates, declarator_location, declarator);
17314
17315
17316     case cdk_function:
17317     case cdk_array:
17318     case cdk_pointer:
17319     case cdk_reference:
17320     case cdk_ptrmem:
17321       return (cp_parser_check_declarator_template_parameters
17322               (parser, declarator->declarator, declarator_location));
17323
17324     case cdk_error:
17325       return true;
17326
17327     default:
17328       gcc_unreachable ();
17329     }
17330   return false;
17331 }
17332
17333 /* NUM_TEMPLATES were used in the current declaration.  If that is
17334    invalid, return FALSE and issue an error messages.  Otherwise,
17335    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
17336    declarator and we can print more accurate diagnostics.  */
17337
17338 static bool
17339 cp_parser_check_template_parameters (cp_parser* parser,
17340                                      unsigned num_templates,
17341                                      location_t location,
17342                                      cp_declarator *declarator)
17343 {
17344   /* If there are the same number of template classes and parameter
17345      lists, that's OK.  */
17346   if (parser->num_template_parameter_lists == num_templates)
17347     return true;
17348   /* If there are more, but only one more, then we are referring to a
17349      member template.  That's OK too.  */
17350   if (parser->num_template_parameter_lists == num_templates + 1)
17351     return true;
17352   /* If there are more template classes than parameter lists, we have
17353      something like:
17354
17355        template <class T> void S<T>::R<T>::f ();  */
17356   if (parser->num_template_parameter_lists < num_templates)
17357     {
17358       if (declarator)
17359         error_at (location, "specializing member %<%T::%E%> "
17360                   "requires %<template<>%> syntax", 
17361                   declarator->u.id.qualifying_scope,
17362                   declarator->u.id.unqualified_name);
17363       else 
17364         error_at (location, "too few template-parameter-lists");
17365       return false;
17366     }
17367   /* Otherwise, there are too many template parameter lists.  We have
17368      something like:
17369
17370      template <class T> template <class U> void S::f();  */
17371   error ("%Htoo many template-parameter-lists", &location);
17372   return false;
17373 }
17374
17375 /* Parse an optional `::' token indicating that the following name is
17376    from the global namespace.  If so, PARSER->SCOPE is set to the
17377    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17378    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17379    Returns the new value of PARSER->SCOPE, if the `::' token is
17380    present, and NULL_TREE otherwise.  */
17381
17382 static tree
17383 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17384 {
17385   cp_token *token;
17386
17387   /* Peek at the next token.  */
17388   token = cp_lexer_peek_token (parser->lexer);
17389   /* If we're looking at a `::' token then we're starting from the
17390      global namespace, not our current location.  */
17391   if (token->type == CPP_SCOPE)
17392     {
17393       /* Consume the `::' token.  */
17394       cp_lexer_consume_token (parser->lexer);
17395       /* Set the SCOPE so that we know where to start the lookup.  */
17396       parser->scope = global_namespace;
17397       parser->qualifying_scope = global_namespace;
17398       parser->object_scope = NULL_TREE;
17399
17400       return parser->scope;
17401     }
17402   else if (!current_scope_valid_p)
17403     {
17404       parser->scope = NULL_TREE;
17405       parser->qualifying_scope = NULL_TREE;
17406       parser->object_scope = NULL_TREE;
17407     }
17408
17409   return NULL_TREE;
17410 }
17411
17412 /* Returns TRUE if the upcoming token sequence is the start of a
17413    constructor declarator.  If FRIEND_P is true, the declarator is
17414    preceded by the `friend' specifier.  */
17415
17416 static bool
17417 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17418 {
17419   bool constructor_p;
17420   tree type_decl = NULL_TREE;
17421   bool nested_name_p;
17422   cp_token *next_token;
17423
17424   /* The common case is that this is not a constructor declarator, so
17425      try to avoid doing lots of work if at all possible.  It's not
17426      valid declare a constructor at function scope.  */
17427   if (parser->in_function_body)
17428     return false;
17429   /* And only certain tokens can begin a constructor declarator.  */
17430   next_token = cp_lexer_peek_token (parser->lexer);
17431   if (next_token->type != CPP_NAME
17432       && next_token->type != CPP_SCOPE
17433       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17434       && next_token->type != CPP_TEMPLATE_ID)
17435     return false;
17436
17437   /* Parse tentatively; we are going to roll back all of the tokens
17438      consumed here.  */
17439   cp_parser_parse_tentatively (parser);
17440   /* Assume that we are looking at a constructor declarator.  */
17441   constructor_p = true;
17442
17443   /* Look for the optional `::' operator.  */
17444   cp_parser_global_scope_opt (parser,
17445                               /*current_scope_valid_p=*/false);
17446   /* Look for the nested-name-specifier.  */
17447   nested_name_p
17448     = (cp_parser_nested_name_specifier_opt (parser,
17449                                             /*typename_keyword_p=*/false,
17450                                             /*check_dependency_p=*/false,
17451                                             /*type_p=*/false,
17452                                             /*is_declaration=*/false)
17453        != NULL_TREE);
17454   /* Outside of a class-specifier, there must be a
17455      nested-name-specifier.  */
17456   if (!nested_name_p &&
17457       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17458        || friend_p))
17459     constructor_p = false;
17460   /* If we still think that this might be a constructor-declarator,
17461      look for a class-name.  */
17462   if (constructor_p)
17463     {
17464       /* If we have:
17465
17466            template <typename T> struct S { S(); };
17467            template <typename T> S<T>::S ();
17468
17469          we must recognize that the nested `S' names a class.
17470          Similarly, for:
17471
17472            template <typename T> S<T>::S<T> ();
17473
17474          we must recognize that the nested `S' names a template.  */
17475       type_decl = cp_parser_class_name (parser,
17476                                         /*typename_keyword_p=*/false,
17477                                         /*template_keyword_p=*/false,
17478                                         none_type,
17479                                         /*check_dependency_p=*/false,
17480                                         /*class_head_p=*/false,
17481                                         /*is_declaration=*/false);
17482       /* If there was no class-name, then this is not a constructor.  */
17483       constructor_p = !cp_parser_error_occurred (parser);
17484     }
17485
17486   /* If we're still considering a constructor, we have to see a `(',
17487      to begin the parameter-declaration-clause, followed by either a
17488      `)', an `...', or a decl-specifier.  We need to check for a
17489      type-specifier to avoid being fooled into thinking that:
17490
17491        S::S (f) (int);
17492
17493      is a constructor.  (It is actually a function named `f' that
17494      takes one parameter (of type `int') and returns a value of type
17495      `S::S'.  */
17496   if (constructor_p
17497       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17498     {
17499       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17500           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17501           /* A parameter declaration begins with a decl-specifier,
17502              which is either the "attribute" keyword, a storage class
17503              specifier, or (usually) a type-specifier.  */
17504           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17505         {
17506           tree type;
17507           tree pushed_scope = NULL_TREE;
17508           unsigned saved_num_template_parameter_lists;
17509
17510           /* Names appearing in the type-specifier should be looked up
17511              in the scope of the class.  */
17512           if (current_class_type)
17513             type = NULL_TREE;
17514           else
17515             {
17516               type = TREE_TYPE (type_decl);
17517               if (TREE_CODE (type) == TYPENAME_TYPE)
17518                 {
17519                   type = resolve_typename_type (type,
17520                                                 /*only_current_p=*/false);
17521                   if (TREE_CODE (type) == TYPENAME_TYPE)
17522                     {
17523                       cp_parser_abort_tentative_parse (parser);
17524                       return false;
17525                     }
17526                 }
17527               pushed_scope = push_scope (type);
17528             }
17529
17530           /* Inside the constructor parameter list, surrounding
17531              template-parameter-lists do not apply.  */
17532           saved_num_template_parameter_lists
17533             = parser->num_template_parameter_lists;
17534           parser->num_template_parameter_lists = 0;
17535
17536           /* Look for the type-specifier.  */
17537           cp_parser_type_specifier (parser,
17538                                     CP_PARSER_FLAGS_NONE,
17539                                     /*decl_specs=*/NULL,
17540                                     /*is_declarator=*/true,
17541                                     /*declares_class_or_enum=*/NULL,
17542                                     /*is_cv_qualifier=*/NULL);
17543
17544           parser->num_template_parameter_lists
17545             = saved_num_template_parameter_lists;
17546
17547           /* Leave the scope of the class.  */
17548           if (pushed_scope)
17549             pop_scope (pushed_scope);
17550
17551           constructor_p = !cp_parser_error_occurred (parser);
17552         }
17553     }
17554   else
17555     constructor_p = false;
17556   /* We did not really want to consume any tokens.  */
17557   cp_parser_abort_tentative_parse (parser);
17558
17559   return constructor_p;
17560 }
17561
17562 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17563    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17564    they must be performed once we are in the scope of the function.
17565
17566    Returns the function defined.  */
17567
17568 static tree
17569 cp_parser_function_definition_from_specifiers_and_declarator
17570   (cp_parser* parser,
17571    cp_decl_specifier_seq *decl_specifiers,
17572    tree attributes,
17573    const cp_declarator *declarator)
17574 {
17575   tree fn;
17576   bool success_p;
17577
17578   /* Begin the function-definition.  */
17579   success_p = start_function (decl_specifiers, declarator, attributes);
17580
17581   /* The things we're about to see are not directly qualified by any
17582      template headers we've seen thus far.  */
17583   reset_specialization ();
17584
17585   /* If there were names looked up in the decl-specifier-seq that we
17586      did not check, check them now.  We must wait until we are in the
17587      scope of the function to perform the checks, since the function
17588      might be a friend.  */
17589   perform_deferred_access_checks ();
17590
17591   if (!success_p)
17592     {
17593       /* Skip the entire function.  */
17594       cp_parser_skip_to_end_of_block_or_statement (parser);
17595       fn = error_mark_node;
17596     }
17597   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17598     {
17599       /* Seen already, skip it.  An error message has already been output.  */
17600       cp_parser_skip_to_end_of_block_or_statement (parser);
17601       fn = current_function_decl;
17602       current_function_decl = NULL_TREE;
17603       /* If this is a function from a class, pop the nested class.  */
17604       if (current_class_name)
17605         pop_nested_class ();
17606     }
17607   else
17608     fn = cp_parser_function_definition_after_declarator (parser,
17609                                                          /*inline_p=*/false);
17610
17611   return fn;
17612 }
17613
17614 /* Parse the part of a function-definition that follows the
17615    declarator.  INLINE_P is TRUE iff this function is an inline
17616    function defined with a class-specifier.
17617
17618    Returns the function defined.  */
17619
17620 static tree
17621 cp_parser_function_definition_after_declarator (cp_parser* parser,
17622                                                 bool inline_p)
17623 {
17624   tree fn;
17625   bool ctor_initializer_p = false;
17626   bool saved_in_unbraced_linkage_specification_p;
17627   bool saved_in_function_body;
17628   unsigned saved_num_template_parameter_lists;
17629   cp_token *token;
17630
17631   saved_in_function_body = parser->in_function_body;
17632   parser->in_function_body = true;
17633   /* If the next token is `return', then the code may be trying to
17634      make use of the "named return value" extension that G++ used to
17635      support.  */
17636   token = cp_lexer_peek_token (parser->lexer);
17637   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17638     {
17639       /* Consume the `return' keyword.  */
17640       cp_lexer_consume_token (parser->lexer);
17641       /* Look for the identifier that indicates what value is to be
17642          returned.  */
17643       cp_parser_identifier (parser);
17644       /* Issue an error message.  */
17645       error ("%Hnamed return values are no longer supported",
17646              &token->location);
17647       /* Skip tokens until we reach the start of the function body.  */
17648       while (true)
17649         {
17650           cp_token *token = cp_lexer_peek_token (parser->lexer);
17651           if (token->type == CPP_OPEN_BRACE
17652               || token->type == CPP_EOF
17653               || token->type == CPP_PRAGMA_EOL)
17654             break;
17655           cp_lexer_consume_token (parser->lexer);
17656         }
17657     }
17658   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17659      anything declared inside `f'.  */
17660   saved_in_unbraced_linkage_specification_p
17661     = parser->in_unbraced_linkage_specification_p;
17662   parser->in_unbraced_linkage_specification_p = false;
17663   /* Inside the function, surrounding template-parameter-lists do not
17664      apply.  */
17665   saved_num_template_parameter_lists
17666     = parser->num_template_parameter_lists;
17667   parser->num_template_parameter_lists = 0;
17668   /* If the next token is `try', then we are looking at a
17669      function-try-block.  */
17670   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17671     ctor_initializer_p = cp_parser_function_try_block (parser);
17672   /* A function-try-block includes the function-body, so we only do
17673      this next part if we're not processing a function-try-block.  */
17674   else
17675     ctor_initializer_p
17676       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17677
17678   /* Finish the function.  */
17679   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17680                         (inline_p ? 2 : 0));
17681   /* Generate code for it, if necessary.  */
17682   expand_or_defer_fn (fn);
17683   /* Restore the saved values.  */
17684   parser->in_unbraced_linkage_specification_p
17685     = saved_in_unbraced_linkage_specification_p;
17686   parser->num_template_parameter_lists
17687     = saved_num_template_parameter_lists;
17688   parser->in_function_body = saved_in_function_body;
17689
17690   return fn;
17691 }
17692
17693 /* Parse a template-declaration, assuming that the `export' (and
17694    `extern') keywords, if present, has already been scanned.  MEMBER_P
17695    is as for cp_parser_template_declaration.  */
17696
17697 static void
17698 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17699 {
17700   tree decl = NULL_TREE;
17701   VEC (deferred_access_check,gc) *checks;
17702   tree parameter_list;
17703   bool friend_p = false;
17704   bool need_lang_pop;
17705   cp_token *token;
17706
17707   /* Look for the `template' keyword.  */
17708   token = cp_lexer_peek_token (parser->lexer);
17709   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17710     return;
17711
17712   /* And the `<'.  */
17713   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17714     return;
17715   if (at_class_scope_p () && current_function_decl)
17716     {
17717       /* 14.5.2.2 [temp.mem]
17718
17719          A local class shall not have member templates.  */
17720       error ("%Hinvalid declaration of member template in local class",
17721              &token->location);
17722       cp_parser_skip_to_end_of_block_or_statement (parser);
17723       return;
17724     }
17725   /* [temp]
17726
17727      A template ... shall not have C linkage.  */
17728   if (current_lang_name == lang_name_c)
17729     {
17730       error ("%Htemplate with C linkage", &token->location);
17731       /* Give it C++ linkage to avoid confusing other parts of the
17732          front end.  */
17733       push_lang_context (lang_name_cplusplus);
17734       need_lang_pop = true;
17735     }
17736   else
17737     need_lang_pop = false;
17738
17739   /* We cannot perform access checks on the template parameter
17740      declarations until we know what is being declared, just as we
17741      cannot check the decl-specifier list.  */
17742   push_deferring_access_checks (dk_deferred);
17743
17744   /* If the next token is `>', then we have an invalid
17745      specialization.  Rather than complain about an invalid template
17746      parameter, issue an error message here.  */
17747   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17748     {
17749       cp_parser_error (parser, "invalid explicit specialization");
17750       begin_specialization ();
17751       parameter_list = NULL_TREE;
17752     }
17753   else
17754     /* Parse the template parameters.  */
17755     parameter_list = cp_parser_template_parameter_list (parser);
17756
17757   /* Get the deferred access checks from the parameter list.  These
17758      will be checked once we know what is being declared, as for a
17759      member template the checks must be performed in the scope of the
17760      class containing the member.  */
17761   checks = get_deferred_access_checks ();
17762
17763   /* Look for the `>'.  */
17764   cp_parser_skip_to_end_of_template_parameter_list (parser);
17765   /* We just processed one more parameter list.  */
17766   ++parser->num_template_parameter_lists;
17767   /* If the next token is `template', there are more template
17768      parameters.  */
17769   if (cp_lexer_next_token_is_keyword (parser->lexer,
17770                                       RID_TEMPLATE))
17771     cp_parser_template_declaration_after_export (parser, member_p);
17772   else
17773     {
17774       /* There are no access checks when parsing a template, as we do not
17775          know if a specialization will be a friend.  */
17776       push_deferring_access_checks (dk_no_check);
17777       token = cp_lexer_peek_token (parser->lexer);
17778       decl = cp_parser_single_declaration (parser,
17779                                            checks,
17780                                            member_p,
17781                                            /*explicit_specialization_p=*/false,
17782                                            &friend_p);
17783       pop_deferring_access_checks ();
17784
17785       /* If this is a member template declaration, let the front
17786          end know.  */
17787       if (member_p && !friend_p && decl)
17788         {
17789           if (TREE_CODE (decl) == TYPE_DECL)
17790             cp_parser_check_access_in_redeclaration (decl, token->location);
17791
17792           decl = finish_member_template_decl (decl);
17793         }
17794       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17795         make_friend_class (current_class_type, TREE_TYPE (decl),
17796                            /*complain=*/true);
17797     }
17798   /* We are done with the current parameter list.  */
17799   --parser->num_template_parameter_lists;
17800
17801   pop_deferring_access_checks ();
17802
17803   /* Finish up.  */
17804   finish_template_decl (parameter_list);
17805
17806   /* Register member declarations.  */
17807   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17808     finish_member_declaration (decl);
17809   /* For the erroneous case of a template with C linkage, we pushed an
17810      implicit C++ linkage scope; exit that scope now.  */
17811   if (need_lang_pop)
17812     pop_lang_context ();
17813   /* If DECL is a function template, we must return to parse it later.
17814      (Even though there is no definition, there might be default
17815      arguments that need handling.)  */
17816   if (member_p && decl
17817       && (TREE_CODE (decl) == FUNCTION_DECL
17818           || DECL_FUNCTION_TEMPLATE_P (decl)))
17819     TREE_VALUE (parser->unparsed_functions_queues)
17820       = tree_cons (NULL_TREE, decl,
17821                    TREE_VALUE (parser->unparsed_functions_queues));
17822 }
17823
17824 /* Perform the deferred access checks from a template-parameter-list.
17825    CHECKS is a TREE_LIST of access checks, as returned by
17826    get_deferred_access_checks.  */
17827
17828 static void
17829 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17830 {
17831   ++processing_template_parmlist;
17832   perform_access_checks (checks);
17833   --processing_template_parmlist;
17834 }
17835
17836 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17837    `function-definition' sequence.  MEMBER_P is true, this declaration
17838    appears in a class scope.
17839
17840    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17841    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17842
17843 static tree
17844 cp_parser_single_declaration (cp_parser* parser,
17845                               VEC (deferred_access_check,gc)* checks,
17846                               bool member_p,
17847                               bool explicit_specialization_p,
17848                               bool* friend_p)
17849 {
17850   int declares_class_or_enum;
17851   tree decl = NULL_TREE;
17852   cp_decl_specifier_seq decl_specifiers;
17853   bool function_definition_p = false;
17854   cp_token *decl_spec_token_start;
17855
17856   /* This function is only used when processing a template
17857      declaration.  */
17858   gcc_assert (innermost_scope_kind () == sk_template_parms
17859               || innermost_scope_kind () == sk_template_spec);
17860
17861   /* Defer access checks until we know what is being declared.  */
17862   push_deferring_access_checks (dk_deferred);
17863
17864   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17865      alternative.  */
17866   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17867   cp_parser_decl_specifier_seq (parser,
17868                                 CP_PARSER_FLAGS_OPTIONAL,
17869                                 &decl_specifiers,
17870                                 &declares_class_or_enum);
17871   if (friend_p)
17872     *friend_p = cp_parser_friend_p (&decl_specifiers);
17873
17874   /* There are no template typedefs.  */
17875   if (decl_specifiers.specs[(int) ds_typedef])
17876     {
17877       error ("%Htemplate declaration of %qs",
17878              &decl_spec_token_start->location, "typedef");
17879       decl = error_mark_node;
17880     }
17881
17882   /* Gather up the access checks that occurred the
17883      decl-specifier-seq.  */
17884   stop_deferring_access_checks ();
17885
17886   /* Check for the declaration of a template class.  */
17887   if (declares_class_or_enum)
17888     {
17889       if (cp_parser_declares_only_class_p (parser))
17890         {
17891           decl = shadow_tag (&decl_specifiers);
17892
17893           /* In this case:
17894
17895                struct C {
17896                  friend template <typename T> struct A<T>::B;
17897                };
17898
17899              A<T>::B will be represented by a TYPENAME_TYPE, and
17900              therefore not recognized by shadow_tag.  */
17901           if (friend_p && *friend_p
17902               && !decl
17903               && decl_specifiers.type
17904               && TYPE_P (decl_specifiers.type))
17905             decl = decl_specifiers.type;
17906
17907           if (decl && decl != error_mark_node)
17908             decl = TYPE_NAME (decl);
17909           else
17910             decl = error_mark_node;
17911
17912           /* Perform access checks for template parameters.  */
17913           cp_parser_perform_template_parameter_access_checks (checks);
17914         }
17915     }
17916   /* If it's not a template class, try for a template function.  If
17917      the next token is a `;', then this declaration does not declare
17918      anything.  But, if there were errors in the decl-specifiers, then
17919      the error might well have come from an attempted class-specifier.
17920      In that case, there's no need to warn about a missing declarator.  */
17921   if (!decl
17922       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17923           || decl_specifiers.type != error_mark_node))
17924     {
17925       decl = cp_parser_init_declarator (parser,
17926                                         &decl_specifiers,
17927                                         checks,
17928                                         /*function_definition_allowed_p=*/true,
17929                                         member_p,
17930                                         declares_class_or_enum,
17931                                         &function_definition_p);
17932
17933     /* 7.1.1-1 [dcl.stc]
17934
17935        A storage-class-specifier shall not be specified in an explicit
17936        specialization...  */
17937     if (decl
17938         && explicit_specialization_p
17939         && decl_specifiers.storage_class != sc_none)
17940       {
17941         error ("%Hexplicit template specialization cannot have a storage class",
17942                &decl_spec_token_start->location);
17943         decl = error_mark_node;
17944       }
17945     }
17946
17947   pop_deferring_access_checks ();
17948
17949   /* Clear any current qualification; whatever comes next is the start
17950      of something new.  */
17951   parser->scope = NULL_TREE;
17952   parser->qualifying_scope = NULL_TREE;
17953   parser->object_scope = NULL_TREE;
17954   /* Look for a trailing `;' after the declaration.  */
17955   if (!function_definition_p
17956       && (decl == error_mark_node
17957           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17958     cp_parser_skip_to_end_of_block_or_statement (parser);
17959
17960   return decl;
17961 }
17962
17963 /* Parse a cast-expression that is not the operand of a unary "&".  */
17964
17965 static tree
17966 cp_parser_simple_cast_expression (cp_parser *parser)
17967 {
17968   return cp_parser_cast_expression (parser, /*address_p=*/false,
17969                                     /*cast_p=*/false, NULL);
17970 }
17971
17972 /* Parse a functional cast to TYPE.  Returns an expression
17973    representing the cast.  */
17974
17975 static tree
17976 cp_parser_functional_cast (cp_parser* parser, tree type)
17977 {
17978   tree expression_list;
17979   tree cast;
17980   bool nonconst_p;
17981
17982   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17983     {
17984       maybe_warn_cpp0x ("extended initializer lists");
17985       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17986       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17987       if (TREE_CODE (type) == TYPE_DECL)
17988         type = TREE_TYPE (type);
17989       return finish_compound_literal (type, expression_list);
17990     }
17991
17992   expression_list
17993     = cp_parser_parenthesized_expression_list (parser, false,
17994                                                /*cast_p=*/true,
17995                                                /*allow_expansion_p=*/true,
17996                                                /*non_constant_p=*/NULL);
17997
17998   cast = build_functional_cast (type, expression_list,
17999                                 tf_warning_or_error);
18000   /* [expr.const]/1: In an integral constant expression "only type
18001      conversions to integral or enumeration type can be used".  */
18002   if (TREE_CODE (type) == TYPE_DECL)
18003     type = TREE_TYPE (type);
18004   if (cast != error_mark_node
18005       && !cast_valid_in_integral_constant_expression_p (type)
18006       && (cp_parser_non_integral_constant_expression
18007           (parser, "a call to a constructor")))
18008     return error_mark_node;
18009   return cast;
18010 }
18011
18012 /* Save the tokens that make up the body of a member function defined
18013    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18014    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18015    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18016    for the member function.  */
18017
18018 static tree
18019 cp_parser_save_member_function_body (cp_parser* parser,
18020                                      cp_decl_specifier_seq *decl_specifiers,
18021                                      cp_declarator *declarator,
18022                                      tree attributes)
18023 {
18024   cp_token *first;
18025   cp_token *last;
18026   tree fn;
18027
18028   /* Create the function-declaration.  */
18029   fn = start_method (decl_specifiers, declarator, attributes);
18030   /* If something went badly wrong, bail out now.  */
18031   if (fn == error_mark_node)
18032     {
18033       /* If there's a function-body, skip it.  */
18034       if (cp_parser_token_starts_function_definition_p
18035           (cp_lexer_peek_token (parser->lexer)))
18036         cp_parser_skip_to_end_of_block_or_statement (parser);
18037       return error_mark_node;
18038     }
18039
18040   /* Remember it, if there default args to post process.  */
18041   cp_parser_save_default_args (parser, fn);
18042
18043   /* Save away the tokens that make up the body of the
18044      function.  */
18045   first = parser->lexer->next_token;
18046   /* We can have braced-init-list mem-initializers before the fn body.  */
18047   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18048     {
18049       cp_lexer_consume_token (parser->lexer);
18050       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18051              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18052         {
18053           /* cache_group will stop after an un-nested { } pair, too.  */
18054           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18055             break;
18056
18057           /* variadic mem-inits have ... after the ')'.  */
18058           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18059             cp_lexer_consume_token (parser->lexer);
18060         }
18061     }
18062   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18063   /* Handle function try blocks.  */
18064   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18065     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18066   last = parser->lexer->next_token;
18067
18068   /* Save away the inline definition; we will process it when the
18069      class is complete.  */
18070   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18071   DECL_PENDING_INLINE_P (fn) = 1;
18072
18073   /* We need to know that this was defined in the class, so that
18074      friend templates are handled correctly.  */
18075   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18076
18077   /* We're done with the inline definition.  */
18078   finish_method (fn);
18079
18080   /* Add FN to the queue of functions to be parsed later.  */
18081   TREE_VALUE (parser->unparsed_functions_queues)
18082     = tree_cons (NULL_TREE, fn,
18083                  TREE_VALUE (parser->unparsed_functions_queues));
18084
18085   return fn;
18086 }
18087
18088 /* Parse a template-argument-list, as well as the trailing ">" (but
18089    not the opening ">").  See cp_parser_template_argument_list for the
18090    return value.  */
18091
18092 static tree
18093 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18094 {
18095   tree arguments;
18096   tree saved_scope;
18097   tree saved_qualifying_scope;
18098   tree saved_object_scope;
18099   bool saved_greater_than_is_operator_p;
18100   bool saved_skip_evaluation;
18101
18102   /* [temp.names]
18103
18104      When parsing a template-id, the first non-nested `>' is taken as
18105      the end of the template-argument-list rather than a greater-than
18106      operator.  */
18107   saved_greater_than_is_operator_p
18108     = parser->greater_than_is_operator_p;
18109   parser->greater_than_is_operator_p = false;
18110   /* Parsing the argument list may modify SCOPE, so we save it
18111      here.  */
18112   saved_scope = parser->scope;
18113   saved_qualifying_scope = parser->qualifying_scope;
18114   saved_object_scope = parser->object_scope;
18115   /* We need to evaluate the template arguments, even though this
18116      template-id may be nested within a "sizeof".  */
18117   saved_skip_evaluation = skip_evaluation;
18118   skip_evaluation = false;
18119   /* Parse the template-argument-list itself.  */
18120   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18121       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18122     arguments = NULL_TREE;
18123   else
18124     arguments = cp_parser_template_argument_list (parser);
18125   /* Look for the `>' that ends the template-argument-list. If we find
18126      a '>>' instead, it's probably just a typo.  */
18127   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18128     {
18129       if (cxx_dialect != cxx98)
18130         {
18131           /* In C++0x, a `>>' in a template argument list or cast
18132              expression is considered to be two separate `>'
18133              tokens. So, change the current token to a `>', but don't
18134              consume it: it will be consumed later when the outer
18135              template argument list (or cast expression) is parsed.
18136              Note that this replacement of `>' for `>>' is necessary
18137              even if we are parsing tentatively: in the tentative
18138              case, after calling
18139              cp_parser_enclosed_template_argument_list we will always
18140              throw away all of the template arguments and the first
18141              closing `>', either because the template argument list
18142              was erroneous or because we are replacing those tokens
18143              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18144              not have been thrown away) is needed either to close an
18145              outer template argument list or to complete a new-style
18146              cast.  */
18147           cp_token *token = cp_lexer_peek_token (parser->lexer);
18148           token->type = CPP_GREATER;
18149         }
18150       else if (!saved_greater_than_is_operator_p)
18151         {
18152           /* If we're in a nested template argument list, the '>>' has
18153             to be a typo for '> >'. We emit the error message, but we
18154             continue parsing and we push a '>' as next token, so that
18155             the argument list will be parsed correctly.  Note that the
18156             global source location is still on the token before the
18157             '>>', so we need to say explicitly where we want it.  */
18158           cp_token *token = cp_lexer_peek_token (parser->lexer);
18159           error ("%H%<>>%> should be %<> >%> "
18160                  "within a nested template argument list",
18161                  &token->location);
18162
18163           token->type = CPP_GREATER;
18164         }
18165       else
18166         {
18167           /* If this is not a nested template argument list, the '>>'
18168             is a typo for '>'. Emit an error message and continue.
18169             Same deal about the token location, but here we can get it
18170             right by consuming the '>>' before issuing the diagnostic.  */
18171           cp_token *token = cp_lexer_consume_token (parser->lexer);
18172           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18173                  "a template argument list", &token->location);
18174         }
18175     }
18176   else
18177     cp_parser_skip_to_end_of_template_parameter_list (parser);
18178   /* The `>' token might be a greater-than operator again now.  */
18179   parser->greater_than_is_operator_p
18180     = saved_greater_than_is_operator_p;
18181   /* Restore the SAVED_SCOPE.  */
18182   parser->scope = saved_scope;
18183   parser->qualifying_scope = saved_qualifying_scope;
18184   parser->object_scope = saved_object_scope;
18185   skip_evaluation = saved_skip_evaluation;
18186
18187   return arguments;
18188 }
18189
18190 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18191    arguments, or the body of the function have not yet been parsed,
18192    parse them now.  */
18193
18194 static void
18195 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18196 {
18197   /* If this member is a template, get the underlying
18198      FUNCTION_DECL.  */
18199   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18200     member_function = DECL_TEMPLATE_RESULT (member_function);
18201
18202   /* There should not be any class definitions in progress at this
18203      point; the bodies of members are only parsed outside of all class
18204      definitions.  */
18205   gcc_assert (parser->num_classes_being_defined == 0);
18206   /* While we're parsing the member functions we might encounter more
18207      classes.  We want to handle them right away, but we don't want
18208      them getting mixed up with functions that are currently in the
18209      queue.  */
18210   parser->unparsed_functions_queues
18211     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18212
18213   /* Make sure that any template parameters are in scope.  */
18214   maybe_begin_member_template_processing (member_function);
18215
18216   /* If the body of the function has not yet been parsed, parse it
18217      now.  */
18218   if (DECL_PENDING_INLINE_P (member_function))
18219     {
18220       tree function_scope;
18221       cp_token_cache *tokens;
18222
18223       /* The function is no longer pending; we are processing it.  */
18224       tokens = DECL_PENDING_INLINE_INFO (member_function);
18225       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18226       DECL_PENDING_INLINE_P (member_function) = 0;
18227
18228       /* If this is a local class, enter the scope of the containing
18229          function.  */
18230       function_scope = current_function_decl;
18231       if (function_scope)
18232         push_function_context ();
18233
18234       /* Push the body of the function onto the lexer stack.  */
18235       cp_parser_push_lexer_for_tokens (parser, tokens);
18236
18237       /* Let the front end know that we going to be defining this
18238          function.  */
18239       start_preparsed_function (member_function, NULL_TREE,
18240                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18241
18242       /* Don't do access checking if it is a templated function.  */
18243       if (processing_template_decl)
18244         push_deferring_access_checks (dk_no_check);
18245
18246       /* Now, parse the body of the function.  */
18247       cp_parser_function_definition_after_declarator (parser,
18248                                                       /*inline_p=*/true);
18249
18250       if (processing_template_decl)
18251         pop_deferring_access_checks ();
18252
18253       /* Leave the scope of the containing function.  */
18254       if (function_scope)
18255         pop_function_context ();
18256       cp_parser_pop_lexer (parser);
18257     }
18258
18259   /* Remove any template parameters from the symbol table.  */
18260   maybe_end_member_template_processing ();
18261
18262   /* Restore the queue.  */
18263   parser->unparsed_functions_queues
18264     = TREE_CHAIN (parser->unparsed_functions_queues);
18265 }
18266
18267 /* If DECL contains any default args, remember it on the unparsed
18268    functions queue.  */
18269
18270 static void
18271 cp_parser_save_default_args (cp_parser* parser, tree decl)
18272 {
18273   tree probe;
18274
18275   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18276        probe;
18277        probe = TREE_CHAIN (probe))
18278     if (TREE_PURPOSE (probe))
18279       {
18280         TREE_PURPOSE (parser->unparsed_functions_queues)
18281           = tree_cons (current_class_type, decl,
18282                        TREE_PURPOSE (parser->unparsed_functions_queues));
18283         break;
18284       }
18285 }
18286
18287 /* FN is a FUNCTION_DECL which may contains a parameter with an
18288    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18289    assumes that the current scope is the scope in which the default
18290    argument should be processed.  */
18291
18292 static void
18293 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18294 {
18295   bool saved_local_variables_forbidden_p;
18296   tree parm;
18297
18298   /* While we're parsing the default args, we might (due to the
18299      statement expression extension) encounter more classes.  We want
18300      to handle them right away, but we don't want them getting mixed
18301      up with default args that are currently in the queue.  */
18302   parser->unparsed_functions_queues
18303     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18304
18305   /* Local variable names (and the `this' keyword) may not appear
18306      in a default argument.  */
18307   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18308   parser->local_variables_forbidden_p = true;
18309
18310   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18311        parm;
18312        parm = TREE_CHAIN (parm))
18313     {
18314       cp_token_cache *tokens;
18315       tree default_arg = TREE_PURPOSE (parm);
18316       tree parsed_arg;
18317       VEC(tree,gc) *insts;
18318       tree copy;
18319       unsigned ix;
18320
18321       if (!default_arg)
18322         continue;
18323
18324       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18325         /* This can happen for a friend declaration for a function
18326            already declared with default arguments.  */
18327         continue;
18328
18329        /* Push the saved tokens for the default argument onto the parser's
18330           lexer stack.  */
18331       tokens = DEFARG_TOKENS (default_arg);
18332       cp_parser_push_lexer_for_tokens (parser, tokens);
18333
18334       /* Parse the assignment-expression.  */
18335       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18336       if (parsed_arg == error_mark_node)
18337         {
18338           cp_parser_pop_lexer (parser);
18339           continue;
18340         }
18341
18342       if (!processing_template_decl)
18343         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18344
18345       TREE_PURPOSE (parm) = parsed_arg;
18346
18347       /* Update any instantiations we've already created.  */
18348       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18349            VEC_iterate (tree, insts, ix, copy); ix++)
18350         TREE_PURPOSE (copy) = parsed_arg;
18351
18352       /* If the token stream has not been completely used up, then
18353          there was extra junk after the end of the default
18354          argument.  */
18355       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18356         cp_parser_error (parser, "expected %<,%>");
18357
18358       /* Revert to the main lexer.  */
18359       cp_parser_pop_lexer (parser);
18360     }
18361
18362   /* Make sure no default arg is missing.  */
18363   check_default_args (fn);
18364
18365   /* Restore the state of local_variables_forbidden_p.  */
18366   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18367
18368   /* Restore the queue.  */
18369   parser->unparsed_functions_queues
18370     = TREE_CHAIN (parser->unparsed_functions_queues);
18371 }
18372
18373 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18374    either a TYPE or an expression, depending on the form of the
18375    input.  The KEYWORD indicates which kind of expression we have
18376    encountered.  */
18377
18378 static tree
18379 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18380 {
18381   tree expr = NULL_TREE;
18382   const char *saved_message;
18383   char *tmp;
18384   bool saved_integral_constant_expression_p;
18385   bool saved_non_integral_constant_expression_p;
18386   bool pack_expansion_p = false;
18387
18388   /* Types cannot be defined in a `sizeof' expression.  Save away the
18389      old message.  */
18390   saved_message = parser->type_definition_forbidden_message;
18391   /* And create the new one.  */
18392   tmp = concat ("types may not be defined in %<",
18393                 IDENTIFIER_POINTER (ridpointers[keyword]),
18394                 "%> expressions", NULL);
18395   parser->type_definition_forbidden_message = tmp;
18396
18397   /* The restrictions on constant-expressions do not apply inside
18398      sizeof expressions.  */
18399   saved_integral_constant_expression_p
18400     = parser->integral_constant_expression_p;
18401   saved_non_integral_constant_expression_p
18402     = parser->non_integral_constant_expression_p;
18403   parser->integral_constant_expression_p = false;
18404
18405   /* If it's a `...', then we are computing the length of a parameter
18406      pack.  */
18407   if (keyword == RID_SIZEOF
18408       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18409     {
18410       /* Consume the `...'.  */
18411       cp_lexer_consume_token (parser->lexer);
18412       maybe_warn_variadic_templates ();
18413
18414       /* Note that this is an expansion.  */
18415       pack_expansion_p = true;
18416     }
18417
18418   /* Do not actually evaluate the expression.  */
18419   ++skip_evaluation;
18420   /* If it's a `(', then we might be looking at the type-id
18421      construction.  */
18422   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18423     {
18424       tree type;
18425       bool saved_in_type_id_in_expr_p;
18426
18427       /* We can't be sure yet whether we're looking at a type-id or an
18428          expression.  */
18429       cp_parser_parse_tentatively (parser);
18430       /* Consume the `('.  */
18431       cp_lexer_consume_token (parser->lexer);
18432       /* Parse the type-id.  */
18433       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18434       parser->in_type_id_in_expr_p = true;
18435       type = cp_parser_type_id (parser);
18436       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18437       /* Now, look for the trailing `)'.  */
18438       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18439       /* If all went well, then we're done.  */
18440       if (cp_parser_parse_definitely (parser))
18441         {
18442           cp_decl_specifier_seq decl_specs;
18443
18444           /* Build a trivial decl-specifier-seq.  */
18445           clear_decl_specs (&decl_specs);
18446           decl_specs.type = type;
18447
18448           /* Call grokdeclarator to figure out what type this is.  */
18449           expr = grokdeclarator (NULL,
18450                                  &decl_specs,
18451                                  TYPENAME,
18452                                  /*initialized=*/0,
18453                                  /*attrlist=*/NULL);
18454         }
18455     }
18456
18457   /* If the type-id production did not work out, then we must be
18458      looking at the unary-expression production.  */
18459   if (!expr)
18460     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18461                                        /*cast_p=*/false, NULL);
18462
18463   if (pack_expansion_p)
18464     /* Build a pack expansion. */
18465     expr = make_pack_expansion (expr);
18466
18467   /* Go back to evaluating expressions.  */
18468   --skip_evaluation;
18469
18470   /* Free the message we created.  */
18471   free (tmp);
18472   /* And restore the old one.  */
18473   parser->type_definition_forbidden_message = saved_message;
18474   parser->integral_constant_expression_p
18475     = saved_integral_constant_expression_p;
18476   parser->non_integral_constant_expression_p
18477     = saved_non_integral_constant_expression_p;
18478
18479   return expr;
18480 }
18481
18482 /* If the current declaration has no declarator, return true.  */
18483
18484 static bool
18485 cp_parser_declares_only_class_p (cp_parser *parser)
18486 {
18487   /* If the next token is a `;' or a `,' then there is no
18488      declarator.  */
18489   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18490           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18491 }
18492
18493 /* Update the DECL_SPECS to reflect the storage class indicated by
18494    KEYWORD.  */
18495
18496 static void
18497 cp_parser_set_storage_class (cp_parser *parser,
18498                              cp_decl_specifier_seq *decl_specs,
18499                              enum rid keyword,
18500                              location_t location)
18501 {
18502   cp_storage_class storage_class;
18503
18504   if (parser->in_unbraced_linkage_specification_p)
18505     {
18506       error ("%Hinvalid use of %qD in linkage specification",
18507              &location, ridpointers[keyword]);
18508       return;
18509     }
18510   else if (decl_specs->storage_class != sc_none)
18511     {
18512       decl_specs->conflicting_specifiers_p = true;
18513       return;
18514     }
18515
18516   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18517       && decl_specs->specs[(int) ds_thread])
18518     {
18519       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18520       decl_specs->specs[(int) ds_thread] = 0;
18521     }
18522
18523   switch (keyword)
18524     {
18525     case RID_AUTO:
18526       storage_class = sc_auto;
18527       break;
18528     case RID_REGISTER:
18529       storage_class = sc_register;
18530       break;
18531     case RID_STATIC:
18532       storage_class = sc_static;
18533       break;
18534     case RID_EXTERN:
18535       storage_class = sc_extern;
18536       break;
18537     case RID_MUTABLE:
18538       storage_class = sc_mutable;
18539       break;
18540     default:
18541       gcc_unreachable ();
18542     }
18543   decl_specs->storage_class = storage_class;
18544
18545   /* A storage class specifier cannot be applied alongside a typedef 
18546      specifier. If there is a typedef specifier present then set 
18547      conflicting_specifiers_p which will trigger an error later
18548      on in grokdeclarator. */
18549   if (decl_specs->specs[(int)ds_typedef])
18550     decl_specs->conflicting_specifiers_p = true;
18551 }
18552
18553 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18554    is true, the type is a user-defined type; otherwise it is a
18555    built-in type specified by a keyword.  */
18556
18557 static void
18558 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18559                               tree type_spec,
18560                               location_t location,
18561                               bool user_defined_p)
18562 {
18563   decl_specs->any_specifiers_p = true;
18564
18565   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18566      (with, for example, in "typedef int wchar_t;") we remember that
18567      this is what happened.  In system headers, we ignore these
18568      declarations so that G++ can work with system headers that are not
18569      C++-safe.  */
18570   if (decl_specs->specs[(int) ds_typedef]
18571       && !user_defined_p
18572       && (type_spec == boolean_type_node
18573           || type_spec == char16_type_node
18574           || type_spec == char32_type_node
18575           || type_spec == wchar_type_node)
18576       && (decl_specs->type
18577           || decl_specs->specs[(int) ds_long]
18578           || decl_specs->specs[(int) ds_short]
18579           || decl_specs->specs[(int) ds_unsigned]
18580           || decl_specs->specs[(int) ds_signed]))
18581     {
18582       decl_specs->redefined_builtin_type = type_spec;
18583       if (!decl_specs->type)
18584         {
18585           decl_specs->type = type_spec;
18586           decl_specs->user_defined_type_p = false;
18587           decl_specs->type_location = location;
18588         }
18589     }
18590   else if (decl_specs->type)
18591     decl_specs->multiple_types_p = true;
18592   else
18593     {
18594       decl_specs->type = type_spec;
18595       decl_specs->user_defined_type_p = user_defined_p;
18596       decl_specs->redefined_builtin_type = NULL_TREE;
18597       decl_specs->type_location = location;
18598     }
18599 }
18600
18601 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18602    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18603
18604 static bool
18605 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18606 {
18607   return decl_specifiers->specs[(int) ds_friend] != 0;
18608 }
18609
18610 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18611    issue an error message indicating that TOKEN_DESC was expected.
18612
18613    Returns the token consumed, if the token had the appropriate type.
18614    Otherwise, returns NULL.  */
18615
18616 static cp_token *
18617 cp_parser_require (cp_parser* parser,
18618                    enum cpp_ttype type,
18619                    const char* token_desc)
18620 {
18621   if (cp_lexer_next_token_is (parser->lexer, type))
18622     return cp_lexer_consume_token (parser->lexer);
18623   else
18624     {
18625       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18626       if (!cp_parser_simulate_error (parser))
18627         {
18628           char *message = concat ("expected ", token_desc, NULL);
18629           cp_parser_error (parser, message);
18630           free (message);
18631         }
18632       return NULL;
18633     }
18634 }
18635
18636 /* An error message is produced if the next token is not '>'.
18637    All further tokens are skipped until the desired token is
18638    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18639
18640 static void
18641 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18642 {
18643   /* Current level of '< ... >'.  */
18644   unsigned level = 0;
18645   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18646   unsigned nesting_depth = 0;
18647
18648   /* Are we ready, yet?  If not, issue error message.  */
18649   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18650     return;
18651
18652   /* Skip tokens until the desired token is found.  */
18653   while (true)
18654     {
18655       /* Peek at the next token.  */
18656       switch (cp_lexer_peek_token (parser->lexer)->type)
18657         {
18658         case CPP_LESS:
18659           if (!nesting_depth)
18660             ++level;
18661           break;
18662
18663         case CPP_RSHIFT:
18664           if (cxx_dialect == cxx98)
18665             /* C++0x views the `>>' operator as two `>' tokens, but
18666                C++98 does not. */
18667             break;
18668           else if (!nesting_depth && level-- == 0)
18669             {
18670               /* We've hit a `>>' where the first `>' closes the
18671                  template argument list, and the second `>' is
18672                  spurious.  Just consume the `>>' and stop; we've
18673                  already produced at least one error.  */
18674               cp_lexer_consume_token (parser->lexer);
18675               return;
18676             }
18677           /* Fall through for C++0x, so we handle the second `>' in
18678              the `>>'.  */
18679
18680         case CPP_GREATER:
18681           if (!nesting_depth && level-- == 0)
18682             {
18683               /* We've reached the token we want, consume it and stop.  */
18684               cp_lexer_consume_token (parser->lexer);
18685               return;
18686             }
18687           break;
18688
18689         case CPP_OPEN_PAREN:
18690         case CPP_OPEN_SQUARE:
18691           ++nesting_depth;
18692           break;
18693
18694         case CPP_CLOSE_PAREN:
18695         case CPP_CLOSE_SQUARE:
18696           if (nesting_depth-- == 0)
18697             return;
18698           break;
18699
18700         case CPP_EOF:
18701         case CPP_PRAGMA_EOL:
18702         case CPP_SEMICOLON:
18703         case CPP_OPEN_BRACE:
18704         case CPP_CLOSE_BRACE:
18705           /* The '>' was probably forgotten, don't look further.  */
18706           return;
18707
18708         default:
18709           break;
18710         }
18711
18712       /* Consume this token.  */
18713       cp_lexer_consume_token (parser->lexer);
18714     }
18715 }
18716
18717 /* If the next token is the indicated keyword, consume it.  Otherwise,
18718    issue an error message indicating that TOKEN_DESC was expected.
18719
18720    Returns the token consumed, if the token had the appropriate type.
18721    Otherwise, returns NULL.  */
18722
18723 static cp_token *
18724 cp_parser_require_keyword (cp_parser* parser,
18725                            enum rid keyword,
18726                            const char* token_desc)
18727 {
18728   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18729
18730   if (token && token->keyword != keyword)
18731     {
18732       dyn_string_t error_msg;
18733
18734       /* Format the error message.  */
18735       error_msg = dyn_string_new (0);
18736       dyn_string_append_cstr (error_msg, "expected ");
18737       dyn_string_append_cstr (error_msg, token_desc);
18738       cp_parser_error (parser, error_msg->s);
18739       dyn_string_delete (error_msg);
18740       return NULL;
18741     }
18742
18743   return token;
18744 }
18745
18746 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18747    function-definition.  */
18748
18749 static bool
18750 cp_parser_token_starts_function_definition_p (cp_token* token)
18751 {
18752   return (/* An ordinary function-body begins with an `{'.  */
18753           token->type == CPP_OPEN_BRACE
18754           /* A ctor-initializer begins with a `:'.  */
18755           || token->type == CPP_COLON
18756           /* A function-try-block begins with `try'.  */
18757           || token->keyword == RID_TRY
18758           /* The named return value extension begins with `return'.  */
18759           || token->keyword == RID_RETURN);
18760 }
18761
18762 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18763    definition.  */
18764
18765 static bool
18766 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18767 {
18768   cp_token *token;
18769
18770   token = cp_lexer_peek_token (parser->lexer);
18771   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18772 }
18773
18774 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18775    C++0x) ending a template-argument.  */
18776
18777 static bool
18778 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18779 {
18780   cp_token *token;
18781
18782   token = cp_lexer_peek_token (parser->lexer);
18783   return (token->type == CPP_COMMA 
18784           || token->type == CPP_GREATER
18785           || token->type == CPP_ELLIPSIS
18786           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18787 }
18788
18789 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18790    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18791
18792 static bool
18793 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18794                                                      size_t n)
18795 {
18796   cp_token *token;
18797
18798   token = cp_lexer_peek_nth_token (parser->lexer, n);
18799   if (token->type == CPP_LESS)
18800     return true;
18801   /* Check for the sequence `<::' in the original code. It would be lexed as
18802      `[:', where `[' is a digraph, and there is no whitespace before
18803      `:'.  */
18804   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18805     {
18806       cp_token *token2;
18807       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18808       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18809         return true;
18810     }
18811   return false;
18812 }
18813
18814 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18815    or none_type otherwise.  */
18816
18817 static enum tag_types
18818 cp_parser_token_is_class_key (cp_token* token)
18819 {
18820   switch (token->keyword)
18821     {
18822     case RID_CLASS:
18823       return class_type;
18824     case RID_STRUCT:
18825       return record_type;
18826     case RID_UNION:
18827       return union_type;
18828
18829     default:
18830       return none_type;
18831     }
18832 }
18833
18834 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18835
18836 static void
18837 cp_parser_check_class_key (enum tag_types class_key, tree type)
18838 {
18839   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18840     permerror (input_location, "%qs tag used in naming %q#T",
18841             class_key == union_type ? "union"
18842              : class_key == record_type ? "struct" : "class",
18843              type);
18844 }
18845
18846 /* Issue an error message if DECL is redeclared with different
18847    access than its original declaration [class.access.spec/3].
18848    This applies to nested classes and nested class templates.
18849    [class.mem/1].  */
18850
18851 static void
18852 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18853 {
18854   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18855     return;
18856
18857   if ((TREE_PRIVATE (decl)
18858        != (current_access_specifier == access_private_node))
18859       || (TREE_PROTECTED (decl)
18860           != (current_access_specifier == access_protected_node)))
18861     error ("%H%qD redeclared with different access", &location, decl);
18862 }
18863
18864 /* Look for the `template' keyword, as a syntactic disambiguator.
18865    Return TRUE iff it is present, in which case it will be
18866    consumed.  */
18867
18868 static bool
18869 cp_parser_optional_template_keyword (cp_parser *parser)
18870 {
18871   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18872     {
18873       /* The `template' keyword can only be used within templates;
18874          outside templates the parser can always figure out what is a
18875          template and what is not.  */
18876       if (!processing_template_decl)
18877         {
18878           cp_token *token = cp_lexer_peek_token (parser->lexer);
18879           error ("%H%<template%> (as a disambiguator) is only allowed "
18880                  "within templates", &token->location);
18881           /* If this part of the token stream is rescanned, the same
18882              error message would be generated.  So, we purge the token
18883              from the stream.  */
18884           cp_lexer_purge_token (parser->lexer);
18885           return false;
18886         }
18887       else
18888         {
18889           /* Consume the `template' keyword.  */
18890           cp_lexer_consume_token (parser->lexer);
18891           return true;
18892         }
18893     }
18894
18895   return false;
18896 }
18897
18898 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18899    set PARSER->SCOPE, and perform other related actions.  */
18900
18901 static void
18902 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18903 {
18904   int i;
18905   struct tree_check *check_value;
18906   deferred_access_check *chk;
18907   VEC (deferred_access_check,gc) *checks;
18908
18909   /* Get the stored value.  */
18910   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18911   /* Perform any access checks that were deferred.  */
18912   checks = check_value->checks;
18913   if (checks)
18914     {
18915       for (i = 0 ;
18916            VEC_iterate (deferred_access_check, checks, i, chk) ;
18917            ++i)
18918         {
18919           perform_or_defer_access_check (chk->binfo,
18920                                          chk->decl,
18921                                          chk->diag_decl);
18922         }
18923     }
18924   /* Set the scope from the stored value.  */
18925   parser->scope = check_value->value;
18926   parser->qualifying_scope = check_value->qualifying_scope;
18927   parser->object_scope = NULL_TREE;
18928 }
18929
18930 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18931    encounter the end of a block before what we were looking for.  */
18932
18933 static bool
18934 cp_parser_cache_group (cp_parser *parser,
18935                        enum cpp_ttype end,
18936                        unsigned depth)
18937 {
18938   while (true)
18939     {
18940       cp_token *token = cp_lexer_peek_token (parser->lexer);
18941
18942       /* Abort a parenthesized expression if we encounter a semicolon.  */
18943       if ((end == CPP_CLOSE_PAREN || depth == 0)
18944           && token->type == CPP_SEMICOLON)
18945         return true;
18946       /* If we've reached the end of the file, stop.  */
18947       if (token->type == CPP_EOF
18948           || (end != CPP_PRAGMA_EOL
18949               && token->type == CPP_PRAGMA_EOL))
18950         return true;
18951       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18952         /* We've hit the end of an enclosing block, so there's been some
18953            kind of syntax error.  */
18954         return true;
18955
18956       /* Consume the token.  */
18957       cp_lexer_consume_token (parser->lexer);
18958       /* See if it starts a new group.  */
18959       if (token->type == CPP_OPEN_BRACE)
18960         {
18961           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18962           /* In theory this should probably check end == '}', but
18963              cp_parser_save_member_function_body needs it to exit
18964              after either '}' or ')' when called with ')'.  */
18965           if (depth == 0)
18966             return false;
18967         }
18968       else if (token->type == CPP_OPEN_PAREN)
18969         {
18970           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18971           if (depth == 0 && end == CPP_CLOSE_PAREN)
18972             return false;
18973         }
18974       else if (token->type == CPP_PRAGMA)
18975         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18976       else if (token->type == end)
18977         return false;
18978     }
18979 }
18980
18981 /* Begin parsing tentatively.  We always save tokens while parsing
18982    tentatively so that if the tentative parsing fails we can restore the
18983    tokens.  */
18984
18985 static void
18986 cp_parser_parse_tentatively (cp_parser* parser)
18987 {
18988   /* Enter a new parsing context.  */
18989   parser->context = cp_parser_context_new (parser->context);
18990   /* Begin saving tokens.  */
18991   cp_lexer_save_tokens (parser->lexer);
18992   /* In order to avoid repetitive access control error messages,
18993      access checks are queued up until we are no longer parsing
18994      tentatively.  */
18995   push_deferring_access_checks (dk_deferred);
18996 }
18997
18998 /* Commit to the currently active tentative parse.  */
18999
19000 static void
19001 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19002 {
19003   cp_parser_context *context;
19004   cp_lexer *lexer;
19005
19006   /* Mark all of the levels as committed.  */
19007   lexer = parser->lexer;
19008   for (context = parser->context; context->next; context = context->next)
19009     {
19010       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19011         break;
19012       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19013       while (!cp_lexer_saving_tokens (lexer))
19014         lexer = lexer->next;
19015       cp_lexer_commit_tokens (lexer);
19016     }
19017 }
19018
19019 /* Abort the currently active tentative parse.  All consumed tokens
19020    will be rolled back, and no diagnostics will be issued.  */
19021
19022 static void
19023 cp_parser_abort_tentative_parse (cp_parser* parser)
19024 {
19025   cp_parser_simulate_error (parser);
19026   /* Now, pretend that we want to see if the construct was
19027      successfully parsed.  */
19028   cp_parser_parse_definitely (parser);
19029 }
19030
19031 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19032    token stream.  Otherwise, commit to the tokens we have consumed.
19033    Returns true if no error occurred; false otherwise.  */
19034
19035 static bool
19036 cp_parser_parse_definitely (cp_parser* parser)
19037 {
19038   bool error_occurred;
19039   cp_parser_context *context;
19040
19041   /* Remember whether or not an error occurred, since we are about to
19042      destroy that information.  */
19043   error_occurred = cp_parser_error_occurred (parser);
19044   /* Remove the topmost context from the stack.  */
19045   context = parser->context;
19046   parser->context = context->next;
19047   /* If no parse errors occurred, commit to the tentative parse.  */
19048   if (!error_occurred)
19049     {
19050       /* Commit to the tokens read tentatively, unless that was
19051          already done.  */
19052       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19053         cp_lexer_commit_tokens (parser->lexer);
19054
19055       pop_to_parent_deferring_access_checks ();
19056     }
19057   /* Otherwise, if errors occurred, roll back our state so that things
19058      are just as they were before we began the tentative parse.  */
19059   else
19060     {
19061       cp_lexer_rollback_tokens (parser->lexer);
19062       pop_deferring_access_checks ();
19063     }
19064   /* Add the context to the front of the free list.  */
19065   context->next = cp_parser_context_free_list;
19066   cp_parser_context_free_list = context;
19067
19068   return !error_occurred;
19069 }
19070
19071 /* Returns true if we are parsing tentatively and are not committed to
19072    this tentative parse.  */
19073
19074 static bool
19075 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19076 {
19077   return (cp_parser_parsing_tentatively (parser)
19078           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19079 }
19080
19081 /* Returns nonzero iff an error has occurred during the most recent
19082    tentative parse.  */
19083
19084 static bool
19085 cp_parser_error_occurred (cp_parser* parser)
19086 {
19087   return (cp_parser_parsing_tentatively (parser)
19088           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19089 }
19090
19091 /* Returns nonzero if GNU extensions are allowed.  */
19092
19093 static bool
19094 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19095 {
19096   return parser->allow_gnu_extensions_p;
19097 }
19098 \f
19099 /* Objective-C++ Productions */
19100
19101
19102 /* Parse an Objective-C expression, which feeds into a primary-expression
19103    above.
19104
19105    objc-expression:
19106      objc-message-expression
19107      objc-string-literal
19108      objc-encode-expression
19109      objc-protocol-expression
19110      objc-selector-expression
19111
19112   Returns a tree representation of the expression.  */
19113
19114 static tree
19115 cp_parser_objc_expression (cp_parser* parser)
19116 {
19117   /* Try to figure out what kind of declaration is present.  */
19118   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19119
19120   switch (kwd->type)
19121     {
19122     case CPP_OPEN_SQUARE:
19123       return cp_parser_objc_message_expression (parser);
19124
19125     case CPP_OBJC_STRING:
19126       kwd = cp_lexer_consume_token (parser->lexer);
19127       return objc_build_string_object (kwd->u.value);
19128
19129     case CPP_KEYWORD:
19130       switch (kwd->keyword)
19131         {
19132         case RID_AT_ENCODE:
19133           return cp_parser_objc_encode_expression (parser);
19134
19135         case RID_AT_PROTOCOL:
19136           return cp_parser_objc_protocol_expression (parser);
19137
19138         case RID_AT_SELECTOR:
19139           return cp_parser_objc_selector_expression (parser);
19140
19141         default:
19142           break;
19143         }
19144     default:
19145       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19146              &kwd->location, kwd->u.value);
19147       cp_parser_skip_to_end_of_block_or_statement (parser);
19148     }
19149
19150   return error_mark_node;
19151 }
19152
19153 /* Parse an Objective-C message expression.
19154
19155    objc-message-expression:
19156      [ objc-message-receiver objc-message-args ]
19157
19158    Returns a representation of an Objective-C message.  */
19159
19160 static tree
19161 cp_parser_objc_message_expression (cp_parser* parser)
19162 {
19163   tree receiver, messageargs;
19164
19165   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19166   receiver = cp_parser_objc_message_receiver (parser);
19167   messageargs = cp_parser_objc_message_args (parser);
19168   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19169
19170   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19171 }
19172
19173 /* Parse an objc-message-receiver.
19174
19175    objc-message-receiver:
19176      expression
19177      simple-type-specifier
19178
19179   Returns a representation of the type or expression.  */
19180
19181 static tree
19182 cp_parser_objc_message_receiver (cp_parser* parser)
19183 {
19184   tree rcv;
19185
19186   /* An Objective-C message receiver may be either (1) a type
19187      or (2) an expression.  */
19188   cp_parser_parse_tentatively (parser);
19189   rcv = cp_parser_expression (parser, false, NULL);
19190
19191   if (cp_parser_parse_definitely (parser))
19192     return rcv;
19193
19194   rcv = cp_parser_simple_type_specifier (parser,
19195                                          /*decl_specs=*/NULL,
19196                                          CP_PARSER_FLAGS_NONE);
19197
19198   return objc_get_class_reference (rcv);
19199 }
19200
19201 /* Parse the arguments and selectors comprising an Objective-C message.
19202
19203    objc-message-args:
19204      objc-selector
19205      objc-selector-args
19206      objc-selector-args , objc-comma-args
19207
19208    objc-selector-args:
19209      objc-selector [opt] : assignment-expression
19210      objc-selector-args objc-selector [opt] : assignment-expression
19211
19212    objc-comma-args:
19213      assignment-expression
19214      objc-comma-args , assignment-expression
19215
19216    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19217    selector arguments and TREE_VALUE containing a list of comma
19218    arguments.  */
19219
19220 static tree
19221 cp_parser_objc_message_args (cp_parser* parser)
19222 {
19223   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19224   bool maybe_unary_selector_p = true;
19225   cp_token *token = cp_lexer_peek_token (parser->lexer);
19226
19227   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19228     {
19229       tree selector = NULL_TREE, arg;
19230
19231       if (token->type != CPP_COLON)
19232         selector = cp_parser_objc_selector (parser);
19233
19234       /* Detect if we have a unary selector.  */
19235       if (maybe_unary_selector_p
19236           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19237         return build_tree_list (selector, NULL_TREE);
19238
19239       maybe_unary_selector_p = false;
19240       cp_parser_require (parser, CPP_COLON, "%<:%>");
19241       arg = cp_parser_assignment_expression (parser, false, NULL);
19242
19243       sel_args
19244         = chainon (sel_args,
19245                    build_tree_list (selector, arg));
19246
19247       token = cp_lexer_peek_token (parser->lexer);
19248     }
19249
19250   /* Handle non-selector arguments, if any. */
19251   while (token->type == CPP_COMMA)
19252     {
19253       tree arg;
19254
19255       cp_lexer_consume_token (parser->lexer);
19256       arg = cp_parser_assignment_expression (parser, false, NULL);
19257
19258       addl_args
19259         = chainon (addl_args,
19260                    build_tree_list (NULL_TREE, arg));
19261
19262       token = cp_lexer_peek_token (parser->lexer);
19263     }
19264
19265   return build_tree_list (sel_args, addl_args);
19266 }
19267
19268 /* Parse an Objective-C encode expression.
19269
19270    objc-encode-expression:
19271      @encode objc-typename
19272
19273    Returns an encoded representation of the type argument.  */
19274
19275 static tree
19276 cp_parser_objc_encode_expression (cp_parser* parser)
19277 {
19278   tree type;
19279   cp_token *token;
19280
19281   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19282   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19283   token = cp_lexer_peek_token (parser->lexer);
19284   type = complete_type (cp_parser_type_id (parser));
19285   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19286
19287   if (!type)
19288     {
19289       error ("%H%<@encode%> must specify a type as an argument",
19290              &token->location);
19291       return error_mark_node;
19292     }
19293
19294   return objc_build_encode_expr (type);
19295 }
19296
19297 /* Parse an Objective-C @defs expression.  */
19298
19299 static tree
19300 cp_parser_objc_defs_expression (cp_parser *parser)
19301 {
19302   tree name;
19303
19304   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19305   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19306   name = cp_parser_identifier (parser);
19307   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19308
19309   return objc_get_class_ivars (name);
19310 }
19311
19312 /* Parse an Objective-C protocol expression.
19313
19314   objc-protocol-expression:
19315     @protocol ( identifier )
19316
19317   Returns a representation of the protocol expression.  */
19318
19319 static tree
19320 cp_parser_objc_protocol_expression (cp_parser* parser)
19321 {
19322   tree proto;
19323
19324   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19325   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19326   proto = cp_parser_identifier (parser);
19327   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19328
19329   return objc_build_protocol_expr (proto);
19330 }
19331
19332 /* Parse an Objective-C selector expression.
19333
19334    objc-selector-expression:
19335      @selector ( objc-method-signature )
19336
19337    objc-method-signature:
19338      objc-selector
19339      objc-selector-seq
19340
19341    objc-selector-seq:
19342      objc-selector :
19343      objc-selector-seq objc-selector :
19344
19345   Returns a representation of the method selector.  */
19346
19347 static tree
19348 cp_parser_objc_selector_expression (cp_parser* parser)
19349 {
19350   tree sel_seq = NULL_TREE;
19351   bool maybe_unary_selector_p = true;
19352   cp_token *token;
19353
19354   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19355   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19356   token = cp_lexer_peek_token (parser->lexer);
19357
19358   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19359          || token->type == CPP_SCOPE)
19360     {
19361       tree selector = NULL_TREE;
19362
19363       if (token->type != CPP_COLON
19364           || token->type == CPP_SCOPE)
19365         selector = cp_parser_objc_selector (parser);
19366
19367       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19368           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19369         {
19370           /* Detect if we have a unary selector.  */
19371           if (maybe_unary_selector_p)
19372             {
19373               sel_seq = selector;
19374               goto finish_selector;
19375             }
19376           else
19377             {
19378               cp_parser_error (parser, "expected %<:%>");
19379             }
19380         }
19381       maybe_unary_selector_p = false;
19382       token = cp_lexer_consume_token (parser->lexer);
19383
19384       if (token->type == CPP_SCOPE)
19385         {
19386           sel_seq
19387             = chainon (sel_seq,
19388                        build_tree_list (selector, NULL_TREE));
19389           sel_seq
19390             = chainon (sel_seq,
19391                        build_tree_list (NULL_TREE, NULL_TREE));
19392         }
19393       else
19394         sel_seq
19395           = chainon (sel_seq,
19396                      build_tree_list (selector, NULL_TREE));
19397
19398       token = cp_lexer_peek_token (parser->lexer);
19399     }
19400
19401  finish_selector:
19402   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19403
19404   return objc_build_selector_expr (sel_seq);
19405 }
19406
19407 /* Parse a list of identifiers.
19408
19409    objc-identifier-list:
19410      identifier
19411      objc-identifier-list , identifier
19412
19413    Returns a TREE_LIST of identifier nodes.  */
19414
19415 static tree
19416 cp_parser_objc_identifier_list (cp_parser* parser)
19417 {
19418   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19419   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19420
19421   while (sep->type == CPP_COMMA)
19422     {
19423       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19424       list = chainon (list,
19425                       build_tree_list (NULL_TREE,
19426                                        cp_parser_identifier (parser)));
19427       sep = cp_lexer_peek_token (parser->lexer);
19428     }
19429
19430   return list;
19431 }
19432
19433 /* Parse an Objective-C alias declaration.
19434
19435    objc-alias-declaration:
19436      @compatibility_alias identifier identifier ;
19437
19438    This function registers the alias mapping with the Objective-C front end.
19439    It returns nothing.  */
19440
19441 static void
19442 cp_parser_objc_alias_declaration (cp_parser* parser)
19443 {
19444   tree alias, orig;
19445
19446   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19447   alias = cp_parser_identifier (parser);
19448   orig = cp_parser_identifier (parser);
19449   objc_declare_alias (alias, orig);
19450   cp_parser_consume_semicolon_at_end_of_statement (parser);
19451 }
19452
19453 /* Parse an Objective-C class forward-declaration.
19454
19455    objc-class-declaration:
19456      @class objc-identifier-list ;
19457
19458    The function registers the forward declarations with the Objective-C
19459    front end.  It returns nothing.  */
19460
19461 static void
19462 cp_parser_objc_class_declaration (cp_parser* parser)
19463 {
19464   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19465   objc_declare_class (cp_parser_objc_identifier_list (parser));
19466   cp_parser_consume_semicolon_at_end_of_statement (parser);
19467 }
19468
19469 /* Parse a list of Objective-C protocol references.
19470
19471    objc-protocol-refs-opt:
19472      objc-protocol-refs [opt]
19473
19474    objc-protocol-refs:
19475      < objc-identifier-list >
19476
19477    Returns a TREE_LIST of identifiers, if any.  */
19478
19479 static tree
19480 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19481 {
19482   tree protorefs = NULL_TREE;
19483
19484   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19485     {
19486       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19487       protorefs = cp_parser_objc_identifier_list (parser);
19488       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19489     }
19490
19491   return protorefs;
19492 }
19493
19494 /* Parse a Objective-C visibility specification.  */
19495
19496 static void
19497 cp_parser_objc_visibility_spec (cp_parser* parser)
19498 {
19499   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19500
19501   switch (vis->keyword)
19502     {
19503     case RID_AT_PRIVATE:
19504       objc_set_visibility (2);
19505       break;
19506     case RID_AT_PROTECTED:
19507       objc_set_visibility (0);
19508       break;
19509     case RID_AT_PUBLIC:
19510       objc_set_visibility (1);
19511       break;
19512     default:
19513       return;
19514     }
19515
19516   /* Eat '@private'/'@protected'/'@public'.  */
19517   cp_lexer_consume_token (parser->lexer);
19518 }
19519
19520 /* Parse an Objective-C method type.  */
19521
19522 static void
19523 cp_parser_objc_method_type (cp_parser* parser)
19524 {
19525   objc_set_method_type
19526    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19527     ? PLUS_EXPR
19528     : MINUS_EXPR);
19529 }
19530
19531 /* Parse an Objective-C protocol qualifier.  */
19532
19533 static tree
19534 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19535 {
19536   tree quals = NULL_TREE, node;
19537   cp_token *token = cp_lexer_peek_token (parser->lexer);
19538
19539   node = token->u.value;
19540
19541   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19542          && (node == ridpointers [(int) RID_IN]
19543              || node == ridpointers [(int) RID_OUT]
19544              || node == ridpointers [(int) RID_INOUT]
19545              || node == ridpointers [(int) RID_BYCOPY]
19546              || node == ridpointers [(int) RID_BYREF]
19547              || node == ridpointers [(int) RID_ONEWAY]))
19548     {
19549       quals = tree_cons (NULL_TREE, node, quals);
19550       cp_lexer_consume_token (parser->lexer);
19551       token = cp_lexer_peek_token (parser->lexer);
19552       node = token->u.value;
19553     }
19554
19555   return quals;
19556 }
19557
19558 /* Parse an Objective-C typename.  */
19559
19560 static tree
19561 cp_parser_objc_typename (cp_parser* parser)
19562 {
19563   tree type_name = NULL_TREE;
19564
19565   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19566     {
19567       tree proto_quals, cp_type = NULL_TREE;
19568
19569       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19570       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19571
19572       /* An ObjC type name may consist of just protocol qualifiers, in which
19573          case the type shall default to 'id'.  */
19574       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19575         cp_type = cp_parser_type_id (parser);
19576
19577       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19578       type_name = build_tree_list (proto_quals, cp_type);
19579     }
19580
19581   return type_name;
19582 }
19583
19584 /* Check to see if TYPE refers to an Objective-C selector name.  */
19585
19586 static bool
19587 cp_parser_objc_selector_p (enum cpp_ttype type)
19588 {
19589   return (type == CPP_NAME || type == CPP_KEYWORD
19590           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19591           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19592           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19593           || type == CPP_XOR || type == CPP_XOR_EQ);
19594 }
19595
19596 /* Parse an Objective-C selector.  */
19597
19598 static tree
19599 cp_parser_objc_selector (cp_parser* parser)
19600 {
19601   cp_token *token = cp_lexer_consume_token (parser->lexer);
19602
19603   if (!cp_parser_objc_selector_p (token->type))
19604     {
19605       error ("%Hinvalid Objective-C++ selector name", &token->location);
19606       return error_mark_node;
19607     }
19608
19609   /* C++ operator names are allowed to appear in ObjC selectors.  */
19610   switch (token->type)
19611     {
19612     case CPP_AND_AND: return get_identifier ("and");
19613     case CPP_AND_EQ: return get_identifier ("and_eq");
19614     case CPP_AND: return get_identifier ("bitand");
19615     case CPP_OR: return get_identifier ("bitor");
19616     case CPP_COMPL: return get_identifier ("compl");
19617     case CPP_NOT: return get_identifier ("not");
19618     case CPP_NOT_EQ: return get_identifier ("not_eq");
19619     case CPP_OR_OR: return get_identifier ("or");
19620     case CPP_OR_EQ: return get_identifier ("or_eq");
19621     case CPP_XOR: return get_identifier ("xor");
19622     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19623     default: return token->u.value;
19624     }
19625 }
19626
19627 /* Parse an Objective-C params list.  */
19628
19629 static tree
19630 cp_parser_objc_method_keyword_params (cp_parser* parser)
19631 {
19632   tree params = NULL_TREE;
19633   bool maybe_unary_selector_p = true;
19634   cp_token *token = cp_lexer_peek_token (parser->lexer);
19635
19636   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19637     {
19638       tree selector = NULL_TREE, type_name, identifier;
19639
19640       if (token->type != CPP_COLON)
19641         selector = cp_parser_objc_selector (parser);
19642
19643       /* Detect if we have a unary selector.  */
19644       if (maybe_unary_selector_p
19645           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19646         return selector;
19647
19648       maybe_unary_selector_p = false;
19649       cp_parser_require (parser, CPP_COLON, "%<:%>");
19650       type_name = cp_parser_objc_typename (parser);
19651       identifier = cp_parser_identifier (parser);
19652
19653       params
19654         = chainon (params,
19655                    objc_build_keyword_decl (selector,
19656                                             type_name,
19657                                             identifier));
19658
19659       token = cp_lexer_peek_token (parser->lexer);
19660     }
19661
19662   return params;
19663 }
19664
19665 /* Parse the non-keyword Objective-C params.  */
19666
19667 static tree
19668 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19669 {
19670   tree params = make_node (TREE_LIST);
19671   cp_token *token = cp_lexer_peek_token (parser->lexer);
19672   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19673
19674   while (token->type == CPP_COMMA)
19675     {
19676       cp_parameter_declarator *parmdecl;
19677       tree parm;
19678
19679       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19680       token = cp_lexer_peek_token (parser->lexer);
19681
19682       if (token->type == CPP_ELLIPSIS)
19683         {
19684           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19685           *ellipsisp = true;
19686           break;
19687         }
19688
19689       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19690       parm = grokdeclarator (parmdecl->declarator,
19691                              &parmdecl->decl_specifiers,
19692                              PARM, /*initialized=*/0,
19693                              /*attrlist=*/NULL);
19694
19695       chainon (params, build_tree_list (NULL_TREE, parm));
19696       token = cp_lexer_peek_token (parser->lexer);
19697     }
19698
19699   return params;
19700 }
19701
19702 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19703
19704 static void
19705 cp_parser_objc_interstitial_code (cp_parser* parser)
19706 {
19707   cp_token *token = cp_lexer_peek_token (parser->lexer);
19708
19709   /* If the next token is `extern' and the following token is a string
19710      literal, then we have a linkage specification.  */
19711   if (token->keyword == RID_EXTERN
19712       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19713     cp_parser_linkage_specification (parser);
19714   /* Handle #pragma, if any.  */
19715   else if (token->type == CPP_PRAGMA)
19716     cp_parser_pragma (parser, pragma_external);
19717   /* Allow stray semicolons.  */
19718   else if (token->type == CPP_SEMICOLON)
19719     cp_lexer_consume_token (parser->lexer);
19720   /* Finally, try to parse a block-declaration, or a function-definition.  */
19721   else
19722     cp_parser_block_declaration (parser, /*statement_p=*/false);
19723 }
19724
19725 /* Parse a method signature.  */
19726
19727 static tree
19728 cp_parser_objc_method_signature (cp_parser* parser)
19729 {
19730   tree rettype, kwdparms, optparms;
19731   bool ellipsis = false;
19732
19733   cp_parser_objc_method_type (parser);
19734   rettype = cp_parser_objc_typename (parser);
19735   kwdparms = cp_parser_objc_method_keyword_params (parser);
19736   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19737
19738   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19739 }
19740
19741 /* Pars an Objective-C method prototype list.  */
19742
19743 static void
19744 cp_parser_objc_method_prototype_list (cp_parser* parser)
19745 {
19746   cp_token *token = cp_lexer_peek_token (parser->lexer);
19747
19748   while (token->keyword != RID_AT_END)
19749     {
19750       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19751         {
19752           objc_add_method_declaration
19753            (cp_parser_objc_method_signature (parser));
19754           cp_parser_consume_semicolon_at_end_of_statement (parser);
19755         }
19756       else
19757         /* Allow for interspersed non-ObjC++ code.  */
19758         cp_parser_objc_interstitial_code (parser);
19759
19760       token = cp_lexer_peek_token (parser->lexer);
19761     }
19762
19763   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19764   objc_finish_interface ();
19765 }
19766
19767 /* Parse an Objective-C method definition list.  */
19768
19769 static void
19770 cp_parser_objc_method_definition_list (cp_parser* parser)
19771 {
19772   cp_token *token = cp_lexer_peek_token (parser->lexer);
19773
19774   while (token->keyword != RID_AT_END)
19775     {
19776       tree meth;
19777
19778       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19779         {
19780           push_deferring_access_checks (dk_deferred);
19781           objc_start_method_definition
19782            (cp_parser_objc_method_signature (parser));
19783
19784           /* For historical reasons, we accept an optional semicolon.  */
19785           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19786             cp_lexer_consume_token (parser->lexer);
19787
19788           perform_deferred_access_checks ();
19789           stop_deferring_access_checks ();
19790           meth = cp_parser_function_definition_after_declarator (parser,
19791                                                                  false);
19792           pop_deferring_access_checks ();
19793           objc_finish_method_definition (meth);
19794         }
19795       else
19796         /* Allow for interspersed non-ObjC++ code.  */
19797         cp_parser_objc_interstitial_code (parser);
19798
19799       token = cp_lexer_peek_token (parser->lexer);
19800     }
19801
19802   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19803   objc_finish_implementation ();
19804 }
19805
19806 /* Parse Objective-C ivars.  */
19807
19808 static void
19809 cp_parser_objc_class_ivars (cp_parser* parser)
19810 {
19811   cp_token *token = cp_lexer_peek_token (parser->lexer);
19812
19813   if (token->type != CPP_OPEN_BRACE)
19814     return;     /* No ivars specified.  */
19815
19816   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19817   token = cp_lexer_peek_token (parser->lexer);
19818
19819   while (token->type != CPP_CLOSE_BRACE)
19820     {
19821       cp_decl_specifier_seq declspecs;
19822       int decl_class_or_enum_p;
19823       tree prefix_attributes;
19824
19825       cp_parser_objc_visibility_spec (parser);
19826
19827       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19828         break;
19829
19830       cp_parser_decl_specifier_seq (parser,
19831                                     CP_PARSER_FLAGS_OPTIONAL,
19832                                     &declspecs,
19833                                     &decl_class_or_enum_p);
19834       prefix_attributes = declspecs.attributes;
19835       declspecs.attributes = NULL_TREE;
19836
19837       /* Keep going until we hit the `;' at the end of the
19838          declaration.  */
19839       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19840         {
19841           tree width = NULL_TREE, attributes, first_attribute, decl;
19842           cp_declarator *declarator = NULL;
19843           int ctor_dtor_or_conv_p;
19844
19845           /* Check for a (possibly unnamed) bitfield declaration.  */
19846           token = cp_lexer_peek_token (parser->lexer);
19847           if (token->type == CPP_COLON)
19848             goto eat_colon;
19849
19850           if (token->type == CPP_NAME
19851               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19852                   == CPP_COLON))
19853             {
19854               /* Get the name of the bitfield.  */
19855               declarator = make_id_declarator (NULL_TREE,
19856                                                cp_parser_identifier (parser),
19857                                                sfk_none);
19858
19859              eat_colon:
19860               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19861               /* Get the width of the bitfield.  */
19862               width
19863                 = cp_parser_constant_expression (parser,
19864                                                  /*allow_non_constant=*/false,
19865                                                  NULL);
19866             }
19867           else
19868             {
19869               /* Parse the declarator.  */
19870               declarator
19871                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19872                                         &ctor_dtor_or_conv_p,
19873                                         /*parenthesized_p=*/NULL,
19874                                         /*member_p=*/false);
19875             }
19876
19877           /* Look for attributes that apply to the ivar.  */
19878           attributes = cp_parser_attributes_opt (parser);
19879           /* Remember which attributes are prefix attributes and
19880              which are not.  */
19881           first_attribute = attributes;
19882           /* Combine the attributes.  */
19883           attributes = chainon (prefix_attributes, attributes);
19884
19885           if (width)
19886               /* Create the bitfield declaration.  */
19887               decl = grokbitfield (declarator, &declspecs,
19888                                    width,
19889                                    attributes);
19890           else
19891             decl = grokfield (declarator, &declspecs,
19892                               NULL_TREE, /*init_const_expr_p=*/false,
19893                               NULL_TREE, attributes);
19894
19895           /* Add the instance variable.  */
19896           objc_add_instance_variable (decl);
19897
19898           /* Reset PREFIX_ATTRIBUTES.  */
19899           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19900             attributes = TREE_CHAIN (attributes);
19901           if (attributes)
19902             TREE_CHAIN (attributes) = NULL_TREE;
19903
19904           token = cp_lexer_peek_token (parser->lexer);
19905
19906           if (token->type == CPP_COMMA)
19907             {
19908               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19909               continue;
19910             }
19911           break;
19912         }
19913
19914       cp_parser_consume_semicolon_at_end_of_statement (parser);
19915       token = cp_lexer_peek_token (parser->lexer);
19916     }
19917
19918   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19919   /* For historical reasons, we accept an optional semicolon.  */
19920   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19921     cp_lexer_consume_token (parser->lexer);
19922 }
19923
19924 /* Parse an Objective-C protocol declaration.  */
19925
19926 static void
19927 cp_parser_objc_protocol_declaration (cp_parser* parser)
19928 {
19929   tree proto, protorefs;
19930   cp_token *tok;
19931
19932   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19933   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19934     {
19935       tok = cp_lexer_peek_token (parser->lexer);
19936       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19937       goto finish;
19938     }
19939
19940   /* See if we have a forward declaration or a definition.  */
19941   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19942
19943   /* Try a forward declaration first.  */
19944   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19945     {
19946       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19947      finish:
19948       cp_parser_consume_semicolon_at_end_of_statement (parser);
19949     }
19950
19951   /* Ok, we got a full-fledged definition (or at least should).  */
19952   else
19953     {
19954       proto = cp_parser_identifier (parser);
19955       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19956       objc_start_protocol (proto, protorefs);
19957       cp_parser_objc_method_prototype_list (parser);
19958     }
19959 }
19960
19961 /* Parse an Objective-C superclass or category.  */
19962
19963 static void
19964 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19965                                                           tree *categ)
19966 {
19967   cp_token *next = cp_lexer_peek_token (parser->lexer);
19968
19969   *super = *categ = NULL_TREE;
19970   if (next->type == CPP_COLON)
19971     {
19972       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19973       *super = cp_parser_identifier (parser);
19974     }
19975   else if (next->type == CPP_OPEN_PAREN)
19976     {
19977       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19978       *categ = cp_parser_identifier (parser);
19979       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19980     }
19981 }
19982
19983 /* Parse an Objective-C class interface.  */
19984
19985 static void
19986 cp_parser_objc_class_interface (cp_parser* parser)
19987 {
19988   tree name, super, categ, protos;
19989
19990   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19991   name = cp_parser_identifier (parser);
19992   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19993   protos = cp_parser_objc_protocol_refs_opt (parser);
19994
19995   /* We have either a class or a category on our hands.  */
19996   if (categ)
19997     objc_start_category_interface (name, categ, protos);
19998   else
19999     {
20000       objc_start_class_interface (name, super, protos);
20001       /* Handle instance variable declarations, if any.  */
20002       cp_parser_objc_class_ivars (parser);
20003       objc_continue_interface ();
20004     }
20005
20006   cp_parser_objc_method_prototype_list (parser);
20007 }
20008
20009 /* Parse an Objective-C class implementation.  */
20010
20011 static void
20012 cp_parser_objc_class_implementation (cp_parser* parser)
20013 {
20014   tree name, super, categ;
20015
20016   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20017   name = cp_parser_identifier (parser);
20018   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20019
20020   /* We have either a class or a category on our hands.  */
20021   if (categ)
20022     objc_start_category_implementation (name, categ);
20023   else
20024     {
20025       objc_start_class_implementation (name, super);
20026       /* Handle instance variable declarations, if any.  */
20027       cp_parser_objc_class_ivars (parser);
20028       objc_continue_implementation ();
20029     }
20030
20031   cp_parser_objc_method_definition_list (parser);
20032 }
20033
20034 /* Consume the @end token and finish off the implementation.  */
20035
20036 static void
20037 cp_parser_objc_end_implementation (cp_parser* parser)
20038 {
20039   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20040   objc_finish_implementation ();
20041 }
20042
20043 /* Parse an Objective-C declaration.  */
20044
20045 static void
20046 cp_parser_objc_declaration (cp_parser* parser)
20047 {
20048   /* Try to figure out what kind of declaration is present.  */
20049   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20050
20051   switch (kwd->keyword)
20052     {
20053     case RID_AT_ALIAS:
20054       cp_parser_objc_alias_declaration (parser);
20055       break;
20056     case RID_AT_CLASS:
20057       cp_parser_objc_class_declaration (parser);
20058       break;
20059     case RID_AT_PROTOCOL:
20060       cp_parser_objc_protocol_declaration (parser);
20061       break;
20062     case RID_AT_INTERFACE:
20063       cp_parser_objc_class_interface (parser);
20064       break;
20065     case RID_AT_IMPLEMENTATION:
20066       cp_parser_objc_class_implementation (parser);
20067       break;
20068     case RID_AT_END:
20069       cp_parser_objc_end_implementation (parser);
20070       break;
20071     default:
20072       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20073              &kwd->location, kwd->u.value);
20074       cp_parser_skip_to_end_of_block_or_statement (parser);
20075     }
20076 }
20077
20078 /* Parse an Objective-C try-catch-finally statement.
20079
20080    objc-try-catch-finally-stmt:
20081      @try compound-statement objc-catch-clause-seq [opt]
20082        objc-finally-clause [opt]
20083
20084    objc-catch-clause-seq:
20085      objc-catch-clause objc-catch-clause-seq [opt]
20086
20087    objc-catch-clause:
20088      @catch ( exception-declaration ) compound-statement
20089
20090    objc-finally-clause
20091      @finally compound-statement
20092
20093    Returns NULL_TREE.  */
20094
20095 static tree
20096 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20097   location_t location;
20098   tree stmt;
20099
20100   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20101   location = cp_lexer_peek_token (parser->lexer)->location;
20102   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20103      node, lest it get absorbed into the surrounding block.  */
20104   stmt = push_stmt_list ();
20105   cp_parser_compound_statement (parser, NULL, false);
20106   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20107
20108   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20109     {
20110       cp_parameter_declarator *parmdecl;
20111       tree parm;
20112
20113       cp_lexer_consume_token (parser->lexer);
20114       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20115       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20116       parm = grokdeclarator (parmdecl->declarator,
20117                              &parmdecl->decl_specifiers,
20118                              PARM, /*initialized=*/0,
20119                              /*attrlist=*/NULL);
20120       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20121       objc_begin_catch_clause (parm);
20122       cp_parser_compound_statement (parser, NULL, false);
20123       objc_finish_catch_clause ();
20124     }
20125
20126   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20127     {
20128       cp_lexer_consume_token (parser->lexer);
20129       location = cp_lexer_peek_token (parser->lexer)->location;
20130       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20131          node, lest it get absorbed into the surrounding block.  */
20132       stmt = push_stmt_list ();
20133       cp_parser_compound_statement (parser, NULL, false);
20134       objc_build_finally_clause (location, pop_stmt_list (stmt));
20135     }
20136
20137   return objc_finish_try_stmt ();
20138 }
20139
20140 /* Parse an Objective-C synchronized statement.
20141
20142    objc-synchronized-stmt:
20143      @synchronized ( expression ) compound-statement
20144
20145    Returns NULL_TREE.  */
20146
20147 static tree
20148 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20149   location_t location;
20150   tree lock, stmt;
20151
20152   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20153
20154   location = cp_lexer_peek_token (parser->lexer)->location;
20155   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20156   lock = cp_parser_expression (parser, false, NULL);
20157   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20158
20159   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20160      node, lest it get absorbed into the surrounding block.  */
20161   stmt = push_stmt_list ();
20162   cp_parser_compound_statement (parser, NULL, false);
20163
20164   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20165 }
20166
20167 /* Parse an Objective-C throw statement.
20168
20169    objc-throw-stmt:
20170      @throw assignment-expression [opt] ;
20171
20172    Returns a constructed '@throw' statement.  */
20173
20174 static tree
20175 cp_parser_objc_throw_statement (cp_parser *parser) {
20176   tree expr = NULL_TREE;
20177
20178   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20179
20180   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20181     expr = cp_parser_assignment_expression (parser, false, NULL);
20182
20183   cp_parser_consume_semicolon_at_end_of_statement (parser);
20184
20185   return objc_build_throw_stmt (expr);
20186 }
20187
20188 /* Parse an Objective-C statement.  */
20189
20190 static tree
20191 cp_parser_objc_statement (cp_parser * parser) {
20192   /* Try to figure out what kind of declaration is present.  */
20193   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20194
20195   switch (kwd->keyword)
20196     {
20197     case RID_AT_TRY:
20198       return cp_parser_objc_try_catch_finally_statement (parser);
20199     case RID_AT_SYNCHRONIZED:
20200       return cp_parser_objc_synchronized_statement (parser);
20201     case RID_AT_THROW:
20202       return cp_parser_objc_throw_statement (parser);
20203     default:
20204       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20205              &kwd->location, kwd->u.value);
20206       cp_parser_skip_to_end_of_block_or_statement (parser);
20207     }
20208
20209   return error_mark_node;
20210 }
20211 \f
20212 /* OpenMP 2.5 parsing routines.  */
20213
20214 /* Returns name of the next clause.
20215    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20216    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20217    returned and the token is consumed.  */
20218
20219 static pragma_omp_clause
20220 cp_parser_omp_clause_name (cp_parser *parser)
20221 {
20222   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20223
20224   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20225     result = PRAGMA_OMP_CLAUSE_IF;
20226   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20227     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20228   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20229     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20230   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20231     {
20232       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20233       const char *p = IDENTIFIER_POINTER (id);
20234
20235       switch (p[0])
20236         {
20237         case 'c':
20238           if (!strcmp ("collapse", p))
20239             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20240           else if (!strcmp ("copyin", p))
20241             result = PRAGMA_OMP_CLAUSE_COPYIN;
20242           else if (!strcmp ("copyprivate", p))
20243             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20244           break;
20245         case 'f':
20246           if (!strcmp ("firstprivate", p))
20247             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20248           break;
20249         case 'l':
20250           if (!strcmp ("lastprivate", p))
20251             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20252           break;
20253         case 'n':
20254           if (!strcmp ("nowait", p))
20255             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20256           else if (!strcmp ("num_threads", p))
20257             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20258           break;
20259         case 'o':
20260           if (!strcmp ("ordered", p))
20261             result = PRAGMA_OMP_CLAUSE_ORDERED;
20262           break;
20263         case 'r':
20264           if (!strcmp ("reduction", p))
20265             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20266           break;
20267         case 's':
20268           if (!strcmp ("schedule", p))
20269             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20270           else if (!strcmp ("shared", p))
20271             result = PRAGMA_OMP_CLAUSE_SHARED;
20272           break;
20273         case 'u':
20274           if (!strcmp ("untied", p))
20275             result = PRAGMA_OMP_CLAUSE_UNTIED;
20276           break;
20277         }
20278     }
20279
20280   if (result != PRAGMA_OMP_CLAUSE_NONE)
20281     cp_lexer_consume_token (parser->lexer);
20282
20283   return result;
20284 }
20285
20286 /* Validate that a clause of the given type does not already exist.  */
20287
20288 static void
20289 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20290                            const char *name, location_t location)
20291 {
20292   tree c;
20293
20294   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20295     if (OMP_CLAUSE_CODE (c) == code)
20296       {
20297         error ("%Htoo many %qs clauses", &location, name);
20298         break;
20299       }
20300 }
20301
20302 /* OpenMP 2.5:
20303    variable-list:
20304      identifier
20305      variable-list , identifier
20306
20307    In addition, we match a closing parenthesis.  An opening parenthesis
20308    will have been consumed by the caller.
20309
20310    If KIND is nonzero, create the appropriate node and install the decl
20311    in OMP_CLAUSE_DECL and add the node to the head of the list.
20312
20313    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20314    return the list created.  */
20315
20316 static tree
20317 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20318                                 tree list)
20319 {
20320   cp_token *token;
20321   while (1)
20322     {
20323       tree name, decl;
20324
20325       token = cp_lexer_peek_token (parser->lexer);
20326       name = cp_parser_id_expression (parser, /*template_p=*/false,
20327                                       /*check_dependency_p=*/true,
20328                                       /*template_p=*/NULL,
20329                                       /*declarator_p=*/false,
20330                                       /*optional_p=*/false);
20331       if (name == error_mark_node)
20332         goto skip_comma;
20333
20334       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20335       if (decl == error_mark_node)
20336         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20337       else if (kind != 0)
20338         {
20339           tree u = build_omp_clause (kind);
20340           OMP_CLAUSE_DECL (u) = decl;
20341           OMP_CLAUSE_CHAIN (u) = list;
20342           list = u;
20343         }
20344       else
20345         list = tree_cons (decl, NULL_TREE, list);
20346
20347     get_comma:
20348       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20349         break;
20350       cp_lexer_consume_token (parser->lexer);
20351     }
20352
20353   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20354     {
20355       int ending;
20356
20357       /* Try to resync to an unnested comma.  Copied from
20358          cp_parser_parenthesized_expression_list.  */
20359     skip_comma:
20360       ending = cp_parser_skip_to_closing_parenthesis (parser,
20361                                                       /*recovering=*/true,
20362                                                       /*or_comma=*/true,
20363                                                       /*consume_paren=*/true);
20364       if (ending < 0)
20365         goto get_comma;
20366     }
20367
20368   return list;
20369 }
20370
20371 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20372    common case for omp clauses.  */
20373
20374 static tree
20375 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20376 {
20377   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20378     return cp_parser_omp_var_list_no_open (parser, kind, list);
20379   return list;
20380 }
20381
20382 /* OpenMP 3.0:
20383    collapse ( constant-expression ) */
20384
20385 static tree
20386 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20387 {
20388   tree c, num;
20389   location_t loc;
20390   HOST_WIDE_INT n;
20391
20392   loc = cp_lexer_peek_token (parser->lexer)->location;
20393   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20394     return list;
20395
20396   num = cp_parser_constant_expression (parser, false, NULL);
20397
20398   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20399     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20400                                            /*or_comma=*/false,
20401                                            /*consume_paren=*/true);
20402
20403   if (num == error_mark_node)
20404     return list;
20405   num = fold_non_dependent_expr (num);
20406   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20407       || !host_integerp (num, 0)
20408       || (n = tree_low_cst (num, 0)) <= 0
20409       || (int) n != n)
20410     {
20411       error ("%Hcollapse argument needs positive constant integer expression",
20412              &loc);
20413       return list;
20414     }
20415
20416   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20417   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20418   OMP_CLAUSE_CHAIN (c) = list;
20419   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20420
20421   return c;
20422 }
20423
20424 /* OpenMP 2.5:
20425    default ( shared | none ) */
20426
20427 static tree
20428 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20429 {
20430   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20431   tree c;
20432
20433   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20434     return list;
20435   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20436     {
20437       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20438       const char *p = IDENTIFIER_POINTER (id);
20439
20440       switch (p[0])
20441         {
20442         case 'n':
20443           if (strcmp ("none", p) != 0)
20444             goto invalid_kind;
20445           kind = OMP_CLAUSE_DEFAULT_NONE;
20446           break;
20447
20448         case 's':
20449           if (strcmp ("shared", p) != 0)
20450             goto invalid_kind;
20451           kind = OMP_CLAUSE_DEFAULT_SHARED;
20452           break;
20453
20454         default:
20455           goto invalid_kind;
20456         }
20457
20458       cp_lexer_consume_token (parser->lexer);
20459     }
20460   else
20461     {
20462     invalid_kind:
20463       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20464     }
20465
20466   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20467     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20468                                            /*or_comma=*/false,
20469                                            /*consume_paren=*/true);
20470
20471   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20472     return list;
20473
20474   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20475   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20476   OMP_CLAUSE_CHAIN (c) = list;
20477   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20478
20479   return c;
20480 }
20481
20482 /* OpenMP 2.5:
20483    if ( expression ) */
20484
20485 static tree
20486 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20487 {
20488   tree t, c;
20489
20490   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20491     return list;
20492
20493   t = cp_parser_condition (parser);
20494
20495   if (t == error_mark_node
20496       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20497     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20498                                            /*or_comma=*/false,
20499                                            /*consume_paren=*/true);
20500
20501   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20502
20503   c = build_omp_clause (OMP_CLAUSE_IF);
20504   OMP_CLAUSE_IF_EXPR (c) = t;
20505   OMP_CLAUSE_CHAIN (c) = list;
20506
20507   return c;
20508 }
20509
20510 /* OpenMP 2.5:
20511    nowait */
20512
20513 static tree
20514 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20515                              tree list, location_t location)
20516 {
20517   tree c;
20518
20519   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20520
20521   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20522   OMP_CLAUSE_CHAIN (c) = list;
20523   return c;
20524 }
20525
20526 /* OpenMP 2.5:
20527    num_threads ( expression ) */
20528
20529 static tree
20530 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20531                                   location_t location)
20532 {
20533   tree t, c;
20534
20535   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20536     return list;
20537
20538   t = cp_parser_expression (parser, false, NULL);
20539
20540   if (t == error_mark_node
20541       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20542     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20543                                            /*or_comma=*/false,
20544                                            /*consume_paren=*/true);
20545
20546   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20547                              "num_threads", location);
20548
20549   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20550   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20551   OMP_CLAUSE_CHAIN (c) = list;
20552
20553   return c;
20554 }
20555
20556 /* OpenMP 2.5:
20557    ordered */
20558
20559 static tree
20560 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20561                               tree list, location_t location)
20562 {
20563   tree c;
20564
20565   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20566                              "ordered", location);
20567
20568   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20569   OMP_CLAUSE_CHAIN (c) = list;
20570   return c;
20571 }
20572
20573 /* OpenMP 2.5:
20574    reduction ( reduction-operator : variable-list )
20575
20576    reduction-operator:
20577      One of: + * - & ^ | && || */
20578
20579 static tree
20580 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20581 {
20582   enum tree_code code;
20583   tree nlist, c;
20584
20585   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20586     return list;
20587
20588   switch (cp_lexer_peek_token (parser->lexer)->type)
20589     {
20590     case CPP_PLUS:
20591       code = PLUS_EXPR;
20592       break;
20593     case CPP_MULT:
20594       code = MULT_EXPR;
20595       break;
20596     case CPP_MINUS:
20597       code = MINUS_EXPR;
20598       break;
20599     case CPP_AND:
20600       code = BIT_AND_EXPR;
20601       break;
20602     case CPP_XOR:
20603       code = BIT_XOR_EXPR;
20604       break;
20605     case CPP_OR:
20606       code = BIT_IOR_EXPR;
20607       break;
20608     case CPP_AND_AND:
20609       code = TRUTH_ANDIF_EXPR;
20610       break;
20611     case CPP_OR_OR:
20612       code = TRUTH_ORIF_EXPR;
20613       break;
20614     default:
20615       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20616                                "%<|%>, %<&&%>, or %<||%>");
20617     resync_fail:
20618       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20619                                              /*or_comma=*/false,
20620                                              /*consume_paren=*/true);
20621       return list;
20622     }
20623   cp_lexer_consume_token (parser->lexer);
20624
20625   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20626     goto resync_fail;
20627
20628   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20629   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20630     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20631
20632   return nlist;
20633 }
20634
20635 /* OpenMP 2.5:
20636    schedule ( schedule-kind )
20637    schedule ( schedule-kind , expression )
20638
20639    schedule-kind:
20640      static | dynamic | guided | runtime | auto  */
20641
20642 static tree
20643 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20644 {
20645   tree c, t;
20646
20647   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20648     return list;
20649
20650   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20651
20652   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20653     {
20654       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20655       const char *p = IDENTIFIER_POINTER (id);
20656
20657       switch (p[0])
20658         {
20659         case 'd':
20660           if (strcmp ("dynamic", p) != 0)
20661             goto invalid_kind;
20662           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20663           break;
20664
20665         case 'g':
20666           if (strcmp ("guided", p) != 0)
20667             goto invalid_kind;
20668           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20669           break;
20670
20671         case 'r':
20672           if (strcmp ("runtime", p) != 0)
20673             goto invalid_kind;
20674           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20675           break;
20676
20677         default:
20678           goto invalid_kind;
20679         }
20680     }
20681   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20682     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20683   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20684     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20685   else
20686     goto invalid_kind;
20687   cp_lexer_consume_token (parser->lexer);
20688
20689   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20690     {
20691       cp_token *token;
20692       cp_lexer_consume_token (parser->lexer);
20693
20694       token = cp_lexer_peek_token (parser->lexer);
20695       t = cp_parser_assignment_expression (parser, false, NULL);
20696
20697       if (t == error_mark_node)
20698         goto resync_fail;
20699       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20700         error ("%Hschedule %<runtime%> does not take "
20701                "a %<chunk_size%> parameter", &token->location);
20702       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20703         error ("%Hschedule %<auto%> does not take "
20704                "a %<chunk_size%> parameter", &token->location);
20705       else
20706         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20707
20708       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20709         goto resync_fail;
20710     }
20711   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20712     goto resync_fail;
20713
20714   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20715   OMP_CLAUSE_CHAIN (c) = list;
20716   return c;
20717
20718  invalid_kind:
20719   cp_parser_error (parser, "invalid schedule kind");
20720  resync_fail:
20721   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20722                                          /*or_comma=*/false,
20723                                          /*consume_paren=*/true);
20724   return list;
20725 }
20726
20727 /* OpenMP 3.0:
20728    untied */
20729
20730 static tree
20731 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20732                              tree list, location_t location)
20733 {
20734   tree c;
20735
20736   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20737
20738   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20739   OMP_CLAUSE_CHAIN (c) = list;
20740   return c;
20741 }
20742
20743 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20744    is a bitmask in MASK.  Return the list of clauses found; the result
20745    of clause default goes in *pdefault.  */
20746
20747 static tree
20748 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20749                            const char *where, cp_token *pragma_tok)
20750 {
20751   tree clauses = NULL;
20752   bool first = true;
20753   cp_token *token = NULL;
20754
20755   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20756     {
20757       pragma_omp_clause c_kind;
20758       const char *c_name;
20759       tree prev = clauses;
20760
20761       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20762         cp_lexer_consume_token (parser->lexer);
20763
20764       token = cp_lexer_peek_token (parser->lexer);
20765       c_kind = cp_parser_omp_clause_name (parser);
20766       first = false;
20767
20768       switch (c_kind)
20769         {
20770         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20771           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20772                                                    token->location);
20773           c_name = "collapse";
20774           break;
20775         case PRAGMA_OMP_CLAUSE_COPYIN:
20776           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20777           c_name = "copyin";
20778           break;
20779         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20780           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20781                                             clauses);
20782           c_name = "copyprivate";
20783           break;
20784         case PRAGMA_OMP_CLAUSE_DEFAULT:
20785           clauses = cp_parser_omp_clause_default (parser, clauses,
20786                                                   token->location);
20787           c_name = "default";
20788           break;
20789         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20790           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20791                                             clauses);
20792           c_name = "firstprivate";
20793           break;
20794         case PRAGMA_OMP_CLAUSE_IF:
20795           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20796           c_name = "if";
20797           break;
20798         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20799           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20800                                             clauses);
20801           c_name = "lastprivate";
20802           break;
20803         case PRAGMA_OMP_CLAUSE_NOWAIT:
20804           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20805           c_name = "nowait";
20806           break;
20807         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20808           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20809                                                       token->location);
20810           c_name = "num_threads";
20811           break;
20812         case PRAGMA_OMP_CLAUSE_ORDERED:
20813           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20814                                                   token->location);
20815           c_name = "ordered";
20816           break;
20817         case PRAGMA_OMP_CLAUSE_PRIVATE:
20818           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20819                                             clauses);
20820           c_name = "private";
20821           break;
20822         case PRAGMA_OMP_CLAUSE_REDUCTION:
20823           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20824           c_name = "reduction";
20825           break;
20826         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20827           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20828                                                    token->location);
20829           c_name = "schedule";
20830           break;
20831         case PRAGMA_OMP_CLAUSE_SHARED:
20832           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20833                                             clauses);
20834           c_name = "shared";
20835           break;
20836         case PRAGMA_OMP_CLAUSE_UNTIED:
20837           clauses = cp_parser_omp_clause_untied (parser, clauses,
20838                                                  token->location);
20839           c_name = "nowait";
20840           break;
20841         default:
20842           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20843           goto saw_error;
20844         }
20845
20846       if (((mask >> c_kind) & 1) == 0)
20847         {
20848           /* Remove the invalid clause(s) from the list to avoid
20849              confusing the rest of the compiler.  */
20850           clauses = prev;
20851           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20852         }
20853     }
20854  saw_error:
20855   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20856   return finish_omp_clauses (clauses);
20857 }
20858
20859 /* OpenMP 2.5:
20860    structured-block:
20861      statement
20862
20863    In practice, we're also interested in adding the statement to an
20864    outer node.  So it is convenient if we work around the fact that
20865    cp_parser_statement calls add_stmt.  */
20866
20867 static unsigned
20868 cp_parser_begin_omp_structured_block (cp_parser *parser)
20869 {
20870   unsigned save = parser->in_statement;
20871
20872   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20873      This preserves the "not within loop or switch" style error messages
20874      for nonsense cases like
20875         void foo() {
20876         #pragma omp single
20877           break;
20878         }
20879   */
20880   if (parser->in_statement)
20881     parser->in_statement = IN_OMP_BLOCK;
20882
20883   return save;
20884 }
20885
20886 static void
20887 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20888 {
20889   parser->in_statement = save;
20890 }
20891
20892 static tree
20893 cp_parser_omp_structured_block (cp_parser *parser)
20894 {
20895   tree stmt = begin_omp_structured_block ();
20896   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20897
20898   cp_parser_statement (parser, NULL_TREE, false, NULL);
20899
20900   cp_parser_end_omp_structured_block (parser, save);
20901   return finish_omp_structured_block (stmt);
20902 }
20903
20904 /* OpenMP 2.5:
20905    # pragma omp atomic new-line
20906      expression-stmt
20907
20908    expression-stmt:
20909      x binop= expr | x++ | ++x | x-- | --x
20910    binop:
20911      +, *, -, /, &, ^, |, <<, >>
20912
20913   where x is an lvalue expression with scalar type.  */
20914
20915 static void
20916 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20917 {
20918   tree lhs, rhs;
20919   enum tree_code code;
20920
20921   cp_parser_require_pragma_eol (parser, pragma_tok);
20922
20923   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20924                                     /*cast_p=*/false, NULL);
20925   switch (TREE_CODE (lhs))
20926     {
20927     case ERROR_MARK:
20928       goto saw_error;
20929
20930     case PREINCREMENT_EXPR:
20931     case POSTINCREMENT_EXPR:
20932       lhs = TREE_OPERAND (lhs, 0);
20933       code = PLUS_EXPR;
20934       rhs = integer_one_node;
20935       break;
20936
20937     case PREDECREMENT_EXPR:
20938     case POSTDECREMENT_EXPR:
20939       lhs = TREE_OPERAND (lhs, 0);
20940       code = MINUS_EXPR;
20941       rhs = integer_one_node;
20942       break;
20943
20944     default:
20945       switch (cp_lexer_peek_token (parser->lexer)->type)
20946         {
20947         case CPP_MULT_EQ:
20948           code = MULT_EXPR;
20949           break;
20950         case CPP_DIV_EQ:
20951           code = TRUNC_DIV_EXPR;
20952           break;
20953         case CPP_PLUS_EQ:
20954           code = PLUS_EXPR;
20955           break;
20956         case CPP_MINUS_EQ:
20957           code = MINUS_EXPR;
20958           break;
20959         case CPP_LSHIFT_EQ:
20960           code = LSHIFT_EXPR;
20961           break;
20962         case CPP_RSHIFT_EQ:
20963           code = RSHIFT_EXPR;
20964           break;
20965         case CPP_AND_EQ:
20966           code = BIT_AND_EXPR;
20967           break;
20968         case CPP_OR_EQ:
20969           code = BIT_IOR_EXPR;
20970           break;
20971         case CPP_XOR_EQ:
20972           code = BIT_XOR_EXPR;
20973           break;
20974         default:
20975           cp_parser_error (parser,
20976                            "invalid operator for %<#pragma omp atomic%>");
20977           goto saw_error;
20978         }
20979       cp_lexer_consume_token (parser->lexer);
20980
20981       rhs = cp_parser_expression (parser, false, NULL);
20982       if (rhs == error_mark_node)
20983         goto saw_error;
20984       break;
20985     }
20986   finish_omp_atomic (code, lhs, rhs);
20987   cp_parser_consume_semicolon_at_end_of_statement (parser);
20988   return;
20989
20990  saw_error:
20991   cp_parser_skip_to_end_of_block_or_statement (parser);
20992 }
20993
20994
20995 /* OpenMP 2.5:
20996    # pragma omp barrier new-line  */
20997
20998 static void
20999 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21000 {
21001   cp_parser_require_pragma_eol (parser, pragma_tok);
21002   finish_omp_barrier ();
21003 }
21004
21005 /* OpenMP 2.5:
21006    # pragma omp critical [(name)] new-line
21007      structured-block  */
21008
21009 static tree
21010 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21011 {
21012   tree stmt, name = NULL;
21013
21014   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21015     {
21016       cp_lexer_consume_token (parser->lexer);
21017
21018       name = cp_parser_identifier (parser);
21019
21020       if (name == error_mark_node
21021           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21022         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21023                                                /*or_comma=*/false,
21024                                                /*consume_paren=*/true);
21025       if (name == error_mark_node)
21026         name = NULL;
21027     }
21028   cp_parser_require_pragma_eol (parser, pragma_tok);
21029
21030   stmt = cp_parser_omp_structured_block (parser);
21031   return c_finish_omp_critical (stmt, name);
21032 }
21033
21034 /* OpenMP 2.5:
21035    # pragma omp flush flush-vars[opt] new-line
21036
21037    flush-vars:
21038      ( variable-list ) */
21039
21040 static void
21041 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21042 {
21043   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21044     (void) cp_parser_omp_var_list (parser, 0, NULL);
21045   cp_parser_require_pragma_eol (parser, pragma_tok);
21046
21047   finish_omp_flush ();
21048 }
21049
21050 /* Helper function, to parse omp for increment expression.  */
21051
21052 static tree
21053 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21054 {
21055   tree cond = cp_parser_binary_expression (parser, false, true,
21056                                            PREC_NOT_OPERATOR, NULL);
21057   bool overloaded_p;
21058
21059   if (cond == error_mark_node
21060       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21061     {
21062       cp_parser_skip_to_end_of_statement (parser);
21063       return error_mark_node;
21064     }
21065
21066   switch (TREE_CODE (cond))
21067     {
21068     case GT_EXPR:
21069     case GE_EXPR:
21070     case LT_EXPR:
21071     case LE_EXPR:
21072       break;
21073     default:
21074       return error_mark_node;
21075     }
21076
21077   /* If decl is an iterator, preserve LHS and RHS of the relational
21078      expr until finish_omp_for.  */
21079   if (decl
21080       && (type_dependent_expression_p (decl)
21081           || CLASS_TYPE_P (TREE_TYPE (decl))))
21082     return cond;
21083
21084   return build_x_binary_op (TREE_CODE (cond),
21085                             TREE_OPERAND (cond, 0), ERROR_MARK,
21086                             TREE_OPERAND (cond, 1), ERROR_MARK,
21087                             &overloaded_p, tf_warning_or_error);
21088 }
21089
21090 /* Helper function, to parse omp for increment expression.  */
21091
21092 static tree
21093 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21094 {
21095   cp_token *token = cp_lexer_peek_token (parser->lexer);
21096   enum tree_code op;
21097   tree lhs, rhs;
21098   cp_id_kind idk;
21099   bool decl_first;
21100
21101   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21102     {
21103       op = (token->type == CPP_PLUS_PLUS
21104             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21105       cp_lexer_consume_token (parser->lexer);
21106       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21107       if (lhs != decl)
21108         return error_mark_node;
21109       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21110     }
21111
21112   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21113   if (lhs != decl)
21114     return error_mark_node;
21115
21116   token = cp_lexer_peek_token (parser->lexer);
21117   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21118     {
21119       op = (token->type == CPP_PLUS_PLUS
21120             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21121       cp_lexer_consume_token (parser->lexer);
21122       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21123     }
21124
21125   op = cp_parser_assignment_operator_opt (parser);
21126   if (op == ERROR_MARK)
21127     return error_mark_node;
21128
21129   if (op != NOP_EXPR)
21130     {
21131       rhs = cp_parser_assignment_expression (parser, false, NULL);
21132       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21133       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21134     }
21135
21136   lhs = cp_parser_binary_expression (parser, false, false,
21137                                      PREC_ADDITIVE_EXPRESSION, NULL);
21138   token = cp_lexer_peek_token (parser->lexer);
21139   decl_first = lhs == decl;
21140   if (decl_first)
21141     lhs = NULL_TREE;
21142   if (token->type != CPP_PLUS
21143       && token->type != CPP_MINUS)
21144     return error_mark_node;
21145
21146   do
21147     {
21148       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21149       cp_lexer_consume_token (parser->lexer);
21150       rhs = cp_parser_binary_expression (parser, false, false,
21151                                          PREC_ADDITIVE_EXPRESSION, NULL);
21152       token = cp_lexer_peek_token (parser->lexer);
21153       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21154         {
21155           if (lhs == NULL_TREE)
21156             {
21157               if (op == PLUS_EXPR)
21158                 lhs = rhs;
21159               else
21160                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21161             }
21162           else
21163             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21164                                      NULL, tf_warning_or_error);
21165         }
21166     }
21167   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21168
21169   if (!decl_first)
21170     {
21171       if (rhs != decl || op == MINUS_EXPR)
21172         return error_mark_node;
21173       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21174     }
21175   else
21176     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21177
21178   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21179 }
21180
21181 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21182
21183 static tree
21184 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21185 {
21186   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21187   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21188   tree this_pre_body, cl;
21189   location_t loc_first;
21190   bool collapse_err = false;
21191   int i, collapse = 1, nbraces = 0;
21192
21193   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21194     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21195       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21196
21197   gcc_assert (collapse >= 1);
21198
21199   declv = make_tree_vec (collapse);
21200   initv = make_tree_vec (collapse);
21201   condv = make_tree_vec (collapse);
21202   incrv = make_tree_vec (collapse);
21203
21204   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21205
21206   for (i = 0; i < collapse; i++)
21207     {
21208       int bracecount = 0;
21209       bool add_private_clause = false;
21210       location_t loc;
21211
21212       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21213         {
21214           cp_parser_error (parser, "for statement expected");
21215           return NULL;
21216         }
21217       loc = cp_lexer_consume_token (parser->lexer)->location;
21218
21219       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21220         return NULL;
21221
21222       init = decl = real_decl = NULL;
21223       this_pre_body = push_stmt_list ();
21224       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21225         {
21226           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21227
21228              init-expr:
21229                        var = lb
21230                        integer-type var = lb
21231                        random-access-iterator-type var = lb
21232                        pointer-type var = lb
21233           */
21234           cp_decl_specifier_seq type_specifiers;
21235
21236           /* First, try to parse as an initialized declaration.  See
21237              cp_parser_condition, from whence the bulk of this is copied.  */
21238
21239           cp_parser_parse_tentatively (parser);
21240           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21241                                         &type_specifiers);
21242           if (cp_parser_parse_definitely (parser))
21243             {
21244               /* If parsing a type specifier seq succeeded, then this
21245                  MUST be a initialized declaration.  */
21246               tree asm_specification, attributes;
21247               cp_declarator *declarator;
21248
21249               declarator = cp_parser_declarator (parser,
21250                                                  CP_PARSER_DECLARATOR_NAMED,
21251                                                  /*ctor_dtor_or_conv_p=*/NULL,
21252                                                  /*parenthesized_p=*/NULL,
21253                                                  /*member_p=*/false);
21254               attributes = cp_parser_attributes_opt (parser);
21255               asm_specification = cp_parser_asm_specification_opt (parser);
21256
21257               if (declarator == cp_error_declarator) 
21258                 cp_parser_skip_to_end_of_statement (parser);
21259
21260               else 
21261                 {
21262                   tree pushed_scope, auto_node;
21263
21264                   decl = start_decl (declarator, &type_specifiers,
21265                                      SD_INITIALIZED, attributes,
21266                                      /*prefix_attributes=*/NULL_TREE,
21267                                      &pushed_scope);
21268
21269                   auto_node = type_uses_auto (TREE_TYPE (decl));
21270                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21271                     {
21272                       if (cp_lexer_next_token_is (parser->lexer, 
21273                                                   CPP_OPEN_PAREN))
21274                         error ("parenthesized initialization is not allowed in "
21275                                "OpenMP %<for%> loop");
21276                       else
21277                         /* Trigger an error.  */
21278                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21279
21280                       init = error_mark_node;
21281                       cp_parser_skip_to_end_of_statement (parser);
21282                     }
21283                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21284                            || type_dependent_expression_p (decl)
21285                            || auto_node)
21286                     {
21287                       bool is_direct_init, is_non_constant_init;
21288
21289                       init = cp_parser_initializer (parser,
21290                                                     &is_direct_init,
21291                                                     &is_non_constant_init);
21292
21293                       if (auto_node && describable_type (init))
21294                         {
21295                           TREE_TYPE (decl)
21296                             = do_auto_deduction (TREE_TYPE (decl), init,
21297                                                  auto_node);
21298
21299                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21300                               && !type_dependent_expression_p (decl))
21301                             goto non_class;
21302                         }
21303                       
21304                       cp_finish_decl (decl, init, !is_non_constant_init,
21305                                       asm_specification,
21306                                       LOOKUP_ONLYCONVERTING);
21307                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21308                         {
21309                           for_block
21310                             = tree_cons (NULL, this_pre_body, for_block);
21311                           init = NULL_TREE;
21312                         }
21313                       else
21314                         init = pop_stmt_list (this_pre_body);
21315                       this_pre_body = NULL_TREE;
21316                     }
21317                   else
21318                     {
21319                       /* Consume '='.  */
21320                       cp_lexer_consume_token (parser->lexer);
21321                       init = cp_parser_assignment_expression (parser, false, NULL);
21322
21323                     non_class:
21324                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21325                         init = error_mark_node;
21326                       else
21327                         cp_finish_decl (decl, NULL_TREE,
21328                                         /*init_const_expr_p=*/false,
21329                                         asm_specification,
21330                                         LOOKUP_ONLYCONVERTING);
21331                     }
21332
21333                   if (pushed_scope)
21334                     pop_scope (pushed_scope);
21335                 }
21336             }
21337           else 
21338             {
21339               cp_id_kind idk;
21340               /* If parsing a type specifier sequence failed, then
21341                  this MUST be a simple expression.  */
21342               cp_parser_parse_tentatively (parser);
21343               decl = cp_parser_primary_expression (parser, false, false,
21344                                                    false, &idk);
21345               if (!cp_parser_error_occurred (parser)
21346                   && decl
21347                   && DECL_P (decl)
21348                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21349                 {
21350                   tree rhs;
21351
21352                   cp_parser_parse_definitely (parser);
21353                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21354                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21355                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21356                                                          rhs,
21357                                                          tf_warning_or_error));
21358                   add_private_clause = true;
21359                 }
21360               else
21361                 {
21362                   decl = NULL;
21363                   cp_parser_abort_tentative_parse (parser);
21364                   init = cp_parser_expression (parser, false, NULL);
21365                   if (init)
21366                     {
21367                       if (TREE_CODE (init) == MODIFY_EXPR
21368                           || TREE_CODE (init) == MODOP_EXPR)
21369                         real_decl = TREE_OPERAND (init, 0);
21370                     }
21371                 }
21372             }
21373         }
21374       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21375       if (this_pre_body)
21376         {
21377           this_pre_body = pop_stmt_list (this_pre_body);
21378           if (pre_body)
21379             {
21380               tree t = pre_body;
21381               pre_body = push_stmt_list ();
21382               add_stmt (t);
21383               add_stmt (this_pre_body);
21384               pre_body = pop_stmt_list (pre_body);
21385             }
21386           else
21387             pre_body = this_pre_body;
21388         }
21389
21390       if (decl)
21391         real_decl = decl;
21392       if (par_clauses != NULL && real_decl != NULL_TREE)
21393         {
21394           tree *c;
21395           for (c = par_clauses; *c ; )
21396             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21397                 && OMP_CLAUSE_DECL (*c) == real_decl)
21398               {
21399                 error ("%Hiteration variable %qD should not be firstprivate",
21400                        &loc, real_decl);
21401                 *c = OMP_CLAUSE_CHAIN (*c);
21402               }
21403             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21404                      && OMP_CLAUSE_DECL (*c) == real_decl)
21405               {
21406                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21407                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21408                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21409                 OMP_CLAUSE_DECL (l) = real_decl;
21410                 OMP_CLAUSE_CHAIN (l) = clauses;
21411                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21412                 clauses = l;
21413                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21414                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21415                 add_private_clause = false;
21416               }
21417             else
21418               {
21419                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21420                     && OMP_CLAUSE_DECL (*c) == real_decl)
21421                   add_private_clause = false;
21422                 c = &OMP_CLAUSE_CHAIN (*c);
21423               }
21424         }
21425
21426       if (add_private_clause)
21427         {
21428           tree c;
21429           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21430             {
21431               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21432                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21433                   && OMP_CLAUSE_DECL (c) == decl)
21434                 break;
21435               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21436                        && OMP_CLAUSE_DECL (c) == decl)
21437                 error ("%Hiteration variable %qD should not be firstprivate",
21438                        &loc, decl);
21439               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21440                        && OMP_CLAUSE_DECL (c) == decl)
21441                 error ("%Hiteration variable %qD should not be reduction",
21442                        &loc, decl);
21443             }
21444           if (c == NULL)
21445             {
21446               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21447               OMP_CLAUSE_DECL (c) = decl;
21448               c = finish_omp_clauses (c);
21449               if (c)
21450                 {
21451                   OMP_CLAUSE_CHAIN (c) = clauses;
21452                   clauses = c;
21453                 }
21454             }
21455         }
21456
21457       cond = NULL;
21458       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21459         cond = cp_parser_omp_for_cond (parser, decl);
21460       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21461
21462       incr = NULL;
21463       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21464         {
21465           /* If decl is an iterator, preserve the operator on decl
21466              until finish_omp_for.  */
21467           if (decl
21468               && (type_dependent_expression_p (decl)
21469                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21470             incr = cp_parser_omp_for_incr (parser, decl);
21471           else
21472             incr = cp_parser_expression (parser, false, NULL);
21473         }
21474
21475       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21476         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21477                                                /*or_comma=*/false,
21478                                                /*consume_paren=*/true);
21479
21480       TREE_VEC_ELT (declv, i) = decl;
21481       TREE_VEC_ELT (initv, i) = init;
21482       TREE_VEC_ELT (condv, i) = cond;
21483       TREE_VEC_ELT (incrv, i) = incr;
21484
21485       if (i == collapse - 1)
21486         break;
21487
21488       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21489          in between the collapsed for loops to be still considered perfectly
21490          nested.  Hopefully the final version clarifies this.
21491          For now handle (multiple) {'s and empty statements.  */
21492       cp_parser_parse_tentatively (parser);
21493       do
21494         {
21495           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21496             break;
21497           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21498             {
21499               cp_lexer_consume_token (parser->lexer);
21500               bracecount++;
21501             }
21502           else if (bracecount
21503                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21504             cp_lexer_consume_token (parser->lexer);
21505           else
21506             {
21507               loc = cp_lexer_peek_token (parser->lexer)->location;
21508               error ("%Hnot enough collapsed for loops", &loc);
21509               collapse_err = true;
21510               cp_parser_abort_tentative_parse (parser);
21511               declv = NULL_TREE;
21512               break;
21513             }
21514         }
21515       while (1);
21516
21517       if (declv)
21518         {
21519           cp_parser_parse_definitely (parser);
21520           nbraces += bracecount;
21521         }
21522     }
21523
21524   /* Note that we saved the original contents of this flag when we entered
21525      the structured block, and so we don't need to re-save it here.  */
21526   parser->in_statement = IN_OMP_FOR;
21527
21528   /* Note that the grammar doesn't call for a structured block here,
21529      though the loop as a whole is a structured block.  */
21530   body = push_stmt_list ();
21531   cp_parser_statement (parser, NULL_TREE, false, NULL);
21532   body = pop_stmt_list (body);
21533
21534   if (declv == NULL_TREE)
21535     ret = NULL_TREE;
21536   else
21537     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21538                           pre_body, clauses);
21539
21540   while (nbraces)
21541     {
21542       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21543         {
21544           cp_lexer_consume_token (parser->lexer);
21545           nbraces--;
21546         }
21547       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21548         cp_lexer_consume_token (parser->lexer);
21549       else
21550         {
21551           if (!collapse_err)
21552             {
21553               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21554               error ("%Hcollapsed loops not perfectly nested", &loc);
21555             }
21556           collapse_err = true;
21557           cp_parser_statement_seq_opt (parser, NULL);
21558           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21559         }
21560     }
21561
21562   while (for_block)
21563     {
21564       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21565       for_block = TREE_CHAIN (for_block);
21566     }
21567
21568   return ret;
21569 }
21570
21571 /* OpenMP 2.5:
21572    #pragma omp for for-clause[optseq] new-line
21573      for-loop  */
21574
21575 #define OMP_FOR_CLAUSE_MASK                             \
21576         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21577         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21578         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21579         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21580         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21581         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21582         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21583         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21584
21585 static tree
21586 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21587 {
21588   tree clauses, sb, ret;
21589   unsigned int save;
21590
21591   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21592                                        "#pragma omp for", pragma_tok);
21593
21594   sb = begin_omp_structured_block ();
21595   save = cp_parser_begin_omp_structured_block (parser);
21596
21597   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21598
21599   cp_parser_end_omp_structured_block (parser, save);
21600   add_stmt (finish_omp_structured_block (sb));
21601
21602   return ret;
21603 }
21604
21605 /* OpenMP 2.5:
21606    # pragma omp master new-line
21607      structured-block  */
21608
21609 static tree
21610 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21611 {
21612   cp_parser_require_pragma_eol (parser, pragma_tok);
21613   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21614 }
21615
21616 /* OpenMP 2.5:
21617    # pragma omp ordered new-line
21618      structured-block  */
21619
21620 static tree
21621 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21622 {
21623   cp_parser_require_pragma_eol (parser, pragma_tok);
21624   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21625 }
21626
21627 /* OpenMP 2.5:
21628
21629    section-scope:
21630      { section-sequence }
21631
21632    section-sequence:
21633      section-directive[opt] structured-block
21634      section-sequence section-directive structured-block  */
21635
21636 static tree
21637 cp_parser_omp_sections_scope (cp_parser *parser)
21638 {
21639   tree stmt, substmt;
21640   bool error_suppress = false;
21641   cp_token *tok;
21642
21643   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21644     return NULL_TREE;
21645
21646   stmt = push_stmt_list ();
21647
21648   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21649     {
21650       unsigned save;
21651
21652       substmt = begin_omp_structured_block ();
21653       save = cp_parser_begin_omp_structured_block (parser);
21654
21655       while (1)
21656         {
21657           cp_parser_statement (parser, NULL_TREE, false, NULL);
21658
21659           tok = cp_lexer_peek_token (parser->lexer);
21660           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21661             break;
21662           if (tok->type == CPP_CLOSE_BRACE)
21663             break;
21664           if (tok->type == CPP_EOF)
21665             break;
21666         }
21667
21668       cp_parser_end_omp_structured_block (parser, save);
21669       substmt = finish_omp_structured_block (substmt);
21670       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21671       add_stmt (substmt);
21672     }
21673
21674   while (1)
21675     {
21676       tok = cp_lexer_peek_token (parser->lexer);
21677       if (tok->type == CPP_CLOSE_BRACE)
21678         break;
21679       if (tok->type == CPP_EOF)
21680         break;
21681
21682       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21683         {
21684           cp_lexer_consume_token (parser->lexer);
21685           cp_parser_require_pragma_eol (parser, tok);
21686           error_suppress = false;
21687         }
21688       else if (!error_suppress)
21689         {
21690           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21691           error_suppress = true;
21692         }
21693
21694       substmt = cp_parser_omp_structured_block (parser);
21695       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21696       add_stmt (substmt);
21697     }
21698   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21699
21700   substmt = pop_stmt_list (stmt);
21701
21702   stmt = make_node (OMP_SECTIONS);
21703   TREE_TYPE (stmt) = void_type_node;
21704   OMP_SECTIONS_BODY (stmt) = substmt;
21705
21706   add_stmt (stmt);
21707   return stmt;
21708 }
21709
21710 /* OpenMP 2.5:
21711    # pragma omp sections sections-clause[optseq] newline
21712      sections-scope  */
21713
21714 #define OMP_SECTIONS_CLAUSE_MASK                        \
21715         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21716         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21717         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21718         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21719         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21720
21721 static tree
21722 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21723 {
21724   tree clauses, ret;
21725
21726   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21727                                        "#pragma omp sections", pragma_tok);
21728
21729   ret = cp_parser_omp_sections_scope (parser);
21730   if (ret)
21731     OMP_SECTIONS_CLAUSES (ret) = clauses;
21732
21733   return ret;
21734 }
21735
21736 /* OpenMP 2.5:
21737    # pragma parallel parallel-clause new-line
21738    # pragma parallel for parallel-for-clause new-line
21739    # pragma parallel sections parallel-sections-clause new-line  */
21740
21741 #define OMP_PARALLEL_CLAUSE_MASK                        \
21742         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21743         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21744         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21745         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21746         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21747         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21748         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21749         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21750
21751 static tree
21752 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21753 {
21754   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21755   const char *p_name = "#pragma omp parallel";
21756   tree stmt, clauses, par_clause, ws_clause, block;
21757   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21758   unsigned int save;
21759
21760   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21761     {
21762       cp_lexer_consume_token (parser->lexer);
21763       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21764       p_name = "#pragma omp parallel for";
21765       mask |= OMP_FOR_CLAUSE_MASK;
21766       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21767     }
21768   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21769     {
21770       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21771       const char *p = IDENTIFIER_POINTER (id);
21772       if (strcmp (p, "sections") == 0)
21773         {
21774           cp_lexer_consume_token (parser->lexer);
21775           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21776           p_name = "#pragma omp parallel sections";
21777           mask |= OMP_SECTIONS_CLAUSE_MASK;
21778           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21779         }
21780     }
21781
21782   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21783   block = begin_omp_parallel ();
21784   save = cp_parser_begin_omp_structured_block (parser);
21785
21786   switch (p_kind)
21787     {
21788     case PRAGMA_OMP_PARALLEL:
21789       cp_parser_statement (parser, NULL_TREE, false, NULL);
21790       par_clause = clauses;
21791       break;
21792
21793     case PRAGMA_OMP_PARALLEL_FOR:
21794       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21795       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21796       break;
21797
21798     case PRAGMA_OMP_PARALLEL_SECTIONS:
21799       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21800       stmt = cp_parser_omp_sections_scope (parser);
21801       if (stmt)
21802         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21803       break;
21804
21805     default:
21806       gcc_unreachable ();
21807     }
21808
21809   cp_parser_end_omp_structured_block (parser, save);
21810   stmt = finish_omp_parallel (par_clause, block);
21811   if (p_kind != PRAGMA_OMP_PARALLEL)
21812     OMP_PARALLEL_COMBINED (stmt) = 1;
21813   return stmt;
21814 }
21815
21816 /* OpenMP 2.5:
21817    # pragma omp single single-clause[optseq] new-line
21818      structured-block  */
21819
21820 #define OMP_SINGLE_CLAUSE_MASK                          \
21821         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21822         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21823         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21824         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21825
21826 static tree
21827 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21828 {
21829   tree stmt = make_node (OMP_SINGLE);
21830   TREE_TYPE (stmt) = void_type_node;
21831
21832   OMP_SINGLE_CLAUSES (stmt)
21833     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21834                                  "#pragma omp single", pragma_tok);
21835   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21836
21837   return add_stmt (stmt);
21838 }
21839
21840 /* OpenMP 3.0:
21841    # pragma omp task task-clause[optseq] new-line
21842      structured-block  */
21843
21844 #define OMP_TASK_CLAUSE_MASK                            \
21845         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21846         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21847         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21848         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21849         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21850         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21851
21852 static tree
21853 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21854 {
21855   tree clauses, block;
21856   unsigned int save;
21857
21858   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21859                                        "#pragma omp task", pragma_tok);
21860   block = begin_omp_task ();
21861   save = cp_parser_begin_omp_structured_block (parser);
21862   cp_parser_statement (parser, NULL_TREE, false, NULL);
21863   cp_parser_end_omp_structured_block (parser, save);
21864   return finish_omp_task (clauses, block);
21865 }
21866
21867 /* OpenMP 3.0:
21868    # pragma omp taskwait new-line  */
21869
21870 static void
21871 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21872 {
21873   cp_parser_require_pragma_eol (parser, pragma_tok);
21874   finish_omp_taskwait ();
21875 }
21876
21877 /* OpenMP 2.5:
21878    # pragma omp threadprivate (variable-list) */
21879
21880 static void
21881 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21882 {
21883   tree vars;
21884
21885   vars = cp_parser_omp_var_list (parser, 0, NULL);
21886   cp_parser_require_pragma_eol (parser, pragma_tok);
21887
21888   finish_omp_threadprivate (vars);
21889 }
21890
21891 /* Main entry point to OpenMP statement pragmas.  */
21892
21893 static void
21894 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21895 {
21896   tree stmt;
21897
21898   switch (pragma_tok->pragma_kind)
21899     {
21900     case PRAGMA_OMP_ATOMIC:
21901       cp_parser_omp_atomic (parser, pragma_tok);
21902       return;
21903     case PRAGMA_OMP_CRITICAL:
21904       stmt = cp_parser_omp_critical (parser, pragma_tok);
21905       break;
21906     case PRAGMA_OMP_FOR:
21907       stmt = cp_parser_omp_for (parser, pragma_tok);
21908       break;
21909     case PRAGMA_OMP_MASTER:
21910       stmt = cp_parser_omp_master (parser, pragma_tok);
21911       break;
21912     case PRAGMA_OMP_ORDERED:
21913       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21914       break;
21915     case PRAGMA_OMP_PARALLEL:
21916       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21917       break;
21918     case PRAGMA_OMP_SECTIONS:
21919       stmt = cp_parser_omp_sections (parser, pragma_tok);
21920       break;
21921     case PRAGMA_OMP_SINGLE:
21922       stmt = cp_parser_omp_single (parser, pragma_tok);
21923       break;
21924     case PRAGMA_OMP_TASK:
21925       stmt = cp_parser_omp_task (parser, pragma_tok);
21926       break;
21927     default:
21928       gcc_unreachable ();
21929     }
21930
21931   if (stmt)
21932     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21933 }
21934 \f
21935 /* The parser.  */
21936
21937 static GTY (()) cp_parser *the_parser;
21938
21939 \f
21940 /* Special handling for the first token or line in the file.  The first
21941    thing in the file might be #pragma GCC pch_preprocess, which loads a
21942    PCH file, which is a GC collection point.  So we need to handle this
21943    first pragma without benefit of an existing lexer structure.
21944
21945    Always returns one token to the caller in *FIRST_TOKEN.  This is
21946    either the true first token of the file, or the first token after
21947    the initial pragma.  */
21948
21949 static void
21950 cp_parser_initial_pragma (cp_token *first_token)
21951 {
21952   tree name = NULL;
21953
21954   cp_lexer_get_preprocessor_token (NULL, first_token);
21955   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21956     return;
21957
21958   cp_lexer_get_preprocessor_token (NULL, first_token);
21959   if (first_token->type == CPP_STRING)
21960     {
21961       name = first_token->u.value;
21962
21963       cp_lexer_get_preprocessor_token (NULL, first_token);
21964       if (first_token->type != CPP_PRAGMA_EOL)
21965         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21966                &first_token->location);
21967     }
21968   else
21969     error ("%Hexpected string literal", &first_token->location);
21970
21971   /* Skip to the end of the pragma.  */
21972   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21973     cp_lexer_get_preprocessor_token (NULL, first_token);
21974
21975   /* Now actually load the PCH file.  */
21976   if (name)
21977     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21978
21979   /* Read one more token to return to our caller.  We have to do this
21980      after reading the PCH file in, since its pointers have to be
21981      live.  */
21982   cp_lexer_get_preprocessor_token (NULL, first_token);
21983 }
21984
21985 /* Normal parsing of a pragma token.  Here we can (and must) use the
21986    regular lexer.  */
21987
21988 static bool
21989 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21990 {
21991   cp_token *pragma_tok;
21992   unsigned int id;
21993
21994   pragma_tok = cp_lexer_consume_token (parser->lexer);
21995   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21996   parser->lexer->in_pragma = true;
21997
21998   id = pragma_tok->pragma_kind;
21999   switch (id)
22000     {
22001     case PRAGMA_GCC_PCH_PREPROCESS:
22002       error ("%H%<#pragma GCC pch_preprocess%> must be first",
22003              &pragma_tok->location);
22004       break;
22005
22006     case PRAGMA_OMP_BARRIER:
22007       switch (context)
22008         {
22009         case pragma_compound:
22010           cp_parser_omp_barrier (parser, pragma_tok);
22011           return false;
22012         case pragma_stmt:
22013           error ("%H%<#pragma omp barrier%> may only be "
22014                  "used in compound statements", &pragma_tok->location);
22015           break;
22016         default:
22017           goto bad_stmt;
22018         }
22019       break;
22020
22021     case PRAGMA_OMP_FLUSH:
22022       switch (context)
22023         {
22024         case pragma_compound:
22025           cp_parser_omp_flush (parser, pragma_tok);
22026           return false;
22027         case pragma_stmt:
22028           error ("%H%<#pragma omp flush%> may only be "
22029                  "used in compound statements", &pragma_tok->location);
22030           break;
22031         default:
22032           goto bad_stmt;
22033         }
22034       break;
22035
22036     case PRAGMA_OMP_TASKWAIT:
22037       switch (context)
22038         {
22039         case pragma_compound:
22040           cp_parser_omp_taskwait (parser, pragma_tok);
22041           return false;
22042         case pragma_stmt:
22043           error ("%H%<#pragma omp taskwait%> may only be "
22044                  "used in compound statements",
22045                  &pragma_tok->location);
22046           break;
22047         default:
22048           goto bad_stmt;
22049         }
22050       break;
22051
22052     case PRAGMA_OMP_THREADPRIVATE:
22053       cp_parser_omp_threadprivate (parser, pragma_tok);
22054       return false;
22055
22056     case PRAGMA_OMP_ATOMIC:
22057     case PRAGMA_OMP_CRITICAL:
22058     case PRAGMA_OMP_FOR:
22059     case PRAGMA_OMP_MASTER:
22060     case PRAGMA_OMP_ORDERED:
22061     case PRAGMA_OMP_PARALLEL:
22062     case PRAGMA_OMP_SECTIONS:
22063     case PRAGMA_OMP_SINGLE:
22064     case PRAGMA_OMP_TASK:
22065       if (context == pragma_external)
22066         goto bad_stmt;
22067       cp_parser_omp_construct (parser, pragma_tok);
22068       return true;
22069
22070     case PRAGMA_OMP_SECTION:
22071       error ("%H%<#pragma omp section%> may only be used in "
22072              "%<#pragma omp sections%> construct", &pragma_tok->location);
22073       break;
22074
22075     default:
22076       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22077       c_invoke_pragma_handler (id);
22078       break;
22079
22080     bad_stmt:
22081       cp_parser_error (parser, "expected declaration specifiers");
22082       break;
22083     }
22084
22085   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22086   return false;
22087 }
22088
22089 /* The interface the pragma parsers have to the lexer.  */
22090
22091 enum cpp_ttype
22092 pragma_lex (tree *value)
22093 {
22094   cp_token *tok;
22095   enum cpp_ttype ret;
22096
22097   tok = cp_lexer_peek_token (the_parser->lexer);
22098
22099   ret = tok->type;
22100   *value = tok->u.value;
22101
22102   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22103     ret = CPP_EOF;
22104   else if (ret == CPP_STRING)
22105     *value = cp_parser_string_literal (the_parser, false, false);
22106   else
22107     {
22108       cp_lexer_consume_token (the_parser->lexer);
22109       if (ret == CPP_KEYWORD)
22110         ret = CPP_NAME;
22111     }
22112
22113   return ret;
22114 }
22115
22116 \f
22117 /* External interface.  */
22118
22119 /* Parse one entire translation unit.  */
22120
22121 void
22122 c_parse_file (void)
22123 {
22124   bool error_occurred;
22125   static bool already_called = false;
22126
22127   if (already_called)
22128     {
22129       sorry ("inter-module optimizations not implemented for C++");
22130       return;
22131     }
22132   already_called = true;
22133
22134   the_parser = cp_parser_new ();
22135   push_deferring_access_checks (flag_access_control
22136                                 ? dk_no_deferred : dk_no_check);
22137   error_occurred = cp_parser_translation_unit (the_parser);
22138   the_parser = NULL;
22139 }
22140
22141 #include "gt-cp-parser.h"