OSDN Git Service

* parser.c (cp_parser_type_id_1): 'auto' type is ok with a
[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 #include "plugin.h"
41
42 \f
43 /* The lexer.  */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46    and c-lex.c) and the C++ parser.  */
47
48 /* A token's value and its associated deferred access checks and
49    qualifying scope.  */
50
51 struct GTY(()) tree_check {
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 GTY (()) cp_token {
64   /* The kind of token.  */
65   ENUM_BITFIELD (cpp_ttype) type : 8;
66   /* If this token is a keyword, this value indicates which keyword.
67      Otherwise, this value is RID_MAX.  */
68   ENUM_BITFIELD (rid) keyword : 8;
69   /* Token flags.  */
70   unsigned char flags;
71   /* Identifier for the pragma.  */
72   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73   /* True if this token is from a context where it is implicitly extern "C" */
74   BOOL_BITFIELD implicit_extern_c : 1;
75   /* True for a CPP_NAME token that is not a keyword (i.e., for which
76      KEYWORD is RID_MAX) iff this name was looked up and found to be
77      ambiguous.  An error has already been reported.  */
78   BOOL_BITFIELD ambiguous_p : 1;
79   /* The location at which this token was found.  */
80   location_t location;
81   /* The value associated with this token, if any.  */
82   union cp_token_value {
83     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
84     struct tree_check* GTY((tag ("1"))) tree_check_value;
85     /* Use for all other tokens.  */
86     tree GTY((tag ("0"))) value;
87   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
88 } cp_token;
89
90 /* We use a stack of token pointer for saving token sets.  */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
94
95 static cp_token eof_token =
96 {
97   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
98 };
99
100 /* The cp_lexer structure represents the C++ lexer.  It is responsible
101    for managing the token stream from the preprocessor and supplying
102    it to the parser.  Tokens are never added to the cp_lexer after
103    it is created.  */
104
105 typedef struct GTY (()) cp_lexer {
106   /* The memory allocated for the buffer.  NULL if this lexer does not
107      own the token buffer.  */
108   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109   /* If the lexer owns the buffer, this is the number of tokens in the
110      buffer.  */
111   size_t buffer_length;
112
113   /* A pointer just past the last available token.  The tokens
114      in this lexer are [buffer, last_token).  */
115   cp_token_position GTY ((skip)) last_token;
116
117   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
118      no more available tokens.  */
119   cp_token_position GTY ((skip)) next_token;
120
121   /* A stack indicating positions at which cp_lexer_save_tokens was
122      called.  The top entry is the most recent position at which we
123      began saving tokens.  If the stack is non-empty, we are saving
124      tokens.  */
125   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126
127   /* The next lexer in a linked list of lexers.  */
128   struct cp_lexer *next;
129
130   /* True if we should output debugging information.  */
131   bool debugging_p;
132
133   /* True if we're in the context of parsing a pragma, and should not
134      increment past the end-of-line marker.  */
135   bool in_pragma;
136 } cp_lexer;
137
138 /* cp_token_cache is a range of tokens.  There is no need to represent
139    allocate heap memory for it, since tokens are never removed from the
140    lexer's array.  There is also no need for the GC to walk through
141    a cp_token_cache, since everything in here is referenced through
142    a lexer.  */
143
144 typedef struct GTY(()) cp_token_cache {
145   /* The beginning of the token range.  */
146   cp_token * GTY((skip)) first;
147
148   /* Points immediately after the last token in the range.  */
149   cp_token * GTY ((skip)) last;
150 } cp_token_cache;
151
152 /* Prototypes.  */
153
154 static cp_lexer *cp_lexer_new_main
155   (void);
156 static cp_lexer *cp_lexer_new_from_tokens
157   (cp_token_cache *tokens);
158 static void cp_lexer_destroy
159   (cp_lexer *);
160 static int cp_lexer_saving_tokens
161   (const cp_lexer *);
162 static cp_token_position cp_lexer_token_position
163   (cp_lexer *, bool);
164 static cp_token *cp_lexer_token_at
165   (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167   (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
169   (cp_lexer *);
170 static cp_token *cp_lexer_peek_nth_token
171   (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173   (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175   (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177   (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
179   (cp_lexer *);
180 static void cp_lexer_purge_token
181   (cp_lexer *);
182 static void cp_lexer_purge_tokens_after
183   (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
185   (cp_lexer *);
186 static void cp_lexer_commit_tokens
187   (cp_lexer *);
188 static void cp_lexer_rollback_tokens
189   (cp_lexer *);
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192   (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
194   (cp_lexer *);
195 static void cp_lexer_start_debugging
196   (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198   (cp_lexer *) ATTRIBUTE_UNUSED;
199 #else
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201    about passing NULL to functions that require non-NULL arguments
202    (fputs, fprintf).  It will never be used, so all we need is a value
203    of the right type that's guaranteed not to be NULL.  */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
208
209 static cp_token_cache *cp_token_cache_new
210   (cp_token *, cp_token *);
211
212 static void cp_parser_initial_pragma
213   (cp_token *);
214
215 /* Manifest constants.  */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
218
219 /* A token type for keywords, as opposed to ordinary identifiers.  */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
221
222 /* A token type for template-ids.  If a template-id is processed while
223    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224    the value of the CPP_TEMPLATE_ID is whatever was returned by
225    cp_parser_template_id.  */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
227
228 /* A token type for nested-name-specifiers.  If a
229    nested-name-specifier is processed while parsing tentatively, it is
230    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232    cp_parser_nested_name_specifier_opt.  */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
234
235 /* A token type for tokens that are not tokens at all; these are used
236    to represent slots in the array where there used to be a token
237    that has now been deleted.  */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
239
240 /* The number of token types, including C++-specific ones.  */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
242
243 /* Variables.  */
244
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written.  */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
249
250 /* Create a new main C++ lexer, the lexer that gets tokens from the
251    preprocessor.  */
252
253 static cp_lexer *
254 cp_lexer_new_main (void)
255 {
256   cp_token first_token;
257   cp_lexer *lexer;
258   cp_token *pos;
259   size_t alloc;
260   size_t space;
261   cp_token *buffer;
262
263   /* It's possible that parsing the first pragma will load a PCH file,
264      which is a GC collection point.  So we have to do that before
265      allocating any memory.  */
266   cp_parser_initial_pragma (&first_token);
267
268   c_common_no_more_pch ();
269
270   /* Allocate the memory.  */
271   lexer = GGC_CNEW (cp_lexer);
272
273 #ifdef ENABLE_CHECKING
274   /* Initially we are not debugging.  */
275   lexer->debugging_p = false;
276 #endif /* ENABLE_CHECKING */
277   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
278                                    CP_SAVED_TOKEN_STACK);
279
280   /* Create the buffer.  */
281   alloc = CP_LEXER_BUFFER_SIZE;
282   buffer = GGC_NEWVEC (cp_token, alloc);
283
284   /* Put the first token in the buffer.  */
285   space = alloc;
286   pos = buffer;
287   *pos = first_token;
288
289   /* Get the remaining tokens from the preprocessor.  */
290   while (pos->type != CPP_EOF)
291     {
292       pos++;
293       if (!--space)
294         {
295           space = alloc;
296           alloc *= 2;
297           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
298           pos = buffer + space;
299         }
300       cp_lexer_get_preprocessor_token (lexer, pos);
301     }
302   lexer->buffer = buffer;
303   lexer->buffer_length = alloc - space;
304   lexer->last_token = pos;
305   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
306
307   /* Subsequent preprocessor diagnostics should use compiler
308      diagnostic functions to get the compiler source location.  */
309   done_lexing = true;
310
311   gcc_assert (lexer->next_token->type != CPP_PURGED);
312   return lexer;
313 }
314
315 /* Create a new lexer whose token stream is primed with the tokens in
316    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
317
318 static cp_lexer *
319 cp_lexer_new_from_tokens (cp_token_cache *cache)
320 {
321   cp_token *first = cache->first;
322   cp_token *last = cache->last;
323   cp_lexer *lexer = GGC_CNEW (cp_lexer);
324
325   /* We do not own the buffer.  */
326   lexer->buffer = NULL;
327   lexer->buffer_length = 0;
328   lexer->next_token = first == last ? &eof_token : first;
329   lexer->last_token = last;
330
331   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
332                                    CP_SAVED_TOKEN_STACK);
333
334 #ifdef ENABLE_CHECKING
335   /* Initially we are not debugging.  */
336   lexer->debugging_p = false;
337 #endif
338
339   gcc_assert (lexer->next_token->type != CPP_PURGED);
340   return lexer;
341 }
342
343 /* Frees all resources associated with LEXER.  */
344
345 static void
346 cp_lexer_destroy (cp_lexer *lexer)
347 {
348   if (lexer->buffer)
349     ggc_free (lexer->buffer);
350   VEC_free (cp_token_position, heap, lexer->saved_tokens);
351   ggc_free (lexer);
352 }
353
354 /* Returns nonzero if debugging information should be output.  */
355
356 #ifdef ENABLE_CHECKING
357
358 static inline bool
359 cp_lexer_debugging_p (cp_lexer *lexer)
360 {
361   return lexer->debugging_p;
362 }
363
364 #endif /* ENABLE_CHECKING */
365
366 static inline cp_token_position
367 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
368 {
369   gcc_assert (!previous_p || lexer->next_token != &eof_token);
370
371   return lexer->next_token - previous_p;
372 }
373
374 static inline cp_token *
375 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
376 {
377   return pos;
378 }
379
380 /* nonzero if we are presently saving tokens.  */
381
382 static inline int
383 cp_lexer_saving_tokens (const cp_lexer* lexer)
384 {
385   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
386 }
387
388 /* Store the next token from the preprocessor in *TOKEN.  Return true
389    if we reach EOF.  If LEXER is NULL, assume we are handling an
390    initial #pragma pch_preprocess, and thus want the lexer to return
391    processed strings.  */
392
393 static void
394 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
395 {
396   static int is_extern_c = 0;
397
398    /* Get a new token from the preprocessor.  */
399   token->type
400     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
401                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
402   token->keyword = RID_MAX;
403   token->pragma_kind = PRAGMA_NONE;
404
405   /* On some systems, some header files are surrounded by an
406      implicit extern "C" block.  Set a flag in the token if it
407      comes from such a header.  */
408   is_extern_c += pending_lang_change;
409   pending_lang_change = 0;
410   token->implicit_extern_c = is_extern_c > 0;
411
412   /* Check to see if this token is a keyword.  */
413   if (token->type == CPP_NAME)
414     {
415       if (C_IS_RESERVED_WORD (token->u.value))
416         {
417           /* Mark this token as a keyword.  */
418           token->type = CPP_KEYWORD;
419           /* Record which keyword.  */
420           token->keyword = C_RID_CODE (token->u.value);
421         }
422       else
423         {
424           if (warn_cxx0x_compat
425               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
426               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
427             {
428               /* Warn about the C++0x keyword (but still treat it as
429                  an identifier).  */
430               warning (OPT_Wc__0x_compat, 
431                        "identifier %qE will become a keyword in C++0x",
432                        token->u.value);
433
434               /* Clear out the C_RID_CODE so we don't warn about this
435                  particular identifier-turned-keyword again.  */
436               C_SET_RID_CODE (token->u.value, RID_MAX);
437             }
438
439           token->ambiguous_p = false;
440           token->keyword = RID_MAX;
441         }
442     }
443   /* Handle Objective-C++ keywords.  */
444   else if (token->type == CPP_AT_NAME)
445     {
446       token->type = CPP_KEYWORD;
447       switch (C_RID_CODE (token->u.value))
448         {
449         /* Map 'class' to '@class', 'private' to '@private', etc.  */
450         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
451         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
452         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
453         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
454         case RID_THROW: token->keyword = RID_AT_THROW; break;
455         case RID_TRY: token->keyword = RID_AT_TRY; break;
456         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
457         default: token->keyword = C_RID_CODE (token->u.value);
458         }
459     }
460   else if (token->type == CPP_PRAGMA)
461     {
462       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
463       token->pragma_kind = ((enum pragma_kind)
464                             TREE_INT_CST_LOW (token->u.value));
465       token->u.value = NULL_TREE;
466     }
467 }
468
469 /* Update the globals input_location and the input file stack from TOKEN.  */
470 static inline void
471 cp_lexer_set_source_position_from_token (cp_token *token)
472 {
473   if (token->type != CPP_EOF)
474     {
475       input_location = token->location;
476     }
477 }
478
479 /* Return a pointer to the next token in the token stream, but do not
480    consume it.  */
481
482 static inline cp_token *
483 cp_lexer_peek_token (cp_lexer *lexer)
484 {
485   if (cp_lexer_debugging_p (lexer))
486     {
487       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
488       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
489       putc ('\n', cp_lexer_debug_stream);
490     }
491   return lexer->next_token;
492 }
493
494 /* Return true if the next token has the indicated TYPE.  */
495
496 static inline bool
497 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
498 {
499   return cp_lexer_peek_token (lexer)->type == type;
500 }
501
502 /* Return true if the next token does not have the indicated TYPE.  */
503
504 static inline bool
505 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
506 {
507   return !cp_lexer_next_token_is (lexer, type);
508 }
509
510 /* Return true if the next token is the indicated KEYWORD.  */
511
512 static inline bool
513 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
514 {
515   return cp_lexer_peek_token (lexer)->keyword == keyword;
516 }
517
518 /* Return true if the next token is not the indicated KEYWORD.  */
519
520 static inline bool
521 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
522 {
523   return cp_lexer_peek_token (lexer)->keyword != keyword;
524 }
525
526 /* Return true if the next token is a keyword for a decl-specifier.  */
527
528 static bool
529 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
530 {
531   cp_token *token;
532
533   token = cp_lexer_peek_token (lexer);
534   switch (token->keyword) 
535     {
536       /* auto specifier: storage-class-specifier in C++,
537          simple-type-specifier in C++0x.  */
538     case RID_AUTO:
539       /* Storage classes.  */
540     case RID_REGISTER:
541     case RID_STATIC:
542     case RID_EXTERN:
543     case RID_MUTABLE:
544     case RID_THREAD:
545       /* Elaborated type specifiers.  */
546     case RID_ENUM:
547     case RID_CLASS:
548     case RID_STRUCT:
549     case RID_UNION:
550     case RID_TYPENAME:
551       /* Simple type specifiers.  */
552     case RID_CHAR:
553     case RID_CHAR16:
554     case RID_CHAR32:
555     case RID_WCHAR:
556     case RID_BOOL:
557     case RID_SHORT:
558     case RID_INT:
559     case RID_LONG:
560     case RID_SIGNED:
561     case RID_UNSIGNED:
562     case RID_FLOAT:
563     case RID_DOUBLE:
564     case RID_VOID:
565       /* GNU extensions.  */ 
566     case RID_ATTRIBUTE:
567     case RID_TYPEOF:
568       /* C++0x extensions.  */
569     case RID_DECLTYPE:
570       return true;
571
572     default:
573       return false;
574     }
575 }
576
577 /* Return a pointer to the Nth token in the token stream.  If N is 1,
578    then this is precisely equivalent to cp_lexer_peek_token (except
579    that it is not inline).  One would like to disallow that case, but
580    there is one case (cp_parser_nth_token_starts_template_id) where
581    the caller passes a variable for N and it might be 1.  */
582
583 static cp_token *
584 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
585 {
586   cp_token *token;
587
588   /* N is 1-based, not zero-based.  */
589   gcc_assert (n > 0);
590
591   if (cp_lexer_debugging_p (lexer))
592     fprintf (cp_lexer_debug_stream,
593              "cp_lexer: peeking ahead %ld at token: ", (long)n);
594
595   --n;
596   token = lexer->next_token;
597   gcc_assert (!n || token != &eof_token);
598   while (n != 0)
599     {
600       ++token;
601       if (token == lexer->last_token)
602         {
603           token = &eof_token;
604           break;
605         }
606
607       if (token->type != CPP_PURGED)
608         --n;
609     }
610
611   if (cp_lexer_debugging_p (lexer))
612     {
613       cp_lexer_print_token (cp_lexer_debug_stream, token);
614       putc ('\n', cp_lexer_debug_stream);
615     }
616
617   return token;
618 }
619
620 /* Return the next token, and advance the lexer's next_token pointer
621    to point to the next non-purged token.  */
622
623 static cp_token *
624 cp_lexer_consume_token (cp_lexer* lexer)
625 {
626   cp_token *token = lexer->next_token;
627
628   gcc_assert (token != &eof_token);
629   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
630
631   do
632     {
633       lexer->next_token++;
634       if (lexer->next_token == lexer->last_token)
635         {
636           lexer->next_token = &eof_token;
637           break;
638         }
639
640     }
641   while (lexer->next_token->type == CPP_PURGED);
642
643   cp_lexer_set_source_position_from_token (token);
644
645   /* Provide debugging output.  */
646   if (cp_lexer_debugging_p (lexer))
647     {
648       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
649       cp_lexer_print_token (cp_lexer_debug_stream, token);
650       putc ('\n', cp_lexer_debug_stream);
651     }
652
653   return token;
654 }
655
656 /* Permanently remove the next token from the token stream, and
657    advance the next_token pointer to refer to the next non-purged
658    token.  */
659
660 static void
661 cp_lexer_purge_token (cp_lexer *lexer)
662 {
663   cp_token *tok = lexer->next_token;
664
665   gcc_assert (tok != &eof_token);
666   tok->type = CPP_PURGED;
667   tok->location = UNKNOWN_LOCATION;
668   tok->u.value = NULL_TREE;
669   tok->keyword = RID_MAX;
670
671   do
672     {
673       tok++;
674       if (tok == lexer->last_token)
675         {
676           tok = &eof_token;
677           break;
678         }
679     }
680   while (tok->type == CPP_PURGED);
681   lexer->next_token = tok;
682 }
683
684 /* Permanently remove all tokens after TOK, up to, but not
685    including, the token that will be returned next by
686    cp_lexer_peek_token.  */
687
688 static void
689 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
690 {
691   cp_token *peek = lexer->next_token;
692
693   if (peek == &eof_token)
694     peek = lexer->last_token;
695
696   gcc_assert (tok < peek);
697
698   for ( tok += 1; tok != peek; tok += 1)
699     {
700       tok->type = CPP_PURGED;
701       tok->location = UNKNOWN_LOCATION;
702       tok->u.value = NULL_TREE;
703       tok->keyword = RID_MAX;
704     }
705 }
706
707 /* Begin saving tokens.  All tokens consumed after this point will be
708    preserved.  */
709
710 static void
711 cp_lexer_save_tokens (cp_lexer* lexer)
712 {
713   /* Provide debugging output.  */
714   if (cp_lexer_debugging_p (lexer))
715     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
716
717   VEC_safe_push (cp_token_position, heap,
718                  lexer->saved_tokens, lexer->next_token);
719 }
720
721 /* Commit to the portion of the token stream most recently saved.  */
722
723 static void
724 cp_lexer_commit_tokens (cp_lexer* lexer)
725 {
726   /* Provide debugging output.  */
727   if (cp_lexer_debugging_p (lexer))
728     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
729
730   VEC_pop (cp_token_position, lexer->saved_tokens);
731 }
732
733 /* Return all tokens saved since the last call to cp_lexer_save_tokens
734    to the token stream.  Stop saving tokens.  */
735
736 static void
737 cp_lexer_rollback_tokens (cp_lexer* lexer)
738 {
739   /* Provide debugging output.  */
740   if (cp_lexer_debugging_p (lexer))
741     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
742
743   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
744 }
745
746 /* Print a representation of the TOKEN on the STREAM.  */
747
748 #ifdef ENABLE_CHECKING
749
750 static void
751 cp_lexer_print_token (FILE * stream, cp_token *token)
752 {
753   /* We don't use cpp_type2name here because the parser defines
754      a few tokens of its own.  */
755   static const char *const token_names[] = {
756     /* cpplib-defined token types */
757 #define OP(e, s) #e,
758 #define TK(e, s) #e,
759     TTYPE_TABLE
760 #undef OP
761 #undef TK
762     /* C++ parser token types - see "Manifest constants", above.  */
763     "KEYWORD",
764     "TEMPLATE_ID",
765     "NESTED_NAME_SPECIFIER",
766     "PURGED"
767   };
768
769   /* If we have a name for the token, print it out.  Otherwise, we
770      simply give the numeric code.  */
771   gcc_assert (token->type < ARRAY_SIZE(token_names));
772   fputs (token_names[token->type], stream);
773
774   /* For some tokens, print the associated data.  */
775   switch (token->type)
776     {
777     case CPP_KEYWORD:
778       /* Some keywords have a value that is not an IDENTIFIER_NODE.
779          For example, `struct' is mapped to an INTEGER_CST.  */
780       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
781         break;
782       /* else fall through */
783     case CPP_NAME:
784       fputs (IDENTIFIER_POINTER (token->u.value), stream);
785       break;
786
787     case CPP_STRING:
788     case CPP_STRING16:
789     case CPP_STRING32:
790     case CPP_WSTRING:
791       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
792       break;
793
794     default:
795       break;
796     }
797 }
798
799 /* Start emitting debugging information.  */
800
801 static void
802 cp_lexer_start_debugging (cp_lexer* lexer)
803 {
804   lexer->debugging_p = true;
805 }
806
807 /* Stop emitting debugging information.  */
808
809 static void
810 cp_lexer_stop_debugging (cp_lexer* lexer)
811 {
812   lexer->debugging_p = false;
813 }
814
815 #endif /* ENABLE_CHECKING */
816
817 /* Create a new cp_token_cache, representing a range of tokens.  */
818
819 static cp_token_cache *
820 cp_token_cache_new (cp_token *first, cp_token *last)
821 {
822   cp_token_cache *cache = GGC_NEW (cp_token_cache);
823   cache->first = first;
824   cache->last = last;
825   return cache;
826 }
827
828 \f
829 /* Decl-specifiers.  */
830
831 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
832
833 static void
834 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
835 {
836   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
837 }
838
839 /* Declarators.  */
840
841 /* Nothing other than the parser should be creating declarators;
842    declarators are a semi-syntactic representation of C++ entities.
843    Other parts of the front end that need to create entities (like
844    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
845
846 static cp_declarator *make_call_declarator
847   (cp_declarator *, tree, cp_cv_quals, tree, tree);
848 static cp_declarator *make_array_declarator
849   (cp_declarator *, tree);
850 static cp_declarator *make_pointer_declarator
851   (cp_cv_quals, cp_declarator *);
852 static cp_declarator *make_reference_declarator
853   (cp_cv_quals, cp_declarator *, bool);
854 static cp_parameter_declarator *make_parameter_declarator
855   (cp_decl_specifier_seq *, cp_declarator *, tree);
856 static cp_declarator *make_ptrmem_declarator
857   (cp_cv_quals, tree, cp_declarator *);
858
859 /* An erroneous declarator.  */
860 static cp_declarator *cp_error_declarator;
861
862 /* The obstack on which declarators and related data structures are
863    allocated.  */
864 static struct obstack declarator_obstack;
865
866 /* Alloc BYTES from the declarator memory pool.  */
867
868 static inline void *
869 alloc_declarator (size_t bytes)
870 {
871   return obstack_alloc (&declarator_obstack, bytes);
872 }
873
874 /* Allocate a declarator of the indicated KIND.  Clear fields that are
875    common to all declarators.  */
876
877 static cp_declarator *
878 make_declarator (cp_declarator_kind kind)
879 {
880   cp_declarator *declarator;
881
882   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
883   declarator->kind = kind;
884   declarator->attributes = NULL_TREE;
885   declarator->declarator = NULL;
886   declarator->parameter_pack_p = false;
887
888   return declarator;
889 }
890
891 /* Make a declarator for a generalized identifier.  If
892    QUALIFYING_SCOPE is non-NULL, the identifier is
893    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
894    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
895    is, if any.   */
896
897 static cp_declarator *
898 make_id_declarator (tree qualifying_scope, tree unqualified_name,
899                     special_function_kind sfk)
900 {
901   cp_declarator *declarator;
902
903   /* It is valid to write:
904
905        class C { void f(); };
906        typedef C D;
907        void D::f();
908
909      The standard is not clear about whether `typedef const C D' is
910      legal; as of 2002-09-15 the committee is considering that
911      question.  EDG 3.0 allows that syntax.  Therefore, we do as
912      well.  */
913   if (qualifying_scope && TYPE_P (qualifying_scope))
914     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
915
916   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
917               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
918               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
919
920   declarator = make_declarator (cdk_id);
921   declarator->u.id.qualifying_scope = qualifying_scope;
922   declarator->u.id.unqualified_name = unqualified_name;
923   declarator->u.id.sfk = sfk;
924   
925   return declarator;
926 }
927
928 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
929    of modifiers such as const or volatile to apply to the pointer
930    type, represented as identifiers.  */
931
932 cp_declarator *
933 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
934 {
935   cp_declarator *declarator;
936
937   declarator = make_declarator (cdk_pointer);
938   declarator->declarator = target;
939   declarator->u.pointer.qualifiers = cv_qualifiers;
940   declarator->u.pointer.class_type = NULL_TREE;
941   if (target)
942     {
943       declarator->parameter_pack_p = target->parameter_pack_p;
944       target->parameter_pack_p = false;
945     }
946   else
947     declarator->parameter_pack_p = false;
948
949   return declarator;
950 }
951
952 /* Like make_pointer_declarator -- but for references.  */
953
954 cp_declarator *
955 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
956                            bool rvalue_ref)
957 {
958   cp_declarator *declarator;
959
960   declarator = make_declarator (cdk_reference);
961   declarator->declarator = target;
962   declarator->u.reference.qualifiers = cv_qualifiers;
963   declarator->u.reference.rvalue_ref = rvalue_ref;
964   if (target)
965     {
966       declarator->parameter_pack_p = target->parameter_pack_p;
967       target->parameter_pack_p = false;
968     }
969   else
970     declarator->parameter_pack_p = false;
971
972   return declarator;
973 }
974
975 /* Like make_pointer_declarator -- but for a pointer to a non-static
976    member of CLASS_TYPE.  */
977
978 cp_declarator *
979 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
980                         cp_declarator *pointee)
981 {
982   cp_declarator *declarator;
983
984   declarator = make_declarator (cdk_ptrmem);
985   declarator->declarator = pointee;
986   declarator->u.pointer.qualifiers = cv_qualifiers;
987   declarator->u.pointer.class_type = class_type;
988
989   if (pointee)
990     {
991       declarator->parameter_pack_p = pointee->parameter_pack_p;
992       pointee->parameter_pack_p = false;
993     }
994   else
995     declarator->parameter_pack_p = false;
996
997   return declarator;
998 }
999
1000 /* Make a declarator for the function given by TARGET, with the
1001    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1002    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1003    indicates what exceptions can be thrown.  */
1004
1005 cp_declarator *
1006 make_call_declarator (cp_declarator *target,
1007                       tree parms,
1008                       cp_cv_quals cv_qualifiers,
1009                       tree exception_specification,
1010                       tree late_return_type)
1011 {
1012   cp_declarator *declarator;
1013
1014   declarator = make_declarator (cdk_function);
1015   declarator->declarator = target;
1016   declarator->u.function.parameters = parms;
1017   declarator->u.function.qualifiers = cv_qualifiers;
1018   declarator->u.function.exception_specification = exception_specification;
1019   declarator->u.function.late_return_type = late_return_type;
1020   if (target)
1021     {
1022       declarator->parameter_pack_p = target->parameter_pack_p;
1023       target->parameter_pack_p = false;
1024     }
1025   else
1026     declarator->parameter_pack_p = false;
1027
1028   return declarator;
1029 }
1030
1031 /* Make a declarator for an array of BOUNDS elements, each of which is
1032    defined by ELEMENT.  */
1033
1034 cp_declarator *
1035 make_array_declarator (cp_declarator *element, tree bounds)
1036 {
1037   cp_declarator *declarator;
1038
1039   declarator = make_declarator (cdk_array);
1040   declarator->declarator = element;
1041   declarator->u.array.bounds = bounds;
1042   if (element)
1043     {
1044       declarator->parameter_pack_p = element->parameter_pack_p;
1045       element->parameter_pack_p = false;
1046     }
1047   else
1048     declarator->parameter_pack_p = false;
1049
1050   return declarator;
1051 }
1052
1053 /* Determine whether the declarator we've seen so far can be a
1054    parameter pack, when followed by an ellipsis.  */
1055 static bool 
1056 declarator_can_be_parameter_pack (cp_declarator *declarator)
1057 {
1058   /* Search for a declarator name, or any other declarator that goes
1059      after the point where the ellipsis could appear in a parameter
1060      pack. If we find any of these, then this declarator can not be
1061      made into a parameter pack.  */
1062   bool found = false;
1063   while (declarator && !found)
1064     {
1065       switch ((int)declarator->kind)
1066         {
1067         case cdk_id:
1068         case cdk_array:
1069           found = true;
1070           break;
1071
1072         case cdk_error:
1073           return true;
1074
1075         default:
1076           declarator = declarator->declarator;
1077           break;
1078         }
1079     }
1080
1081   return !found;
1082 }
1083
1084 cp_parameter_declarator *no_parameters;
1085
1086 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1087    DECLARATOR and DEFAULT_ARGUMENT.  */
1088
1089 cp_parameter_declarator *
1090 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1091                            cp_declarator *declarator,
1092                            tree default_argument)
1093 {
1094   cp_parameter_declarator *parameter;
1095
1096   parameter = ((cp_parameter_declarator *)
1097                alloc_declarator (sizeof (cp_parameter_declarator)));
1098   parameter->next = NULL;
1099   if (decl_specifiers)
1100     parameter->decl_specifiers = *decl_specifiers;
1101   else
1102     clear_decl_specs (&parameter->decl_specifiers);
1103   parameter->declarator = declarator;
1104   parameter->default_argument = default_argument;
1105   parameter->ellipsis_p = false;
1106
1107   return parameter;
1108 }
1109
1110 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1111
1112 static bool
1113 function_declarator_p (const cp_declarator *declarator)
1114 {
1115   while (declarator)
1116     {
1117       if (declarator->kind == cdk_function
1118           && declarator->declarator->kind == cdk_id)
1119         return true;
1120       if (declarator->kind == cdk_id
1121           || declarator->kind == cdk_error)
1122         return false;
1123       declarator = declarator->declarator;
1124     }
1125   return false;
1126 }
1127  
1128 /* The parser.  */
1129
1130 /* Overview
1131    --------
1132
1133    A cp_parser parses the token stream as specified by the C++
1134    grammar.  Its job is purely parsing, not semantic analysis.  For
1135    example, the parser breaks the token stream into declarators,
1136    expressions, statements, and other similar syntactic constructs.
1137    It does not check that the types of the expressions on either side
1138    of an assignment-statement are compatible, or that a function is
1139    not declared with a parameter of type `void'.
1140
1141    The parser invokes routines elsewhere in the compiler to perform
1142    semantic analysis and to build up the abstract syntax tree for the
1143    code processed.
1144
1145    The parser (and the template instantiation code, which is, in a
1146    way, a close relative of parsing) are the only parts of the
1147    compiler that should be calling push_scope and pop_scope, or
1148    related functions.  The parser (and template instantiation code)
1149    keeps track of what scope is presently active; everything else
1150    should simply honor that.  (The code that generates static
1151    initializers may also need to set the scope, in order to check
1152    access control correctly when emitting the initializers.)
1153
1154    Methodology
1155    -----------
1156
1157    The parser is of the standard recursive-descent variety.  Upcoming
1158    tokens in the token stream are examined in order to determine which
1159    production to use when parsing a non-terminal.  Some C++ constructs
1160    require arbitrary look ahead to disambiguate.  For example, it is
1161    impossible, in the general case, to tell whether a statement is an
1162    expression or declaration without scanning the entire statement.
1163    Therefore, the parser is capable of "parsing tentatively."  When the
1164    parser is not sure what construct comes next, it enters this mode.
1165    Then, while we attempt to parse the construct, the parser queues up
1166    error messages, rather than issuing them immediately, and saves the
1167    tokens it consumes.  If the construct is parsed successfully, the
1168    parser "commits", i.e., it issues any queued error messages and
1169    the tokens that were being preserved are permanently discarded.
1170    If, however, the construct is not parsed successfully, the parser
1171    rolls back its state completely so that it can resume parsing using
1172    a different alternative.
1173
1174    Future Improvements
1175    -------------------
1176
1177    The performance of the parser could probably be improved substantially.
1178    We could often eliminate the need to parse tentatively by looking ahead
1179    a little bit.  In some places, this approach might not entirely eliminate
1180    the need to parse tentatively, but it might still speed up the average
1181    case.  */
1182
1183 /* Flags that are passed to some parsing functions.  These values can
1184    be bitwise-ored together.  */
1185
1186 enum
1187 {
1188   /* No flags.  */
1189   CP_PARSER_FLAGS_NONE = 0x0,
1190   /* The construct is optional.  If it is not present, then no error
1191      should be issued.  */
1192   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1193   /* When parsing a type-specifier, do not allow user-defined types.  */
1194   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1195 };
1196
1197 /* This type is used for parameters and variables which hold
1198    combinations of the above flags.  */
1199 typedef int cp_parser_flags;
1200
1201 /* The different kinds of declarators we want to parse.  */
1202
1203 typedef enum cp_parser_declarator_kind
1204 {
1205   /* We want an abstract declarator.  */
1206   CP_PARSER_DECLARATOR_ABSTRACT,
1207   /* We want a named declarator.  */
1208   CP_PARSER_DECLARATOR_NAMED,
1209   /* We don't mind, but the name must be an unqualified-id.  */
1210   CP_PARSER_DECLARATOR_EITHER
1211 } cp_parser_declarator_kind;
1212
1213 /* The precedence values used to parse binary expressions.  The minimum value
1214    of PREC must be 1, because zero is reserved to quickly discriminate
1215    binary operators from other tokens.  */
1216
1217 enum cp_parser_prec
1218 {
1219   PREC_NOT_OPERATOR,
1220   PREC_LOGICAL_OR_EXPRESSION,
1221   PREC_LOGICAL_AND_EXPRESSION,
1222   PREC_INCLUSIVE_OR_EXPRESSION,
1223   PREC_EXCLUSIVE_OR_EXPRESSION,
1224   PREC_AND_EXPRESSION,
1225   PREC_EQUALITY_EXPRESSION,
1226   PREC_RELATIONAL_EXPRESSION,
1227   PREC_SHIFT_EXPRESSION,
1228   PREC_ADDITIVE_EXPRESSION,
1229   PREC_MULTIPLICATIVE_EXPRESSION,
1230   PREC_PM_EXPRESSION,
1231   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1232 };
1233
1234 /* A mapping from a token type to a corresponding tree node type, with a
1235    precedence value.  */
1236
1237 typedef struct cp_parser_binary_operations_map_node
1238 {
1239   /* The token type.  */
1240   enum cpp_ttype token_type;
1241   /* The corresponding tree code.  */
1242   enum tree_code tree_type;
1243   /* The precedence of this operator.  */
1244   enum cp_parser_prec prec;
1245 } cp_parser_binary_operations_map_node;
1246
1247 /* The status of a tentative parse.  */
1248
1249 typedef enum cp_parser_status_kind
1250 {
1251   /* No errors have occurred.  */
1252   CP_PARSER_STATUS_KIND_NO_ERROR,
1253   /* An error has occurred.  */
1254   CP_PARSER_STATUS_KIND_ERROR,
1255   /* We are committed to this tentative parse, whether or not an error
1256      has occurred.  */
1257   CP_PARSER_STATUS_KIND_COMMITTED
1258 } cp_parser_status_kind;
1259
1260 typedef struct cp_parser_expression_stack_entry
1261 {
1262   /* Left hand side of the binary operation we are currently
1263      parsing.  */
1264   tree lhs;
1265   /* Original tree code for left hand side, if it was a binary
1266      expression itself (used for -Wparentheses).  */
1267   enum tree_code lhs_type;
1268   /* Tree code for the binary operation we are parsing.  */
1269   enum tree_code tree_type;
1270   /* Precedence of the binary operation we are parsing.  */
1271   enum cp_parser_prec prec;
1272 } cp_parser_expression_stack_entry;
1273
1274 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1275    entries because precedence levels on the stack are monotonically
1276    increasing.  */
1277 typedef struct cp_parser_expression_stack_entry
1278   cp_parser_expression_stack[NUM_PREC_VALUES];
1279
1280 /* Context that is saved and restored when parsing tentatively.  */
1281 typedef struct GTY (()) cp_parser_context {
1282   /* If this is a tentative parsing context, the status of the
1283      tentative parse.  */
1284   enum cp_parser_status_kind status;
1285   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1286      that are looked up in this context must be looked up both in the
1287      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1288      the context of the containing expression.  */
1289   tree object_type;
1290
1291   /* The next parsing context in the stack.  */
1292   struct cp_parser_context *next;
1293 } cp_parser_context;
1294
1295 /* Prototypes.  */
1296
1297 /* Constructors and destructors.  */
1298
1299 static cp_parser_context *cp_parser_context_new
1300   (cp_parser_context *);
1301
1302 /* Class variables.  */
1303
1304 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1305
1306 /* The operator-precedence table used by cp_parser_binary_expression.
1307    Transformed into an associative array (binops_by_token) by
1308    cp_parser_new.  */
1309
1310 static const cp_parser_binary_operations_map_node binops[] = {
1311   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1312   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1313
1314   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1315   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1316   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317
1318   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1319   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1320
1321   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1322   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1323
1324   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1325   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1326   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1327   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1328
1329   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1330   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1331
1332   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1333
1334   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1335
1336   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1337
1338   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1339
1340   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1341 };
1342
1343 /* The same as binops, but initialized by cp_parser_new so that
1344    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1345    for speed.  */
1346 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1347
1348 /* Constructors and destructors.  */
1349
1350 /* Construct a new context.  The context below this one on the stack
1351    is given by NEXT.  */
1352
1353 static cp_parser_context *
1354 cp_parser_context_new (cp_parser_context* next)
1355 {
1356   cp_parser_context *context;
1357
1358   /* Allocate the storage.  */
1359   if (cp_parser_context_free_list != NULL)
1360     {
1361       /* Pull the first entry from the free list.  */
1362       context = cp_parser_context_free_list;
1363       cp_parser_context_free_list = context->next;
1364       memset (context, 0, sizeof (*context));
1365     }
1366   else
1367     context = GGC_CNEW (cp_parser_context);
1368
1369   /* No errors have occurred yet in this context.  */
1370   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1371   /* If this is not the bottommost context, copy information that we
1372      need from the previous context.  */
1373   if (next)
1374     {
1375       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1376          expression, then we are parsing one in this context, too.  */
1377       context->object_type = next->object_type;
1378       /* Thread the stack.  */
1379       context->next = next;
1380     }
1381
1382   return context;
1383 }
1384
1385 /* The cp_parser structure represents the C++ parser.  */
1386
1387 typedef struct GTY(()) cp_parser {
1388   /* The lexer from which we are obtaining tokens.  */
1389   cp_lexer *lexer;
1390
1391   /* The scope in which names should be looked up.  If NULL_TREE, then
1392      we look up names in the scope that is currently open in the
1393      source program.  If non-NULL, this is either a TYPE or
1394      NAMESPACE_DECL for the scope in which we should look.  It can
1395      also be ERROR_MARK, when we've parsed a bogus scope.
1396
1397      This value is not cleared automatically after a name is looked
1398      up, so we must be careful to clear it before starting a new look
1399      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1400      will look up `Z' in the scope of `X', rather than the current
1401      scope.)  Unfortunately, it is difficult to tell when name lookup
1402      is complete, because we sometimes peek at a token, look it up,
1403      and then decide not to consume it.   */
1404   tree scope;
1405
1406   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1407      last lookup took place.  OBJECT_SCOPE is used if an expression
1408      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1409      respectively.  QUALIFYING_SCOPE is used for an expression of the
1410      form "X::Y"; it refers to X.  */
1411   tree object_scope;
1412   tree qualifying_scope;
1413
1414   /* A stack of parsing contexts.  All but the bottom entry on the
1415      stack will be tentative contexts.
1416
1417      We parse tentatively in order to determine which construct is in
1418      use in some situations.  For example, in order to determine
1419      whether a statement is an expression-statement or a
1420      declaration-statement we parse it tentatively as a
1421      declaration-statement.  If that fails, we then reparse the same
1422      token stream as an expression-statement.  */
1423   cp_parser_context *context;
1424
1425   /* True if we are parsing GNU C++.  If this flag is not set, then
1426      GNU extensions are not recognized.  */
1427   bool allow_gnu_extensions_p;
1428
1429   /* TRUE if the `>' token should be interpreted as the greater-than
1430      operator.  FALSE if it is the end of a template-id or
1431      template-parameter-list. In C++0x mode, this flag also applies to
1432      `>>' tokens, which are viewed as two consecutive `>' tokens when
1433      this flag is FALSE.  */
1434   bool greater_than_is_operator_p;
1435
1436   /* TRUE if default arguments are allowed within a parameter list
1437      that starts at this point. FALSE if only a gnu extension makes
1438      them permissible.  */
1439   bool default_arg_ok_p;
1440
1441   /* TRUE if we are parsing an integral constant-expression.  See
1442      [expr.const] for a precise definition.  */
1443   bool integral_constant_expression_p;
1444
1445   /* TRUE if we are parsing an integral constant-expression -- but a
1446      non-constant expression should be permitted as well.  This flag
1447      is used when parsing an array bound so that GNU variable-length
1448      arrays are tolerated.  */
1449   bool allow_non_integral_constant_expression_p;
1450
1451   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1452      been seen that makes the expression non-constant.  */
1453   bool non_integral_constant_expression_p;
1454
1455   /* TRUE if local variable names and `this' are forbidden in the
1456      current context.  */
1457   bool local_variables_forbidden_p;
1458
1459   /* TRUE if the declaration we are parsing is part of a
1460      linkage-specification of the form `extern string-literal
1461      declaration'.  */
1462   bool in_unbraced_linkage_specification_p;
1463
1464   /* TRUE if we are presently parsing a declarator, after the
1465      direct-declarator.  */
1466   bool in_declarator_p;
1467
1468   /* TRUE if we are presently parsing a template-argument-list.  */
1469   bool in_template_argument_list_p;
1470
1471   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1472      to IN_OMP_BLOCK if parsing OpenMP structured block and
1473      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1474      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1475      iteration-statement, OpenMP block or loop within that switch.  */
1476 #define IN_SWITCH_STMT          1
1477 #define IN_ITERATION_STMT       2
1478 #define IN_OMP_BLOCK            4
1479 #define IN_OMP_FOR              8
1480 #define IN_IF_STMT             16
1481   unsigned char in_statement;
1482
1483   /* TRUE if we are presently parsing the body of a switch statement.
1484      Note that this doesn't quite overlap with in_statement above.
1485      The difference relates to giving the right sets of error messages:
1486      "case not in switch" vs "break statement used with OpenMP...".  */
1487   bool in_switch_statement_p;
1488
1489   /* TRUE if we are parsing a type-id in an expression context.  In
1490      such a situation, both "type (expr)" and "type (type)" are valid
1491      alternatives.  */
1492   bool in_type_id_in_expr_p;
1493
1494   /* TRUE if we are currently in a header file where declarations are
1495      implicitly extern "C".  */
1496   bool implicit_extern_c;
1497
1498   /* TRUE if strings in expressions should be translated to the execution
1499      character set.  */
1500   bool translate_strings_p;
1501
1502   /* TRUE if we are presently parsing the body of a function, but not
1503      a local class.  */
1504   bool in_function_body;
1505
1506   /* If non-NULL, then we are parsing a construct where new type
1507      definitions are not permitted.  The string stored here will be
1508      issued as an error message if a type is defined.  */
1509   const char *type_definition_forbidden_message;
1510
1511   /* A list of lists. The outer list is a stack, used for member
1512      functions of local classes. At each level there are two sub-list,
1513      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1514      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1515      TREE_VALUE's. The functions are chained in reverse declaration
1516      order.
1517
1518      The TREE_PURPOSE sublist contains those functions with default
1519      arguments that need post processing, and the TREE_VALUE sublist
1520      contains those functions with definitions that need post
1521      processing.
1522
1523      These lists can only be processed once the outermost class being
1524      defined is complete.  */
1525   tree unparsed_functions_queues;
1526
1527   /* The number of classes whose definitions are currently in
1528      progress.  */
1529   unsigned num_classes_being_defined;
1530
1531   /* The number of template parameter lists that apply directly to the
1532      current declaration.  */
1533   unsigned num_template_parameter_lists;
1534 } cp_parser;
1535
1536 /* Prototypes.  */
1537
1538 /* Constructors and destructors.  */
1539
1540 static cp_parser *cp_parser_new
1541   (void);
1542
1543 /* Routines to parse various constructs.
1544
1545    Those that return `tree' will return the error_mark_node (rather
1546    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1547    Sometimes, they will return an ordinary node if error-recovery was
1548    attempted, even though a parse error occurred.  So, to check
1549    whether or not a parse error occurred, you should always use
1550    cp_parser_error_occurred.  If the construct is optional (indicated
1551    either by an `_opt' in the name of the function that does the
1552    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1553    the construct is not present.  */
1554
1555 /* Lexical conventions [gram.lex]  */
1556
1557 static tree cp_parser_identifier
1558   (cp_parser *);
1559 static tree cp_parser_string_literal
1560   (cp_parser *, bool, bool);
1561
1562 /* Basic concepts [gram.basic]  */
1563
1564 static bool cp_parser_translation_unit
1565   (cp_parser *);
1566
1567 /* Expressions [gram.expr]  */
1568
1569 static tree cp_parser_primary_expression
1570   (cp_parser *, bool, bool, bool, cp_id_kind *);
1571 static tree cp_parser_id_expression
1572   (cp_parser *, bool, bool, bool *, bool, bool);
1573 static tree cp_parser_unqualified_id
1574   (cp_parser *, bool, bool, bool, bool);
1575 static tree cp_parser_nested_name_specifier_opt
1576   (cp_parser *, bool, bool, bool, bool);
1577 static tree cp_parser_nested_name_specifier
1578   (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_qualifying_entity
1580   (cp_parser *, bool, bool, bool, bool, bool);
1581 static tree cp_parser_postfix_expression
1582   (cp_parser *, bool, bool, bool, cp_id_kind *);
1583 static tree cp_parser_postfix_open_square_expression
1584   (cp_parser *, tree, bool);
1585 static tree cp_parser_postfix_dot_deref_expression
1586   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1587 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1588   (cp_parser *, bool, bool, bool, bool *);
1589 static void cp_parser_pseudo_destructor_name
1590   (cp_parser *, tree *, tree *);
1591 static tree cp_parser_unary_expression
1592   (cp_parser *, bool, bool, cp_id_kind *);
1593 static enum tree_code cp_parser_unary_operator
1594   (cp_token *);
1595 static tree cp_parser_new_expression
1596   (cp_parser *);
1597 static VEC(tree,gc) *cp_parser_new_placement
1598   (cp_parser *);
1599 static tree cp_parser_new_type_id
1600   (cp_parser *, tree *);
1601 static cp_declarator *cp_parser_new_declarator_opt
1602   (cp_parser *);
1603 static cp_declarator *cp_parser_direct_new_declarator
1604   (cp_parser *);
1605 static VEC(tree,gc) *cp_parser_new_initializer
1606   (cp_parser *);
1607 static tree cp_parser_delete_expression
1608   (cp_parser *);
1609 static tree cp_parser_cast_expression
1610   (cp_parser *, bool, bool, cp_id_kind *);
1611 static tree cp_parser_binary_expression
1612   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1613 static tree cp_parser_question_colon_clause
1614   (cp_parser *, tree);
1615 static tree cp_parser_assignment_expression
1616   (cp_parser *, bool, cp_id_kind *);
1617 static enum tree_code cp_parser_assignment_operator_opt
1618   (cp_parser *);
1619 static tree cp_parser_expression
1620   (cp_parser *, bool, cp_id_kind *);
1621 static tree cp_parser_constant_expression
1622   (cp_parser *, bool, bool *);
1623 static tree cp_parser_builtin_offsetof
1624   (cp_parser *);
1625
1626 /* Statements [gram.stmt.stmt]  */
1627
1628 static void cp_parser_statement
1629   (cp_parser *, tree, bool, bool *);
1630 static void cp_parser_label_for_labeled_statement
1631   (cp_parser *);
1632 static tree cp_parser_expression_statement
1633   (cp_parser *, tree);
1634 static tree cp_parser_compound_statement
1635   (cp_parser *, tree, bool);
1636 static void cp_parser_statement_seq_opt
1637   (cp_parser *, tree);
1638 static tree cp_parser_selection_statement
1639   (cp_parser *, bool *);
1640 static tree cp_parser_condition
1641   (cp_parser *);
1642 static tree cp_parser_iteration_statement
1643   (cp_parser *);
1644 static void cp_parser_for_init_statement
1645   (cp_parser *);
1646 static tree cp_parser_jump_statement
1647   (cp_parser *);
1648 static void cp_parser_declaration_statement
1649   (cp_parser *);
1650
1651 static tree cp_parser_implicitly_scoped_statement
1652   (cp_parser *, bool *);
1653 static void cp_parser_already_scoped_statement
1654   (cp_parser *);
1655
1656 /* Declarations [gram.dcl.dcl] */
1657
1658 static void cp_parser_declaration_seq_opt
1659   (cp_parser *);
1660 static void cp_parser_declaration
1661   (cp_parser *);
1662 static void cp_parser_block_declaration
1663   (cp_parser *, bool);
1664 static void cp_parser_simple_declaration
1665   (cp_parser *, bool);
1666 static void cp_parser_decl_specifier_seq
1667   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1668 static tree cp_parser_storage_class_specifier_opt
1669   (cp_parser *);
1670 static tree cp_parser_function_specifier_opt
1671   (cp_parser *, cp_decl_specifier_seq *);
1672 static tree cp_parser_type_specifier
1673   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1674    int *, bool *);
1675 static tree cp_parser_simple_type_specifier
1676   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1677 static tree cp_parser_type_name
1678   (cp_parser *);
1679 static tree cp_parser_nonclass_name 
1680   (cp_parser* parser);
1681 static tree cp_parser_elaborated_type_specifier
1682   (cp_parser *, bool, bool);
1683 static tree cp_parser_enum_specifier
1684   (cp_parser *);
1685 static void cp_parser_enumerator_list
1686   (cp_parser *, tree);
1687 static void cp_parser_enumerator_definition
1688   (cp_parser *, tree);
1689 static tree cp_parser_namespace_name
1690   (cp_parser *);
1691 static void cp_parser_namespace_definition
1692   (cp_parser *);
1693 static void cp_parser_namespace_body
1694   (cp_parser *);
1695 static tree cp_parser_qualified_namespace_specifier
1696   (cp_parser *);
1697 static void cp_parser_namespace_alias_definition
1698   (cp_parser *);
1699 static bool cp_parser_using_declaration
1700   (cp_parser *, bool);
1701 static void cp_parser_using_directive
1702   (cp_parser *);
1703 static void cp_parser_asm_definition
1704   (cp_parser *);
1705 static void cp_parser_linkage_specification
1706   (cp_parser *);
1707 static void cp_parser_static_assert
1708   (cp_parser *, bool);
1709 static tree cp_parser_decltype
1710   (cp_parser *);
1711
1712 /* Declarators [gram.dcl.decl] */
1713
1714 static tree cp_parser_init_declarator
1715   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1716 static cp_declarator *cp_parser_declarator
1717   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1718 static cp_declarator *cp_parser_direct_declarator
1719   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1720 static enum tree_code cp_parser_ptr_operator
1721   (cp_parser *, tree *, cp_cv_quals *);
1722 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1723   (cp_parser *);
1724 static tree cp_parser_late_return_type_opt
1725   (cp_parser *);
1726 static tree cp_parser_declarator_id
1727   (cp_parser *, bool);
1728 static tree cp_parser_type_id
1729   (cp_parser *);
1730 static tree cp_parser_template_type_arg
1731   (cp_parser *);
1732 static tree cp_parser_type_id_1
1733   (cp_parser *, bool);
1734 static void cp_parser_type_specifier_seq
1735   (cp_parser *, bool, cp_decl_specifier_seq *);
1736 static tree cp_parser_parameter_declaration_clause
1737   (cp_parser *);
1738 static tree cp_parser_parameter_declaration_list
1739   (cp_parser *, bool *);
1740 static cp_parameter_declarator *cp_parser_parameter_declaration
1741   (cp_parser *, bool, bool *);
1742 static tree cp_parser_default_argument 
1743   (cp_parser *, bool);
1744 static void cp_parser_function_body
1745   (cp_parser *);
1746 static tree cp_parser_initializer
1747   (cp_parser *, bool *, bool *);
1748 static tree cp_parser_initializer_clause
1749   (cp_parser *, bool *);
1750 static tree cp_parser_braced_list
1751   (cp_parser*, bool*);
1752 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1753   (cp_parser *, bool *);
1754
1755 static bool cp_parser_ctor_initializer_opt_and_function_body
1756   (cp_parser *);
1757
1758 /* Classes [gram.class] */
1759
1760 static tree cp_parser_class_name
1761   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1762 static tree cp_parser_class_specifier
1763   (cp_parser *);
1764 static tree cp_parser_class_head
1765   (cp_parser *, bool *, tree *, tree *);
1766 static enum tag_types cp_parser_class_key
1767   (cp_parser *);
1768 static void cp_parser_member_specification_opt
1769   (cp_parser *);
1770 static void cp_parser_member_declaration
1771   (cp_parser *);
1772 static tree cp_parser_pure_specifier
1773   (cp_parser *);
1774 static tree cp_parser_constant_initializer
1775   (cp_parser *);
1776
1777 /* Derived classes [gram.class.derived] */
1778
1779 static tree cp_parser_base_clause
1780   (cp_parser *);
1781 static tree cp_parser_base_specifier
1782   (cp_parser *);
1783
1784 /* Special member functions [gram.special] */
1785
1786 static tree cp_parser_conversion_function_id
1787   (cp_parser *);
1788 static tree cp_parser_conversion_type_id
1789   (cp_parser *);
1790 static cp_declarator *cp_parser_conversion_declarator_opt
1791   (cp_parser *);
1792 static bool cp_parser_ctor_initializer_opt
1793   (cp_parser *);
1794 static void cp_parser_mem_initializer_list
1795   (cp_parser *);
1796 static tree cp_parser_mem_initializer
1797   (cp_parser *);
1798 static tree cp_parser_mem_initializer_id
1799   (cp_parser *);
1800
1801 /* Overloading [gram.over] */
1802
1803 static tree cp_parser_operator_function_id
1804   (cp_parser *);
1805 static tree cp_parser_operator
1806   (cp_parser *);
1807
1808 /* Templates [gram.temp] */
1809
1810 static void cp_parser_template_declaration
1811   (cp_parser *, bool);
1812 static tree cp_parser_template_parameter_list
1813   (cp_parser *);
1814 static tree cp_parser_template_parameter
1815   (cp_parser *, bool *, bool *);
1816 static tree cp_parser_type_parameter
1817   (cp_parser *, bool *);
1818 static tree cp_parser_template_id
1819   (cp_parser *, bool, bool, bool);
1820 static tree cp_parser_template_name
1821   (cp_parser *, bool, bool, bool, bool *);
1822 static tree cp_parser_template_argument_list
1823   (cp_parser *);
1824 static tree cp_parser_template_argument
1825   (cp_parser *);
1826 static void cp_parser_explicit_instantiation
1827   (cp_parser *);
1828 static void cp_parser_explicit_specialization
1829   (cp_parser *);
1830
1831 /* Exception handling [gram.exception] */
1832
1833 static tree cp_parser_try_block
1834   (cp_parser *);
1835 static bool cp_parser_function_try_block
1836   (cp_parser *);
1837 static void cp_parser_handler_seq
1838   (cp_parser *);
1839 static void cp_parser_handler
1840   (cp_parser *);
1841 static tree cp_parser_exception_declaration
1842   (cp_parser *);
1843 static tree cp_parser_throw_expression
1844   (cp_parser *);
1845 static tree cp_parser_exception_specification_opt
1846   (cp_parser *);
1847 static tree cp_parser_type_id_list
1848   (cp_parser *);
1849
1850 /* GNU Extensions */
1851
1852 static tree cp_parser_asm_specification_opt
1853   (cp_parser *);
1854 static tree cp_parser_asm_operand_list
1855   (cp_parser *);
1856 static tree cp_parser_asm_clobber_list
1857   (cp_parser *);
1858 static tree cp_parser_attributes_opt
1859   (cp_parser *);
1860 static tree cp_parser_attribute_list
1861   (cp_parser *);
1862 static bool cp_parser_extension_opt
1863   (cp_parser *, int *);
1864 static void cp_parser_label_declaration
1865   (cp_parser *);
1866
1867 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1868 static bool cp_parser_pragma
1869   (cp_parser *, enum pragma_context);
1870
1871 /* Objective-C++ Productions */
1872
1873 static tree cp_parser_objc_message_receiver
1874   (cp_parser *);
1875 static tree cp_parser_objc_message_args
1876   (cp_parser *);
1877 static tree cp_parser_objc_message_expression
1878   (cp_parser *);
1879 static tree cp_parser_objc_encode_expression
1880   (cp_parser *);
1881 static tree cp_parser_objc_defs_expression
1882   (cp_parser *);
1883 static tree cp_parser_objc_protocol_expression
1884   (cp_parser *);
1885 static tree cp_parser_objc_selector_expression
1886   (cp_parser *);
1887 static tree cp_parser_objc_expression
1888   (cp_parser *);
1889 static bool cp_parser_objc_selector_p
1890   (enum cpp_ttype);
1891 static tree cp_parser_objc_selector
1892   (cp_parser *);
1893 static tree cp_parser_objc_protocol_refs_opt
1894   (cp_parser *);
1895 static void cp_parser_objc_declaration
1896   (cp_parser *);
1897 static tree cp_parser_objc_statement
1898   (cp_parser *);
1899
1900 /* Utility Routines */
1901
1902 static tree cp_parser_lookup_name
1903   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1904 static tree cp_parser_lookup_name_simple
1905   (cp_parser *, tree, location_t);
1906 static tree cp_parser_maybe_treat_template_as_class
1907   (tree, bool);
1908 static bool cp_parser_check_declarator_template_parameters
1909   (cp_parser *, cp_declarator *, location_t);
1910 static bool cp_parser_check_template_parameters
1911   (cp_parser *, unsigned, location_t, cp_declarator *);
1912 static tree cp_parser_simple_cast_expression
1913   (cp_parser *);
1914 static tree cp_parser_global_scope_opt
1915   (cp_parser *, bool);
1916 static bool cp_parser_constructor_declarator_p
1917   (cp_parser *, bool);
1918 static tree cp_parser_function_definition_from_specifiers_and_declarator
1919   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1920 static tree cp_parser_function_definition_after_declarator
1921   (cp_parser *, bool);
1922 static void cp_parser_template_declaration_after_export
1923   (cp_parser *, bool);
1924 static void cp_parser_perform_template_parameter_access_checks
1925   (VEC (deferred_access_check,gc)*);
1926 static tree cp_parser_single_declaration
1927   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1928 static tree cp_parser_functional_cast
1929   (cp_parser *, tree);
1930 static tree cp_parser_save_member_function_body
1931   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1932 static tree cp_parser_enclosed_template_argument_list
1933   (cp_parser *);
1934 static void cp_parser_save_default_args
1935   (cp_parser *, tree);
1936 static void cp_parser_late_parsing_for_member
1937   (cp_parser *, tree);
1938 static void cp_parser_late_parsing_default_args
1939   (cp_parser *, tree);
1940 static tree cp_parser_sizeof_operand
1941   (cp_parser *, enum rid);
1942 static tree cp_parser_trait_expr
1943   (cp_parser *, enum rid);
1944 static bool cp_parser_declares_only_class_p
1945   (cp_parser *);
1946 static void cp_parser_set_storage_class
1947   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1948 static void cp_parser_set_decl_spec_type
1949   (cp_decl_specifier_seq *, tree, location_t, bool);
1950 static bool cp_parser_friend_p
1951   (const cp_decl_specifier_seq *);
1952 static cp_token *cp_parser_require
1953   (cp_parser *, enum cpp_ttype, const char *);
1954 static cp_token *cp_parser_require_keyword
1955   (cp_parser *, enum rid, const char *);
1956 static bool cp_parser_token_starts_function_definition_p
1957   (cp_token *);
1958 static bool cp_parser_next_token_starts_class_definition_p
1959   (cp_parser *);
1960 static bool cp_parser_next_token_ends_template_argument_p
1961   (cp_parser *);
1962 static bool cp_parser_nth_token_starts_template_argument_list_p
1963   (cp_parser *, size_t);
1964 static enum tag_types cp_parser_token_is_class_key
1965   (cp_token *);
1966 static void cp_parser_check_class_key
1967   (enum tag_types, tree type);
1968 static void cp_parser_check_access_in_redeclaration
1969   (tree type, location_t location);
1970 static bool cp_parser_optional_template_keyword
1971   (cp_parser *);
1972 static void cp_parser_pre_parsed_nested_name_specifier
1973   (cp_parser *);
1974 static bool cp_parser_cache_group
1975   (cp_parser *, enum cpp_ttype, unsigned);
1976 static void cp_parser_parse_tentatively
1977   (cp_parser *);
1978 static void cp_parser_commit_to_tentative_parse
1979   (cp_parser *);
1980 static void cp_parser_abort_tentative_parse
1981   (cp_parser *);
1982 static bool cp_parser_parse_definitely
1983   (cp_parser *);
1984 static inline bool cp_parser_parsing_tentatively
1985   (cp_parser *);
1986 static bool cp_parser_uncommitted_to_tentative_parse_p
1987   (cp_parser *);
1988 static void cp_parser_error
1989   (cp_parser *, const char *);
1990 static void cp_parser_name_lookup_error
1991   (cp_parser *, tree, tree, const char *, location_t);
1992 static bool cp_parser_simulate_error
1993   (cp_parser *);
1994 static bool cp_parser_check_type_definition
1995   (cp_parser *);
1996 static void cp_parser_check_for_definition_in_return_type
1997   (cp_declarator *, tree, location_t type_location);
1998 static void cp_parser_check_for_invalid_template_id
1999   (cp_parser *, tree, location_t location);
2000 static bool cp_parser_non_integral_constant_expression
2001   (cp_parser *, const char *);
2002 static void cp_parser_diagnose_invalid_type_name
2003   (cp_parser *, tree, tree, location_t);
2004 static bool cp_parser_parse_and_diagnose_invalid_type_name
2005   (cp_parser *);
2006 static int cp_parser_skip_to_closing_parenthesis
2007   (cp_parser *, bool, bool, bool);
2008 static void cp_parser_skip_to_end_of_statement
2009   (cp_parser *);
2010 static void cp_parser_consume_semicolon_at_end_of_statement
2011   (cp_parser *);
2012 static void cp_parser_skip_to_end_of_block_or_statement
2013   (cp_parser *);
2014 static bool cp_parser_skip_to_closing_brace
2015   (cp_parser *);
2016 static void cp_parser_skip_to_end_of_template_parameter_list
2017   (cp_parser *);
2018 static void cp_parser_skip_to_pragma_eol
2019   (cp_parser*, cp_token *);
2020 static bool cp_parser_error_occurred
2021   (cp_parser *);
2022 static bool cp_parser_allow_gnu_extensions_p
2023   (cp_parser *);
2024 static bool cp_parser_is_string_literal
2025   (cp_token *);
2026 static bool cp_parser_is_keyword
2027   (cp_token *, enum rid);
2028 static tree cp_parser_make_typename_type
2029   (cp_parser *, tree, tree, location_t location);
2030 static cp_declarator * cp_parser_make_indirect_declarator
2031   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2032
2033 /* Returns nonzero if we are parsing tentatively.  */
2034
2035 static inline bool
2036 cp_parser_parsing_tentatively (cp_parser* parser)
2037 {
2038   return parser->context->next != NULL;
2039 }
2040
2041 /* Returns nonzero if TOKEN is a string literal.  */
2042
2043 static bool
2044 cp_parser_is_string_literal (cp_token* token)
2045 {
2046   return (token->type == CPP_STRING ||
2047           token->type == CPP_STRING16 ||
2048           token->type == CPP_STRING32 ||
2049           token->type == CPP_WSTRING);
2050 }
2051
2052 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2053
2054 static bool
2055 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2056 {
2057   return token->keyword == keyword;
2058 }
2059
2060 /* If not parsing tentatively, issue a diagnostic of the form
2061       FILE:LINE: MESSAGE before TOKEN
2062    where TOKEN is the next token in the input stream.  MESSAGE
2063    (specified by the caller) is usually of the form "expected
2064    OTHER-TOKEN".  */
2065
2066 static void
2067 cp_parser_error (cp_parser* parser, const char* message)
2068 {
2069   if (!cp_parser_simulate_error (parser))
2070     {
2071       cp_token *token = cp_lexer_peek_token (parser->lexer);
2072       /* This diagnostic makes more sense if it is tagged to the line
2073          of the token we just peeked at.  */
2074       cp_lexer_set_source_position_from_token (token);
2075
2076       if (token->type == CPP_PRAGMA)
2077         {
2078           error ("%H%<#pragma%> is not allowed here", &token->location);
2079           cp_parser_skip_to_pragma_eol (parser, token);
2080           return;
2081         }
2082
2083       c_parse_error (message,
2084                      /* Because c_parser_error does not understand
2085                         CPP_KEYWORD, keywords are treated like
2086                         identifiers.  */
2087                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2088                      token->u.value, token->flags);
2089     }
2090 }
2091
2092 /* Issue an error about name-lookup failing.  NAME is the
2093    IDENTIFIER_NODE DECL is the result of
2094    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2095    the thing that we hoped to find.  */
2096
2097 static void
2098 cp_parser_name_lookup_error (cp_parser* parser,
2099                              tree name,
2100                              tree decl,
2101                              const char* desired,
2102                              location_t location)
2103 {
2104   /* If name lookup completely failed, tell the user that NAME was not
2105      declared.  */
2106   if (decl == error_mark_node)
2107     {
2108       if (parser->scope && parser->scope != global_namespace)
2109         error ("%H%<%E::%E%> has not been declared",
2110                &location, parser->scope, name);
2111       else if (parser->scope == global_namespace)
2112         error ("%H%<::%E%> has not been declared", &location, name);
2113       else if (parser->object_scope
2114                && !CLASS_TYPE_P (parser->object_scope))
2115         error ("%Hrequest for member %qE in non-class type %qT",
2116                &location, name, parser->object_scope);
2117       else if (parser->object_scope)
2118         error ("%H%<%T::%E%> has not been declared",
2119                &location, parser->object_scope, name);
2120       else
2121         error ("%H%qE has not been declared", &location, name);
2122     }
2123   else if (parser->scope && parser->scope != global_namespace)
2124     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2125   else if (parser->scope == global_namespace)
2126     error ("%H%<::%E%> %s", &location, name, desired);
2127   else
2128     error ("%H%qE %s", &location, name, desired);
2129 }
2130
2131 /* If we are parsing tentatively, remember that an error has occurred
2132    during this tentative parse.  Returns true if the error was
2133    simulated; false if a message should be issued by the caller.  */
2134
2135 static bool
2136 cp_parser_simulate_error (cp_parser* parser)
2137 {
2138   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2139     {
2140       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2141       return true;
2142     }
2143   return false;
2144 }
2145
2146 /* Check for repeated decl-specifiers.  */
2147
2148 static void
2149 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2150                            location_t location)
2151 {
2152   int ds;
2153
2154   for (ds = ds_first; ds != ds_last; ++ds)
2155     {
2156       unsigned count = decl_specs->specs[ds];
2157       if (count < 2)
2158         continue;
2159       /* The "long" specifier is a special case because of "long long".  */
2160       if (ds == ds_long)
2161         {
2162           if (count > 2)
2163             error ("%H%<long long long%> is too long for GCC", &location);
2164           else 
2165             pedwarn_cxx98 (location, OPT_Wlong_long, 
2166                            "ISO C++ 1998 does not support %<long long%>");
2167         }
2168       else if (count > 1)
2169         {
2170           static const char *const decl_spec_names[] = {
2171             "signed",
2172             "unsigned",
2173             "short",
2174             "long",
2175             "const",
2176             "volatile",
2177             "restrict",
2178             "inline",
2179             "virtual",
2180             "explicit",
2181             "friend",
2182             "typedef",
2183             "__complex",
2184             "__thread"
2185           };
2186           error ("%Hduplicate %qs", &location, decl_spec_names[ds]);
2187         }
2188     }
2189 }
2190
2191 /* This function is called when a type is defined.  If type
2192    definitions are forbidden at this point, an error message is
2193    issued.  */
2194
2195 static bool
2196 cp_parser_check_type_definition (cp_parser* parser)
2197 {
2198   /* If types are forbidden here, issue a message.  */
2199   if (parser->type_definition_forbidden_message)
2200     {
2201       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2202          in the message need to be interpreted.  */
2203       error (parser->type_definition_forbidden_message);
2204       return false;
2205     }
2206   return true;
2207 }
2208
2209 /* This function is called when the DECLARATOR is processed.  The TYPE
2210    was a type defined in the decl-specifiers.  If it is invalid to
2211    define a type in the decl-specifiers for DECLARATOR, an error is
2212    issued. TYPE_LOCATION is the location of TYPE and is used
2213    for error reporting.  */
2214
2215 static void
2216 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2217                                                tree type, location_t type_location)
2218 {
2219   /* [dcl.fct] forbids type definitions in return types.
2220      Unfortunately, it's not easy to know whether or not we are
2221      processing a return type until after the fact.  */
2222   while (declarator
2223          && (declarator->kind == cdk_pointer
2224              || declarator->kind == cdk_reference
2225              || declarator->kind == cdk_ptrmem))
2226     declarator = declarator->declarator;
2227   if (declarator
2228       && declarator->kind == cdk_function)
2229     {
2230       error ("%Hnew types may not be defined in a return type", &type_location);
2231       inform (type_location, 
2232               "(perhaps a semicolon is missing after the definition of %qT)",
2233               type);
2234     }
2235 }
2236
2237 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2238    "<" in any valid C++ program.  If the next token is indeed "<",
2239    issue a message warning the user about what appears to be an
2240    invalid attempt to form a template-id. LOCATION is the location
2241    of the type-specifier (TYPE) */
2242
2243 static void
2244 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2245                                          tree type, location_t location)
2246 {
2247   cp_token_position start = 0;
2248
2249   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2250     {
2251       if (TYPE_P (type))
2252         error ("%H%qT is not a template", &location, type);
2253       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2254         error ("%H%qE is not a template", &location, type);
2255       else
2256         error ("%Hinvalid template-id", &location);
2257       /* Remember the location of the invalid "<".  */
2258       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2259         start = cp_lexer_token_position (parser->lexer, true);
2260       /* Consume the "<".  */
2261       cp_lexer_consume_token (parser->lexer);
2262       /* Parse the template arguments.  */
2263       cp_parser_enclosed_template_argument_list (parser);
2264       /* Permanently remove the invalid template arguments so that
2265          this error message is not issued again.  */
2266       if (start)
2267         cp_lexer_purge_tokens_after (parser->lexer, start);
2268     }
2269 }
2270
2271 /* If parsing an integral constant-expression, issue an error message
2272    about the fact that THING appeared and return true.  Otherwise,
2273    return false.  In either case, set
2274    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2275
2276 static bool
2277 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2278                                             const char *thing)
2279 {
2280   parser->non_integral_constant_expression_p = true;
2281   if (parser->integral_constant_expression_p)
2282     {
2283       if (!parser->allow_non_integral_constant_expression_p)
2284         {
2285           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2286              in the message need to be interpreted.  */
2287           char *message = concat (thing,
2288                                   " cannot appear in a constant-expression",
2289                                   NULL);
2290           error (message);
2291           free (message);
2292           return true;
2293         }
2294     }
2295   return false;
2296 }
2297
2298 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2299    qualifying scope (or NULL, if none) for ID.  This function commits
2300    to the current active tentative parse, if any.  (Otherwise, the
2301    problematic construct might be encountered again later, resulting
2302    in duplicate error messages.) LOCATION is the location of ID.  */
2303
2304 static void
2305 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2306                                       tree scope, tree id,
2307                                       location_t location)
2308 {
2309   tree decl, old_scope;
2310   /* Try to lookup the identifier.  */
2311   old_scope = parser->scope;
2312   parser->scope = scope;
2313   decl = cp_parser_lookup_name_simple (parser, id, location);
2314   parser->scope = old_scope;
2315   /* If the lookup found a template-name, it means that the user forgot
2316   to specify an argument list. Emit a useful error message.  */
2317   if (TREE_CODE (decl) == TEMPLATE_DECL)
2318     error ("%Hinvalid use of template-name %qE without an argument list",
2319            &location, decl);
2320   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2321     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2322   else if (TREE_CODE (decl) == TYPE_DECL)
2323     /* Something like 'unsigned A a;'  */
2324     error ("%Hinvalid combination of multiple type-specifiers",
2325            &location);
2326   else if (!parser->scope)
2327     {
2328       /* Issue an error message.  */
2329       error ("%H%qE does not name a type", &location, id);
2330       /* If we're in a template class, it's possible that the user was
2331          referring to a type from a base class.  For example:
2332
2333            template <typename T> struct A { typedef T X; };
2334            template <typename T> struct B : public A<T> { X x; };
2335
2336          The user should have said "typename A<T>::X".  */
2337       if (processing_template_decl && current_class_type
2338           && TYPE_BINFO (current_class_type))
2339         {
2340           tree b;
2341
2342           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2343                b;
2344                b = TREE_CHAIN (b))
2345             {
2346               tree base_type = BINFO_TYPE (b);
2347               if (CLASS_TYPE_P (base_type)
2348                   && dependent_type_p (base_type))
2349                 {
2350                   tree field;
2351                   /* Go from a particular instantiation of the
2352                      template (which will have an empty TYPE_FIELDs),
2353                      to the main version.  */
2354                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2355                   for (field = TYPE_FIELDS (base_type);
2356                        field;
2357                        field = TREE_CHAIN (field))
2358                     if (TREE_CODE (field) == TYPE_DECL
2359                         && DECL_NAME (field) == id)
2360                       {
2361                         inform (location, 
2362                                 "(perhaps %<typename %T::%E%> was intended)",
2363                                 BINFO_TYPE (b), id);
2364                         break;
2365                       }
2366                   if (field)
2367                     break;
2368                 }
2369             }
2370         }
2371     }
2372   /* Here we diagnose qualified-ids where the scope is actually correct,
2373      but the identifier does not resolve to a valid type name.  */
2374   else if (parser->scope != error_mark_node)
2375     {
2376       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2377         error ("%H%qE in namespace %qE does not name a type",
2378                &location, id, parser->scope);
2379       else if (TYPE_P (parser->scope))
2380         error ("%H%qE in class %qT does not name a type",
2381                &location, id, parser->scope);
2382       else
2383         gcc_unreachable ();
2384     }
2385   cp_parser_commit_to_tentative_parse (parser);
2386 }
2387
2388 /* Check for a common situation where a type-name should be present,
2389    but is not, and issue a sensible error message.  Returns true if an
2390    invalid type-name was detected.
2391
2392    The situation handled by this function are variable declarations of the
2393    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2394    Usually, `ID' should name a type, but if we got here it means that it
2395    does not. We try to emit the best possible error message depending on
2396    how exactly the id-expression looks like.  */
2397
2398 static bool
2399 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2400 {
2401   tree id;
2402   cp_token *token = cp_lexer_peek_token (parser->lexer);
2403
2404   cp_parser_parse_tentatively (parser);
2405   id = cp_parser_id_expression (parser,
2406                                 /*template_keyword_p=*/false,
2407                                 /*check_dependency_p=*/true,
2408                                 /*template_p=*/NULL,
2409                                 /*declarator_p=*/true,
2410                                 /*optional_p=*/false);
2411   /* After the id-expression, there should be a plain identifier,
2412      otherwise this is not a simple variable declaration. Also, if
2413      the scope is dependent, we cannot do much.  */
2414   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2415       || (parser->scope && TYPE_P (parser->scope)
2416           && dependent_type_p (parser->scope))
2417       || TREE_CODE (id) == TYPE_DECL)
2418     {
2419       cp_parser_abort_tentative_parse (parser);
2420       return false;
2421     }
2422   if (!cp_parser_parse_definitely (parser))
2423     return false;
2424
2425   /* Emit a diagnostic for the invalid type.  */
2426   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2427                                         id, token->location);
2428   /* Skip to the end of the declaration; there's no point in
2429      trying to process it.  */
2430   cp_parser_skip_to_end_of_block_or_statement (parser);
2431   return true;
2432 }
2433
2434 /* Consume tokens up to, and including, the next non-nested closing `)'.
2435    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2436    are doing error recovery. Returns -1 if OR_COMMA is true and we
2437    found an unnested comma.  */
2438
2439 static int
2440 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2441                                        bool recovering,
2442                                        bool or_comma,
2443                                        bool consume_paren)
2444 {
2445   unsigned paren_depth = 0;
2446   unsigned brace_depth = 0;
2447
2448   if (recovering && !or_comma
2449       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2450     return 0;
2451
2452   while (true)
2453     {
2454       cp_token * token = cp_lexer_peek_token (parser->lexer);
2455
2456       switch (token->type)
2457         {
2458         case CPP_EOF:
2459         case CPP_PRAGMA_EOL:
2460           /* If we've run out of tokens, then there is no closing `)'.  */
2461           return 0;
2462
2463         case CPP_SEMICOLON:
2464           /* This matches the processing in skip_to_end_of_statement.  */
2465           if (!brace_depth)
2466             return 0;
2467           break;
2468
2469         case CPP_OPEN_BRACE:
2470           ++brace_depth;
2471           break;
2472         case CPP_CLOSE_BRACE:
2473           if (!brace_depth--)
2474             return 0;
2475           break;
2476
2477         case CPP_COMMA:
2478           if (recovering && or_comma && !brace_depth && !paren_depth)
2479             return -1;
2480           break;
2481
2482         case CPP_OPEN_PAREN:
2483           if (!brace_depth)
2484             ++paren_depth;
2485           break;
2486
2487         case CPP_CLOSE_PAREN:
2488           if (!brace_depth && !paren_depth--)
2489             {
2490               if (consume_paren)
2491                 cp_lexer_consume_token (parser->lexer);
2492               return 1;
2493             }
2494           break;
2495
2496         default:
2497           break;
2498         }
2499
2500       /* Consume the token.  */
2501       cp_lexer_consume_token (parser->lexer);
2502     }
2503 }
2504
2505 /* Consume tokens until we reach the end of the current statement.
2506    Normally, that will be just before consuming a `;'.  However, if a
2507    non-nested `}' comes first, then we stop before consuming that.  */
2508
2509 static void
2510 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2511 {
2512   unsigned nesting_depth = 0;
2513
2514   while (true)
2515     {
2516       cp_token *token = cp_lexer_peek_token (parser->lexer);
2517
2518       switch (token->type)
2519         {
2520         case CPP_EOF:
2521         case CPP_PRAGMA_EOL:
2522           /* If we've run out of tokens, stop.  */
2523           return;
2524
2525         case CPP_SEMICOLON:
2526           /* If the next token is a `;', we have reached the end of the
2527              statement.  */
2528           if (!nesting_depth)
2529             return;
2530           break;
2531
2532         case CPP_CLOSE_BRACE:
2533           /* If this is a non-nested '}', stop before consuming it.
2534              That way, when confronted with something like:
2535
2536                { 3 + }
2537
2538              we stop before consuming the closing '}', even though we
2539              have not yet reached a `;'.  */
2540           if (nesting_depth == 0)
2541             return;
2542
2543           /* If it is the closing '}' for a block that we have
2544              scanned, stop -- but only after consuming the token.
2545              That way given:
2546
2547                 void f g () { ... }
2548                 typedef int I;
2549
2550              we will stop after the body of the erroneously declared
2551              function, but before consuming the following `typedef'
2552              declaration.  */
2553           if (--nesting_depth == 0)
2554             {
2555               cp_lexer_consume_token (parser->lexer);
2556               return;
2557             }
2558
2559         case CPP_OPEN_BRACE:
2560           ++nesting_depth;
2561           break;
2562
2563         default:
2564           break;
2565         }
2566
2567       /* Consume the token.  */
2568       cp_lexer_consume_token (parser->lexer);
2569     }
2570 }
2571
2572 /* This function is called at the end of a statement or declaration.
2573    If the next token is a semicolon, it is consumed; otherwise, error
2574    recovery is attempted.  */
2575
2576 static void
2577 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2578 {
2579   /* Look for the trailing `;'.  */
2580   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2581     {
2582       /* If there is additional (erroneous) input, skip to the end of
2583          the statement.  */
2584       cp_parser_skip_to_end_of_statement (parser);
2585       /* If the next token is now a `;', consume it.  */
2586       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2587         cp_lexer_consume_token (parser->lexer);
2588     }
2589 }
2590
2591 /* Skip tokens until we have consumed an entire block, or until we
2592    have consumed a non-nested `;'.  */
2593
2594 static void
2595 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2596 {
2597   int nesting_depth = 0;
2598
2599   while (nesting_depth >= 0)
2600     {
2601       cp_token *token = cp_lexer_peek_token (parser->lexer);
2602
2603       switch (token->type)
2604         {
2605         case CPP_EOF:
2606         case CPP_PRAGMA_EOL:
2607           /* If we've run out of tokens, stop.  */
2608           return;
2609
2610         case CPP_SEMICOLON:
2611           /* Stop if this is an unnested ';'. */
2612           if (!nesting_depth)
2613             nesting_depth = -1;
2614           break;
2615
2616         case CPP_CLOSE_BRACE:
2617           /* Stop if this is an unnested '}', or closes the outermost
2618              nesting level.  */
2619           nesting_depth--;
2620           if (nesting_depth < 0)
2621             return;
2622           if (!nesting_depth)
2623             nesting_depth = -1;
2624           break;
2625
2626         case CPP_OPEN_BRACE:
2627           /* Nest. */
2628           nesting_depth++;
2629           break;
2630
2631         default:
2632           break;
2633         }
2634
2635       /* Consume the token.  */
2636       cp_lexer_consume_token (parser->lexer);
2637     }
2638 }
2639
2640 /* Skip tokens until a non-nested closing curly brace is the next
2641    token, or there are no more tokens. Return true in the first case,
2642    false otherwise.  */
2643
2644 static bool
2645 cp_parser_skip_to_closing_brace (cp_parser *parser)
2646 {
2647   unsigned nesting_depth = 0;
2648
2649   while (true)
2650     {
2651       cp_token *token = cp_lexer_peek_token (parser->lexer);
2652
2653       switch (token->type)
2654         {
2655         case CPP_EOF:
2656         case CPP_PRAGMA_EOL:
2657           /* If we've run out of tokens, stop.  */
2658           return false;
2659
2660         case CPP_CLOSE_BRACE:
2661           /* If the next token is a non-nested `}', then we have reached
2662              the end of the current block.  */
2663           if (nesting_depth-- == 0)
2664             return true;
2665           break;
2666
2667         case CPP_OPEN_BRACE:
2668           /* If it the next token is a `{', then we are entering a new
2669              block.  Consume the entire block.  */
2670           ++nesting_depth;
2671           break;
2672
2673         default:
2674           break;
2675         }
2676
2677       /* Consume the token.  */
2678       cp_lexer_consume_token (parser->lexer);
2679     }
2680 }
2681
2682 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2683    parameter is the PRAGMA token, allowing us to purge the entire pragma
2684    sequence.  */
2685
2686 static void
2687 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2688 {
2689   cp_token *token;
2690
2691   parser->lexer->in_pragma = false;
2692
2693   do
2694     token = cp_lexer_consume_token (parser->lexer);
2695   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2696
2697   /* Ensure that the pragma is not parsed again.  */
2698   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2699 }
2700
2701 /* Require pragma end of line, resyncing with it as necessary.  The
2702    arguments are as for cp_parser_skip_to_pragma_eol.  */
2703
2704 static void
2705 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2706 {
2707   parser->lexer->in_pragma = false;
2708   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2709     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2710 }
2711
2712 /* This is a simple wrapper around make_typename_type. When the id is
2713    an unresolved identifier node, we can provide a superior diagnostic
2714    using cp_parser_diagnose_invalid_type_name.  */
2715
2716 static tree
2717 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2718                               tree id, location_t id_location)
2719 {
2720   tree result;
2721   if (TREE_CODE (id) == IDENTIFIER_NODE)
2722     {
2723       result = make_typename_type (scope, id, typename_type,
2724                                    /*complain=*/tf_none);
2725       if (result == error_mark_node)
2726         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2727       return result;
2728     }
2729   return make_typename_type (scope, id, typename_type, tf_error);
2730 }
2731
2732 /* This is a wrapper around the
2733    make_{pointer,ptrmem,reference}_declarator functions that decides
2734    which one to call based on the CODE and CLASS_TYPE arguments. The
2735    CODE argument should be one of the values returned by
2736    cp_parser_ptr_operator. */
2737 static cp_declarator *
2738 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2739                                     cp_cv_quals cv_qualifiers,
2740                                     cp_declarator *target)
2741 {
2742   if (code == ERROR_MARK)
2743     return cp_error_declarator;
2744
2745   if (code == INDIRECT_REF)
2746     if (class_type == NULL_TREE)
2747       return make_pointer_declarator (cv_qualifiers, target);
2748     else
2749       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2750   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2751     return make_reference_declarator (cv_qualifiers, target, false);
2752   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2753     return make_reference_declarator (cv_qualifiers, target, true);
2754   gcc_unreachable ();
2755 }
2756
2757 /* Create a new C++ parser.  */
2758
2759 static cp_parser *
2760 cp_parser_new (void)
2761 {
2762   cp_parser *parser;
2763   cp_lexer *lexer;
2764   unsigned i;
2765
2766   /* cp_lexer_new_main is called before calling ggc_alloc because
2767      cp_lexer_new_main might load a PCH file.  */
2768   lexer = cp_lexer_new_main ();
2769
2770   /* Initialize the binops_by_token so that we can get the tree
2771      directly from the token.  */
2772   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2773     binops_by_token[binops[i].token_type] = binops[i];
2774
2775   parser = GGC_CNEW (cp_parser);
2776   parser->lexer = lexer;
2777   parser->context = cp_parser_context_new (NULL);
2778
2779   /* For now, we always accept GNU extensions.  */
2780   parser->allow_gnu_extensions_p = 1;
2781
2782   /* The `>' token is a greater-than operator, not the end of a
2783      template-id.  */
2784   parser->greater_than_is_operator_p = true;
2785
2786   parser->default_arg_ok_p = true;
2787
2788   /* We are not parsing a constant-expression.  */
2789   parser->integral_constant_expression_p = false;
2790   parser->allow_non_integral_constant_expression_p = false;
2791   parser->non_integral_constant_expression_p = false;
2792
2793   /* Local variable names are not forbidden.  */
2794   parser->local_variables_forbidden_p = false;
2795
2796   /* We are not processing an `extern "C"' declaration.  */
2797   parser->in_unbraced_linkage_specification_p = false;
2798
2799   /* We are not processing a declarator.  */
2800   parser->in_declarator_p = false;
2801
2802   /* We are not processing a template-argument-list.  */
2803   parser->in_template_argument_list_p = false;
2804
2805   /* We are not in an iteration statement.  */
2806   parser->in_statement = 0;
2807
2808   /* We are not in a switch statement.  */
2809   parser->in_switch_statement_p = false;
2810
2811   /* We are not parsing a type-id inside an expression.  */
2812   parser->in_type_id_in_expr_p = false;
2813
2814   /* Declarations aren't implicitly extern "C".  */
2815   parser->implicit_extern_c = false;
2816
2817   /* String literals should be translated to the execution character set.  */
2818   parser->translate_strings_p = true;
2819
2820   /* We are not parsing a function body.  */
2821   parser->in_function_body = false;
2822
2823   /* The unparsed function queue is empty.  */
2824   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2825
2826   /* There are no classes being defined.  */
2827   parser->num_classes_being_defined = 0;
2828
2829   /* No template parameters apply.  */
2830   parser->num_template_parameter_lists = 0;
2831
2832   return parser;
2833 }
2834
2835 /* Create a cp_lexer structure which will emit the tokens in CACHE
2836    and push it onto the parser's lexer stack.  This is used for delayed
2837    parsing of in-class method bodies and default arguments, and should
2838    not be confused with tentative parsing.  */
2839 static void
2840 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2841 {
2842   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2843   lexer->next = parser->lexer;
2844   parser->lexer = lexer;
2845
2846   /* Move the current source position to that of the first token in the
2847      new lexer.  */
2848   cp_lexer_set_source_position_from_token (lexer->next_token);
2849 }
2850
2851 /* Pop the top lexer off the parser stack.  This is never used for the
2852    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2853 static void
2854 cp_parser_pop_lexer (cp_parser *parser)
2855 {
2856   cp_lexer *lexer = parser->lexer;
2857   parser->lexer = lexer->next;
2858   cp_lexer_destroy (lexer);
2859
2860   /* Put the current source position back where it was before this
2861      lexer was pushed.  */
2862   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2863 }
2864
2865 /* Lexical conventions [gram.lex]  */
2866
2867 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2868    identifier.  */
2869
2870 static tree
2871 cp_parser_identifier (cp_parser* parser)
2872 {
2873   cp_token *token;
2874
2875   /* Look for the identifier.  */
2876   token = cp_parser_require (parser, CPP_NAME, "identifier");
2877   /* Return the value.  */
2878   return token ? token->u.value : error_mark_node;
2879 }
2880
2881 /* Parse a sequence of adjacent string constants.  Returns a
2882    TREE_STRING representing the combined, nul-terminated string
2883    constant.  If TRANSLATE is true, translate the string to the
2884    execution character set.  If WIDE_OK is true, a wide string is
2885    invalid here.
2886
2887    C++98 [lex.string] says that if a narrow string literal token is
2888    adjacent to a wide string literal token, the behavior is undefined.
2889    However, C99 6.4.5p4 says that this results in a wide string literal.
2890    We follow C99 here, for consistency with the C front end.
2891
2892    This code is largely lifted from lex_string() in c-lex.c.
2893
2894    FUTURE: ObjC++ will need to handle @-strings here.  */
2895 static tree
2896 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2897 {
2898   tree value;
2899   size_t count;
2900   struct obstack str_ob;
2901   cpp_string str, istr, *strs;
2902   cp_token *tok;
2903   enum cpp_ttype type;
2904
2905   tok = cp_lexer_peek_token (parser->lexer);
2906   if (!cp_parser_is_string_literal (tok))
2907     {
2908       cp_parser_error (parser, "expected string-literal");
2909       return error_mark_node;
2910     }
2911
2912   type = tok->type;
2913
2914   /* Try to avoid the overhead of creating and destroying an obstack
2915      for the common case of just one string.  */
2916   if (!cp_parser_is_string_literal
2917       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2918     {
2919       cp_lexer_consume_token (parser->lexer);
2920
2921       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2922       str.len = TREE_STRING_LENGTH (tok->u.value);
2923       count = 1;
2924
2925       strs = &str;
2926     }
2927   else
2928     {
2929       gcc_obstack_init (&str_ob);
2930       count = 0;
2931
2932       do
2933         {
2934           cp_lexer_consume_token (parser->lexer);
2935           count++;
2936           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2937           str.len = TREE_STRING_LENGTH (tok->u.value);
2938
2939           if (type != tok->type)
2940             {
2941               if (type == CPP_STRING)
2942                 type = tok->type;
2943               else if (tok->type != CPP_STRING)
2944                 error ("%Hunsupported non-standard concatenation "
2945                        "of string literals", &tok->location);
2946             }
2947
2948           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2949
2950           tok = cp_lexer_peek_token (parser->lexer);
2951         }
2952       while (cp_parser_is_string_literal (tok));
2953
2954       strs = (cpp_string *) obstack_finish (&str_ob);
2955     }
2956
2957   if (type != CPP_STRING && !wide_ok)
2958     {
2959       cp_parser_error (parser, "a wide string is invalid in this context");
2960       type = CPP_STRING;
2961     }
2962
2963   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2964       (parse_in, strs, count, &istr, type))
2965     {
2966       value = build_string (istr.len, (const char *)istr.text);
2967       free (CONST_CAST (unsigned char *, istr.text));
2968
2969       switch (type)
2970         {
2971         default:
2972         case CPP_STRING:
2973           TREE_TYPE (value) = char_array_type_node;
2974           break;
2975         case CPP_STRING16:
2976           TREE_TYPE (value) = char16_array_type_node;
2977           break;
2978         case CPP_STRING32:
2979           TREE_TYPE (value) = char32_array_type_node;
2980           break;
2981         case CPP_WSTRING:
2982           TREE_TYPE (value) = wchar_array_type_node;
2983           break;
2984         }
2985
2986       value = fix_string_type (value);
2987     }
2988   else
2989     /* cpp_interpret_string has issued an error.  */
2990     value = error_mark_node;
2991
2992   if (count > 1)
2993     obstack_free (&str_ob, 0);
2994
2995   return value;
2996 }
2997
2998
2999 /* Basic concepts [gram.basic]  */
3000
3001 /* Parse a translation-unit.
3002
3003    translation-unit:
3004      declaration-seq [opt]
3005
3006    Returns TRUE if all went well.  */
3007
3008 static bool
3009 cp_parser_translation_unit (cp_parser* parser)
3010 {
3011   /* The address of the first non-permanent object on the declarator
3012      obstack.  */
3013   static void *declarator_obstack_base;
3014
3015   bool success;
3016
3017   /* Create the declarator obstack, if necessary.  */
3018   if (!cp_error_declarator)
3019     {
3020       gcc_obstack_init (&declarator_obstack);
3021       /* Create the error declarator.  */
3022       cp_error_declarator = make_declarator (cdk_error);
3023       /* Create the empty parameter list.  */
3024       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3025       /* Remember where the base of the declarator obstack lies.  */
3026       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3027     }
3028
3029   cp_parser_declaration_seq_opt (parser);
3030
3031   /* If there are no tokens left then all went well.  */
3032   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3033     {
3034       /* Get rid of the token array; we don't need it any more.  */
3035       cp_lexer_destroy (parser->lexer);
3036       parser->lexer = NULL;
3037
3038       /* This file might have been a context that's implicitly extern
3039          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3040       if (parser->implicit_extern_c)
3041         {
3042           pop_lang_context ();
3043           parser->implicit_extern_c = false;
3044         }
3045
3046       /* Finish up.  */
3047       finish_translation_unit ();
3048
3049       success = true;
3050     }
3051   else
3052     {
3053       cp_parser_error (parser, "expected declaration");
3054       success = false;
3055     }
3056
3057   /* Make sure the declarator obstack was fully cleaned up.  */
3058   gcc_assert (obstack_next_free (&declarator_obstack)
3059               == declarator_obstack_base);
3060
3061   /* All went well.  */
3062   return success;
3063 }
3064
3065 /* Expressions [gram.expr] */
3066
3067 /* Parse a primary-expression.
3068
3069    primary-expression:
3070      literal
3071      this
3072      ( expression )
3073      id-expression
3074
3075    GNU Extensions:
3076
3077    primary-expression:
3078      ( compound-statement )
3079      __builtin_va_arg ( assignment-expression , type-id )
3080      __builtin_offsetof ( type-id , offsetof-expression )
3081
3082    C++ Extensions:
3083      __has_nothrow_assign ( type-id )   
3084      __has_nothrow_constructor ( type-id )
3085      __has_nothrow_copy ( type-id )
3086      __has_trivial_assign ( type-id )   
3087      __has_trivial_constructor ( type-id )
3088      __has_trivial_copy ( type-id )
3089      __has_trivial_destructor ( type-id )
3090      __has_virtual_destructor ( type-id )     
3091      __is_abstract ( type-id )
3092      __is_base_of ( type-id , type-id )
3093      __is_class ( type-id )
3094      __is_convertible_to ( type-id , type-id )     
3095      __is_empty ( type-id )
3096      __is_enum ( type-id )
3097      __is_pod ( type-id )
3098      __is_polymorphic ( type-id )
3099      __is_union ( type-id )
3100
3101    Objective-C++ Extension:
3102
3103    primary-expression:
3104      objc-expression
3105
3106    literal:
3107      __null
3108
3109    ADDRESS_P is true iff this expression was immediately preceded by
3110    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3111    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3112    true iff this expression is a template argument.
3113
3114    Returns a representation of the expression.  Upon return, *IDK
3115    indicates what kind of id-expression (if any) was present.  */
3116
3117 static tree
3118 cp_parser_primary_expression (cp_parser *parser,
3119                               bool address_p,
3120                               bool cast_p,
3121                               bool template_arg_p,
3122                               cp_id_kind *idk)
3123 {
3124   cp_token *token = NULL;
3125
3126   /* Assume the primary expression is not an id-expression.  */
3127   *idk = CP_ID_KIND_NONE;
3128
3129   /* Peek at the next token.  */
3130   token = cp_lexer_peek_token (parser->lexer);
3131   switch (token->type)
3132     {
3133       /* literal:
3134            integer-literal
3135            character-literal
3136            floating-literal
3137            string-literal
3138            boolean-literal  */
3139     case CPP_CHAR:
3140     case CPP_CHAR16:
3141     case CPP_CHAR32:
3142     case CPP_WCHAR:
3143     case CPP_NUMBER:
3144       token = cp_lexer_consume_token (parser->lexer);
3145       if (TREE_CODE (token->u.value) == FIXED_CST)
3146         {
3147           error ("%Hfixed-point types not supported in C++",
3148                  &token->location);
3149           return error_mark_node;
3150         }
3151       /* Floating-point literals are only allowed in an integral
3152          constant expression if they are cast to an integral or
3153          enumeration type.  */
3154       if (TREE_CODE (token->u.value) == REAL_CST
3155           && parser->integral_constant_expression_p
3156           && pedantic)
3157         {
3158           /* CAST_P will be set even in invalid code like "int(2.7 +
3159              ...)".   Therefore, we have to check that the next token
3160              is sure to end the cast.  */
3161           if (cast_p)
3162             {
3163               cp_token *next_token;
3164
3165               next_token = cp_lexer_peek_token (parser->lexer);
3166               if (/* The comma at the end of an
3167                      enumerator-definition.  */
3168                   next_token->type != CPP_COMMA
3169                   /* The curly brace at the end of an enum-specifier.  */
3170                   && next_token->type != CPP_CLOSE_BRACE
3171                   /* The end of a statement.  */
3172                   && next_token->type != CPP_SEMICOLON
3173                   /* The end of the cast-expression.  */
3174                   && next_token->type != CPP_CLOSE_PAREN
3175                   /* The end of an array bound.  */
3176                   && next_token->type != CPP_CLOSE_SQUARE
3177                   /* The closing ">" in a template-argument-list.  */
3178                   && (next_token->type != CPP_GREATER
3179                       || parser->greater_than_is_operator_p)
3180                   /* C++0x only: A ">>" treated like two ">" tokens,
3181                      in a template-argument-list.  */
3182                   && (next_token->type != CPP_RSHIFT
3183                       || (cxx_dialect == cxx98)
3184                       || parser->greater_than_is_operator_p))
3185                 cast_p = false;
3186             }
3187
3188           /* If we are within a cast, then the constraint that the
3189              cast is to an integral or enumeration type will be
3190              checked at that point.  If we are not within a cast, then
3191              this code is invalid.  */
3192           if (!cast_p)
3193             cp_parser_non_integral_constant_expression
3194               (parser, "floating-point literal");
3195         }
3196       return token->u.value;
3197
3198     case CPP_STRING:
3199     case CPP_STRING16:
3200     case CPP_STRING32:
3201     case CPP_WSTRING:
3202       /* ??? Should wide strings be allowed when parser->translate_strings_p
3203          is false (i.e. in attributes)?  If not, we can kill the third
3204          argument to cp_parser_string_literal.  */
3205       return cp_parser_string_literal (parser,
3206                                        parser->translate_strings_p,
3207                                        true);
3208
3209     case CPP_OPEN_PAREN:
3210       {
3211         tree expr;
3212         bool saved_greater_than_is_operator_p;
3213
3214         /* Consume the `('.  */
3215         cp_lexer_consume_token (parser->lexer);
3216         /* Within a parenthesized expression, a `>' token is always
3217            the greater-than operator.  */
3218         saved_greater_than_is_operator_p
3219           = parser->greater_than_is_operator_p;
3220         parser->greater_than_is_operator_p = true;
3221         /* If we see `( { ' then we are looking at the beginning of
3222            a GNU statement-expression.  */
3223         if (cp_parser_allow_gnu_extensions_p (parser)
3224             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3225           {
3226             /* Statement-expressions are not allowed by the standard.  */
3227             pedwarn (token->location, OPT_pedantic, 
3228                      "ISO C++ forbids braced-groups within expressions");
3229
3230             /* And they're not allowed outside of a function-body; you
3231                cannot, for example, write:
3232
3233                  int i = ({ int j = 3; j + 1; });
3234
3235                at class or namespace scope.  */
3236             if (!parser->in_function_body
3237                 || parser->in_template_argument_list_p)
3238               {
3239                 error ("%Hstatement-expressions are not allowed outside "
3240                        "functions nor in template-argument lists",
3241                        &token->location);
3242                 cp_parser_skip_to_end_of_block_or_statement (parser);
3243                 expr = error_mark_node;
3244               }
3245             else
3246               {
3247                 /* Start the statement-expression.  */
3248                 expr = begin_stmt_expr ();
3249                 /* Parse the compound-statement.  */
3250                 cp_parser_compound_statement (parser, expr, false);
3251                 /* Finish up.  */
3252                 expr = finish_stmt_expr (expr, false);
3253               }
3254           }
3255         else
3256           {
3257             /* Parse the parenthesized expression.  */
3258             expr = cp_parser_expression (parser, cast_p, idk);
3259             /* Let the front end know that this expression was
3260                enclosed in parentheses. This matters in case, for
3261                example, the expression is of the form `A::B', since
3262                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3263                not.  */
3264             finish_parenthesized_expr (expr);
3265           }
3266         /* The `>' token might be the end of a template-id or
3267            template-parameter-list now.  */
3268         parser->greater_than_is_operator_p
3269           = saved_greater_than_is_operator_p;
3270         /* Consume the `)'.  */
3271         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3272           cp_parser_skip_to_end_of_statement (parser);
3273
3274         return expr;
3275       }
3276
3277     case CPP_KEYWORD:
3278       switch (token->keyword)
3279         {
3280           /* These two are the boolean literals.  */
3281         case RID_TRUE:
3282           cp_lexer_consume_token (parser->lexer);
3283           return boolean_true_node;
3284         case RID_FALSE:
3285           cp_lexer_consume_token (parser->lexer);
3286           return boolean_false_node;
3287
3288           /* The `__null' literal.  */
3289         case RID_NULL:
3290           cp_lexer_consume_token (parser->lexer);
3291           return null_node;
3292
3293           /* Recognize the `this' keyword.  */
3294         case RID_THIS:
3295           cp_lexer_consume_token (parser->lexer);
3296           if (parser->local_variables_forbidden_p)
3297             {
3298               error ("%H%<this%> may not be used in this context",
3299                      &token->location);
3300               return error_mark_node;
3301             }
3302           /* Pointers cannot appear in constant-expressions.  */
3303           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3304             return error_mark_node;
3305           return finish_this_expr ();
3306
3307           /* The `operator' keyword can be the beginning of an
3308              id-expression.  */
3309         case RID_OPERATOR:
3310           goto id_expression;
3311
3312         case RID_FUNCTION_NAME:
3313         case RID_PRETTY_FUNCTION_NAME:
3314         case RID_C99_FUNCTION_NAME:
3315           {
3316             const char *name;
3317
3318             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3319                __func__ are the names of variables -- but they are
3320                treated specially.  Therefore, they are handled here,
3321                rather than relying on the generic id-expression logic
3322                below.  Grammatically, these names are id-expressions.
3323
3324                Consume the token.  */
3325             token = cp_lexer_consume_token (parser->lexer);
3326
3327             switch (token->keyword)
3328               {
3329               case RID_FUNCTION_NAME:
3330                 name = "%<__FUNCTION__%>";
3331                 break;
3332               case RID_PRETTY_FUNCTION_NAME:
3333                 name = "%<__PRETTY_FUNCTION__%>";
3334                 break;
3335               case RID_C99_FUNCTION_NAME:
3336                 name = "%<__func__%>";
3337                 break;
3338               default:
3339                 gcc_unreachable ();
3340               }
3341
3342             if (cp_parser_non_integral_constant_expression (parser, name))
3343               return error_mark_node;
3344
3345             /* Look up the name.  */
3346             return finish_fname (token->u.value);
3347           }
3348
3349         case RID_VA_ARG:
3350           {
3351             tree expression;
3352             tree type;
3353
3354             /* The `__builtin_va_arg' construct is used to handle
3355                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3356             cp_lexer_consume_token (parser->lexer);
3357             /* Look for the opening `('.  */
3358             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3359             /* Now, parse the assignment-expression.  */
3360             expression = cp_parser_assignment_expression (parser,
3361                                                           /*cast_p=*/false, NULL);
3362             /* Look for the `,'.  */
3363             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3364             /* Parse the type-id.  */
3365             type = cp_parser_type_id (parser);
3366             /* Look for the closing `)'.  */
3367             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3368             /* Using `va_arg' in a constant-expression is not
3369                allowed.  */
3370             if (cp_parser_non_integral_constant_expression (parser,
3371                                                             "%<va_arg%>"))
3372               return error_mark_node;
3373             return build_x_va_arg (expression, type);
3374           }
3375
3376         case RID_OFFSETOF:
3377           return cp_parser_builtin_offsetof (parser);
3378
3379         case RID_HAS_NOTHROW_ASSIGN:
3380         case RID_HAS_NOTHROW_CONSTRUCTOR:
3381         case RID_HAS_NOTHROW_COPY:        
3382         case RID_HAS_TRIVIAL_ASSIGN:
3383         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3384         case RID_HAS_TRIVIAL_COPY:        
3385         case RID_HAS_TRIVIAL_DESTRUCTOR:
3386         case RID_HAS_VIRTUAL_DESTRUCTOR:
3387         case RID_IS_ABSTRACT:
3388         case RID_IS_BASE_OF:
3389         case RID_IS_CLASS:
3390         case RID_IS_CONVERTIBLE_TO:
3391         case RID_IS_EMPTY:
3392         case RID_IS_ENUM:
3393         case RID_IS_POD:
3394         case RID_IS_POLYMORPHIC:
3395         case RID_IS_UNION:
3396           return cp_parser_trait_expr (parser, token->keyword);
3397
3398         /* Objective-C++ expressions.  */
3399         case RID_AT_ENCODE:
3400         case RID_AT_PROTOCOL:
3401         case RID_AT_SELECTOR:
3402           return cp_parser_objc_expression (parser);
3403
3404         default:
3405           cp_parser_error (parser, "expected primary-expression");
3406           return error_mark_node;
3407         }
3408
3409       /* An id-expression can start with either an identifier, a
3410          `::' as the beginning of a qualified-id, or the "operator"
3411          keyword.  */
3412     case CPP_NAME:
3413     case CPP_SCOPE:
3414     case CPP_TEMPLATE_ID:
3415     case CPP_NESTED_NAME_SPECIFIER:
3416       {
3417         tree id_expression;
3418         tree decl;
3419         const char *error_msg;
3420         bool template_p;
3421         bool done;
3422         cp_token *id_expr_token;
3423
3424       id_expression:
3425         /* Parse the id-expression.  */
3426         id_expression
3427           = cp_parser_id_expression (parser,
3428                                      /*template_keyword_p=*/false,
3429                                      /*check_dependency_p=*/true,
3430                                      &template_p,
3431                                      /*declarator_p=*/false,
3432                                      /*optional_p=*/false);
3433         if (id_expression == error_mark_node)
3434           return error_mark_node;
3435         id_expr_token = token;
3436         token = cp_lexer_peek_token (parser->lexer);
3437         done = (token->type != CPP_OPEN_SQUARE
3438                 && token->type != CPP_OPEN_PAREN
3439                 && token->type != CPP_DOT
3440                 && token->type != CPP_DEREF
3441                 && token->type != CPP_PLUS_PLUS
3442                 && token->type != CPP_MINUS_MINUS);
3443         /* If we have a template-id, then no further lookup is
3444            required.  If the template-id was for a template-class, we
3445            will sometimes have a TYPE_DECL at this point.  */
3446         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3447                  || TREE_CODE (id_expression) == TYPE_DECL)
3448           decl = id_expression;
3449         /* Look up the name.  */
3450         else
3451           {
3452             tree ambiguous_decls;
3453
3454             decl = cp_parser_lookup_name (parser, id_expression,
3455                                           none_type,
3456                                           template_p,
3457                                           /*is_namespace=*/false,
3458                                           /*check_dependency=*/true,
3459                                           &ambiguous_decls,
3460                                           id_expr_token->location);
3461             /* If the lookup was ambiguous, an error will already have
3462                been issued.  */
3463             if (ambiguous_decls)
3464               return error_mark_node;
3465
3466             /* In Objective-C++, an instance variable (ivar) may be preferred
3467                to whatever cp_parser_lookup_name() found.  */
3468             decl = objc_lookup_ivar (decl, id_expression);
3469
3470             /* If name lookup gives us a SCOPE_REF, then the
3471                qualifying scope was dependent.  */
3472             if (TREE_CODE (decl) == SCOPE_REF)
3473               {
3474                 /* At this point, we do not know if DECL is a valid
3475                    integral constant expression.  We assume that it is
3476                    in fact such an expression, so that code like:
3477
3478                       template <int N> struct A {
3479                         int a[B<N>::i];
3480                       };
3481                      
3482                    is accepted.  At template-instantiation time, we
3483                    will check that B<N>::i is actually a constant.  */
3484                 return decl;
3485               }
3486             /* Check to see if DECL is a local variable in a context
3487                where that is forbidden.  */
3488             if (parser->local_variables_forbidden_p
3489                 && local_variable_p (decl))
3490               {
3491                 /* It might be that we only found DECL because we are
3492                    trying to be generous with pre-ISO scoping rules.
3493                    For example, consider:
3494
3495                      int i;
3496                      void g() {
3497                        for (int i = 0; i < 10; ++i) {}
3498                        extern void f(int j = i);
3499                      }
3500
3501                    Here, name look up will originally find the out
3502                    of scope `i'.  We need to issue a warning message,
3503                    but then use the global `i'.  */
3504                 decl = check_for_out_of_scope_variable (decl);
3505                 if (local_variable_p (decl))
3506                   {
3507                     error ("%Hlocal variable %qD may not appear in this context",
3508                            &id_expr_token->location, decl);
3509                     return error_mark_node;
3510                   }
3511               }
3512           }
3513
3514         decl = (finish_id_expression
3515                 (id_expression, decl, parser->scope,
3516                  idk,
3517                  parser->integral_constant_expression_p,
3518                  parser->allow_non_integral_constant_expression_p,
3519                  &parser->non_integral_constant_expression_p,
3520                  template_p, done, address_p,
3521                  template_arg_p,
3522                  &error_msg,
3523                  id_expr_token->location));
3524         if (error_msg)
3525           cp_parser_error (parser, error_msg);
3526         return decl;
3527       }
3528
3529       /* Anything else is an error.  */
3530     default:
3531       /* ...unless we have an Objective-C++ message or string literal,
3532          that is.  */
3533       if (c_dialect_objc ()
3534           && (token->type == CPP_OPEN_SQUARE
3535               || token->type == CPP_OBJC_STRING))
3536         return cp_parser_objc_expression (parser);
3537
3538       cp_parser_error (parser, "expected primary-expression");
3539       return error_mark_node;
3540     }
3541 }
3542
3543 /* Parse an id-expression.
3544
3545    id-expression:
3546      unqualified-id
3547      qualified-id
3548
3549    qualified-id:
3550      :: [opt] nested-name-specifier template [opt] unqualified-id
3551      :: identifier
3552      :: operator-function-id
3553      :: template-id
3554
3555    Return a representation of the unqualified portion of the
3556    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3557    a `::' or nested-name-specifier.
3558
3559    Often, if the id-expression was a qualified-id, the caller will
3560    want to make a SCOPE_REF to represent the qualified-id.  This
3561    function does not do this in order to avoid wastefully creating
3562    SCOPE_REFs when they are not required.
3563
3564    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3565    `template' keyword.
3566
3567    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3568    uninstantiated templates.
3569
3570    If *TEMPLATE_P is non-NULL, it is set to true iff the
3571    `template' keyword is used to explicitly indicate that the entity
3572    named is a template.
3573
3574    If DECLARATOR_P is true, the id-expression is appearing as part of
3575    a declarator, rather than as part of an expression.  */
3576
3577 static tree
3578 cp_parser_id_expression (cp_parser *parser,
3579                          bool template_keyword_p,
3580                          bool check_dependency_p,
3581                          bool *template_p,
3582                          bool declarator_p,
3583                          bool optional_p)
3584 {
3585   bool global_scope_p;
3586   bool nested_name_specifier_p;
3587
3588   /* Assume the `template' keyword was not used.  */
3589   if (template_p)
3590     *template_p = template_keyword_p;
3591
3592   /* Look for the optional `::' operator.  */
3593   global_scope_p
3594     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3595        != NULL_TREE);
3596   /* Look for the optional nested-name-specifier.  */
3597   nested_name_specifier_p
3598     = (cp_parser_nested_name_specifier_opt (parser,
3599                                             /*typename_keyword_p=*/false,
3600                                             check_dependency_p,
3601                                             /*type_p=*/false,
3602                                             declarator_p)
3603        != NULL_TREE);
3604   /* If there is a nested-name-specifier, then we are looking at
3605      the first qualified-id production.  */
3606   if (nested_name_specifier_p)
3607     {
3608       tree saved_scope;
3609       tree saved_object_scope;
3610       tree saved_qualifying_scope;
3611       tree unqualified_id;
3612       bool is_template;
3613
3614       /* See if the next token is the `template' keyword.  */
3615       if (!template_p)
3616         template_p = &is_template;
3617       *template_p = cp_parser_optional_template_keyword (parser);
3618       /* Name lookup we do during the processing of the
3619          unqualified-id might obliterate SCOPE.  */
3620       saved_scope = parser->scope;
3621       saved_object_scope = parser->object_scope;
3622       saved_qualifying_scope = parser->qualifying_scope;
3623       /* Process the final unqualified-id.  */
3624       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3625                                                  check_dependency_p,
3626                                                  declarator_p,
3627                                                  /*optional_p=*/false);
3628       /* Restore the SAVED_SCOPE for our caller.  */
3629       parser->scope = saved_scope;
3630       parser->object_scope = saved_object_scope;
3631       parser->qualifying_scope = saved_qualifying_scope;
3632
3633       return unqualified_id;
3634     }
3635   /* Otherwise, if we are in global scope, then we are looking at one
3636      of the other qualified-id productions.  */
3637   else if (global_scope_p)
3638     {
3639       cp_token *token;
3640       tree id;
3641
3642       /* Peek at the next token.  */
3643       token = cp_lexer_peek_token (parser->lexer);
3644
3645       /* If it's an identifier, and the next token is not a "<", then
3646          we can avoid the template-id case.  This is an optimization
3647          for this common case.  */
3648       if (token->type == CPP_NAME
3649           && !cp_parser_nth_token_starts_template_argument_list_p
3650                (parser, 2))
3651         return cp_parser_identifier (parser);
3652
3653       cp_parser_parse_tentatively (parser);
3654       /* Try a template-id.  */
3655       id = cp_parser_template_id (parser,
3656                                   /*template_keyword_p=*/false,
3657                                   /*check_dependency_p=*/true,
3658                                   declarator_p);
3659       /* If that worked, we're done.  */
3660       if (cp_parser_parse_definitely (parser))
3661         return id;
3662
3663       /* Peek at the next token.  (Changes in the token buffer may
3664          have invalidated the pointer obtained above.)  */
3665       token = cp_lexer_peek_token (parser->lexer);
3666
3667       switch (token->type)
3668         {
3669         case CPP_NAME:
3670           return cp_parser_identifier (parser);
3671
3672         case CPP_KEYWORD:
3673           if (token->keyword == RID_OPERATOR)
3674             return cp_parser_operator_function_id (parser);
3675           /* Fall through.  */
3676
3677         default:
3678           cp_parser_error (parser, "expected id-expression");
3679           return error_mark_node;
3680         }
3681     }
3682   else
3683     return cp_parser_unqualified_id (parser, template_keyword_p,
3684                                      /*check_dependency_p=*/true,
3685                                      declarator_p,
3686                                      optional_p);
3687 }
3688
3689 /* Parse an unqualified-id.
3690
3691    unqualified-id:
3692      identifier
3693      operator-function-id
3694      conversion-function-id
3695      ~ class-name
3696      template-id
3697
3698    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3699    keyword, in a construct like `A::template ...'.
3700
3701    Returns a representation of unqualified-id.  For the `identifier'
3702    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3703    production a BIT_NOT_EXPR is returned; the operand of the
3704    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3705    other productions, see the documentation accompanying the
3706    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3707    names are looked up in uninstantiated templates.  If DECLARATOR_P
3708    is true, the unqualified-id is appearing as part of a declarator,
3709    rather than as part of an expression.  */
3710
3711 static tree
3712 cp_parser_unqualified_id (cp_parser* parser,
3713                           bool template_keyword_p,
3714                           bool check_dependency_p,
3715                           bool declarator_p,
3716                           bool optional_p)
3717 {
3718   cp_token *token;
3719
3720   /* Peek at the next token.  */
3721   token = cp_lexer_peek_token (parser->lexer);
3722
3723   switch (token->type)
3724     {
3725     case CPP_NAME:
3726       {
3727         tree id;
3728
3729         /* We don't know yet whether or not this will be a
3730            template-id.  */
3731         cp_parser_parse_tentatively (parser);
3732         /* Try a template-id.  */
3733         id = cp_parser_template_id (parser, template_keyword_p,
3734                                     check_dependency_p,
3735                                     declarator_p);
3736         /* If it worked, we're done.  */
3737         if (cp_parser_parse_definitely (parser))
3738           return id;
3739         /* Otherwise, it's an ordinary identifier.  */
3740         return cp_parser_identifier (parser);
3741       }
3742
3743     case CPP_TEMPLATE_ID:
3744       return cp_parser_template_id (parser, template_keyword_p,
3745                                     check_dependency_p,
3746                                     declarator_p);
3747
3748     case CPP_COMPL:
3749       {
3750         tree type_decl;
3751         tree qualifying_scope;
3752         tree object_scope;
3753         tree scope;
3754         bool done;
3755
3756         /* Consume the `~' token.  */
3757         cp_lexer_consume_token (parser->lexer);
3758         /* Parse the class-name.  The standard, as written, seems to
3759            say that:
3760
3761              template <typename T> struct S { ~S (); };
3762              template <typename T> S<T>::~S() {}
3763
3764            is invalid, since `~' must be followed by a class-name, but
3765            `S<T>' is dependent, and so not known to be a class.
3766            That's not right; we need to look in uninstantiated
3767            templates.  A further complication arises from:
3768
3769              template <typename T> void f(T t) {
3770                t.T::~T();
3771              }
3772
3773            Here, it is not possible to look up `T' in the scope of `T'
3774            itself.  We must look in both the current scope, and the
3775            scope of the containing complete expression.
3776
3777            Yet another issue is:
3778
3779              struct S {
3780                int S;
3781                ~S();
3782              };
3783
3784              S::~S() {}
3785
3786            The standard does not seem to say that the `S' in `~S'
3787            should refer to the type `S' and not the data member
3788            `S::S'.  */
3789
3790         /* DR 244 says that we look up the name after the "~" in the
3791            same scope as we looked up the qualifying name.  That idea
3792            isn't fully worked out; it's more complicated than that.  */
3793         scope = parser->scope;
3794         object_scope = parser->object_scope;
3795         qualifying_scope = parser->qualifying_scope;
3796
3797         /* Check for invalid scopes.  */
3798         if (scope == error_mark_node)
3799           {
3800             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3801               cp_lexer_consume_token (parser->lexer);
3802             return error_mark_node;
3803           }
3804         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3805           {
3806             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3807               error ("%Hscope %qT before %<~%> is not a class-name",
3808                      &token->location, scope);
3809             cp_parser_simulate_error (parser);
3810             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3811               cp_lexer_consume_token (parser->lexer);
3812             return error_mark_node;
3813           }
3814         gcc_assert (!scope || TYPE_P (scope));
3815
3816         /* If the name is of the form "X::~X" it's OK.  */
3817         token = cp_lexer_peek_token (parser->lexer);
3818         if (scope
3819             && token->type == CPP_NAME
3820             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3821                 == CPP_OPEN_PAREN)
3822             && constructor_name_p (token->u.value, scope))
3823           {
3824             cp_lexer_consume_token (parser->lexer);
3825             return build_nt (BIT_NOT_EXPR, scope);
3826           }
3827
3828         /* If there was an explicit qualification (S::~T), first look
3829            in the scope given by the qualification (i.e., S).  */
3830         done = false;
3831         type_decl = NULL_TREE;
3832         if (scope)
3833           {
3834             cp_parser_parse_tentatively (parser);
3835             type_decl = cp_parser_class_name (parser,
3836                                               /*typename_keyword_p=*/false,
3837                                               /*template_keyword_p=*/false,
3838                                               none_type,
3839                                               /*check_dependency=*/false,
3840                                               /*class_head_p=*/false,
3841                                               declarator_p);
3842             if (cp_parser_parse_definitely (parser))
3843               done = true;
3844           }
3845         /* In "N::S::~S", look in "N" as well.  */
3846         if (!done && scope && qualifying_scope)
3847           {
3848             cp_parser_parse_tentatively (parser);
3849             parser->scope = qualifying_scope;
3850             parser->object_scope = NULL_TREE;
3851             parser->qualifying_scope = NULL_TREE;
3852             type_decl
3853               = cp_parser_class_name (parser,
3854                                       /*typename_keyword_p=*/false,
3855                                       /*template_keyword_p=*/false,
3856                                       none_type,
3857                                       /*check_dependency=*/false,
3858                                       /*class_head_p=*/false,
3859                                       declarator_p);
3860             if (cp_parser_parse_definitely (parser))
3861               done = true;
3862           }
3863         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3864         else if (!done && object_scope)
3865           {
3866             cp_parser_parse_tentatively (parser);
3867             parser->scope = object_scope;
3868             parser->object_scope = NULL_TREE;
3869             parser->qualifying_scope = NULL_TREE;
3870             type_decl
3871               = cp_parser_class_name (parser,
3872                                       /*typename_keyword_p=*/false,
3873                                       /*template_keyword_p=*/false,
3874                                       none_type,
3875                                       /*check_dependency=*/false,
3876                                       /*class_head_p=*/false,
3877                                       declarator_p);
3878             if (cp_parser_parse_definitely (parser))
3879               done = true;
3880           }
3881         /* Look in the surrounding context.  */
3882         if (!done)
3883           {
3884             parser->scope = NULL_TREE;
3885             parser->object_scope = NULL_TREE;
3886             parser->qualifying_scope = NULL_TREE;
3887             if (processing_template_decl)
3888               cp_parser_parse_tentatively (parser);
3889             type_decl
3890               = cp_parser_class_name (parser,
3891                                       /*typename_keyword_p=*/false,
3892                                       /*template_keyword_p=*/false,
3893                                       none_type,
3894                                       /*check_dependency=*/false,
3895                                       /*class_head_p=*/false,
3896                                       declarator_p);
3897             if (processing_template_decl
3898                 && ! cp_parser_parse_definitely (parser))
3899               {
3900                 /* We couldn't find a type with this name, so just accept
3901                    it and check for a match at instantiation time.  */
3902                 type_decl = cp_parser_identifier (parser);
3903                 if (type_decl != error_mark_node)
3904                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3905                 return type_decl;
3906               }
3907           }
3908         /* If an error occurred, assume that the name of the
3909            destructor is the same as the name of the qualifying
3910            class.  That allows us to keep parsing after running
3911            into ill-formed destructor names.  */
3912         if (type_decl == error_mark_node && scope)
3913           return build_nt (BIT_NOT_EXPR, scope);
3914         else if (type_decl == error_mark_node)
3915           return error_mark_node;
3916
3917         /* Check that destructor name and scope match.  */
3918         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3919           {
3920             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3921               error ("%Hdeclaration of %<~%T%> as member of %qT",
3922                      &token->location, type_decl, scope);
3923             cp_parser_simulate_error (parser);
3924             return error_mark_node;
3925           }
3926
3927         /* [class.dtor]
3928
3929            A typedef-name that names a class shall not be used as the
3930            identifier in the declarator for a destructor declaration.  */
3931         if (declarator_p
3932             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3933             && !DECL_SELF_REFERENCE_P (type_decl)
3934             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3935           error ("%Htypedef-name %qD used as destructor declarator",
3936                  &token->location, type_decl);
3937
3938         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3939       }
3940
3941     case CPP_KEYWORD:
3942       if (token->keyword == RID_OPERATOR)
3943         {
3944           tree id;
3945
3946           /* This could be a template-id, so we try that first.  */
3947           cp_parser_parse_tentatively (parser);
3948           /* Try a template-id.  */
3949           id = cp_parser_template_id (parser, template_keyword_p,
3950                                       /*check_dependency_p=*/true,
3951                                       declarator_p);
3952           /* If that worked, we're done.  */
3953           if (cp_parser_parse_definitely (parser))
3954             return id;
3955           /* We still don't know whether we're looking at an
3956              operator-function-id or a conversion-function-id.  */
3957           cp_parser_parse_tentatively (parser);
3958           /* Try an operator-function-id.  */
3959           id = cp_parser_operator_function_id (parser);
3960           /* If that didn't work, try a conversion-function-id.  */
3961           if (!cp_parser_parse_definitely (parser))
3962             id = cp_parser_conversion_function_id (parser);
3963
3964           return id;
3965         }
3966       /* Fall through.  */
3967
3968     default:
3969       if (optional_p)
3970         return NULL_TREE;
3971       cp_parser_error (parser, "expected unqualified-id");
3972       return error_mark_node;
3973     }
3974 }
3975
3976 /* Parse an (optional) nested-name-specifier.
3977
3978    nested-name-specifier: [C++98]
3979      class-or-namespace-name :: nested-name-specifier [opt]
3980      class-or-namespace-name :: template nested-name-specifier [opt]
3981
3982    nested-name-specifier: [C++0x]
3983      type-name ::
3984      namespace-name ::
3985      nested-name-specifier identifier ::
3986      nested-name-specifier template [opt] simple-template-id ::
3987
3988    PARSER->SCOPE should be set appropriately before this function is
3989    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3990    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3991    in name lookups.
3992
3993    Sets PARSER->SCOPE to the class (TYPE) or namespace
3994    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3995    it unchanged if there is no nested-name-specifier.  Returns the new
3996    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3997
3998    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3999    part of a declaration and/or decl-specifier.  */
4000
4001 static tree
4002 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4003                                      bool typename_keyword_p,
4004                                      bool check_dependency_p,
4005                                      bool type_p,
4006                                      bool is_declaration)
4007 {
4008   bool success = false;
4009   cp_token_position start = 0;
4010   cp_token *token;
4011
4012   /* Remember where the nested-name-specifier starts.  */
4013   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4014     {
4015       start = cp_lexer_token_position (parser->lexer, false);
4016       push_deferring_access_checks (dk_deferred);
4017     }
4018
4019   while (true)
4020     {
4021       tree new_scope;
4022       tree old_scope;
4023       tree saved_qualifying_scope;
4024       bool template_keyword_p;
4025
4026       /* Spot cases that cannot be the beginning of a
4027          nested-name-specifier.  */
4028       token = cp_lexer_peek_token (parser->lexer);
4029
4030       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4031          the already parsed nested-name-specifier.  */
4032       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4033         {
4034           /* Grab the nested-name-specifier and continue the loop.  */
4035           cp_parser_pre_parsed_nested_name_specifier (parser);
4036           /* If we originally encountered this nested-name-specifier
4037              with IS_DECLARATION set to false, we will not have
4038              resolved TYPENAME_TYPEs, so we must do so here.  */
4039           if (is_declaration
4040               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4041             {
4042               new_scope = resolve_typename_type (parser->scope,
4043                                                  /*only_current_p=*/false);
4044               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4045                 parser->scope = new_scope;
4046             }
4047           success = true;
4048           continue;
4049         }
4050
4051       /* Spot cases that cannot be the beginning of a
4052          nested-name-specifier.  On the second and subsequent times
4053          through the loop, we look for the `template' keyword.  */
4054       if (success && token->keyword == RID_TEMPLATE)
4055         ;
4056       /* A template-id can start a nested-name-specifier.  */
4057       else if (token->type == CPP_TEMPLATE_ID)
4058         ;
4059       else
4060         {
4061           /* If the next token is not an identifier, then it is
4062              definitely not a type-name or namespace-name.  */
4063           if (token->type != CPP_NAME)
4064             break;
4065           /* If the following token is neither a `<' (to begin a
4066              template-id), nor a `::', then we are not looking at a
4067              nested-name-specifier.  */
4068           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4069           if (token->type != CPP_SCOPE
4070               && !cp_parser_nth_token_starts_template_argument_list_p
4071                   (parser, 2))
4072             break;
4073         }
4074
4075       /* The nested-name-specifier is optional, so we parse
4076          tentatively.  */
4077       cp_parser_parse_tentatively (parser);
4078
4079       /* Look for the optional `template' keyword, if this isn't the
4080          first time through the loop.  */
4081       if (success)
4082         template_keyword_p = cp_parser_optional_template_keyword (parser);
4083       else
4084         template_keyword_p = false;
4085
4086       /* Save the old scope since the name lookup we are about to do
4087          might destroy it.  */
4088       old_scope = parser->scope;
4089       saved_qualifying_scope = parser->qualifying_scope;
4090       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4091          look up names in "X<T>::I" in order to determine that "Y" is
4092          a template.  So, if we have a typename at this point, we make
4093          an effort to look through it.  */
4094       if (is_declaration
4095           && !typename_keyword_p
4096           && parser->scope
4097           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4098         parser->scope = resolve_typename_type (parser->scope,
4099                                                /*only_current_p=*/false);
4100       /* Parse the qualifying entity.  */
4101       new_scope
4102         = cp_parser_qualifying_entity (parser,
4103                                        typename_keyword_p,
4104                                        template_keyword_p,
4105                                        check_dependency_p,
4106                                        type_p,
4107                                        is_declaration);
4108       /* Look for the `::' token.  */
4109       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4110
4111       /* If we found what we wanted, we keep going; otherwise, we're
4112          done.  */
4113       if (!cp_parser_parse_definitely (parser))
4114         {
4115           bool error_p = false;
4116
4117           /* Restore the OLD_SCOPE since it was valid before the
4118              failed attempt at finding the last
4119              class-or-namespace-name.  */
4120           parser->scope = old_scope;
4121           parser->qualifying_scope = saved_qualifying_scope;
4122           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4123             break;
4124           /* If the next token is an identifier, and the one after
4125              that is a `::', then any valid interpretation would have
4126              found a class-or-namespace-name.  */
4127           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4128                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4129                      == CPP_SCOPE)
4130                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4131                      != CPP_COMPL))
4132             {
4133               token = cp_lexer_consume_token (parser->lexer);
4134               if (!error_p)
4135                 {
4136                   if (!token->ambiguous_p)
4137                     {
4138                       tree decl;
4139                       tree ambiguous_decls;
4140
4141                       decl = cp_parser_lookup_name (parser, token->u.value,
4142                                                     none_type,
4143                                                     /*is_template=*/false,
4144                                                     /*is_namespace=*/false,
4145                                                     /*check_dependency=*/true,
4146                                                     &ambiguous_decls,
4147                                                     token->location);
4148                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4149                         error ("%H%qD used without template parameters",
4150                                &token->location, decl);
4151                       else if (ambiguous_decls)
4152                         {
4153                           error ("%Hreference to %qD is ambiguous",
4154                                  &token->location, token->u.value);
4155                           print_candidates (ambiguous_decls);
4156                           decl = error_mark_node;
4157                         }
4158                       else
4159                         {
4160                           const char* msg = "is not a class or namespace";
4161                           if (cxx_dialect != cxx98)
4162                             msg = "is not a class, namespace, or enumeration";
4163                           cp_parser_name_lookup_error
4164                             (parser, token->u.value, decl, msg,
4165                              token->location);
4166                         }
4167                     }
4168                   parser->scope = error_mark_node;
4169                   error_p = true;
4170                   /* Treat this as a successful nested-name-specifier
4171                      due to:
4172
4173                      [basic.lookup.qual]
4174
4175                      If the name found is not a class-name (clause
4176                      _class_) or namespace-name (_namespace.def_), the
4177                      program is ill-formed.  */
4178                   success = true;
4179                 }
4180               cp_lexer_consume_token (parser->lexer);
4181             }
4182           break;
4183         }
4184       /* We've found one valid nested-name-specifier.  */
4185       success = true;
4186       /* Name lookup always gives us a DECL.  */
4187       if (TREE_CODE (new_scope) == TYPE_DECL)
4188         new_scope = TREE_TYPE (new_scope);
4189       /* Uses of "template" must be followed by actual templates.  */
4190       if (template_keyword_p
4191           && !(CLASS_TYPE_P (new_scope)
4192                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4193                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4194                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4195           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4196                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4197                    == TEMPLATE_ID_EXPR)))
4198         permerror (input_location, TYPE_P (new_scope)
4199                    ? "%qT is not a template"
4200                    : "%qD is not a template",
4201                    new_scope);
4202       /* If it is a class scope, try to complete it; we are about to
4203          be looking up names inside the class.  */
4204       if (TYPE_P (new_scope)
4205           /* Since checking types for dependency can be expensive,
4206              avoid doing it if the type is already complete.  */
4207           && !COMPLETE_TYPE_P (new_scope)
4208           /* Do not try to complete dependent types.  */
4209           && !dependent_type_p (new_scope))
4210         {
4211           new_scope = complete_type (new_scope);
4212           /* If it is a typedef to current class, use the current
4213              class instead, as the typedef won't have any names inside
4214              it yet.  */
4215           if (!COMPLETE_TYPE_P (new_scope)
4216               && currently_open_class (new_scope))
4217             new_scope = TYPE_MAIN_VARIANT (new_scope);
4218         }
4219       /* Make sure we look in the right scope the next time through
4220          the loop.  */
4221       parser->scope = new_scope;
4222     }
4223
4224   /* If parsing tentatively, replace the sequence of tokens that makes
4225      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4226      token.  That way, should we re-parse the token stream, we will
4227      not have to repeat the effort required to do the parse, nor will
4228      we issue duplicate error messages.  */
4229   if (success && start)
4230     {
4231       cp_token *token;
4232
4233       token = cp_lexer_token_at (parser->lexer, start);
4234       /* Reset the contents of the START token.  */
4235       token->type = CPP_NESTED_NAME_SPECIFIER;
4236       /* Retrieve any deferred checks.  Do not pop this access checks yet
4237          so the memory will not be reclaimed during token replacing below.  */
4238       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4239       token->u.tree_check_value->value = parser->scope;
4240       token->u.tree_check_value->checks = get_deferred_access_checks ();
4241       token->u.tree_check_value->qualifying_scope =
4242         parser->qualifying_scope;
4243       token->keyword = RID_MAX;
4244
4245       /* Purge all subsequent tokens.  */
4246       cp_lexer_purge_tokens_after (parser->lexer, start);
4247     }
4248
4249   if (start)
4250     pop_to_parent_deferring_access_checks ();
4251
4252   return success ? parser->scope : NULL_TREE;
4253 }
4254
4255 /* Parse a nested-name-specifier.  See
4256    cp_parser_nested_name_specifier_opt for details.  This function
4257    behaves identically, except that it will an issue an error if no
4258    nested-name-specifier is present.  */
4259
4260 static tree
4261 cp_parser_nested_name_specifier (cp_parser *parser,
4262                                  bool typename_keyword_p,
4263                                  bool check_dependency_p,
4264                                  bool type_p,
4265                                  bool is_declaration)
4266 {
4267   tree scope;
4268
4269   /* Look for the nested-name-specifier.  */
4270   scope = cp_parser_nested_name_specifier_opt (parser,
4271                                                typename_keyword_p,
4272                                                check_dependency_p,
4273                                                type_p,
4274                                                is_declaration);
4275   /* If it was not present, issue an error message.  */
4276   if (!scope)
4277     {
4278       cp_parser_error (parser, "expected nested-name-specifier");
4279       parser->scope = NULL_TREE;
4280     }
4281
4282   return scope;
4283 }
4284
4285 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4286    this is either a class-name or a namespace-name (which corresponds
4287    to the class-or-namespace-name production in the grammar). For
4288    C++0x, it can also be a type-name that refers to an enumeration
4289    type.
4290
4291    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4292    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4293    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4294    TYPE_P is TRUE iff the next name should be taken as a class-name,
4295    even the same name is declared to be another entity in the same
4296    scope.
4297
4298    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4299    specified by the class-or-namespace-name.  If neither is found the
4300    ERROR_MARK_NODE is returned.  */
4301
4302 static tree
4303 cp_parser_qualifying_entity (cp_parser *parser,
4304                              bool typename_keyword_p,
4305                              bool template_keyword_p,
4306                              bool check_dependency_p,
4307                              bool type_p,
4308                              bool is_declaration)
4309 {
4310   tree saved_scope;
4311   tree saved_qualifying_scope;
4312   tree saved_object_scope;
4313   tree scope;
4314   bool only_class_p;
4315   bool successful_parse_p;
4316
4317   /* Before we try to parse the class-name, we must save away the
4318      current PARSER->SCOPE since cp_parser_class_name will destroy
4319      it.  */
4320   saved_scope = parser->scope;
4321   saved_qualifying_scope = parser->qualifying_scope;
4322   saved_object_scope = parser->object_scope;
4323   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4324      there is no need to look for a namespace-name.  */
4325   only_class_p = template_keyword_p 
4326     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4327   if (!only_class_p)
4328     cp_parser_parse_tentatively (parser);
4329   scope = cp_parser_class_name (parser,
4330                                 typename_keyword_p,
4331                                 template_keyword_p,
4332                                 type_p ? class_type : none_type,
4333                                 check_dependency_p,
4334                                 /*class_head_p=*/false,
4335                                 is_declaration);
4336   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4337   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4338   if (!only_class_p 
4339       && cxx_dialect != cxx98
4340       && !successful_parse_p)
4341     {
4342       /* Restore the saved scope.  */
4343       parser->scope = saved_scope;
4344       parser->qualifying_scope = saved_qualifying_scope;
4345       parser->object_scope = saved_object_scope;
4346
4347       /* Parse tentatively.  */
4348       cp_parser_parse_tentatively (parser);
4349      
4350       /* Parse a typedef-name or enum-name.  */
4351       scope = cp_parser_nonclass_name (parser);
4352       successful_parse_p = cp_parser_parse_definitely (parser);
4353     }
4354   /* If that didn't work, try for a namespace-name.  */
4355   if (!only_class_p && !successful_parse_p)
4356     {
4357       /* Restore the saved scope.  */
4358       parser->scope = saved_scope;
4359       parser->qualifying_scope = saved_qualifying_scope;
4360       parser->object_scope = saved_object_scope;
4361       /* If we are not looking at an identifier followed by the scope
4362          resolution operator, then this is not part of a
4363          nested-name-specifier.  (Note that this function is only used
4364          to parse the components of a nested-name-specifier.)  */
4365       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4366           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4367         return error_mark_node;
4368       scope = cp_parser_namespace_name (parser);
4369     }
4370
4371   return scope;
4372 }
4373
4374 /* Parse a postfix-expression.
4375
4376    postfix-expression:
4377      primary-expression
4378      postfix-expression [ expression ]
4379      postfix-expression ( expression-list [opt] )
4380      simple-type-specifier ( expression-list [opt] )
4381      typename :: [opt] nested-name-specifier identifier
4382        ( expression-list [opt] )
4383      typename :: [opt] nested-name-specifier template [opt] template-id
4384        ( expression-list [opt] )
4385      postfix-expression . template [opt] id-expression
4386      postfix-expression -> template [opt] id-expression
4387      postfix-expression . pseudo-destructor-name
4388      postfix-expression -> pseudo-destructor-name
4389      postfix-expression ++
4390      postfix-expression --
4391      dynamic_cast < type-id > ( expression )
4392      static_cast < type-id > ( expression )
4393      reinterpret_cast < type-id > ( expression )
4394      const_cast < type-id > ( expression )
4395      typeid ( expression )
4396      typeid ( type-id )
4397
4398    GNU Extension:
4399
4400    postfix-expression:
4401      ( type-id ) { initializer-list , [opt] }
4402
4403    This extension is a GNU version of the C99 compound-literal
4404    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4405    but they are essentially the same concept.)
4406
4407    If ADDRESS_P is true, the postfix expression is the operand of the
4408    `&' operator.  CAST_P is true if this expression is the target of a
4409    cast.
4410
4411    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4412    class member access expressions [expr.ref].
4413
4414    Returns a representation of the expression.  */
4415
4416 static tree
4417 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4418                               bool member_access_only_p,
4419                               cp_id_kind * pidk_return)
4420 {
4421   cp_token *token;
4422   enum rid keyword;
4423   cp_id_kind idk = CP_ID_KIND_NONE;
4424   tree postfix_expression = NULL_TREE;
4425   bool is_member_access = false;
4426
4427   /* Peek at the next token.  */
4428   token = cp_lexer_peek_token (parser->lexer);
4429   /* Some of the productions are determined by keywords.  */
4430   keyword = token->keyword;
4431   switch (keyword)
4432     {
4433     case RID_DYNCAST:
4434     case RID_STATCAST:
4435     case RID_REINTCAST:
4436     case RID_CONSTCAST:
4437       {
4438         tree type;
4439         tree expression;
4440         const char *saved_message;
4441
4442         /* All of these can be handled in the same way from the point
4443            of view of parsing.  Begin by consuming the token
4444            identifying the cast.  */
4445         cp_lexer_consume_token (parser->lexer);
4446
4447         /* New types cannot be defined in the cast.  */
4448         saved_message = parser->type_definition_forbidden_message;
4449         parser->type_definition_forbidden_message
4450           = "types may not be defined in casts";
4451
4452         /* Look for the opening `<'.  */
4453         cp_parser_require (parser, CPP_LESS, "%<<%>");
4454         /* Parse the type to which we are casting.  */
4455         type = cp_parser_type_id (parser);
4456         /* Look for the closing `>'.  */
4457         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4458         /* Restore the old message.  */
4459         parser->type_definition_forbidden_message = saved_message;
4460
4461         /* And the expression which is being cast.  */
4462         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4463         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4464         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4465
4466         /* Only type conversions to integral or enumeration types
4467            can be used in constant-expressions.  */
4468         if (!cast_valid_in_integral_constant_expression_p (type)
4469             && (cp_parser_non_integral_constant_expression
4470                 (parser,
4471                  "a cast to a type other than an integral or "
4472                  "enumeration type")))
4473           return error_mark_node;
4474
4475         switch (keyword)
4476           {
4477           case RID_DYNCAST:
4478             postfix_expression
4479               = build_dynamic_cast (type, expression, tf_warning_or_error);
4480             break;
4481           case RID_STATCAST:
4482             postfix_expression
4483               = build_static_cast (type, expression, tf_warning_or_error);
4484             break;
4485           case RID_REINTCAST:
4486             postfix_expression
4487               = build_reinterpret_cast (type, expression, 
4488                                         tf_warning_or_error);
4489             break;
4490           case RID_CONSTCAST:
4491             postfix_expression
4492               = build_const_cast (type, expression, tf_warning_or_error);
4493             break;
4494           default:
4495             gcc_unreachable ();
4496           }
4497       }
4498       break;
4499
4500     case RID_TYPEID:
4501       {
4502         tree type;
4503         const char *saved_message;
4504         bool saved_in_type_id_in_expr_p;
4505
4506         /* Consume the `typeid' token.  */
4507         cp_lexer_consume_token (parser->lexer);
4508         /* Look for the `(' token.  */
4509         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4510         /* Types cannot be defined in a `typeid' expression.  */
4511         saved_message = parser->type_definition_forbidden_message;
4512         parser->type_definition_forbidden_message
4513           = "types may not be defined in a %<typeid%> expression";
4514         /* We can't be sure yet whether we're looking at a type-id or an
4515            expression.  */
4516         cp_parser_parse_tentatively (parser);
4517         /* Try a type-id first.  */
4518         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4519         parser->in_type_id_in_expr_p = true;
4520         type = cp_parser_type_id (parser);
4521         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4522         /* Look for the `)' token.  Otherwise, we can't be sure that
4523            we're not looking at an expression: consider `typeid (int
4524            (3))', for example.  */
4525         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4526         /* If all went well, simply lookup the type-id.  */
4527         if (cp_parser_parse_definitely (parser))
4528           postfix_expression = get_typeid (type);
4529         /* Otherwise, fall back to the expression variant.  */
4530         else
4531           {
4532             tree expression;
4533
4534             /* Look for an expression.  */
4535             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4536             /* Compute its typeid.  */
4537             postfix_expression = build_typeid (expression);
4538             /* Look for the `)' token.  */
4539             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4540           }
4541         /* Restore the saved message.  */
4542         parser->type_definition_forbidden_message = saved_message;
4543         /* `typeid' may not appear in an integral constant expression.  */
4544         if (cp_parser_non_integral_constant_expression(parser,
4545                                                        "%<typeid%> operator"))
4546           return error_mark_node;
4547       }
4548       break;
4549
4550     case RID_TYPENAME:
4551       {
4552         tree type;
4553         /* The syntax permitted here is the same permitted for an
4554            elaborated-type-specifier.  */
4555         type = cp_parser_elaborated_type_specifier (parser,
4556                                                     /*is_friend=*/false,
4557                                                     /*is_declaration=*/false);
4558         postfix_expression = cp_parser_functional_cast (parser, type);
4559       }
4560       break;
4561
4562     default:
4563       {
4564         tree type;
4565
4566         /* If the next thing is a simple-type-specifier, we may be
4567            looking at a functional cast.  We could also be looking at
4568            an id-expression.  So, we try the functional cast, and if
4569            that doesn't work we fall back to the primary-expression.  */
4570         cp_parser_parse_tentatively (parser);
4571         /* Look for the simple-type-specifier.  */
4572         type = cp_parser_simple_type_specifier (parser,
4573                                                 /*decl_specs=*/NULL,
4574                                                 CP_PARSER_FLAGS_NONE);
4575         /* Parse the cast itself.  */
4576         if (!cp_parser_error_occurred (parser))
4577           postfix_expression
4578             = cp_parser_functional_cast (parser, type);
4579         /* If that worked, we're done.  */
4580         if (cp_parser_parse_definitely (parser))
4581           break;
4582
4583         /* If the functional-cast didn't work out, try a
4584            compound-literal.  */
4585         if (cp_parser_allow_gnu_extensions_p (parser)
4586             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4587           {
4588             VEC(constructor_elt,gc) *initializer_list = NULL;
4589             bool saved_in_type_id_in_expr_p;
4590
4591             cp_parser_parse_tentatively (parser);
4592             /* Consume the `('.  */
4593             cp_lexer_consume_token (parser->lexer);
4594             /* Parse the type.  */
4595             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4596             parser->in_type_id_in_expr_p = true;
4597             type = cp_parser_type_id (parser);
4598             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4599             /* Look for the `)'.  */
4600             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4601             /* Look for the `{'.  */
4602             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4603             /* If things aren't going well, there's no need to
4604                keep going.  */
4605             if (!cp_parser_error_occurred (parser))
4606               {
4607                 bool non_constant_p;
4608                 /* Parse the initializer-list.  */
4609                 initializer_list
4610                   = cp_parser_initializer_list (parser, &non_constant_p);
4611                 /* Allow a trailing `,'.  */
4612                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4613                   cp_lexer_consume_token (parser->lexer);
4614                 /* Look for the final `}'.  */
4615                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4616               }
4617             /* If that worked, we're definitely looking at a
4618                compound-literal expression.  */
4619             if (cp_parser_parse_definitely (parser))
4620               {
4621                 /* Warn the user that a compound literal is not
4622                    allowed in standard C++.  */
4623                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4624                 /* For simplicity, we disallow compound literals in
4625                    constant-expressions.  We could
4626                    allow compound literals of integer type, whose
4627                    initializer was a constant, in constant
4628                    expressions.  Permitting that usage, as a further
4629                    extension, would not change the meaning of any
4630                    currently accepted programs.  (Of course, as
4631                    compound literals are not part of ISO C++, the
4632                    standard has nothing to say.)  */
4633                 if (cp_parser_non_integral_constant_expression 
4634                     (parser, "non-constant compound literals"))
4635                   {
4636                     postfix_expression = error_mark_node;
4637                     break;
4638                   }
4639                 /* Form the representation of the compound-literal.  */
4640                 postfix_expression
4641                   = (finish_compound_literal
4642                      (type, build_constructor (init_list_type_node,
4643                                                initializer_list)));
4644                 break;
4645               }
4646           }
4647
4648         /* It must be a primary-expression.  */
4649         postfix_expression
4650           = cp_parser_primary_expression (parser, address_p, cast_p,
4651                                           /*template_arg_p=*/false,
4652                                           &idk);
4653       }
4654       break;
4655     }
4656
4657   /* Keep looping until the postfix-expression is complete.  */
4658   while (true)
4659     {
4660       if (idk == CP_ID_KIND_UNQUALIFIED
4661           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4662           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4663         /* It is not a Koenig lookup function call.  */
4664         postfix_expression
4665           = unqualified_name_lookup_error (postfix_expression);
4666
4667       /* Peek at the next token.  */
4668       token = cp_lexer_peek_token (parser->lexer);
4669
4670       switch (token->type)
4671         {
4672         case CPP_OPEN_SQUARE:
4673           postfix_expression
4674             = cp_parser_postfix_open_square_expression (parser,
4675                                                         postfix_expression,
4676                                                         false);
4677           idk = CP_ID_KIND_NONE;
4678           is_member_access = false;
4679           break;
4680
4681         case CPP_OPEN_PAREN:
4682           /* postfix-expression ( expression-list [opt] ) */
4683           {
4684             bool koenig_p;
4685             bool is_builtin_constant_p;
4686             bool saved_integral_constant_expression_p = false;
4687             bool saved_non_integral_constant_expression_p = false;
4688             VEC(tree,gc) *args;
4689
4690             is_member_access = false;
4691
4692             is_builtin_constant_p
4693               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4694             if (is_builtin_constant_p)
4695               {
4696                 /* The whole point of __builtin_constant_p is to allow
4697                    non-constant expressions to appear as arguments.  */
4698                 saved_integral_constant_expression_p
4699                   = parser->integral_constant_expression_p;
4700                 saved_non_integral_constant_expression_p
4701                   = parser->non_integral_constant_expression_p;
4702                 parser->integral_constant_expression_p = false;
4703               }
4704             args = (cp_parser_parenthesized_expression_list
4705                     (parser, /*is_attribute_list=*/false,
4706                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4707                      /*non_constant_p=*/NULL));
4708             if (is_builtin_constant_p)
4709               {
4710                 parser->integral_constant_expression_p
4711                   = saved_integral_constant_expression_p;
4712                 parser->non_integral_constant_expression_p
4713                   = saved_non_integral_constant_expression_p;
4714               }
4715
4716             if (args == NULL)
4717               {
4718                 postfix_expression = error_mark_node;
4719                 break;
4720               }
4721
4722             /* Function calls are not permitted in
4723                constant-expressions.  */
4724             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4725                 && cp_parser_non_integral_constant_expression (parser,
4726                                                                "a function call"))
4727               {
4728                 postfix_expression = error_mark_node;
4729                 release_tree_vector (args);
4730                 break;
4731               }
4732
4733             koenig_p = false;
4734             if (idk == CP_ID_KIND_UNQUALIFIED
4735                 || idk == CP_ID_KIND_TEMPLATE_ID)
4736               {
4737                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4738                   {
4739                     if (!VEC_empty (tree, args))
4740                       {
4741                         koenig_p = true;
4742                         if (!any_type_dependent_arguments_p (args))
4743                           postfix_expression
4744                             = perform_koenig_lookup (postfix_expression, args);
4745                       }
4746                     else
4747                       postfix_expression
4748                         = unqualified_fn_lookup_error (postfix_expression);
4749                   }
4750                 /* We do not perform argument-dependent lookup if
4751                    normal lookup finds a non-function, in accordance
4752                    with the expected resolution of DR 218.  */
4753                 else if (!VEC_empty (tree, args)
4754                          && is_overloaded_fn (postfix_expression))
4755                   {
4756                     tree fn = get_first_fn (postfix_expression);
4757
4758                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4759                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4760
4761                     /* Only do argument dependent lookup if regular
4762                        lookup does not find a set of member functions.
4763                        [basic.lookup.koenig]/2a  */
4764                     if (!DECL_FUNCTION_MEMBER_P (fn))
4765                       {
4766                         koenig_p = true;
4767                         if (!any_type_dependent_arguments_p (args))
4768                           postfix_expression
4769                             = perform_koenig_lookup (postfix_expression, args);
4770                       }
4771                   }
4772               }
4773
4774             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4775               {
4776                 tree instance = TREE_OPERAND (postfix_expression, 0);
4777                 tree fn = TREE_OPERAND (postfix_expression, 1);
4778
4779                 if (processing_template_decl
4780                     && (type_dependent_expression_p (instance)
4781                         || (!BASELINK_P (fn)
4782                             && TREE_CODE (fn) != FIELD_DECL)
4783                         || type_dependent_expression_p (fn)
4784                         || any_type_dependent_arguments_p (args)))
4785                   {
4786                     postfix_expression
4787                       = build_nt_call_vec (postfix_expression, args);
4788                     release_tree_vector (args);
4789                     break;
4790                   }
4791
4792                 if (BASELINK_P (fn))
4793                   {
4794                   postfix_expression
4795                     = (build_new_method_call
4796                        (instance, fn, &args, NULL_TREE,
4797                         (idk == CP_ID_KIND_QUALIFIED
4798                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4799                         /*fn_p=*/NULL,
4800                         tf_warning_or_error));
4801                   }
4802                 else
4803                   postfix_expression
4804                     = finish_call_expr (postfix_expression, &args,
4805                                         /*disallow_virtual=*/false,
4806                                         /*koenig_p=*/false,
4807                                         tf_warning_or_error);
4808               }
4809             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4810                      || TREE_CODE (postfix_expression) == MEMBER_REF
4811                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4812               postfix_expression = (build_offset_ref_call_from_tree
4813                                     (postfix_expression, &args));
4814             else if (idk == CP_ID_KIND_QUALIFIED)
4815               /* A call to a static class member, or a namespace-scope
4816                  function.  */
4817               postfix_expression
4818                 = finish_call_expr (postfix_expression, &args,
4819                                     /*disallow_virtual=*/true,
4820                                     koenig_p,
4821                                     tf_warning_or_error);
4822             else
4823               /* All other function calls.  */
4824               postfix_expression
4825                 = finish_call_expr (postfix_expression, &args,
4826                                     /*disallow_virtual=*/false,
4827                                     koenig_p,
4828                                     tf_warning_or_error);
4829
4830             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4831             idk = CP_ID_KIND_NONE;
4832
4833             release_tree_vector (args);
4834           }
4835           break;
4836
4837         case CPP_DOT:
4838         case CPP_DEREF:
4839           /* postfix-expression . template [opt] id-expression
4840              postfix-expression . pseudo-destructor-name
4841              postfix-expression -> template [opt] id-expression
4842              postfix-expression -> pseudo-destructor-name */
4843
4844           /* Consume the `.' or `->' operator.  */
4845           cp_lexer_consume_token (parser->lexer);
4846
4847           postfix_expression
4848             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4849                                                       postfix_expression,
4850                                                       false, &idk,
4851                                                       token->location);
4852
4853           is_member_access = true;
4854           break;
4855
4856         case CPP_PLUS_PLUS:
4857           /* postfix-expression ++  */
4858           /* Consume the `++' token.  */
4859           cp_lexer_consume_token (parser->lexer);
4860           /* Generate a representation for the complete expression.  */
4861           postfix_expression
4862             = finish_increment_expr (postfix_expression,
4863                                      POSTINCREMENT_EXPR);
4864           /* Increments may not appear in constant-expressions.  */
4865           if (cp_parser_non_integral_constant_expression (parser,
4866                                                           "an increment"))
4867             postfix_expression = error_mark_node;
4868           idk = CP_ID_KIND_NONE;
4869           is_member_access = false;
4870           break;
4871
4872         case CPP_MINUS_MINUS:
4873           /* postfix-expression -- */
4874           /* Consume the `--' token.  */
4875           cp_lexer_consume_token (parser->lexer);
4876           /* Generate a representation for the complete expression.  */
4877           postfix_expression
4878             = finish_increment_expr (postfix_expression,
4879                                      POSTDECREMENT_EXPR);
4880           /* Decrements may not appear in constant-expressions.  */
4881           if (cp_parser_non_integral_constant_expression (parser,
4882                                                           "a decrement"))
4883             postfix_expression = error_mark_node;
4884           idk = CP_ID_KIND_NONE;
4885           is_member_access = false;
4886           break;
4887
4888         default:
4889           if (pidk_return != NULL)
4890             * pidk_return = idk;
4891           if (member_access_only_p)
4892             return is_member_access? postfix_expression : error_mark_node;
4893           else
4894             return postfix_expression;
4895         }
4896     }
4897
4898   /* We should never get here.  */
4899   gcc_unreachable ();
4900   return error_mark_node;
4901 }
4902
4903 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4904    by cp_parser_builtin_offsetof.  We're looking for
4905
4906      postfix-expression [ expression ]
4907
4908    FOR_OFFSETOF is set if we're being called in that context, which
4909    changes how we deal with integer constant expressions.  */
4910
4911 static tree
4912 cp_parser_postfix_open_square_expression (cp_parser *parser,
4913                                           tree postfix_expression,
4914                                           bool for_offsetof)
4915 {
4916   tree index;
4917
4918   /* Consume the `[' token.  */
4919   cp_lexer_consume_token (parser->lexer);
4920
4921   /* Parse the index expression.  */
4922   /* ??? For offsetof, there is a question of what to allow here.  If
4923      offsetof is not being used in an integral constant expression context,
4924      then we *could* get the right answer by computing the value at runtime.
4925      If we are in an integral constant expression context, then we might
4926      could accept any constant expression; hard to say without analysis.
4927      Rather than open the barn door too wide right away, allow only integer
4928      constant expressions here.  */
4929   if (for_offsetof)
4930     index = cp_parser_constant_expression (parser, false, NULL);
4931   else
4932     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4933
4934   /* Look for the closing `]'.  */
4935   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4936
4937   /* Build the ARRAY_REF.  */
4938   postfix_expression = grok_array_decl (postfix_expression, index);
4939
4940   /* When not doing offsetof, array references are not permitted in
4941      constant-expressions.  */
4942   if (!for_offsetof
4943       && (cp_parser_non_integral_constant_expression
4944           (parser, "an array reference")))
4945     postfix_expression = error_mark_node;
4946
4947   return postfix_expression;
4948 }
4949
4950 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4951    by cp_parser_builtin_offsetof.  We're looking for
4952
4953      postfix-expression . template [opt] id-expression
4954      postfix-expression . pseudo-destructor-name
4955      postfix-expression -> template [opt] id-expression
4956      postfix-expression -> pseudo-destructor-name
4957
4958    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4959    limits what of the above we'll actually accept, but nevermind.
4960    TOKEN_TYPE is the "." or "->" token, which will already have been
4961    removed from the stream.  */
4962
4963 static tree
4964 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4965                                         enum cpp_ttype token_type,
4966                                         tree postfix_expression,
4967                                         bool for_offsetof, cp_id_kind *idk,
4968                                         location_t location)
4969 {
4970   tree name;
4971   bool dependent_p;
4972   bool pseudo_destructor_p;
4973   tree scope = NULL_TREE;
4974
4975   /* If this is a `->' operator, dereference the pointer.  */
4976   if (token_type == CPP_DEREF)
4977     postfix_expression = build_x_arrow (postfix_expression);
4978   /* Check to see whether or not the expression is type-dependent.  */
4979   dependent_p = type_dependent_expression_p (postfix_expression);
4980   /* The identifier following the `->' or `.' is not qualified.  */
4981   parser->scope = NULL_TREE;
4982   parser->qualifying_scope = NULL_TREE;
4983   parser->object_scope = NULL_TREE;
4984   *idk = CP_ID_KIND_NONE;
4985
4986   /* Enter the scope corresponding to the type of the object
4987      given by the POSTFIX_EXPRESSION.  */
4988   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4989     {
4990       scope = TREE_TYPE (postfix_expression);
4991       /* According to the standard, no expression should ever have
4992          reference type.  Unfortunately, we do not currently match
4993          the standard in this respect in that our internal representation
4994          of an expression may have reference type even when the standard
4995          says it does not.  Therefore, we have to manually obtain the
4996          underlying type here.  */
4997       scope = non_reference (scope);
4998       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4999       if (scope == unknown_type_node)
5000         {
5001           error ("%H%qE does not have class type", &location, postfix_expression);
5002           scope = NULL_TREE;
5003         }
5004       else
5005         scope = complete_type_or_else (scope, NULL_TREE);
5006       /* Let the name lookup machinery know that we are processing a
5007          class member access expression.  */
5008       parser->context->object_type = scope;
5009       /* If something went wrong, we want to be able to discern that case,
5010          as opposed to the case where there was no SCOPE due to the type
5011          of expression being dependent.  */
5012       if (!scope)
5013         scope = error_mark_node;
5014       /* If the SCOPE was erroneous, make the various semantic analysis
5015          functions exit quickly -- and without issuing additional error
5016          messages.  */
5017       if (scope == error_mark_node)
5018         postfix_expression = error_mark_node;
5019     }
5020
5021   /* Assume this expression is not a pseudo-destructor access.  */
5022   pseudo_destructor_p = false;
5023
5024   /* If the SCOPE is a scalar type, then, if this is a valid program,
5025      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5026      is type dependent, it can be pseudo-destructor-name or something else.
5027      Try to parse it as pseudo-destructor-name first.  */
5028   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5029     {
5030       tree s;
5031       tree type;
5032
5033       cp_parser_parse_tentatively (parser);
5034       /* Parse the pseudo-destructor-name.  */
5035       s = NULL_TREE;
5036       cp_parser_pseudo_destructor_name (parser, &s, &type);
5037       if (dependent_p
5038           && (cp_parser_error_occurred (parser)
5039               || TREE_CODE (type) != TYPE_DECL
5040               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5041         cp_parser_abort_tentative_parse (parser);
5042       else if (cp_parser_parse_definitely (parser))
5043         {
5044           pseudo_destructor_p = true;
5045           postfix_expression
5046             = finish_pseudo_destructor_expr (postfix_expression,
5047                                              s, TREE_TYPE (type));
5048         }
5049     }
5050
5051   if (!pseudo_destructor_p)
5052     {
5053       /* If the SCOPE is not a scalar type, we are looking at an
5054          ordinary class member access expression, rather than a
5055          pseudo-destructor-name.  */
5056       bool template_p;
5057       cp_token *token = cp_lexer_peek_token (parser->lexer);
5058       /* Parse the id-expression.  */
5059       name = (cp_parser_id_expression
5060               (parser,
5061                cp_parser_optional_template_keyword (parser),
5062                /*check_dependency_p=*/true,
5063                &template_p,
5064                /*declarator_p=*/false,
5065                /*optional_p=*/false));
5066       /* In general, build a SCOPE_REF if the member name is qualified.
5067          However, if the name was not dependent and has already been
5068          resolved; there is no need to build the SCOPE_REF.  For example;
5069
5070              struct X { void f(); };
5071              template <typename T> void f(T* t) { t->X::f(); }
5072
5073          Even though "t" is dependent, "X::f" is not and has been resolved
5074          to a BASELINK; there is no need to include scope information.  */
5075
5076       /* But we do need to remember that there was an explicit scope for
5077          virtual function calls.  */
5078       if (parser->scope)
5079         *idk = CP_ID_KIND_QUALIFIED;
5080
5081       /* If the name is a template-id that names a type, we will get a
5082          TYPE_DECL here.  That is invalid code.  */
5083       if (TREE_CODE (name) == TYPE_DECL)
5084         {
5085           error ("%Hinvalid use of %qD", &token->location, name);
5086           postfix_expression = error_mark_node;
5087         }
5088       else
5089         {
5090           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5091             {
5092               name = build_qualified_name (/*type=*/NULL_TREE,
5093                                            parser->scope,
5094                                            name,
5095                                            template_p);
5096               parser->scope = NULL_TREE;
5097               parser->qualifying_scope = NULL_TREE;
5098               parser->object_scope = NULL_TREE;
5099             }
5100           if (scope && name && BASELINK_P (name))
5101             adjust_result_of_qualified_name_lookup
5102               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5103           postfix_expression
5104             = finish_class_member_access_expr (postfix_expression, name,
5105                                                template_p, 
5106                                                tf_warning_or_error);
5107         }
5108     }
5109
5110   /* We no longer need to look up names in the scope of the object on
5111      the left-hand side of the `.' or `->' operator.  */
5112   parser->context->object_type = NULL_TREE;
5113
5114   /* Outside of offsetof, these operators may not appear in
5115      constant-expressions.  */
5116   if (!for_offsetof
5117       && (cp_parser_non_integral_constant_expression
5118           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5119     postfix_expression = error_mark_node;
5120
5121   return postfix_expression;
5122 }
5123
5124 /* Parse a parenthesized expression-list.
5125
5126    expression-list:
5127      assignment-expression
5128      expression-list, assignment-expression
5129
5130    attribute-list:
5131      expression-list
5132      identifier
5133      identifier, expression-list
5134
5135    CAST_P is true if this expression is the target of a cast.
5136
5137    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5138    argument pack.
5139
5140    Returns a vector of trees.  Each element is a representation of an
5141    assignment-expression.  NULL is returned if the ( and or ) are
5142    missing.  An empty, but allocated, vector is returned on no
5143    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5144    if this is really an attribute list being parsed.  If
5145    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5146    not all of the expressions in the list were constant.  */
5147
5148 static VEC(tree,gc) *
5149 cp_parser_parenthesized_expression_list (cp_parser* parser,
5150                                          bool is_attribute_list,
5151                                          bool cast_p,
5152                                          bool allow_expansion_p,
5153                                          bool *non_constant_p)
5154 {
5155   VEC(tree,gc) *expression_list;
5156   bool fold_expr_p = is_attribute_list;
5157   tree identifier = NULL_TREE;
5158   bool saved_greater_than_is_operator_p;
5159
5160   /* Assume all the expressions will be constant.  */
5161   if (non_constant_p)
5162     *non_constant_p = false;
5163
5164   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5165     return NULL;
5166
5167   expression_list = make_tree_vector ();
5168
5169   /* Within a parenthesized expression, a `>' token is always
5170      the greater-than operator.  */
5171   saved_greater_than_is_operator_p
5172     = parser->greater_than_is_operator_p;
5173   parser->greater_than_is_operator_p = true;
5174
5175   /* Consume expressions until there are no more.  */
5176   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5177     while (true)
5178       {
5179         tree expr;
5180
5181         /* At the beginning of attribute lists, check to see if the
5182            next token is an identifier.  */
5183         if (is_attribute_list
5184             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5185           {
5186             cp_token *token;
5187
5188             /* Consume the identifier.  */
5189             token = cp_lexer_consume_token (parser->lexer);
5190             /* Save the identifier.  */
5191             identifier = token->u.value;
5192           }
5193         else
5194           {
5195             bool expr_non_constant_p;
5196
5197             /* Parse the next assignment-expression.  */
5198             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5199               {
5200                 /* A braced-init-list.  */
5201                 maybe_warn_cpp0x ("extended initializer lists");
5202                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5203                 if (non_constant_p && expr_non_constant_p)
5204                   *non_constant_p = true;
5205               }
5206             else if (non_constant_p)
5207               {
5208                 expr = (cp_parser_constant_expression
5209                         (parser, /*allow_non_constant_p=*/true,
5210                          &expr_non_constant_p));
5211                 if (expr_non_constant_p)
5212                   *non_constant_p = true;
5213               }
5214             else
5215               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5216
5217             if (fold_expr_p)
5218               expr = fold_non_dependent_expr (expr);
5219
5220             /* If we have an ellipsis, then this is an expression
5221                expansion.  */
5222             if (allow_expansion_p
5223                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5224               {
5225                 /* Consume the `...'.  */
5226                 cp_lexer_consume_token (parser->lexer);
5227
5228                 /* Build the argument pack.  */
5229                 expr = make_pack_expansion (expr);
5230               }
5231
5232              /* Add it to the list.  We add error_mark_node
5233                 expressions to the list, so that we can still tell if
5234                 the correct form for a parenthesized expression-list
5235                 is found. That gives better errors.  */
5236             VEC_safe_push (tree, gc, expression_list, expr);
5237
5238             if (expr == error_mark_node)
5239               goto skip_comma;
5240           }
5241
5242         /* After the first item, attribute lists look the same as
5243            expression lists.  */
5244         is_attribute_list = false;
5245
5246       get_comma:;
5247         /* If the next token isn't a `,', then we are done.  */
5248         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5249           break;
5250
5251         /* Otherwise, consume the `,' and keep going.  */
5252         cp_lexer_consume_token (parser->lexer);
5253       }
5254
5255   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5256     {
5257       int ending;
5258
5259     skip_comma:;
5260       /* We try and resync to an unnested comma, as that will give the
5261          user better diagnostics.  */
5262       ending = cp_parser_skip_to_closing_parenthesis (parser,
5263                                                       /*recovering=*/true,
5264                                                       /*or_comma=*/true,
5265                                                       /*consume_paren=*/true);
5266       if (ending < 0)
5267         goto get_comma;
5268       if (!ending)
5269         {
5270           parser->greater_than_is_operator_p
5271             = saved_greater_than_is_operator_p;
5272           return NULL;
5273         }
5274     }
5275
5276   parser->greater_than_is_operator_p
5277     = saved_greater_than_is_operator_p;
5278
5279   if (identifier)
5280     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5281
5282   return expression_list;
5283 }
5284
5285 /* Parse a pseudo-destructor-name.
5286
5287    pseudo-destructor-name:
5288      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5289      :: [opt] nested-name-specifier template template-id :: ~ type-name
5290      :: [opt] nested-name-specifier [opt] ~ type-name
5291
5292    If either of the first two productions is used, sets *SCOPE to the
5293    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5294    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5295    or ERROR_MARK_NODE if the parse fails.  */
5296
5297 static void
5298 cp_parser_pseudo_destructor_name (cp_parser* parser,
5299                                   tree* scope,
5300                                   tree* type)
5301 {
5302   bool nested_name_specifier_p;
5303
5304   /* Assume that things will not work out.  */
5305   *type = error_mark_node;
5306
5307   /* Look for the optional `::' operator.  */
5308   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5309   /* Look for the optional nested-name-specifier.  */
5310   nested_name_specifier_p
5311     = (cp_parser_nested_name_specifier_opt (parser,
5312                                             /*typename_keyword_p=*/false,
5313                                             /*check_dependency_p=*/true,
5314                                             /*type_p=*/false,
5315                                             /*is_declaration=*/false)
5316        != NULL_TREE);
5317   /* Now, if we saw a nested-name-specifier, we might be doing the
5318      second production.  */
5319   if (nested_name_specifier_p
5320       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5321     {
5322       /* Consume the `template' keyword.  */
5323       cp_lexer_consume_token (parser->lexer);
5324       /* Parse the template-id.  */
5325       cp_parser_template_id (parser,
5326                              /*template_keyword_p=*/true,
5327                              /*check_dependency_p=*/false,
5328                              /*is_declaration=*/true);
5329       /* Look for the `::' token.  */
5330       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5331     }
5332   /* If the next token is not a `~', then there might be some
5333      additional qualification.  */
5334   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5335     {
5336       /* At this point, we're looking for "type-name :: ~".  The type-name
5337          must not be a class-name, since this is a pseudo-destructor.  So,
5338          it must be either an enum-name, or a typedef-name -- both of which
5339          are just identifiers.  So, we peek ahead to check that the "::"
5340          and "~" tokens are present; if they are not, then we can avoid
5341          calling type_name.  */
5342       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5343           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5344           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5345         {
5346           cp_parser_error (parser, "non-scalar type");
5347           return;
5348         }
5349
5350       /* Look for the type-name.  */
5351       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5352       if (*scope == error_mark_node)
5353         return;
5354
5355       /* Look for the `::' token.  */
5356       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5357     }
5358   else
5359     *scope = NULL_TREE;
5360
5361   /* Look for the `~'.  */
5362   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5363   /* Look for the type-name again.  We are not responsible for
5364      checking that it matches the first type-name.  */
5365   *type = cp_parser_nonclass_name (parser);
5366 }
5367
5368 /* Parse a unary-expression.
5369
5370    unary-expression:
5371      postfix-expression
5372      ++ cast-expression
5373      -- cast-expression
5374      unary-operator cast-expression
5375      sizeof unary-expression
5376      sizeof ( type-id )
5377      new-expression
5378      delete-expression
5379
5380    GNU Extensions:
5381
5382    unary-expression:
5383      __extension__ cast-expression
5384      __alignof__ unary-expression
5385      __alignof__ ( type-id )
5386      __real__ cast-expression
5387      __imag__ cast-expression
5388      && identifier
5389
5390    ADDRESS_P is true iff the unary-expression is appearing as the
5391    operand of the `&' operator.   CAST_P is true if this expression is
5392    the target of a cast.
5393
5394    Returns a representation of the expression.  */
5395
5396 static tree
5397 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5398                             cp_id_kind * pidk)
5399 {
5400   cp_token *token;
5401   enum tree_code unary_operator;
5402
5403   /* Peek at the next token.  */
5404   token = cp_lexer_peek_token (parser->lexer);
5405   /* Some keywords give away the kind of expression.  */
5406   if (token->type == CPP_KEYWORD)
5407     {
5408       enum rid keyword = token->keyword;
5409
5410       switch (keyword)
5411         {
5412         case RID_ALIGNOF:
5413         case RID_SIZEOF:
5414           {
5415             tree operand;
5416             enum tree_code op;
5417
5418             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5419             /* Consume the token.  */
5420             cp_lexer_consume_token (parser->lexer);
5421             /* Parse the operand.  */
5422             operand = cp_parser_sizeof_operand (parser, keyword);
5423
5424             if (TYPE_P (operand))
5425               return cxx_sizeof_or_alignof_type (operand, op, true);
5426             else
5427               return cxx_sizeof_or_alignof_expr (operand, op, true);
5428           }
5429
5430         case RID_NEW:
5431           return cp_parser_new_expression (parser);
5432
5433         case RID_DELETE:
5434           return cp_parser_delete_expression (parser);
5435
5436         case RID_EXTENSION:
5437           {
5438             /* The saved value of the PEDANTIC flag.  */
5439             int saved_pedantic;
5440             tree expr;
5441
5442             /* Save away the PEDANTIC flag.  */
5443             cp_parser_extension_opt (parser, &saved_pedantic);
5444             /* Parse the cast-expression.  */
5445             expr = cp_parser_simple_cast_expression (parser);
5446             /* Restore the PEDANTIC flag.  */
5447             pedantic = saved_pedantic;
5448
5449             return expr;
5450           }
5451
5452         case RID_REALPART:
5453         case RID_IMAGPART:
5454           {
5455             tree expression;
5456
5457             /* Consume the `__real__' or `__imag__' token.  */
5458             cp_lexer_consume_token (parser->lexer);
5459             /* Parse the cast-expression.  */
5460             expression = cp_parser_simple_cast_expression (parser);
5461             /* Create the complete representation.  */
5462             return build_x_unary_op ((keyword == RID_REALPART
5463                                       ? REALPART_EXPR : IMAGPART_EXPR),
5464                                      expression,
5465                                      tf_warning_or_error);
5466           }
5467           break;
5468
5469         default:
5470           break;
5471         }
5472     }
5473
5474   /* Look for the `:: new' and `:: delete', which also signal the
5475      beginning of a new-expression, or delete-expression,
5476      respectively.  If the next token is `::', then it might be one of
5477      these.  */
5478   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5479     {
5480       enum rid keyword;
5481
5482       /* See if the token after the `::' is one of the keywords in
5483          which we're interested.  */
5484       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5485       /* If it's `new', we have a new-expression.  */
5486       if (keyword == RID_NEW)
5487         return cp_parser_new_expression (parser);
5488       /* Similarly, for `delete'.  */
5489       else if (keyword == RID_DELETE)
5490         return cp_parser_delete_expression (parser);
5491     }
5492
5493   /* Look for a unary operator.  */
5494   unary_operator = cp_parser_unary_operator (token);
5495   /* The `++' and `--' operators can be handled similarly, even though
5496      they are not technically unary-operators in the grammar.  */
5497   if (unary_operator == ERROR_MARK)
5498     {
5499       if (token->type == CPP_PLUS_PLUS)
5500         unary_operator = PREINCREMENT_EXPR;
5501       else if (token->type == CPP_MINUS_MINUS)
5502         unary_operator = PREDECREMENT_EXPR;
5503       /* Handle the GNU address-of-label extension.  */
5504       else if (cp_parser_allow_gnu_extensions_p (parser)
5505                && token->type == CPP_AND_AND)
5506         {
5507           tree identifier;
5508           tree expression;
5509           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5510
5511           /* Consume the '&&' token.  */
5512           cp_lexer_consume_token (parser->lexer);
5513           /* Look for the identifier.  */
5514           identifier = cp_parser_identifier (parser);
5515           /* Create an expression representing the address.  */
5516           expression = finish_label_address_expr (identifier, loc);
5517           if (cp_parser_non_integral_constant_expression (parser,
5518                                                 "the address of a label"))
5519             expression = error_mark_node;
5520           return expression;
5521         }
5522     }
5523   if (unary_operator != ERROR_MARK)
5524     {
5525       tree cast_expression;
5526       tree expression = error_mark_node;
5527       const char *non_constant_p = NULL;
5528
5529       /* Consume the operator token.  */
5530       token = cp_lexer_consume_token (parser->lexer);
5531       /* Parse the cast-expression.  */
5532       cast_expression
5533         = cp_parser_cast_expression (parser,
5534                                      unary_operator == ADDR_EXPR,
5535                                      /*cast_p=*/false, pidk);
5536       /* Now, build an appropriate representation.  */
5537       switch (unary_operator)
5538         {
5539         case INDIRECT_REF:
5540           non_constant_p = "%<*%>";
5541           expression = build_x_indirect_ref (cast_expression, "unary *",
5542                                              tf_warning_or_error);
5543           break;
5544
5545         case ADDR_EXPR:
5546           non_constant_p = "%<&%>";
5547           /* Fall through.  */
5548         case BIT_NOT_EXPR:
5549           expression = build_x_unary_op (unary_operator, cast_expression,
5550                                          tf_warning_or_error);
5551           break;
5552
5553         case PREINCREMENT_EXPR:
5554         case PREDECREMENT_EXPR:
5555           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5556                             ? "%<++%>" : "%<--%>");
5557           /* Fall through.  */
5558         case UNARY_PLUS_EXPR:
5559         case NEGATE_EXPR:
5560         case TRUTH_NOT_EXPR:
5561           expression = finish_unary_op_expr (unary_operator, cast_expression);
5562           break;
5563
5564         default:
5565           gcc_unreachable ();
5566         }
5567
5568       if (non_constant_p
5569           && cp_parser_non_integral_constant_expression (parser,
5570                                                          non_constant_p))
5571         expression = error_mark_node;
5572
5573       return expression;
5574     }
5575
5576   return cp_parser_postfix_expression (parser, address_p, cast_p,
5577                                        /*member_access_only_p=*/false,
5578                                        pidk);
5579 }
5580
5581 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5582    unary-operator, the corresponding tree code is returned.  */
5583
5584 static enum tree_code
5585 cp_parser_unary_operator (cp_token* token)
5586 {
5587   switch (token->type)
5588     {
5589     case CPP_MULT:
5590       return INDIRECT_REF;
5591
5592     case CPP_AND:
5593       return ADDR_EXPR;
5594
5595     case CPP_PLUS:
5596       return UNARY_PLUS_EXPR;
5597
5598     case CPP_MINUS:
5599       return NEGATE_EXPR;
5600
5601     case CPP_NOT:
5602       return TRUTH_NOT_EXPR;
5603
5604     case CPP_COMPL:
5605       return BIT_NOT_EXPR;
5606
5607     default:
5608       return ERROR_MARK;
5609     }
5610 }
5611
5612 /* Parse a new-expression.
5613
5614    new-expression:
5615      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5616      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5617
5618    Returns a representation of the expression.  */
5619
5620 static tree
5621 cp_parser_new_expression (cp_parser* parser)
5622 {
5623   bool global_scope_p;
5624   VEC(tree,gc) *placement;
5625   tree type;
5626   VEC(tree,gc) *initializer;
5627   tree nelts;
5628   tree ret;
5629
5630   /* Look for the optional `::' operator.  */
5631   global_scope_p
5632     = (cp_parser_global_scope_opt (parser,
5633                                    /*current_scope_valid_p=*/false)
5634        != NULL_TREE);
5635   /* Look for the `new' operator.  */
5636   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5637   /* There's no easy way to tell a new-placement from the
5638      `( type-id )' construct.  */
5639   cp_parser_parse_tentatively (parser);
5640   /* Look for a new-placement.  */
5641   placement = cp_parser_new_placement (parser);
5642   /* If that didn't work out, there's no new-placement.  */
5643   if (!cp_parser_parse_definitely (parser))
5644     {
5645       if (placement != NULL)
5646         release_tree_vector (placement);
5647       placement = NULL;
5648     }
5649
5650   /* If the next token is a `(', then we have a parenthesized
5651      type-id.  */
5652   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5653     {
5654       cp_token *token;
5655       /* Consume the `('.  */
5656       cp_lexer_consume_token (parser->lexer);
5657       /* Parse the type-id.  */
5658       type = cp_parser_type_id (parser);
5659       /* Look for the closing `)'.  */
5660       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5661       token = cp_lexer_peek_token (parser->lexer);
5662       /* There should not be a direct-new-declarator in this production,
5663          but GCC used to allowed this, so we check and emit a sensible error
5664          message for this case.  */
5665       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5666         {
5667           error ("%Harray bound forbidden after parenthesized type-id",
5668                  &token->location);
5669           inform (token->location, 
5670                   "try removing the parentheses around the type-id");
5671           cp_parser_direct_new_declarator (parser);
5672         }
5673       nelts = NULL_TREE;
5674     }
5675   /* Otherwise, there must be a new-type-id.  */
5676   else
5677     type = cp_parser_new_type_id (parser, &nelts);
5678
5679   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5680   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5681       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5682     initializer = cp_parser_new_initializer (parser);
5683   else
5684     initializer = NULL;
5685
5686   /* A new-expression may not appear in an integral constant
5687      expression.  */
5688   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5689     ret = error_mark_node;
5690   else
5691     {
5692       /* Create a representation of the new-expression.  */
5693       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5694                        tf_warning_or_error);
5695     }
5696
5697   if (placement != NULL)
5698     release_tree_vector (placement);
5699   if (initializer != NULL)
5700     release_tree_vector (initializer);
5701
5702   return ret;
5703 }
5704
5705 /* Parse a new-placement.
5706
5707    new-placement:
5708      ( expression-list )
5709
5710    Returns the same representation as for an expression-list.  */
5711
5712 static VEC(tree,gc) *
5713 cp_parser_new_placement (cp_parser* parser)
5714 {
5715   VEC(tree,gc) *expression_list;
5716
5717   /* Parse the expression-list.  */
5718   expression_list = (cp_parser_parenthesized_expression_list
5719                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5720                       /*non_constant_p=*/NULL));
5721
5722   return expression_list;
5723 }
5724
5725 /* Parse a new-type-id.
5726
5727    new-type-id:
5728      type-specifier-seq new-declarator [opt]
5729
5730    Returns the TYPE allocated.  If the new-type-id indicates an array
5731    type, *NELTS is set to the number of elements in the last array
5732    bound; the TYPE will not include the last array bound.  */
5733
5734 static tree
5735 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5736 {
5737   cp_decl_specifier_seq type_specifier_seq;
5738   cp_declarator *new_declarator;
5739   cp_declarator *declarator;
5740   cp_declarator *outer_declarator;
5741   const char *saved_message;
5742   tree type;
5743
5744   /* The type-specifier sequence must not contain type definitions.
5745      (It cannot contain declarations of new types either, but if they
5746      are not definitions we will catch that because they are not
5747      complete.)  */
5748   saved_message = parser->type_definition_forbidden_message;
5749   parser->type_definition_forbidden_message
5750     = "types may not be defined in a new-type-id";
5751   /* Parse the type-specifier-seq.  */
5752   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5753                                 &type_specifier_seq);
5754   /* Restore the old message.  */
5755   parser->type_definition_forbidden_message = saved_message;
5756   /* Parse the new-declarator.  */
5757   new_declarator = cp_parser_new_declarator_opt (parser);
5758
5759   /* Determine the number of elements in the last array dimension, if
5760      any.  */
5761   *nelts = NULL_TREE;
5762   /* Skip down to the last array dimension.  */
5763   declarator = new_declarator;
5764   outer_declarator = NULL;
5765   while (declarator && (declarator->kind == cdk_pointer
5766                         || declarator->kind == cdk_ptrmem))
5767     {
5768       outer_declarator = declarator;
5769       declarator = declarator->declarator;
5770     }
5771   while (declarator
5772          && declarator->kind == cdk_array
5773          && declarator->declarator
5774          && declarator->declarator->kind == cdk_array)
5775     {
5776       outer_declarator = declarator;
5777       declarator = declarator->declarator;
5778     }
5779
5780   if (declarator && declarator->kind == cdk_array)
5781     {
5782       *nelts = declarator->u.array.bounds;
5783       if (*nelts == error_mark_node)
5784         *nelts = integer_one_node;
5785
5786       if (outer_declarator)
5787         outer_declarator->declarator = declarator->declarator;
5788       else
5789         new_declarator = NULL;
5790     }
5791
5792   type = groktypename (&type_specifier_seq, new_declarator, false);
5793   return type;
5794 }
5795
5796 /* Parse an (optional) new-declarator.
5797
5798    new-declarator:
5799      ptr-operator new-declarator [opt]
5800      direct-new-declarator
5801
5802    Returns the declarator.  */
5803
5804 static cp_declarator *
5805 cp_parser_new_declarator_opt (cp_parser* parser)
5806 {
5807   enum tree_code code;
5808   tree type;
5809   cp_cv_quals cv_quals;
5810
5811   /* We don't know if there's a ptr-operator next, or not.  */
5812   cp_parser_parse_tentatively (parser);
5813   /* Look for a ptr-operator.  */
5814   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5815   /* If that worked, look for more new-declarators.  */
5816   if (cp_parser_parse_definitely (parser))
5817     {
5818       cp_declarator *declarator;
5819
5820       /* Parse another optional declarator.  */
5821       declarator = cp_parser_new_declarator_opt (parser);
5822
5823       return cp_parser_make_indirect_declarator
5824         (code, type, cv_quals, declarator);
5825     }
5826
5827   /* If the next token is a `[', there is a direct-new-declarator.  */
5828   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5829     return cp_parser_direct_new_declarator (parser);
5830
5831   return NULL;
5832 }
5833
5834 /* Parse a direct-new-declarator.
5835
5836    direct-new-declarator:
5837      [ expression ]
5838      direct-new-declarator [constant-expression]
5839
5840    */
5841
5842 static cp_declarator *
5843 cp_parser_direct_new_declarator (cp_parser* parser)
5844 {
5845   cp_declarator *declarator = NULL;
5846
5847   while (true)
5848     {
5849       tree expression;
5850
5851       /* Look for the opening `['.  */
5852       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5853       /* The first expression is not required to be constant.  */
5854       if (!declarator)
5855         {
5856           cp_token *token = cp_lexer_peek_token (parser->lexer);
5857           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5858           /* The standard requires that the expression have integral
5859              type.  DR 74 adds enumeration types.  We believe that the
5860              real intent is that these expressions be handled like the
5861              expression in a `switch' condition, which also allows
5862              classes with a single conversion to integral or
5863              enumeration type.  */
5864           if (!processing_template_decl)
5865             {
5866               expression
5867                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5868                                               expression,
5869                                               /*complain=*/true);
5870               if (!expression)
5871                 {
5872                   error ("%Hexpression in new-declarator must have integral "
5873                          "or enumeration type", &token->location);
5874                   expression = error_mark_node;
5875                 }
5876             }
5877         }
5878       /* But all the other expressions must be.  */
5879       else
5880         expression
5881           = cp_parser_constant_expression (parser,
5882                                            /*allow_non_constant=*/false,
5883                                            NULL);
5884       /* Look for the closing `]'.  */
5885       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5886
5887       /* Add this bound to the declarator.  */
5888       declarator = make_array_declarator (declarator, expression);
5889
5890       /* If the next token is not a `[', then there are no more
5891          bounds.  */
5892       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5893         break;
5894     }
5895
5896   return declarator;
5897 }
5898
5899 /* Parse a new-initializer.
5900
5901    new-initializer:
5902      ( expression-list [opt] )
5903      braced-init-list
5904
5905    Returns a representation of the expression-list.  */
5906
5907 static VEC(tree,gc) *
5908 cp_parser_new_initializer (cp_parser* parser)
5909 {
5910   VEC(tree,gc) *expression_list;
5911
5912   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5913     {
5914       tree t;
5915       bool expr_non_constant_p;
5916       maybe_warn_cpp0x ("extended initializer lists");
5917       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5918       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5919       expression_list = make_tree_vector_single (t);
5920     }
5921   else
5922     expression_list = (cp_parser_parenthesized_expression_list
5923                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5924                         /*non_constant_p=*/NULL));
5925
5926   return expression_list;
5927 }
5928
5929 /* Parse a delete-expression.
5930
5931    delete-expression:
5932      :: [opt] delete cast-expression
5933      :: [opt] delete [ ] cast-expression
5934
5935    Returns a representation of the expression.  */
5936
5937 static tree
5938 cp_parser_delete_expression (cp_parser* parser)
5939 {
5940   bool global_scope_p;
5941   bool array_p;
5942   tree expression;
5943
5944   /* Look for the optional `::' operator.  */
5945   global_scope_p
5946     = (cp_parser_global_scope_opt (parser,
5947                                    /*current_scope_valid_p=*/false)
5948        != NULL_TREE);
5949   /* Look for the `delete' keyword.  */
5950   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5951   /* See if the array syntax is in use.  */
5952   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5953     {
5954       /* Consume the `[' token.  */
5955       cp_lexer_consume_token (parser->lexer);
5956       /* Look for the `]' token.  */
5957       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5958       /* Remember that this is the `[]' construct.  */
5959       array_p = true;
5960     }
5961   else
5962     array_p = false;
5963
5964   /* Parse the cast-expression.  */
5965   expression = cp_parser_simple_cast_expression (parser);
5966
5967   /* A delete-expression may not appear in an integral constant
5968      expression.  */
5969   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5970     return error_mark_node;
5971
5972   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5973 }
5974
5975 /* Returns true if TOKEN may start a cast-expression and false
5976    otherwise.  */
5977
5978 static bool
5979 cp_parser_token_starts_cast_expression (cp_token *token)
5980 {
5981   switch (token->type)
5982     {
5983     case CPP_COMMA:
5984     case CPP_SEMICOLON:
5985     case CPP_QUERY:
5986     case CPP_COLON:
5987     case CPP_CLOSE_SQUARE:
5988     case CPP_CLOSE_PAREN:
5989     case CPP_CLOSE_BRACE:
5990     case CPP_DOT:
5991     case CPP_DOT_STAR:
5992     case CPP_DEREF:
5993     case CPP_DEREF_STAR:
5994     case CPP_DIV:
5995     case CPP_MOD:
5996     case CPP_LSHIFT:
5997     case CPP_RSHIFT:
5998     case CPP_LESS:
5999     case CPP_GREATER:
6000     case CPP_LESS_EQ:
6001     case CPP_GREATER_EQ:
6002     case CPP_EQ_EQ:
6003     case CPP_NOT_EQ:
6004     case CPP_EQ:
6005     case CPP_MULT_EQ:
6006     case CPP_DIV_EQ:
6007     case CPP_MOD_EQ:
6008     case CPP_PLUS_EQ:
6009     case CPP_MINUS_EQ:
6010     case CPP_RSHIFT_EQ:
6011     case CPP_LSHIFT_EQ:
6012     case CPP_AND_EQ:
6013     case CPP_XOR_EQ:
6014     case CPP_OR_EQ:
6015     case CPP_XOR:
6016     case CPP_OR:
6017     case CPP_OR_OR:
6018     case CPP_EOF:
6019       return false;
6020
6021       /* '[' may start a primary-expression in obj-c++.  */
6022     case CPP_OPEN_SQUARE:
6023       return c_dialect_objc ();
6024
6025     default:
6026       return true;
6027     }
6028 }
6029
6030 /* Parse a cast-expression.
6031
6032    cast-expression:
6033      unary-expression
6034      ( type-id ) cast-expression
6035
6036    ADDRESS_P is true iff the unary-expression is appearing as the
6037    operand of the `&' operator.   CAST_P is true if this expression is
6038    the target of a cast.
6039
6040    Returns a representation of the expression.  */
6041
6042 static tree
6043 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6044                            cp_id_kind * pidk)
6045 {
6046   /* If it's a `(', then we might be looking at a cast.  */
6047   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6048     {
6049       tree type = NULL_TREE;
6050       tree expr = NULL_TREE;
6051       bool compound_literal_p;
6052       const char *saved_message;
6053
6054       /* There's no way to know yet whether or not this is a cast.
6055          For example, `(int (3))' is a unary-expression, while `(int)
6056          3' is a cast.  So, we resort to parsing tentatively.  */
6057       cp_parser_parse_tentatively (parser);
6058       /* Types may not be defined in a cast.  */
6059       saved_message = parser->type_definition_forbidden_message;
6060       parser->type_definition_forbidden_message
6061         = "types may not be defined in casts";
6062       /* Consume the `('.  */
6063       cp_lexer_consume_token (parser->lexer);
6064       /* A very tricky bit is that `(struct S) { 3 }' is a
6065          compound-literal (which we permit in C++ as an extension).
6066          But, that construct is not a cast-expression -- it is a
6067          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6068          is legal; if the compound-literal were a cast-expression,
6069          you'd need an extra set of parentheses.)  But, if we parse
6070          the type-id, and it happens to be a class-specifier, then we
6071          will commit to the parse at that point, because we cannot
6072          undo the action that is done when creating a new class.  So,
6073          then we cannot back up and do a postfix-expression.
6074
6075          Therefore, we scan ahead to the closing `)', and check to see
6076          if the token after the `)' is a `{'.  If so, we are not
6077          looking at a cast-expression.
6078
6079          Save tokens so that we can put them back.  */
6080       cp_lexer_save_tokens (parser->lexer);
6081       /* Skip tokens until the next token is a closing parenthesis.
6082          If we find the closing `)', and the next token is a `{', then
6083          we are looking at a compound-literal.  */
6084       compound_literal_p
6085         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6086                                                   /*consume_paren=*/true)
6087            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6088       /* Roll back the tokens we skipped.  */
6089       cp_lexer_rollback_tokens (parser->lexer);
6090       /* If we were looking at a compound-literal, simulate an error
6091          so that the call to cp_parser_parse_definitely below will
6092          fail.  */
6093       if (compound_literal_p)
6094         cp_parser_simulate_error (parser);
6095       else
6096         {
6097           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6098           parser->in_type_id_in_expr_p = true;
6099           /* Look for the type-id.  */
6100           type = cp_parser_type_id (parser);
6101           /* Look for the closing `)'.  */
6102           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6103           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6104         }
6105
6106       /* Restore the saved message.  */
6107       parser->type_definition_forbidden_message = saved_message;
6108
6109       /* At this point this can only be either a cast or a
6110          parenthesized ctor such as `(T ())' that looks like a cast to
6111          function returning T.  */
6112       if (!cp_parser_error_occurred (parser)
6113           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6114                                                      (parser->lexer)))
6115         {
6116           cp_parser_parse_definitely (parser);
6117           expr = cp_parser_cast_expression (parser,
6118                                             /*address_p=*/false,
6119                                             /*cast_p=*/true, pidk);
6120
6121           /* Warn about old-style casts, if so requested.  */
6122           if (warn_old_style_cast
6123               && !in_system_header
6124               && !VOID_TYPE_P (type)
6125               && current_lang_name != lang_name_c)
6126             warning (OPT_Wold_style_cast, "use of old-style cast");
6127
6128           /* Only type conversions to integral or enumeration types
6129              can be used in constant-expressions.  */
6130           if (!cast_valid_in_integral_constant_expression_p (type)
6131               && (cp_parser_non_integral_constant_expression
6132                   (parser,
6133                    "a cast to a type other than an integral or "
6134                    "enumeration type")))
6135             return error_mark_node;
6136
6137           /* Perform the cast.  */
6138           expr = build_c_cast (type, expr);
6139           return expr;
6140         }
6141       else 
6142         cp_parser_abort_tentative_parse (parser);
6143     }
6144
6145   /* If we get here, then it's not a cast, so it must be a
6146      unary-expression.  */
6147   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6148 }
6149
6150 /* Parse a binary expression of the general form:
6151
6152    pm-expression:
6153      cast-expression
6154      pm-expression .* cast-expression
6155      pm-expression ->* cast-expression
6156
6157    multiplicative-expression:
6158      pm-expression
6159      multiplicative-expression * pm-expression
6160      multiplicative-expression / pm-expression
6161      multiplicative-expression % pm-expression
6162
6163    additive-expression:
6164      multiplicative-expression
6165      additive-expression + multiplicative-expression
6166      additive-expression - multiplicative-expression
6167
6168    shift-expression:
6169      additive-expression
6170      shift-expression << additive-expression
6171      shift-expression >> additive-expression
6172
6173    relational-expression:
6174      shift-expression
6175      relational-expression < shift-expression
6176      relational-expression > shift-expression
6177      relational-expression <= shift-expression
6178      relational-expression >= shift-expression
6179
6180   GNU Extension:
6181
6182    relational-expression:
6183      relational-expression <? shift-expression
6184      relational-expression >? shift-expression
6185
6186    equality-expression:
6187      relational-expression
6188      equality-expression == relational-expression
6189      equality-expression != relational-expression
6190
6191    and-expression:
6192      equality-expression
6193      and-expression & equality-expression
6194
6195    exclusive-or-expression:
6196      and-expression
6197      exclusive-or-expression ^ and-expression
6198
6199    inclusive-or-expression:
6200      exclusive-or-expression
6201      inclusive-or-expression | exclusive-or-expression
6202
6203    logical-and-expression:
6204      inclusive-or-expression
6205      logical-and-expression && inclusive-or-expression
6206
6207    logical-or-expression:
6208      logical-and-expression
6209      logical-or-expression || logical-and-expression
6210
6211    All these are implemented with a single function like:
6212
6213    binary-expression:
6214      simple-cast-expression
6215      binary-expression <token> binary-expression
6216
6217    CAST_P is true if this expression is the target of a cast.
6218
6219    The binops_by_token map is used to get the tree codes for each <token> type.
6220    binary-expressions are associated according to a precedence table.  */
6221
6222 #define TOKEN_PRECEDENCE(token)                              \
6223 (((token->type == CPP_GREATER                                \
6224    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6225   && !parser->greater_than_is_operator_p)                    \
6226  ? PREC_NOT_OPERATOR                                         \
6227  : binops_by_token[token->type].prec)
6228
6229 static tree
6230 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6231                              bool no_toplevel_fold_p,
6232                              enum cp_parser_prec prec,
6233                              cp_id_kind * pidk)
6234 {
6235   cp_parser_expression_stack stack;
6236   cp_parser_expression_stack_entry *sp = &stack[0];
6237   tree lhs, rhs;
6238   cp_token *token;
6239   enum tree_code tree_type, lhs_type, rhs_type;
6240   enum cp_parser_prec new_prec, lookahead_prec;
6241   bool overloaded_p;
6242
6243   /* Parse the first expression.  */
6244   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6245   lhs_type = ERROR_MARK;
6246
6247   for (;;)
6248     {
6249       /* Get an operator token.  */
6250       token = cp_lexer_peek_token (parser->lexer);
6251
6252       if (warn_cxx0x_compat
6253           && token->type == CPP_RSHIFT
6254           && !parser->greater_than_is_operator_p)
6255         {
6256           warning (OPT_Wc__0x_compat, 
6257                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6258                    &token->location);
6259           warning (OPT_Wc__0x_compat, 
6260                    "suggest parentheses around %<>>%> expression");
6261         }
6262
6263       new_prec = TOKEN_PRECEDENCE (token);
6264
6265       /* Popping an entry off the stack means we completed a subexpression:
6266          - either we found a token which is not an operator (`>' where it is not
6267            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6268            will happen repeatedly;
6269          - or, we found an operator which has lower priority.  This is the case
6270            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6271            parsing `3 * 4'.  */
6272       if (new_prec <= prec)
6273         {
6274           if (sp == stack)
6275             break;
6276           else
6277             goto pop;
6278         }
6279
6280      get_rhs:
6281       tree_type = binops_by_token[token->type].tree_type;
6282
6283       /* We used the operator token.  */
6284       cp_lexer_consume_token (parser->lexer);
6285
6286       /* Extract another operand.  It may be the RHS of this expression
6287          or the LHS of a new, higher priority expression.  */
6288       rhs = cp_parser_simple_cast_expression (parser);
6289       rhs_type = ERROR_MARK;
6290
6291       /* Get another operator token.  Look up its precedence to avoid
6292          building a useless (immediately popped) stack entry for common
6293          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6294       token = cp_lexer_peek_token (parser->lexer);
6295       lookahead_prec = TOKEN_PRECEDENCE (token);
6296       if (lookahead_prec > new_prec)
6297         {
6298           /* ... and prepare to parse the RHS of the new, higher priority
6299              expression.  Since precedence levels on the stack are
6300              monotonically increasing, we do not have to care about
6301              stack overflows.  */
6302           sp->prec = prec;
6303           sp->tree_type = tree_type;
6304           sp->lhs = lhs;
6305           sp->lhs_type = lhs_type;
6306           sp++;
6307           lhs = rhs;
6308           lhs_type = rhs_type;
6309           prec = new_prec;
6310           new_prec = lookahead_prec;
6311           goto get_rhs;
6312
6313          pop:
6314           lookahead_prec = new_prec;
6315           /* If the stack is not empty, we have parsed into LHS the right side
6316              (`4' in the example above) of an expression we had suspended.
6317              We can use the information on the stack to recover the LHS (`3')
6318              from the stack together with the tree code (`MULT_EXPR'), and
6319              the precedence of the higher level subexpression
6320              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6321              which will be used to actually build the additive expression.  */
6322           --sp;
6323           prec = sp->prec;
6324           tree_type = sp->tree_type;
6325           rhs = lhs;
6326           rhs_type = lhs_type;
6327           lhs = sp->lhs;
6328           lhs_type = sp->lhs_type;
6329         }
6330
6331       overloaded_p = false;
6332       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6333          ERROR_MARK for everything that is not a binary expression.
6334          This makes warn_about_parentheses miss some warnings that
6335          involve unary operators.  For unary expressions we should
6336          pass the correct tree_code unless the unary expression was
6337          surrounded by parentheses.
6338       */
6339       if (no_toplevel_fold_p
6340           && lookahead_prec <= prec
6341           && sp == stack
6342           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6343         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6344       else
6345         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6346                                  &overloaded_p, tf_warning_or_error);
6347       lhs_type = tree_type;
6348
6349       /* If the binary operator required the use of an overloaded operator,
6350          then this expression cannot be an integral constant-expression.
6351          An overloaded operator can be used even if both operands are
6352          otherwise permissible in an integral constant-expression if at
6353          least one of the operands is of enumeration type.  */
6354
6355       if (overloaded_p
6356           && (cp_parser_non_integral_constant_expression
6357               (parser, "calls to overloaded operators")))
6358         return error_mark_node;
6359     }
6360
6361   return lhs;
6362 }
6363
6364
6365 /* Parse the `? expression : assignment-expression' part of a
6366    conditional-expression.  The LOGICAL_OR_EXPR is the
6367    logical-or-expression that started the conditional-expression.
6368    Returns a representation of the entire conditional-expression.
6369
6370    This routine is used by cp_parser_assignment_expression.
6371
6372      ? expression : assignment-expression
6373
6374    GNU Extensions:
6375
6376      ? : assignment-expression */
6377
6378 static tree
6379 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6380 {
6381   tree expr;
6382   tree assignment_expr;
6383
6384   /* Consume the `?' token.  */
6385   cp_lexer_consume_token (parser->lexer);
6386   if (cp_parser_allow_gnu_extensions_p (parser)
6387       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6388     /* Implicit true clause.  */
6389     expr = NULL_TREE;
6390   else
6391     /* Parse the expression.  */
6392     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6393
6394   /* The next token should be a `:'.  */
6395   cp_parser_require (parser, CPP_COLON, "%<:%>");
6396   /* Parse the assignment-expression.  */
6397   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6398
6399   /* Build the conditional-expression.  */
6400   return build_x_conditional_expr (logical_or_expr,
6401                                    expr,
6402                                    assignment_expr,
6403                                    tf_warning_or_error);
6404 }
6405
6406 /* Parse an assignment-expression.
6407
6408    assignment-expression:
6409      conditional-expression
6410      logical-or-expression assignment-operator assignment_expression
6411      throw-expression
6412
6413    CAST_P is true if this expression is the target of a cast.
6414
6415    Returns a representation for the expression.  */
6416
6417 static tree
6418 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6419                                  cp_id_kind * pidk)
6420 {
6421   tree expr;
6422
6423   /* If the next token is the `throw' keyword, then we're looking at
6424      a throw-expression.  */
6425   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6426     expr = cp_parser_throw_expression (parser);
6427   /* Otherwise, it must be that we are looking at a
6428      logical-or-expression.  */
6429   else
6430     {
6431       /* Parse the binary expressions (logical-or-expression).  */
6432       expr = cp_parser_binary_expression (parser, cast_p, false,
6433                                           PREC_NOT_OPERATOR, pidk);
6434       /* If the next token is a `?' then we're actually looking at a
6435          conditional-expression.  */
6436       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6437         return cp_parser_question_colon_clause (parser, expr);
6438       else
6439         {
6440           enum tree_code assignment_operator;
6441
6442           /* If it's an assignment-operator, we're using the second
6443              production.  */
6444           assignment_operator
6445             = cp_parser_assignment_operator_opt (parser);
6446           if (assignment_operator != ERROR_MARK)
6447             {
6448               bool non_constant_p;
6449
6450               /* Parse the right-hand side of the assignment.  */
6451               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6452
6453               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6454                 maybe_warn_cpp0x ("extended initializer lists");
6455
6456               /* An assignment may not appear in a
6457                  constant-expression.  */
6458               if (cp_parser_non_integral_constant_expression (parser,
6459                                                               "an assignment"))
6460                 return error_mark_node;
6461               /* Build the assignment expression.  */
6462               expr = build_x_modify_expr (expr,
6463                                           assignment_operator,
6464                                           rhs,
6465                                           tf_warning_or_error);
6466             }
6467         }
6468     }
6469
6470   return expr;
6471 }
6472
6473 /* Parse an (optional) assignment-operator.
6474
6475    assignment-operator: one of
6476      = *= /= %= += -= >>= <<= &= ^= |=
6477
6478    GNU Extension:
6479
6480    assignment-operator: one of
6481      <?= >?=
6482
6483    If the next token is an assignment operator, the corresponding tree
6484    code is returned, and the token is consumed.  For example, for
6485    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6486    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6487    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6488    operator, ERROR_MARK is returned.  */
6489
6490 static enum tree_code
6491 cp_parser_assignment_operator_opt (cp_parser* parser)
6492 {
6493   enum tree_code op;
6494   cp_token *token;
6495
6496   /* Peek at the next token.  */
6497   token = cp_lexer_peek_token (parser->lexer);
6498
6499   switch (token->type)
6500     {
6501     case CPP_EQ:
6502       op = NOP_EXPR;
6503       break;
6504
6505     case CPP_MULT_EQ:
6506       op = MULT_EXPR;
6507       break;
6508
6509     case CPP_DIV_EQ:
6510       op = TRUNC_DIV_EXPR;
6511       break;
6512
6513     case CPP_MOD_EQ:
6514       op = TRUNC_MOD_EXPR;
6515       break;
6516
6517     case CPP_PLUS_EQ:
6518       op = PLUS_EXPR;
6519       break;
6520
6521     case CPP_MINUS_EQ:
6522       op = MINUS_EXPR;
6523       break;
6524
6525     case CPP_RSHIFT_EQ:
6526       op = RSHIFT_EXPR;
6527       break;
6528
6529     case CPP_LSHIFT_EQ:
6530       op = LSHIFT_EXPR;
6531       break;
6532
6533     case CPP_AND_EQ:
6534       op = BIT_AND_EXPR;
6535       break;
6536
6537     case CPP_XOR_EQ:
6538       op = BIT_XOR_EXPR;
6539       break;
6540
6541     case CPP_OR_EQ:
6542       op = BIT_IOR_EXPR;
6543       break;
6544
6545     default:
6546       /* Nothing else is an assignment operator.  */
6547       op = ERROR_MARK;
6548     }
6549
6550   /* If it was an assignment operator, consume it.  */
6551   if (op != ERROR_MARK)
6552     cp_lexer_consume_token (parser->lexer);
6553
6554   return op;
6555 }
6556
6557 /* Parse an expression.
6558
6559    expression:
6560      assignment-expression
6561      expression , assignment-expression
6562
6563    CAST_P is true if this expression is the target of a cast.
6564
6565    Returns a representation of the expression.  */
6566
6567 static tree
6568 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6569 {
6570   tree expression = NULL_TREE;
6571
6572   while (true)
6573     {
6574       tree assignment_expression;
6575
6576       /* Parse the next assignment-expression.  */
6577       assignment_expression
6578         = cp_parser_assignment_expression (parser, cast_p, pidk);
6579       /* If this is the first assignment-expression, we can just
6580          save it away.  */
6581       if (!expression)
6582         expression = assignment_expression;
6583       else
6584         expression = build_x_compound_expr (expression,
6585                                             assignment_expression,
6586                                             tf_warning_or_error);
6587       /* If the next token is not a comma, then we are done with the
6588          expression.  */
6589       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6590         break;
6591       /* Consume the `,'.  */
6592       cp_lexer_consume_token (parser->lexer);
6593       /* A comma operator cannot appear in a constant-expression.  */
6594       if (cp_parser_non_integral_constant_expression (parser,
6595                                                       "a comma operator"))
6596         expression = error_mark_node;
6597     }
6598
6599   return expression;
6600 }
6601
6602 /* Parse a constant-expression.
6603
6604    constant-expression:
6605      conditional-expression
6606
6607   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6608   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6609   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6610   is false, NON_CONSTANT_P should be NULL.  */
6611
6612 static tree
6613 cp_parser_constant_expression (cp_parser* parser,
6614                                bool allow_non_constant_p,
6615                                bool *non_constant_p)
6616 {
6617   bool saved_integral_constant_expression_p;
6618   bool saved_allow_non_integral_constant_expression_p;
6619   bool saved_non_integral_constant_expression_p;
6620   tree expression;
6621
6622   /* It might seem that we could simply parse the
6623      conditional-expression, and then check to see if it were
6624      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6625      one that the compiler can figure out is constant, possibly after
6626      doing some simplifications or optimizations.  The standard has a
6627      precise definition of constant-expression, and we must honor
6628      that, even though it is somewhat more restrictive.
6629
6630      For example:
6631
6632        int i[(2, 3)];
6633
6634      is not a legal declaration, because `(2, 3)' is not a
6635      constant-expression.  The `,' operator is forbidden in a
6636      constant-expression.  However, GCC's constant-folding machinery
6637      will fold this operation to an INTEGER_CST for `3'.  */
6638
6639   /* Save the old settings.  */
6640   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6641   saved_allow_non_integral_constant_expression_p
6642     = parser->allow_non_integral_constant_expression_p;
6643   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6644   /* We are now parsing a constant-expression.  */
6645   parser->integral_constant_expression_p = true;
6646   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6647   parser->non_integral_constant_expression_p = false;
6648   /* Although the grammar says "conditional-expression", we parse an
6649      "assignment-expression", which also permits "throw-expression"
6650      and the use of assignment operators.  In the case that
6651      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6652      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6653      actually essential that we look for an assignment-expression.
6654      For example, cp_parser_initializer_clauses uses this function to
6655      determine whether a particular assignment-expression is in fact
6656      constant.  */
6657   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6658   /* Restore the old settings.  */
6659   parser->integral_constant_expression_p
6660     = saved_integral_constant_expression_p;
6661   parser->allow_non_integral_constant_expression_p
6662     = saved_allow_non_integral_constant_expression_p;
6663   if (allow_non_constant_p)
6664     *non_constant_p = parser->non_integral_constant_expression_p;
6665   else if (parser->non_integral_constant_expression_p)
6666     expression = error_mark_node;
6667   parser->non_integral_constant_expression_p
6668     = saved_non_integral_constant_expression_p;
6669
6670   return expression;
6671 }
6672
6673 /* Parse __builtin_offsetof.
6674
6675    offsetof-expression:
6676      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6677
6678    offsetof-member-designator:
6679      id-expression
6680      | offsetof-member-designator "." id-expression
6681      | offsetof-member-designator "[" expression "]"
6682      | offsetof-member-designator "->" id-expression  */
6683
6684 static tree
6685 cp_parser_builtin_offsetof (cp_parser *parser)
6686 {
6687   int save_ice_p, save_non_ice_p;
6688   tree type, expr;
6689   cp_id_kind dummy;
6690   cp_token *token;
6691
6692   /* We're about to accept non-integral-constant things, but will
6693      definitely yield an integral constant expression.  Save and
6694      restore these values around our local parsing.  */
6695   save_ice_p = parser->integral_constant_expression_p;
6696   save_non_ice_p = parser->non_integral_constant_expression_p;
6697
6698   /* Consume the "__builtin_offsetof" token.  */
6699   cp_lexer_consume_token (parser->lexer);
6700   /* Consume the opening `('.  */
6701   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6702   /* Parse the type-id.  */
6703   type = cp_parser_type_id (parser);
6704   /* Look for the `,'.  */
6705   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6706   token = cp_lexer_peek_token (parser->lexer);
6707
6708   /* Build the (type *)null that begins the traditional offsetof macro.  */
6709   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6710                             tf_warning_or_error);
6711
6712   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6713   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6714                                                  true, &dummy, token->location);
6715   while (true)
6716     {
6717       token = cp_lexer_peek_token (parser->lexer);
6718       switch (token->type)
6719         {
6720         case CPP_OPEN_SQUARE:
6721           /* offsetof-member-designator "[" expression "]" */
6722           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6723           break;
6724
6725         case CPP_DEREF:
6726           /* offsetof-member-designator "->" identifier */
6727           expr = grok_array_decl (expr, integer_zero_node);
6728           /* FALLTHRU */
6729
6730         case CPP_DOT:
6731           /* offsetof-member-designator "." identifier */
6732           cp_lexer_consume_token (parser->lexer);
6733           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6734                                                          expr, true, &dummy,
6735                                                          token->location);
6736           break;
6737
6738         case CPP_CLOSE_PAREN:
6739           /* Consume the ")" token.  */
6740           cp_lexer_consume_token (parser->lexer);
6741           goto success;
6742
6743         default:
6744           /* Error.  We know the following require will fail, but
6745              that gives the proper error message.  */
6746           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6747           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6748           expr = error_mark_node;
6749           goto failure;
6750         }
6751     }
6752
6753  success:
6754   /* If we're processing a template, we can't finish the semantics yet.
6755      Otherwise we can fold the entire expression now.  */
6756   if (processing_template_decl)
6757     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6758   else
6759     expr = finish_offsetof (expr);
6760
6761  failure:
6762   parser->integral_constant_expression_p = save_ice_p;
6763   parser->non_integral_constant_expression_p = save_non_ice_p;
6764
6765   return expr;
6766 }
6767
6768 /* Parse a trait expression.  */
6769
6770 static tree
6771 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6772 {
6773   cp_trait_kind kind;
6774   tree type1, type2 = NULL_TREE;
6775   bool binary = false;
6776   cp_decl_specifier_seq decl_specs;
6777
6778   switch (keyword)
6779     {
6780     case RID_HAS_NOTHROW_ASSIGN:
6781       kind = CPTK_HAS_NOTHROW_ASSIGN;
6782       break;
6783     case RID_HAS_NOTHROW_CONSTRUCTOR:
6784       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6785       break;
6786     case RID_HAS_NOTHROW_COPY:
6787       kind = CPTK_HAS_NOTHROW_COPY;
6788       break;
6789     case RID_HAS_TRIVIAL_ASSIGN:
6790       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6791       break;
6792     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6793       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6794       break;
6795     case RID_HAS_TRIVIAL_COPY:
6796       kind = CPTK_HAS_TRIVIAL_COPY;
6797       break;
6798     case RID_HAS_TRIVIAL_DESTRUCTOR:
6799       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6800       break;
6801     case RID_HAS_VIRTUAL_DESTRUCTOR:
6802       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6803       break;
6804     case RID_IS_ABSTRACT:
6805       kind = CPTK_IS_ABSTRACT;
6806       break;
6807     case RID_IS_BASE_OF:
6808       kind = CPTK_IS_BASE_OF;
6809       binary = true;
6810       break;
6811     case RID_IS_CLASS:
6812       kind = CPTK_IS_CLASS;
6813       break;
6814     case RID_IS_CONVERTIBLE_TO:
6815       kind = CPTK_IS_CONVERTIBLE_TO;
6816       binary = true;
6817       break;
6818     case RID_IS_EMPTY:
6819       kind = CPTK_IS_EMPTY;
6820       break;
6821     case RID_IS_ENUM:
6822       kind = CPTK_IS_ENUM;
6823       break;
6824     case RID_IS_POD:
6825       kind = CPTK_IS_POD;
6826       break;
6827     case RID_IS_POLYMORPHIC:
6828       kind = CPTK_IS_POLYMORPHIC;
6829       break;
6830     case RID_IS_UNION:
6831       kind = CPTK_IS_UNION;
6832       break;
6833     default:
6834       gcc_unreachable ();
6835     }
6836
6837   /* Consume the token.  */
6838   cp_lexer_consume_token (parser->lexer);
6839
6840   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6841
6842   type1 = cp_parser_type_id (parser);
6843
6844   if (type1 == error_mark_node)
6845     return error_mark_node;
6846
6847   /* Build a trivial decl-specifier-seq.  */
6848   clear_decl_specs (&decl_specs);
6849   decl_specs.type = type1;
6850
6851   /* Call grokdeclarator to figure out what type this is.  */
6852   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6853                           /*initialized=*/0, /*attrlist=*/NULL);
6854
6855   if (binary)
6856     {
6857       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6858  
6859       type2 = cp_parser_type_id (parser);
6860
6861       if (type2 == error_mark_node)
6862         return error_mark_node;
6863
6864       /* Build a trivial decl-specifier-seq.  */
6865       clear_decl_specs (&decl_specs);
6866       decl_specs.type = type2;
6867
6868       /* Call grokdeclarator to figure out what type this is.  */
6869       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6870                               /*initialized=*/0, /*attrlist=*/NULL);
6871     }
6872
6873   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6874
6875   /* Complete the trait expression, which may mean either processing
6876      the trait expr now or saving it for template instantiation.  */
6877   return finish_trait_expr (kind, type1, type2);
6878 }
6879
6880 /* Statements [gram.stmt.stmt]  */
6881
6882 /* Parse a statement.
6883
6884    statement:
6885      labeled-statement
6886      expression-statement
6887      compound-statement
6888      selection-statement
6889      iteration-statement
6890      jump-statement
6891      declaration-statement
6892      try-block
6893
6894   IN_COMPOUND is true when the statement is nested inside a
6895   cp_parser_compound_statement; this matters for certain pragmas.
6896
6897   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6898   is a (possibly labeled) if statement which is not enclosed in braces
6899   and has an else clause.  This is used to implement -Wparentheses.  */
6900
6901 static void
6902 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6903                      bool in_compound, bool *if_p)
6904 {
6905   tree statement;
6906   cp_token *token;
6907   location_t statement_location;
6908
6909  restart:
6910   if (if_p != NULL)
6911     *if_p = false;
6912   /* There is no statement yet.  */
6913   statement = NULL_TREE;
6914   /* Peek at the next token.  */
6915   token = cp_lexer_peek_token (parser->lexer);
6916   /* Remember the location of the first token in the statement.  */
6917   statement_location = token->location;
6918   /* If this is a keyword, then that will often determine what kind of
6919      statement we have.  */
6920   if (token->type == CPP_KEYWORD)
6921     {
6922       enum rid keyword = token->keyword;
6923
6924       switch (keyword)
6925         {
6926         case RID_CASE:
6927         case RID_DEFAULT:
6928           /* Looks like a labeled-statement with a case label.
6929              Parse the label, and then use tail recursion to parse
6930              the statement.  */
6931           cp_parser_label_for_labeled_statement (parser);
6932           goto restart;
6933
6934         case RID_IF:
6935         case RID_SWITCH:
6936           statement = cp_parser_selection_statement (parser, if_p);
6937           break;
6938
6939         case RID_WHILE:
6940         case RID_DO:
6941         case RID_FOR:
6942           statement = cp_parser_iteration_statement (parser);
6943           break;
6944
6945         case RID_BREAK:
6946         case RID_CONTINUE:
6947         case RID_RETURN:
6948         case RID_GOTO:
6949           statement = cp_parser_jump_statement (parser);
6950           break;
6951
6952           /* Objective-C++ exception-handling constructs.  */
6953         case RID_AT_TRY:
6954         case RID_AT_CATCH:
6955         case RID_AT_FINALLY:
6956         case RID_AT_SYNCHRONIZED:
6957         case RID_AT_THROW:
6958           statement = cp_parser_objc_statement (parser);
6959           break;
6960
6961         case RID_TRY:
6962           statement = cp_parser_try_block (parser);
6963           break;
6964
6965         case RID_NAMESPACE:
6966           /* This must be a namespace alias definition.  */
6967           cp_parser_declaration_statement (parser);
6968           return;
6969           
6970         default:
6971           /* It might be a keyword like `int' that can start a
6972              declaration-statement.  */
6973           break;
6974         }
6975     }
6976   else if (token->type == CPP_NAME)
6977     {
6978       /* If the next token is a `:', then we are looking at a
6979          labeled-statement.  */
6980       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6981       if (token->type == CPP_COLON)
6982         {
6983           /* Looks like a labeled-statement with an ordinary label.
6984              Parse the label, and then use tail recursion to parse
6985              the statement.  */
6986           cp_parser_label_for_labeled_statement (parser);
6987           goto restart;
6988         }
6989     }
6990   /* Anything that starts with a `{' must be a compound-statement.  */
6991   else if (token->type == CPP_OPEN_BRACE)
6992     statement = cp_parser_compound_statement (parser, NULL, false);
6993   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6994      a statement all its own.  */
6995   else if (token->type == CPP_PRAGMA)
6996     {
6997       /* Only certain OpenMP pragmas are attached to statements, and thus
6998          are considered statements themselves.  All others are not.  In
6999          the context of a compound, accept the pragma as a "statement" and
7000          return so that we can check for a close brace.  Otherwise we
7001          require a real statement and must go back and read one.  */
7002       if (in_compound)
7003         cp_parser_pragma (parser, pragma_compound);
7004       else if (!cp_parser_pragma (parser, pragma_stmt))
7005         goto restart;
7006       return;
7007     }
7008   else if (token->type == CPP_EOF)
7009     {
7010       cp_parser_error (parser, "expected statement");
7011       return;
7012     }
7013
7014   /* Everything else must be a declaration-statement or an
7015      expression-statement.  Try for the declaration-statement
7016      first, unless we are looking at a `;', in which case we know that
7017      we have an expression-statement.  */
7018   if (!statement)
7019     {
7020       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7021         {
7022           cp_parser_parse_tentatively (parser);
7023           /* Try to parse the declaration-statement.  */
7024           cp_parser_declaration_statement (parser);
7025           /* If that worked, we're done.  */
7026           if (cp_parser_parse_definitely (parser))
7027             return;
7028         }
7029       /* Look for an expression-statement instead.  */
7030       statement = cp_parser_expression_statement (parser, in_statement_expr);
7031     }
7032
7033   /* Set the line number for the statement.  */
7034   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7035     SET_EXPR_LOCATION (statement, statement_location);
7036 }
7037
7038 /* Parse the label for a labeled-statement, i.e.
7039
7040    identifier :
7041    case constant-expression :
7042    default :
7043
7044    GNU Extension:
7045    case constant-expression ... constant-expression : statement
7046
7047    When a label is parsed without errors, the label is added to the
7048    parse tree by the finish_* functions, so this function doesn't
7049    have to return the label.  */
7050
7051 static void
7052 cp_parser_label_for_labeled_statement (cp_parser* parser)
7053 {
7054   cp_token *token;
7055   tree label = NULL_TREE;
7056
7057   /* The next token should be an identifier.  */
7058   token = cp_lexer_peek_token (parser->lexer);
7059   if (token->type != CPP_NAME
7060       && token->type != CPP_KEYWORD)
7061     {
7062       cp_parser_error (parser, "expected labeled-statement");
7063       return;
7064     }
7065
7066   switch (token->keyword)
7067     {
7068     case RID_CASE:
7069       {
7070         tree expr, expr_hi;
7071         cp_token *ellipsis;
7072
7073         /* Consume the `case' token.  */
7074         cp_lexer_consume_token (parser->lexer);
7075         /* Parse the constant-expression.  */
7076         expr = cp_parser_constant_expression (parser,
7077                                               /*allow_non_constant_p=*/false,
7078                                               NULL);
7079
7080         ellipsis = cp_lexer_peek_token (parser->lexer);
7081         if (ellipsis->type == CPP_ELLIPSIS)
7082           {
7083             /* Consume the `...' token.  */
7084             cp_lexer_consume_token (parser->lexer);
7085             expr_hi =
7086               cp_parser_constant_expression (parser,
7087                                              /*allow_non_constant_p=*/false,
7088                                              NULL);
7089             /* We don't need to emit warnings here, as the common code
7090                will do this for us.  */
7091           }
7092         else
7093           expr_hi = NULL_TREE;
7094
7095         if (parser->in_switch_statement_p)
7096           finish_case_label (expr, expr_hi);
7097         else
7098           error ("%Hcase label %qE not within a switch statement",
7099                  &token->location, expr);
7100       }
7101       break;
7102
7103     case RID_DEFAULT:
7104       /* Consume the `default' token.  */
7105       cp_lexer_consume_token (parser->lexer);
7106
7107       if (parser->in_switch_statement_p)
7108         finish_case_label (NULL_TREE, NULL_TREE);
7109       else
7110         error ("%Hcase label not within a switch statement", &token->location);
7111       break;
7112
7113     default:
7114       /* Anything else must be an ordinary label.  */
7115       label = finish_label_stmt (cp_parser_identifier (parser));
7116       break;
7117     }
7118
7119   /* Require the `:' token.  */
7120   cp_parser_require (parser, CPP_COLON, "%<:%>");
7121
7122   /* An ordinary label may optionally be followed by attributes.
7123      However, this is only permitted if the attributes are then
7124      followed by a semicolon.  This is because, for backward
7125      compatibility, when parsing
7126        lab: __attribute__ ((unused)) int i;
7127      we want the attribute to attach to "i", not "lab".  */
7128   if (label != NULL_TREE
7129       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7130     {
7131       tree attrs;
7132
7133       cp_parser_parse_tentatively (parser);
7134       attrs = cp_parser_attributes_opt (parser);
7135       if (attrs == NULL_TREE
7136           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7137         cp_parser_abort_tentative_parse (parser);
7138       else if (!cp_parser_parse_definitely (parser))
7139         ;
7140       else
7141         cplus_decl_attributes (&label, attrs, 0);
7142     }
7143 }
7144
7145 /* Parse an expression-statement.
7146
7147    expression-statement:
7148      expression [opt] ;
7149
7150    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7151    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7152    indicates whether this expression-statement is part of an
7153    expression statement.  */
7154
7155 static tree
7156 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7157 {
7158   tree statement = NULL_TREE;
7159
7160   /* If the next token is a ';', then there is no expression
7161      statement.  */
7162   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7163     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7164
7165   /* Consume the final `;'.  */
7166   cp_parser_consume_semicolon_at_end_of_statement (parser);
7167
7168   if (in_statement_expr
7169       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7170     /* This is the final expression statement of a statement
7171        expression.  */
7172     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7173   else if (statement)
7174     statement = finish_expr_stmt (statement);
7175   else
7176     finish_stmt ();
7177
7178   return statement;
7179 }
7180
7181 /* Parse a compound-statement.
7182
7183    compound-statement:
7184      { statement-seq [opt] }
7185
7186    GNU extension:
7187
7188    compound-statement:
7189      { label-declaration-seq [opt] statement-seq [opt] }
7190
7191    label-declaration-seq:
7192      label-declaration
7193      label-declaration-seq label-declaration
7194
7195    Returns a tree representing the statement.  */
7196
7197 static tree
7198 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7199                               bool in_try)
7200 {
7201   tree compound_stmt;
7202
7203   /* Consume the `{'.  */
7204   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7205     return error_mark_node;
7206   /* Begin the compound-statement.  */
7207   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7208   /* If the next keyword is `__label__' we have a label declaration.  */
7209   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7210     cp_parser_label_declaration (parser);
7211   /* Parse an (optional) statement-seq.  */
7212   cp_parser_statement_seq_opt (parser, in_statement_expr);
7213   /* Finish the compound-statement.  */
7214   finish_compound_stmt (compound_stmt);
7215   /* Consume the `}'.  */
7216   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7217
7218   return compound_stmt;
7219 }
7220
7221 /* Parse an (optional) statement-seq.
7222
7223    statement-seq:
7224      statement
7225      statement-seq [opt] statement  */
7226
7227 static void
7228 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7229 {
7230   /* Scan statements until there aren't any more.  */
7231   while (true)
7232     {
7233       cp_token *token = cp_lexer_peek_token (parser->lexer);
7234
7235       /* If we're looking at a `}', then we've run out of statements.  */
7236       if (token->type == CPP_CLOSE_BRACE
7237           || token->type == CPP_EOF
7238           || token->type == CPP_PRAGMA_EOL)
7239         break;
7240       
7241       /* If we are in a compound statement and find 'else' then
7242          something went wrong.  */
7243       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7244         {
7245           if (parser->in_statement & IN_IF_STMT) 
7246             break;
7247           else
7248             {
7249               token = cp_lexer_consume_token (parser->lexer);
7250               error ("%H%<else%> without a previous %<if%>", &token->location);
7251             }
7252         }
7253
7254       /* Parse the statement.  */
7255       cp_parser_statement (parser, in_statement_expr, true, NULL);
7256     }
7257 }
7258
7259 /* Parse a selection-statement.
7260
7261    selection-statement:
7262      if ( condition ) statement
7263      if ( condition ) statement else statement
7264      switch ( condition ) statement
7265
7266    Returns the new IF_STMT or SWITCH_STMT.
7267
7268    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7269    is a (possibly labeled) if statement which is not enclosed in
7270    braces and has an else clause.  This is used to implement
7271    -Wparentheses.  */
7272
7273 static tree
7274 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7275 {
7276   cp_token *token;
7277   enum rid keyword;
7278
7279   if (if_p != NULL)
7280     *if_p = false;
7281
7282   /* Peek at the next token.  */
7283   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7284
7285   /* See what kind of keyword it is.  */
7286   keyword = token->keyword;
7287   switch (keyword)
7288     {
7289     case RID_IF:
7290     case RID_SWITCH:
7291       {
7292         tree statement;
7293         tree condition;
7294
7295         /* Look for the `('.  */
7296         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7297           {
7298             cp_parser_skip_to_end_of_statement (parser);
7299             return error_mark_node;
7300           }
7301
7302         /* Begin the selection-statement.  */
7303         if (keyword == RID_IF)
7304           statement = begin_if_stmt ();
7305         else
7306           statement = begin_switch_stmt ();
7307
7308         /* Parse the condition.  */
7309         condition = cp_parser_condition (parser);
7310         /* Look for the `)'.  */
7311         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7312           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7313                                                  /*consume_paren=*/true);
7314
7315         if (keyword == RID_IF)
7316           {
7317             bool nested_if;
7318             unsigned char in_statement;
7319
7320             /* Add the condition.  */
7321             finish_if_stmt_cond (condition, statement);
7322
7323             /* Parse the then-clause.  */
7324             in_statement = parser->in_statement;
7325             parser->in_statement |= IN_IF_STMT;
7326             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7327               {
7328                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7329                 add_stmt (build_empty_stmt ());
7330                 cp_lexer_consume_token (parser->lexer);
7331                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7332                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7333                               "empty body in an %<if%> statement");
7334                 nested_if = false;
7335               }
7336             else
7337               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7338             parser->in_statement = in_statement;
7339
7340             finish_then_clause (statement);
7341
7342             /* If the next token is `else', parse the else-clause.  */
7343             if (cp_lexer_next_token_is_keyword (parser->lexer,
7344                                                 RID_ELSE))
7345               {
7346                 /* Consume the `else' keyword.  */
7347                 cp_lexer_consume_token (parser->lexer);
7348                 begin_else_clause (statement);
7349                 /* Parse the else-clause.  */
7350                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7351                   {
7352                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7353                                 OPT_Wempty_body, "suggest braces around "
7354                                 "empty body in an %<else%> statement");
7355                     add_stmt (build_empty_stmt ());
7356                     cp_lexer_consume_token (parser->lexer);
7357                   }
7358                 else
7359                   cp_parser_implicitly_scoped_statement (parser, NULL);
7360
7361                 finish_else_clause (statement);
7362
7363                 /* If we are currently parsing a then-clause, then
7364                    IF_P will not be NULL.  We set it to true to
7365                    indicate that this if statement has an else clause.
7366                    This may trigger the Wparentheses warning below
7367                    when we get back up to the parent if statement.  */
7368                 if (if_p != NULL)
7369                   *if_p = true;
7370               }
7371             else
7372               {
7373                 /* This if statement does not have an else clause.  If
7374                    NESTED_IF is true, then the then-clause is an if
7375                    statement which does have an else clause.  We warn
7376                    about the potential ambiguity.  */
7377                 if (nested_if)
7378                   warning (OPT_Wparentheses,
7379                            ("%Hsuggest explicit braces "
7380                             "to avoid ambiguous %<else%>"),
7381                            EXPR_LOCUS (statement));
7382               }
7383
7384             /* Now we're all done with the if-statement.  */
7385             finish_if_stmt (statement);
7386           }
7387         else
7388           {
7389             bool in_switch_statement_p;
7390             unsigned char in_statement;
7391
7392             /* Add the condition.  */
7393             finish_switch_cond (condition, statement);
7394
7395             /* Parse the body of the switch-statement.  */
7396             in_switch_statement_p = parser->in_switch_statement_p;
7397             in_statement = parser->in_statement;
7398             parser->in_switch_statement_p = true;
7399             parser->in_statement |= IN_SWITCH_STMT;
7400             cp_parser_implicitly_scoped_statement (parser, NULL);
7401             parser->in_switch_statement_p = in_switch_statement_p;
7402             parser->in_statement = in_statement;
7403
7404             /* Now we're all done with the switch-statement.  */
7405             finish_switch_stmt (statement);
7406           }
7407
7408         return statement;
7409       }
7410       break;
7411
7412     default:
7413       cp_parser_error (parser, "expected selection-statement");
7414       return error_mark_node;
7415     }
7416 }
7417
7418 /* Parse a condition.
7419
7420    condition:
7421      expression
7422      type-specifier-seq declarator = initializer-clause
7423      type-specifier-seq declarator braced-init-list
7424
7425    GNU Extension:
7426
7427    condition:
7428      type-specifier-seq declarator asm-specification [opt]
7429        attributes [opt] = assignment-expression
7430
7431    Returns the expression that should be tested.  */
7432
7433 static tree
7434 cp_parser_condition (cp_parser* parser)
7435 {
7436   cp_decl_specifier_seq type_specifiers;
7437   const char *saved_message;
7438
7439   /* Try the declaration first.  */
7440   cp_parser_parse_tentatively (parser);
7441   /* New types are not allowed in the type-specifier-seq for a
7442      condition.  */
7443   saved_message = parser->type_definition_forbidden_message;
7444   parser->type_definition_forbidden_message
7445     = "types may not be defined in conditions";
7446   /* Parse the type-specifier-seq.  */
7447   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7448                                 &type_specifiers);
7449   /* Restore the saved message.  */
7450   parser->type_definition_forbidden_message = saved_message;
7451   /* If all is well, we might be looking at a declaration.  */
7452   if (!cp_parser_error_occurred (parser))
7453     {
7454       tree decl;
7455       tree asm_specification;
7456       tree attributes;
7457       cp_declarator *declarator;
7458       tree initializer = NULL_TREE;
7459
7460       /* Parse the declarator.  */
7461       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7462                                          /*ctor_dtor_or_conv_p=*/NULL,
7463                                          /*parenthesized_p=*/NULL,
7464                                          /*member_p=*/false);
7465       /* Parse the attributes.  */
7466       attributes = cp_parser_attributes_opt (parser);
7467       /* Parse the asm-specification.  */
7468       asm_specification = cp_parser_asm_specification_opt (parser);
7469       /* If the next token is not an `=' or '{', then we might still be
7470          looking at an expression.  For example:
7471
7472            if (A(a).x)
7473
7474          looks like a decl-specifier-seq and a declarator -- but then
7475          there is no `=', so this is an expression.  */
7476       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7477           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7478         cp_parser_simulate_error (parser);
7479         
7480       /* If we did see an `=' or '{', then we are looking at a declaration
7481          for sure.  */
7482       if (cp_parser_parse_definitely (parser))
7483         {
7484           tree pushed_scope;
7485           bool non_constant_p;
7486           bool flags = LOOKUP_ONLYCONVERTING;
7487
7488           /* Create the declaration.  */
7489           decl = start_decl (declarator, &type_specifiers,
7490                              /*initialized_p=*/true,
7491                              attributes, /*prefix_attributes=*/NULL_TREE,
7492                              &pushed_scope);
7493
7494           /* Parse the initializer.  */
7495           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7496             {
7497               initializer = cp_parser_braced_list (parser, &non_constant_p);
7498               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7499               flags = 0;
7500             }
7501           else
7502             {
7503               /* Consume the `='.  */
7504               cp_parser_require (parser, CPP_EQ, "%<=%>");
7505               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7506             }
7507           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7508             maybe_warn_cpp0x ("extended initializer lists");
7509
7510           if (!non_constant_p)
7511             initializer = fold_non_dependent_expr (initializer);
7512
7513           /* Process the initializer.  */
7514           cp_finish_decl (decl,
7515                           initializer, !non_constant_p,
7516                           asm_specification,
7517                           flags);
7518
7519           if (pushed_scope)
7520             pop_scope (pushed_scope);
7521
7522           return convert_from_reference (decl);
7523         }
7524     }
7525   /* If we didn't even get past the declarator successfully, we are
7526      definitely not looking at a declaration.  */
7527   else
7528     cp_parser_abort_tentative_parse (parser);
7529
7530   /* Otherwise, we are looking at an expression.  */
7531   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7532 }
7533
7534 /* Parse an iteration-statement.
7535
7536    iteration-statement:
7537      while ( condition ) statement
7538      do statement while ( expression ) ;
7539      for ( for-init-statement condition [opt] ; expression [opt] )
7540        statement
7541
7542    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7543
7544 static tree
7545 cp_parser_iteration_statement (cp_parser* parser)
7546 {
7547   cp_token *token;
7548   enum rid keyword;
7549   tree statement;
7550   unsigned char in_statement;
7551
7552   /* Peek at the next token.  */
7553   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7554   if (!token)
7555     return error_mark_node;
7556
7557   /* Remember whether or not we are already within an iteration
7558      statement.  */
7559   in_statement = parser->in_statement;
7560
7561   /* See what kind of keyword it is.  */
7562   keyword = token->keyword;
7563   switch (keyword)
7564     {
7565     case RID_WHILE:
7566       {
7567         tree condition;
7568
7569         /* Begin the while-statement.  */
7570         statement = begin_while_stmt ();
7571         /* Look for the `('.  */
7572         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7573         /* Parse the condition.  */
7574         condition = cp_parser_condition (parser);
7575         finish_while_stmt_cond (condition, statement);
7576         /* Look for the `)'.  */
7577         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7578         /* Parse the dependent statement.  */
7579         parser->in_statement = IN_ITERATION_STMT;
7580         cp_parser_already_scoped_statement (parser);
7581         parser->in_statement = in_statement;
7582         /* We're done with the while-statement.  */
7583         finish_while_stmt (statement);
7584       }
7585       break;
7586
7587     case RID_DO:
7588       {
7589         tree expression;
7590
7591         /* Begin the do-statement.  */
7592         statement = begin_do_stmt ();
7593         /* Parse the body of the do-statement.  */
7594         parser->in_statement = IN_ITERATION_STMT;
7595         cp_parser_implicitly_scoped_statement (parser, NULL);
7596         parser->in_statement = in_statement;
7597         finish_do_body (statement);
7598         /* Look for the `while' keyword.  */
7599         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7600         /* Look for the `('.  */
7601         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7602         /* Parse the expression.  */
7603         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7604         /* We're done with the do-statement.  */
7605         finish_do_stmt (expression, statement);
7606         /* Look for the `)'.  */
7607         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7608         /* Look for the `;'.  */
7609         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7610       }
7611       break;
7612
7613     case RID_FOR:
7614       {
7615         tree condition = NULL_TREE;
7616         tree expression = NULL_TREE;
7617
7618         /* Begin the for-statement.  */
7619         statement = begin_for_stmt ();
7620         /* Look for the `('.  */
7621         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7622         /* Parse the initialization.  */
7623         cp_parser_for_init_statement (parser);
7624         finish_for_init_stmt (statement);
7625
7626         /* If there's a condition, process it.  */
7627         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7628           condition = cp_parser_condition (parser);
7629         finish_for_cond (condition, statement);
7630         /* Look for the `;'.  */
7631         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7632
7633         /* If there's an expression, process it.  */
7634         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7635           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7636         finish_for_expr (expression, statement);
7637         /* Look for the `)'.  */
7638         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7639
7640         /* Parse the body of the for-statement.  */
7641         parser->in_statement = IN_ITERATION_STMT;
7642         cp_parser_already_scoped_statement (parser);
7643         parser->in_statement = in_statement;
7644
7645         /* We're done with the for-statement.  */
7646         finish_for_stmt (statement);
7647       }
7648       break;
7649
7650     default:
7651       cp_parser_error (parser, "expected iteration-statement");
7652       statement = error_mark_node;
7653       break;
7654     }
7655
7656   return statement;
7657 }
7658
7659 /* Parse a for-init-statement.
7660
7661    for-init-statement:
7662      expression-statement
7663      simple-declaration  */
7664
7665 static void
7666 cp_parser_for_init_statement (cp_parser* parser)
7667 {
7668   /* If the next token is a `;', then we have an empty
7669      expression-statement.  Grammatically, this is also a
7670      simple-declaration, but an invalid one, because it does not
7671      declare anything.  Therefore, if we did not handle this case
7672      specially, we would issue an error message about an invalid
7673      declaration.  */
7674   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7675     {
7676       /* We're going to speculatively look for a declaration, falling back
7677          to an expression, if necessary.  */
7678       cp_parser_parse_tentatively (parser);
7679       /* Parse the declaration.  */
7680       cp_parser_simple_declaration (parser,
7681                                     /*function_definition_allowed_p=*/false);
7682       /* If the tentative parse failed, then we shall need to look for an
7683          expression-statement.  */
7684       if (cp_parser_parse_definitely (parser))
7685         return;
7686     }
7687
7688   cp_parser_expression_statement (parser, false);
7689 }
7690
7691 /* Parse a jump-statement.
7692
7693    jump-statement:
7694      break ;
7695      continue ;
7696      return expression [opt] ;
7697      return braced-init-list ;
7698      goto identifier ;
7699
7700    GNU extension:
7701
7702    jump-statement:
7703      goto * expression ;
7704
7705    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7706
7707 static tree
7708 cp_parser_jump_statement (cp_parser* parser)
7709 {
7710   tree statement = error_mark_node;
7711   cp_token *token;
7712   enum rid keyword;
7713   unsigned char in_statement;
7714
7715   /* Peek at the next token.  */
7716   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7717   if (!token)
7718     return error_mark_node;
7719
7720   /* See what kind of keyword it is.  */
7721   keyword = token->keyword;
7722   switch (keyword)
7723     {
7724     case RID_BREAK:
7725       in_statement = parser->in_statement & ~IN_IF_STMT;      
7726       switch (in_statement)
7727         {
7728         case 0:
7729           error ("%Hbreak statement not within loop or switch", &token->location);
7730           break;
7731         default:
7732           gcc_assert ((in_statement & IN_SWITCH_STMT)
7733                       || in_statement == IN_ITERATION_STMT);
7734           statement = finish_break_stmt ();
7735           break;
7736         case IN_OMP_BLOCK:
7737           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7738           break;
7739         case IN_OMP_FOR:
7740           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7741           break;
7742         }
7743       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7744       break;
7745
7746     case RID_CONTINUE:
7747       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7748         {
7749         case 0:
7750           error ("%Hcontinue statement not within a loop", &token->location);
7751           break;
7752         case IN_ITERATION_STMT:
7753         case IN_OMP_FOR:
7754           statement = finish_continue_stmt ();
7755           break;
7756         case IN_OMP_BLOCK:
7757           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7758           break;
7759         default:
7760           gcc_unreachable ();
7761         }
7762       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7763       break;
7764
7765     case RID_RETURN:
7766       {
7767         tree expr;
7768         bool expr_non_constant_p;
7769
7770         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7771           {
7772             maybe_warn_cpp0x ("extended initializer lists");
7773             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7774           }
7775         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7776           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7777         else
7778           /* If the next token is a `;', then there is no
7779              expression.  */
7780           expr = NULL_TREE;
7781         /* Build the return-statement.  */
7782         statement = finish_return_stmt (expr);
7783         /* Look for the final `;'.  */
7784         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7785       }
7786       break;
7787
7788     case RID_GOTO:
7789       /* Create the goto-statement.  */
7790       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7791         {
7792           /* Issue a warning about this use of a GNU extension.  */
7793           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7794           /* Consume the '*' token.  */
7795           cp_lexer_consume_token (parser->lexer);
7796           /* Parse the dependent expression.  */
7797           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7798         }
7799       else
7800         finish_goto_stmt (cp_parser_identifier (parser));
7801       /* Look for the final `;'.  */
7802       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7803       break;
7804
7805     default:
7806       cp_parser_error (parser, "expected jump-statement");
7807       break;
7808     }
7809
7810   return statement;
7811 }
7812
7813 /* Parse a declaration-statement.
7814
7815    declaration-statement:
7816      block-declaration  */
7817
7818 static void
7819 cp_parser_declaration_statement (cp_parser* parser)
7820 {
7821   void *p;
7822
7823   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7824   p = obstack_alloc (&declarator_obstack, 0);
7825
7826  /* Parse the block-declaration.  */
7827   cp_parser_block_declaration (parser, /*statement_p=*/true);
7828
7829   /* Free any declarators allocated.  */
7830   obstack_free (&declarator_obstack, p);
7831
7832   /* Finish off the statement.  */
7833   finish_stmt ();
7834 }
7835
7836 /* Some dependent statements (like `if (cond) statement'), are
7837    implicitly in their own scope.  In other words, if the statement is
7838    a single statement (as opposed to a compound-statement), it is
7839    none-the-less treated as if it were enclosed in braces.  Any
7840    declarations appearing in the dependent statement are out of scope
7841    after control passes that point.  This function parses a statement,
7842    but ensures that is in its own scope, even if it is not a
7843    compound-statement.
7844
7845    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7846    is a (possibly labeled) if statement which is not enclosed in
7847    braces and has an else clause.  This is used to implement
7848    -Wparentheses.
7849
7850    Returns the new statement.  */
7851
7852 static tree
7853 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7854 {
7855   tree statement;
7856
7857   if (if_p != NULL)
7858     *if_p = false;
7859
7860   /* Mark if () ; with a special NOP_EXPR.  */
7861   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7862     {
7863       cp_lexer_consume_token (parser->lexer);
7864       statement = add_stmt (build_empty_stmt ());
7865     }
7866   /* if a compound is opened, we simply parse the statement directly.  */
7867   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7868     statement = cp_parser_compound_statement (parser, NULL, false);
7869   /* If the token is not a `{', then we must take special action.  */
7870   else
7871     {
7872       /* Create a compound-statement.  */
7873       statement = begin_compound_stmt (0);
7874       /* Parse the dependent-statement.  */
7875       cp_parser_statement (parser, NULL_TREE, false, if_p);
7876       /* Finish the dummy compound-statement.  */
7877       finish_compound_stmt (statement);
7878     }
7879
7880   /* Return the statement.  */
7881   return statement;
7882 }
7883
7884 /* For some dependent statements (like `while (cond) statement'), we
7885    have already created a scope.  Therefore, even if the dependent
7886    statement is a compound-statement, we do not want to create another
7887    scope.  */
7888
7889 static void
7890 cp_parser_already_scoped_statement (cp_parser* parser)
7891 {
7892   /* If the token is a `{', then we must take special action.  */
7893   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7894     cp_parser_statement (parser, NULL_TREE, false, NULL);
7895   else
7896     {
7897       /* Avoid calling cp_parser_compound_statement, so that we
7898          don't create a new scope.  Do everything else by hand.  */
7899       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7900       /* If the next keyword is `__label__' we have a label declaration.  */
7901       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7902         cp_parser_label_declaration (parser);
7903       /* Parse an (optional) statement-seq.  */
7904       cp_parser_statement_seq_opt (parser, NULL_TREE);
7905       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7906     }
7907 }
7908
7909 /* Declarations [gram.dcl.dcl] */
7910
7911 /* Parse an optional declaration-sequence.
7912
7913    declaration-seq:
7914      declaration
7915      declaration-seq declaration  */
7916
7917 static void
7918 cp_parser_declaration_seq_opt (cp_parser* parser)
7919 {
7920   while (true)
7921     {
7922       cp_token *token;
7923
7924       token = cp_lexer_peek_token (parser->lexer);
7925
7926       if (token->type == CPP_CLOSE_BRACE
7927           || token->type == CPP_EOF
7928           || token->type == CPP_PRAGMA_EOL)
7929         break;
7930
7931       if (token->type == CPP_SEMICOLON)
7932         {
7933           /* A declaration consisting of a single semicolon is
7934              invalid.  Allow it unless we're being pedantic.  */
7935           cp_lexer_consume_token (parser->lexer);
7936           if (!in_system_header)
7937             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7938           continue;
7939         }
7940
7941       /* If we're entering or exiting a region that's implicitly
7942          extern "C", modify the lang context appropriately.  */
7943       if (!parser->implicit_extern_c && token->implicit_extern_c)
7944         {
7945           push_lang_context (lang_name_c);
7946           parser->implicit_extern_c = true;
7947         }
7948       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7949         {
7950           pop_lang_context ();
7951           parser->implicit_extern_c = false;
7952         }
7953
7954       if (token->type == CPP_PRAGMA)
7955         {
7956           /* A top-level declaration can consist solely of a #pragma.
7957              A nested declaration cannot, so this is done here and not
7958              in cp_parser_declaration.  (A #pragma at block scope is
7959              handled in cp_parser_statement.)  */
7960           cp_parser_pragma (parser, pragma_external);
7961           continue;
7962         }
7963
7964       /* Parse the declaration itself.  */
7965       cp_parser_declaration (parser);
7966     }
7967 }
7968
7969 /* Parse a declaration.
7970
7971    declaration:
7972      block-declaration
7973      function-definition
7974      template-declaration
7975      explicit-instantiation
7976      explicit-specialization
7977      linkage-specification
7978      namespace-definition
7979
7980    GNU extension:
7981
7982    declaration:
7983       __extension__ declaration */
7984
7985 static void
7986 cp_parser_declaration (cp_parser* parser)
7987 {
7988   cp_token token1;
7989   cp_token token2;
7990   int saved_pedantic;
7991   void *p;
7992
7993   /* Check for the `__extension__' keyword.  */
7994   if (cp_parser_extension_opt (parser, &saved_pedantic))
7995     {
7996       /* Parse the qualified declaration.  */
7997       cp_parser_declaration (parser);
7998       /* Restore the PEDANTIC flag.  */
7999       pedantic = saved_pedantic;
8000
8001       return;
8002     }
8003
8004   /* Try to figure out what kind of declaration is present.  */
8005   token1 = *cp_lexer_peek_token (parser->lexer);
8006
8007   if (token1.type != CPP_EOF)
8008     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8009   else
8010     {
8011       token2.type = CPP_EOF;
8012       token2.keyword = RID_MAX;
8013     }
8014
8015   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8016   p = obstack_alloc (&declarator_obstack, 0);
8017
8018   /* If the next token is `extern' and the following token is a string
8019      literal, then we have a linkage specification.  */
8020   if (token1.keyword == RID_EXTERN
8021       && cp_parser_is_string_literal (&token2))
8022     cp_parser_linkage_specification (parser);
8023   /* If the next token is `template', then we have either a template
8024      declaration, an explicit instantiation, or an explicit
8025      specialization.  */
8026   else if (token1.keyword == RID_TEMPLATE)
8027     {
8028       /* `template <>' indicates a template specialization.  */
8029       if (token2.type == CPP_LESS
8030           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8031         cp_parser_explicit_specialization (parser);
8032       /* `template <' indicates a template declaration.  */
8033       else if (token2.type == CPP_LESS)
8034         cp_parser_template_declaration (parser, /*member_p=*/false);
8035       /* Anything else must be an explicit instantiation.  */
8036       else
8037         cp_parser_explicit_instantiation (parser);
8038     }
8039   /* If the next token is `export', then we have a template
8040      declaration.  */
8041   else if (token1.keyword == RID_EXPORT)
8042     cp_parser_template_declaration (parser, /*member_p=*/false);
8043   /* If the next token is `extern', 'static' or 'inline' and the one
8044      after that is `template', we have a GNU extended explicit
8045      instantiation directive.  */
8046   else if (cp_parser_allow_gnu_extensions_p (parser)
8047            && (token1.keyword == RID_EXTERN
8048                || token1.keyword == RID_STATIC
8049                || token1.keyword == RID_INLINE)
8050            && token2.keyword == RID_TEMPLATE)
8051     cp_parser_explicit_instantiation (parser);
8052   /* If the next token is `namespace', check for a named or unnamed
8053      namespace definition.  */
8054   else if (token1.keyword == RID_NAMESPACE
8055            && (/* A named namespace definition.  */
8056                (token2.type == CPP_NAME
8057                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8058                     != CPP_EQ))
8059                /* An unnamed namespace definition.  */
8060                || token2.type == CPP_OPEN_BRACE
8061                || token2.keyword == RID_ATTRIBUTE))
8062     cp_parser_namespace_definition (parser);
8063   /* An inline (associated) namespace definition.  */
8064   else if (token1.keyword == RID_INLINE
8065            && token2.keyword == RID_NAMESPACE)
8066     cp_parser_namespace_definition (parser);
8067   /* Objective-C++ declaration/definition.  */
8068   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8069     cp_parser_objc_declaration (parser);
8070   /* We must have either a block declaration or a function
8071      definition.  */
8072   else
8073     /* Try to parse a block-declaration, or a function-definition.  */
8074     cp_parser_block_declaration (parser, /*statement_p=*/false);
8075
8076   /* Free any declarators allocated.  */
8077   obstack_free (&declarator_obstack, p);
8078 }
8079
8080 /* Parse a block-declaration.
8081
8082    block-declaration:
8083      simple-declaration
8084      asm-definition
8085      namespace-alias-definition
8086      using-declaration
8087      using-directive
8088
8089    GNU Extension:
8090
8091    block-declaration:
8092      __extension__ block-declaration
8093
8094    C++0x Extension:
8095
8096    block-declaration:
8097      static_assert-declaration
8098
8099    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8100    part of a declaration-statement.  */
8101
8102 static void
8103 cp_parser_block_declaration (cp_parser *parser,
8104                              bool      statement_p)
8105 {
8106   cp_token *token1;
8107   int saved_pedantic;
8108
8109   /* Check for the `__extension__' keyword.  */
8110   if (cp_parser_extension_opt (parser, &saved_pedantic))
8111     {
8112       /* Parse the qualified declaration.  */
8113       cp_parser_block_declaration (parser, statement_p);
8114       /* Restore the PEDANTIC flag.  */
8115       pedantic = saved_pedantic;
8116
8117       return;
8118     }
8119
8120   /* Peek at the next token to figure out which kind of declaration is
8121      present.  */
8122   token1 = cp_lexer_peek_token (parser->lexer);
8123
8124   /* If the next keyword is `asm', we have an asm-definition.  */
8125   if (token1->keyword == RID_ASM)
8126     {
8127       if (statement_p)
8128         cp_parser_commit_to_tentative_parse (parser);
8129       cp_parser_asm_definition (parser);
8130     }
8131   /* If the next keyword is `namespace', we have a
8132      namespace-alias-definition.  */
8133   else if (token1->keyword == RID_NAMESPACE)
8134     cp_parser_namespace_alias_definition (parser);
8135   /* If the next keyword is `using', we have either a
8136      using-declaration or a using-directive.  */
8137   else if (token1->keyword == RID_USING)
8138     {
8139       cp_token *token2;
8140
8141       if (statement_p)
8142         cp_parser_commit_to_tentative_parse (parser);
8143       /* If the token after `using' is `namespace', then we have a
8144          using-directive.  */
8145       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8146       if (token2->keyword == RID_NAMESPACE)
8147         cp_parser_using_directive (parser);
8148       /* Otherwise, it's a using-declaration.  */
8149       else
8150         cp_parser_using_declaration (parser,
8151                                      /*access_declaration_p=*/false);
8152     }
8153   /* If the next keyword is `__label__' we have a misplaced label
8154      declaration.  */
8155   else if (token1->keyword == RID_LABEL)
8156     {
8157       cp_lexer_consume_token (parser->lexer);
8158       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8159       cp_parser_skip_to_end_of_statement (parser);
8160       /* If the next token is now a `;', consume it.  */
8161       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8162         cp_lexer_consume_token (parser->lexer);
8163     }
8164   /* If the next token is `static_assert' we have a static assertion.  */
8165   else if (token1->keyword == RID_STATIC_ASSERT)
8166     cp_parser_static_assert (parser, /*member_p=*/false);
8167   /* Anything else must be a simple-declaration.  */
8168   else
8169     cp_parser_simple_declaration (parser, !statement_p);
8170 }
8171
8172 /* Parse a simple-declaration.
8173
8174    simple-declaration:
8175      decl-specifier-seq [opt] init-declarator-list [opt] ;
8176
8177    init-declarator-list:
8178      init-declarator
8179      init-declarator-list , init-declarator
8180
8181    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8182    function-definition as a simple-declaration.  */
8183
8184 static void
8185 cp_parser_simple_declaration (cp_parser* parser,
8186                               bool function_definition_allowed_p)
8187 {
8188   cp_decl_specifier_seq decl_specifiers;
8189   int declares_class_or_enum;
8190   bool saw_declarator;
8191
8192   /* Defer access checks until we know what is being declared; the
8193      checks for names appearing in the decl-specifier-seq should be
8194      done as if we were in the scope of the thing being declared.  */
8195   push_deferring_access_checks (dk_deferred);
8196
8197   /* Parse the decl-specifier-seq.  We have to keep track of whether
8198      or not the decl-specifier-seq declares a named class or
8199      enumeration type, since that is the only case in which the
8200      init-declarator-list is allowed to be empty.
8201
8202      [dcl.dcl]
8203
8204      In a simple-declaration, the optional init-declarator-list can be
8205      omitted only when declaring a class or enumeration, that is when
8206      the decl-specifier-seq contains either a class-specifier, an
8207      elaborated-type-specifier, or an enum-specifier.  */
8208   cp_parser_decl_specifier_seq (parser,
8209                                 CP_PARSER_FLAGS_OPTIONAL,
8210                                 &decl_specifiers,
8211                                 &declares_class_or_enum);
8212   /* We no longer need to defer access checks.  */
8213   stop_deferring_access_checks ();
8214
8215   /* In a block scope, a valid declaration must always have a
8216      decl-specifier-seq.  By not trying to parse declarators, we can
8217      resolve the declaration/expression ambiguity more quickly.  */
8218   if (!function_definition_allowed_p
8219       && !decl_specifiers.any_specifiers_p)
8220     {
8221       cp_parser_error (parser, "expected declaration");
8222       goto done;
8223     }
8224
8225   /* If the next two tokens are both identifiers, the code is
8226      erroneous. The usual cause of this situation is code like:
8227
8228        T t;
8229
8230      where "T" should name a type -- but does not.  */
8231   if (!decl_specifiers.type
8232       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8233     {
8234       /* If parsing tentatively, we should commit; we really are
8235          looking at a declaration.  */
8236       cp_parser_commit_to_tentative_parse (parser);
8237       /* Give up.  */
8238       goto done;
8239     }
8240
8241   /* If we have seen at least one decl-specifier, and the next token
8242      is not a parenthesis, then we must be looking at a declaration.
8243      (After "int (" we might be looking at a functional cast.)  */
8244   if (decl_specifiers.any_specifiers_p
8245       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8246       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8247       && !cp_parser_error_occurred (parser))
8248     cp_parser_commit_to_tentative_parse (parser);
8249
8250   /* Keep going until we hit the `;' at the end of the simple
8251      declaration.  */
8252   saw_declarator = false;
8253   while (cp_lexer_next_token_is_not (parser->lexer,
8254                                      CPP_SEMICOLON))
8255     {
8256       cp_token *token;
8257       bool function_definition_p;
8258       tree decl;
8259
8260       if (saw_declarator)
8261         {
8262           /* If we are processing next declarator, coma is expected */
8263           token = cp_lexer_peek_token (parser->lexer);
8264           gcc_assert (token->type == CPP_COMMA);
8265           cp_lexer_consume_token (parser->lexer);
8266         }
8267       else
8268         saw_declarator = true;
8269
8270       /* Parse the init-declarator.  */
8271       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8272                                         /*checks=*/NULL,
8273                                         function_definition_allowed_p,
8274                                         /*member_p=*/false,
8275                                         declares_class_or_enum,
8276                                         &function_definition_p);
8277       /* If an error occurred while parsing tentatively, exit quickly.
8278          (That usually happens when in the body of a function; each
8279          statement is treated as a declaration-statement until proven
8280          otherwise.)  */
8281       if (cp_parser_error_occurred (parser))
8282         goto done;
8283       /* Handle function definitions specially.  */
8284       if (function_definition_p)
8285         {
8286           /* If the next token is a `,', then we are probably
8287              processing something like:
8288
8289                void f() {}, *p;
8290
8291              which is erroneous.  */
8292           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8293             {
8294               cp_token *token = cp_lexer_peek_token (parser->lexer);
8295               error ("%Hmixing declarations and function-definitions is forbidden",
8296                      &token->location);
8297             }
8298           /* Otherwise, we're done with the list of declarators.  */
8299           else
8300             {
8301               pop_deferring_access_checks ();
8302               return;
8303             }
8304         }
8305       /* The next token should be either a `,' or a `;'.  */
8306       token = cp_lexer_peek_token (parser->lexer);
8307       /* If it's a `,', there are more declarators to come.  */
8308       if (token->type == CPP_COMMA)
8309         /* will be consumed next time around */;
8310       /* If it's a `;', we are done.  */
8311       else if (token->type == CPP_SEMICOLON)
8312         break;
8313       /* Anything else is an error.  */
8314       else
8315         {
8316           /* If we have already issued an error message we don't need
8317              to issue another one.  */
8318           if (decl != error_mark_node
8319               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8320             cp_parser_error (parser, "expected %<,%> or %<;%>");
8321           /* Skip tokens until we reach the end of the statement.  */
8322           cp_parser_skip_to_end_of_statement (parser);
8323           /* If the next token is now a `;', consume it.  */
8324           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8325             cp_lexer_consume_token (parser->lexer);
8326           goto done;
8327         }
8328       /* After the first time around, a function-definition is not
8329          allowed -- even if it was OK at first.  For example:
8330
8331            int i, f() {}
8332
8333          is not valid.  */
8334       function_definition_allowed_p = false;
8335     }
8336
8337   /* Issue an error message if no declarators are present, and the
8338      decl-specifier-seq does not itself declare a class or
8339      enumeration.  */
8340   if (!saw_declarator)
8341     {
8342       if (cp_parser_declares_only_class_p (parser))
8343         shadow_tag (&decl_specifiers);
8344       /* Perform any deferred access checks.  */
8345       perform_deferred_access_checks ();
8346     }
8347
8348   /* Consume the `;'.  */
8349   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8350
8351  done:
8352   pop_deferring_access_checks ();
8353 }
8354
8355 /* Parse a decl-specifier-seq.
8356
8357    decl-specifier-seq:
8358      decl-specifier-seq [opt] decl-specifier
8359
8360    decl-specifier:
8361      storage-class-specifier
8362      type-specifier
8363      function-specifier
8364      friend
8365      typedef
8366
8367    GNU Extension:
8368
8369    decl-specifier:
8370      attributes
8371
8372    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8373
8374    The parser flags FLAGS is used to control type-specifier parsing.
8375
8376    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8377    flags:
8378
8379      1: one of the decl-specifiers is an elaborated-type-specifier
8380         (i.e., a type declaration)
8381      2: one of the decl-specifiers is an enum-specifier or a
8382         class-specifier (i.e., a type definition)
8383
8384    */
8385
8386 static void
8387 cp_parser_decl_specifier_seq (cp_parser* parser,
8388                               cp_parser_flags flags,
8389                               cp_decl_specifier_seq *decl_specs,
8390                               int* declares_class_or_enum)
8391 {
8392   bool constructor_possible_p = !parser->in_declarator_p;
8393   cp_token *start_token = NULL;
8394
8395   /* Clear DECL_SPECS.  */
8396   clear_decl_specs (decl_specs);
8397
8398   /* Assume no class or enumeration type is declared.  */
8399   *declares_class_or_enum = 0;
8400
8401   /* Keep reading specifiers until there are no more to read.  */
8402   while (true)
8403     {
8404       bool constructor_p;
8405       bool found_decl_spec;
8406       cp_token *token;
8407
8408       /* Peek at the next token.  */
8409       token = cp_lexer_peek_token (parser->lexer);
8410
8411       /* Save the first token of the decl spec list for error
8412          reporting.  */
8413       if (!start_token)
8414         start_token = token;
8415       /* Handle attributes.  */
8416       if (token->keyword == RID_ATTRIBUTE)
8417         {
8418           /* Parse the attributes.  */
8419           decl_specs->attributes
8420             = chainon (decl_specs->attributes,
8421                        cp_parser_attributes_opt (parser));
8422           continue;
8423         }
8424       /* Assume we will find a decl-specifier keyword.  */
8425       found_decl_spec = true;
8426       /* If the next token is an appropriate keyword, we can simply
8427          add it to the list.  */
8428       switch (token->keyword)
8429         {
8430           /* decl-specifier:
8431                friend  */
8432         case RID_FRIEND:
8433           if (!at_class_scope_p ())
8434             {
8435               error ("%H%<friend%> used outside of class", &token->location);
8436               cp_lexer_purge_token (parser->lexer);
8437             }
8438           else
8439             {
8440               ++decl_specs->specs[(int) ds_friend];
8441               /* Consume the token.  */
8442               cp_lexer_consume_token (parser->lexer);
8443             }
8444           break;
8445
8446           /* function-specifier:
8447                inline
8448                virtual
8449                explicit  */
8450         case RID_INLINE:
8451         case RID_VIRTUAL:
8452         case RID_EXPLICIT:
8453           cp_parser_function_specifier_opt (parser, decl_specs);
8454           break;
8455
8456           /* decl-specifier:
8457                typedef  */
8458         case RID_TYPEDEF:
8459           ++decl_specs->specs[(int) ds_typedef];
8460           /* Consume the token.  */
8461           cp_lexer_consume_token (parser->lexer);
8462           /* A constructor declarator cannot appear in a typedef.  */
8463           constructor_possible_p = false;
8464           /* The "typedef" keyword can only occur in a declaration; we
8465              may as well commit at this point.  */
8466           cp_parser_commit_to_tentative_parse (parser);
8467
8468           if (decl_specs->storage_class != sc_none)
8469             decl_specs->conflicting_specifiers_p = true;
8470           break;
8471
8472           /* storage-class-specifier:
8473                auto
8474                register
8475                static
8476                extern
8477                mutable
8478
8479              GNU Extension:
8480                thread  */
8481         case RID_AUTO:
8482           if (cxx_dialect == cxx98) 
8483             {
8484               /* Consume the token.  */
8485               cp_lexer_consume_token (parser->lexer);
8486
8487               /* Complain about `auto' as a storage specifier, if
8488                  we're complaining about C++0x compatibility.  */
8489               warning 
8490                 (OPT_Wc__0x_compat, 
8491                  "%H%<auto%> will change meaning in C++0x; please remove it",
8492                  &token->location);
8493
8494               /* Set the storage class anyway.  */
8495               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8496                                            token->location);
8497             }
8498           else
8499             /* C++0x auto type-specifier.  */
8500             found_decl_spec = false;
8501           break;
8502
8503         case RID_REGISTER:
8504         case RID_STATIC:
8505         case RID_EXTERN:
8506         case RID_MUTABLE:
8507           /* Consume the token.  */
8508           cp_lexer_consume_token (parser->lexer);
8509           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8510                                        token->location);
8511           break;
8512         case RID_THREAD:
8513           /* Consume the token.  */
8514           cp_lexer_consume_token (parser->lexer);
8515           ++decl_specs->specs[(int) ds_thread];
8516           break;
8517
8518         default:
8519           /* We did not yet find a decl-specifier yet.  */
8520           found_decl_spec = false;
8521           break;
8522         }
8523
8524       /* Constructors are a special case.  The `S' in `S()' is not a
8525          decl-specifier; it is the beginning of the declarator.  */
8526       constructor_p
8527         = (!found_decl_spec
8528            && constructor_possible_p
8529            && (cp_parser_constructor_declarator_p
8530                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8531
8532       /* If we don't have a DECL_SPEC yet, then we must be looking at
8533          a type-specifier.  */
8534       if (!found_decl_spec && !constructor_p)
8535         {
8536           int decl_spec_declares_class_or_enum;
8537           bool is_cv_qualifier;
8538           tree type_spec;
8539
8540           type_spec
8541             = cp_parser_type_specifier (parser, flags,
8542                                         decl_specs,
8543                                         /*is_declaration=*/true,
8544                                         &decl_spec_declares_class_or_enum,
8545                                         &is_cv_qualifier);
8546           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8547
8548           /* If this type-specifier referenced a user-defined type
8549              (a typedef, class-name, etc.), then we can't allow any
8550              more such type-specifiers henceforth.
8551
8552              [dcl.spec]
8553
8554              The longest sequence of decl-specifiers that could
8555              possibly be a type name is taken as the
8556              decl-specifier-seq of a declaration.  The sequence shall
8557              be self-consistent as described below.
8558
8559              [dcl.type]
8560
8561              As a general rule, at most one type-specifier is allowed
8562              in the complete decl-specifier-seq of a declaration.  The
8563              only exceptions are the following:
8564
8565              -- const or volatile can be combined with any other
8566                 type-specifier.
8567
8568              -- signed or unsigned can be combined with char, long,
8569                 short, or int.
8570
8571              -- ..
8572
8573              Example:
8574
8575                typedef char* Pc;
8576                void g (const int Pc);
8577
8578              Here, Pc is *not* part of the decl-specifier seq; it's
8579              the declarator.  Therefore, once we see a type-specifier
8580              (other than a cv-qualifier), we forbid any additional
8581              user-defined types.  We *do* still allow things like `int
8582              int' to be considered a decl-specifier-seq, and issue the
8583              error message later.  */
8584           if (type_spec && !is_cv_qualifier)
8585             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8586           /* A constructor declarator cannot follow a type-specifier.  */
8587           if (type_spec)
8588             {
8589               constructor_possible_p = false;
8590               found_decl_spec = true;
8591             }
8592         }
8593
8594       /* If we still do not have a DECL_SPEC, then there are no more
8595          decl-specifiers.  */
8596       if (!found_decl_spec)
8597         break;
8598
8599       decl_specs->any_specifiers_p = true;
8600       /* After we see one decl-specifier, further decl-specifiers are
8601          always optional.  */
8602       flags |= CP_PARSER_FLAGS_OPTIONAL;
8603     }
8604
8605   cp_parser_check_decl_spec (decl_specs, start_token->location);
8606
8607   /* Don't allow a friend specifier with a class definition.  */
8608   if (decl_specs->specs[(int) ds_friend] != 0
8609       && (*declares_class_or_enum & 2))
8610     error ("%Hclass definition may not be declared a friend",
8611             &start_token->location);
8612 }
8613
8614 /* Parse an (optional) storage-class-specifier.
8615
8616    storage-class-specifier:
8617      auto
8618      register
8619      static
8620      extern
8621      mutable
8622
8623    GNU Extension:
8624
8625    storage-class-specifier:
8626      thread
8627
8628    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8629
8630 static tree
8631 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8632 {
8633   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8634     {
8635     case RID_AUTO:
8636       if (cxx_dialect != cxx98)
8637         return NULL_TREE;
8638       /* Fall through for C++98.  */
8639
8640     case RID_REGISTER:
8641     case RID_STATIC:
8642     case RID_EXTERN:
8643     case RID_MUTABLE:
8644     case RID_THREAD:
8645       /* Consume the token.  */
8646       return cp_lexer_consume_token (parser->lexer)->u.value;
8647
8648     default:
8649       return NULL_TREE;
8650     }
8651 }
8652
8653 /* Parse an (optional) function-specifier.
8654
8655    function-specifier:
8656      inline
8657      virtual
8658      explicit
8659
8660    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8661    Updates DECL_SPECS, if it is non-NULL.  */
8662
8663 static tree
8664 cp_parser_function_specifier_opt (cp_parser* parser,
8665                                   cp_decl_specifier_seq *decl_specs)
8666 {
8667   cp_token *token = cp_lexer_peek_token (parser->lexer);
8668   switch (token->keyword)
8669     {
8670     case RID_INLINE:
8671       if (decl_specs)
8672         ++decl_specs->specs[(int) ds_inline];
8673       break;
8674
8675     case RID_VIRTUAL:
8676       /* 14.5.2.3 [temp.mem]
8677
8678          A member function template shall not be virtual.  */
8679       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8680         error ("%Htemplates may not be %<virtual%>", &token->location);
8681       else if (decl_specs)
8682         ++decl_specs->specs[(int) ds_virtual];
8683       break;
8684
8685     case RID_EXPLICIT:
8686       if (decl_specs)
8687         ++decl_specs->specs[(int) ds_explicit];
8688       break;
8689
8690     default:
8691       return NULL_TREE;
8692     }
8693
8694   /* Consume the token.  */
8695   return cp_lexer_consume_token (parser->lexer)->u.value;
8696 }
8697
8698 /* Parse a linkage-specification.
8699
8700    linkage-specification:
8701      extern string-literal { declaration-seq [opt] }
8702      extern string-literal declaration  */
8703
8704 static void
8705 cp_parser_linkage_specification (cp_parser* parser)
8706 {
8707   tree linkage;
8708
8709   /* Look for the `extern' keyword.  */
8710   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8711
8712   /* Look for the string-literal.  */
8713   linkage = cp_parser_string_literal (parser, false, false);
8714
8715   /* Transform the literal into an identifier.  If the literal is a
8716      wide-character string, or contains embedded NULs, then we can't
8717      handle it as the user wants.  */
8718   if (strlen (TREE_STRING_POINTER (linkage))
8719       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8720     {
8721       cp_parser_error (parser, "invalid linkage-specification");
8722       /* Assume C++ linkage.  */
8723       linkage = lang_name_cplusplus;
8724     }
8725   else
8726     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8727
8728   /* We're now using the new linkage.  */
8729   push_lang_context (linkage);
8730
8731   /* If the next token is a `{', then we're using the first
8732      production.  */
8733   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8734     {
8735       /* Consume the `{' token.  */
8736       cp_lexer_consume_token (parser->lexer);
8737       /* Parse the declarations.  */
8738       cp_parser_declaration_seq_opt (parser);
8739       /* Look for the closing `}'.  */
8740       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8741     }
8742   /* Otherwise, there's just one declaration.  */
8743   else
8744     {
8745       bool saved_in_unbraced_linkage_specification_p;
8746
8747       saved_in_unbraced_linkage_specification_p
8748         = parser->in_unbraced_linkage_specification_p;
8749       parser->in_unbraced_linkage_specification_p = true;
8750       cp_parser_declaration (parser);
8751       parser->in_unbraced_linkage_specification_p
8752         = saved_in_unbraced_linkage_specification_p;
8753     }
8754
8755   /* We're done with the linkage-specification.  */
8756   pop_lang_context ();
8757 }
8758
8759 /* Parse a static_assert-declaration.
8760
8761    static_assert-declaration:
8762      static_assert ( constant-expression , string-literal ) ; 
8763
8764    If MEMBER_P, this static_assert is a class member.  */
8765
8766 static void 
8767 cp_parser_static_assert(cp_parser *parser, bool member_p)
8768 {
8769   tree condition;
8770   tree message;
8771   cp_token *token;
8772   location_t saved_loc;
8773
8774   /* Peek at the `static_assert' token so we can keep track of exactly
8775      where the static assertion started.  */
8776   token = cp_lexer_peek_token (parser->lexer);
8777   saved_loc = token->location;
8778
8779   /* Look for the `static_assert' keyword.  */
8780   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8781                                   "%<static_assert%>"))
8782     return;
8783
8784   /*  We know we are in a static assertion; commit to any tentative
8785       parse.  */
8786   if (cp_parser_parsing_tentatively (parser))
8787     cp_parser_commit_to_tentative_parse (parser);
8788
8789   /* Parse the `(' starting the static assertion condition.  */
8790   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8791
8792   /* Parse the constant-expression.  */
8793   condition = 
8794     cp_parser_constant_expression (parser,
8795                                    /*allow_non_constant_p=*/false,
8796                                    /*non_constant_p=*/NULL);
8797
8798   /* Parse the separating `,'.  */
8799   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8800
8801   /* Parse the string-literal message.  */
8802   message = cp_parser_string_literal (parser, 
8803                                       /*translate=*/false,
8804                                       /*wide_ok=*/true);
8805
8806   /* A `)' completes the static assertion.  */
8807   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8808     cp_parser_skip_to_closing_parenthesis (parser, 
8809                                            /*recovering=*/true, 
8810                                            /*or_comma=*/false,
8811                                            /*consume_paren=*/true);
8812
8813   /* A semicolon terminates the declaration.  */
8814   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8815
8816   /* Complete the static assertion, which may mean either processing 
8817      the static assert now or saving it for template instantiation.  */
8818   finish_static_assert (condition, message, saved_loc, member_p);
8819 }
8820
8821 /* Parse a `decltype' type. Returns the type. 
8822
8823    simple-type-specifier:
8824      decltype ( expression )  */
8825
8826 static tree
8827 cp_parser_decltype (cp_parser *parser)
8828 {
8829   tree expr;
8830   bool id_expression_or_member_access_p = false;
8831   const char *saved_message;
8832   bool saved_integral_constant_expression_p;
8833   bool saved_non_integral_constant_expression_p;
8834   cp_token *id_expr_start_token;
8835
8836   /* Look for the `decltype' token.  */
8837   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8838     return error_mark_node;
8839
8840   /* Types cannot be defined in a `decltype' expression.  Save away the
8841      old message.  */
8842   saved_message = parser->type_definition_forbidden_message;
8843
8844   /* And create the new one.  */
8845   parser->type_definition_forbidden_message
8846     = "types may not be defined in %<decltype%> expressions";
8847
8848   /* The restrictions on constant-expressions do not apply inside
8849      decltype expressions.  */
8850   saved_integral_constant_expression_p
8851     = parser->integral_constant_expression_p;
8852   saved_non_integral_constant_expression_p
8853     = parser->non_integral_constant_expression_p;
8854   parser->integral_constant_expression_p = false;
8855
8856   /* Do not actually evaluate the expression.  */
8857   ++skip_evaluation;
8858
8859   /* Parse the opening `('.  */
8860   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8861     return error_mark_node;
8862   
8863   /* First, try parsing an id-expression.  */
8864   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8865   cp_parser_parse_tentatively (parser);
8866   expr = cp_parser_id_expression (parser,
8867                                   /*template_keyword_p=*/false,
8868                                   /*check_dependency_p=*/true,
8869                                   /*template_p=*/NULL,
8870                                   /*declarator_p=*/false,
8871                                   /*optional_p=*/false);
8872
8873   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8874     {
8875       bool non_integral_constant_expression_p = false;
8876       tree id_expression = expr;
8877       cp_id_kind idk;
8878       const char *error_msg;
8879
8880       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8881         /* Lookup the name we got back from the id-expression.  */
8882         expr = cp_parser_lookup_name (parser, expr,
8883                                       none_type,
8884                                       /*is_template=*/false,
8885                                       /*is_namespace=*/false,
8886                                       /*check_dependency=*/true,
8887                                       /*ambiguous_decls=*/NULL,
8888                                       id_expr_start_token->location);
8889
8890       if (expr
8891           && expr != error_mark_node
8892           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8893           && TREE_CODE (expr) != TYPE_DECL
8894           && (TREE_CODE (expr) != BIT_NOT_EXPR
8895               || !TYPE_P (TREE_OPERAND (expr, 0)))
8896           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8897         {
8898           /* Complete lookup of the id-expression.  */
8899           expr = (finish_id_expression
8900                   (id_expression, expr, parser->scope, &idk,
8901                    /*integral_constant_expression_p=*/false,
8902                    /*allow_non_integral_constant_expression_p=*/true,
8903                    &non_integral_constant_expression_p,
8904                    /*template_p=*/false,
8905                    /*done=*/true,
8906                    /*address_p=*/false,
8907                    /*template_arg_p=*/false,
8908                    &error_msg,
8909                    id_expr_start_token->location));
8910
8911           if (expr == error_mark_node)
8912             /* We found an id-expression, but it was something that we
8913                should not have found. This is an error, not something
8914                we can recover from, so note that we found an
8915                id-expression and we'll recover as gracefully as
8916                possible.  */
8917             id_expression_or_member_access_p = true;
8918         }
8919
8920       if (expr 
8921           && expr != error_mark_node
8922           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8923         /* We have an id-expression.  */
8924         id_expression_or_member_access_p = true;
8925     }
8926
8927   if (!id_expression_or_member_access_p)
8928     {
8929       /* Abort the id-expression parse.  */
8930       cp_parser_abort_tentative_parse (parser);
8931
8932       /* Parsing tentatively, again.  */
8933       cp_parser_parse_tentatively (parser);
8934
8935       /* Parse a class member access.  */
8936       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8937                                            /*cast_p=*/false,
8938                                            /*member_access_only_p=*/true, NULL);
8939
8940       if (expr 
8941           && expr != error_mark_node
8942           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8943         /* We have an id-expression.  */
8944         id_expression_or_member_access_p = true;
8945     }
8946
8947   if (id_expression_or_member_access_p)
8948     /* We have parsed the complete id-expression or member access.  */
8949     cp_parser_parse_definitely (parser);
8950   else
8951     {
8952       /* Abort our attempt to parse an id-expression or member access
8953          expression.  */
8954       cp_parser_abort_tentative_parse (parser);
8955
8956       /* Parse a full expression.  */
8957       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8958     }
8959
8960   /* Go back to evaluating expressions.  */
8961   --skip_evaluation;
8962
8963   /* Restore the old message and the integral constant expression
8964      flags.  */
8965   parser->type_definition_forbidden_message = saved_message;
8966   parser->integral_constant_expression_p
8967     = saved_integral_constant_expression_p;
8968   parser->non_integral_constant_expression_p
8969     = saved_non_integral_constant_expression_p;
8970
8971   if (expr == error_mark_node)
8972     {
8973       /* Skip everything up to the closing `)'.  */
8974       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8975                                              /*consume_paren=*/true);
8976       return error_mark_node;
8977     }
8978   
8979   /* Parse to the closing `)'.  */
8980   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8981     {
8982       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8983                                              /*consume_paren=*/true);
8984       return error_mark_node;
8985     }
8986
8987   return finish_decltype_type (expr, id_expression_or_member_access_p);
8988 }
8989
8990 /* Special member functions [gram.special] */
8991
8992 /* Parse a conversion-function-id.
8993
8994    conversion-function-id:
8995      operator conversion-type-id
8996
8997    Returns an IDENTIFIER_NODE representing the operator.  */
8998
8999 static tree
9000 cp_parser_conversion_function_id (cp_parser* parser)
9001 {
9002   tree type;
9003   tree saved_scope;
9004   tree saved_qualifying_scope;
9005   tree saved_object_scope;
9006   tree pushed_scope = NULL_TREE;
9007
9008   /* Look for the `operator' token.  */
9009   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9010     return error_mark_node;
9011   /* When we parse the conversion-type-id, the current scope will be
9012      reset.  However, we need that information in able to look up the
9013      conversion function later, so we save it here.  */
9014   saved_scope = parser->scope;
9015   saved_qualifying_scope = parser->qualifying_scope;
9016   saved_object_scope = parser->object_scope;
9017   /* We must enter the scope of the class so that the names of
9018      entities declared within the class are available in the
9019      conversion-type-id.  For example, consider:
9020
9021        struct S {
9022          typedef int I;
9023          operator I();
9024        };
9025
9026        S::operator I() { ... }
9027
9028      In order to see that `I' is a type-name in the definition, we
9029      must be in the scope of `S'.  */
9030   if (saved_scope)
9031     pushed_scope = push_scope (saved_scope);
9032   /* Parse the conversion-type-id.  */
9033   type = cp_parser_conversion_type_id (parser);
9034   /* Leave the scope of the class, if any.  */
9035   if (pushed_scope)
9036     pop_scope (pushed_scope);
9037   /* Restore the saved scope.  */
9038   parser->scope = saved_scope;
9039   parser->qualifying_scope = saved_qualifying_scope;
9040   parser->object_scope = saved_object_scope;
9041   /* If the TYPE is invalid, indicate failure.  */
9042   if (type == error_mark_node)
9043     return error_mark_node;
9044   return mangle_conv_op_name_for_type (type);
9045 }
9046
9047 /* Parse a conversion-type-id:
9048
9049    conversion-type-id:
9050      type-specifier-seq conversion-declarator [opt]
9051
9052    Returns the TYPE specified.  */
9053
9054 static tree
9055 cp_parser_conversion_type_id (cp_parser* parser)
9056 {
9057   tree attributes;
9058   cp_decl_specifier_seq type_specifiers;
9059   cp_declarator *declarator;
9060   tree type_specified;
9061
9062   /* Parse the attributes.  */
9063   attributes = cp_parser_attributes_opt (parser);
9064   /* Parse the type-specifiers.  */
9065   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9066                                 &type_specifiers);
9067   /* If that didn't work, stop.  */
9068   if (type_specifiers.type == error_mark_node)
9069     return error_mark_node;
9070   /* Parse the conversion-declarator.  */
9071   declarator = cp_parser_conversion_declarator_opt (parser);
9072
9073   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9074                                     /*initialized=*/0, &attributes);
9075   if (attributes)
9076     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9077
9078   /* Don't give this error when parsing tentatively.  This happens to
9079      work because we always parse this definitively once.  */
9080   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9081       && type_uses_auto (type_specified))
9082     {
9083       error ("invalid use of %<auto%> in conversion operator");
9084       return error_mark_node;
9085     }
9086
9087   return type_specified;
9088 }
9089
9090 /* Parse an (optional) conversion-declarator.
9091
9092    conversion-declarator:
9093      ptr-operator conversion-declarator [opt]
9094
9095    */
9096
9097 static cp_declarator *
9098 cp_parser_conversion_declarator_opt (cp_parser* parser)
9099 {
9100   enum tree_code code;
9101   tree class_type;
9102   cp_cv_quals cv_quals;
9103
9104   /* We don't know if there's a ptr-operator next, or not.  */
9105   cp_parser_parse_tentatively (parser);
9106   /* Try the ptr-operator.  */
9107   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9108   /* If it worked, look for more conversion-declarators.  */
9109   if (cp_parser_parse_definitely (parser))
9110     {
9111       cp_declarator *declarator;
9112
9113       /* Parse another optional declarator.  */
9114       declarator = cp_parser_conversion_declarator_opt (parser);
9115
9116       return cp_parser_make_indirect_declarator
9117         (code, class_type, cv_quals, declarator);
9118    }
9119
9120   return NULL;
9121 }
9122
9123 /* Parse an (optional) ctor-initializer.
9124
9125    ctor-initializer:
9126      : mem-initializer-list
9127
9128    Returns TRUE iff the ctor-initializer was actually present.  */
9129
9130 static bool
9131 cp_parser_ctor_initializer_opt (cp_parser* parser)
9132 {
9133   /* If the next token is not a `:', then there is no
9134      ctor-initializer.  */
9135   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9136     {
9137       /* Do default initialization of any bases and members.  */
9138       if (DECL_CONSTRUCTOR_P (current_function_decl))
9139         finish_mem_initializers (NULL_TREE);
9140
9141       return false;
9142     }
9143
9144   /* Consume the `:' token.  */
9145   cp_lexer_consume_token (parser->lexer);
9146   /* And the mem-initializer-list.  */
9147   cp_parser_mem_initializer_list (parser);
9148
9149   return true;
9150 }
9151
9152 /* Parse a mem-initializer-list.
9153
9154    mem-initializer-list:
9155      mem-initializer ... [opt]
9156      mem-initializer ... [opt] , mem-initializer-list  */
9157
9158 static void
9159 cp_parser_mem_initializer_list (cp_parser* parser)
9160 {
9161   tree mem_initializer_list = NULL_TREE;
9162   cp_token *token = cp_lexer_peek_token (parser->lexer);
9163
9164   /* Let the semantic analysis code know that we are starting the
9165      mem-initializer-list.  */
9166   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9167     error ("%Honly constructors take base initializers",
9168            &token->location);
9169
9170   /* Loop through the list.  */
9171   while (true)
9172     {
9173       tree mem_initializer;
9174
9175       token = cp_lexer_peek_token (parser->lexer);
9176       /* Parse the mem-initializer.  */
9177       mem_initializer = cp_parser_mem_initializer (parser);
9178       /* If the next token is a `...', we're expanding member initializers. */
9179       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9180         {
9181           /* Consume the `...'. */
9182           cp_lexer_consume_token (parser->lexer);
9183
9184           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9185              can be expanded but members cannot. */
9186           if (mem_initializer != error_mark_node
9187               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9188             {
9189               error ("%Hcannot expand initializer for member %<%D%>",
9190                      &token->location, TREE_PURPOSE (mem_initializer));
9191               mem_initializer = error_mark_node;
9192             }
9193
9194           /* Construct the pack expansion type. */
9195           if (mem_initializer != error_mark_node)
9196             mem_initializer = make_pack_expansion (mem_initializer);
9197         }
9198       /* Add it to the list, unless it was erroneous.  */
9199       if (mem_initializer != error_mark_node)
9200         {
9201           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9202           mem_initializer_list = mem_initializer;
9203         }
9204       /* If the next token is not a `,', we're done.  */
9205       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9206         break;
9207       /* Consume the `,' token.  */
9208       cp_lexer_consume_token (parser->lexer);
9209     }
9210
9211   /* Perform semantic analysis.  */
9212   if (DECL_CONSTRUCTOR_P (current_function_decl))
9213     finish_mem_initializers (mem_initializer_list);
9214 }
9215
9216 /* Parse a mem-initializer.
9217
9218    mem-initializer:
9219      mem-initializer-id ( expression-list [opt] )
9220      mem-initializer-id braced-init-list
9221
9222    GNU extension:
9223
9224    mem-initializer:
9225      ( expression-list [opt] )
9226
9227    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9228    class) or FIELD_DECL (for a non-static data member) to initialize;
9229    the TREE_VALUE is the expression-list.  An empty initialization
9230    list is represented by void_list_node.  */
9231
9232 static tree
9233 cp_parser_mem_initializer (cp_parser* parser)
9234 {
9235   tree mem_initializer_id;
9236   tree expression_list;
9237   tree member;
9238   cp_token *token = cp_lexer_peek_token (parser->lexer);
9239
9240   /* Find out what is being initialized.  */
9241   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9242     {
9243       permerror (token->location,
9244                  "anachronistic old-style base class initializer");
9245       mem_initializer_id = NULL_TREE;
9246     }
9247   else
9248     {
9249       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9250       if (mem_initializer_id == error_mark_node)
9251         return mem_initializer_id;
9252     }
9253   member = expand_member_init (mem_initializer_id);
9254   if (member && !DECL_P (member))
9255     in_base_initializer = 1;
9256
9257   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9258     {
9259       bool expr_non_constant_p;
9260       maybe_warn_cpp0x ("extended initializer lists");
9261       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9262       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9263       expression_list = build_tree_list (NULL_TREE, expression_list);
9264     }
9265   else
9266     {
9267       VEC(tree,gc)* vec;
9268       vec = cp_parser_parenthesized_expression_list (parser, false,
9269                                                      /*cast_p=*/false,
9270                                                      /*allow_expansion_p=*/true,
9271                                                      /*non_constant_p=*/NULL);
9272       if (vec == NULL)
9273         return error_mark_node;
9274       expression_list = build_tree_list_vec (vec);
9275       release_tree_vector (vec);
9276     }
9277
9278   if (expression_list == error_mark_node)
9279     return error_mark_node;
9280   if (!expression_list)
9281     expression_list = void_type_node;
9282
9283   in_base_initializer = 0;
9284
9285   return member ? build_tree_list (member, expression_list) : error_mark_node;
9286 }
9287
9288 /* Parse a mem-initializer-id.
9289
9290    mem-initializer-id:
9291      :: [opt] nested-name-specifier [opt] class-name
9292      identifier
9293
9294    Returns a TYPE indicating the class to be initializer for the first
9295    production.  Returns an IDENTIFIER_NODE indicating the data member
9296    to be initialized for the second production.  */
9297
9298 static tree
9299 cp_parser_mem_initializer_id (cp_parser* parser)
9300 {
9301   bool global_scope_p;
9302   bool nested_name_specifier_p;
9303   bool template_p = false;
9304   tree id;
9305
9306   cp_token *token = cp_lexer_peek_token (parser->lexer);
9307
9308   /* `typename' is not allowed in this context ([temp.res]).  */
9309   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9310     {
9311       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9312              "member initializer is implicitly a type)",
9313              &token->location);
9314       cp_lexer_consume_token (parser->lexer);
9315     }
9316   /* Look for the optional `::' operator.  */
9317   global_scope_p
9318     = (cp_parser_global_scope_opt (parser,
9319                                    /*current_scope_valid_p=*/false)
9320        != NULL_TREE);
9321   /* Look for the optional nested-name-specifier.  The simplest way to
9322      implement:
9323
9324        [temp.res]
9325
9326        The keyword `typename' is not permitted in a base-specifier or
9327        mem-initializer; in these contexts a qualified name that
9328        depends on a template-parameter is implicitly assumed to be a
9329        type name.
9330
9331      is to assume that we have seen the `typename' keyword at this
9332      point.  */
9333   nested_name_specifier_p
9334     = (cp_parser_nested_name_specifier_opt (parser,
9335                                             /*typename_keyword_p=*/true,
9336                                             /*check_dependency_p=*/true,
9337                                             /*type_p=*/true,
9338                                             /*is_declaration=*/true)
9339        != NULL_TREE);
9340   if (nested_name_specifier_p)
9341     template_p = cp_parser_optional_template_keyword (parser);
9342   /* If there is a `::' operator or a nested-name-specifier, then we
9343      are definitely looking for a class-name.  */
9344   if (global_scope_p || nested_name_specifier_p)
9345     return cp_parser_class_name (parser,
9346                                  /*typename_keyword_p=*/true,
9347                                  /*template_keyword_p=*/template_p,
9348                                  none_type,
9349                                  /*check_dependency_p=*/true,
9350                                  /*class_head_p=*/false,
9351                                  /*is_declaration=*/true);
9352   /* Otherwise, we could also be looking for an ordinary identifier.  */
9353   cp_parser_parse_tentatively (parser);
9354   /* Try a class-name.  */
9355   id = cp_parser_class_name (parser,
9356                              /*typename_keyword_p=*/true,
9357                              /*template_keyword_p=*/false,
9358                              none_type,
9359                              /*check_dependency_p=*/true,
9360                              /*class_head_p=*/false,
9361                              /*is_declaration=*/true);
9362   /* If we found one, we're done.  */
9363   if (cp_parser_parse_definitely (parser))
9364     return id;
9365   /* Otherwise, look for an ordinary identifier.  */
9366   return cp_parser_identifier (parser);
9367 }
9368
9369 /* Overloading [gram.over] */
9370
9371 /* Parse an operator-function-id.
9372
9373    operator-function-id:
9374      operator operator
9375
9376    Returns an IDENTIFIER_NODE for the operator which is a
9377    human-readable spelling of the identifier, e.g., `operator +'.  */
9378
9379 static tree
9380 cp_parser_operator_function_id (cp_parser* parser)
9381 {
9382   /* Look for the `operator' keyword.  */
9383   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9384     return error_mark_node;
9385   /* And then the name of the operator itself.  */
9386   return cp_parser_operator (parser);
9387 }
9388
9389 /* Parse an operator.
9390
9391    operator:
9392      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9393      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9394      || ++ -- , ->* -> () []
9395
9396    GNU Extensions:
9397
9398    operator:
9399      <? >? <?= >?=
9400
9401    Returns an IDENTIFIER_NODE for the operator which is a
9402    human-readable spelling of the identifier, e.g., `operator +'.  */
9403
9404 static tree
9405 cp_parser_operator (cp_parser* parser)
9406 {
9407   tree id = NULL_TREE;
9408   cp_token *token;
9409
9410   /* Peek at the next token.  */
9411   token = cp_lexer_peek_token (parser->lexer);
9412   /* Figure out which operator we have.  */
9413   switch (token->type)
9414     {
9415     case CPP_KEYWORD:
9416       {
9417         enum tree_code op;
9418
9419         /* The keyword should be either `new' or `delete'.  */
9420         if (token->keyword == RID_NEW)
9421           op = NEW_EXPR;
9422         else if (token->keyword == RID_DELETE)
9423           op = DELETE_EXPR;
9424         else
9425           break;
9426
9427         /* Consume the `new' or `delete' token.  */
9428         cp_lexer_consume_token (parser->lexer);
9429
9430         /* Peek at the next token.  */
9431         token = cp_lexer_peek_token (parser->lexer);
9432         /* If it's a `[' token then this is the array variant of the
9433            operator.  */
9434         if (token->type == CPP_OPEN_SQUARE)
9435           {
9436             /* Consume the `[' token.  */
9437             cp_lexer_consume_token (parser->lexer);
9438             /* Look for the `]' token.  */
9439             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9440             id = ansi_opname (op == NEW_EXPR
9441                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9442           }
9443         /* Otherwise, we have the non-array variant.  */
9444         else
9445           id = ansi_opname (op);
9446
9447         return id;
9448       }
9449
9450     case CPP_PLUS:
9451       id = ansi_opname (PLUS_EXPR);
9452       break;
9453
9454     case CPP_MINUS:
9455       id = ansi_opname (MINUS_EXPR);
9456       break;
9457
9458     case CPP_MULT:
9459       id = ansi_opname (MULT_EXPR);
9460       break;
9461
9462     case CPP_DIV:
9463       id = ansi_opname (TRUNC_DIV_EXPR);
9464       break;
9465
9466     case CPP_MOD:
9467       id = ansi_opname (TRUNC_MOD_EXPR);
9468       break;
9469
9470     case CPP_XOR:
9471       id = ansi_opname (BIT_XOR_EXPR);
9472       break;
9473
9474     case CPP_AND:
9475       id = ansi_opname (BIT_AND_EXPR);
9476       break;
9477
9478     case CPP_OR:
9479       id = ansi_opname (BIT_IOR_EXPR);
9480       break;
9481
9482     case CPP_COMPL:
9483       id = ansi_opname (BIT_NOT_EXPR);
9484       break;
9485
9486     case CPP_NOT:
9487       id = ansi_opname (TRUTH_NOT_EXPR);
9488       break;
9489
9490     case CPP_EQ:
9491       id = ansi_assopname (NOP_EXPR);
9492       break;
9493
9494     case CPP_LESS:
9495       id = ansi_opname (LT_EXPR);
9496       break;
9497
9498     case CPP_GREATER:
9499       id = ansi_opname (GT_EXPR);
9500       break;
9501
9502     case CPP_PLUS_EQ:
9503       id = ansi_assopname (PLUS_EXPR);
9504       break;
9505
9506     case CPP_MINUS_EQ:
9507       id = ansi_assopname (MINUS_EXPR);
9508       break;
9509
9510     case CPP_MULT_EQ:
9511       id = ansi_assopname (MULT_EXPR);
9512       break;
9513
9514     case CPP_DIV_EQ:
9515       id = ansi_assopname (TRUNC_DIV_EXPR);
9516       break;
9517
9518     case CPP_MOD_EQ:
9519       id = ansi_assopname (TRUNC_MOD_EXPR);
9520       break;
9521
9522     case CPP_XOR_EQ:
9523       id = ansi_assopname (BIT_XOR_EXPR);
9524       break;
9525
9526     case CPP_AND_EQ:
9527       id = ansi_assopname (BIT_AND_EXPR);
9528       break;
9529
9530     case CPP_OR_EQ:
9531       id = ansi_assopname (BIT_IOR_EXPR);
9532       break;
9533
9534     case CPP_LSHIFT:
9535       id = ansi_opname (LSHIFT_EXPR);
9536       break;
9537
9538     case CPP_RSHIFT:
9539       id = ansi_opname (RSHIFT_EXPR);
9540       break;
9541
9542     case CPP_LSHIFT_EQ:
9543       id = ansi_assopname (LSHIFT_EXPR);
9544       break;
9545
9546     case CPP_RSHIFT_EQ:
9547       id = ansi_assopname (RSHIFT_EXPR);
9548       break;
9549
9550     case CPP_EQ_EQ:
9551       id = ansi_opname (EQ_EXPR);
9552       break;
9553
9554     case CPP_NOT_EQ:
9555       id = ansi_opname (NE_EXPR);
9556       break;
9557
9558     case CPP_LESS_EQ:
9559       id = ansi_opname (LE_EXPR);
9560       break;
9561
9562     case CPP_GREATER_EQ:
9563       id = ansi_opname (GE_EXPR);
9564       break;
9565
9566     case CPP_AND_AND:
9567       id = ansi_opname (TRUTH_ANDIF_EXPR);
9568       break;
9569
9570     case CPP_OR_OR:
9571       id = ansi_opname (TRUTH_ORIF_EXPR);
9572       break;
9573
9574     case CPP_PLUS_PLUS:
9575       id = ansi_opname (POSTINCREMENT_EXPR);
9576       break;
9577
9578     case CPP_MINUS_MINUS:
9579       id = ansi_opname (PREDECREMENT_EXPR);
9580       break;
9581
9582     case CPP_COMMA:
9583       id = ansi_opname (COMPOUND_EXPR);
9584       break;
9585
9586     case CPP_DEREF_STAR:
9587       id = ansi_opname (MEMBER_REF);
9588       break;
9589
9590     case CPP_DEREF:
9591       id = ansi_opname (COMPONENT_REF);
9592       break;
9593
9594     case CPP_OPEN_PAREN:
9595       /* Consume the `('.  */
9596       cp_lexer_consume_token (parser->lexer);
9597       /* Look for the matching `)'.  */
9598       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9599       return ansi_opname (CALL_EXPR);
9600
9601     case CPP_OPEN_SQUARE:
9602       /* Consume the `['.  */
9603       cp_lexer_consume_token (parser->lexer);
9604       /* Look for the matching `]'.  */
9605       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9606       return ansi_opname (ARRAY_REF);
9607
9608     default:
9609       /* Anything else is an error.  */
9610       break;
9611     }
9612
9613   /* If we have selected an identifier, we need to consume the
9614      operator token.  */
9615   if (id)
9616     cp_lexer_consume_token (parser->lexer);
9617   /* Otherwise, no valid operator name was present.  */
9618   else
9619     {
9620       cp_parser_error (parser, "expected operator");
9621       id = error_mark_node;
9622     }
9623
9624   return id;
9625 }
9626
9627 /* Parse a template-declaration.
9628
9629    template-declaration:
9630      export [opt] template < template-parameter-list > declaration
9631
9632    If MEMBER_P is TRUE, this template-declaration occurs within a
9633    class-specifier.
9634
9635    The grammar rule given by the standard isn't correct.  What
9636    is really meant is:
9637
9638    template-declaration:
9639      export [opt] template-parameter-list-seq
9640        decl-specifier-seq [opt] init-declarator [opt] ;
9641      export [opt] template-parameter-list-seq
9642        function-definition
9643
9644    template-parameter-list-seq:
9645      template-parameter-list-seq [opt]
9646      template < template-parameter-list >  */
9647
9648 static void
9649 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9650 {
9651   /* Check for `export'.  */
9652   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9653     {
9654       /* Consume the `export' token.  */
9655       cp_lexer_consume_token (parser->lexer);
9656       /* Warn that we do not support `export'.  */
9657       warning (0, "keyword %<export%> not implemented, and will be ignored");
9658     }
9659
9660   cp_parser_template_declaration_after_export (parser, member_p);
9661 }
9662
9663 /* Parse a template-parameter-list.
9664
9665    template-parameter-list:
9666      template-parameter
9667      template-parameter-list , template-parameter
9668
9669    Returns a TREE_LIST.  Each node represents a template parameter.
9670    The nodes are connected via their TREE_CHAINs.  */
9671
9672 static tree
9673 cp_parser_template_parameter_list (cp_parser* parser)
9674 {
9675   tree parameter_list = NULL_TREE;
9676
9677   begin_template_parm_list ();
9678   while (true)
9679     {
9680       tree parameter;
9681       bool is_non_type;
9682       bool is_parameter_pack;
9683
9684       /* Parse the template-parameter.  */
9685       parameter = cp_parser_template_parameter (parser, 
9686                                                 &is_non_type,
9687                                                 &is_parameter_pack);
9688       /* Add it to the list.  */
9689       if (parameter != error_mark_node)
9690         parameter_list = process_template_parm (parameter_list,
9691                                                 parameter,
9692                                                 is_non_type,
9693                                                 is_parameter_pack);
9694       else
9695        {
9696          tree err_parm = build_tree_list (parameter, parameter);
9697          TREE_VALUE (err_parm) = error_mark_node;
9698          parameter_list = chainon (parameter_list, err_parm);
9699        }
9700
9701       /* If the next token is not a `,', we're done.  */
9702       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9703         break;
9704       /* Otherwise, consume the `,' token.  */
9705       cp_lexer_consume_token (parser->lexer);
9706     }
9707
9708   return end_template_parm_list (parameter_list);
9709 }
9710
9711 /* Parse a template-parameter.
9712
9713    template-parameter:
9714      type-parameter
9715      parameter-declaration
9716
9717    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9718    the parameter.  The TREE_PURPOSE is the default value, if any.
9719    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9720    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9721    set to true iff this parameter is a parameter pack. */
9722
9723 static tree
9724 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9725                               bool *is_parameter_pack)
9726 {
9727   cp_token *token;
9728   cp_parameter_declarator *parameter_declarator;
9729   cp_declarator *id_declarator;
9730   tree parm;
9731
9732   /* Assume it is a type parameter or a template parameter.  */
9733   *is_non_type = false;
9734   /* Assume it not a parameter pack. */
9735   *is_parameter_pack = false;
9736   /* Peek at the next token.  */
9737   token = cp_lexer_peek_token (parser->lexer);
9738   /* If it is `class' or `template', we have a type-parameter.  */
9739   if (token->keyword == RID_TEMPLATE)
9740     return cp_parser_type_parameter (parser, is_parameter_pack);
9741   /* If it is `class' or `typename' we do not know yet whether it is a
9742      type parameter or a non-type parameter.  Consider:
9743
9744        template <typename T, typename T::X X> ...
9745
9746      or:
9747
9748        template <class C, class D*> ...
9749
9750      Here, the first parameter is a type parameter, and the second is
9751      a non-type parameter.  We can tell by looking at the token after
9752      the identifier -- if it is a `,', `=', or `>' then we have a type
9753      parameter.  */
9754   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9755     {
9756       /* Peek at the token after `class' or `typename'.  */
9757       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9758       /* If it's an ellipsis, we have a template type parameter
9759          pack. */
9760       if (token->type == CPP_ELLIPSIS)
9761         return cp_parser_type_parameter (parser, is_parameter_pack);
9762       /* If it's an identifier, skip it.  */
9763       if (token->type == CPP_NAME)
9764         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9765       /* Now, see if the token looks like the end of a template
9766          parameter.  */
9767       if (token->type == CPP_COMMA
9768           || token->type == CPP_EQ
9769           || token->type == CPP_GREATER)
9770         return cp_parser_type_parameter (parser, is_parameter_pack);
9771     }
9772
9773   /* Otherwise, it is a non-type parameter.
9774
9775      [temp.param]
9776
9777      When parsing a default template-argument for a non-type
9778      template-parameter, the first non-nested `>' is taken as the end
9779      of the template parameter-list rather than a greater-than
9780      operator.  */
9781   *is_non_type = true;
9782   parameter_declarator
9783      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9784                                         /*parenthesized_p=*/NULL);
9785
9786   /* If the parameter declaration is marked as a parameter pack, set
9787      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9788      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9789      grokdeclarator. */
9790   if (parameter_declarator
9791       && parameter_declarator->declarator
9792       && parameter_declarator->declarator->parameter_pack_p)
9793     {
9794       *is_parameter_pack = true;
9795       parameter_declarator->declarator->parameter_pack_p = false;
9796     }
9797
9798   /* If the next token is an ellipsis, and we don't already have it
9799      marked as a parameter pack, then we have a parameter pack (that
9800      has no declarator).  */
9801   if (!*is_parameter_pack
9802       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9803       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9804     {
9805       /* Consume the `...'.  */
9806       cp_lexer_consume_token (parser->lexer);
9807       maybe_warn_variadic_templates ();
9808       
9809       *is_parameter_pack = true;
9810     }
9811   /* We might end up with a pack expansion as the type of the non-type
9812      template parameter, in which case this is a non-type template
9813      parameter pack.  */
9814   else if (parameter_declarator
9815            && parameter_declarator->decl_specifiers.type
9816            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9817     {
9818       *is_parameter_pack = true;
9819       parameter_declarator->decl_specifiers.type = 
9820         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9821     }
9822
9823   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9824     {
9825       /* Parameter packs cannot have default arguments.  However, a
9826          user may try to do so, so we'll parse them and give an
9827          appropriate diagnostic here.  */
9828
9829       /* Consume the `='.  */
9830       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9831       cp_lexer_consume_token (parser->lexer);
9832       
9833       /* Find the name of the parameter pack.  */     
9834       id_declarator = parameter_declarator->declarator;
9835       while (id_declarator && id_declarator->kind != cdk_id)
9836         id_declarator = id_declarator->declarator;
9837       
9838       if (id_declarator && id_declarator->kind == cdk_id)
9839         error ("%Htemplate parameter pack %qD cannot have a default argument",
9840                &start_token->location, id_declarator->u.id.unqualified_name);
9841       else
9842         error ("%Htemplate parameter pack cannot have a default argument",
9843                &start_token->location);
9844       
9845       /* Parse the default argument, but throw away the result.  */
9846       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9847     }
9848
9849   parm = grokdeclarator (parameter_declarator->declarator,
9850                          &parameter_declarator->decl_specifiers,
9851                          PARM, /*initialized=*/0,
9852                          /*attrlist=*/NULL);
9853   if (parm == error_mark_node)
9854     return error_mark_node;
9855
9856   return build_tree_list (parameter_declarator->default_argument, parm);
9857 }
9858
9859 /* Parse a type-parameter.
9860
9861    type-parameter:
9862      class identifier [opt]
9863      class identifier [opt] = type-id
9864      typename identifier [opt]
9865      typename identifier [opt] = type-id
9866      template < template-parameter-list > class identifier [opt]
9867      template < template-parameter-list > class identifier [opt]
9868        = id-expression
9869
9870    GNU Extension (variadic templates):
9871
9872    type-parameter:
9873      class ... identifier [opt]
9874      typename ... identifier [opt]
9875
9876    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9877    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9878    the declaration of the parameter.
9879
9880    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9881
9882 static tree
9883 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9884 {
9885   cp_token *token;
9886   tree parameter;
9887
9888   /* Look for a keyword to tell us what kind of parameter this is.  */
9889   token = cp_parser_require (parser, CPP_KEYWORD,
9890                              "%<class%>, %<typename%>, or %<template%>");
9891   if (!token)
9892     return error_mark_node;
9893
9894   switch (token->keyword)
9895     {
9896     case RID_CLASS:
9897     case RID_TYPENAME:
9898       {
9899         tree identifier;
9900         tree default_argument;
9901
9902         /* If the next token is an ellipsis, we have a template
9903            argument pack. */
9904         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9905           {
9906             /* Consume the `...' token. */
9907             cp_lexer_consume_token (parser->lexer);
9908             maybe_warn_variadic_templates ();
9909
9910             *is_parameter_pack = true;
9911           }
9912
9913         /* If the next token is an identifier, then it names the
9914            parameter.  */
9915         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9916           identifier = cp_parser_identifier (parser);
9917         else
9918           identifier = NULL_TREE;
9919
9920         /* Create the parameter.  */
9921         parameter = finish_template_type_parm (class_type_node, identifier);
9922
9923         /* If the next token is an `=', we have a default argument.  */
9924         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9925           {
9926             /* Consume the `=' token.  */
9927             cp_lexer_consume_token (parser->lexer);
9928             /* Parse the default-argument.  */
9929             push_deferring_access_checks (dk_no_deferred);
9930             default_argument = cp_parser_type_id (parser);
9931
9932             /* Template parameter packs cannot have default
9933                arguments. */
9934             if (*is_parameter_pack)
9935               {
9936                 if (identifier)
9937                   error ("%Htemplate parameter pack %qD cannot have a "
9938                          "default argument", &token->location, identifier);
9939                 else
9940                   error ("%Htemplate parameter packs cannot have "
9941                          "default arguments", &token->location);
9942                 default_argument = NULL_TREE;
9943               }
9944             pop_deferring_access_checks ();
9945           }
9946         else
9947           default_argument = NULL_TREE;
9948
9949         /* Create the combined representation of the parameter and the
9950            default argument.  */
9951         parameter = build_tree_list (default_argument, parameter);
9952       }
9953       break;
9954
9955     case RID_TEMPLATE:
9956       {
9957         tree parameter_list;
9958         tree identifier;
9959         tree default_argument;
9960
9961         /* Look for the `<'.  */
9962         cp_parser_require (parser, CPP_LESS, "%<<%>");
9963         /* Parse the template-parameter-list.  */
9964         parameter_list = cp_parser_template_parameter_list (parser);
9965         /* Look for the `>'.  */
9966         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9967         /* Look for the `class' keyword.  */
9968         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9969         /* If the next token is an ellipsis, we have a template
9970            argument pack. */
9971         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9972           {
9973             /* Consume the `...' token. */
9974             cp_lexer_consume_token (parser->lexer);
9975             maybe_warn_variadic_templates ();
9976
9977             *is_parameter_pack = true;
9978           }
9979         /* If the next token is an `=', then there is a
9980            default-argument.  If the next token is a `>', we are at
9981            the end of the parameter-list.  If the next token is a `,',
9982            then we are at the end of this parameter.  */
9983         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9984             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9985             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9986           {
9987             identifier = cp_parser_identifier (parser);
9988             /* Treat invalid names as if the parameter were nameless.  */
9989             if (identifier == error_mark_node)
9990               identifier = NULL_TREE;
9991           }
9992         else
9993           identifier = NULL_TREE;
9994
9995         /* Create the template parameter.  */
9996         parameter = finish_template_template_parm (class_type_node,
9997                                                    identifier);
9998
9999         /* If the next token is an `=', then there is a
10000            default-argument.  */
10001         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10002           {
10003             bool is_template;
10004
10005             /* Consume the `='.  */
10006             cp_lexer_consume_token (parser->lexer);
10007             /* Parse the id-expression.  */
10008             push_deferring_access_checks (dk_no_deferred);
10009             /* save token before parsing the id-expression, for error
10010                reporting */
10011             token = cp_lexer_peek_token (parser->lexer);
10012             default_argument
10013               = cp_parser_id_expression (parser,
10014                                          /*template_keyword_p=*/false,
10015                                          /*check_dependency_p=*/true,
10016                                          /*template_p=*/&is_template,
10017                                          /*declarator_p=*/false,
10018                                          /*optional_p=*/false);
10019             if (TREE_CODE (default_argument) == TYPE_DECL)
10020               /* If the id-expression was a template-id that refers to
10021                  a template-class, we already have the declaration here,
10022                  so no further lookup is needed.  */
10023                  ;
10024             else
10025               /* Look up the name.  */
10026               default_argument
10027                 = cp_parser_lookup_name (parser, default_argument,
10028                                          none_type,
10029                                          /*is_template=*/is_template,
10030                                          /*is_namespace=*/false,
10031                                          /*check_dependency=*/true,
10032                                          /*ambiguous_decls=*/NULL,
10033                                          token->location);
10034             /* See if the default argument is valid.  */
10035             default_argument
10036               = check_template_template_default_arg (default_argument);
10037
10038             /* Template parameter packs cannot have default
10039                arguments. */
10040             if (*is_parameter_pack)
10041               {
10042                 if (identifier)
10043                   error ("%Htemplate parameter pack %qD cannot "
10044                          "have a default argument",
10045                          &token->location, identifier);
10046                 else
10047                   error ("%Htemplate parameter packs cannot "
10048                          "have default arguments",
10049                          &token->location);
10050                 default_argument = NULL_TREE;
10051               }
10052             pop_deferring_access_checks ();
10053           }
10054         else
10055           default_argument = NULL_TREE;
10056
10057         /* Create the combined representation of the parameter and the
10058            default argument.  */
10059         parameter = build_tree_list (default_argument, parameter);
10060       }
10061       break;
10062
10063     default:
10064       gcc_unreachable ();
10065       break;
10066     }
10067
10068   return parameter;
10069 }
10070
10071 /* Parse a template-id.
10072
10073    template-id:
10074      template-name < template-argument-list [opt] >
10075
10076    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10077    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10078    returned.  Otherwise, if the template-name names a function, or set
10079    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10080    names a class, returns a TYPE_DECL for the specialization.
10081
10082    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10083    uninstantiated templates.  */
10084
10085 static tree
10086 cp_parser_template_id (cp_parser *parser,
10087                        bool template_keyword_p,
10088                        bool check_dependency_p,
10089                        bool is_declaration)
10090 {
10091   int i;
10092   tree templ;
10093   tree arguments;
10094   tree template_id;
10095   cp_token_position start_of_id = 0;
10096   deferred_access_check *chk;
10097   VEC (deferred_access_check,gc) *access_check;
10098   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10099   bool is_identifier;
10100
10101   /* If the next token corresponds to a template-id, there is no need
10102      to reparse it.  */
10103   next_token = cp_lexer_peek_token (parser->lexer);
10104   if (next_token->type == CPP_TEMPLATE_ID)
10105     {
10106       struct tree_check *check_value;
10107
10108       /* Get the stored value.  */
10109       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10110       /* Perform any access checks that were deferred.  */
10111       access_check = check_value->checks;
10112       if (access_check)
10113         {
10114           for (i = 0 ;
10115                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10116                ++i)
10117             {
10118               perform_or_defer_access_check (chk->binfo,
10119                                              chk->decl,
10120                                              chk->diag_decl);
10121             }
10122         }
10123       /* Return the stored value.  */
10124       return check_value->value;
10125     }
10126
10127   /* Avoid performing name lookup if there is no possibility of
10128      finding a template-id.  */
10129   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10130       || (next_token->type == CPP_NAME
10131           && !cp_parser_nth_token_starts_template_argument_list_p
10132                (parser, 2)))
10133     {
10134       cp_parser_error (parser, "expected template-id");
10135       return error_mark_node;
10136     }
10137
10138   /* Remember where the template-id starts.  */
10139   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10140     start_of_id = cp_lexer_token_position (parser->lexer, false);
10141
10142   push_deferring_access_checks (dk_deferred);
10143
10144   /* Parse the template-name.  */
10145   is_identifier = false;
10146   token = cp_lexer_peek_token (parser->lexer);
10147   templ = cp_parser_template_name (parser, template_keyword_p,
10148                                    check_dependency_p,
10149                                    is_declaration,
10150                                    &is_identifier);
10151   if (templ == error_mark_node || is_identifier)
10152     {
10153       pop_deferring_access_checks ();
10154       return templ;
10155     }
10156
10157   /* If we find the sequence `[:' after a template-name, it's probably
10158      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10159      parse correctly the argument list.  */
10160   next_token = cp_lexer_peek_token (parser->lexer);
10161   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10162   if (next_token->type == CPP_OPEN_SQUARE
10163       && next_token->flags & DIGRAPH
10164       && next_token_2->type == CPP_COLON
10165       && !(next_token_2->flags & PREV_WHITE))
10166     {
10167       cp_parser_parse_tentatively (parser);
10168       /* Change `:' into `::'.  */
10169       next_token_2->type = CPP_SCOPE;
10170       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10171          CPP_LESS.  */
10172       cp_lexer_consume_token (parser->lexer);
10173
10174       /* Parse the arguments.  */
10175       arguments = cp_parser_enclosed_template_argument_list (parser);
10176       if (!cp_parser_parse_definitely (parser))
10177         {
10178           /* If we couldn't parse an argument list, then we revert our changes
10179              and return simply an error. Maybe this is not a template-id
10180              after all.  */
10181           next_token_2->type = CPP_COLON;
10182           cp_parser_error (parser, "expected %<<%>");
10183           pop_deferring_access_checks ();
10184           return error_mark_node;
10185         }
10186       /* Otherwise, emit an error about the invalid digraph, but continue
10187          parsing because we got our argument list.  */
10188       if (permerror (next_token->location,
10189                      "%<<::%> cannot begin a template-argument list"))
10190         {
10191           static bool hint = false;
10192           inform (next_token->location,
10193                   "%<<:%> is an alternate spelling for %<[%>."
10194                   " Insert whitespace between %<<%> and %<::%>");
10195           if (!hint && !flag_permissive)
10196             {
10197               inform (next_token->location, "(if you use %<-fpermissive%>"
10198                       " G++ will accept your code)");
10199               hint = true;
10200             }
10201         }
10202     }
10203   else
10204     {
10205       /* Look for the `<' that starts the template-argument-list.  */
10206       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10207         {
10208           pop_deferring_access_checks ();
10209           return error_mark_node;
10210         }
10211       /* Parse the arguments.  */
10212       arguments = cp_parser_enclosed_template_argument_list (parser);
10213     }
10214
10215   /* Build a representation of the specialization.  */
10216   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10217     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10218   else if (DECL_CLASS_TEMPLATE_P (templ)
10219            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10220     {
10221       bool entering_scope;
10222       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10223          template (rather than some instantiation thereof) only if
10224          is not nested within some other construct.  For example, in
10225          "template <typename T> void f(T) { A<T>::", A<T> is just an
10226          instantiation of A.  */
10227       entering_scope = (template_parm_scope_p ()
10228                         && cp_lexer_next_token_is (parser->lexer,
10229                                                    CPP_SCOPE));
10230       template_id
10231         = finish_template_type (templ, arguments, entering_scope);
10232     }
10233   else
10234     {
10235       /* If it's not a class-template or a template-template, it should be
10236          a function-template.  */
10237       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10238                    || TREE_CODE (templ) == OVERLOAD
10239                    || BASELINK_P (templ)));
10240
10241       template_id = lookup_template_function (templ, arguments);
10242     }
10243
10244   /* If parsing tentatively, replace the sequence of tokens that makes
10245      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10246      should we re-parse the token stream, we will not have to repeat
10247      the effort required to do the parse, nor will we issue duplicate
10248      error messages about problems during instantiation of the
10249      template.  */
10250   if (start_of_id)
10251     {
10252       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10253
10254       /* Reset the contents of the START_OF_ID token.  */
10255       token->type = CPP_TEMPLATE_ID;
10256       /* Retrieve any deferred checks.  Do not pop this access checks yet
10257          so the memory will not be reclaimed during token replacing below.  */
10258       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10259       token->u.tree_check_value->value = template_id;
10260       token->u.tree_check_value->checks = get_deferred_access_checks ();
10261       token->keyword = RID_MAX;
10262
10263       /* Purge all subsequent tokens.  */
10264       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10265
10266       /* ??? Can we actually assume that, if template_id ==
10267          error_mark_node, we will have issued a diagnostic to the
10268          user, as opposed to simply marking the tentative parse as
10269          failed?  */
10270       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10271         error ("%Hparse error in template argument list",
10272                &token->location);
10273     }
10274
10275   pop_deferring_access_checks ();
10276   return template_id;
10277 }
10278
10279 /* Parse a template-name.
10280
10281    template-name:
10282      identifier
10283
10284    The standard should actually say:
10285
10286    template-name:
10287      identifier
10288      operator-function-id
10289
10290    A defect report has been filed about this issue.
10291
10292    A conversion-function-id cannot be a template name because they cannot
10293    be part of a template-id. In fact, looking at this code:
10294
10295    a.operator K<int>()
10296
10297    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10298    It is impossible to call a templated conversion-function-id with an
10299    explicit argument list, since the only allowed template parameter is
10300    the type to which it is converting.
10301
10302    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10303    `template' keyword, in a construction like:
10304
10305      T::template f<3>()
10306
10307    In that case `f' is taken to be a template-name, even though there
10308    is no way of knowing for sure.
10309
10310    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10311    name refers to a set of overloaded functions, at least one of which
10312    is a template, or an IDENTIFIER_NODE with the name of the template,
10313    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10314    names are looked up inside uninstantiated templates.  */
10315
10316 static tree
10317 cp_parser_template_name (cp_parser* parser,
10318                          bool template_keyword_p,
10319                          bool check_dependency_p,
10320                          bool is_declaration,
10321                          bool *is_identifier)
10322 {
10323   tree identifier;
10324   tree decl;
10325   tree fns;
10326   cp_token *token = cp_lexer_peek_token (parser->lexer);
10327
10328   /* If the next token is `operator', then we have either an
10329      operator-function-id or a conversion-function-id.  */
10330   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10331     {
10332       /* We don't know whether we're looking at an
10333          operator-function-id or a conversion-function-id.  */
10334       cp_parser_parse_tentatively (parser);
10335       /* Try an operator-function-id.  */
10336       identifier = cp_parser_operator_function_id (parser);
10337       /* If that didn't work, try a conversion-function-id.  */
10338       if (!cp_parser_parse_definitely (parser))
10339         {
10340           cp_parser_error (parser, "expected template-name");
10341           return error_mark_node;
10342         }
10343     }
10344   /* Look for the identifier.  */
10345   else
10346     identifier = cp_parser_identifier (parser);
10347
10348   /* If we didn't find an identifier, we don't have a template-id.  */
10349   if (identifier == error_mark_node)
10350     return error_mark_node;
10351
10352   /* If the name immediately followed the `template' keyword, then it
10353      is a template-name.  However, if the next token is not `<', then
10354      we do not treat it as a template-name, since it is not being used
10355      as part of a template-id.  This enables us to handle constructs
10356      like:
10357
10358        template <typename T> struct S { S(); };
10359        template <typename T> S<T>::S();
10360
10361      correctly.  We would treat `S' as a template -- if it were `S<T>'
10362      -- but we do not if there is no `<'.  */
10363
10364   if (processing_template_decl
10365       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10366     {
10367       /* In a declaration, in a dependent context, we pretend that the
10368          "template" keyword was present in order to improve error
10369          recovery.  For example, given:
10370
10371            template <typename T> void f(T::X<int>);
10372
10373          we want to treat "X<int>" as a template-id.  */
10374       if (is_declaration
10375           && !template_keyword_p
10376           && parser->scope && TYPE_P (parser->scope)
10377           && check_dependency_p
10378           && dependent_scope_p (parser->scope)
10379           /* Do not do this for dtors (or ctors), since they never
10380              need the template keyword before their name.  */
10381           && !constructor_name_p (identifier, parser->scope))
10382         {
10383           cp_token_position start = 0;
10384
10385           /* Explain what went wrong.  */
10386           error ("%Hnon-template %qD used as template",
10387                  &token->location, identifier);
10388           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10389                   parser->scope, identifier);
10390           /* If parsing tentatively, find the location of the "<" token.  */
10391           if (cp_parser_simulate_error (parser))
10392             start = cp_lexer_token_position (parser->lexer, true);
10393           /* Parse the template arguments so that we can issue error
10394              messages about them.  */
10395           cp_lexer_consume_token (parser->lexer);
10396           cp_parser_enclosed_template_argument_list (parser);
10397           /* Skip tokens until we find a good place from which to
10398              continue parsing.  */
10399           cp_parser_skip_to_closing_parenthesis (parser,
10400                                                  /*recovering=*/true,
10401                                                  /*or_comma=*/true,
10402                                                  /*consume_paren=*/false);
10403           /* If parsing tentatively, permanently remove the
10404              template argument list.  That will prevent duplicate
10405              error messages from being issued about the missing
10406              "template" keyword.  */
10407           if (start)
10408             cp_lexer_purge_tokens_after (parser->lexer, start);
10409           if (is_identifier)
10410             *is_identifier = true;
10411           return identifier;
10412         }
10413
10414       /* If the "template" keyword is present, then there is generally
10415          no point in doing name-lookup, so we just return IDENTIFIER.
10416          But, if the qualifying scope is non-dependent then we can
10417          (and must) do name-lookup normally.  */
10418       if (template_keyword_p
10419           && (!parser->scope
10420               || (TYPE_P (parser->scope)
10421                   && dependent_type_p (parser->scope))))
10422         return identifier;
10423     }
10424
10425   /* Look up the name.  */
10426   decl = cp_parser_lookup_name (parser, identifier,
10427                                 none_type,
10428                                 /*is_template=*/false,
10429                                 /*is_namespace=*/false,
10430                                 check_dependency_p,
10431                                 /*ambiguous_decls=*/NULL,
10432                                 token->location);
10433   decl = maybe_get_template_decl_from_type_decl (decl);
10434
10435   /* If DECL is a template, then the name was a template-name.  */
10436   if (TREE_CODE (decl) == TEMPLATE_DECL)
10437     ;
10438   else
10439     {
10440       tree fn = NULL_TREE;
10441
10442       /* The standard does not explicitly indicate whether a name that
10443          names a set of overloaded declarations, some of which are
10444          templates, is a template-name.  However, such a name should
10445          be a template-name; otherwise, there is no way to form a
10446          template-id for the overloaded templates.  */
10447       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10448       if (TREE_CODE (fns) == OVERLOAD)
10449         for (fn = fns; fn; fn = OVL_NEXT (fn))
10450           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10451             break;
10452
10453       if (!fn)
10454         {
10455           /* The name does not name a template.  */
10456           cp_parser_error (parser, "expected template-name");
10457           return error_mark_node;
10458         }
10459     }
10460
10461   /* If DECL is dependent, and refers to a function, then just return
10462      its name; we will look it up again during template instantiation.  */
10463   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10464     {
10465       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10466       if (TYPE_P (scope) && dependent_type_p (scope))
10467         return identifier;
10468     }
10469
10470   return decl;
10471 }
10472
10473 /* Parse a template-argument-list.
10474
10475    template-argument-list:
10476      template-argument ... [opt]
10477      template-argument-list , template-argument ... [opt]
10478
10479    Returns a TREE_VEC containing the arguments.  */
10480
10481 static tree
10482 cp_parser_template_argument_list (cp_parser* parser)
10483 {
10484   tree fixed_args[10];
10485   unsigned n_args = 0;
10486   unsigned alloced = 10;
10487   tree *arg_ary = fixed_args;
10488   tree vec;
10489   bool saved_in_template_argument_list_p;
10490   bool saved_ice_p;
10491   bool saved_non_ice_p;
10492
10493   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10494   parser->in_template_argument_list_p = true;
10495   /* Even if the template-id appears in an integral
10496      constant-expression, the contents of the argument list do
10497      not.  */
10498   saved_ice_p = parser->integral_constant_expression_p;
10499   parser->integral_constant_expression_p = false;
10500   saved_non_ice_p = parser->non_integral_constant_expression_p;
10501   parser->non_integral_constant_expression_p = false;
10502   /* Parse the arguments.  */
10503   do
10504     {
10505       tree argument;
10506
10507       if (n_args)
10508         /* Consume the comma.  */
10509         cp_lexer_consume_token (parser->lexer);
10510
10511       /* Parse the template-argument.  */
10512       argument = cp_parser_template_argument (parser);
10513
10514       /* If the next token is an ellipsis, we're expanding a template
10515          argument pack. */
10516       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10517         {
10518           if (argument == error_mark_node)
10519             {
10520               cp_token *token = cp_lexer_peek_token (parser->lexer);
10521               error ("%Hexpected parameter pack before %<...%>",
10522                      &token->location);
10523             }
10524           /* Consume the `...' token. */
10525           cp_lexer_consume_token (parser->lexer);
10526
10527           /* Make the argument into a TYPE_PACK_EXPANSION or
10528              EXPR_PACK_EXPANSION. */
10529           argument = make_pack_expansion (argument);
10530         }
10531
10532       if (n_args == alloced)
10533         {
10534           alloced *= 2;
10535
10536           if (arg_ary == fixed_args)
10537             {
10538               arg_ary = XNEWVEC (tree, alloced);
10539               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10540             }
10541           else
10542             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10543         }
10544       arg_ary[n_args++] = argument;
10545     }
10546   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10547
10548   vec = make_tree_vec (n_args);
10549
10550   while (n_args--)
10551     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10552
10553   if (arg_ary != fixed_args)
10554     free (arg_ary);
10555   parser->non_integral_constant_expression_p = saved_non_ice_p;
10556   parser->integral_constant_expression_p = saved_ice_p;
10557   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10558   return vec;
10559 }
10560
10561 /* Parse a template-argument.
10562
10563    template-argument:
10564      assignment-expression
10565      type-id
10566      id-expression
10567
10568    The representation is that of an assignment-expression, type-id, or
10569    id-expression -- except that the qualified id-expression is
10570    evaluated, so that the value returned is either a DECL or an
10571    OVERLOAD.
10572
10573    Although the standard says "assignment-expression", it forbids
10574    throw-expressions or assignments in the template argument.
10575    Therefore, we use "conditional-expression" instead.  */
10576
10577 static tree
10578 cp_parser_template_argument (cp_parser* parser)
10579 {
10580   tree argument;
10581   bool template_p;
10582   bool address_p;
10583   bool maybe_type_id = false;
10584   cp_token *token = NULL, *argument_start_token = NULL;
10585   cp_id_kind idk;
10586
10587   /* There's really no way to know what we're looking at, so we just
10588      try each alternative in order.
10589
10590        [temp.arg]
10591
10592        In a template-argument, an ambiguity between a type-id and an
10593        expression is resolved to a type-id, regardless of the form of
10594        the corresponding template-parameter.
10595
10596      Therefore, we try a type-id first.  */
10597   cp_parser_parse_tentatively (parser);
10598   argument = cp_parser_template_type_arg (parser);
10599   /* If there was no error parsing the type-id but the next token is a
10600      '>>', our behavior depends on which dialect of C++ we're
10601      parsing. In C++98, we probably found a typo for '> >'. But there
10602      are type-id which are also valid expressions. For instance:
10603
10604      struct X { int operator >> (int); };
10605      template <int V> struct Foo {};
10606      Foo<X () >> 5> r;
10607
10608      Here 'X()' is a valid type-id of a function type, but the user just
10609      wanted to write the expression "X() >> 5". Thus, we remember that we
10610      found a valid type-id, but we still try to parse the argument as an
10611      expression to see what happens. 
10612
10613      In C++0x, the '>>' will be considered two separate '>'
10614      tokens.  */
10615   if (!cp_parser_error_occurred (parser)
10616       && cxx_dialect == cxx98
10617       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10618     {
10619       maybe_type_id = true;
10620       cp_parser_abort_tentative_parse (parser);
10621     }
10622   else
10623     {
10624       /* If the next token isn't a `,' or a `>', then this argument wasn't
10625       really finished. This means that the argument is not a valid
10626       type-id.  */
10627       if (!cp_parser_next_token_ends_template_argument_p (parser))
10628         cp_parser_error (parser, "expected template-argument");
10629       /* If that worked, we're done.  */
10630       if (cp_parser_parse_definitely (parser))
10631         return argument;
10632     }
10633   /* We're still not sure what the argument will be.  */
10634   cp_parser_parse_tentatively (parser);
10635   /* Try a template.  */
10636   argument_start_token = cp_lexer_peek_token (parser->lexer);
10637   argument = cp_parser_id_expression (parser,
10638                                       /*template_keyword_p=*/false,
10639                                       /*check_dependency_p=*/true,
10640                                       &template_p,
10641                                       /*declarator_p=*/false,
10642                                       /*optional_p=*/false);
10643   /* If the next token isn't a `,' or a `>', then this argument wasn't
10644      really finished.  */
10645   if (!cp_parser_next_token_ends_template_argument_p (parser))
10646     cp_parser_error (parser, "expected template-argument");
10647   if (!cp_parser_error_occurred (parser))
10648     {
10649       /* Figure out what is being referred to.  If the id-expression
10650          was for a class template specialization, then we will have a
10651          TYPE_DECL at this point.  There is no need to do name lookup
10652          at this point in that case.  */
10653       if (TREE_CODE (argument) != TYPE_DECL)
10654         argument = cp_parser_lookup_name (parser, argument,
10655                                           none_type,
10656                                           /*is_template=*/template_p,
10657                                           /*is_namespace=*/false,
10658                                           /*check_dependency=*/true,
10659                                           /*ambiguous_decls=*/NULL,
10660                                           argument_start_token->location);
10661       if (TREE_CODE (argument) != TEMPLATE_DECL
10662           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10663         cp_parser_error (parser, "expected template-name");
10664     }
10665   if (cp_parser_parse_definitely (parser))
10666     return argument;
10667   /* It must be a non-type argument.  There permitted cases are given
10668      in [temp.arg.nontype]:
10669
10670      -- an integral constant-expression of integral or enumeration
10671         type; or
10672
10673      -- the name of a non-type template-parameter; or
10674
10675      -- the name of an object or function with external linkage...
10676
10677      -- the address of an object or function with external linkage...
10678
10679      -- a pointer to member...  */
10680   /* Look for a non-type template parameter.  */
10681   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10682     {
10683       cp_parser_parse_tentatively (parser);
10684       argument = cp_parser_primary_expression (parser,
10685                                                /*address_p=*/false,
10686                                                /*cast_p=*/false,
10687                                                /*template_arg_p=*/true,
10688                                                &idk);
10689       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10690           || !cp_parser_next_token_ends_template_argument_p (parser))
10691         cp_parser_simulate_error (parser);
10692       if (cp_parser_parse_definitely (parser))
10693         return argument;
10694     }
10695
10696   /* If the next token is "&", the argument must be the address of an
10697      object or function with external linkage.  */
10698   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10699   if (address_p)
10700     cp_lexer_consume_token (parser->lexer);
10701   /* See if we might have an id-expression.  */
10702   token = cp_lexer_peek_token (parser->lexer);
10703   if (token->type == CPP_NAME
10704       || token->keyword == RID_OPERATOR
10705       || token->type == CPP_SCOPE
10706       || token->type == CPP_TEMPLATE_ID
10707       || token->type == CPP_NESTED_NAME_SPECIFIER)
10708     {
10709       cp_parser_parse_tentatively (parser);
10710       argument = cp_parser_primary_expression (parser,
10711                                                address_p,
10712                                                /*cast_p=*/false,
10713                                                /*template_arg_p=*/true,
10714                                                &idk);
10715       if (cp_parser_error_occurred (parser)
10716           || !cp_parser_next_token_ends_template_argument_p (parser))
10717         cp_parser_abort_tentative_parse (parser);
10718       else
10719         {
10720           if (TREE_CODE (argument) == INDIRECT_REF)
10721             {
10722               gcc_assert (REFERENCE_REF_P (argument));
10723               argument = TREE_OPERAND (argument, 0);
10724             }
10725
10726           if (TREE_CODE (argument) == VAR_DECL)
10727             {
10728               /* A variable without external linkage might still be a
10729                  valid constant-expression, so no error is issued here
10730                  if the external-linkage check fails.  */
10731               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10732                 cp_parser_simulate_error (parser);
10733             }
10734           else if (is_overloaded_fn (argument))
10735             /* All overloaded functions are allowed; if the external
10736                linkage test does not pass, an error will be issued
10737                later.  */
10738             ;
10739           else if (address_p
10740                    && (TREE_CODE (argument) == OFFSET_REF
10741                        || TREE_CODE (argument) == SCOPE_REF))
10742             /* A pointer-to-member.  */
10743             ;
10744           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10745             ;
10746           else
10747             cp_parser_simulate_error (parser);
10748
10749           if (cp_parser_parse_definitely (parser))
10750             {
10751               if (address_p)
10752                 argument = build_x_unary_op (ADDR_EXPR, argument,
10753                                              tf_warning_or_error);
10754               return argument;
10755             }
10756         }
10757     }
10758   /* If the argument started with "&", there are no other valid
10759      alternatives at this point.  */
10760   if (address_p)
10761     {
10762       cp_parser_error (parser, "invalid non-type template argument");
10763       return error_mark_node;
10764     }
10765
10766   /* If the argument wasn't successfully parsed as a type-id followed
10767      by '>>', the argument can only be a constant expression now.
10768      Otherwise, we try parsing the constant-expression tentatively,
10769      because the argument could really be a type-id.  */
10770   if (maybe_type_id)
10771     cp_parser_parse_tentatively (parser);
10772   argument = cp_parser_constant_expression (parser,
10773                                             /*allow_non_constant_p=*/false,
10774                                             /*non_constant_p=*/NULL);
10775   argument = fold_non_dependent_expr (argument);
10776   if (!maybe_type_id)
10777     return argument;
10778   if (!cp_parser_next_token_ends_template_argument_p (parser))
10779     cp_parser_error (parser, "expected template-argument");
10780   if (cp_parser_parse_definitely (parser))
10781     return argument;
10782   /* We did our best to parse the argument as a non type-id, but that
10783      was the only alternative that matched (albeit with a '>' after
10784      it). We can assume it's just a typo from the user, and a
10785      diagnostic will then be issued.  */
10786   return cp_parser_template_type_arg (parser);
10787 }
10788
10789 /* Parse an explicit-instantiation.
10790
10791    explicit-instantiation:
10792      template declaration
10793
10794    Although the standard says `declaration', what it really means is:
10795
10796    explicit-instantiation:
10797      template decl-specifier-seq [opt] declarator [opt] ;
10798
10799    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10800    supposed to be allowed.  A defect report has been filed about this
10801    issue.
10802
10803    GNU Extension:
10804
10805    explicit-instantiation:
10806      storage-class-specifier template
10807        decl-specifier-seq [opt] declarator [opt] ;
10808      function-specifier template
10809        decl-specifier-seq [opt] declarator [opt] ;  */
10810
10811 static void
10812 cp_parser_explicit_instantiation (cp_parser* parser)
10813 {
10814   int declares_class_or_enum;
10815   cp_decl_specifier_seq decl_specifiers;
10816   tree extension_specifier = NULL_TREE;
10817   cp_token *token;
10818
10819   /* Look for an (optional) storage-class-specifier or
10820      function-specifier.  */
10821   if (cp_parser_allow_gnu_extensions_p (parser))
10822     {
10823       extension_specifier
10824         = cp_parser_storage_class_specifier_opt (parser);
10825       if (!extension_specifier)
10826         extension_specifier
10827           = cp_parser_function_specifier_opt (parser,
10828                                               /*decl_specs=*/NULL);
10829     }
10830
10831   /* Look for the `template' keyword.  */
10832   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10833   /* Let the front end know that we are processing an explicit
10834      instantiation.  */
10835   begin_explicit_instantiation ();
10836   /* [temp.explicit] says that we are supposed to ignore access
10837      control while processing explicit instantiation directives.  */
10838   push_deferring_access_checks (dk_no_check);
10839   /* Parse a decl-specifier-seq.  */
10840   token = cp_lexer_peek_token (parser->lexer);
10841   cp_parser_decl_specifier_seq (parser,
10842                                 CP_PARSER_FLAGS_OPTIONAL,
10843                                 &decl_specifiers,
10844                                 &declares_class_or_enum);
10845   /* If there was exactly one decl-specifier, and it declared a class,
10846      and there's no declarator, then we have an explicit type
10847      instantiation.  */
10848   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10849     {
10850       tree type;
10851
10852       type = check_tag_decl (&decl_specifiers);
10853       /* Turn access control back on for names used during
10854          template instantiation.  */
10855       pop_deferring_access_checks ();
10856       if (type)
10857         do_type_instantiation (type, extension_specifier,
10858                                /*complain=*/tf_error);
10859     }
10860   else
10861     {
10862       cp_declarator *declarator;
10863       tree decl;
10864
10865       /* Parse the declarator.  */
10866       declarator
10867         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10868                                 /*ctor_dtor_or_conv_p=*/NULL,
10869                                 /*parenthesized_p=*/NULL,
10870                                 /*member_p=*/false);
10871       if (declares_class_or_enum & 2)
10872         cp_parser_check_for_definition_in_return_type (declarator,
10873                                                        decl_specifiers.type,
10874                                                        decl_specifiers.type_location);
10875       if (declarator != cp_error_declarator)
10876         {
10877           decl = grokdeclarator (declarator, &decl_specifiers,
10878                                  NORMAL, 0, &decl_specifiers.attributes);
10879           /* Turn access control back on for names used during
10880              template instantiation.  */
10881           pop_deferring_access_checks ();
10882           /* Do the explicit instantiation.  */
10883           do_decl_instantiation (decl, extension_specifier);
10884         }
10885       else
10886         {
10887           pop_deferring_access_checks ();
10888           /* Skip the body of the explicit instantiation.  */
10889           cp_parser_skip_to_end_of_statement (parser);
10890         }
10891     }
10892   /* We're done with the instantiation.  */
10893   end_explicit_instantiation ();
10894
10895   cp_parser_consume_semicolon_at_end_of_statement (parser);
10896 }
10897
10898 /* Parse an explicit-specialization.
10899
10900    explicit-specialization:
10901      template < > declaration
10902
10903    Although the standard says `declaration', what it really means is:
10904
10905    explicit-specialization:
10906      template <> decl-specifier [opt] init-declarator [opt] ;
10907      template <> function-definition
10908      template <> explicit-specialization
10909      template <> template-declaration  */
10910
10911 static void
10912 cp_parser_explicit_specialization (cp_parser* parser)
10913 {
10914   bool need_lang_pop;
10915   cp_token *token = cp_lexer_peek_token (parser->lexer);
10916
10917   /* Look for the `template' keyword.  */
10918   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10919   /* Look for the `<'.  */
10920   cp_parser_require (parser, CPP_LESS, "%<<%>");
10921   /* Look for the `>'.  */
10922   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10923   /* We have processed another parameter list.  */
10924   ++parser->num_template_parameter_lists;
10925   /* [temp]
10926
10927      A template ... explicit specialization ... shall not have C
10928      linkage.  */
10929   if (current_lang_name == lang_name_c)
10930     {
10931       error ("%Htemplate specialization with C linkage", &token->location);
10932       /* Give it C++ linkage to avoid confusing other parts of the
10933          front end.  */
10934       push_lang_context (lang_name_cplusplus);
10935       need_lang_pop = true;
10936     }
10937   else
10938     need_lang_pop = false;
10939   /* Let the front end know that we are beginning a specialization.  */
10940   if (!begin_specialization ())
10941     {
10942       end_specialization ();
10943       return;
10944     }
10945
10946   /* If the next keyword is `template', we need to figure out whether
10947      or not we're looking a template-declaration.  */
10948   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10949     {
10950       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10951           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10952         cp_parser_template_declaration_after_export (parser,
10953                                                      /*member_p=*/false);
10954       else
10955         cp_parser_explicit_specialization (parser);
10956     }
10957   else
10958     /* Parse the dependent declaration.  */
10959     cp_parser_single_declaration (parser,
10960                                   /*checks=*/NULL,
10961                                   /*member_p=*/false,
10962                                   /*explicit_specialization_p=*/true,
10963                                   /*friend_p=*/NULL);
10964   /* We're done with the specialization.  */
10965   end_specialization ();
10966   /* For the erroneous case of a template with C linkage, we pushed an
10967      implicit C++ linkage scope; exit that scope now.  */
10968   if (need_lang_pop)
10969     pop_lang_context ();
10970   /* We're done with this parameter list.  */
10971   --parser->num_template_parameter_lists;
10972 }
10973
10974 /* Parse a type-specifier.
10975
10976    type-specifier:
10977      simple-type-specifier
10978      class-specifier
10979      enum-specifier
10980      elaborated-type-specifier
10981      cv-qualifier
10982
10983    GNU Extension:
10984
10985    type-specifier:
10986      __complex__
10987
10988    Returns a representation of the type-specifier.  For a
10989    class-specifier, enum-specifier, or elaborated-type-specifier, a
10990    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10991
10992    The parser flags FLAGS is used to control type-specifier parsing.
10993
10994    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10995    in a decl-specifier-seq.
10996
10997    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10998    class-specifier, enum-specifier, or elaborated-type-specifier, then
10999    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11000    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11001    zero.
11002
11003    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11004    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11005    is set to FALSE.  */
11006
11007 static tree
11008 cp_parser_type_specifier (cp_parser* parser,
11009                           cp_parser_flags flags,
11010                           cp_decl_specifier_seq *decl_specs,
11011                           bool is_declaration,
11012                           int* declares_class_or_enum,
11013                           bool* is_cv_qualifier)
11014 {
11015   tree type_spec = NULL_TREE;
11016   cp_token *token;
11017   enum rid keyword;
11018   cp_decl_spec ds = ds_last;
11019
11020   /* Assume this type-specifier does not declare a new type.  */
11021   if (declares_class_or_enum)
11022     *declares_class_or_enum = 0;
11023   /* And that it does not specify a cv-qualifier.  */
11024   if (is_cv_qualifier)
11025     *is_cv_qualifier = false;
11026   /* Peek at the next token.  */
11027   token = cp_lexer_peek_token (parser->lexer);
11028
11029   /* If we're looking at a keyword, we can use that to guide the
11030      production we choose.  */
11031   keyword = token->keyword;
11032   switch (keyword)
11033     {
11034     case RID_ENUM:
11035       /* Look for the enum-specifier.  */
11036       type_spec = cp_parser_enum_specifier (parser);
11037       /* If that worked, we're done.  */
11038       if (type_spec)
11039         {
11040           if (declares_class_or_enum)
11041             *declares_class_or_enum = 2;
11042           if (decl_specs)
11043             cp_parser_set_decl_spec_type (decl_specs,
11044                                           type_spec,
11045                                           token->location,
11046                                           /*user_defined_p=*/true);
11047           return type_spec;
11048         }
11049       else
11050         goto elaborated_type_specifier;
11051
11052       /* Any of these indicate either a class-specifier, or an
11053          elaborated-type-specifier.  */
11054     case RID_CLASS:
11055     case RID_STRUCT:
11056     case RID_UNION:
11057       /* Parse tentatively so that we can back up if we don't find a
11058          class-specifier.  */
11059       cp_parser_parse_tentatively (parser);
11060       /* Look for the class-specifier.  */
11061       type_spec = cp_parser_class_specifier (parser);
11062       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11063       /* If that worked, we're done.  */
11064       if (cp_parser_parse_definitely (parser))
11065         {
11066           if (declares_class_or_enum)
11067             *declares_class_or_enum = 2;
11068           if (decl_specs)
11069             cp_parser_set_decl_spec_type (decl_specs,
11070                                           type_spec,
11071                                           token->location,
11072                                           /*user_defined_p=*/true);
11073           return type_spec;
11074         }
11075
11076       /* Fall through.  */
11077     elaborated_type_specifier:
11078       /* We're declaring (not defining) a class or enum.  */
11079       if (declares_class_or_enum)
11080         *declares_class_or_enum = 1;
11081
11082       /* Fall through.  */
11083     case RID_TYPENAME:
11084       /* Look for an elaborated-type-specifier.  */
11085       type_spec
11086         = (cp_parser_elaborated_type_specifier
11087            (parser,
11088             decl_specs && decl_specs->specs[(int) ds_friend],
11089             is_declaration));
11090       if (decl_specs)
11091         cp_parser_set_decl_spec_type (decl_specs,
11092                                       type_spec,
11093                                       token->location,
11094                                       /*user_defined_p=*/true);
11095       return type_spec;
11096
11097     case RID_CONST:
11098       ds = ds_const;
11099       if (is_cv_qualifier)
11100         *is_cv_qualifier = true;
11101       break;
11102
11103     case RID_VOLATILE:
11104       ds = ds_volatile;
11105       if (is_cv_qualifier)
11106         *is_cv_qualifier = true;
11107       break;
11108
11109     case RID_RESTRICT:
11110       ds = ds_restrict;
11111       if (is_cv_qualifier)
11112         *is_cv_qualifier = true;
11113       break;
11114
11115     case RID_COMPLEX:
11116       /* The `__complex__' keyword is a GNU extension.  */
11117       ds = ds_complex;
11118       break;
11119
11120     default:
11121       break;
11122     }
11123
11124   /* Handle simple keywords.  */
11125   if (ds != ds_last)
11126     {
11127       if (decl_specs)
11128         {
11129           ++decl_specs->specs[(int)ds];
11130           decl_specs->any_specifiers_p = true;
11131         }
11132       return cp_lexer_consume_token (parser->lexer)->u.value;
11133     }
11134
11135   /* If we do not already have a type-specifier, assume we are looking
11136      at a simple-type-specifier.  */
11137   type_spec = cp_parser_simple_type_specifier (parser,
11138                                                decl_specs,
11139                                                flags);
11140
11141   /* If we didn't find a type-specifier, and a type-specifier was not
11142      optional in this context, issue an error message.  */
11143   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11144     {
11145       cp_parser_error (parser, "expected type specifier");
11146       return error_mark_node;
11147     }
11148
11149   return type_spec;
11150 }
11151
11152 /* Parse a simple-type-specifier.
11153
11154    simple-type-specifier:
11155      :: [opt] nested-name-specifier [opt] type-name
11156      :: [opt] nested-name-specifier template template-id
11157      char
11158      wchar_t
11159      bool
11160      short
11161      int
11162      long
11163      signed
11164      unsigned
11165      float
11166      double
11167      void
11168
11169    C++0x Extension:
11170
11171    simple-type-specifier:
11172      auto
11173      decltype ( expression )   
11174      char16_t
11175      char32_t
11176
11177    GNU Extension:
11178
11179    simple-type-specifier:
11180      __typeof__ unary-expression
11181      __typeof__ ( type-id )
11182
11183    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11184    appropriately updated.  */
11185
11186 static tree
11187 cp_parser_simple_type_specifier (cp_parser* parser,
11188                                  cp_decl_specifier_seq *decl_specs,
11189                                  cp_parser_flags flags)
11190 {
11191   tree type = NULL_TREE;
11192   cp_token *token;
11193
11194   /* Peek at the next token.  */
11195   token = cp_lexer_peek_token (parser->lexer);
11196
11197   /* If we're looking at a keyword, things are easy.  */
11198   switch (token->keyword)
11199     {
11200     case RID_CHAR:
11201       if (decl_specs)
11202         decl_specs->explicit_char_p = true;
11203       type = char_type_node;
11204       break;
11205     case RID_CHAR16:
11206       type = char16_type_node;
11207       break;
11208     case RID_CHAR32:
11209       type = char32_type_node;
11210       break;
11211     case RID_WCHAR:
11212       type = wchar_type_node;
11213       break;
11214     case RID_BOOL:
11215       type = boolean_type_node;
11216       break;
11217     case RID_SHORT:
11218       if (decl_specs)
11219         ++decl_specs->specs[(int) ds_short];
11220       type = short_integer_type_node;
11221       break;
11222     case RID_INT:
11223       if (decl_specs)
11224         decl_specs->explicit_int_p = true;
11225       type = integer_type_node;
11226       break;
11227     case RID_LONG:
11228       if (decl_specs)
11229         ++decl_specs->specs[(int) ds_long];
11230       type = long_integer_type_node;
11231       break;
11232     case RID_SIGNED:
11233       if (decl_specs)
11234         ++decl_specs->specs[(int) ds_signed];
11235       type = integer_type_node;
11236       break;
11237     case RID_UNSIGNED:
11238       if (decl_specs)
11239         ++decl_specs->specs[(int) ds_unsigned];
11240       type = unsigned_type_node;
11241       break;
11242     case RID_FLOAT:
11243       type = float_type_node;
11244       break;
11245     case RID_DOUBLE:
11246       type = double_type_node;
11247       break;
11248     case RID_VOID:
11249       type = void_type_node;
11250       break;
11251       
11252     case RID_AUTO:
11253       maybe_warn_cpp0x ("C++0x auto");
11254       type = make_auto ();
11255       break;
11256
11257     case RID_DECLTYPE:
11258       /* Parse the `decltype' type.  */
11259       type = cp_parser_decltype (parser);
11260
11261       if (decl_specs)
11262         cp_parser_set_decl_spec_type (decl_specs, type,
11263                                       token->location,
11264                                       /*user_defined_p=*/true);
11265
11266       return type;
11267
11268     case RID_TYPEOF:
11269       /* Consume the `typeof' token.  */
11270       cp_lexer_consume_token (parser->lexer);
11271       /* Parse the operand to `typeof'.  */
11272       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11273       /* If it is not already a TYPE, take its type.  */
11274       if (!TYPE_P (type))
11275         type = finish_typeof (type);
11276
11277       if (decl_specs)
11278         cp_parser_set_decl_spec_type (decl_specs, type,
11279                                       token->location,
11280                                       /*user_defined_p=*/true);
11281
11282       return type;
11283
11284     default:
11285       break;
11286     }
11287
11288   /* If the type-specifier was for a built-in type, we're done.  */
11289   if (type)
11290     {
11291       tree id;
11292
11293       /* Record the type.  */
11294       if (decl_specs
11295           && (token->keyword != RID_SIGNED
11296               && token->keyword != RID_UNSIGNED
11297               && token->keyword != RID_SHORT
11298               && token->keyword != RID_LONG))
11299         cp_parser_set_decl_spec_type (decl_specs,
11300                                       type,
11301                                       token->location,
11302                                       /*user_defined=*/false);
11303       if (decl_specs)
11304         decl_specs->any_specifiers_p = true;
11305
11306       /* Consume the token.  */
11307       id = cp_lexer_consume_token (parser->lexer)->u.value;
11308
11309       /* There is no valid C++ program where a non-template type is
11310          followed by a "<".  That usually indicates that the user thought
11311          that the type was a template.  */
11312       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11313
11314       return TYPE_NAME (type);
11315     }
11316
11317   /* The type-specifier must be a user-defined type.  */
11318   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11319     {
11320       bool qualified_p;
11321       bool global_p;
11322
11323       /* Don't gobble tokens or issue error messages if this is an
11324          optional type-specifier.  */
11325       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11326         cp_parser_parse_tentatively (parser);
11327
11328       /* Look for the optional `::' operator.  */
11329       global_p
11330         = (cp_parser_global_scope_opt (parser,
11331                                        /*current_scope_valid_p=*/false)
11332            != NULL_TREE);
11333       /* Look for the nested-name specifier.  */
11334       qualified_p
11335         = (cp_parser_nested_name_specifier_opt (parser,
11336                                                 /*typename_keyword_p=*/false,
11337                                                 /*check_dependency_p=*/true,
11338                                                 /*type_p=*/false,
11339                                                 /*is_declaration=*/false)
11340            != NULL_TREE);
11341       token = cp_lexer_peek_token (parser->lexer);
11342       /* If we have seen a nested-name-specifier, and the next token
11343          is `template', then we are using the template-id production.  */
11344       if (parser->scope
11345           && cp_parser_optional_template_keyword (parser))
11346         {
11347           /* Look for the template-id.  */
11348           type = cp_parser_template_id (parser,
11349                                         /*template_keyword_p=*/true,
11350                                         /*check_dependency_p=*/true,
11351                                         /*is_declaration=*/false);
11352           /* If the template-id did not name a type, we are out of
11353              luck.  */
11354           if (TREE_CODE (type) != TYPE_DECL)
11355             {
11356               cp_parser_error (parser, "expected template-id for type");
11357               type = NULL_TREE;
11358             }
11359         }
11360       /* Otherwise, look for a type-name.  */
11361       else
11362         type = cp_parser_type_name (parser);
11363       /* Keep track of all name-lookups performed in class scopes.  */
11364       if (type
11365           && !global_p
11366           && !qualified_p
11367           && TREE_CODE (type) == TYPE_DECL
11368           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11369         maybe_note_name_used_in_class (DECL_NAME (type), type);
11370       /* If it didn't work out, we don't have a TYPE.  */
11371       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11372           && !cp_parser_parse_definitely (parser))
11373         type = NULL_TREE;
11374       if (type && decl_specs)
11375         cp_parser_set_decl_spec_type (decl_specs, type,
11376                                       token->location,
11377                                       /*user_defined=*/true);
11378     }
11379
11380   /* If we didn't get a type-name, issue an error message.  */
11381   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11382     {
11383       cp_parser_error (parser, "expected type-name");
11384       return error_mark_node;
11385     }
11386
11387   /* There is no valid C++ program where a non-template type is
11388      followed by a "<".  That usually indicates that the user thought
11389      that the type was a template.  */
11390   if (type && type != error_mark_node)
11391     {
11392       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11393          If it is, then the '<'...'>' enclose protocol names rather than
11394          template arguments, and so everything is fine.  */
11395       if (c_dialect_objc ()
11396           && (objc_is_id (type) || objc_is_class_name (type)))
11397         {
11398           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11399           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11400
11401           /* Clobber the "unqualified" type previously entered into
11402              DECL_SPECS with the new, improved protocol-qualified version.  */
11403           if (decl_specs)
11404             decl_specs->type = qual_type;
11405
11406           return qual_type;
11407         }
11408
11409       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11410                                                token->location);
11411     }
11412
11413   return type;
11414 }
11415
11416 /* Parse a type-name.
11417
11418    type-name:
11419      class-name
11420      enum-name
11421      typedef-name
11422
11423    enum-name:
11424      identifier
11425
11426    typedef-name:
11427      identifier
11428
11429    Returns a TYPE_DECL for the type.  */
11430
11431 static tree
11432 cp_parser_type_name (cp_parser* parser)
11433 {
11434   tree type_decl;
11435
11436   /* We can't know yet whether it is a class-name or not.  */
11437   cp_parser_parse_tentatively (parser);
11438   /* Try a class-name.  */
11439   type_decl = cp_parser_class_name (parser,
11440                                     /*typename_keyword_p=*/false,
11441                                     /*template_keyword_p=*/false,
11442                                     none_type,
11443                                     /*check_dependency_p=*/true,
11444                                     /*class_head_p=*/false,
11445                                     /*is_declaration=*/false);
11446   /* If it's not a class-name, keep looking.  */
11447   if (!cp_parser_parse_definitely (parser))
11448     {
11449       /* It must be a typedef-name or an enum-name.  */
11450       return cp_parser_nonclass_name (parser);
11451     }
11452
11453   return type_decl;
11454 }
11455
11456 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11457
11458    enum-name:
11459      identifier
11460
11461    typedef-name:
11462      identifier
11463
11464    Returns a TYPE_DECL for the type.  */
11465
11466 static tree
11467 cp_parser_nonclass_name (cp_parser* parser)
11468 {
11469   tree type_decl;
11470   tree identifier;
11471
11472   cp_token *token = cp_lexer_peek_token (parser->lexer);
11473   identifier = cp_parser_identifier (parser);
11474   if (identifier == error_mark_node)
11475     return error_mark_node;
11476
11477   /* Look up the type-name.  */
11478   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11479
11480   if (TREE_CODE (type_decl) != TYPE_DECL
11481       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11482     {
11483       /* See if this is an Objective-C type.  */
11484       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11485       tree type = objc_get_protocol_qualified_type (identifier, protos);
11486       if (type)
11487         type_decl = TYPE_NAME (type);
11488     }
11489   
11490   /* Issue an error if we did not find a type-name.  */
11491   if (TREE_CODE (type_decl) != TYPE_DECL)
11492     {
11493       if (!cp_parser_simulate_error (parser))
11494         cp_parser_name_lookup_error (parser, identifier, type_decl,
11495                                      "is not a type", token->location);
11496       return error_mark_node;
11497     }
11498   /* Remember that the name was used in the definition of the
11499      current class so that we can check later to see if the
11500      meaning would have been different after the class was
11501      entirely defined.  */
11502   else if (type_decl != error_mark_node
11503            && !parser->scope)
11504     maybe_note_name_used_in_class (identifier, type_decl);
11505   
11506   return type_decl;
11507 }
11508
11509 /* Parse an elaborated-type-specifier.  Note that the grammar given
11510    here incorporates the resolution to DR68.
11511
11512    elaborated-type-specifier:
11513      class-key :: [opt] nested-name-specifier [opt] identifier
11514      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11515      enum-key :: [opt] nested-name-specifier [opt] identifier
11516      typename :: [opt] nested-name-specifier identifier
11517      typename :: [opt] nested-name-specifier template [opt]
11518        template-id
11519
11520    GNU extension:
11521
11522    elaborated-type-specifier:
11523      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11524      class-key attributes :: [opt] nested-name-specifier [opt]
11525                template [opt] template-id
11526      enum attributes :: [opt] nested-name-specifier [opt] identifier
11527
11528    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11529    declared `friend'.  If IS_DECLARATION is TRUE, then this
11530    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11531    something is being declared.
11532
11533    Returns the TYPE specified.  */
11534
11535 static tree
11536 cp_parser_elaborated_type_specifier (cp_parser* parser,
11537                                      bool is_friend,
11538                                      bool is_declaration)
11539 {
11540   enum tag_types tag_type;
11541   tree identifier;
11542   tree type = NULL_TREE;
11543   tree attributes = NULL_TREE;
11544   cp_token *token = NULL;
11545
11546   /* See if we're looking at the `enum' keyword.  */
11547   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11548     {
11549       /* Consume the `enum' token.  */
11550       cp_lexer_consume_token (parser->lexer);
11551       /* Remember that it's an enumeration type.  */
11552       tag_type = enum_type;
11553       /* Parse the optional `struct' or `class' key (for C++0x scoped
11554          enums).  */
11555       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11556           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11557         {
11558           if (cxx_dialect == cxx98)
11559             maybe_warn_cpp0x ("scoped enums");
11560
11561           /* Consume the `struct' or `class'.  */
11562           cp_lexer_consume_token (parser->lexer);
11563         }
11564       /* Parse the attributes.  */
11565       attributes = cp_parser_attributes_opt (parser);
11566     }
11567   /* Or, it might be `typename'.  */
11568   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11569                                            RID_TYPENAME))
11570     {
11571       /* Consume the `typename' token.  */
11572       cp_lexer_consume_token (parser->lexer);
11573       /* Remember that it's a `typename' type.  */
11574       tag_type = typename_type;
11575       /* The `typename' keyword is only allowed in templates.  */
11576       if (!processing_template_decl)
11577         permerror (input_location, "using %<typename%> outside of template");
11578     }
11579   /* Otherwise it must be a class-key.  */
11580   else
11581     {
11582       tag_type = cp_parser_class_key (parser);
11583       if (tag_type == none_type)
11584         return error_mark_node;
11585       /* Parse the attributes.  */
11586       attributes = cp_parser_attributes_opt (parser);
11587     }
11588
11589   /* Look for the `::' operator.  */
11590   cp_parser_global_scope_opt (parser,
11591                               /*current_scope_valid_p=*/false);
11592   /* Look for the nested-name-specifier.  */
11593   if (tag_type == typename_type)
11594     {
11595       if (!cp_parser_nested_name_specifier (parser,
11596                                            /*typename_keyword_p=*/true,
11597                                            /*check_dependency_p=*/true,
11598                                            /*type_p=*/true,
11599                                             is_declaration))
11600         return error_mark_node;
11601     }
11602   else
11603     /* Even though `typename' is not present, the proposed resolution
11604        to Core Issue 180 says that in `class A<T>::B', `B' should be
11605        considered a type-name, even if `A<T>' is dependent.  */
11606     cp_parser_nested_name_specifier_opt (parser,
11607                                          /*typename_keyword_p=*/true,
11608                                          /*check_dependency_p=*/true,
11609                                          /*type_p=*/true,
11610                                          is_declaration);
11611  /* For everything but enumeration types, consider a template-id.
11612     For an enumeration type, consider only a plain identifier.  */
11613   if (tag_type != enum_type)
11614     {
11615       bool template_p = false;
11616       tree decl;
11617
11618       /* Allow the `template' keyword.  */
11619       template_p = cp_parser_optional_template_keyword (parser);
11620       /* If we didn't see `template', we don't know if there's a
11621          template-id or not.  */
11622       if (!template_p)
11623         cp_parser_parse_tentatively (parser);
11624       /* Parse the template-id.  */
11625       token = cp_lexer_peek_token (parser->lexer);
11626       decl = cp_parser_template_id (parser, template_p,
11627                                     /*check_dependency_p=*/true,
11628                                     is_declaration);
11629       /* If we didn't find a template-id, look for an ordinary
11630          identifier.  */
11631       if (!template_p && !cp_parser_parse_definitely (parser))
11632         ;
11633       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11634          in effect, then we must assume that, upon instantiation, the
11635          template will correspond to a class.  */
11636       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11637                && tag_type == typename_type)
11638         type = make_typename_type (parser->scope, decl,
11639                                    typename_type,
11640                                    /*complain=*/tf_error);
11641       /* If the `typename' keyword is in effect and DECL is not a type
11642          decl. Then type is non existant.   */
11643       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11644         type = NULL_TREE; 
11645       else 
11646         type = TREE_TYPE (decl);
11647     }
11648
11649   if (!type)
11650     {
11651       token = cp_lexer_peek_token (parser->lexer);
11652       identifier = cp_parser_identifier (parser);
11653
11654       if (identifier == error_mark_node)
11655         {
11656           parser->scope = NULL_TREE;
11657           return error_mark_node;
11658         }
11659
11660       /* For a `typename', we needn't call xref_tag.  */
11661       if (tag_type == typename_type
11662           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11663         return cp_parser_make_typename_type (parser, parser->scope,
11664                                              identifier,
11665                                              token->location);
11666       /* Look up a qualified name in the usual way.  */
11667       if (parser->scope)
11668         {
11669           tree decl;
11670           tree ambiguous_decls;
11671
11672           decl = cp_parser_lookup_name (parser, identifier,
11673                                         tag_type,
11674                                         /*is_template=*/false,
11675                                         /*is_namespace=*/false,
11676                                         /*check_dependency=*/true,
11677                                         &ambiguous_decls,
11678                                         token->location);
11679
11680           /* If the lookup was ambiguous, an error will already have been
11681              issued.  */
11682           if (ambiguous_decls)
11683             return error_mark_node;
11684
11685           /* If we are parsing friend declaration, DECL may be a
11686              TEMPLATE_DECL tree node here.  However, we need to check
11687              whether this TEMPLATE_DECL results in valid code.  Consider
11688              the following example:
11689
11690                namespace N {
11691                  template <class T> class C {};
11692                }
11693                class X {
11694                  template <class T> friend class N::C; // #1, valid code
11695                };
11696                template <class T> class Y {
11697                  friend class N::C;                    // #2, invalid code
11698                };
11699
11700              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11701              name lookup of `N::C'.  We see that friend declaration must
11702              be template for the code to be valid.  Note that
11703              processing_template_decl does not work here since it is
11704              always 1 for the above two cases.  */
11705
11706           decl = (cp_parser_maybe_treat_template_as_class
11707                   (decl, /*tag_name_p=*/is_friend
11708                          && parser->num_template_parameter_lists));
11709
11710           if (TREE_CODE (decl) != TYPE_DECL)
11711             {
11712               cp_parser_diagnose_invalid_type_name (parser,
11713                                                     parser->scope,
11714                                                     identifier,
11715                                                     token->location);
11716               return error_mark_node;
11717             }
11718
11719           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11720             {
11721               bool allow_template = (parser->num_template_parameter_lists
11722                                       || DECL_SELF_REFERENCE_P (decl));
11723               type = check_elaborated_type_specifier (tag_type, decl, 
11724                                                       allow_template);
11725
11726               if (type == error_mark_node)
11727                 return error_mark_node;
11728             }
11729
11730           /* Forward declarations of nested types, such as
11731
11732                class C1::C2;
11733                class C1::C2::C3;
11734
11735              are invalid unless all components preceding the final '::'
11736              are complete.  If all enclosing types are complete, these
11737              declarations become merely pointless.
11738
11739              Invalid forward declarations of nested types are errors
11740              caught elsewhere in parsing.  Those that are pointless arrive
11741              here.  */
11742
11743           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11744               && !is_friend && !processing_explicit_instantiation)
11745             warning (0, "declaration %qD does not declare anything", decl);
11746
11747           type = TREE_TYPE (decl);
11748         }
11749       else
11750         {
11751           /* An elaborated-type-specifier sometimes introduces a new type and
11752              sometimes names an existing type.  Normally, the rule is that it
11753              introduces a new type only if there is not an existing type of
11754              the same name already in scope.  For example, given:
11755
11756                struct S {};
11757                void f() { struct S s; }
11758
11759              the `struct S' in the body of `f' is the same `struct S' as in
11760              the global scope; the existing definition is used.  However, if
11761              there were no global declaration, this would introduce a new
11762              local class named `S'.
11763
11764              An exception to this rule applies to the following code:
11765
11766                namespace N { struct S; }
11767
11768              Here, the elaborated-type-specifier names a new type
11769              unconditionally; even if there is already an `S' in the
11770              containing scope this declaration names a new type.
11771              This exception only applies if the elaborated-type-specifier
11772              forms the complete declaration:
11773
11774                [class.name]
11775
11776                A declaration consisting solely of `class-key identifier ;' is
11777                either a redeclaration of the name in the current scope or a
11778                forward declaration of the identifier as a class name.  It
11779                introduces the name into the current scope.
11780
11781              We are in this situation precisely when the next token is a `;'.
11782
11783              An exception to the exception is that a `friend' declaration does
11784              *not* name a new type; i.e., given:
11785
11786                struct S { friend struct T; };
11787
11788              `T' is not a new type in the scope of `S'.
11789
11790              Also, `new struct S' or `sizeof (struct S)' never results in the
11791              definition of a new type; a new type can only be declared in a
11792              declaration context.  */
11793
11794           tag_scope ts;
11795           bool template_p;
11796
11797           if (is_friend)
11798             /* Friends have special name lookup rules.  */
11799             ts = ts_within_enclosing_non_class;
11800           else if (is_declaration
11801                    && cp_lexer_next_token_is (parser->lexer,
11802                                               CPP_SEMICOLON))
11803             /* This is a `class-key identifier ;' */
11804             ts = ts_current;
11805           else
11806             ts = ts_global;
11807
11808           template_p =
11809             (parser->num_template_parameter_lists
11810              && (cp_parser_next_token_starts_class_definition_p (parser)
11811                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11812           /* An unqualified name was used to reference this type, so
11813              there were no qualifying templates.  */
11814           if (!cp_parser_check_template_parameters (parser,
11815                                                     /*num_templates=*/0,
11816                                                     token->location,
11817                                                     /*declarator=*/NULL))
11818             return error_mark_node;
11819           type = xref_tag (tag_type, identifier, ts, template_p);
11820         }
11821     }
11822
11823   if (type == error_mark_node)
11824     return error_mark_node;
11825
11826   /* Allow attributes on forward declarations of classes.  */
11827   if (attributes)
11828     {
11829       if (TREE_CODE (type) == TYPENAME_TYPE)
11830         warning (OPT_Wattributes,
11831                  "attributes ignored on uninstantiated type");
11832       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11833                && ! processing_explicit_instantiation)
11834         warning (OPT_Wattributes,
11835                  "attributes ignored on template instantiation");
11836       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11837         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11838       else
11839         warning (OPT_Wattributes,
11840                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11841     }
11842
11843   if (tag_type != enum_type)
11844     cp_parser_check_class_key (tag_type, type);
11845
11846   /* A "<" cannot follow an elaborated type specifier.  If that
11847      happens, the user was probably trying to form a template-id.  */
11848   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11849
11850   return type;
11851 }
11852
11853 /* Parse an enum-specifier.
11854
11855    enum-specifier:
11856      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11857
11858    enum-key:
11859      enum
11860      enum class   [C++0x]
11861      enum struct  [C++0x]
11862
11863    enum-base:   [C++0x]
11864      : type-specifier-seq
11865
11866    GNU Extensions:
11867      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11868        { enumerator-list [opt] }attributes[opt]
11869
11870    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11871    if the token stream isn't an enum-specifier after all.  */
11872
11873 static tree
11874 cp_parser_enum_specifier (cp_parser* parser)
11875 {
11876   tree identifier;
11877   tree type;
11878   tree attributes;
11879   bool scoped_enum_p = false;
11880   bool has_underlying_type = false;
11881   tree underlying_type = NULL_TREE;
11882
11883   /* Parse tentatively so that we can back up if we don't find a
11884      enum-specifier.  */
11885   cp_parser_parse_tentatively (parser);
11886
11887   /* Caller guarantees that the current token is 'enum', an identifier
11888      possibly follows, and the token after that is an opening brace.
11889      If we don't have an identifier, fabricate an anonymous name for
11890      the enumeration being defined.  */
11891   cp_lexer_consume_token (parser->lexer);
11892
11893   /* Parse the "class" or "struct", which indicates a scoped
11894      enumeration type in C++0x.  */
11895   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11896       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11897     {
11898       if (cxx_dialect == cxx98)
11899         maybe_warn_cpp0x ("scoped enums");
11900
11901       /* Consume the `struct' or `class' token.  */
11902       cp_lexer_consume_token (parser->lexer);
11903
11904       scoped_enum_p = true;
11905     }
11906
11907   attributes = cp_parser_attributes_opt (parser);
11908
11909   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11910     identifier = cp_parser_identifier (parser);
11911   else
11912     identifier = make_anon_name ();
11913
11914   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11915   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11916     {
11917       cp_decl_specifier_seq type_specifiers;
11918
11919       /* At this point this is surely not elaborated type specifier.  */
11920       if (!cp_parser_parse_definitely (parser))
11921         return NULL_TREE;
11922
11923       if (cxx_dialect == cxx98)
11924         maybe_warn_cpp0x ("scoped enums");
11925
11926       /* Consume the `:'.  */
11927       cp_lexer_consume_token (parser->lexer);
11928
11929       has_underlying_type = true;
11930
11931       /* Parse the type-specifier-seq.  */
11932       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11933                                     &type_specifiers);
11934
11935       /* If that didn't work, stop.  */
11936       if (type_specifiers.type != error_mark_node)
11937         {
11938           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11939                                             /*initialized=*/0, NULL);
11940           if (underlying_type == error_mark_node)
11941             underlying_type = NULL_TREE;
11942         }
11943     }
11944
11945   /* Look for the `{' but don't consume it yet.  */
11946   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11947     {
11948       cp_parser_error (parser, "expected %<{%>");
11949       if (has_underlying_type)
11950         return NULL_TREE;
11951     }
11952
11953   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11954     return NULL_TREE;
11955
11956   /* Issue an error message if type-definitions are forbidden here.  */
11957   if (!cp_parser_check_type_definition (parser))
11958     type = error_mark_node;
11959   else
11960     /* Create the new type.  We do this before consuming the opening
11961        brace so the enum will be recorded as being on the line of its
11962        tag (or the 'enum' keyword, if there is no tag).  */
11963     type = start_enum (identifier, underlying_type, scoped_enum_p);
11964   
11965   /* Consume the opening brace.  */
11966   cp_lexer_consume_token (parser->lexer);
11967
11968   if (type == error_mark_node)
11969     {
11970       cp_parser_skip_to_end_of_block_or_statement (parser);
11971       return error_mark_node;
11972     }
11973
11974   /* If the next token is not '}', then there are some enumerators.  */
11975   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11976     cp_parser_enumerator_list (parser, type);
11977
11978   /* Consume the final '}'.  */
11979   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11980
11981   /* Look for trailing attributes to apply to this enumeration, and
11982      apply them if appropriate.  */
11983   if (cp_parser_allow_gnu_extensions_p (parser))
11984     {
11985       tree trailing_attr = cp_parser_attributes_opt (parser);
11986       trailing_attr = chainon (trailing_attr, attributes);
11987       cplus_decl_attributes (&type,
11988                              trailing_attr,
11989                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11990     }
11991
11992   /* Finish up the enumeration.  */
11993   finish_enum (type);
11994
11995   return type;
11996 }
11997
11998 /* Parse an enumerator-list.  The enumerators all have the indicated
11999    TYPE.
12000
12001    enumerator-list:
12002      enumerator-definition
12003      enumerator-list , enumerator-definition  */
12004
12005 static void
12006 cp_parser_enumerator_list (cp_parser* parser, tree type)
12007 {
12008   while (true)
12009     {
12010       /* Parse an enumerator-definition.  */
12011       cp_parser_enumerator_definition (parser, type);
12012
12013       /* If the next token is not a ',', we've reached the end of
12014          the list.  */
12015       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12016         break;
12017       /* Otherwise, consume the `,' and keep going.  */
12018       cp_lexer_consume_token (parser->lexer);
12019       /* If the next token is a `}', there is a trailing comma.  */
12020       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12021         {
12022           if (!in_system_header)
12023             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12024           break;
12025         }
12026     }
12027 }
12028
12029 /* Parse an enumerator-definition.  The enumerator has the indicated
12030    TYPE.
12031
12032    enumerator-definition:
12033      enumerator
12034      enumerator = constant-expression
12035
12036    enumerator:
12037      identifier  */
12038
12039 static void
12040 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12041 {
12042   tree identifier;
12043   tree value;
12044
12045   /* Look for the identifier.  */
12046   identifier = cp_parser_identifier (parser);
12047   if (identifier == error_mark_node)
12048     return;
12049
12050   /* If the next token is an '=', then there is an explicit value.  */
12051   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12052     {
12053       /* Consume the `=' token.  */
12054       cp_lexer_consume_token (parser->lexer);
12055       /* Parse the value.  */
12056       value = cp_parser_constant_expression (parser,
12057                                              /*allow_non_constant_p=*/false,
12058                                              NULL);
12059     }
12060   else
12061     value = NULL_TREE;
12062
12063   /* If we are processing a template, make sure the initializer of the
12064      enumerator doesn't contain any bare template parameter pack.  */
12065   if (check_for_bare_parameter_packs (value))
12066     value = error_mark_node;
12067
12068   /* Create the enumerator.  */
12069   build_enumerator (identifier, value, type);
12070 }
12071
12072 /* Parse a namespace-name.
12073
12074    namespace-name:
12075      original-namespace-name
12076      namespace-alias
12077
12078    Returns the NAMESPACE_DECL for the namespace.  */
12079
12080 static tree
12081 cp_parser_namespace_name (cp_parser* parser)
12082 {
12083   tree identifier;
12084   tree namespace_decl;
12085
12086   cp_token *token = cp_lexer_peek_token (parser->lexer);
12087
12088   /* Get the name of the namespace.  */
12089   identifier = cp_parser_identifier (parser);
12090   if (identifier == error_mark_node)
12091     return error_mark_node;
12092
12093   /* Look up the identifier in the currently active scope.  Look only
12094      for namespaces, due to:
12095
12096        [basic.lookup.udir]
12097
12098        When looking up a namespace-name in a using-directive or alias
12099        definition, only namespace names are considered.
12100
12101      And:
12102
12103        [basic.lookup.qual]
12104
12105        During the lookup of a name preceding the :: scope resolution
12106        operator, object, function, and enumerator names are ignored.
12107
12108      (Note that cp_parser_qualifying_entity only calls this
12109      function if the token after the name is the scope resolution
12110      operator.)  */
12111   namespace_decl = cp_parser_lookup_name (parser, identifier,
12112                                           none_type,
12113                                           /*is_template=*/false,
12114                                           /*is_namespace=*/true,
12115                                           /*check_dependency=*/true,
12116                                           /*ambiguous_decls=*/NULL,
12117                                           token->location);
12118   /* If it's not a namespace, issue an error.  */
12119   if (namespace_decl == error_mark_node
12120       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12121     {
12122       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12123         error ("%H%qD is not a namespace-name", &token->location, identifier);
12124       cp_parser_error (parser, "expected namespace-name");
12125       namespace_decl = error_mark_node;
12126     }
12127
12128   return namespace_decl;
12129 }
12130
12131 /* Parse a namespace-definition.
12132
12133    namespace-definition:
12134      named-namespace-definition
12135      unnamed-namespace-definition
12136
12137    named-namespace-definition:
12138      original-namespace-definition
12139      extension-namespace-definition
12140
12141    original-namespace-definition:
12142      namespace identifier { namespace-body }
12143
12144    extension-namespace-definition:
12145      namespace original-namespace-name { namespace-body }
12146
12147    unnamed-namespace-definition:
12148      namespace { namespace-body } */
12149
12150 static void
12151 cp_parser_namespace_definition (cp_parser* parser)
12152 {
12153   tree identifier, attribs;
12154   bool has_visibility;
12155   bool is_inline;
12156
12157   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12158     {
12159       is_inline = true;
12160       cp_lexer_consume_token (parser->lexer);
12161     }
12162   else
12163     is_inline = false;
12164
12165   /* Look for the `namespace' keyword.  */
12166   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12167
12168   /* Get the name of the namespace.  We do not attempt to distinguish
12169      between an original-namespace-definition and an
12170      extension-namespace-definition at this point.  The semantic
12171      analysis routines are responsible for that.  */
12172   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12173     identifier = cp_parser_identifier (parser);
12174   else
12175     identifier = NULL_TREE;
12176
12177   /* Parse any specified attributes.  */
12178   attribs = cp_parser_attributes_opt (parser);
12179
12180   /* Look for the `{' to start the namespace.  */
12181   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12182   /* Start the namespace.  */
12183   push_namespace (identifier);
12184
12185   /* "inline namespace" is equivalent to a stub namespace definition
12186      followed by a strong using directive.  */
12187   if (is_inline)
12188     {
12189       tree name_space = current_namespace;
12190       /* Set up namespace association.  */
12191       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12192         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12193                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12194       /* Import the contents of the inline namespace.  */
12195       pop_namespace ();
12196       do_using_directive (name_space);
12197       push_namespace (identifier);
12198     }
12199
12200   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12201
12202   /* Parse the body of the namespace.  */
12203   cp_parser_namespace_body (parser);
12204
12205 #ifdef HANDLE_PRAGMA_VISIBILITY
12206   if (has_visibility)
12207     pop_visibility ();
12208 #endif
12209
12210   /* Finish the namespace.  */
12211   pop_namespace ();
12212   /* Look for the final `}'.  */
12213   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12214 }
12215
12216 /* Parse a namespace-body.
12217
12218    namespace-body:
12219      declaration-seq [opt]  */
12220
12221 static void
12222 cp_parser_namespace_body (cp_parser* parser)
12223 {
12224   cp_parser_declaration_seq_opt (parser);
12225 }
12226
12227 /* Parse a namespace-alias-definition.
12228
12229    namespace-alias-definition:
12230      namespace identifier = qualified-namespace-specifier ;  */
12231
12232 static void
12233 cp_parser_namespace_alias_definition (cp_parser* parser)
12234 {
12235   tree identifier;
12236   tree namespace_specifier;
12237
12238   cp_token *token = cp_lexer_peek_token (parser->lexer);
12239
12240   /* Look for the `namespace' keyword.  */
12241   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12242   /* Look for the identifier.  */
12243   identifier = cp_parser_identifier (parser);
12244   if (identifier == error_mark_node)
12245     return;
12246   /* Look for the `=' token.  */
12247   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12248       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12249     {
12250       error ("%H%<namespace%> definition is not allowed here", &token->location);
12251       /* Skip the definition.  */
12252       cp_lexer_consume_token (parser->lexer);
12253       if (cp_parser_skip_to_closing_brace (parser))
12254         cp_lexer_consume_token (parser->lexer);
12255       return;
12256     }
12257   cp_parser_require (parser, CPP_EQ, "%<=%>");
12258   /* Look for the qualified-namespace-specifier.  */
12259   namespace_specifier
12260     = cp_parser_qualified_namespace_specifier (parser);
12261   /* Look for the `;' token.  */
12262   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12263
12264   /* Register the alias in the symbol table.  */
12265   do_namespace_alias (identifier, namespace_specifier);
12266 }
12267
12268 /* Parse a qualified-namespace-specifier.
12269
12270    qualified-namespace-specifier:
12271      :: [opt] nested-name-specifier [opt] namespace-name
12272
12273    Returns a NAMESPACE_DECL corresponding to the specified
12274    namespace.  */
12275
12276 static tree
12277 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12278 {
12279   /* Look for the optional `::'.  */
12280   cp_parser_global_scope_opt (parser,
12281                               /*current_scope_valid_p=*/false);
12282
12283   /* Look for the optional nested-name-specifier.  */
12284   cp_parser_nested_name_specifier_opt (parser,
12285                                        /*typename_keyword_p=*/false,
12286                                        /*check_dependency_p=*/true,
12287                                        /*type_p=*/false,
12288                                        /*is_declaration=*/true);
12289
12290   return cp_parser_namespace_name (parser);
12291 }
12292
12293 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12294    access declaration.
12295
12296    using-declaration:
12297      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12298      using :: unqualified-id ;  
12299
12300    access-declaration:
12301      qualified-id ;  
12302
12303    */
12304
12305 static bool
12306 cp_parser_using_declaration (cp_parser* parser, 
12307                              bool access_declaration_p)
12308 {
12309   cp_token *token;
12310   bool typename_p = false;
12311   bool global_scope_p;
12312   tree decl;
12313   tree identifier;
12314   tree qscope;
12315
12316   if (access_declaration_p)
12317     cp_parser_parse_tentatively (parser);
12318   else
12319     {
12320       /* Look for the `using' keyword.  */
12321       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12322       
12323       /* Peek at the next token.  */
12324       token = cp_lexer_peek_token (parser->lexer);
12325       /* See if it's `typename'.  */
12326       if (token->keyword == RID_TYPENAME)
12327         {
12328           /* Remember that we've seen it.  */
12329           typename_p = true;
12330           /* Consume the `typename' token.  */
12331           cp_lexer_consume_token (parser->lexer);
12332         }
12333     }
12334
12335   /* Look for the optional global scope qualification.  */
12336   global_scope_p
12337     = (cp_parser_global_scope_opt (parser,
12338                                    /*current_scope_valid_p=*/false)
12339        != NULL_TREE);
12340
12341   /* If we saw `typename', or didn't see `::', then there must be a
12342      nested-name-specifier present.  */
12343   if (typename_p || !global_scope_p)
12344     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12345                                               /*check_dependency_p=*/true,
12346                                               /*type_p=*/false,
12347                                               /*is_declaration=*/true);
12348   /* Otherwise, we could be in either of the two productions.  In that
12349      case, treat the nested-name-specifier as optional.  */
12350   else
12351     qscope = cp_parser_nested_name_specifier_opt (parser,
12352                                                   /*typename_keyword_p=*/false,
12353                                                   /*check_dependency_p=*/true,
12354                                                   /*type_p=*/false,
12355                                                   /*is_declaration=*/true);
12356   if (!qscope)
12357     qscope = global_namespace;
12358
12359   if (access_declaration_p && cp_parser_error_occurred (parser))
12360     /* Something has already gone wrong; there's no need to parse
12361        further.  Since an error has occurred, the return value of
12362        cp_parser_parse_definitely will be false, as required.  */
12363     return cp_parser_parse_definitely (parser);
12364
12365   token = cp_lexer_peek_token (parser->lexer);
12366   /* Parse the unqualified-id.  */
12367   identifier = cp_parser_unqualified_id (parser,
12368                                          /*template_keyword_p=*/false,
12369                                          /*check_dependency_p=*/true,
12370                                          /*declarator_p=*/true,
12371                                          /*optional_p=*/false);
12372
12373   if (access_declaration_p)
12374     {
12375       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12376         cp_parser_simulate_error (parser);
12377       if (!cp_parser_parse_definitely (parser))
12378         return false;
12379     }
12380
12381   /* The function we call to handle a using-declaration is different
12382      depending on what scope we are in.  */
12383   if (qscope == error_mark_node || identifier == error_mark_node)
12384     ;
12385   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12386            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12387     /* [namespace.udecl]
12388
12389        A using declaration shall not name a template-id.  */
12390     error ("%Ha template-id may not appear in a using-declaration",
12391             &token->location);
12392   else
12393     {
12394       if (at_class_scope_p ())
12395         {
12396           /* Create the USING_DECL.  */
12397           decl = do_class_using_decl (parser->scope, identifier);
12398
12399           if (check_for_bare_parameter_packs (decl))
12400             return false;
12401           else
12402             /* Add it to the list of members in this class.  */
12403             finish_member_declaration (decl);
12404         }
12405       else
12406         {
12407           decl = cp_parser_lookup_name_simple (parser,
12408                                                identifier,
12409                                                token->location);
12410           if (decl == error_mark_node)
12411             cp_parser_name_lookup_error (parser, identifier,
12412                                          decl, NULL,
12413                                          token->location);
12414           else if (check_for_bare_parameter_packs (decl))
12415             return false;
12416           else if (!at_namespace_scope_p ())
12417             do_local_using_decl (decl, qscope, identifier);
12418           else
12419             do_toplevel_using_decl (decl, qscope, identifier);
12420         }
12421     }
12422
12423   /* Look for the final `;'.  */
12424   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12425   
12426   return true;
12427 }
12428
12429 /* Parse a using-directive.
12430
12431    using-directive:
12432      using namespace :: [opt] nested-name-specifier [opt]
12433        namespace-name ;  */
12434
12435 static void
12436 cp_parser_using_directive (cp_parser* parser)
12437 {
12438   tree namespace_decl;
12439   tree attribs;
12440
12441   /* Look for the `using' keyword.  */
12442   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12443   /* And the `namespace' keyword.  */
12444   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12445   /* Look for the optional `::' operator.  */
12446   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12447   /* And the optional nested-name-specifier.  */
12448   cp_parser_nested_name_specifier_opt (parser,
12449                                        /*typename_keyword_p=*/false,
12450                                        /*check_dependency_p=*/true,
12451                                        /*type_p=*/false,
12452                                        /*is_declaration=*/true);
12453   /* Get the namespace being used.  */
12454   namespace_decl = cp_parser_namespace_name (parser);
12455   /* And any specified attributes.  */
12456   attribs = cp_parser_attributes_opt (parser);
12457   /* Update the symbol table.  */
12458   parse_using_directive (namespace_decl, attribs);
12459   /* Look for the final `;'.  */
12460   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12461 }
12462
12463 /* Parse an asm-definition.
12464
12465    asm-definition:
12466      asm ( string-literal ) ;
12467
12468    GNU Extension:
12469
12470    asm-definition:
12471      asm volatile [opt] ( string-literal ) ;
12472      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12473      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12474                           : asm-operand-list [opt] ) ;
12475      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12476                           : asm-operand-list [opt]
12477                           : asm-operand-list [opt] ) ;  */
12478
12479 static void
12480 cp_parser_asm_definition (cp_parser* parser)
12481 {
12482   tree string;
12483   tree outputs = NULL_TREE;
12484   tree inputs = NULL_TREE;
12485   tree clobbers = NULL_TREE;
12486   tree asm_stmt;
12487   bool volatile_p = false;
12488   bool extended_p = false;
12489   bool invalid_inputs_p = false;
12490   bool invalid_outputs_p = false;
12491
12492   /* Look for the `asm' keyword.  */
12493   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12494   /* See if the next token is `volatile'.  */
12495   if (cp_parser_allow_gnu_extensions_p (parser)
12496       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12497     {
12498       /* Remember that we saw the `volatile' keyword.  */
12499       volatile_p = true;
12500       /* Consume the token.  */
12501       cp_lexer_consume_token (parser->lexer);
12502     }
12503   /* Look for the opening `('.  */
12504   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12505     return;
12506   /* Look for the string.  */
12507   string = cp_parser_string_literal (parser, false, false);
12508   if (string == error_mark_node)
12509     {
12510       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12511                                              /*consume_paren=*/true);
12512       return;
12513     }
12514
12515   /* If we're allowing GNU extensions, check for the extended assembly
12516      syntax.  Unfortunately, the `:' tokens need not be separated by
12517      a space in C, and so, for compatibility, we tolerate that here
12518      too.  Doing that means that we have to treat the `::' operator as
12519      two `:' tokens.  */
12520   if (cp_parser_allow_gnu_extensions_p (parser)
12521       && parser->in_function_body
12522       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12523           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12524     {
12525       bool inputs_p = false;
12526       bool clobbers_p = false;
12527
12528       /* The extended syntax was used.  */
12529       extended_p = true;
12530
12531       /* Look for outputs.  */
12532       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12533         {
12534           /* Consume the `:'.  */
12535           cp_lexer_consume_token (parser->lexer);
12536           /* Parse the output-operands.  */
12537           if (cp_lexer_next_token_is_not (parser->lexer,
12538                                           CPP_COLON)
12539               && cp_lexer_next_token_is_not (parser->lexer,
12540                                              CPP_SCOPE)
12541               && cp_lexer_next_token_is_not (parser->lexer,
12542                                              CPP_CLOSE_PAREN))
12543             outputs = cp_parser_asm_operand_list (parser);
12544
12545             if (outputs == error_mark_node)
12546               invalid_outputs_p = true;
12547         }
12548       /* If the next token is `::', there are no outputs, and the
12549          next token is the beginning of the inputs.  */
12550       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12551         /* The inputs are coming next.  */
12552         inputs_p = true;
12553
12554       /* Look for inputs.  */
12555       if (inputs_p
12556           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12557         {
12558           /* Consume the `:' or `::'.  */
12559           cp_lexer_consume_token (parser->lexer);
12560           /* Parse the output-operands.  */
12561           if (cp_lexer_next_token_is_not (parser->lexer,
12562                                           CPP_COLON)
12563               && cp_lexer_next_token_is_not (parser->lexer,
12564                                              CPP_CLOSE_PAREN))
12565             inputs = cp_parser_asm_operand_list (parser);
12566
12567             if (inputs == error_mark_node)
12568               invalid_inputs_p = true;
12569         }
12570       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12571         /* The clobbers are coming next.  */
12572         clobbers_p = true;
12573
12574       /* Look for clobbers.  */
12575       if (clobbers_p
12576           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12577         {
12578           /* Consume the `:' or `::'.  */
12579           cp_lexer_consume_token (parser->lexer);
12580           /* Parse the clobbers.  */
12581           if (cp_lexer_next_token_is_not (parser->lexer,
12582                                           CPP_CLOSE_PAREN))
12583             clobbers = cp_parser_asm_clobber_list (parser);
12584         }
12585     }
12586   /* Look for the closing `)'.  */
12587   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12588     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12589                                            /*consume_paren=*/true);
12590   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12591
12592   if (!invalid_inputs_p && !invalid_outputs_p)
12593     {
12594       /* Create the ASM_EXPR.  */
12595       if (parser->in_function_body)
12596         {
12597           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12598                                       inputs, clobbers);
12599           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12600           if (!extended_p)
12601             {
12602               tree temp = asm_stmt;
12603               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12604                 temp = TREE_OPERAND (temp, 0);
12605
12606               ASM_INPUT_P (temp) = 1;
12607             }
12608         }
12609       else
12610         cgraph_add_asm_node (string);
12611     }
12612 }
12613
12614 /* Declarators [gram.dcl.decl] */
12615
12616 /* Parse an init-declarator.
12617
12618    init-declarator:
12619      declarator initializer [opt]
12620
12621    GNU Extension:
12622
12623    init-declarator:
12624      declarator asm-specification [opt] attributes [opt] initializer [opt]
12625
12626    function-definition:
12627      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12628        function-body
12629      decl-specifier-seq [opt] declarator function-try-block
12630
12631    GNU Extension:
12632
12633    function-definition:
12634      __extension__ function-definition
12635
12636    The DECL_SPECIFIERS apply to this declarator.  Returns a
12637    representation of the entity declared.  If MEMBER_P is TRUE, then
12638    this declarator appears in a class scope.  The new DECL created by
12639    this declarator is returned.
12640
12641    The CHECKS are access checks that should be performed once we know
12642    what entity is being declared (and, therefore, what classes have
12643    befriended it).
12644
12645    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12646    for a function-definition here as well.  If the declarator is a
12647    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12648    be TRUE upon return.  By that point, the function-definition will
12649    have been completely parsed.
12650
12651    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12652    is FALSE.  */
12653
12654 static tree
12655 cp_parser_init_declarator (cp_parser* parser,
12656                            cp_decl_specifier_seq *decl_specifiers,
12657                            VEC (deferred_access_check,gc)* checks,
12658                            bool function_definition_allowed_p,
12659                            bool member_p,
12660                            int declares_class_or_enum,
12661                            bool* function_definition_p)
12662 {
12663   cp_token *token = NULL, *asm_spec_start_token = NULL,
12664            *attributes_start_token = NULL;
12665   cp_declarator *declarator;
12666   tree prefix_attributes;
12667   tree attributes;
12668   tree asm_specification;
12669   tree initializer;
12670   tree decl = NULL_TREE;
12671   tree scope;
12672   int is_initialized;
12673   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12674      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12675      "(...)".  */
12676   enum cpp_ttype initialization_kind;
12677   bool is_direct_init = false;
12678   bool is_non_constant_init;
12679   int ctor_dtor_or_conv_p;
12680   bool friend_p;
12681   tree pushed_scope = NULL;
12682
12683   /* Gather the attributes that were provided with the
12684      decl-specifiers.  */
12685   prefix_attributes = decl_specifiers->attributes;
12686
12687   /* Assume that this is not the declarator for a function
12688      definition.  */
12689   if (function_definition_p)
12690     *function_definition_p = false;
12691
12692   /* Defer access checks while parsing the declarator; we cannot know
12693      what names are accessible until we know what is being
12694      declared.  */
12695   resume_deferring_access_checks ();
12696
12697   /* Parse the declarator.  */
12698   token = cp_lexer_peek_token (parser->lexer);
12699   declarator
12700     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12701                             &ctor_dtor_or_conv_p,
12702                             /*parenthesized_p=*/NULL,
12703                             /*member_p=*/false);
12704   /* Gather up the deferred checks.  */
12705   stop_deferring_access_checks ();
12706
12707   /* If the DECLARATOR was erroneous, there's no need to go
12708      further.  */
12709   if (declarator == cp_error_declarator)
12710     return error_mark_node;
12711
12712   /* Check that the number of template-parameter-lists is OK.  */
12713   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12714                                                        token->location))
12715     return error_mark_node;
12716
12717   if (declares_class_or_enum & 2)
12718     cp_parser_check_for_definition_in_return_type (declarator,
12719                                                    decl_specifiers->type,
12720                                                    decl_specifiers->type_location);
12721
12722   /* Figure out what scope the entity declared by the DECLARATOR is
12723      located in.  `grokdeclarator' sometimes changes the scope, so
12724      we compute it now.  */
12725   scope = get_scope_of_declarator (declarator);
12726
12727   /* If we're allowing GNU extensions, look for an asm-specification
12728      and attributes.  */
12729   if (cp_parser_allow_gnu_extensions_p (parser))
12730     {
12731       /* Look for an asm-specification.  */
12732       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12733       asm_specification = cp_parser_asm_specification_opt (parser);
12734       /* And attributes.  */
12735       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12736       attributes = cp_parser_attributes_opt (parser);
12737     }
12738   else
12739     {
12740       asm_specification = NULL_TREE;
12741       attributes = NULL_TREE;
12742     }
12743
12744   /* Peek at the next token.  */
12745   token = cp_lexer_peek_token (parser->lexer);
12746   /* Check to see if the token indicates the start of a
12747      function-definition.  */
12748   if (function_declarator_p (declarator)
12749       && cp_parser_token_starts_function_definition_p (token))
12750     {
12751       if (!function_definition_allowed_p)
12752         {
12753           /* If a function-definition should not appear here, issue an
12754              error message.  */
12755           cp_parser_error (parser,
12756                            "a function-definition is not allowed here");
12757           return error_mark_node;
12758         }
12759       else
12760         {
12761           location_t func_brace_location
12762             = cp_lexer_peek_token (parser->lexer)->location;
12763
12764           /* Neither attributes nor an asm-specification are allowed
12765              on a function-definition.  */
12766           if (asm_specification)
12767             error ("%Han asm-specification is not allowed "
12768                    "on a function-definition",
12769                    &asm_spec_start_token->location);
12770           if (attributes)
12771             error ("%Hattributes are not allowed on a function-definition",
12772                    &attributes_start_token->location);
12773           /* This is a function-definition.  */
12774           *function_definition_p = true;
12775
12776           /* Parse the function definition.  */
12777           if (member_p)
12778             decl = cp_parser_save_member_function_body (parser,
12779                                                         decl_specifiers,
12780                                                         declarator,
12781                                                         prefix_attributes);
12782           else
12783             decl
12784               = (cp_parser_function_definition_from_specifiers_and_declarator
12785                  (parser, decl_specifiers, prefix_attributes, declarator));
12786
12787           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12788             {
12789               /* This is where the prologue starts...  */
12790               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12791                 = func_brace_location;
12792             }
12793
12794           return decl;
12795         }
12796     }
12797
12798   /* [dcl.dcl]
12799
12800      Only in function declarations for constructors, destructors, and
12801      type conversions can the decl-specifier-seq be omitted.
12802
12803      We explicitly postpone this check past the point where we handle
12804      function-definitions because we tolerate function-definitions
12805      that are missing their return types in some modes.  */
12806   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12807     {
12808       cp_parser_error (parser,
12809                        "expected constructor, destructor, or type conversion");
12810       return error_mark_node;
12811     }
12812
12813   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12814   if (token->type == CPP_EQ
12815       || token->type == CPP_OPEN_PAREN
12816       || token->type == CPP_OPEN_BRACE)
12817     {
12818       is_initialized = SD_INITIALIZED;
12819       initialization_kind = token->type;
12820
12821       if (token->type == CPP_EQ
12822           && function_declarator_p (declarator))
12823         {
12824           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12825           if (t2->keyword == RID_DEFAULT)
12826             is_initialized = SD_DEFAULTED;
12827           else if (t2->keyword == RID_DELETE)
12828             is_initialized = SD_DELETED;
12829         }
12830     }
12831   else
12832     {
12833       /* If the init-declarator isn't initialized and isn't followed by a
12834          `,' or `;', it's not a valid init-declarator.  */
12835       if (token->type != CPP_COMMA
12836           && token->type != CPP_SEMICOLON)
12837         {
12838           cp_parser_error (parser, "expected initializer");
12839           return error_mark_node;
12840         }
12841       is_initialized = SD_UNINITIALIZED;
12842       initialization_kind = CPP_EOF;
12843     }
12844
12845   /* Because start_decl has side-effects, we should only call it if we
12846      know we're going ahead.  By this point, we know that we cannot
12847      possibly be looking at any other construct.  */
12848   cp_parser_commit_to_tentative_parse (parser);
12849
12850   /* If the decl specifiers were bad, issue an error now that we're
12851      sure this was intended to be a declarator.  Then continue
12852      declaring the variable(s), as int, to try to cut down on further
12853      errors.  */
12854   if (decl_specifiers->any_specifiers_p
12855       && decl_specifiers->type == error_mark_node)
12856     {
12857       cp_parser_error (parser, "invalid type in declaration");
12858       decl_specifiers->type = integer_type_node;
12859     }
12860
12861   /* Check to see whether or not this declaration is a friend.  */
12862   friend_p = cp_parser_friend_p (decl_specifiers);
12863
12864   /* Enter the newly declared entry in the symbol table.  If we're
12865      processing a declaration in a class-specifier, we wait until
12866      after processing the initializer.  */
12867   if (!member_p)
12868     {
12869       if (parser->in_unbraced_linkage_specification_p)
12870         decl_specifiers->storage_class = sc_extern;
12871       decl = start_decl (declarator, decl_specifiers,
12872                          is_initialized, attributes, prefix_attributes,
12873                          &pushed_scope);
12874     }
12875   else if (scope)
12876     /* Enter the SCOPE.  That way unqualified names appearing in the
12877        initializer will be looked up in SCOPE.  */
12878     pushed_scope = push_scope (scope);
12879
12880   /* Perform deferred access control checks, now that we know in which
12881      SCOPE the declared entity resides.  */
12882   if (!member_p && decl)
12883     {
12884       tree saved_current_function_decl = NULL_TREE;
12885
12886       /* If the entity being declared is a function, pretend that we
12887          are in its scope.  If it is a `friend', it may have access to
12888          things that would not otherwise be accessible.  */
12889       if (TREE_CODE (decl) == FUNCTION_DECL)
12890         {
12891           saved_current_function_decl = current_function_decl;
12892           current_function_decl = decl;
12893         }
12894
12895       /* Perform access checks for template parameters.  */
12896       cp_parser_perform_template_parameter_access_checks (checks);
12897
12898       /* Perform the access control checks for the declarator and the
12899          decl-specifiers.  */
12900       perform_deferred_access_checks ();
12901
12902       /* Restore the saved value.  */
12903       if (TREE_CODE (decl) == FUNCTION_DECL)
12904         current_function_decl = saved_current_function_decl;
12905     }
12906
12907   /* Parse the initializer.  */
12908   initializer = NULL_TREE;
12909   is_direct_init = false;
12910   is_non_constant_init = true;
12911   if (is_initialized)
12912     {
12913       if (function_declarator_p (declarator))
12914         {
12915           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12916            if (initialization_kind == CPP_EQ)
12917              initializer = cp_parser_pure_specifier (parser);
12918            else
12919              {
12920                /* If the declaration was erroneous, we don't really
12921                   know what the user intended, so just silently
12922                   consume the initializer.  */
12923                if (decl != error_mark_node)
12924                  error ("%Hinitializer provided for function",
12925                         &initializer_start_token->location);
12926                cp_parser_skip_to_closing_parenthesis (parser,
12927                                                       /*recovering=*/true,
12928                                                       /*or_comma=*/false,
12929                                                       /*consume_paren=*/true);
12930              }
12931         }
12932       else
12933         initializer = cp_parser_initializer (parser,
12934                                              &is_direct_init,
12935                                              &is_non_constant_init);
12936     }
12937
12938   /* The old parser allows attributes to appear after a parenthesized
12939      initializer.  Mark Mitchell proposed removing this functionality
12940      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12941      attributes -- but ignores them.  */
12942   if (cp_parser_allow_gnu_extensions_p (parser)
12943       && initialization_kind == CPP_OPEN_PAREN)
12944     if (cp_parser_attributes_opt (parser))
12945       warning (OPT_Wattributes,
12946                "attributes after parenthesized initializer ignored");
12947
12948   /* For an in-class declaration, use `grokfield' to create the
12949      declaration.  */
12950   if (member_p)
12951     {
12952       if (pushed_scope)
12953         {
12954           pop_scope (pushed_scope);
12955           pushed_scope = false;
12956         }
12957       decl = grokfield (declarator, decl_specifiers,
12958                         initializer, !is_non_constant_init,
12959                         /*asmspec=*/NULL_TREE,
12960                         prefix_attributes);
12961       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12962         cp_parser_save_default_args (parser, decl);
12963     }
12964
12965   /* Finish processing the declaration.  But, skip friend
12966      declarations.  */
12967   if (!friend_p && decl && decl != error_mark_node)
12968     {
12969       cp_finish_decl (decl,
12970                       initializer, !is_non_constant_init,
12971                       asm_specification,
12972                       /* If the initializer is in parentheses, then this is
12973                          a direct-initialization, which means that an
12974                          `explicit' constructor is OK.  Otherwise, an
12975                          `explicit' constructor cannot be used.  */
12976                       ((is_direct_init || !is_initialized)
12977                        ? 0 : LOOKUP_ONLYCONVERTING));
12978     }
12979   else if ((cxx_dialect != cxx98) && friend_p
12980            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12981     /* Core issue #226 (C++0x only): A default template-argument
12982        shall not be specified in a friend class template
12983        declaration. */
12984     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12985                              /*is_partial=*/0, /*is_friend_decl=*/1);
12986
12987   if (!friend_p && pushed_scope)
12988     pop_scope (pushed_scope);
12989
12990   return decl;
12991 }
12992
12993 /* Parse a declarator.
12994
12995    declarator:
12996      direct-declarator
12997      ptr-operator declarator
12998
12999    abstract-declarator:
13000      ptr-operator abstract-declarator [opt]
13001      direct-abstract-declarator
13002
13003    GNU Extensions:
13004
13005    declarator:
13006      attributes [opt] direct-declarator
13007      attributes [opt] ptr-operator declarator
13008
13009    abstract-declarator:
13010      attributes [opt] ptr-operator abstract-declarator [opt]
13011      attributes [opt] direct-abstract-declarator
13012
13013    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13014    detect constructor, destructor or conversion operators. It is set
13015    to -1 if the declarator is a name, and +1 if it is a
13016    function. Otherwise it is set to zero. Usually you just want to
13017    test for >0, but internally the negative value is used.
13018
13019    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13020    a decl-specifier-seq unless it declares a constructor, destructor,
13021    or conversion.  It might seem that we could check this condition in
13022    semantic analysis, rather than parsing, but that makes it difficult
13023    to handle something like `f()'.  We want to notice that there are
13024    no decl-specifiers, and therefore realize that this is an
13025    expression, not a declaration.)
13026
13027    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13028    the declarator is a direct-declarator of the form "(...)".
13029
13030    MEMBER_P is true iff this declarator is a member-declarator.  */
13031
13032 static cp_declarator *
13033 cp_parser_declarator (cp_parser* parser,
13034                       cp_parser_declarator_kind dcl_kind,
13035                       int* ctor_dtor_or_conv_p,
13036                       bool* parenthesized_p,
13037                       bool member_p)
13038 {
13039   cp_token *token;
13040   cp_declarator *declarator;
13041   enum tree_code code;
13042   cp_cv_quals cv_quals;
13043   tree class_type;
13044   tree attributes = NULL_TREE;
13045
13046   /* Assume this is not a constructor, destructor, or type-conversion
13047      operator.  */
13048   if (ctor_dtor_or_conv_p)
13049     *ctor_dtor_or_conv_p = 0;
13050
13051   if (cp_parser_allow_gnu_extensions_p (parser))
13052     attributes = cp_parser_attributes_opt (parser);
13053
13054   /* Peek at the next token.  */
13055   token = cp_lexer_peek_token (parser->lexer);
13056
13057   /* Check for the ptr-operator production.  */
13058   cp_parser_parse_tentatively (parser);
13059   /* Parse the ptr-operator.  */
13060   code = cp_parser_ptr_operator (parser,
13061                                  &class_type,
13062                                  &cv_quals);
13063   /* If that worked, then we have a ptr-operator.  */
13064   if (cp_parser_parse_definitely (parser))
13065     {
13066       /* If a ptr-operator was found, then this declarator was not
13067          parenthesized.  */
13068       if (parenthesized_p)
13069         *parenthesized_p = true;
13070       /* The dependent declarator is optional if we are parsing an
13071          abstract-declarator.  */
13072       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13073         cp_parser_parse_tentatively (parser);
13074
13075       /* Parse the dependent declarator.  */
13076       declarator = cp_parser_declarator (parser, dcl_kind,
13077                                          /*ctor_dtor_or_conv_p=*/NULL,
13078                                          /*parenthesized_p=*/NULL,
13079                                          /*member_p=*/false);
13080
13081       /* If we are parsing an abstract-declarator, we must handle the
13082          case where the dependent declarator is absent.  */
13083       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13084           && !cp_parser_parse_definitely (parser))
13085         declarator = NULL;
13086
13087       declarator = cp_parser_make_indirect_declarator
13088         (code, class_type, cv_quals, declarator);
13089     }
13090   /* Everything else is a direct-declarator.  */
13091   else
13092     {
13093       if (parenthesized_p)
13094         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13095                                                    CPP_OPEN_PAREN);
13096       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13097                                                 ctor_dtor_or_conv_p,
13098                                                 member_p);
13099     }
13100
13101   if (attributes && declarator && declarator != cp_error_declarator)
13102     declarator->attributes = attributes;
13103
13104   return declarator;
13105 }
13106
13107 /* Parse a direct-declarator or direct-abstract-declarator.
13108
13109    direct-declarator:
13110      declarator-id
13111      direct-declarator ( parameter-declaration-clause )
13112        cv-qualifier-seq [opt]
13113        exception-specification [opt]
13114      direct-declarator [ constant-expression [opt] ]
13115      ( declarator )
13116
13117    direct-abstract-declarator:
13118      direct-abstract-declarator [opt]
13119        ( parameter-declaration-clause )
13120        cv-qualifier-seq [opt]
13121        exception-specification [opt]
13122      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13123      ( abstract-declarator )
13124
13125    Returns a representation of the declarator.  DCL_KIND is
13126    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13127    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13128    we are parsing a direct-declarator.  It is
13129    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13130    of ambiguity we prefer an abstract declarator, as per
13131    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13132    cp_parser_declarator.  */
13133
13134 static cp_declarator *
13135 cp_parser_direct_declarator (cp_parser* parser,
13136                              cp_parser_declarator_kind dcl_kind,
13137                              int* ctor_dtor_or_conv_p,
13138                              bool member_p)
13139 {
13140   cp_token *token;
13141   cp_declarator *declarator = NULL;
13142   tree scope = NULL_TREE;
13143   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13144   bool saved_in_declarator_p = parser->in_declarator_p;
13145   bool first = true;
13146   tree pushed_scope = NULL_TREE;
13147
13148   while (true)
13149     {
13150       /* Peek at the next token.  */
13151       token = cp_lexer_peek_token (parser->lexer);
13152       if (token->type == CPP_OPEN_PAREN)
13153         {
13154           /* This is either a parameter-declaration-clause, or a
13155              parenthesized declarator. When we know we are parsing a
13156              named declarator, it must be a parenthesized declarator
13157              if FIRST is true. For instance, `(int)' is a
13158              parameter-declaration-clause, with an omitted
13159              direct-abstract-declarator. But `((*))', is a
13160              parenthesized abstract declarator. Finally, when T is a
13161              template parameter `(T)' is a
13162              parameter-declaration-clause, and not a parenthesized
13163              named declarator.
13164
13165              We first try and parse a parameter-declaration-clause,
13166              and then try a nested declarator (if FIRST is true).
13167
13168              It is not an error for it not to be a
13169              parameter-declaration-clause, even when FIRST is
13170              false. Consider,
13171
13172                int i (int);
13173                int i (3);
13174
13175              The first is the declaration of a function while the
13176              second is the definition of a variable, including its
13177              initializer.
13178
13179              Having seen only the parenthesis, we cannot know which of
13180              these two alternatives should be selected.  Even more
13181              complex are examples like:
13182
13183                int i (int (a));
13184                int i (int (3));
13185
13186              The former is a function-declaration; the latter is a
13187              variable initialization.
13188
13189              Thus again, we try a parameter-declaration-clause, and if
13190              that fails, we back out and return.  */
13191
13192           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13193             {
13194               tree params;
13195               unsigned saved_num_template_parameter_lists;
13196               bool is_declarator = false;
13197               tree t;
13198
13199               /* In a member-declarator, the only valid interpretation
13200                  of a parenthesis is the start of a
13201                  parameter-declaration-clause.  (It is invalid to
13202                  initialize a static data member with a parenthesized
13203                  initializer; only the "=" form of initialization is
13204                  permitted.)  */
13205               if (!member_p)
13206                 cp_parser_parse_tentatively (parser);
13207
13208               /* Consume the `('.  */
13209               cp_lexer_consume_token (parser->lexer);
13210               if (first)
13211                 {
13212                   /* If this is going to be an abstract declarator, we're
13213                      in a declarator and we can't have default args.  */
13214                   parser->default_arg_ok_p = false;
13215                   parser->in_declarator_p = true;
13216                 }
13217
13218               /* Inside the function parameter list, surrounding
13219                  template-parameter-lists do not apply.  */
13220               saved_num_template_parameter_lists
13221                 = parser->num_template_parameter_lists;
13222               parser->num_template_parameter_lists = 0;
13223
13224               begin_scope (sk_function_parms, NULL_TREE);
13225
13226               /* Parse the parameter-declaration-clause.  */
13227               params = cp_parser_parameter_declaration_clause (parser);
13228
13229               parser->num_template_parameter_lists
13230                 = saved_num_template_parameter_lists;
13231
13232               /* If all went well, parse the cv-qualifier-seq and the
13233                  exception-specification.  */
13234               if (member_p || cp_parser_parse_definitely (parser))
13235                 {
13236                   cp_cv_quals cv_quals;
13237                   tree exception_specification;
13238                   tree late_return;
13239
13240                   is_declarator = true;
13241
13242                   if (ctor_dtor_or_conv_p)
13243                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13244                   first = false;
13245                   /* Consume the `)'.  */
13246                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13247
13248                   /* Parse the cv-qualifier-seq.  */
13249                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13250                   /* And the exception-specification.  */
13251                   exception_specification
13252                     = cp_parser_exception_specification_opt (parser);
13253
13254                   late_return
13255                     = cp_parser_late_return_type_opt (parser);
13256
13257                   /* Create the function-declarator.  */
13258                   declarator = make_call_declarator (declarator,
13259                                                      params,
13260                                                      cv_quals,
13261                                                      exception_specification,
13262                                                      late_return);
13263                   /* Any subsequent parameter lists are to do with
13264                      return type, so are not those of the declared
13265                      function.  */
13266                   parser->default_arg_ok_p = false;
13267                 }
13268
13269               /* Remove the function parms from scope.  */
13270               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13271                 pop_binding (DECL_NAME (t), t);
13272               leave_scope();
13273
13274               if (is_declarator)
13275                 /* Repeat the main loop.  */
13276                 continue;
13277             }
13278
13279           /* If this is the first, we can try a parenthesized
13280              declarator.  */
13281           if (first)
13282             {
13283               bool saved_in_type_id_in_expr_p;
13284
13285               parser->default_arg_ok_p = saved_default_arg_ok_p;
13286               parser->in_declarator_p = saved_in_declarator_p;
13287
13288               /* Consume the `('.  */
13289               cp_lexer_consume_token (parser->lexer);
13290               /* Parse the nested declarator.  */
13291               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13292               parser->in_type_id_in_expr_p = true;
13293               declarator
13294                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13295                                         /*parenthesized_p=*/NULL,
13296                                         member_p);
13297               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13298               first = false;
13299               /* Expect a `)'.  */
13300               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13301                 declarator = cp_error_declarator;
13302               if (declarator == cp_error_declarator)
13303                 break;
13304
13305               goto handle_declarator;
13306             }
13307           /* Otherwise, we must be done.  */
13308           else
13309             break;
13310         }
13311       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13312                && token->type == CPP_OPEN_SQUARE)
13313         {
13314           /* Parse an array-declarator.  */
13315           tree bounds;
13316
13317           if (ctor_dtor_or_conv_p)
13318             *ctor_dtor_or_conv_p = 0;
13319
13320           first = false;
13321           parser->default_arg_ok_p = false;
13322           parser->in_declarator_p = true;
13323           /* Consume the `['.  */
13324           cp_lexer_consume_token (parser->lexer);
13325           /* Peek at the next token.  */
13326           token = cp_lexer_peek_token (parser->lexer);
13327           /* If the next token is `]', then there is no
13328              constant-expression.  */
13329           if (token->type != CPP_CLOSE_SQUARE)
13330             {
13331               bool non_constant_p;
13332
13333               bounds
13334                 = cp_parser_constant_expression (parser,
13335                                                  /*allow_non_constant=*/true,
13336                                                  &non_constant_p);
13337               if (!non_constant_p)
13338                 bounds = fold_non_dependent_expr (bounds);
13339               /* Normally, the array bound must be an integral constant
13340                  expression.  However, as an extension, we allow VLAs
13341                  in function scopes.  */
13342               else if (!parser->in_function_body)
13343                 {
13344                   error ("%Harray bound is not an integer constant",
13345                          &token->location);
13346                   bounds = error_mark_node;
13347                 }
13348               else if (processing_template_decl && !error_operand_p (bounds))
13349                 {
13350                   /* Remember this wasn't a constant-expression.  */
13351                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13352                   TREE_SIDE_EFFECTS (bounds) = 1;
13353                 }
13354             }
13355           else
13356             bounds = NULL_TREE;
13357           /* Look for the closing `]'.  */
13358           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13359             {
13360               declarator = cp_error_declarator;
13361               break;
13362             }
13363
13364           declarator = make_array_declarator (declarator, bounds);
13365         }
13366       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13367         {
13368           tree qualifying_scope;
13369           tree unqualified_name;
13370           special_function_kind sfk;
13371           bool abstract_ok;
13372           bool pack_expansion_p = false;
13373           cp_token *declarator_id_start_token;
13374
13375           /* Parse a declarator-id */
13376           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13377           if (abstract_ok)
13378             {
13379               cp_parser_parse_tentatively (parser);
13380
13381               /* If we see an ellipsis, we should be looking at a
13382                  parameter pack. */
13383               if (token->type == CPP_ELLIPSIS)
13384                 {
13385                   /* Consume the `...' */
13386                   cp_lexer_consume_token (parser->lexer);
13387
13388                   pack_expansion_p = true;
13389                 }
13390             }
13391
13392           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13393           unqualified_name
13394             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13395           qualifying_scope = parser->scope;
13396           if (abstract_ok)
13397             {
13398               bool okay = false;
13399
13400               if (!unqualified_name && pack_expansion_p)
13401                 {
13402                   /* Check whether an error occurred. */
13403                   okay = !cp_parser_error_occurred (parser);
13404
13405                   /* We already consumed the ellipsis to mark a
13406                      parameter pack, but we have no way to report it,
13407                      so abort the tentative parse. We will be exiting
13408                      immediately anyway. */
13409                   cp_parser_abort_tentative_parse (parser);
13410                 }
13411               else
13412                 okay = cp_parser_parse_definitely (parser);
13413
13414               if (!okay)
13415                 unqualified_name = error_mark_node;
13416               else if (unqualified_name
13417                        && (qualifying_scope
13418                            || (TREE_CODE (unqualified_name)
13419                                != IDENTIFIER_NODE)))
13420                 {
13421                   cp_parser_error (parser, "expected unqualified-id");
13422                   unqualified_name = error_mark_node;
13423                 }
13424             }
13425
13426           if (!unqualified_name)
13427             return NULL;
13428           if (unqualified_name == error_mark_node)
13429             {
13430               declarator = cp_error_declarator;
13431               pack_expansion_p = false;
13432               declarator->parameter_pack_p = false;
13433               break;
13434             }
13435
13436           if (qualifying_scope && at_namespace_scope_p ()
13437               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13438             {
13439               /* In the declaration of a member of a template class
13440                  outside of the class itself, the SCOPE will sometimes
13441                  be a TYPENAME_TYPE.  For example, given:
13442
13443                  template <typename T>
13444                  int S<T>::R::i = 3;
13445
13446                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13447                  this context, we must resolve S<T>::R to an ordinary
13448                  type, rather than a typename type.
13449
13450                  The reason we normally avoid resolving TYPENAME_TYPEs
13451                  is that a specialization of `S' might render
13452                  `S<T>::R' not a type.  However, if `S' is
13453                  specialized, then this `i' will not be used, so there
13454                  is no harm in resolving the types here.  */
13455               tree type;
13456
13457               /* Resolve the TYPENAME_TYPE.  */
13458               type = resolve_typename_type (qualifying_scope,
13459                                             /*only_current_p=*/false);
13460               /* If that failed, the declarator is invalid.  */
13461               if (TREE_CODE (type) == TYPENAME_TYPE)
13462                 error ("%H%<%T::%E%> is not a type",
13463                        &declarator_id_start_token->location,
13464                        TYPE_CONTEXT (qualifying_scope),
13465                        TYPE_IDENTIFIER (qualifying_scope));
13466               qualifying_scope = type;
13467             }
13468
13469           sfk = sfk_none;
13470
13471           if (unqualified_name)
13472             {
13473               tree class_type;
13474
13475               if (qualifying_scope
13476                   && CLASS_TYPE_P (qualifying_scope))
13477                 class_type = qualifying_scope;
13478               else
13479                 class_type = current_class_type;
13480
13481               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13482                 {
13483                   tree name_type = TREE_TYPE (unqualified_name);
13484                   if (class_type && same_type_p (name_type, class_type))
13485                     {
13486                       if (qualifying_scope
13487                           && CLASSTYPE_USE_TEMPLATE (name_type))
13488                         {
13489                           error ("%Hinvalid use of constructor as a template",
13490                                  &declarator_id_start_token->location);
13491                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13492                                   "name the constructor in a qualified name",
13493                                   class_type,
13494                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13495                                   class_type, name_type);
13496                           declarator = cp_error_declarator;
13497                           break;
13498                         }
13499                       else
13500                         unqualified_name = constructor_name (class_type);
13501                     }
13502                   else
13503                     {
13504                       /* We do not attempt to print the declarator
13505                          here because we do not have enough
13506                          information about its original syntactic
13507                          form.  */
13508                       cp_parser_error (parser, "invalid declarator");
13509                       declarator = cp_error_declarator;
13510                       break;
13511                     }
13512                 }
13513
13514               if (class_type)
13515                 {
13516                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13517                     sfk = sfk_destructor;
13518                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13519                     sfk = sfk_conversion;
13520                   else if (/* There's no way to declare a constructor
13521                               for an anonymous type, even if the type
13522                               got a name for linkage purposes.  */
13523                            !TYPE_WAS_ANONYMOUS (class_type)
13524                            && constructor_name_p (unqualified_name,
13525                                                   class_type))
13526                     {
13527                       unqualified_name = constructor_name (class_type);
13528                       sfk = sfk_constructor;
13529                     }
13530
13531                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13532                     *ctor_dtor_or_conv_p = -1;
13533                 }
13534             }
13535           declarator = make_id_declarator (qualifying_scope,
13536                                            unqualified_name,
13537                                            sfk);
13538           declarator->id_loc = token->location;
13539           declarator->parameter_pack_p = pack_expansion_p;
13540
13541           if (pack_expansion_p)
13542             maybe_warn_variadic_templates ();
13543
13544         handle_declarator:;
13545           scope = get_scope_of_declarator (declarator);
13546           if (scope)
13547             /* Any names that appear after the declarator-id for a
13548                member are looked up in the containing scope.  */
13549             pushed_scope = push_scope (scope);
13550           parser->in_declarator_p = true;
13551           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13552               || (declarator && declarator->kind == cdk_id))
13553             /* Default args are only allowed on function
13554                declarations.  */
13555             parser->default_arg_ok_p = saved_default_arg_ok_p;
13556           else
13557             parser->default_arg_ok_p = false;
13558
13559           first = false;
13560         }
13561       /* We're done.  */
13562       else
13563         break;
13564     }
13565
13566   /* For an abstract declarator, we might wind up with nothing at this
13567      point.  That's an error; the declarator is not optional.  */
13568   if (!declarator)
13569     cp_parser_error (parser, "expected declarator");
13570
13571   /* If we entered a scope, we must exit it now.  */
13572   if (pushed_scope)
13573     pop_scope (pushed_scope);
13574
13575   parser->default_arg_ok_p = saved_default_arg_ok_p;
13576   parser->in_declarator_p = saved_in_declarator_p;
13577
13578   return declarator;
13579 }
13580
13581 /* Parse a ptr-operator.
13582
13583    ptr-operator:
13584      * cv-qualifier-seq [opt]
13585      &
13586      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13587
13588    GNU Extension:
13589
13590    ptr-operator:
13591      & cv-qualifier-seq [opt]
13592
13593    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13594    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13595    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13596    filled in with the TYPE containing the member.  *CV_QUALS is
13597    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13598    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13599    Note that the tree codes returned by this function have nothing
13600    to do with the types of trees that will be eventually be created
13601    to represent the pointer or reference type being parsed. They are
13602    just constants with suggestive names. */
13603 static enum tree_code
13604 cp_parser_ptr_operator (cp_parser* parser,
13605                         tree* type,
13606                         cp_cv_quals *cv_quals)
13607 {
13608   enum tree_code code = ERROR_MARK;
13609   cp_token *token;
13610
13611   /* Assume that it's not a pointer-to-member.  */
13612   *type = NULL_TREE;
13613   /* And that there are no cv-qualifiers.  */
13614   *cv_quals = TYPE_UNQUALIFIED;
13615
13616   /* Peek at the next token.  */
13617   token = cp_lexer_peek_token (parser->lexer);
13618
13619   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13620   if (token->type == CPP_MULT)
13621     code = INDIRECT_REF;
13622   else if (token->type == CPP_AND)
13623     code = ADDR_EXPR;
13624   else if ((cxx_dialect != cxx98) &&
13625            token->type == CPP_AND_AND) /* C++0x only */
13626     code = NON_LVALUE_EXPR;
13627
13628   if (code != ERROR_MARK)
13629     {
13630       /* Consume the `*', `&' or `&&'.  */
13631       cp_lexer_consume_token (parser->lexer);
13632
13633       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13634          `&', if we are allowing GNU extensions.  (The only qualifier
13635          that can legally appear after `&' is `restrict', but that is
13636          enforced during semantic analysis.  */
13637       if (code == INDIRECT_REF
13638           || cp_parser_allow_gnu_extensions_p (parser))
13639         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13640     }
13641   else
13642     {
13643       /* Try the pointer-to-member case.  */
13644       cp_parser_parse_tentatively (parser);
13645       /* Look for the optional `::' operator.  */
13646       cp_parser_global_scope_opt (parser,
13647                                   /*current_scope_valid_p=*/false);
13648       /* Look for the nested-name specifier.  */
13649       token = cp_lexer_peek_token (parser->lexer);
13650       cp_parser_nested_name_specifier (parser,
13651                                        /*typename_keyword_p=*/false,
13652                                        /*check_dependency_p=*/true,
13653                                        /*type_p=*/false,
13654                                        /*is_declaration=*/false);
13655       /* If we found it, and the next token is a `*', then we are
13656          indeed looking at a pointer-to-member operator.  */
13657       if (!cp_parser_error_occurred (parser)
13658           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13659         {
13660           /* Indicate that the `*' operator was used.  */
13661           code = INDIRECT_REF;
13662
13663           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13664             error ("%H%qD is a namespace", &token->location, parser->scope);
13665           else
13666             {
13667               /* The type of which the member is a member is given by the
13668                  current SCOPE.  */
13669               *type = parser->scope;
13670               /* The next name will not be qualified.  */
13671               parser->scope = NULL_TREE;
13672               parser->qualifying_scope = NULL_TREE;
13673               parser->object_scope = NULL_TREE;
13674               /* Look for the optional cv-qualifier-seq.  */
13675               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13676             }
13677         }
13678       /* If that didn't work we don't have a ptr-operator.  */
13679       if (!cp_parser_parse_definitely (parser))
13680         cp_parser_error (parser, "expected ptr-operator");
13681     }
13682
13683   return code;
13684 }
13685
13686 /* Parse an (optional) cv-qualifier-seq.
13687
13688    cv-qualifier-seq:
13689      cv-qualifier cv-qualifier-seq [opt]
13690
13691    cv-qualifier:
13692      const
13693      volatile
13694
13695    GNU Extension:
13696
13697    cv-qualifier:
13698      __restrict__
13699
13700    Returns a bitmask representing the cv-qualifiers.  */
13701
13702 static cp_cv_quals
13703 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13704 {
13705   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13706
13707   while (true)
13708     {
13709       cp_token *token;
13710       cp_cv_quals cv_qualifier;
13711
13712       /* Peek at the next token.  */
13713       token = cp_lexer_peek_token (parser->lexer);
13714       /* See if it's a cv-qualifier.  */
13715       switch (token->keyword)
13716         {
13717         case RID_CONST:
13718           cv_qualifier = TYPE_QUAL_CONST;
13719           break;
13720
13721         case RID_VOLATILE:
13722           cv_qualifier = TYPE_QUAL_VOLATILE;
13723           break;
13724
13725         case RID_RESTRICT:
13726           cv_qualifier = TYPE_QUAL_RESTRICT;
13727           break;
13728
13729         default:
13730           cv_qualifier = TYPE_UNQUALIFIED;
13731           break;
13732         }
13733
13734       if (!cv_qualifier)
13735         break;
13736
13737       if (cv_quals & cv_qualifier)
13738         {
13739           error ("%Hduplicate cv-qualifier", &token->location);
13740           cp_lexer_purge_token (parser->lexer);
13741         }
13742       else
13743         {
13744           cp_lexer_consume_token (parser->lexer);
13745           cv_quals |= cv_qualifier;
13746         }
13747     }
13748
13749   return cv_quals;
13750 }
13751
13752 /* Parse a late-specified return type, if any.  This is not a separate
13753    non-terminal, but part of a function declarator, which looks like
13754
13755    -> type-id
13756
13757    Returns the type indicated by the type-id.  */
13758
13759 static tree
13760 cp_parser_late_return_type_opt (cp_parser* parser)
13761 {
13762   cp_token *token;
13763
13764   /* Peek at the next token.  */
13765   token = cp_lexer_peek_token (parser->lexer);
13766   /* A late-specified return type is indicated by an initial '->'. */
13767   if (token->type != CPP_DEREF)
13768     return NULL_TREE;
13769
13770   /* Consume the ->.  */
13771   cp_lexer_consume_token (parser->lexer);
13772
13773   return cp_parser_type_id (parser);
13774 }
13775
13776 /* Parse a declarator-id.
13777
13778    declarator-id:
13779      id-expression
13780      :: [opt] nested-name-specifier [opt] type-name
13781
13782    In the `id-expression' case, the value returned is as for
13783    cp_parser_id_expression if the id-expression was an unqualified-id.
13784    If the id-expression was a qualified-id, then a SCOPE_REF is
13785    returned.  The first operand is the scope (either a NAMESPACE_DECL
13786    or TREE_TYPE), but the second is still just a representation of an
13787    unqualified-id.  */
13788
13789 static tree
13790 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13791 {
13792   tree id;
13793   /* The expression must be an id-expression.  Assume that qualified
13794      names are the names of types so that:
13795
13796        template <class T>
13797        int S<T>::R::i = 3;
13798
13799      will work; we must treat `S<T>::R' as the name of a type.
13800      Similarly, assume that qualified names are templates, where
13801      required, so that:
13802
13803        template <class T>
13804        int S<T>::R<T>::i = 3;
13805
13806      will work, too.  */
13807   id = cp_parser_id_expression (parser,
13808                                 /*template_keyword_p=*/false,
13809                                 /*check_dependency_p=*/false,
13810                                 /*template_p=*/NULL,
13811                                 /*declarator_p=*/true,
13812                                 optional_p);
13813   if (id && BASELINK_P (id))
13814     id = BASELINK_FUNCTIONS (id);
13815   return id;
13816 }
13817
13818 /* Parse a type-id.
13819
13820    type-id:
13821      type-specifier-seq abstract-declarator [opt]
13822
13823    Returns the TYPE specified.  */
13824
13825 static tree
13826 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13827 {
13828   cp_decl_specifier_seq type_specifier_seq;
13829   cp_declarator *abstract_declarator;
13830
13831   /* Parse the type-specifier-seq.  */
13832   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13833                                 &type_specifier_seq);
13834   if (type_specifier_seq.type == error_mark_node)
13835     return error_mark_node;
13836
13837   /* There might or might not be an abstract declarator.  */
13838   cp_parser_parse_tentatively (parser);
13839   /* Look for the declarator.  */
13840   abstract_declarator
13841     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13842                             /*parenthesized_p=*/NULL,
13843                             /*member_p=*/false);
13844   /* Check to see if there really was a declarator.  */
13845   if (!cp_parser_parse_definitely (parser))
13846     abstract_declarator = NULL;
13847
13848   if (type_specifier_seq.type
13849       && type_uses_auto (type_specifier_seq.type))
13850     {
13851       /* A type-id with type 'auto' is only ok if the abstract declarator
13852          is a function declarator with a late-specified return type.  */
13853       if (abstract_declarator
13854           && abstract_declarator->kind == cdk_function
13855           && abstract_declarator->u.function.late_return_type)
13856         /* OK */;
13857       else
13858         {
13859           error ("invalid use of %<auto%>");
13860           return error_mark_node;
13861         }
13862     }
13863   
13864   return groktypename (&type_specifier_seq, abstract_declarator,
13865                        is_template_arg);
13866 }
13867
13868 static tree cp_parser_type_id (cp_parser *parser)
13869 {
13870   return cp_parser_type_id_1 (parser, false);
13871 }
13872
13873 static tree cp_parser_template_type_arg (cp_parser *parser)
13874 {
13875   return cp_parser_type_id_1 (parser, true);
13876 }
13877
13878 /* Parse a type-specifier-seq.
13879
13880    type-specifier-seq:
13881      type-specifier type-specifier-seq [opt]
13882
13883    GNU extension:
13884
13885    type-specifier-seq:
13886      attributes type-specifier-seq [opt]
13887
13888    If IS_CONDITION is true, we are at the start of a "condition",
13889    e.g., we've just seen "if (".
13890
13891    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13892
13893 static void
13894 cp_parser_type_specifier_seq (cp_parser* parser,
13895                               bool is_condition,
13896                               cp_decl_specifier_seq *type_specifier_seq)
13897 {
13898   bool seen_type_specifier = false;
13899   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13900   cp_token *start_token = NULL;
13901
13902   /* Clear the TYPE_SPECIFIER_SEQ.  */
13903   clear_decl_specs (type_specifier_seq);
13904
13905   /* Parse the type-specifiers and attributes.  */
13906   while (true)
13907     {
13908       tree type_specifier;
13909       bool is_cv_qualifier;
13910
13911       /* Check for attributes first.  */
13912       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13913         {
13914           type_specifier_seq->attributes =
13915             chainon (type_specifier_seq->attributes,
13916                      cp_parser_attributes_opt (parser));
13917           continue;
13918         }
13919
13920       /* record the token of the beginning of the type specifier seq,
13921          for error reporting purposes*/
13922      if (!start_token)
13923        start_token = cp_lexer_peek_token (parser->lexer);
13924
13925       /* Look for the type-specifier.  */
13926       type_specifier = cp_parser_type_specifier (parser,
13927                                                  flags,
13928                                                  type_specifier_seq,
13929                                                  /*is_declaration=*/false,
13930                                                  NULL,
13931                                                  &is_cv_qualifier);
13932       if (!type_specifier)
13933         {
13934           /* If the first type-specifier could not be found, this is not a
13935              type-specifier-seq at all.  */
13936           if (!seen_type_specifier)
13937             {
13938               cp_parser_error (parser, "expected type-specifier");
13939               type_specifier_seq->type = error_mark_node;
13940               return;
13941             }
13942           /* If subsequent type-specifiers could not be found, the
13943              type-specifier-seq is complete.  */
13944           break;
13945         }
13946
13947       seen_type_specifier = true;
13948       /* The standard says that a condition can be:
13949
13950             type-specifier-seq declarator = assignment-expression
13951
13952          However, given:
13953
13954            struct S {};
13955            if (int S = ...)
13956
13957          we should treat the "S" as a declarator, not as a
13958          type-specifier.  The standard doesn't say that explicitly for
13959          type-specifier-seq, but it does say that for
13960          decl-specifier-seq in an ordinary declaration.  Perhaps it
13961          would be clearer just to allow a decl-specifier-seq here, and
13962          then add a semantic restriction that if any decl-specifiers
13963          that are not type-specifiers appear, the program is invalid.  */
13964       if (is_condition && !is_cv_qualifier)
13965         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13966     }
13967
13968   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13969 }
13970
13971 /* Parse a parameter-declaration-clause.
13972
13973    parameter-declaration-clause:
13974      parameter-declaration-list [opt] ... [opt]
13975      parameter-declaration-list , ...
13976
13977    Returns a representation for the parameter declarations.  A return
13978    value of NULL indicates a parameter-declaration-clause consisting
13979    only of an ellipsis.  */
13980
13981 static tree
13982 cp_parser_parameter_declaration_clause (cp_parser* parser)
13983 {
13984   tree parameters;
13985   cp_token *token;
13986   bool ellipsis_p;
13987   bool is_error;
13988
13989   /* Peek at the next token.  */
13990   token = cp_lexer_peek_token (parser->lexer);
13991   /* Check for trivial parameter-declaration-clauses.  */
13992   if (token->type == CPP_ELLIPSIS)
13993     {
13994       /* Consume the `...' token.  */
13995       cp_lexer_consume_token (parser->lexer);
13996       return NULL_TREE;
13997     }
13998   else if (token->type == CPP_CLOSE_PAREN)
13999     /* There are no parameters.  */
14000     {
14001 #ifndef NO_IMPLICIT_EXTERN_C
14002       if (in_system_header && current_class_type == NULL
14003           && current_lang_name == lang_name_c)
14004         return NULL_TREE;
14005       else
14006 #endif
14007         return void_list_node;
14008     }
14009   /* Check for `(void)', too, which is a special case.  */
14010   else if (token->keyword == RID_VOID
14011            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14012                == CPP_CLOSE_PAREN))
14013     {
14014       /* Consume the `void' token.  */
14015       cp_lexer_consume_token (parser->lexer);
14016       /* There are no parameters.  */
14017       return void_list_node;
14018     }
14019
14020   /* Parse the parameter-declaration-list.  */
14021   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14022   /* If a parse error occurred while parsing the
14023      parameter-declaration-list, then the entire
14024      parameter-declaration-clause is erroneous.  */
14025   if (is_error)
14026     return NULL;
14027
14028   /* Peek at the next token.  */
14029   token = cp_lexer_peek_token (parser->lexer);
14030   /* If it's a `,', the clause should terminate with an ellipsis.  */
14031   if (token->type == CPP_COMMA)
14032     {
14033       /* Consume the `,'.  */
14034       cp_lexer_consume_token (parser->lexer);
14035       /* Expect an ellipsis.  */
14036       ellipsis_p
14037         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14038     }
14039   /* It might also be `...' if the optional trailing `,' was
14040      omitted.  */
14041   else if (token->type == CPP_ELLIPSIS)
14042     {
14043       /* Consume the `...' token.  */
14044       cp_lexer_consume_token (parser->lexer);
14045       /* And remember that we saw it.  */
14046       ellipsis_p = true;
14047     }
14048   else
14049     ellipsis_p = false;
14050
14051   /* Finish the parameter list.  */
14052   if (!ellipsis_p)
14053     parameters = chainon (parameters, void_list_node);
14054
14055   return parameters;
14056 }
14057
14058 /* Parse a parameter-declaration-list.
14059
14060    parameter-declaration-list:
14061      parameter-declaration
14062      parameter-declaration-list , parameter-declaration
14063
14064    Returns a representation of the parameter-declaration-list, as for
14065    cp_parser_parameter_declaration_clause.  However, the
14066    `void_list_node' is never appended to the list.  Upon return,
14067    *IS_ERROR will be true iff an error occurred.  */
14068
14069 static tree
14070 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14071 {
14072   tree parameters = NULL_TREE;
14073   tree *tail = &parameters; 
14074   bool saved_in_unbraced_linkage_specification_p;
14075
14076   /* Assume all will go well.  */
14077   *is_error = false;
14078   /* The special considerations that apply to a function within an
14079      unbraced linkage specifications do not apply to the parameters
14080      to the function.  */
14081   saved_in_unbraced_linkage_specification_p 
14082     = parser->in_unbraced_linkage_specification_p;
14083   parser->in_unbraced_linkage_specification_p = false;
14084
14085   /* Look for more parameters.  */
14086   while (true)
14087     {
14088       cp_parameter_declarator *parameter;
14089       tree decl = error_mark_node;
14090       bool parenthesized_p;
14091       /* Parse the parameter.  */
14092       parameter
14093         = cp_parser_parameter_declaration (parser,
14094                                            /*template_parm_p=*/false,
14095                                            &parenthesized_p);
14096
14097       /* We don't know yet if the enclosing context is deprecated, so wait
14098          and warn in grokparms if appropriate.  */
14099       deprecated_state = DEPRECATED_SUPPRESS;
14100
14101       if (parameter)
14102         decl = grokdeclarator (parameter->declarator,
14103                                &parameter->decl_specifiers,
14104                                PARM,
14105                                parameter->default_argument != NULL_TREE,
14106                                &parameter->decl_specifiers.attributes);
14107
14108       deprecated_state = DEPRECATED_NORMAL;
14109
14110       /* If a parse error occurred parsing the parameter declaration,
14111          then the entire parameter-declaration-list is erroneous.  */
14112       if (decl == error_mark_node)
14113         {
14114           *is_error = true;
14115           parameters = error_mark_node;
14116           break;
14117         }
14118
14119       if (parameter->decl_specifiers.attributes)
14120         cplus_decl_attributes (&decl,
14121                                parameter->decl_specifiers.attributes,
14122                                0);
14123       if (DECL_NAME (decl))
14124         decl = pushdecl (decl);
14125
14126       /* Add the new parameter to the list.  */
14127       *tail = build_tree_list (parameter->default_argument, decl);
14128       tail = &TREE_CHAIN (*tail);
14129
14130       /* Peek at the next token.  */
14131       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14132           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14133           /* These are for Objective-C++ */
14134           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14135           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14136         /* The parameter-declaration-list is complete.  */
14137         break;
14138       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14139         {
14140           cp_token *token;
14141
14142           /* Peek at the next token.  */
14143           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14144           /* If it's an ellipsis, then the list is complete.  */
14145           if (token->type == CPP_ELLIPSIS)
14146             break;
14147           /* Otherwise, there must be more parameters.  Consume the
14148              `,'.  */
14149           cp_lexer_consume_token (parser->lexer);
14150           /* When parsing something like:
14151
14152                 int i(float f, double d)
14153
14154              we can tell after seeing the declaration for "f" that we
14155              are not looking at an initialization of a variable "i",
14156              but rather at the declaration of a function "i".
14157
14158              Due to the fact that the parsing of template arguments
14159              (as specified to a template-id) requires backtracking we
14160              cannot use this technique when inside a template argument
14161              list.  */
14162           if (!parser->in_template_argument_list_p
14163               && !parser->in_type_id_in_expr_p
14164               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14165               /* However, a parameter-declaration of the form
14166                  "foat(f)" (which is a valid declaration of a
14167                  parameter "f") can also be interpreted as an
14168                  expression (the conversion of "f" to "float").  */
14169               && !parenthesized_p)
14170             cp_parser_commit_to_tentative_parse (parser);
14171         }
14172       else
14173         {
14174           cp_parser_error (parser, "expected %<,%> or %<...%>");
14175           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14176             cp_parser_skip_to_closing_parenthesis (parser,
14177                                                    /*recovering=*/true,
14178                                                    /*or_comma=*/false,
14179                                                    /*consume_paren=*/false);
14180           break;
14181         }
14182     }
14183
14184   parser->in_unbraced_linkage_specification_p
14185     = saved_in_unbraced_linkage_specification_p;
14186
14187   return parameters;
14188 }
14189
14190 /* Parse a parameter declaration.
14191
14192    parameter-declaration:
14193      decl-specifier-seq ... [opt] declarator
14194      decl-specifier-seq declarator = assignment-expression
14195      decl-specifier-seq ... [opt] abstract-declarator [opt]
14196      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14197
14198    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14199    declares a template parameter.  (In that case, a non-nested `>'
14200    token encountered during the parsing of the assignment-expression
14201    is not interpreted as a greater-than operator.)
14202
14203    Returns a representation of the parameter, or NULL if an error
14204    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14205    true iff the declarator is of the form "(p)".  */
14206
14207 static cp_parameter_declarator *
14208 cp_parser_parameter_declaration (cp_parser *parser,
14209                                  bool template_parm_p,
14210                                  bool *parenthesized_p)
14211 {
14212   int declares_class_or_enum;
14213   bool greater_than_is_operator_p;
14214   cp_decl_specifier_seq decl_specifiers;
14215   cp_declarator *declarator;
14216   tree default_argument;
14217   cp_token *token = NULL, *declarator_token_start = NULL;
14218   const char *saved_message;
14219
14220   /* In a template parameter, `>' is not an operator.
14221
14222      [temp.param]
14223
14224      When parsing a default template-argument for a non-type
14225      template-parameter, the first non-nested `>' is taken as the end
14226      of the template parameter-list rather than a greater-than
14227      operator.  */
14228   greater_than_is_operator_p = !template_parm_p;
14229
14230   /* Type definitions may not appear in parameter types.  */
14231   saved_message = parser->type_definition_forbidden_message;
14232   parser->type_definition_forbidden_message
14233     = "types may not be defined in parameter types";
14234
14235   /* Parse the declaration-specifiers.  */
14236   cp_parser_decl_specifier_seq (parser,
14237                                 CP_PARSER_FLAGS_NONE,
14238                                 &decl_specifiers,
14239                                 &declares_class_or_enum);
14240   /* If an error occurred, there's no reason to attempt to parse the
14241      rest of the declaration.  */
14242   if (cp_parser_error_occurred (parser))
14243     {
14244       parser->type_definition_forbidden_message = saved_message;
14245       return NULL;
14246     }
14247
14248   /* Peek at the next token.  */
14249   token = cp_lexer_peek_token (parser->lexer);
14250
14251   /* If the next token is a `)', `,', `=', `>', or `...', then there
14252      is no declarator. However, when variadic templates are enabled,
14253      there may be a declarator following `...'.  */
14254   if (token->type == CPP_CLOSE_PAREN
14255       || token->type == CPP_COMMA
14256       || token->type == CPP_EQ
14257       || token->type == CPP_GREATER)
14258     {
14259       declarator = NULL;
14260       if (parenthesized_p)
14261         *parenthesized_p = false;
14262     }
14263   /* Otherwise, there should be a declarator.  */
14264   else
14265     {
14266       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14267       parser->default_arg_ok_p = false;
14268
14269       /* After seeing a decl-specifier-seq, if the next token is not a
14270          "(", there is no possibility that the code is a valid
14271          expression.  Therefore, if parsing tentatively, we commit at
14272          this point.  */
14273       if (!parser->in_template_argument_list_p
14274           /* In an expression context, having seen:
14275
14276                (int((char ...
14277
14278              we cannot be sure whether we are looking at a
14279              function-type (taking a "char" as a parameter) or a cast
14280              of some object of type "char" to "int".  */
14281           && !parser->in_type_id_in_expr_p
14282           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14283           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14284         cp_parser_commit_to_tentative_parse (parser);
14285       /* Parse the declarator.  */
14286       declarator_token_start = token;
14287       declarator = cp_parser_declarator (parser,
14288                                          CP_PARSER_DECLARATOR_EITHER,
14289                                          /*ctor_dtor_or_conv_p=*/NULL,
14290                                          parenthesized_p,
14291                                          /*member_p=*/false);
14292       parser->default_arg_ok_p = saved_default_arg_ok_p;
14293       /* After the declarator, allow more attributes.  */
14294       decl_specifiers.attributes
14295         = chainon (decl_specifiers.attributes,
14296                    cp_parser_attributes_opt (parser));
14297     }
14298
14299   /* If the next token is an ellipsis, and we have not seen a
14300      declarator name, and the type of the declarator contains parameter
14301      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14302      a parameter pack expansion expression. Otherwise, leave the
14303      ellipsis for a C-style variadic function. */
14304   token = cp_lexer_peek_token (parser->lexer);
14305   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14306     {
14307       tree type = decl_specifiers.type;
14308
14309       if (type && DECL_P (type))
14310         type = TREE_TYPE (type);
14311
14312       if (type
14313           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14314           && declarator_can_be_parameter_pack (declarator)
14315           && (!declarator || !declarator->parameter_pack_p)
14316           && uses_parameter_packs (type))
14317         {
14318           /* Consume the `...'. */
14319           cp_lexer_consume_token (parser->lexer);
14320           maybe_warn_variadic_templates ();
14321           
14322           /* Build a pack expansion type */
14323           if (declarator)
14324             declarator->parameter_pack_p = true;
14325           else
14326             decl_specifiers.type = make_pack_expansion (type);
14327         }
14328     }
14329
14330   /* The restriction on defining new types applies only to the type
14331      of the parameter, not to the default argument.  */
14332   parser->type_definition_forbidden_message = saved_message;
14333
14334   /* If the next token is `=', then process a default argument.  */
14335   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14336     {
14337       /* Consume the `='.  */
14338       cp_lexer_consume_token (parser->lexer);
14339
14340       /* If we are defining a class, then the tokens that make up the
14341          default argument must be saved and processed later.  */
14342       if (!template_parm_p && at_class_scope_p ()
14343           && TYPE_BEING_DEFINED (current_class_type))
14344         {
14345           unsigned depth = 0;
14346           int maybe_template_id = 0;
14347           cp_token *first_token;
14348           cp_token *token;
14349
14350           /* Add tokens until we have processed the entire default
14351              argument.  We add the range [first_token, token).  */
14352           first_token = cp_lexer_peek_token (parser->lexer);
14353           while (true)
14354             {
14355               bool done = false;
14356
14357               /* Peek at the next token.  */
14358               token = cp_lexer_peek_token (parser->lexer);
14359               /* What we do depends on what token we have.  */
14360               switch (token->type)
14361                 {
14362                   /* In valid code, a default argument must be
14363                      immediately followed by a `,' `)', or `...'.  */
14364                 case CPP_COMMA:
14365                   if (depth == 0 && maybe_template_id)
14366                     {
14367                       /* If we've seen a '<', we might be in a
14368                          template-argument-list.  Until Core issue 325 is
14369                          resolved, we don't know how this situation ought
14370                          to be handled, so try to DTRT.  We check whether
14371                          what comes after the comma is a valid parameter
14372                          declaration list.  If it is, then the comma ends
14373                          the default argument; otherwise the default
14374                          argument continues.  */
14375                       bool error = false;
14376
14377                       /* Set ITALP so cp_parser_parameter_declaration_list
14378                          doesn't decide to commit to this parse.  */
14379                       bool saved_italp = parser->in_template_argument_list_p;
14380                       parser->in_template_argument_list_p = true;
14381
14382                       cp_parser_parse_tentatively (parser);
14383                       cp_lexer_consume_token (parser->lexer);
14384                       cp_parser_parameter_declaration_list (parser, &error);
14385                       if (!cp_parser_error_occurred (parser) && !error)
14386                         done = true;
14387                       cp_parser_abort_tentative_parse (parser);
14388
14389                       parser->in_template_argument_list_p = saved_italp;
14390                       break;
14391                     }
14392                 case CPP_CLOSE_PAREN:
14393                 case CPP_ELLIPSIS:
14394                   /* If we run into a non-nested `;', `}', or `]',
14395                      then the code is invalid -- but the default
14396                      argument is certainly over.  */
14397                 case CPP_SEMICOLON:
14398                 case CPP_CLOSE_BRACE:
14399                 case CPP_CLOSE_SQUARE:
14400                   if (depth == 0)
14401                     done = true;
14402                   /* Update DEPTH, if necessary.  */
14403                   else if (token->type == CPP_CLOSE_PAREN
14404                            || token->type == CPP_CLOSE_BRACE
14405                            || token->type == CPP_CLOSE_SQUARE)
14406                     --depth;
14407                   break;
14408
14409                 case CPP_OPEN_PAREN:
14410                 case CPP_OPEN_SQUARE:
14411                 case CPP_OPEN_BRACE:
14412                   ++depth;
14413                   break;
14414
14415                 case CPP_LESS:
14416                   if (depth == 0)
14417                     /* This might be the comparison operator, or it might
14418                        start a template argument list.  */
14419                     ++maybe_template_id;
14420                   break;
14421
14422                 case CPP_RSHIFT:
14423                   if (cxx_dialect == cxx98)
14424                     break;
14425                   /* Fall through for C++0x, which treats the `>>'
14426                      operator like two `>' tokens in certain
14427                      cases.  */
14428
14429                 case CPP_GREATER:
14430                   if (depth == 0)
14431                     {
14432                       /* This might be an operator, or it might close a
14433                          template argument list.  But if a previous '<'
14434                          started a template argument list, this will have
14435                          closed it, so we can't be in one anymore.  */
14436                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14437                       if (maybe_template_id < 0)
14438                         maybe_template_id = 0;
14439                     }
14440                   break;
14441
14442                   /* If we run out of tokens, issue an error message.  */
14443                 case CPP_EOF:
14444                 case CPP_PRAGMA_EOL:
14445                   error ("%Hfile ends in default argument", &token->location);
14446                   done = true;
14447                   break;
14448
14449                 case CPP_NAME:
14450                 case CPP_SCOPE:
14451                   /* In these cases, we should look for template-ids.
14452                      For example, if the default argument is
14453                      `X<int, double>()', we need to do name lookup to
14454                      figure out whether or not `X' is a template; if
14455                      so, the `,' does not end the default argument.
14456
14457                      That is not yet done.  */
14458                   break;
14459
14460                 default:
14461                   break;
14462                 }
14463
14464               /* If we've reached the end, stop.  */
14465               if (done)
14466                 break;
14467
14468               /* Add the token to the token block.  */
14469               token = cp_lexer_consume_token (parser->lexer);
14470             }
14471
14472           /* Create a DEFAULT_ARG to represent the unparsed default
14473              argument.  */
14474           default_argument = make_node (DEFAULT_ARG);
14475           DEFARG_TOKENS (default_argument)
14476             = cp_token_cache_new (first_token, token);
14477           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14478         }
14479       /* Outside of a class definition, we can just parse the
14480          assignment-expression.  */
14481       else
14482         {
14483           token = cp_lexer_peek_token (parser->lexer);
14484           default_argument 
14485             = cp_parser_default_argument (parser, template_parm_p);
14486         }
14487
14488       if (!parser->default_arg_ok_p)
14489         {
14490           if (flag_permissive)
14491             warning (0, "deprecated use of default argument for parameter of non-function");
14492           else
14493             {
14494               error ("%Hdefault arguments are only "
14495                      "permitted for function parameters",
14496                      &token->location);
14497               default_argument = NULL_TREE;
14498             }
14499         }
14500       else if ((declarator && declarator->parameter_pack_p)
14501                || (decl_specifiers.type
14502                    && PACK_EXPANSION_P (decl_specifiers.type)))
14503         {
14504           const char* kind = template_parm_p? "template " : "";
14505           
14506           /* Find the name of the parameter pack.  */     
14507           cp_declarator *id_declarator = declarator;
14508           while (id_declarator && id_declarator->kind != cdk_id)
14509             id_declarator = id_declarator->declarator;
14510           
14511           if (id_declarator && id_declarator->kind == cdk_id)
14512             error ("%H%sparameter pack %qD cannot have a default argument",
14513                    &declarator_token_start->location,
14514                    kind, id_declarator->u.id.unqualified_name);
14515           else
14516             error ("%H%sparameter pack cannot have a default argument",
14517                    &declarator_token_start->location, kind);
14518           
14519           default_argument = NULL_TREE;
14520         }
14521     }
14522   else
14523     default_argument = NULL_TREE;
14524
14525   return make_parameter_declarator (&decl_specifiers,
14526                                     declarator,
14527                                     default_argument);
14528 }
14529
14530 /* Parse a default argument and return it.
14531
14532    TEMPLATE_PARM_P is true if this is a default argument for a
14533    non-type template parameter.  */
14534 static tree
14535 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14536 {
14537   tree default_argument = NULL_TREE;
14538   bool saved_greater_than_is_operator_p;
14539   bool saved_local_variables_forbidden_p;
14540
14541   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14542      set correctly.  */
14543   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14544   parser->greater_than_is_operator_p = !template_parm_p;
14545   /* Local variable names (and the `this' keyword) may not
14546      appear in a default argument.  */
14547   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14548   parser->local_variables_forbidden_p = true;
14549   /* The default argument expression may cause implicitly
14550      defined member functions to be synthesized, which will
14551      result in garbage collection.  We must treat this
14552      situation as if we were within the body of function so as
14553      to avoid collecting live data on the stack.  */
14554   ++function_depth;
14555   /* Parse the assignment-expression.  */
14556   if (template_parm_p)
14557     push_deferring_access_checks (dk_no_deferred);
14558   default_argument
14559     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14560   if (template_parm_p)
14561     pop_deferring_access_checks ();
14562   /* Restore saved state.  */
14563   --function_depth;
14564   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14565   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14566
14567   return default_argument;
14568 }
14569
14570 /* Parse a function-body.
14571
14572    function-body:
14573      compound_statement  */
14574
14575 static void
14576 cp_parser_function_body (cp_parser *parser)
14577 {
14578   cp_parser_compound_statement (parser, NULL, false);
14579 }
14580
14581 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14582    true if a ctor-initializer was present.  */
14583
14584 static bool
14585 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14586 {
14587   tree body;
14588   bool ctor_initializer_p;
14589
14590   /* Begin the function body.  */
14591   body = begin_function_body ();
14592   /* Parse the optional ctor-initializer.  */
14593   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14594   /* Parse the function-body.  */
14595   cp_parser_function_body (parser);
14596   /* Finish the function body.  */
14597   finish_function_body (body);
14598
14599   return ctor_initializer_p;
14600 }
14601
14602 /* Parse an initializer.
14603
14604    initializer:
14605      = initializer-clause
14606      ( expression-list )
14607
14608    Returns an expression representing the initializer.  If no
14609    initializer is present, NULL_TREE is returned.
14610
14611    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14612    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14613    set to TRUE if there is no initializer present.  If there is an
14614    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14615    is set to true; otherwise it is set to false.  */
14616
14617 static tree
14618 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14619                        bool* non_constant_p)
14620 {
14621   cp_token *token;
14622   tree init;
14623
14624   /* Peek at the next token.  */
14625   token = cp_lexer_peek_token (parser->lexer);
14626
14627   /* Let our caller know whether or not this initializer was
14628      parenthesized.  */
14629   *is_direct_init = (token->type != CPP_EQ);
14630   /* Assume that the initializer is constant.  */
14631   *non_constant_p = false;
14632
14633   if (token->type == CPP_EQ)
14634     {
14635       /* Consume the `='.  */
14636       cp_lexer_consume_token (parser->lexer);
14637       /* Parse the initializer-clause.  */
14638       init = cp_parser_initializer_clause (parser, non_constant_p);
14639     }
14640   else if (token->type == CPP_OPEN_PAREN)
14641     {
14642       VEC(tree,gc) *vec;
14643       vec = cp_parser_parenthesized_expression_list (parser, false,
14644                                                      /*cast_p=*/false,
14645                                                      /*allow_expansion_p=*/true,
14646                                                      non_constant_p);
14647       if (vec == NULL)
14648         return error_mark_node;
14649       init = build_tree_list_vec (vec);
14650       release_tree_vector (vec);
14651     }
14652   else if (token->type == CPP_OPEN_BRACE)
14653     {
14654       maybe_warn_cpp0x ("extended initializer lists");
14655       init = cp_parser_braced_list (parser, non_constant_p);
14656       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14657     }
14658   else
14659     {
14660       /* Anything else is an error.  */
14661       cp_parser_error (parser, "expected initializer");
14662       init = error_mark_node;
14663     }
14664
14665   return init;
14666 }
14667
14668 /* Parse an initializer-clause.
14669
14670    initializer-clause:
14671      assignment-expression
14672      braced-init-list
14673
14674    Returns an expression representing the initializer.
14675
14676    If the `assignment-expression' production is used the value
14677    returned is simply a representation for the expression.
14678
14679    Otherwise, calls cp_parser_braced_list.  */
14680
14681 static tree
14682 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14683 {
14684   tree initializer;
14685
14686   /* Assume the expression is constant.  */
14687   *non_constant_p = false;
14688
14689   /* If it is not a `{', then we are looking at an
14690      assignment-expression.  */
14691   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14692     {
14693       initializer
14694         = cp_parser_constant_expression (parser,
14695                                         /*allow_non_constant_p=*/true,
14696                                         non_constant_p);
14697       if (!*non_constant_p)
14698         initializer = fold_non_dependent_expr (initializer);
14699     }
14700   else
14701     initializer = cp_parser_braced_list (parser, non_constant_p);
14702
14703   return initializer;
14704 }
14705
14706 /* Parse a brace-enclosed initializer list.
14707
14708    braced-init-list:
14709      { initializer-list , [opt] }
14710      { }
14711
14712    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14713    the elements of the initializer-list (or NULL, if the last
14714    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14715    NULL_TREE.  There is no way to detect whether or not the optional
14716    trailing `,' was provided.  NON_CONSTANT_P is as for
14717    cp_parser_initializer.  */     
14718
14719 static tree
14720 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14721 {
14722   tree initializer;
14723
14724   /* Consume the `{' token.  */
14725   cp_lexer_consume_token (parser->lexer);
14726   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14727   initializer = make_node (CONSTRUCTOR);
14728   /* If it's not a `}', then there is a non-trivial initializer.  */
14729   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14730     {
14731       /* Parse the initializer list.  */
14732       CONSTRUCTOR_ELTS (initializer)
14733         = cp_parser_initializer_list (parser, non_constant_p);
14734       /* A trailing `,' token is allowed.  */
14735       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14736         cp_lexer_consume_token (parser->lexer);
14737     }
14738   /* Now, there should be a trailing `}'.  */
14739   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14740   TREE_TYPE (initializer) = init_list_type_node;
14741   return initializer;
14742 }
14743
14744 /* Parse an initializer-list.
14745
14746    initializer-list:
14747      initializer-clause ... [opt]
14748      initializer-list , initializer-clause ... [opt]
14749
14750    GNU Extension:
14751
14752    initializer-list:
14753      identifier : initializer-clause
14754      initializer-list, identifier : initializer-clause
14755
14756    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14757    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14758    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14759    as for cp_parser_initializer.  */
14760
14761 static VEC(constructor_elt,gc) *
14762 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14763 {
14764   VEC(constructor_elt,gc) *v = NULL;
14765
14766   /* Assume all of the expressions are constant.  */
14767   *non_constant_p = false;
14768
14769   /* Parse the rest of the list.  */
14770   while (true)
14771     {
14772       cp_token *token;
14773       tree identifier;
14774       tree initializer;
14775       bool clause_non_constant_p;
14776
14777       /* If the next token is an identifier and the following one is a
14778          colon, we are looking at the GNU designated-initializer
14779          syntax.  */
14780       if (cp_parser_allow_gnu_extensions_p (parser)
14781           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14782           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14783         {
14784           /* Warn the user that they are using an extension.  */
14785           pedwarn (input_location, OPT_pedantic, 
14786                    "ISO C++ does not allow designated initializers");
14787           /* Consume the identifier.  */
14788           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14789           /* Consume the `:'.  */
14790           cp_lexer_consume_token (parser->lexer);
14791         }
14792       else
14793         identifier = NULL_TREE;
14794
14795       /* Parse the initializer.  */
14796       initializer = cp_parser_initializer_clause (parser,
14797                                                   &clause_non_constant_p);
14798       /* If any clause is non-constant, so is the entire initializer.  */
14799       if (clause_non_constant_p)
14800         *non_constant_p = true;
14801
14802       /* If we have an ellipsis, this is an initializer pack
14803          expansion.  */
14804       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14805         {
14806           /* Consume the `...'.  */
14807           cp_lexer_consume_token (parser->lexer);
14808
14809           /* Turn the initializer into an initializer expansion.  */
14810           initializer = make_pack_expansion (initializer);
14811         }
14812
14813       /* Add it to the vector.  */
14814       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14815
14816       /* If the next token is not a comma, we have reached the end of
14817          the list.  */
14818       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14819         break;
14820
14821       /* Peek at the next token.  */
14822       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14823       /* If the next token is a `}', then we're still done.  An
14824          initializer-clause can have a trailing `,' after the
14825          initializer-list and before the closing `}'.  */
14826       if (token->type == CPP_CLOSE_BRACE)
14827         break;
14828
14829       /* Consume the `,' token.  */
14830       cp_lexer_consume_token (parser->lexer);
14831     }
14832
14833   return v;
14834 }
14835
14836 /* Classes [gram.class] */
14837
14838 /* Parse a class-name.
14839
14840    class-name:
14841      identifier
14842      template-id
14843
14844    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14845    to indicate that names looked up in dependent types should be
14846    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14847    keyword has been used to indicate that the name that appears next
14848    is a template.  TAG_TYPE indicates the explicit tag given before
14849    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14850    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14851    is the class being defined in a class-head.
14852
14853    Returns the TYPE_DECL representing the class.  */
14854
14855 static tree
14856 cp_parser_class_name (cp_parser *parser,
14857                       bool typename_keyword_p,
14858                       bool template_keyword_p,
14859                       enum tag_types tag_type,
14860                       bool check_dependency_p,
14861                       bool class_head_p,
14862                       bool is_declaration)
14863 {
14864   tree decl;
14865   tree scope;
14866   bool typename_p;
14867   cp_token *token;
14868   tree identifier = NULL_TREE;
14869
14870   /* All class-names start with an identifier.  */
14871   token = cp_lexer_peek_token (parser->lexer);
14872   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14873     {
14874       cp_parser_error (parser, "expected class-name");
14875       return error_mark_node;
14876     }
14877
14878   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14879      to a template-id, so we save it here.  */
14880   scope = parser->scope;
14881   if (scope == error_mark_node)
14882     return error_mark_node;
14883
14884   /* Any name names a type if we're following the `typename' keyword
14885      in a qualified name where the enclosing scope is type-dependent.  */
14886   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14887                 && dependent_type_p (scope));
14888   /* Handle the common case (an identifier, but not a template-id)
14889      efficiently.  */
14890   if (token->type == CPP_NAME
14891       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14892     {
14893       cp_token *identifier_token;
14894       bool ambiguous_p;
14895
14896       /* Look for the identifier.  */
14897       identifier_token = cp_lexer_peek_token (parser->lexer);
14898       ambiguous_p = identifier_token->ambiguous_p;
14899       identifier = cp_parser_identifier (parser);
14900       /* If the next token isn't an identifier, we are certainly not
14901          looking at a class-name.  */
14902       if (identifier == error_mark_node)
14903         decl = error_mark_node;
14904       /* If we know this is a type-name, there's no need to look it
14905          up.  */
14906       else if (typename_p)
14907         decl = identifier;
14908       else
14909         {
14910           tree ambiguous_decls;
14911           /* If we already know that this lookup is ambiguous, then
14912              we've already issued an error message; there's no reason
14913              to check again.  */
14914           if (ambiguous_p)
14915             {
14916               cp_parser_simulate_error (parser);
14917               return error_mark_node;
14918             }
14919           /* If the next token is a `::', then the name must be a type
14920              name.
14921
14922              [basic.lookup.qual]
14923
14924              During the lookup for a name preceding the :: scope
14925              resolution operator, object, function, and enumerator
14926              names are ignored.  */
14927           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14928             tag_type = typename_type;
14929           /* Look up the name.  */
14930           decl = cp_parser_lookup_name (parser, identifier,
14931                                         tag_type,
14932                                         /*is_template=*/false,
14933                                         /*is_namespace=*/false,
14934                                         check_dependency_p,
14935                                         &ambiguous_decls,
14936                                         identifier_token->location);
14937           if (ambiguous_decls)
14938             {
14939               error ("%Hreference to %qD is ambiguous",
14940                      &identifier_token->location, identifier);
14941               print_candidates (ambiguous_decls);
14942               if (cp_parser_parsing_tentatively (parser))
14943                 {
14944                   identifier_token->ambiguous_p = true;
14945                   cp_parser_simulate_error (parser);
14946                 }
14947               return error_mark_node;
14948             }
14949         }
14950     }
14951   else
14952     {
14953       /* Try a template-id.  */
14954       decl = cp_parser_template_id (parser, template_keyword_p,
14955                                     check_dependency_p,
14956                                     is_declaration);
14957       if (decl == error_mark_node)
14958         return error_mark_node;
14959     }
14960
14961   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14962
14963   /* If this is a typename, create a TYPENAME_TYPE.  */
14964   if (typename_p && decl != error_mark_node)
14965     {
14966       decl = make_typename_type (scope, decl, typename_type,
14967                                  /*complain=*/tf_error);
14968       if (decl != error_mark_node)
14969         decl = TYPE_NAME (decl);
14970     }
14971
14972   /* Check to see that it is really the name of a class.  */
14973   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14974       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14975       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14976     /* Situations like this:
14977
14978          template <typename T> struct A {
14979            typename T::template X<int>::I i;
14980          };
14981
14982        are problematic.  Is `T::template X<int>' a class-name?  The
14983        standard does not seem to be definitive, but there is no other
14984        valid interpretation of the following `::'.  Therefore, those
14985        names are considered class-names.  */
14986     {
14987       decl = make_typename_type (scope, decl, tag_type, tf_error);
14988       if (decl != error_mark_node)
14989         decl = TYPE_NAME (decl);
14990     }
14991   else if (TREE_CODE (decl) != TYPE_DECL
14992            || TREE_TYPE (decl) == error_mark_node
14993            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14994     decl = error_mark_node;
14995
14996   if (decl == error_mark_node)
14997     cp_parser_error (parser, "expected class-name");
14998   else if (identifier && !parser->scope)
14999     maybe_note_name_used_in_class (identifier, decl);
15000
15001   return decl;
15002 }
15003
15004 /* Parse a class-specifier.
15005
15006    class-specifier:
15007      class-head { member-specification [opt] }
15008
15009    Returns the TREE_TYPE representing the class.  */
15010
15011 static tree
15012 cp_parser_class_specifier (cp_parser* parser)
15013 {
15014   tree type;
15015   tree attributes = NULL_TREE;
15016   bool nested_name_specifier_p;
15017   unsigned saved_num_template_parameter_lists;
15018   bool saved_in_function_body;
15019   bool saved_in_unbraced_linkage_specification_p;
15020   tree old_scope = NULL_TREE;
15021   tree scope = NULL_TREE;
15022   tree bases;
15023
15024   push_deferring_access_checks (dk_no_deferred);
15025
15026   /* Parse the class-head.  */
15027   type = cp_parser_class_head (parser,
15028                                &nested_name_specifier_p,
15029                                &attributes,
15030                                &bases);
15031   /* If the class-head was a semantic disaster, skip the entire body
15032      of the class.  */
15033   if (!type)
15034     {
15035       cp_parser_skip_to_end_of_block_or_statement (parser);
15036       pop_deferring_access_checks ();
15037       return error_mark_node;
15038     }
15039
15040   /* Look for the `{'.  */
15041   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15042     {
15043       pop_deferring_access_checks ();
15044       return error_mark_node;
15045     }
15046
15047   /* Process the base classes. If they're invalid, skip the 
15048      entire class body.  */
15049   if (!xref_basetypes (type, bases))
15050     {
15051       /* Consuming the closing brace yields better error messages
15052          later on.  */
15053       if (cp_parser_skip_to_closing_brace (parser))
15054         cp_lexer_consume_token (parser->lexer);
15055       pop_deferring_access_checks ();
15056       return error_mark_node;
15057     }
15058
15059   /* Issue an error message if type-definitions are forbidden here.  */
15060   cp_parser_check_type_definition (parser);
15061   /* Remember that we are defining one more class.  */
15062   ++parser->num_classes_being_defined;
15063   /* Inside the class, surrounding template-parameter-lists do not
15064      apply.  */
15065   saved_num_template_parameter_lists
15066     = parser->num_template_parameter_lists;
15067   parser->num_template_parameter_lists = 0;
15068   /* We are not in a function body.  */
15069   saved_in_function_body = parser->in_function_body;
15070   parser->in_function_body = false;
15071   /* We are not immediately inside an extern "lang" block.  */
15072   saved_in_unbraced_linkage_specification_p
15073     = parser->in_unbraced_linkage_specification_p;
15074   parser->in_unbraced_linkage_specification_p = false;
15075
15076   /* Start the class.  */
15077   if (nested_name_specifier_p)
15078     {
15079       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15080       old_scope = push_inner_scope (scope);
15081     }
15082   type = begin_class_definition (type, attributes);
15083
15084   if (type == error_mark_node)
15085     /* If the type is erroneous, skip the entire body of the class.  */
15086     cp_parser_skip_to_closing_brace (parser);
15087   else
15088     /* Parse the member-specification.  */
15089     cp_parser_member_specification_opt (parser);
15090
15091   /* Look for the trailing `}'.  */
15092   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15093   /* Look for trailing attributes to apply to this class.  */
15094   if (cp_parser_allow_gnu_extensions_p (parser))
15095     attributes = cp_parser_attributes_opt (parser);
15096   if (type != error_mark_node)
15097     type = finish_struct (type, attributes);
15098   if (nested_name_specifier_p)
15099     pop_inner_scope (old_scope, scope);
15100   /* If this class is not itself within the scope of another class,
15101      then we need to parse the bodies of all of the queued function
15102      definitions.  Note that the queued functions defined in a class
15103      are not always processed immediately following the
15104      class-specifier for that class.  Consider:
15105
15106        struct A {
15107          struct B { void f() { sizeof (A); } };
15108        };
15109
15110      If `f' were processed before the processing of `A' were
15111      completed, there would be no way to compute the size of `A'.
15112      Note that the nesting we are interested in here is lexical --
15113      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15114      for:
15115
15116        struct A { struct B; };
15117        struct A::B { void f() { } };
15118
15119      there is no need to delay the parsing of `A::B::f'.  */
15120   if (--parser->num_classes_being_defined == 0)
15121     {
15122       tree queue_entry;
15123       tree fn;
15124       tree class_type = NULL_TREE;
15125       tree pushed_scope = NULL_TREE;
15126
15127       /* In a first pass, parse default arguments to the functions.
15128          Then, in a second pass, parse the bodies of the functions.
15129          This two-phased approach handles cases like:
15130
15131             struct S {
15132               void f() { g(); }
15133               void g(int i = 3);
15134             };
15135
15136          */
15137       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15138              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15139            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15140            TREE_PURPOSE (parser->unparsed_functions_queues)
15141              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15142         {
15143           fn = TREE_VALUE (queue_entry);
15144           /* If there are default arguments that have not yet been processed,
15145              take care of them now.  */
15146           if (class_type != TREE_PURPOSE (queue_entry))
15147             {
15148               if (pushed_scope)
15149                 pop_scope (pushed_scope);
15150               class_type = TREE_PURPOSE (queue_entry);
15151               pushed_scope = push_scope (class_type);
15152             }
15153           /* Make sure that any template parameters are in scope.  */
15154           maybe_begin_member_template_processing (fn);
15155           /* Parse the default argument expressions.  */
15156           cp_parser_late_parsing_default_args (parser, fn);
15157           /* Remove any template parameters from the symbol table.  */
15158           maybe_end_member_template_processing ();
15159         }
15160       if (pushed_scope)
15161         pop_scope (pushed_scope);
15162       /* Now parse the body of the functions.  */
15163       for (TREE_VALUE (parser->unparsed_functions_queues)
15164              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15165            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15166            TREE_VALUE (parser->unparsed_functions_queues)
15167              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15168         {
15169           /* Figure out which function we need to process.  */
15170           fn = TREE_VALUE (queue_entry);
15171           /* Parse the function.  */
15172           cp_parser_late_parsing_for_member (parser, fn);
15173         }
15174     }
15175
15176   /* Put back any saved access checks.  */
15177   pop_deferring_access_checks ();
15178
15179   /* Restore saved state.  */
15180   parser->in_function_body = saved_in_function_body;
15181   parser->num_template_parameter_lists
15182     = saved_num_template_parameter_lists;
15183   parser->in_unbraced_linkage_specification_p
15184     = saved_in_unbraced_linkage_specification_p;
15185
15186   return type;
15187 }
15188
15189 /* Parse a class-head.
15190
15191    class-head:
15192      class-key identifier [opt] base-clause [opt]
15193      class-key nested-name-specifier identifier base-clause [opt]
15194      class-key nested-name-specifier [opt] template-id
15195        base-clause [opt]
15196
15197    GNU Extensions:
15198      class-key attributes identifier [opt] base-clause [opt]
15199      class-key attributes nested-name-specifier identifier base-clause [opt]
15200      class-key attributes nested-name-specifier [opt] template-id
15201        base-clause [opt]
15202
15203    Upon return BASES is initialized to the list of base classes (or
15204    NULL, if there are none) in the same form returned by
15205    cp_parser_base_clause.
15206
15207    Returns the TYPE of the indicated class.  Sets
15208    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15209    involving a nested-name-specifier was used, and FALSE otherwise.
15210
15211    Returns error_mark_node if this is not a class-head.
15212
15213    Returns NULL_TREE if the class-head is syntactically valid, but
15214    semantically invalid in a way that means we should skip the entire
15215    body of the class.  */
15216
15217 static tree
15218 cp_parser_class_head (cp_parser* parser,
15219                       bool* nested_name_specifier_p,
15220                       tree *attributes_p,
15221                       tree *bases)
15222 {
15223   tree nested_name_specifier;
15224   enum tag_types class_key;
15225   tree id = NULL_TREE;
15226   tree type = NULL_TREE;
15227   tree attributes;
15228   bool template_id_p = false;
15229   bool qualified_p = false;
15230   bool invalid_nested_name_p = false;
15231   bool invalid_explicit_specialization_p = false;
15232   tree pushed_scope = NULL_TREE;
15233   unsigned num_templates;
15234   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15235   /* Assume no nested-name-specifier will be present.  */
15236   *nested_name_specifier_p = false;
15237   /* Assume no template parameter lists will be used in defining the
15238      type.  */
15239   num_templates = 0;
15240
15241   *bases = NULL_TREE;
15242
15243   /* Look for the class-key.  */
15244   class_key = cp_parser_class_key (parser);
15245   if (class_key == none_type)
15246     return error_mark_node;
15247
15248   /* Parse the attributes.  */
15249   attributes = cp_parser_attributes_opt (parser);
15250
15251   /* If the next token is `::', that is invalid -- but sometimes
15252      people do try to write:
15253
15254        struct ::S {};
15255
15256      Handle this gracefully by accepting the extra qualifier, and then
15257      issuing an error about it later if this really is a
15258      class-head.  If it turns out just to be an elaborated type
15259      specifier, remain silent.  */
15260   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15261     qualified_p = true;
15262
15263   push_deferring_access_checks (dk_no_check);
15264
15265   /* Determine the name of the class.  Begin by looking for an
15266      optional nested-name-specifier.  */
15267   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15268   nested_name_specifier
15269     = cp_parser_nested_name_specifier_opt (parser,
15270                                            /*typename_keyword_p=*/false,
15271                                            /*check_dependency_p=*/false,
15272                                            /*type_p=*/false,
15273                                            /*is_declaration=*/false);
15274   /* If there was a nested-name-specifier, then there *must* be an
15275      identifier.  */
15276   if (nested_name_specifier)
15277     {
15278       type_start_token = cp_lexer_peek_token (parser->lexer);
15279       /* Although the grammar says `identifier', it really means
15280          `class-name' or `template-name'.  You are only allowed to
15281          define a class that has already been declared with this
15282          syntax.
15283
15284          The proposed resolution for Core Issue 180 says that wherever
15285          you see `class T::X' you should treat `X' as a type-name.
15286
15287          It is OK to define an inaccessible class; for example:
15288
15289            class A { class B; };
15290            class A::B {};
15291
15292          We do not know if we will see a class-name, or a
15293          template-name.  We look for a class-name first, in case the
15294          class-name is a template-id; if we looked for the
15295          template-name first we would stop after the template-name.  */
15296       cp_parser_parse_tentatively (parser);
15297       type = cp_parser_class_name (parser,
15298                                    /*typename_keyword_p=*/false,
15299                                    /*template_keyword_p=*/false,
15300                                    class_type,
15301                                    /*check_dependency_p=*/false,
15302                                    /*class_head_p=*/true,
15303                                    /*is_declaration=*/false);
15304       /* If that didn't work, ignore the nested-name-specifier.  */
15305       if (!cp_parser_parse_definitely (parser))
15306         {
15307           invalid_nested_name_p = true;
15308           type_start_token = cp_lexer_peek_token (parser->lexer);
15309           id = cp_parser_identifier (parser);
15310           if (id == error_mark_node)
15311             id = NULL_TREE;
15312         }
15313       /* If we could not find a corresponding TYPE, treat this
15314          declaration like an unqualified declaration.  */
15315       if (type == error_mark_node)
15316         nested_name_specifier = NULL_TREE;
15317       /* Otherwise, count the number of templates used in TYPE and its
15318          containing scopes.  */
15319       else
15320         {
15321           tree scope;
15322
15323           for (scope = TREE_TYPE (type);
15324                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15325                scope = (TYPE_P (scope)
15326                         ? TYPE_CONTEXT (scope)
15327                         : DECL_CONTEXT (scope)))
15328             if (TYPE_P (scope)
15329                 && CLASS_TYPE_P (scope)
15330                 && CLASSTYPE_TEMPLATE_INFO (scope)
15331                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15332                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15333               ++num_templates;
15334         }
15335     }
15336   /* Otherwise, the identifier is optional.  */
15337   else
15338     {
15339       /* We don't know whether what comes next is a template-id,
15340          an identifier, or nothing at all.  */
15341       cp_parser_parse_tentatively (parser);
15342       /* Check for a template-id.  */
15343       type_start_token = cp_lexer_peek_token (parser->lexer);
15344       id = cp_parser_template_id (parser,
15345                                   /*template_keyword_p=*/false,
15346                                   /*check_dependency_p=*/true,
15347                                   /*is_declaration=*/true);
15348       /* If that didn't work, it could still be an identifier.  */
15349       if (!cp_parser_parse_definitely (parser))
15350         {
15351           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15352             {
15353               type_start_token = cp_lexer_peek_token (parser->lexer);
15354               id = cp_parser_identifier (parser);
15355             }
15356           else
15357             id = NULL_TREE;
15358         }
15359       else
15360         {
15361           template_id_p = true;
15362           ++num_templates;
15363         }
15364     }
15365
15366   pop_deferring_access_checks ();
15367
15368   if (id)
15369     cp_parser_check_for_invalid_template_id (parser, id,
15370                                              type_start_token->location);
15371
15372   /* If it's not a `:' or a `{' then we can't really be looking at a
15373      class-head, since a class-head only appears as part of a
15374      class-specifier.  We have to detect this situation before calling
15375      xref_tag, since that has irreversible side-effects.  */
15376   if (!cp_parser_next_token_starts_class_definition_p (parser))
15377     {
15378       cp_parser_error (parser, "expected %<{%> or %<:%>");
15379       return error_mark_node;
15380     }
15381
15382   /* At this point, we're going ahead with the class-specifier, even
15383      if some other problem occurs.  */
15384   cp_parser_commit_to_tentative_parse (parser);
15385   /* Issue the error about the overly-qualified name now.  */
15386   if (qualified_p)
15387     {
15388       cp_parser_error (parser,
15389                        "global qualification of class name is invalid");
15390       return error_mark_node;
15391     }
15392   else if (invalid_nested_name_p)
15393     {
15394       cp_parser_error (parser,
15395                        "qualified name does not name a class");
15396       return error_mark_node;
15397     }
15398   else if (nested_name_specifier)
15399     {
15400       tree scope;
15401
15402       /* Reject typedef-names in class heads.  */
15403       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15404         {
15405           error ("%Hinvalid class name in declaration of %qD",
15406                  &type_start_token->location, type);
15407           type = NULL_TREE;
15408           goto done;
15409         }
15410
15411       /* Figure out in what scope the declaration is being placed.  */
15412       scope = current_scope ();
15413       /* If that scope does not contain the scope in which the
15414          class was originally declared, the program is invalid.  */
15415       if (scope && !is_ancestor (scope, nested_name_specifier))
15416         {
15417           if (at_namespace_scope_p ())
15418             error ("%Hdeclaration of %qD in namespace %qD which does not "
15419                    "enclose %qD",
15420                    &type_start_token->location,
15421                    type, scope, nested_name_specifier);
15422           else
15423             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15424                    &type_start_token->location,
15425                    type, scope, nested_name_specifier);
15426           type = NULL_TREE;
15427           goto done;
15428         }
15429       /* [dcl.meaning]
15430
15431          A declarator-id shall not be qualified except for the
15432          definition of a ... nested class outside of its class
15433          ... [or] the definition or explicit instantiation of a
15434          class member of a namespace outside of its namespace.  */
15435       if (scope == nested_name_specifier)
15436         {
15437           permerror (input_location, "%Hextra qualification not allowed",
15438                      &nested_name_specifier_token_start->location);
15439           nested_name_specifier = NULL_TREE;
15440           num_templates = 0;
15441         }
15442     }
15443   /* An explicit-specialization must be preceded by "template <>".  If
15444      it is not, try to recover gracefully.  */
15445   if (at_namespace_scope_p ()
15446       && parser->num_template_parameter_lists == 0
15447       && template_id_p)
15448     {
15449       error ("%Han explicit specialization must be preceded by %<template <>%>",
15450              &type_start_token->location);
15451       invalid_explicit_specialization_p = true;
15452       /* Take the same action that would have been taken by
15453          cp_parser_explicit_specialization.  */
15454       ++parser->num_template_parameter_lists;
15455       begin_specialization ();
15456     }
15457   /* There must be no "return" statements between this point and the
15458      end of this function; set "type "to the correct return value and
15459      use "goto done;" to return.  */
15460   /* Make sure that the right number of template parameters were
15461      present.  */
15462   if (!cp_parser_check_template_parameters (parser, num_templates,
15463                                             type_start_token->location,
15464                                             /*declarator=*/NULL))
15465     {
15466       /* If something went wrong, there is no point in even trying to
15467          process the class-definition.  */
15468       type = NULL_TREE;
15469       goto done;
15470     }
15471
15472   /* Look up the type.  */
15473   if (template_id_p)
15474     {
15475       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15476           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15477               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15478         {
15479           error ("%Hfunction template %qD redeclared as a class template",
15480                  &type_start_token->location, id);
15481           type = error_mark_node;
15482         }
15483       else
15484         {
15485           type = TREE_TYPE (id);
15486           type = maybe_process_partial_specialization (type);
15487         }
15488       if (nested_name_specifier)
15489         pushed_scope = push_scope (nested_name_specifier);
15490     }
15491   else if (nested_name_specifier)
15492     {
15493       tree class_type;
15494
15495       /* Given:
15496
15497             template <typename T> struct S { struct T };
15498             template <typename T> struct S<T>::T { };
15499
15500          we will get a TYPENAME_TYPE when processing the definition of
15501          `S::T'.  We need to resolve it to the actual type before we
15502          try to define it.  */
15503       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15504         {
15505           class_type = resolve_typename_type (TREE_TYPE (type),
15506                                               /*only_current_p=*/false);
15507           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15508             type = TYPE_NAME (class_type);
15509           else
15510             {
15511               cp_parser_error (parser, "could not resolve typename type");
15512               type = error_mark_node;
15513             }
15514         }
15515
15516       if (maybe_process_partial_specialization (TREE_TYPE (type))
15517           == error_mark_node)
15518         {
15519           type = NULL_TREE;
15520           goto done;
15521         }
15522
15523       class_type = current_class_type;
15524       /* Enter the scope indicated by the nested-name-specifier.  */
15525       pushed_scope = push_scope (nested_name_specifier);
15526       /* Get the canonical version of this type.  */
15527       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15528       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15529           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15530         {
15531           type = push_template_decl (type);
15532           if (type == error_mark_node)
15533             {
15534               type = NULL_TREE;
15535               goto done;
15536             }
15537         }
15538
15539       type = TREE_TYPE (type);
15540       *nested_name_specifier_p = true;
15541     }
15542   else      /* The name is not a nested name.  */
15543     {
15544       /* If the class was unnamed, create a dummy name.  */
15545       if (!id)
15546         id = make_anon_name ();
15547       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15548                        parser->num_template_parameter_lists);
15549     }
15550
15551   /* Indicate whether this class was declared as a `class' or as a
15552      `struct'.  */
15553   if (TREE_CODE (type) == RECORD_TYPE)
15554     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15555   cp_parser_check_class_key (class_key, type);
15556
15557   /* If this type was already complete, and we see another definition,
15558      that's an error.  */
15559   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15560     {
15561       error ("%Hredefinition of %q#T",
15562              &type_start_token->location, type);
15563       error ("%Hprevious definition of %q+#T",
15564              &type_start_token->location, type);
15565       type = NULL_TREE;
15566       goto done;
15567     }
15568   else if (type == error_mark_node)
15569     type = NULL_TREE;
15570
15571   /* We will have entered the scope containing the class; the names of
15572      base classes should be looked up in that context.  For example:
15573
15574        struct A { struct B {}; struct C; };
15575        struct A::C : B {};
15576
15577      is valid.  */
15578
15579   /* Get the list of base-classes, if there is one.  */
15580   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15581     *bases = cp_parser_base_clause (parser);
15582
15583  done:
15584   /* Leave the scope given by the nested-name-specifier.  We will
15585      enter the class scope itself while processing the members.  */
15586   if (pushed_scope)
15587     pop_scope (pushed_scope);
15588
15589   if (invalid_explicit_specialization_p)
15590     {
15591       end_specialization ();
15592       --parser->num_template_parameter_lists;
15593     }
15594   *attributes_p = attributes;
15595   return type;
15596 }
15597
15598 /* Parse a class-key.
15599
15600    class-key:
15601      class
15602      struct
15603      union
15604
15605    Returns the kind of class-key specified, or none_type to indicate
15606    error.  */
15607
15608 static enum tag_types
15609 cp_parser_class_key (cp_parser* parser)
15610 {
15611   cp_token *token;
15612   enum tag_types tag_type;
15613
15614   /* Look for the class-key.  */
15615   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15616   if (!token)
15617     return none_type;
15618
15619   /* Check to see if the TOKEN is a class-key.  */
15620   tag_type = cp_parser_token_is_class_key (token);
15621   if (!tag_type)
15622     cp_parser_error (parser, "expected class-key");
15623   return tag_type;
15624 }
15625
15626 /* Parse an (optional) member-specification.
15627
15628    member-specification:
15629      member-declaration member-specification [opt]
15630      access-specifier : member-specification [opt]  */
15631
15632 static void
15633 cp_parser_member_specification_opt (cp_parser* parser)
15634 {
15635   while (true)
15636     {
15637       cp_token *token;
15638       enum rid keyword;
15639
15640       /* Peek at the next token.  */
15641       token = cp_lexer_peek_token (parser->lexer);
15642       /* If it's a `}', or EOF then we've seen all the members.  */
15643       if (token->type == CPP_CLOSE_BRACE
15644           || token->type == CPP_EOF
15645           || token->type == CPP_PRAGMA_EOL)
15646         break;
15647
15648       /* See if this token is a keyword.  */
15649       keyword = token->keyword;
15650       switch (keyword)
15651         {
15652         case RID_PUBLIC:
15653         case RID_PROTECTED:
15654         case RID_PRIVATE:
15655           /* Consume the access-specifier.  */
15656           cp_lexer_consume_token (parser->lexer);
15657           /* Remember which access-specifier is active.  */
15658           current_access_specifier = token->u.value;
15659           /* Look for the `:'.  */
15660           cp_parser_require (parser, CPP_COLON, "%<:%>");
15661           break;
15662
15663         default:
15664           /* Accept #pragmas at class scope.  */
15665           if (token->type == CPP_PRAGMA)
15666             {
15667               cp_parser_pragma (parser, pragma_external);
15668               break;
15669             }
15670
15671           /* Otherwise, the next construction must be a
15672              member-declaration.  */
15673           cp_parser_member_declaration (parser);
15674         }
15675     }
15676 }
15677
15678 /* Parse a member-declaration.
15679
15680    member-declaration:
15681      decl-specifier-seq [opt] member-declarator-list [opt] ;
15682      function-definition ; [opt]
15683      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15684      using-declaration
15685      template-declaration
15686
15687    member-declarator-list:
15688      member-declarator
15689      member-declarator-list , member-declarator
15690
15691    member-declarator:
15692      declarator pure-specifier [opt]
15693      declarator constant-initializer [opt]
15694      identifier [opt] : constant-expression
15695
15696    GNU Extensions:
15697
15698    member-declaration:
15699      __extension__ member-declaration
15700
15701    member-declarator:
15702      declarator attributes [opt] pure-specifier [opt]
15703      declarator attributes [opt] constant-initializer [opt]
15704      identifier [opt] attributes [opt] : constant-expression  
15705
15706    C++0x Extensions:
15707
15708    member-declaration:
15709      static_assert-declaration  */
15710
15711 static void
15712 cp_parser_member_declaration (cp_parser* parser)
15713 {
15714   cp_decl_specifier_seq decl_specifiers;
15715   tree prefix_attributes;
15716   tree decl;
15717   int declares_class_or_enum;
15718   bool friend_p;
15719   cp_token *token = NULL;
15720   cp_token *decl_spec_token_start = NULL;
15721   cp_token *initializer_token_start = NULL;
15722   int saved_pedantic;
15723
15724   /* Check for the `__extension__' keyword.  */
15725   if (cp_parser_extension_opt (parser, &saved_pedantic))
15726     {
15727       /* Recurse.  */
15728       cp_parser_member_declaration (parser);
15729       /* Restore the old value of the PEDANTIC flag.  */
15730       pedantic = saved_pedantic;
15731
15732       return;
15733     }
15734
15735   /* Check for a template-declaration.  */
15736   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15737     {
15738       /* An explicit specialization here is an error condition, and we
15739          expect the specialization handler to detect and report this.  */
15740       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15741           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15742         cp_parser_explicit_specialization (parser);
15743       else
15744         cp_parser_template_declaration (parser, /*member_p=*/true);
15745
15746       return;
15747     }
15748
15749   /* Check for a using-declaration.  */
15750   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15751     {
15752       /* Parse the using-declaration.  */
15753       cp_parser_using_declaration (parser,
15754                                    /*access_declaration_p=*/false);
15755       return;
15756     }
15757
15758   /* Check for @defs.  */
15759   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15760     {
15761       tree ivar, member;
15762       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15763       ivar = ivar_chains;
15764       while (ivar)
15765         {
15766           member = ivar;
15767           ivar = TREE_CHAIN (member);
15768           TREE_CHAIN (member) = NULL_TREE;
15769           finish_member_declaration (member);
15770         }
15771       return;
15772     }
15773
15774   /* If the next token is `static_assert' we have a static assertion.  */
15775   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15776     {
15777       cp_parser_static_assert (parser, /*member_p=*/true);
15778       return;
15779     }
15780
15781   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15782     return;
15783
15784   /* Parse the decl-specifier-seq.  */
15785   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15786   cp_parser_decl_specifier_seq (parser,
15787                                 CP_PARSER_FLAGS_OPTIONAL,
15788                                 &decl_specifiers,
15789                                 &declares_class_or_enum);
15790   prefix_attributes = decl_specifiers.attributes;
15791   decl_specifiers.attributes = NULL_TREE;
15792   /* Check for an invalid type-name.  */
15793   if (!decl_specifiers.type
15794       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15795     return;
15796   /* If there is no declarator, then the decl-specifier-seq should
15797      specify a type.  */
15798   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15799     {
15800       /* If there was no decl-specifier-seq, and the next token is a
15801          `;', then we have something like:
15802
15803            struct S { ; };
15804
15805          [class.mem]
15806
15807          Each member-declaration shall declare at least one member
15808          name of the class.  */
15809       if (!decl_specifiers.any_specifiers_p)
15810         {
15811           cp_token *token = cp_lexer_peek_token (parser->lexer);
15812           if (!in_system_header_at (token->location))
15813             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15814         }
15815       else
15816         {
15817           tree type;
15818
15819           /* See if this declaration is a friend.  */
15820           friend_p = cp_parser_friend_p (&decl_specifiers);
15821           /* If there were decl-specifiers, check to see if there was
15822              a class-declaration.  */
15823           type = check_tag_decl (&decl_specifiers);
15824           /* Nested classes have already been added to the class, but
15825              a `friend' needs to be explicitly registered.  */
15826           if (friend_p)
15827             {
15828               /* If the `friend' keyword was present, the friend must
15829                  be introduced with a class-key.  */
15830                if (!declares_class_or_enum)
15831                  error ("%Ha class-key must be used when declaring a friend",
15832                         &decl_spec_token_start->location);
15833                /* In this case:
15834
15835                     template <typename T> struct A {
15836                       friend struct A<T>::B;
15837                     };
15838
15839                   A<T>::B will be represented by a TYPENAME_TYPE, and
15840                   therefore not recognized by check_tag_decl.  */
15841                if (!type
15842                    && decl_specifiers.type
15843                    && TYPE_P (decl_specifiers.type))
15844                  type = decl_specifiers.type;
15845                if (!type || !TYPE_P (type))
15846                  error ("%Hfriend declaration does not name a class or "
15847                         "function", &decl_spec_token_start->location);
15848                else
15849                  make_friend_class (current_class_type, type,
15850                                     /*complain=*/true);
15851             }
15852           /* If there is no TYPE, an error message will already have
15853              been issued.  */
15854           else if (!type || type == error_mark_node)
15855             ;
15856           /* An anonymous aggregate has to be handled specially; such
15857              a declaration really declares a data member (with a
15858              particular type), as opposed to a nested class.  */
15859           else if (ANON_AGGR_TYPE_P (type))
15860             {
15861               /* Remove constructors and such from TYPE, now that we
15862                  know it is an anonymous aggregate.  */
15863               fixup_anonymous_aggr (type);
15864               /* And make the corresponding data member.  */
15865               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15866               /* Add it to the class.  */
15867               finish_member_declaration (decl);
15868             }
15869           else
15870             cp_parser_check_access_in_redeclaration
15871                                               (TYPE_NAME (type),
15872                                                decl_spec_token_start->location);
15873         }
15874     }
15875   else
15876     {
15877       /* See if these declarations will be friends.  */
15878       friend_p = cp_parser_friend_p (&decl_specifiers);
15879
15880       /* Keep going until we hit the `;' at the end of the
15881          declaration.  */
15882       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15883         {
15884           tree attributes = NULL_TREE;
15885           tree first_attribute;
15886
15887           /* Peek at the next token.  */
15888           token = cp_lexer_peek_token (parser->lexer);
15889
15890           /* Check for a bitfield declaration.  */
15891           if (token->type == CPP_COLON
15892               || (token->type == CPP_NAME
15893                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15894                   == CPP_COLON))
15895             {
15896               tree identifier;
15897               tree width;
15898
15899               /* Get the name of the bitfield.  Note that we cannot just
15900                  check TOKEN here because it may have been invalidated by
15901                  the call to cp_lexer_peek_nth_token above.  */
15902               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15903                 identifier = cp_parser_identifier (parser);
15904               else
15905                 identifier = NULL_TREE;
15906
15907               /* Consume the `:' token.  */
15908               cp_lexer_consume_token (parser->lexer);
15909               /* Get the width of the bitfield.  */
15910               width
15911                 = cp_parser_constant_expression (parser,
15912                                                  /*allow_non_constant=*/false,
15913                                                  NULL);
15914
15915               /* Look for attributes that apply to the bitfield.  */
15916               attributes = cp_parser_attributes_opt (parser);
15917               /* Remember which attributes are prefix attributes and
15918                  which are not.  */
15919               first_attribute = attributes;
15920               /* Combine the attributes.  */
15921               attributes = chainon (prefix_attributes, attributes);
15922
15923               /* Create the bitfield declaration.  */
15924               decl = grokbitfield (identifier
15925                                    ? make_id_declarator (NULL_TREE,
15926                                                          identifier,
15927                                                          sfk_none)
15928                                    : NULL,
15929                                    &decl_specifiers,
15930                                    width,
15931                                    attributes);
15932             }
15933           else
15934             {
15935               cp_declarator *declarator;
15936               tree initializer;
15937               tree asm_specification;
15938               int ctor_dtor_or_conv_p;
15939
15940               /* Parse the declarator.  */
15941               declarator
15942                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15943                                         &ctor_dtor_or_conv_p,
15944                                         /*parenthesized_p=*/NULL,
15945                                         /*member_p=*/true);
15946
15947               /* If something went wrong parsing the declarator, make sure
15948                  that we at least consume some tokens.  */
15949               if (declarator == cp_error_declarator)
15950                 {
15951                   /* Skip to the end of the statement.  */
15952                   cp_parser_skip_to_end_of_statement (parser);
15953                   /* If the next token is not a semicolon, that is
15954                      probably because we just skipped over the body of
15955                      a function.  So, we consume a semicolon if
15956                      present, but do not issue an error message if it
15957                      is not present.  */
15958                   if (cp_lexer_next_token_is (parser->lexer,
15959                                               CPP_SEMICOLON))
15960                     cp_lexer_consume_token (parser->lexer);
15961                   return;
15962                 }
15963
15964               if (declares_class_or_enum & 2)
15965                 cp_parser_check_for_definition_in_return_type
15966                                             (declarator, decl_specifiers.type,
15967                                              decl_specifiers.type_location);
15968
15969               /* Look for an asm-specification.  */
15970               asm_specification = cp_parser_asm_specification_opt (parser);
15971               /* Look for attributes that apply to the declaration.  */
15972               attributes = cp_parser_attributes_opt (parser);
15973               /* Remember which attributes are prefix attributes and
15974                  which are not.  */
15975               first_attribute = attributes;
15976               /* Combine the attributes.  */
15977               attributes = chainon (prefix_attributes, attributes);
15978
15979               /* If it's an `=', then we have a constant-initializer or a
15980                  pure-specifier.  It is not correct to parse the
15981                  initializer before registering the member declaration
15982                  since the member declaration should be in scope while
15983                  its initializer is processed.  However, the rest of the
15984                  front end does not yet provide an interface that allows
15985                  us to handle this correctly.  */
15986               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15987                 {
15988                   /* In [class.mem]:
15989
15990                      A pure-specifier shall be used only in the declaration of
15991                      a virtual function.
15992
15993                      A member-declarator can contain a constant-initializer
15994                      only if it declares a static member of integral or
15995                      enumeration type.
15996
15997                      Therefore, if the DECLARATOR is for a function, we look
15998                      for a pure-specifier; otherwise, we look for a
15999                      constant-initializer.  When we call `grokfield', it will
16000                      perform more stringent semantics checks.  */
16001                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16002                   if (function_declarator_p (declarator))
16003                     initializer = cp_parser_pure_specifier (parser);
16004                   else
16005                     /* Parse the initializer.  */
16006                     initializer = cp_parser_constant_initializer (parser);
16007                 }
16008               /* Otherwise, there is no initializer.  */
16009               else
16010                 initializer = NULL_TREE;
16011
16012               /* See if we are probably looking at a function
16013                  definition.  We are certainly not looking at a
16014                  member-declarator.  Calling `grokfield' has
16015                  side-effects, so we must not do it unless we are sure
16016                  that we are looking at a member-declarator.  */
16017               if (cp_parser_token_starts_function_definition_p
16018                   (cp_lexer_peek_token (parser->lexer)))
16019                 {
16020                   /* The grammar does not allow a pure-specifier to be
16021                      used when a member function is defined.  (It is
16022                      possible that this fact is an oversight in the
16023                      standard, since a pure function may be defined
16024                      outside of the class-specifier.  */
16025                   if (initializer)
16026                     error ("%Hpure-specifier on function-definition",
16027                            &initializer_token_start->location);
16028                   decl = cp_parser_save_member_function_body (parser,
16029                                                               &decl_specifiers,
16030                                                               declarator,
16031                                                               attributes);
16032                   /* If the member was not a friend, declare it here.  */
16033                   if (!friend_p)
16034                     finish_member_declaration (decl);
16035                   /* Peek at the next token.  */
16036                   token = cp_lexer_peek_token (parser->lexer);
16037                   /* If the next token is a semicolon, consume it.  */
16038                   if (token->type == CPP_SEMICOLON)
16039                     cp_lexer_consume_token (parser->lexer);
16040                   return;
16041                 }
16042               else
16043                 if (declarator->kind == cdk_function)
16044                   declarator->id_loc = token->location;
16045                 /* Create the declaration.  */
16046                 decl = grokfield (declarator, &decl_specifiers,
16047                                   initializer, /*init_const_expr_p=*/true,
16048                                   asm_specification,
16049                                   attributes);
16050             }
16051
16052           /* Reset PREFIX_ATTRIBUTES.  */
16053           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16054             attributes = TREE_CHAIN (attributes);
16055           if (attributes)
16056             TREE_CHAIN (attributes) = NULL_TREE;
16057
16058           /* If there is any qualification still in effect, clear it
16059              now; we will be starting fresh with the next declarator.  */
16060           parser->scope = NULL_TREE;
16061           parser->qualifying_scope = NULL_TREE;
16062           parser->object_scope = NULL_TREE;
16063           /* If it's a `,', then there are more declarators.  */
16064           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16065             cp_lexer_consume_token (parser->lexer);
16066           /* If the next token isn't a `;', then we have a parse error.  */
16067           else if (cp_lexer_next_token_is_not (parser->lexer,
16068                                                CPP_SEMICOLON))
16069             {
16070               cp_parser_error (parser, "expected %<;%>");
16071               /* Skip tokens until we find a `;'.  */
16072               cp_parser_skip_to_end_of_statement (parser);
16073
16074               break;
16075             }
16076
16077           if (decl)
16078             {
16079               /* Add DECL to the list of members.  */
16080               if (!friend_p)
16081                 finish_member_declaration (decl);
16082
16083               if (TREE_CODE (decl) == FUNCTION_DECL)
16084                 cp_parser_save_default_args (parser, decl);
16085             }
16086         }
16087     }
16088
16089   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16090 }
16091
16092 /* Parse a pure-specifier.
16093
16094    pure-specifier:
16095      = 0
16096
16097    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16098    Otherwise, ERROR_MARK_NODE is returned.  */
16099
16100 static tree
16101 cp_parser_pure_specifier (cp_parser* parser)
16102 {
16103   cp_token *token;
16104
16105   /* Look for the `=' token.  */
16106   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16107     return error_mark_node;
16108   /* Look for the `0' token.  */
16109   token = cp_lexer_peek_token (parser->lexer);
16110
16111   if (token->type == CPP_EOF
16112       || token->type == CPP_PRAGMA_EOL)
16113     return error_mark_node;
16114
16115   cp_lexer_consume_token (parser->lexer);
16116
16117   /* Accept = default or = delete in c++0x mode.  */
16118   if (token->keyword == RID_DEFAULT
16119       || token->keyword == RID_DELETE)
16120     {
16121       maybe_warn_cpp0x ("defaulted and deleted functions");
16122       return token->u.value;
16123     }
16124
16125   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16126   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16127     {
16128       cp_parser_error (parser,
16129                        "invalid pure specifier (only %<= 0%> is allowed)");
16130       cp_parser_skip_to_end_of_statement (parser);
16131       return error_mark_node;
16132     }
16133   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16134     {
16135       error ("%Htemplates may not be %<virtual%>", &token->location);
16136       return error_mark_node;
16137     }
16138
16139   return integer_zero_node;
16140 }
16141
16142 /* Parse a constant-initializer.
16143
16144    constant-initializer:
16145      = constant-expression
16146
16147    Returns a representation of the constant-expression.  */
16148
16149 static tree
16150 cp_parser_constant_initializer (cp_parser* parser)
16151 {
16152   /* Look for the `=' token.  */
16153   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16154     return error_mark_node;
16155
16156   /* It is invalid to write:
16157
16158        struct S { static const int i = { 7 }; };
16159
16160      */
16161   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16162     {
16163       cp_parser_error (parser,
16164                        "a brace-enclosed initializer is not allowed here");
16165       /* Consume the opening brace.  */
16166       cp_lexer_consume_token (parser->lexer);
16167       /* Skip the initializer.  */
16168       cp_parser_skip_to_closing_brace (parser);
16169       /* Look for the trailing `}'.  */
16170       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16171
16172       return error_mark_node;
16173     }
16174
16175   return cp_parser_constant_expression (parser,
16176                                         /*allow_non_constant=*/false,
16177                                         NULL);
16178 }
16179
16180 /* Derived classes [gram.class.derived] */
16181
16182 /* Parse a base-clause.
16183
16184    base-clause:
16185      : base-specifier-list
16186
16187    base-specifier-list:
16188      base-specifier ... [opt]
16189      base-specifier-list , base-specifier ... [opt]
16190
16191    Returns a TREE_LIST representing the base-classes, in the order in
16192    which they were declared.  The representation of each node is as
16193    described by cp_parser_base_specifier.
16194
16195    In the case that no bases are specified, this function will return
16196    NULL_TREE, not ERROR_MARK_NODE.  */
16197
16198 static tree
16199 cp_parser_base_clause (cp_parser* parser)
16200 {
16201   tree bases = NULL_TREE;
16202
16203   /* Look for the `:' that begins the list.  */
16204   cp_parser_require (parser, CPP_COLON, "%<:%>");
16205
16206   /* Scan the base-specifier-list.  */
16207   while (true)
16208     {
16209       cp_token *token;
16210       tree base;
16211       bool pack_expansion_p = false;
16212
16213       /* Look for the base-specifier.  */
16214       base = cp_parser_base_specifier (parser);
16215       /* Look for the (optional) ellipsis. */
16216       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16217         {
16218           /* Consume the `...'. */
16219           cp_lexer_consume_token (parser->lexer);
16220
16221           pack_expansion_p = true;
16222         }
16223
16224       /* Add BASE to the front of the list.  */
16225       if (base != error_mark_node)
16226         {
16227           if (pack_expansion_p)
16228             /* Make this a pack expansion type. */
16229             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16230           
16231
16232           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16233             {
16234               TREE_CHAIN (base) = bases;
16235               bases = base;
16236             }
16237         }
16238       /* Peek at the next token.  */
16239       token = cp_lexer_peek_token (parser->lexer);
16240       /* If it's not a comma, then the list is complete.  */
16241       if (token->type != CPP_COMMA)
16242         break;
16243       /* Consume the `,'.  */
16244       cp_lexer_consume_token (parser->lexer);
16245     }
16246
16247   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16248      base class had a qualified name.  However, the next name that
16249      appears is certainly not qualified.  */
16250   parser->scope = NULL_TREE;
16251   parser->qualifying_scope = NULL_TREE;
16252   parser->object_scope = NULL_TREE;
16253
16254   return nreverse (bases);
16255 }
16256
16257 /* Parse a base-specifier.
16258
16259    base-specifier:
16260      :: [opt] nested-name-specifier [opt] class-name
16261      virtual access-specifier [opt] :: [opt] nested-name-specifier
16262        [opt] class-name
16263      access-specifier virtual [opt] :: [opt] nested-name-specifier
16264        [opt] class-name
16265
16266    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16267    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16268    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16269    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16270
16271 static tree
16272 cp_parser_base_specifier (cp_parser* parser)
16273 {
16274   cp_token *token;
16275   bool done = false;
16276   bool virtual_p = false;
16277   bool duplicate_virtual_error_issued_p = false;
16278   bool duplicate_access_error_issued_p = false;
16279   bool class_scope_p, template_p;
16280   tree access = access_default_node;
16281   tree type;
16282
16283   /* Process the optional `virtual' and `access-specifier'.  */
16284   while (!done)
16285     {
16286       /* Peek at the next token.  */
16287       token = cp_lexer_peek_token (parser->lexer);
16288       /* Process `virtual'.  */
16289       switch (token->keyword)
16290         {
16291         case RID_VIRTUAL:
16292           /* If `virtual' appears more than once, issue an error.  */
16293           if (virtual_p && !duplicate_virtual_error_issued_p)
16294             {
16295               cp_parser_error (parser,
16296                                "%<virtual%> specified more than once in base-specified");
16297               duplicate_virtual_error_issued_p = true;
16298             }
16299
16300           virtual_p = true;
16301
16302           /* Consume the `virtual' token.  */
16303           cp_lexer_consume_token (parser->lexer);
16304
16305           break;
16306
16307         case RID_PUBLIC:
16308         case RID_PROTECTED:
16309         case RID_PRIVATE:
16310           /* If more than one access specifier appears, issue an
16311              error.  */
16312           if (access != access_default_node
16313               && !duplicate_access_error_issued_p)
16314             {
16315               cp_parser_error (parser,
16316                                "more than one access specifier in base-specified");
16317               duplicate_access_error_issued_p = true;
16318             }
16319
16320           access = ridpointers[(int) token->keyword];
16321
16322           /* Consume the access-specifier.  */
16323           cp_lexer_consume_token (parser->lexer);
16324
16325           break;
16326
16327         default:
16328           done = true;
16329           break;
16330         }
16331     }
16332   /* It is not uncommon to see programs mechanically, erroneously, use
16333      the 'typename' keyword to denote (dependent) qualified types
16334      as base classes.  */
16335   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16336     {
16337       token = cp_lexer_peek_token (parser->lexer);
16338       if (!processing_template_decl)
16339         error ("%Hkeyword %<typename%> not allowed outside of templates",
16340                &token->location);
16341       else
16342         error ("%Hkeyword %<typename%> not allowed in this context "
16343                "(the base class is implicitly a type)",
16344                &token->location);
16345       cp_lexer_consume_token (parser->lexer);
16346     }
16347
16348   /* Look for the optional `::' operator.  */
16349   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16350   /* Look for the nested-name-specifier.  The simplest way to
16351      implement:
16352
16353        [temp.res]
16354
16355        The keyword `typename' is not permitted in a base-specifier or
16356        mem-initializer; in these contexts a qualified name that
16357        depends on a template-parameter is implicitly assumed to be a
16358        type name.
16359
16360      is to pretend that we have seen the `typename' keyword at this
16361      point.  */
16362   cp_parser_nested_name_specifier_opt (parser,
16363                                        /*typename_keyword_p=*/true,
16364                                        /*check_dependency_p=*/true,
16365                                        typename_type,
16366                                        /*is_declaration=*/true);
16367   /* If the base class is given by a qualified name, assume that names
16368      we see are type names or templates, as appropriate.  */
16369   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16370   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16371
16372   /* Finally, look for the class-name.  */
16373   type = cp_parser_class_name (parser,
16374                                class_scope_p,
16375                                template_p,
16376                                typename_type,
16377                                /*check_dependency_p=*/true,
16378                                /*class_head_p=*/false,
16379                                /*is_declaration=*/true);
16380
16381   if (type == error_mark_node)
16382     return error_mark_node;
16383
16384   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16385 }
16386
16387 /* Exception handling [gram.exception] */
16388
16389 /* Parse an (optional) exception-specification.
16390
16391    exception-specification:
16392      throw ( type-id-list [opt] )
16393
16394    Returns a TREE_LIST representing the exception-specification.  The
16395    TREE_VALUE of each node is a type.  */
16396
16397 static tree
16398 cp_parser_exception_specification_opt (cp_parser* parser)
16399 {
16400   cp_token *token;
16401   tree type_id_list;
16402
16403   /* Peek at the next token.  */
16404   token = cp_lexer_peek_token (parser->lexer);
16405   /* If it's not `throw', then there's no exception-specification.  */
16406   if (!cp_parser_is_keyword (token, RID_THROW))
16407     return NULL_TREE;
16408
16409   /* Consume the `throw'.  */
16410   cp_lexer_consume_token (parser->lexer);
16411
16412   /* Look for the `('.  */
16413   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16414
16415   /* Peek at the next token.  */
16416   token = cp_lexer_peek_token (parser->lexer);
16417   /* If it's not a `)', then there is a type-id-list.  */
16418   if (token->type != CPP_CLOSE_PAREN)
16419     {
16420       const char *saved_message;
16421
16422       /* Types may not be defined in an exception-specification.  */
16423       saved_message = parser->type_definition_forbidden_message;
16424       parser->type_definition_forbidden_message
16425         = "types may not be defined in an exception-specification";
16426       /* Parse the type-id-list.  */
16427       type_id_list = cp_parser_type_id_list (parser);
16428       /* Restore the saved message.  */
16429       parser->type_definition_forbidden_message = saved_message;
16430     }
16431   else
16432     type_id_list = empty_except_spec;
16433
16434   /* Look for the `)'.  */
16435   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16436
16437   return type_id_list;
16438 }
16439
16440 /* Parse an (optional) type-id-list.
16441
16442    type-id-list:
16443      type-id ... [opt]
16444      type-id-list , type-id ... [opt]
16445
16446    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16447    in the order that the types were presented.  */
16448
16449 static tree
16450 cp_parser_type_id_list (cp_parser* parser)
16451 {
16452   tree types = NULL_TREE;
16453
16454   while (true)
16455     {
16456       cp_token *token;
16457       tree type;
16458
16459       /* Get the next type-id.  */
16460       type = cp_parser_type_id (parser);
16461       /* Parse the optional ellipsis. */
16462       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16463         {
16464           /* Consume the `...'. */
16465           cp_lexer_consume_token (parser->lexer);
16466
16467           /* Turn the type into a pack expansion expression. */
16468           type = make_pack_expansion (type);
16469         }
16470       /* Add it to the list.  */
16471       types = add_exception_specifier (types, type, /*complain=*/1);
16472       /* Peek at the next token.  */
16473       token = cp_lexer_peek_token (parser->lexer);
16474       /* If it is not a `,', we are done.  */
16475       if (token->type != CPP_COMMA)
16476         break;
16477       /* Consume the `,'.  */
16478       cp_lexer_consume_token (parser->lexer);
16479     }
16480
16481   return nreverse (types);
16482 }
16483
16484 /* Parse a try-block.
16485
16486    try-block:
16487      try compound-statement handler-seq  */
16488
16489 static tree
16490 cp_parser_try_block (cp_parser* parser)
16491 {
16492   tree try_block;
16493
16494   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16495   try_block = begin_try_block ();
16496   cp_parser_compound_statement (parser, NULL, true);
16497   finish_try_block (try_block);
16498   cp_parser_handler_seq (parser);
16499   finish_handler_sequence (try_block);
16500
16501   return try_block;
16502 }
16503
16504 /* Parse a function-try-block.
16505
16506    function-try-block:
16507      try ctor-initializer [opt] function-body handler-seq  */
16508
16509 static bool
16510 cp_parser_function_try_block (cp_parser* parser)
16511 {
16512   tree compound_stmt;
16513   tree try_block;
16514   bool ctor_initializer_p;
16515
16516   /* Look for the `try' keyword.  */
16517   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16518     return false;
16519   /* Let the rest of the front end know where we are.  */
16520   try_block = begin_function_try_block (&compound_stmt);
16521   /* Parse the function-body.  */
16522   ctor_initializer_p
16523     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16524   /* We're done with the `try' part.  */
16525   finish_function_try_block (try_block);
16526   /* Parse the handlers.  */
16527   cp_parser_handler_seq (parser);
16528   /* We're done with the handlers.  */
16529   finish_function_handler_sequence (try_block, compound_stmt);
16530
16531   return ctor_initializer_p;
16532 }
16533
16534 /* Parse a handler-seq.
16535
16536    handler-seq:
16537      handler handler-seq [opt]  */
16538
16539 static void
16540 cp_parser_handler_seq (cp_parser* parser)
16541 {
16542   while (true)
16543     {
16544       cp_token *token;
16545
16546       /* Parse the handler.  */
16547       cp_parser_handler (parser);
16548       /* Peek at the next token.  */
16549       token = cp_lexer_peek_token (parser->lexer);
16550       /* If it's not `catch' then there are no more handlers.  */
16551       if (!cp_parser_is_keyword (token, RID_CATCH))
16552         break;
16553     }
16554 }
16555
16556 /* Parse a handler.
16557
16558    handler:
16559      catch ( exception-declaration ) compound-statement  */
16560
16561 static void
16562 cp_parser_handler (cp_parser* parser)
16563 {
16564   tree handler;
16565   tree declaration;
16566
16567   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16568   handler = begin_handler ();
16569   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16570   declaration = cp_parser_exception_declaration (parser);
16571   finish_handler_parms (declaration, handler);
16572   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16573   cp_parser_compound_statement (parser, NULL, false);
16574   finish_handler (handler);
16575 }
16576
16577 /* Parse an exception-declaration.
16578
16579    exception-declaration:
16580      type-specifier-seq declarator
16581      type-specifier-seq abstract-declarator
16582      type-specifier-seq
16583      ...
16584
16585    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16586    ellipsis variant is used.  */
16587
16588 static tree
16589 cp_parser_exception_declaration (cp_parser* parser)
16590 {
16591   cp_decl_specifier_seq type_specifiers;
16592   cp_declarator *declarator;
16593   const char *saved_message;
16594
16595   /* If it's an ellipsis, it's easy to handle.  */
16596   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16597     {
16598       /* Consume the `...' token.  */
16599       cp_lexer_consume_token (parser->lexer);
16600       return NULL_TREE;
16601     }
16602
16603   /* Types may not be defined in exception-declarations.  */
16604   saved_message = parser->type_definition_forbidden_message;
16605   parser->type_definition_forbidden_message
16606     = "types may not be defined in exception-declarations";
16607
16608   /* Parse the type-specifier-seq.  */
16609   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16610                                 &type_specifiers);
16611   /* If it's a `)', then there is no declarator.  */
16612   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16613     declarator = NULL;
16614   else
16615     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16616                                        /*ctor_dtor_or_conv_p=*/NULL,
16617                                        /*parenthesized_p=*/NULL,
16618                                        /*member_p=*/false);
16619
16620   /* Restore the saved message.  */
16621   parser->type_definition_forbidden_message = saved_message;
16622
16623   if (!type_specifiers.any_specifiers_p)
16624     return error_mark_node;
16625
16626   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16627 }
16628
16629 /* Parse a throw-expression.
16630
16631    throw-expression:
16632      throw assignment-expression [opt]
16633
16634    Returns a THROW_EXPR representing the throw-expression.  */
16635
16636 static tree
16637 cp_parser_throw_expression (cp_parser* parser)
16638 {
16639   tree expression;
16640   cp_token* token;
16641
16642   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16643   token = cp_lexer_peek_token (parser->lexer);
16644   /* Figure out whether or not there is an assignment-expression
16645      following the "throw" keyword.  */
16646   if (token->type == CPP_COMMA
16647       || token->type == CPP_SEMICOLON
16648       || token->type == CPP_CLOSE_PAREN
16649       || token->type == CPP_CLOSE_SQUARE
16650       || token->type == CPP_CLOSE_BRACE
16651       || token->type == CPP_COLON)
16652     expression = NULL_TREE;
16653   else
16654     expression = cp_parser_assignment_expression (parser,
16655                                                   /*cast_p=*/false, NULL);
16656
16657   return build_throw (expression);
16658 }
16659
16660 /* GNU Extensions */
16661
16662 /* Parse an (optional) asm-specification.
16663
16664    asm-specification:
16665      asm ( string-literal )
16666
16667    If the asm-specification is present, returns a STRING_CST
16668    corresponding to the string-literal.  Otherwise, returns
16669    NULL_TREE.  */
16670
16671 static tree
16672 cp_parser_asm_specification_opt (cp_parser* parser)
16673 {
16674   cp_token *token;
16675   tree asm_specification;
16676
16677   /* Peek at the next token.  */
16678   token = cp_lexer_peek_token (parser->lexer);
16679   /* If the next token isn't the `asm' keyword, then there's no
16680      asm-specification.  */
16681   if (!cp_parser_is_keyword (token, RID_ASM))
16682     return NULL_TREE;
16683
16684   /* Consume the `asm' token.  */
16685   cp_lexer_consume_token (parser->lexer);
16686   /* Look for the `('.  */
16687   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16688
16689   /* Look for the string-literal.  */
16690   asm_specification = cp_parser_string_literal (parser, false, false);
16691
16692   /* Look for the `)'.  */
16693   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16694
16695   return asm_specification;
16696 }
16697
16698 /* Parse an asm-operand-list.
16699
16700    asm-operand-list:
16701      asm-operand
16702      asm-operand-list , asm-operand
16703
16704    asm-operand:
16705      string-literal ( expression )
16706      [ string-literal ] string-literal ( expression )
16707
16708    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16709    each node is the expression.  The TREE_PURPOSE is itself a
16710    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16711    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16712    is a STRING_CST for the string literal before the parenthesis. Returns
16713    ERROR_MARK_NODE if any of the operands are invalid.  */
16714
16715 static tree
16716 cp_parser_asm_operand_list (cp_parser* parser)
16717 {
16718   tree asm_operands = NULL_TREE;
16719   bool invalid_operands = false;
16720
16721   while (true)
16722     {
16723       tree string_literal;
16724       tree expression;
16725       tree name;
16726
16727       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16728         {
16729           /* Consume the `[' token.  */
16730           cp_lexer_consume_token (parser->lexer);
16731           /* Read the operand name.  */
16732           name = cp_parser_identifier (parser);
16733           if (name != error_mark_node)
16734             name = build_string (IDENTIFIER_LENGTH (name),
16735                                  IDENTIFIER_POINTER (name));
16736           /* Look for the closing `]'.  */
16737           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16738         }
16739       else
16740         name = NULL_TREE;
16741       /* Look for the string-literal.  */
16742       string_literal = cp_parser_string_literal (parser, false, false);
16743
16744       /* Look for the `('.  */
16745       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16746       /* Parse the expression.  */
16747       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16748       /* Look for the `)'.  */
16749       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16750
16751       if (name == error_mark_node 
16752           || string_literal == error_mark_node 
16753           || expression == error_mark_node)
16754         invalid_operands = true;
16755
16756       /* Add this operand to the list.  */
16757       asm_operands = tree_cons (build_tree_list (name, string_literal),
16758                                 expression,
16759                                 asm_operands);
16760       /* If the next token is not a `,', there are no more
16761          operands.  */
16762       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16763         break;
16764       /* Consume the `,'.  */
16765       cp_lexer_consume_token (parser->lexer);
16766     }
16767
16768   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16769 }
16770
16771 /* Parse an asm-clobber-list.
16772
16773    asm-clobber-list:
16774      string-literal
16775      asm-clobber-list , string-literal
16776
16777    Returns a TREE_LIST, indicating the clobbers in the order that they
16778    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16779
16780 static tree
16781 cp_parser_asm_clobber_list (cp_parser* parser)
16782 {
16783   tree clobbers = NULL_TREE;
16784
16785   while (true)
16786     {
16787       tree string_literal;
16788
16789       /* Look for the string literal.  */
16790       string_literal = cp_parser_string_literal (parser, false, false);
16791       /* Add it to the list.  */
16792       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16793       /* If the next token is not a `,', then the list is
16794          complete.  */
16795       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16796         break;
16797       /* Consume the `,' token.  */
16798       cp_lexer_consume_token (parser->lexer);
16799     }
16800
16801   return clobbers;
16802 }
16803
16804 /* Parse an (optional) series of attributes.
16805
16806    attributes:
16807      attributes attribute
16808
16809    attribute:
16810      __attribute__ (( attribute-list [opt] ))
16811
16812    The return value is as for cp_parser_attribute_list.  */
16813
16814 static tree
16815 cp_parser_attributes_opt (cp_parser* parser)
16816 {
16817   tree attributes = NULL_TREE;
16818
16819   while (true)
16820     {
16821       cp_token *token;
16822       tree attribute_list;
16823
16824       /* Peek at the next token.  */
16825       token = cp_lexer_peek_token (parser->lexer);
16826       /* If it's not `__attribute__', then we're done.  */
16827       if (token->keyword != RID_ATTRIBUTE)
16828         break;
16829
16830       /* Consume the `__attribute__' keyword.  */
16831       cp_lexer_consume_token (parser->lexer);
16832       /* Look for the two `(' tokens.  */
16833       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16834       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16835
16836       /* Peek at the next token.  */
16837       token = cp_lexer_peek_token (parser->lexer);
16838       if (token->type != CPP_CLOSE_PAREN)
16839         /* Parse the attribute-list.  */
16840         attribute_list = cp_parser_attribute_list (parser);
16841       else
16842         /* If the next token is a `)', then there is no attribute
16843            list.  */
16844         attribute_list = NULL;
16845
16846       /* Look for the two `)' tokens.  */
16847       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16848       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16849
16850       /* Add these new attributes to the list.  */
16851       attributes = chainon (attributes, attribute_list);
16852     }
16853
16854   return attributes;
16855 }
16856
16857 /* Parse an attribute-list.
16858
16859    attribute-list:
16860      attribute
16861      attribute-list , attribute
16862
16863    attribute:
16864      identifier
16865      identifier ( identifier )
16866      identifier ( identifier , expression-list )
16867      identifier ( expression-list )
16868
16869    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16870    to an attribute.  The TREE_PURPOSE of each node is the identifier
16871    indicating which attribute is in use.  The TREE_VALUE represents
16872    the arguments, if any.  */
16873
16874 static tree
16875 cp_parser_attribute_list (cp_parser* parser)
16876 {
16877   tree attribute_list = NULL_TREE;
16878   bool save_translate_strings_p = parser->translate_strings_p;
16879
16880   parser->translate_strings_p = false;
16881   while (true)
16882     {
16883       cp_token *token;
16884       tree identifier;
16885       tree attribute;
16886
16887       /* Look for the identifier.  We also allow keywords here; for
16888          example `__attribute__ ((const))' is legal.  */
16889       token = cp_lexer_peek_token (parser->lexer);
16890       if (token->type == CPP_NAME
16891           || token->type == CPP_KEYWORD)
16892         {
16893           tree arguments = NULL_TREE;
16894
16895           /* Consume the token.  */
16896           token = cp_lexer_consume_token (parser->lexer);
16897
16898           /* Save away the identifier that indicates which attribute
16899              this is.  */
16900           identifier = (token->type == CPP_KEYWORD) 
16901             /* For keywords, use the canonical spelling, not the
16902                parsed identifier.  */
16903             ? ridpointers[(int) token->keyword]
16904             : token->u.value;
16905           
16906           attribute = build_tree_list (identifier, NULL_TREE);
16907
16908           /* Peek at the next token.  */
16909           token = cp_lexer_peek_token (parser->lexer);
16910           /* If it's an `(', then parse the attribute arguments.  */
16911           if (token->type == CPP_OPEN_PAREN)
16912             {
16913               VEC(tree,gc) *vec;
16914               vec = cp_parser_parenthesized_expression_list
16915                     (parser, true, /*cast_p=*/false,
16916                      /*allow_expansion_p=*/false,
16917                      /*non_constant_p=*/NULL);
16918               if (vec == NULL)
16919                 arguments = error_mark_node;
16920               else
16921                 {
16922                   arguments = build_tree_list_vec (vec);
16923                   release_tree_vector (vec);
16924                 }
16925               /* Save the arguments away.  */
16926               TREE_VALUE (attribute) = arguments;
16927             }
16928
16929           if (arguments != error_mark_node)
16930             {
16931               /* Add this attribute to the list.  */
16932               TREE_CHAIN (attribute) = attribute_list;
16933               attribute_list = attribute;
16934             }
16935
16936           token = cp_lexer_peek_token (parser->lexer);
16937         }
16938       /* Now, look for more attributes.  If the next token isn't a
16939          `,', we're done.  */
16940       if (token->type != CPP_COMMA)
16941         break;
16942
16943       /* Consume the comma and keep going.  */
16944       cp_lexer_consume_token (parser->lexer);
16945     }
16946   parser->translate_strings_p = save_translate_strings_p;
16947
16948   /* We built up the list in reverse order.  */
16949   return nreverse (attribute_list);
16950 }
16951
16952 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16953    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16954    current value of the PEDANTIC flag, regardless of whether or not
16955    the `__extension__' keyword is present.  The caller is responsible
16956    for restoring the value of the PEDANTIC flag.  */
16957
16958 static bool
16959 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16960 {
16961   /* Save the old value of the PEDANTIC flag.  */
16962   *saved_pedantic = pedantic;
16963
16964   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16965     {
16966       /* Consume the `__extension__' token.  */
16967       cp_lexer_consume_token (parser->lexer);
16968       /* We're not being pedantic while the `__extension__' keyword is
16969          in effect.  */
16970       pedantic = 0;
16971
16972       return true;
16973     }
16974
16975   return false;
16976 }
16977
16978 /* Parse a label declaration.
16979
16980    label-declaration:
16981      __label__ label-declarator-seq ;
16982
16983    label-declarator-seq:
16984      identifier , label-declarator-seq
16985      identifier  */
16986
16987 static void
16988 cp_parser_label_declaration (cp_parser* parser)
16989 {
16990   /* Look for the `__label__' keyword.  */
16991   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16992
16993   while (true)
16994     {
16995       tree identifier;
16996
16997       /* Look for an identifier.  */
16998       identifier = cp_parser_identifier (parser);
16999       /* If we failed, stop.  */
17000       if (identifier == error_mark_node)
17001         break;
17002       /* Declare it as a label.  */
17003       finish_label_decl (identifier);
17004       /* If the next token is a `;', stop.  */
17005       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17006         break;
17007       /* Look for the `,' separating the label declarations.  */
17008       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17009     }
17010
17011   /* Look for the final `;'.  */
17012   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17013 }
17014
17015 /* Support Functions */
17016
17017 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17018    NAME should have one of the representations used for an
17019    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17020    is returned.  If PARSER->SCOPE is a dependent type, then a
17021    SCOPE_REF is returned.
17022
17023    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17024    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17025    was formed.  Abstractly, such entities should not be passed to this
17026    function, because they do not need to be looked up, but it is
17027    simpler to check for this special case here, rather than at the
17028    call-sites.
17029
17030    In cases not explicitly covered above, this function returns a
17031    DECL, OVERLOAD, or baselink representing the result of the lookup.
17032    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17033    is returned.
17034
17035    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17036    (e.g., "struct") that was used.  In that case bindings that do not
17037    refer to types are ignored.
17038
17039    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17040    ignored.
17041
17042    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17043    are ignored.
17044
17045    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17046    types.
17047
17048    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17049    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17050    NULL_TREE otherwise.  */
17051
17052 static tree
17053 cp_parser_lookup_name (cp_parser *parser, tree name,
17054                        enum tag_types tag_type,
17055                        bool is_template,
17056                        bool is_namespace,
17057                        bool check_dependency,
17058                        tree *ambiguous_decls,
17059                        location_t name_location)
17060 {
17061   int flags = 0;
17062   tree decl;
17063   tree object_type = parser->context->object_type;
17064
17065   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17066     flags |= LOOKUP_COMPLAIN;
17067
17068   /* Assume that the lookup will be unambiguous.  */
17069   if (ambiguous_decls)
17070     *ambiguous_decls = NULL_TREE;
17071
17072   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17073      no longer valid.  Note that if we are parsing tentatively, and
17074      the parse fails, OBJECT_TYPE will be automatically restored.  */
17075   parser->context->object_type = NULL_TREE;
17076
17077   if (name == error_mark_node)
17078     return error_mark_node;
17079
17080   /* A template-id has already been resolved; there is no lookup to
17081      do.  */
17082   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17083     return name;
17084   if (BASELINK_P (name))
17085     {
17086       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17087                   == TEMPLATE_ID_EXPR);
17088       return name;
17089     }
17090
17091   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17092      it should already have been checked to make sure that the name
17093      used matches the type being destroyed.  */
17094   if (TREE_CODE (name) == BIT_NOT_EXPR)
17095     {
17096       tree type;
17097
17098       /* Figure out to which type this destructor applies.  */
17099       if (parser->scope)
17100         type = parser->scope;
17101       else if (object_type)
17102         type = object_type;
17103       else
17104         type = current_class_type;
17105       /* If that's not a class type, there is no destructor.  */
17106       if (!type || !CLASS_TYPE_P (type))
17107         return error_mark_node;
17108       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17109         lazily_declare_fn (sfk_destructor, type);
17110       if (!CLASSTYPE_DESTRUCTORS (type))
17111           return error_mark_node;
17112       /* If it was a class type, return the destructor.  */
17113       return CLASSTYPE_DESTRUCTORS (type);
17114     }
17115
17116   /* By this point, the NAME should be an ordinary identifier.  If
17117      the id-expression was a qualified name, the qualifying scope is
17118      stored in PARSER->SCOPE at this point.  */
17119   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17120
17121   /* Perform the lookup.  */
17122   if (parser->scope)
17123     {
17124       bool dependent_p;
17125
17126       if (parser->scope == error_mark_node)
17127         return error_mark_node;
17128
17129       /* If the SCOPE is dependent, the lookup must be deferred until
17130          the template is instantiated -- unless we are explicitly
17131          looking up names in uninstantiated templates.  Even then, we
17132          cannot look up the name if the scope is not a class type; it
17133          might, for example, be a template type parameter.  */
17134       dependent_p = (TYPE_P (parser->scope)
17135                      && dependent_scope_p (parser->scope));
17136       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17137           && dependent_p)
17138         /* Defer lookup.  */
17139         decl = error_mark_node;
17140       else
17141         {
17142           tree pushed_scope = NULL_TREE;
17143
17144           /* If PARSER->SCOPE is a dependent type, then it must be a
17145              class type, and we must not be checking dependencies;
17146              otherwise, we would have processed this lookup above.  So
17147              that PARSER->SCOPE is not considered a dependent base by
17148              lookup_member, we must enter the scope here.  */
17149           if (dependent_p)
17150             pushed_scope = push_scope (parser->scope);
17151           /* If the PARSER->SCOPE is a template specialization, it
17152              may be instantiated during name lookup.  In that case,
17153              errors may be issued.  Even if we rollback the current
17154              tentative parse, those errors are valid.  */
17155           decl = lookup_qualified_name (parser->scope, name,
17156                                         tag_type != none_type,
17157                                         /*complain=*/true);
17158
17159           /* If we have a single function from a using decl, pull it out.  */
17160           if (TREE_CODE (decl) == OVERLOAD
17161               && !really_overloaded_fn (decl))
17162             decl = OVL_FUNCTION (decl);
17163
17164           if (pushed_scope)
17165             pop_scope (pushed_scope);
17166         }
17167
17168       /* If the scope is a dependent type and either we deferred lookup or
17169          we did lookup but didn't find the name, rememeber the name.  */
17170       if (decl == error_mark_node && TYPE_P (parser->scope)
17171           && dependent_type_p (parser->scope))
17172         {
17173           if (tag_type)
17174             {
17175               tree type;
17176
17177               /* The resolution to Core Issue 180 says that `struct
17178                  A::B' should be considered a type-name, even if `A'
17179                  is dependent.  */
17180               type = make_typename_type (parser->scope, name, tag_type,
17181                                          /*complain=*/tf_error);
17182               decl = TYPE_NAME (type);
17183             }
17184           else if (is_template
17185                    && (cp_parser_next_token_ends_template_argument_p (parser)
17186                        || cp_lexer_next_token_is (parser->lexer,
17187                                                   CPP_CLOSE_PAREN)))
17188             decl = make_unbound_class_template (parser->scope,
17189                                                 name, NULL_TREE,
17190                                                 /*complain=*/tf_error);
17191           else
17192             decl = build_qualified_name (/*type=*/NULL_TREE,
17193                                          parser->scope, name,
17194                                          is_template);
17195         }
17196       parser->qualifying_scope = parser->scope;
17197       parser->object_scope = NULL_TREE;
17198     }
17199   else if (object_type)
17200     {
17201       tree object_decl = NULL_TREE;
17202       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17203          OBJECT_TYPE is not a class.  */
17204       if (CLASS_TYPE_P (object_type))
17205         /* If the OBJECT_TYPE is a template specialization, it may
17206            be instantiated during name lookup.  In that case, errors
17207            may be issued.  Even if we rollback the current tentative
17208            parse, those errors are valid.  */
17209         object_decl = lookup_member (object_type,
17210                                      name,
17211                                      /*protect=*/0,
17212                                      tag_type != none_type);
17213       /* Look it up in the enclosing context, too.  */
17214       decl = lookup_name_real (name, tag_type != none_type,
17215                                /*nonclass=*/0,
17216                                /*block_p=*/true, is_namespace, flags);
17217       parser->object_scope = object_type;
17218       parser->qualifying_scope = NULL_TREE;
17219       if (object_decl)
17220         decl = object_decl;
17221     }
17222   else
17223     {
17224       decl = lookup_name_real (name, tag_type != none_type,
17225                                /*nonclass=*/0,
17226                                /*block_p=*/true, is_namespace, flags);
17227       parser->qualifying_scope = NULL_TREE;
17228       parser->object_scope = NULL_TREE;
17229     }
17230
17231   /* If the lookup failed, let our caller know.  */
17232   if (!decl || decl == error_mark_node)
17233     return error_mark_node;
17234
17235   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17236   if (TREE_CODE (decl) == TREE_LIST)
17237     {
17238       if (ambiguous_decls)
17239         *ambiguous_decls = decl;
17240       /* The error message we have to print is too complicated for
17241          cp_parser_error, so we incorporate its actions directly.  */
17242       if (!cp_parser_simulate_error (parser))
17243         {
17244           error ("%Hreference to %qD is ambiguous",
17245                  &name_location, name);
17246           print_candidates (decl);
17247         }
17248       return error_mark_node;
17249     }
17250
17251   gcc_assert (DECL_P (decl)
17252               || TREE_CODE (decl) == OVERLOAD
17253               || TREE_CODE (decl) == SCOPE_REF
17254               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17255               || BASELINK_P (decl));
17256
17257   /* If we have resolved the name of a member declaration, check to
17258      see if the declaration is accessible.  When the name resolves to
17259      set of overloaded functions, accessibility is checked when
17260      overload resolution is done.
17261
17262      During an explicit instantiation, access is not checked at all,
17263      as per [temp.explicit].  */
17264   if (DECL_P (decl))
17265     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17266
17267   return decl;
17268 }
17269
17270 /* Like cp_parser_lookup_name, but for use in the typical case where
17271    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17272    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17273
17274 static tree
17275 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17276 {
17277   return cp_parser_lookup_name (parser, name,
17278                                 none_type,
17279                                 /*is_template=*/false,
17280                                 /*is_namespace=*/false,
17281                                 /*check_dependency=*/true,
17282                                 /*ambiguous_decls=*/NULL,
17283                                 location);
17284 }
17285
17286 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17287    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17288    true, the DECL indicates the class being defined in a class-head,
17289    or declared in an elaborated-type-specifier.
17290
17291    Otherwise, return DECL.  */
17292
17293 static tree
17294 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17295 {
17296   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17297      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17298
17299        struct A {
17300          template <typename T> struct B;
17301        };
17302
17303        template <typename T> struct A::B {};
17304
17305      Similarly, in an elaborated-type-specifier:
17306
17307        namespace N { struct X{}; }
17308
17309        struct A {
17310          template <typename T> friend struct N::X;
17311        };
17312
17313      However, if the DECL refers to a class type, and we are in
17314      the scope of the class, then the name lookup automatically
17315      finds the TYPE_DECL created by build_self_reference rather
17316      than a TEMPLATE_DECL.  For example, in:
17317
17318        template <class T> struct S {
17319          S s;
17320        };
17321
17322      there is no need to handle such case.  */
17323
17324   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17325     return DECL_TEMPLATE_RESULT (decl);
17326
17327   return decl;
17328 }
17329
17330 /* If too many, or too few, template-parameter lists apply to the
17331    declarator, issue an error message.  Returns TRUE if all went well,
17332    and FALSE otherwise.  */
17333
17334 static bool
17335 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17336                                                 cp_declarator *declarator,
17337                                                 location_t declarator_location)
17338 {
17339   unsigned num_templates;
17340
17341   /* We haven't seen any classes that involve template parameters yet.  */
17342   num_templates = 0;
17343
17344   switch (declarator->kind)
17345     {
17346     case cdk_id:
17347       if (declarator->u.id.qualifying_scope)
17348         {
17349           tree scope;
17350           tree member;
17351
17352           scope = declarator->u.id.qualifying_scope;
17353           member = declarator->u.id.unqualified_name;
17354
17355           while (scope && CLASS_TYPE_P (scope))
17356             {
17357               /* You're supposed to have one `template <...>'
17358                  for every template class, but you don't need one
17359                  for a full specialization.  For example:
17360
17361                  template <class T> struct S{};
17362                  template <> struct S<int> { void f(); };
17363                  void S<int>::f () {}
17364
17365                  is correct; there shouldn't be a `template <>' for
17366                  the definition of `S<int>::f'.  */
17367               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17368                 /* If SCOPE does not have template information of any
17369                    kind, then it is not a template, nor is it nested
17370                    within a template.  */
17371                 break;
17372               if (explicit_class_specialization_p (scope))
17373                 break;
17374               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17375                 ++num_templates;
17376
17377               scope = TYPE_CONTEXT (scope);
17378             }
17379         }
17380       else if (TREE_CODE (declarator->u.id.unqualified_name)
17381                == TEMPLATE_ID_EXPR)
17382         /* If the DECLARATOR has the form `X<y>' then it uses one
17383            additional level of template parameters.  */
17384         ++num_templates;
17385
17386       return cp_parser_check_template_parameters 
17387         (parser, num_templates, declarator_location, declarator);
17388
17389
17390     case cdk_function:
17391     case cdk_array:
17392     case cdk_pointer:
17393     case cdk_reference:
17394     case cdk_ptrmem:
17395       return (cp_parser_check_declarator_template_parameters
17396               (parser, declarator->declarator, declarator_location));
17397
17398     case cdk_error:
17399       return true;
17400
17401     default:
17402       gcc_unreachable ();
17403     }
17404   return false;
17405 }
17406
17407 /* NUM_TEMPLATES were used in the current declaration.  If that is
17408    invalid, return FALSE and issue an error messages.  Otherwise,
17409    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
17410    declarator and we can print more accurate diagnostics.  */
17411
17412 static bool
17413 cp_parser_check_template_parameters (cp_parser* parser,
17414                                      unsigned num_templates,
17415                                      location_t location,
17416                                      cp_declarator *declarator)
17417 {
17418   /* If there are the same number of template classes and parameter
17419      lists, that's OK.  */
17420   if (parser->num_template_parameter_lists == num_templates)
17421     return true;
17422   /* If there are more, but only one more, then we are referring to a
17423      member template.  That's OK too.  */
17424   if (parser->num_template_parameter_lists == num_templates + 1)
17425     return true;
17426   /* If there are more template classes than parameter lists, we have
17427      something like:
17428
17429        template <class T> void S<T>::R<T>::f ();  */
17430   if (parser->num_template_parameter_lists < num_templates)
17431     {
17432       if (declarator)
17433         error_at (location, "specializing member %<%T::%E%> "
17434                   "requires %<template<>%> syntax", 
17435                   declarator->u.id.qualifying_scope,
17436                   declarator->u.id.unqualified_name);
17437       else 
17438         error_at (location, "too few template-parameter-lists");
17439       return false;
17440     }
17441   /* Otherwise, there are too many template parameter lists.  We have
17442      something like:
17443
17444      template <class T> template <class U> void S::f();  */
17445   error ("%Htoo many template-parameter-lists", &location);
17446   return false;
17447 }
17448
17449 /* Parse an optional `::' token indicating that the following name is
17450    from the global namespace.  If so, PARSER->SCOPE is set to the
17451    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17452    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17453    Returns the new value of PARSER->SCOPE, if the `::' token is
17454    present, and NULL_TREE otherwise.  */
17455
17456 static tree
17457 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17458 {
17459   cp_token *token;
17460
17461   /* Peek at the next token.  */
17462   token = cp_lexer_peek_token (parser->lexer);
17463   /* If we're looking at a `::' token then we're starting from the
17464      global namespace, not our current location.  */
17465   if (token->type == CPP_SCOPE)
17466     {
17467       /* Consume the `::' token.  */
17468       cp_lexer_consume_token (parser->lexer);
17469       /* Set the SCOPE so that we know where to start the lookup.  */
17470       parser->scope = global_namespace;
17471       parser->qualifying_scope = global_namespace;
17472       parser->object_scope = NULL_TREE;
17473
17474       return parser->scope;
17475     }
17476   else if (!current_scope_valid_p)
17477     {
17478       parser->scope = NULL_TREE;
17479       parser->qualifying_scope = NULL_TREE;
17480       parser->object_scope = NULL_TREE;
17481     }
17482
17483   return NULL_TREE;
17484 }
17485
17486 /* Returns TRUE if the upcoming token sequence is the start of a
17487    constructor declarator.  If FRIEND_P is true, the declarator is
17488    preceded by the `friend' specifier.  */
17489
17490 static bool
17491 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17492 {
17493   bool constructor_p;
17494   tree type_decl = NULL_TREE;
17495   bool nested_name_p;
17496   cp_token *next_token;
17497
17498   /* The common case is that this is not a constructor declarator, so
17499      try to avoid doing lots of work if at all possible.  It's not
17500      valid declare a constructor at function scope.  */
17501   if (parser->in_function_body)
17502     return false;
17503   /* And only certain tokens can begin a constructor declarator.  */
17504   next_token = cp_lexer_peek_token (parser->lexer);
17505   if (next_token->type != CPP_NAME
17506       && next_token->type != CPP_SCOPE
17507       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17508       && next_token->type != CPP_TEMPLATE_ID)
17509     return false;
17510
17511   /* Parse tentatively; we are going to roll back all of the tokens
17512      consumed here.  */
17513   cp_parser_parse_tentatively (parser);
17514   /* Assume that we are looking at a constructor declarator.  */
17515   constructor_p = true;
17516
17517   /* Look for the optional `::' operator.  */
17518   cp_parser_global_scope_opt (parser,
17519                               /*current_scope_valid_p=*/false);
17520   /* Look for the nested-name-specifier.  */
17521   nested_name_p
17522     = (cp_parser_nested_name_specifier_opt (parser,
17523                                             /*typename_keyword_p=*/false,
17524                                             /*check_dependency_p=*/false,
17525                                             /*type_p=*/false,
17526                                             /*is_declaration=*/false)
17527        != NULL_TREE);
17528   /* Outside of a class-specifier, there must be a
17529      nested-name-specifier.  */
17530   if (!nested_name_p &&
17531       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17532        || friend_p))
17533     constructor_p = false;
17534   /* If we still think that this might be a constructor-declarator,
17535      look for a class-name.  */
17536   if (constructor_p)
17537     {
17538       /* If we have:
17539
17540            template <typename T> struct S { S(); };
17541            template <typename T> S<T>::S ();
17542
17543          we must recognize that the nested `S' names a class.
17544          Similarly, for:
17545
17546            template <typename T> S<T>::S<T> ();
17547
17548          we must recognize that the nested `S' names a template.  */
17549       type_decl = cp_parser_class_name (parser,
17550                                         /*typename_keyword_p=*/false,
17551                                         /*template_keyword_p=*/false,
17552                                         none_type,
17553                                         /*check_dependency_p=*/false,
17554                                         /*class_head_p=*/false,
17555                                         /*is_declaration=*/false);
17556       /* If there was no class-name, then this is not a constructor.  */
17557       constructor_p = !cp_parser_error_occurred (parser);
17558     }
17559
17560   /* If we're still considering a constructor, we have to see a `(',
17561      to begin the parameter-declaration-clause, followed by either a
17562      `)', an `...', or a decl-specifier.  We need to check for a
17563      type-specifier to avoid being fooled into thinking that:
17564
17565        S::S (f) (int);
17566
17567      is a constructor.  (It is actually a function named `f' that
17568      takes one parameter (of type `int') and returns a value of type
17569      `S::S'.  */
17570   if (constructor_p
17571       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17572     {
17573       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17574           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17575           /* A parameter declaration begins with a decl-specifier,
17576              which is either the "attribute" keyword, a storage class
17577              specifier, or (usually) a type-specifier.  */
17578           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17579         {
17580           tree type;
17581           tree pushed_scope = NULL_TREE;
17582           unsigned saved_num_template_parameter_lists;
17583
17584           /* Names appearing in the type-specifier should be looked up
17585              in the scope of the class.  */
17586           if (current_class_type)
17587             type = NULL_TREE;
17588           else
17589             {
17590               type = TREE_TYPE (type_decl);
17591               if (TREE_CODE (type) == TYPENAME_TYPE)
17592                 {
17593                   type = resolve_typename_type (type,
17594                                                 /*only_current_p=*/false);
17595                   if (TREE_CODE (type) == TYPENAME_TYPE)
17596                     {
17597                       cp_parser_abort_tentative_parse (parser);
17598                       return false;
17599                     }
17600                 }
17601               pushed_scope = push_scope (type);
17602             }
17603
17604           /* Inside the constructor parameter list, surrounding
17605              template-parameter-lists do not apply.  */
17606           saved_num_template_parameter_lists
17607             = parser->num_template_parameter_lists;
17608           parser->num_template_parameter_lists = 0;
17609
17610           /* Look for the type-specifier.  */
17611           cp_parser_type_specifier (parser,
17612                                     CP_PARSER_FLAGS_NONE,
17613                                     /*decl_specs=*/NULL,
17614                                     /*is_declarator=*/true,
17615                                     /*declares_class_or_enum=*/NULL,
17616                                     /*is_cv_qualifier=*/NULL);
17617
17618           parser->num_template_parameter_lists
17619             = saved_num_template_parameter_lists;
17620
17621           /* Leave the scope of the class.  */
17622           if (pushed_scope)
17623             pop_scope (pushed_scope);
17624
17625           constructor_p = !cp_parser_error_occurred (parser);
17626         }
17627     }
17628   else
17629     constructor_p = false;
17630   /* We did not really want to consume any tokens.  */
17631   cp_parser_abort_tentative_parse (parser);
17632
17633   return constructor_p;
17634 }
17635
17636 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17637    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17638    they must be performed once we are in the scope of the function.
17639
17640    Returns the function defined.  */
17641
17642 static tree
17643 cp_parser_function_definition_from_specifiers_and_declarator
17644   (cp_parser* parser,
17645    cp_decl_specifier_seq *decl_specifiers,
17646    tree attributes,
17647    const cp_declarator *declarator)
17648 {
17649   tree fn;
17650   bool success_p;
17651
17652   /* Begin the function-definition.  */
17653   success_p = start_function (decl_specifiers, declarator, attributes);
17654
17655   /* The things we're about to see are not directly qualified by any
17656      template headers we've seen thus far.  */
17657   reset_specialization ();
17658
17659   /* If there were names looked up in the decl-specifier-seq that we
17660      did not check, check them now.  We must wait until we are in the
17661      scope of the function to perform the checks, since the function
17662      might be a friend.  */
17663   perform_deferred_access_checks ();
17664
17665   if (!success_p)
17666     {
17667       /* Skip the entire function.  */
17668       cp_parser_skip_to_end_of_block_or_statement (parser);
17669       fn = error_mark_node;
17670     }
17671   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17672     {
17673       /* Seen already, skip it.  An error message has already been output.  */
17674       cp_parser_skip_to_end_of_block_or_statement (parser);
17675       fn = current_function_decl;
17676       current_function_decl = NULL_TREE;
17677       /* If this is a function from a class, pop the nested class.  */
17678       if (current_class_name)
17679         pop_nested_class ();
17680     }
17681   else
17682     fn = cp_parser_function_definition_after_declarator (parser,
17683                                                          /*inline_p=*/false);
17684
17685   return fn;
17686 }
17687
17688 /* Parse the part of a function-definition that follows the
17689    declarator.  INLINE_P is TRUE iff this function is an inline
17690    function defined with a class-specifier.
17691
17692    Returns the function defined.  */
17693
17694 static tree
17695 cp_parser_function_definition_after_declarator (cp_parser* parser,
17696                                                 bool inline_p)
17697 {
17698   tree fn;
17699   bool ctor_initializer_p = false;
17700   bool saved_in_unbraced_linkage_specification_p;
17701   bool saved_in_function_body;
17702   unsigned saved_num_template_parameter_lists;
17703   cp_token *token;
17704
17705   saved_in_function_body = parser->in_function_body;
17706   parser->in_function_body = true;
17707   /* If the next token is `return', then the code may be trying to
17708      make use of the "named return value" extension that G++ used to
17709      support.  */
17710   token = cp_lexer_peek_token (parser->lexer);
17711   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17712     {
17713       /* Consume the `return' keyword.  */
17714       cp_lexer_consume_token (parser->lexer);
17715       /* Look for the identifier that indicates what value is to be
17716          returned.  */
17717       cp_parser_identifier (parser);
17718       /* Issue an error message.  */
17719       error ("%Hnamed return values are no longer supported",
17720              &token->location);
17721       /* Skip tokens until we reach the start of the function body.  */
17722       while (true)
17723         {
17724           cp_token *token = cp_lexer_peek_token (parser->lexer);
17725           if (token->type == CPP_OPEN_BRACE
17726               || token->type == CPP_EOF
17727               || token->type == CPP_PRAGMA_EOL)
17728             break;
17729           cp_lexer_consume_token (parser->lexer);
17730         }
17731     }
17732   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17733      anything declared inside `f'.  */
17734   saved_in_unbraced_linkage_specification_p
17735     = parser->in_unbraced_linkage_specification_p;
17736   parser->in_unbraced_linkage_specification_p = false;
17737   /* Inside the function, surrounding template-parameter-lists do not
17738      apply.  */
17739   saved_num_template_parameter_lists
17740     = parser->num_template_parameter_lists;
17741   parser->num_template_parameter_lists = 0;
17742   /* If the next token is `try', then we are looking at a
17743      function-try-block.  */
17744   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17745     ctor_initializer_p = cp_parser_function_try_block (parser);
17746   /* A function-try-block includes the function-body, so we only do
17747      this next part if we're not processing a function-try-block.  */
17748   else
17749     ctor_initializer_p
17750       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17751
17752   /* Finish the function.  */
17753   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17754                         (inline_p ? 2 : 0));
17755   /* Generate code for it, if necessary.  */
17756   expand_or_defer_fn (fn);
17757   /* Restore the saved values.  */
17758   parser->in_unbraced_linkage_specification_p
17759     = saved_in_unbraced_linkage_specification_p;
17760   parser->num_template_parameter_lists
17761     = saved_num_template_parameter_lists;
17762   parser->in_function_body = saved_in_function_body;
17763
17764   return fn;
17765 }
17766
17767 /* Parse a template-declaration, assuming that the `export' (and
17768    `extern') keywords, if present, has already been scanned.  MEMBER_P
17769    is as for cp_parser_template_declaration.  */
17770
17771 static void
17772 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17773 {
17774   tree decl = NULL_TREE;
17775   VEC (deferred_access_check,gc) *checks;
17776   tree parameter_list;
17777   bool friend_p = false;
17778   bool need_lang_pop;
17779   cp_token *token;
17780
17781   /* Look for the `template' keyword.  */
17782   token = cp_lexer_peek_token (parser->lexer);
17783   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17784     return;
17785
17786   /* And the `<'.  */
17787   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17788     return;
17789   if (at_class_scope_p () && current_function_decl)
17790     {
17791       /* 14.5.2.2 [temp.mem]
17792
17793          A local class shall not have member templates.  */
17794       error ("%Hinvalid declaration of member template in local class",
17795              &token->location);
17796       cp_parser_skip_to_end_of_block_or_statement (parser);
17797       return;
17798     }
17799   /* [temp]
17800
17801      A template ... shall not have C linkage.  */
17802   if (current_lang_name == lang_name_c)
17803     {
17804       error ("%Htemplate with C linkage", &token->location);
17805       /* Give it C++ linkage to avoid confusing other parts of the
17806          front end.  */
17807       push_lang_context (lang_name_cplusplus);
17808       need_lang_pop = true;
17809     }
17810   else
17811     need_lang_pop = false;
17812
17813   /* We cannot perform access checks on the template parameter
17814      declarations until we know what is being declared, just as we
17815      cannot check the decl-specifier list.  */
17816   push_deferring_access_checks (dk_deferred);
17817
17818   /* If the next token is `>', then we have an invalid
17819      specialization.  Rather than complain about an invalid template
17820      parameter, issue an error message here.  */
17821   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17822     {
17823       cp_parser_error (parser, "invalid explicit specialization");
17824       begin_specialization ();
17825       parameter_list = NULL_TREE;
17826     }
17827   else
17828     /* Parse the template parameters.  */
17829     parameter_list = cp_parser_template_parameter_list (parser);
17830
17831   /* Get the deferred access checks from the parameter list.  These
17832      will be checked once we know what is being declared, as for a
17833      member template the checks must be performed in the scope of the
17834      class containing the member.  */
17835   checks = get_deferred_access_checks ();
17836
17837   /* Look for the `>'.  */
17838   cp_parser_skip_to_end_of_template_parameter_list (parser);
17839   /* We just processed one more parameter list.  */
17840   ++parser->num_template_parameter_lists;
17841   /* If the next token is `template', there are more template
17842      parameters.  */
17843   if (cp_lexer_next_token_is_keyword (parser->lexer,
17844                                       RID_TEMPLATE))
17845     cp_parser_template_declaration_after_export (parser, member_p);
17846   else
17847     {
17848       /* There are no access checks when parsing a template, as we do not
17849          know if a specialization will be a friend.  */
17850       push_deferring_access_checks (dk_no_check);
17851       token = cp_lexer_peek_token (parser->lexer);
17852       decl = cp_parser_single_declaration (parser,
17853                                            checks,
17854                                            member_p,
17855                                            /*explicit_specialization_p=*/false,
17856                                            &friend_p);
17857       pop_deferring_access_checks ();
17858
17859       /* If this is a member template declaration, let the front
17860          end know.  */
17861       if (member_p && !friend_p && decl)
17862         {
17863           if (TREE_CODE (decl) == TYPE_DECL)
17864             cp_parser_check_access_in_redeclaration (decl, token->location);
17865
17866           decl = finish_member_template_decl (decl);
17867         }
17868       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17869         make_friend_class (current_class_type, TREE_TYPE (decl),
17870                            /*complain=*/true);
17871     }
17872   /* We are done with the current parameter list.  */
17873   --parser->num_template_parameter_lists;
17874
17875   pop_deferring_access_checks ();
17876
17877   /* Finish up.  */
17878   finish_template_decl (parameter_list);
17879
17880   /* Register member declarations.  */
17881   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17882     finish_member_declaration (decl);
17883   /* For the erroneous case of a template with C linkage, we pushed an
17884      implicit C++ linkage scope; exit that scope now.  */
17885   if (need_lang_pop)
17886     pop_lang_context ();
17887   /* If DECL is a function template, we must return to parse it later.
17888      (Even though there is no definition, there might be default
17889      arguments that need handling.)  */
17890   if (member_p && decl
17891       && (TREE_CODE (decl) == FUNCTION_DECL
17892           || DECL_FUNCTION_TEMPLATE_P (decl)))
17893     TREE_VALUE (parser->unparsed_functions_queues)
17894       = tree_cons (NULL_TREE, decl,
17895                    TREE_VALUE (parser->unparsed_functions_queues));
17896 }
17897
17898 /* Perform the deferred access checks from a template-parameter-list.
17899    CHECKS is a TREE_LIST of access checks, as returned by
17900    get_deferred_access_checks.  */
17901
17902 static void
17903 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17904 {
17905   ++processing_template_parmlist;
17906   perform_access_checks (checks);
17907   --processing_template_parmlist;
17908 }
17909
17910 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17911    `function-definition' sequence.  MEMBER_P is true, this declaration
17912    appears in a class scope.
17913
17914    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17915    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17916
17917 static tree
17918 cp_parser_single_declaration (cp_parser* parser,
17919                               VEC (deferred_access_check,gc)* checks,
17920                               bool member_p,
17921                               bool explicit_specialization_p,
17922                               bool* friend_p)
17923 {
17924   int declares_class_or_enum;
17925   tree decl = NULL_TREE;
17926   cp_decl_specifier_seq decl_specifiers;
17927   bool function_definition_p = false;
17928   cp_token *decl_spec_token_start;
17929
17930   /* This function is only used when processing a template
17931      declaration.  */
17932   gcc_assert (innermost_scope_kind () == sk_template_parms
17933               || innermost_scope_kind () == sk_template_spec);
17934
17935   /* Defer access checks until we know what is being declared.  */
17936   push_deferring_access_checks (dk_deferred);
17937
17938   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17939      alternative.  */
17940   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17941   cp_parser_decl_specifier_seq (parser,
17942                                 CP_PARSER_FLAGS_OPTIONAL,
17943                                 &decl_specifiers,
17944                                 &declares_class_or_enum);
17945   if (friend_p)
17946     *friend_p = cp_parser_friend_p (&decl_specifiers);
17947
17948   /* There are no template typedefs.  */
17949   if (decl_specifiers.specs[(int) ds_typedef])
17950     {
17951       error ("%Htemplate declaration of %qs",
17952              &decl_spec_token_start->location, "typedef");
17953       decl = error_mark_node;
17954     }
17955
17956   /* Gather up the access checks that occurred the
17957      decl-specifier-seq.  */
17958   stop_deferring_access_checks ();
17959
17960   /* Check for the declaration of a template class.  */
17961   if (declares_class_or_enum)
17962     {
17963       if (cp_parser_declares_only_class_p (parser))
17964         {
17965           decl = shadow_tag (&decl_specifiers);
17966
17967           /* In this case:
17968
17969                struct C {
17970                  friend template <typename T> struct A<T>::B;
17971                };
17972
17973              A<T>::B will be represented by a TYPENAME_TYPE, and
17974              therefore not recognized by shadow_tag.  */
17975           if (friend_p && *friend_p
17976               && !decl
17977               && decl_specifiers.type
17978               && TYPE_P (decl_specifiers.type))
17979             decl = decl_specifiers.type;
17980
17981           if (decl && decl != error_mark_node)
17982             decl = TYPE_NAME (decl);
17983           else
17984             decl = error_mark_node;
17985
17986           /* Perform access checks for template parameters.  */
17987           cp_parser_perform_template_parameter_access_checks (checks);
17988         }
17989     }
17990   /* If it's not a template class, try for a template function.  If
17991      the next token is a `;', then this declaration does not declare
17992      anything.  But, if there were errors in the decl-specifiers, then
17993      the error might well have come from an attempted class-specifier.
17994      In that case, there's no need to warn about a missing declarator.  */
17995   if (!decl
17996       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17997           || decl_specifiers.type != error_mark_node))
17998     {
17999       decl = cp_parser_init_declarator (parser,
18000                                         &decl_specifiers,
18001                                         checks,
18002                                         /*function_definition_allowed_p=*/true,
18003                                         member_p,
18004                                         declares_class_or_enum,
18005                                         &function_definition_p);
18006
18007     /* 7.1.1-1 [dcl.stc]
18008
18009        A storage-class-specifier shall not be specified in an explicit
18010        specialization...  */
18011     if (decl
18012         && explicit_specialization_p
18013         && decl_specifiers.storage_class != sc_none)
18014       {
18015         error ("%Hexplicit template specialization cannot have a storage class",
18016                &decl_spec_token_start->location);
18017         decl = error_mark_node;
18018       }
18019     }
18020
18021   pop_deferring_access_checks ();
18022
18023   /* Clear any current qualification; whatever comes next is the start
18024      of something new.  */
18025   parser->scope = NULL_TREE;
18026   parser->qualifying_scope = NULL_TREE;
18027   parser->object_scope = NULL_TREE;
18028   /* Look for a trailing `;' after the declaration.  */
18029   if (!function_definition_p
18030       && (decl == error_mark_node
18031           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18032     cp_parser_skip_to_end_of_block_or_statement (parser);
18033
18034   return decl;
18035 }
18036
18037 /* Parse a cast-expression that is not the operand of a unary "&".  */
18038
18039 static tree
18040 cp_parser_simple_cast_expression (cp_parser *parser)
18041 {
18042   return cp_parser_cast_expression (parser, /*address_p=*/false,
18043                                     /*cast_p=*/false, NULL);
18044 }
18045
18046 /* Parse a functional cast to TYPE.  Returns an expression
18047    representing the cast.  */
18048
18049 static tree
18050 cp_parser_functional_cast (cp_parser* parser, tree type)
18051 {
18052   VEC(tree,gc) *vec;
18053   tree expression_list;
18054   tree cast;
18055   bool nonconst_p;
18056
18057   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18058     {
18059       maybe_warn_cpp0x ("extended initializer lists");
18060       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18061       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18062       if (TREE_CODE (type) == TYPE_DECL)
18063         type = TREE_TYPE (type);
18064       return finish_compound_literal (type, expression_list);
18065     }
18066
18067
18068   vec = cp_parser_parenthesized_expression_list (parser, false,
18069                                                  /*cast_p=*/true,
18070                                                  /*allow_expansion_p=*/true,
18071                                                  /*non_constant_p=*/NULL);
18072   if (vec == NULL)
18073     expression_list = error_mark_node;
18074   else
18075     {
18076       expression_list = build_tree_list_vec (vec);
18077       release_tree_vector (vec);
18078     }
18079
18080   cast = build_functional_cast (type, expression_list,
18081                                 tf_warning_or_error);
18082   /* [expr.const]/1: In an integral constant expression "only type
18083      conversions to integral or enumeration type can be used".  */
18084   if (TREE_CODE (type) == TYPE_DECL)
18085     type = TREE_TYPE (type);
18086   if (cast != error_mark_node
18087       && !cast_valid_in_integral_constant_expression_p (type)
18088       && (cp_parser_non_integral_constant_expression
18089           (parser, "a call to a constructor")))
18090     return error_mark_node;
18091   return cast;
18092 }
18093
18094 /* Save the tokens that make up the body of a member function defined
18095    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18096    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18097    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18098    for the member function.  */
18099
18100 static tree
18101 cp_parser_save_member_function_body (cp_parser* parser,
18102                                      cp_decl_specifier_seq *decl_specifiers,
18103                                      cp_declarator *declarator,
18104                                      tree attributes)
18105 {
18106   cp_token *first;
18107   cp_token *last;
18108   tree fn;
18109
18110   /* Create the function-declaration.  */
18111   fn = start_method (decl_specifiers, declarator, attributes);
18112   /* If something went badly wrong, bail out now.  */
18113   if (fn == error_mark_node)
18114     {
18115       /* If there's a function-body, skip it.  */
18116       if (cp_parser_token_starts_function_definition_p
18117           (cp_lexer_peek_token (parser->lexer)))
18118         cp_parser_skip_to_end_of_block_or_statement (parser);
18119       return error_mark_node;
18120     }
18121
18122   /* Remember it, if there default args to post process.  */
18123   cp_parser_save_default_args (parser, fn);
18124
18125   /* Save away the tokens that make up the body of the
18126      function.  */
18127   first = parser->lexer->next_token;
18128   /* We can have braced-init-list mem-initializers before the fn body.  */
18129   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18130     {
18131       cp_lexer_consume_token (parser->lexer);
18132       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18133              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18134         {
18135           /* cache_group will stop after an un-nested { } pair, too.  */
18136           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18137             break;
18138
18139           /* variadic mem-inits have ... after the ')'.  */
18140           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18141             cp_lexer_consume_token (parser->lexer);
18142         }
18143     }
18144   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18145   /* Handle function try blocks.  */
18146   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18147     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18148   last = parser->lexer->next_token;
18149
18150   /* Save away the inline definition; we will process it when the
18151      class is complete.  */
18152   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18153   DECL_PENDING_INLINE_P (fn) = 1;
18154
18155   /* We need to know that this was defined in the class, so that
18156      friend templates are handled correctly.  */
18157   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18158
18159   /* We're done with the inline definition.  */
18160   finish_method (fn);
18161
18162   /* Add FN to the queue of functions to be parsed later.  */
18163   TREE_VALUE (parser->unparsed_functions_queues)
18164     = tree_cons (NULL_TREE, fn,
18165                  TREE_VALUE (parser->unparsed_functions_queues));
18166
18167   return fn;
18168 }
18169
18170 /* Parse a template-argument-list, as well as the trailing ">" (but
18171    not the opening ">").  See cp_parser_template_argument_list for the
18172    return value.  */
18173
18174 static tree
18175 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18176 {
18177   tree arguments;
18178   tree saved_scope;
18179   tree saved_qualifying_scope;
18180   tree saved_object_scope;
18181   bool saved_greater_than_is_operator_p;
18182   bool saved_skip_evaluation;
18183
18184   /* [temp.names]
18185
18186      When parsing a template-id, the first non-nested `>' is taken as
18187      the end of the template-argument-list rather than a greater-than
18188      operator.  */
18189   saved_greater_than_is_operator_p
18190     = parser->greater_than_is_operator_p;
18191   parser->greater_than_is_operator_p = false;
18192   /* Parsing the argument list may modify SCOPE, so we save it
18193      here.  */
18194   saved_scope = parser->scope;
18195   saved_qualifying_scope = parser->qualifying_scope;
18196   saved_object_scope = parser->object_scope;
18197   /* We need to evaluate the template arguments, even though this
18198      template-id may be nested within a "sizeof".  */
18199   saved_skip_evaluation = skip_evaluation;
18200   skip_evaluation = false;
18201   /* Parse the template-argument-list itself.  */
18202   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18203       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18204     arguments = NULL_TREE;
18205   else
18206     arguments = cp_parser_template_argument_list (parser);
18207   /* Look for the `>' that ends the template-argument-list. If we find
18208      a '>>' instead, it's probably just a typo.  */
18209   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18210     {
18211       if (cxx_dialect != cxx98)
18212         {
18213           /* In C++0x, a `>>' in a template argument list or cast
18214              expression is considered to be two separate `>'
18215              tokens. So, change the current token to a `>', but don't
18216              consume it: it will be consumed later when the outer
18217              template argument list (or cast expression) is parsed.
18218              Note that this replacement of `>' for `>>' is necessary
18219              even if we are parsing tentatively: in the tentative
18220              case, after calling
18221              cp_parser_enclosed_template_argument_list we will always
18222              throw away all of the template arguments and the first
18223              closing `>', either because the template argument list
18224              was erroneous or because we are replacing those tokens
18225              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18226              not have been thrown away) is needed either to close an
18227              outer template argument list or to complete a new-style
18228              cast.  */
18229           cp_token *token = cp_lexer_peek_token (parser->lexer);
18230           token->type = CPP_GREATER;
18231         }
18232       else if (!saved_greater_than_is_operator_p)
18233         {
18234           /* If we're in a nested template argument list, the '>>' has
18235             to be a typo for '> >'. We emit the error message, but we
18236             continue parsing and we push a '>' as next token, so that
18237             the argument list will be parsed correctly.  Note that the
18238             global source location is still on the token before the
18239             '>>', so we need to say explicitly where we want it.  */
18240           cp_token *token = cp_lexer_peek_token (parser->lexer);
18241           error ("%H%<>>%> should be %<> >%> "
18242                  "within a nested template argument list",
18243                  &token->location);
18244
18245           token->type = CPP_GREATER;
18246         }
18247       else
18248         {
18249           /* If this is not a nested template argument list, the '>>'
18250             is a typo for '>'. Emit an error message and continue.
18251             Same deal about the token location, but here we can get it
18252             right by consuming the '>>' before issuing the diagnostic.  */
18253           cp_token *token = cp_lexer_consume_token (parser->lexer);
18254           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18255                  "a template argument list", &token->location);
18256         }
18257     }
18258   else
18259     cp_parser_skip_to_end_of_template_parameter_list (parser);
18260   /* The `>' token might be a greater-than operator again now.  */
18261   parser->greater_than_is_operator_p
18262     = saved_greater_than_is_operator_p;
18263   /* Restore the SAVED_SCOPE.  */
18264   parser->scope = saved_scope;
18265   parser->qualifying_scope = saved_qualifying_scope;
18266   parser->object_scope = saved_object_scope;
18267   skip_evaluation = saved_skip_evaluation;
18268
18269   return arguments;
18270 }
18271
18272 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18273    arguments, or the body of the function have not yet been parsed,
18274    parse them now.  */
18275
18276 static void
18277 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18278 {
18279   /* If this member is a template, get the underlying
18280      FUNCTION_DECL.  */
18281   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18282     member_function = DECL_TEMPLATE_RESULT (member_function);
18283
18284   /* There should not be any class definitions in progress at this
18285      point; the bodies of members are only parsed outside of all class
18286      definitions.  */
18287   gcc_assert (parser->num_classes_being_defined == 0);
18288   /* While we're parsing the member functions we might encounter more
18289      classes.  We want to handle them right away, but we don't want
18290      them getting mixed up with functions that are currently in the
18291      queue.  */
18292   parser->unparsed_functions_queues
18293     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18294
18295   /* Make sure that any template parameters are in scope.  */
18296   maybe_begin_member_template_processing (member_function);
18297
18298   /* If the body of the function has not yet been parsed, parse it
18299      now.  */
18300   if (DECL_PENDING_INLINE_P (member_function))
18301     {
18302       tree function_scope;
18303       cp_token_cache *tokens;
18304
18305       /* The function is no longer pending; we are processing it.  */
18306       tokens = DECL_PENDING_INLINE_INFO (member_function);
18307       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18308       DECL_PENDING_INLINE_P (member_function) = 0;
18309
18310       /* If this is a local class, enter the scope of the containing
18311          function.  */
18312       function_scope = current_function_decl;
18313       if (function_scope)
18314         push_function_context ();
18315
18316       /* Push the body of the function onto the lexer stack.  */
18317       cp_parser_push_lexer_for_tokens (parser, tokens);
18318
18319       /* Let the front end know that we going to be defining this
18320          function.  */
18321       start_preparsed_function (member_function, NULL_TREE,
18322                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18323
18324       /* Don't do access checking if it is a templated function.  */
18325       if (processing_template_decl)
18326         push_deferring_access_checks (dk_no_check);
18327
18328       /* Now, parse the body of the function.  */
18329       cp_parser_function_definition_after_declarator (parser,
18330                                                       /*inline_p=*/true);
18331
18332       if (processing_template_decl)
18333         pop_deferring_access_checks ();
18334
18335       /* Leave the scope of the containing function.  */
18336       if (function_scope)
18337         pop_function_context ();
18338       cp_parser_pop_lexer (parser);
18339     }
18340
18341   /* Remove any template parameters from the symbol table.  */
18342   maybe_end_member_template_processing ();
18343
18344   /* Restore the queue.  */
18345   parser->unparsed_functions_queues
18346     = TREE_CHAIN (parser->unparsed_functions_queues);
18347 }
18348
18349 /* If DECL contains any default args, remember it on the unparsed
18350    functions queue.  */
18351
18352 static void
18353 cp_parser_save_default_args (cp_parser* parser, tree decl)
18354 {
18355   tree probe;
18356
18357   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18358        probe;
18359        probe = TREE_CHAIN (probe))
18360     if (TREE_PURPOSE (probe))
18361       {
18362         TREE_PURPOSE (parser->unparsed_functions_queues)
18363           = tree_cons (current_class_type, decl,
18364                        TREE_PURPOSE (parser->unparsed_functions_queues));
18365         break;
18366       }
18367 }
18368
18369 /* FN is a FUNCTION_DECL which may contains a parameter with an
18370    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18371    assumes that the current scope is the scope in which the default
18372    argument should be processed.  */
18373
18374 static void
18375 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18376 {
18377   bool saved_local_variables_forbidden_p;
18378   tree parm;
18379
18380   /* While we're parsing the default args, we might (due to the
18381      statement expression extension) encounter more classes.  We want
18382      to handle them right away, but we don't want them getting mixed
18383      up with default args that are currently in the queue.  */
18384   parser->unparsed_functions_queues
18385     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18386
18387   /* Local variable names (and the `this' keyword) may not appear
18388      in a default argument.  */
18389   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18390   parser->local_variables_forbidden_p = true;
18391
18392   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18393        parm;
18394        parm = TREE_CHAIN (parm))
18395     {
18396       cp_token_cache *tokens;
18397       tree default_arg = TREE_PURPOSE (parm);
18398       tree parsed_arg;
18399       VEC(tree,gc) *insts;
18400       tree copy;
18401       unsigned ix;
18402
18403       if (!default_arg)
18404         continue;
18405
18406       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18407         /* This can happen for a friend declaration for a function
18408            already declared with default arguments.  */
18409         continue;
18410
18411        /* Push the saved tokens for the default argument onto the parser's
18412           lexer stack.  */
18413       tokens = DEFARG_TOKENS (default_arg);
18414       cp_parser_push_lexer_for_tokens (parser, tokens);
18415
18416       /* Parse the assignment-expression.  */
18417       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18418       if (parsed_arg == error_mark_node)
18419         {
18420           cp_parser_pop_lexer (parser);
18421           continue;
18422         }
18423
18424       if (!processing_template_decl)
18425         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18426
18427       TREE_PURPOSE (parm) = parsed_arg;
18428
18429       /* Update any instantiations we've already created.  */
18430       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18431            VEC_iterate (tree, insts, ix, copy); ix++)
18432         TREE_PURPOSE (copy) = parsed_arg;
18433
18434       /* If the token stream has not been completely used up, then
18435          there was extra junk after the end of the default
18436          argument.  */
18437       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18438         cp_parser_error (parser, "expected %<,%>");
18439
18440       /* Revert to the main lexer.  */
18441       cp_parser_pop_lexer (parser);
18442     }
18443
18444   /* Make sure no default arg is missing.  */
18445   check_default_args (fn);
18446
18447   /* Restore the state of local_variables_forbidden_p.  */
18448   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18449
18450   /* Restore the queue.  */
18451   parser->unparsed_functions_queues
18452     = TREE_CHAIN (parser->unparsed_functions_queues);
18453 }
18454
18455 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18456    either a TYPE or an expression, depending on the form of the
18457    input.  The KEYWORD indicates which kind of expression we have
18458    encountered.  */
18459
18460 static tree
18461 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18462 {
18463   tree expr = NULL_TREE;
18464   const char *saved_message;
18465   char *tmp;
18466   bool saved_integral_constant_expression_p;
18467   bool saved_non_integral_constant_expression_p;
18468   bool pack_expansion_p = false;
18469
18470   /* Types cannot be defined in a `sizeof' expression.  Save away the
18471      old message.  */
18472   saved_message = parser->type_definition_forbidden_message;
18473   /* And create the new one.  */
18474   tmp = concat ("types may not be defined in %<",
18475                 IDENTIFIER_POINTER (ridpointers[keyword]),
18476                 "%> expressions", NULL);
18477   parser->type_definition_forbidden_message = tmp;
18478
18479   /* The restrictions on constant-expressions do not apply inside
18480      sizeof expressions.  */
18481   saved_integral_constant_expression_p
18482     = parser->integral_constant_expression_p;
18483   saved_non_integral_constant_expression_p
18484     = parser->non_integral_constant_expression_p;
18485   parser->integral_constant_expression_p = false;
18486
18487   /* If it's a `...', then we are computing the length of a parameter
18488      pack.  */
18489   if (keyword == RID_SIZEOF
18490       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18491     {
18492       /* Consume the `...'.  */
18493       cp_lexer_consume_token (parser->lexer);
18494       maybe_warn_variadic_templates ();
18495
18496       /* Note that this is an expansion.  */
18497       pack_expansion_p = true;
18498     }
18499
18500   /* Do not actually evaluate the expression.  */
18501   ++skip_evaluation;
18502   /* If it's a `(', then we might be looking at the type-id
18503      construction.  */
18504   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18505     {
18506       tree type;
18507       bool saved_in_type_id_in_expr_p;
18508
18509       /* We can't be sure yet whether we're looking at a type-id or an
18510          expression.  */
18511       cp_parser_parse_tentatively (parser);
18512       /* Consume the `('.  */
18513       cp_lexer_consume_token (parser->lexer);
18514       /* Parse the type-id.  */
18515       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18516       parser->in_type_id_in_expr_p = true;
18517       type = cp_parser_type_id (parser);
18518       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18519       /* Now, look for the trailing `)'.  */
18520       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18521       /* If all went well, then we're done.  */
18522       if (cp_parser_parse_definitely (parser))
18523         {
18524           cp_decl_specifier_seq decl_specs;
18525
18526           /* Build a trivial decl-specifier-seq.  */
18527           clear_decl_specs (&decl_specs);
18528           decl_specs.type = type;
18529
18530           /* Call grokdeclarator to figure out what type this is.  */
18531           expr = grokdeclarator (NULL,
18532                                  &decl_specs,
18533                                  TYPENAME,
18534                                  /*initialized=*/0,
18535                                  /*attrlist=*/NULL);
18536         }
18537     }
18538
18539   /* If the type-id production did not work out, then we must be
18540      looking at the unary-expression production.  */
18541   if (!expr)
18542     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18543                                        /*cast_p=*/false, NULL);
18544
18545   if (pack_expansion_p)
18546     /* Build a pack expansion. */
18547     expr = make_pack_expansion (expr);
18548
18549   /* Go back to evaluating expressions.  */
18550   --skip_evaluation;
18551
18552   /* Free the message we created.  */
18553   free (tmp);
18554   /* And restore the old one.  */
18555   parser->type_definition_forbidden_message = saved_message;
18556   parser->integral_constant_expression_p
18557     = saved_integral_constant_expression_p;
18558   parser->non_integral_constant_expression_p
18559     = saved_non_integral_constant_expression_p;
18560
18561   return expr;
18562 }
18563
18564 /* If the current declaration has no declarator, return true.  */
18565
18566 static bool
18567 cp_parser_declares_only_class_p (cp_parser *parser)
18568 {
18569   /* If the next token is a `;' or a `,' then there is no
18570      declarator.  */
18571   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18572           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18573 }
18574
18575 /* Update the DECL_SPECS to reflect the storage class indicated by
18576    KEYWORD.  */
18577
18578 static void
18579 cp_parser_set_storage_class (cp_parser *parser,
18580                              cp_decl_specifier_seq *decl_specs,
18581                              enum rid keyword,
18582                              location_t location)
18583 {
18584   cp_storage_class storage_class;
18585
18586   if (parser->in_unbraced_linkage_specification_p)
18587     {
18588       error ("%Hinvalid use of %qD in linkage specification",
18589              &location, ridpointers[keyword]);
18590       return;
18591     }
18592   else if (decl_specs->storage_class != sc_none)
18593     {
18594       decl_specs->conflicting_specifiers_p = true;
18595       return;
18596     }
18597
18598   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18599       && decl_specs->specs[(int) ds_thread])
18600     {
18601       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18602       decl_specs->specs[(int) ds_thread] = 0;
18603     }
18604
18605   switch (keyword)
18606     {
18607     case RID_AUTO:
18608       storage_class = sc_auto;
18609       break;
18610     case RID_REGISTER:
18611       storage_class = sc_register;
18612       break;
18613     case RID_STATIC:
18614       storage_class = sc_static;
18615       break;
18616     case RID_EXTERN:
18617       storage_class = sc_extern;
18618       break;
18619     case RID_MUTABLE:
18620       storage_class = sc_mutable;
18621       break;
18622     default:
18623       gcc_unreachable ();
18624     }
18625   decl_specs->storage_class = storage_class;
18626
18627   /* A storage class specifier cannot be applied alongside a typedef 
18628      specifier. If there is a typedef specifier present then set 
18629      conflicting_specifiers_p which will trigger an error later
18630      on in grokdeclarator. */
18631   if (decl_specs->specs[(int)ds_typedef])
18632     decl_specs->conflicting_specifiers_p = true;
18633 }
18634
18635 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18636    is true, the type is a user-defined type; otherwise it is a
18637    built-in type specified by a keyword.  */
18638
18639 static void
18640 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18641                               tree type_spec,
18642                               location_t location,
18643                               bool user_defined_p)
18644 {
18645   decl_specs->any_specifiers_p = true;
18646
18647   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18648      (with, for example, in "typedef int wchar_t;") we remember that
18649      this is what happened.  In system headers, we ignore these
18650      declarations so that G++ can work with system headers that are not
18651      C++-safe.  */
18652   if (decl_specs->specs[(int) ds_typedef]
18653       && !user_defined_p
18654       && (type_spec == boolean_type_node
18655           || type_spec == char16_type_node
18656           || type_spec == char32_type_node
18657           || type_spec == wchar_type_node)
18658       && (decl_specs->type
18659           || decl_specs->specs[(int) ds_long]
18660           || decl_specs->specs[(int) ds_short]
18661           || decl_specs->specs[(int) ds_unsigned]
18662           || decl_specs->specs[(int) ds_signed]))
18663     {
18664       decl_specs->redefined_builtin_type = type_spec;
18665       if (!decl_specs->type)
18666         {
18667           decl_specs->type = type_spec;
18668           decl_specs->user_defined_type_p = false;
18669           decl_specs->type_location = location;
18670         }
18671     }
18672   else if (decl_specs->type)
18673     decl_specs->multiple_types_p = true;
18674   else
18675     {
18676       decl_specs->type = type_spec;
18677       decl_specs->user_defined_type_p = user_defined_p;
18678       decl_specs->redefined_builtin_type = NULL_TREE;
18679       decl_specs->type_location = location;
18680     }
18681 }
18682
18683 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18684    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18685
18686 static bool
18687 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18688 {
18689   return decl_specifiers->specs[(int) ds_friend] != 0;
18690 }
18691
18692 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18693    issue an error message indicating that TOKEN_DESC was expected.
18694
18695    Returns the token consumed, if the token had the appropriate type.
18696    Otherwise, returns NULL.  */
18697
18698 static cp_token *
18699 cp_parser_require (cp_parser* parser,
18700                    enum cpp_ttype type,
18701                    const char* token_desc)
18702 {
18703   if (cp_lexer_next_token_is (parser->lexer, type))
18704     return cp_lexer_consume_token (parser->lexer);
18705   else
18706     {
18707       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18708       if (!cp_parser_simulate_error (parser))
18709         {
18710           char *message = concat ("expected ", token_desc, NULL);
18711           cp_parser_error (parser, message);
18712           free (message);
18713         }
18714       return NULL;
18715     }
18716 }
18717
18718 /* An error message is produced if the next token is not '>'.
18719    All further tokens are skipped until the desired token is
18720    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18721
18722 static void
18723 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18724 {
18725   /* Current level of '< ... >'.  */
18726   unsigned level = 0;
18727   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18728   unsigned nesting_depth = 0;
18729
18730   /* Are we ready, yet?  If not, issue error message.  */
18731   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18732     return;
18733
18734   /* Skip tokens until the desired token is found.  */
18735   while (true)
18736     {
18737       /* Peek at the next token.  */
18738       switch (cp_lexer_peek_token (parser->lexer)->type)
18739         {
18740         case CPP_LESS:
18741           if (!nesting_depth)
18742             ++level;
18743           break;
18744
18745         case CPP_RSHIFT:
18746           if (cxx_dialect == cxx98)
18747             /* C++0x views the `>>' operator as two `>' tokens, but
18748                C++98 does not. */
18749             break;
18750           else if (!nesting_depth && level-- == 0)
18751             {
18752               /* We've hit a `>>' where the first `>' closes the
18753                  template argument list, and the second `>' is
18754                  spurious.  Just consume the `>>' and stop; we've
18755                  already produced at least one error.  */
18756               cp_lexer_consume_token (parser->lexer);
18757               return;
18758             }
18759           /* Fall through for C++0x, so we handle the second `>' in
18760              the `>>'.  */
18761
18762         case CPP_GREATER:
18763           if (!nesting_depth && level-- == 0)
18764             {
18765               /* We've reached the token we want, consume it and stop.  */
18766               cp_lexer_consume_token (parser->lexer);
18767               return;
18768             }
18769           break;
18770
18771         case CPP_OPEN_PAREN:
18772         case CPP_OPEN_SQUARE:
18773           ++nesting_depth;
18774           break;
18775
18776         case CPP_CLOSE_PAREN:
18777         case CPP_CLOSE_SQUARE:
18778           if (nesting_depth-- == 0)
18779             return;
18780           break;
18781
18782         case CPP_EOF:
18783         case CPP_PRAGMA_EOL:
18784         case CPP_SEMICOLON:
18785         case CPP_OPEN_BRACE:
18786         case CPP_CLOSE_BRACE:
18787           /* The '>' was probably forgotten, don't look further.  */
18788           return;
18789
18790         default:
18791           break;
18792         }
18793
18794       /* Consume this token.  */
18795       cp_lexer_consume_token (parser->lexer);
18796     }
18797 }
18798
18799 /* If the next token is the indicated keyword, consume it.  Otherwise,
18800    issue an error message indicating that TOKEN_DESC was expected.
18801
18802    Returns the token consumed, if the token had the appropriate type.
18803    Otherwise, returns NULL.  */
18804
18805 static cp_token *
18806 cp_parser_require_keyword (cp_parser* parser,
18807                            enum rid keyword,
18808                            const char* token_desc)
18809 {
18810   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18811
18812   if (token && token->keyword != keyword)
18813     {
18814       dyn_string_t error_msg;
18815
18816       /* Format the error message.  */
18817       error_msg = dyn_string_new (0);
18818       dyn_string_append_cstr (error_msg, "expected ");
18819       dyn_string_append_cstr (error_msg, token_desc);
18820       cp_parser_error (parser, error_msg->s);
18821       dyn_string_delete (error_msg);
18822       return NULL;
18823     }
18824
18825   return token;
18826 }
18827
18828 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18829    function-definition.  */
18830
18831 static bool
18832 cp_parser_token_starts_function_definition_p (cp_token* token)
18833 {
18834   return (/* An ordinary function-body begins with an `{'.  */
18835           token->type == CPP_OPEN_BRACE
18836           /* A ctor-initializer begins with a `:'.  */
18837           || token->type == CPP_COLON
18838           /* A function-try-block begins with `try'.  */
18839           || token->keyword == RID_TRY
18840           /* The named return value extension begins with `return'.  */
18841           || token->keyword == RID_RETURN);
18842 }
18843
18844 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18845    definition.  */
18846
18847 static bool
18848 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18849 {
18850   cp_token *token;
18851
18852   token = cp_lexer_peek_token (parser->lexer);
18853   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18854 }
18855
18856 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18857    C++0x) ending a template-argument.  */
18858
18859 static bool
18860 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18861 {
18862   cp_token *token;
18863
18864   token = cp_lexer_peek_token (parser->lexer);
18865   return (token->type == CPP_COMMA 
18866           || token->type == CPP_GREATER
18867           || token->type == CPP_ELLIPSIS
18868           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18869 }
18870
18871 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18872    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18873
18874 static bool
18875 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18876                                                      size_t n)
18877 {
18878   cp_token *token;
18879
18880   token = cp_lexer_peek_nth_token (parser->lexer, n);
18881   if (token->type == CPP_LESS)
18882     return true;
18883   /* Check for the sequence `<::' in the original code. It would be lexed as
18884      `[:', where `[' is a digraph, and there is no whitespace before
18885      `:'.  */
18886   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18887     {
18888       cp_token *token2;
18889       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18890       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18891         return true;
18892     }
18893   return false;
18894 }
18895
18896 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18897    or none_type otherwise.  */
18898
18899 static enum tag_types
18900 cp_parser_token_is_class_key (cp_token* token)
18901 {
18902   switch (token->keyword)
18903     {
18904     case RID_CLASS:
18905       return class_type;
18906     case RID_STRUCT:
18907       return record_type;
18908     case RID_UNION:
18909       return union_type;
18910
18911     default:
18912       return none_type;
18913     }
18914 }
18915
18916 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18917
18918 static void
18919 cp_parser_check_class_key (enum tag_types class_key, tree type)
18920 {
18921   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18922     permerror (input_location, "%qs tag used in naming %q#T",
18923             class_key == union_type ? "union"
18924              : class_key == record_type ? "struct" : "class",
18925              type);
18926 }
18927
18928 /* Issue an error message if DECL is redeclared with different
18929    access than its original declaration [class.access.spec/3].
18930    This applies to nested classes and nested class templates.
18931    [class.mem/1].  */
18932
18933 static void
18934 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18935 {
18936   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18937     return;
18938
18939   if ((TREE_PRIVATE (decl)
18940        != (current_access_specifier == access_private_node))
18941       || (TREE_PROTECTED (decl)
18942           != (current_access_specifier == access_protected_node)))
18943     error ("%H%qD redeclared with different access", &location, decl);
18944 }
18945
18946 /* Look for the `template' keyword, as a syntactic disambiguator.
18947    Return TRUE iff it is present, in which case it will be
18948    consumed.  */
18949
18950 static bool
18951 cp_parser_optional_template_keyword (cp_parser *parser)
18952 {
18953   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18954     {
18955       /* The `template' keyword can only be used within templates;
18956          outside templates the parser can always figure out what is a
18957          template and what is not.  */
18958       if (!processing_template_decl)
18959         {
18960           cp_token *token = cp_lexer_peek_token (parser->lexer);
18961           error ("%H%<template%> (as a disambiguator) is only allowed "
18962                  "within templates", &token->location);
18963           /* If this part of the token stream is rescanned, the same
18964              error message would be generated.  So, we purge the token
18965              from the stream.  */
18966           cp_lexer_purge_token (parser->lexer);
18967           return false;
18968         }
18969       else
18970         {
18971           /* Consume the `template' keyword.  */
18972           cp_lexer_consume_token (parser->lexer);
18973           return true;
18974         }
18975     }
18976
18977   return false;
18978 }
18979
18980 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18981    set PARSER->SCOPE, and perform other related actions.  */
18982
18983 static void
18984 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18985 {
18986   int i;
18987   struct tree_check *check_value;
18988   deferred_access_check *chk;
18989   VEC (deferred_access_check,gc) *checks;
18990
18991   /* Get the stored value.  */
18992   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18993   /* Perform any access checks that were deferred.  */
18994   checks = check_value->checks;
18995   if (checks)
18996     {
18997       for (i = 0 ;
18998            VEC_iterate (deferred_access_check, checks, i, chk) ;
18999            ++i)
19000         {
19001           perform_or_defer_access_check (chk->binfo,
19002                                          chk->decl,
19003                                          chk->diag_decl);
19004         }
19005     }
19006   /* Set the scope from the stored value.  */
19007   parser->scope = check_value->value;
19008   parser->qualifying_scope = check_value->qualifying_scope;
19009   parser->object_scope = NULL_TREE;
19010 }
19011
19012 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19013    encounter the end of a block before what we were looking for.  */
19014
19015 static bool
19016 cp_parser_cache_group (cp_parser *parser,
19017                        enum cpp_ttype end,
19018                        unsigned depth)
19019 {
19020   while (true)
19021     {
19022       cp_token *token = cp_lexer_peek_token (parser->lexer);
19023
19024       /* Abort a parenthesized expression if we encounter a semicolon.  */
19025       if ((end == CPP_CLOSE_PAREN || depth == 0)
19026           && token->type == CPP_SEMICOLON)
19027         return true;
19028       /* If we've reached the end of the file, stop.  */
19029       if (token->type == CPP_EOF
19030           || (end != CPP_PRAGMA_EOL
19031               && token->type == CPP_PRAGMA_EOL))
19032         return true;
19033       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19034         /* We've hit the end of an enclosing block, so there's been some
19035            kind of syntax error.  */
19036         return true;
19037
19038       /* Consume the token.  */
19039       cp_lexer_consume_token (parser->lexer);
19040       /* See if it starts a new group.  */
19041       if (token->type == CPP_OPEN_BRACE)
19042         {
19043           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19044           /* In theory this should probably check end == '}', but
19045              cp_parser_save_member_function_body needs it to exit
19046              after either '}' or ')' when called with ')'.  */
19047           if (depth == 0)
19048             return false;
19049         }
19050       else if (token->type == CPP_OPEN_PAREN)
19051         {
19052           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19053           if (depth == 0 && end == CPP_CLOSE_PAREN)
19054             return false;
19055         }
19056       else if (token->type == CPP_PRAGMA)
19057         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19058       else if (token->type == end)
19059         return false;
19060     }
19061 }
19062
19063 /* Begin parsing tentatively.  We always save tokens while parsing
19064    tentatively so that if the tentative parsing fails we can restore the
19065    tokens.  */
19066
19067 static void
19068 cp_parser_parse_tentatively (cp_parser* parser)
19069 {
19070   /* Enter a new parsing context.  */
19071   parser->context = cp_parser_context_new (parser->context);
19072   /* Begin saving tokens.  */
19073   cp_lexer_save_tokens (parser->lexer);
19074   /* In order to avoid repetitive access control error messages,
19075      access checks are queued up until we are no longer parsing
19076      tentatively.  */
19077   push_deferring_access_checks (dk_deferred);
19078 }
19079
19080 /* Commit to the currently active tentative parse.  */
19081
19082 static void
19083 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19084 {
19085   cp_parser_context *context;
19086   cp_lexer *lexer;
19087
19088   /* Mark all of the levels as committed.  */
19089   lexer = parser->lexer;
19090   for (context = parser->context; context->next; context = context->next)
19091     {
19092       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19093         break;
19094       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19095       while (!cp_lexer_saving_tokens (lexer))
19096         lexer = lexer->next;
19097       cp_lexer_commit_tokens (lexer);
19098     }
19099 }
19100
19101 /* Abort the currently active tentative parse.  All consumed tokens
19102    will be rolled back, and no diagnostics will be issued.  */
19103
19104 static void
19105 cp_parser_abort_tentative_parse (cp_parser* parser)
19106 {
19107   cp_parser_simulate_error (parser);
19108   /* Now, pretend that we want to see if the construct was
19109      successfully parsed.  */
19110   cp_parser_parse_definitely (parser);
19111 }
19112
19113 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19114    token stream.  Otherwise, commit to the tokens we have consumed.
19115    Returns true if no error occurred; false otherwise.  */
19116
19117 static bool
19118 cp_parser_parse_definitely (cp_parser* parser)
19119 {
19120   bool error_occurred;
19121   cp_parser_context *context;
19122
19123   /* Remember whether or not an error occurred, since we are about to
19124      destroy that information.  */
19125   error_occurred = cp_parser_error_occurred (parser);
19126   /* Remove the topmost context from the stack.  */
19127   context = parser->context;
19128   parser->context = context->next;
19129   /* If no parse errors occurred, commit to the tentative parse.  */
19130   if (!error_occurred)
19131     {
19132       /* Commit to the tokens read tentatively, unless that was
19133          already done.  */
19134       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19135         cp_lexer_commit_tokens (parser->lexer);
19136
19137       pop_to_parent_deferring_access_checks ();
19138     }
19139   /* Otherwise, if errors occurred, roll back our state so that things
19140      are just as they were before we began the tentative parse.  */
19141   else
19142     {
19143       cp_lexer_rollback_tokens (parser->lexer);
19144       pop_deferring_access_checks ();
19145     }
19146   /* Add the context to the front of the free list.  */
19147   context->next = cp_parser_context_free_list;
19148   cp_parser_context_free_list = context;
19149
19150   return !error_occurred;
19151 }
19152
19153 /* Returns true if we are parsing tentatively and are not committed to
19154    this tentative parse.  */
19155
19156 static bool
19157 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19158 {
19159   return (cp_parser_parsing_tentatively (parser)
19160           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19161 }
19162
19163 /* Returns nonzero iff an error has occurred during the most recent
19164    tentative parse.  */
19165
19166 static bool
19167 cp_parser_error_occurred (cp_parser* parser)
19168 {
19169   return (cp_parser_parsing_tentatively (parser)
19170           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19171 }
19172
19173 /* Returns nonzero if GNU extensions are allowed.  */
19174
19175 static bool
19176 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19177 {
19178   return parser->allow_gnu_extensions_p;
19179 }
19180 \f
19181 /* Objective-C++ Productions */
19182
19183
19184 /* Parse an Objective-C expression, which feeds into a primary-expression
19185    above.
19186
19187    objc-expression:
19188      objc-message-expression
19189      objc-string-literal
19190      objc-encode-expression
19191      objc-protocol-expression
19192      objc-selector-expression
19193
19194   Returns a tree representation of the expression.  */
19195
19196 static tree
19197 cp_parser_objc_expression (cp_parser* parser)
19198 {
19199   /* Try to figure out what kind of declaration is present.  */
19200   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19201
19202   switch (kwd->type)
19203     {
19204     case CPP_OPEN_SQUARE:
19205       return cp_parser_objc_message_expression (parser);
19206
19207     case CPP_OBJC_STRING:
19208       kwd = cp_lexer_consume_token (parser->lexer);
19209       return objc_build_string_object (kwd->u.value);
19210
19211     case CPP_KEYWORD:
19212       switch (kwd->keyword)
19213         {
19214         case RID_AT_ENCODE:
19215           return cp_parser_objc_encode_expression (parser);
19216
19217         case RID_AT_PROTOCOL:
19218           return cp_parser_objc_protocol_expression (parser);
19219
19220         case RID_AT_SELECTOR:
19221           return cp_parser_objc_selector_expression (parser);
19222
19223         default:
19224           break;
19225         }
19226     default:
19227       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19228              &kwd->location, kwd->u.value);
19229       cp_parser_skip_to_end_of_block_or_statement (parser);
19230     }
19231
19232   return error_mark_node;
19233 }
19234
19235 /* Parse an Objective-C message expression.
19236
19237    objc-message-expression:
19238      [ objc-message-receiver objc-message-args ]
19239
19240    Returns a representation of an Objective-C message.  */
19241
19242 static tree
19243 cp_parser_objc_message_expression (cp_parser* parser)
19244 {
19245   tree receiver, messageargs;
19246
19247   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19248   receiver = cp_parser_objc_message_receiver (parser);
19249   messageargs = cp_parser_objc_message_args (parser);
19250   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19251
19252   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19253 }
19254
19255 /* Parse an objc-message-receiver.
19256
19257    objc-message-receiver:
19258      expression
19259      simple-type-specifier
19260
19261   Returns a representation of the type or expression.  */
19262
19263 static tree
19264 cp_parser_objc_message_receiver (cp_parser* parser)
19265 {
19266   tree rcv;
19267
19268   /* An Objective-C message receiver may be either (1) a type
19269      or (2) an expression.  */
19270   cp_parser_parse_tentatively (parser);
19271   rcv = cp_parser_expression (parser, false, NULL);
19272
19273   if (cp_parser_parse_definitely (parser))
19274     return rcv;
19275
19276   rcv = cp_parser_simple_type_specifier (parser,
19277                                          /*decl_specs=*/NULL,
19278                                          CP_PARSER_FLAGS_NONE);
19279
19280   return objc_get_class_reference (rcv);
19281 }
19282
19283 /* Parse the arguments and selectors comprising an Objective-C message.
19284
19285    objc-message-args:
19286      objc-selector
19287      objc-selector-args
19288      objc-selector-args , objc-comma-args
19289
19290    objc-selector-args:
19291      objc-selector [opt] : assignment-expression
19292      objc-selector-args objc-selector [opt] : assignment-expression
19293
19294    objc-comma-args:
19295      assignment-expression
19296      objc-comma-args , assignment-expression
19297
19298    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19299    selector arguments and TREE_VALUE containing a list of comma
19300    arguments.  */
19301
19302 static tree
19303 cp_parser_objc_message_args (cp_parser* parser)
19304 {
19305   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19306   bool maybe_unary_selector_p = true;
19307   cp_token *token = cp_lexer_peek_token (parser->lexer);
19308
19309   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19310     {
19311       tree selector = NULL_TREE, arg;
19312
19313       if (token->type != CPP_COLON)
19314         selector = cp_parser_objc_selector (parser);
19315
19316       /* Detect if we have a unary selector.  */
19317       if (maybe_unary_selector_p
19318           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19319         return build_tree_list (selector, NULL_TREE);
19320
19321       maybe_unary_selector_p = false;
19322       cp_parser_require (parser, CPP_COLON, "%<:%>");
19323       arg = cp_parser_assignment_expression (parser, false, NULL);
19324
19325       sel_args
19326         = chainon (sel_args,
19327                    build_tree_list (selector, arg));
19328
19329       token = cp_lexer_peek_token (parser->lexer);
19330     }
19331
19332   /* Handle non-selector arguments, if any. */
19333   while (token->type == CPP_COMMA)
19334     {
19335       tree arg;
19336
19337       cp_lexer_consume_token (parser->lexer);
19338       arg = cp_parser_assignment_expression (parser, false, NULL);
19339
19340       addl_args
19341         = chainon (addl_args,
19342                    build_tree_list (NULL_TREE, arg));
19343
19344       token = cp_lexer_peek_token (parser->lexer);
19345     }
19346
19347   return build_tree_list (sel_args, addl_args);
19348 }
19349
19350 /* Parse an Objective-C encode expression.
19351
19352    objc-encode-expression:
19353      @encode objc-typename
19354
19355    Returns an encoded representation of the type argument.  */
19356
19357 static tree
19358 cp_parser_objc_encode_expression (cp_parser* parser)
19359 {
19360   tree type;
19361   cp_token *token;
19362
19363   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19364   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19365   token = cp_lexer_peek_token (parser->lexer);
19366   type = complete_type (cp_parser_type_id (parser));
19367   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19368
19369   if (!type)
19370     {
19371       error ("%H%<@encode%> must specify a type as an argument",
19372              &token->location);
19373       return error_mark_node;
19374     }
19375
19376   return objc_build_encode_expr (type);
19377 }
19378
19379 /* Parse an Objective-C @defs expression.  */
19380
19381 static tree
19382 cp_parser_objc_defs_expression (cp_parser *parser)
19383 {
19384   tree name;
19385
19386   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19387   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19388   name = cp_parser_identifier (parser);
19389   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19390
19391   return objc_get_class_ivars (name);
19392 }
19393
19394 /* Parse an Objective-C protocol expression.
19395
19396   objc-protocol-expression:
19397     @protocol ( identifier )
19398
19399   Returns a representation of the protocol expression.  */
19400
19401 static tree
19402 cp_parser_objc_protocol_expression (cp_parser* parser)
19403 {
19404   tree proto;
19405
19406   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19407   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19408   proto = cp_parser_identifier (parser);
19409   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19410
19411   return objc_build_protocol_expr (proto);
19412 }
19413
19414 /* Parse an Objective-C selector expression.
19415
19416    objc-selector-expression:
19417      @selector ( objc-method-signature )
19418
19419    objc-method-signature:
19420      objc-selector
19421      objc-selector-seq
19422
19423    objc-selector-seq:
19424      objc-selector :
19425      objc-selector-seq objc-selector :
19426
19427   Returns a representation of the method selector.  */
19428
19429 static tree
19430 cp_parser_objc_selector_expression (cp_parser* parser)
19431 {
19432   tree sel_seq = NULL_TREE;
19433   bool maybe_unary_selector_p = true;
19434   cp_token *token;
19435
19436   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19437   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19438   token = cp_lexer_peek_token (parser->lexer);
19439
19440   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19441          || token->type == CPP_SCOPE)
19442     {
19443       tree selector = NULL_TREE;
19444
19445       if (token->type != CPP_COLON
19446           || token->type == CPP_SCOPE)
19447         selector = cp_parser_objc_selector (parser);
19448
19449       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19450           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19451         {
19452           /* Detect if we have a unary selector.  */
19453           if (maybe_unary_selector_p)
19454             {
19455               sel_seq = selector;
19456               goto finish_selector;
19457             }
19458           else
19459             {
19460               cp_parser_error (parser, "expected %<:%>");
19461             }
19462         }
19463       maybe_unary_selector_p = false;
19464       token = cp_lexer_consume_token (parser->lexer);
19465
19466       if (token->type == CPP_SCOPE)
19467         {
19468           sel_seq
19469             = chainon (sel_seq,
19470                        build_tree_list (selector, NULL_TREE));
19471           sel_seq
19472             = chainon (sel_seq,
19473                        build_tree_list (NULL_TREE, NULL_TREE));
19474         }
19475       else
19476         sel_seq
19477           = chainon (sel_seq,
19478                      build_tree_list (selector, NULL_TREE));
19479
19480       token = cp_lexer_peek_token (parser->lexer);
19481     }
19482
19483  finish_selector:
19484   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19485
19486   return objc_build_selector_expr (sel_seq);
19487 }
19488
19489 /* Parse a list of identifiers.
19490
19491    objc-identifier-list:
19492      identifier
19493      objc-identifier-list , identifier
19494
19495    Returns a TREE_LIST of identifier nodes.  */
19496
19497 static tree
19498 cp_parser_objc_identifier_list (cp_parser* parser)
19499 {
19500   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19501   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19502
19503   while (sep->type == CPP_COMMA)
19504     {
19505       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19506       list = chainon (list,
19507                       build_tree_list (NULL_TREE,
19508                                        cp_parser_identifier (parser)));
19509       sep = cp_lexer_peek_token (parser->lexer);
19510     }
19511
19512   return list;
19513 }
19514
19515 /* Parse an Objective-C alias declaration.
19516
19517    objc-alias-declaration:
19518      @compatibility_alias identifier identifier ;
19519
19520    This function registers the alias mapping with the Objective-C front end.
19521    It returns nothing.  */
19522
19523 static void
19524 cp_parser_objc_alias_declaration (cp_parser* parser)
19525 {
19526   tree alias, orig;
19527
19528   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19529   alias = cp_parser_identifier (parser);
19530   orig = cp_parser_identifier (parser);
19531   objc_declare_alias (alias, orig);
19532   cp_parser_consume_semicolon_at_end_of_statement (parser);
19533 }
19534
19535 /* Parse an Objective-C class forward-declaration.
19536
19537    objc-class-declaration:
19538      @class objc-identifier-list ;
19539
19540    The function registers the forward declarations with the Objective-C
19541    front end.  It returns nothing.  */
19542
19543 static void
19544 cp_parser_objc_class_declaration (cp_parser* parser)
19545 {
19546   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19547   objc_declare_class (cp_parser_objc_identifier_list (parser));
19548   cp_parser_consume_semicolon_at_end_of_statement (parser);
19549 }
19550
19551 /* Parse a list of Objective-C protocol references.
19552
19553    objc-protocol-refs-opt:
19554      objc-protocol-refs [opt]
19555
19556    objc-protocol-refs:
19557      < objc-identifier-list >
19558
19559    Returns a TREE_LIST of identifiers, if any.  */
19560
19561 static tree
19562 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19563 {
19564   tree protorefs = NULL_TREE;
19565
19566   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19567     {
19568       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19569       protorefs = cp_parser_objc_identifier_list (parser);
19570       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19571     }
19572
19573   return protorefs;
19574 }
19575
19576 /* Parse a Objective-C visibility specification.  */
19577
19578 static void
19579 cp_parser_objc_visibility_spec (cp_parser* parser)
19580 {
19581   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19582
19583   switch (vis->keyword)
19584     {
19585     case RID_AT_PRIVATE:
19586       objc_set_visibility (2);
19587       break;
19588     case RID_AT_PROTECTED:
19589       objc_set_visibility (0);
19590       break;
19591     case RID_AT_PUBLIC:
19592       objc_set_visibility (1);
19593       break;
19594     default:
19595       return;
19596     }
19597
19598   /* Eat '@private'/'@protected'/'@public'.  */
19599   cp_lexer_consume_token (parser->lexer);
19600 }
19601
19602 /* Parse an Objective-C method type.  */
19603
19604 static void
19605 cp_parser_objc_method_type (cp_parser* parser)
19606 {
19607   objc_set_method_type
19608    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19609     ? PLUS_EXPR
19610     : MINUS_EXPR);
19611 }
19612
19613 /* Parse an Objective-C protocol qualifier.  */
19614
19615 static tree
19616 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19617 {
19618   tree quals = NULL_TREE, node;
19619   cp_token *token = cp_lexer_peek_token (parser->lexer);
19620
19621   node = token->u.value;
19622
19623   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19624          && (node == ridpointers [(int) RID_IN]
19625              || node == ridpointers [(int) RID_OUT]
19626              || node == ridpointers [(int) RID_INOUT]
19627              || node == ridpointers [(int) RID_BYCOPY]
19628              || node == ridpointers [(int) RID_BYREF]
19629              || node == ridpointers [(int) RID_ONEWAY]))
19630     {
19631       quals = tree_cons (NULL_TREE, node, quals);
19632       cp_lexer_consume_token (parser->lexer);
19633       token = cp_lexer_peek_token (parser->lexer);
19634       node = token->u.value;
19635     }
19636
19637   return quals;
19638 }
19639
19640 /* Parse an Objective-C typename.  */
19641
19642 static tree
19643 cp_parser_objc_typename (cp_parser* parser)
19644 {
19645   tree type_name = NULL_TREE;
19646
19647   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19648     {
19649       tree proto_quals, cp_type = NULL_TREE;
19650
19651       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19652       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19653
19654       /* An ObjC type name may consist of just protocol qualifiers, in which
19655          case the type shall default to 'id'.  */
19656       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19657         cp_type = cp_parser_type_id (parser);
19658
19659       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19660       type_name = build_tree_list (proto_quals, cp_type);
19661     }
19662
19663   return type_name;
19664 }
19665
19666 /* Check to see if TYPE refers to an Objective-C selector name.  */
19667
19668 static bool
19669 cp_parser_objc_selector_p (enum cpp_ttype type)
19670 {
19671   return (type == CPP_NAME || type == CPP_KEYWORD
19672           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19673           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19674           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19675           || type == CPP_XOR || type == CPP_XOR_EQ);
19676 }
19677
19678 /* Parse an Objective-C selector.  */
19679
19680 static tree
19681 cp_parser_objc_selector (cp_parser* parser)
19682 {
19683   cp_token *token = cp_lexer_consume_token (parser->lexer);
19684
19685   if (!cp_parser_objc_selector_p (token->type))
19686     {
19687       error ("%Hinvalid Objective-C++ selector name", &token->location);
19688       return error_mark_node;
19689     }
19690
19691   /* C++ operator names are allowed to appear in ObjC selectors.  */
19692   switch (token->type)
19693     {
19694     case CPP_AND_AND: return get_identifier ("and");
19695     case CPP_AND_EQ: return get_identifier ("and_eq");
19696     case CPP_AND: return get_identifier ("bitand");
19697     case CPP_OR: return get_identifier ("bitor");
19698     case CPP_COMPL: return get_identifier ("compl");
19699     case CPP_NOT: return get_identifier ("not");
19700     case CPP_NOT_EQ: return get_identifier ("not_eq");
19701     case CPP_OR_OR: return get_identifier ("or");
19702     case CPP_OR_EQ: return get_identifier ("or_eq");
19703     case CPP_XOR: return get_identifier ("xor");
19704     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19705     default: return token->u.value;
19706     }
19707 }
19708
19709 /* Parse an Objective-C params list.  */
19710
19711 static tree
19712 cp_parser_objc_method_keyword_params (cp_parser* parser)
19713 {
19714   tree params = NULL_TREE;
19715   bool maybe_unary_selector_p = true;
19716   cp_token *token = cp_lexer_peek_token (parser->lexer);
19717
19718   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19719     {
19720       tree selector = NULL_TREE, type_name, identifier;
19721
19722       if (token->type != CPP_COLON)
19723         selector = cp_parser_objc_selector (parser);
19724
19725       /* Detect if we have a unary selector.  */
19726       if (maybe_unary_selector_p
19727           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19728         return selector;
19729
19730       maybe_unary_selector_p = false;
19731       cp_parser_require (parser, CPP_COLON, "%<:%>");
19732       type_name = cp_parser_objc_typename (parser);
19733       identifier = cp_parser_identifier (parser);
19734
19735       params
19736         = chainon (params,
19737                    objc_build_keyword_decl (selector,
19738                                             type_name,
19739                                             identifier));
19740
19741       token = cp_lexer_peek_token (parser->lexer);
19742     }
19743
19744   return params;
19745 }
19746
19747 /* Parse the non-keyword Objective-C params.  */
19748
19749 static tree
19750 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19751 {
19752   tree params = make_node (TREE_LIST);
19753   cp_token *token = cp_lexer_peek_token (parser->lexer);
19754   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19755
19756   while (token->type == CPP_COMMA)
19757     {
19758       cp_parameter_declarator *parmdecl;
19759       tree parm;
19760
19761       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19762       token = cp_lexer_peek_token (parser->lexer);
19763
19764       if (token->type == CPP_ELLIPSIS)
19765         {
19766           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19767           *ellipsisp = true;
19768           break;
19769         }
19770
19771       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19772       parm = grokdeclarator (parmdecl->declarator,
19773                              &parmdecl->decl_specifiers,
19774                              PARM, /*initialized=*/0,
19775                              /*attrlist=*/NULL);
19776
19777       chainon (params, build_tree_list (NULL_TREE, parm));
19778       token = cp_lexer_peek_token (parser->lexer);
19779     }
19780
19781   return params;
19782 }
19783
19784 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19785
19786 static void
19787 cp_parser_objc_interstitial_code (cp_parser* parser)
19788 {
19789   cp_token *token = cp_lexer_peek_token (parser->lexer);
19790
19791   /* If the next token is `extern' and the following token is a string
19792      literal, then we have a linkage specification.  */
19793   if (token->keyword == RID_EXTERN
19794       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19795     cp_parser_linkage_specification (parser);
19796   /* Handle #pragma, if any.  */
19797   else if (token->type == CPP_PRAGMA)
19798     cp_parser_pragma (parser, pragma_external);
19799   /* Allow stray semicolons.  */
19800   else if (token->type == CPP_SEMICOLON)
19801     cp_lexer_consume_token (parser->lexer);
19802   /* Finally, try to parse a block-declaration, or a function-definition.  */
19803   else
19804     cp_parser_block_declaration (parser, /*statement_p=*/false);
19805 }
19806
19807 /* Parse a method signature.  */
19808
19809 static tree
19810 cp_parser_objc_method_signature (cp_parser* parser)
19811 {
19812   tree rettype, kwdparms, optparms;
19813   bool ellipsis = false;
19814
19815   cp_parser_objc_method_type (parser);
19816   rettype = cp_parser_objc_typename (parser);
19817   kwdparms = cp_parser_objc_method_keyword_params (parser);
19818   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19819
19820   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19821 }
19822
19823 /* Pars an Objective-C method prototype list.  */
19824
19825 static void
19826 cp_parser_objc_method_prototype_list (cp_parser* parser)
19827 {
19828   cp_token *token = cp_lexer_peek_token (parser->lexer);
19829
19830   while (token->keyword != RID_AT_END)
19831     {
19832       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19833         {
19834           objc_add_method_declaration
19835            (cp_parser_objc_method_signature (parser));
19836           cp_parser_consume_semicolon_at_end_of_statement (parser);
19837         }
19838       else
19839         /* Allow for interspersed non-ObjC++ code.  */
19840         cp_parser_objc_interstitial_code (parser);
19841
19842       token = cp_lexer_peek_token (parser->lexer);
19843     }
19844
19845   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19846   objc_finish_interface ();
19847 }
19848
19849 /* Parse an Objective-C method definition list.  */
19850
19851 static void
19852 cp_parser_objc_method_definition_list (cp_parser* parser)
19853 {
19854   cp_token *token = cp_lexer_peek_token (parser->lexer);
19855
19856   while (token->keyword != RID_AT_END)
19857     {
19858       tree meth;
19859
19860       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19861         {
19862           push_deferring_access_checks (dk_deferred);
19863           objc_start_method_definition
19864            (cp_parser_objc_method_signature (parser));
19865
19866           /* For historical reasons, we accept an optional semicolon.  */
19867           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19868             cp_lexer_consume_token (parser->lexer);
19869
19870           perform_deferred_access_checks ();
19871           stop_deferring_access_checks ();
19872           meth = cp_parser_function_definition_after_declarator (parser,
19873                                                                  false);
19874           pop_deferring_access_checks ();
19875           objc_finish_method_definition (meth);
19876         }
19877       else
19878         /* Allow for interspersed non-ObjC++ code.  */
19879         cp_parser_objc_interstitial_code (parser);
19880
19881       token = cp_lexer_peek_token (parser->lexer);
19882     }
19883
19884   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19885   objc_finish_implementation ();
19886 }
19887
19888 /* Parse Objective-C ivars.  */
19889
19890 static void
19891 cp_parser_objc_class_ivars (cp_parser* parser)
19892 {
19893   cp_token *token = cp_lexer_peek_token (parser->lexer);
19894
19895   if (token->type != CPP_OPEN_BRACE)
19896     return;     /* No ivars specified.  */
19897
19898   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19899   token = cp_lexer_peek_token (parser->lexer);
19900
19901   while (token->type != CPP_CLOSE_BRACE)
19902     {
19903       cp_decl_specifier_seq declspecs;
19904       int decl_class_or_enum_p;
19905       tree prefix_attributes;
19906
19907       cp_parser_objc_visibility_spec (parser);
19908
19909       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19910         break;
19911
19912       cp_parser_decl_specifier_seq (parser,
19913                                     CP_PARSER_FLAGS_OPTIONAL,
19914                                     &declspecs,
19915                                     &decl_class_or_enum_p);
19916       prefix_attributes = declspecs.attributes;
19917       declspecs.attributes = NULL_TREE;
19918
19919       /* Keep going until we hit the `;' at the end of the
19920          declaration.  */
19921       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19922         {
19923           tree width = NULL_TREE, attributes, first_attribute, decl;
19924           cp_declarator *declarator = NULL;
19925           int ctor_dtor_or_conv_p;
19926
19927           /* Check for a (possibly unnamed) bitfield declaration.  */
19928           token = cp_lexer_peek_token (parser->lexer);
19929           if (token->type == CPP_COLON)
19930             goto eat_colon;
19931
19932           if (token->type == CPP_NAME
19933               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19934                   == CPP_COLON))
19935             {
19936               /* Get the name of the bitfield.  */
19937               declarator = make_id_declarator (NULL_TREE,
19938                                                cp_parser_identifier (parser),
19939                                                sfk_none);
19940
19941              eat_colon:
19942               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19943               /* Get the width of the bitfield.  */
19944               width
19945                 = cp_parser_constant_expression (parser,
19946                                                  /*allow_non_constant=*/false,
19947                                                  NULL);
19948             }
19949           else
19950             {
19951               /* Parse the declarator.  */
19952               declarator
19953                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19954                                         &ctor_dtor_or_conv_p,
19955                                         /*parenthesized_p=*/NULL,
19956                                         /*member_p=*/false);
19957             }
19958
19959           /* Look for attributes that apply to the ivar.  */
19960           attributes = cp_parser_attributes_opt (parser);
19961           /* Remember which attributes are prefix attributes and
19962              which are not.  */
19963           first_attribute = attributes;
19964           /* Combine the attributes.  */
19965           attributes = chainon (prefix_attributes, attributes);
19966
19967           if (width)
19968               /* Create the bitfield declaration.  */
19969               decl = grokbitfield (declarator, &declspecs,
19970                                    width,
19971                                    attributes);
19972           else
19973             decl = grokfield (declarator, &declspecs,
19974                               NULL_TREE, /*init_const_expr_p=*/false,
19975                               NULL_TREE, attributes);
19976
19977           /* Add the instance variable.  */
19978           objc_add_instance_variable (decl);
19979
19980           /* Reset PREFIX_ATTRIBUTES.  */
19981           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19982             attributes = TREE_CHAIN (attributes);
19983           if (attributes)
19984             TREE_CHAIN (attributes) = NULL_TREE;
19985
19986           token = cp_lexer_peek_token (parser->lexer);
19987
19988           if (token->type == CPP_COMMA)
19989             {
19990               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19991               continue;
19992             }
19993           break;
19994         }
19995
19996       cp_parser_consume_semicolon_at_end_of_statement (parser);
19997       token = cp_lexer_peek_token (parser->lexer);
19998     }
19999
20000   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20001   /* For historical reasons, we accept an optional semicolon.  */
20002   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20003     cp_lexer_consume_token (parser->lexer);
20004 }
20005
20006 /* Parse an Objective-C protocol declaration.  */
20007
20008 static void
20009 cp_parser_objc_protocol_declaration (cp_parser* parser)
20010 {
20011   tree proto, protorefs;
20012   cp_token *tok;
20013
20014   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20015   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20016     {
20017       tok = cp_lexer_peek_token (parser->lexer);
20018       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
20019       goto finish;
20020     }
20021
20022   /* See if we have a forward declaration or a definition.  */
20023   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20024
20025   /* Try a forward declaration first.  */
20026   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20027     {
20028       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20029      finish:
20030       cp_parser_consume_semicolon_at_end_of_statement (parser);
20031     }
20032
20033   /* Ok, we got a full-fledged definition (or at least should).  */
20034   else
20035     {
20036       proto = cp_parser_identifier (parser);
20037       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20038       objc_start_protocol (proto, protorefs);
20039       cp_parser_objc_method_prototype_list (parser);
20040     }
20041 }
20042
20043 /* Parse an Objective-C superclass or category.  */
20044
20045 static void
20046 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20047                                                           tree *categ)
20048 {
20049   cp_token *next = cp_lexer_peek_token (parser->lexer);
20050
20051   *super = *categ = NULL_TREE;
20052   if (next->type == CPP_COLON)
20053     {
20054       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20055       *super = cp_parser_identifier (parser);
20056     }
20057   else if (next->type == CPP_OPEN_PAREN)
20058     {
20059       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20060       *categ = cp_parser_identifier (parser);
20061       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20062     }
20063 }
20064
20065 /* Parse an Objective-C class interface.  */
20066
20067 static void
20068 cp_parser_objc_class_interface (cp_parser* parser)
20069 {
20070   tree name, super, categ, protos;
20071
20072   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20073   name = cp_parser_identifier (parser);
20074   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20075   protos = cp_parser_objc_protocol_refs_opt (parser);
20076
20077   /* We have either a class or a category on our hands.  */
20078   if (categ)
20079     objc_start_category_interface (name, categ, protos);
20080   else
20081     {
20082       objc_start_class_interface (name, super, protos);
20083       /* Handle instance variable declarations, if any.  */
20084       cp_parser_objc_class_ivars (parser);
20085       objc_continue_interface ();
20086     }
20087
20088   cp_parser_objc_method_prototype_list (parser);
20089 }
20090
20091 /* Parse an Objective-C class implementation.  */
20092
20093 static void
20094 cp_parser_objc_class_implementation (cp_parser* parser)
20095 {
20096   tree name, super, categ;
20097
20098   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20099   name = cp_parser_identifier (parser);
20100   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20101
20102   /* We have either a class or a category on our hands.  */
20103   if (categ)
20104     objc_start_category_implementation (name, categ);
20105   else
20106     {
20107       objc_start_class_implementation (name, super);
20108       /* Handle instance variable declarations, if any.  */
20109       cp_parser_objc_class_ivars (parser);
20110       objc_continue_implementation ();
20111     }
20112
20113   cp_parser_objc_method_definition_list (parser);
20114 }
20115
20116 /* Consume the @end token and finish off the implementation.  */
20117
20118 static void
20119 cp_parser_objc_end_implementation (cp_parser* parser)
20120 {
20121   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20122   objc_finish_implementation ();
20123 }
20124
20125 /* Parse an Objective-C declaration.  */
20126
20127 static void
20128 cp_parser_objc_declaration (cp_parser* parser)
20129 {
20130   /* Try to figure out what kind of declaration is present.  */
20131   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20132
20133   switch (kwd->keyword)
20134     {
20135     case RID_AT_ALIAS:
20136       cp_parser_objc_alias_declaration (parser);
20137       break;
20138     case RID_AT_CLASS:
20139       cp_parser_objc_class_declaration (parser);
20140       break;
20141     case RID_AT_PROTOCOL:
20142       cp_parser_objc_protocol_declaration (parser);
20143       break;
20144     case RID_AT_INTERFACE:
20145       cp_parser_objc_class_interface (parser);
20146       break;
20147     case RID_AT_IMPLEMENTATION:
20148       cp_parser_objc_class_implementation (parser);
20149       break;
20150     case RID_AT_END:
20151       cp_parser_objc_end_implementation (parser);
20152       break;
20153     default:
20154       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20155              &kwd->location, kwd->u.value);
20156       cp_parser_skip_to_end_of_block_or_statement (parser);
20157     }
20158 }
20159
20160 /* Parse an Objective-C try-catch-finally statement.
20161
20162    objc-try-catch-finally-stmt:
20163      @try compound-statement objc-catch-clause-seq [opt]
20164        objc-finally-clause [opt]
20165
20166    objc-catch-clause-seq:
20167      objc-catch-clause objc-catch-clause-seq [opt]
20168
20169    objc-catch-clause:
20170      @catch ( exception-declaration ) compound-statement
20171
20172    objc-finally-clause
20173      @finally compound-statement
20174
20175    Returns NULL_TREE.  */
20176
20177 static tree
20178 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20179   location_t location;
20180   tree stmt;
20181
20182   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20183   location = cp_lexer_peek_token (parser->lexer)->location;
20184   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20185      node, lest it get absorbed into the surrounding block.  */
20186   stmt = push_stmt_list ();
20187   cp_parser_compound_statement (parser, NULL, false);
20188   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20189
20190   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20191     {
20192       cp_parameter_declarator *parmdecl;
20193       tree parm;
20194
20195       cp_lexer_consume_token (parser->lexer);
20196       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20197       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20198       parm = grokdeclarator (parmdecl->declarator,
20199                              &parmdecl->decl_specifiers,
20200                              PARM, /*initialized=*/0,
20201                              /*attrlist=*/NULL);
20202       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20203       objc_begin_catch_clause (parm);
20204       cp_parser_compound_statement (parser, NULL, false);
20205       objc_finish_catch_clause ();
20206     }
20207
20208   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20209     {
20210       cp_lexer_consume_token (parser->lexer);
20211       location = cp_lexer_peek_token (parser->lexer)->location;
20212       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20213          node, lest it get absorbed into the surrounding block.  */
20214       stmt = push_stmt_list ();
20215       cp_parser_compound_statement (parser, NULL, false);
20216       objc_build_finally_clause (location, pop_stmt_list (stmt));
20217     }
20218
20219   return objc_finish_try_stmt ();
20220 }
20221
20222 /* Parse an Objective-C synchronized statement.
20223
20224    objc-synchronized-stmt:
20225      @synchronized ( expression ) compound-statement
20226
20227    Returns NULL_TREE.  */
20228
20229 static tree
20230 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20231   location_t location;
20232   tree lock, stmt;
20233
20234   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20235
20236   location = cp_lexer_peek_token (parser->lexer)->location;
20237   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20238   lock = cp_parser_expression (parser, false, NULL);
20239   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20240
20241   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20242      node, lest it get absorbed into the surrounding block.  */
20243   stmt = push_stmt_list ();
20244   cp_parser_compound_statement (parser, NULL, false);
20245
20246   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20247 }
20248
20249 /* Parse an Objective-C throw statement.
20250
20251    objc-throw-stmt:
20252      @throw assignment-expression [opt] ;
20253
20254    Returns a constructed '@throw' statement.  */
20255
20256 static tree
20257 cp_parser_objc_throw_statement (cp_parser *parser) {
20258   tree expr = NULL_TREE;
20259
20260   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20261
20262   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20263     expr = cp_parser_assignment_expression (parser, false, NULL);
20264
20265   cp_parser_consume_semicolon_at_end_of_statement (parser);
20266
20267   return objc_build_throw_stmt (expr);
20268 }
20269
20270 /* Parse an Objective-C statement.  */
20271
20272 static tree
20273 cp_parser_objc_statement (cp_parser * parser) {
20274   /* Try to figure out what kind of declaration is present.  */
20275   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20276
20277   switch (kwd->keyword)
20278     {
20279     case RID_AT_TRY:
20280       return cp_parser_objc_try_catch_finally_statement (parser);
20281     case RID_AT_SYNCHRONIZED:
20282       return cp_parser_objc_synchronized_statement (parser);
20283     case RID_AT_THROW:
20284       return cp_parser_objc_throw_statement (parser);
20285     default:
20286       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20287              &kwd->location, kwd->u.value);
20288       cp_parser_skip_to_end_of_block_or_statement (parser);
20289     }
20290
20291   return error_mark_node;
20292 }
20293 \f
20294 /* OpenMP 2.5 parsing routines.  */
20295
20296 /* Returns name of the next clause.
20297    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20298    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20299    returned and the token is consumed.  */
20300
20301 static pragma_omp_clause
20302 cp_parser_omp_clause_name (cp_parser *parser)
20303 {
20304   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20305
20306   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20307     result = PRAGMA_OMP_CLAUSE_IF;
20308   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20309     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20310   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20311     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20312   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20313     {
20314       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20315       const char *p = IDENTIFIER_POINTER (id);
20316
20317       switch (p[0])
20318         {
20319         case 'c':
20320           if (!strcmp ("collapse", p))
20321             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20322           else if (!strcmp ("copyin", p))
20323             result = PRAGMA_OMP_CLAUSE_COPYIN;
20324           else if (!strcmp ("copyprivate", p))
20325             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20326           break;
20327         case 'f':
20328           if (!strcmp ("firstprivate", p))
20329             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20330           break;
20331         case 'l':
20332           if (!strcmp ("lastprivate", p))
20333             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20334           break;
20335         case 'n':
20336           if (!strcmp ("nowait", p))
20337             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20338           else if (!strcmp ("num_threads", p))
20339             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20340           break;
20341         case 'o':
20342           if (!strcmp ("ordered", p))
20343             result = PRAGMA_OMP_CLAUSE_ORDERED;
20344           break;
20345         case 'r':
20346           if (!strcmp ("reduction", p))
20347             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20348           break;
20349         case 's':
20350           if (!strcmp ("schedule", p))
20351             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20352           else if (!strcmp ("shared", p))
20353             result = PRAGMA_OMP_CLAUSE_SHARED;
20354           break;
20355         case 'u':
20356           if (!strcmp ("untied", p))
20357             result = PRAGMA_OMP_CLAUSE_UNTIED;
20358           break;
20359         }
20360     }
20361
20362   if (result != PRAGMA_OMP_CLAUSE_NONE)
20363     cp_lexer_consume_token (parser->lexer);
20364
20365   return result;
20366 }
20367
20368 /* Validate that a clause of the given type does not already exist.  */
20369
20370 static void
20371 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20372                            const char *name, location_t location)
20373 {
20374   tree c;
20375
20376   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20377     if (OMP_CLAUSE_CODE (c) == code)
20378       {
20379         error ("%Htoo many %qs clauses", &location, name);
20380         break;
20381       }
20382 }
20383
20384 /* OpenMP 2.5:
20385    variable-list:
20386      identifier
20387      variable-list , identifier
20388
20389    In addition, we match a closing parenthesis.  An opening parenthesis
20390    will have been consumed by the caller.
20391
20392    If KIND is nonzero, create the appropriate node and install the decl
20393    in OMP_CLAUSE_DECL and add the node to the head of the list.
20394
20395    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20396    return the list created.  */
20397
20398 static tree
20399 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20400                                 tree list)
20401 {
20402   cp_token *token;
20403   while (1)
20404     {
20405       tree name, decl;
20406
20407       token = cp_lexer_peek_token (parser->lexer);
20408       name = cp_parser_id_expression (parser, /*template_p=*/false,
20409                                       /*check_dependency_p=*/true,
20410                                       /*template_p=*/NULL,
20411                                       /*declarator_p=*/false,
20412                                       /*optional_p=*/false);
20413       if (name == error_mark_node)
20414         goto skip_comma;
20415
20416       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20417       if (decl == error_mark_node)
20418         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20419       else if (kind != 0)
20420         {
20421           tree u = build_omp_clause (kind);
20422           OMP_CLAUSE_DECL (u) = decl;
20423           OMP_CLAUSE_CHAIN (u) = list;
20424           list = u;
20425         }
20426       else
20427         list = tree_cons (decl, NULL_TREE, list);
20428
20429     get_comma:
20430       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20431         break;
20432       cp_lexer_consume_token (parser->lexer);
20433     }
20434
20435   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20436     {
20437       int ending;
20438
20439       /* Try to resync to an unnested comma.  Copied from
20440          cp_parser_parenthesized_expression_list.  */
20441     skip_comma:
20442       ending = cp_parser_skip_to_closing_parenthesis (parser,
20443                                                       /*recovering=*/true,
20444                                                       /*or_comma=*/true,
20445                                                       /*consume_paren=*/true);
20446       if (ending < 0)
20447         goto get_comma;
20448     }
20449
20450   return list;
20451 }
20452
20453 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20454    common case for omp clauses.  */
20455
20456 static tree
20457 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20458 {
20459   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20460     return cp_parser_omp_var_list_no_open (parser, kind, list);
20461   return list;
20462 }
20463
20464 /* OpenMP 3.0:
20465    collapse ( constant-expression ) */
20466
20467 static tree
20468 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20469 {
20470   tree c, num;
20471   location_t loc;
20472   HOST_WIDE_INT n;
20473
20474   loc = cp_lexer_peek_token (parser->lexer)->location;
20475   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20476     return list;
20477
20478   num = cp_parser_constant_expression (parser, false, NULL);
20479
20480   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20481     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20482                                            /*or_comma=*/false,
20483                                            /*consume_paren=*/true);
20484
20485   if (num == error_mark_node)
20486     return list;
20487   num = fold_non_dependent_expr (num);
20488   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20489       || !host_integerp (num, 0)
20490       || (n = tree_low_cst (num, 0)) <= 0
20491       || (int) n != n)
20492     {
20493       error ("%Hcollapse argument needs positive constant integer expression",
20494              &loc);
20495       return list;
20496     }
20497
20498   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20499   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20500   OMP_CLAUSE_CHAIN (c) = list;
20501   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20502
20503   return c;
20504 }
20505
20506 /* OpenMP 2.5:
20507    default ( shared | none ) */
20508
20509 static tree
20510 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20511 {
20512   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20513   tree c;
20514
20515   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20516     return list;
20517   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20518     {
20519       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20520       const char *p = IDENTIFIER_POINTER (id);
20521
20522       switch (p[0])
20523         {
20524         case 'n':
20525           if (strcmp ("none", p) != 0)
20526             goto invalid_kind;
20527           kind = OMP_CLAUSE_DEFAULT_NONE;
20528           break;
20529
20530         case 's':
20531           if (strcmp ("shared", p) != 0)
20532             goto invalid_kind;
20533           kind = OMP_CLAUSE_DEFAULT_SHARED;
20534           break;
20535
20536         default:
20537           goto invalid_kind;
20538         }
20539
20540       cp_lexer_consume_token (parser->lexer);
20541     }
20542   else
20543     {
20544     invalid_kind:
20545       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20546     }
20547
20548   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20549     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20550                                            /*or_comma=*/false,
20551                                            /*consume_paren=*/true);
20552
20553   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20554     return list;
20555
20556   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20557   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20558   OMP_CLAUSE_CHAIN (c) = list;
20559   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20560
20561   return c;
20562 }
20563
20564 /* OpenMP 2.5:
20565    if ( expression ) */
20566
20567 static tree
20568 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20569 {
20570   tree t, c;
20571
20572   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20573     return list;
20574
20575   t = cp_parser_condition (parser);
20576
20577   if (t == error_mark_node
20578       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20579     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20580                                            /*or_comma=*/false,
20581                                            /*consume_paren=*/true);
20582
20583   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20584
20585   c = build_omp_clause (OMP_CLAUSE_IF);
20586   OMP_CLAUSE_IF_EXPR (c) = t;
20587   OMP_CLAUSE_CHAIN (c) = list;
20588
20589   return c;
20590 }
20591
20592 /* OpenMP 2.5:
20593    nowait */
20594
20595 static tree
20596 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20597                              tree list, location_t location)
20598 {
20599   tree c;
20600
20601   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20602
20603   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20604   OMP_CLAUSE_CHAIN (c) = list;
20605   return c;
20606 }
20607
20608 /* OpenMP 2.5:
20609    num_threads ( expression ) */
20610
20611 static tree
20612 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20613                                   location_t location)
20614 {
20615   tree t, c;
20616
20617   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20618     return list;
20619
20620   t = cp_parser_expression (parser, false, NULL);
20621
20622   if (t == error_mark_node
20623       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20624     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20625                                            /*or_comma=*/false,
20626                                            /*consume_paren=*/true);
20627
20628   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20629                              "num_threads", location);
20630
20631   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20632   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20633   OMP_CLAUSE_CHAIN (c) = list;
20634
20635   return c;
20636 }
20637
20638 /* OpenMP 2.5:
20639    ordered */
20640
20641 static tree
20642 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20643                               tree list, location_t location)
20644 {
20645   tree c;
20646
20647   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20648                              "ordered", location);
20649
20650   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20651   OMP_CLAUSE_CHAIN (c) = list;
20652   return c;
20653 }
20654
20655 /* OpenMP 2.5:
20656    reduction ( reduction-operator : variable-list )
20657
20658    reduction-operator:
20659      One of: + * - & ^ | && || */
20660
20661 static tree
20662 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20663 {
20664   enum tree_code code;
20665   tree nlist, c;
20666
20667   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20668     return list;
20669
20670   switch (cp_lexer_peek_token (parser->lexer)->type)
20671     {
20672     case CPP_PLUS:
20673       code = PLUS_EXPR;
20674       break;
20675     case CPP_MULT:
20676       code = MULT_EXPR;
20677       break;
20678     case CPP_MINUS:
20679       code = MINUS_EXPR;
20680       break;
20681     case CPP_AND:
20682       code = BIT_AND_EXPR;
20683       break;
20684     case CPP_XOR:
20685       code = BIT_XOR_EXPR;
20686       break;
20687     case CPP_OR:
20688       code = BIT_IOR_EXPR;
20689       break;
20690     case CPP_AND_AND:
20691       code = TRUTH_ANDIF_EXPR;
20692       break;
20693     case CPP_OR_OR:
20694       code = TRUTH_ORIF_EXPR;
20695       break;
20696     default:
20697       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20698                                "%<|%>, %<&&%>, or %<||%>");
20699     resync_fail:
20700       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20701                                              /*or_comma=*/false,
20702                                              /*consume_paren=*/true);
20703       return list;
20704     }
20705   cp_lexer_consume_token (parser->lexer);
20706
20707   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20708     goto resync_fail;
20709
20710   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20711   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20712     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20713
20714   return nlist;
20715 }
20716
20717 /* OpenMP 2.5:
20718    schedule ( schedule-kind )
20719    schedule ( schedule-kind , expression )
20720
20721    schedule-kind:
20722      static | dynamic | guided | runtime | auto  */
20723
20724 static tree
20725 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20726 {
20727   tree c, t;
20728
20729   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20730     return list;
20731
20732   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20733
20734   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20735     {
20736       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20737       const char *p = IDENTIFIER_POINTER (id);
20738
20739       switch (p[0])
20740         {
20741         case 'd':
20742           if (strcmp ("dynamic", p) != 0)
20743             goto invalid_kind;
20744           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20745           break;
20746
20747         case 'g':
20748           if (strcmp ("guided", p) != 0)
20749             goto invalid_kind;
20750           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20751           break;
20752
20753         case 'r':
20754           if (strcmp ("runtime", p) != 0)
20755             goto invalid_kind;
20756           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20757           break;
20758
20759         default:
20760           goto invalid_kind;
20761         }
20762     }
20763   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20764     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20765   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20766     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20767   else
20768     goto invalid_kind;
20769   cp_lexer_consume_token (parser->lexer);
20770
20771   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20772     {
20773       cp_token *token;
20774       cp_lexer_consume_token (parser->lexer);
20775
20776       token = cp_lexer_peek_token (parser->lexer);
20777       t = cp_parser_assignment_expression (parser, false, NULL);
20778
20779       if (t == error_mark_node)
20780         goto resync_fail;
20781       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20782         error ("%Hschedule %<runtime%> does not take "
20783                "a %<chunk_size%> parameter", &token->location);
20784       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20785         error ("%Hschedule %<auto%> does not take "
20786                "a %<chunk_size%> parameter", &token->location);
20787       else
20788         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20789
20790       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20791         goto resync_fail;
20792     }
20793   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20794     goto resync_fail;
20795
20796   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20797   OMP_CLAUSE_CHAIN (c) = list;
20798   return c;
20799
20800  invalid_kind:
20801   cp_parser_error (parser, "invalid schedule kind");
20802  resync_fail:
20803   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20804                                          /*or_comma=*/false,
20805                                          /*consume_paren=*/true);
20806   return list;
20807 }
20808
20809 /* OpenMP 3.0:
20810    untied */
20811
20812 static tree
20813 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20814                              tree list, location_t location)
20815 {
20816   tree c;
20817
20818   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20819
20820   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20821   OMP_CLAUSE_CHAIN (c) = list;
20822   return c;
20823 }
20824
20825 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20826    is a bitmask in MASK.  Return the list of clauses found; the result
20827    of clause default goes in *pdefault.  */
20828
20829 static tree
20830 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20831                            const char *where, cp_token *pragma_tok)
20832 {
20833   tree clauses = NULL;
20834   bool first = true;
20835   cp_token *token = NULL;
20836
20837   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20838     {
20839       pragma_omp_clause c_kind;
20840       const char *c_name;
20841       tree prev = clauses;
20842
20843       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20844         cp_lexer_consume_token (parser->lexer);
20845
20846       token = cp_lexer_peek_token (parser->lexer);
20847       c_kind = cp_parser_omp_clause_name (parser);
20848       first = false;
20849
20850       switch (c_kind)
20851         {
20852         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20853           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20854                                                    token->location);
20855           c_name = "collapse";
20856           break;
20857         case PRAGMA_OMP_CLAUSE_COPYIN:
20858           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20859           c_name = "copyin";
20860           break;
20861         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20862           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20863                                             clauses);
20864           c_name = "copyprivate";
20865           break;
20866         case PRAGMA_OMP_CLAUSE_DEFAULT:
20867           clauses = cp_parser_omp_clause_default (parser, clauses,
20868                                                   token->location);
20869           c_name = "default";
20870           break;
20871         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20872           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20873                                             clauses);
20874           c_name = "firstprivate";
20875           break;
20876         case PRAGMA_OMP_CLAUSE_IF:
20877           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20878           c_name = "if";
20879           break;
20880         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20881           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20882                                             clauses);
20883           c_name = "lastprivate";
20884           break;
20885         case PRAGMA_OMP_CLAUSE_NOWAIT:
20886           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20887           c_name = "nowait";
20888           break;
20889         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20890           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20891                                                       token->location);
20892           c_name = "num_threads";
20893           break;
20894         case PRAGMA_OMP_CLAUSE_ORDERED:
20895           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20896                                                   token->location);
20897           c_name = "ordered";
20898           break;
20899         case PRAGMA_OMP_CLAUSE_PRIVATE:
20900           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20901                                             clauses);
20902           c_name = "private";
20903           break;
20904         case PRAGMA_OMP_CLAUSE_REDUCTION:
20905           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20906           c_name = "reduction";
20907           break;
20908         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20909           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20910                                                    token->location);
20911           c_name = "schedule";
20912           break;
20913         case PRAGMA_OMP_CLAUSE_SHARED:
20914           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20915                                             clauses);
20916           c_name = "shared";
20917           break;
20918         case PRAGMA_OMP_CLAUSE_UNTIED:
20919           clauses = cp_parser_omp_clause_untied (parser, clauses,
20920                                                  token->location);
20921           c_name = "nowait";
20922           break;
20923         default:
20924           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20925           goto saw_error;
20926         }
20927
20928       if (((mask >> c_kind) & 1) == 0)
20929         {
20930           /* Remove the invalid clause(s) from the list to avoid
20931              confusing the rest of the compiler.  */
20932           clauses = prev;
20933           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20934         }
20935     }
20936  saw_error:
20937   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20938   return finish_omp_clauses (clauses);
20939 }
20940
20941 /* OpenMP 2.5:
20942    structured-block:
20943      statement
20944
20945    In practice, we're also interested in adding the statement to an
20946    outer node.  So it is convenient if we work around the fact that
20947    cp_parser_statement calls add_stmt.  */
20948
20949 static unsigned
20950 cp_parser_begin_omp_structured_block (cp_parser *parser)
20951 {
20952   unsigned save = parser->in_statement;
20953
20954   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20955      This preserves the "not within loop or switch" style error messages
20956      for nonsense cases like
20957         void foo() {
20958         #pragma omp single
20959           break;
20960         }
20961   */
20962   if (parser->in_statement)
20963     parser->in_statement = IN_OMP_BLOCK;
20964
20965   return save;
20966 }
20967
20968 static void
20969 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20970 {
20971   parser->in_statement = save;
20972 }
20973
20974 static tree
20975 cp_parser_omp_structured_block (cp_parser *parser)
20976 {
20977   tree stmt = begin_omp_structured_block ();
20978   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20979
20980   cp_parser_statement (parser, NULL_TREE, false, NULL);
20981
20982   cp_parser_end_omp_structured_block (parser, save);
20983   return finish_omp_structured_block (stmt);
20984 }
20985
20986 /* OpenMP 2.5:
20987    # pragma omp atomic new-line
20988      expression-stmt
20989
20990    expression-stmt:
20991      x binop= expr | x++ | ++x | x-- | --x
20992    binop:
20993      +, *, -, /, &, ^, |, <<, >>
20994
20995   where x is an lvalue expression with scalar type.  */
20996
20997 static void
20998 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20999 {
21000   tree lhs, rhs;
21001   enum tree_code code;
21002
21003   cp_parser_require_pragma_eol (parser, pragma_tok);
21004
21005   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21006                                     /*cast_p=*/false, NULL);
21007   switch (TREE_CODE (lhs))
21008     {
21009     case ERROR_MARK:
21010       goto saw_error;
21011
21012     case PREINCREMENT_EXPR:
21013     case POSTINCREMENT_EXPR:
21014       lhs = TREE_OPERAND (lhs, 0);
21015       code = PLUS_EXPR;
21016       rhs = integer_one_node;
21017       break;
21018
21019     case PREDECREMENT_EXPR:
21020     case POSTDECREMENT_EXPR:
21021       lhs = TREE_OPERAND (lhs, 0);
21022       code = MINUS_EXPR;
21023       rhs = integer_one_node;
21024       break;
21025
21026     default:
21027       switch (cp_lexer_peek_token (parser->lexer)->type)
21028         {
21029         case CPP_MULT_EQ:
21030           code = MULT_EXPR;
21031           break;
21032         case CPP_DIV_EQ:
21033           code = TRUNC_DIV_EXPR;
21034           break;
21035         case CPP_PLUS_EQ:
21036           code = PLUS_EXPR;
21037           break;
21038         case CPP_MINUS_EQ:
21039           code = MINUS_EXPR;
21040           break;
21041         case CPP_LSHIFT_EQ:
21042           code = LSHIFT_EXPR;
21043           break;
21044         case CPP_RSHIFT_EQ:
21045           code = RSHIFT_EXPR;
21046           break;
21047         case CPP_AND_EQ:
21048           code = BIT_AND_EXPR;
21049           break;
21050         case CPP_OR_EQ:
21051           code = BIT_IOR_EXPR;
21052           break;
21053         case CPP_XOR_EQ:
21054           code = BIT_XOR_EXPR;
21055           break;
21056         default:
21057           cp_parser_error (parser,
21058                            "invalid operator for %<#pragma omp atomic%>");
21059           goto saw_error;
21060         }
21061       cp_lexer_consume_token (parser->lexer);
21062
21063       rhs = cp_parser_expression (parser, false, NULL);
21064       if (rhs == error_mark_node)
21065         goto saw_error;
21066       break;
21067     }
21068   finish_omp_atomic (code, lhs, rhs);
21069   cp_parser_consume_semicolon_at_end_of_statement (parser);
21070   return;
21071
21072  saw_error:
21073   cp_parser_skip_to_end_of_block_or_statement (parser);
21074 }
21075
21076
21077 /* OpenMP 2.5:
21078    # pragma omp barrier new-line  */
21079
21080 static void
21081 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21082 {
21083   cp_parser_require_pragma_eol (parser, pragma_tok);
21084   finish_omp_barrier ();
21085 }
21086
21087 /* OpenMP 2.5:
21088    # pragma omp critical [(name)] new-line
21089      structured-block  */
21090
21091 static tree
21092 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21093 {
21094   tree stmt, name = NULL;
21095
21096   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21097     {
21098       cp_lexer_consume_token (parser->lexer);
21099
21100       name = cp_parser_identifier (parser);
21101
21102       if (name == error_mark_node
21103           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21104         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21105                                                /*or_comma=*/false,
21106                                                /*consume_paren=*/true);
21107       if (name == error_mark_node)
21108         name = NULL;
21109     }
21110   cp_parser_require_pragma_eol (parser, pragma_tok);
21111
21112   stmt = cp_parser_omp_structured_block (parser);
21113   return c_finish_omp_critical (stmt, name);
21114 }
21115
21116 /* OpenMP 2.5:
21117    # pragma omp flush flush-vars[opt] new-line
21118
21119    flush-vars:
21120      ( variable-list ) */
21121
21122 static void
21123 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21124 {
21125   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21126     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21127   cp_parser_require_pragma_eol (parser, pragma_tok);
21128
21129   finish_omp_flush ();
21130 }
21131
21132 /* Helper function, to parse omp for increment expression.  */
21133
21134 static tree
21135 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21136 {
21137   tree cond = cp_parser_binary_expression (parser, false, true,
21138                                            PREC_NOT_OPERATOR, NULL);
21139   bool overloaded_p;
21140
21141   if (cond == error_mark_node
21142       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21143     {
21144       cp_parser_skip_to_end_of_statement (parser);
21145       return error_mark_node;
21146     }
21147
21148   switch (TREE_CODE (cond))
21149     {
21150     case GT_EXPR:
21151     case GE_EXPR:
21152     case LT_EXPR:
21153     case LE_EXPR:
21154       break;
21155     default:
21156       return error_mark_node;
21157     }
21158
21159   /* If decl is an iterator, preserve LHS and RHS of the relational
21160      expr until finish_omp_for.  */
21161   if (decl
21162       && (type_dependent_expression_p (decl)
21163           || CLASS_TYPE_P (TREE_TYPE (decl))))
21164     return cond;
21165
21166   return build_x_binary_op (TREE_CODE (cond),
21167                             TREE_OPERAND (cond, 0), ERROR_MARK,
21168                             TREE_OPERAND (cond, 1), ERROR_MARK,
21169                             &overloaded_p, tf_warning_or_error);
21170 }
21171
21172 /* Helper function, to parse omp for increment expression.  */
21173
21174 static tree
21175 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21176 {
21177   cp_token *token = cp_lexer_peek_token (parser->lexer);
21178   enum tree_code op;
21179   tree lhs, rhs;
21180   cp_id_kind idk;
21181   bool decl_first;
21182
21183   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21184     {
21185       op = (token->type == CPP_PLUS_PLUS
21186             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21187       cp_lexer_consume_token (parser->lexer);
21188       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21189       if (lhs != decl)
21190         return error_mark_node;
21191       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21192     }
21193
21194   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21195   if (lhs != decl)
21196     return error_mark_node;
21197
21198   token = cp_lexer_peek_token (parser->lexer);
21199   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21200     {
21201       op = (token->type == CPP_PLUS_PLUS
21202             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21203       cp_lexer_consume_token (parser->lexer);
21204       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21205     }
21206
21207   op = cp_parser_assignment_operator_opt (parser);
21208   if (op == ERROR_MARK)
21209     return error_mark_node;
21210
21211   if (op != NOP_EXPR)
21212     {
21213       rhs = cp_parser_assignment_expression (parser, false, NULL);
21214       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21215       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21216     }
21217
21218   lhs = cp_parser_binary_expression (parser, false, false,
21219                                      PREC_ADDITIVE_EXPRESSION, NULL);
21220   token = cp_lexer_peek_token (parser->lexer);
21221   decl_first = lhs == decl;
21222   if (decl_first)
21223     lhs = NULL_TREE;
21224   if (token->type != CPP_PLUS
21225       && token->type != CPP_MINUS)
21226     return error_mark_node;
21227
21228   do
21229     {
21230       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21231       cp_lexer_consume_token (parser->lexer);
21232       rhs = cp_parser_binary_expression (parser, false, false,
21233                                          PREC_ADDITIVE_EXPRESSION, NULL);
21234       token = cp_lexer_peek_token (parser->lexer);
21235       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21236         {
21237           if (lhs == NULL_TREE)
21238             {
21239               if (op == PLUS_EXPR)
21240                 lhs = rhs;
21241               else
21242                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21243             }
21244           else
21245             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21246                                      NULL, tf_warning_or_error);
21247         }
21248     }
21249   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21250
21251   if (!decl_first)
21252     {
21253       if (rhs != decl || op == MINUS_EXPR)
21254         return error_mark_node;
21255       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21256     }
21257   else
21258     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21259
21260   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21261 }
21262
21263 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21264
21265 static tree
21266 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21267 {
21268   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21269   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21270   tree this_pre_body, cl;
21271   location_t loc_first;
21272   bool collapse_err = false;
21273   int i, collapse = 1, nbraces = 0;
21274
21275   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21276     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21277       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21278
21279   gcc_assert (collapse >= 1);
21280
21281   declv = make_tree_vec (collapse);
21282   initv = make_tree_vec (collapse);
21283   condv = make_tree_vec (collapse);
21284   incrv = make_tree_vec (collapse);
21285
21286   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21287
21288   for (i = 0; i < collapse; i++)
21289     {
21290       int bracecount = 0;
21291       bool add_private_clause = false;
21292       location_t loc;
21293
21294       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21295         {
21296           cp_parser_error (parser, "for statement expected");
21297           return NULL;
21298         }
21299       loc = cp_lexer_consume_token (parser->lexer)->location;
21300
21301       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21302         return NULL;
21303
21304       init = decl = real_decl = NULL;
21305       this_pre_body = push_stmt_list ();
21306       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21307         {
21308           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21309
21310              init-expr:
21311                        var = lb
21312                        integer-type var = lb
21313                        random-access-iterator-type var = lb
21314                        pointer-type var = lb
21315           */
21316           cp_decl_specifier_seq type_specifiers;
21317
21318           /* First, try to parse as an initialized declaration.  See
21319              cp_parser_condition, from whence the bulk of this is copied.  */
21320
21321           cp_parser_parse_tentatively (parser);
21322           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21323                                         &type_specifiers);
21324           if (cp_parser_parse_definitely (parser))
21325             {
21326               /* If parsing a type specifier seq succeeded, then this
21327                  MUST be a initialized declaration.  */
21328               tree asm_specification, attributes;
21329               cp_declarator *declarator;
21330
21331               declarator = cp_parser_declarator (parser,
21332                                                  CP_PARSER_DECLARATOR_NAMED,
21333                                                  /*ctor_dtor_or_conv_p=*/NULL,
21334                                                  /*parenthesized_p=*/NULL,
21335                                                  /*member_p=*/false);
21336               attributes = cp_parser_attributes_opt (parser);
21337               asm_specification = cp_parser_asm_specification_opt (parser);
21338
21339               if (declarator == cp_error_declarator) 
21340                 cp_parser_skip_to_end_of_statement (parser);
21341
21342               else 
21343                 {
21344                   tree pushed_scope, auto_node;
21345
21346                   decl = start_decl (declarator, &type_specifiers,
21347                                      SD_INITIALIZED, attributes,
21348                                      /*prefix_attributes=*/NULL_TREE,
21349                                      &pushed_scope);
21350
21351                   auto_node = type_uses_auto (TREE_TYPE (decl));
21352                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21353                     {
21354                       if (cp_lexer_next_token_is (parser->lexer, 
21355                                                   CPP_OPEN_PAREN))
21356                         error ("parenthesized initialization is not allowed in "
21357                                "OpenMP %<for%> loop");
21358                       else
21359                         /* Trigger an error.  */
21360                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21361
21362                       init = error_mark_node;
21363                       cp_parser_skip_to_end_of_statement (parser);
21364                     }
21365                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21366                            || type_dependent_expression_p (decl)
21367                            || auto_node)
21368                     {
21369                       bool is_direct_init, is_non_constant_init;
21370
21371                       init = cp_parser_initializer (parser,
21372                                                     &is_direct_init,
21373                                                     &is_non_constant_init);
21374
21375                       if (auto_node && describable_type (init))
21376                         {
21377                           TREE_TYPE (decl)
21378                             = do_auto_deduction (TREE_TYPE (decl), init,
21379                                                  auto_node);
21380
21381                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21382                               && !type_dependent_expression_p (decl))
21383                             goto non_class;
21384                         }
21385                       
21386                       cp_finish_decl (decl, init, !is_non_constant_init,
21387                                       asm_specification,
21388                                       LOOKUP_ONLYCONVERTING);
21389                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21390                         {
21391                           for_block
21392                             = tree_cons (NULL, this_pre_body, for_block);
21393                           init = NULL_TREE;
21394                         }
21395                       else
21396                         init = pop_stmt_list (this_pre_body);
21397                       this_pre_body = NULL_TREE;
21398                     }
21399                   else
21400                     {
21401                       /* Consume '='.  */
21402                       cp_lexer_consume_token (parser->lexer);
21403                       init = cp_parser_assignment_expression (parser, false, NULL);
21404
21405                     non_class:
21406                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21407                         init = error_mark_node;
21408                       else
21409                         cp_finish_decl (decl, NULL_TREE,
21410                                         /*init_const_expr_p=*/false,
21411                                         asm_specification,
21412                                         LOOKUP_ONLYCONVERTING);
21413                     }
21414
21415                   if (pushed_scope)
21416                     pop_scope (pushed_scope);
21417                 }
21418             }
21419           else 
21420             {
21421               cp_id_kind idk;
21422               /* If parsing a type specifier sequence failed, then
21423                  this MUST be a simple expression.  */
21424               cp_parser_parse_tentatively (parser);
21425               decl = cp_parser_primary_expression (parser, false, false,
21426                                                    false, &idk);
21427               if (!cp_parser_error_occurred (parser)
21428                   && decl
21429                   && DECL_P (decl)
21430                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21431                 {
21432                   tree rhs;
21433
21434                   cp_parser_parse_definitely (parser);
21435                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21436                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21437                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21438                                                          rhs,
21439                                                          tf_warning_or_error));
21440                   add_private_clause = true;
21441                 }
21442               else
21443                 {
21444                   decl = NULL;
21445                   cp_parser_abort_tentative_parse (parser);
21446                   init = cp_parser_expression (parser, false, NULL);
21447                   if (init)
21448                     {
21449                       if (TREE_CODE (init) == MODIFY_EXPR
21450                           || TREE_CODE (init) == MODOP_EXPR)
21451                         real_decl = TREE_OPERAND (init, 0);
21452                     }
21453                 }
21454             }
21455         }
21456       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21457       if (this_pre_body)
21458         {
21459           this_pre_body = pop_stmt_list (this_pre_body);
21460           if (pre_body)
21461             {
21462               tree t = pre_body;
21463               pre_body = push_stmt_list ();
21464               add_stmt (t);
21465               add_stmt (this_pre_body);
21466               pre_body = pop_stmt_list (pre_body);
21467             }
21468           else
21469             pre_body = this_pre_body;
21470         }
21471
21472       if (decl)
21473         real_decl = decl;
21474       if (par_clauses != NULL && real_decl != NULL_TREE)
21475         {
21476           tree *c;
21477           for (c = par_clauses; *c ; )
21478             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21479                 && OMP_CLAUSE_DECL (*c) == real_decl)
21480               {
21481                 error ("%Hiteration variable %qD should not be firstprivate",
21482                        &loc, real_decl);
21483                 *c = OMP_CLAUSE_CHAIN (*c);
21484               }
21485             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21486                      && OMP_CLAUSE_DECL (*c) == real_decl)
21487               {
21488                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21489                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21490                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21491                 OMP_CLAUSE_DECL (l) = real_decl;
21492                 OMP_CLAUSE_CHAIN (l) = clauses;
21493                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21494                 clauses = l;
21495                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21496                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21497                 add_private_clause = false;
21498               }
21499             else
21500               {
21501                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21502                     && OMP_CLAUSE_DECL (*c) == real_decl)
21503                   add_private_clause = false;
21504                 c = &OMP_CLAUSE_CHAIN (*c);
21505               }
21506         }
21507
21508       if (add_private_clause)
21509         {
21510           tree c;
21511           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21512             {
21513               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21514                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21515                   && OMP_CLAUSE_DECL (c) == decl)
21516                 break;
21517               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21518                        && OMP_CLAUSE_DECL (c) == decl)
21519                 error ("%Hiteration variable %qD should not be firstprivate",
21520                        &loc, decl);
21521               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21522                        && OMP_CLAUSE_DECL (c) == decl)
21523                 error ("%Hiteration variable %qD should not be reduction",
21524                        &loc, decl);
21525             }
21526           if (c == NULL)
21527             {
21528               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21529               OMP_CLAUSE_DECL (c) = decl;
21530               c = finish_omp_clauses (c);
21531               if (c)
21532                 {
21533                   OMP_CLAUSE_CHAIN (c) = clauses;
21534                   clauses = c;
21535                 }
21536             }
21537         }
21538
21539       cond = NULL;
21540       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21541         cond = cp_parser_omp_for_cond (parser, decl);
21542       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21543
21544       incr = NULL;
21545       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21546         {
21547           /* If decl is an iterator, preserve the operator on decl
21548              until finish_omp_for.  */
21549           if (decl
21550               && (type_dependent_expression_p (decl)
21551                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21552             incr = cp_parser_omp_for_incr (parser, decl);
21553           else
21554             incr = cp_parser_expression (parser, false, NULL);
21555         }
21556
21557       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21558         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21559                                                /*or_comma=*/false,
21560                                                /*consume_paren=*/true);
21561
21562       TREE_VEC_ELT (declv, i) = decl;
21563       TREE_VEC_ELT (initv, i) = init;
21564       TREE_VEC_ELT (condv, i) = cond;
21565       TREE_VEC_ELT (incrv, i) = incr;
21566
21567       if (i == collapse - 1)
21568         break;
21569
21570       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21571          in between the collapsed for loops to be still considered perfectly
21572          nested.  Hopefully the final version clarifies this.
21573          For now handle (multiple) {'s and empty statements.  */
21574       cp_parser_parse_tentatively (parser);
21575       do
21576         {
21577           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21578             break;
21579           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21580             {
21581               cp_lexer_consume_token (parser->lexer);
21582               bracecount++;
21583             }
21584           else if (bracecount
21585                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21586             cp_lexer_consume_token (parser->lexer);
21587           else
21588             {
21589               loc = cp_lexer_peek_token (parser->lexer)->location;
21590               error ("%Hnot enough collapsed for loops", &loc);
21591               collapse_err = true;
21592               cp_parser_abort_tentative_parse (parser);
21593               declv = NULL_TREE;
21594               break;
21595             }
21596         }
21597       while (1);
21598
21599       if (declv)
21600         {
21601           cp_parser_parse_definitely (parser);
21602           nbraces += bracecount;
21603         }
21604     }
21605
21606   /* Note that we saved the original contents of this flag when we entered
21607      the structured block, and so we don't need to re-save it here.  */
21608   parser->in_statement = IN_OMP_FOR;
21609
21610   /* Note that the grammar doesn't call for a structured block here,
21611      though the loop as a whole is a structured block.  */
21612   body = push_stmt_list ();
21613   cp_parser_statement (parser, NULL_TREE, false, NULL);
21614   body = pop_stmt_list (body);
21615
21616   if (declv == NULL_TREE)
21617     ret = NULL_TREE;
21618   else
21619     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21620                           pre_body, clauses);
21621
21622   while (nbraces)
21623     {
21624       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21625         {
21626           cp_lexer_consume_token (parser->lexer);
21627           nbraces--;
21628         }
21629       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21630         cp_lexer_consume_token (parser->lexer);
21631       else
21632         {
21633           if (!collapse_err)
21634             {
21635               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21636               error ("%Hcollapsed loops not perfectly nested", &loc);
21637             }
21638           collapse_err = true;
21639           cp_parser_statement_seq_opt (parser, NULL);
21640           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21641         }
21642     }
21643
21644   while (for_block)
21645     {
21646       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21647       for_block = TREE_CHAIN (for_block);
21648     }
21649
21650   return ret;
21651 }
21652
21653 /* OpenMP 2.5:
21654    #pragma omp for for-clause[optseq] new-line
21655      for-loop  */
21656
21657 #define OMP_FOR_CLAUSE_MASK                             \
21658         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21659         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21660         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21661         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21662         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21663         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21664         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21665         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21666
21667 static tree
21668 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21669 {
21670   tree clauses, sb, ret;
21671   unsigned int save;
21672
21673   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21674                                        "#pragma omp for", pragma_tok);
21675
21676   sb = begin_omp_structured_block ();
21677   save = cp_parser_begin_omp_structured_block (parser);
21678
21679   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21680
21681   cp_parser_end_omp_structured_block (parser, save);
21682   add_stmt (finish_omp_structured_block (sb));
21683
21684   return ret;
21685 }
21686
21687 /* OpenMP 2.5:
21688    # pragma omp master new-line
21689      structured-block  */
21690
21691 static tree
21692 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21693 {
21694   cp_parser_require_pragma_eol (parser, pragma_tok);
21695   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21696 }
21697
21698 /* OpenMP 2.5:
21699    # pragma omp ordered new-line
21700      structured-block  */
21701
21702 static tree
21703 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21704 {
21705   cp_parser_require_pragma_eol (parser, pragma_tok);
21706   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21707 }
21708
21709 /* OpenMP 2.5:
21710
21711    section-scope:
21712      { section-sequence }
21713
21714    section-sequence:
21715      section-directive[opt] structured-block
21716      section-sequence section-directive structured-block  */
21717
21718 static tree
21719 cp_parser_omp_sections_scope (cp_parser *parser)
21720 {
21721   tree stmt, substmt;
21722   bool error_suppress = false;
21723   cp_token *tok;
21724
21725   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21726     return NULL_TREE;
21727
21728   stmt = push_stmt_list ();
21729
21730   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21731     {
21732       unsigned save;
21733
21734       substmt = begin_omp_structured_block ();
21735       save = cp_parser_begin_omp_structured_block (parser);
21736
21737       while (1)
21738         {
21739           cp_parser_statement (parser, NULL_TREE, false, NULL);
21740
21741           tok = cp_lexer_peek_token (parser->lexer);
21742           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21743             break;
21744           if (tok->type == CPP_CLOSE_BRACE)
21745             break;
21746           if (tok->type == CPP_EOF)
21747             break;
21748         }
21749
21750       cp_parser_end_omp_structured_block (parser, save);
21751       substmt = finish_omp_structured_block (substmt);
21752       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21753       add_stmt (substmt);
21754     }
21755
21756   while (1)
21757     {
21758       tok = cp_lexer_peek_token (parser->lexer);
21759       if (tok->type == CPP_CLOSE_BRACE)
21760         break;
21761       if (tok->type == CPP_EOF)
21762         break;
21763
21764       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21765         {
21766           cp_lexer_consume_token (parser->lexer);
21767           cp_parser_require_pragma_eol (parser, tok);
21768           error_suppress = false;
21769         }
21770       else if (!error_suppress)
21771         {
21772           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21773           error_suppress = true;
21774         }
21775
21776       substmt = cp_parser_omp_structured_block (parser);
21777       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21778       add_stmt (substmt);
21779     }
21780   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21781
21782   substmt = pop_stmt_list (stmt);
21783
21784   stmt = make_node (OMP_SECTIONS);
21785   TREE_TYPE (stmt) = void_type_node;
21786   OMP_SECTIONS_BODY (stmt) = substmt;
21787
21788   add_stmt (stmt);
21789   return stmt;
21790 }
21791
21792 /* OpenMP 2.5:
21793    # pragma omp sections sections-clause[optseq] newline
21794      sections-scope  */
21795
21796 #define OMP_SECTIONS_CLAUSE_MASK                        \
21797         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21798         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21799         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21800         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21801         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21802
21803 static tree
21804 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21805 {
21806   tree clauses, ret;
21807
21808   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21809                                        "#pragma omp sections", pragma_tok);
21810
21811   ret = cp_parser_omp_sections_scope (parser);
21812   if (ret)
21813     OMP_SECTIONS_CLAUSES (ret) = clauses;
21814
21815   return ret;
21816 }
21817
21818 /* OpenMP 2.5:
21819    # pragma parallel parallel-clause new-line
21820    # pragma parallel for parallel-for-clause new-line
21821    # pragma parallel sections parallel-sections-clause new-line  */
21822
21823 #define OMP_PARALLEL_CLAUSE_MASK                        \
21824         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21825         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21826         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21827         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21828         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21829         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21830         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21831         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21832
21833 static tree
21834 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21835 {
21836   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21837   const char *p_name = "#pragma omp parallel";
21838   tree stmt, clauses, par_clause, ws_clause, block;
21839   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21840   unsigned int save;
21841
21842   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21843     {
21844       cp_lexer_consume_token (parser->lexer);
21845       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21846       p_name = "#pragma omp parallel for";
21847       mask |= OMP_FOR_CLAUSE_MASK;
21848       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21849     }
21850   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21851     {
21852       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21853       const char *p = IDENTIFIER_POINTER (id);
21854       if (strcmp (p, "sections") == 0)
21855         {
21856           cp_lexer_consume_token (parser->lexer);
21857           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21858           p_name = "#pragma omp parallel sections";
21859           mask |= OMP_SECTIONS_CLAUSE_MASK;
21860           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21861         }
21862     }
21863
21864   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21865   block = begin_omp_parallel ();
21866   save = cp_parser_begin_omp_structured_block (parser);
21867
21868   switch (p_kind)
21869     {
21870     case PRAGMA_OMP_PARALLEL:
21871       cp_parser_statement (parser, NULL_TREE, false, NULL);
21872       par_clause = clauses;
21873       break;
21874
21875     case PRAGMA_OMP_PARALLEL_FOR:
21876       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21877       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21878       break;
21879
21880     case PRAGMA_OMP_PARALLEL_SECTIONS:
21881       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21882       stmt = cp_parser_omp_sections_scope (parser);
21883       if (stmt)
21884         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21885       break;
21886
21887     default:
21888       gcc_unreachable ();
21889     }
21890
21891   cp_parser_end_omp_structured_block (parser, save);
21892   stmt = finish_omp_parallel (par_clause, block);
21893   if (p_kind != PRAGMA_OMP_PARALLEL)
21894     OMP_PARALLEL_COMBINED (stmt) = 1;
21895   return stmt;
21896 }
21897
21898 /* OpenMP 2.5:
21899    # pragma omp single single-clause[optseq] new-line
21900      structured-block  */
21901
21902 #define OMP_SINGLE_CLAUSE_MASK                          \
21903         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21904         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21905         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21906         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21907
21908 static tree
21909 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21910 {
21911   tree stmt = make_node (OMP_SINGLE);
21912   TREE_TYPE (stmt) = void_type_node;
21913
21914   OMP_SINGLE_CLAUSES (stmt)
21915     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21916                                  "#pragma omp single", pragma_tok);
21917   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21918
21919   return add_stmt (stmt);
21920 }
21921
21922 /* OpenMP 3.0:
21923    # pragma omp task task-clause[optseq] new-line
21924      structured-block  */
21925
21926 #define OMP_TASK_CLAUSE_MASK                            \
21927         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21928         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21929         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21930         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21931         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21932         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21933
21934 static tree
21935 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21936 {
21937   tree clauses, block;
21938   unsigned int save;
21939
21940   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21941                                        "#pragma omp task", pragma_tok);
21942   block = begin_omp_task ();
21943   save = cp_parser_begin_omp_structured_block (parser);
21944   cp_parser_statement (parser, NULL_TREE, false, NULL);
21945   cp_parser_end_omp_structured_block (parser, save);
21946   return finish_omp_task (clauses, block);
21947 }
21948
21949 /* OpenMP 3.0:
21950    # pragma omp taskwait new-line  */
21951
21952 static void
21953 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21954 {
21955   cp_parser_require_pragma_eol (parser, pragma_tok);
21956   finish_omp_taskwait ();
21957 }
21958
21959 /* OpenMP 2.5:
21960    # pragma omp threadprivate (variable-list) */
21961
21962 static void
21963 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21964 {
21965   tree vars;
21966
21967   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21968   cp_parser_require_pragma_eol (parser, pragma_tok);
21969
21970   finish_omp_threadprivate (vars);
21971 }
21972
21973 /* Main entry point to OpenMP statement pragmas.  */
21974
21975 static void
21976 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21977 {
21978   tree stmt;
21979
21980   switch (pragma_tok->pragma_kind)
21981     {
21982     case PRAGMA_OMP_ATOMIC:
21983       cp_parser_omp_atomic (parser, pragma_tok);
21984       return;
21985     case PRAGMA_OMP_CRITICAL:
21986       stmt = cp_parser_omp_critical (parser, pragma_tok);
21987       break;
21988     case PRAGMA_OMP_FOR:
21989       stmt = cp_parser_omp_for (parser, pragma_tok);
21990       break;
21991     case PRAGMA_OMP_MASTER:
21992       stmt = cp_parser_omp_master (parser, pragma_tok);
21993       break;
21994     case PRAGMA_OMP_ORDERED:
21995       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21996       break;
21997     case PRAGMA_OMP_PARALLEL:
21998       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21999       break;
22000     case PRAGMA_OMP_SECTIONS:
22001       stmt = cp_parser_omp_sections (parser, pragma_tok);
22002       break;
22003     case PRAGMA_OMP_SINGLE:
22004       stmt = cp_parser_omp_single (parser, pragma_tok);
22005       break;
22006     case PRAGMA_OMP_TASK:
22007       stmt = cp_parser_omp_task (parser, pragma_tok);
22008       break;
22009     default:
22010       gcc_unreachable ();
22011     }
22012
22013   if (stmt)
22014     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22015 }
22016 \f
22017 /* The parser.  */
22018
22019 static GTY (()) cp_parser *the_parser;
22020
22021 \f
22022 /* Special handling for the first token or line in the file.  The first
22023    thing in the file might be #pragma GCC pch_preprocess, which loads a
22024    PCH file, which is a GC collection point.  So we need to handle this
22025    first pragma without benefit of an existing lexer structure.
22026
22027    Always returns one token to the caller in *FIRST_TOKEN.  This is
22028    either the true first token of the file, or the first token after
22029    the initial pragma.  */
22030
22031 static void
22032 cp_parser_initial_pragma (cp_token *first_token)
22033 {
22034   tree name = NULL;
22035
22036   cp_lexer_get_preprocessor_token (NULL, first_token);
22037   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22038     return;
22039
22040   cp_lexer_get_preprocessor_token (NULL, first_token);
22041   if (first_token->type == CPP_STRING)
22042     {
22043       name = first_token->u.value;
22044
22045       cp_lexer_get_preprocessor_token (NULL, first_token);
22046       if (first_token->type != CPP_PRAGMA_EOL)
22047         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
22048                &first_token->location);
22049     }
22050   else
22051     error ("%Hexpected string literal", &first_token->location);
22052
22053   /* Skip to the end of the pragma.  */
22054   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22055     cp_lexer_get_preprocessor_token (NULL, first_token);
22056
22057   /* Now actually load the PCH file.  */
22058   if (name)
22059     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22060
22061   /* Read one more token to return to our caller.  We have to do this
22062      after reading the PCH file in, since its pointers have to be
22063      live.  */
22064   cp_lexer_get_preprocessor_token (NULL, first_token);
22065 }
22066
22067 /* Normal parsing of a pragma token.  Here we can (and must) use the
22068    regular lexer.  */
22069
22070 static bool
22071 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22072 {
22073   cp_token *pragma_tok;
22074   unsigned int id;
22075
22076   pragma_tok = cp_lexer_consume_token (parser->lexer);
22077   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22078   parser->lexer->in_pragma = true;
22079
22080   id = pragma_tok->pragma_kind;
22081   switch (id)
22082     {
22083     case PRAGMA_GCC_PCH_PREPROCESS:
22084       error ("%H%<#pragma GCC pch_preprocess%> must be first",
22085              &pragma_tok->location);
22086       break;
22087
22088     case PRAGMA_OMP_BARRIER:
22089       switch (context)
22090         {
22091         case pragma_compound:
22092           cp_parser_omp_barrier (parser, pragma_tok);
22093           return false;
22094         case pragma_stmt:
22095           error ("%H%<#pragma omp barrier%> may only be "
22096                  "used in compound statements", &pragma_tok->location);
22097           break;
22098         default:
22099           goto bad_stmt;
22100         }
22101       break;
22102
22103     case PRAGMA_OMP_FLUSH:
22104       switch (context)
22105         {
22106         case pragma_compound:
22107           cp_parser_omp_flush (parser, pragma_tok);
22108           return false;
22109         case pragma_stmt:
22110           error ("%H%<#pragma omp flush%> may only be "
22111                  "used in compound statements", &pragma_tok->location);
22112           break;
22113         default:
22114           goto bad_stmt;
22115         }
22116       break;
22117
22118     case PRAGMA_OMP_TASKWAIT:
22119       switch (context)
22120         {
22121         case pragma_compound:
22122           cp_parser_omp_taskwait (parser, pragma_tok);
22123           return false;
22124         case pragma_stmt:
22125           error ("%H%<#pragma omp taskwait%> may only be "
22126                  "used in compound statements",
22127                  &pragma_tok->location);
22128           break;
22129         default:
22130           goto bad_stmt;
22131         }
22132       break;
22133
22134     case PRAGMA_OMP_THREADPRIVATE:
22135       cp_parser_omp_threadprivate (parser, pragma_tok);
22136       return false;
22137
22138     case PRAGMA_OMP_ATOMIC:
22139     case PRAGMA_OMP_CRITICAL:
22140     case PRAGMA_OMP_FOR:
22141     case PRAGMA_OMP_MASTER:
22142     case PRAGMA_OMP_ORDERED:
22143     case PRAGMA_OMP_PARALLEL:
22144     case PRAGMA_OMP_SECTIONS:
22145     case PRAGMA_OMP_SINGLE:
22146     case PRAGMA_OMP_TASK:
22147       if (context == pragma_external)
22148         goto bad_stmt;
22149       cp_parser_omp_construct (parser, pragma_tok);
22150       return true;
22151
22152     case PRAGMA_OMP_SECTION:
22153       error ("%H%<#pragma omp section%> may only be used in "
22154              "%<#pragma omp sections%> construct", &pragma_tok->location);
22155       break;
22156
22157     default:
22158       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22159       c_invoke_pragma_handler (id);
22160       break;
22161
22162     bad_stmt:
22163       cp_parser_error (parser, "expected declaration specifiers");
22164       break;
22165     }
22166
22167   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22168   return false;
22169 }
22170
22171 /* The interface the pragma parsers have to the lexer.  */
22172
22173 enum cpp_ttype
22174 pragma_lex (tree *value)
22175 {
22176   cp_token *tok;
22177   enum cpp_ttype ret;
22178
22179   tok = cp_lexer_peek_token (the_parser->lexer);
22180
22181   ret = tok->type;
22182   *value = tok->u.value;
22183
22184   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22185     ret = CPP_EOF;
22186   else if (ret == CPP_STRING)
22187     *value = cp_parser_string_literal (the_parser, false, false);
22188   else
22189     {
22190       cp_lexer_consume_token (the_parser->lexer);
22191       if (ret == CPP_KEYWORD)
22192         ret = CPP_NAME;
22193     }
22194
22195   return ret;
22196 }
22197
22198 \f
22199 /* External interface.  */
22200
22201 /* Parse one entire translation unit.  */
22202
22203 void
22204 c_parse_file (void)
22205 {
22206   bool error_occurred;
22207   static bool already_called = false;
22208
22209   if (already_called)
22210     {
22211       sorry ("inter-module optimizations not implemented for C++");
22212       return;
22213     }
22214   already_called = true;
22215
22216   the_parser = cp_parser_new ();
22217   push_deferring_access_checks (flag_access_control
22218                                 ? dk_no_deferred : dk_no_check);
22219   error_occurred = cp_parser_translation_unit (the_parser);
22220   the_parser = NULL;
22221 }
22222
22223 #include "gt-cp-parser.h"