OSDN Git Service

PR preprocessor/34695
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The location at which this token was found.  */
81   location_t location;
82   /* The value associated with this token, if any.  */
83   union cp_token_value {
84     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
85     struct tree_check* GTY((tag ("1"))) tree_check_value;
86     /* Use for all other tokens.  */
87     tree GTY((tag ("0"))) value;
88   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99 };
100
101 /* The cp_lexer structure represents the C++ lexer.  It is responsible
102    for managing the token stream from the preprocessor and supplying
103    it to the parser.  Tokens are never added to the cp_lexer after
104    it is created.  */
105
106 typedef struct cp_lexer GTY (())
107 {
108   /* The memory allocated for the buffer.  NULL if this lexer does not
109      own the token buffer.  */
110   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
111   /* If the lexer owns the buffer, this is the number of tokens in the
112      buffer.  */
113   size_t buffer_length;
114
115   /* A pointer just past the last available token.  The tokens
116      in this lexer are [buffer, last_token).  */
117   cp_token_position GTY ((skip)) last_token;
118
119   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
120      no more available tokens.  */
121   cp_token_position GTY ((skip)) next_token;
122
123   /* A stack indicating positions at which cp_lexer_save_tokens was
124      called.  The top entry is the most recent position at which we
125      began saving tokens.  If the stack is non-empty, we are saving
126      tokens.  */
127   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
128
129   /* The next lexer in a linked list of lexers.  */
130   struct cp_lexer *next;
131
132   /* True if we should output debugging information.  */
133   bool debugging_p;
134
135   /* True if we're in the context of parsing a pragma, and should not
136      increment past the end-of-line marker.  */
137   bool in_pragma;
138 } cp_lexer;
139
140 /* cp_token_cache is a range of tokens.  There is no need to represent
141    allocate heap memory for it, since tokens are never removed from the
142    lexer's array.  There is also no need for the GC to walk through
143    a cp_token_cache, since everything in here is referenced through
144    a lexer.  */
145
146 typedef struct cp_token_cache GTY(())
147 {
148   /* The beginning of the token range.  */
149   cp_token * GTY((skip)) first;
150
151   /* Points immediately after the last token in the range.  */
152   cp_token * GTY ((skip)) last;
153 } cp_token_cache;
154
155 /* Prototypes.  */
156
157 static cp_lexer *cp_lexer_new_main
158   (void);
159 static cp_lexer *cp_lexer_new_from_tokens
160   (cp_token_cache *tokens);
161 static void cp_lexer_destroy
162   (cp_lexer *);
163 static int cp_lexer_saving_tokens
164   (const cp_lexer *);
165 static cp_token_position cp_lexer_token_position
166   (cp_lexer *, bool);
167 static cp_token *cp_lexer_token_at
168   (cp_lexer *, cp_token_position);
169 static void cp_lexer_get_preprocessor_token
170   (cp_lexer *, cp_token *);
171 static inline cp_token *cp_lexer_peek_token
172   (cp_lexer *);
173 static cp_token *cp_lexer_peek_nth_token
174   (cp_lexer *, size_t);
175 static inline bool cp_lexer_next_token_is
176   (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_not
178   (cp_lexer *, enum cpp_ttype);
179 static bool cp_lexer_next_token_is_keyword
180   (cp_lexer *, enum rid);
181 static cp_token *cp_lexer_consume_token
182   (cp_lexer *);
183 static void cp_lexer_purge_token
184   (cp_lexer *);
185 static void cp_lexer_purge_tokens_after
186   (cp_lexer *, cp_token_position);
187 static void cp_lexer_save_tokens
188   (cp_lexer *);
189 static void cp_lexer_commit_tokens
190   (cp_lexer *);
191 static void cp_lexer_rollback_tokens
192   (cp_lexer *);
193 #ifdef ENABLE_CHECKING
194 static void cp_lexer_print_token
195   (FILE *, cp_token *);
196 static inline bool cp_lexer_debugging_p
197   (cp_lexer *);
198 static void cp_lexer_start_debugging
199   (cp_lexer *) ATTRIBUTE_UNUSED;
200 static void cp_lexer_stop_debugging
201   (cp_lexer *) ATTRIBUTE_UNUSED;
202 #else
203 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
204    about passing NULL to functions that require non-NULL arguments
205    (fputs, fprintf).  It will never be used, so all we need is a value
206    of the right type that's guaranteed not to be NULL.  */
207 #define cp_lexer_debug_stream stdout
208 #define cp_lexer_print_token(str, tok) (void) 0
209 #define cp_lexer_debugging_p(lexer) 0
210 #endif /* ENABLE_CHECKING */
211
212 static cp_token_cache *cp_token_cache_new
213   (cp_token *, cp_token *);
214
215 static void cp_parser_initial_pragma
216   (cp_token *);
217
218 /* Manifest constants.  */
219 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
220 #define CP_SAVED_TOKEN_STACK 5
221
222 /* A token type for keywords, as opposed to ordinary identifiers.  */
223 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
224
225 /* A token type for template-ids.  If a template-id is processed while
226    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
227    the value of the CPP_TEMPLATE_ID is whatever was returned by
228    cp_parser_template_id.  */
229 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
230
231 /* A token type for nested-name-specifiers.  If a
232    nested-name-specifier is processed while parsing tentatively, it is
233    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
234    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
235    cp_parser_nested_name_specifier_opt.  */
236 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
237
238 /* A token type for tokens that are not tokens at all; these are used
239    to represent slots in the array where there used to be a token
240    that has now been deleted.  */
241 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
242
243 /* The number of token types, including C++-specific ones.  */
244 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245
246 /* Variables.  */
247
248 #ifdef ENABLE_CHECKING
249 /* The stream to which debugging output should be written.  */
250 static FILE *cp_lexer_debug_stream;
251 #endif /* ENABLE_CHECKING */
252
253 /* Create a new main C++ lexer, the lexer that gets tokens from the
254    preprocessor.  */
255
256 static cp_lexer *
257 cp_lexer_new_main (void)
258 {
259   cp_token first_token;
260   cp_lexer *lexer;
261   cp_token *pos;
262   size_t alloc;
263   size_t space;
264   cp_token *buffer;
265
266   /* It's possible that parsing the first pragma will load a PCH file,
267      which is a GC collection point.  So we have to do that before
268      allocating any memory.  */
269   cp_parser_initial_pragma (&first_token);
270
271   c_common_no_more_pch ();
272
273   /* Allocate the memory.  */
274   lexer = GGC_CNEW (cp_lexer);
275
276 #ifdef ENABLE_CHECKING
277   /* Initially we are not debugging.  */
278   lexer->debugging_p = false;
279 #endif /* ENABLE_CHECKING */
280   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
281                                    CP_SAVED_TOKEN_STACK);
282
283   /* Create the buffer.  */
284   alloc = CP_LEXER_BUFFER_SIZE;
285   buffer = GGC_NEWVEC (cp_token, alloc);
286
287   /* Put the first token in the buffer.  */
288   space = alloc;
289   pos = buffer;
290   *pos = first_token;
291
292   /* Get the remaining tokens from the preprocessor.  */
293   while (pos->type != CPP_EOF)
294     {
295       pos++;
296       if (!--space)
297         {
298           space = alloc;
299           alloc *= 2;
300           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
301           pos = buffer + space;
302         }
303       cp_lexer_get_preprocessor_token (lexer, pos);
304     }
305   lexer->buffer = buffer;
306   lexer->buffer_length = alloc - space;
307   lexer->last_token = pos;
308   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
309
310   /* Subsequent preprocessor diagnostics should use compiler
311      diagnostic functions to get the compiler source location.  */
312   done_lexing = true;
313
314   gcc_assert (lexer->next_token->type != CPP_PURGED);
315   return lexer;
316 }
317
318 /* Create a new lexer whose token stream is primed with the tokens in
319    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
320
321 static cp_lexer *
322 cp_lexer_new_from_tokens (cp_token_cache *cache)
323 {
324   cp_token *first = cache->first;
325   cp_token *last = cache->last;
326   cp_lexer *lexer = GGC_CNEW (cp_lexer);
327
328   /* We do not own the buffer.  */
329   lexer->buffer = NULL;
330   lexer->buffer_length = 0;
331   lexer->next_token = first == last ? &eof_token : first;
332   lexer->last_token = last;
333
334   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
335                                    CP_SAVED_TOKEN_STACK);
336
337 #ifdef ENABLE_CHECKING
338   /* Initially we are not debugging.  */
339   lexer->debugging_p = false;
340 #endif
341
342   gcc_assert (lexer->next_token->type != CPP_PURGED);
343   return lexer;
344 }
345
346 /* Frees all resources associated with LEXER.  */
347
348 static void
349 cp_lexer_destroy (cp_lexer *lexer)
350 {
351   if (lexer->buffer)
352     ggc_free (lexer->buffer);
353   VEC_free (cp_token_position, heap, lexer->saved_tokens);
354   ggc_free (lexer);
355 }
356
357 /* Returns nonzero if debugging information should be output.  */
358
359 #ifdef ENABLE_CHECKING
360
361 static inline bool
362 cp_lexer_debugging_p (cp_lexer *lexer)
363 {
364   return lexer->debugging_p;
365 }
366
367 #endif /* ENABLE_CHECKING */
368
369 static inline cp_token_position
370 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
371 {
372   gcc_assert (!previous_p || lexer->next_token != &eof_token);
373
374   return lexer->next_token - previous_p;
375 }
376
377 static inline cp_token *
378 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
379 {
380   return pos;
381 }
382
383 /* nonzero if we are presently saving tokens.  */
384
385 static inline int
386 cp_lexer_saving_tokens (const cp_lexer* lexer)
387 {
388   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
389 }
390
391 /* Store the next token from the preprocessor in *TOKEN.  Return true
392    if we reach EOF.  If LEXER is NULL, assume we are handling an
393    initial #pragma pch_preprocess, and thus want the lexer to return
394    processed strings.  */
395
396 static void
397 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
398 {
399   static int is_extern_c = 0;
400
401    /* Get a new token from the preprocessor.  */
402   token->type
403     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
404                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
405   token->keyword = RID_MAX;
406   token->pragma_kind = PRAGMA_NONE;
407
408   /* On some systems, some header files are surrounded by an
409      implicit extern "C" block.  Set a flag in the token if it
410      comes from such a header.  */
411   is_extern_c += pending_lang_change;
412   pending_lang_change = 0;
413   token->implicit_extern_c = is_extern_c > 0;
414
415   /* Check to see if this token is a keyword.  */
416   if (token->type == CPP_NAME)
417     {
418       if (C_IS_RESERVED_WORD (token->u.value))
419         {
420           /* Mark this token as a keyword.  */
421           token->type = CPP_KEYWORD;
422           /* Record which keyword.  */
423           token->keyword = C_RID_CODE (token->u.value);
424           /* Update the value.  Some keywords are mapped to particular
425              entities, rather than simply having the value of the
426              corresponding IDENTIFIER_NODE.  For example, `__const' is
427              mapped to `const'.  */
428           token->u.value = ridpointers[token->keyword];
429         }
430       else
431         {
432           if (warn_cxx0x_compat
433               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
434               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
435             {
436               /* Warn about the C++0x keyword (but still treat it as
437                  an identifier).  */
438               warning (OPT_Wc__0x_compat, 
439                        "identifier %<%s%> will become a keyword in C++0x",
440                        IDENTIFIER_POINTER (token->u.value));
441
442               /* Clear out the C_RID_CODE so we don't warn about this
443                  particular identifier-turned-keyword again.  */
444               C_SET_RID_CODE (token->u.value, RID_MAX);
445             }
446
447           token->ambiguous_p = false;
448           token->keyword = RID_MAX;
449         }
450     }
451   /* Handle Objective-C++ keywords.  */
452   else if (token->type == CPP_AT_NAME)
453     {
454       token->type = CPP_KEYWORD;
455       switch (C_RID_CODE (token->u.value))
456         {
457         /* Map 'class' to '@class', 'private' to '@private', etc.  */
458         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
459         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
460         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
461         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
462         case RID_THROW: token->keyword = RID_AT_THROW; break;
463         case RID_TRY: token->keyword = RID_AT_TRY; break;
464         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
465         default: token->keyword = C_RID_CODE (token->u.value);
466         }
467     }
468   else if (token->type == CPP_PRAGMA)
469     {
470       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
471       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
472       token->u.value = NULL_TREE;
473     }
474 }
475
476 /* Update the globals input_location and the input file stack from TOKEN.  */
477 static inline void
478 cp_lexer_set_source_position_from_token (cp_token *token)
479 {
480   if (token->type != CPP_EOF)
481     {
482       input_location = token->location;
483     }
484 }
485
486 /* Return a pointer to the next token in the token stream, but do not
487    consume it.  */
488
489 static inline cp_token *
490 cp_lexer_peek_token (cp_lexer *lexer)
491 {
492   if (cp_lexer_debugging_p (lexer))
493     {
494       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
495       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
496       putc ('\n', cp_lexer_debug_stream);
497     }
498   return lexer->next_token;
499 }
500
501 /* Return true if the next token has the indicated TYPE.  */
502
503 static inline bool
504 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
505 {
506   return cp_lexer_peek_token (lexer)->type == type;
507 }
508
509 /* Return true if the next token does not have the indicated TYPE.  */
510
511 static inline bool
512 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
513 {
514   return !cp_lexer_next_token_is (lexer, type);
515 }
516
517 /* Return true if the next token is the indicated KEYWORD.  */
518
519 static inline bool
520 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
521 {
522   return cp_lexer_peek_token (lexer)->keyword == keyword;
523 }
524
525 /* Return true if the next token is not the indicated KEYWORD.  */
526
527 static inline bool
528 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
529 {
530   return cp_lexer_peek_token (lexer)->keyword != keyword;
531 }
532
533 /* Return true if the next token is a keyword for a decl-specifier.  */
534
535 static bool
536 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
537 {
538   cp_token *token;
539
540   token = cp_lexer_peek_token (lexer);
541   switch (token->keyword) 
542     {
543       /* auto specifier: storage-class-specifier in C++,
544          simple-type-specifier in C++0x.  */
545     case RID_AUTO:
546       /* Storage classes.  */
547     case RID_REGISTER:
548     case RID_STATIC:
549     case RID_EXTERN:
550     case RID_MUTABLE:
551     case RID_THREAD:
552       /* Elaborated type specifiers.  */
553     case RID_ENUM:
554     case RID_CLASS:
555     case RID_STRUCT:
556     case RID_UNION:
557     case RID_TYPENAME:
558       /* Simple type specifiers.  */
559     case RID_CHAR:
560     case RID_CHAR16:
561     case RID_CHAR32:
562     case RID_WCHAR:
563     case RID_BOOL:
564     case RID_SHORT:
565     case RID_INT:
566     case RID_LONG:
567     case RID_SIGNED:
568     case RID_UNSIGNED:
569     case RID_FLOAT:
570     case RID_DOUBLE:
571     case RID_VOID:
572       /* GNU extensions.  */ 
573     case RID_ATTRIBUTE:
574     case RID_TYPEOF:
575       /* C++0x extensions.  */
576     case RID_DECLTYPE:
577       return true;
578
579     default:
580       return false;
581     }
582 }
583
584 /* Return a pointer to the Nth token in the token stream.  If N is 1,
585    then this is precisely equivalent to cp_lexer_peek_token (except
586    that it is not inline).  One would like to disallow that case, but
587    there is one case (cp_parser_nth_token_starts_template_id) where
588    the caller passes a variable for N and it might be 1.  */
589
590 static cp_token *
591 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
592 {
593   cp_token *token;
594
595   /* N is 1-based, not zero-based.  */
596   gcc_assert (n > 0);
597
598   if (cp_lexer_debugging_p (lexer))
599     fprintf (cp_lexer_debug_stream,
600              "cp_lexer: peeking ahead %ld at token: ", (long)n);
601
602   --n;
603   token = lexer->next_token;
604   gcc_assert (!n || token != &eof_token);
605   while (n != 0)
606     {
607       ++token;
608       if (token == lexer->last_token)
609         {
610           token = &eof_token;
611           break;
612         }
613
614       if (token->type != CPP_PURGED)
615         --n;
616     }
617
618   if (cp_lexer_debugging_p (lexer))
619     {
620       cp_lexer_print_token (cp_lexer_debug_stream, token);
621       putc ('\n', cp_lexer_debug_stream);
622     }
623
624   return token;
625 }
626
627 /* Return the next token, and advance the lexer's next_token pointer
628    to point to the next non-purged token.  */
629
630 static cp_token *
631 cp_lexer_consume_token (cp_lexer* lexer)
632 {
633   cp_token *token = lexer->next_token;
634
635   gcc_assert (token != &eof_token);
636   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
637
638   do
639     {
640       lexer->next_token++;
641       if (lexer->next_token == lexer->last_token)
642         {
643           lexer->next_token = &eof_token;
644           break;
645         }
646
647     }
648   while (lexer->next_token->type == CPP_PURGED);
649
650   cp_lexer_set_source_position_from_token (token);
651
652   /* Provide debugging output.  */
653   if (cp_lexer_debugging_p (lexer))
654     {
655       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
656       cp_lexer_print_token (cp_lexer_debug_stream, token);
657       putc ('\n', cp_lexer_debug_stream);
658     }
659
660   return token;
661 }
662
663 /* Permanently remove the next token from the token stream, and
664    advance the next_token pointer to refer to the next non-purged
665    token.  */
666
667 static void
668 cp_lexer_purge_token (cp_lexer *lexer)
669 {
670   cp_token *tok = lexer->next_token;
671
672   gcc_assert (tok != &eof_token);
673   tok->type = CPP_PURGED;
674   tok->location = UNKNOWN_LOCATION;
675   tok->u.value = NULL_TREE;
676   tok->keyword = RID_MAX;
677
678   do
679     {
680       tok++;
681       if (tok == lexer->last_token)
682         {
683           tok = &eof_token;
684           break;
685         }
686     }
687   while (tok->type == CPP_PURGED);
688   lexer->next_token = tok;
689 }
690
691 /* Permanently remove all tokens after TOK, up to, but not
692    including, the token that will be returned next by
693    cp_lexer_peek_token.  */
694
695 static void
696 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
697 {
698   cp_token *peek = lexer->next_token;
699
700   if (peek == &eof_token)
701     peek = lexer->last_token;
702
703   gcc_assert (tok < peek);
704
705   for ( tok += 1; tok != peek; tok += 1)
706     {
707       tok->type = CPP_PURGED;
708       tok->location = UNKNOWN_LOCATION;
709       tok->u.value = NULL_TREE;
710       tok->keyword = RID_MAX;
711     }
712 }
713
714 /* Begin saving tokens.  All tokens consumed after this point will be
715    preserved.  */
716
717 static void
718 cp_lexer_save_tokens (cp_lexer* lexer)
719 {
720   /* Provide debugging output.  */
721   if (cp_lexer_debugging_p (lexer))
722     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
723
724   VEC_safe_push (cp_token_position, heap,
725                  lexer->saved_tokens, lexer->next_token);
726 }
727
728 /* Commit to the portion of the token stream most recently saved.  */
729
730 static void
731 cp_lexer_commit_tokens (cp_lexer* lexer)
732 {
733   /* Provide debugging output.  */
734   if (cp_lexer_debugging_p (lexer))
735     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
736
737   VEC_pop (cp_token_position, lexer->saved_tokens);
738 }
739
740 /* Return all tokens saved since the last call to cp_lexer_save_tokens
741    to the token stream.  Stop saving tokens.  */
742
743 static void
744 cp_lexer_rollback_tokens (cp_lexer* lexer)
745 {
746   /* Provide debugging output.  */
747   if (cp_lexer_debugging_p (lexer))
748     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
749
750   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
751 }
752
753 /* Print a representation of the TOKEN on the STREAM.  */
754
755 #ifdef ENABLE_CHECKING
756
757 static void
758 cp_lexer_print_token (FILE * stream, cp_token *token)
759 {
760   /* We don't use cpp_type2name here because the parser defines
761      a few tokens of its own.  */
762   static const char *const token_names[] = {
763     /* cpplib-defined token types */
764 #define OP(e, s) #e,
765 #define TK(e, s) #e,
766     TTYPE_TABLE
767 #undef OP
768 #undef TK
769     /* C++ parser token types - see "Manifest constants", above.  */
770     "KEYWORD",
771     "TEMPLATE_ID",
772     "NESTED_NAME_SPECIFIER",
773     "PURGED"
774   };
775
776   /* If we have a name for the token, print it out.  Otherwise, we
777      simply give the numeric code.  */
778   gcc_assert (token->type < ARRAY_SIZE(token_names));
779   fputs (token_names[token->type], stream);
780
781   /* For some tokens, print the associated data.  */
782   switch (token->type)
783     {
784     case CPP_KEYWORD:
785       /* Some keywords have a value that is not an IDENTIFIER_NODE.
786          For example, `struct' is mapped to an INTEGER_CST.  */
787       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
788         break;
789       /* else fall through */
790     case CPP_NAME:
791       fputs (IDENTIFIER_POINTER (token->u.value), stream);
792       break;
793
794     case CPP_STRING:
795     case CPP_STRING16:
796     case CPP_STRING32:
797     case CPP_WSTRING:
798       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
799       break;
800
801     default:
802       break;
803     }
804 }
805
806 /* Start emitting debugging information.  */
807
808 static void
809 cp_lexer_start_debugging (cp_lexer* lexer)
810 {
811   lexer->debugging_p = true;
812 }
813
814 /* Stop emitting debugging information.  */
815
816 static void
817 cp_lexer_stop_debugging (cp_lexer* lexer)
818 {
819   lexer->debugging_p = false;
820 }
821
822 #endif /* ENABLE_CHECKING */
823
824 /* Create a new cp_token_cache, representing a range of tokens.  */
825
826 static cp_token_cache *
827 cp_token_cache_new (cp_token *first, cp_token *last)
828 {
829   cp_token_cache *cache = GGC_NEW (cp_token_cache);
830   cache->first = first;
831   cache->last = last;
832   return cache;
833 }
834
835 \f
836 /* Decl-specifiers.  */
837
838 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
839
840 static void
841 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
842 {
843   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
844 }
845
846 /* Declarators.  */
847
848 /* Nothing other than the parser should be creating declarators;
849    declarators are a semi-syntactic representation of C++ entities.
850    Other parts of the front end that need to create entities (like
851    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
852
853 static cp_declarator *make_call_declarator
854   (cp_declarator *, tree, cp_cv_quals, tree, tree);
855 static cp_declarator *make_array_declarator
856   (cp_declarator *, tree);
857 static cp_declarator *make_pointer_declarator
858   (cp_cv_quals, cp_declarator *);
859 static cp_declarator *make_reference_declarator
860   (cp_cv_quals, cp_declarator *, bool);
861 static cp_parameter_declarator *make_parameter_declarator
862   (cp_decl_specifier_seq *, cp_declarator *, tree);
863 static cp_declarator *make_ptrmem_declarator
864   (cp_cv_quals, tree, cp_declarator *);
865
866 /* An erroneous declarator.  */
867 static cp_declarator *cp_error_declarator;
868
869 /* The obstack on which declarators and related data structures are
870    allocated.  */
871 static struct obstack declarator_obstack;
872
873 /* Alloc BYTES from the declarator memory pool.  */
874
875 static inline void *
876 alloc_declarator (size_t bytes)
877 {
878   return obstack_alloc (&declarator_obstack, bytes);
879 }
880
881 /* Allocate a declarator of the indicated KIND.  Clear fields that are
882    common to all declarators.  */
883
884 static cp_declarator *
885 make_declarator (cp_declarator_kind kind)
886 {
887   cp_declarator *declarator;
888
889   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
890   declarator->kind = kind;
891   declarator->attributes = NULL_TREE;
892   declarator->declarator = NULL;
893   declarator->parameter_pack_p = false;
894
895   return declarator;
896 }
897
898 /* Make a declarator for a generalized identifier.  If
899    QUALIFYING_SCOPE is non-NULL, the identifier is
900    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
902    is, if any.   */
903
904 static cp_declarator *
905 make_id_declarator (tree qualifying_scope, tree unqualified_name,
906                     special_function_kind sfk)
907 {
908   cp_declarator *declarator;
909
910   /* It is valid to write:
911
912        class C { void f(); };
913        typedef C D;
914        void D::f();
915
916      The standard is not clear about whether `typedef const C D' is
917      legal; as of 2002-09-15 the committee is considering that
918      question.  EDG 3.0 allows that syntax.  Therefore, we do as
919      well.  */
920   if (qualifying_scope && TYPE_P (qualifying_scope))
921     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922
923   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926
927   declarator = make_declarator (cdk_id);
928   declarator->u.id.qualifying_scope = qualifying_scope;
929   declarator->u.id.unqualified_name = unqualified_name;
930   declarator->u.id.sfk = sfk;
931   
932   return declarator;
933 }
934
935 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
936    of modifiers such as const or volatile to apply to the pointer
937    type, represented as identifiers.  */
938
939 cp_declarator *
940 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 {
942   cp_declarator *declarator;
943
944   declarator = make_declarator (cdk_pointer);
945   declarator->declarator = target;
946   declarator->u.pointer.qualifiers = cv_qualifiers;
947   declarator->u.pointer.class_type = NULL_TREE;
948   if (target)
949     {
950       declarator->parameter_pack_p = target->parameter_pack_p;
951       target->parameter_pack_p = false;
952     }
953   else
954     declarator->parameter_pack_p = false;
955
956   return declarator;
957 }
958
959 /* Like make_pointer_declarator -- but for references.  */
960
961 cp_declarator *
962 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963                            bool rvalue_ref)
964 {
965   cp_declarator *declarator;
966
967   declarator = make_declarator (cdk_reference);
968   declarator->declarator = target;
969   declarator->u.reference.qualifiers = cv_qualifiers;
970   declarator->u.reference.rvalue_ref = rvalue_ref;
971   if (target)
972     {
973       declarator->parameter_pack_p = target->parameter_pack_p;
974       target->parameter_pack_p = false;
975     }
976   else
977     declarator->parameter_pack_p = false;
978
979   return declarator;
980 }
981
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983    member of CLASS_TYPE.  */
984
985 cp_declarator *
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987                         cp_declarator *pointee)
988 {
989   cp_declarator *declarator;
990
991   declarator = make_declarator (cdk_ptrmem);
992   declarator->declarator = pointee;
993   declarator->u.pointer.qualifiers = cv_qualifiers;
994   declarator->u.pointer.class_type = class_type;
995
996   if (pointee)
997     {
998       declarator->parameter_pack_p = pointee->parameter_pack_p;
999       pointee->parameter_pack_p = false;
1000     }
1001   else
1002     declarator->parameter_pack_p = false;
1003
1004   return declarator;
1005 }
1006
1007 /* Make a declarator for the function given by TARGET, with the
1008    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1009    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1010    indicates what exceptions can be thrown.  */
1011
1012 cp_declarator *
1013 make_call_declarator (cp_declarator *target,
1014                       tree parms,
1015                       cp_cv_quals cv_qualifiers,
1016                       tree exception_specification,
1017                       tree late_return_type)
1018 {
1019   cp_declarator *declarator;
1020
1021   declarator = make_declarator (cdk_function);
1022   declarator->declarator = target;
1023   declarator->u.function.parameters = parms;
1024   declarator->u.function.qualifiers = cv_qualifiers;
1025   declarator->u.function.exception_specification = exception_specification;
1026   declarator->u.function.late_return_type = late_return_type;
1027   if (target)
1028     {
1029       declarator->parameter_pack_p = target->parameter_pack_p;
1030       target->parameter_pack_p = false;
1031     }
1032   else
1033     declarator->parameter_pack_p = false;
1034
1035   return declarator;
1036 }
1037
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039    defined by ELEMENT.  */
1040
1041 cp_declarator *
1042 make_array_declarator (cp_declarator *element, tree bounds)
1043 {
1044   cp_declarator *declarator;
1045
1046   declarator = make_declarator (cdk_array);
1047   declarator->declarator = element;
1048   declarator->u.array.bounds = bounds;
1049   if (element)
1050     {
1051       declarator->parameter_pack_p = element->parameter_pack_p;
1052       element->parameter_pack_p = false;
1053     }
1054   else
1055     declarator->parameter_pack_p = false;
1056
1057   return declarator;
1058 }
1059
1060 /* Determine whether the declarator we've seen so far can be a
1061    parameter pack, when followed by an ellipsis.  */
1062 static bool 
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 {
1065   /* Search for a declarator name, or any other declarator that goes
1066      after the point where the ellipsis could appear in a parameter
1067      pack. If we find any of these, then this declarator can not be
1068      made into a parameter pack.  */
1069   bool found = false;
1070   while (declarator && !found)
1071     {
1072       switch ((int)declarator->kind)
1073         {
1074         case cdk_id:
1075         case cdk_array:
1076           found = true;
1077           break;
1078
1079         case cdk_error:
1080           return true;
1081
1082         default:
1083           declarator = declarator->declarator;
1084           break;
1085         }
1086     }
1087
1088   return !found;
1089 }
1090
1091 cp_parameter_declarator *no_parameters;
1092
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094    DECLARATOR and DEFAULT_ARGUMENT.  */
1095
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098                            cp_declarator *declarator,
1099                            tree default_argument)
1100 {
1101   cp_parameter_declarator *parameter;
1102
1103   parameter = ((cp_parameter_declarator *)
1104                alloc_declarator (sizeof (cp_parameter_declarator)));
1105   parameter->next = NULL;
1106   if (decl_specifiers)
1107     parameter->decl_specifiers = *decl_specifiers;
1108   else
1109     clear_decl_specs (&parameter->decl_specifiers);
1110   parameter->declarator = declarator;
1111   parameter->default_argument = default_argument;
1112   parameter->ellipsis_p = false;
1113
1114   return parameter;
1115 }
1116
1117 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1118
1119 static bool
1120 function_declarator_p (const cp_declarator *declarator)
1121 {
1122   while (declarator)
1123     {
1124       if (declarator->kind == cdk_function
1125           && declarator->declarator->kind == cdk_id)
1126         return true;
1127       if (declarator->kind == cdk_id
1128           || declarator->kind == cdk_error)
1129         return false;
1130       declarator = declarator->declarator;
1131     }
1132   return false;
1133 }
1134  
1135 /* The parser.  */
1136
1137 /* Overview
1138    --------
1139
1140    A cp_parser parses the token stream as specified by the C++
1141    grammar.  Its job is purely parsing, not semantic analysis.  For
1142    example, the parser breaks the token stream into declarators,
1143    expressions, statements, and other similar syntactic constructs.
1144    It does not check that the types of the expressions on either side
1145    of an assignment-statement are compatible, or that a function is
1146    not declared with a parameter of type `void'.
1147
1148    The parser invokes routines elsewhere in the compiler to perform
1149    semantic analysis and to build up the abstract syntax tree for the
1150    code processed.
1151
1152    The parser (and the template instantiation code, which is, in a
1153    way, a close relative of parsing) are the only parts of the
1154    compiler that should be calling push_scope and pop_scope, or
1155    related functions.  The parser (and template instantiation code)
1156    keeps track of what scope is presently active; everything else
1157    should simply honor that.  (The code that generates static
1158    initializers may also need to set the scope, in order to check
1159    access control correctly when emitting the initializers.)
1160
1161    Methodology
1162    -----------
1163
1164    The parser is of the standard recursive-descent variety.  Upcoming
1165    tokens in the token stream are examined in order to determine which
1166    production to use when parsing a non-terminal.  Some C++ constructs
1167    require arbitrary look ahead to disambiguate.  For example, it is
1168    impossible, in the general case, to tell whether a statement is an
1169    expression or declaration without scanning the entire statement.
1170    Therefore, the parser is capable of "parsing tentatively."  When the
1171    parser is not sure what construct comes next, it enters this mode.
1172    Then, while we attempt to parse the construct, the parser queues up
1173    error messages, rather than issuing them immediately, and saves the
1174    tokens it consumes.  If the construct is parsed successfully, the
1175    parser "commits", i.e., it issues any queued error messages and
1176    the tokens that were being preserved are permanently discarded.
1177    If, however, the construct is not parsed successfully, the parser
1178    rolls back its state completely so that it can resume parsing using
1179    a different alternative.
1180
1181    Future Improvements
1182    -------------------
1183
1184    The performance of the parser could probably be improved substantially.
1185    We could often eliminate the need to parse tentatively by looking ahead
1186    a little bit.  In some places, this approach might not entirely eliminate
1187    the need to parse tentatively, but it might still speed up the average
1188    case.  */
1189
1190 /* Flags that are passed to some parsing functions.  These values can
1191    be bitwise-ored together.  */
1192
1193 typedef enum cp_parser_flags
1194 {
1195   /* No flags.  */
1196   CP_PARSER_FLAGS_NONE = 0x0,
1197   /* The construct is optional.  If it is not present, then no error
1198      should be issued.  */
1199   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200   /* When parsing a type-specifier, do not allow user-defined types.  */
1201   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1202 } cp_parser_flags;
1203
1204 /* The different kinds of declarators we want to parse.  */
1205
1206 typedef enum cp_parser_declarator_kind
1207 {
1208   /* We want an abstract declarator.  */
1209   CP_PARSER_DECLARATOR_ABSTRACT,
1210   /* We want a named declarator.  */
1211   CP_PARSER_DECLARATOR_NAMED,
1212   /* We don't mind, but the name must be an unqualified-id.  */
1213   CP_PARSER_DECLARATOR_EITHER
1214 } cp_parser_declarator_kind;
1215
1216 /* The precedence values used to parse binary expressions.  The minimum value
1217    of PREC must be 1, because zero is reserved to quickly discriminate
1218    binary operators from other tokens.  */
1219
1220 enum cp_parser_prec
1221 {
1222   PREC_NOT_OPERATOR,
1223   PREC_LOGICAL_OR_EXPRESSION,
1224   PREC_LOGICAL_AND_EXPRESSION,
1225   PREC_INCLUSIVE_OR_EXPRESSION,
1226   PREC_EXCLUSIVE_OR_EXPRESSION,
1227   PREC_AND_EXPRESSION,
1228   PREC_EQUALITY_EXPRESSION,
1229   PREC_RELATIONAL_EXPRESSION,
1230   PREC_SHIFT_EXPRESSION,
1231   PREC_ADDITIVE_EXPRESSION,
1232   PREC_MULTIPLICATIVE_EXPRESSION,
1233   PREC_PM_EXPRESSION,
1234   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1235 };
1236
1237 /* A mapping from a token type to a corresponding tree node type, with a
1238    precedence value.  */
1239
1240 typedef struct cp_parser_binary_operations_map_node
1241 {
1242   /* The token type.  */
1243   enum cpp_ttype token_type;
1244   /* The corresponding tree code.  */
1245   enum tree_code tree_type;
1246   /* The precedence of this operator.  */
1247   enum cp_parser_prec prec;
1248 } cp_parser_binary_operations_map_node;
1249
1250 /* The status of a tentative parse.  */
1251
1252 typedef enum cp_parser_status_kind
1253 {
1254   /* No errors have occurred.  */
1255   CP_PARSER_STATUS_KIND_NO_ERROR,
1256   /* An error has occurred.  */
1257   CP_PARSER_STATUS_KIND_ERROR,
1258   /* We are committed to this tentative parse, whether or not an error
1259      has occurred.  */
1260   CP_PARSER_STATUS_KIND_COMMITTED
1261 } cp_parser_status_kind;
1262
1263 typedef struct cp_parser_expression_stack_entry
1264 {
1265   /* Left hand side of the binary operation we are currently
1266      parsing.  */
1267   tree lhs;
1268   /* Original tree code for left hand side, if it was a binary
1269      expression itself (used for -Wparentheses).  */
1270   enum tree_code lhs_type;
1271   /* Tree code for the binary operation we are parsing.  */
1272   enum tree_code tree_type;
1273   /* Precedence of the binary operation we are parsing.  */
1274   int prec;
1275 } cp_parser_expression_stack_entry;
1276
1277 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1278    entries because precedence levels on the stack are monotonically
1279    increasing.  */
1280 typedef struct cp_parser_expression_stack_entry
1281   cp_parser_expression_stack[NUM_PREC_VALUES];
1282
1283 /* Context that is saved and restored when parsing tentatively.  */
1284 typedef struct cp_parser_context GTY (())
1285 {
1286   /* If this is a tentative parsing context, the status of the
1287      tentative parse.  */
1288   enum cp_parser_status_kind status;
1289   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1290      that are looked up in this context must be looked up both in the
1291      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292      the context of the containing expression.  */
1293   tree object_type;
1294
1295   /* The next parsing context in the stack.  */
1296   struct cp_parser_context *next;
1297 } cp_parser_context;
1298
1299 /* Prototypes.  */
1300
1301 /* Constructors and destructors.  */
1302
1303 static cp_parser_context *cp_parser_context_new
1304   (cp_parser_context *);
1305
1306 /* Class variables.  */
1307
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1309
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311    Transformed into an associative array (binops_by_token) by
1312    cp_parser_new.  */
1313
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1317
1318   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321
1322   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324
1325   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327
1328   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332
1333   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1335
1336   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1337
1338   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1339
1340   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1343
1344   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1345 };
1346
1347 /* The same as binops, but initialized by cp_parser_new so that
1348    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1349    for speed.  */
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1351
1352 /* Constructors and destructors.  */
1353
1354 /* Construct a new context.  The context below this one on the stack
1355    is given by NEXT.  */
1356
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1359 {
1360   cp_parser_context *context;
1361
1362   /* Allocate the storage.  */
1363   if (cp_parser_context_free_list != NULL)
1364     {
1365       /* Pull the first entry from the free list.  */
1366       context = cp_parser_context_free_list;
1367       cp_parser_context_free_list = context->next;
1368       memset (context, 0, sizeof (*context));
1369     }
1370   else
1371     context = GGC_CNEW (cp_parser_context);
1372
1373   /* No errors have occurred yet in this context.  */
1374   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375   /* If this is not the bottommost context, copy information that we
1376      need from the previous context.  */
1377   if (next)
1378     {
1379       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380          expression, then we are parsing one in this context, too.  */
1381       context->object_type = next->object_type;
1382       /* Thread the stack.  */
1383       context->next = next;
1384     }
1385
1386   return context;
1387 }
1388
1389 /* The cp_parser structure represents the C++ parser.  */
1390
1391 typedef struct cp_parser GTY(())
1392 {
1393   /* The lexer from which we are obtaining tokens.  */
1394   cp_lexer *lexer;
1395
1396   /* The scope in which names should be looked up.  If NULL_TREE, then
1397      we look up names in the scope that is currently open in the
1398      source program.  If non-NULL, this is either a TYPE or
1399      NAMESPACE_DECL for the scope in which we should look.  It can
1400      also be ERROR_MARK, when we've parsed a bogus scope.
1401
1402      This value is not cleared automatically after a name is looked
1403      up, so we must be careful to clear it before starting a new look
1404      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1405      will look up `Z' in the scope of `X', rather than the current
1406      scope.)  Unfortunately, it is difficult to tell when name lookup
1407      is complete, because we sometimes peek at a token, look it up,
1408      and then decide not to consume it.   */
1409   tree scope;
1410
1411   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1412      last lookup took place.  OBJECT_SCOPE is used if an expression
1413      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1414      respectively.  QUALIFYING_SCOPE is used for an expression of the
1415      form "X::Y"; it refers to X.  */
1416   tree object_scope;
1417   tree qualifying_scope;
1418
1419   /* A stack of parsing contexts.  All but the bottom entry on the
1420      stack will be tentative contexts.
1421
1422      We parse tentatively in order to determine which construct is in
1423      use in some situations.  For example, in order to determine
1424      whether a statement is an expression-statement or a
1425      declaration-statement we parse it tentatively as a
1426      declaration-statement.  If that fails, we then reparse the same
1427      token stream as an expression-statement.  */
1428   cp_parser_context *context;
1429
1430   /* True if we are parsing GNU C++.  If this flag is not set, then
1431      GNU extensions are not recognized.  */
1432   bool allow_gnu_extensions_p;
1433
1434   /* TRUE if the `>' token should be interpreted as the greater-than
1435      operator.  FALSE if it is the end of a template-id or
1436      template-parameter-list. In C++0x mode, this flag also applies to
1437      `>>' tokens, which are viewed as two consecutive `>' tokens when
1438      this flag is FALSE.  */
1439   bool greater_than_is_operator_p;
1440
1441   /* TRUE if default arguments are allowed within a parameter list
1442      that starts at this point. FALSE if only a gnu extension makes
1443      them permissible.  */
1444   bool default_arg_ok_p;
1445
1446   /* TRUE if we are parsing an integral constant-expression.  See
1447      [expr.const] for a precise definition.  */
1448   bool integral_constant_expression_p;
1449
1450   /* TRUE if we are parsing an integral constant-expression -- but a
1451      non-constant expression should be permitted as well.  This flag
1452      is used when parsing an array bound so that GNU variable-length
1453      arrays are tolerated.  */
1454   bool allow_non_integral_constant_expression_p;
1455
1456   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1457      been seen that makes the expression non-constant.  */
1458   bool non_integral_constant_expression_p;
1459
1460   /* TRUE if local variable names and `this' are forbidden in the
1461      current context.  */
1462   bool local_variables_forbidden_p;
1463
1464   /* TRUE if the declaration we are parsing is part of a
1465      linkage-specification of the form `extern string-literal
1466      declaration'.  */
1467   bool in_unbraced_linkage_specification_p;
1468
1469   /* TRUE if we are presently parsing a declarator, after the
1470      direct-declarator.  */
1471   bool in_declarator_p;
1472
1473   /* TRUE if we are presently parsing a template-argument-list.  */
1474   bool in_template_argument_list_p;
1475
1476   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1477      to IN_OMP_BLOCK if parsing OpenMP structured block and
1478      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1479      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1480      iteration-statement, OpenMP block or loop within that switch.  */
1481 #define IN_SWITCH_STMT          1
1482 #define IN_ITERATION_STMT       2
1483 #define IN_OMP_BLOCK            4
1484 #define IN_OMP_FOR              8
1485 #define IN_IF_STMT             16
1486   unsigned char in_statement;
1487
1488   /* TRUE if we are presently parsing the body of a switch statement.
1489      Note that this doesn't quite overlap with in_statement above.
1490      The difference relates to giving the right sets of error messages:
1491      "case not in switch" vs "break statement used with OpenMP...".  */
1492   bool in_switch_statement_p;
1493
1494   /* TRUE if we are parsing a type-id in an expression context.  In
1495      such a situation, both "type (expr)" and "type (type)" are valid
1496      alternatives.  */
1497   bool in_type_id_in_expr_p;
1498
1499   /* TRUE if we are currently in a header file where declarations are
1500      implicitly extern "C".  */
1501   bool implicit_extern_c;
1502
1503   /* TRUE if strings in expressions should be translated to the execution
1504      character set.  */
1505   bool translate_strings_p;
1506
1507   /* TRUE if we are presently parsing the body of a function, but not
1508      a local class.  */
1509   bool in_function_body;
1510
1511   /* If non-NULL, then we are parsing a construct where new type
1512      definitions are not permitted.  The string stored here will be
1513      issued as an error message if a type is defined.  */
1514   const char *type_definition_forbidden_message;
1515
1516   /* A list of lists. The outer list is a stack, used for member
1517      functions of local classes. At each level there are two sub-list,
1518      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1519      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1520      TREE_VALUE's. The functions are chained in reverse declaration
1521      order.
1522
1523      The TREE_PURPOSE sublist contains those functions with default
1524      arguments that need post processing, and the TREE_VALUE sublist
1525      contains those functions with definitions that need post
1526      processing.
1527
1528      These lists can only be processed once the outermost class being
1529      defined is complete.  */
1530   tree unparsed_functions_queues;
1531
1532   /* The number of classes whose definitions are currently in
1533      progress.  */
1534   unsigned num_classes_being_defined;
1535
1536   /* The number of template parameter lists that apply directly to the
1537      current declaration.  */
1538   unsigned num_template_parameter_lists;
1539 } cp_parser;
1540
1541 /* Prototypes.  */
1542
1543 /* Constructors and destructors.  */
1544
1545 static cp_parser *cp_parser_new
1546   (void);
1547
1548 /* Routines to parse various constructs.
1549
1550    Those that return `tree' will return the error_mark_node (rather
1551    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1552    Sometimes, they will return an ordinary node if error-recovery was
1553    attempted, even though a parse error occurred.  So, to check
1554    whether or not a parse error occurred, you should always use
1555    cp_parser_error_occurred.  If the construct is optional (indicated
1556    either by an `_opt' in the name of the function that does the
1557    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1558    the construct is not present.  */
1559
1560 /* Lexical conventions [gram.lex]  */
1561
1562 static tree cp_parser_identifier
1563   (cp_parser *);
1564 static tree cp_parser_string_literal
1565   (cp_parser *, bool, bool);
1566
1567 /* Basic concepts [gram.basic]  */
1568
1569 static bool cp_parser_translation_unit
1570   (cp_parser *);
1571
1572 /* Expressions [gram.expr]  */
1573
1574 static tree cp_parser_primary_expression
1575   (cp_parser *, bool, bool, bool, cp_id_kind *);
1576 static tree cp_parser_id_expression
1577   (cp_parser *, bool, bool, bool *, bool, bool);
1578 static tree cp_parser_unqualified_id
1579   (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier_opt
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_qualifying_entity
1585   (cp_parser *, bool, bool, bool, bool, bool);
1586 static tree cp_parser_postfix_expression
1587   (cp_parser *, bool, bool, bool, cp_id_kind *);
1588 static tree cp_parser_postfix_open_square_expression
1589   (cp_parser *, tree, bool);
1590 static tree cp_parser_postfix_dot_deref_expression
1591   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1592 static tree cp_parser_parenthesized_expression_list
1593   (cp_parser *, bool, bool, bool, bool *);
1594 static void cp_parser_pseudo_destructor_name
1595   (cp_parser *, tree *, tree *);
1596 static tree cp_parser_unary_expression
1597   (cp_parser *, bool, bool, cp_id_kind *);
1598 static enum tree_code cp_parser_unary_operator
1599   (cp_token *);
1600 static tree cp_parser_new_expression
1601   (cp_parser *);
1602 static tree cp_parser_new_placement
1603   (cp_parser *);
1604 static tree cp_parser_new_type_id
1605   (cp_parser *, tree *);
1606 static cp_declarator *cp_parser_new_declarator_opt
1607   (cp_parser *);
1608 static cp_declarator *cp_parser_direct_new_declarator
1609   (cp_parser *);
1610 static tree cp_parser_new_initializer
1611   (cp_parser *);
1612 static tree cp_parser_delete_expression
1613   (cp_parser *);
1614 static tree cp_parser_cast_expression
1615   (cp_parser *, bool, bool, cp_id_kind *);
1616 static tree cp_parser_binary_expression
1617   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1618 static tree cp_parser_question_colon_clause
1619   (cp_parser *, tree);
1620 static tree cp_parser_assignment_expression
1621   (cp_parser *, bool, cp_id_kind *);
1622 static enum tree_code cp_parser_assignment_operator_opt
1623   (cp_parser *);
1624 static tree cp_parser_expression
1625   (cp_parser *, bool, cp_id_kind *);
1626 static tree cp_parser_constant_expression
1627   (cp_parser *, bool, bool *);
1628 static tree cp_parser_builtin_offsetof
1629   (cp_parser *);
1630
1631 /* Statements [gram.stmt.stmt]  */
1632
1633 static void cp_parser_statement
1634   (cp_parser *, tree, bool, bool *);
1635 static void cp_parser_label_for_labeled_statement
1636   (cp_parser *);
1637 static tree cp_parser_expression_statement
1638   (cp_parser *, tree);
1639 static tree cp_parser_compound_statement
1640   (cp_parser *, tree, bool);
1641 static void cp_parser_statement_seq_opt
1642   (cp_parser *, tree);
1643 static tree cp_parser_selection_statement
1644   (cp_parser *, bool *);
1645 static tree cp_parser_condition
1646   (cp_parser *);
1647 static tree cp_parser_iteration_statement
1648   (cp_parser *);
1649 static void cp_parser_for_init_statement
1650   (cp_parser *);
1651 static tree cp_parser_jump_statement
1652   (cp_parser *);
1653 static void cp_parser_declaration_statement
1654   (cp_parser *);
1655
1656 static tree cp_parser_implicitly_scoped_statement
1657   (cp_parser *, bool *);
1658 static void cp_parser_already_scoped_statement
1659   (cp_parser *);
1660
1661 /* Declarations [gram.dcl.dcl] */
1662
1663 static void cp_parser_declaration_seq_opt
1664   (cp_parser *);
1665 static void cp_parser_declaration
1666   (cp_parser *);
1667 static void cp_parser_block_declaration
1668   (cp_parser *, bool);
1669 static void cp_parser_simple_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_decl_specifier_seq
1672   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1673 static tree cp_parser_storage_class_specifier_opt
1674   (cp_parser *);
1675 static tree cp_parser_function_specifier_opt
1676   (cp_parser *, cp_decl_specifier_seq *);
1677 static tree cp_parser_type_specifier
1678   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1679    int *, bool *);
1680 static tree cp_parser_simple_type_specifier
1681   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1682 static tree cp_parser_type_name
1683   (cp_parser *);
1684 static tree cp_parser_nonclass_name 
1685   (cp_parser* parser);
1686 static tree cp_parser_elaborated_type_specifier
1687   (cp_parser *, bool, bool);
1688 static tree cp_parser_enum_specifier
1689   (cp_parser *);
1690 static void cp_parser_enumerator_list
1691   (cp_parser *, tree);
1692 static void cp_parser_enumerator_definition
1693   (cp_parser *, tree);
1694 static tree cp_parser_namespace_name
1695   (cp_parser *);
1696 static void cp_parser_namespace_definition
1697   (cp_parser *);
1698 static void cp_parser_namespace_body
1699   (cp_parser *);
1700 static tree cp_parser_qualified_namespace_specifier
1701   (cp_parser *);
1702 static void cp_parser_namespace_alias_definition
1703   (cp_parser *);
1704 static bool cp_parser_using_declaration
1705   (cp_parser *, bool);
1706 static void cp_parser_using_directive
1707   (cp_parser *);
1708 static void cp_parser_asm_definition
1709   (cp_parser *);
1710 static void cp_parser_linkage_specification
1711   (cp_parser *);
1712 static void cp_parser_static_assert
1713   (cp_parser *, bool);
1714 static tree cp_parser_decltype
1715   (cp_parser *);
1716
1717 /* Declarators [gram.dcl.decl] */
1718
1719 static tree cp_parser_init_declarator
1720   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1721 static cp_declarator *cp_parser_declarator
1722   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1723 static cp_declarator *cp_parser_direct_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1725 static enum tree_code cp_parser_ptr_operator
1726   (cp_parser *, tree *, cp_cv_quals *);
1727 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1728   (cp_parser *);
1729 static tree cp_parser_late_return_type_opt
1730   (cp_parser *);
1731 static tree cp_parser_declarator_id
1732   (cp_parser *, bool);
1733 static tree cp_parser_type_id
1734   (cp_parser *);
1735 static void cp_parser_type_specifier_seq
1736   (cp_parser *, bool, cp_decl_specifier_seq *);
1737 static tree cp_parser_parameter_declaration_clause
1738   (cp_parser *);
1739 static tree cp_parser_parameter_declaration_list
1740   (cp_parser *, bool *);
1741 static cp_parameter_declarator *cp_parser_parameter_declaration
1742   (cp_parser *, bool, bool *);
1743 static tree cp_parser_default_argument 
1744   (cp_parser *, bool);
1745 static void cp_parser_function_body
1746   (cp_parser *);
1747 static tree cp_parser_initializer
1748   (cp_parser *, bool *, bool *);
1749 static tree cp_parser_initializer_clause
1750   (cp_parser *, bool *);
1751 static tree cp_parser_braced_list
1752   (cp_parser*, bool*);
1753 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1754   (cp_parser *, bool *);
1755
1756 static bool cp_parser_ctor_initializer_opt_and_function_body
1757   (cp_parser *);
1758
1759 /* Classes [gram.class] */
1760
1761 static tree cp_parser_class_name
1762   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1763 static tree cp_parser_class_specifier
1764   (cp_parser *);
1765 static tree cp_parser_class_head
1766   (cp_parser *, bool *, tree *, tree *);
1767 static enum tag_types cp_parser_class_key
1768   (cp_parser *);
1769 static void cp_parser_member_specification_opt
1770   (cp_parser *);
1771 static void cp_parser_member_declaration
1772   (cp_parser *);
1773 static tree cp_parser_pure_specifier
1774   (cp_parser *);
1775 static tree cp_parser_constant_initializer
1776   (cp_parser *);
1777
1778 /* Derived classes [gram.class.derived] */
1779
1780 static tree cp_parser_base_clause
1781   (cp_parser *);
1782 static tree cp_parser_base_specifier
1783   (cp_parser *);
1784
1785 /* Special member functions [gram.special] */
1786
1787 static tree cp_parser_conversion_function_id
1788   (cp_parser *);
1789 static tree cp_parser_conversion_type_id
1790   (cp_parser *);
1791 static cp_declarator *cp_parser_conversion_declarator_opt
1792   (cp_parser *);
1793 static bool cp_parser_ctor_initializer_opt
1794   (cp_parser *);
1795 static void cp_parser_mem_initializer_list
1796   (cp_parser *);
1797 static tree cp_parser_mem_initializer
1798   (cp_parser *);
1799 static tree cp_parser_mem_initializer_id
1800   (cp_parser *);
1801
1802 /* Overloading [gram.over] */
1803
1804 static tree cp_parser_operator_function_id
1805   (cp_parser *);
1806 static tree cp_parser_operator
1807   (cp_parser *);
1808
1809 /* Templates [gram.temp] */
1810
1811 static void cp_parser_template_declaration
1812   (cp_parser *, bool);
1813 static tree cp_parser_template_parameter_list
1814   (cp_parser *);
1815 static tree cp_parser_template_parameter
1816   (cp_parser *, bool *, bool *);
1817 static tree cp_parser_type_parameter
1818   (cp_parser *, bool *);
1819 static tree cp_parser_template_id
1820   (cp_parser *, bool, bool, bool);
1821 static tree cp_parser_template_name
1822   (cp_parser *, bool, bool, bool, bool *);
1823 static tree cp_parser_template_argument_list
1824   (cp_parser *);
1825 static tree cp_parser_template_argument
1826   (cp_parser *);
1827 static void cp_parser_explicit_instantiation
1828   (cp_parser *);
1829 static void cp_parser_explicit_specialization
1830   (cp_parser *);
1831
1832 /* Exception handling [gram.exception] */
1833
1834 static tree cp_parser_try_block
1835   (cp_parser *);
1836 static bool cp_parser_function_try_block
1837   (cp_parser *);
1838 static void cp_parser_handler_seq
1839   (cp_parser *);
1840 static void cp_parser_handler
1841   (cp_parser *);
1842 static tree cp_parser_exception_declaration
1843   (cp_parser *);
1844 static tree cp_parser_throw_expression
1845   (cp_parser *);
1846 static tree cp_parser_exception_specification_opt
1847   (cp_parser *);
1848 static tree cp_parser_type_id_list
1849   (cp_parser *);
1850
1851 /* GNU Extensions */
1852
1853 static tree cp_parser_asm_specification_opt
1854   (cp_parser *);
1855 static tree cp_parser_asm_operand_list
1856   (cp_parser *);
1857 static tree cp_parser_asm_clobber_list
1858   (cp_parser *);
1859 static tree cp_parser_attributes_opt
1860   (cp_parser *);
1861 static tree cp_parser_attribute_list
1862   (cp_parser *);
1863 static bool cp_parser_extension_opt
1864   (cp_parser *, int *);
1865 static void cp_parser_label_declaration
1866   (cp_parser *);
1867
1868 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1869 static bool cp_parser_pragma
1870   (cp_parser *, enum pragma_context);
1871
1872 /* Objective-C++ Productions */
1873
1874 static tree cp_parser_objc_message_receiver
1875   (cp_parser *);
1876 static tree cp_parser_objc_message_args
1877   (cp_parser *);
1878 static tree cp_parser_objc_message_expression
1879   (cp_parser *);
1880 static tree cp_parser_objc_encode_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_defs_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_protocol_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_selector_expression
1887   (cp_parser *);
1888 static tree cp_parser_objc_expression
1889   (cp_parser *);
1890 static bool cp_parser_objc_selector_p
1891   (enum cpp_ttype);
1892 static tree cp_parser_objc_selector
1893   (cp_parser *);
1894 static tree cp_parser_objc_protocol_refs_opt
1895   (cp_parser *);
1896 static void cp_parser_objc_declaration
1897   (cp_parser *);
1898 static tree cp_parser_objc_statement
1899   (cp_parser *);
1900
1901 /* Utility Routines */
1902
1903 static tree cp_parser_lookup_name
1904   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1905 static tree cp_parser_lookup_name_simple
1906   (cp_parser *, tree, location_t);
1907 static tree cp_parser_maybe_treat_template_as_class
1908   (tree, bool);
1909 static bool cp_parser_check_declarator_template_parameters
1910   (cp_parser *, cp_declarator *, location_t);
1911 static bool cp_parser_check_template_parameters
1912   (cp_parser *, unsigned, location_t);
1913 static tree cp_parser_simple_cast_expression
1914   (cp_parser *);
1915 static tree cp_parser_global_scope_opt
1916   (cp_parser *, bool);
1917 static bool cp_parser_constructor_declarator_p
1918   (cp_parser *, bool);
1919 static tree cp_parser_function_definition_from_specifiers_and_declarator
1920   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1921 static tree cp_parser_function_definition_after_declarator
1922   (cp_parser *, bool);
1923 static void cp_parser_template_declaration_after_export
1924   (cp_parser *, bool);
1925 static void cp_parser_perform_template_parameter_access_checks
1926   (VEC (deferred_access_check,gc)*);
1927 static tree cp_parser_single_declaration
1928   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1929 static tree cp_parser_functional_cast
1930   (cp_parser *, tree);
1931 static tree cp_parser_save_member_function_body
1932   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1933 static tree cp_parser_enclosed_template_argument_list
1934   (cp_parser *);
1935 static void cp_parser_save_default_args
1936   (cp_parser *, tree);
1937 static void cp_parser_late_parsing_for_member
1938   (cp_parser *, tree);
1939 static void cp_parser_late_parsing_default_args
1940   (cp_parser *, tree);
1941 static tree cp_parser_sizeof_operand
1942   (cp_parser *, enum rid);
1943 static tree cp_parser_trait_expr
1944   (cp_parser *, enum rid);
1945 static bool cp_parser_declares_only_class_p
1946   (cp_parser *);
1947 static void cp_parser_set_storage_class
1948   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1949 static void cp_parser_set_decl_spec_type
1950   (cp_decl_specifier_seq *, tree, location_t, bool);
1951 static bool cp_parser_friend_p
1952   (const cp_decl_specifier_seq *);
1953 static cp_token *cp_parser_require
1954   (cp_parser *, enum cpp_ttype, const char *);
1955 static cp_token *cp_parser_require_keyword
1956   (cp_parser *, enum rid, const char *);
1957 static bool cp_parser_token_starts_function_definition_p
1958   (cp_token *);
1959 static bool cp_parser_next_token_starts_class_definition_p
1960   (cp_parser *);
1961 static bool cp_parser_next_token_ends_template_argument_p
1962   (cp_parser *);
1963 static bool cp_parser_nth_token_starts_template_argument_list_p
1964   (cp_parser *, size_t);
1965 static enum tag_types cp_parser_token_is_class_key
1966   (cp_token *);
1967 static void cp_parser_check_class_key
1968   (enum tag_types, tree type);
1969 static void cp_parser_check_access_in_redeclaration
1970   (tree type, location_t location);
1971 static bool cp_parser_optional_template_keyword
1972   (cp_parser *);
1973 static void cp_parser_pre_parsed_nested_name_specifier
1974   (cp_parser *);
1975 static bool cp_parser_cache_group
1976   (cp_parser *, enum cpp_ttype, unsigned);
1977 static void cp_parser_parse_tentatively
1978   (cp_parser *);
1979 static void cp_parser_commit_to_tentative_parse
1980   (cp_parser *);
1981 static void cp_parser_abort_tentative_parse
1982   (cp_parser *);
1983 static bool cp_parser_parse_definitely
1984   (cp_parser *);
1985 static inline bool cp_parser_parsing_tentatively
1986   (cp_parser *);
1987 static bool cp_parser_uncommitted_to_tentative_parse_p
1988   (cp_parser *);
1989 static void cp_parser_error
1990   (cp_parser *, const char *);
1991 static void cp_parser_name_lookup_error
1992   (cp_parser *, tree, tree, const char *, location_t);
1993 static bool cp_parser_simulate_error
1994   (cp_parser *);
1995 static bool cp_parser_check_type_definition
1996   (cp_parser *);
1997 static void cp_parser_check_for_definition_in_return_type
1998   (cp_declarator *, tree, location_t type_location);
1999 static void cp_parser_check_for_invalid_template_id
2000   (cp_parser *, tree, location_t location);
2001 static bool cp_parser_non_integral_constant_expression
2002   (cp_parser *, const char *);
2003 static void cp_parser_diagnose_invalid_type_name
2004   (cp_parser *, tree, tree, location_t);
2005 static bool cp_parser_parse_and_diagnose_invalid_type_name
2006   (cp_parser *);
2007 static int cp_parser_skip_to_closing_parenthesis
2008   (cp_parser *, bool, bool, bool);
2009 static void cp_parser_skip_to_end_of_statement
2010   (cp_parser *);
2011 static void cp_parser_consume_semicolon_at_end_of_statement
2012   (cp_parser *);
2013 static void cp_parser_skip_to_end_of_block_or_statement
2014   (cp_parser *);
2015 static bool cp_parser_skip_to_closing_brace
2016   (cp_parser *);
2017 static void cp_parser_skip_to_end_of_template_parameter_list
2018   (cp_parser *);
2019 static void cp_parser_skip_to_pragma_eol
2020   (cp_parser*, cp_token *);
2021 static bool cp_parser_error_occurred
2022   (cp_parser *);
2023 static bool cp_parser_allow_gnu_extensions_p
2024   (cp_parser *);
2025 static bool cp_parser_is_string_literal
2026   (cp_token *);
2027 static bool cp_parser_is_keyword
2028   (cp_token *, enum rid);
2029 static tree cp_parser_make_typename_type
2030   (cp_parser *, tree, tree, location_t location);
2031 static cp_declarator * cp_parser_make_indirect_declarator
2032   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2033
2034 /* Returns nonzero if we are parsing tentatively.  */
2035
2036 static inline bool
2037 cp_parser_parsing_tentatively (cp_parser* parser)
2038 {
2039   return parser->context->next != NULL;
2040 }
2041
2042 /* Returns nonzero if TOKEN is a string literal.  */
2043
2044 static bool
2045 cp_parser_is_string_literal (cp_token* token)
2046 {
2047   return (token->type == CPP_STRING ||
2048           token->type == CPP_STRING16 ||
2049           token->type == CPP_STRING32 ||
2050           token->type == CPP_WSTRING);
2051 }
2052
2053 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2054
2055 static bool
2056 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2057 {
2058   return token->keyword == keyword;
2059 }
2060
2061 /* If not parsing tentatively, issue a diagnostic of the form
2062       FILE:LINE: MESSAGE before TOKEN
2063    where TOKEN is the next token in the input stream.  MESSAGE
2064    (specified by the caller) is usually of the form "expected
2065    OTHER-TOKEN".  */
2066
2067 static void
2068 cp_parser_error (cp_parser* parser, const char* message)
2069 {
2070   if (!cp_parser_simulate_error (parser))
2071     {
2072       cp_token *token = cp_lexer_peek_token (parser->lexer);
2073       /* This diagnostic makes more sense if it is tagged to the line
2074          of the token we just peeked at.  */
2075       cp_lexer_set_source_position_from_token (token);
2076
2077       if (token->type == CPP_PRAGMA)
2078         {
2079           error ("%H%<#pragma%> is not allowed here", &token->location);
2080           cp_parser_skip_to_pragma_eol (parser, token);
2081           return;
2082         }
2083
2084       c_parse_error (message,
2085                      /* Because c_parser_error does not understand
2086                         CPP_KEYWORD, keywords are treated like
2087                         identifiers.  */
2088                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2089                      token->u.value);
2090     }
2091 }
2092
2093 /* Issue an error about name-lookup failing.  NAME is the
2094    IDENTIFIER_NODE DECL is the result of
2095    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2096    the thing that we hoped to find.  */
2097
2098 static void
2099 cp_parser_name_lookup_error (cp_parser* parser,
2100                              tree name,
2101                              tree decl,
2102                              const char* desired,
2103                              location_t location)
2104 {
2105   /* If name lookup completely failed, tell the user that NAME was not
2106      declared.  */
2107   if (decl == error_mark_node)
2108     {
2109       if (parser->scope && parser->scope != global_namespace)
2110         error ("%H%<%E::%E%> has not been declared",
2111                &location, parser->scope, name);
2112       else if (parser->scope == global_namespace)
2113         error ("%H%<::%E%> has not been declared", &location, name);
2114       else if (parser->object_scope
2115                && !CLASS_TYPE_P (parser->object_scope))
2116         error ("%Hrequest for member %qE in non-class type %qT",
2117                &location, name, parser->object_scope);
2118       else if (parser->object_scope)
2119         error ("%H%<%T::%E%> has not been declared",
2120                &location, parser->object_scope, name);
2121       else
2122         error ("%H%qE has not been declared", &location, name);
2123     }
2124   else if (parser->scope && parser->scope != global_namespace)
2125     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2126   else if (parser->scope == global_namespace)
2127     error ("%H%<::%E%> %s", &location, name, desired);
2128   else
2129     error ("%H%qE %s", &location, name, desired);
2130 }
2131
2132 /* If we are parsing tentatively, remember that an error has occurred
2133    during this tentative parse.  Returns true if the error was
2134    simulated; false if a message should be issued by the caller.  */
2135
2136 static bool
2137 cp_parser_simulate_error (cp_parser* parser)
2138 {
2139   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2140     {
2141       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2142       return true;
2143     }
2144   return false;
2145 }
2146
2147 /* Check for repeated decl-specifiers.  */
2148
2149 static void
2150 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2151                            location_t location)
2152 {
2153   cp_decl_spec ds;
2154
2155   for (ds = ds_first; ds != ds_last; ++ds)
2156     {
2157       unsigned count = decl_specs->specs[(int)ds];
2158       if (count < 2)
2159         continue;
2160       /* The "long" specifier is a special case because of "long long".  */
2161       if (ds == ds_long)
2162         {
2163           if (count > 2)
2164             error ("%H%<long long long%> is too long for GCC", &location);
2165           else if (pedantic && !in_system_header && warn_long_long
2166                    && cxx_dialect == cxx98)
2167             pedwarn (location, OPT_Wlong_long, 
2168                      "ISO C++ 1998 does not support %<long long%>");
2169         }
2170       else if (count > 1)
2171         {
2172           static const char *const decl_spec_names[] = {
2173             "signed",
2174             "unsigned",
2175             "short",
2176             "long",
2177             "const",
2178             "volatile",
2179             "restrict",
2180             "inline",
2181             "virtual",
2182             "explicit",
2183             "friend",
2184             "typedef",
2185             "__complex",
2186             "__thread"
2187           };
2188           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2189         }
2190     }
2191 }
2192
2193 /* This function is called when a type is defined.  If type
2194    definitions are forbidden at this point, an error message is
2195    issued.  */
2196
2197 static bool
2198 cp_parser_check_type_definition (cp_parser* parser)
2199 {
2200   /* If types are forbidden here, issue a message.  */
2201   if (parser->type_definition_forbidden_message)
2202     {
2203       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2204          in the message need to be interpreted.  */
2205       error (parser->type_definition_forbidden_message);
2206       return false;
2207     }
2208   return true;
2209 }
2210
2211 /* This function is called when the DECLARATOR is processed.  The TYPE
2212    was a type defined in the decl-specifiers.  If it is invalid to
2213    define a type in the decl-specifiers for DECLARATOR, an error is
2214    issued. TYPE_LOCATION is the location of TYPE and is used
2215    for error reporting.  */
2216
2217 static void
2218 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2219                                                tree type, location_t type_location)
2220 {
2221   /* [dcl.fct] forbids type definitions in return types.
2222      Unfortunately, it's not easy to know whether or not we are
2223      processing a return type until after the fact.  */
2224   while (declarator
2225          && (declarator->kind == cdk_pointer
2226              || declarator->kind == cdk_reference
2227              || declarator->kind == cdk_ptrmem))
2228     declarator = declarator->declarator;
2229   if (declarator
2230       && declarator->kind == cdk_function)
2231     {
2232       error ("%Hnew types may not be defined in a return type", &type_location);
2233       inform (type_location, 
2234               "(perhaps a semicolon is missing after the definition of %qT)",
2235               type);
2236     }
2237 }
2238
2239 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2240    "<" in any valid C++ program.  If the next token is indeed "<",
2241    issue a message warning the user about what appears to be an
2242    invalid attempt to form a template-id. LOCATION is the location
2243    of the type-specifier (TYPE) */
2244
2245 static void
2246 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2247                                          tree type, location_t location)
2248 {
2249   cp_token_position start = 0;
2250
2251   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2252     {
2253       if (TYPE_P (type))
2254         error ("%H%qT is not a template", &location, type);
2255       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2256         error ("%H%qE is not a template", &location, type);
2257       else
2258         error ("%Hinvalid template-id", &location);
2259       /* Remember the location of the invalid "<".  */
2260       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2261         start = cp_lexer_token_position (parser->lexer, true);
2262       /* Consume the "<".  */
2263       cp_lexer_consume_token (parser->lexer);
2264       /* Parse the template arguments.  */
2265       cp_parser_enclosed_template_argument_list (parser);
2266       /* Permanently remove the invalid template arguments so that
2267          this error message is not issued again.  */
2268       if (start)
2269         cp_lexer_purge_tokens_after (parser->lexer, start);
2270     }
2271 }
2272
2273 /* If parsing an integral constant-expression, issue an error message
2274    about the fact that THING appeared and return true.  Otherwise,
2275    return false.  In either case, set
2276    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2277
2278 static bool
2279 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2280                                             const char *thing)
2281 {
2282   parser->non_integral_constant_expression_p = true;
2283   if (parser->integral_constant_expression_p)
2284     {
2285       if (!parser->allow_non_integral_constant_expression_p)
2286         {
2287           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2288              in the message need to be interpreted.  */
2289           char *message = concat (thing,
2290                                   " cannot appear in a constant-expression",
2291                                   NULL);
2292           error (message);
2293           free (message);
2294           return true;
2295         }
2296     }
2297   return false;
2298 }
2299
2300 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2301    qualifying scope (or NULL, if none) for ID.  This function commits
2302    to the current active tentative parse, if any.  (Otherwise, the
2303    problematic construct might be encountered again later, resulting
2304    in duplicate error messages.) LOCATION is the location of ID.  */
2305
2306 static void
2307 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2308                                       tree scope, tree id,
2309                                       location_t location)
2310 {
2311   tree decl, old_scope;
2312   /* Try to lookup the identifier.  */
2313   old_scope = parser->scope;
2314   parser->scope = scope;
2315   decl = cp_parser_lookup_name_simple (parser, id, location);
2316   parser->scope = old_scope;
2317   /* If the lookup found a template-name, it means that the user forgot
2318   to specify an argument list. Emit a useful error message.  */
2319   if (TREE_CODE (decl) == TEMPLATE_DECL)
2320     error ("%Hinvalid use of template-name %qE without an argument list",
2321            &location, decl);
2322   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2323     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2324   else if (TREE_CODE (decl) == TYPE_DECL)
2325     /* Something like 'unsigned A a;'  */
2326     error ("%Hinvalid combination of multiple type-specifiers",
2327            &location);
2328   else if (!parser->scope)
2329     {
2330       /* Issue an error message.  */
2331       error ("%H%qE does not name a type", &location, id);
2332       /* If we're in a template class, it's possible that the user was
2333          referring to a type from a base class.  For example:
2334
2335            template <typename T> struct A { typedef T X; };
2336            template <typename T> struct B : public A<T> { X x; };
2337
2338          The user should have said "typename A<T>::X".  */
2339       if (processing_template_decl && current_class_type
2340           && TYPE_BINFO (current_class_type))
2341         {
2342           tree b;
2343
2344           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2345                b;
2346                b = TREE_CHAIN (b))
2347             {
2348               tree base_type = BINFO_TYPE (b);
2349               if (CLASS_TYPE_P (base_type)
2350                   && dependent_type_p (base_type))
2351                 {
2352                   tree field;
2353                   /* Go from a particular instantiation of the
2354                      template (which will have an empty TYPE_FIELDs),
2355                      to the main version.  */
2356                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2357                   for (field = TYPE_FIELDS (base_type);
2358                        field;
2359                        field = TREE_CHAIN (field))
2360                     if (TREE_CODE (field) == TYPE_DECL
2361                         && DECL_NAME (field) == id)
2362                       {
2363                         inform (location, 
2364                                 "(perhaps %<typename %T::%E%> was intended)",
2365                                 BINFO_TYPE (b), id);
2366                         break;
2367                       }
2368                   if (field)
2369                     break;
2370                 }
2371             }
2372         }
2373     }
2374   /* Here we diagnose qualified-ids where the scope is actually correct,
2375      but the identifier does not resolve to a valid type name.  */
2376   else if (parser->scope != error_mark_node)
2377     {
2378       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2379         error ("%H%qE in namespace %qE does not name a type",
2380                &location, id, parser->scope);
2381       else if (TYPE_P (parser->scope))
2382         error ("%H%qE in class %qT does not name a type",
2383                &location, id, parser->scope);
2384       else
2385         gcc_unreachable ();
2386     }
2387   cp_parser_commit_to_tentative_parse (parser);
2388 }
2389
2390 /* Check for a common situation where a type-name should be present,
2391    but is not, and issue a sensible error message.  Returns true if an
2392    invalid type-name was detected.
2393
2394    The situation handled by this function are variable declarations of the
2395    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2396    Usually, `ID' should name a type, but if we got here it means that it
2397    does not. We try to emit the best possible error message depending on
2398    how exactly the id-expression looks like.  */
2399
2400 static bool
2401 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2402 {
2403   tree id;
2404   cp_token *token = cp_lexer_peek_token (parser->lexer);
2405
2406   cp_parser_parse_tentatively (parser);
2407   id = cp_parser_id_expression (parser,
2408                                 /*template_keyword_p=*/false,
2409                                 /*check_dependency_p=*/true,
2410                                 /*template_p=*/NULL,
2411                                 /*declarator_p=*/true,
2412                                 /*optional_p=*/false);
2413   /* After the id-expression, there should be a plain identifier,
2414      otherwise this is not a simple variable declaration. Also, if
2415      the scope is dependent, we cannot do much.  */
2416   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2417       || (parser->scope && TYPE_P (parser->scope)
2418           && dependent_type_p (parser->scope))
2419       || TREE_CODE (id) == TYPE_DECL)
2420     {
2421       cp_parser_abort_tentative_parse (parser);
2422       return false;
2423     }
2424   if (!cp_parser_parse_definitely (parser))
2425     return false;
2426
2427   /* Emit a diagnostic for the invalid type.  */
2428   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2429                                         id, token->location);
2430   /* Skip to the end of the declaration; there's no point in
2431      trying to process it.  */
2432   cp_parser_skip_to_end_of_block_or_statement (parser);
2433   return true;
2434 }
2435
2436 /* Consume tokens up to, and including, the next non-nested closing `)'.
2437    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2438    are doing error recovery. Returns -1 if OR_COMMA is true and we
2439    found an unnested comma.  */
2440
2441 static int
2442 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2443                                        bool recovering,
2444                                        bool or_comma,
2445                                        bool consume_paren)
2446 {
2447   unsigned paren_depth = 0;
2448   unsigned brace_depth = 0;
2449
2450   if (recovering && !or_comma
2451       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2452     return 0;
2453
2454   while (true)
2455     {
2456       cp_token * token = cp_lexer_peek_token (parser->lexer);
2457
2458       switch (token->type)
2459         {
2460         case CPP_EOF:
2461         case CPP_PRAGMA_EOL:
2462           /* If we've run out of tokens, then there is no closing `)'.  */
2463           return 0;
2464
2465         case CPP_SEMICOLON:
2466           /* This matches the processing in skip_to_end_of_statement.  */
2467           if (!brace_depth)
2468             return 0;
2469           break;
2470
2471         case CPP_OPEN_BRACE:
2472           ++brace_depth;
2473           break;
2474         case CPP_CLOSE_BRACE:
2475           if (!brace_depth--)
2476             return 0;
2477           break;
2478
2479         case CPP_COMMA:
2480           if (recovering && or_comma && !brace_depth && !paren_depth)
2481             return -1;
2482           break;
2483
2484         case CPP_OPEN_PAREN:
2485           if (!brace_depth)
2486             ++paren_depth;
2487           break;
2488
2489         case CPP_CLOSE_PAREN:
2490           if (!brace_depth && !paren_depth--)
2491             {
2492               if (consume_paren)
2493                 cp_lexer_consume_token (parser->lexer);
2494               return 1;
2495             }
2496           break;
2497
2498         default:
2499           break;
2500         }
2501
2502       /* Consume the token.  */
2503       cp_lexer_consume_token (parser->lexer);
2504     }
2505 }
2506
2507 /* Consume tokens until we reach the end of the current statement.
2508    Normally, that will be just before consuming a `;'.  However, if a
2509    non-nested `}' comes first, then we stop before consuming that.  */
2510
2511 static void
2512 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2513 {
2514   unsigned nesting_depth = 0;
2515
2516   while (true)
2517     {
2518       cp_token *token = cp_lexer_peek_token (parser->lexer);
2519
2520       switch (token->type)
2521         {
2522         case CPP_EOF:
2523         case CPP_PRAGMA_EOL:
2524           /* If we've run out of tokens, stop.  */
2525           return;
2526
2527         case CPP_SEMICOLON:
2528           /* If the next token is a `;', we have reached the end of the
2529              statement.  */
2530           if (!nesting_depth)
2531             return;
2532           break;
2533
2534         case CPP_CLOSE_BRACE:
2535           /* If this is a non-nested '}', stop before consuming it.
2536              That way, when confronted with something like:
2537
2538                { 3 + }
2539
2540              we stop before consuming the closing '}', even though we
2541              have not yet reached a `;'.  */
2542           if (nesting_depth == 0)
2543             return;
2544
2545           /* If it is the closing '}' for a block that we have
2546              scanned, stop -- but only after consuming the token.
2547              That way given:
2548
2549                 void f g () { ... }
2550                 typedef int I;
2551
2552              we will stop after the body of the erroneously declared
2553              function, but before consuming the following `typedef'
2554              declaration.  */
2555           if (--nesting_depth == 0)
2556             {
2557               cp_lexer_consume_token (parser->lexer);
2558               return;
2559             }
2560
2561         case CPP_OPEN_BRACE:
2562           ++nesting_depth;
2563           break;
2564
2565         default:
2566           break;
2567         }
2568
2569       /* Consume the token.  */
2570       cp_lexer_consume_token (parser->lexer);
2571     }
2572 }
2573
2574 /* This function is called at the end of a statement or declaration.
2575    If the next token is a semicolon, it is consumed; otherwise, error
2576    recovery is attempted.  */
2577
2578 static void
2579 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2580 {
2581   /* Look for the trailing `;'.  */
2582   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2583     {
2584       /* If there is additional (erroneous) input, skip to the end of
2585          the statement.  */
2586       cp_parser_skip_to_end_of_statement (parser);
2587       /* If the next token is now a `;', consume it.  */
2588       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2589         cp_lexer_consume_token (parser->lexer);
2590     }
2591 }
2592
2593 /* Skip tokens until we have consumed an entire block, or until we
2594    have consumed a non-nested `;'.  */
2595
2596 static void
2597 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2598 {
2599   int nesting_depth = 0;
2600
2601   while (nesting_depth >= 0)
2602     {
2603       cp_token *token = cp_lexer_peek_token (parser->lexer);
2604
2605       switch (token->type)
2606         {
2607         case CPP_EOF:
2608         case CPP_PRAGMA_EOL:
2609           /* If we've run out of tokens, stop.  */
2610           return;
2611
2612         case CPP_SEMICOLON:
2613           /* Stop if this is an unnested ';'. */
2614           if (!nesting_depth)
2615             nesting_depth = -1;
2616           break;
2617
2618         case CPP_CLOSE_BRACE:
2619           /* Stop if this is an unnested '}', or closes the outermost
2620              nesting level.  */
2621           nesting_depth--;
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             tree 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 == error_mark_node)
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                 break;
4730               }
4731
4732             koenig_p = false;
4733             if (idk == CP_ID_KIND_UNQUALIFIED
4734                 || idk == CP_ID_KIND_TEMPLATE_ID)
4735               {
4736                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4737                   {
4738                     if (args)
4739                       {
4740                         koenig_p = true;
4741                         postfix_expression
4742                           = perform_koenig_lookup (postfix_expression, args);
4743                       }
4744                     else
4745                       postfix_expression
4746                         = unqualified_fn_lookup_error (postfix_expression);
4747                   }
4748                 /* We do not perform argument-dependent lookup if
4749                    normal lookup finds a non-function, in accordance
4750                    with the expected resolution of DR 218.  */
4751                 else if (args && is_overloaded_fn (postfix_expression))
4752                   {
4753                     tree fn = get_first_fn (postfix_expression);
4754
4755                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4756                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4757
4758                     /* Only do argument dependent lookup if regular
4759                        lookup does not find a set of member functions.
4760                        [basic.lookup.koenig]/2a  */
4761                     if (!DECL_FUNCTION_MEMBER_P (fn))
4762                       {
4763                         koenig_p = true;
4764                         postfix_expression
4765                           = perform_koenig_lookup (postfix_expression, args);
4766                       }
4767                   }
4768               }
4769
4770             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4771               {
4772                 tree instance = TREE_OPERAND (postfix_expression, 0);
4773                 tree fn = TREE_OPERAND (postfix_expression, 1);
4774
4775                 if (processing_template_decl
4776                     && (type_dependent_expression_p (instance)
4777                         || (!BASELINK_P (fn)
4778                             && TREE_CODE (fn) != FIELD_DECL)
4779                         || type_dependent_expression_p (fn)
4780                         || any_type_dependent_arguments_p (args)))
4781                   {
4782                     postfix_expression
4783                       = build_nt_call_list (postfix_expression, args);
4784                     break;
4785                   }
4786
4787                 if (BASELINK_P (fn))
4788                   {
4789                   postfix_expression
4790                     = (build_new_method_call
4791                        (instance, fn, args, NULL_TREE,
4792                         (idk == CP_ID_KIND_QUALIFIED
4793                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4794                         /*fn_p=*/NULL,
4795                         tf_warning_or_error));
4796                   }
4797                 else
4798                   postfix_expression
4799                     = finish_call_expr (postfix_expression, args,
4800                                         /*disallow_virtual=*/false,
4801                                         /*koenig_p=*/false,
4802                                         tf_warning_or_error);
4803               }
4804             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4805                      || TREE_CODE (postfix_expression) == MEMBER_REF
4806                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4807               postfix_expression = (build_offset_ref_call_from_tree
4808                                     (postfix_expression, args));
4809             else if (idk == CP_ID_KIND_QUALIFIED)
4810               /* A call to a static class member, or a namespace-scope
4811                  function.  */
4812               postfix_expression
4813                 = finish_call_expr (postfix_expression, args,
4814                                     /*disallow_virtual=*/true,
4815                                     koenig_p,
4816                                     tf_warning_or_error);
4817             else
4818               /* All other function calls.  */
4819               postfix_expression
4820                 = finish_call_expr (postfix_expression, args,
4821                                     /*disallow_virtual=*/false,
4822                                     koenig_p,
4823                                     tf_warning_or_error);
4824
4825             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4826             idk = CP_ID_KIND_NONE;
4827           }
4828           break;
4829
4830         case CPP_DOT:
4831         case CPP_DEREF:
4832           /* postfix-expression . template [opt] id-expression
4833              postfix-expression . pseudo-destructor-name
4834              postfix-expression -> template [opt] id-expression
4835              postfix-expression -> pseudo-destructor-name */
4836
4837           /* Consume the `.' or `->' operator.  */
4838           cp_lexer_consume_token (parser->lexer);
4839
4840           postfix_expression
4841             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4842                                                       postfix_expression,
4843                                                       false, &idk,
4844                                                       token->location);
4845
4846           is_member_access = true;
4847           break;
4848
4849         case CPP_PLUS_PLUS:
4850           /* postfix-expression ++  */
4851           /* Consume the `++' token.  */
4852           cp_lexer_consume_token (parser->lexer);
4853           /* Generate a representation for the complete expression.  */
4854           postfix_expression
4855             = finish_increment_expr (postfix_expression,
4856                                      POSTINCREMENT_EXPR);
4857           /* Increments may not appear in constant-expressions.  */
4858           if (cp_parser_non_integral_constant_expression (parser,
4859                                                           "an increment"))
4860             postfix_expression = error_mark_node;
4861           idk = CP_ID_KIND_NONE;
4862           is_member_access = false;
4863           break;
4864
4865         case CPP_MINUS_MINUS:
4866           /* postfix-expression -- */
4867           /* Consume the `--' token.  */
4868           cp_lexer_consume_token (parser->lexer);
4869           /* Generate a representation for the complete expression.  */
4870           postfix_expression
4871             = finish_increment_expr (postfix_expression,
4872                                      POSTDECREMENT_EXPR);
4873           /* Decrements may not appear in constant-expressions.  */
4874           if (cp_parser_non_integral_constant_expression (parser,
4875                                                           "a decrement"))
4876             postfix_expression = error_mark_node;
4877           idk = CP_ID_KIND_NONE;
4878           is_member_access = false;
4879           break;
4880
4881         default:
4882           if (pidk_return != NULL)
4883             * pidk_return = idk;
4884           if (member_access_only_p)
4885             return is_member_access? postfix_expression : error_mark_node;
4886           else
4887             return postfix_expression;
4888         }
4889     }
4890
4891   /* We should never get here.  */
4892   gcc_unreachable ();
4893   return error_mark_node;
4894 }
4895
4896 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4897    by cp_parser_builtin_offsetof.  We're looking for
4898
4899      postfix-expression [ expression ]
4900
4901    FOR_OFFSETOF is set if we're being called in that context, which
4902    changes how we deal with integer constant expressions.  */
4903
4904 static tree
4905 cp_parser_postfix_open_square_expression (cp_parser *parser,
4906                                           tree postfix_expression,
4907                                           bool for_offsetof)
4908 {
4909   tree index;
4910
4911   /* Consume the `[' token.  */
4912   cp_lexer_consume_token (parser->lexer);
4913
4914   /* Parse the index expression.  */
4915   /* ??? For offsetof, there is a question of what to allow here.  If
4916      offsetof is not being used in an integral constant expression context,
4917      then we *could* get the right answer by computing the value at runtime.
4918      If we are in an integral constant expression context, then we might
4919      could accept any constant expression; hard to say without analysis.
4920      Rather than open the barn door too wide right away, allow only integer
4921      constant expressions here.  */
4922   if (for_offsetof)
4923     index = cp_parser_constant_expression (parser, false, NULL);
4924   else
4925     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4926
4927   /* Look for the closing `]'.  */
4928   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4929
4930   /* Build the ARRAY_REF.  */
4931   postfix_expression = grok_array_decl (postfix_expression, index);
4932
4933   /* When not doing offsetof, array references are not permitted in
4934      constant-expressions.  */
4935   if (!for_offsetof
4936       && (cp_parser_non_integral_constant_expression
4937           (parser, "an array reference")))
4938     postfix_expression = error_mark_node;
4939
4940   return postfix_expression;
4941 }
4942
4943 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4944    by cp_parser_builtin_offsetof.  We're looking for
4945
4946      postfix-expression . template [opt] id-expression
4947      postfix-expression . pseudo-destructor-name
4948      postfix-expression -> template [opt] id-expression
4949      postfix-expression -> pseudo-destructor-name
4950
4951    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4952    limits what of the above we'll actually accept, but nevermind.
4953    TOKEN_TYPE is the "." or "->" token, which will already have been
4954    removed from the stream.  */
4955
4956 static tree
4957 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4958                                         enum cpp_ttype token_type,
4959                                         tree postfix_expression,
4960                                         bool for_offsetof, cp_id_kind *idk,
4961                                         location_t location)
4962 {
4963   tree name;
4964   bool dependent_p;
4965   bool pseudo_destructor_p;
4966   tree scope = NULL_TREE;
4967
4968   /* If this is a `->' operator, dereference the pointer.  */
4969   if (token_type == CPP_DEREF)
4970     postfix_expression = build_x_arrow (postfix_expression);
4971   /* Check to see whether or not the expression is type-dependent.  */
4972   dependent_p = type_dependent_expression_p (postfix_expression);
4973   /* The identifier following the `->' or `.' is not qualified.  */
4974   parser->scope = NULL_TREE;
4975   parser->qualifying_scope = NULL_TREE;
4976   parser->object_scope = NULL_TREE;
4977   *idk = CP_ID_KIND_NONE;
4978
4979   /* Enter the scope corresponding to the type of the object
4980      given by the POSTFIX_EXPRESSION.  */
4981   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4982     {
4983       scope = TREE_TYPE (postfix_expression);
4984       /* According to the standard, no expression should ever have
4985          reference type.  Unfortunately, we do not currently match
4986          the standard in this respect in that our internal representation
4987          of an expression may have reference type even when the standard
4988          says it does not.  Therefore, we have to manually obtain the
4989          underlying type here.  */
4990       scope = non_reference (scope);
4991       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4992       if (scope == unknown_type_node)
4993         {
4994           error ("%H%qE does not have class type", &location, postfix_expression);
4995           scope = NULL_TREE;
4996         }
4997       else
4998         scope = complete_type_or_else (scope, NULL_TREE);
4999       /* Let the name lookup machinery know that we are processing a
5000          class member access expression.  */
5001       parser->context->object_type = scope;
5002       /* If something went wrong, we want to be able to discern that case,
5003          as opposed to the case where there was no SCOPE due to the type
5004          of expression being dependent.  */
5005       if (!scope)
5006         scope = error_mark_node;
5007       /* If the SCOPE was erroneous, make the various semantic analysis
5008          functions exit quickly -- and without issuing additional error
5009          messages.  */
5010       if (scope == error_mark_node)
5011         postfix_expression = error_mark_node;
5012     }
5013
5014   /* Assume this expression is not a pseudo-destructor access.  */
5015   pseudo_destructor_p = false;
5016
5017   /* If the SCOPE is a scalar type, then, if this is a valid program,
5018      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5019      is type dependent, it can be pseudo-destructor-name or something else.
5020      Try to parse it as pseudo-destructor-name first.  */
5021   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5022     {
5023       tree s;
5024       tree type;
5025
5026       cp_parser_parse_tentatively (parser);
5027       /* Parse the pseudo-destructor-name.  */
5028       s = NULL_TREE;
5029       cp_parser_pseudo_destructor_name (parser, &s, &type);
5030       if (dependent_p
5031           && (cp_parser_error_occurred (parser)
5032               || TREE_CODE (type) != TYPE_DECL
5033               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5034         cp_parser_abort_tentative_parse (parser);
5035       else if (cp_parser_parse_definitely (parser))
5036         {
5037           pseudo_destructor_p = true;
5038           postfix_expression
5039             = finish_pseudo_destructor_expr (postfix_expression,
5040                                              s, TREE_TYPE (type));
5041         }
5042     }
5043
5044   if (!pseudo_destructor_p)
5045     {
5046       /* If the SCOPE is not a scalar type, we are looking at an
5047          ordinary class member access expression, rather than a
5048          pseudo-destructor-name.  */
5049       bool template_p;
5050       cp_token *token = cp_lexer_peek_token (parser->lexer);
5051       /* Parse the id-expression.  */
5052       name = (cp_parser_id_expression
5053               (parser,
5054                cp_parser_optional_template_keyword (parser),
5055                /*check_dependency_p=*/true,
5056                &template_p,
5057                /*declarator_p=*/false,
5058                /*optional_p=*/false));
5059       /* In general, build a SCOPE_REF if the member name is qualified.
5060          However, if the name was not dependent and has already been
5061          resolved; there is no need to build the SCOPE_REF.  For example;
5062
5063              struct X { void f(); };
5064              template <typename T> void f(T* t) { t->X::f(); }
5065
5066          Even though "t" is dependent, "X::f" is not and has been resolved
5067          to a BASELINK; there is no need to include scope information.  */
5068
5069       /* But we do need to remember that there was an explicit scope for
5070          virtual function calls.  */
5071       if (parser->scope)
5072         *idk = CP_ID_KIND_QUALIFIED;
5073
5074       /* If the name is a template-id that names a type, we will get a
5075          TYPE_DECL here.  That is invalid code.  */
5076       if (TREE_CODE (name) == TYPE_DECL)
5077         {
5078           error ("%Hinvalid use of %qD", &token->location, name);
5079           postfix_expression = error_mark_node;
5080         }
5081       else
5082         {
5083           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5084             {
5085               name = build_qualified_name (/*type=*/NULL_TREE,
5086                                            parser->scope,
5087                                            name,
5088                                            template_p);
5089               parser->scope = NULL_TREE;
5090               parser->qualifying_scope = NULL_TREE;
5091               parser->object_scope = NULL_TREE;
5092             }
5093           if (scope && name && BASELINK_P (name))
5094             adjust_result_of_qualified_name_lookup
5095               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5096           postfix_expression
5097             = finish_class_member_access_expr (postfix_expression, name,
5098                                                template_p, 
5099                                                tf_warning_or_error);
5100         }
5101     }
5102
5103   /* We no longer need to look up names in the scope of the object on
5104      the left-hand side of the `.' or `->' operator.  */
5105   parser->context->object_type = NULL_TREE;
5106
5107   /* Outside of offsetof, these operators may not appear in
5108      constant-expressions.  */
5109   if (!for_offsetof
5110       && (cp_parser_non_integral_constant_expression
5111           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5112     postfix_expression = error_mark_node;
5113
5114   return postfix_expression;
5115 }
5116
5117 /* Parse a parenthesized expression-list.
5118
5119    expression-list:
5120      assignment-expression
5121      expression-list, assignment-expression
5122
5123    attribute-list:
5124      expression-list
5125      identifier
5126      identifier, expression-list
5127
5128    CAST_P is true if this expression is the target of a cast.
5129
5130    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5131    argument pack.
5132
5133    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5134    representation of an assignment-expression.  Note that a TREE_LIST
5135    is returned even if there is only a single expression in the list.
5136    error_mark_node is returned if the ( and or ) are
5137    missing. NULL_TREE is returned on no expressions. The parentheses
5138    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5139    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5140    indicates whether or not all of the expressions in the list were
5141    constant.  */
5142
5143 static tree
5144 cp_parser_parenthesized_expression_list (cp_parser* parser,
5145                                          bool is_attribute_list,
5146                                          bool cast_p,
5147                                          bool allow_expansion_p,
5148                                          bool *non_constant_p)
5149 {
5150   tree expression_list = NULL_TREE;
5151   bool fold_expr_p = is_attribute_list;
5152   tree identifier = NULL_TREE;
5153   bool saved_greater_than_is_operator_p;
5154
5155   /* Assume all the expressions will be constant.  */
5156   if (non_constant_p)
5157     *non_constant_p = false;
5158
5159   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5160     return error_mark_node;
5161
5162   /* Within a parenthesized expression, a `>' token is always
5163      the greater-than operator.  */
5164   saved_greater_than_is_operator_p
5165     = parser->greater_than_is_operator_p;
5166   parser->greater_than_is_operator_p = true;
5167
5168   /* Consume expressions until there are no more.  */
5169   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5170     while (true)
5171       {
5172         tree expr;
5173
5174         /* At the beginning of attribute lists, check to see if the
5175            next token is an identifier.  */
5176         if (is_attribute_list
5177             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5178           {
5179             cp_token *token;
5180
5181             /* Consume the identifier.  */
5182             token = cp_lexer_consume_token (parser->lexer);
5183             /* Save the identifier.  */
5184             identifier = token->u.value;
5185           }
5186         else
5187           {
5188             bool expr_non_constant_p;
5189
5190             /* Parse the next assignment-expression.  */
5191             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5192               {
5193                 /* A braced-init-list.  */
5194                 maybe_warn_cpp0x ("extended initializer lists");
5195                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5196                 if (non_constant_p && expr_non_constant_p)
5197                   *non_constant_p = true;
5198               }
5199             else if (non_constant_p)
5200               {
5201                 expr = (cp_parser_constant_expression
5202                         (parser, /*allow_non_constant_p=*/true,
5203                          &expr_non_constant_p));
5204                 if (expr_non_constant_p)
5205                   *non_constant_p = true;
5206               }
5207             else
5208               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5209
5210             if (fold_expr_p)
5211               expr = fold_non_dependent_expr (expr);
5212
5213             /* If we have an ellipsis, then this is an expression
5214                expansion.  */
5215             if (allow_expansion_p
5216                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5217               {
5218                 /* Consume the `...'.  */
5219                 cp_lexer_consume_token (parser->lexer);
5220
5221                 /* Build the argument pack.  */
5222                 expr = make_pack_expansion (expr);
5223               }
5224
5225              /* Add it to the list.  We add error_mark_node
5226                 expressions to the list, so that we can still tell if
5227                 the correct form for a parenthesized expression-list
5228                 is found. That gives better errors.  */
5229             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5230
5231             if (expr == error_mark_node)
5232               goto skip_comma;
5233           }
5234
5235         /* After the first item, attribute lists look the same as
5236            expression lists.  */
5237         is_attribute_list = false;
5238
5239       get_comma:;
5240         /* If the next token isn't a `,', then we are done.  */
5241         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5242           break;
5243
5244         /* Otherwise, consume the `,' and keep going.  */
5245         cp_lexer_consume_token (parser->lexer);
5246       }
5247
5248   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5249     {
5250       int ending;
5251
5252     skip_comma:;
5253       /* We try and resync to an unnested comma, as that will give the
5254          user better diagnostics.  */
5255       ending = cp_parser_skip_to_closing_parenthesis (parser,
5256                                                       /*recovering=*/true,
5257                                                       /*or_comma=*/true,
5258                                                       /*consume_paren=*/true);
5259       if (ending < 0)
5260         goto get_comma;
5261       if (!ending)
5262         {
5263           parser->greater_than_is_operator_p
5264             = saved_greater_than_is_operator_p;
5265           return error_mark_node;
5266         }
5267     }
5268
5269   parser->greater_than_is_operator_p
5270     = saved_greater_than_is_operator_p;
5271
5272   /* We built up the list in reverse order so we must reverse it now.  */
5273   expression_list = nreverse (expression_list);
5274   if (identifier)
5275     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5276
5277   return expression_list;
5278 }
5279
5280 /* Parse a pseudo-destructor-name.
5281
5282    pseudo-destructor-name:
5283      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5284      :: [opt] nested-name-specifier template template-id :: ~ type-name
5285      :: [opt] nested-name-specifier [opt] ~ type-name
5286
5287    If either of the first two productions is used, sets *SCOPE to the
5288    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5289    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5290    or ERROR_MARK_NODE if the parse fails.  */
5291
5292 static void
5293 cp_parser_pseudo_destructor_name (cp_parser* parser,
5294                                   tree* scope,
5295                                   tree* type)
5296 {
5297   bool nested_name_specifier_p;
5298
5299   /* Assume that things will not work out.  */
5300   *type = error_mark_node;
5301
5302   /* Look for the optional `::' operator.  */
5303   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5304   /* Look for the optional nested-name-specifier.  */
5305   nested_name_specifier_p
5306     = (cp_parser_nested_name_specifier_opt (parser,
5307                                             /*typename_keyword_p=*/false,
5308                                             /*check_dependency_p=*/true,
5309                                             /*type_p=*/false,
5310                                             /*is_declaration=*/false)
5311        != NULL_TREE);
5312   /* Now, if we saw a nested-name-specifier, we might be doing the
5313      second production.  */
5314   if (nested_name_specifier_p
5315       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5316     {
5317       /* Consume the `template' keyword.  */
5318       cp_lexer_consume_token (parser->lexer);
5319       /* Parse the template-id.  */
5320       cp_parser_template_id (parser,
5321                              /*template_keyword_p=*/true,
5322                              /*check_dependency_p=*/false,
5323                              /*is_declaration=*/true);
5324       /* Look for the `::' token.  */
5325       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5326     }
5327   /* If the next token is not a `~', then there might be some
5328      additional qualification.  */
5329   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5330     {
5331       /* At this point, we're looking for "type-name :: ~".  The type-name
5332          must not be a class-name, since this is a pseudo-destructor.  So,
5333          it must be either an enum-name, or a typedef-name -- both of which
5334          are just identifiers.  So, we peek ahead to check that the "::"
5335          and "~" tokens are present; if they are not, then we can avoid
5336          calling type_name.  */
5337       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5338           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5339           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5340         {
5341           cp_parser_error (parser, "non-scalar type");
5342           return;
5343         }
5344
5345       /* Look for the type-name.  */
5346       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5347       if (*scope == error_mark_node)
5348         return;
5349
5350       /* Look for the `::' token.  */
5351       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5352     }
5353   else
5354     *scope = NULL_TREE;
5355
5356   /* Look for the `~'.  */
5357   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5358   /* Look for the type-name again.  We are not responsible for
5359      checking that it matches the first type-name.  */
5360   *type = cp_parser_nonclass_name (parser);
5361 }
5362
5363 /* Parse a unary-expression.
5364
5365    unary-expression:
5366      postfix-expression
5367      ++ cast-expression
5368      -- cast-expression
5369      unary-operator cast-expression
5370      sizeof unary-expression
5371      sizeof ( type-id )
5372      new-expression
5373      delete-expression
5374
5375    GNU Extensions:
5376
5377    unary-expression:
5378      __extension__ cast-expression
5379      __alignof__ unary-expression
5380      __alignof__ ( type-id )
5381      __real__ cast-expression
5382      __imag__ cast-expression
5383      && identifier
5384
5385    ADDRESS_P is true iff the unary-expression is appearing as the
5386    operand of the `&' operator.   CAST_P is true if this expression is
5387    the target of a cast.
5388
5389    Returns a representation of the expression.  */
5390
5391 static tree
5392 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5393                             cp_id_kind * pidk)
5394 {
5395   cp_token *token;
5396   enum tree_code unary_operator;
5397
5398   /* Peek at the next token.  */
5399   token = cp_lexer_peek_token (parser->lexer);
5400   /* Some keywords give away the kind of expression.  */
5401   if (token->type == CPP_KEYWORD)
5402     {
5403       enum rid keyword = token->keyword;
5404
5405       switch (keyword)
5406         {
5407         case RID_ALIGNOF:
5408         case RID_SIZEOF:
5409           {
5410             tree operand;
5411             enum tree_code op;
5412
5413             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5414             /* Consume the token.  */
5415             cp_lexer_consume_token (parser->lexer);
5416             /* Parse the operand.  */
5417             operand = cp_parser_sizeof_operand (parser, keyword);
5418
5419             if (TYPE_P (operand))
5420               return cxx_sizeof_or_alignof_type (operand, op, true);
5421             else
5422               return cxx_sizeof_or_alignof_expr (operand, op, true);
5423           }
5424
5425         case RID_NEW:
5426           return cp_parser_new_expression (parser);
5427
5428         case RID_DELETE:
5429           return cp_parser_delete_expression (parser);
5430
5431         case RID_EXTENSION:
5432           {
5433             /* The saved value of the PEDANTIC flag.  */
5434             int saved_pedantic;
5435             tree expr;
5436
5437             /* Save away the PEDANTIC flag.  */
5438             cp_parser_extension_opt (parser, &saved_pedantic);
5439             /* Parse the cast-expression.  */
5440             expr = cp_parser_simple_cast_expression (parser);
5441             /* Restore the PEDANTIC flag.  */
5442             pedantic = saved_pedantic;
5443
5444             return expr;
5445           }
5446
5447         case RID_REALPART:
5448         case RID_IMAGPART:
5449           {
5450             tree expression;
5451
5452             /* Consume the `__real__' or `__imag__' token.  */
5453             cp_lexer_consume_token (parser->lexer);
5454             /* Parse the cast-expression.  */
5455             expression = cp_parser_simple_cast_expression (parser);
5456             /* Create the complete representation.  */
5457             return build_x_unary_op ((keyword == RID_REALPART
5458                                       ? REALPART_EXPR : IMAGPART_EXPR),
5459                                      expression,
5460                                      tf_warning_or_error);
5461           }
5462           break;
5463
5464         default:
5465           break;
5466         }
5467     }
5468
5469   /* Look for the `:: new' and `:: delete', which also signal the
5470      beginning of a new-expression, or delete-expression,
5471      respectively.  If the next token is `::', then it might be one of
5472      these.  */
5473   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5474     {
5475       enum rid keyword;
5476
5477       /* See if the token after the `::' is one of the keywords in
5478          which we're interested.  */
5479       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5480       /* If it's `new', we have a new-expression.  */
5481       if (keyword == RID_NEW)
5482         return cp_parser_new_expression (parser);
5483       /* Similarly, for `delete'.  */
5484       else if (keyword == RID_DELETE)
5485         return cp_parser_delete_expression (parser);
5486     }
5487
5488   /* Look for a unary operator.  */
5489   unary_operator = cp_parser_unary_operator (token);
5490   /* The `++' and `--' operators can be handled similarly, even though
5491      they are not technically unary-operators in the grammar.  */
5492   if (unary_operator == ERROR_MARK)
5493     {
5494       if (token->type == CPP_PLUS_PLUS)
5495         unary_operator = PREINCREMENT_EXPR;
5496       else if (token->type == CPP_MINUS_MINUS)
5497         unary_operator = PREDECREMENT_EXPR;
5498       /* Handle the GNU address-of-label extension.  */
5499       else if (cp_parser_allow_gnu_extensions_p (parser)
5500                && token->type == CPP_AND_AND)
5501         {
5502           tree identifier;
5503           tree expression;
5504           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5505
5506           /* Consume the '&&' token.  */
5507           cp_lexer_consume_token (parser->lexer);
5508           /* Look for the identifier.  */
5509           identifier = cp_parser_identifier (parser);
5510           /* Create an expression representing the address.  */
5511           expression = finish_label_address_expr (identifier, loc);
5512           if (cp_parser_non_integral_constant_expression (parser,
5513                                                 "the address of a label"))
5514             expression = error_mark_node;
5515           return expression;
5516         }
5517     }
5518   if (unary_operator != ERROR_MARK)
5519     {
5520       tree cast_expression;
5521       tree expression = error_mark_node;
5522       const char *non_constant_p = NULL;
5523
5524       /* Consume the operator token.  */
5525       token = cp_lexer_consume_token (parser->lexer);
5526       /* Parse the cast-expression.  */
5527       cast_expression
5528         = cp_parser_cast_expression (parser,
5529                                      unary_operator == ADDR_EXPR,
5530                                      /*cast_p=*/false, pidk);
5531       /* Now, build an appropriate representation.  */
5532       switch (unary_operator)
5533         {
5534         case INDIRECT_REF:
5535           non_constant_p = "%<*%>";
5536           expression = build_x_indirect_ref (cast_expression, "unary *",
5537                                              tf_warning_or_error);
5538           break;
5539
5540         case ADDR_EXPR:
5541           non_constant_p = "%<&%>";
5542           /* Fall through.  */
5543         case BIT_NOT_EXPR:
5544           expression = build_x_unary_op (unary_operator, cast_expression,
5545                                          tf_warning_or_error);
5546           break;
5547
5548         case PREINCREMENT_EXPR:
5549         case PREDECREMENT_EXPR:
5550           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5551                             ? "%<++%>" : "%<--%>");
5552           /* Fall through.  */
5553         case UNARY_PLUS_EXPR:
5554         case NEGATE_EXPR:
5555         case TRUTH_NOT_EXPR:
5556           expression = finish_unary_op_expr (unary_operator, cast_expression);
5557           break;
5558
5559         default:
5560           gcc_unreachable ();
5561         }
5562
5563       if (non_constant_p
5564           && cp_parser_non_integral_constant_expression (parser,
5565                                                          non_constant_p))
5566         expression = error_mark_node;
5567
5568       return expression;
5569     }
5570
5571   return cp_parser_postfix_expression (parser, address_p, cast_p,
5572                                        /*member_access_only_p=*/false,
5573                                        pidk);
5574 }
5575
5576 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5577    unary-operator, the corresponding tree code is returned.  */
5578
5579 static enum tree_code
5580 cp_parser_unary_operator (cp_token* token)
5581 {
5582   switch (token->type)
5583     {
5584     case CPP_MULT:
5585       return INDIRECT_REF;
5586
5587     case CPP_AND:
5588       return ADDR_EXPR;
5589
5590     case CPP_PLUS:
5591       return UNARY_PLUS_EXPR;
5592
5593     case CPP_MINUS:
5594       return NEGATE_EXPR;
5595
5596     case CPP_NOT:
5597       return TRUTH_NOT_EXPR;
5598
5599     case CPP_COMPL:
5600       return BIT_NOT_EXPR;
5601
5602     default:
5603       return ERROR_MARK;
5604     }
5605 }
5606
5607 /* Parse a new-expression.
5608
5609    new-expression:
5610      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5611      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5612
5613    Returns a representation of the expression.  */
5614
5615 static tree
5616 cp_parser_new_expression (cp_parser* parser)
5617 {
5618   bool global_scope_p;
5619   tree placement;
5620   tree type;
5621   tree initializer;
5622   tree nelts;
5623
5624   /* Look for the optional `::' operator.  */
5625   global_scope_p
5626     = (cp_parser_global_scope_opt (parser,
5627                                    /*current_scope_valid_p=*/false)
5628        != NULL_TREE);
5629   /* Look for the `new' operator.  */
5630   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5631   /* There's no easy way to tell a new-placement from the
5632      `( type-id )' construct.  */
5633   cp_parser_parse_tentatively (parser);
5634   /* Look for a new-placement.  */
5635   placement = cp_parser_new_placement (parser);
5636   /* If that didn't work out, there's no new-placement.  */
5637   if (!cp_parser_parse_definitely (parser))
5638     placement = NULL_TREE;
5639
5640   /* If the next token is a `(', then we have a parenthesized
5641      type-id.  */
5642   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5643     {
5644       cp_token *token;
5645       /* Consume the `('.  */
5646       cp_lexer_consume_token (parser->lexer);
5647       /* Parse the type-id.  */
5648       type = cp_parser_type_id (parser);
5649       /* Look for the closing `)'.  */
5650       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5651       token = cp_lexer_peek_token (parser->lexer);
5652       /* There should not be a direct-new-declarator in this production,
5653          but GCC used to allowed this, so we check and emit a sensible error
5654          message for this case.  */
5655       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5656         {
5657           error ("%Harray bound forbidden after parenthesized type-id",
5658                  &token->location);
5659           inform (token->location, 
5660                   "try removing the parentheses around the type-id");
5661           cp_parser_direct_new_declarator (parser);
5662         }
5663       nelts = NULL_TREE;
5664     }
5665   /* Otherwise, there must be a new-type-id.  */
5666   else
5667     type = cp_parser_new_type_id (parser, &nelts);
5668
5669   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5670   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5671       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5672     initializer = cp_parser_new_initializer (parser);
5673   else
5674     initializer = NULL_TREE;
5675
5676   /* A new-expression may not appear in an integral constant
5677      expression.  */
5678   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5679     return error_mark_node;
5680
5681   /* Create a representation of the new-expression.  */
5682   return build_new (placement, type, nelts, initializer, global_scope_p,
5683                     tf_warning_or_error);
5684 }
5685
5686 /* Parse a new-placement.
5687
5688    new-placement:
5689      ( expression-list )
5690
5691    Returns the same representation as for an expression-list.  */
5692
5693 static tree
5694 cp_parser_new_placement (cp_parser* parser)
5695 {
5696   tree expression_list;
5697
5698   /* Parse the expression-list.  */
5699   expression_list = (cp_parser_parenthesized_expression_list
5700                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5701                       /*non_constant_p=*/NULL));
5702
5703   return expression_list;
5704 }
5705
5706 /* Parse a new-type-id.
5707
5708    new-type-id:
5709      type-specifier-seq new-declarator [opt]
5710
5711    Returns the TYPE allocated.  If the new-type-id indicates an array
5712    type, *NELTS is set to the number of elements in the last array
5713    bound; the TYPE will not include the last array bound.  */
5714
5715 static tree
5716 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5717 {
5718   cp_decl_specifier_seq type_specifier_seq;
5719   cp_declarator *new_declarator;
5720   cp_declarator *declarator;
5721   cp_declarator *outer_declarator;
5722   const char *saved_message;
5723   tree type;
5724
5725   /* The type-specifier sequence must not contain type definitions.
5726      (It cannot contain declarations of new types either, but if they
5727      are not definitions we will catch that because they are not
5728      complete.)  */
5729   saved_message = parser->type_definition_forbidden_message;
5730   parser->type_definition_forbidden_message
5731     = "types may not be defined in a new-type-id";
5732   /* Parse the type-specifier-seq.  */
5733   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5734                                 &type_specifier_seq);
5735   /* Restore the old message.  */
5736   parser->type_definition_forbidden_message = saved_message;
5737   /* Parse the new-declarator.  */
5738   new_declarator = cp_parser_new_declarator_opt (parser);
5739
5740   /* Determine the number of elements in the last array dimension, if
5741      any.  */
5742   *nelts = NULL_TREE;
5743   /* Skip down to the last array dimension.  */
5744   declarator = new_declarator;
5745   outer_declarator = NULL;
5746   while (declarator && (declarator->kind == cdk_pointer
5747                         || declarator->kind == cdk_ptrmem))
5748     {
5749       outer_declarator = declarator;
5750       declarator = declarator->declarator;
5751     }
5752   while (declarator
5753          && declarator->kind == cdk_array
5754          && declarator->declarator
5755          && declarator->declarator->kind == cdk_array)
5756     {
5757       outer_declarator = declarator;
5758       declarator = declarator->declarator;
5759     }
5760
5761   if (declarator && declarator->kind == cdk_array)
5762     {
5763       *nelts = declarator->u.array.bounds;
5764       if (*nelts == error_mark_node)
5765         *nelts = integer_one_node;
5766
5767       if (outer_declarator)
5768         outer_declarator->declarator = declarator->declarator;
5769       else
5770         new_declarator = NULL;
5771     }
5772
5773   type = groktypename (&type_specifier_seq, new_declarator);
5774   return type;
5775 }
5776
5777 /* Parse an (optional) new-declarator.
5778
5779    new-declarator:
5780      ptr-operator new-declarator [opt]
5781      direct-new-declarator
5782
5783    Returns the declarator.  */
5784
5785 static cp_declarator *
5786 cp_parser_new_declarator_opt (cp_parser* parser)
5787 {
5788   enum tree_code code;
5789   tree type;
5790   cp_cv_quals cv_quals;
5791
5792   /* We don't know if there's a ptr-operator next, or not.  */
5793   cp_parser_parse_tentatively (parser);
5794   /* Look for a ptr-operator.  */
5795   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5796   /* If that worked, look for more new-declarators.  */
5797   if (cp_parser_parse_definitely (parser))
5798     {
5799       cp_declarator *declarator;
5800
5801       /* Parse another optional declarator.  */
5802       declarator = cp_parser_new_declarator_opt (parser);
5803
5804       return cp_parser_make_indirect_declarator
5805         (code, type, cv_quals, declarator);
5806     }
5807
5808   /* If the next token is a `[', there is a direct-new-declarator.  */
5809   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5810     return cp_parser_direct_new_declarator (parser);
5811
5812   return NULL;
5813 }
5814
5815 /* Parse a direct-new-declarator.
5816
5817    direct-new-declarator:
5818      [ expression ]
5819      direct-new-declarator [constant-expression]
5820
5821    */
5822
5823 static cp_declarator *
5824 cp_parser_direct_new_declarator (cp_parser* parser)
5825 {
5826   cp_declarator *declarator = NULL;
5827
5828   while (true)
5829     {
5830       tree expression;
5831
5832       /* Look for the opening `['.  */
5833       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5834       /* The first expression is not required to be constant.  */
5835       if (!declarator)
5836         {
5837           cp_token *token = cp_lexer_peek_token (parser->lexer);
5838           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5839           /* The standard requires that the expression have integral
5840              type.  DR 74 adds enumeration types.  We believe that the
5841              real intent is that these expressions be handled like the
5842              expression in a `switch' condition, which also allows
5843              classes with a single conversion to integral or
5844              enumeration type.  */
5845           if (!processing_template_decl)
5846             {
5847               expression
5848                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5849                                               expression,
5850                                               /*complain=*/true);
5851               if (!expression)
5852                 {
5853                   error ("%Hexpression in new-declarator must have integral "
5854                          "or enumeration type", &token->location);
5855                   expression = error_mark_node;
5856                 }
5857             }
5858         }
5859       /* But all the other expressions must be.  */
5860       else
5861         expression
5862           = cp_parser_constant_expression (parser,
5863                                            /*allow_non_constant=*/false,
5864                                            NULL);
5865       /* Look for the closing `]'.  */
5866       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5867
5868       /* Add this bound to the declarator.  */
5869       declarator = make_array_declarator (declarator, expression);
5870
5871       /* If the next token is not a `[', then there are no more
5872          bounds.  */
5873       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5874         break;
5875     }
5876
5877   return declarator;
5878 }
5879
5880 /* Parse a new-initializer.
5881
5882    new-initializer:
5883      ( expression-list [opt] )
5884      braced-init-list
5885
5886    Returns a representation of the expression-list.  If there is no
5887    expression-list, VOID_ZERO_NODE is returned.  */
5888
5889 static tree
5890 cp_parser_new_initializer (cp_parser* parser)
5891 {
5892   tree expression_list;
5893
5894   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5895     {
5896       bool expr_non_constant_p;
5897       maybe_warn_cpp0x ("extended initializer lists");
5898       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5899       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5900       expression_list = build_tree_list (NULL_TREE, expression_list);
5901     }
5902   else
5903     expression_list = (cp_parser_parenthesized_expression_list
5904                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5905                         /*non_constant_p=*/NULL));
5906   if (!expression_list)
5907     expression_list = void_zero_node;
5908
5909   return expression_list;
5910 }
5911
5912 /* Parse a delete-expression.
5913
5914    delete-expression:
5915      :: [opt] delete cast-expression
5916      :: [opt] delete [ ] cast-expression
5917
5918    Returns a representation of the expression.  */
5919
5920 static tree
5921 cp_parser_delete_expression (cp_parser* parser)
5922 {
5923   bool global_scope_p;
5924   bool array_p;
5925   tree expression;
5926
5927   /* Look for the optional `::' operator.  */
5928   global_scope_p
5929     = (cp_parser_global_scope_opt (parser,
5930                                    /*current_scope_valid_p=*/false)
5931        != NULL_TREE);
5932   /* Look for the `delete' keyword.  */
5933   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5934   /* See if the array syntax is in use.  */
5935   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5936     {
5937       /* Consume the `[' token.  */
5938       cp_lexer_consume_token (parser->lexer);
5939       /* Look for the `]' token.  */
5940       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5941       /* Remember that this is the `[]' construct.  */
5942       array_p = true;
5943     }
5944   else
5945     array_p = false;
5946
5947   /* Parse the cast-expression.  */
5948   expression = cp_parser_simple_cast_expression (parser);
5949
5950   /* A delete-expression may not appear in an integral constant
5951      expression.  */
5952   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5953     return error_mark_node;
5954
5955   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5956 }
5957
5958 /* Returns true if TOKEN may start a cast-expression and false
5959    otherwise.  */
5960
5961 static bool
5962 cp_parser_token_starts_cast_expression (cp_token *token)
5963 {
5964   switch (token->type)
5965     {
5966     case CPP_COMMA:
5967     case CPP_SEMICOLON:
5968     case CPP_QUERY:
5969     case CPP_COLON:
5970     case CPP_CLOSE_SQUARE:
5971     case CPP_CLOSE_PAREN:
5972     case CPP_CLOSE_BRACE:
5973     case CPP_DOT:
5974     case CPP_DOT_STAR:
5975     case CPP_DEREF:
5976     case CPP_DEREF_STAR:
5977     case CPP_DIV:
5978     case CPP_MOD:
5979     case CPP_LSHIFT:
5980     case CPP_RSHIFT:
5981     case CPP_LESS:
5982     case CPP_GREATER:
5983     case CPP_LESS_EQ:
5984     case CPP_GREATER_EQ:
5985     case CPP_EQ_EQ:
5986     case CPP_NOT_EQ:
5987     case CPP_EQ:
5988     case CPP_MULT_EQ:
5989     case CPP_DIV_EQ:
5990     case CPP_MOD_EQ:
5991     case CPP_PLUS_EQ:
5992     case CPP_MINUS_EQ:
5993     case CPP_RSHIFT_EQ:
5994     case CPP_LSHIFT_EQ:
5995     case CPP_AND_EQ:
5996     case CPP_XOR_EQ:
5997     case CPP_OR_EQ:
5998     case CPP_XOR:
5999     case CPP_OR:
6000     case CPP_OR_OR:
6001     case CPP_EOF:
6002       return false;
6003
6004       /* '[' may start a primary-expression in obj-c++.  */
6005     case CPP_OPEN_SQUARE:
6006       return c_dialect_objc ();
6007
6008     default:
6009       return true;
6010     }
6011 }
6012
6013 /* Parse a cast-expression.
6014
6015    cast-expression:
6016      unary-expression
6017      ( type-id ) cast-expression
6018
6019    ADDRESS_P is true iff the unary-expression is appearing as the
6020    operand of the `&' operator.   CAST_P is true if this expression is
6021    the target of a cast.
6022
6023    Returns a representation of the expression.  */
6024
6025 static tree
6026 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6027                            cp_id_kind * pidk)
6028 {
6029   /* If it's a `(', then we might be looking at a cast.  */
6030   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6031     {
6032       tree type = NULL_TREE;
6033       tree expr = NULL_TREE;
6034       bool compound_literal_p;
6035       const char *saved_message;
6036
6037       /* There's no way to know yet whether or not this is a cast.
6038          For example, `(int (3))' is a unary-expression, while `(int)
6039          3' is a cast.  So, we resort to parsing tentatively.  */
6040       cp_parser_parse_tentatively (parser);
6041       /* Types may not be defined in a cast.  */
6042       saved_message = parser->type_definition_forbidden_message;
6043       parser->type_definition_forbidden_message
6044         = "types may not be defined in casts";
6045       /* Consume the `('.  */
6046       cp_lexer_consume_token (parser->lexer);
6047       /* A very tricky bit is that `(struct S) { 3 }' is a
6048          compound-literal (which we permit in C++ as an extension).
6049          But, that construct is not a cast-expression -- it is a
6050          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6051          is legal; if the compound-literal were a cast-expression,
6052          you'd need an extra set of parentheses.)  But, if we parse
6053          the type-id, and it happens to be a class-specifier, then we
6054          will commit to the parse at that point, because we cannot
6055          undo the action that is done when creating a new class.  So,
6056          then we cannot back up and do a postfix-expression.
6057
6058          Therefore, we scan ahead to the closing `)', and check to see
6059          if the token after the `)' is a `{'.  If so, we are not
6060          looking at a cast-expression.
6061
6062          Save tokens so that we can put them back.  */
6063       cp_lexer_save_tokens (parser->lexer);
6064       /* Skip tokens until the next token is a closing parenthesis.
6065          If we find the closing `)', and the next token is a `{', then
6066          we are looking at a compound-literal.  */
6067       compound_literal_p
6068         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6069                                                   /*consume_paren=*/true)
6070            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6071       /* Roll back the tokens we skipped.  */
6072       cp_lexer_rollback_tokens (parser->lexer);
6073       /* If we were looking at a compound-literal, simulate an error
6074          so that the call to cp_parser_parse_definitely below will
6075          fail.  */
6076       if (compound_literal_p)
6077         cp_parser_simulate_error (parser);
6078       else
6079         {
6080           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6081           parser->in_type_id_in_expr_p = true;
6082           /* Look for the type-id.  */
6083           type = cp_parser_type_id (parser);
6084           /* Look for the closing `)'.  */
6085           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6086           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6087         }
6088
6089       /* Restore the saved message.  */
6090       parser->type_definition_forbidden_message = saved_message;
6091
6092       /* At this point this can only be either a cast or a
6093          parenthesized ctor such as `(T ())' that looks like a cast to
6094          function returning T.  */
6095       if (!cp_parser_error_occurred (parser)
6096           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6097                                                      (parser->lexer)))
6098         {
6099           cp_parser_parse_definitely (parser);
6100           expr = cp_parser_cast_expression (parser,
6101                                             /*address_p=*/false,
6102                                             /*cast_p=*/true, pidk);
6103
6104           /* Warn about old-style casts, if so requested.  */
6105           if (warn_old_style_cast
6106               && !in_system_header
6107               && !VOID_TYPE_P (type)
6108               && current_lang_name != lang_name_c)
6109             warning (OPT_Wold_style_cast, "use of old-style cast");
6110
6111           /* Only type conversions to integral or enumeration types
6112              can be used in constant-expressions.  */
6113           if (!cast_valid_in_integral_constant_expression_p (type)
6114               && (cp_parser_non_integral_constant_expression
6115                   (parser,
6116                    "a cast to a type other than an integral or "
6117                    "enumeration type")))
6118             return error_mark_node;
6119
6120           /* Perform the cast.  */
6121           expr = build_c_cast (type, expr);
6122           return expr;
6123         }
6124       else 
6125         cp_parser_abort_tentative_parse (parser);
6126     }
6127
6128   /* If we get here, then it's not a cast, so it must be a
6129      unary-expression.  */
6130   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6131 }
6132
6133 /* Parse a binary expression of the general form:
6134
6135    pm-expression:
6136      cast-expression
6137      pm-expression .* cast-expression
6138      pm-expression ->* cast-expression
6139
6140    multiplicative-expression:
6141      pm-expression
6142      multiplicative-expression * pm-expression
6143      multiplicative-expression / pm-expression
6144      multiplicative-expression % pm-expression
6145
6146    additive-expression:
6147      multiplicative-expression
6148      additive-expression + multiplicative-expression
6149      additive-expression - multiplicative-expression
6150
6151    shift-expression:
6152      additive-expression
6153      shift-expression << additive-expression
6154      shift-expression >> additive-expression
6155
6156    relational-expression:
6157      shift-expression
6158      relational-expression < shift-expression
6159      relational-expression > shift-expression
6160      relational-expression <= shift-expression
6161      relational-expression >= shift-expression
6162
6163   GNU Extension:
6164
6165    relational-expression:
6166      relational-expression <? shift-expression
6167      relational-expression >? shift-expression
6168
6169    equality-expression:
6170      relational-expression
6171      equality-expression == relational-expression
6172      equality-expression != relational-expression
6173
6174    and-expression:
6175      equality-expression
6176      and-expression & equality-expression
6177
6178    exclusive-or-expression:
6179      and-expression
6180      exclusive-or-expression ^ and-expression
6181
6182    inclusive-or-expression:
6183      exclusive-or-expression
6184      inclusive-or-expression | exclusive-or-expression
6185
6186    logical-and-expression:
6187      inclusive-or-expression
6188      logical-and-expression && inclusive-or-expression
6189
6190    logical-or-expression:
6191      logical-and-expression
6192      logical-or-expression || logical-and-expression
6193
6194    All these are implemented with a single function like:
6195
6196    binary-expression:
6197      simple-cast-expression
6198      binary-expression <token> binary-expression
6199
6200    CAST_P is true if this expression is the target of a cast.
6201
6202    The binops_by_token map is used to get the tree codes for each <token> type.
6203    binary-expressions are associated according to a precedence table.  */
6204
6205 #define TOKEN_PRECEDENCE(token)                              \
6206 (((token->type == CPP_GREATER                                \
6207    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6208   && !parser->greater_than_is_operator_p)                    \
6209  ? PREC_NOT_OPERATOR                                         \
6210  : binops_by_token[token->type].prec)
6211
6212 static tree
6213 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6214                              bool no_toplevel_fold_p,
6215                              enum cp_parser_prec prec,
6216                              cp_id_kind * pidk)
6217 {
6218   cp_parser_expression_stack stack;
6219   cp_parser_expression_stack_entry *sp = &stack[0];
6220   tree lhs, rhs;
6221   cp_token *token;
6222   enum tree_code tree_type, lhs_type, rhs_type;
6223   enum cp_parser_prec new_prec, lookahead_prec;
6224   bool overloaded_p;
6225
6226   /* Parse the first expression.  */
6227   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6228   lhs_type = ERROR_MARK;
6229
6230   for (;;)
6231     {
6232       /* Get an operator token.  */
6233       token = cp_lexer_peek_token (parser->lexer);
6234
6235       if (warn_cxx0x_compat
6236           && token->type == CPP_RSHIFT
6237           && !parser->greater_than_is_operator_p)
6238         {
6239           warning (OPT_Wc__0x_compat, 
6240                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6241                    &token->location);
6242           warning (OPT_Wc__0x_compat, 
6243                    "suggest parentheses around %<>>%> expression");
6244         }
6245
6246       new_prec = TOKEN_PRECEDENCE (token);
6247
6248       /* Popping an entry off the stack means we completed a subexpression:
6249          - either we found a token which is not an operator (`>' where it is not
6250            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6251            will happen repeatedly;
6252          - or, we found an operator which has lower priority.  This is the case
6253            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6254            parsing `3 * 4'.  */
6255       if (new_prec <= prec)
6256         {
6257           if (sp == stack)
6258             break;
6259           else
6260             goto pop;
6261         }
6262
6263      get_rhs:
6264       tree_type = binops_by_token[token->type].tree_type;
6265
6266       /* We used the operator token.  */
6267       cp_lexer_consume_token (parser->lexer);
6268
6269       /* Extract another operand.  It may be the RHS of this expression
6270          or the LHS of a new, higher priority expression.  */
6271       rhs = cp_parser_simple_cast_expression (parser);
6272       rhs_type = ERROR_MARK;
6273
6274       /* Get another operator token.  Look up its precedence to avoid
6275          building a useless (immediately popped) stack entry for common
6276          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6277       token = cp_lexer_peek_token (parser->lexer);
6278       lookahead_prec = TOKEN_PRECEDENCE (token);
6279       if (lookahead_prec > new_prec)
6280         {
6281           /* ... and prepare to parse the RHS of the new, higher priority
6282              expression.  Since precedence levels on the stack are
6283              monotonically increasing, we do not have to care about
6284              stack overflows.  */
6285           sp->prec = prec;
6286           sp->tree_type = tree_type;
6287           sp->lhs = lhs;
6288           sp->lhs_type = lhs_type;
6289           sp++;
6290           lhs = rhs;
6291           lhs_type = rhs_type;
6292           prec = new_prec;
6293           new_prec = lookahead_prec;
6294           goto get_rhs;
6295
6296          pop:
6297           lookahead_prec = new_prec;
6298           /* If the stack is not empty, we have parsed into LHS the right side
6299              (`4' in the example above) of an expression we had suspended.
6300              We can use the information on the stack to recover the LHS (`3')
6301              from the stack together with the tree code (`MULT_EXPR'), and
6302              the precedence of the higher level subexpression
6303              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6304              which will be used to actually build the additive expression.  */
6305           --sp;
6306           prec = sp->prec;
6307           tree_type = sp->tree_type;
6308           rhs = lhs;
6309           rhs_type = lhs_type;
6310           lhs = sp->lhs;
6311           lhs_type = sp->lhs_type;
6312         }
6313
6314       overloaded_p = false;
6315       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6316          ERROR_MARK for everything that is not a binary expression.
6317          This makes warn_about_parentheses miss some warnings that
6318          involve unary operators.  For unary expressions we should
6319          pass the correct tree_code unless the unary expression was
6320          surrounded by parentheses.
6321       */
6322       if (no_toplevel_fold_p
6323           && lookahead_prec <= prec
6324           && sp == stack
6325           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6326         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6327       else
6328         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6329                                  &overloaded_p, tf_warning_or_error);
6330       lhs_type = tree_type;
6331
6332       /* If the binary operator required the use of an overloaded operator,
6333          then this expression cannot be an integral constant-expression.
6334          An overloaded operator can be used even if both operands are
6335          otherwise permissible in an integral constant-expression if at
6336          least one of the operands is of enumeration type.  */
6337
6338       if (overloaded_p
6339           && (cp_parser_non_integral_constant_expression
6340               (parser, "calls to overloaded operators")))
6341         return error_mark_node;
6342     }
6343
6344   return lhs;
6345 }
6346
6347
6348 /* Parse the `? expression : assignment-expression' part of a
6349    conditional-expression.  The LOGICAL_OR_EXPR is the
6350    logical-or-expression that started the conditional-expression.
6351    Returns a representation of the entire conditional-expression.
6352
6353    This routine is used by cp_parser_assignment_expression.
6354
6355      ? expression : assignment-expression
6356
6357    GNU Extensions:
6358
6359      ? : assignment-expression */
6360
6361 static tree
6362 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6363 {
6364   tree expr;
6365   tree assignment_expr;
6366
6367   /* Consume the `?' token.  */
6368   cp_lexer_consume_token (parser->lexer);
6369   if (cp_parser_allow_gnu_extensions_p (parser)
6370       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6371     /* Implicit true clause.  */
6372     expr = NULL_TREE;
6373   else
6374     /* Parse the expression.  */
6375     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6376
6377   /* The next token should be a `:'.  */
6378   cp_parser_require (parser, CPP_COLON, "%<:%>");
6379   /* Parse the assignment-expression.  */
6380   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6381
6382   /* Build the conditional-expression.  */
6383   return build_x_conditional_expr (logical_or_expr,
6384                                    expr,
6385                                    assignment_expr,
6386                                    tf_warning_or_error);
6387 }
6388
6389 /* Parse an assignment-expression.
6390
6391    assignment-expression:
6392      conditional-expression
6393      logical-or-expression assignment-operator assignment_expression
6394      throw-expression
6395
6396    CAST_P is true if this expression is the target of a cast.
6397
6398    Returns a representation for the expression.  */
6399
6400 static tree
6401 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6402                                  cp_id_kind * pidk)
6403 {
6404   tree expr;
6405
6406   /* If the next token is the `throw' keyword, then we're looking at
6407      a throw-expression.  */
6408   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6409     expr = cp_parser_throw_expression (parser);
6410   /* Otherwise, it must be that we are looking at a
6411      logical-or-expression.  */
6412   else
6413     {
6414       /* Parse the binary expressions (logical-or-expression).  */
6415       expr = cp_parser_binary_expression (parser, cast_p, false,
6416                                           PREC_NOT_OPERATOR, pidk);
6417       /* If the next token is a `?' then we're actually looking at a
6418          conditional-expression.  */
6419       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6420         return cp_parser_question_colon_clause (parser, expr);
6421       else
6422         {
6423           enum tree_code assignment_operator;
6424
6425           /* If it's an assignment-operator, we're using the second
6426              production.  */
6427           assignment_operator
6428             = cp_parser_assignment_operator_opt (parser);
6429           if (assignment_operator != ERROR_MARK)
6430             {
6431               bool non_constant_p;
6432
6433               /* Parse the right-hand side of the assignment.  */
6434               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6435
6436               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6437                 maybe_warn_cpp0x ("extended initializer lists");
6438
6439               /* An assignment may not appear in a
6440                  constant-expression.  */
6441               if (cp_parser_non_integral_constant_expression (parser,
6442                                                               "an assignment"))
6443                 return error_mark_node;
6444               /* Build the assignment expression.  */
6445               expr = build_x_modify_expr (expr,
6446                                           assignment_operator,
6447                                           rhs,
6448                                           tf_warning_or_error);
6449             }
6450         }
6451     }
6452
6453   return expr;
6454 }
6455
6456 /* Parse an (optional) assignment-operator.
6457
6458    assignment-operator: one of
6459      = *= /= %= += -= >>= <<= &= ^= |=
6460
6461    GNU Extension:
6462
6463    assignment-operator: one of
6464      <?= >?=
6465
6466    If the next token is an assignment operator, the corresponding tree
6467    code is returned, and the token is consumed.  For example, for
6468    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6469    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6470    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6471    operator, ERROR_MARK is returned.  */
6472
6473 static enum tree_code
6474 cp_parser_assignment_operator_opt (cp_parser* parser)
6475 {
6476   enum tree_code op;
6477   cp_token *token;
6478
6479   /* Peek at the next token.  */
6480   token = cp_lexer_peek_token (parser->lexer);
6481
6482   switch (token->type)
6483     {
6484     case CPP_EQ:
6485       op = NOP_EXPR;
6486       break;
6487
6488     case CPP_MULT_EQ:
6489       op = MULT_EXPR;
6490       break;
6491
6492     case CPP_DIV_EQ:
6493       op = TRUNC_DIV_EXPR;
6494       break;
6495
6496     case CPP_MOD_EQ:
6497       op = TRUNC_MOD_EXPR;
6498       break;
6499
6500     case CPP_PLUS_EQ:
6501       op = PLUS_EXPR;
6502       break;
6503
6504     case CPP_MINUS_EQ:
6505       op = MINUS_EXPR;
6506       break;
6507
6508     case CPP_RSHIFT_EQ:
6509       op = RSHIFT_EXPR;
6510       break;
6511
6512     case CPP_LSHIFT_EQ:
6513       op = LSHIFT_EXPR;
6514       break;
6515
6516     case CPP_AND_EQ:
6517       op = BIT_AND_EXPR;
6518       break;
6519
6520     case CPP_XOR_EQ:
6521       op = BIT_XOR_EXPR;
6522       break;
6523
6524     case CPP_OR_EQ:
6525       op = BIT_IOR_EXPR;
6526       break;
6527
6528     default:
6529       /* Nothing else is an assignment operator.  */
6530       op = ERROR_MARK;
6531     }
6532
6533   /* If it was an assignment operator, consume it.  */
6534   if (op != ERROR_MARK)
6535     cp_lexer_consume_token (parser->lexer);
6536
6537   return op;
6538 }
6539
6540 /* Parse an expression.
6541
6542    expression:
6543      assignment-expression
6544      expression , assignment-expression
6545
6546    CAST_P is true if this expression is the target of a cast.
6547
6548    Returns a representation of the expression.  */
6549
6550 static tree
6551 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6552 {
6553   tree expression = NULL_TREE;
6554
6555   while (true)
6556     {
6557       tree assignment_expression;
6558
6559       /* Parse the next assignment-expression.  */
6560       assignment_expression
6561         = cp_parser_assignment_expression (parser, cast_p, pidk);
6562       /* If this is the first assignment-expression, we can just
6563          save it away.  */
6564       if (!expression)
6565         expression = assignment_expression;
6566       else
6567         expression = build_x_compound_expr (expression,
6568                                             assignment_expression,
6569                                             tf_warning_or_error);
6570       /* If the next token is not a comma, then we are done with the
6571          expression.  */
6572       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6573         break;
6574       /* Consume the `,'.  */
6575       cp_lexer_consume_token (parser->lexer);
6576       /* A comma operator cannot appear in a constant-expression.  */
6577       if (cp_parser_non_integral_constant_expression (parser,
6578                                                       "a comma operator"))
6579         expression = error_mark_node;
6580     }
6581
6582   return expression;
6583 }
6584
6585 /* Parse a constant-expression.
6586
6587    constant-expression:
6588      conditional-expression
6589
6590   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6591   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6592   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6593   is false, NON_CONSTANT_P should be NULL.  */
6594
6595 static tree
6596 cp_parser_constant_expression (cp_parser* parser,
6597                                bool allow_non_constant_p,
6598                                bool *non_constant_p)
6599 {
6600   bool saved_integral_constant_expression_p;
6601   bool saved_allow_non_integral_constant_expression_p;
6602   bool saved_non_integral_constant_expression_p;
6603   tree expression;
6604
6605   /* It might seem that we could simply parse the
6606      conditional-expression, and then check to see if it were
6607      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6608      one that the compiler can figure out is constant, possibly after
6609      doing some simplifications or optimizations.  The standard has a
6610      precise definition of constant-expression, and we must honor
6611      that, even though it is somewhat more restrictive.
6612
6613      For example:
6614
6615        int i[(2, 3)];
6616
6617      is not a legal declaration, because `(2, 3)' is not a
6618      constant-expression.  The `,' operator is forbidden in a
6619      constant-expression.  However, GCC's constant-folding machinery
6620      will fold this operation to an INTEGER_CST for `3'.  */
6621
6622   /* Save the old settings.  */
6623   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6624   saved_allow_non_integral_constant_expression_p
6625     = parser->allow_non_integral_constant_expression_p;
6626   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6627   /* We are now parsing a constant-expression.  */
6628   parser->integral_constant_expression_p = true;
6629   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6630   parser->non_integral_constant_expression_p = false;
6631   /* Although the grammar says "conditional-expression", we parse an
6632      "assignment-expression", which also permits "throw-expression"
6633      and the use of assignment operators.  In the case that
6634      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6635      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6636      actually essential that we look for an assignment-expression.
6637      For example, cp_parser_initializer_clauses uses this function to
6638      determine whether a particular assignment-expression is in fact
6639      constant.  */
6640   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6641   /* Restore the old settings.  */
6642   parser->integral_constant_expression_p
6643     = saved_integral_constant_expression_p;
6644   parser->allow_non_integral_constant_expression_p
6645     = saved_allow_non_integral_constant_expression_p;
6646   if (allow_non_constant_p)
6647     *non_constant_p = parser->non_integral_constant_expression_p;
6648   else if (parser->non_integral_constant_expression_p)
6649     expression = error_mark_node;
6650   parser->non_integral_constant_expression_p
6651     = saved_non_integral_constant_expression_p;
6652
6653   return expression;
6654 }
6655
6656 /* Parse __builtin_offsetof.
6657
6658    offsetof-expression:
6659      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6660
6661    offsetof-member-designator:
6662      id-expression
6663      | offsetof-member-designator "." id-expression
6664      | offsetof-member-designator "[" expression "]"
6665      | offsetof-member-designator "->" id-expression  */
6666
6667 static tree
6668 cp_parser_builtin_offsetof (cp_parser *parser)
6669 {
6670   int save_ice_p, save_non_ice_p;
6671   tree type, expr;
6672   cp_id_kind dummy;
6673   cp_token *token;
6674
6675   /* We're about to accept non-integral-constant things, but will
6676      definitely yield an integral constant expression.  Save and
6677      restore these values around our local parsing.  */
6678   save_ice_p = parser->integral_constant_expression_p;
6679   save_non_ice_p = parser->non_integral_constant_expression_p;
6680
6681   /* Consume the "__builtin_offsetof" token.  */
6682   cp_lexer_consume_token (parser->lexer);
6683   /* Consume the opening `('.  */
6684   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6685   /* Parse the type-id.  */
6686   type = cp_parser_type_id (parser);
6687   /* Look for the `,'.  */
6688   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6689   token = cp_lexer_peek_token (parser->lexer);
6690
6691   /* Build the (type *)null that begins the traditional offsetof macro.  */
6692   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6693                             tf_warning_or_error);
6694
6695   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6696   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6697                                                  true, &dummy, token->location);
6698   while (true)
6699     {
6700       token = cp_lexer_peek_token (parser->lexer);
6701       switch (token->type)
6702         {
6703         case CPP_OPEN_SQUARE:
6704           /* offsetof-member-designator "[" expression "]" */
6705           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6706           break;
6707
6708         case CPP_DEREF:
6709           /* offsetof-member-designator "->" identifier */
6710           expr = grok_array_decl (expr, integer_zero_node);
6711           /* FALLTHRU */
6712
6713         case CPP_DOT:
6714           /* offsetof-member-designator "." identifier */
6715           cp_lexer_consume_token (parser->lexer);
6716           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6717                                                          expr, true, &dummy,
6718                                                          token->location);
6719           break;
6720
6721         case CPP_CLOSE_PAREN:
6722           /* Consume the ")" token.  */
6723           cp_lexer_consume_token (parser->lexer);
6724           goto success;
6725
6726         default:
6727           /* Error.  We know the following require will fail, but
6728              that gives the proper error message.  */
6729           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6730           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6731           expr = error_mark_node;
6732           goto failure;
6733         }
6734     }
6735
6736  success:
6737   /* If we're processing a template, we can't finish the semantics yet.
6738      Otherwise we can fold the entire expression now.  */
6739   if (processing_template_decl)
6740     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6741   else
6742     expr = finish_offsetof (expr);
6743
6744  failure:
6745   parser->integral_constant_expression_p = save_ice_p;
6746   parser->non_integral_constant_expression_p = save_non_ice_p;
6747
6748   return expr;
6749 }
6750
6751 /* Parse a trait expression.  */
6752
6753 static tree
6754 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6755 {
6756   cp_trait_kind kind;
6757   tree type1, type2 = NULL_TREE;
6758   bool binary = false;
6759   cp_decl_specifier_seq decl_specs;
6760
6761   switch (keyword)
6762     {
6763     case RID_HAS_NOTHROW_ASSIGN:
6764       kind = CPTK_HAS_NOTHROW_ASSIGN;
6765       break;
6766     case RID_HAS_NOTHROW_CONSTRUCTOR:
6767       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6768       break;
6769     case RID_HAS_NOTHROW_COPY:
6770       kind = CPTK_HAS_NOTHROW_COPY;
6771       break;
6772     case RID_HAS_TRIVIAL_ASSIGN:
6773       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6774       break;
6775     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6776       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6777       break;
6778     case RID_HAS_TRIVIAL_COPY:
6779       kind = CPTK_HAS_TRIVIAL_COPY;
6780       break;
6781     case RID_HAS_TRIVIAL_DESTRUCTOR:
6782       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6783       break;
6784     case RID_HAS_VIRTUAL_DESTRUCTOR:
6785       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6786       break;
6787     case RID_IS_ABSTRACT:
6788       kind = CPTK_IS_ABSTRACT;
6789       break;
6790     case RID_IS_BASE_OF:
6791       kind = CPTK_IS_BASE_OF;
6792       binary = true;
6793       break;
6794     case RID_IS_CLASS:
6795       kind = CPTK_IS_CLASS;
6796       break;
6797     case RID_IS_CONVERTIBLE_TO:
6798       kind = CPTK_IS_CONVERTIBLE_TO;
6799       binary = true;
6800       break;
6801     case RID_IS_EMPTY:
6802       kind = CPTK_IS_EMPTY;
6803       break;
6804     case RID_IS_ENUM:
6805       kind = CPTK_IS_ENUM;
6806       break;
6807     case RID_IS_POD:
6808       kind = CPTK_IS_POD;
6809       break;
6810     case RID_IS_POLYMORPHIC:
6811       kind = CPTK_IS_POLYMORPHIC;
6812       break;
6813     case RID_IS_UNION:
6814       kind = CPTK_IS_UNION;
6815       break;
6816     default:
6817       gcc_unreachable ();
6818     }
6819
6820   /* Consume the token.  */
6821   cp_lexer_consume_token (parser->lexer);
6822
6823   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6824
6825   type1 = cp_parser_type_id (parser);
6826
6827   if (type1 == error_mark_node)
6828     return error_mark_node;
6829
6830   /* Build a trivial decl-specifier-seq.  */
6831   clear_decl_specs (&decl_specs);
6832   decl_specs.type = type1;
6833
6834   /* Call grokdeclarator to figure out what type this is.  */
6835   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6836                           /*initialized=*/0, /*attrlist=*/NULL);
6837
6838   if (binary)
6839     {
6840       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6841  
6842       type2 = cp_parser_type_id (parser);
6843
6844       if (type2 == 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 = type2;
6850
6851       /* Call grokdeclarator to figure out what type this is.  */
6852       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6853                               /*initialized=*/0, /*attrlist=*/NULL);
6854     }
6855
6856   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6857
6858   /* Complete the trait expression, which may mean either processing
6859      the trait expr now or saving it for template instantiation.  */
6860   return finish_trait_expr (kind, type1, type2);
6861 }
6862
6863 /* Statements [gram.stmt.stmt]  */
6864
6865 /* Parse a statement.
6866
6867    statement:
6868      labeled-statement
6869      expression-statement
6870      compound-statement
6871      selection-statement
6872      iteration-statement
6873      jump-statement
6874      declaration-statement
6875      try-block
6876
6877   IN_COMPOUND is true when the statement is nested inside a
6878   cp_parser_compound_statement; this matters for certain pragmas.
6879
6880   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6881   is a (possibly labeled) if statement which is not enclosed in braces
6882   and has an else clause.  This is used to implement -Wparentheses.  */
6883
6884 static void
6885 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6886                      bool in_compound, bool *if_p)
6887 {
6888   tree statement;
6889   cp_token *token;
6890   location_t statement_location;
6891
6892  restart:
6893   if (if_p != NULL)
6894     *if_p = false;
6895   /* There is no statement yet.  */
6896   statement = NULL_TREE;
6897   /* Peek at the next token.  */
6898   token = cp_lexer_peek_token (parser->lexer);
6899   /* Remember the location of the first token in the statement.  */
6900   statement_location = token->location;
6901   /* If this is a keyword, then that will often determine what kind of
6902      statement we have.  */
6903   if (token->type == CPP_KEYWORD)
6904     {
6905       enum rid keyword = token->keyword;
6906
6907       switch (keyword)
6908         {
6909         case RID_CASE:
6910         case RID_DEFAULT:
6911           /* Looks like a labeled-statement with a case label.
6912              Parse the label, and then use tail recursion to parse
6913              the statement.  */
6914           cp_parser_label_for_labeled_statement (parser);
6915           goto restart;
6916
6917         case RID_IF:
6918         case RID_SWITCH:
6919           statement = cp_parser_selection_statement (parser, if_p);
6920           break;
6921
6922         case RID_WHILE:
6923         case RID_DO:
6924         case RID_FOR:
6925           statement = cp_parser_iteration_statement (parser);
6926           break;
6927
6928         case RID_BREAK:
6929         case RID_CONTINUE:
6930         case RID_RETURN:
6931         case RID_GOTO:
6932           statement = cp_parser_jump_statement (parser);
6933           break;
6934
6935           /* Objective-C++ exception-handling constructs.  */
6936         case RID_AT_TRY:
6937         case RID_AT_CATCH:
6938         case RID_AT_FINALLY:
6939         case RID_AT_SYNCHRONIZED:
6940         case RID_AT_THROW:
6941           statement = cp_parser_objc_statement (parser);
6942           break;
6943
6944         case RID_TRY:
6945           statement = cp_parser_try_block (parser);
6946           break;
6947
6948         case RID_NAMESPACE:
6949           /* This must be a namespace alias definition.  */
6950           cp_parser_declaration_statement (parser);
6951           return;
6952           
6953         default:
6954           /* It might be a keyword like `int' that can start a
6955              declaration-statement.  */
6956           break;
6957         }
6958     }
6959   else if (token->type == CPP_NAME)
6960     {
6961       /* If the next token is a `:', then we are looking at a
6962          labeled-statement.  */
6963       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6964       if (token->type == CPP_COLON)
6965         {
6966           /* Looks like a labeled-statement with an ordinary label.
6967              Parse the label, and then use tail recursion to parse
6968              the statement.  */
6969           cp_parser_label_for_labeled_statement (parser);
6970           goto restart;
6971         }
6972     }
6973   /* Anything that starts with a `{' must be a compound-statement.  */
6974   else if (token->type == CPP_OPEN_BRACE)
6975     statement = cp_parser_compound_statement (parser, NULL, false);
6976   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6977      a statement all its own.  */
6978   else if (token->type == CPP_PRAGMA)
6979     {
6980       /* Only certain OpenMP pragmas are attached to statements, and thus
6981          are considered statements themselves.  All others are not.  In
6982          the context of a compound, accept the pragma as a "statement" and
6983          return so that we can check for a close brace.  Otherwise we
6984          require a real statement and must go back and read one.  */
6985       if (in_compound)
6986         cp_parser_pragma (parser, pragma_compound);
6987       else if (!cp_parser_pragma (parser, pragma_stmt))
6988         goto restart;
6989       return;
6990     }
6991   else if (token->type == CPP_EOF)
6992     {
6993       cp_parser_error (parser, "expected statement");
6994       return;
6995     }
6996
6997   /* Everything else must be a declaration-statement or an
6998      expression-statement.  Try for the declaration-statement
6999      first, unless we are looking at a `;', in which case we know that
7000      we have an expression-statement.  */
7001   if (!statement)
7002     {
7003       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7004         {
7005           cp_parser_parse_tentatively (parser);
7006           /* Try to parse the declaration-statement.  */
7007           cp_parser_declaration_statement (parser);
7008           /* If that worked, we're done.  */
7009           if (cp_parser_parse_definitely (parser))
7010             return;
7011         }
7012       /* Look for an expression-statement instead.  */
7013       statement = cp_parser_expression_statement (parser, in_statement_expr);
7014     }
7015
7016   /* Set the line number for the statement.  */
7017   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7018     SET_EXPR_LOCATION (statement, statement_location);
7019 }
7020
7021 /* Parse the label for a labeled-statement, i.e.
7022
7023    identifier :
7024    case constant-expression :
7025    default :
7026
7027    GNU Extension:
7028    case constant-expression ... constant-expression : statement
7029
7030    When a label is parsed without errors, the label is added to the
7031    parse tree by the finish_* functions, so this function doesn't
7032    have to return the label.  */
7033
7034 static void
7035 cp_parser_label_for_labeled_statement (cp_parser* parser)
7036 {
7037   cp_token *token;
7038
7039   /* The next token should be an identifier.  */
7040   token = cp_lexer_peek_token (parser->lexer);
7041   if (token->type != CPP_NAME
7042       && token->type != CPP_KEYWORD)
7043     {
7044       cp_parser_error (parser, "expected labeled-statement");
7045       return;
7046     }
7047
7048   switch (token->keyword)
7049     {
7050     case RID_CASE:
7051       {
7052         tree expr, expr_hi;
7053         cp_token *ellipsis;
7054
7055         /* Consume the `case' token.  */
7056         cp_lexer_consume_token (parser->lexer);
7057         /* Parse the constant-expression.  */
7058         expr = cp_parser_constant_expression (parser,
7059                                               /*allow_non_constant_p=*/false,
7060                                               NULL);
7061
7062         ellipsis = cp_lexer_peek_token (parser->lexer);
7063         if (ellipsis->type == CPP_ELLIPSIS)
7064           {
7065             /* Consume the `...' token.  */
7066             cp_lexer_consume_token (parser->lexer);
7067             expr_hi =
7068               cp_parser_constant_expression (parser,
7069                                              /*allow_non_constant_p=*/false,
7070                                              NULL);
7071             /* We don't need to emit warnings here, as the common code
7072                will do this for us.  */
7073           }
7074         else
7075           expr_hi = NULL_TREE;
7076
7077         if (parser->in_switch_statement_p)
7078           finish_case_label (expr, expr_hi);
7079         else
7080           error ("%Hcase label %qE not within a switch statement",
7081                  &token->location, expr);
7082       }
7083       break;
7084
7085     case RID_DEFAULT:
7086       /* Consume the `default' token.  */
7087       cp_lexer_consume_token (parser->lexer);
7088
7089       if (parser->in_switch_statement_p)
7090         finish_case_label (NULL_TREE, NULL_TREE);
7091       else
7092         error ("%Hcase label not within a switch statement", &token->location);
7093       break;
7094
7095     default:
7096       /* Anything else must be an ordinary label.  */
7097       finish_label_stmt (cp_parser_identifier (parser));
7098       break;
7099     }
7100
7101   /* Require the `:' token.  */
7102   cp_parser_require (parser, CPP_COLON, "%<:%>");
7103 }
7104
7105 /* Parse an expression-statement.
7106
7107    expression-statement:
7108      expression [opt] ;
7109
7110    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7111    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7112    indicates whether this expression-statement is part of an
7113    expression statement.  */
7114
7115 static tree
7116 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7117 {
7118   tree statement = NULL_TREE;
7119
7120   /* If the next token is a ';', then there is no expression
7121      statement.  */
7122   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7123     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7124
7125   /* Consume the final `;'.  */
7126   cp_parser_consume_semicolon_at_end_of_statement (parser);
7127
7128   if (in_statement_expr
7129       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7130     /* This is the final expression statement of a statement
7131        expression.  */
7132     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7133   else if (statement)
7134     statement = finish_expr_stmt (statement);
7135   else
7136     finish_stmt ();
7137
7138   return statement;
7139 }
7140
7141 /* Parse a compound-statement.
7142
7143    compound-statement:
7144      { statement-seq [opt] }
7145
7146    GNU extension:
7147
7148    compound-statement:
7149      { label-declaration-seq [opt] statement-seq [opt] }
7150
7151    label-declaration-seq:
7152      label-declaration
7153      label-declaration-seq label-declaration
7154
7155    Returns a tree representing the statement.  */
7156
7157 static tree
7158 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7159                               bool in_try)
7160 {
7161   tree compound_stmt;
7162
7163   /* Consume the `{'.  */
7164   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7165     return error_mark_node;
7166   /* Begin the compound-statement.  */
7167   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7168   /* If the next keyword is `__label__' we have a label declaration.  */
7169   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7170     cp_parser_label_declaration (parser);
7171   /* Parse an (optional) statement-seq.  */
7172   cp_parser_statement_seq_opt (parser, in_statement_expr);
7173   /* Finish the compound-statement.  */
7174   finish_compound_stmt (compound_stmt);
7175   /* Consume the `}'.  */
7176   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7177
7178   return compound_stmt;
7179 }
7180
7181 /* Parse an (optional) statement-seq.
7182
7183    statement-seq:
7184      statement
7185      statement-seq [opt] statement  */
7186
7187 static void
7188 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7189 {
7190   /* Scan statements until there aren't any more.  */
7191   while (true)
7192     {
7193       cp_token *token = cp_lexer_peek_token (parser->lexer);
7194
7195       /* If we're looking at a `}', then we've run out of statements.  */
7196       if (token->type == CPP_CLOSE_BRACE
7197           || token->type == CPP_EOF
7198           || token->type == CPP_PRAGMA_EOL)
7199         break;
7200       
7201       /* If we are in a compound statement and find 'else' then
7202          something went wrong.  */
7203       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7204         {
7205           if (parser->in_statement & IN_IF_STMT) 
7206             break;
7207           else
7208             {
7209               token = cp_lexer_consume_token (parser->lexer);
7210               error ("%H%<else%> without a previous %<if%>", &token->location);
7211             }
7212         }
7213
7214       /* Parse the statement.  */
7215       cp_parser_statement (parser, in_statement_expr, true, NULL);
7216     }
7217 }
7218
7219 /* Parse a selection-statement.
7220
7221    selection-statement:
7222      if ( condition ) statement
7223      if ( condition ) statement else statement
7224      switch ( condition ) statement
7225
7226    Returns the new IF_STMT or SWITCH_STMT.
7227
7228    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7229    is a (possibly labeled) if statement which is not enclosed in
7230    braces and has an else clause.  This is used to implement
7231    -Wparentheses.  */
7232
7233 static tree
7234 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7235 {
7236   cp_token *token;
7237   enum rid keyword;
7238
7239   if (if_p != NULL)
7240     *if_p = false;
7241
7242   /* Peek at the next token.  */
7243   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7244
7245   /* See what kind of keyword it is.  */
7246   keyword = token->keyword;
7247   switch (keyword)
7248     {
7249     case RID_IF:
7250     case RID_SWITCH:
7251       {
7252         tree statement;
7253         tree condition;
7254
7255         /* Look for the `('.  */
7256         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7257           {
7258             cp_parser_skip_to_end_of_statement (parser);
7259             return error_mark_node;
7260           }
7261
7262         /* Begin the selection-statement.  */
7263         if (keyword == RID_IF)
7264           statement = begin_if_stmt ();
7265         else
7266           statement = begin_switch_stmt ();
7267
7268         /* Parse the condition.  */
7269         condition = cp_parser_condition (parser);
7270         /* Look for the `)'.  */
7271         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7272           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7273                                                  /*consume_paren=*/true);
7274
7275         if (keyword == RID_IF)
7276           {
7277             bool nested_if;
7278             unsigned char in_statement;
7279
7280             /* Add the condition.  */
7281             finish_if_stmt_cond (condition, statement);
7282
7283             /* Parse the then-clause.  */
7284             in_statement = parser->in_statement;
7285             parser->in_statement |= IN_IF_STMT;
7286             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7287               {
7288                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7289                 add_stmt (build_empty_stmt ());
7290                 cp_lexer_consume_token (parser->lexer);
7291                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7292                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7293                               "empty body in an %<if%> statement");
7294                 nested_if = false;
7295               }
7296             else
7297               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7298             parser->in_statement = in_statement;
7299
7300             finish_then_clause (statement);
7301
7302             /* If the next token is `else', parse the else-clause.  */
7303             if (cp_lexer_next_token_is_keyword (parser->lexer,
7304                                                 RID_ELSE))
7305               {
7306                 /* Consume the `else' keyword.  */
7307                 cp_lexer_consume_token (parser->lexer);
7308                 begin_else_clause (statement);
7309                 /* Parse the else-clause.  */
7310                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7311                   {
7312                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7313                                 OPT_Wempty_body, "suggest braces around "
7314                                 "empty body in an %<else%> statement");
7315                     add_stmt (build_empty_stmt ());
7316                     cp_lexer_consume_token (parser->lexer);
7317                   }
7318                 else
7319                   cp_parser_implicitly_scoped_statement (parser, NULL);
7320
7321                 finish_else_clause (statement);
7322
7323                 /* If we are currently parsing a then-clause, then
7324                    IF_P will not be NULL.  We set it to true to
7325                    indicate that this if statement has an else clause.
7326                    This may trigger the Wparentheses warning below
7327                    when we get back up to the parent if statement.  */
7328                 if (if_p != NULL)
7329                   *if_p = true;
7330               }
7331             else
7332               {
7333                 /* This if statement does not have an else clause.  If
7334                    NESTED_IF is true, then the then-clause is an if
7335                    statement which does have an else clause.  We warn
7336                    about the potential ambiguity.  */
7337                 if (nested_if)
7338                   warning (OPT_Wparentheses,
7339                            ("%Hsuggest explicit braces "
7340                             "to avoid ambiguous %<else%>"),
7341                            EXPR_LOCUS (statement));
7342               }
7343
7344             /* Now we're all done with the if-statement.  */
7345             finish_if_stmt (statement);
7346           }
7347         else
7348           {
7349             bool in_switch_statement_p;
7350             unsigned char in_statement;
7351
7352             /* Add the condition.  */
7353             finish_switch_cond (condition, statement);
7354
7355             /* Parse the body of the switch-statement.  */
7356             in_switch_statement_p = parser->in_switch_statement_p;
7357             in_statement = parser->in_statement;
7358             parser->in_switch_statement_p = true;
7359             parser->in_statement |= IN_SWITCH_STMT;
7360             cp_parser_implicitly_scoped_statement (parser, NULL);
7361             parser->in_switch_statement_p = in_switch_statement_p;
7362             parser->in_statement = in_statement;
7363
7364             /* Now we're all done with the switch-statement.  */
7365             finish_switch_stmt (statement);
7366           }
7367
7368         return statement;
7369       }
7370       break;
7371
7372     default:
7373       cp_parser_error (parser, "expected selection-statement");
7374       return error_mark_node;
7375     }
7376 }
7377
7378 /* Parse a condition.
7379
7380    condition:
7381      expression
7382      type-specifier-seq declarator = initializer-clause
7383      type-specifier-seq declarator braced-init-list
7384
7385    GNU Extension:
7386
7387    condition:
7388      type-specifier-seq declarator asm-specification [opt]
7389        attributes [opt] = assignment-expression
7390
7391    Returns the expression that should be tested.  */
7392
7393 static tree
7394 cp_parser_condition (cp_parser* parser)
7395 {
7396   cp_decl_specifier_seq type_specifiers;
7397   const char *saved_message;
7398
7399   /* Try the declaration first.  */
7400   cp_parser_parse_tentatively (parser);
7401   /* New types are not allowed in the type-specifier-seq for a
7402      condition.  */
7403   saved_message = parser->type_definition_forbidden_message;
7404   parser->type_definition_forbidden_message
7405     = "types may not be defined in conditions";
7406   /* Parse the type-specifier-seq.  */
7407   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7408                                 &type_specifiers);
7409   /* Restore the saved message.  */
7410   parser->type_definition_forbidden_message = saved_message;
7411   /* If all is well, we might be looking at a declaration.  */
7412   if (!cp_parser_error_occurred (parser))
7413     {
7414       tree decl;
7415       tree asm_specification;
7416       tree attributes;
7417       cp_declarator *declarator;
7418       tree initializer = NULL_TREE;
7419
7420       /* Parse the declarator.  */
7421       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7422                                          /*ctor_dtor_or_conv_p=*/NULL,
7423                                          /*parenthesized_p=*/NULL,
7424                                          /*member_p=*/false);
7425       /* Parse the attributes.  */
7426       attributes = cp_parser_attributes_opt (parser);
7427       /* Parse the asm-specification.  */
7428       asm_specification = cp_parser_asm_specification_opt (parser);
7429       /* If the next token is not an `=' or '{', then we might still be
7430          looking at an expression.  For example:
7431
7432            if (A(a).x)
7433
7434          looks like a decl-specifier-seq and a declarator -- but then
7435          there is no `=', so this is an expression.  */
7436       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7437           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7438         cp_parser_simulate_error (parser);
7439         
7440       /* If we did see an `=' or '{', then we are looking at a declaration
7441          for sure.  */
7442       if (cp_parser_parse_definitely (parser))
7443         {
7444           tree pushed_scope;
7445           bool non_constant_p;
7446           bool flags = LOOKUP_ONLYCONVERTING;
7447
7448           /* Create the declaration.  */
7449           decl = start_decl (declarator, &type_specifiers,
7450                              /*initialized_p=*/true,
7451                              attributes, /*prefix_attributes=*/NULL_TREE,
7452                              &pushed_scope);
7453
7454           /* Parse the initializer.  */
7455           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7456             {
7457               initializer = cp_parser_braced_list (parser, &non_constant_p);
7458               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7459               flags = 0;
7460             }
7461           else
7462             {
7463               /* Consume the `='.  */
7464               cp_parser_require (parser, CPP_EQ, "%<=%>");
7465               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7466             }
7467           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7468             maybe_warn_cpp0x ("extended initializer lists");
7469
7470           if (!non_constant_p)
7471             initializer = fold_non_dependent_expr (initializer);
7472
7473           /* Process the initializer.  */
7474           cp_finish_decl (decl,
7475                           initializer, !non_constant_p,
7476                           asm_specification,
7477                           flags);
7478
7479           if (pushed_scope)
7480             pop_scope (pushed_scope);
7481
7482           return convert_from_reference (decl);
7483         }
7484     }
7485   /* If we didn't even get past the declarator successfully, we are
7486      definitely not looking at a declaration.  */
7487   else
7488     cp_parser_abort_tentative_parse (parser);
7489
7490   /* Otherwise, we are looking at an expression.  */
7491   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7492 }
7493
7494 /* Parse an iteration-statement.
7495
7496    iteration-statement:
7497      while ( condition ) statement
7498      do statement while ( expression ) ;
7499      for ( for-init-statement condition [opt] ; expression [opt] )
7500        statement
7501
7502    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7503
7504 static tree
7505 cp_parser_iteration_statement (cp_parser* parser)
7506 {
7507   cp_token *token;
7508   enum rid keyword;
7509   tree statement;
7510   unsigned char in_statement;
7511
7512   /* Peek at the next token.  */
7513   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7514   if (!token)
7515     return error_mark_node;
7516
7517   /* Remember whether or not we are already within an iteration
7518      statement.  */
7519   in_statement = parser->in_statement;
7520
7521   /* See what kind of keyword it is.  */
7522   keyword = token->keyword;
7523   switch (keyword)
7524     {
7525     case RID_WHILE:
7526       {
7527         tree condition;
7528
7529         /* Begin the while-statement.  */
7530         statement = begin_while_stmt ();
7531         /* Look for the `('.  */
7532         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7533         /* Parse the condition.  */
7534         condition = cp_parser_condition (parser);
7535         finish_while_stmt_cond (condition, statement);
7536         /* Look for the `)'.  */
7537         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7538         /* Parse the dependent statement.  */
7539         parser->in_statement = IN_ITERATION_STMT;
7540         cp_parser_already_scoped_statement (parser);
7541         parser->in_statement = in_statement;
7542         /* We're done with the while-statement.  */
7543         finish_while_stmt (statement);
7544       }
7545       break;
7546
7547     case RID_DO:
7548       {
7549         tree expression;
7550
7551         /* Begin the do-statement.  */
7552         statement = begin_do_stmt ();
7553         /* Parse the body of the do-statement.  */
7554         parser->in_statement = IN_ITERATION_STMT;
7555         cp_parser_implicitly_scoped_statement (parser, NULL);
7556         parser->in_statement = in_statement;
7557         finish_do_body (statement);
7558         /* Look for the `while' keyword.  */
7559         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7560         /* Look for the `('.  */
7561         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7562         /* Parse the expression.  */
7563         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7564         /* We're done with the do-statement.  */
7565         finish_do_stmt (expression, statement);
7566         /* Look for the `)'.  */
7567         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7568         /* Look for the `;'.  */
7569         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7570       }
7571       break;
7572
7573     case RID_FOR:
7574       {
7575         tree condition = NULL_TREE;
7576         tree expression = NULL_TREE;
7577
7578         /* Begin the for-statement.  */
7579         statement = begin_for_stmt ();
7580         /* Look for the `('.  */
7581         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7582         /* Parse the initialization.  */
7583         cp_parser_for_init_statement (parser);
7584         finish_for_init_stmt (statement);
7585
7586         /* If there's a condition, process it.  */
7587         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7588           condition = cp_parser_condition (parser);
7589         finish_for_cond (condition, statement);
7590         /* Look for the `;'.  */
7591         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7592
7593         /* If there's an expression, process it.  */
7594         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7595           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7596         finish_for_expr (expression, statement);
7597         /* Look for the `)'.  */
7598         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7599
7600         /* Parse the body of the for-statement.  */
7601         parser->in_statement = IN_ITERATION_STMT;
7602         cp_parser_already_scoped_statement (parser);
7603         parser->in_statement = in_statement;
7604
7605         /* We're done with the for-statement.  */
7606         finish_for_stmt (statement);
7607       }
7608       break;
7609
7610     default:
7611       cp_parser_error (parser, "expected iteration-statement");
7612       statement = error_mark_node;
7613       break;
7614     }
7615
7616   return statement;
7617 }
7618
7619 /* Parse a for-init-statement.
7620
7621    for-init-statement:
7622      expression-statement
7623      simple-declaration  */
7624
7625 static void
7626 cp_parser_for_init_statement (cp_parser* parser)
7627 {
7628   /* If the next token is a `;', then we have an empty
7629      expression-statement.  Grammatically, this is also a
7630      simple-declaration, but an invalid one, because it does not
7631      declare anything.  Therefore, if we did not handle this case
7632      specially, we would issue an error message about an invalid
7633      declaration.  */
7634   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7635     {
7636       /* We're going to speculatively look for a declaration, falling back
7637          to an expression, if necessary.  */
7638       cp_parser_parse_tentatively (parser);
7639       /* Parse the declaration.  */
7640       cp_parser_simple_declaration (parser,
7641                                     /*function_definition_allowed_p=*/false);
7642       /* If the tentative parse failed, then we shall need to look for an
7643          expression-statement.  */
7644       if (cp_parser_parse_definitely (parser))
7645         return;
7646     }
7647
7648   cp_parser_expression_statement (parser, false);
7649 }
7650
7651 /* Parse a jump-statement.
7652
7653    jump-statement:
7654      break ;
7655      continue ;
7656      return expression [opt] ;
7657      return braced-init-list ;
7658      goto identifier ;
7659
7660    GNU extension:
7661
7662    jump-statement:
7663      goto * expression ;
7664
7665    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7666
7667 static tree
7668 cp_parser_jump_statement (cp_parser* parser)
7669 {
7670   tree statement = error_mark_node;
7671   cp_token *token;
7672   enum rid keyword;
7673   unsigned char in_statement;
7674
7675   /* Peek at the next token.  */
7676   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7677   if (!token)
7678     return error_mark_node;
7679
7680   /* See what kind of keyword it is.  */
7681   keyword = token->keyword;
7682   switch (keyword)
7683     {
7684     case RID_BREAK:
7685       in_statement = parser->in_statement & ~IN_IF_STMT;      
7686       switch (in_statement)
7687         {
7688         case 0:
7689           error ("%Hbreak statement not within loop or switch", &token->location);
7690           break;
7691         default:
7692           gcc_assert ((in_statement & IN_SWITCH_STMT)
7693                       || in_statement == IN_ITERATION_STMT);
7694           statement = finish_break_stmt ();
7695           break;
7696         case IN_OMP_BLOCK:
7697           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7698           break;
7699         case IN_OMP_FOR:
7700           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7701           break;
7702         }
7703       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7704       break;
7705
7706     case RID_CONTINUE:
7707       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7708         {
7709         case 0:
7710           error ("%Hcontinue statement not within a loop", &token->location);
7711           break;
7712         case IN_ITERATION_STMT:
7713         case IN_OMP_FOR:
7714           statement = finish_continue_stmt ();
7715           break;
7716         case IN_OMP_BLOCK:
7717           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7718           break;
7719         default:
7720           gcc_unreachable ();
7721         }
7722       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7723       break;
7724
7725     case RID_RETURN:
7726       {
7727         tree expr;
7728         bool expr_non_constant_p;
7729
7730         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7731           {
7732             maybe_warn_cpp0x ("extended initializer lists");
7733             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7734           }
7735         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7736           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7737         else
7738           /* If the next token is a `;', then there is no
7739              expression.  */
7740           expr = NULL_TREE;
7741         /* Build the return-statement.  */
7742         statement = finish_return_stmt (expr);
7743         /* Look for the final `;'.  */
7744         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7745       }
7746       break;
7747
7748     case RID_GOTO:
7749       /* Create the goto-statement.  */
7750       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7751         {
7752           /* Issue a warning about this use of a GNU extension.  */
7753           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7754           /* Consume the '*' token.  */
7755           cp_lexer_consume_token (parser->lexer);
7756           /* Parse the dependent expression.  */
7757           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7758         }
7759       else
7760         finish_goto_stmt (cp_parser_identifier (parser));
7761       /* Look for the final `;'.  */
7762       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7763       break;
7764
7765     default:
7766       cp_parser_error (parser, "expected jump-statement");
7767       break;
7768     }
7769
7770   return statement;
7771 }
7772
7773 /* Parse a declaration-statement.
7774
7775    declaration-statement:
7776      block-declaration  */
7777
7778 static void
7779 cp_parser_declaration_statement (cp_parser* parser)
7780 {
7781   void *p;
7782
7783   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7784   p = obstack_alloc (&declarator_obstack, 0);
7785
7786  /* Parse the block-declaration.  */
7787   cp_parser_block_declaration (parser, /*statement_p=*/true);
7788
7789   /* Free any declarators allocated.  */
7790   obstack_free (&declarator_obstack, p);
7791
7792   /* Finish off the statement.  */
7793   finish_stmt ();
7794 }
7795
7796 /* Some dependent statements (like `if (cond) statement'), are
7797    implicitly in their own scope.  In other words, if the statement is
7798    a single statement (as opposed to a compound-statement), it is
7799    none-the-less treated as if it were enclosed in braces.  Any
7800    declarations appearing in the dependent statement are out of scope
7801    after control passes that point.  This function parses a statement,
7802    but ensures that is in its own scope, even if it is not a
7803    compound-statement.
7804
7805    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7806    is a (possibly labeled) if statement which is not enclosed in
7807    braces and has an else clause.  This is used to implement
7808    -Wparentheses.
7809
7810    Returns the new statement.  */
7811
7812 static tree
7813 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7814 {
7815   tree statement;
7816
7817   if (if_p != NULL)
7818     *if_p = false;
7819
7820   /* Mark if () ; with a special NOP_EXPR.  */
7821   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7822     {
7823       cp_lexer_consume_token (parser->lexer);
7824       statement = add_stmt (build_empty_stmt ());
7825     }
7826   /* if a compound is opened, we simply parse the statement directly.  */
7827   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7828     statement = cp_parser_compound_statement (parser, NULL, false);
7829   /* If the token is not a `{', then we must take special action.  */
7830   else
7831     {
7832       /* Create a compound-statement.  */
7833       statement = begin_compound_stmt (0);
7834       /* Parse the dependent-statement.  */
7835       cp_parser_statement (parser, NULL_TREE, false, if_p);
7836       /* Finish the dummy compound-statement.  */
7837       finish_compound_stmt (statement);
7838     }
7839
7840   /* Return the statement.  */
7841   return statement;
7842 }
7843
7844 /* For some dependent statements (like `while (cond) statement'), we
7845    have already created a scope.  Therefore, even if the dependent
7846    statement is a compound-statement, we do not want to create another
7847    scope.  */
7848
7849 static void
7850 cp_parser_already_scoped_statement (cp_parser* parser)
7851 {
7852   /* If the token is a `{', then we must take special action.  */
7853   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7854     cp_parser_statement (parser, NULL_TREE, false, NULL);
7855   else
7856     {
7857       /* Avoid calling cp_parser_compound_statement, so that we
7858          don't create a new scope.  Do everything else by hand.  */
7859       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7860       /* If the next keyword is `__label__' we have a label declaration.  */
7861       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7862         cp_parser_label_declaration (parser);
7863       /* Parse an (optional) statement-seq.  */
7864       cp_parser_statement_seq_opt (parser, NULL_TREE);
7865       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7866     }
7867 }
7868
7869 /* Declarations [gram.dcl.dcl] */
7870
7871 /* Parse an optional declaration-sequence.
7872
7873    declaration-seq:
7874      declaration
7875      declaration-seq declaration  */
7876
7877 static void
7878 cp_parser_declaration_seq_opt (cp_parser* parser)
7879 {
7880   while (true)
7881     {
7882       cp_token *token;
7883
7884       token = cp_lexer_peek_token (parser->lexer);
7885
7886       if (token->type == CPP_CLOSE_BRACE
7887           || token->type == CPP_EOF
7888           || token->type == CPP_PRAGMA_EOL)
7889         break;
7890
7891       if (token->type == CPP_SEMICOLON)
7892         {
7893           /* A declaration consisting of a single semicolon is
7894              invalid.  Allow it unless we're being pedantic.  */
7895           cp_lexer_consume_token (parser->lexer);
7896           if (!in_system_header)
7897             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7898           continue;
7899         }
7900
7901       /* If we're entering or exiting a region that's implicitly
7902          extern "C", modify the lang context appropriately.  */
7903       if (!parser->implicit_extern_c && token->implicit_extern_c)
7904         {
7905           push_lang_context (lang_name_c);
7906           parser->implicit_extern_c = true;
7907         }
7908       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7909         {
7910           pop_lang_context ();
7911           parser->implicit_extern_c = false;
7912         }
7913
7914       if (token->type == CPP_PRAGMA)
7915         {
7916           /* A top-level declaration can consist solely of a #pragma.
7917              A nested declaration cannot, so this is done here and not
7918              in cp_parser_declaration.  (A #pragma at block scope is
7919              handled in cp_parser_statement.)  */
7920           cp_parser_pragma (parser, pragma_external);
7921           continue;
7922         }
7923
7924       /* Parse the declaration itself.  */
7925       cp_parser_declaration (parser);
7926     }
7927 }
7928
7929 /* Parse a declaration.
7930
7931    declaration:
7932      block-declaration
7933      function-definition
7934      template-declaration
7935      explicit-instantiation
7936      explicit-specialization
7937      linkage-specification
7938      namespace-definition
7939
7940    GNU extension:
7941
7942    declaration:
7943       __extension__ declaration */
7944
7945 static void
7946 cp_parser_declaration (cp_parser* parser)
7947 {
7948   cp_token token1;
7949   cp_token token2;
7950   int saved_pedantic;
7951   void *p;
7952
7953   /* Check for the `__extension__' keyword.  */
7954   if (cp_parser_extension_opt (parser, &saved_pedantic))
7955     {
7956       /* Parse the qualified declaration.  */
7957       cp_parser_declaration (parser);
7958       /* Restore the PEDANTIC flag.  */
7959       pedantic = saved_pedantic;
7960
7961       return;
7962     }
7963
7964   /* Try to figure out what kind of declaration is present.  */
7965   token1 = *cp_lexer_peek_token (parser->lexer);
7966
7967   if (token1.type != CPP_EOF)
7968     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7969   else
7970     {
7971       token2.type = CPP_EOF;
7972       token2.keyword = RID_MAX;
7973     }
7974
7975   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7976   p = obstack_alloc (&declarator_obstack, 0);
7977
7978   /* If the next token is `extern' and the following token is a string
7979      literal, then we have a linkage specification.  */
7980   if (token1.keyword == RID_EXTERN
7981       && cp_parser_is_string_literal (&token2))
7982     cp_parser_linkage_specification (parser);
7983   /* If the next token is `template', then we have either a template
7984      declaration, an explicit instantiation, or an explicit
7985      specialization.  */
7986   else if (token1.keyword == RID_TEMPLATE)
7987     {
7988       /* `template <>' indicates a template specialization.  */
7989       if (token2.type == CPP_LESS
7990           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7991         cp_parser_explicit_specialization (parser);
7992       /* `template <' indicates a template declaration.  */
7993       else if (token2.type == CPP_LESS)
7994         cp_parser_template_declaration (parser, /*member_p=*/false);
7995       /* Anything else must be an explicit instantiation.  */
7996       else
7997         cp_parser_explicit_instantiation (parser);
7998     }
7999   /* If the next token is `export', then we have a template
8000      declaration.  */
8001   else if (token1.keyword == RID_EXPORT)
8002     cp_parser_template_declaration (parser, /*member_p=*/false);
8003   /* If the next token is `extern', 'static' or 'inline' and the one
8004      after that is `template', we have a GNU extended explicit
8005      instantiation directive.  */
8006   else if (cp_parser_allow_gnu_extensions_p (parser)
8007            && (token1.keyword == RID_EXTERN
8008                || token1.keyword == RID_STATIC
8009                || token1.keyword == RID_INLINE)
8010            && token2.keyword == RID_TEMPLATE)
8011     cp_parser_explicit_instantiation (parser);
8012   /* If the next token is `namespace', check for a named or unnamed
8013      namespace definition.  */
8014   else if (token1.keyword == RID_NAMESPACE
8015            && (/* A named namespace definition.  */
8016                (token2.type == CPP_NAME
8017                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8018                     != CPP_EQ))
8019                /* An unnamed namespace definition.  */
8020                || token2.type == CPP_OPEN_BRACE
8021                || token2.keyword == RID_ATTRIBUTE))
8022     cp_parser_namespace_definition (parser);
8023   /* An inline (associated) namespace definition.  */
8024   else if (token1.keyword == RID_INLINE
8025            && token2.keyword == RID_NAMESPACE)
8026     cp_parser_namespace_definition (parser);
8027   /* Objective-C++ declaration/definition.  */
8028   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8029     cp_parser_objc_declaration (parser);
8030   /* We must have either a block declaration or a function
8031      definition.  */
8032   else
8033     /* Try to parse a block-declaration, or a function-definition.  */
8034     cp_parser_block_declaration (parser, /*statement_p=*/false);
8035
8036   /* Free any declarators allocated.  */
8037   obstack_free (&declarator_obstack, p);
8038 }
8039
8040 /* Parse a block-declaration.
8041
8042    block-declaration:
8043      simple-declaration
8044      asm-definition
8045      namespace-alias-definition
8046      using-declaration
8047      using-directive
8048
8049    GNU Extension:
8050
8051    block-declaration:
8052      __extension__ block-declaration
8053
8054    C++0x Extension:
8055
8056    block-declaration:
8057      static_assert-declaration
8058
8059    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8060    part of a declaration-statement.  */
8061
8062 static void
8063 cp_parser_block_declaration (cp_parser *parser,
8064                              bool      statement_p)
8065 {
8066   cp_token *token1;
8067   int saved_pedantic;
8068
8069   /* Check for the `__extension__' keyword.  */
8070   if (cp_parser_extension_opt (parser, &saved_pedantic))
8071     {
8072       /* Parse the qualified declaration.  */
8073       cp_parser_block_declaration (parser, statement_p);
8074       /* Restore the PEDANTIC flag.  */
8075       pedantic = saved_pedantic;
8076
8077       return;
8078     }
8079
8080   /* Peek at the next token to figure out which kind of declaration is
8081      present.  */
8082   token1 = cp_lexer_peek_token (parser->lexer);
8083
8084   /* If the next keyword is `asm', we have an asm-definition.  */
8085   if (token1->keyword == RID_ASM)
8086     {
8087       if (statement_p)
8088         cp_parser_commit_to_tentative_parse (parser);
8089       cp_parser_asm_definition (parser);
8090     }
8091   /* If the next keyword is `namespace', we have a
8092      namespace-alias-definition.  */
8093   else if (token1->keyword == RID_NAMESPACE)
8094     cp_parser_namespace_alias_definition (parser);
8095   /* If the next keyword is `using', we have either a
8096      using-declaration or a using-directive.  */
8097   else if (token1->keyword == RID_USING)
8098     {
8099       cp_token *token2;
8100
8101       if (statement_p)
8102         cp_parser_commit_to_tentative_parse (parser);
8103       /* If the token after `using' is `namespace', then we have a
8104          using-directive.  */
8105       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8106       if (token2->keyword == RID_NAMESPACE)
8107         cp_parser_using_directive (parser);
8108       /* Otherwise, it's a using-declaration.  */
8109       else
8110         cp_parser_using_declaration (parser,
8111                                      /*access_declaration_p=*/false);
8112     }
8113   /* If the next keyword is `__label__' we have a misplaced label
8114      declaration.  */
8115   else if (token1->keyword == RID_LABEL)
8116     {
8117       cp_lexer_consume_token (parser->lexer);
8118       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8119       cp_parser_skip_to_end_of_statement (parser);
8120       /* If the next token is now a `;', consume it.  */
8121       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8122         cp_lexer_consume_token (parser->lexer);
8123     }
8124   /* If the next token is `static_assert' we have a static assertion.  */
8125   else if (token1->keyword == RID_STATIC_ASSERT)
8126     cp_parser_static_assert (parser, /*member_p=*/false);
8127   /* Anything else must be a simple-declaration.  */
8128   else
8129     cp_parser_simple_declaration (parser, !statement_p);
8130 }
8131
8132 /* Parse a simple-declaration.
8133
8134    simple-declaration:
8135      decl-specifier-seq [opt] init-declarator-list [opt] ;
8136
8137    init-declarator-list:
8138      init-declarator
8139      init-declarator-list , init-declarator
8140
8141    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8142    function-definition as a simple-declaration.  */
8143
8144 static void
8145 cp_parser_simple_declaration (cp_parser* parser,
8146                               bool function_definition_allowed_p)
8147 {
8148   cp_decl_specifier_seq decl_specifiers;
8149   int declares_class_or_enum;
8150   bool saw_declarator;
8151
8152   /* Defer access checks until we know what is being declared; the
8153      checks for names appearing in the decl-specifier-seq should be
8154      done as if we were in the scope of the thing being declared.  */
8155   push_deferring_access_checks (dk_deferred);
8156
8157   /* Parse the decl-specifier-seq.  We have to keep track of whether
8158      or not the decl-specifier-seq declares a named class or
8159      enumeration type, since that is the only case in which the
8160      init-declarator-list is allowed to be empty.
8161
8162      [dcl.dcl]
8163
8164      In a simple-declaration, the optional init-declarator-list can be
8165      omitted only when declaring a class or enumeration, that is when
8166      the decl-specifier-seq contains either a class-specifier, an
8167      elaborated-type-specifier, or an enum-specifier.  */
8168   cp_parser_decl_specifier_seq (parser,
8169                                 CP_PARSER_FLAGS_OPTIONAL,
8170                                 &decl_specifiers,
8171                                 &declares_class_or_enum);
8172   /* We no longer need to defer access checks.  */
8173   stop_deferring_access_checks ();
8174
8175   /* In a block scope, a valid declaration must always have a
8176      decl-specifier-seq.  By not trying to parse declarators, we can
8177      resolve the declaration/expression ambiguity more quickly.  */
8178   if (!function_definition_allowed_p
8179       && !decl_specifiers.any_specifiers_p)
8180     {
8181       cp_parser_error (parser, "expected declaration");
8182       goto done;
8183     }
8184
8185   /* If the next two tokens are both identifiers, the code is
8186      erroneous. The usual cause of this situation is code like:
8187
8188        T t;
8189
8190      where "T" should name a type -- but does not.  */
8191   if (!decl_specifiers.type
8192       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8193     {
8194       /* If parsing tentatively, we should commit; we really are
8195          looking at a declaration.  */
8196       cp_parser_commit_to_tentative_parse (parser);
8197       /* Give up.  */
8198       goto done;
8199     }
8200
8201   /* If we have seen at least one decl-specifier, and the next token
8202      is not a parenthesis, then we must be looking at a declaration.
8203      (After "int (" we might be looking at a functional cast.)  */
8204   if (decl_specifiers.any_specifiers_p
8205       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8206       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8207       && !cp_parser_error_occurred (parser))
8208     cp_parser_commit_to_tentative_parse (parser);
8209
8210   /* Keep going until we hit the `;' at the end of the simple
8211      declaration.  */
8212   saw_declarator = false;
8213   while (cp_lexer_next_token_is_not (parser->lexer,
8214                                      CPP_SEMICOLON))
8215     {
8216       cp_token *token;
8217       bool function_definition_p;
8218       tree decl;
8219
8220       if (saw_declarator)
8221         {
8222           /* If we are processing next declarator, coma is expected */
8223           token = cp_lexer_peek_token (parser->lexer);
8224           gcc_assert (token->type == CPP_COMMA);
8225           cp_lexer_consume_token (parser->lexer);
8226         }
8227       else
8228         saw_declarator = true;
8229
8230       /* Parse the init-declarator.  */
8231       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8232                                         /*checks=*/NULL,
8233                                         function_definition_allowed_p,
8234                                         /*member_p=*/false,
8235                                         declares_class_or_enum,
8236                                         &function_definition_p);
8237       /* If an error occurred while parsing tentatively, exit quickly.
8238          (That usually happens when in the body of a function; each
8239          statement is treated as a declaration-statement until proven
8240          otherwise.)  */
8241       if (cp_parser_error_occurred (parser))
8242         goto done;
8243       /* Handle function definitions specially.  */
8244       if (function_definition_p)
8245         {
8246           /* If the next token is a `,', then we are probably
8247              processing something like:
8248
8249                void f() {}, *p;
8250
8251              which is erroneous.  */
8252           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8253             {
8254               cp_token *token = cp_lexer_peek_token (parser->lexer);
8255               error ("%Hmixing declarations and function-definitions is forbidden",
8256                      &token->location);
8257             }
8258           /* Otherwise, we're done with the list of declarators.  */
8259           else
8260             {
8261               pop_deferring_access_checks ();
8262               return;
8263             }
8264         }
8265       /* The next token should be either a `,' or a `;'.  */
8266       token = cp_lexer_peek_token (parser->lexer);
8267       /* If it's a `,', there are more declarators to come.  */
8268       if (token->type == CPP_COMMA)
8269         /* will be consumed next time around */;
8270       /* If it's a `;', we are done.  */
8271       else if (token->type == CPP_SEMICOLON)
8272         break;
8273       /* Anything else is an error.  */
8274       else
8275         {
8276           /* If we have already issued an error message we don't need
8277              to issue another one.  */
8278           if (decl != error_mark_node
8279               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8280             cp_parser_error (parser, "expected %<,%> or %<;%>");
8281           /* Skip tokens until we reach the end of the statement.  */
8282           cp_parser_skip_to_end_of_statement (parser);
8283           /* If the next token is now a `;', consume it.  */
8284           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8285             cp_lexer_consume_token (parser->lexer);
8286           goto done;
8287         }
8288       /* After the first time around, a function-definition is not
8289          allowed -- even if it was OK at first.  For example:
8290
8291            int i, f() {}
8292
8293          is not valid.  */
8294       function_definition_allowed_p = false;
8295     }
8296
8297   /* Issue an error message if no declarators are present, and the
8298      decl-specifier-seq does not itself declare a class or
8299      enumeration.  */
8300   if (!saw_declarator)
8301     {
8302       if (cp_parser_declares_only_class_p (parser))
8303         shadow_tag (&decl_specifiers);
8304       /* Perform any deferred access checks.  */
8305       perform_deferred_access_checks ();
8306     }
8307
8308   /* Consume the `;'.  */
8309   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8310
8311  done:
8312   pop_deferring_access_checks ();
8313 }
8314
8315 /* Parse a decl-specifier-seq.
8316
8317    decl-specifier-seq:
8318      decl-specifier-seq [opt] decl-specifier
8319
8320    decl-specifier:
8321      storage-class-specifier
8322      type-specifier
8323      function-specifier
8324      friend
8325      typedef
8326
8327    GNU Extension:
8328
8329    decl-specifier:
8330      attributes
8331
8332    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8333
8334    The parser flags FLAGS is used to control type-specifier parsing.
8335
8336    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8337    flags:
8338
8339      1: one of the decl-specifiers is an elaborated-type-specifier
8340         (i.e., a type declaration)
8341      2: one of the decl-specifiers is an enum-specifier or a
8342         class-specifier (i.e., a type definition)
8343
8344    */
8345
8346 static void
8347 cp_parser_decl_specifier_seq (cp_parser* parser,
8348                               cp_parser_flags flags,
8349                               cp_decl_specifier_seq *decl_specs,
8350                               int* declares_class_or_enum)
8351 {
8352   bool constructor_possible_p = !parser->in_declarator_p;
8353   cp_token *start_token = NULL;
8354
8355   /* Clear DECL_SPECS.  */
8356   clear_decl_specs (decl_specs);
8357
8358   /* Assume no class or enumeration type is declared.  */
8359   *declares_class_or_enum = 0;
8360
8361   /* Keep reading specifiers until there are no more to read.  */
8362   while (true)
8363     {
8364       bool constructor_p;
8365       bool found_decl_spec;
8366       cp_token *token;
8367
8368       /* Peek at the next token.  */
8369       token = cp_lexer_peek_token (parser->lexer);
8370
8371       /* Save the first token of the decl spec list for error
8372          reporting.  */
8373       if (!start_token)
8374         start_token = token;
8375       /* Handle attributes.  */
8376       if (token->keyword == RID_ATTRIBUTE)
8377         {
8378           /* Parse the attributes.  */
8379           decl_specs->attributes
8380             = chainon (decl_specs->attributes,
8381                        cp_parser_attributes_opt (parser));
8382           continue;
8383         }
8384       /* Assume we will find a decl-specifier keyword.  */
8385       found_decl_spec = true;
8386       /* If the next token is an appropriate keyword, we can simply
8387          add it to the list.  */
8388       switch (token->keyword)
8389         {
8390           /* decl-specifier:
8391                friend  */
8392         case RID_FRIEND:
8393           if (!at_class_scope_p ())
8394             {
8395               error ("%H%<friend%> used outside of class", &token->location);
8396               cp_lexer_purge_token (parser->lexer);
8397             }
8398           else
8399             {
8400               ++decl_specs->specs[(int) ds_friend];
8401               /* Consume the token.  */
8402               cp_lexer_consume_token (parser->lexer);
8403             }
8404           break;
8405
8406           /* function-specifier:
8407                inline
8408                virtual
8409                explicit  */
8410         case RID_INLINE:
8411         case RID_VIRTUAL:
8412         case RID_EXPLICIT:
8413           cp_parser_function_specifier_opt (parser, decl_specs);
8414           break;
8415
8416           /* decl-specifier:
8417                typedef  */
8418         case RID_TYPEDEF:
8419           ++decl_specs->specs[(int) ds_typedef];
8420           /* Consume the token.  */
8421           cp_lexer_consume_token (parser->lexer);
8422           /* A constructor declarator cannot appear in a typedef.  */
8423           constructor_possible_p = false;
8424           /* The "typedef" keyword can only occur in a declaration; we
8425              may as well commit at this point.  */
8426           cp_parser_commit_to_tentative_parse (parser);
8427
8428           if (decl_specs->storage_class != sc_none)
8429             decl_specs->conflicting_specifiers_p = true;
8430           break;
8431
8432           /* storage-class-specifier:
8433                auto
8434                register
8435                static
8436                extern
8437                mutable
8438
8439              GNU Extension:
8440                thread  */
8441         case RID_AUTO:
8442           if (cxx_dialect == cxx98) 
8443             {
8444               /* Consume the token.  */
8445               cp_lexer_consume_token (parser->lexer);
8446
8447               /* Complain about `auto' as a storage specifier, if
8448                  we're complaining about C++0x compatibility.  */
8449               warning 
8450                 (OPT_Wc__0x_compat, 
8451                  "%H%<auto%> will change meaning in C++0x; please remove it",
8452                  &token->location);
8453
8454               /* Set the storage class anyway.  */
8455               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8456                                            token->location);
8457             }
8458           else
8459             /* C++0x auto type-specifier.  */
8460             found_decl_spec = false;
8461           break;
8462
8463         case RID_REGISTER:
8464         case RID_STATIC:
8465         case RID_EXTERN:
8466         case RID_MUTABLE:
8467           /* Consume the token.  */
8468           cp_lexer_consume_token (parser->lexer);
8469           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8470                                        token->location);
8471           break;
8472         case RID_THREAD:
8473           /* Consume the token.  */
8474           cp_lexer_consume_token (parser->lexer);
8475           ++decl_specs->specs[(int) ds_thread];
8476           break;
8477
8478         default:
8479           /* We did not yet find a decl-specifier yet.  */
8480           found_decl_spec = false;
8481           break;
8482         }
8483
8484       /* Constructors are a special case.  The `S' in `S()' is not a
8485          decl-specifier; it is the beginning of the declarator.  */
8486       constructor_p
8487         = (!found_decl_spec
8488            && constructor_possible_p
8489            && (cp_parser_constructor_declarator_p
8490                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8491
8492       /* If we don't have a DECL_SPEC yet, then we must be looking at
8493          a type-specifier.  */
8494       if (!found_decl_spec && !constructor_p)
8495         {
8496           int decl_spec_declares_class_or_enum;
8497           bool is_cv_qualifier;
8498           tree type_spec;
8499
8500           type_spec
8501             = cp_parser_type_specifier (parser, flags,
8502                                         decl_specs,
8503                                         /*is_declaration=*/true,
8504                                         &decl_spec_declares_class_or_enum,
8505                                         &is_cv_qualifier);
8506           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8507
8508           /* If this type-specifier referenced a user-defined type
8509              (a typedef, class-name, etc.), then we can't allow any
8510              more such type-specifiers henceforth.
8511
8512              [dcl.spec]
8513
8514              The longest sequence of decl-specifiers that could
8515              possibly be a type name is taken as the
8516              decl-specifier-seq of a declaration.  The sequence shall
8517              be self-consistent as described below.
8518
8519              [dcl.type]
8520
8521              As a general rule, at most one type-specifier is allowed
8522              in the complete decl-specifier-seq of a declaration.  The
8523              only exceptions are the following:
8524
8525              -- const or volatile can be combined with any other
8526                 type-specifier.
8527
8528              -- signed or unsigned can be combined with char, long,
8529                 short, or int.
8530
8531              -- ..
8532
8533              Example:
8534
8535                typedef char* Pc;
8536                void g (const int Pc);
8537
8538              Here, Pc is *not* part of the decl-specifier seq; it's
8539              the declarator.  Therefore, once we see a type-specifier
8540              (other than a cv-qualifier), we forbid any additional
8541              user-defined types.  We *do* still allow things like `int
8542              int' to be considered a decl-specifier-seq, and issue the
8543              error message later.  */
8544           if (type_spec && !is_cv_qualifier)
8545             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8546           /* A constructor declarator cannot follow a type-specifier.  */
8547           if (type_spec)
8548             {
8549               constructor_possible_p = false;
8550               found_decl_spec = true;
8551             }
8552         }
8553
8554       /* If we still do not have a DECL_SPEC, then there are no more
8555          decl-specifiers.  */
8556       if (!found_decl_spec)
8557         break;
8558
8559       decl_specs->any_specifiers_p = true;
8560       /* After we see one decl-specifier, further decl-specifiers are
8561          always optional.  */
8562       flags |= CP_PARSER_FLAGS_OPTIONAL;
8563     }
8564
8565   cp_parser_check_decl_spec (decl_specs, start_token->location);
8566
8567   /* Don't allow a friend specifier with a class definition.  */
8568   if (decl_specs->specs[(int) ds_friend] != 0
8569       && (*declares_class_or_enum & 2))
8570     error ("%Hclass definition may not be declared a friend",
8571             &start_token->location);
8572 }
8573
8574 /* Parse an (optional) storage-class-specifier.
8575
8576    storage-class-specifier:
8577      auto
8578      register
8579      static
8580      extern
8581      mutable
8582
8583    GNU Extension:
8584
8585    storage-class-specifier:
8586      thread
8587
8588    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8589
8590 static tree
8591 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8592 {
8593   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8594     {
8595     case RID_AUTO:
8596       if (cxx_dialect != cxx98)
8597         return NULL_TREE;
8598       /* Fall through for C++98.  */
8599
8600     case RID_REGISTER:
8601     case RID_STATIC:
8602     case RID_EXTERN:
8603     case RID_MUTABLE:
8604     case RID_THREAD:
8605       /* Consume the token.  */
8606       return cp_lexer_consume_token (parser->lexer)->u.value;
8607
8608     default:
8609       return NULL_TREE;
8610     }
8611 }
8612
8613 /* Parse an (optional) function-specifier.
8614
8615    function-specifier:
8616      inline
8617      virtual
8618      explicit
8619
8620    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8621    Updates DECL_SPECS, if it is non-NULL.  */
8622
8623 static tree
8624 cp_parser_function_specifier_opt (cp_parser* parser,
8625                                   cp_decl_specifier_seq *decl_specs)
8626 {
8627   cp_token *token = cp_lexer_peek_token (parser->lexer);
8628   switch (token->keyword)
8629     {
8630     case RID_INLINE:
8631       if (decl_specs)
8632         ++decl_specs->specs[(int) ds_inline];
8633       break;
8634
8635     case RID_VIRTUAL:
8636       /* 14.5.2.3 [temp.mem]
8637
8638          A member function template shall not be virtual.  */
8639       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8640         error ("%Htemplates may not be %<virtual%>", &token->location);
8641       else if (decl_specs)
8642         ++decl_specs->specs[(int) ds_virtual];
8643       break;
8644
8645     case RID_EXPLICIT:
8646       if (decl_specs)
8647         ++decl_specs->specs[(int) ds_explicit];
8648       break;
8649
8650     default:
8651       return NULL_TREE;
8652     }
8653
8654   /* Consume the token.  */
8655   return cp_lexer_consume_token (parser->lexer)->u.value;
8656 }
8657
8658 /* Parse a linkage-specification.
8659
8660    linkage-specification:
8661      extern string-literal { declaration-seq [opt] }
8662      extern string-literal declaration  */
8663
8664 static void
8665 cp_parser_linkage_specification (cp_parser* parser)
8666 {
8667   tree linkage;
8668
8669   /* Look for the `extern' keyword.  */
8670   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8671
8672   /* Look for the string-literal.  */
8673   linkage = cp_parser_string_literal (parser, false, false);
8674
8675   /* Transform the literal into an identifier.  If the literal is a
8676      wide-character string, or contains embedded NULs, then we can't
8677      handle it as the user wants.  */
8678   if (strlen (TREE_STRING_POINTER (linkage))
8679       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8680     {
8681       cp_parser_error (parser, "invalid linkage-specification");
8682       /* Assume C++ linkage.  */
8683       linkage = lang_name_cplusplus;
8684     }
8685   else
8686     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8687
8688   /* We're now using the new linkage.  */
8689   push_lang_context (linkage);
8690
8691   /* If the next token is a `{', then we're using the first
8692      production.  */
8693   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8694     {
8695       /* Consume the `{' token.  */
8696       cp_lexer_consume_token (parser->lexer);
8697       /* Parse the declarations.  */
8698       cp_parser_declaration_seq_opt (parser);
8699       /* Look for the closing `}'.  */
8700       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8701     }
8702   /* Otherwise, there's just one declaration.  */
8703   else
8704     {
8705       bool saved_in_unbraced_linkage_specification_p;
8706
8707       saved_in_unbraced_linkage_specification_p
8708         = parser->in_unbraced_linkage_specification_p;
8709       parser->in_unbraced_linkage_specification_p = true;
8710       cp_parser_declaration (parser);
8711       parser->in_unbraced_linkage_specification_p
8712         = saved_in_unbraced_linkage_specification_p;
8713     }
8714
8715   /* We're done with the linkage-specification.  */
8716   pop_lang_context ();
8717 }
8718
8719 /* Parse a static_assert-declaration.
8720
8721    static_assert-declaration:
8722      static_assert ( constant-expression , string-literal ) ; 
8723
8724    If MEMBER_P, this static_assert is a class member.  */
8725
8726 static void 
8727 cp_parser_static_assert(cp_parser *parser, bool member_p)
8728 {
8729   tree condition;
8730   tree message;
8731   cp_token *token;
8732   location_t saved_loc;
8733
8734   /* Peek at the `static_assert' token so we can keep track of exactly
8735      where the static assertion started.  */
8736   token = cp_lexer_peek_token (parser->lexer);
8737   saved_loc = token->location;
8738
8739   /* Look for the `static_assert' keyword.  */
8740   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8741                                   "%<static_assert%>"))
8742     return;
8743
8744   /*  We know we are in a static assertion; commit to any tentative
8745       parse.  */
8746   if (cp_parser_parsing_tentatively (parser))
8747     cp_parser_commit_to_tentative_parse (parser);
8748
8749   /* Parse the `(' starting the static assertion condition.  */
8750   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8751
8752   /* Parse the constant-expression.  */
8753   condition = 
8754     cp_parser_constant_expression (parser,
8755                                    /*allow_non_constant_p=*/false,
8756                                    /*non_constant_p=*/NULL);
8757
8758   /* Parse the separating `,'.  */
8759   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8760
8761   /* Parse the string-literal message.  */
8762   message = cp_parser_string_literal (parser, 
8763                                       /*translate=*/false,
8764                                       /*wide_ok=*/true);
8765
8766   /* A `)' completes the static assertion.  */
8767   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8768     cp_parser_skip_to_closing_parenthesis (parser, 
8769                                            /*recovering=*/true, 
8770                                            /*or_comma=*/false,
8771                                            /*consume_paren=*/true);
8772
8773   /* A semicolon terminates the declaration.  */
8774   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8775
8776   /* Complete the static assertion, which may mean either processing 
8777      the static assert now or saving it for template instantiation.  */
8778   finish_static_assert (condition, message, saved_loc, member_p);
8779 }
8780
8781 /* Parse a `decltype' type. Returns the type. 
8782
8783    simple-type-specifier:
8784      decltype ( expression )  */
8785
8786 static tree
8787 cp_parser_decltype (cp_parser *parser)
8788 {
8789   tree expr;
8790   bool id_expression_or_member_access_p = false;
8791   const char *saved_message;
8792   bool saved_integral_constant_expression_p;
8793   bool saved_non_integral_constant_expression_p;
8794   cp_token *id_expr_start_token;
8795
8796   /* Look for the `decltype' token.  */
8797   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8798     return error_mark_node;
8799
8800   /* Types cannot be defined in a `decltype' expression.  Save away the
8801      old message.  */
8802   saved_message = parser->type_definition_forbidden_message;
8803
8804   /* And create the new one.  */
8805   parser->type_definition_forbidden_message
8806     = "types may not be defined in %<decltype%> expressions";
8807
8808   /* The restrictions on constant-expressions do not apply inside
8809      decltype expressions.  */
8810   saved_integral_constant_expression_p
8811     = parser->integral_constant_expression_p;
8812   saved_non_integral_constant_expression_p
8813     = parser->non_integral_constant_expression_p;
8814   parser->integral_constant_expression_p = false;
8815
8816   /* Do not actually evaluate the expression.  */
8817   ++skip_evaluation;
8818
8819   /* Parse the opening `('.  */
8820   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8821     return error_mark_node;
8822   
8823   /* First, try parsing an id-expression.  */
8824   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8825   cp_parser_parse_tentatively (parser);
8826   expr = cp_parser_id_expression (parser,
8827                                   /*template_keyword_p=*/false,
8828                                   /*check_dependency_p=*/true,
8829                                   /*template_p=*/NULL,
8830                                   /*declarator_p=*/false,
8831                                   /*optional_p=*/false);
8832
8833   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8834     {
8835       bool non_integral_constant_expression_p = false;
8836       tree id_expression = expr;
8837       cp_id_kind idk;
8838       const char *error_msg;
8839
8840       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8841         /* Lookup the name we got back from the id-expression.  */
8842         expr = cp_parser_lookup_name (parser, expr,
8843                                       none_type,
8844                                       /*is_template=*/false,
8845                                       /*is_namespace=*/false,
8846                                       /*check_dependency=*/true,
8847                                       /*ambiguous_decls=*/NULL,
8848                                       id_expr_start_token->location);
8849
8850       if (expr
8851           && expr != error_mark_node
8852           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8853           && TREE_CODE (expr) != TYPE_DECL
8854           && (TREE_CODE (expr) != BIT_NOT_EXPR
8855               || !TYPE_P (TREE_OPERAND (expr, 0)))
8856           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8857         {
8858           /* Complete lookup of the id-expression.  */
8859           expr = (finish_id_expression
8860                   (id_expression, expr, parser->scope, &idk,
8861                    /*integral_constant_expression_p=*/false,
8862                    /*allow_non_integral_constant_expression_p=*/true,
8863                    &non_integral_constant_expression_p,
8864                    /*template_p=*/false,
8865                    /*done=*/true,
8866                    /*address_p=*/false,
8867                    /*template_arg_p=*/false,
8868                    &error_msg,
8869                    id_expr_start_token->location));
8870
8871           if (expr == error_mark_node)
8872             /* We found an id-expression, but it was something that we
8873                should not have found. This is an error, not something
8874                we can recover from, so note that we found an
8875                id-expression and we'll recover as gracefully as
8876                possible.  */
8877             id_expression_or_member_access_p = true;
8878         }
8879
8880       if (expr 
8881           && expr != error_mark_node
8882           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8883         /* We have an id-expression.  */
8884         id_expression_or_member_access_p = true;
8885     }
8886
8887   if (!id_expression_or_member_access_p)
8888     {
8889       /* Abort the id-expression parse.  */
8890       cp_parser_abort_tentative_parse (parser);
8891
8892       /* Parsing tentatively, again.  */
8893       cp_parser_parse_tentatively (parser);
8894
8895       /* Parse a class member access.  */
8896       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8897                                            /*cast_p=*/false,
8898                                            /*member_access_only_p=*/true, NULL);
8899
8900       if (expr 
8901           && expr != error_mark_node
8902           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8903         /* We have an id-expression.  */
8904         id_expression_or_member_access_p = true;
8905     }
8906
8907   if (id_expression_or_member_access_p)
8908     /* We have parsed the complete id-expression or member access.  */
8909     cp_parser_parse_definitely (parser);
8910   else
8911     {
8912       /* Abort our attempt to parse an id-expression or member access
8913          expression.  */
8914       cp_parser_abort_tentative_parse (parser);
8915
8916       /* Parse a full expression.  */
8917       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8918     }
8919
8920   /* Go back to evaluating expressions.  */
8921   --skip_evaluation;
8922
8923   /* Restore the old message and the integral constant expression
8924      flags.  */
8925   parser->type_definition_forbidden_message = saved_message;
8926   parser->integral_constant_expression_p
8927     = saved_integral_constant_expression_p;
8928   parser->non_integral_constant_expression_p
8929     = saved_non_integral_constant_expression_p;
8930
8931   if (expr == error_mark_node)
8932     {
8933       /* Skip everything up to the closing `)'.  */
8934       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8935                                              /*consume_paren=*/true);
8936       return error_mark_node;
8937     }
8938   
8939   /* Parse to the closing `)'.  */
8940   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8941     {
8942       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8943                                              /*consume_paren=*/true);
8944       return error_mark_node;
8945     }
8946
8947   return finish_decltype_type (expr, id_expression_or_member_access_p);
8948 }
8949
8950 /* Special member functions [gram.special] */
8951
8952 /* Parse a conversion-function-id.
8953
8954    conversion-function-id:
8955      operator conversion-type-id
8956
8957    Returns an IDENTIFIER_NODE representing the operator.  */
8958
8959 static tree
8960 cp_parser_conversion_function_id (cp_parser* parser)
8961 {
8962   tree type;
8963   tree saved_scope;
8964   tree saved_qualifying_scope;
8965   tree saved_object_scope;
8966   tree pushed_scope = NULL_TREE;
8967
8968   /* Look for the `operator' token.  */
8969   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8970     return error_mark_node;
8971   /* When we parse the conversion-type-id, the current scope will be
8972      reset.  However, we need that information in able to look up the
8973      conversion function later, so we save it here.  */
8974   saved_scope = parser->scope;
8975   saved_qualifying_scope = parser->qualifying_scope;
8976   saved_object_scope = parser->object_scope;
8977   /* We must enter the scope of the class so that the names of
8978      entities declared within the class are available in the
8979      conversion-type-id.  For example, consider:
8980
8981        struct S {
8982          typedef int I;
8983          operator I();
8984        };
8985
8986        S::operator I() { ... }
8987
8988      In order to see that `I' is a type-name in the definition, we
8989      must be in the scope of `S'.  */
8990   if (saved_scope)
8991     pushed_scope = push_scope (saved_scope);
8992   /* Parse the conversion-type-id.  */
8993   type = cp_parser_conversion_type_id (parser);
8994   /* Leave the scope of the class, if any.  */
8995   if (pushed_scope)
8996     pop_scope (pushed_scope);
8997   /* Restore the saved scope.  */
8998   parser->scope = saved_scope;
8999   parser->qualifying_scope = saved_qualifying_scope;
9000   parser->object_scope = saved_object_scope;
9001   /* If the TYPE is invalid, indicate failure.  */
9002   if (type == error_mark_node)
9003     return error_mark_node;
9004   return mangle_conv_op_name_for_type (type);
9005 }
9006
9007 /* Parse a conversion-type-id:
9008
9009    conversion-type-id:
9010      type-specifier-seq conversion-declarator [opt]
9011
9012    Returns the TYPE specified.  */
9013
9014 static tree
9015 cp_parser_conversion_type_id (cp_parser* parser)
9016 {
9017   tree attributes;
9018   cp_decl_specifier_seq type_specifiers;
9019   cp_declarator *declarator;
9020   tree type_specified;
9021
9022   /* Parse the attributes.  */
9023   attributes = cp_parser_attributes_opt (parser);
9024   /* Parse the type-specifiers.  */
9025   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9026                                 &type_specifiers);
9027   /* If that didn't work, stop.  */
9028   if (type_specifiers.type == error_mark_node)
9029     return error_mark_node;
9030   /* Parse the conversion-declarator.  */
9031   declarator = cp_parser_conversion_declarator_opt (parser);
9032
9033   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9034                                     /*initialized=*/0, &attributes);
9035   if (attributes)
9036     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9037
9038   /* Don't give this error when parsing tentatively.  This happens to
9039      work because we always parse this definitively once.  */
9040   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9041       && type_uses_auto (type_specified))
9042     {
9043       error ("invalid use of %<auto%> in conversion operator");
9044       return error_mark_node;
9045     }
9046
9047   return type_specified;
9048 }
9049
9050 /* Parse an (optional) conversion-declarator.
9051
9052    conversion-declarator:
9053      ptr-operator conversion-declarator [opt]
9054
9055    */
9056
9057 static cp_declarator *
9058 cp_parser_conversion_declarator_opt (cp_parser* parser)
9059 {
9060   enum tree_code code;
9061   tree class_type;
9062   cp_cv_quals cv_quals;
9063
9064   /* We don't know if there's a ptr-operator next, or not.  */
9065   cp_parser_parse_tentatively (parser);
9066   /* Try the ptr-operator.  */
9067   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9068   /* If it worked, look for more conversion-declarators.  */
9069   if (cp_parser_parse_definitely (parser))
9070     {
9071       cp_declarator *declarator;
9072
9073       /* Parse another optional declarator.  */
9074       declarator = cp_parser_conversion_declarator_opt (parser);
9075
9076       return cp_parser_make_indirect_declarator
9077         (code, class_type, cv_quals, declarator);
9078    }
9079
9080   return NULL;
9081 }
9082
9083 /* Parse an (optional) ctor-initializer.
9084
9085    ctor-initializer:
9086      : mem-initializer-list
9087
9088    Returns TRUE iff the ctor-initializer was actually present.  */
9089
9090 static bool
9091 cp_parser_ctor_initializer_opt (cp_parser* parser)
9092 {
9093   /* If the next token is not a `:', then there is no
9094      ctor-initializer.  */
9095   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9096     {
9097       /* Do default initialization of any bases and members.  */
9098       if (DECL_CONSTRUCTOR_P (current_function_decl))
9099         finish_mem_initializers (NULL_TREE);
9100
9101       return false;
9102     }
9103
9104   /* Consume the `:' token.  */
9105   cp_lexer_consume_token (parser->lexer);
9106   /* And the mem-initializer-list.  */
9107   cp_parser_mem_initializer_list (parser);
9108
9109   return true;
9110 }
9111
9112 /* Parse a mem-initializer-list.
9113
9114    mem-initializer-list:
9115      mem-initializer ... [opt]
9116      mem-initializer ... [opt] , mem-initializer-list  */
9117
9118 static void
9119 cp_parser_mem_initializer_list (cp_parser* parser)
9120 {
9121   tree mem_initializer_list = NULL_TREE;
9122   cp_token *token = cp_lexer_peek_token (parser->lexer);
9123
9124   /* Let the semantic analysis code know that we are starting the
9125      mem-initializer-list.  */
9126   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9127     error ("%Honly constructors take base initializers",
9128            &token->location);
9129
9130   /* Loop through the list.  */
9131   while (true)
9132     {
9133       tree mem_initializer;
9134
9135       token = cp_lexer_peek_token (parser->lexer);
9136       /* Parse the mem-initializer.  */
9137       mem_initializer = cp_parser_mem_initializer (parser);
9138       /* If the next token is a `...', we're expanding member initializers. */
9139       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9140         {
9141           /* Consume the `...'. */
9142           cp_lexer_consume_token (parser->lexer);
9143
9144           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9145              can be expanded but members cannot. */
9146           if (mem_initializer != error_mark_node
9147               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9148             {
9149               error ("%Hcannot expand initializer for member %<%D%>",
9150                      &token->location, TREE_PURPOSE (mem_initializer));
9151               mem_initializer = error_mark_node;
9152             }
9153
9154           /* Construct the pack expansion type. */
9155           if (mem_initializer != error_mark_node)
9156             mem_initializer = make_pack_expansion (mem_initializer);
9157         }
9158       /* Add it to the list, unless it was erroneous.  */
9159       if (mem_initializer != error_mark_node)
9160         {
9161           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9162           mem_initializer_list = mem_initializer;
9163         }
9164       /* If the next token is not a `,', we're done.  */
9165       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9166         break;
9167       /* Consume the `,' token.  */
9168       cp_lexer_consume_token (parser->lexer);
9169     }
9170
9171   /* Perform semantic analysis.  */
9172   if (DECL_CONSTRUCTOR_P (current_function_decl))
9173     finish_mem_initializers (mem_initializer_list);
9174 }
9175
9176 /* Parse a mem-initializer.
9177
9178    mem-initializer:
9179      mem-initializer-id ( expression-list [opt] )
9180      mem-initializer-id braced-init-list
9181
9182    GNU extension:
9183
9184    mem-initializer:
9185      ( expression-list [opt] )
9186
9187    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9188    class) or FIELD_DECL (for a non-static data member) to initialize;
9189    the TREE_VALUE is the expression-list.  An empty initialization
9190    list is represented by void_list_node.  */
9191
9192 static tree
9193 cp_parser_mem_initializer (cp_parser* parser)
9194 {
9195   tree mem_initializer_id;
9196   tree expression_list;
9197   tree member;
9198   cp_token *token = cp_lexer_peek_token (parser->lexer);
9199
9200   /* Find out what is being initialized.  */
9201   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9202     {
9203       permerror (token->location,
9204                  "anachronistic old-style base class initializer");
9205       mem_initializer_id = NULL_TREE;
9206     }
9207   else
9208     {
9209       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9210       if (mem_initializer_id == error_mark_node)
9211         return mem_initializer_id;
9212     }
9213   member = expand_member_init (mem_initializer_id);
9214   if (member && !DECL_P (member))
9215     in_base_initializer = 1;
9216
9217   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9218     {
9219       bool expr_non_constant_p;
9220       maybe_warn_cpp0x ("extended initializer lists");
9221       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9222       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9223       expression_list = build_tree_list (NULL_TREE, expression_list);
9224     }
9225   else
9226     expression_list
9227       = cp_parser_parenthesized_expression_list (parser, false,
9228                                                  /*cast_p=*/false,
9229                                                  /*allow_expansion_p=*/true,
9230                                                  /*non_constant_p=*/NULL);
9231   if (expression_list == error_mark_node)
9232     return error_mark_node;
9233   if (!expression_list)
9234     expression_list = void_type_node;
9235
9236   in_base_initializer = 0;
9237
9238   return member ? build_tree_list (member, expression_list) : error_mark_node;
9239 }
9240
9241 /* Parse a mem-initializer-id.
9242
9243    mem-initializer-id:
9244      :: [opt] nested-name-specifier [opt] class-name
9245      identifier
9246
9247    Returns a TYPE indicating the class to be initializer for the first
9248    production.  Returns an IDENTIFIER_NODE indicating the data member
9249    to be initialized for the second production.  */
9250
9251 static tree
9252 cp_parser_mem_initializer_id (cp_parser* parser)
9253 {
9254   bool global_scope_p;
9255   bool nested_name_specifier_p;
9256   bool template_p = false;
9257   tree id;
9258
9259   cp_token *token = cp_lexer_peek_token (parser->lexer);
9260
9261   /* `typename' is not allowed in this context ([temp.res]).  */
9262   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9263     {
9264       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9265              "member initializer is implicitly a type)",
9266              &token->location);
9267       cp_lexer_consume_token (parser->lexer);
9268     }
9269   /* Look for the optional `::' operator.  */
9270   global_scope_p
9271     = (cp_parser_global_scope_opt (parser,
9272                                    /*current_scope_valid_p=*/false)
9273        != NULL_TREE);
9274   /* Look for the optional nested-name-specifier.  The simplest way to
9275      implement:
9276
9277        [temp.res]
9278
9279        The keyword `typename' is not permitted in a base-specifier or
9280        mem-initializer; in these contexts a qualified name that
9281        depends on a template-parameter is implicitly assumed to be a
9282        type name.
9283
9284      is to assume that we have seen the `typename' keyword at this
9285      point.  */
9286   nested_name_specifier_p
9287     = (cp_parser_nested_name_specifier_opt (parser,
9288                                             /*typename_keyword_p=*/true,
9289                                             /*check_dependency_p=*/true,
9290                                             /*type_p=*/true,
9291                                             /*is_declaration=*/true)
9292        != NULL_TREE);
9293   if (nested_name_specifier_p)
9294     template_p = cp_parser_optional_template_keyword (parser);
9295   /* If there is a `::' operator or a nested-name-specifier, then we
9296      are definitely looking for a class-name.  */
9297   if (global_scope_p || nested_name_specifier_p)
9298     return cp_parser_class_name (parser,
9299                                  /*typename_keyword_p=*/true,
9300                                  /*template_keyword_p=*/template_p,
9301                                  none_type,
9302                                  /*check_dependency_p=*/true,
9303                                  /*class_head_p=*/false,
9304                                  /*is_declaration=*/true);
9305   /* Otherwise, we could also be looking for an ordinary identifier.  */
9306   cp_parser_parse_tentatively (parser);
9307   /* Try a class-name.  */
9308   id = cp_parser_class_name (parser,
9309                              /*typename_keyword_p=*/true,
9310                              /*template_keyword_p=*/false,
9311                              none_type,
9312                              /*check_dependency_p=*/true,
9313                              /*class_head_p=*/false,
9314                              /*is_declaration=*/true);
9315   /* If we found one, we're done.  */
9316   if (cp_parser_parse_definitely (parser))
9317     return id;
9318   /* Otherwise, look for an ordinary identifier.  */
9319   return cp_parser_identifier (parser);
9320 }
9321
9322 /* Overloading [gram.over] */
9323
9324 /* Parse an operator-function-id.
9325
9326    operator-function-id:
9327      operator operator
9328
9329    Returns an IDENTIFIER_NODE for the operator which is a
9330    human-readable spelling of the identifier, e.g., `operator +'.  */
9331
9332 static tree
9333 cp_parser_operator_function_id (cp_parser* parser)
9334 {
9335   /* Look for the `operator' keyword.  */
9336   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9337     return error_mark_node;
9338   /* And then the name of the operator itself.  */
9339   return cp_parser_operator (parser);
9340 }
9341
9342 /* Parse an operator.
9343
9344    operator:
9345      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9346      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9347      || ++ -- , ->* -> () []
9348
9349    GNU Extensions:
9350
9351    operator:
9352      <? >? <?= >?=
9353
9354    Returns an IDENTIFIER_NODE for the operator which is a
9355    human-readable spelling of the identifier, e.g., `operator +'.  */
9356
9357 static tree
9358 cp_parser_operator (cp_parser* parser)
9359 {
9360   tree id = NULL_TREE;
9361   cp_token *token;
9362
9363   /* Peek at the next token.  */
9364   token = cp_lexer_peek_token (parser->lexer);
9365   /* Figure out which operator we have.  */
9366   switch (token->type)
9367     {
9368     case CPP_KEYWORD:
9369       {
9370         enum tree_code op;
9371
9372         /* The keyword should be either `new' or `delete'.  */
9373         if (token->keyword == RID_NEW)
9374           op = NEW_EXPR;
9375         else if (token->keyword == RID_DELETE)
9376           op = DELETE_EXPR;
9377         else
9378           break;
9379
9380         /* Consume the `new' or `delete' token.  */
9381         cp_lexer_consume_token (parser->lexer);
9382
9383         /* Peek at the next token.  */
9384         token = cp_lexer_peek_token (parser->lexer);
9385         /* If it's a `[' token then this is the array variant of the
9386            operator.  */
9387         if (token->type == CPP_OPEN_SQUARE)
9388           {
9389             /* Consume the `[' token.  */
9390             cp_lexer_consume_token (parser->lexer);
9391             /* Look for the `]' token.  */
9392             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9393             id = ansi_opname (op == NEW_EXPR
9394                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9395           }
9396         /* Otherwise, we have the non-array variant.  */
9397         else
9398           id = ansi_opname (op);
9399
9400         return id;
9401       }
9402
9403     case CPP_PLUS:
9404       id = ansi_opname (PLUS_EXPR);
9405       break;
9406
9407     case CPP_MINUS:
9408       id = ansi_opname (MINUS_EXPR);
9409       break;
9410
9411     case CPP_MULT:
9412       id = ansi_opname (MULT_EXPR);
9413       break;
9414
9415     case CPP_DIV:
9416       id = ansi_opname (TRUNC_DIV_EXPR);
9417       break;
9418
9419     case CPP_MOD:
9420       id = ansi_opname (TRUNC_MOD_EXPR);
9421       break;
9422
9423     case CPP_XOR:
9424       id = ansi_opname (BIT_XOR_EXPR);
9425       break;
9426
9427     case CPP_AND:
9428       id = ansi_opname (BIT_AND_EXPR);
9429       break;
9430
9431     case CPP_OR:
9432       id = ansi_opname (BIT_IOR_EXPR);
9433       break;
9434
9435     case CPP_COMPL:
9436       id = ansi_opname (BIT_NOT_EXPR);
9437       break;
9438
9439     case CPP_NOT:
9440       id = ansi_opname (TRUTH_NOT_EXPR);
9441       break;
9442
9443     case CPP_EQ:
9444       id = ansi_assopname (NOP_EXPR);
9445       break;
9446
9447     case CPP_LESS:
9448       id = ansi_opname (LT_EXPR);
9449       break;
9450
9451     case CPP_GREATER:
9452       id = ansi_opname (GT_EXPR);
9453       break;
9454
9455     case CPP_PLUS_EQ:
9456       id = ansi_assopname (PLUS_EXPR);
9457       break;
9458
9459     case CPP_MINUS_EQ:
9460       id = ansi_assopname (MINUS_EXPR);
9461       break;
9462
9463     case CPP_MULT_EQ:
9464       id = ansi_assopname (MULT_EXPR);
9465       break;
9466
9467     case CPP_DIV_EQ:
9468       id = ansi_assopname (TRUNC_DIV_EXPR);
9469       break;
9470
9471     case CPP_MOD_EQ:
9472       id = ansi_assopname (TRUNC_MOD_EXPR);
9473       break;
9474
9475     case CPP_XOR_EQ:
9476       id = ansi_assopname (BIT_XOR_EXPR);
9477       break;
9478
9479     case CPP_AND_EQ:
9480       id = ansi_assopname (BIT_AND_EXPR);
9481       break;
9482
9483     case CPP_OR_EQ:
9484       id = ansi_assopname (BIT_IOR_EXPR);
9485       break;
9486
9487     case CPP_LSHIFT:
9488       id = ansi_opname (LSHIFT_EXPR);
9489       break;
9490
9491     case CPP_RSHIFT:
9492       id = ansi_opname (RSHIFT_EXPR);
9493       break;
9494
9495     case CPP_LSHIFT_EQ:
9496       id = ansi_assopname (LSHIFT_EXPR);
9497       break;
9498
9499     case CPP_RSHIFT_EQ:
9500       id = ansi_assopname (RSHIFT_EXPR);
9501       break;
9502
9503     case CPP_EQ_EQ:
9504       id = ansi_opname (EQ_EXPR);
9505       break;
9506
9507     case CPP_NOT_EQ:
9508       id = ansi_opname (NE_EXPR);
9509       break;
9510
9511     case CPP_LESS_EQ:
9512       id = ansi_opname (LE_EXPR);
9513       break;
9514
9515     case CPP_GREATER_EQ:
9516       id = ansi_opname (GE_EXPR);
9517       break;
9518
9519     case CPP_AND_AND:
9520       id = ansi_opname (TRUTH_ANDIF_EXPR);
9521       break;
9522
9523     case CPP_OR_OR:
9524       id = ansi_opname (TRUTH_ORIF_EXPR);
9525       break;
9526
9527     case CPP_PLUS_PLUS:
9528       id = ansi_opname (POSTINCREMENT_EXPR);
9529       break;
9530
9531     case CPP_MINUS_MINUS:
9532       id = ansi_opname (PREDECREMENT_EXPR);
9533       break;
9534
9535     case CPP_COMMA:
9536       id = ansi_opname (COMPOUND_EXPR);
9537       break;
9538
9539     case CPP_DEREF_STAR:
9540       id = ansi_opname (MEMBER_REF);
9541       break;
9542
9543     case CPP_DEREF:
9544       id = ansi_opname (COMPONENT_REF);
9545       break;
9546
9547     case CPP_OPEN_PAREN:
9548       /* Consume the `('.  */
9549       cp_lexer_consume_token (parser->lexer);
9550       /* Look for the matching `)'.  */
9551       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9552       return ansi_opname (CALL_EXPR);
9553
9554     case CPP_OPEN_SQUARE:
9555       /* Consume the `['.  */
9556       cp_lexer_consume_token (parser->lexer);
9557       /* Look for the matching `]'.  */
9558       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9559       return ansi_opname (ARRAY_REF);
9560
9561     default:
9562       /* Anything else is an error.  */
9563       break;
9564     }
9565
9566   /* If we have selected an identifier, we need to consume the
9567      operator token.  */
9568   if (id)
9569     cp_lexer_consume_token (parser->lexer);
9570   /* Otherwise, no valid operator name was present.  */
9571   else
9572     {
9573       cp_parser_error (parser, "expected operator");
9574       id = error_mark_node;
9575     }
9576
9577   return id;
9578 }
9579
9580 /* Parse a template-declaration.
9581
9582    template-declaration:
9583      export [opt] template < template-parameter-list > declaration
9584
9585    If MEMBER_P is TRUE, this template-declaration occurs within a
9586    class-specifier.
9587
9588    The grammar rule given by the standard isn't correct.  What
9589    is really meant is:
9590
9591    template-declaration:
9592      export [opt] template-parameter-list-seq
9593        decl-specifier-seq [opt] init-declarator [opt] ;
9594      export [opt] template-parameter-list-seq
9595        function-definition
9596
9597    template-parameter-list-seq:
9598      template-parameter-list-seq [opt]
9599      template < template-parameter-list >  */
9600
9601 static void
9602 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9603 {
9604   /* Check for `export'.  */
9605   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9606     {
9607       /* Consume the `export' token.  */
9608       cp_lexer_consume_token (parser->lexer);
9609       /* Warn that we do not support `export'.  */
9610       warning (0, "keyword %<export%> not implemented, and will be ignored");
9611     }
9612
9613   cp_parser_template_declaration_after_export (parser, member_p);
9614 }
9615
9616 /* Parse a template-parameter-list.
9617
9618    template-parameter-list:
9619      template-parameter
9620      template-parameter-list , template-parameter
9621
9622    Returns a TREE_LIST.  Each node represents a template parameter.
9623    The nodes are connected via their TREE_CHAINs.  */
9624
9625 static tree
9626 cp_parser_template_parameter_list (cp_parser* parser)
9627 {
9628   tree parameter_list = NULL_TREE;
9629
9630   begin_template_parm_list ();
9631   while (true)
9632     {
9633       tree parameter;
9634       bool is_non_type;
9635       bool is_parameter_pack;
9636
9637       /* Parse the template-parameter.  */
9638       parameter = cp_parser_template_parameter (parser, 
9639                                                 &is_non_type,
9640                                                 &is_parameter_pack);
9641       /* Add it to the list.  */
9642       if (parameter != error_mark_node)
9643         parameter_list = process_template_parm (parameter_list,
9644                                                 parameter,
9645                                                 is_non_type,
9646                                                 is_parameter_pack);
9647       else
9648        {
9649          tree err_parm = build_tree_list (parameter, parameter);
9650          TREE_VALUE (err_parm) = error_mark_node;
9651          parameter_list = chainon (parameter_list, err_parm);
9652        }
9653
9654       /* If the next token is not a `,', we're done.  */
9655       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9656         break;
9657       /* Otherwise, consume the `,' token.  */
9658       cp_lexer_consume_token (parser->lexer);
9659     }
9660
9661   return end_template_parm_list (parameter_list);
9662 }
9663
9664 /* Parse a template-parameter.
9665
9666    template-parameter:
9667      type-parameter
9668      parameter-declaration
9669
9670    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9671    the parameter.  The TREE_PURPOSE is the default value, if any.
9672    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9673    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9674    set to true iff this parameter is a parameter pack. */
9675
9676 static tree
9677 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9678                               bool *is_parameter_pack)
9679 {
9680   cp_token *token;
9681   cp_parameter_declarator *parameter_declarator;
9682   cp_declarator *id_declarator;
9683   tree parm;
9684
9685   /* Assume it is a type parameter or a template parameter.  */
9686   *is_non_type = false;
9687   /* Assume it not a parameter pack. */
9688   *is_parameter_pack = false;
9689   /* Peek at the next token.  */
9690   token = cp_lexer_peek_token (parser->lexer);
9691   /* If it is `class' or `template', we have a type-parameter.  */
9692   if (token->keyword == RID_TEMPLATE)
9693     return cp_parser_type_parameter (parser, is_parameter_pack);
9694   /* If it is `class' or `typename' we do not know yet whether it is a
9695      type parameter or a non-type parameter.  Consider:
9696
9697        template <typename T, typename T::X X> ...
9698
9699      or:
9700
9701        template <class C, class D*> ...
9702
9703      Here, the first parameter is a type parameter, and the second is
9704      a non-type parameter.  We can tell by looking at the token after
9705      the identifier -- if it is a `,', `=', or `>' then we have a type
9706      parameter.  */
9707   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9708     {
9709       /* Peek at the token after `class' or `typename'.  */
9710       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9711       /* If it's an ellipsis, we have a template type parameter
9712          pack. */
9713       if (token->type == CPP_ELLIPSIS)
9714         return cp_parser_type_parameter (parser, is_parameter_pack);
9715       /* If it's an identifier, skip it.  */
9716       if (token->type == CPP_NAME)
9717         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9718       /* Now, see if the token looks like the end of a template
9719          parameter.  */
9720       if (token->type == CPP_COMMA
9721           || token->type == CPP_EQ
9722           || token->type == CPP_GREATER)
9723         return cp_parser_type_parameter (parser, is_parameter_pack);
9724     }
9725
9726   /* Otherwise, it is a non-type parameter.
9727
9728      [temp.param]
9729
9730      When parsing a default template-argument for a non-type
9731      template-parameter, the first non-nested `>' is taken as the end
9732      of the template parameter-list rather than a greater-than
9733      operator.  */
9734   *is_non_type = true;
9735   parameter_declarator
9736      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9737                                         /*parenthesized_p=*/NULL);
9738
9739   /* If the parameter declaration is marked as a parameter pack, set
9740      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9741      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9742      grokdeclarator. */
9743   if (parameter_declarator
9744       && parameter_declarator->declarator
9745       && parameter_declarator->declarator->parameter_pack_p)
9746     {
9747       *is_parameter_pack = true;
9748       parameter_declarator->declarator->parameter_pack_p = false;
9749     }
9750
9751   /* If the next token is an ellipsis, and we don't already have it
9752      marked as a parameter pack, then we have a parameter pack (that
9753      has no declarator).  */
9754   if (!*is_parameter_pack
9755       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9756       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9757     {
9758       /* Consume the `...'.  */
9759       cp_lexer_consume_token (parser->lexer);
9760       maybe_warn_variadic_templates ();
9761       
9762       *is_parameter_pack = true;
9763     }
9764   /* We might end up with a pack expansion as the type of the non-type
9765      template parameter, in which case this is a non-type template
9766      parameter pack.  */
9767   else if (parameter_declarator
9768            && parameter_declarator->decl_specifiers.type
9769            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9770     {
9771       *is_parameter_pack = true;
9772       parameter_declarator->decl_specifiers.type = 
9773         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9774     }
9775
9776   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9777     {
9778       /* Parameter packs cannot have default arguments.  However, a
9779          user may try to do so, so we'll parse them and give an
9780          appropriate diagnostic here.  */
9781
9782       /* Consume the `='.  */
9783       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9784       cp_lexer_consume_token (parser->lexer);
9785       
9786       /* Find the name of the parameter pack.  */     
9787       id_declarator = parameter_declarator->declarator;
9788       while (id_declarator && id_declarator->kind != cdk_id)
9789         id_declarator = id_declarator->declarator;
9790       
9791       if (id_declarator && id_declarator->kind == cdk_id)
9792         error ("%Htemplate parameter pack %qD cannot have a default argument",
9793                &start_token->location, id_declarator->u.id.unqualified_name);
9794       else
9795         error ("%Htemplate parameter pack cannot have a default argument",
9796                &start_token->location);
9797       
9798       /* Parse the default argument, but throw away the result.  */
9799       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9800     }
9801
9802   parm = grokdeclarator (parameter_declarator->declarator,
9803                          &parameter_declarator->decl_specifiers,
9804                          PARM, /*initialized=*/0,
9805                          /*attrlist=*/NULL);
9806   if (parm == error_mark_node)
9807     return error_mark_node;
9808
9809   return build_tree_list (parameter_declarator->default_argument, parm);
9810 }
9811
9812 /* Parse a type-parameter.
9813
9814    type-parameter:
9815      class identifier [opt]
9816      class identifier [opt] = type-id
9817      typename identifier [opt]
9818      typename identifier [opt] = type-id
9819      template < template-parameter-list > class identifier [opt]
9820      template < template-parameter-list > class identifier [opt]
9821        = id-expression
9822
9823    GNU Extension (variadic templates):
9824
9825    type-parameter:
9826      class ... identifier [opt]
9827      typename ... identifier [opt]
9828
9829    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9830    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9831    the declaration of the parameter.
9832
9833    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9834
9835 static tree
9836 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9837 {
9838   cp_token *token;
9839   tree parameter;
9840
9841   /* Look for a keyword to tell us what kind of parameter this is.  */
9842   token = cp_parser_require (parser, CPP_KEYWORD,
9843                              "%<class%>, %<typename%>, or %<template%>");
9844   if (!token)
9845     return error_mark_node;
9846
9847   switch (token->keyword)
9848     {
9849     case RID_CLASS:
9850     case RID_TYPENAME:
9851       {
9852         tree identifier;
9853         tree default_argument;
9854
9855         /* If the next token is an ellipsis, we have a template
9856            argument pack. */
9857         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9858           {
9859             /* Consume the `...' token. */
9860             cp_lexer_consume_token (parser->lexer);
9861             maybe_warn_variadic_templates ();
9862
9863             *is_parameter_pack = true;
9864           }
9865
9866         /* If the next token is an identifier, then it names the
9867            parameter.  */
9868         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9869           identifier = cp_parser_identifier (parser);
9870         else
9871           identifier = NULL_TREE;
9872
9873         /* Create the parameter.  */
9874         parameter = finish_template_type_parm (class_type_node, identifier);
9875
9876         /* If the next token is an `=', we have a default argument.  */
9877         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9878           {
9879             /* Consume the `=' token.  */
9880             cp_lexer_consume_token (parser->lexer);
9881             /* Parse the default-argument.  */
9882             push_deferring_access_checks (dk_no_deferred);
9883             default_argument = cp_parser_type_id (parser);
9884
9885             /* Template parameter packs cannot have default
9886                arguments. */
9887             if (*is_parameter_pack)
9888               {
9889                 if (identifier)
9890                   error ("%Htemplate parameter pack %qD cannot have a "
9891                          "default argument", &token->location, identifier);
9892                 else
9893                   error ("%Htemplate parameter packs cannot have "
9894                          "default arguments", &token->location);
9895                 default_argument = NULL_TREE;
9896               }
9897             pop_deferring_access_checks ();
9898           }
9899         else
9900           default_argument = NULL_TREE;
9901
9902         /* Create the combined representation of the parameter and the
9903            default argument.  */
9904         parameter = build_tree_list (default_argument, parameter);
9905       }
9906       break;
9907
9908     case RID_TEMPLATE:
9909       {
9910         tree parameter_list;
9911         tree identifier;
9912         tree default_argument;
9913
9914         /* Look for the `<'.  */
9915         cp_parser_require (parser, CPP_LESS, "%<<%>");
9916         /* Parse the template-parameter-list.  */
9917         parameter_list = cp_parser_template_parameter_list (parser);
9918         /* Look for the `>'.  */
9919         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9920         /* Look for the `class' keyword.  */
9921         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9922         /* If the next token is an ellipsis, we have a template
9923            argument pack. */
9924         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9925           {
9926             /* Consume the `...' token. */
9927             cp_lexer_consume_token (parser->lexer);
9928             maybe_warn_variadic_templates ();
9929
9930             *is_parameter_pack = true;
9931           }
9932         /* If the next token is an `=', then there is a
9933            default-argument.  If the next token is a `>', we are at
9934            the end of the parameter-list.  If the next token is a `,',
9935            then we are at the end of this parameter.  */
9936         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9937             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9938             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9939           {
9940             identifier = cp_parser_identifier (parser);
9941             /* Treat invalid names as if the parameter were nameless.  */
9942             if (identifier == error_mark_node)
9943               identifier = NULL_TREE;
9944           }
9945         else
9946           identifier = NULL_TREE;
9947
9948         /* Create the template parameter.  */
9949         parameter = finish_template_template_parm (class_type_node,
9950                                                    identifier);
9951
9952         /* If the next token is an `=', then there is a
9953            default-argument.  */
9954         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9955           {
9956             bool is_template;
9957
9958             /* Consume the `='.  */
9959             cp_lexer_consume_token (parser->lexer);
9960             /* Parse the id-expression.  */
9961             push_deferring_access_checks (dk_no_deferred);
9962             /* save token before parsing the id-expression, for error
9963                reporting */
9964             token = cp_lexer_peek_token (parser->lexer);
9965             default_argument
9966               = cp_parser_id_expression (parser,
9967                                          /*template_keyword_p=*/false,
9968                                          /*check_dependency_p=*/true,
9969                                          /*template_p=*/&is_template,
9970                                          /*declarator_p=*/false,
9971                                          /*optional_p=*/false);
9972             if (TREE_CODE (default_argument) == TYPE_DECL)
9973               /* If the id-expression was a template-id that refers to
9974                  a template-class, we already have the declaration here,
9975                  so no further lookup is needed.  */
9976                  ;
9977             else
9978               /* Look up the name.  */
9979               default_argument
9980                 = cp_parser_lookup_name (parser, default_argument,
9981                                          none_type,
9982                                          /*is_template=*/is_template,
9983                                          /*is_namespace=*/false,
9984                                          /*check_dependency=*/true,
9985                                          /*ambiguous_decls=*/NULL,
9986                                          token->location);
9987             /* See if the default argument is valid.  */
9988             default_argument
9989               = check_template_template_default_arg (default_argument);
9990
9991             /* Template parameter packs cannot have default
9992                arguments. */
9993             if (*is_parameter_pack)
9994               {
9995                 if (identifier)
9996                   error ("%Htemplate parameter pack %qD cannot "
9997                          "have a default argument",
9998                          &token->location, identifier);
9999                 else
10000                   error ("%Htemplate parameter packs cannot "
10001                          "have default arguments",
10002                          &token->location);
10003                 default_argument = NULL_TREE;
10004               }
10005             pop_deferring_access_checks ();
10006           }
10007         else
10008           default_argument = NULL_TREE;
10009
10010         /* Create the combined representation of the parameter and the
10011            default argument.  */
10012         parameter = build_tree_list (default_argument, parameter);
10013       }
10014       break;
10015
10016     default:
10017       gcc_unreachable ();
10018       break;
10019     }
10020
10021   return parameter;
10022 }
10023
10024 /* Parse a template-id.
10025
10026    template-id:
10027      template-name < template-argument-list [opt] >
10028
10029    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10030    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10031    returned.  Otherwise, if the template-name names a function, or set
10032    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10033    names a class, returns a TYPE_DECL for the specialization.
10034
10035    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10036    uninstantiated templates.  */
10037
10038 static tree
10039 cp_parser_template_id (cp_parser *parser,
10040                        bool template_keyword_p,
10041                        bool check_dependency_p,
10042                        bool is_declaration)
10043 {
10044   int i;
10045   tree templ;
10046   tree arguments;
10047   tree template_id;
10048   cp_token_position start_of_id = 0;
10049   deferred_access_check *chk;
10050   VEC (deferred_access_check,gc) *access_check;
10051   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10052   bool is_identifier;
10053
10054   /* If the next token corresponds to a template-id, there is no need
10055      to reparse it.  */
10056   next_token = cp_lexer_peek_token (parser->lexer);
10057   if (next_token->type == CPP_TEMPLATE_ID)
10058     {
10059       struct tree_check *check_value;
10060
10061       /* Get the stored value.  */
10062       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10063       /* Perform any access checks that were deferred.  */
10064       access_check = check_value->checks;
10065       if (access_check)
10066         {
10067           for (i = 0 ;
10068                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10069                ++i)
10070             {
10071               perform_or_defer_access_check (chk->binfo,
10072                                              chk->decl,
10073                                              chk->diag_decl);
10074             }
10075         }
10076       /* Return the stored value.  */
10077       return check_value->value;
10078     }
10079
10080   /* Avoid performing name lookup if there is no possibility of
10081      finding a template-id.  */
10082   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10083       || (next_token->type == CPP_NAME
10084           && !cp_parser_nth_token_starts_template_argument_list_p
10085                (parser, 2)))
10086     {
10087       cp_parser_error (parser, "expected template-id");
10088       return error_mark_node;
10089     }
10090
10091   /* Remember where the template-id starts.  */
10092   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10093     start_of_id = cp_lexer_token_position (parser->lexer, false);
10094
10095   push_deferring_access_checks (dk_deferred);
10096
10097   /* Parse the template-name.  */
10098   is_identifier = false;
10099   token = cp_lexer_peek_token (parser->lexer);
10100   templ = cp_parser_template_name (parser, template_keyword_p,
10101                                    check_dependency_p,
10102                                    is_declaration,
10103                                    &is_identifier);
10104   if (templ == error_mark_node || is_identifier)
10105     {
10106       pop_deferring_access_checks ();
10107       return templ;
10108     }
10109
10110   /* If we find the sequence `[:' after a template-name, it's probably
10111      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10112      parse correctly the argument list.  */
10113   next_token = cp_lexer_peek_token (parser->lexer);
10114   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10115   if (next_token->type == CPP_OPEN_SQUARE
10116       && next_token->flags & DIGRAPH
10117       && next_token_2->type == CPP_COLON
10118       && !(next_token_2->flags & PREV_WHITE))
10119     {
10120       cp_parser_parse_tentatively (parser);
10121       /* Change `:' into `::'.  */
10122       next_token_2->type = CPP_SCOPE;
10123       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10124          CPP_LESS.  */
10125       cp_lexer_consume_token (parser->lexer);
10126
10127       /* Parse the arguments.  */
10128       arguments = cp_parser_enclosed_template_argument_list (parser);
10129       if (!cp_parser_parse_definitely (parser))
10130         {
10131           /* If we couldn't parse an argument list, then we revert our changes
10132              and return simply an error. Maybe this is not a template-id
10133              after all.  */
10134           next_token_2->type = CPP_COLON;
10135           cp_parser_error (parser, "expected %<<%>");
10136           pop_deferring_access_checks ();
10137           return error_mark_node;
10138         }
10139       /* Otherwise, emit an error about the invalid digraph, but continue
10140          parsing because we got our argument list.  */
10141       if (permerror (next_token->location,
10142                      "%<<::%> cannot begin a template-argument list"))
10143         {
10144           static bool hint = false;
10145           inform (next_token->location,
10146                   "%<<:%> is an alternate spelling for %<[%>."
10147                   " Insert whitespace between %<<%> and %<::%>");
10148           if (!hint && !flag_permissive)
10149             {
10150               inform (next_token->location, "(if you use %<-fpermissive%>"
10151                       " G++ will accept your code)");
10152               hint = true;
10153             }
10154         }
10155     }
10156   else
10157     {
10158       /* Look for the `<' that starts the template-argument-list.  */
10159       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10160         {
10161           pop_deferring_access_checks ();
10162           return error_mark_node;
10163         }
10164       /* Parse the arguments.  */
10165       arguments = cp_parser_enclosed_template_argument_list (parser);
10166     }
10167
10168   /* Build a representation of the specialization.  */
10169   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10170     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10171   else if (DECL_CLASS_TEMPLATE_P (templ)
10172            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10173     {
10174       bool entering_scope;
10175       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10176          template (rather than some instantiation thereof) only if
10177          is not nested within some other construct.  For example, in
10178          "template <typename T> void f(T) { A<T>::", A<T> is just an
10179          instantiation of A.  */
10180       entering_scope = (template_parm_scope_p ()
10181                         && cp_lexer_next_token_is (parser->lexer,
10182                                                    CPP_SCOPE));
10183       template_id
10184         = finish_template_type (templ, arguments, entering_scope);
10185     }
10186   else
10187     {
10188       /* If it's not a class-template or a template-template, it should be
10189          a function-template.  */
10190       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10191                    || TREE_CODE (templ) == OVERLOAD
10192                    || BASELINK_P (templ)));
10193
10194       template_id = lookup_template_function (templ, arguments);
10195     }
10196
10197   /* If parsing tentatively, replace the sequence of tokens that makes
10198      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10199      should we re-parse the token stream, we will not have to repeat
10200      the effort required to do the parse, nor will we issue duplicate
10201      error messages about problems during instantiation of the
10202      template.  */
10203   if (start_of_id)
10204     {
10205       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10206
10207       /* Reset the contents of the START_OF_ID token.  */
10208       token->type = CPP_TEMPLATE_ID;
10209       /* Retrieve any deferred checks.  Do not pop this access checks yet
10210          so the memory will not be reclaimed during token replacing below.  */
10211       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10212       token->u.tree_check_value->value = template_id;
10213       token->u.tree_check_value->checks = get_deferred_access_checks ();
10214       token->keyword = RID_MAX;
10215
10216       /* Purge all subsequent tokens.  */
10217       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10218
10219       /* ??? Can we actually assume that, if template_id ==
10220          error_mark_node, we will have issued a diagnostic to the
10221          user, as opposed to simply marking the tentative parse as
10222          failed?  */
10223       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10224         error ("%Hparse error in template argument list",
10225                &token->location);
10226     }
10227
10228   pop_deferring_access_checks ();
10229   return template_id;
10230 }
10231
10232 /* Parse a template-name.
10233
10234    template-name:
10235      identifier
10236
10237    The standard should actually say:
10238
10239    template-name:
10240      identifier
10241      operator-function-id
10242
10243    A defect report has been filed about this issue.
10244
10245    A conversion-function-id cannot be a template name because they cannot
10246    be part of a template-id. In fact, looking at this code:
10247
10248    a.operator K<int>()
10249
10250    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10251    It is impossible to call a templated conversion-function-id with an
10252    explicit argument list, since the only allowed template parameter is
10253    the type to which it is converting.
10254
10255    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10256    `template' keyword, in a construction like:
10257
10258      T::template f<3>()
10259
10260    In that case `f' is taken to be a template-name, even though there
10261    is no way of knowing for sure.
10262
10263    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10264    name refers to a set of overloaded functions, at least one of which
10265    is a template, or an IDENTIFIER_NODE with the name of the template,
10266    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10267    names are looked up inside uninstantiated templates.  */
10268
10269 static tree
10270 cp_parser_template_name (cp_parser* parser,
10271                          bool template_keyword_p,
10272                          bool check_dependency_p,
10273                          bool is_declaration,
10274                          bool *is_identifier)
10275 {
10276   tree identifier;
10277   tree decl;
10278   tree fns;
10279   cp_token *token = cp_lexer_peek_token (parser->lexer);
10280
10281   /* If the next token is `operator', then we have either an
10282      operator-function-id or a conversion-function-id.  */
10283   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10284     {
10285       /* We don't know whether we're looking at an
10286          operator-function-id or a conversion-function-id.  */
10287       cp_parser_parse_tentatively (parser);
10288       /* Try an operator-function-id.  */
10289       identifier = cp_parser_operator_function_id (parser);
10290       /* If that didn't work, try a conversion-function-id.  */
10291       if (!cp_parser_parse_definitely (parser))
10292         {
10293           cp_parser_error (parser, "expected template-name");
10294           return error_mark_node;
10295         }
10296     }
10297   /* Look for the identifier.  */
10298   else
10299     identifier = cp_parser_identifier (parser);
10300
10301   /* If we didn't find an identifier, we don't have a template-id.  */
10302   if (identifier == error_mark_node)
10303     return error_mark_node;
10304
10305   /* If the name immediately followed the `template' keyword, then it
10306      is a template-name.  However, if the next token is not `<', then
10307      we do not treat it as a template-name, since it is not being used
10308      as part of a template-id.  This enables us to handle constructs
10309      like:
10310
10311        template <typename T> struct S { S(); };
10312        template <typename T> S<T>::S();
10313
10314      correctly.  We would treat `S' as a template -- if it were `S<T>'
10315      -- but we do not if there is no `<'.  */
10316
10317   if (processing_template_decl
10318       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10319     {
10320       /* In a declaration, in a dependent context, we pretend that the
10321          "template" keyword was present in order to improve error
10322          recovery.  For example, given:
10323
10324            template <typename T> void f(T::X<int>);
10325
10326          we want to treat "X<int>" as a template-id.  */
10327       if (is_declaration
10328           && !template_keyword_p
10329           && parser->scope && TYPE_P (parser->scope)
10330           && check_dependency_p
10331           && dependent_scope_p (parser->scope)
10332           /* Do not do this for dtors (or ctors), since they never
10333              need the template keyword before their name.  */
10334           && !constructor_name_p (identifier, parser->scope))
10335         {
10336           cp_token_position start = 0;
10337
10338           /* Explain what went wrong.  */
10339           error ("%Hnon-template %qD used as template",
10340                  &token->location, identifier);
10341           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10342                   parser->scope, identifier);
10343           /* If parsing tentatively, find the location of the "<" token.  */
10344           if (cp_parser_simulate_error (parser))
10345             start = cp_lexer_token_position (parser->lexer, true);
10346           /* Parse the template arguments so that we can issue error
10347              messages about them.  */
10348           cp_lexer_consume_token (parser->lexer);
10349           cp_parser_enclosed_template_argument_list (parser);
10350           /* Skip tokens until we find a good place from which to
10351              continue parsing.  */
10352           cp_parser_skip_to_closing_parenthesis (parser,
10353                                                  /*recovering=*/true,
10354                                                  /*or_comma=*/true,
10355                                                  /*consume_paren=*/false);
10356           /* If parsing tentatively, permanently remove the
10357              template argument list.  That will prevent duplicate
10358              error messages from being issued about the missing
10359              "template" keyword.  */
10360           if (start)
10361             cp_lexer_purge_tokens_after (parser->lexer, start);
10362           if (is_identifier)
10363             *is_identifier = true;
10364           return identifier;
10365         }
10366
10367       /* If the "template" keyword is present, then there is generally
10368          no point in doing name-lookup, so we just return IDENTIFIER.
10369          But, if the qualifying scope is non-dependent then we can
10370          (and must) do name-lookup normally.  */
10371       if (template_keyword_p
10372           && (!parser->scope
10373               || (TYPE_P (parser->scope)
10374                   && dependent_type_p (parser->scope))))
10375         return identifier;
10376     }
10377
10378   /* Look up the name.  */
10379   decl = cp_parser_lookup_name (parser, identifier,
10380                                 none_type,
10381                                 /*is_template=*/false,
10382                                 /*is_namespace=*/false,
10383                                 check_dependency_p,
10384                                 /*ambiguous_decls=*/NULL,
10385                                 token->location);
10386   decl = maybe_get_template_decl_from_type_decl (decl);
10387
10388   /* If DECL is a template, then the name was a template-name.  */
10389   if (TREE_CODE (decl) == TEMPLATE_DECL)
10390     ;
10391   else
10392     {
10393       tree fn = NULL_TREE;
10394
10395       /* The standard does not explicitly indicate whether a name that
10396          names a set of overloaded declarations, some of which are
10397          templates, is a template-name.  However, such a name should
10398          be a template-name; otherwise, there is no way to form a
10399          template-id for the overloaded templates.  */
10400       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10401       if (TREE_CODE (fns) == OVERLOAD)
10402         for (fn = fns; fn; fn = OVL_NEXT (fn))
10403           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10404             break;
10405
10406       if (!fn)
10407         {
10408           /* The name does not name a template.  */
10409           cp_parser_error (parser, "expected template-name");
10410           return error_mark_node;
10411         }
10412     }
10413
10414   /* If DECL is dependent, and refers to a function, then just return
10415      its name; we will look it up again during template instantiation.  */
10416   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10417     {
10418       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10419       if (TYPE_P (scope) && dependent_type_p (scope))
10420         return identifier;
10421     }
10422
10423   return decl;
10424 }
10425
10426 /* Parse a template-argument-list.
10427
10428    template-argument-list:
10429      template-argument ... [opt]
10430      template-argument-list , template-argument ... [opt]
10431
10432    Returns a TREE_VEC containing the arguments.  */
10433
10434 static tree
10435 cp_parser_template_argument_list (cp_parser* parser)
10436 {
10437   tree fixed_args[10];
10438   unsigned n_args = 0;
10439   unsigned alloced = 10;
10440   tree *arg_ary = fixed_args;
10441   tree vec;
10442   bool saved_in_template_argument_list_p;
10443   bool saved_ice_p;
10444   bool saved_non_ice_p;
10445
10446   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10447   parser->in_template_argument_list_p = true;
10448   /* Even if the template-id appears in an integral
10449      constant-expression, the contents of the argument list do
10450      not.  */
10451   saved_ice_p = parser->integral_constant_expression_p;
10452   parser->integral_constant_expression_p = false;
10453   saved_non_ice_p = parser->non_integral_constant_expression_p;
10454   parser->non_integral_constant_expression_p = false;
10455   /* Parse the arguments.  */
10456   do
10457     {
10458       tree argument;
10459
10460       if (n_args)
10461         /* Consume the comma.  */
10462         cp_lexer_consume_token (parser->lexer);
10463
10464       /* Parse the template-argument.  */
10465       argument = cp_parser_template_argument (parser);
10466
10467       /* If the next token is an ellipsis, we're expanding a template
10468          argument pack. */
10469       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10470         {
10471           /* Consume the `...' token. */
10472           cp_lexer_consume_token (parser->lexer);
10473
10474           /* Make the argument into a TYPE_PACK_EXPANSION or
10475              EXPR_PACK_EXPANSION. */
10476           argument = make_pack_expansion (argument);
10477         }
10478
10479       if (n_args == alloced)
10480         {
10481           alloced *= 2;
10482
10483           if (arg_ary == fixed_args)
10484             {
10485               arg_ary = XNEWVEC (tree, alloced);
10486               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10487             }
10488           else
10489             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10490         }
10491       arg_ary[n_args++] = argument;
10492     }
10493   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10494
10495   vec = make_tree_vec (n_args);
10496
10497   while (n_args--)
10498     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10499
10500   if (arg_ary != fixed_args)
10501     free (arg_ary);
10502   parser->non_integral_constant_expression_p = saved_non_ice_p;
10503   parser->integral_constant_expression_p = saved_ice_p;
10504   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10505   return vec;
10506 }
10507
10508 /* Parse a template-argument.
10509
10510    template-argument:
10511      assignment-expression
10512      type-id
10513      id-expression
10514
10515    The representation is that of an assignment-expression, type-id, or
10516    id-expression -- except that the qualified id-expression is
10517    evaluated, so that the value returned is either a DECL or an
10518    OVERLOAD.
10519
10520    Although the standard says "assignment-expression", it forbids
10521    throw-expressions or assignments in the template argument.
10522    Therefore, we use "conditional-expression" instead.  */
10523
10524 static tree
10525 cp_parser_template_argument (cp_parser* parser)
10526 {
10527   tree argument;
10528   bool template_p;
10529   bool address_p;
10530   bool maybe_type_id = false;
10531   cp_token *token = NULL, *argument_start_token = NULL;
10532   cp_id_kind idk;
10533
10534   /* There's really no way to know what we're looking at, so we just
10535      try each alternative in order.
10536
10537        [temp.arg]
10538
10539        In a template-argument, an ambiguity between a type-id and an
10540        expression is resolved to a type-id, regardless of the form of
10541        the corresponding template-parameter.
10542
10543      Therefore, we try a type-id first.  */
10544   cp_parser_parse_tentatively (parser);
10545   argument = cp_parser_type_id (parser);
10546   /* If there was no error parsing the type-id but the next token is a
10547      '>>', our behavior depends on which dialect of C++ we're
10548      parsing. In C++98, we probably found a typo for '> >'. But there
10549      are type-id which are also valid expressions. For instance:
10550
10551      struct X { int operator >> (int); };
10552      template <int V> struct Foo {};
10553      Foo<X () >> 5> r;
10554
10555      Here 'X()' is a valid type-id of a function type, but the user just
10556      wanted to write the expression "X() >> 5". Thus, we remember that we
10557      found a valid type-id, but we still try to parse the argument as an
10558      expression to see what happens. 
10559
10560      In C++0x, the '>>' will be considered two separate '>'
10561      tokens.  */
10562   if (!cp_parser_error_occurred (parser)
10563       && cxx_dialect == cxx98
10564       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10565     {
10566       maybe_type_id = true;
10567       cp_parser_abort_tentative_parse (parser);
10568     }
10569   else
10570     {
10571       /* If the next token isn't a `,' or a `>', then this argument wasn't
10572       really finished. This means that the argument is not a valid
10573       type-id.  */
10574       if (!cp_parser_next_token_ends_template_argument_p (parser))
10575         cp_parser_error (parser, "expected template-argument");
10576       /* If that worked, we're done.  */
10577       if (cp_parser_parse_definitely (parser))
10578         return argument;
10579     }
10580   /* We're still not sure what the argument will be.  */
10581   cp_parser_parse_tentatively (parser);
10582   /* Try a template.  */
10583   argument_start_token = cp_lexer_peek_token (parser->lexer);
10584   argument = cp_parser_id_expression (parser,
10585                                       /*template_keyword_p=*/false,
10586                                       /*check_dependency_p=*/true,
10587                                       &template_p,
10588                                       /*declarator_p=*/false,
10589                                       /*optional_p=*/false);
10590   /* If the next token isn't a `,' or a `>', then this argument wasn't
10591      really finished.  */
10592   if (!cp_parser_next_token_ends_template_argument_p (parser))
10593     cp_parser_error (parser, "expected template-argument");
10594   if (!cp_parser_error_occurred (parser))
10595     {
10596       /* Figure out what is being referred to.  If the id-expression
10597          was for a class template specialization, then we will have a
10598          TYPE_DECL at this point.  There is no need to do name lookup
10599          at this point in that case.  */
10600       if (TREE_CODE (argument) != TYPE_DECL)
10601         argument = cp_parser_lookup_name (parser, argument,
10602                                           none_type,
10603                                           /*is_template=*/template_p,
10604                                           /*is_namespace=*/false,
10605                                           /*check_dependency=*/true,
10606                                           /*ambiguous_decls=*/NULL,
10607                                           argument_start_token->location);
10608       if (TREE_CODE (argument) != TEMPLATE_DECL
10609           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10610         cp_parser_error (parser, "expected template-name");
10611     }
10612   if (cp_parser_parse_definitely (parser))
10613     return argument;
10614   /* It must be a non-type argument.  There permitted cases are given
10615      in [temp.arg.nontype]:
10616
10617      -- an integral constant-expression of integral or enumeration
10618         type; or
10619
10620      -- the name of a non-type template-parameter; or
10621
10622      -- the name of an object or function with external linkage...
10623
10624      -- the address of an object or function with external linkage...
10625
10626      -- a pointer to member...  */
10627   /* Look for a non-type template parameter.  */
10628   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10629     {
10630       cp_parser_parse_tentatively (parser);
10631       argument = cp_parser_primary_expression (parser,
10632                                                /*address_p=*/false,
10633                                                /*cast_p=*/false,
10634                                                /*template_arg_p=*/true,
10635                                                &idk);
10636       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10637           || !cp_parser_next_token_ends_template_argument_p (parser))
10638         cp_parser_simulate_error (parser);
10639       if (cp_parser_parse_definitely (parser))
10640         return argument;
10641     }
10642
10643   /* If the next token is "&", the argument must be the address of an
10644      object or function with external linkage.  */
10645   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10646   if (address_p)
10647     cp_lexer_consume_token (parser->lexer);
10648   /* See if we might have an id-expression.  */
10649   token = cp_lexer_peek_token (parser->lexer);
10650   if (token->type == CPP_NAME
10651       || token->keyword == RID_OPERATOR
10652       || token->type == CPP_SCOPE
10653       || token->type == CPP_TEMPLATE_ID
10654       || token->type == CPP_NESTED_NAME_SPECIFIER)
10655     {
10656       cp_parser_parse_tentatively (parser);
10657       argument = cp_parser_primary_expression (parser,
10658                                                address_p,
10659                                                /*cast_p=*/false,
10660                                                /*template_arg_p=*/true,
10661                                                &idk);
10662       if (cp_parser_error_occurred (parser)
10663           || !cp_parser_next_token_ends_template_argument_p (parser))
10664         cp_parser_abort_tentative_parse (parser);
10665       else
10666         {
10667           if (TREE_CODE (argument) == INDIRECT_REF)
10668             {
10669               gcc_assert (REFERENCE_REF_P (argument));
10670               argument = TREE_OPERAND (argument, 0);
10671             }
10672
10673           if (TREE_CODE (argument) == VAR_DECL)
10674             {
10675               /* A variable without external linkage might still be a
10676                  valid constant-expression, so no error is issued here
10677                  if the external-linkage check fails.  */
10678               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10679                 cp_parser_simulate_error (parser);
10680             }
10681           else if (is_overloaded_fn (argument))
10682             /* All overloaded functions are allowed; if the external
10683                linkage test does not pass, an error will be issued
10684                later.  */
10685             ;
10686           else if (address_p
10687                    && (TREE_CODE (argument) == OFFSET_REF
10688                        || TREE_CODE (argument) == SCOPE_REF))
10689             /* A pointer-to-member.  */
10690             ;
10691           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10692             ;
10693           else
10694             cp_parser_simulate_error (parser);
10695
10696           if (cp_parser_parse_definitely (parser))
10697             {
10698               if (address_p)
10699                 argument = build_x_unary_op (ADDR_EXPR, argument,
10700                                              tf_warning_or_error);
10701               return argument;
10702             }
10703         }
10704     }
10705   /* If the argument started with "&", there are no other valid
10706      alternatives at this point.  */
10707   if (address_p)
10708     {
10709       cp_parser_error (parser, "invalid non-type template argument");
10710       return error_mark_node;
10711     }
10712
10713   /* If the argument wasn't successfully parsed as a type-id followed
10714      by '>>', the argument can only be a constant expression now.
10715      Otherwise, we try parsing the constant-expression tentatively,
10716      because the argument could really be a type-id.  */
10717   if (maybe_type_id)
10718     cp_parser_parse_tentatively (parser);
10719   argument = cp_parser_constant_expression (parser,
10720                                             /*allow_non_constant_p=*/false,
10721                                             /*non_constant_p=*/NULL);
10722   argument = fold_non_dependent_expr (argument);
10723   if (!maybe_type_id)
10724     return argument;
10725   if (!cp_parser_next_token_ends_template_argument_p (parser))
10726     cp_parser_error (parser, "expected template-argument");
10727   if (cp_parser_parse_definitely (parser))
10728     return argument;
10729   /* We did our best to parse the argument as a non type-id, but that
10730      was the only alternative that matched (albeit with a '>' after
10731      it). We can assume it's just a typo from the user, and a
10732      diagnostic will then be issued.  */
10733   return cp_parser_type_id (parser);
10734 }
10735
10736 /* Parse an explicit-instantiation.
10737
10738    explicit-instantiation:
10739      template declaration
10740
10741    Although the standard says `declaration', what it really means is:
10742
10743    explicit-instantiation:
10744      template decl-specifier-seq [opt] declarator [opt] ;
10745
10746    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10747    supposed to be allowed.  A defect report has been filed about this
10748    issue.
10749
10750    GNU Extension:
10751
10752    explicit-instantiation:
10753      storage-class-specifier template
10754        decl-specifier-seq [opt] declarator [opt] ;
10755      function-specifier template
10756        decl-specifier-seq [opt] declarator [opt] ;  */
10757
10758 static void
10759 cp_parser_explicit_instantiation (cp_parser* parser)
10760 {
10761   int declares_class_or_enum;
10762   cp_decl_specifier_seq decl_specifiers;
10763   tree extension_specifier = NULL_TREE;
10764   cp_token *token;
10765
10766   /* Look for an (optional) storage-class-specifier or
10767      function-specifier.  */
10768   if (cp_parser_allow_gnu_extensions_p (parser))
10769     {
10770       extension_specifier
10771         = cp_parser_storage_class_specifier_opt (parser);
10772       if (!extension_specifier)
10773         extension_specifier
10774           = cp_parser_function_specifier_opt (parser,
10775                                               /*decl_specs=*/NULL);
10776     }
10777
10778   /* Look for the `template' keyword.  */
10779   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10780   /* Let the front end know that we are processing an explicit
10781      instantiation.  */
10782   begin_explicit_instantiation ();
10783   /* [temp.explicit] says that we are supposed to ignore access
10784      control while processing explicit instantiation directives.  */
10785   push_deferring_access_checks (dk_no_check);
10786   /* Parse a decl-specifier-seq.  */
10787   token = cp_lexer_peek_token (parser->lexer);
10788   cp_parser_decl_specifier_seq (parser,
10789                                 CP_PARSER_FLAGS_OPTIONAL,
10790                                 &decl_specifiers,
10791                                 &declares_class_or_enum);
10792   /* If there was exactly one decl-specifier, and it declared a class,
10793      and there's no declarator, then we have an explicit type
10794      instantiation.  */
10795   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10796     {
10797       tree type;
10798
10799       type = check_tag_decl (&decl_specifiers);
10800       /* Turn access control back on for names used during
10801          template instantiation.  */
10802       pop_deferring_access_checks ();
10803       if (type)
10804         do_type_instantiation (type, extension_specifier,
10805                                /*complain=*/tf_error);
10806     }
10807   else
10808     {
10809       cp_declarator *declarator;
10810       tree decl;
10811
10812       /* Parse the declarator.  */
10813       declarator
10814         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10815                                 /*ctor_dtor_or_conv_p=*/NULL,
10816                                 /*parenthesized_p=*/NULL,
10817                                 /*member_p=*/false);
10818       if (declares_class_or_enum & 2)
10819         cp_parser_check_for_definition_in_return_type (declarator,
10820                                                        decl_specifiers.type,
10821                                                        decl_specifiers.type_location);
10822       if (declarator != cp_error_declarator)
10823         {
10824           decl = grokdeclarator (declarator, &decl_specifiers,
10825                                  NORMAL, 0, &decl_specifiers.attributes);
10826           /* Turn access control back on for names used during
10827              template instantiation.  */
10828           pop_deferring_access_checks ();
10829           /* Do the explicit instantiation.  */
10830           do_decl_instantiation (decl, extension_specifier);
10831         }
10832       else
10833         {
10834           pop_deferring_access_checks ();
10835           /* Skip the body of the explicit instantiation.  */
10836           cp_parser_skip_to_end_of_statement (parser);
10837         }
10838     }
10839   /* We're done with the instantiation.  */
10840   end_explicit_instantiation ();
10841
10842   cp_parser_consume_semicolon_at_end_of_statement (parser);
10843 }
10844
10845 /* Parse an explicit-specialization.
10846
10847    explicit-specialization:
10848      template < > declaration
10849
10850    Although the standard says `declaration', what it really means is:
10851
10852    explicit-specialization:
10853      template <> decl-specifier [opt] init-declarator [opt] ;
10854      template <> function-definition
10855      template <> explicit-specialization
10856      template <> template-declaration  */
10857
10858 static void
10859 cp_parser_explicit_specialization (cp_parser* parser)
10860 {
10861   bool need_lang_pop;
10862   cp_token *token = cp_lexer_peek_token (parser->lexer);
10863
10864   /* Look for the `template' keyword.  */
10865   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10866   /* Look for the `<'.  */
10867   cp_parser_require (parser, CPP_LESS, "%<<%>");
10868   /* Look for the `>'.  */
10869   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10870   /* We have processed another parameter list.  */
10871   ++parser->num_template_parameter_lists;
10872   /* [temp]
10873
10874      A template ... explicit specialization ... shall not have C
10875      linkage.  */
10876   if (current_lang_name == lang_name_c)
10877     {
10878       error ("%Htemplate specialization with C linkage", &token->location);
10879       /* Give it C++ linkage to avoid confusing other parts of the
10880          front end.  */
10881       push_lang_context (lang_name_cplusplus);
10882       need_lang_pop = true;
10883     }
10884   else
10885     need_lang_pop = false;
10886   /* Let the front end know that we are beginning a specialization.  */
10887   if (!begin_specialization ())
10888     {
10889       end_specialization ();
10890       return;
10891     }
10892
10893   /* If the next keyword is `template', we need to figure out whether
10894      or not we're looking a template-declaration.  */
10895   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10896     {
10897       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10898           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10899         cp_parser_template_declaration_after_export (parser,
10900                                                      /*member_p=*/false);
10901       else
10902         cp_parser_explicit_specialization (parser);
10903     }
10904   else
10905     /* Parse the dependent declaration.  */
10906     cp_parser_single_declaration (parser,
10907                                   /*checks=*/NULL,
10908                                   /*member_p=*/false,
10909                                   /*explicit_specialization_p=*/true,
10910                                   /*friend_p=*/NULL);
10911   /* We're done with the specialization.  */
10912   end_specialization ();
10913   /* For the erroneous case of a template with C linkage, we pushed an
10914      implicit C++ linkage scope; exit that scope now.  */
10915   if (need_lang_pop)
10916     pop_lang_context ();
10917   /* We're done with this parameter list.  */
10918   --parser->num_template_parameter_lists;
10919 }
10920
10921 /* Parse a type-specifier.
10922
10923    type-specifier:
10924      simple-type-specifier
10925      class-specifier
10926      enum-specifier
10927      elaborated-type-specifier
10928      cv-qualifier
10929
10930    GNU Extension:
10931
10932    type-specifier:
10933      __complex__
10934
10935    Returns a representation of the type-specifier.  For a
10936    class-specifier, enum-specifier, or elaborated-type-specifier, a
10937    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10938
10939    The parser flags FLAGS is used to control type-specifier parsing.
10940
10941    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10942    in a decl-specifier-seq.
10943
10944    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10945    class-specifier, enum-specifier, or elaborated-type-specifier, then
10946    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10947    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10948    zero.
10949
10950    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10951    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10952    is set to FALSE.  */
10953
10954 static tree
10955 cp_parser_type_specifier (cp_parser* parser,
10956                           cp_parser_flags flags,
10957                           cp_decl_specifier_seq *decl_specs,
10958                           bool is_declaration,
10959                           int* declares_class_or_enum,
10960                           bool* is_cv_qualifier)
10961 {
10962   tree type_spec = NULL_TREE;
10963   cp_token *token;
10964   enum rid keyword;
10965   cp_decl_spec ds = ds_last;
10966
10967   /* Assume this type-specifier does not declare a new type.  */
10968   if (declares_class_or_enum)
10969     *declares_class_or_enum = 0;
10970   /* And that it does not specify a cv-qualifier.  */
10971   if (is_cv_qualifier)
10972     *is_cv_qualifier = false;
10973   /* Peek at the next token.  */
10974   token = cp_lexer_peek_token (parser->lexer);
10975
10976   /* If we're looking at a keyword, we can use that to guide the
10977      production we choose.  */
10978   keyword = token->keyword;
10979   switch (keyword)
10980     {
10981     case RID_ENUM:
10982       /* Look for the enum-specifier.  */
10983       type_spec = cp_parser_enum_specifier (parser);
10984       /* If that worked, we're done.  */
10985       if (type_spec)
10986         {
10987           if (declares_class_or_enum)
10988             *declares_class_or_enum = 2;
10989           if (decl_specs)
10990             cp_parser_set_decl_spec_type (decl_specs,
10991                                           type_spec,
10992                                           token->location,
10993                                           /*user_defined_p=*/true);
10994           return type_spec;
10995         }
10996       else
10997         goto elaborated_type_specifier;
10998
10999       /* Any of these indicate either a class-specifier, or an
11000          elaborated-type-specifier.  */
11001     case RID_CLASS:
11002     case RID_STRUCT:
11003     case RID_UNION:
11004       /* Parse tentatively so that we can back up if we don't find a
11005          class-specifier.  */
11006       cp_parser_parse_tentatively (parser);
11007       /* Look for the class-specifier.  */
11008       type_spec = cp_parser_class_specifier (parser);
11009       /* If that worked, we're done.  */
11010       if (cp_parser_parse_definitely (parser))
11011         {
11012           if (declares_class_or_enum)
11013             *declares_class_or_enum = 2;
11014           if (decl_specs)
11015             cp_parser_set_decl_spec_type (decl_specs,
11016                                           type_spec,
11017                                           token->location,
11018                                           /*user_defined_p=*/true);
11019           return type_spec;
11020         }
11021
11022       /* Fall through.  */
11023     elaborated_type_specifier:
11024       /* We're declaring (not defining) a class or enum.  */
11025       if (declares_class_or_enum)
11026         *declares_class_or_enum = 1;
11027
11028       /* Fall through.  */
11029     case RID_TYPENAME:
11030       /* Look for an elaborated-type-specifier.  */
11031       type_spec
11032         = (cp_parser_elaborated_type_specifier
11033            (parser,
11034             decl_specs && decl_specs->specs[(int) ds_friend],
11035             is_declaration));
11036       if (decl_specs)
11037         cp_parser_set_decl_spec_type (decl_specs,
11038                                       type_spec,
11039                                       token->location,
11040                                       /*user_defined_p=*/true);
11041       return type_spec;
11042
11043     case RID_CONST:
11044       ds = ds_const;
11045       if (is_cv_qualifier)
11046         *is_cv_qualifier = true;
11047       break;
11048
11049     case RID_VOLATILE:
11050       ds = ds_volatile;
11051       if (is_cv_qualifier)
11052         *is_cv_qualifier = true;
11053       break;
11054
11055     case RID_RESTRICT:
11056       ds = ds_restrict;
11057       if (is_cv_qualifier)
11058         *is_cv_qualifier = true;
11059       break;
11060
11061     case RID_COMPLEX:
11062       /* The `__complex__' keyword is a GNU extension.  */
11063       ds = ds_complex;
11064       break;
11065
11066     default:
11067       break;
11068     }
11069
11070   /* Handle simple keywords.  */
11071   if (ds != ds_last)
11072     {
11073       if (decl_specs)
11074         {
11075           ++decl_specs->specs[(int)ds];
11076           decl_specs->any_specifiers_p = true;
11077         }
11078       return cp_lexer_consume_token (parser->lexer)->u.value;
11079     }
11080
11081   /* If we do not already have a type-specifier, assume we are looking
11082      at a simple-type-specifier.  */
11083   type_spec = cp_parser_simple_type_specifier (parser,
11084                                                decl_specs,
11085                                                flags);
11086
11087   /* If we didn't find a type-specifier, and a type-specifier was not
11088      optional in this context, issue an error message.  */
11089   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11090     {
11091       cp_parser_error (parser, "expected type specifier");
11092       return error_mark_node;
11093     }
11094
11095   return type_spec;
11096 }
11097
11098 /* Parse a simple-type-specifier.
11099
11100    simple-type-specifier:
11101      :: [opt] nested-name-specifier [opt] type-name
11102      :: [opt] nested-name-specifier template template-id
11103      char
11104      wchar_t
11105      bool
11106      short
11107      int
11108      long
11109      signed
11110      unsigned
11111      float
11112      double
11113      void
11114
11115    C++0x Extension:
11116
11117    simple-type-specifier:
11118      auto
11119      decltype ( expression )   
11120      char16_t
11121      char32_t
11122
11123    GNU Extension:
11124
11125    simple-type-specifier:
11126      __typeof__ unary-expression
11127      __typeof__ ( type-id )
11128
11129    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11130    appropriately updated.  */
11131
11132 static tree
11133 cp_parser_simple_type_specifier (cp_parser* parser,
11134                                  cp_decl_specifier_seq *decl_specs,
11135                                  cp_parser_flags flags)
11136 {
11137   tree type = NULL_TREE;
11138   cp_token *token;
11139
11140   /* Peek at the next token.  */
11141   token = cp_lexer_peek_token (parser->lexer);
11142
11143   /* If we're looking at a keyword, things are easy.  */
11144   switch (token->keyword)
11145     {
11146     case RID_CHAR:
11147       if (decl_specs)
11148         decl_specs->explicit_char_p = true;
11149       type = char_type_node;
11150       break;
11151     case RID_CHAR16:
11152       type = char16_type_node;
11153       break;
11154     case RID_CHAR32:
11155       type = char32_type_node;
11156       break;
11157     case RID_WCHAR:
11158       type = wchar_type_node;
11159       break;
11160     case RID_BOOL:
11161       type = boolean_type_node;
11162       break;
11163     case RID_SHORT:
11164       if (decl_specs)
11165         ++decl_specs->specs[(int) ds_short];
11166       type = short_integer_type_node;
11167       break;
11168     case RID_INT:
11169       if (decl_specs)
11170         decl_specs->explicit_int_p = true;
11171       type = integer_type_node;
11172       break;
11173     case RID_LONG:
11174       if (decl_specs)
11175         ++decl_specs->specs[(int) ds_long];
11176       type = long_integer_type_node;
11177       break;
11178     case RID_SIGNED:
11179       if (decl_specs)
11180         ++decl_specs->specs[(int) ds_signed];
11181       type = integer_type_node;
11182       break;
11183     case RID_UNSIGNED:
11184       if (decl_specs)
11185         ++decl_specs->specs[(int) ds_unsigned];
11186       type = unsigned_type_node;
11187       break;
11188     case RID_FLOAT:
11189       type = float_type_node;
11190       break;
11191     case RID_DOUBLE:
11192       type = double_type_node;
11193       break;
11194     case RID_VOID:
11195       type = void_type_node;
11196       break;
11197       
11198     case RID_AUTO:
11199       maybe_warn_cpp0x ("C++0x auto");
11200       type = make_auto ();
11201       break;
11202
11203     case RID_DECLTYPE:
11204       /* Parse the `decltype' type.  */
11205       type = cp_parser_decltype (parser);
11206
11207       if (decl_specs)
11208         cp_parser_set_decl_spec_type (decl_specs, type,
11209                                       token->location,
11210                                       /*user_defined_p=*/true);
11211
11212       return type;
11213
11214     case RID_TYPEOF:
11215       /* Consume the `typeof' token.  */
11216       cp_lexer_consume_token (parser->lexer);
11217       /* Parse the operand to `typeof'.  */
11218       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11219       /* If it is not already a TYPE, take its type.  */
11220       if (!TYPE_P (type))
11221         type = finish_typeof (type);
11222
11223       if (decl_specs)
11224         cp_parser_set_decl_spec_type (decl_specs, type,
11225                                       token->location,
11226                                       /*user_defined_p=*/true);
11227
11228       return type;
11229
11230     default:
11231       break;
11232     }
11233
11234   /* If the type-specifier was for a built-in type, we're done.  */
11235   if (type)
11236     {
11237       tree id;
11238
11239       /* Record the type.  */
11240       if (decl_specs
11241           && (token->keyword != RID_SIGNED
11242               && token->keyword != RID_UNSIGNED
11243               && token->keyword != RID_SHORT
11244               && token->keyword != RID_LONG))
11245         cp_parser_set_decl_spec_type (decl_specs,
11246                                       type,
11247                                       token->location,
11248                                       /*user_defined=*/false);
11249       if (decl_specs)
11250         decl_specs->any_specifiers_p = true;
11251
11252       /* Consume the token.  */
11253       id = cp_lexer_consume_token (parser->lexer)->u.value;
11254
11255       /* There is no valid C++ program where a non-template type is
11256          followed by a "<".  That usually indicates that the user thought
11257          that the type was a template.  */
11258       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11259
11260       return TYPE_NAME (type);
11261     }
11262
11263   /* The type-specifier must be a user-defined type.  */
11264   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11265     {
11266       bool qualified_p;
11267       bool global_p;
11268
11269       /* Don't gobble tokens or issue error messages if this is an
11270          optional type-specifier.  */
11271       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11272         cp_parser_parse_tentatively (parser);
11273
11274       /* Look for the optional `::' operator.  */
11275       global_p
11276         = (cp_parser_global_scope_opt (parser,
11277                                        /*current_scope_valid_p=*/false)
11278            != NULL_TREE);
11279       /* Look for the nested-name specifier.  */
11280       qualified_p
11281         = (cp_parser_nested_name_specifier_opt (parser,
11282                                                 /*typename_keyword_p=*/false,
11283                                                 /*check_dependency_p=*/true,
11284                                                 /*type_p=*/false,
11285                                                 /*is_declaration=*/false)
11286            != NULL_TREE);
11287       token = cp_lexer_peek_token (parser->lexer);
11288       /* If we have seen a nested-name-specifier, and the next token
11289          is `template', then we are using the template-id production.  */
11290       if (parser->scope
11291           && cp_parser_optional_template_keyword (parser))
11292         {
11293           /* Look for the template-id.  */
11294           type = cp_parser_template_id (parser,
11295                                         /*template_keyword_p=*/true,
11296                                         /*check_dependency_p=*/true,
11297                                         /*is_declaration=*/false);
11298           /* If the template-id did not name a type, we are out of
11299              luck.  */
11300           if (TREE_CODE (type) != TYPE_DECL)
11301             {
11302               cp_parser_error (parser, "expected template-id for type");
11303               type = NULL_TREE;
11304             }
11305         }
11306       /* Otherwise, look for a type-name.  */
11307       else
11308         type = cp_parser_type_name (parser);
11309       /* Keep track of all name-lookups performed in class scopes.  */
11310       if (type
11311           && !global_p
11312           && !qualified_p
11313           && TREE_CODE (type) == TYPE_DECL
11314           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11315         maybe_note_name_used_in_class (DECL_NAME (type), type);
11316       /* If it didn't work out, we don't have a TYPE.  */
11317       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11318           && !cp_parser_parse_definitely (parser))
11319         type = NULL_TREE;
11320       if (type && decl_specs)
11321         cp_parser_set_decl_spec_type (decl_specs, type,
11322                                       token->location,
11323                                       /*user_defined=*/true);
11324     }
11325
11326   /* If we didn't get a type-name, issue an error message.  */
11327   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11328     {
11329       cp_parser_error (parser, "expected type-name");
11330       return error_mark_node;
11331     }
11332
11333   /* There is no valid C++ program where a non-template type is
11334      followed by a "<".  That usually indicates that the user thought
11335      that the type was a template.  */
11336   if (type && type != error_mark_node)
11337     {
11338       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11339          If it is, then the '<'...'>' enclose protocol names rather than
11340          template arguments, and so everything is fine.  */
11341       if (c_dialect_objc ()
11342           && (objc_is_id (type) || objc_is_class_name (type)))
11343         {
11344           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11345           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11346
11347           /* Clobber the "unqualified" type previously entered into
11348              DECL_SPECS with the new, improved protocol-qualified version.  */
11349           if (decl_specs)
11350             decl_specs->type = qual_type;
11351
11352           return qual_type;
11353         }
11354
11355       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11356                                                token->location);
11357     }
11358
11359   return type;
11360 }
11361
11362 /* Parse a type-name.
11363
11364    type-name:
11365      class-name
11366      enum-name
11367      typedef-name
11368
11369    enum-name:
11370      identifier
11371
11372    typedef-name:
11373      identifier
11374
11375    Returns a TYPE_DECL for the type.  */
11376
11377 static tree
11378 cp_parser_type_name (cp_parser* parser)
11379 {
11380   tree type_decl;
11381
11382   /* We can't know yet whether it is a class-name or not.  */
11383   cp_parser_parse_tentatively (parser);
11384   /* Try a class-name.  */
11385   type_decl = cp_parser_class_name (parser,
11386                                     /*typename_keyword_p=*/false,
11387                                     /*template_keyword_p=*/false,
11388                                     none_type,
11389                                     /*check_dependency_p=*/true,
11390                                     /*class_head_p=*/false,
11391                                     /*is_declaration=*/false);
11392   /* If it's not a class-name, keep looking.  */
11393   if (!cp_parser_parse_definitely (parser))
11394     {
11395       /* It must be a typedef-name or an enum-name.  */
11396       return cp_parser_nonclass_name (parser);
11397     }
11398
11399   return type_decl;
11400 }
11401
11402 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11403
11404    enum-name:
11405      identifier
11406
11407    typedef-name:
11408      identifier
11409
11410    Returns a TYPE_DECL for the type.  */
11411
11412 static tree
11413 cp_parser_nonclass_name (cp_parser* parser)
11414 {
11415   tree type_decl;
11416   tree identifier;
11417
11418   cp_token *token = cp_lexer_peek_token (parser->lexer);
11419   identifier = cp_parser_identifier (parser);
11420   if (identifier == error_mark_node)
11421     return error_mark_node;
11422
11423   /* Look up the type-name.  */
11424   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11425
11426   if (TREE_CODE (type_decl) != TYPE_DECL
11427       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11428     {
11429       /* See if this is an Objective-C type.  */
11430       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11431       tree type = objc_get_protocol_qualified_type (identifier, protos);
11432       if (type)
11433         type_decl = TYPE_NAME (type);
11434     }
11435   
11436   /* Issue an error if we did not find a type-name.  */
11437   if (TREE_CODE (type_decl) != TYPE_DECL)
11438     {
11439       if (!cp_parser_simulate_error (parser))
11440         cp_parser_name_lookup_error (parser, identifier, type_decl,
11441                                      "is not a type", token->location);
11442       return error_mark_node;
11443     }
11444   /* Remember that the name was used in the definition of the
11445      current class so that we can check later to see if the
11446      meaning would have been different after the class was
11447      entirely defined.  */
11448   else if (type_decl != error_mark_node
11449            && !parser->scope)
11450     maybe_note_name_used_in_class (identifier, type_decl);
11451   
11452   return type_decl;
11453 }
11454
11455 /* Parse an elaborated-type-specifier.  Note that the grammar given
11456    here incorporates the resolution to DR68.
11457
11458    elaborated-type-specifier:
11459      class-key :: [opt] nested-name-specifier [opt] identifier
11460      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11461      enum-key :: [opt] nested-name-specifier [opt] identifier
11462      typename :: [opt] nested-name-specifier identifier
11463      typename :: [opt] nested-name-specifier template [opt]
11464        template-id
11465
11466    GNU extension:
11467
11468    elaborated-type-specifier:
11469      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11470      class-key attributes :: [opt] nested-name-specifier [opt]
11471                template [opt] template-id
11472      enum attributes :: [opt] nested-name-specifier [opt] identifier
11473
11474    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11475    declared `friend'.  If IS_DECLARATION is TRUE, then this
11476    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11477    something is being declared.
11478
11479    Returns the TYPE specified.  */
11480
11481 static tree
11482 cp_parser_elaborated_type_specifier (cp_parser* parser,
11483                                      bool is_friend,
11484                                      bool is_declaration)
11485 {
11486   enum tag_types tag_type;
11487   tree identifier;
11488   tree type = NULL_TREE;
11489   tree attributes = NULL_TREE;
11490   cp_token *token = NULL;
11491
11492   /* See if we're looking at the `enum' keyword.  */
11493   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11494     {
11495       /* Consume the `enum' token.  */
11496       cp_lexer_consume_token (parser->lexer);
11497       /* Remember that it's an enumeration type.  */
11498       tag_type = enum_type;
11499       /* Parse the optional `struct' or `class' key (for C++0x scoped
11500          enums).  */
11501       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11502           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11503         {
11504           if (cxx_dialect == cxx98)
11505             maybe_warn_cpp0x ("scoped enums");
11506
11507           /* Consume the `struct' or `class'.  */
11508           cp_lexer_consume_token (parser->lexer);
11509         }
11510       /* Parse the attributes.  */
11511       attributes = cp_parser_attributes_opt (parser);
11512     }
11513   /* Or, it might be `typename'.  */
11514   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11515                                            RID_TYPENAME))
11516     {
11517       /* Consume the `typename' token.  */
11518       cp_lexer_consume_token (parser->lexer);
11519       /* Remember that it's a `typename' type.  */
11520       tag_type = typename_type;
11521       /* The `typename' keyword is only allowed in templates.  */
11522       if (!processing_template_decl)
11523         permerror (input_location, "using %<typename%> outside of template");
11524     }
11525   /* Otherwise it must be a class-key.  */
11526   else
11527     {
11528       tag_type = cp_parser_class_key (parser);
11529       if (tag_type == none_type)
11530         return error_mark_node;
11531       /* Parse the attributes.  */
11532       attributes = cp_parser_attributes_opt (parser);
11533     }
11534
11535   /* Look for the `::' operator.  */
11536   cp_parser_global_scope_opt (parser,
11537                               /*current_scope_valid_p=*/false);
11538   /* Look for the nested-name-specifier.  */
11539   if (tag_type == typename_type)
11540     {
11541       if (!cp_parser_nested_name_specifier (parser,
11542                                            /*typename_keyword_p=*/true,
11543                                            /*check_dependency_p=*/true,
11544                                            /*type_p=*/true,
11545                                             is_declaration))
11546         return error_mark_node;
11547     }
11548   else
11549     /* Even though `typename' is not present, the proposed resolution
11550        to Core Issue 180 says that in `class A<T>::B', `B' should be
11551        considered a type-name, even if `A<T>' is dependent.  */
11552     cp_parser_nested_name_specifier_opt (parser,
11553                                          /*typename_keyword_p=*/true,
11554                                          /*check_dependency_p=*/true,
11555                                          /*type_p=*/true,
11556                                          is_declaration);
11557  /* For everything but enumeration types, consider a template-id.
11558     For an enumeration type, consider only a plain identifier.  */
11559   if (tag_type != enum_type)
11560     {
11561       bool template_p = false;
11562       tree decl;
11563
11564       /* Allow the `template' keyword.  */
11565       template_p = cp_parser_optional_template_keyword (parser);
11566       /* If we didn't see `template', we don't know if there's a
11567          template-id or not.  */
11568       if (!template_p)
11569         cp_parser_parse_tentatively (parser);
11570       /* Parse the template-id.  */
11571       token = cp_lexer_peek_token (parser->lexer);
11572       decl = cp_parser_template_id (parser, template_p,
11573                                     /*check_dependency_p=*/true,
11574                                     is_declaration);
11575       /* If we didn't find a template-id, look for an ordinary
11576          identifier.  */
11577       if (!template_p && !cp_parser_parse_definitely (parser))
11578         ;
11579       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11580          in effect, then we must assume that, upon instantiation, the
11581          template will correspond to a class.  */
11582       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11583                && tag_type == typename_type)
11584         type = make_typename_type (parser->scope, decl,
11585                                    typename_type,
11586                                    /*complain=*/tf_error);
11587       /* If the `typename' keyword is in effect and DECL is not a type
11588          decl. Then type is non existant.   */
11589       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11590         type = NULL_TREE; 
11591       else 
11592         type = TREE_TYPE (decl);
11593     }
11594
11595   if (!type)
11596     {
11597       token = cp_lexer_peek_token (parser->lexer);
11598       identifier = cp_parser_identifier (parser);
11599
11600       if (identifier == error_mark_node)
11601         {
11602           parser->scope = NULL_TREE;
11603           return error_mark_node;
11604         }
11605
11606       /* For a `typename', we needn't call xref_tag.  */
11607       if (tag_type == typename_type
11608           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11609         return cp_parser_make_typename_type (parser, parser->scope,
11610                                              identifier,
11611                                              token->location);
11612       /* Look up a qualified name in the usual way.  */
11613       if (parser->scope)
11614         {
11615           tree decl;
11616           tree ambiguous_decls;
11617
11618           decl = cp_parser_lookup_name (parser, identifier,
11619                                         tag_type,
11620                                         /*is_template=*/false,
11621                                         /*is_namespace=*/false,
11622                                         /*check_dependency=*/true,
11623                                         &ambiguous_decls,
11624                                         token->location);
11625
11626           /* If the lookup was ambiguous, an error will already have been
11627              issued.  */
11628           if (ambiguous_decls)
11629             return error_mark_node;
11630
11631           /* If we are parsing friend declaration, DECL may be a
11632              TEMPLATE_DECL tree node here.  However, we need to check
11633              whether this TEMPLATE_DECL results in valid code.  Consider
11634              the following example:
11635
11636                namespace N {
11637                  template <class T> class C {};
11638                }
11639                class X {
11640                  template <class T> friend class N::C; // #1, valid code
11641                };
11642                template <class T> class Y {
11643                  friend class N::C;                    // #2, invalid code
11644                };
11645
11646              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11647              name lookup of `N::C'.  We see that friend declaration must
11648              be template for the code to be valid.  Note that
11649              processing_template_decl does not work here since it is
11650              always 1 for the above two cases.  */
11651
11652           decl = (cp_parser_maybe_treat_template_as_class
11653                   (decl, /*tag_name_p=*/is_friend
11654                          && parser->num_template_parameter_lists));
11655
11656           if (TREE_CODE (decl) != TYPE_DECL)
11657             {
11658               cp_parser_diagnose_invalid_type_name (parser,
11659                                                     parser->scope,
11660                                                     identifier,
11661                                                     token->location);
11662               return error_mark_node;
11663             }
11664
11665           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11666             {
11667               bool allow_template = (parser->num_template_parameter_lists
11668                                       || DECL_SELF_REFERENCE_P (decl));
11669               type = check_elaborated_type_specifier (tag_type, decl, 
11670                                                       allow_template);
11671
11672               if (type == error_mark_node)
11673                 return error_mark_node;
11674             }
11675
11676           /* Forward declarations of nested types, such as
11677
11678                class C1::C2;
11679                class C1::C2::C3;
11680
11681              are invalid unless all components preceding the final '::'
11682              are complete.  If all enclosing types are complete, these
11683              declarations become merely pointless.
11684
11685              Invalid forward declarations of nested types are errors
11686              caught elsewhere in parsing.  Those that are pointless arrive
11687              here.  */
11688
11689           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11690               && !is_friend && !processing_explicit_instantiation)
11691             warning (0, "declaration %qD does not declare anything", decl);
11692
11693           type = TREE_TYPE (decl);
11694         }
11695       else
11696         {
11697           /* An elaborated-type-specifier sometimes introduces a new type and
11698              sometimes names an existing type.  Normally, the rule is that it
11699              introduces a new type only if there is not an existing type of
11700              the same name already in scope.  For example, given:
11701
11702                struct S {};
11703                void f() { struct S s; }
11704
11705              the `struct S' in the body of `f' is the same `struct S' as in
11706              the global scope; the existing definition is used.  However, if
11707              there were no global declaration, this would introduce a new
11708              local class named `S'.
11709
11710              An exception to this rule applies to the following code:
11711
11712                namespace N { struct S; }
11713
11714              Here, the elaborated-type-specifier names a new type
11715              unconditionally; even if there is already an `S' in the
11716              containing scope this declaration names a new type.
11717              This exception only applies if the elaborated-type-specifier
11718              forms the complete declaration:
11719
11720                [class.name]
11721
11722                A declaration consisting solely of `class-key identifier ;' is
11723                either a redeclaration of the name in the current scope or a
11724                forward declaration of the identifier as a class name.  It
11725                introduces the name into the current scope.
11726
11727              We are in this situation precisely when the next token is a `;'.
11728
11729              An exception to the exception is that a `friend' declaration does
11730              *not* name a new type; i.e., given:
11731
11732                struct S { friend struct T; };
11733
11734              `T' is not a new type in the scope of `S'.
11735
11736              Also, `new struct S' or `sizeof (struct S)' never results in the
11737              definition of a new type; a new type can only be declared in a
11738              declaration context.  */
11739
11740           tag_scope ts;
11741           bool template_p;
11742
11743           if (is_friend)
11744             /* Friends have special name lookup rules.  */
11745             ts = ts_within_enclosing_non_class;
11746           else if (is_declaration
11747                    && cp_lexer_next_token_is (parser->lexer,
11748                                               CPP_SEMICOLON))
11749             /* This is a `class-key identifier ;' */
11750             ts = ts_current;
11751           else
11752             ts = ts_global;
11753
11754           template_p =
11755             (parser->num_template_parameter_lists
11756              && (cp_parser_next_token_starts_class_definition_p (parser)
11757                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11758           /* An unqualified name was used to reference this type, so
11759              there were no qualifying templates.  */
11760           if (!cp_parser_check_template_parameters (parser,
11761                                                     /*num_templates=*/0,
11762                                                     token->location))
11763             return error_mark_node;
11764           type = xref_tag (tag_type, identifier, ts, template_p);
11765         }
11766     }
11767
11768   if (type == error_mark_node)
11769     return error_mark_node;
11770
11771   /* Allow attributes on forward declarations of classes.  */
11772   if (attributes)
11773     {
11774       if (TREE_CODE (type) == TYPENAME_TYPE)
11775         warning (OPT_Wattributes,
11776                  "attributes ignored on uninstantiated type");
11777       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11778                && ! processing_explicit_instantiation)
11779         warning (OPT_Wattributes,
11780                  "attributes ignored on template instantiation");
11781       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11782         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11783       else
11784         warning (OPT_Wattributes,
11785                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11786     }
11787
11788   if (tag_type != enum_type)
11789     cp_parser_check_class_key (tag_type, type);
11790
11791   /* A "<" cannot follow an elaborated type specifier.  If that
11792      happens, the user was probably trying to form a template-id.  */
11793   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11794
11795   return type;
11796 }
11797
11798 /* Parse an enum-specifier.
11799
11800    enum-specifier:
11801      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11802
11803    enum-key:
11804      enum
11805      enum class   [C++0x]
11806      enum struct  [C++0x]
11807
11808    enum-base:   [C++0x]
11809      : type-specifier-seq
11810
11811    GNU Extensions:
11812      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11813        { enumerator-list [opt] }attributes[opt]
11814
11815    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11816    if the token stream isn't an enum-specifier after all.  */
11817
11818 static tree
11819 cp_parser_enum_specifier (cp_parser* parser)
11820 {
11821   tree identifier;
11822   tree type;
11823   tree attributes;
11824   bool scoped_enum_p = false;
11825   bool has_underlying_type = false;
11826   tree underlying_type = NULL_TREE;
11827
11828   /* Parse tentatively so that we can back up if we don't find a
11829      enum-specifier.  */
11830   cp_parser_parse_tentatively (parser);
11831
11832   /* Caller guarantees that the current token is 'enum', an identifier
11833      possibly follows, and the token after that is an opening brace.
11834      If we don't have an identifier, fabricate an anonymous name for
11835      the enumeration being defined.  */
11836   cp_lexer_consume_token (parser->lexer);
11837
11838   /* Parse the "class" or "struct", which indicates a scoped
11839      enumeration type in C++0x.  */
11840   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11841       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11842     {
11843       if (cxx_dialect == cxx98)
11844         maybe_warn_cpp0x ("scoped enums");
11845
11846       /* Consume the `struct' or `class' token.  */
11847       cp_lexer_consume_token (parser->lexer);
11848
11849       scoped_enum_p = true;
11850     }
11851
11852   attributes = cp_parser_attributes_opt (parser);
11853
11854   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11855     identifier = cp_parser_identifier (parser);
11856   else
11857     identifier = make_anon_name ();
11858
11859   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11860   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11861     {
11862       cp_decl_specifier_seq type_specifiers;
11863
11864       /* At this point this is surely not elaborated type specifier.  */
11865       if (!cp_parser_parse_definitely (parser))
11866         return NULL_TREE;
11867
11868       if (cxx_dialect == cxx98)
11869         maybe_warn_cpp0x ("scoped enums");
11870
11871       /* Consume the `:'.  */
11872       cp_lexer_consume_token (parser->lexer);
11873
11874       has_underlying_type = true;
11875
11876       /* Parse the type-specifier-seq.  */
11877       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11878                                     &type_specifiers);
11879
11880       /* If that didn't work, stop.  */
11881       if (type_specifiers.type != error_mark_node)
11882         {
11883           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11884                                             /*initialized=*/0, NULL);
11885           if (underlying_type == error_mark_node)
11886             underlying_type = NULL_TREE;
11887         }
11888     }
11889
11890   /* Look for the `{' but don't consume it yet.  */
11891   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11892     {
11893       cp_parser_error (parser, "expected %<{%>");
11894       if (has_underlying_type)
11895         return NULL_TREE;
11896     }
11897
11898   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11899     return NULL_TREE;
11900
11901   /* Issue an error message if type-definitions are forbidden here.  */
11902   if (!cp_parser_check_type_definition (parser))
11903     type = error_mark_node;
11904   else
11905     /* Create the new type.  We do this before consuming the opening
11906        brace so the enum will be recorded as being on the line of its
11907        tag (or the 'enum' keyword, if there is no tag).  */
11908     type = start_enum (identifier, underlying_type, scoped_enum_p);
11909   
11910   /* Consume the opening brace.  */
11911   cp_lexer_consume_token (parser->lexer);
11912
11913   if (type == error_mark_node)
11914     {
11915       cp_parser_skip_to_end_of_block_or_statement (parser);
11916       return error_mark_node;
11917     }
11918
11919   /* If the next token is not '}', then there are some enumerators.  */
11920   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11921     cp_parser_enumerator_list (parser, type);
11922
11923   /* Consume the final '}'.  */
11924   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11925
11926   /* Look for trailing attributes to apply to this enumeration, and
11927      apply them if appropriate.  */
11928   if (cp_parser_allow_gnu_extensions_p (parser))
11929     {
11930       tree trailing_attr = cp_parser_attributes_opt (parser);
11931       trailing_attr = chainon (trailing_attr, attributes);
11932       cplus_decl_attributes (&type,
11933                              trailing_attr,
11934                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11935     }
11936
11937   /* Finish up the enumeration.  */
11938   finish_enum (type);
11939
11940   return type;
11941 }
11942
11943 /* Parse an enumerator-list.  The enumerators all have the indicated
11944    TYPE.
11945
11946    enumerator-list:
11947      enumerator-definition
11948      enumerator-list , enumerator-definition  */
11949
11950 static void
11951 cp_parser_enumerator_list (cp_parser* parser, tree type)
11952 {
11953   while (true)
11954     {
11955       /* Parse an enumerator-definition.  */
11956       cp_parser_enumerator_definition (parser, type);
11957
11958       /* If the next token is not a ',', we've reached the end of
11959          the list.  */
11960       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11961         break;
11962       /* Otherwise, consume the `,' and keep going.  */
11963       cp_lexer_consume_token (parser->lexer);
11964       /* If the next token is a `}', there is a trailing comma.  */
11965       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11966         {
11967           if (!in_system_header)
11968             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11969           break;
11970         }
11971     }
11972 }
11973
11974 /* Parse an enumerator-definition.  The enumerator has the indicated
11975    TYPE.
11976
11977    enumerator-definition:
11978      enumerator
11979      enumerator = constant-expression
11980
11981    enumerator:
11982      identifier  */
11983
11984 static void
11985 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11986 {
11987   tree identifier;
11988   tree value;
11989
11990   /* Look for the identifier.  */
11991   identifier = cp_parser_identifier (parser);
11992   if (identifier == error_mark_node)
11993     return;
11994
11995   /* If the next token is an '=', then there is an explicit value.  */
11996   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11997     {
11998       /* Consume the `=' token.  */
11999       cp_lexer_consume_token (parser->lexer);
12000       /* Parse the value.  */
12001       value = cp_parser_constant_expression (parser,
12002                                              /*allow_non_constant_p=*/false,
12003                                              NULL);
12004     }
12005   else
12006     value = NULL_TREE;
12007
12008   /* Create the enumerator.  */
12009   build_enumerator (identifier, value, type);
12010 }
12011
12012 /* Parse a namespace-name.
12013
12014    namespace-name:
12015      original-namespace-name
12016      namespace-alias
12017
12018    Returns the NAMESPACE_DECL for the namespace.  */
12019
12020 static tree
12021 cp_parser_namespace_name (cp_parser* parser)
12022 {
12023   tree identifier;
12024   tree namespace_decl;
12025
12026   cp_token *token = cp_lexer_peek_token (parser->lexer);
12027
12028   /* Get the name of the namespace.  */
12029   identifier = cp_parser_identifier (parser);
12030   if (identifier == error_mark_node)
12031     return error_mark_node;
12032
12033   /* Look up the identifier in the currently active scope.  Look only
12034      for namespaces, due to:
12035
12036        [basic.lookup.udir]
12037
12038        When looking up a namespace-name in a using-directive or alias
12039        definition, only namespace names are considered.
12040
12041      And:
12042
12043        [basic.lookup.qual]
12044
12045        During the lookup of a name preceding the :: scope resolution
12046        operator, object, function, and enumerator names are ignored.
12047
12048      (Note that cp_parser_qualifying_entity only calls this
12049      function if the token after the name is the scope resolution
12050      operator.)  */
12051   namespace_decl = cp_parser_lookup_name (parser, identifier,
12052                                           none_type,
12053                                           /*is_template=*/false,
12054                                           /*is_namespace=*/true,
12055                                           /*check_dependency=*/true,
12056                                           /*ambiguous_decls=*/NULL,
12057                                           token->location);
12058   /* If it's not a namespace, issue an error.  */
12059   if (namespace_decl == error_mark_node
12060       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12061     {
12062       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12063         error ("%H%qD is not a namespace-name", &token->location, identifier);
12064       cp_parser_error (parser, "expected namespace-name");
12065       namespace_decl = error_mark_node;
12066     }
12067
12068   return namespace_decl;
12069 }
12070
12071 /* Parse a namespace-definition.
12072
12073    namespace-definition:
12074      named-namespace-definition
12075      unnamed-namespace-definition
12076
12077    named-namespace-definition:
12078      original-namespace-definition
12079      extension-namespace-definition
12080
12081    original-namespace-definition:
12082      namespace identifier { namespace-body }
12083
12084    extension-namespace-definition:
12085      namespace original-namespace-name { namespace-body }
12086
12087    unnamed-namespace-definition:
12088      namespace { namespace-body } */
12089
12090 static void
12091 cp_parser_namespace_definition (cp_parser* parser)
12092 {
12093   tree identifier, attribs;
12094   bool has_visibility;
12095   bool is_inline;
12096
12097   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12098     {
12099       is_inline = true;
12100       cp_lexer_consume_token (parser->lexer);
12101     }
12102   else
12103     is_inline = false;
12104
12105   /* Look for the `namespace' keyword.  */
12106   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12107
12108   /* Get the name of the namespace.  We do not attempt to distinguish
12109      between an original-namespace-definition and an
12110      extension-namespace-definition at this point.  The semantic
12111      analysis routines are responsible for that.  */
12112   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12113     identifier = cp_parser_identifier (parser);
12114   else
12115     identifier = NULL_TREE;
12116
12117   /* Parse any specified attributes.  */
12118   attribs = cp_parser_attributes_opt (parser);
12119
12120   /* Look for the `{' to start the namespace.  */
12121   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12122   /* Start the namespace.  */
12123   push_namespace (identifier);
12124
12125   /* "inline namespace" is equivalent to a stub namespace definition
12126      followed by a strong using directive.  */
12127   if (is_inline)
12128     {
12129       tree name_space = current_namespace;
12130       /* Set up namespace association.  */
12131       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12132         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12133                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12134       /* Import the contents of the inline namespace.  */
12135       pop_namespace ();
12136       do_using_directive (name_space);
12137       push_namespace (identifier);
12138     }
12139
12140   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12141
12142   /* Parse the body of the namespace.  */
12143   cp_parser_namespace_body (parser);
12144
12145 #ifdef HANDLE_PRAGMA_VISIBILITY
12146   if (has_visibility)
12147     pop_visibility ();
12148 #endif
12149
12150   /* Finish the namespace.  */
12151   pop_namespace ();
12152   /* Look for the final `}'.  */
12153   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12154 }
12155
12156 /* Parse a namespace-body.
12157
12158    namespace-body:
12159      declaration-seq [opt]  */
12160
12161 static void
12162 cp_parser_namespace_body (cp_parser* parser)
12163 {
12164   cp_parser_declaration_seq_opt (parser);
12165 }
12166
12167 /* Parse a namespace-alias-definition.
12168
12169    namespace-alias-definition:
12170      namespace identifier = qualified-namespace-specifier ;  */
12171
12172 static void
12173 cp_parser_namespace_alias_definition (cp_parser* parser)
12174 {
12175   tree identifier;
12176   tree namespace_specifier;
12177
12178   cp_token *token = cp_lexer_peek_token (parser->lexer);
12179
12180   /* Look for the `namespace' keyword.  */
12181   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12182   /* Look for the identifier.  */
12183   identifier = cp_parser_identifier (parser);
12184   if (identifier == error_mark_node)
12185     return;
12186   /* Look for the `=' token.  */
12187   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12188       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12189     {
12190       error ("%H%<namespace%> definition is not allowed here", &token->location);
12191       /* Skip the definition.  */
12192       cp_lexer_consume_token (parser->lexer);
12193       if (cp_parser_skip_to_closing_brace (parser))
12194         cp_lexer_consume_token (parser->lexer);
12195       return;
12196     }
12197   cp_parser_require (parser, CPP_EQ, "%<=%>");
12198   /* Look for the qualified-namespace-specifier.  */
12199   namespace_specifier
12200     = cp_parser_qualified_namespace_specifier (parser);
12201   /* Look for the `;' token.  */
12202   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12203
12204   /* Register the alias in the symbol table.  */
12205   do_namespace_alias (identifier, namespace_specifier);
12206 }
12207
12208 /* Parse a qualified-namespace-specifier.
12209
12210    qualified-namespace-specifier:
12211      :: [opt] nested-name-specifier [opt] namespace-name
12212
12213    Returns a NAMESPACE_DECL corresponding to the specified
12214    namespace.  */
12215
12216 static tree
12217 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12218 {
12219   /* Look for the optional `::'.  */
12220   cp_parser_global_scope_opt (parser,
12221                               /*current_scope_valid_p=*/false);
12222
12223   /* Look for the optional nested-name-specifier.  */
12224   cp_parser_nested_name_specifier_opt (parser,
12225                                        /*typename_keyword_p=*/false,
12226                                        /*check_dependency_p=*/true,
12227                                        /*type_p=*/false,
12228                                        /*is_declaration=*/true);
12229
12230   return cp_parser_namespace_name (parser);
12231 }
12232
12233 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12234    access declaration.
12235
12236    using-declaration:
12237      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12238      using :: unqualified-id ;  
12239
12240    access-declaration:
12241      qualified-id ;  
12242
12243    */
12244
12245 static bool
12246 cp_parser_using_declaration (cp_parser* parser, 
12247                              bool access_declaration_p)
12248 {
12249   cp_token *token;
12250   bool typename_p = false;
12251   bool global_scope_p;
12252   tree decl;
12253   tree identifier;
12254   tree qscope;
12255
12256   if (access_declaration_p)
12257     cp_parser_parse_tentatively (parser);
12258   else
12259     {
12260       /* Look for the `using' keyword.  */
12261       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12262       
12263       /* Peek at the next token.  */
12264       token = cp_lexer_peek_token (parser->lexer);
12265       /* See if it's `typename'.  */
12266       if (token->keyword == RID_TYPENAME)
12267         {
12268           /* Remember that we've seen it.  */
12269           typename_p = true;
12270           /* Consume the `typename' token.  */
12271           cp_lexer_consume_token (parser->lexer);
12272         }
12273     }
12274
12275   /* Look for the optional global scope qualification.  */
12276   global_scope_p
12277     = (cp_parser_global_scope_opt (parser,
12278                                    /*current_scope_valid_p=*/false)
12279        != NULL_TREE);
12280
12281   /* If we saw `typename', or didn't see `::', then there must be a
12282      nested-name-specifier present.  */
12283   if (typename_p || !global_scope_p)
12284     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12285                                               /*check_dependency_p=*/true,
12286                                               /*type_p=*/false,
12287                                               /*is_declaration=*/true);
12288   /* Otherwise, we could be in either of the two productions.  In that
12289      case, treat the nested-name-specifier as optional.  */
12290   else
12291     qscope = cp_parser_nested_name_specifier_opt (parser,
12292                                                   /*typename_keyword_p=*/false,
12293                                                   /*check_dependency_p=*/true,
12294                                                   /*type_p=*/false,
12295                                                   /*is_declaration=*/true);
12296   if (!qscope)
12297     qscope = global_namespace;
12298
12299   if (access_declaration_p && cp_parser_error_occurred (parser))
12300     /* Something has already gone wrong; there's no need to parse
12301        further.  Since an error has occurred, the return value of
12302        cp_parser_parse_definitely will be false, as required.  */
12303     return cp_parser_parse_definitely (parser);
12304
12305   token = cp_lexer_peek_token (parser->lexer);
12306   /* Parse the unqualified-id.  */
12307   identifier = cp_parser_unqualified_id (parser,
12308                                          /*template_keyword_p=*/false,
12309                                          /*check_dependency_p=*/true,
12310                                          /*declarator_p=*/true,
12311                                          /*optional_p=*/false);
12312
12313   if (access_declaration_p)
12314     {
12315       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12316         cp_parser_simulate_error (parser);
12317       if (!cp_parser_parse_definitely (parser))
12318         return false;
12319     }
12320
12321   /* The function we call to handle a using-declaration is different
12322      depending on what scope we are in.  */
12323   if (qscope == error_mark_node || identifier == error_mark_node)
12324     ;
12325   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12326            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12327     /* [namespace.udecl]
12328
12329        A using declaration shall not name a template-id.  */
12330     error ("%Ha template-id may not appear in a using-declaration",
12331             &token->location);
12332   else
12333     {
12334       if (at_class_scope_p ())
12335         {
12336           /* Create the USING_DECL.  */
12337           decl = do_class_using_decl (parser->scope, identifier);
12338
12339           if (check_for_bare_parameter_packs (decl))
12340             return false;
12341           else
12342             /* Add it to the list of members in this class.  */
12343             finish_member_declaration (decl);
12344         }
12345       else
12346         {
12347           decl = cp_parser_lookup_name_simple (parser,
12348                                                identifier,
12349                                                token->location);
12350           if (decl == error_mark_node)
12351             cp_parser_name_lookup_error (parser, identifier,
12352                                          decl, NULL,
12353                                          token->location);
12354           else if (check_for_bare_parameter_packs (decl))
12355             return false;
12356           else if (!at_namespace_scope_p ())
12357             do_local_using_decl (decl, qscope, identifier);
12358           else
12359             do_toplevel_using_decl (decl, qscope, identifier);
12360         }
12361     }
12362
12363   /* Look for the final `;'.  */
12364   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12365   
12366   return true;
12367 }
12368
12369 /* Parse a using-directive.
12370
12371    using-directive:
12372      using namespace :: [opt] nested-name-specifier [opt]
12373        namespace-name ;  */
12374
12375 static void
12376 cp_parser_using_directive (cp_parser* parser)
12377 {
12378   tree namespace_decl;
12379   tree attribs;
12380
12381   /* Look for the `using' keyword.  */
12382   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12383   /* And the `namespace' keyword.  */
12384   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12385   /* Look for the optional `::' operator.  */
12386   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12387   /* And the optional nested-name-specifier.  */
12388   cp_parser_nested_name_specifier_opt (parser,
12389                                        /*typename_keyword_p=*/false,
12390                                        /*check_dependency_p=*/true,
12391                                        /*type_p=*/false,
12392                                        /*is_declaration=*/true);
12393   /* Get the namespace being used.  */
12394   namespace_decl = cp_parser_namespace_name (parser);
12395   /* And any specified attributes.  */
12396   attribs = cp_parser_attributes_opt (parser);
12397   /* Update the symbol table.  */
12398   parse_using_directive (namespace_decl, attribs);
12399   /* Look for the final `;'.  */
12400   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12401 }
12402
12403 /* Parse an asm-definition.
12404
12405    asm-definition:
12406      asm ( string-literal ) ;
12407
12408    GNU Extension:
12409
12410    asm-definition:
12411      asm volatile [opt] ( string-literal ) ;
12412      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12413      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12414                           : asm-operand-list [opt] ) ;
12415      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12416                           : asm-operand-list [opt]
12417                           : asm-operand-list [opt] ) ;  */
12418
12419 static void
12420 cp_parser_asm_definition (cp_parser* parser)
12421 {
12422   tree string;
12423   tree outputs = NULL_TREE;
12424   tree inputs = NULL_TREE;
12425   tree clobbers = NULL_TREE;
12426   tree asm_stmt;
12427   bool volatile_p = false;
12428   bool extended_p = false;
12429   bool invalid_inputs_p = false;
12430   bool invalid_outputs_p = false;
12431
12432   /* Look for the `asm' keyword.  */
12433   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12434   /* See if the next token is `volatile'.  */
12435   if (cp_parser_allow_gnu_extensions_p (parser)
12436       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12437     {
12438       /* Remember that we saw the `volatile' keyword.  */
12439       volatile_p = true;
12440       /* Consume the token.  */
12441       cp_lexer_consume_token (parser->lexer);
12442     }
12443   /* Look for the opening `('.  */
12444   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12445     return;
12446   /* Look for the string.  */
12447   string = cp_parser_string_literal (parser, false, false);
12448   if (string == error_mark_node)
12449     {
12450       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12451                                              /*consume_paren=*/true);
12452       return;
12453     }
12454
12455   /* If we're allowing GNU extensions, check for the extended assembly
12456      syntax.  Unfortunately, the `:' tokens need not be separated by
12457      a space in C, and so, for compatibility, we tolerate that here
12458      too.  Doing that means that we have to treat the `::' operator as
12459      two `:' tokens.  */
12460   if (cp_parser_allow_gnu_extensions_p (parser)
12461       && parser->in_function_body
12462       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12463           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12464     {
12465       bool inputs_p = false;
12466       bool clobbers_p = false;
12467
12468       /* The extended syntax was used.  */
12469       extended_p = true;
12470
12471       /* Look for outputs.  */
12472       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12473         {
12474           /* Consume the `:'.  */
12475           cp_lexer_consume_token (parser->lexer);
12476           /* Parse the output-operands.  */
12477           if (cp_lexer_next_token_is_not (parser->lexer,
12478                                           CPP_COLON)
12479               && cp_lexer_next_token_is_not (parser->lexer,
12480                                              CPP_SCOPE)
12481               && cp_lexer_next_token_is_not (parser->lexer,
12482                                              CPP_CLOSE_PAREN))
12483             outputs = cp_parser_asm_operand_list (parser);
12484
12485             if (outputs == error_mark_node)
12486               invalid_outputs_p = true;
12487         }
12488       /* If the next token is `::', there are no outputs, and the
12489          next token is the beginning of the inputs.  */
12490       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12491         /* The inputs are coming next.  */
12492         inputs_p = true;
12493
12494       /* Look for inputs.  */
12495       if (inputs_p
12496           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12497         {
12498           /* Consume the `:' or `::'.  */
12499           cp_lexer_consume_token (parser->lexer);
12500           /* Parse the output-operands.  */
12501           if (cp_lexer_next_token_is_not (parser->lexer,
12502                                           CPP_COLON)
12503               && cp_lexer_next_token_is_not (parser->lexer,
12504                                              CPP_CLOSE_PAREN))
12505             inputs = cp_parser_asm_operand_list (parser);
12506
12507             if (inputs == error_mark_node)
12508               invalid_inputs_p = true;
12509         }
12510       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12511         /* The clobbers are coming next.  */
12512         clobbers_p = true;
12513
12514       /* Look for clobbers.  */
12515       if (clobbers_p
12516           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12517         {
12518           /* Consume the `:' or `::'.  */
12519           cp_lexer_consume_token (parser->lexer);
12520           /* Parse the clobbers.  */
12521           if (cp_lexer_next_token_is_not (parser->lexer,
12522                                           CPP_CLOSE_PAREN))
12523             clobbers = cp_parser_asm_clobber_list (parser);
12524         }
12525     }
12526   /* Look for the closing `)'.  */
12527   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12528     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12529                                            /*consume_paren=*/true);
12530   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12531
12532   if (!invalid_inputs_p && !invalid_outputs_p)
12533     {
12534       /* Create the ASM_EXPR.  */
12535       if (parser->in_function_body)
12536         {
12537           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12538                                       inputs, clobbers);
12539           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12540           if (!extended_p)
12541             {
12542               tree temp = asm_stmt;
12543               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12544                 temp = TREE_OPERAND (temp, 0);
12545
12546               ASM_INPUT_P (temp) = 1;
12547             }
12548         }
12549       else
12550         cgraph_add_asm_node (string);
12551     }
12552 }
12553
12554 /* Declarators [gram.dcl.decl] */
12555
12556 /* Parse an init-declarator.
12557
12558    init-declarator:
12559      declarator initializer [opt]
12560
12561    GNU Extension:
12562
12563    init-declarator:
12564      declarator asm-specification [opt] attributes [opt] initializer [opt]
12565
12566    function-definition:
12567      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12568        function-body
12569      decl-specifier-seq [opt] declarator function-try-block
12570
12571    GNU Extension:
12572
12573    function-definition:
12574      __extension__ function-definition
12575
12576    The DECL_SPECIFIERS apply to this declarator.  Returns a
12577    representation of the entity declared.  If MEMBER_P is TRUE, then
12578    this declarator appears in a class scope.  The new DECL created by
12579    this declarator is returned.
12580
12581    The CHECKS are access checks that should be performed once we know
12582    what entity is being declared (and, therefore, what classes have
12583    befriended it).
12584
12585    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12586    for a function-definition here as well.  If the declarator is a
12587    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12588    be TRUE upon return.  By that point, the function-definition will
12589    have been completely parsed.
12590
12591    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12592    is FALSE.  */
12593
12594 static tree
12595 cp_parser_init_declarator (cp_parser* parser,
12596                            cp_decl_specifier_seq *decl_specifiers,
12597                            VEC (deferred_access_check,gc)* checks,
12598                            bool function_definition_allowed_p,
12599                            bool member_p,
12600                            int declares_class_or_enum,
12601                            bool* function_definition_p)
12602 {
12603   cp_token *token = NULL, *asm_spec_start_token = NULL,
12604            *attributes_start_token = NULL;
12605   cp_declarator *declarator;
12606   tree prefix_attributes;
12607   tree attributes;
12608   tree asm_specification;
12609   tree initializer;
12610   tree decl = NULL_TREE;
12611   tree scope;
12612   int is_initialized;
12613   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12614      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12615      "(...)".  */
12616   enum cpp_ttype initialization_kind;
12617   bool is_direct_init = false;
12618   bool is_non_constant_init;
12619   int ctor_dtor_or_conv_p;
12620   bool friend_p;
12621   tree pushed_scope = NULL;
12622
12623   /* Gather the attributes that were provided with the
12624      decl-specifiers.  */
12625   prefix_attributes = decl_specifiers->attributes;
12626
12627   /* Assume that this is not the declarator for a function
12628      definition.  */
12629   if (function_definition_p)
12630     *function_definition_p = false;
12631
12632   /* Defer access checks while parsing the declarator; we cannot know
12633      what names are accessible until we know what is being
12634      declared.  */
12635   resume_deferring_access_checks ();
12636
12637   /* Parse the declarator.  */
12638   token = cp_lexer_peek_token (parser->lexer);
12639   declarator
12640     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12641                             &ctor_dtor_or_conv_p,
12642                             /*parenthesized_p=*/NULL,
12643                             /*member_p=*/false);
12644   /* Gather up the deferred checks.  */
12645   stop_deferring_access_checks ();
12646
12647   /* If the DECLARATOR was erroneous, there's no need to go
12648      further.  */
12649   if (declarator == cp_error_declarator)
12650     return error_mark_node;
12651
12652   /* Check that the number of template-parameter-lists is OK.  */
12653   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12654                                                        token->location))
12655     return error_mark_node;
12656
12657   if (declares_class_or_enum & 2)
12658     cp_parser_check_for_definition_in_return_type (declarator,
12659                                                    decl_specifiers->type,
12660                                                    decl_specifiers->type_location);
12661
12662   /* Figure out what scope the entity declared by the DECLARATOR is
12663      located in.  `grokdeclarator' sometimes changes the scope, so
12664      we compute it now.  */
12665   scope = get_scope_of_declarator (declarator);
12666
12667   /* If we're allowing GNU extensions, look for an asm-specification
12668      and attributes.  */
12669   if (cp_parser_allow_gnu_extensions_p (parser))
12670     {
12671       /* Look for an asm-specification.  */
12672       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12673       asm_specification = cp_parser_asm_specification_opt (parser);
12674       /* And attributes.  */
12675       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12676       attributes = cp_parser_attributes_opt (parser);
12677     }
12678   else
12679     {
12680       asm_specification = NULL_TREE;
12681       attributes = NULL_TREE;
12682     }
12683
12684   /* Peek at the next token.  */
12685   token = cp_lexer_peek_token (parser->lexer);
12686   /* Check to see if the token indicates the start of a
12687      function-definition.  */
12688   if (function_declarator_p (declarator)
12689       && cp_parser_token_starts_function_definition_p (token))
12690     {
12691       if (!function_definition_allowed_p)
12692         {
12693           /* If a function-definition should not appear here, issue an
12694              error message.  */
12695           cp_parser_error (parser,
12696                            "a function-definition is not allowed here");
12697           return error_mark_node;
12698         }
12699       else
12700         {
12701           location_t func_brace_location
12702             = cp_lexer_peek_token (parser->lexer)->location;
12703
12704           /* Neither attributes nor an asm-specification are allowed
12705              on a function-definition.  */
12706           if (asm_specification)
12707             error ("%Han asm-specification is not allowed "
12708                    "on a function-definition",
12709                    &asm_spec_start_token->location);
12710           if (attributes)
12711             error ("%Hattributes are not allowed on a function-definition",
12712                    &attributes_start_token->location);
12713           /* This is a function-definition.  */
12714           *function_definition_p = true;
12715
12716           /* Parse the function definition.  */
12717           if (member_p)
12718             decl = cp_parser_save_member_function_body (parser,
12719                                                         decl_specifiers,
12720                                                         declarator,
12721                                                         prefix_attributes);
12722           else
12723             decl
12724               = (cp_parser_function_definition_from_specifiers_and_declarator
12725                  (parser, decl_specifiers, prefix_attributes, declarator));
12726
12727           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12728             {
12729               /* This is where the prologue starts...  */
12730               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12731                 = func_brace_location;
12732             }
12733
12734           return decl;
12735         }
12736     }
12737
12738   /* [dcl.dcl]
12739
12740      Only in function declarations for constructors, destructors, and
12741      type conversions can the decl-specifier-seq be omitted.
12742
12743      We explicitly postpone this check past the point where we handle
12744      function-definitions because we tolerate function-definitions
12745      that are missing their return types in some modes.  */
12746   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12747     {
12748       cp_parser_error (parser,
12749                        "expected constructor, destructor, or type conversion");
12750       return error_mark_node;
12751     }
12752
12753   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12754   if (token->type == CPP_EQ
12755       || token->type == CPP_OPEN_PAREN
12756       || token->type == CPP_OPEN_BRACE)
12757     {
12758       is_initialized = SD_INITIALIZED;
12759       initialization_kind = token->type;
12760
12761       if (token->type == CPP_EQ
12762           && function_declarator_p (declarator))
12763         {
12764           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12765           if (t2->keyword == RID_DEFAULT)
12766             is_initialized = SD_DEFAULTED;
12767           else if (t2->keyword == RID_DELETE)
12768             is_initialized = SD_DELETED;
12769         }
12770     }
12771   else
12772     {
12773       /* If the init-declarator isn't initialized and isn't followed by a
12774          `,' or `;', it's not a valid init-declarator.  */
12775       if (token->type != CPP_COMMA
12776           && token->type != CPP_SEMICOLON)
12777         {
12778           cp_parser_error (parser, "expected initializer");
12779           return error_mark_node;
12780         }
12781       is_initialized = SD_UNINITIALIZED;
12782       initialization_kind = CPP_EOF;
12783     }
12784
12785   /* Because start_decl has side-effects, we should only call it if we
12786      know we're going ahead.  By this point, we know that we cannot
12787      possibly be looking at any other construct.  */
12788   cp_parser_commit_to_tentative_parse (parser);
12789
12790   /* If the decl specifiers were bad, issue an error now that we're
12791      sure this was intended to be a declarator.  Then continue
12792      declaring the variable(s), as int, to try to cut down on further
12793      errors.  */
12794   if (decl_specifiers->any_specifiers_p
12795       && decl_specifiers->type == error_mark_node)
12796     {
12797       cp_parser_error (parser, "invalid type in declaration");
12798       decl_specifiers->type = integer_type_node;
12799     }
12800
12801   /* Check to see whether or not this declaration is a friend.  */
12802   friend_p = cp_parser_friend_p (decl_specifiers);
12803
12804   /* Enter the newly declared entry in the symbol table.  If we're
12805      processing a declaration in a class-specifier, we wait until
12806      after processing the initializer.  */
12807   if (!member_p)
12808     {
12809       if (parser->in_unbraced_linkage_specification_p)
12810         decl_specifiers->storage_class = sc_extern;
12811       decl = start_decl (declarator, decl_specifiers,
12812                          is_initialized, attributes, prefix_attributes,
12813                          &pushed_scope);
12814     }
12815   else if (scope)
12816     /* Enter the SCOPE.  That way unqualified names appearing in the
12817        initializer will be looked up in SCOPE.  */
12818     pushed_scope = push_scope (scope);
12819
12820   /* Perform deferred access control checks, now that we know in which
12821      SCOPE the declared entity resides.  */
12822   if (!member_p && decl)
12823     {
12824       tree saved_current_function_decl = NULL_TREE;
12825
12826       /* If the entity being declared is a function, pretend that we
12827          are in its scope.  If it is a `friend', it may have access to
12828          things that would not otherwise be accessible.  */
12829       if (TREE_CODE (decl) == FUNCTION_DECL)
12830         {
12831           saved_current_function_decl = current_function_decl;
12832           current_function_decl = decl;
12833         }
12834
12835       /* Perform access checks for template parameters.  */
12836       cp_parser_perform_template_parameter_access_checks (checks);
12837
12838       /* Perform the access control checks for the declarator and the
12839          decl-specifiers.  */
12840       perform_deferred_access_checks ();
12841
12842       /* Restore the saved value.  */
12843       if (TREE_CODE (decl) == FUNCTION_DECL)
12844         current_function_decl = saved_current_function_decl;
12845     }
12846
12847   /* Parse the initializer.  */
12848   initializer = NULL_TREE;
12849   is_direct_init = false;
12850   is_non_constant_init = true;
12851   if (is_initialized)
12852     {
12853       if (function_declarator_p (declarator))
12854         {
12855           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12856            if (initialization_kind == CPP_EQ)
12857              initializer = cp_parser_pure_specifier (parser);
12858            else
12859              {
12860                /* If the declaration was erroneous, we don't really
12861                   know what the user intended, so just silently
12862                   consume the initializer.  */
12863                if (decl != error_mark_node)
12864                  error ("%Hinitializer provided for function",
12865                         &initializer_start_token->location);
12866                cp_parser_skip_to_closing_parenthesis (parser,
12867                                                       /*recovering=*/true,
12868                                                       /*or_comma=*/false,
12869                                                       /*consume_paren=*/true);
12870              }
12871         }
12872       else
12873         initializer = cp_parser_initializer (parser,
12874                                              &is_direct_init,
12875                                              &is_non_constant_init);
12876     }
12877
12878   /* The old parser allows attributes to appear after a parenthesized
12879      initializer.  Mark Mitchell proposed removing this functionality
12880      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12881      attributes -- but ignores them.  */
12882   if (cp_parser_allow_gnu_extensions_p (parser)
12883       && initialization_kind == CPP_OPEN_PAREN)
12884     if (cp_parser_attributes_opt (parser))
12885       warning (OPT_Wattributes,
12886                "attributes after parenthesized initializer ignored");
12887
12888   /* For an in-class declaration, use `grokfield' to create the
12889      declaration.  */
12890   if (member_p)
12891     {
12892       if (pushed_scope)
12893         {
12894           pop_scope (pushed_scope);
12895           pushed_scope = false;
12896         }
12897       decl = grokfield (declarator, decl_specifiers,
12898                         initializer, !is_non_constant_init,
12899                         /*asmspec=*/NULL_TREE,
12900                         prefix_attributes);
12901       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12902         cp_parser_save_default_args (parser, decl);
12903     }
12904
12905   /* Finish processing the declaration.  But, skip friend
12906      declarations.  */
12907   if (!friend_p && decl && decl != error_mark_node)
12908     {
12909       cp_finish_decl (decl,
12910                       initializer, !is_non_constant_init,
12911                       asm_specification,
12912                       /* If the initializer is in parentheses, then this is
12913                          a direct-initialization, which means that an
12914                          `explicit' constructor is OK.  Otherwise, an
12915                          `explicit' constructor cannot be used.  */
12916                       ((is_direct_init || !is_initialized)
12917                        ? 0 : LOOKUP_ONLYCONVERTING));
12918     }
12919   else if ((cxx_dialect != cxx98) && friend_p
12920            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12921     /* Core issue #226 (C++0x only): A default template-argument
12922        shall not be specified in a friend class template
12923        declaration. */
12924     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12925                              /*is_partial=*/0, /*is_friend_decl=*/1);
12926
12927   if (!friend_p && pushed_scope)
12928     pop_scope (pushed_scope);
12929
12930   return decl;
12931 }
12932
12933 /* Parse a declarator.
12934
12935    declarator:
12936      direct-declarator
12937      ptr-operator declarator
12938
12939    abstract-declarator:
12940      ptr-operator abstract-declarator [opt]
12941      direct-abstract-declarator
12942
12943    GNU Extensions:
12944
12945    declarator:
12946      attributes [opt] direct-declarator
12947      attributes [opt] ptr-operator declarator
12948
12949    abstract-declarator:
12950      attributes [opt] ptr-operator abstract-declarator [opt]
12951      attributes [opt] direct-abstract-declarator
12952
12953    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12954    detect constructor, destructor or conversion operators. It is set
12955    to -1 if the declarator is a name, and +1 if it is a
12956    function. Otherwise it is set to zero. Usually you just want to
12957    test for >0, but internally the negative value is used.
12958
12959    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12960    a decl-specifier-seq unless it declares a constructor, destructor,
12961    or conversion.  It might seem that we could check this condition in
12962    semantic analysis, rather than parsing, but that makes it difficult
12963    to handle something like `f()'.  We want to notice that there are
12964    no decl-specifiers, and therefore realize that this is an
12965    expression, not a declaration.)
12966
12967    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12968    the declarator is a direct-declarator of the form "(...)".
12969
12970    MEMBER_P is true iff this declarator is a member-declarator.  */
12971
12972 static cp_declarator *
12973 cp_parser_declarator (cp_parser* parser,
12974                       cp_parser_declarator_kind dcl_kind,
12975                       int* ctor_dtor_or_conv_p,
12976                       bool* parenthesized_p,
12977                       bool member_p)
12978 {
12979   cp_token *token;
12980   cp_declarator *declarator;
12981   enum tree_code code;
12982   cp_cv_quals cv_quals;
12983   tree class_type;
12984   tree attributes = NULL_TREE;
12985
12986   /* Assume this is not a constructor, destructor, or type-conversion
12987      operator.  */
12988   if (ctor_dtor_or_conv_p)
12989     *ctor_dtor_or_conv_p = 0;
12990
12991   if (cp_parser_allow_gnu_extensions_p (parser))
12992     attributes = cp_parser_attributes_opt (parser);
12993
12994   /* Peek at the next token.  */
12995   token = cp_lexer_peek_token (parser->lexer);
12996
12997   /* Check for the ptr-operator production.  */
12998   cp_parser_parse_tentatively (parser);
12999   /* Parse the ptr-operator.  */
13000   code = cp_parser_ptr_operator (parser,
13001                                  &class_type,
13002                                  &cv_quals);
13003   /* If that worked, then we have a ptr-operator.  */
13004   if (cp_parser_parse_definitely (parser))
13005     {
13006       /* If a ptr-operator was found, then this declarator was not
13007          parenthesized.  */
13008       if (parenthesized_p)
13009         *parenthesized_p = true;
13010       /* The dependent declarator is optional if we are parsing an
13011          abstract-declarator.  */
13012       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13013         cp_parser_parse_tentatively (parser);
13014
13015       /* Parse the dependent declarator.  */
13016       declarator = cp_parser_declarator (parser, dcl_kind,
13017                                          /*ctor_dtor_or_conv_p=*/NULL,
13018                                          /*parenthesized_p=*/NULL,
13019                                          /*member_p=*/false);
13020
13021       /* If we are parsing an abstract-declarator, we must handle the
13022          case where the dependent declarator is absent.  */
13023       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13024           && !cp_parser_parse_definitely (parser))
13025         declarator = NULL;
13026
13027       declarator = cp_parser_make_indirect_declarator
13028         (code, class_type, cv_quals, declarator);
13029     }
13030   /* Everything else is a direct-declarator.  */
13031   else
13032     {
13033       if (parenthesized_p)
13034         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13035                                                    CPP_OPEN_PAREN);
13036       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13037                                                 ctor_dtor_or_conv_p,
13038                                                 member_p);
13039     }
13040
13041   if (attributes && declarator && declarator != cp_error_declarator)
13042     declarator->attributes = attributes;
13043
13044   return declarator;
13045 }
13046
13047 /* Parse a direct-declarator or direct-abstract-declarator.
13048
13049    direct-declarator:
13050      declarator-id
13051      direct-declarator ( parameter-declaration-clause )
13052        cv-qualifier-seq [opt]
13053        exception-specification [opt]
13054      direct-declarator [ constant-expression [opt] ]
13055      ( declarator )
13056
13057    direct-abstract-declarator:
13058      direct-abstract-declarator [opt]
13059        ( parameter-declaration-clause )
13060        cv-qualifier-seq [opt]
13061        exception-specification [opt]
13062      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13063      ( abstract-declarator )
13064
13065    Returns a representation of the declarator.  DCL_KIND is
13066    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13067    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13068    we are parsing a direct-declarator.  It is
13069    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13070    of ambiguity we prefer an abstract declarator, as per
13071    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13072    cp_parser_declarator.  */
13073
13074 static cp_declarator *
13075 cp_parser_direct_declarator (cp_parser* parser,
13076                              cp_parser_declarator_kind dcl_kind,
13077                              int* ctor_dtor_or_conv_p,
13078                              bool member_p)
13079 {
13080   cp_token *token;
13081   cp_declarator *declarator = NULL;
13082   tree scope = NULL_TREE;
13083   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13084   bool saved_in_declarator_p = parser->in_declarator_p;
13085   bool first = true;
13086   tree pushed_scope = NULL_TREE;
13087
13088   while (true)
13089     {
13090       /* Peek at the next token.  */
13091       token = cp_lexer_peek_token (parser->lexer);
13092       if (token->type == CPP_OPEN_PAREN)
13093         {
13094           /* This is either a parameter-declaration-clause, or a
13095              parenthesized declarator. When we know we are parsing a
13096              named declarator, it must be a parenthesized declarator
13097              if FIRST is true. For instance, `(int)' is a
13098              parameter-declaration-clause, with an omitted
13099              direct-abstract-declarator. But `((*))', is a
13100              parenthesized abstract declarator. Finally, when T is a
13101              template parameter `(T)' is a
13102              parameter-declaration-clause, and not a parenthesized
13103              named declarator.
13104
13105              We first try and parse a parameter-declaration-clause,
13106              and then try a nested declarator (if FIRST is true).
13107
13108              It is not an error for it not to be a
13109              parameter-declaration-clause, even when FIRST is
13110              false. Consider,
13111
13112                int i (int);
13113                int i (3);
13114
13115              The first is the declaration of a function while the
13116              second is the definition of a variable, including its
13117              initializer.
13118
13119              Having seen only the parenthesis, we cannot know which of
13120              these two alternatives should be selected.  Even more
13121              complex are examples like:
13122
13123                int i (int (a));
13124                int i (int (3));
13125
13126              The former is a function-declaration; the latter is a
13127              variable initialization.
13128
13129              Thus again, we try a parameter-declaration-clause, and if
13130              that fails, we back out and return.  */
13131
13132           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13133             {
13134               tree params;
13135               unsigned saved_num_template_parameter_lists;
13136               bool is_declarator = false;
13137               tree t;
13138
13139               /* In a member-declarator, the only valid interpretation
13140                  of a parenthesis is the start of a
13141                  parameter-declaration-clause.  (It is invalid to
13142                  initialize a static data member with a parenthesized
13143                  initializer; only the "=" form of initialization is
13144                  permitted.)  */
13145               if (!member_p)
13146                 cp_parser_parse_tentatively (parser);
13147
13148               /* Consume the `('.  */
13149               cp_lexer_consume_token (parser->lexer);
13150               if (first)
13151                 {
13152                   /* If this is going to be an abstract declarator, we're
13153                      in a declarator and we can't have default args.  */
13154                   parser->default_arg_ok_p = false;
13155                   parser->in_declarator_p = true;
13156                 }
13157
13158               /* Inside the function parameter list, surrounding
13159                  template-parameter-lists do not apply.  */
13160               saved_num_template_parameter_lists
13161                 = parser->num_template_parameter_lists;
13162               parser->num_template_parameter_lists = 0;
13163
13164               begin_scope (sk_function_parms, NULL_TREE);
13165
13166               /* Parse the parameter-declaration-clause.  */
13167               params = cp_parser_parameter_declaration_clause (parser);
13168
13169               parser->num_template_parameter_lists
13170                 = saved_num_template_parameter_lists;
13171
13172               /* If all went well, parse the cv-qualifier-seq and the
13173                  exception-specification.  */
13174               if (member_p || cp_parser_parse_definitely (parser))
13175                 {
13176                   cp_cv_quals cv_quals;
13177                   tree exception_specification;
13178                   tree late_return;
13179
13180                   is_declarator = true;
13181
13182                   if (ctor_dtor_or_conv_p)
13183                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13184                   first = false;
13185                   /* Consume the `)'.  */
13186                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13187
13188                   /* Parse the cv-qualifier-seq.  */
13189                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13190                   /* And the exception-specification.  */
13191                   exception_specification
13192                     = cp_parser_exception_specification_opt (parser);
13193
13194                   late_return
13195                     = cp_parser_late_return_type_opt (parser);
13196
13197                   /* Create the function-declarator.  */
13198                   declarator = make_call_declarator (declarator,
13199                                                      params,
13200                                                      cv_quals,
13201                                                      exception_specification,
13202                                                      late_return);
13203                   /* Any subsequent parameter lists are to do with
13204                      return type, so are not those of the declared
13205                      function.  */
13206                   parser->default_arg_ok_p = false;
13207                 }
13208
13209               /* Remove the function parms from scope.  */
13210               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13211                 pop_binding (DECL_NAME (t), t);
13212               leave_scope();
13213
13214               if (is_declarator)
13215                 /* Repeat the main loop.  */
13216                 continue;
13217             }
13218
13219           /* If this is the first, we can try a parenthesized
13220              declarator.  */
13221           if (first)
13222             {
13223               bool saved_in_type_id_in_expr_p;
13224
13225               parser->default_arg_ok_p = saved_default_arg_ok_p;
13226               parser->in_declarator_p = saved_in_declarator_p;
13227
13228               /* Consume the `('.  */
13229               cp_lexer_consume_token (parser->lexer);
13230               /* Parse the nested declarator.  */
13231               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13232               parser->in_type_id_in_expr_p = true;
13233               declarator
13234                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13235                                         /*parenthesized_p=*/NULL,
13236                                         member_p);
13237               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13238               first = false;
13239               /* Expect a `)'.  */
13240               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13241                 declarator = cp_error_declarator;
13242               if (declarator == cp_error_declarator)
13243                 break;
13244
13245               goto handle_declarator;
13246             }
13247           /* Otherwise, we must be done.  */
13248           else
13249             break;
13250         }
13251       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13252                && token->type == CPP_OPEN_SQUARE)
13253         {
13254           /* Parse an array-declarator.  */
13255           tree bounds;
13256
13257           if (ctor_dtor_or_conv_p)
13258             *ctor_dtor_or_conv_p = 0;
13259
13260           first = false;
13261           parser->default_arg_ok_p = false;
13262           parser->in_declarator_p = true;
13263           /* Consume the `['.  */
13264           cp_lexer_consume_token (parser->lexer);
13265           /* Peek at the next token.  */
13266           token = cp_lexer_peek_token (parser->lexer);
13267           /* If the next token is `]', then there is no
13268              constant-expression.  */
13269           if (token->type != CPP_CLOSE_SQUARE)
13270             {
13271               bool non_constant_p;
13272
13273               bounds
13274                 = cp_parser_constant_expression (parser,
13275                                                  /*allow_non_constant=*/true,
13276                                                  &non_constant_p);
13277               if (!non_constant_p)
13278                 bounds = fold_non_dependent_expr (bounds);
13279               else if (processing_template_decl)
13280                 {
13281                   /* Remember this wasn't a constant-expression.  */
13282                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13283                   TREE_SIDE_EFFECTS (bounds) = 1;
13284                 }
13285
13286               /* Normally, the array bound must be an integral constant
13287                  expression.  However, as an extension, we allow VLAs
13288                  in function scopes.  */
13289               else if (!parser->in_function_body)
13290                 {
13291                   error ("%Harray bound is not an integer constant",
13292                          &token->location);
13293                   bounds = error_mark_node;
13294                 }
13295             }
13296           else
13297             bounds = NULL_TREE;
13298           /* Look for the closing `]'.  */
13299           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13300             {
13301               declarator = cp_error_declarator;
13302               break;
13303             }
13304
13305           declarator = make_array_declarator (declarator, bounds);
13306         }
13307       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13308         {
13309           tree qualifying_scope;
13310           tree unqualified_name;
13311           special_function_kind sfk;
13312           bool abstract_ok;
13313           bool pack_expansion_p = false;
13314           cp_token *declarator_id_start_token;
13315
13316           /* Parse a declarator-id */
13317           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13318           if (abstract_ok)
13319             {
13320               cp_parser_parse_tentatively (parser);
13321
13322               /* If we see an ellipsis, we should be looking at a
13323                  parameter pack. */
13324               if (token->type == CPP_ELLIPSIS)
13325                 {
13326                   /* Consume the `...' */
13327                   cp_lexer_consume_token (parser->lexer);
13328
13329                   pack_expansion_p = true;
13330                 }
13331             }
13332
13333           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13334           unqualified_name
13335             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13336           qualifying_scope = parser->scope;
13337           if (abstract_ok)
13338             {
13339               bool okay = false;
13340
13341               if (!unqualified_name && pack_expansion_p)
13342                 {
13343                   /* Check whether an error occurred. */
13344                   okay = !cp_parser_error_occurred (parser);
13345
13346                   /* We already consumed the ellipsis to mark a
13347                      parameter pack, but we have no way to report it,
13348                      so abort the tentative parse. We will be exiting
13349                      immediately anyway. */
13350                   cp_parser_abort_tentative_parse (parser);
13351                 }
13352               else
13353                 okay = cp_parser_parse_definitely (parser);
13354
13355               if (!okay)
13356                 unqualified_name = error_mark_node;
13357               else if (unqualified_name
13358                        && (qualifying_scope
13359                            || (TREE_CODE (unqualified_name)
13360                                != IDENTIFIER_NODE)))
13361                 {
13362                   cp_parser_error (parser, "expected unqualified-id");
13363                   unqualified_name = error_mark_node;
13364                 }
13365             }
13366
13367           if (!unqualified_name)
13368             return NULL;
13369           if (unqualified_name == error_mark_node)
13370             {
13371               declarator = cp_error_declarator;
13372               pack_expansion_p = false;
13373               declarator->parameter_pack_p = false;
13374               break;
13375             }
13376
13377           if (qualifying_scope && at_namespace_scope_p ()
13378               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13379             {
13380               /* In the declaration of a member of a template class
13381                  outside of the class itself, the SCOPE will sometimes
13382                  be a TYPENAME_TYPE.  For example, given:
13383
13384                  template <typename T>
13385                  int S<T>::R::i = 3;
13386
13387                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13388                  this context, we must resolve S<T>::R to an ordinary
13389                  type, rather than a typename type.
13390
13391                  The reason we normally avoid resolving TYPENAME_TYPEs
13392                  is that a specialization of `S' might render
13393                  `S<T>::R' not a type.  However, if `S' is
13394                  specialized, then this `i' will not be used, so there
13395                  is no harm in resolving the types here.  */
13396               tree type;
13397
13398               /* Resolve the TYPENAME_TYPE.  */
13399               type = resolve_typename_type (qualifying_scope,
13400                                             /*only_current_p=*/false);
13401               /* If that failed, the declarator is invalid.  */
13402               if (TREE_CODE (type) == TYPENAME_TYPE)
13403                 error ("%H%<%T::%E%> is not a type",
13404                        &declarator_id_start_token->location,
13405                        TYPE_CONTEXT (qualifying_scope),
13406                        TYPE_IDENTIFIER (qualifying_scope));
13407               qualifying_scope = type;
13408             }
13409
13410           sfk = sfk_none;
13411
13412           if (unqualified_name)
13413             {
13414               tree class_type;
13415
13416               if (qualifying_scope
13417                   && CLASS_TYPE_P (qualifying_scope))
13418                 class_type = qualifying_scope;
13419               else
13420                 class_type = current_class_type;
13421
13422               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13423                 {
13424                   tree name_type = TREE_TYPE (unqualified_name);
13425                   if (class_type && same_type_p (name_type, class_type))
13426                     {
13427                       if (qualifying_scope
13428                           && CLASSTYPE_USE_TEMPLATE (name_type))
13429                         {
13430                           error ("%Hinvalid use of constructor as a template",
13431                                  &declarator_id_start_token->location);
13432                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13433                                   "name the constructor in a qualified name",
13434                                   class_type,
13435                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13436                                   class_type, name_type);
13437                           declarator = cp_error_declarator;
13438                           break;
13439                         }
13440                       else
13441                         unqualified_name = constructor_name (class_type);
13442                     }
13443                   else
13444                     {
13445                       /* We do not attempt to print the declarator
13446                          here because we do not have enough
13447                          information about its original syntactic
13448                          form.  */
13449                       cp_parser_error (parser, "invalid declarator");
13450                       declarator = cp_error_declarator;
13451                       break;
13452                     }
13453                 }
13454
13455               if (class_type)
13456                 {
13457                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13458                     sfk = sfk_destructor;
13459                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13460                     sfk = sfk_conversion;
13461                   else if (/* There's no way to declare a constructor
13462                               for an anonymous type, even if the type
13463                               got a name for linkage purposes.  */
13464                            !TYPE_WAS_ANONYMOUS (class_type)
13465                            && constructor_name_p (unqualified_name,
13466                                                   class_type))
13467                     {
13468                       unqualified_name = constructor_name (class_type);
13469                       sfk = sfk_constructor;
13470                     }
13471
13472                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13473                     *ctor_dtor_or_conv_p = -1;
13474                 }
13475             }
13476           declarator = make_id_declarator (qualifying_scope,
13477                                            unqualified_name,
13478                                            sfk);
13479           declarator->id_loc = token->location;
13480           declarator->parameter_pack_p = pack_expansion_p;
13481
13482           if (pack_expansion_p)
13483             maybe_warn_variadic_templates ();
13484
13485         handle_declarator:;
13486           scope = get_scope_of_declarator (declarator);
13487           if (scope)
13488             /* Any names that appear after the declarator-id for a
13489                member are looked up in the containing scope.  */
13490             pushed_scope = push_scope (scope);
13491           parser->in_declarator_p = true;
13492           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13493               || (declarator && declarator->kind == cdk_id))
13494             /* Default args are only allowed on function
13495                declarations.  */
13496             parser->default_arg_ok_p = saved_default_arg_ok_p;
13497           else
13498             parser->default_arg_ok_p = false;
13499
13500           first = false;
13501         }
13502       /* We're done.  */
13503       else
13504         break;
13505     }
13506
13507   /* For an abstract declarator, we might wind up with nothing at this
13508      point.  That's an error; the declarator is not optional.  */
13509   if (!declarator)
13510     cp_parser_error (parser, "expected declarator");
13511
13512   /* If we entered a scope, we must exit it now.  */
13513   if (pushed_scope)
13514     pop_scope (pushed_scope);
13515
13516   parser->default_arg_ok_p = saved_default_arg_ok_p;
13517   parser->in_declarator_p = saved_in_declarator_p;
13518
13519   return declarator;
13520 }
13521
13522 /* Parse a ptr-operator.
13523
13524    ptr-operator:
13525      * cv-qualifier-seq [opt]
13526      &
13527      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13528
13529    GNU Extension:
13530
13531    ptr-operator:
13532      & cv-qualifier-seq [opt]
13533
13534    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13535    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13536    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13537    filled in with the TYPE containing the member.  *CV_QUALS is
13538    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13539    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13540    Note that the tree codes returned by this function have nothing
13541    to do with the types of trees that will be eventually be created
13542    to represent the pointer or reference type being parsed. They are
13543    just constants with suggestive names. */
13544 static enum tree_code
13545 cp_parser_ptr_operator (cp_parser* parser,
13546                         tree* type,
13547                         cp_cv_quals *cv_quals)
13548 {
13549   enum tree_code code = ERROR_MARK;
13550   cp_token *token;
13551
13552   /* Assume that it's not a pointer-to-member.  */
13553   *type = NULL_TREE;
13554   /* And that there are no cv-qualifiers.  */
13555   *cv_quals = TYPE_UNQUALIFIED;
13556
13557   /* Peek at the next token.  */
13558   token = cp_lexer_peek_token (parser->lexer);
13559
13560   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13561   if (token->type == CPP_MULT)
13562     code = INDIRECT_REF;
13563   else if (token->type == CPP_AND)
13564     code = ADDR_EXPR;
13565   else if ((cxx_dialect != cxx98) &&
13566            token->type == CPP_AND_AND) /* C++0x only */
13567     code = NON_LVALUE_EXPR;
13568
13569   if (code != ERROR_MARK)
13570     {
13571       /* Consume the `*', `&' or `&&'.  */
13572       cp_lexer_consume_token (parser->lexer);
13573
13574       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13575          `&', if we are allowing GNU extensions.  (The only qualifier
13576          that can legally appear after `&' is `restrict', but that is
13577          enforced during semantic analysis.  */
13578       if (code == INDIRECT_REF
13579           || cp_parser_allow_gnu_extensions_p (parser))
13580         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13581     }
13582   else
13583     {
13584       /* Try the pointer-to-member case.  */
13585       cp_parser_parse_tentatively (parser);
13586       /* Look for the optional `::' operator.  */
13587       cp_parser_global_scope_opt (parser,
13588                                   /*current_scope_valid_p=*/false);
13589       /* Look for the nested-name specifier.  */
13590       token = cp_lexer_peek_token (parser->lexer);
13591       cp_parser_nested_name_specifier (parser,
13592                                        /*typename_keyword_p=*/false,
13593                                        /*check_dependency_p=*/true,
13594                                        /*type_p=*/false,
13595                                        /*is_declaration=*/false);
13596       /* If we found it, and the next token is a `*', then we are
13597          indeed looking at a pointer-to-member operator.  */
13598       if (!cp_parser_error_occurred (parser)
13599           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13600         {
13601           /* Indicate that the `*' operator was used.  */
13602           code = INDIRECT_REF;
13603
13604           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13605             error ("%H%qD is a namespace", &token->location, parser->scope);
13606           else
13607             {
13608               /* The type of which the member is a member is given by the
13609                  current SCOPE.  */
13610               *type = parser->scope;
13611               /* The next name will not be qualified.  */
13612               parser->scope = NULL_TREE;
13613               parser->qualifying_scope = NULL_TREE;
13614               parser->object_scope = NULL_TREE;
13615               /* Look for the optional cv-qualifier-seq.  */
13616               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13617             }
13618         }
13619       /* If that didn't work we don't have a ptr-operator.  */
13620       if (!cp_parser_parse_definitely (parser))
13621         cp_parser_error (parser, "expected ptr-operator");
13622     }
13623
13624   return code;
13625 }
13626
13627 /* Parse an (optional) cv-qualifier-seq.
13628
13629    cv-qualifier-seq:
13630      cv-qualifier cv-qualifier-seq [opt]
13631
13632    cv-qualifier:
13633      const
13634      volatile
13635
13636    GNU Extension:
13637
13638    cv-qualifier:
13639      __restrict__
13640
13641    Returns a bitmask representing the cv-qualifiers.  */
13642
13643 static cp_cv_quals
13644 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13645 {
13646   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13647
13648   while (true)
13649     {
13650       cp_token *token;
13651       cp_cv_quals cv_qualifier;
13652
13653       /* Peek at the next token.  */
13654       token = cp_lexer_peek_token (parser->lexer);
13655       /* See if it's a cv-qualifier.  */
13656       switch (token->keyword)
13657         {
13658         case RID_CONST:
13659           cv_qualifier = TYPE_QUAL_CONST;
13660           break;
13661
13662         case RID_VOLATILE:
13663           cv_qualifier = TYPE_QUAL_VOLATILE;
13664           break;
13665
13666         case RID_RESTRICT:
13667           cv_qualifier = TYPE_QUAL_RESTRICT;
13668           break;
13669
13670         default:
13671           cv_qualifier = TYPE_UNQUALIFIED;
13672           break;
13673         }
13674
13675       if (!cv_qualifier)
13676         break;
13677
13678       if (cv_quals & cv_qualifier)
13679         {
13680           error ("%Hduplicate cv-qualifier", &token->location);
13681           cp_lexer_purge_token (parser->lexer);
13682         }
13683       else
13684         {
13685           cp_lexer_consume_token (parser->lexer);
13686           cv_quals |= cv_qualifier;
13687         }
13688     }
13689
13690   return cv_quals;
13691 }
13692
13693 /* Parse a late-specified return type, if any.  This is not a separate
13694    non-terminal, but part of a function declarator, which looks like
13695
13696    -> type-id
13697
13698    Returns the type indicated by the type-id.  */
13699
13700 static tree
13701 cp_parser_late_return_type_opt (cp_parser* parser)
13702 {
13703   cp_token *token;
13704
13705   /* Peek at the next token.  */
13706   token = cp_lexer_peek_token (parser->lexer);
13707   /* A late-specified return type is indicated by an initial '->'. */
13708   if (token->type != CPP_DEREF)
13709     return NULL_TREE;
13710
13711   /* Consume the ->.  */
13712   cp_lexer_consume_token (parser->lexer);
13713
13714   return cp_parser_type_id (parser);
13715 }
13716
13717 /* Parse a declarator-id.
13718
13719    declarator-id:
13720      id-expression
13721      :: [opt] nested-name-specifier [opt] type-name
13722
13723    In the `id-expression' case, the value returned is as for
13724    cp_parser_id_expression if the id-expression was an unqualified-id.
13725    If the id-expression was a qualified-id, then a SCOPE_REF is
13726    returned.  The first operand is the scope (either a NAMESPACE_DECL
13727    or TREE_TYPE), but the second is still just a representation of an
13728    unqualified-id.  */
13729
13730 static tree
13731 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13732 {
13733   tree id;
13734   /* The expression must be an id-expression.  Assume that qualified
13735      names are the names of types so that:
13736
13737        template <class T>
13738        int S<T>::R::i = 3;
13739
13740      will work; we must treat `S<T>::R' as the name of a type.
13741      Similarly, assume that qualified names are templates, where
13742      required, so that:
13743
13744        template <class T>
13745        int S<T>::R<T>::i = 3;
13746
13747      will work, too.  */
13748   id = cp_parser_id_expression (parser,
13749                                 /*template_keyword_p=*/false,
13750                                 /*check_dependency_p=*/false,
13751                                 /*template_p=*/NULL,
13752                                 /*declarator_p=*/true,
13753                                 optional_p);
13754   if (id && BASELINK_P (id))
13755     id = BASELINK_FUNCTIONS (id);
13756   return id;
13757 }
13758
13759 /* Parse a type-id.
13760
13761    type-id:
13762      type-specifier-seq abstract-declarator [opt]
13763
13764    Returns the TYPE specified.  */
13765
13766 static tree
13767 cp_parser_type_id (cp_parser* parser)
13768 {
13769   cp_decl_specifier_seq type_specifier_seq;
13770   cp_declarator *abstract_declarator;
13771
13772   /* Parse the type-specifier-seq.  */
13773   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13774                                 &type_specifier_seq);
13775   if (type_specifier_seq.type == error_mark_node)
13776     return error_mark_node;
13777
13778   /* There might or might not be an abstract declarator.  */
13779   cp_parser_parse_tentatively (parser);
13780   /* Look for the declarator.  */
13781   abstract_declarator
13782     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13783                             /*parenthesized_p=*/NULL,
13784                             /*member_p=*/false);
13785   /* Check to see if there really was a declarator.  */
13786   if (!cp_parser_parse_definitely (parser))
13787     abstract_declarator = NULL;
13788
13789   if (type_specifier_seq.type
13790       && type_uses_auto (type_specifier_seq.type))
13791     {
13792       error ("invalid use of %<auto%>");
13793       return error_mark_node;
13794     }
13795   
13796   return groktypename (&type_specifier_seq, abstract_declarator);
13797 }
13798
13799 /* Parse a type-specifier-seq.
13800
13801    type-specifier-seq:
13802      type-specifier type-specifier-seq [opt]
13803
13804    GNU extension:
13805
13806    type-specifier-seq:
13807      attributes type-specifier-seq [opt]
13808
13809    If IS_CONDITION is true, we are at the start of a "condition",
13810    e.g., we've just seen "if (".
13811
13812    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13813
13814 static void
13815 cp_parser_type_specifier_seq (cp_parser* parser,
13816                               bool is_condition,
13817                               cp_decl_specifier_seq *type_specifier_seq)
13818 {
13819   bool seen_type_specifier = false;
13820   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13821   cp_token *start_token = NULL;
13822
13823   /* Clear the TYPE_SPECIFIER_SEQ.  */
13824   clear_decl_specs (type_specifier_seq);
13825
13826   /* Parse the type-specifiers and attributes.  */
13827   while (true)
13828     {
13829       tree type_specifier;
13830       bool is_cv_qualifier;
13831
13832       /* Check for attributes first.  */
13833       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13834         {
13835           type_specifier_seq->attributes =
13836             chainon (type_specifier_seq->attributes,
13837                      cp_parser_attributes_opt (parser));
13838           continue;
13839         }
13840
13841       /* record the token of the beginning of the type specifier seq,
13842          for error reporting purposes*/
13843      if (!start_token)
13844        start_token = cp_lexer_peek_token (parser->lexer);
13845
13846       /* Look for the type-specifier.  */
13847       type_specifier = cp_parser_type_specifier (parser,
13848                                                  flags,
13849                                                  type_specifier_seq,
13850                                                  /*is_declaration=*/false,
13851                                                  NULL,
13852                                                  &is_cv_qualifier);
13853       if (!type_specifier)
13854         {
13855           /* If the first type-specifier could not be found, this is not a
13856              type-specifier-seq at all.  */
13857           if (!seen_type_specifier)
13858             {
13859               cp_parser_error (parser, "expected type-specifier");
13860               type_specifier_seq->type = error_mark_node;
13861               return;
13862             }
13863           /* If subsequent type-specifiers could not be found, the
13864              type-specifier-seq is complete.  */
13865           break;
13866         }
13867
13868       seen_type_specifier = true;
13869       /* The standard says that a condition can be:
13870
13871             type-specifier-seq declarator = assignment-expression
13872
13873          However, given:
13874
13875            struct S {};
13876            if (int S = ...)
13877
13878          we should treat the "S" as a declarator, not as a
13879          type-specifier.  The standard doesn't say that explicitly for
13880          type-specifier-seq, but it does say that for
13881          decl-specifier-seq in an ordinary declaration.  Perhaps it
13882          would be clearer just to allow a decl-specifier-seq here, and
13883          then add a semantic restriction that if any decl-specifiers
13884          that are not type-specifiers appear, the program is invalid.  */
13885       if (is_condition && !is_cv_qualifier)
13886         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13887     }
13888
13889   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13890 }
13891
13892 /* Parse a parameter-declaration-clause.
13893
13894    parameter-declaration-clause:
13895      parameter-declaration-list [opt] ... [opt]
13896      parameter-declaration-list , ...
13897
13898    Returns a representation for the parameter declarations.  A return
13899    value of NULL indicates a parameter-declaration-clause consisting
13900    only of an ellipsis.  */
13901
13902 static tree
13903 cp_parser_parameter_declaration_clause (cp_parser* parser)
13904 {
13905   tree parameters;
13906   cp_token *token;
13907   bool ellipsis_p;
13908   bool is_error;
13909
13910   /* Peek at the next token.  */
13911   token = cp_lexer_peek_token (parser->lexer);
13912   /* Check for trivial parameter-declaration-clauses.  */
13913   if (token->type == CPP_ELLIPSIS)
13914     {
13915       /* Consume the `...' token.  */
13916       cp_lexer_consume_token (parser->lexer);
13917       return NULL_TREE;
13918     }
13919   else if (token->type == CPP_CLOSE_PAREN)
13920     /* There are no parameters.  */
13921     {
13922 #ifndef NO_IMPLICIT_EXTERN_C
13923       if (in_system_header && current_class_type == NULL
13924           && current_lang_name == lang_name_c)
13925         return NULL_TREE;
13926       else
13927 #endif
13928         return void_list_node;
13929     }
13930   /* Check for `(void)', too, which is a special case.  */
13931   else if (token->keyword == RID_VOID
13932            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13933                == CPP_CLOSE_PAREN))
13934     {
13935       /* Consume the `void' token.  */
13936       cp_lexer_consume_token (parser->lexer);
13937       /* There are no parameters.  */
13938       return void_list_node;
13939     }
13940
13941   /* Parse the parameter-declaration-list.  */
13942   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13943   /* If a parse error occurred while parsing the
13944      parameter-declaration-list, then the entire
13945      parameter-declaration-clause is erroneous.  */
13946   if (is_error)
13947     return NULL;
13948
13949   /* Peek at the next token.  */
13950   token = cp_lexer_peek_token (parser->lexer);
13951   /* If it's a `,', the clause should terminate with an ellipsis.  */
13952   if (token->type == CPP_COMMA)
13953     {
13954       /* Consume the `,'.  */
13955       cp_lexer_consume_token (parser->lexer);
13956       /* Expect an ellipsis.  */
13957       ellipsis_p
13958         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13959     }
13960   /* It might also be `...' if the optional trailing `,' was
13961      omitted.  */
13962   else if (token->type == CPP_ELLIPSIS)
13963     {
13964       /* Consume the `...' token.  */
13965       cp_lexer_consume_token (parser->lexer);
13966       /* And remember that we saw it.  */
13967       ellipsis_p = true;
13968     }
13969   else
13970     ellipsis_p = false;
13971
13972   /* Finish the parameter list.  */
13973   if (!ellipsis_p)
13974     parameters = chainon (parameters, void_list_node);
13975
13976   return parameters;
13977 }
13978
13979 /* Parse a parameter-declaration-list.
13980
13981    parameter-declaration-list:
13982      parameter-declaration
13983      parameter-declaration-list , parameter-declaration
13984
13985    Returns a representation of the parameter-declaration-list, as for
13986    cp_parser_parameter_declaration_clause.  However, the
13987    `void_list_node' is never appended to the list.  Upon return,
13988    *IS_ERROR will be true iff an error occurred.  */
13989
13990 static tree
13991 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13992 {
13993   tree parameters = NULL_TREE;
13994   tree *tail = &parameters; 
13995   bool saved_in_unbraced_linkage_specification_p;
13996
13997   /* Assume all will go well.  */
13998   *is_error = false;
13999   /* The special considerations that apply to a function within an
14000      unbraced linkage specifications do not apply to the parameters
14001      to the function.  */
14002   saved_in_unbraced_linkage_specification_p 
14003     = parser->in_unbraced_linkage_specification_p;
14004   parser->in_unbraced_linkage_specification_p = false;
14005
14006   /* Look for more parameters.  */
14007   while (true)
14008     {
14009       cp_parameter_declarator *parameter;
14010       tree decl = error_mark_node;
14011       bool parenthesized_p;
14012       /* Parse the parameter.  */
14013       parameter
14014         = cp_parser_parameter_declaration (parser,
14015                                            /*template_parm_p=*/false,
14016                                            &parenthesized_p);
14017
14018       /* We don't know yet if the enclosing context is deprecated, so wait
14019          and warn in grokparms if appropriate.  */
14020       deprecated_state = DEPRECATED_SUPPRESS;
14021
14022       if (parameter)
14023         decl = grokdeclarator (parameter->declarator,
14024                                &parameter->decl_specifiers,
14025                                PARM,
14026                                parameter->default_argument != NULL_TREE,
14027                                &parameter->decl_specifiers.attributes);
14028
14029       deprecated_state = DEPRECATED_NORMAL;
14030
14031       /* If a parse error occurred parsing the parameter declaration,
14032          then the entire parameter-declaration-list is erroneous.  */
14033       if (decl == error_mark_node)
14034         {
14035           *is_error = true;
14036           parameters = error_mark_node;
14037           break;
14038         }
14039
14040       if (parameter->decl_specifiers.attributes)
14041         cplus_decl_attributes (&decl,
14042                                parameter->decl_specifiers.attributes,
14043                                0);
14044       if (DECL_NAME (decl))
14045         decl = pushdecl (decl);
14046
14047       /* Add the new parameter to the list.  */
14048       *tail = build_tree_list (parameter->default_argument, decl);
14049       tail = &TREE_CHAIN (*tail);
14050
14051       /* Peek at the next token.  */
14052       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14053           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14054           /* These are for Objective-C++ */
14055           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14056           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14057         /* The parameter-declaration-list is complete.  */
14058         break;
14059       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14060         {
14061           cp_token *token;
14062
14063           /* Peek at the next token.  */
14064           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14065           /* If it's an ellipsis, then the list is complete.  */
14066           if (token->type == CPP_ELLIPSIS)
14067             break;
14068           /* Otherwise, there must be more parameters.  Consume the
14069              `,'.  */
14070           cp_lexer_consume_token (parser->lexer);
14071           /* When parsing something like:
14072
14073                 int i(float f, double d)
14074
14075              we can tell after seeing the declaration for "f" that we
14076              are not looking at an initialization of a variable "i",
14077              but rather at the declaration of a function "i".
14078
14079              Due to the fact that the parsing of template arguments
14080              (as specified to a template-id) requires backtracking we
14081              cannot use this technique when inside a template argument
14082              list.  */
14083           if (!parser->in_template_argument_list_p
14084               && !parser->in_type_id_in_expr_p
14085               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14086               /* However, a parameter-declaration of the form
14087                  "foat(f)" (which is a valid declaration of a
14088                  parameter "f") can also be interpreted as an
14089                  expression (the conversion of "f" to "float").  */
14090               && !parenthesized_p)
14091             cp_parser_commit_to_tentative_parse (parser);
14092         }
14093       else
14094         {
14095           cp_parser_error (parser, "expected %<,%> or %<...%>");
14096           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14097             cp_parser_skip_to_closing_parenthesis (parser,
14098                                                    /*recovering=*/true,
14099                                                    /*or_comma=*/false,
14100                                                    /*consume_paren=*/false);
14101           break;
14102         }
14103     }
14104
14105   parser->in_unbraced_linkage_specification_p
14106     = saved_in_unbraced_linkage_specification_p;
14107
14108   return parameters;
14109 }
14110
14111 /* Parse a parameter declaration.
14112
14113    parameter-declaration:
14114      decl-specifier-seq ... [opt] declarator
14115      decl-specifier-seq declarator = assignment-expression
14116      decl-specifier-seq ... [opt] abstract-declarator [opt]
14117      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14118
14119    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14120    declares a template parameter.  (In that case, a non-nested `>'
14121    token encountered during the parsing of the assignment-expression
14122    is not interpreted as a greater-than operator.)
14123
14124    Returns a representation of the parameter, or NULL if an error
14125    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14126    true iff the declarator is of the form "(p)".  */
14127
14128 static cp_parameter_declarator *
14129 cp_parser_parameter_declaration (cp_parser *parser,
14130                                  bool template_parm_p,
14131                                  bool *parenthesized_p)
14132 {
14133   int declares_class_or_enum;
14134   bool greater_than_is_operator_p;
14135   cp_decl_specifier_seq decl_specifiers;
14136   cp_declarator *declarator;
14137   tree default_argument;
14138   cp_token *token = NULL, *declarator_token_start = NULL;
14139   const char *saved_message;
14140
14141   /* In a template parameter, `>' is not an operator.
14142
14143      [temp.param]
14144
14145      When parsing a default template-argument for a non-type
14146      template-parameter, the first non-nested `>' is taken as the end
14147      of the template parameter-list rather than a greater-than
14148      operator.  */
14149   greater_than_is_operator_p = !template_parm_p;
14150
14151   /* Type definitions may not appear in parameter types.  */
14152   saved_message = parser->type_definition_forbidden_message;
14153   parser->type_definition_forbidden_message
14154     = "types may not be defined in parameter types";
14155
14156   /* Parse the declaration-specifiers.  */
14157   cp_parser_decl_specifier_seq (parser,
14158                                 CP_PARSER_FLAGS_NONE,
14159                                 &decl_specifiers,
14160                                 &declares_class_or_enum);
14161   /* If an error occurred, there's no reason to attempt to parse the
14162      rest of the declaration.  */
14163   if (cp_parser_error_occurred (parser))
14164     {
14165       parser->type_definition_forbidden_message = saved_message;
14166       return NULL;
14167     }
14168
14169   /* Peek at the next token.  */
14170   token = cp_lexer_peek_token (parser->lexer);
14171
14172   /* If the next token is a `)', `,', `=', `>', or `...', then there
14173      is no declarator. However, when variadic templates are enabled,
14174      there may be a declarator following `...'.  */
14175   if (token->type == CPP_CLOSE_PAREN
14176       || token->type == CPP_COMMA
14177       || token->type == CPP_EQ
14178       || token->type == CPP_GREATER)
14179     {
14180       declarator = NULL;
14181       if (parenthesized_p)
14182         *parenthesized_p = false;
14183     }
14184   /* Otherwise, there should be a declarator.  */
14185   else
14186     {
14187       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14188       parser->default_arg_ok_p = false;
14189
14190       /* After seeing a decl-specifier-seq, if the next token is not a
14191          "(", there is no possibility that the code is a valid
14192          expression.  Therefore, if parsing tentatively, we commit at
14193          this point.  */
14194       if (!parser->in_template_argument_list_p
14195           /* In an expression context, having seen:
14196
14197                (int((char ...
14198
14199              we cannot be sure whether we are looking at a
14200              function-type (taking a "char" as a parameter) or a cast
14201              of some object of type "char" to "int".  */
14202           && !parser->in_type_id_in_expr_p
14203           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14204           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14205         cp_parser_commit_to_tentative_parse (parser);
14206       /* Parse the declarator.  */
14207       declarator_token_start = token;
14208       declarator = cp_parser_declarator (parser,
14209                                          CP_PARSER_DECLARATOR_EITHER,
14210                                          /*ctor_dtor_or_conv_p=*/NULL,
14211                                          parenthesized_p,
14212                                          /*member_p=*/false);
14213       parser->default_arg_ok_p = saved_default_arg_ok_p;
14214       /* After the declarator, allow more attributes.  */
14215       decl_specifiers.attributes
14216         = chainon (decl_specifiers.attributes,
14217                    cp_parser_attributes_opt (parser));
14218     }
14219
14220   /* If the next token is an ellipsis, and we have not seen a
14221      declarator name, and the type of the declarator contains parameter
14222      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14223      a parameter pack expansion expression. Otherwise, leave the
14224      ellipsis for a C-style variadic function. */
14225   token = cp_lexer_peek_token (parser->lexer);
14226   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14227     {
14228       tree type = decl_specifiers.type;
14229
14230       if (type && DECL_P (type))
14231         type = TREE_TYPE (type);
14232
14233       if (type
14234           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14235           && declarator_can_be_parameter_pack (declarator)
14236           && (!declarator || !declarator->parameter_pack_p)
14237           && uses_parameter_packs (type))
14238         {
14239           /* Consume the `...'. */
14240           cp_lexer_consume_token (parser->lexer);
14241           maybe_warn_variadic_templates ();
14242           
14243           /* Build a pack expansion type */
14244           if (declarator)
14245             declarator->parameter_pack_p = true;
14246           else
14247             decl_specifiers.type = make_pack_expansion (type);
14248         }
14249     }
14250
14251   /* The restriction on defining new types applies only to the type
14252      of the parameter, not to the default argument.  */
14253   parser->type_definition_forbidden_message = saved_message;
14254
14255   /* If the next token is `=', then process a default argument.  */
14256   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14257     {
14258       /* Consume the `='.  */
14259       cp_lexer_consume_token (parser->lexer);
14260
14261       /* If we are defining a class, then the tokens that make up the
14262          default argument must be saved and processed later.  */
14263       if (!template_parm_p && at_class_scope_p ()
14264           && TYPE_BEING_DEFINED (current_class_type))
14265         {
14266           unsigned depth = 0;
14267           int maybe_template_id = 0;
14268           cp_token *first_token;
14269           cp_token *token;
14270
14271           /* Add tokens until we have processed the entire default
14272              argument.  We add the range [first_token, token).  */
14273           first_token = cp_lexer_peek_token (parser->lexer);
14274           while (true)
14275             {
14276               bool done = false;
14277
14278               /* Peek at the next token.  */
14279               token = cp_lexer_peek_token (parser->lexer);
14280               /* What we do depends on what token we have.  */
14281               switch (token->type)
14282                 {
14283                   /* In valid code, a default argument must be
14284                      immediately followed by a `,' `)', or `...'.  */
14285                 case CPP_COMMA:
14286                   if (depth == 0 && maybe_template_id)
14287                     {
14288                       /* If we've seen a '<', we might be in a
14289                          template-argument-list.  Until Core issue 325 is
14290                          resolved, we don't know how this situation ought
14291                          to be handled, so try to DTRT.  We check whether
14292                          what comes after the comma is a valid parameter
14293                          declaration list.  If it is, then the comma ends
14294                          the default argument; otherwise the default
14295                          argument continues.  */
14296                       bool error = false;
14297
14298                       /* Set ITALP so cp_parser_parameter_declaration_list
14299                          doesn't decide to commit to this parse.  */
14300                       bool saved_italp = parser->in_template_argument_list_p;
14301                       parser->in_template_argument_list_p = true;
14302
14303                       cp_parser_parse_tentatively (parser);
14304                       cp_lexer_consume_token (parser->lexer);
14305                       cp_parser_parameter_declaration_list (parser, &error);
14306                       if (!cp_parser_error_occurred (parser) && !error)
14307                         done = true;
14308                       cp_parser_abort_tentative_parse (parser);
14309
14310                       parser->in_template_argument_list_p = saved_italp;
14311                       break;
14312                     }
14313                 case CPP_CLOSE_PAREN:
14314                 case CPP_ELLIPSIS:
14315                   /* If we run into a non-nested `;', `}', or `]',
14316                      then the code is invalid -- but the default
14317                      argument is certainly over.  */
14318                 case CPP_SEMICOLON:
14319                 case CPP_CLOSE_BRACE:
14320                 case CPP_CLOSE_SQUARE:
14321                   if (depth == 0)
14322                     done = true;
14323                   /* Update DEPTH, if necessary.  */
14324                   else if (token->type == CPP_CLOSE_PAREN
14325                            || token->type == CPP_CLOSE_BRACE
14326                            || token->type == CPP_CLOSE_SQUARE)
14327                     --depth;
14328                   break;
14329
14330                 case CPP_OPEN_PAREN:
14331                 case CPP_OPEN_SQUARE:
14332                 case CPP_OPEN_BRACE:
14333                   ++depth;
14334                   break;
14335
14336                 case CPP_LESS:
14337                   if (depth == 0)
14338                     /* This might be the comparison operator, or it might
14339                        start a template argument list.  */
14340                     ++maybe_template_id;
14341                   break;
14342
14343                 case CPP_RSHIFT:
14344                   if (cxx_dialect == cxx98)
14345                     break;
14346                   /* Fall through for C++0x, which treats the `>>'
14347                      operator like two `>' tokens in certain
14348                      cases.  */
14349
14350                 case CPP_GREATER:
14351                   if (depth == 0)
14352                     {
14353                       /* This might be an operator, or it might close a
14354                          template argument list.  But if a previous '<'
14355                          started a template argument list, this will have
14356                          closed it, so we can't be in one anymore.  */
14357                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14358                       if (maybe_template_id < 0)
14359                         maybe_template_id = 0;
14360                     }
14361                   break;
14362
14363                   /* If we run out of tokens, issue an error message.  */
14364                 case CPP_EOF:
14365                 case CPP_PRAGMA_EOL:
14366                   error ("%Hfile ends in default argument", &token->location);
14367                   done = true;
14368                   break;
14369
14370                 case CPP_NAME:
14371                 case CPP_SCOPE:
14372                   /* In these cases, we should look for template-ids.
14373                      For example, if the default argument is
14374                      `X<int, double>()', we need to do name lookup to
14375                      figure out whether or not `X' is a template; if
14376                      so, the `,' does not end the default argument.
14377
14378                      That is not yet done.  */
14379                   break;
14380
14381                 default:
14382                   break;
14383                 }
14384
14385               /* If we've reached the end, stop.  */
14386               if (done)
14387                 break;
14388
14389               /* Add the token to the token block.  */
14390               token = cp_lexer_consume_token (parser->lexer);
14391             }
14392
14393           /* Create a DEFAULT_ARG to represent the unparsed default
14394              argument.  */
14395           default_argument = make_node (DEFAULT_ARG);
14396           DEFARG_TOKENS (default_argument)
14397             = cp_token_cache_new (first_token, token);
14398           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14399         }
14400       /* Outside of a class definition, we can just parse the
14401          assignment-expression.  */
14402       else
14403         {
14404           token = cp_lexer_peek_token (parser->lexer);
14405           default_argument 
14406             = cp_parser_default_argument (parser, template_parm_p);
14407         }
14408
14409       if (!parser->default_arg_ok_p)
14410         {
14411           if (flag_permissive)
14412             warning (0, "deprecated use of default argument for parameter of non-function");
14413           else
14414             {
14415               error ("%Hdefault arguments are only "
14416                      "permitted for function parameters",
14417                      &token->location);
14418               default_argument = NULL_TREE;
14419             }
14420         }
14421       else if ((declarator && declarator->parameter_pack_p)
14422                || (decl_specifiers.type
14423                    && PACK_EXPANSION_P (decl_specifiers.type)))
14424         {
14425           const char* kind = template_parm_p? "template " : "";
14426           
14427           /* Find the name of the parameter pack.  */     
14428           cp_declarator *id_declarator = declarator;
14429           while (id_declarator && id_declarator->kind != cdk_id)
14430             id_declarator = id_declarator->declarator;
14431           
14432           if (id_declarator && id_declarator->kind == cdk_id)
14433             error ("%H%sparameter pack %qD cannot have a default argument",
14434                    &declarator_token_start->location,
14435                    kind, id_declarator->u.id.unqualified_name);
14436           else
14437             error ("%H%sparameter pack cannot have a default argument",
14438                    &declarator_token_start->location, kind);
14439           
14440           default_argument = NULL_TREE;
14441         }
14442     }
14443   else
14444     default_argument = NULL_TREE;
14445
14446   return make_parameter_declarator (&decl_specifiers,
14447                                     declarator,
14448                                     default_argument);
14449 }
14450
14451 /* Parse a default argument and return it.
14452
14453    TEMPLATE_PARM_P is true if this is a default argument for a
14454    non-type template parameter.  */
14455 static tree
14456 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14457 {
14458   tree default_argument = NULL_TREE;
14459   bool saved_greater_than_is_operator_p;
14460   bool saved_local_variables_forbidden_p;
14461
14462   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14463      set correctly.  */
14464   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14465   parser->greater_than_is_operator_p = !template_parm_p;
14466   /* Local variable names (and the `this' keyword) may not
14467      appear in a default argument.  */
14468   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14469   parser->local_variables_forbidden_p = true;
14470   /* The default argument expression may cause implicitly
14471      defined member functions to be synthesized, which will
14472      result in garbage collection.  We must treat this
14473      situation as if we were within the body of function so as
14474      to avoid collecting live data on the stack.  */
14475   ++function_depth;
14476   /* Parse the assignment-expression.  */
14477   if (template_parm_p)
14478     push_deferring_access_checks (dk_no_deferred);
14479   default_argument
14480     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14481   if (template_parm_p)
14482     pop_deferring_access_checks ();
14483   /* Restore saved state.  */
14484   --function_depth;
14485   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14486   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14487
14488   return default_argument;
14489 }
14490
14491 /* Parse a function-body.
14492
14493    function-body:
14494      compound_statement  */
14495
14496 static void
14497 cp_parser_function_body (cp_parser *parser)
14498 {
14499   cp_parser_compound_statement (parser, NULL, false);
14500 }
14501
14502 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14503    true if a ctor-initializer was present.  */
14504
14505 static bool
14506 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14507 {
14508   tree body;
14509   bool ctor_initializer_p;
14510
14511   /* Begin the function body.  */
14512   body = begin_function_body ();
14513   /* Parse the optional ctor-initializer.  */
14514   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14515   /* Parse the function-body.  */
14516   cp_parser_function_body (parser);
14517   /* Finish the function body.  */
14518   finish_function_body (body);
14519
14520   return ctor_initializer_p;
14521 }
14522
14523 /* Parse an initializer.
14524
14525    initializer:
14526      = initializer-clause
14527      ( expression-list )
14528
14529    Returns an expression representing the initializer.  If no
14530    initializer is present, NULL_TREE is returned.
14531
14532    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14533    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14534    set to TRUE if there is no initializer present.  If there is an
14535    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14536    is set to true; otherwise it is set to false.  */
14537
14538 static tree
14539 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14540                        bool* non_constant_p)
14541 {
14542   cp_token *token;
14543   tree init;
14544
14545   /* Peek at the next token.  */
14546   token = cp_lexer_peek_token (parser->lexer);
14547
14548   /* Let our caller know whether or not this initializer was
14549      parenthesized.  */
14550   *is_direct_init = (token->type != CPP_EQ);
14551   /* Assume that the initializer is constant.  */
14552   *non_constant_p = false;
14553
14554   if (token->type == CPP_EQ)
14555     {
14556       /* Consume the `='.  */
14557       cp_lexer_consume_token (parser->lexer);
14558       /* Parse the initializer-clause.  */
14559       init = cp_parser_initializer_clause (parser, non_constant_p);
14560     }
14561   else if (token->type == CPP_OPEN_PAREN)
14562     init = cp_parser_parenthesized_expression_list (parser, false,
14563                                                     /*cast_p=*/false,
14564                                                     /*allow_expansion_p=*/true,
14565                                                     non_constant_p);
14566   else if (token->type == CPP_OPEN_BRACE)
14567     {
14568       maybe_warn_cpp0x ("extended initializer lists");
14569       init = cp_parser_braced_list (parser, non_constant_p);
14570       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14571     }
14572   else
14573     {
14574       /* Anything else is an error.  */
14575       cp_parser_error (parser, "expected initializer");
14576       init = error_mark_node;
14577     }
14578
14579   return init;
14580 }
14581
14582 /* Parse an initializer-clause.
14583
14584    initializer-clause:
14585      assignment-expression
14586      braced-init-list
14587
14588    Returns an expression representing the initializer.
14589
14590    If the `assignment-expression' production is used the value
14591    returned is simply a representation for the expression.
14592
14593    Otherwise, calls cp_parser_braced_list.  */
14594
14595 static tree
14596 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14597 {
14598   tree initializer;
14599
14600   /* Assume the expression is constant.  */
14601   *non_constant_p = false;
14602
14603   /* If it is not a `{', then we are looking at an
14604      assignment-expression.  */
14605   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14606     {
14607       initializer
14608         = cp_parser_constant_expression (parser,
14609                                         /*allow_non_constant_p=*/true,
14610                                         non_constant_p);
14611       if (!*non_constant_p)
14612         initializer = fold_non_dependent_expr (initializer);
14613     }
14614   else
14615     initializer = cp_parser_braced_list (parser, non_constant_p);
14616
14617   return initializer;
14618 }
14619
14620 /* Parse a brace-enclosed initializer list.
14621
14622    braced-init-list:
14623      { initializer-list , [opt] }
14624      { }
14625
14626    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14627    the elements of the initializer-list (or NULL, if the last
14628    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14629    NULL_TREE.  There is no way to detect whether or not the optional
14630    trailing `,' was provided.  NON_CONSTANT_P is as for
14631    cp_parser_initializer.  */     
14632
14633 static tree
14634 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14635 {
14636   tree initializer;
14637
14638   /* Consume the `{' token.  */
14639   cp_lexer_consume_token (parser->lexer);
14640   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14641   initializer = make_node (CONSTRUCTOR);
14642   /* If it's not a `}', then there is a non-trivial initializer.  */
14643   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14644     {
14645       /* Parse the initializer list.  */
14646       CONSTRUCTOR_ELTS (initializer)
14647         = cp_parser_initializer_list (parser, non_constant_p);
14648       /* A trailing `,' token is allowed.  */
14649       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14650         cp_lexer_consume_token (parser->lexer);
14651     }
14652   /* Now, there should be a trailing `}'.  */
14653   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14654   TREE_TYPE (initializer) = init_list_type_node;
14655   return initializer;
14656 }
14657
14658 /* Parse an initializer-list.
14659
14660    initializer-list:
14661      initializer-clause ... [opt]
14662      initializer-list , initializer-clause ... [opt]
14663
14664    GNU Extension:
14665
14666    initializer-list:
14667      identifier : initializer-clause
14668      initializer-list, identifier : initializer-clause
14669
14670    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14671    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14672    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14673    as for cp_parser_initializer.  */
14674
14675 static VEC(constructor_elt,gc) *
14676 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14677 {
14678   VEC(constructor_elt,gc) *v = NULL;
14679
14680   /* Assume all of the expressions are constant.  */
14681   *non_constant_p = false;
14682
14683   /* Parse the rest of the list.  */
14684   while (true)
14685     {
14686       cp_token *token;
14687       tree identifier;
14688       tree initializer;
14689       bool clause_non_constant_p;
14690
14691       /* If the next token is an identifier and the following one is a
14692          colon, we are looking at the GNU designated-initializer
14693          syntax.  */
14694       if (cp_parser_allow_gnu_extensions_p (parser)
14695           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14696           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14697         {
14698           /* Warn the user that they are using an extension.  */
14699           pedwarn (input_location, OPT_pedantic, 
14700                    "ISO C++ does not allow designated initializers");
14701           /* Consume the identifier.  */
14702           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14703           /* Consume the `:'.  */
14704           cp_lexer_consume_token (parser->lexer);
14705         }
14706       else
14707         identifier = NULL_TREE;
14708
14709       /* Parse the initializer.  */
14710       initializer = cp_parser_initializer_clause (parser,
14711                                                   &clause_non_constant_p);
14712       /* If any clause is non-constant, so is the entire initializer.  */
14713       if (clause_non_constant_p)
14714         *non_constant_p = true;
14715
14716       /* If we have an ellipsis, this is an initializer pack
14717          expansion.  */
14718       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14719         {
14720           /* Consume the `...'.  */
14721           cp_lexer_consume_token (parser->lexer);
14722
14723           /* Turn the initializer into an initializer expansion.  */
14724           initializer = make_pack_expansion (initializer);
14725         }
14726
14727       /* Add it to the vector.  */
14728       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14729
14730       /* If the next token is not a comma, we have reached the end of
14731          the list.  */
14732       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14733         break;
14734
14735       /* Peek at the next token.  */
14736       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14737       /* If the next token is a `}', then we're still done.  An
14738          initializer-clause can have a trailing `,' after the
14739          initializer-list and before the closing `}'.  */
14740       if (token->type == CPP_CLOSE_BRACE)
14741         break;
14742
14743       /* Consume the `,' token.  */
14744       cp_lexer_consume_token (parser->lexer);
14745     }
14746
14747   return v;
14748 }
14749
14750 /* Classes [gram.class] */
14751
14752 /* Parse a class-name.
14753
14754    class-name:
14755      identifier
14756      template-id
14757
14758    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14759    to indicate that names looked up in dependent types should be
14760    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14761    keyword has been used to indicate that the name that appears next
14762    is a template.  TAG_TYPE indicates the explicit tag given before
14763    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14764    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14765    is the class being defined in a class-head.
14766
14767    Returns the TYPE_DECL representing the class.  */
14768
14769 static tree
14770 cp_parser_class_name (cp_parser *parser,
14771                       bool typename_keyword_p,
14772                       bool template_keyword_p,
14773                       enum tag_types tag_type,
14774                       bool check_dependency_p,
14775                       bool class_head_p,
14776                       bool is_declaration)
14777 {
14778   tree decl;
14779   tree scope;
14780   bool typename_p;
14781   cp_token *token;
14782   tree identifier = NULL_TREE;
14783
14784   /* All class-names start with an identifier.  */
14785   token = cp_lexer_peek_token (parser->lexer);
14786   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14787     {
14788       cp_parser_error (parser, "expected class-name");
14789       return error_mark_node;
14790     }
14791
14792   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14793      to a template-id, so we save it here.  */
14794   scope = parser->scope;
14795   if (scope == error_mark_node)
14796     return error_mark_node;
14797
14798   /* Any name names a type if we're following the `typename' keyword
14799      in a qualified name where the enclosing scope is type-dependent.  */
14800   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14801                 && dependent_type_p (scope));
14802   /* Handle the common case (an identifier, but not a template-id)
14803      efficiently.  */
14804   if (token->type == CPP_NAME
14805       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14806     {
14807       cp_token *identifier_token;
14808       bool ambiguous_p;
14809
14810       /* Look for the identifier.  */
14811       identifier_token = cp_lexer_peek_token (parser->lexer);
14812       ambiguous_p = identifier_token->ambiguous_p;
14813       identifier = cp_parser_identifier (parser);
14814       /* If the next token isn't an identifier, we are certainly not
14815          looking at a class-name.  */
14816       if (identifier == error_mark_node)
14817         decl = error_mark_node;
14818       /* If we know this is a type-name, there's no need to look it
14819          up.  */
14820       else if (typename_p)
14821         decl = identifier;
14822       else
14823         {
14824           tree ambiguous_decls;
14825           /* If we already know that this lookup is ambiguous, then
14826              we've already issued an error message; there's no reason
14827              to check again.  */
14828           if (ambiguous_p)
14829             {
14830               cp_parser_simulate_error (parser);
14831               return error_mark_node;
14832             }
14833           /* If the next token is a `::', then the name must be a type
14834              name.
14835
14836              [basic.lookup.qual]
14837
14838              During the lookup for a name preceding the :: scope
14839              resolution operator, object, function, and enumerator
14840              names are ignored.  */
14841           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14842             tag_type = typename_type;
14843           /* Look up the name.  */
14844           decl = cp_parser_lookup_name (parser, identifier,
14845                                         tag_type,
14846                                         /*is_template=*/false,
14847                                         /*is_namespace=*/false,
14848                                         check_dependency_p,
14849                                         &ambiguous_decls,
14850                                         identifier_token->location);
14851           if (ambiguous_decls)
14852             {
14853               error ("%Hreference to %qD is ambiguous",
14854                      &identifier_token->location, identifier);
14855               print_candidates (ambiguous_decls);
14856               if (cp_parser_parsing_tentatively (parser))
14857                 {
14858                   identifier_token->ambiguous_p = true;
14859                   cp_parser_simulate_error (parser);
14860                 }
14861               return error_mark_node;
14862             }
14863         }
14864     }
14865   else
14866     {
14867       /* Try a template-id.  */
14868       decl = cp_parser_template_id (parser, template_keyword_p,
14869                                     check_dependency_p,
14870                                     is_declaration);
14871       if (decl == error_mark_node)
14872         return error_mark_node;
14873     }
14874
14875   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14876
14877   /* If this is a typename, create a TYPENAME_TYPE.  */
14878   if (typename_p && decl != error_mark_node)
14879     {
14880       decl = make_typename_type (scope, decl, typename_type,
14881                                  /*complain=*/tf_error);
14882       if (decl != error_mark_node)
14883         decl = TYPE_NAME (decl);
14884     }
14885
14886   /* Check to see that it is really the name of a class.  */
14887   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14888       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14889       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14890     /* Situations like this:
14891
14892          template <typename T> struct A {
14893            typename T::template X<int>::I i;
14894          };
14895
14896        are problematic.  Is `T::template X<int>' a class-name?  The
14897        standard does not seem to be definitive, but there is no other
14898        valid interpretation of the following `::'.  Therefore, those
14899        names are considered class-names.  */
14900     {
14901       decl = make_typename_type (scope, decl, tag_type, tf_error);
14902       if (decl != error_mark_node)
14903         decl = TYPE_NAME (decl);
14904     }
14905   else if (TREE_CODE (decl) != TYPE_DECL
14906            || TREE_TYPE (decl) == error_mark_node
14907            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14908     decl = error_mark_node;
14909
14910   if (decl == error_mark_node)
14911     cp_parser_error (parser, "expected class-name");
14912   else if (identifier && !parser->scope)
14913     maybe_note_name_used_in_class (identifier, decl);
14914
14915   return decl;
14916 }
14917
14918 /* Parse a class-specifier.
14919
14920    class-specifier:
14921      class-head { member-specification [opt] }
14922
14923    Returns the TREE_TYPE representing the class.  */
14924
14925 static tree
14926 cp_parser_class_specifier (cp_parser* parser)
14927 {
14928   cp_token *token;
14929   tree type;
14930   tree attributes = NULL_TREE;
14931   int has_trailing_semicolon;
14932   bool nested_name_specifier_p;
14933   unsigned saved_num_template_parameter_lists;
14934   bool saved_in_function_body;
14935   bool saved_in_unbraced_linkage_specification_p;
14936   tree old_scope = NULL_TREE;
14937   tree scope = NULL_TREE;
14938   tree bases;
14939
14940   push_deferring_access_checks (dk_no_deferred);
14941
14942   /* Parse the class-head.  */
14943   type = cp_parser_class_head (parser,
14944                                &nested_name_specifier_p,
14945                                &attributes,
14946                                &bases);
14947   /* If the class-head was a semantic disaster, skip the entire body
14948      of the class.  */
14949   if (!type)
14950     {
14951       cp_parser_skip_to_end_of_block_or_statement (parser);
14952       pop_deferring_access_checks ();
14953       return error_mark_node;
14954     }
14955
14956   /* Look for the `{'.  */
14957   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14958     {
14959       pop_deferring_access_checks ();
14960       return error_mark_node;
14961     }
14962
14963   /* Process the base classes. If they're invalid, skip the 
14964      entire class body.  */
14965   if (!xref_basetypes (type, bases))
14966     {
14967       /* Consuming the closing brace yields better error messages
14968          later on.  */
14969       if (cp_parser_skip_to_closing_brace (parser))
14970         cp_lexer_consume_token (parser->lexer);
14971       pop_deferring_access_checks ();
14972       return error_mark_node;
14973     }
14974
14975   /* Issue an error message if type-definitions are forbidden here.  */
14976   cp_parser_check_type_definition (parser);
14977   /* Remember that we are defining one more class.  */
14978   ++parser->num_classes_being_defined;
14979   /* Inside the class, surrounding template-parameter-lists do not
14980      apply.  */
14981   saved_num_template_parameter_lists
14982     = parser->num_template_parameter_lists;
14983   parser->num_template_parameter_lists = 0;
14984   /* We are not in a function body.  */
14985   saved_in_function_body = parser->in_function_body;
14986   parser->in_function_body = false;
14987   /* We are not immediately inside an extern "lang" block.  */
14988   saved_in_unbraced_linkage_specification_p
14989     = parser->in_unbraced_linkage_specification_p;
14990   parser->in_unbraced_linkage_specification_p = false;
14991
14992   /* Start the class.  */
14993   if (nested_name_specifier_p)
14994     {
14995       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14996       old_scope = push_inner_scope (scope);
14997     }
14998   type = begin_class_definition (type, attributes);
14999
15000   if (type == error_mark_node)
15001     /* If the type is erroneous, skip the entire body of the class.  */
15002     cp_parser_skip_to_closing_brace (parser);
15003   else
15004     /* Parse the member-specification.  */
15005     cp_parser_member_specification_opt (parser);
15006
15007   /* Look for the trailing `}'.  */
15008   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15009   /* We get better error messages by noticing a common problem: a
15010      missing trailing `;'.  */
15011   token = cp_lexer_peek_token (parser->lexer);
15012   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
15013   /* Look for trailing attributes to apply to this class.  */
15014   if (cp_parser_allow_gnu_extensions_p (parser))
15015     attributes = cp_parser_attributes_opt (parser);
15016   if (type != error_mark_node)
15017     type = finish_struct (type, attributes);
15018   if (nested_name_specifier_p)
15019     pop_inner_scope (old_scope, scope);
15020   /* If this class is not itself within the scope of another class,
15021      then we need to parse the bodies of all of the queued function
15022      definitions.  Note that the queued functions defined in a class
15023      are not always processed immediately following the
15024      class-specifier for that class.  Consider:
15025
15026        struct A {
15027          struct B { void f() { sizeof (A); } };
15028        };
15029
15030      If `f' were processed before the processing of `A' were
15031      completed, there would be no way to compute the size of `A'.
15032      Note that the nesting we are interested in here is lexical --
15033      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15034      for:
15035
15036        struct A { struct B; };
15037        struct A::B { void f() { } };
15038
15039      there is no need to delay the parsing of `A::B::f'.  */
15040   if (--parser->num_classes_being_defined == 0)
15041     {
15042       tree queue_entry;
15043       tree fn;
15044       tree class_type = NULL_TREE;
15045       tree pushed_scope = NULL_TREE;
15046
15047       /* In a first pass, parse default arguments to the functions.
15048          Then, in a second pass, parse the bodies of the functions.
15049          This two-phased approach handles cases like:
15050
15051             struct S {
15052               void f() { g(); }
15053               void g(int i = 3);
15054             };
15055
15056          */
15057       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15058              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15059            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15060            TREE_PURPOSE (parser->unparsed_functions_queues)
15061              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15062         {
15063           fn = TREE_VALUE (queue_entry);
15064           /* If there are default arguments that have not yet been processed,
15065              take care of them now.  */
15066           if (class_type != TREE_PURPOSE (queue_entry))
15067             {
15068               if (pushed_scope)
15069                 pop_scope (pushed_scope);
15070               class_type = TREE_PURPOSE (queue_entry);
15071               pushed_scope = push_scope (class_type);
15072             }
15073           /* Make sure that any template parameters are in scope.  */
15074           maybe_begin_member_template_processing (fn);
15075           /* Parse the default argument expressions.  */
15076           cp_parser_late_parsing_default_args (parser, fn);
15077           /* Remove any template parameters from the symbol table.  */
15078           maybe_end_member_template_processing ();
15079         }
15080       if (pushed_scope)
15081         pop_scope (pushed_scope);
15082       /* Now parse the body of the functions.  */
15083       for (TREE_VALUE (parser->unparsed_functions_queues)
15084              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15085            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15086            TREE_VALUE (parser->unparsed_functions_queues)
15087              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15088         {
15089           /* Figure out which function we need to process.  */
15090           fn = TREE_VALUE (queue_entry);
15091           /* Parse the function.  */
15092           cp_parser_late_parsing_for_member (parser, fn);
15093         }
15094     }
15095
15096   /* Put back any saved access checks.  */
15097   pop_deferring_access_checks ();
15098
15099   /* Restore saved state.  */
15100   parser->in_function_body = saved_in_function_body;
15101   parser->num_template_parameter_lists
15102     = saved_num_template_parameter_lists;
15103   parser->in_unbraced_linkage_specification_p
15104     = saved_in_unbraced_linkage_specification_p;
15105
15106   return type;
15107 }
15108
15109 /* Parse a class-head.
15110
15111    class-head:
15112      class-key identifier [opt] base-clause [opt]
15113      class-key nested-name-specifier identifier base-clause [opt]
15114      class-key nested-name-specifier [opt] template-id
15115        base-clause [opt]
15116
15117    GNU Extensions:
15118      class-key attributes identifier [opt] base-clause [opt]
15119      class-key attributes nested-name-specifier identifier base-clause [opt]
15120      class-key attributes nested-name-specifier [opt] template-id
15121        base-clause [opt]
15122
15123    Upon return BASES is initialized to the list of base classes (or
15124    NULL, if there are none) in the same form returned by
15125    cp_parser_base_clause.
15126
15127    Returns the TYPE of the indicated class.  Sets
15128    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15129    involving a nested-name-specifier was used, and FALSE otherwise.
15130
15131    Returns error_mark_node if this is not a class-head.
15132
15133    Returns NULL_TREE if the class-head is syntactically valid, but
15134    semantically invalid in a way that means we should skip the entire
15135    body of the class.  */
15136
15137 static tree
15138 cp_parser_class_head (cp_parser* parser,
15139                       bool* nested_name_specifier_p,
15140                       tree *attributes_p,
15141                       tree *bases)
15142 {
15143   tree nested_name_specifier;
15144   enum tag_types class_key;
15145   tree id = NULL_TREE;
15146   tree type = NULL_TREE;
15147   tree attributes;
15148   bool template_id_p = false;
15149   bool qualified_p = false;
15150   bool invalid_nested_name_p = false;
15151   bool invalid_explicit_specialization_p = false;
15152   tree pushed_scope = NULL_TREE;
15153   unsigned num_templates;
15154   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15155   /* Assume no nested-name-specifier will be present.  */
15156   *nested_name_specifier_p = false;
15157   /* Assume no template parameter lists will be used in defining the
15158      type.  */
15159   num_templates = 0;
15160
15161   *bases = NULL_TREE;
15162
15163   /* Look for the class-key.  */
15164   class_key = cp_parser_class_key (parser);
15165   if (class_key == none_type)
15166     return error_mark_node;
15167
15168   /* Parse the attributes.  */
15169   attributes = cp_parser_attributes_opt (parser);
15170
15171   /* If the next token is `::', that is invalid -- but sometimes
15172      people do try to write:
15173
15174        struct ::S {};
15175
15176      Handle this gracefully by accepting the extra qualifier, and then
15177      issuing an error about it later if this really is a
15178      class-head.  If it turns out just to be an elaborated type
15179      specifier, remain silent.  */
15180   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15181     qualified_p = true;
15182
15183   push_deferring_access_checks (dk_no_check);
15184
15185   /* Determine the name of the class.  Begin by looking for an
15186      optional nested-name-specifier.  */
15187   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15188   nested_name_specifier
15189     = cp_parser_nested_name_specifier_opt (parser,
15190                                            /*typename_keyword_p=*/false,
15191                                            /*check_dependency_p=*/false,
15192                                            /*type_p=*/false,
15193                                            /*is_declaration=*/false);
15194   /* If there was a nested-name-specifier, then there *must* be an
15195      identifier.  */
15196   if (nested_name_specifier)
15197     {
15198       type_start_token = cp_lexer_peek_token (parser->lexer);
15199       /* Although the grammar says `identifier', it really means
15200          `class-name' or `template-name'.  You are only allowed to
15201          define a class that has already been declared with this
15202          syntax.
15203
15204          The proposed resolution for Core Issue 180 says that wherever
15205          you see `class T::X' you should treat `X' as a type-name.
15206
15207          It is OK to define an inaccessible class; for example:
15208
15209            class A { class B; };
15210            class A::B {};
15211
15212          We do not know if we will see a class-name, or a
15213          template-name.  We look for a class-name first, in case the
15214          class-name is a template-id; if we looked for the
15215          template-name first we would stop after the template-name.  */
15216       cp_parser_parse_tentatively (parser);
15217       type = cp_parser_class_name (parser,
15218                                    /*typename_keyword_p=*/false,
15219                                    /*template_keyword_p=*/false,
15220                                    class_type,
15221                                    /*check_dependency_p=*/false,
15222                                    /*class_head_p=*/true,
15223                                    /*is_declaration=*/false);
15224       /* If that didn't work, ignore the nested-name-specifier.  */
15225       if (!cp_parser_parse_definitely (parser))
15226         {
15227           invalid_nested_name_p = true;
15228           type_start_token = cp_lexer_peek_token (parser->lexer);
15229           id = cp_parser_identifier (parser);
15230           if (id == error_mark_node)
15231             id = NULL_TREE;
15232         }
15233       /* If we could not find a corresponding TYPE, treat this
15234          declaration like an unqualified declaration.  */
15235       if (type == error_mark_node)
15236         nested_name_specifier = NULL_TREE;
15237       /* Otherwise, count the number of templates used in TYPE and its
15238          containing scopes.  */
15239       else
15240         {
15241           tree scope;
15242
15243           for (scope = TREE_TYPE (type);
15244                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15245                scope = (TYPE_P (scope)
15246                         ? TYPE_CONTEXT (scope)
15247                         : DECL_CONTEXT (scope)))
15248             if (TYPE_P (scope)
15249                 && CLASS_TYPE_P (scope)
15250                 && CLASSTYPE_TEMPLATE_INFO (scope)
15251                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15252                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15253               ++num_templates;
15254         }
15255     }
15256   /* Otherwise, the identifier is optional.  */
15257   else
15258     {
15259       /* We don't know whether what comes next is a template-id,
15260          an identifier, or nothing at all.  */
15261       cp_parser_parse_tentatively (parser);
15262       /* Check for a template-id.  */
15263       type_start_token = cp_lexer_peek_token (parser->lexer);
15264       id = cp_parser_template_id (parser,
15265                                   /*template_keyword_p=*/false,
15266                                   /*check_dependency_p=*/true,
15267                                   /*is_declaration=*/true);
15268       /* If that didn't work, it could still be an identifier.  */
15269       if (!cp_parser_parse_definitely (parser))
15270         {
15271           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15272             {
15273               type_start_token = cp_lexer_peek_token (parser->lexer);
15274               id = cp_parser_identifier (parser);
15275             }
15276           else
15277             id = NULL_TREE;
15278         }
15279       else
15280         {
15281           template_id_p = true;
15282           ++num_templates;
15283         }
15284     }
15285
15286   pop_deferring_access_checks ();
15287
15288   if (id)
15289     cp_parser_check_for_invalid_template_id (parser, id,
15290                                              type_start_token->location);
15291
15292   /* If it's not a `:' or a `{' then we can't really be looking at a
15293      class-head, since a class-head only appears as part of a
15294      class-specifier.  We have to detect this situation before calling
15295      xref_tag, since that has irreversible side-effects.  */
15296   if (!cp_parser_next_token_starts_class_definition_p (parser))
15297     {
15298       cp_parser_error (parser, "expected %<{%> or %<:%>");
15299       return error_mark_node;
15300     }
15301
15302   /* At this point, we're going ahead with the class-specifier, even
15303      if some other problem occurs.  */
15304   cp_parser_commit_to_tentative_parse (parser);
15305   /* Issue the error about the overly-qualified name now.  */
15306   if (qualified_p)
15307     {
15308       cp_parser_error (parser,
15309                        "global qualification of class name is invalid");
15310       return error_mark_node;
15311     }
15312   else if (invalid_nested_name_p)
15313     {
15314       cp_parser_error (parser,
15315                        "qualified name does not name a class");
15316       return error_mark_node;
15317     }
15318   else if (nested_name_specifier)
15319     {
15320       tree scope;
15321
15322       /* Reject typedef-names in class heads.  */
15323       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15324         {
15325           error ("%Hinvalid class name in declaration of %qD",
15326                  &type_start_token->location, type);
15327           type = NULL_TREE;
15328           goto done;
15329         }
15330
15331       /* Figure out in what scope the declaration is being placed.  */
15332       scope = current_scope ();
15333       /* If that scope does not contain the scope in which the
15334          class was originally declared, the program is invalid.  */
15335       if (scope && !is_ancestor (scope, nested_name_specifier))
15336         {
15337           if (at_namespace_scope_p ())
15338             error ("%Hdeclaration of %qD in namespace %qD which does not "
15339                    "enclose %qD",
15340                    &type_start_token->location,
15341                    type, scope, nested_name_specifier);
15342           else
15343             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15344                    &type_start_token->location,
15345                    type, scope, nested_name_specifier);
15346           type = NULL_TREE;
15347           goto done;
15348         }
15349       /* [dcl.meaning]
15350
15351          A declarator-id shall not be qualified except for the
15352          definition of a ... nested class outside of its class
15353          ... [or] the definition or explicit instantiation of a
15354          class member of a namespace outside of its namespace.  */
15355       if (scope == nested_name_specifier)
15356         {
15357           permerror (input_location, "%Hextra qualification not allowed",
15358                      &nested_name_specifier_token_start->location);
15359           nested_name_specifier = NULL_TREE;
15360           num_templates = 0;
15361         }
15362     }
15363   /* An explicit-specialization must be preceded by "template <>".  If
15364      it is not, try to recover gracefully.  */
15365   if (at_namespace_scope_p ()
15366       && parser->num_template_parameter_lists == 0
15367       && template_id_p)
15368     {
15369       error ("%Han explicit specialization must be preceded by %<template <>%>",
15370              &type_start_token->location);
15371       invalid_explicit_specialization_p = true;
15372       /* Take the same action that would have been taken by
15373          cp_parser_explicit_specialization.  */
15374       ++parser->num_template_parameter_lists;
15375       begin_specialization ();
15376     }
15377   /* There must be no "return" statements between this point and the
15378      end of this function; set "type "to the correct return value and
15379      use "goto done;" to return.  */
15380   /* Make sure that the right number of template parameters were
15381      present.  */
15382   if (!cp_parser_check_template_parameters (parser, num_templates,
15383                                             type_start_token->location))
15384     {
15385       /* If something went wrong, there is no point in even trying to
15386          process the class-definition.  */
15387       type = NULL_TREE;
15388       goto done;
15389     }
15390
15391   /* Look up the type.  */
15392   if (template_id_p)
15393     {
15394       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15395           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15396               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15397         {
15398           error ("%Hfunction template %qD redeclared as a class template",
15399                  &type_start_token->location, id);
15400           type = error_mark_node;
15401         }
15402       else
15403         {
15404           type = TREE_TYPE (id);
15405           type = maybe_process_partial_specialization (type);
15406         }
15407       if (nested_name_specifier)
15408         pushed_scope = push_scope (nested_name_specifier);
15409     }
15410   else if (nested_name_specifier)
15411     {
15412       tree class_type;
15413
15414       /* Given:
15415
15416             template <typename T> struct S { struct T };
15417             template <typename T> struct S<T>::T { };
15418
15419          we will get a TYPENAME_TYPE when processing the definition of
15420          `S::T'.  We need to resolve it to the actual type before we
15421          try to define it.  */
15422       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15423         {
15424           class_type = resolve_typename_type (TREE_TYPE (type),
15425                                               /*only_current_p=*/false);
15426           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15427             type = TYPE_NAME (class_type);
15428           else
15429             {
15430               cp_parser_error (parser, "could not resolve typename type");
15431               type = error_mark_node;
15432             }
15433         }
15434
15435       if (maybe_process_partial_specialization (TREE_TYPE (type))
15436           == error_mark_node)
15437         {
15438           type = NULL_TREE;
15439           goto done;
15440         }
15441
15442       class_type = current_class_type;
15443       /* Enter the scope indicated by the nested-name-specifier.  */
15444       pushed_scope = push_scope (nested_name_specifier);
15445       /* Get the canonical version of this type.  */
15446       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15447       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15448           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15449         {
15450           type = push_template_decl (type);
15451           if (type == error_mark_node)
15452             {
15453               type = NULL_TREE;
15454               goto done;
15455             }
15456         }
15457
15458       type = TREE_TYPE (type);
15459       *nested_name_specifier_p = true;
15460     }
15461   else      /* The name is not a nested name.  */
15462     {
15463       /* If the class was unnamed, create a dummy name.  */
15464       if (!id)
15465         id = make_anon_name ();
15466       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15467                        parser->num_template_parameter_lists);
15468     }
15469
15470   /* Indicate whether this class was declared as a `class' or as a
15471      `struct'.  */
15472   if (TREE_CODE (type) == RECORD_TYPE)
15473     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15474   cp_parser_check_class_key (class_key, type);
15475
15476   /* If this type was already complete, and we see another definition,
15477      that's an error.  */
15478   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15479     {
15480       error ("%Hredefinition of %q#T",
15481              &type_start_token->location, type);
15482       error ("%Hprevious definition of %q+#T",
15483              &type_start_token->location, type);
15484       type = NULL_TREE;
15485       goto done;
15486     }
15487   else if (type == error_mark_node)
15488     type = NULL_TREE;
15489
15490   /* We will have entered the scope containing the class; the names of
15491      base classes should be looked up in that context.  For example:
15492
15493        struct A { struct B {}; struct C; };
15494        struct A::C : B {};
15495
15496      is valid.  */
15497
15498   /* Get the list of base-classes, if there is one.  */
15499   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15500     *bases = cp_parser_base_clause (parser);
15501
15502  done:
15503   /* Leave the scope given by the nested-name-specifier.  We will
15504      enter the class scope itself while processing the members.  */
15505   if (pushed_scope)
15506     pop_scope (pushed_scope);
15507
15508   if (invalid_explicit_specialization_p)
15509     {
15510       end_specialization ();
15511       --parser->num_template_parameter_lists;
15512     }
15513   *attributes_p = attributes;
15514   return type;
15515 }
15516
15517 /* Parse a class-key.
15518
15519    class-key:
15520      class
15521      struct
15522      union
15523
15524    Returns the kind of class-key specified, or none_type to indicate
15525    error.  */
15526
15527 static enum tag_types
15528 cp_parser_class_key (cp_parser* parser)
15529 {
15530   cp_token *token;
15531   enum tag_types tag_type;
15532
15533   /* Look for the class-key.  */
15534   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15535   if (!token)
15536     return none_type;
15537
15538   /* Check to see if the TOKEN is a class-key.  */
15539   tag_type = cp_parser_token_is_class_key (token);
15540   if (!tag_type)
15541     cp_parser_error (parser, "expected class-key");
15542   return tag_type;
15543 }
15544
15545 /* Parse an (optional) member-specification.
15546
15547    member-specification:
15548      member-declaration member-specification [opt]
15549      access-specifier : member-specification [opt]  */
15550
15551 static void
15552 cp_parser_member_specification_opt (cp_parser* parser)
15553 {
15554   while (true)
15555     {
15556       cp_token *token;
15557       enum rid keyword;
15558
15559       /* Peek at the next token.  */
15560       token = cp_lexer_peek_token (parser->lexer);
15561       /* If it's a `}', or EOF then we've seen all the members.  */
15562       if (token->type == CPP_CLOSE_BRACE
15563           || token->type == CPP_EOF
15564           || token->type == CPP_PRAGMA_EOL)
15565         break;
15566
15567       /* See if this token is a keyword.  */
15568       keyword = token->keyword;
15569       switch (keyword)
15570         {
15571         case RID_PUBLIC:
15572         case RID_PROTECTED:
15573         case RID_PRIVATE:
15574           /* Consume the access-specifier.  */
15575           cp_lexer_consume_token (parser->lexer);
15576           /* Remember which access-specifier is active.  */
15577           current_access_specifier = token->u.value;
15578           /* Look for the `:'.  */
15579           cp_parser_require (parser, CPP_COLON, "%<:%>");
15580           break;
15581
15582         default:
15583           /* Accept #pragmas at class scope.  */
15584           if (token->type == CPP_PRAGMA)
15585             {
15586               cp_parser_pragma (parser, pragma_external);
15587               break;
15588             }
15589
15590           /* Otherwise, the next construction must be a
15591              member-declaration.  */
15592           cp_parser_member_declaration (parser);
15593         }
15594     }
15595 }
15596
15597 /* Parse a member-declaration.
15598
15599    member-declaration:
15600      decl-specifier-seq [opt] member-declarator-list [opt] ;
15601      function-definition ; [opt]
15602      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15603      using-declaration
15604      template-declaration
15605
15606    member-declarator-list:
15607      member-declarator
15608      member-declarator-list , member-declarator
15609
15610    member-declarator:
15611      declarator pure-specifier [opt]
15612      declarator constant-initializer [opt]
15613      identifier [opt] : constant-expression
15614
15615    GNU Extensions:
15616
15617    member-declaration:
15618      __extension__ member-declaration
15619
15620    member-declarator:
15621      declarator attributes [opt] pure-specifier [opt]
15622      declarator attributes [opt] constant-initializer [opt]
15623      identifier [opt] attributes [opt] : constant-expression  
15624
15625    C++0x Extensions:
15626
15627    member-declaration:
15628      static_assert-declaration  */
15629
15630 static void
15631 cp_parser_member_declaration (cp_parser* parser)
15632 {
15633   cp_decl_specifier_seq decl_specifiers;
15634   tree prefix_attributes;
15635   tree decl;
15636   int declares_class_or_enum;
15637   bool friend_p;
15638   cp_token *token = NULL;
15639   cp_token *decl_spec_token_start = NULL;
15640   cp_token *initializer_token_start = NULL;
15641   int saved_pedantic;
15642
15643   /* Check for the `__extension__' keyword.  */
15644   if (cp_parser_extension_opt (parser, &saved_pedantic))
15645     {
15646       /* Recurse.  */
15647       cp_parser_member_declaration (parser);
15648       /* Restore the old value of the PEDANTIC flag.  */
15649       pedantic = saved_pedantic;
15650
15651       return;
15652     }
15653
15654   /* Check for a template-declaration.  */
15655   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15656     {
15657       /* An explicit specialization here is an error condition, and we
15658          expect the specialization handler to detect and report this.  */
15659       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15660           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15661         cp_parser_explicit_specialization (parser);
15662       else
15663         cp_parser_template_declaration (parser, /*member_p=*/true);
15664
15665       return;
15666     }
15667
15668   /* Check for a using-declaration.  */
15669   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15670     {
15671       /* Parse the using-declaration.  */
15672       cp_parser_using_declaration (parser,
15673                                    /*access_declaration_p=*/false);
15674       return;
15675     }
15676
15677   /* Check for @defs.  */
15678   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15679     {
15680       tree ivar, member;
15681       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15682       ivar = ivar_chains;
15683       while (ivar)
15684         {
15685           member = ivar;
15686           ivar = TREE_CHAIN (member);
15687           TREE_CHAIN (member) = NULL_TREE;
15688           finish_member_declaration (member);
15689         }
15690       return;
15691     }
15692
15693   /* If the next token is `static_assert' we have a static assertion.  */
15694   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15695     {
15696       cp_parser_static_assert (parser, /*member_p=*/true);
15697       return;
15698     }
15699
15700   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15701     return;
15702
15703   /* Parse the decl-specifier-seq.  */
15704   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15705   cp_parser_decl_specifier_seq (parser,
15706                                 CP_PARSER_FLAGS_OPTIONAL,
15707                                 &decl_specifiers,
15708                                 &declares_class_or_enum);
15709   prefix_attributes = decl_specifiers.attributes;
15710   decl_specifiers.attributes = NULL_TREE;
15711   /* Check for an invalid type-name.  */
15712   if (!decl_specifiers.type
15713       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15714     return;
15715   /* If there is no declarator, then the decl-specifier-seq should
15716      specify a type.  */
15717   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15718     {
15719       /* If there was no decl-specifier-seq, and the next token is a
15720          `;', then we have something like:
15721
15722            struct S { ; };
15723
15724          [class.mem]
15725
15726          Each member-declaration shall declare at least one member
15727          name of the class.  */
15728       if (!decl_specifiers.any_specifiers_p)
15729         {
15730           cp_token *token = cp_lexer_peek_token (parser->lexer);
15731           if (!in_system_header_at (token->location))
15732             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15733         }
15734       else
15735         {
15736           tree type;
15737
15738           /* See if this declaration is a friend.  */
15739           friend_p = cp_parser_friend_p (&decl_specifiers);
15740           /* If there were decl-specifiers, check to see if there was
15741              a class-declaration.  */
15742           type = check_tag_decl (&decl_specifiers);
15743           /* Nested classes have already been added to the class, but
15744              a `friend' needs to be explicitly registered.  */
15745           if (friend_p)
15746             {
15747               /* If the `friend' keyword was present, the friend must
15748                  be introduced with a class-key.  */
15749                if (!declares_class_or_enum)
15750                  error ("%Ha class-key must be used when declaring a friend",
15751                         &decl_spec_token_start->location);
15752                /* In this case:
15753
15754                     template <typename T> struct A {
15755                       friend struct A<T>::B;
15756                     };
15757
15758                   A<T>::B will be represented by a TYPENAME_TYPE, and
15759                   therefore not recognized by check_tag_decl.  */
15760                if (!type
15761                    && decl_specifiers.type
15762                    && TYPE_P (decl_specifiers.type))
15763                  type = decl_specifiers.type;
15764                if (!type || !TYPE_P (type))
15765                  error ("%Hfriend declaration does not name a class or "
15766                         "function", &decl_spec_token_start->location);
15767                else
15768                  make_friend_class (current_class_type, type,
15769                                     /*complain=*/true);
15770             }
15771           /* If there is no TYPE, an error message will already have
15772              been issued.  */
15773           else if (!type || type == error_mark_node)
15774             ;
15775           /* An anonymous aggregate has to be handled specially; such
15776              a declaration really declares a data member (with a
15777              particular type), as opposed to a nested class.  */
15778           else if (ANON_AGGR_TYPE_P (type))
15779             {
15780               /* Remove constructors and such from TYPE, now that we
15781                  know it is an anonymous aggregate.  */
15782               fixup_anonymous_aggr (type);
15783               /* And make the corresponding data member.  */
15784               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15785               /* Add it to the class.  */
15786               finish_member_declaration (decl);
15787             }
15788           else
15789             cp_parser_check_access_in_redeclaration
15790                                               (TYPE_NAME (type),
15791                                                decl_spec_token_start->location);
15792         }
15793     }
15794   else
15795     {
15796       /* See if these declarations will be friends.  */
15797       friend_p = cp_parser_friend_p (&decl_specifiers);
15798
15799       /* Keep going until we hit the `;' at the end of the
15800          declaration.  */
15801       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15802         {
15803           tree attributes = NULL_TREE;
15804           tree first_attribute;
15805
15806           /* Peek at the next token.  */
15807           token = cp_lexer_peek_token (parser->lexer);
15808
15809           /* Check for a bitfield declaration.  */
15810           if (token->type == CPP_COLON
15811               || (token->type == CPP_NAME
15812                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15813                   == CPP_COLON))
15814             {
15815               tree identifier;
15816               tree width;
15817
15818               /* Get the name of the bitfield.  Note that we cannot just
15819                  check TOKEN here because it may have been invalidated by
15820                  the call to cp_lexer_peek_nth_token above.  */
15821               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15822                 identifier = cp_parser_identifier (parser);
15823               else
15824                 identifier = NULL_TREE;
15825
15826               /* Consume the `:' token.  */
15827               cp_lexer_consume_token (parser->lexer);
15828               /* Get the width of the bitfield.  */
15829               width
15830                 = cp_parser_constant_expression (parser,
15831                                                  /*allow_non_constant=*/false,
15832                                                  NULL);
15833
15834               /* Look for attributes that apply to the bitfield.  */
15835               attributes = cp_parser_attributes_opt (parser);
15836               /* Remember which attributes are prefix attributes and
15837                  which are not.  */
15838               first_attribute = attributes;
15839               /* Combine the attributes.  */
15840               attributes = chainon (prefix_attributes, attributes);
15841
15842               /* Create the bitfield declaration.  */
15843               decl = grokbitfield (identifier
15844                                    ? make_id_declarator (NULL_TREE,
15845                                                          identifier,
15846                                                          sfk_none)
15847                                    : NULL,
15848                                    &decl_specifiers,
15849                                    width,
15850                                    attributes);
15851             }
15852           else
15853             {
15854               cp_declarator *declarator;
15855               tree initializer;
15856               tree asm_specification;
15857               int ctor_dtor_or_conv_p;
15858
15859               /* Parse the declarator.  */
15860               declarator
15861                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15862                                         &ctor_dtor_or_conv_p,
15863                                         /*parenthesized_p=*/NULL,
15864                                         /*member_p=*/true);
15865
15866               /* If something went wrong parsing the declarator, make sure
15867                  that we at least consume some tokens.  */
15868               if (declarator == cp_error_declarator)
15869                 {
15870                   /* Skip to the end of the statement.  */
15871                   cp_parser_skip_to_end_of_statement (parser);
15872                   /* If the next token is not a semicolon, that is
15873                      probably because we just skipped over the body of
15874                      a function.  So, we consume a semicolon if
15875                      present, but do not issue an error message if it
15876                      is not present.  */
15877                   if (cp_lexer_next_token_is (parser->lexer,
15878                                               CPP_SEMICOLON))
15879                     cp_lexer_consume_token (parser->lexer);
15880                   return;
15881                 }
15882
15883               if (declares_class_or_enum & 2)
15884                 cp_parser_check_for_definition_in_return_type
15885                                             (declarator, decl_specifiers.type,
15886                                              decl_specifiers.type_location);
15887
15888               /* Look for an asm-specification.  */
15889               asm_specification = cp_parser_asm_specification_opt (parser);
15890               /* Look for attributes that apply to the declaration.  */
15891               attributes = cp_parser_attributes_opt (parser);
15892               /* Remember which attributes are prefix attributes and
15893                  which are not.  */
15894               first_attribute = attributes;
15895               /* Combine the attributes.  */
15896               attributes = chainon (prefix_attributes, attributes);
15897
15898               /* If it's an `=', then we have a constant-initializer or a
15899                  pure-specifier.  It is not correct to parse the
15900                  initializer before registering the member declaration
15901                  since the member declaration should be in scope while
15902                  its initializer is processed.  However, the rest of the
15903                  front end does not yet provide an interface that allows
15904                  us to handle this correctly.  */
15905               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15906                 {
15907                   /* In [class.mem]:
15908
15909                      A pure-specifier shall be used only in the declaration of
15910                      a virtual function.
15911
15912                      A member-declarator can contain a constant-initializer
15913                      only if it declares a static member of integral or
15914                      enumeration type.
15915
15916                      Therefore, if the DECLARATOR is for a function, we look
15917                      for a pure-specifier; otherwise, we look for a
15918                      constant-initializer.  When we call `grokfield', it will
15919                      perform more stringent semantics checks.  */
15920                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15921                   if (function_declarator_p (declarator))
15922                     initializer = cp_parser_pure_specifier (parser);
15923                   else
15924                     /* Parse the initializer.  */
15925                     initializer = cp_parser_constant_initializer (parser);
15926                 }
15927               /* Otherwise, there is no initializer.  */
15928               else
15929                 initializer = NULL_TREE;
15930
15931               /* See if we are probably looking at a function
15932                  definition.  We are certainly not looking at a
15933                  member-declarator.  Calling `grokfield' has
15934                  side-effects, so we must not do it unless we are sure
15935                  that we are looking at a member-declarator.  */
15936               if (cp_parser_token_starts_function_definition_p
15937                   (cp_lexer_peek_token (parser->lexer)))
15938                 {
15939                   /* The grammar does not allow a pure-specifier to be
15940                      used when a member function is defined.  (It is
15941                      possible that this fact is an oversight in the
15942                      standard, since a pure function may be defined
15943                      outside of the class-specifier.  */
15944                   if (initializer)
15945                     error ("%Hpure-specifier on function-definition",
15946                            &initializer_token_start->location);
15947                   decl = cp_parser_save_member_function_body (parser,
15948                                                               &decl_specifiers,
15949                                                               declarator,
15950                                                               attributes);
15951                   /* If the member was not a friend, declare it here.  */
15952                   if (!friend_p)
15953                     finish_member_declaration (decl);
15954                   /* Peek at the next token.  */
15955                   token = cp_lexer_peek_token (parser->lexer);
15956                   /* If the next token is a semicolon, consume it.  */
15957                   if (token->type == CPP_SEMICOLON)
15958                     cp_lexer_consume_token (parser->lexer);
15959                   return;
15960                 }
15961               else
15962                 if (declarator->kind == cdk_function)
15963                   declarator->id_loc = token->location;
15964                 /* Create the declaration.  */
15965                 decl = grokfield (declarator, &decl_specifiers,
15966                                   initializer, /*init_const_expr_p=*/true,
15967                                   asm_specification,
15968                                   attributes);
15969             }
15970
15971           /* Reset PREFIX_ATTRIBUTES.  */
15972           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15973             attributes = TREE_CHAIN (attributes);
15974           if (attributes)
15975             TREE_CHAIN (attributes) = NULL_TREE;
15976
15977           /* If there is any qualification still in effect, clear it
15978              now; we will be starting fresh with the next declarator.  */
15979           parser->scope = NULL_TREE;
15980           parser->qualifying_scope = NULL_TREE;
15981           parser->object_scope = NULL_TREE;
15982           /* If it's a `,', then there are more declarators.  */
15983           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15984             cp_lexer_consume_token (parser->lexer);
15985           /* If the next token isn't a `;', then we have a parse error.  */
15986           else if (cp_lexer_next_token_is_not (parser->lexer,
15987                                                CPP_SEMICOLON))
15988             {
15989               cp_parser_error (parser, "expected %<;%>");
15990               /* Skip tokens until we find a `;'.  */
15991               cp_parser_skip_to_end_of_statement (parser);
15992
15993               break;
15994             }
15995
15996           if (decl)
15997             {
15998               /* Add DECL to the list of members.  */
15999               if (!friend_p)
16000                 finish_member_declaration (decl);
16001
16002               if (TREE_CODE (decl) == FUNCTION_DECL)
16003                 cp_parser_save_default_args (parser, decl);
16004             }
16005         }
16006     }
16007
16008   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16009 }
16010
16011 /* Parse a pure-specifier.
16012
16013    pure-specifier:
16014      = 0
16015
16016    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16017    Otherwise, ERROR_MARK_NODE is returned.  */
16018
16019 static tree
16020 cp_parser_pure_specifier (cp_parser* parser)
16021 {
16022   cp_token *token;
16023
16024   /* Look for the `=' token.  */
16025   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16026     return error_mark_node;
16027   /* Look for the `0' token.  */
16028   token = cp_lexer_peek_token (parser->lexer);
16029
16030   if (token->type == CPP_EOF
16031       || token->type == CPP_PRAGMA_EOL)
16032     return error_mark_node;
16033
16034   cp_lexer_consume_token (parser->lexer);
16035
16036   /* Accept = default or = delete in c++0x mode.  */
16037   if (token->keyword == RID_DEFAULT
16038       || token->keyword == RID_DELETE)
16039     {
16040       maybe_warn_cpp0x ("defaulted and deleted functions");
16041       return token->u.value;
16042     }
16043
16044   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16045   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16046     {
16047       cp_parser_error (parser,
16048                        "invalid pure specifier (only %<= 0%> is allowed)");
16049       cp_parser_skip_to_end_of_statement (parser);
16050       return error_mark_node;
16051     }
16052   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16053     {
16054       error ("%Htemplates may not be %<virtual%>", &token->location);
16055       return error_mark_node;
16056     }
16057
16058   return integer_zero_node;
16059 }
16060
16061 /* Parse a constant-initializer.
16062
16063    constant-initializer:
16064      = constant-expression
16065
16066    Returns a representation of the constant-expression.  */
16067
16068 static tree
16069 cp_parser_constant_initializer (cp_parser* parser)
16070 {
16071   /* Look for the `=' token.  */
16072   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16073     return error_mark_node;
16074
16075   /* It is invalid to write:
16076
16077        struct S { static const int i = { 7 }; };
16078
16079      */
16080   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16081     {
16082       cp_parser_error (parser,
16083                        "a brace-enclosed initializer is not allowed here");
16084       /* Consume the opening brace.  */
16085       cp_lexer_consume_token (parser->lexer);
16086       /* Skip the initializer.  */
16087       cp_parser_skip_to_closing_brace (parser);
16088       /* Look for the trailing `}'.  */
16089       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16090
16091       return error_mark_node;
16092     }
16093
16094   return cp_parser_constant_expression (parser,
16095                                         /*allow_non_constant=*/false,
16096                                         NULL);
16097 }
16098
16099 /* Derived classes [gram.class.derived] */
16100
16101 /* Parse a base-clause.
16102
16103    base-clause:
16104      : base-specifier-list
16105
16106    base-specifier-list:
16107      base-specifier ... [opt]
16108      base-specifier-list , base-specifier ... [opt]
16109
16110    Returns a TREE_LIST representing the base-classes, in the order in
16111    which they were declared.  The representation of each node is as
16112    described by cp_parser_base_specifier.
16113
16114    In the case that no bases are specified, this function will return
16115    NULL_TREE, not ERROR_MARK_NODE.  */
16116
16117 static tree
16118 cp_parser_base_clause (cp_parser* parser)
16119 {
16120   tree bases = NULL_TREE;
16121
16122   /* Look for the `:' that begins the list.  */
16123   cp_parser_require (parser, CPP_COLON, "%<:%>");
16124
16125   /* Scan the base-specifier-list.  */
16126   while (true)
16127     {
16128       cp_token *token;
16129       tree base;
16130       bool pack_expansion_p = false;
16131
16132       /* Look for the base-specifier.  */
16133       base = cp_parser_base_specifier (parser);
16134       /* Look for the (optional) ellipsis. */
16135       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16136         {
16137           /* Consume the `...'. */
16138           cp_lexer_consume_token (parser->lexer);
16139
16140           pack_expansion_p = true;
16141         }
16142
16143       /* Add BASE to the front of the list.  */
16144       if (base != error_mark_node)
16145         {
16146           if (pack_expansion_p)
16147             /* Make this a pack expansion type. */
16148             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16149           
16150
16151           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16152             {
16153               TREE_CHAIN (base) = bases;
16154               bases = base;
16155             }
16156         }
16157       /* Peek at the next token.  */
16158       token = cp_lexer_peek_token (parser->lexer);
16159       /* If it's not a comma, then the list is complete.  */
16160       if (token->type != CPP_COMMA)
16161         break;
16162       /* Consume the `,'.  */
16163       cp_lexer_consume_token (parser->lexer);
16164     }
16165
16166   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16167      base class had a qualified name.  However, the next name that
16168      appears is certainly not qualified.  */
16169   parser->scope = NULL_TREE;
16170   parser->qualifying_scope = NULL_TREE;
16171   parser->object_scope = NULL_TREE;
16172
16173   return nreverse (bases);
16174 }
16175
16176 /* Parse a base-specifier.
16177
16178    base-specifier:
16179      :: [opt] nested-name-specifier [opt] class-name
16180      virtual access-specifier [opt] :: [opt] nested-name-specifier
16181        [opt] class-name
16182      access-specifier virtual [opt] :: [opt] nested-name-specifier
16183        [opt] class-name
16184
16185    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16186    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16187    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16188    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16189
16190 static tree
16191 cp_parser_base_specifier (cp_parser* parser)
16192 {
16193   cp_token *token;
16194   bool done = false;
16195   bool virtual_p = false;
16196   bool duplicate_virtual_error_issued_p = false;
16197   bool duplicate_access_error_issued_p = false;
16198   bool class_scope_p, template_p;
16199   tree access = access_default_node;
16200   tree type;
16201
16202   /* Process the optional `virtual' and `access-specifier'.  */
16203   while (!done)
16204     {
16205       /* Peek at the next token.  */
16206       token = cp_lexer_peek_token (parser->lexer);
16207       /* Process `virtual'.  */
16208       switch (token->keyword)
16209         {
16210         case RID_VIRTUAL:
16211           /* If `virtual' appears more than once, issue an error.  */
16212           if (virtual_p && !duplicate_virtual_error_issued_p)
16213             {
16214               cp_parser_error (parser,
16215                                "%<virtual%> specified more than once in base-specified");
16216               duplicate_virtual_error_issued_p = true;
16217             }
16218
16219           virtual_p = true;
16220
16221           /* Consume the `virtual' token.  */
16222           cp_lexer_consume_token (parser->lexer);
16223
16224           break;
16225
16226         case RID_PUBLIC:
16227         case RID_PROTECTED:
16228         case RID_PRIVATE:
16229           /* If more than one access specifier appears, issue an
16230              error.  */
16231           if (access != access_default_node
16232               && !duplicate_access_error_issued_p)
16233             {
16234               cp_parser_error (parser,
16235                                "more than one access specifier in base-specified");
16236               duplicate_access_error_issued_p = true;
16237             }
16238
16239           access = ridpointers[(int) token->keyword];
16240
16241           /* Consume the access-specifier.  */
16242           cp_lexer_consume_token (parser->lexer);
16243
16244           break;
16245
16246         default:
16247           done = true;
16248           break;
16249         }
16250     }
16251   /* It is not uncommon to see programs mechanically, erroneously, use
16252      the 'typename' keyword to denote (dependent) qualified types
16253      as base classes.  */
16254   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16255     {
16256       token = cp_lexer_peek_token (parser->lexer);
16257       if (!processing_template_decl)
16258         error ("%Hkeyword %<typename%> not allowed outside of templates",
16259                &token->location);
16260       else
16261         error ("%Hkeyword %<typename%> not allowed in this context "
16262                "(the base class is implicitly a type)",
16263                &token->location);
16264       cp_lexer_consume_token (parser->lexer);
16265     }
16266
16267   /* Look for the optional `::' operator.  */
16268   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16269   /* Look for the nested-name-specifier.  The simplest way to
16270      implement:
16271
16272        [temp.res]
16273
16274        The keyword `typename' is not permitted in a base-specifier or
16275        mem-initializer; in these contexts a qualified name that
16276        depends on a template-parameter is implicitly assumed to be a
16277        type name.
16278
16279      is to pretend that we have seen the `typename' keyword at this
16280      point.  */
16281   cp_parser_nested_name_specifier_opt (parser,
16282                                        /*typename_keyword_p=*/true,
16283                                        /*check_dependency_p=*/true,
16284                                        typename_type,
16285                                        /*is_declaration=*/true);
16286   /* If the base class is given by a qualified name, assume that names
16287      we see are type names or templates, as appropriate.  */
16288   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16289   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16290
16291   /* Finally, look for the class-name.  */
16292   type = cp_parser_class_name (parser,
16293                                class_scope_p,
16294                                template_p,
16295                                typename_type,
16296                                /*check_dependency_p=*/true,
16297                                /*class_head_p=*/false,
16298                                /*is_declaration=*/true);
16299
16300   if (type == error_mark_node)
16301     return error_mark_node;
16302
16303   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16304 }
16305
16306 /* Exception handling [gram.exception] */
16307
16308 /* Parse an (optional) exception-specification.
16309
16310    exception-specification:
16311      throw ( type-id-list [opt] )
16312
16313    Returns a TREE_LIST representing the exception-specification.  The
16314    TREE_VALUE of each node is a type.  */
16315
16316 static tree
16317 cp_parser_exception_specification_opt (cp_parser* parser)
16318 {
16319   cp_token *token;
16320   tree type_id_list;
16321
16322   /* Peek at the next token.  */
16323   token = cp_lexer_peek_token (parser->lexer);
16324   /* If it's not `throw', then there's no exception-specification.  */
16325   if (!cp_parser_is_keyword (token, RID_THROW))
16326     return NULL_TREE;
16327
16328   /* Consume the `throw'.  */
16329   cp_lexer_consume_token (parser->lexer);
16330
16331   /* Look for the `('.  */
16332   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16333
16334   /* Peek at the next token.  */
16335   token = cp_lexer_peek_token (parser->lexer);
16336   /* If it's not a `)', then there is a type-id-list.  */
16337   if (token->type != CPP_CLOSE_PAREN)
16338     {
16339       const char *saved_message;
16340
16341       /* Types may not be defined in an exception-specification.  */
16342       saved_message = parser->type_definition_forbidden_message;
16343       parser->type_definition_forbidden_message
16344         = "types may not be defined in an exception-specification";
16345       /* Parse the type-id-list.  */
16346       type_id_list = cp_parser_type_id_list (parser);
16347       /* Restore the saved message.  */
16348       parser->type_definition_forbidden_message = saved_message;
16349     }
16350   else
16351     type_id_list = empty_except_spec;
16352
16353   /* Look for the `)'.  */
16354   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16355
16356   return type_id_list;
16357 }
16358
16359 /* Parse an (optional) type-id-list.
16360
16361    type-id-list:
16362      type-id ... [opt]
16363      type-id-list , type-id ... [opt]
16364
16365    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16366    in the order that the types were presented.  */
16367
16368 static tree
16369 cp_parser_type_id_list (cp_parser* parser)
16370 {
16371   tree types = NULL_TREE;
16372
16373   while (true)
16374     {
16375       cp_token *token;
16376       tree type;
16377
16378       /* Get the next type-id.  */
16379       type = cp_parser_type_id (parser);
16380       /* Parse the optional ellipsis. */
16381       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16382         {
16383           /* Consume the `...'. */
16384           cp_lexer_consume_token (parser->lexer);
16385
16386           /* Turn the type into a pack expansion expression. */
16387           type = make_pack_expansion (type);
16388         }
16389       /* Add it to the list.  */
16390       types = add_exception_specifier (types, type, /*complain=*/1);
16391       /* Peek at the next token.  */
16392       token = cp_lexer_peek_token (parser->lexer);
16393       /* If it is not a `,', we are done.  */
16394       if (token->type != CPP_COMMA)
16395         break;
16396       /* Consume the `,'.  */
16397       cp_lexer_consume_token (parser->lexer);
16398     }
16399
16400   return nreverse (types);
16401 }
16402
16403 /* Parse a try-block.
16404
16405    try-block:
16406      try compound-statement handler-seq  */
16407
16408 static tree
16409 cp_parser_try_block (cp_parser* parser)
16410 {
16411   tree try_block;
16412
16413   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16414   try_block = begin_try_block ();
16415   cp_parser_compound_statement (parser, NULL, true);
16416   finish_try_block (try_block);
16417   cp_parser_handler_seq (parser);
16418   finish_handler_sequence (try_block);
16419
16420   return try_block;
16421 }
16422
16423 /* Parse a function-try-block.
16424
16425    function-try-block:
16426      try ctor-initializer [opt] function-body handler-seq  */
16427
16428 static bool
16429 cp_parser_function_try_block (cp_parser* parser)
16430 {
16431   tree compound_stmt;
16432   tree try_block;
16433   bool ctor_initializer_p;
16434
16435   /* Look for the `try' keyword.  */
16436   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16437     return false;
16438   /* Let the rest of the front end know where we are.  */
16439   try_block = begin_function_try_block (&compound_stmt);
16440   /* Parse the function-body.  */
16441   ctor_initializer_p
16442     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16443   /* We're done with the `try' part.  */
16444   finish_function_try_block (try_block);
16445   /* Parse the handlers.  */
16446   cp_parser_handler_seq (parser);
16447   /* We're done with the handlers.  */
16448   finish_function_handler_sequence (try_block, compound_stmt);
16449
16450   return ctor_initializer_p;
16451 }
16452
16453 /* Parse a handler-seq.
16454
16455    handler-seq:
16456      handler handler-seq [opt]  */
16457
16458 static void
16459 cp_parser_handler_seq (cp_parser* parser)
16460 {
16461   while (true)
16462     {
16463       cp_token *token;
16464
16465       /* Parse the handler.  */
16466       cp_parser_handler (parser);
16467       /* Peek at the next token.  */
16468       token = cp_lexer_peek_token (parser->lexer);
16469       /* If it's not `catch' then there are no more handlers.  */
16470       if (!cp_parser_is_keyword (token, RID_CATCH))
16471         break;
16472     }
16473 }
16474
16475 /* Parse a handler.
16476
16477    handler:
16478      catch ( exception-declaration ) compound-statement  */
16479
16480 static void
16481 cp_parser_handler (cp_parser* parser)
16482 {
16483   tree handler;
16484   tree declaration;
16485
16486   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16487   handler = begin_handler ();
16488   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16489   declaration = cp_parser_exception_declaration (parser);
16490   finish_handler_parms (declaration, handler);
16491   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16492   cp_parser_compound_statement (parser, NULL, false);
16493   finish_handler (handler);
16494 }
16495
16496 /* Parse an exception-declaration.
16497
16498    exception-declaration:
16499      type-specifier-seq declarator
16500      type-specifier-seq abstract-declarator
16501      type-specifier-seq
16502      ...
16503
16504    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16505    ellipsis variant is used.  */
16506
16507 static tree
16508 cp_parser_exception_declaration (cp_parser* parser)
16509 {
16510   cp_decl_specifier_seq type_specifiers;
16511   cp_declarator *declarator;
16512   const char *saved_message;
16513
16514   /* If it's an ellipsis, it's easy to handle.  */
16515   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16516     {
16517       /* Consume the `...' token.  */
16518       cp_lexer_consume_token (parser->lexer);
16519       return NULL_TREE;
16520     }
16521
16522   /* Types may not be defined in exception-declarations.  */
16523   saved_message = parser->type_definition_forbidden_message;
16524   parser->type_definition_forbidden_message
16525     = "types may not be defined in exception-declarations";
16526
16527   /* Parse the type-specifier-seq.  */
16528   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16529                                 &type_specifiers);
16530   /* If it's a `)', then there is no declarator.  */
16531   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16532     declarator = NULL;
16533   else
16534     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16535                                        /*ctor_dtor_or_conv_p=*/NULL,
16536                                        /*parenthesized_p=*/NULL,
16537                                        /*member_p=*/false);
16538
16539   /* Restore the saved message.  */
16540   parser->type_definition_forbidden_message = saved_message;
16541
16542   if (!type_specifiers.any_specifiers_p)
16543     return error_mark_node;
16544
16545   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16546 }
16547
16548 /* Parse a throw-expression.
16549
16550    throw-expression:
16551      throw assignment-expression [opt]
16552
16553    Returns a THROW_EXPR representing the throw-expression.  */
16554
16555 static tree
16556 cp_parser_throw_expression (cp_parser* parser)
16557 {
16558   tree expression;
16559   cp_token* token;
16560
16561   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16562   token = cp_lexer_peek_token (parser->lexer);
16563   /* Figure out whether or not there is an assignment-expression
16564      following the "throw" keyword.  */
16565   if (token->type == CPP_COMMA
16566       || token->type == CPP_SEMICOLON
16567       || token->type == CPP_CLOSE_PAREN
16568       || token->type == CPP_CLOSE_SQUARE
16569       || token->type == CPP_CLOSE_BRACE
16570       || token->type == CPP_COLON)
16571     expression = NULL_TREE;
16572   else
16573     expression = cp_parser_assignment_expression (parser,
16574                                                   /*cast_p=*/false, NULL);
16575
16576   return build_throw (expression);
16577 }
16578
16579 /* GNU Extensions */
16580
16581 /* Parse an (optional) asm-specification.
16582
16583    asm-specification:
16584      asm ( string-literal )
16585
16586    If the asm-specification is present, returns a STRING_CST
16587    corresponding to the string-literal.  Otherwise, returns
16588    NULL_TREE.  */
16589
16590 static tree
16591 cp_parser_asm_specification_opt (cp_parser* parser)
16592 {
16593   cp_token *token;
16594   tree asm_specification;
16595
16596   /* Peek at the next token.  */
16597   token = cp_lexer_peek_token (parser->lexer);
16598   /* If the next token isn't the `asm' keyword, then there's no
16599      asm-specification.  */
16600   if (!cp_parser_is_keyword (token, RID_ASM))
16601     return NULL_TREE;
16602
16603   /* Consume the `asm' token.  */
16604   cp_lexer_consume_token (parser->lexer);
16605   /* Look for the `('.  */
16606   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16607
16608   /* Look for the string-literal.  */
16609   asm_specification = cp_parser_string_literal (parser, false, false);
16610
16611   /* Look for the `)'.  */
16612   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16613
16614   return asm_specification;
16615 }
16616
16617 /* Parse an asm-operand-list.
16618
16619    asm-operand-list:
16620      asm-operand
16621      asm-operand-list , asm-operand
16622
16623    asm-operand:
16624      string-literal ( expression )
16625      [ string-literal ] string-literal ( expression )
16626
16627    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16628    each node is the expression.  The TREE_PURPOSE is itself a
16629    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16630    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16631    is a STRING_CST for the string literal before the parenthesis. Returns
16632    ERROR_MARK_NODE if any of the operands are invalid.  */
16633
16634 static tree
16635 cp_parser_asm_operand_list (cp_parser* parser)
16636 {
16637   tree asm_operands = NULL_TREE;
16638   bool invalid_operands = false;
16639
16640   while (true)
16641     {
16642       tree string_literal;
16643       tree expression;
16644       tree name;
16645
16646       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16647         {
16648           /* Consume the `[' token.  */
16649           cp_lexer_consume_token (parser->lexer);
16650           /* Read the operand name.  */
16651           name = cp_parser_identifier (parser);
16652           if (name != error_mark_node)
16653             name = build_string (IDENTIFIER_LENGTH (name),
16654                                  IDENTIFIER_POINTER (name));
16655           /* Look for the closing `]'.  */
16656           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16657         }
16658       else
16659         name = NULL_TREE;
16660       /* Look for the string-literal.  */
16661       string_literal = cp_parser_string_literal (parser, false, false);
16662
16663       /* Look for the `('.  */
16664       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16665       /* Parse the expression.  */
16666       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16667       /* Look for the `)'.  */
16668       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16669
16670       if (name == error_mark_node 
16671           || string_literal == error_mark_node 
16672           || expression == error_mark_node)
16673         invalid_operands = true;
16674
16675       /* Add this operand to the list.  */
16676       asm_operands = tree_cons (build_tree_list (name, string_literal),
16677                                 expression,
16678                                 asm_operands);
16679       /* If the next token is not a `,', there are no more
16680          operands.  */
16681       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16682         break;
16683       /* Consume the `,'.  */
16684       cp_lexer_consume_token (parser->lexer);
16685     }
16686
16687   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16688 }
16689
16690 /* Parse an asm-clobber-list.
16691
16692    asm-clobber-list:
16693      string-literal
16694      asm-clobber-list , string-literal
16695
16696    Returns a TREE_LIST, indicating the clobbers in the order that they
16697    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16698
16699 static tree
16700 cp_parser_asm_clobber_list (cp_parser* parser)
16701 {
16702   tree clobbers = NULL_TREE;
16703
16704   while (true)
16705     {
16706       tree string_literal;
16707
16708       /* Look for the string literal.  */
16709       string_literal = cp_parser_string_literal (parser, false, false);
16710       /* Add it to the list.  */
16711       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16712       /* If the next token is not a `,', then the list is
16713          complete.  */
16714       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16715         break;
16716       /* Consume the `,' token.  */
16717       cp_lexer_consume_token (parser->lexer);
16718     }
16719
16720   return clobbers;
16721 }
16722
16723 /* Parse an (optional) series of attributes.
16724
16725    attributes:
16726      attributes attribute
16727
16728    attribute:
16729      __attribute__ (( attribute-list [opt] ))
16730
16731    The return value is as for cp_parser_attribute_list.  */
16732
16733 static tree
16734 cp_parser_attributes_opt (cp_parser* parser)
16735 {
16736   tree attributes = NULL_TREE;
16737
16738   while (true)
16739     {
16740       cp_token *token;
16741       tree attribute_list;
16742
16743       /* Peek at the next token.  */
16744       token = cp_lexer_peek_token (parser->lexer);
16745       /* If it's not `__attribute__', then we're done.  */
16746       if (token->keyword != RID_ATTRIBUTE)
16747         break;
16748
16749       /* Consume the `__attribute__' keyword.  */
16750       cp_lexer_consume_token (parser->lexer);
16751       /* Look for the two `(' tokens.  */
16752       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16753       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16754
16755       /* Peek at the next token.  */
16756       token = cp_lexer_peek_token (parser->lexer);
16757       if (token->type != CPP_CLOSE_PAREN)
16758         /* Parse the attribute-list.  */
16759         attribute_list = cp_parser_attribute_list (parser);
16760       else
16761         /* If the next token is a `)', then there is no attribute
16762            list.  */
16763         attribute_list = NULL;
16764
16765       /* Look for the two `)' tokens.  */
16766       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16767       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16768
16769       /* Add these new attributes to the list.  */
16770       attributes = chainon (attributes, attribute_list);
16771     }
16772
16773   return attributes;
16774 }
16775
16776 /* Parse an attribute-list.
16777
16778    attribute-list:
16779      attribute
16780      attribute-list , attribute
16781
16782    attribute:
16783      identifier
16784      identifier ( identifier )
16785      identifier ( identifier , expression-list )
16786      identifier ( expression-list )
16787
16788    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16789    to an attribute.  The TREE_PURPOSE of each node is the identifier
16790    indicating which attribute is in use.  The TREE_VALUE represents
16791    the arguments, if any.  */
16792
16793 static tree
16794 cp_parser_attribute_list (cp_parser* parser)
16795 {
16796   tree attribute_list = NULL_TREE;
16797   bool save_translate_strings_p = parser->translate_strings_p;
16798
16799   parser->translate_strings_p = false;
16800   while (true)
16801     {
16802       cp_token *token;
16803       tree identifier;
16804       tree attribute;
16805
16806       /* Look for the identifier.  We also allow keywords here; for
16807          example `__attribute__ ((const))' is legal.  */
16808       token = cp_lexer_peek_token (parser->lexer);
16809       if (token->type == CPP_NAME
16810           || token->type == CPP_KEYWORD)
16811         {
16812           tree arguments = NULL_TREE;
16813
16814           /* Consume the token.  */
16815           token = cp_lexer_consume_token (parser->lexer);
16816
16817           /* Save away the identifier that indicates which attribute
16818              this is.  */
16819           identifier = token->u.value;
16820           attribute = build_tree_list (identifier, NULL_TREE);
16821
16822           /* Peek at the next token.  */
16823           token = cp_lexer_peek_token (parser->lexer);
16824           /* If it's an `(', then parse the attribute arguments.  */
16825           if (token->type == CPP_OPEN_PAREN)
16826             {
16827               arguments = cp_parser_parenthesized_expression_list
16828                           (parser, true, /*cast_p=*/false,
16829                            /*allow_expansion_p=*/false,
16830                            /*non_constant_p=*/NULL);
16831               /* Save the arguments away.  */
16832               TREE_VALUE (attribute) = arguments;
16833             }
16834
16835           if (arguments != error_mark_node)
16836             {
16837               /* Add this attribute to the list.  */
16838               TREE_CHAIN (attribute) = attribute_list;
16839               attribute_list = attribute;
16840             }
16841
16842           token = cp_lexer_peek_token (parser->lexer);
16843         }
16844       /* Now, look for more attributes.  If the next token isn't a
16845          `,', we're done.  */
16846       if (token->type != CPP_COMMA)
16847         break;
16848
16849       /* Consume the comma and keep going.  */
16850       cp_lexer_consume_token (parser->lexer);
16851     }
16852   parser->translate_strings_p = save_translate_strings_p;
16853
16854   /* We built up the list in reverse order.  */
16855   return nreverse (attribute_list);
16856 }
16857
16858 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16859    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16860    current value of the PEDANTIC flag, regardless of whether or not
16861    the `__extension__' keyword is present.  The caller is responsible
16862    for restoring the value of the PEDANTIC flag.  */
16863
16864 static bool
16865 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16866 {
16867   /* Save the old value of the PEDANTIC flag.  */
16868   *saved_pedantic = pedantic;
16869
16870   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16871     {
16872       /* Consume the `__extension__' token.  */
16873       cp_lexer_consume_token (parser->lexer);
16874       /* We're not being pedantic while the `__extension__' keyword is
16875          in effect.  */
16876       pedantic = 0;
16877
16878       return true;
16879     }
16880
16881   return false;
16882 }
16883
16884 /* Parse a label declaration.
16885
16886    label-declaration:
16887      __label__ label-declarator-seq ;
16888
16889    label-declarator-seq:
16890      identifier , label-declarator-seq
16891      identifier  */
16892
16893 static void
16894 cp_parser_label_declaration (cp_parser* parser)
16895 {
16896   /* Look for the `__label__' keyword.  */
16897   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16898
16899   while (true)
16900     {
16901       tree identifier;
16902
16903       /* Look for an identifier.  */
16904       identifier = cp_parser_identifier (parser);
16905       /* If we failed, stop.  */
16906       if (identifier == error_mark_node)
16907         break;
16908       /* Declare it as a label.  */
16909       finish_label_decl (identifier);
16910       /* If the next token is a `;', stop.  */
16911       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16912         break;
16913       /* Look for the `,' separating the label declarations.  */
16914       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16915     }
16916
16917   /* Look for the final `;'.  */
16918   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16919 }
16920
16921 /* Support Functions */
16922
16923 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16924    NAME should have one of the representations used for an
16925    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16926    is returned.  If PARSER->SCOPE is a dependent type, then a
16927    SCOPE_REF is returned.
16928
16929    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16930    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16931    was formed.  Abstractly, such entities should not be passed to this
16932    function, because they do not need to be looked up, but it is
16933    simpler to check for this special case here, rather than at the
16934    call-sites.
16935
16936    In cases not explicitly covered above, this function returns a
16937    DECL, OVERLOAD, or baselink representing the result of the lookup.
16938    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16939    is returned.
16940
16941    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16942    (e.g., "struct") that was used.  In that case bindings that do not
16943    refer to types are ignored.
16944
16945    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16946    ignored.
16947
16948    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16949    are ignored.
16950
16951    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16952    types.
16953
16954    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16955    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16956    NULL_TREE otherwise.  */
16957
16958 static tree
16959 cp_parser_lookup_name (cp_parser *parser, tree name,
16960                        enum tag_types tag_type,
16961                        bool is_template,
16962                        bool is_namespace,
16963                        bool check_dependency,
16964                        tree *ambiguous_decls,
16965                        location_t name_location)
16966 {
16967   int flags = 0;
16968   tree decl;
16969   tree object_type = parser->context->object_type;
16970
16971   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16972     flags |= LOOKUP_COMPLAIN;
16973
16974   /* Assume that the lookup will be unambiguous.  */
16975   if (ambiguous_decls)
16976     *ambiguous_decls = NULL_TREE;
16977
16978   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16979      no longer valid.  Note that if we are parsing tentatively, and
16980      the parse fails, OBJECT_TYPE will be automatically restored.  */
16981   parser->context->object_type = NULL_TREE;
16982
16983   if (name == error_mark_node)
16984     return error_mark_node;
16985
16986   /* A template-id has already been resolved; there is no lookup to
16987      do.  */
16988   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16989     return name;
16990   if (BASELINK_P (name))
16991     {
16992       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16993                   == TEMPLATE_ID_EXPR);
16994       return name;
16995     }
16996
16997   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16998      it should already have been checked to make sure that the name
16999      used matches the type being destroyed.  */
17000   if (TREE_CODE (name) == BIT_NOT_EXPR)
17001     {
17002       tree type;
17003
17004       /* Figure out to which type this destructor applies.  */
17005       if (parser->scope)
17006         type = parser->scope;
17007       else if (object_type)
17008         type = object_type;
17009       else
17010         type = current_class_type;
17011       /* If that's not a class type, there is no destructor.  */
17012       if (!type || !CLASS_TYPE_P (type))
17013         return error_mark_node;
17014       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17015         lazily_declare_fn (sfk_destructor, type);
17016       if (!CLASSTYPE_DESTRUCTORS (type))
17017           return error_mark_node;
17018       /* If it was a class type, return the destructor.  */
17019       return CLASSTYPE_DESTRUCTORS (type);
17020     }
17021
17022   /* By this point, the NAME should be an ordinary identifier.  If
17023      the id-expression was a qualified name, the qualifying scope is
17024      stored in PARSER->SCOPE at this point.  */
17025   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17026
17027   /* Perform the lookup.  */
17028   if (parser->scope)
17029     {
17030       bool dependent_p;
17031
17032       if (parser->scope == error_mark_node)
17033         return error_mark_node;
17034
17035       /* If the SCOPE is dependent, the lookup must be deferred until
17036          the template is instantiated -- unless we are explicitly
17037          looking up names in uninstantiated templates.  Even then, we
17038          cannot look up the name if the scope is not a class type; it
17039          might, for example, be a template type parameter.  */
17040       dependent_p = (TYPE_P (parser->scope)
17041                      && dependent_scope_p (parser->scope));
17042       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17043           && dependent_p)
17044         /* Defer lookup.  */
17045         decl = error_mark_node;
17046       else
17047         {
17048           tree pushed_scope = NULL_TREE;
17049
17050           /* If PARSER->SCOPE is a dependent type, then it must be a
17051              class type, and we must not be checking dependencies;
17052              otherwise, we would have processed this lookup above.  So
17053              that PARSER->SCOPE is not considered a dependent base by
17054              lookup_member, we must enter the scope here.  */
17055           if (dependent_p)
17056             pushed_scope = push_scope (parser->scope);
17057           /* If the PARSER->SCOPE is a template specialization, it
17058              may be instantiated during name lookup.  In that case,
17059              errors may be issued.  Even if we rollback the current
17060              tentative parse, those errors are valid.  */
17061           decl = lookup_qualified_name (parser->scope, name,
17062                                         tag_type != none_type,
17063                                         /*complain=*/true);
17064
17065           /* If we have a single function from a using decl, pull it out.  */
17066           if (TREE_CODE (decl) == OVERLOAD
17067               && !really_overloaded_fn (decl))
17068             decl = OVL_FUNCTION (decl);
17069
17070           if (pushed_scope)
17071             pop_scope (pushed_scope);
17072         }
17073
17074       /* If the scope is a dependent type and either we deferred lookup or
17075          we did lookup but didn't find the name, rememeber the name.  */
17076       if (decl == error_mark_node && TYPE_P (parser->scope)
17077           && dependent_type_p (parser->scope))
17078         {
17079           if (tag_type)
17080             {
17081               tree type;
17082
17083               /* The resolution to Core Issue 180 says that `struct
17084                  A::B' should be considered a type-name, even if `A'
17085                  is dependent.  */
17086               type = make_typename_type (parser->scope, name, tag_type,
17087                                          /*complain=*/tf_error);
17088               decl = TYPE_NAME (type);
17089             }
17090           else if (is_template
17091                    && (cp_parser_next_token_ends_template_argument_p (parser)
17092                        || cp_lexer_next_token_is (parser->lexer,
17093                                                   CPP_CLOSE_PAREN)))
17094             decl = make_unbound_class_template (parser->scope,
17095                                                 name, NULL_TREE,
17096                                                 /*complain=*/tf_error);
17097           else
17098             decl = build_qualified_name (/*type=*/NULL_TREE,
17099                                          parser->scope, name,
17100                                          is_template);
17101         }
17102       parser->qualifying_scope = parser->scope;
17103       parser->object_scope = NULL_TREE;
17104     }
17105   else if (object_type)
17106     {
17107       tree object_decl = NULL_TREE;
17108       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17109          OBJECT_TYPE is not a class.  */
17110       if (CLASS_TYPE_P (object_type))
17111         /* If the OBJECT_TYPE is a template specialization, it may
17112            be instantiated during name lookup.  In that case, errors
17113            may be issued.  Even if we rollback the current tentative
17114            parse, those errors are valid.  */
17115         object_decl = lookup_member (object_type,
17116                                      name,
17117                                      /*protect=*/0,
17118                                      tag_type != none_type);
17119       /* Look it up in the enclosing context, too.  */
17120       decl = lookup_name_real (name, tag_type != none_type,
17121                                /*nonclass=*/0,
17122                                /*block_p=*/true, is_namespace, flags);
17123       parser->object_scope = object_type;
17124       parser->qualifying_scope = NULL_TREE;
17125       if (object_decl)
17126         decl = object_decl;
17127     }
17128   else
17129     {
17130       decl = lookup_name_real (name, tag_type != none_type,
17131                                /*nonclass=*/0,
17132                                /*block_p=*/true, is_namespace, flags);
17133       parser->qualifying_scope = NULL_TREE;
17134       parser->object_scope = NULL_TREE;
17135     }
17136
17137   /* If the lookup failed, let our caller know.  */
17138   if (!decl || decl == error_mark_node)
17139     return error_mark_node;
17140
17141   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17142   if (TREE_CODE (decl) == TREE_LIST)
17143     {
17144       if (ambiguous_decls)
17145         *ambiguous_decls = decl;
17146       /* The error message we have to print is too complicated for
17147          cp_parser_error, so we incorporate its actions directly.  */
17148       if (!cp_parser_simulate_error (parser))
17149         {
17150           error ("%Hreference to %qD is ambiguous",
17151                  &name_location, name);
17152           print_candidates (decl);
17153         }
17154       return error_mark_node;
17155     }
17156
17157   gcc_assert (DECL_P (decl)
17158               || TREE_CODE (decl) == OVERLOAD
17159               || TREE_CODE (decl) == SCOPE_REF
17160               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17161               || BASELINK_P (decl));
17162
17163   /* If we have resolved the name of a member declaration, check to
17164      see if the declaration is accessible.  When the name resolves to
17165      set of overloaded functions, accessibility is checked when
17166      overload resolution is done.
17167
17168      During an explicit instantiation, access is not checked at all,
17169      as per [temp.explicit].  */
17170   if (DECL_P (decl))
17171     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17172
17173   return decl;
17174 }
17175
17176 /* Like cp_parser_lookup_name, but for use in the typical case where
17177    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17178    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17179
17180 static tree
17181 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17182 {
17183   return cp_parser_lookup_name (parser, name,
17184                                 none_type,
17185                                 /*is_template=*/false,
17186                                 /*is_namespace=*/false,
17187                                 /*check_dependency=*/true,
17188                                 /*ambiguous_decls=*/NULL,
17189                                 location);
17190 }
17191
17192 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17193    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17194    true, the DECL indicates the class being defined in a class-head,
17195    or declared in an elaborated-type-specifier.
17196
17197    Otherwise, return DECL.  */
17198
17199 static tree
17200 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17201 {
17202   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17203      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17204
17205        struct A {
17206          template <typename T> struct B;
17207        };
17208
17209        template <typename T> struct A::B {};
17210
17211      Similarly, in an elaborated-type-specifier:
17212
17213        namespace N { struct X{}; }
17214
17215        struct A {
17216          template <typename T> friend struct N::X;
17217        };
17218
17219      However, if the DECL refers to a class type, and we are in
17220      the scope of the class, then the name lookup automatically
17221      finds the TYPE_DECL created by build_self_reference rather
17222      than a TEMPLATE_DECL.  For example, in:
17223
17224        template <class T> struct S {
17225          S s;
17226        };
17227
17228      there is no need to handle such case.  */
17229
17230   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17231     return DECL_TEMPLATE_RESULT (decl);
17232
17233   return decl;
17234 }
17235
17236 /* If too many, or too few, template-parameter lists apply to the
17237    declarator, issue an error message.  Returns TRUE if all went well,
17238    and FALSE otherwise.  */
17239
17240 static bool
17241 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17242                                                 cp_declarator *declarator,
17243                                                 location_t declarator_location)
17244 {
17245   unsigned num_templates;
17246
17247   /* We haven't seen any classes that involve template parameters yet.  */
17248   num_templates = 0;
17249
17250   switch (declarator->kind)
17251     {
17252     case cdk_id:
17253       if (declarator->u.id.qualifying_scope)
17254         {
17255           tree scope;
17256           tree member;
17257
17258           scope = declarator->u.id.qualifying_scope;
17259           member = declarator->u.id.unqualified_name;
17260
17261           while (scope && CLASS_TYPE_P (scope))
17262             {
17263               /* You're supposed to have one `template <...>'
17264                  for every template class, but you don't need one
17265                  for a full specialization.  For example:
17266
17267                  template <class T> struct S{};
17268                  template <> struct S<int> { void f(); };
17269                  void S<int>::f () {}
17270
17271                  is correct; there shouldn't be a `template <>' for
17272                  the definition of `S<int>::f'.  */
17273               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17274                 /* If SCOPE does not have template information of any
17275                    kind, then it is not a template, nor is it nested
17276                    within a template.  */
17277                 break;
17278               if (explicit_class_specialization_p (scope))
17279                 break;
17280               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17281                 ++num_templates;
17282
17283               scope = TYPE_CONTEXT (scope);
17284             }
17285         }
17286       else if (TREE_CODE (declarator->u.id.unqualified_name)
17287                == TEMPLATE_ID_EXPR)
17288         /* If the DECLARATOR has the form `X<y>' then it uses one
17289            additional level of template parameters.  */
17290         ++num_templates;
17291
17292       return cp_parser_check_template_parameters (parser,
17293                                                   num_templates,
17294                                                   declarator_location);
17295
17296     case cdk_function:
17297     case cdk_array:
17298     case cdk_pointer:
17299     case cdk_reference:
17300     case cdk_ptrmem:
17301       return (cp_parser_check_declarator_template_parameters
17302               (parser, declarator->declarator, declarator_location));
17303
17304     case cdk_error:
17305       return true;
17306
17307     default:
17308       gcc_unreachable ();
17309     }
17310   return false;
17311 }
17312
17313 /* NUM_TEMPLATES were used in the current declaration.  If that is
17314    invalid, return FALSE and issue an error messages.  Otherwise,
17315    return TRUE.  */
17316
17317 static bool
17318 cp_parser_check_template_parameters (cp_parser* parser,
17319                                      unsigned num_templates,
17320                                      location_t location)
17321 {
17322   /* If there are more template classes than parameter lists, we have
17323      something like:
17324
17325        template <class T> void S<T>::R<T>::f ();  */
17326   if (parser->num_template_parameter_lists < num_templates)
17327     {
17328       error ("%Htoo few template-parameter-lists", &location);
17329       return false;
17330     }
17331   /* If there are the same number of template classes and parameter
17332      lists, that's OK.  */
17333   if (parser->num_template_parameter_lists == num_templates)
17334     return true;
17335   /* If there are more, but only one more, then we are referring to a
17336      member template.  That's OK too.  */
17337   if (parser->num_template_parameter_lists == num_templates + 1)
17338       return true;
17339   /* Otherwise, there are too many template parameter lists.  We have
17340      something like:
17341
17342      template <class T> template <class U> void S::f();  */
17343   error ("%Htoo many template-parameter-lists", &location);
17344   return false;
17345 }
17346
17347 /* Parse an optional `::' token indicating that the following name is
17348    from the global namespace.  If so, PARSER->SCOPE is set to the
17349    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17350    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17351    Returns the new value of PARSER->SCOPE, if the `::' token is
17352    present, and NULL_TREE otherwise.  */
17353
17354 static tree
17355 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17356 {
17357   cp_token *token;
17358
17359   /* Peek at the next token.  */
17360   token = cp_lexer_peek_token (parser->lexer);
17361   /* If we're looking at a `::' token then we're starting from the
17362      global namespace, not our current location.  */
17363   if (token->type == CPP_SCOPE)
17364     {
17365       /* Consume the `::' token.  */
17366       cp_lexer_consume_token (parser->lexer);
17367       /* Set the SCOPE so that we know where to start the lookup.  */
17368       parser->scope = global_namespace;
17369       parser->qualifying_scope = global_namespace;
17370       parser->object_scope = NULL_TREE;
17371
17372       return parser->scope;
17373     }
17374   else if (!current_scope_valid_p)
17375     {
17376       parser->scope = NULL_TREE;
17377       parser->qualifying_scope = NULL_TREE;
17378       parser->object_scope = NULL_TREE;
17379     }
17380
17381   return NULL_TREE;
17382 }
17383
17384 /* Returns TRUE if the upcoming token sequence is the start of a
17385    constructor declarator.  If FRIEND_P is true, the declarator is
17386    preceded by the `friend' specifier.  */
17387
17388 static bool
17389 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17390 {
17391   bool constructor_p;
17392   tree type_decl = NULL_TREE;
17393   bool nested_name_p;
17394   cp_token *next_token;
17395
17396   /* The common case is that this is not a constructor declarator, so
17397      try to avoid doing lots of work if at all possible.  It's not
17398      valid declare a constructor at function scope.  */
17399   if (parser->in_function_body)
17400     return false;
17401   /* And only certain tokens can begin a constructor declarator.  */
17402   next_token = cp_lexer_peek_token (parser->lexer);
17403   if (next_token->type != CPP_NAME
17404       && next_token->type != CPP_SCOPE
17405       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17406       && next_token->type != CPP_TEMPLATE_ID)
17407     return false;
17408
17409   /* Parse tentatively; we are going to roll back all of the tokens
17410      consumed here.  */
17411   cp_parser_parse_tentatively (parser);
17412   /* Assume that we are looking at a constructor declarator.  */
17413   constructor_p = true;
17414
17415   /* Look for the optional `::' operator.  */
17416   cp_parser_global_scope_opt (parser,
17417                               /*current_scope_valid_p=*/false);
17418   /* Look for the nested-name-specifier.  */
17419   nested_name_p
17420     = (cp_parser_nested_name_specifier_opt (parser,
17421                                             /*typename_keyword_p=*/false,
17422                                             /*check_dependency_p=*/false,
17423                                             /*type_p=*/false,
17424                                             /*is_declaration=*/false)
17425        != NULL_TREE);
17426   /* Outside of a class-specifier, there must be a
17427      nested-name-specifier.  */
17428   if (!nested_name_p &&
17429       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17430        || friend_p))
17431     constructor_p = false;
17432   /* If we still think that this might be a constructor-declarator,
17433      look for a class-name.  */
17434   if (constructor_p)
17435     {
17436       /* If we have:
17437
17438            template <typename T> struct S { S(); };
17439            template <typename T> S<T>::S ();
17440
17441          we must recognize that the nested `S' names a class.
17442          Similarly, for:
17443
17444            template <typename T> S<T>::S<T> ();
17445
17446          we must recognize that the nested `S' names a template.  */
17447       type_decl = cp_parser_class_name (parser,
17448                                         /*typename_keyword_p=*/false,
17449                                         /*template_keyword_p=*/false,
17450                                         none_type,
17451                                         /*check_dependency_p=*/false,
17452                                         /*class_head_p=*/false,
17453                                         /*is_declaration=*/false);
17454       /* If there was no class-name, then this is not a constructor.  */
17455       constructor_p = !cp_parser_error_occurred (parser);
17456     }
17457
17458   /* If we're still considering a constructor, we have to see a `(',
17459      to begin the parameter-declaration-clause, followed by either a
17460      `)', an `...', or a decl-specifier.  We need to check for a
17461      type-specifier to avoid being fooled into thinking that:
17462
17463        S::S (f) (int);
17464
17465      is a constructor.  (It is actually a function named `f' that
17466      takes one parameter (of type `int') and returns a value of type
17467      `S::S'.  */
17468   if (constructor_p
17469       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17470     {
17471       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17472           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17473           /* A parameter declaration begins with a decl-specifier,
17474              which is either the "attribute" keyword, a storage class
17475              specifier, or (usually) a type-specifier.  */
17476           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17477         {
17478           tree type;
17479           tree pushed_scope = NULL_TREE;
17480           unsigned saved_num_template_parameter_lists;
17481
17482           /* Names appearing in the type-specifier should be looked up
17483              in the scope of the class.  */
17484           if (current_class_type)
17485             type = NULL_TREE;
17486           else
17487             {
17488               type = TREE_TYPE (type_decl);
17489               if (TREE_CODE (type) == TYPENAME_TYPE)
17490                 {
17491                   type = resolve_typename_type (type,
17492                                                 /*only_current_p=*/false);
17493                   if (TREE_CODE (type) == TYPENAME_TYPE)
17494                     {
17495                       cp_parser_abort_tentative_parse (parser);
17496                       return false;
17497                     }
17498                 }
17499               pushed_scope = push_scope (type);
17500             }
17501
17502           /* Inside the constructor parameter list, surrounding
17503              template-parameter-lists do not apply.  */
17504           saved_num_template_parameter_lists
17505             = parser->num_template_parameter_lists;
17506           parser->num_template_parameter_lists = 0;
17507
17508           /* Look for the type-specifier.  */
17509           cp_parser_type_specifier (parser,
17510                                     CP_PARSER_FLAGS_NONE,
17511                                     /*decl_specs=*/NULL,
17512                                     /*is_declarator=*/true,
17513                                     /*declares_class_or_enum=*/NULL,
17514                                     /*is_cv_qualifier=*/NULL);
17515
17516           parser->num_template_parameter_lists
17517             = saved_num_template_parameter_lists;
17518
17519           /* Leave the scope of the class.  */
17520           if (pushed_scope)
17521             pop_scope (pushed_scope);
17522
17523           constructor_p = !cp_parser_error_occurred (parser);
17524         }
17525     }
17526   else
17527     constructor_p = false;
17528   /* We did not really want to consume any tokens.  */
17529   cp_parser_abort_tentative_parse (parser);
17530
17531   return constructor_p;
17532 }
17533
17534 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17535    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17536    they must be performed once we are in the scope of the function.
17537
17538    Returns the function defined.  */
17539
17540 static tree
17541 cp_parser_function_definition_from_specifiers_and_declarator
17542   (cp_parser* parser,
17543    cp_decl_specifier_seq *decl_specifiers,
17544    tree attributes,
17545    const cp_declarator *declarator)
17546 {
17547   tree fn;
17548   bool success_p;
17549
17550   /* Begin the function-definition.  */
17551   success_p = start_function (decl_specifiers, declarator, attributes);
17552
17553   /* The things we're about to see are not directly qualified by any
17554      template headers we've seen thus far.  */
17555   reset_specialization ();
17556
17557   /* If there were names looked up in the decl-specifier-seq that we
17558      did not check, check them now.  We must wait until we are in the
17559      scope of the function to perform the checks, since the function
17560      might be a friend.  */
17561   perform_deferred_access_checks ();
17562
17563   if (!success_p)
17564     {
17565       /* Skip the entire function.  */
17566       cp_parser_skip_to_end_of_block_or_statement (parser);
17567       fn = error_mark_node;
17568     }
17569   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17570     {
17571       /* Seen already, skip it.  An error message has already been output.  */
17572       cp_parser_skip_to_end_of_block_or_statement (parser);
17573       fn = current_function_decl;
17574       current_function_decl = NULL_TREE;
17575       /* If this is a function from a class, pop the nested class.  */
17576       if (current_class_name)
17577         pop_nested_class ();
17578     }
17579   else
17580     fn = cp_parser_function_definition_after_declarator (parser,
17581                                                          /*inline_p=*/false);
17582
17583   return fn;
17584 }
17585
17586 /* Parse the part of a function-definition that follows the
17587    declarator.  INLINE_P is TRUE iff this function is an inline
17588    function defined with a class-specifier.
17589
17590    Returns the function defined.  */
17591
17592 static tree
17593 cp_parser_function_definition_after_declarator (cp_parser* parser,
17594                                                 bool inline_p)
17595 {
17596   tree fn;
17597   bool ctor_initializer_p = false;
17598   bool saved_in_unbraced_linkage_specification_p;
17599   bool saved_in_function_body;
17600   unsigned saved_num_template_parameter_lists;
17601   cp_token *token;
17602
17603   saved_in_function_body = parser->in_function_body;
17604   parser->in_function_body = true;
17605   /* If the next token is `return', then the code may be trying to
17606      make use of the "named return value" extension that G++ used to
17607      support.  */
17608   token = cp_lexer_peek_token (parser->lexer);
17609   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17610     {
17611       /* Consume the `return' keyword.  */
17612       cp_lexer_consume_token (parser->lexer);
17613       /* Look for the identifier that indicates what value is to be
17614          returned.  */
17615       cp_parser_identifier (parser);
17616       /* Issue an error message.  */
17617       error ("%Hnamed return values are no longer supported",
17618              &token->location);
17619       /* Skip tokens until we reach the start of the function body.  */
17620       while (true)
17621         {
17622           cp_token *token = cp_lexer_peek_token (parser->lexer);
17623           if (token->type == CPP_OPEN_BRACE
17624               || token->type == CPP_EOF
17625               || token->type == CPP_PRAGMA_EOL)
17626             break;
17627           cp_lexer_consume_token (parser->lexer);
17628         }
17629     }
17630   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17631      anything declared inside `f'.  */
17632   saved_in_unbraced_linkage_specification_p
17633     = parser->in_unbraced_linkage_specification_p;
17634   parser->in_unbraced_linkage_specification_p = false;
17635   /* Inside the function, surrounding template-parameter-lists do not
17636      apply.  */
17637   saved_num_template_parameter_lists
17638     = parser->num_template_parameter_lists;
17639   parser->num_template_parameter_lists = 0;
17640   /* If the next token is `try', then we are looking at a
17641      function-try-block.  */
17642   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17643     ctor_initializer_p = cp_parser_function_try_block (parser);
17644   /* A function-try-block includes the function-body, so we only do
17645      this next part if we're not processing a function-try-block.  */
17646   else
17647     ctor_initializer_p
17648       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17649
17650   /* Finish the function.  */
17651   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17652                         (inline_p ? 2 : 0));
17653   /* Generate code for it, if necessary.  */
17654   expand_or_defer_fn (fn);
17655   /* Restore the saved values.  */
17656   parser->in_unbraced_linkage_specification_p
17657     = saved_in_unbraced_linkage_specification_p;
17658   parser->num_template_parameter_lists
17659     = saved_num_template_parameter_lists;
17660   parser->in_function_body = saved_in_function_body;
17661
17662   return fn;
17663 }
17664
17665 /* Parse a template-declaration, assuming that the `export' (and
17666    `extern') keywords, if present, has already been scanned.  MEMBER_P
17667    is as for cp_parser_template_declaration.  */
17668
17669 static void
17670 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17671 {
17672   tree decl = NULL_TREE;
17673   VEC (deferred_access_check,gc) *checks;
17674   tree parameter_list;
17675   bool friend_p = false;
17676   bool need_lang_pop;
17677   cp_token *token;
17678
17679   /* Look for the `template' keyword.  */
17680   token = cp_lexer_peek_token (parser->lexer);
17681   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17682     return;
17683
17684   /* And the `<'.  */
17685   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17686     return;
17687   if (at_class_scope_p () && current_function_decl)
17688     {
17689       /* 14.5.2.2 [temp.mem]
17690
17691          A local class shall not have member templates.  */
17692       error ("%Hinvalid declaration of member template in local class",
17693              &token->location);
17694       cp_parser_skip_to_end_of_block_or_statement (parser);
17695       return;
17696     }
17697   /* [temp]
17698
17699      A template ... shall not have C linkage.  */
17700   if (current_lang_name == lang_name_c)
17701     {
17702       error ("%Htemplate with C linkage", &token->location);
17703       /* Give it C++ linkage to avoid confusing other parts of the
17704          front end.  */
17705       push_lang_context (lang_name_cplusplus);
17706       need_lang_pop = true;
17707     }
17708   else
17709     need_lang_pop = false;
17710
17711   /* We cannot perform access checks on the template parameter
17712      declarations until we know what is being declared, just as we
17713      cannot check the decl-specifier list.  */
17714   push_deferring_access_checks (dk_deferred);
17715
17716   /* If the next token is `>', then we have an invalid
17717      specialization.  Rather than complain about an invalid template
17718      parameter, issue an error message here.  */
17719   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17720     {
17721       cp_parser_error (parser, "invalid explicit specialization");
17722       begin_specialization ();
17723       parameter_list = NULL_TREE;
17724     }
17725   else
17726     /* Parse the template parameters.  */
17727     parameter_list = cp_parser_template_parameter_list (parser);
17728
17729   /* Get the deferred access checks from the parameter list.  These
17730      will be checked once we know what is being declared, as for a
17731      member template the checks must be performed in the scope of the
17732      class containing the member.  */
17733   checks = get_deferred_access_checks ();
17734
17735   /* Look for the `>'.  */
17736   cp_parser_skip_to_end_of_template_parameter_list (parser);
17737   /* We just processed one more parameter list.  */
17738   ++parser->num_template_parameter_lists;
17739   /* If the next token is `template', there are more template
17740      parameters.  */
17741   if (cp_lexer_next_token_is_keyword (parser->lexer,
17742                                       RID_TEMPLATE))
17743     cp_parser_template_declaration_after_export (parser, member_p);
17744   else
17745     {
17746       /* There are no access checks when parsing a template, as we do not
17747          know if a specialization will be a friend.  */
17748       push_deferring_access_checks (dk_no_check);
17749       token = cp_lexer_peek_token (parser->lexer);
17750       decl = cp_parser_single_declaration (parser,
17751                                            checks,
17752                                            member_p,
17753                                            /*explicit_specialization_p=*/false,
17754                                            &friend_p);
17755       pop_deferring_access_checks ();
17756
17757       /* If this is a member template declaration, let the front
17758          end know.  */
17759       if (member_p && !friend_p && decl)
17760         {
17761           if (TREE_CODE (decl) == TYPE_DECL)
17762             cp_parser_check_access_in_redeclaration (decl, token->location);
17763
17764           decl = finish_member_template_decl (decl);
17765         }
17766       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17767         make_friend_class (current_class_type, TREE_TYPE (decl),
17768                            /*complain=*/true);
17769     }
17770   /* We are done with the current parameter list.  */
17771   --parser->num_template_parameter_lists;
17772
17773   pop_deferring_access_checks ();
17774
17775   /* Finish up.  */
17776   finish_template_decl (parameter_list);
17777
17778   /* Register member declarations.  */
17779   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17780     finish_member_declaration (decl);
17781   /* For the erroneous case of a template with C linkage, we pushed an
17782      implicit C++ linkage scope; exit that scope now.  */
17783   if (need_lang_pop)
17784     pop_lang_context ();
17785   /* If DECL is a function template, we must return to parse it later.
17786      (Even though there is no definition, there might be default
17787      arguments that need handling.)  */
17788   if (member_p && decl
17789       && (TREE_CODE (decl) == FUNCTION_DECL
17790           || DECL_FUNCTION_TEMPLATE_P (decl)))
17791     TREE_VALUE (parser->unparsed_functions_queues)
17792       = tree_cons (NULL_TREE, decl,
17793                    TREE_VALUE (parser->unparsed_functions_queues));
17794 }
17795
17796 /* Perform the deferred access checks from a template-parameter-list.
17797    CHECKS is a TREE_LIST of access checks, as returned by
17798    get_deferred_access_checks.  */
17799
17800 static void
17801 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17802 {
17803   ++processing_template_parmlist;
17804   perform_access_checks (checks);
17805   --processing_template_parmlist;
17806 }
17807
17808 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17809    `function-definition' sequence.  MEMBER_P is true, this declaration
17810    appears in a class scope.
17811
17812    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17813    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17814
17815 static tree
17816 cp_parser_single_declaration (cp_parser* parser,
17817                               VEC (deferred_access_check,gc)* checks,
17818                               bool member_p,
17819                               bool explicit_specialization_p,
17820                               bool* friend_p)
17821 {
17822   int declares_class_or_enum;
17823   tree decl = NULL_TREE;
17824   cp_decl_specifier_seq decl_specifiers;
17825   bool function_definition_p = false;
17826   cp_token *decl_spec_token_start;
17827
17828   /* This function is only used when processing a template
17829      declaration.  */
17830   gcc_assert (innermost_scope_kind () == sk_template_parms
17831               || innermost_scope_kind () == sk_template_spec);
17832
17833   /* Defer access checks until we know what is being declared.  */
17834   push_deferring_access_checks (dk_deferred);
17835
17836   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17837      alternative.  */
17838   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17839   cp_parser_decl_specifier_seq (parser,
17840                                 CP_PARSER_FLAGS_OPTIONAL,
17841                                 &decl_specifiers,
17842                                 &declares_class_or_enum);
17843   if (friend_p)
17844     *friend_p = cp_parser_friend_p (&decl_specifiers);
17845
17846   /* There are no template typedefs.  */
17847   if (decl_specifiers.specs[(int) ds_typedef])
17848     {
17849       error ("%Htemplate declaration of %qs",
17850              &decl_spec_token_start->location, "typedef");
17851       decl = error_mark_node;
17852     }
17853
17854   /* Gather up the access checks that occurred the
17855      decl-specifier-seq.  */
17856   stop_deferring_access_checks ();
17857
17858   /* Check for the declaration of a template class.  */
17859   if (declares_class_or_enum)
17860     {
17861       if (cp_parser_declares_only_class_p (parser))
17862         {
17863           decl = shadow_tag (&decl_specifiers);
17864
17865           /* In this case:
17866
17867                struct C {
17868                  friend template <typename T> struct A<T>::B;
17869                };
17870
17871              A<T>::B will be represented by a TYPENAME_TYPE, and
17872              therefore not recognized by shadow_tag.  */
17873           if (friend_p && *friend_p
17874               && !decl
17875               && decl_specifiers.type
17876               && TYPE_P (decl_specifiers.type))
17877             decl = decl_specifiers.type;
17878
17879           if (decl && decl != error_mark_node)
17880             decl = TYPE_NAME (decl);
17881           else
17882             decl = error_mark_node;
17883
17884           /* Perform access checks for template parameters.  */
17885           cp_parser_perform_template_parameter_access_checks (checks);
17886         }
17887     }
17888   /* If it's not a template class, try for a template function.  If
17889      the next token is a `;', then this declaration does not declare
17890      anything.  But, if there were errors in the decl-specifiers, then
17891      the error might well have come from an attempted class-specifier.
17892      In that case, there's no need to warn about a missing declarator.  */
17893   if (!decl
17894       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17895           || decl_specifiers.type != error_mark_node))
17896     {
17897       decl = cp_parser_init_declarator (parser,
17898                                         &decl_specifiers,
17899                                         checks,
17900                                         /*function_definition_allowed_p=*/true,
17901                                         member_p,
17902                                         declares_class_or_enum,
17903                                         &function_definition_p);
17904
17905     /* 7.1.1-1 [dcl.stc]
17906
17907        A storage-class-specifier shall not be specified in an explicit
17908        specialization...  */
17909     if (decl
17910         && explicit_specialization_p
17911         && decl_specifiers.storage_class != sc_none)
17912       {
17913         error ("%Hexplicit template specialization cannot have a storage class",
17914                &decl_spec_token_start->location);
17915         decl = error_mark_node;
17916       }
17917     }
17918
17919   pop_deferring_access_checks ();
17920
17921   /* Clear any current qualification; whatever comes next is the start
17922      of something new.  */
17923   parser->scope = NULL_TREE;
17924   parser->qualifying_scope = NULL_TREE;
17925   parser->object_scope = NULL_TREE;
17926   /* Look for a trailing `;' after the declaration.  */
17927   if (!function_definition_p
17928       && (decl == error_mark_node
17929           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17930     cp_parser_skip_to_end_of_block_or_statement (parser);
17931
17932   return decl;
17933 }
17934
17935 /* Parse a cast-expression that is not the operand of a unary "&".  */
17936
17937 static tree
17938 cp_parser_simple_cast_expression (cp_parser *parser)
17939 {
17940   return cp_parser_cast_expression (parser, /*address_p=*/false,
17941                                     /*cast_p=*/false, NULL);
17942 }
17943
17944 /* Parse a functional cast to TYPE.  Returns an expression
17945    representing the cast.  */
17946
17947 static tree
17948 cp_parser_functional_cast (cp_parser* parser, tree type)
17949 {
17950   tree expression_list;
17951   tree cast;
17952   bool nonconst_p;
17953
17954   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17955     {
17956       maybe_warn_cpp0x ("extended initializer lists");
17957       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17958       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17959       if (TREE_CODE (type) == TYPE_DECL)
17960         type = TREE_TYPE (type);
17961       return finish_compound_literal (type, expression_list);
17962     }
17963
17964   expression_list
17965     = cp_parser_parenthesized_expression_list (parser, false,
17966                                                /*cast_p=*/true,
17967                                                /*allow_expansion_p=*/true,
17968                                                /*non_constant_p=*/NULL);
17969
17970   cast = build_functional_cast (type, expression_list,
17971                                 tf_warning_or_error);
17972   /* [expr.const]/1: In an integral constant expression "only type
17973      conversions to integral or enumeration type can be used".  */
17974   if (TREE_CODE (type) == TYPE_DECL)
17975     type = TREE_TYPE (type);
17976   if (cast != error_mark_node
17977       && !cast_valid_in_integral_constant_expression_p (type)
17978       && (cp_parser_non_integral_constant_expression
17979           (parser, "a call to a constructor")))
17980     return error_mark_node;
17981   return cast;
17982 }
17983
17984 /* Save the tokens that make up the body of a member function defined
17985    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17986    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17987    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17988    for the member function.  */
17989
17990 static tree
17991 cp_parser_save_member_function_body (cp_parser* parser,
17992                                      cp_decl_specifier_seq *decl_specifiers,
17993                                      cp_declarator *declarator,
17994                                      tree attributes)
17995 {
17996   cp_token *first;
17997   cp_token *last;
17998   tree fn;
17999
18000   /* Create the function-declaration.  */
18001   fn = start_method (decl_specifiers, declarator, attributes);
18002   /* If something went badly wrong, bail out now.  */
18003   if (fn == error_mark_node)
18004     {
18005       /* If there's a function-body, skip it.  */
18006       if (cp_parser_token_starts_function_definition_p
18007           (cp_lexer_peek_token (parser->lexer)))
18008         cp_parser_skip_to_end_of_block_or_statement (parser);
18009       return error_mark_node;
18010     }
18011
18012   /* Remember it, if there default args to post process.  */
18013   cp_parser_save_default_args (parser, fn);
18014
18015   /* Save away the tokens that make up the body of the
18016      function.  */
18017   first = parser->lexer->next_token;
18018   /* We can have braced-init-list mem-initializers before the fn body.  */
18019   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18020     {
18021       cp_lexer_consume_token (parser->lexer);
18022       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18023              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18024         {
18025           /* cache_group will stop after an un-nested { } pair, too.  */
18026           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18027             break;
18028
18029           /* variadic mem-inits have ... after the ')'.  */
18030           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18031             cp_lexer_consume_token (parser->lexer);
18032         }
18033     }
18034   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18035   /* Handle function try blocks.  */
18036   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18037     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18038   last = parser->lexer->next_token;
18039
18040   /* Save away the inline definition; we will process it when the
18041      class is complete.  */
18042   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18043   DECL_PENDING_INLINE_P (fn) = 1;
18044
18045   /* We need to know that this was defined in the class, so that
18046      friend templates are handled correctly.  */
18047   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18048
18049   /* We're done with the inline definition.  */
18050   finish_method (fn);
18051
18052   /* Add FN to the queue of functions to be parsed later.  */
18053   TREE_VALUE (parser->unparsed_functions_queues)
18054     = tree_cons (NULL_TREE, fn,
18055                  TREE_VALUE (parser->unparsed_functions_queues));
18056
18057   return fn;
18058 }
18059
18060 /* Parse a template-argument-list, as well as the trailing ">" (but
18061    not the opening ">").  See cp_parser_template_argument_list for the
18062    return value.  */
18063
18064 static tree
18065 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18066 {
18067   tree arguments;
18068   tree saved_scope;
18069   tree saved_qualifying_scope;
18070   tree saved_object_scope;
18071   bool saved_greater_than_is_operator_p;
18072   bool saved_skip_evaluation;
18073
18074   /* [temp.names]
18075
18076      When parsing a template-id, the first non-nested `>' is taken as
18077      the end of the template-argument-list rather than a greater-than
18078      operator.  */
18079   saved_greater_than_is_operator_p
18080     = parser->greater_than_is_operator_p;
18081   parser->greater_than_is_operator_p = false;
18082   /* Parsing the argument list may modify SCOPE, so we save it
18083      here.  */
18084   saved_scope = parser->scope;
18085   saved_qualifying_scope = parser->qualifying_scope;
18086   saved_object_scope = parser->object_scope;
18087   /* We need to evaluate the template arguments, even though this
18088      template-id may be nested within a "sizeof".  */
18089   saved_skip_evaluation = skip_evaluation;
18090   skip_evaluation = false;
18091   /* Parse the template-argument-list itself.  */
18092   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18093       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18094     arguments = NULL_TREE;
18095   else
18096     arguments = cp_parser_template_argument_list (parser);
18097   /* Look for the `>' that ends the template-argument-list. If we find
18098      a '>>' instead, it's probably just a typo.  */
18099   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18100     {
18101       if (cxx_dialect != cxx98)
18102         {
18103           /* In C++0x, a `>>' in a template argument list or cast
18104              expression is considered to be two separate `>'
18105              tokens. So, change the current token to a `>', but don't
18106              consume it: it will be consumed later when the outer
18107              template argument list (or cast expression) is parsed.
18108              Note that this replacement of `>' for `>>' is necessary
18109              even if we are parsing tentatively: in the tentative
18110              case, after calling
18111              cp_parser_enclosed_template_argument_list we will always
18112              throw away all of the template arguments and the first
18113              closing `>', either because the template argument list
18114              was erroneous or because we are replacing those tokens
18115              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18116              not have been thrown away) is needed either to close an
18117              outer template argument list or to complete a new-style
18118              cast.  */
18119           cp_token *token = cp_lexer_peek_token (parser->lexer);
18120           token->type = CPP_GREATER;
18121         }
18122       else if (!saved_greater_than_is_operator_p)
18123         {
18124           /* If we're in a nested template argument list, the '>>' has
18125             to be a typo for '> >'. We emit the error message, but we
18126             continue parsing and we push a '>' as next token, so that
18127             the argument list will be parsed correctly.  Note that the
18128             global source location is still on the token before the
18129             '>>', so we need to say explicitly where we want it.  */
18130           cp_token *token = cp_lexer_peek_token (parser->lexer);
18131           error ("%H%<>>%> should be %<> >%> "
18132                  "within a nested template argument list",
18133                  &token->location);
18134
18135           token->type = CPP_GREATER;
18136         }
18137       else
18138         {
18139           /* If this is not a nested template argument list, the '>>'
18140             is a typo for '>'. Emit an error message and continue.
18141             Same deal about the token location, but here we can get it
18142             right by consuming the '>>' before issuing the diagnostic.  */
18143           cp_token *token = cp_lexer_consume_token (parser->lexer);
18144           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18145                  "a template argument list", &token->location);
18146         }
18147     }
18148   else
18149     cp_parser_skip_to_end_of_template_parameter_list (parser);
18150   /* The `>' token might be a greater-than operator again now.  */
18151   parser->greater_than_is_operator_p
18152     = saved_greater_than_is_operator_p;
18153   /* Restore the SAVED_SCOPE.  */
18154   parser->scope = saved_scope;
18155   parser->qualifying_scope = saved_qualifying_scope;
18156   parser->object_scope = saved_object_scope;
18157   skip_evaluation = saved_skip_evaluation;
18158
18159   return arguments;
18160 }
18161
18162 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18163    arguments, or the body of the function have not yet been parsed,
18164    parse them now.  */
18165
18166 static void
18167 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18168 {
18169   /* If this member is a template, get the underlying
18170      FUNCTION_DECL.  */
18171   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18172     member_function = DECL_TEMPLATE_RESULT (member_function);
18173
18174   /* There should not be any class definitions in progress at this
18175      point; the bodies of members are only parsed outside of all class
18176      definitions.  */
18177   gcc_assert (parser->num_classes_being_defined == 0);
18178   /* While we're parsing the member functions we might encounter more
18179      classes.  We want to handle them right away, but we don't want
18180      them getting mixed up with functions that are currently in the
18181      queue.  */
18182   parser->unparsed_functions_queues
18183     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18184
18185   /* Make sure that any template parameters are in scope.  */
18186   maybe_begin_member_template_processing (member_function);
18187
18188   /* If the body of the function has not yet been parsed, parse it
18189      now.  */
18190   if (DECL_PENDING_INLINE_P (member_function))
18191     {
18192       tree function_scope;
18193       cp_token_cache *tokens;
18194
18195       /* The function is no longer pending; we are processing it.  */
18196       tokens = DECL_PENDING_INLINE_INFO (member_function);
18197       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18198       DECL_PENDING_INLINE_P (member_function) = 0;
18199
18200       /* If this is a local class, enter the scope of the containing
18201          function.  */
18202       function_scope = current_function_decl;
18203       if (function_scope)
18204         push_function_context ();
18205
18206       /* Push the body of the function onto the lexer stack.  */
18207       cp_parser_push_lexer_for_tokens (parser, tokens);
18208
18209       /* Let the front end know that we going to be defining this
18210          function.  */
18211       start_preparsed_function (member_function, NULL_TREE,
18212                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18213
18214       /* Don't do access checking if it is a templated function.  */
18215       if (processing_template_decl)
18216         push_deferring_access_checks (dk_no_check);
18217
18218       /* Now, parse the body of the function.  */
18219       cp_parser_function_definition_after_declarator (parser,
18220                                                       /*inline_p=*/true);
18221
18222       if (processing_template_decl)
18223         pop_deferring_access_checks ();
18224
18225       /* Leave the scope of the containing function.  */
18226       if (function_scope)
18227         pop_function_context ();
18228       cp_parser_pop_lexer (parser);
18229     }
18230
18231   /* Remove any template parameters from the symbol table.  */
18232   maybe_end_member_template_processing ();
18233
18234   /* Restore the queue.  */
18235   parser->unparsed_functions_queues
18236     = TREE_CHAIN (parser->unparsed_functions_queues);
18237 }
18238
18239 /* If DECL contains any default args, remember it on the unparsed
18240    functions queue.  */
18241
18242 static void
18243 cp_parser_save_default_args (cp_parser* parser, tree decl)
18244 {
18245   tree probe;
18246
18247   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18248        probe;
18249        probe = TREE_CHAIN (probe))
18250     if (TREE_PURPOSE (probe))
18251       {
18252         TREE_PURPOSE (parser->unparsed_functions_queues)
18253           = tree_cons (current_class_type, decl,
18254                        TREE_PURPOSE (parser->unparsed_functions_queues));
18255         break;
18256       }
18257 }
18258
18259 /* FN is a FUNCTION_DECL which may contains a parameter with an
18260    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18261    assumes that the current scope is the scope in which the default
18262    argument should be processed.  */
18263
18264 static void
18265 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18266 {
18267   bool saved_local_variables_forbidden_p;
18268   tree parm;
18269
18270   /* While we're parsing the default args, we might (due to the
18271      statement expression extension) encounter more classes.  We want
18272      to handle them right away, but we don't want them getting mixed
18273      up with default args that are currently in the queue.  */
18274   parser->unparsed_functions_queues
18275     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18276
18277   /* Local variable names (and the `this' keyword) may not appear
18278      in a default argument.  */
18279   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18280   parser->local_variables_forbidden_p = true;
18281
18282   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18283        parm;
18284        parm = TREE_CHAIN (parm))
18285     {
18286       cp_token_cache *tokens;
18287       tree default_arg = TREE_PURPOSE (parm);
18288       tree parsed_arg;
18289       VEC(tree,gc) *insts;
18290       tree copy;
18291       unsigned ix;
18292
18293       if (!default_arg)
18294         continue;
18295
18296       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18297         /* This can happen for a friend declaration for a function
18298            already declared with default arguments.  */
18299         continue;
18300
18301        /* Push the saved tokens for the default argument onto the parser's
18302           lexer stack.  */
18303       tokens = DEFARG_TOKENS (default_arg);
18304       cp_parser_push_lexer_for_tokens (parser, tokens);
18305
18306       /* Parse the assignment-expression.  */
18307       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18308       if (parsed_arg == error_mark_node)
18309         {
18310           cp_parser_pop_lexer (parser);
18311           continue;
18312         }
18313
18314       if (!processing_template_decl)
18315         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18316
18317       TREE_PURPOSE (parm) = parsed_arg;
18318
18319       /* Update any instantiations we've already created.  */
18320       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18321            VEC_iterate (tree, insts, ix, copy); ix++)
18322         TREE_PURPOSE (copy) = parsed_arg;
18323
18324       /* If the token stream has not been completely used up, then
18325          there was extra junk after the end of the default
18326          argument.  */
18327       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18328         cp_parser_error (parser, "expected %<,%>");
18329
18330       /* Revert to the main lexer.  */
18331       cp_parser_pop_lexer (parser);
18332     }
18333
18334   /* Make sure no default arg is missing.  */
18335   check_default_args (fn);
18336
18337   /* Restore the state of local_variables_forbidden_p.  */
18338   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18339
18340   /* Restore the queue.  */
18341   parser->unparsed_functions_queues
18342     = TREE_CHAIN (parser->unparsed_functions_queues);
18343 }
18344
18345 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18346    either a TYPE or an expression, depending on the form of the
18347    input.  The KEYWORD indicates which kind of expression we have
18348    encountered.  */
18349
18350 static tree
18351 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18352 {
18353   tree expr = NULL_TREE;
18354   const char *saved_message;
18355   char *tmp;
18356   bool saved_integral_constant_expression_p;
18357   bool saved_non_integral_constant_expression_p;
18358   bool pack_expansion_p = false;
18359
18360   /* Types cannot be defined in a `sizeof' expression.  Save away the
18361      old message.  */
18362   saved_message = parser->type_definition_forbidden_message;
18363   /* And create the new one.  */
18364   tmp = concat ("types may not be defined in %<",
18365                 IDENTIFIER_POINTER (ridpointers[keyword]),
18366                 "%> expressions", NULL);
18367   parser->type_definition_forbidden_message = tmp;
18368
18369   /* The restrictions on constant-expressions do not apply inside
18370      sizeof expressions.  */
18371   saved_integral_constant_expression_p
18372     = parser->integral_constant_expression_p;
18373   saved_non_integral_constant_expression_p
18374     = parser->non_integral_constant_expression_p;
18375   parser->integral_constant_expression_p = false;
18376
18377   /* If it's a `...', then we are computing the length of a parameter
18378      pack.  */
18379   if (keyword == RID_SIZEOF
18380       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18381     {
18382       /* Consume the `...'.  */
18383       cp_lexer_consume_token (parser->lexer);
18384       maybe_warn_variadic_templates ();
18385
18386       /* Note that this is an expansion.  */
18387       pack_expansion_p = true;
18388     }
18389
18390   /* Do not actually evaluate the expression.  */
18391   ++skip_evaluation;
18392   /* If it's a `(', then we might be looking at the type-id
18393      construction.  */
18394   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18395     {
18396       tree type;
18397       bool saved_in_type_id_in_expr_p;
18398
18399       /* We can't be sure yet whether we're looking at a type-id or an
18400          expression.  */
18401       cp_parser_parse_tentatively (parser);
18402       /* Consume the `('.  */
18403       cp_lexer_consume_token (parser->lexer);
18404       /* Parse the type-id.  */
18405       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18406       parser->in_type_id_in_expr_p = true;
18407       type = cp_parser_type_id (parser);
18408       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18409       /* Now, look for the trailing `)'.  */
18410       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18411       /* If all went well, then we're done.  */
18412       if (cp_parser_parse_definitely (parser))
18413         {
18414           cp_decl_specifier_seq decl_specs;
18415
18416           /* Build a trivial decl-specifier-seq.  */
18417           clear_decl_specs (&decl_specs);
18418           decl_specs.type = type;
18419
18420           /* Call grokdeclarator to figure out what type this is.  */
18421           expr = grokdeclarator (NULL,
18422                                  &decl_specs,
18423                                  TYPENAME,
18424                                  /*initialized=*/0,
18425                                  /*attrlist=*/NULL);
18426         }
18427     }
18428
18429   /* If the type-id production did not work out, then we must be
18430      looking at the unary-expression production.  */
18431   if (!expr)
18432     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18433                                        /*cast_p=*/false, NULL);
18434
18435   if (pack_expansion_p)
18436     /* Build a pack expansion. */
18437     expr = make_pack_expansion (expr);
18438
18439   /* Go back to evaluating expressions.  */
18440   --skip_evaluation;
18441
18442   /* Free the message we created.  */
18443   free (tmp);
18444   /* And restore the old one.  */
18445   parser->type_definition_forbidden_message = saved_message;
18446   parser->integral_constant_expression_p
18447     = saved_integral_constant_expression_p;
18448   parser->non_integral_constant_expression_p
18449     = saved_non_integral_constant_expression_p;
18450
18451   return expr;
18452 }
18453
18454 /* If the current declaration has no declarator, return true.  */
18455
18456 static bool
18457 cp_parser_declares_only_class_p (cp_parser *parser)
18458 {
18459   /* If the next token is a `;' or a `,' then there is no
18460      declarator.  */
18461   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18462           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18463 }
18464
18465 /* Update the DECL_SPECS to reflect the storage class indicated by
18466    KEYWORD.  */
18467
18468 static void
18469 cp_parser_set_storage_class (cp_parser *parser,
18470                              cp_decl_specifier_seq *decl_specs,
18471                              enum rid keyword,
18472                              location_t location)
18473 {
18474   cp_storage_class storage_class;
18475
18476   if (parser->in_unbraced_linkage_specification_p)
18477     {
18478       error ("%Hinvalid use of %qD in linkage specification",
18479              &location, ridpointers[keyword]);
18480       return;
18481     }
18482   else if (decl_specs->storage_class != sc_none)
18483     {
18484       decl_specs->conflicting_specifiers_p = true;
18485       return;
18486     }
18487
18488   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18489       && decl_specs->specs[(int) ds_thread])
18490     {
18491       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18492       decl_specs->specs[(int) ds_thread] = 0;
18493     }
18494
18495   switch (keyword)
18496     {
18497     case RID_AUTO:
18498       storage_class = sc_auto;
18499       break;
18500     case RID_REGISTER:
18501       storage_class = sc_register;
18502       break;
18503     case RID_STATIC:
18504       storage_class = sc_static;
18505       break;
18506     case RID_EXTERN:
18507       storage_class = sc_extern;
18508       break;
18509     case RID_MUTABLE:
18510       storage_class = sc_mutable;
18511       break;
18512     default:
18513       gcc_unreachable ();
18514     }
18515   decl_specs->storage_class = storage_class;
18516
18517   /* A storage class specifier cannot be applied alongside a typedef 
18518      specifier. If there is a typedef specifier present then set 
18519      conflicting_specifiers_p which will trigger an error later
18520      on in grokdeclarator. */
18521   if (decl_specs->specs[(int)ds_typedef])
18522     decl_specs->conflicting_specifiers_p = true;
18523 }
18524
18525 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18526    is true, the type is a user-defined type; otherwise it is a
18527    built-in type specified by a keyword.  */
18528
18529 static void
18530 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18531                               tree type_spec,
18532                               location_t location,
18533                               bool user_defined_p)
18534 {
18535   decl_specs->any_specifiers_p = true;
18536
18537   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18538      (with, for example, in "typedef int wchar_t;") we remember that
18539      this is what happened.  In system headers, we ignore these
18540      declarations so that G++ can work with system headers that are not
18541      C++-safe.  */
18542   if (decl_specs->specs[(int) ds_typedef]
18543       && !user_defined_p
18544       && (type_spec == boolean_type_node
18545           || type_spec == char16_type_node
18546           || type_spec == char32_type_node
18547           || type_spec == wchar_type_node)
18548       && (decl_specs->type
18549           || decl_specs->specs[(int) ds_long]
18550           || decl_specs->specs[(int) ds_short]
18551           || decl_specs->specs[(int) ds_unsigned]
18552           || decl_specs->specs[(int) ds_signed]))
18553     {
18554       decl_specs->redefined_builtin_type = type_spec;
18555       if (!decl_specs->type)
18556         {
18557           decl_specs->type = type_spec;
18558           decl_specs->user_defined_type_p = false;
18559           decl_specs->type_location = location;
18560         }
18561     }
18562   else if (decl_specs->type)
18563     decl_specs->multiple_types_p = true;
18564   else
18565     {
18566       decl_specs->type = type_spec;
18567       decl_specs->user_defined_type_p = user_defined_p;
18568       decl_specs->redefined_builtin_type = NULL_TREE;
18569       decl_specs->type_location = location;
18570     }
18571 }
18572
18573 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18574    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18575
18576 static bool
18577 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18578 {
18579   return decl_specifiers->specs[(int) ds_friend] != 0;
18580 }
18581
18582 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18583    issue an error message indicating that TOKEN_DESC was expected.
18584
18585    Returns the token consumed, if the token had the appropriate type.
18586    Otherwise, returns NULL.  */
18587
18588 static cp_token *
18589 cp_parser_require (cp_parser* parser,
18590                    enum cpp_ttype type,
18591                    const char* token_desc)
18592 {
18593   if (cp_lexer_next_token_is (parser->lexer, type))
18594     return cp_lexer_consume_token (parser->lexer);
18595   else
18596     {
18597       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18598       if (!cp_parser_simulate_error (parser))
18599         {
18600           char *message = concat ("expected ", token_desc, NULL);
18601           cp_parser_error (parser, message);
18602           free (message);
18603         }
18604       return NULL;
18605     }
18606 }
18607
18608 /* An error message is produced if the next token is not '>'.
18609    All further tokens are skipped until the desired token is
18610    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18611
18612 static void
18613 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18614 {
18615   /* Current level of '< ... >'.  */
18616   unsigned level = 0;
18617   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18618   unsigned nesting_depth = 0;
18619
18620   /* Are we ready, yet?  If not, issue error message.  */
18621   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18622     return;
18623
18624   /* Skip tokens until the desired token is found.  */
18625   while (true)
18626     {
18627       /* Peek at the next token.  */
18628       switch (cp_lexer_peek_token (parser->lexer)->type)
18629         {
18630         case CPP_LESS:
18631           if (!nesting_depth)
18632             ++level;
18633           break;
18634
18635         case CPP_RSHIFT:
18636           if (cxx_dialect == cxx98)
18637             /* C++0x views the `>>' operator as two `>' tokens, but
18638                C++98 does not. */
18639             break;
18640           else if (!nesting_depth && level-- == 0)
18641             {
18642               /* We've hit a `>>' where the first `>' closes the
18643                  template argument list, and the second `>' is
18644                  spurious.  Just consume the `>>' and stop; we've
18645                  already produced at least one error.  */
18646               cp_lexer_consume_token (parser->lexer);
18647               return;
18648             }
18649           /* Fall through for C++0x, so we handle the second `>' in
18650              the `>>'.  */
18651
18652         case CPP_GREATER:
18653           if (!nesting_depth && level-- == 0)
18654             {
18655               /* We've reached the token we want, consume it and stop.  */
18656               cp_lexer_consume_token (parser->lexer);
18657               return;
18658             }
18659           break;
18660
18661         case CPP_OPEN_PAREN:
18662         case CPP_OPEN_SQUARE:
18663           ++nesting_depth;
18664           break;
18665
18666         case CPP_CLOSE_PAREN:
18667         case CPP_CLOSE_SQUARE:
18668           if (nesting_depth-- == 0)
18669             return;
18670           break;
18671
18672         case CPP_EOF:
18673         case CPP_PRAGMA_EOL:
18674         case CPP_SEMICOLON:
18675         case CPP_OPEN_BRACE:
18676         case CPP_CLOSE_BRACE:
18677           /* The '>' was probably forgotten, don't look further.  */
18678           return;
18679
18680         default:
18681           break;
18682         }
18683
18684       /* Consume this token.  */
18685       cp_lexer_consume_token (parser->lexer);
18686     }
18687 }
18688
18689 /* If the next token is the indicated keyword, consume it.  Otherwise,
18690    issue an error message indicating that TOKEN_DESC was expected.
18691
18692    Returns the token consumed, if the token had the appropriate type.
18693    Otherwise, returns NULL.  */
18694
18695 static cp_token *
18696 cp_parser_require_keyword (cp_parser* parser,
18697                            enum rid keyword,
18698                            const char* token_desc)
18699 {
18700   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18701
18702   if (token && token->keyword != keyword)
18703     {
18704       dyn_string_t error_msg;
18705
18706       /* Format the error message.  */
18707       error_msg = dyn_string_new (0);
18708       dyn_string_append_cstr (error_msg, "expected ");
18709       dyn_string_append_cstr (error_msg, token_desc);
18710       cp_parser_error (parser, error_msg->s);
18711       dyn_string_delete (error_msg);
18712       return NULL;
18713     }
18714
18715   return token;
18716 }
18717
18718 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18719    function-definition.  */
18720
18721 static bool
18722 cp_parser_token_starts_function_definition_p (cp_token* token)
18723 {
18724   return (/* An ordinary function-body begins with an `{'.  */
18725           token->type == CPP_OPEN_BRACE
18726           /* A ctor-initializer begins with a `:'.  */
18727           || token->type == CPP_COLON
18728           /* A function-try-block begins with `try'.  */
18729           || token->keyword == RID_TRY
18730           /* The named return value extension begins with `return'.  */
18731           || token->keyword == RID_RETURN);
18732 }
18733
18734 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18735    definition.  */
18736
18737 static bool
18738 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18739 {
18740   cp_token *token;
18741
18742   token = cp_lexer_peek_token (parser->lexer);
18743   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18744 }
18745
18746 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18747    C++0x) ending a template-argument.  */
18748
18749 static bool
18750 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18751 {
18752   cp_token *token;
18753
18754   token = cp_lexer_peek_token (parser->lexer);
18755   return (token->type == CPP_COMMA 
18756           || token->type == CPP_GREATER
18757           || token->type == CPP_ELLIPSIS
18758           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18759 }
18760
18761 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18762    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18763
18764 static bool
18765 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18766                                                      size_t n)
18767 {
18768   cp_token *token;
18769
18770   token = cp_lexer_peek_nth_token (parser->lexer, n);
18771   if (token->type == CPP_LESS)
18772     return true;
18773   /* Check for the sequence `<::' in the original code. It would be lexed as
18774      `[:', where `[' is a digraph, and there is no whitespace before
18775      `:'.  */
18776   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18777     {
18778       cp_token *token2;
18779       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18780       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18781         return true;
18782     }
18783   return false;
18784 }
18785
18786 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18787    or none_type otherwise.  */
18788
18789 static enum tag_types
18790 cp_parser_token_is_class_key (cp_token* token)
18791 {
18792   switch (token->keyword)
18793     {
18794     case RID_CLASS:
18795       return class_type;
18796     case RID_STRUCT:
18797       return record_type;
18798     case RID_UNION:
18799       return union_type;
18800
18801     default:
18802       return none_type;
18803     }
18804 }
18805
18806 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18807
18808 static void
18809 cp_parser_check_class_key (enum tag_types class_key, tree type)
18810 {
18811   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18812     permerror (input_location, "%qs tag used in naming %q#T",
18813             class_key == union_type ? "union"
18814              : class_key == record_type ? "struct" : "class",
18815              type);
18816 }
18817
18818 /* Issue an error message if DECL is redeclared with different
18819    access than its original declaration [class.access.spec/3].
18820    This applies to nested classes and nested class templates.
18821    [class.mem/1].  */
18822
18823 static void
18824 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18825 {
18826   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18827     return;
18828
18829   if ((TREE_PRIVATE (decl)
18830        != (current_access_specifier == access_private_node))
18831       || (TREE_PROTECTED (decl)
18832           != (current_access_specifier == access_protected_node)))
18833     error ("%H%qD redeclared with different access", &location, decl);
18834 }
18835
18836 /* Look for the `template' keyword, as a syntactic disambiguator.
18837    Return TRUE iff it is present, in which case it will be
18838    consumed.  */
18839
18840 static bool
18841 cp_parser_optional_template_keyword (cp_parser *parser)
18842 {
18843   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18844     {
18845       /* The `template' keyword can only be used within templates;
18846          outside templates the parser can always figure out what is a
18847          template and what is not.  */
18848       if (!processing_template_decl)
18849         {
18850           cp_token *token = cp_lexer_peek_token (parser->lexer);
18851           error ("%H%<template%> (as a disambiguator) is only allowed "
18852                  "within templates", &token->location);
18853           /* If this part of the token stream is rescanned, the same
18854              error message would be generated.  So, we purge the token
18855              from the stream.  */
18856           cp_lexer_purge_token (parser->lexer);
18857           return false;
18858         }
18859       else
18860         {
18861           /* Consume the `template' keyword.  */
18862           cp_lexer_consume_token (parser->lexer);
18863           return true;
18864         }
18865     }
18866
18867   return false;
18868 }
18869
18870 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18871    set PARSER->SCOPE, and perform other related actions.  */
18872
18873 static void
18874 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18875 {
18876   int i;
18877   struct tree_check *check_value;
18878   deferred_access_check *chk;
18879   VEC (deferred_access_check,gc) *checks;
18880
18881   /* Get the stored value.  */
18882   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18883   /* Perform any access checks that were deferred.  */
18884   checks = check_value->checks;
18885   if (checks)
18886     {
18887       for (i = 0 ;
18888            VEC_iterate (deferred_access_check, checks, i, chk) ;
18889            ++i)
18890         {
18891           perform_or_defer_access_check (chk->binfo,
18892                                          chk->decl,
18893                                          chk->diag_decl);
18894         }
18895     }
18896   /* Set the scope from the stored value.  */
18897   parser->scope = check_value->value;
18898   parser->qualifying_scope = check_value->qualifying_scope;
18899   parser->object_scope = NULL_TREE;
18900 }
18901
18902 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18903    encounter the end of a block before what we were looking for.  */
18904
18905 static bool
18906 cp_parser_cache_group (cp_parser *parser,
18907                        enum cpp_ttype end,
18908                        unsigned depth)
18909 {
18910   while (true)
18911     {
18912       cp_token *token = cp_lexer_peek_token (parser->lexer);
18913
18914       /* Abort a parenthesized expression if we encounter a semicolon.  */
18915       if ((end == CPP_CLOSE_PAREN || depth == 0)
18916           && token->type == CPP_SEMICOLON)
18917         return true;
18918       /* If we've reached the end of the file, stop.  */
18919       if (token->type == CPP_EOF
18920           || (end != CPP_PRAGMA_EOL
18921               && token->type == CPP_PRAGMA_EOL))
18922         return true;
18923       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18924         /* We've hit the end of an enclosing block, so there's been some
18925            kind of syntax error.  */
18926         return true;
18927
18928       /* Consume the token.  */
18929       cp_lexer_consume_token (parser->lexer);
18930       /* See if it starts a new group.  */
18931       if (token->type == CPP_OPEN_BRACE)
18932         {
18933           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18934           /* In theory this should probably check end == '}', but
18935              cp_parser_save_member_function_body needs it to exit
18936              after either '}' or ')' when called with ')'.  */
18937           if (depth == 0)
18938             return false;
18939         }
18940       else if (token->type == CPP_OPEN_PAREN)
18941         {
18942           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18943           if (depth == 0 && end == CPP_CLOSE_PAREN)
18944             return false;
18945         }
18946       else if (token->type == CPP_PRAGMA)
18947         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18948       else if (token->type == end)
18949         return false;
18950     }
18951 }
18952
18953 /* Begin parsing tentatively.  We always save tokens while parsing
18954    tentatively so that if the tentative parsing fails we can restore the
18955    tokens.  */
18956
18957 static void
18958 cp_parser_parse_tentatively (cp_parser* parser)
18959 {
18960   /* Enter a new parsing context.  */
18961   parser->context = cp_parser_context_new (parser->context);
18962   /* Begin saving tokens.  */
18963   cp_lexer_save_tokens (parser->lexer);
18964   /* In order to avoid repetitive access control error messages,
18965      access checks are queued up until we are no longer parsing
18966      tentatively.  */
18967   push_deferring_access_checks (dk_deferred);
18968 }
18969
18970 /* Commit to the currently active tentative parse.  */
18971
18972 static void
18973 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18974 {
18975   cp_parser_context *context;
18976   cp_lexer *lexer;
18977
18978   /* Mark all of the levels as committed.  */
18979   lexer = parser->lexer;
18980   for (context = parser->context; context->next; context = context->next)
18981     {
18982       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18983         break;
18984       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18985       while (!cp_lexer_saving_tokens (lexer))
18986         lexer = lexer->next;
18987       cp_lexer_commit_tokens (lexer);
18988     }
18989 }
18990
18991 /* Abort the currently active tentative parse.  All consumed tokens
18992    will be rolled back, and no diagnostics will be issued.  */
18993
18994 static void
18995 cp_parser_abort_tentative_parse (cp_parser* parser)
18996 {
18997   cp_parser_simulate_error (parser);
18998   /* Now, pretend that we want to see if the construct was
18999      successfully parsed.  */
19000   cp_parser_parse_definitely (parser);
19001 }
19002
19003 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19004    token stream.  Otherwise, commit to the tokens we have consumed.
19005    Returns true if no error occurred; false otherwise.  */
19006
19007 static bool
19008 cp_parser_parse_definitely (cp_parser* parser)
19009 {
19010   bool error_occurred;
19011   cp_parser_context *context;
19012
19013   /* Remember whether or not an error occurred, since we are about to
19014      destroy that information.  */
19015   error_occurred = cp_parser_error_occurred (parser);
19016   /* Remove the topmost context from the stack.  */
19017   context = parser->context;
19018   parser->context = context->next;
19019   /* If no parse errors occurred, commit to the tentative parse.  */
19020   if (!error_occurred)
19021     {
19022       /* Commit to the tokens read tentatively, unless that was
19023          already done.  */
19024       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19025         cp_lexer_commit_tokens (parser->lexer);
19026
19027       pop_to_parent_deferring_access_checks ();
19028     }
19029   /* Otherwise, if errors occurred, roll back our state so that things
19030      are just as they were before we began the tentative parse.  */
19031   else
19032     {
19033       cp_lexer_rollback_tokens (parser->lexer);
19034       pop_deferring_access_checks ();
19035     }
19036   /* Add the context to the front of the free list.  */
19037   context->next = cp_parser_context_free_list;
19038   cp_parser_context_free_list = context;
19039
19040   return !error_occurred;
19041 }
19042
19043 /* Returns true if we are parsing tentatively and are not committed to
19044    this tentative parse.  */
19045
19046 static bool
19047 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19048 {
19049   return (cp_parser_parsing_tentatively (parser)
19050           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19051 }
19052
19053 /* Returns nonzero iff an error has occurred during the most recent
19054    tentative parse.  */
19055
19056 static bool
19057 cp_parser_error_occurred (cp_parser* parser)
19058 {
19059   return (cp_parser_parsing_tentatively (parser)
19060           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19061 }
19062
19063 /* Returns nonzero if GNU extensions are allowed.  */
19064
19065 static bool
19066 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19067 {
19068   return parser->allow_gnu_extensions_p;
19069 }
19070 \f
19071 /* Objective-C++ Productions */
19072
19073
19074 /* Parse an Objective-C expression, which feeds into a primary-expression
19075    above.
19076
19077    objc-expression:
19078      objc-message-expression
19079      objc-string-literal
19080      objc-encode-expression
19081      objc-protocol-expression
19082      objc-selector-expression
19083
19084   Returns a tree representation of the expression.  */
19085
19086 static tree
19087 cp_parser_objc_expression (cp_parser* parser)
19088 {
19089   /* Try to figure out what kind of declaration is present.  */
19090   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19091
19092   switch (kwd->type)
19093     {
19094     case CPP_OPEN_SQUARE:
19095       return cp_parser_objc_message_expression (parser);
19096
19097     case CPP_OBJC_STRING:
19098       kwd = cp_lexer_consume_token (parser->lexer);
19099       return objc_build_string_object (kwd->u.value);
19100
19101     case CPP_KEYWORD:
19102       switch (kwd->keyword)
19103         {
19104         case RID_AT_ENCODE:
19105           return cp_parser_objc_encode_expression (parser);
19106
19107         case RID_AT_PROTOCOL:
19108           return cp_parser_objc_protocol_expression (parser);
19109
19110         case RID_AT_SELECTOR:
19111           return cp_parser_objc_selector_expression (parser);
19112
19113         default:
19114           break;
19115         }
19116     default:
19117       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19118              &kwd->location, kwd->u.value);
19119       cp_parser_skip_to_end_of_block_or_statement (parser);
19120     }
19121
19122   return error_mark_node;
19123 }
19124
19125 /* Parse an Objective-C message expression.
19126
19127    objc-message-expression:
19128      [ objc-message-receiver objc-message-args ]
19129
19130    Returns a representation of an Objective-C message.  */
19131
19132 static tree
19133 cp_parser_objc_message_expression (cp_parser* parser)
19134 {
19135   tree receiver, messageargs;
19136
19137   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19138   receiver = cp_parser_objc_message_receiver (parser);
19139   messageargs = cp_parser_objc_message_args (parser);
19140   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19141
19142   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19143 }
19144
19145 /* Parse an objc-message-receiver.
19146
19147    objc-message-receiver:
19148      expression
19149      simple-type-specifier
19150
19151   Returns a representation of the type or expression.  */
19152
19153 static tree
19154 cp_parser_objc_message_receiver (cp_parser* parser)
19155 {
19156   tree rcv;
19157
19158   /* An Objective-C message receiver may be either (1) a type
19159      or (2) an expression.  */
19160   cp_parser_parse_tentatively (parser);
19161   rcv = cp_parser_expression (parser, false, NULL);
19162
19163   if (cp_parser_parse_definitely (parser))
19164     return rcv;
19165
19166   rcv = cp_parser_simple_type_specifier (parser,
19167                                          /*decl_specs=*/NULL,
19168                                          CP_PARSER_FLAGS_NONE);
19169
19170   return objc_get_class_reference (rcv);
19171 }
19172
19173 /* Parse the arguments and selectors comprising an Objective-C message.
19174
19175    objc-message-args:
19176      objc-selector
19177      objc-selector-args
19178      objc-selector-args , objc-comma-args
19179
19180    objc-selector-args:
19181      objc-selector [opt] : assignment-expression
19182      objc-selector-args objc-selector [opt] : assignment-expression
19183
19184    objc-comma-args:
19185      assignment-expression
19186      objc-comma-args , assignment-expression
19187
19188    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19189    selector arguments and TREE_VALUE containing a list of comma
19190    arguments.  */
19191
19192 static tree
19193 cp_parser_objc_message_args (cp_parser* parser)
19194 {
19195   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19196   bool maybe_unary_selector_p = true;
19197   cp_token *token = cp_lexer_peek_token (parser->lexer);
19198
19199   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19200     {
19201       tree selector = NULL_TREE, arg;
19202
19203       if (token->type != CPP_COLON)
19204         selector = cp_parser_objc_selector (parser);
19205
19206       /* Detect if we have a unary selector.  */
19207       if (maybe_unary_selector_p
19208           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19209         return build_tree_list (selector, NULL_TREE);
19210
19211       maybe_unary_selector_p = false;
19212       cp_parser_require (parser, CPP_COLON, "%<:%>");
19213       arg = cp_parser_assignment_expression (parser, false, NULL);
19214
19215       sel_args
19216         = chainon (sel_args,
19217                    build_tree_list (selector, arg));
19218
19219       token = cp_lexer_peek_token (parser->lexer);
19220     }
19221
19222   /* Handle non-selector arguments, if any. */
19223   while (token->type == CPP_COMMA)
19224     {
19225       tree arg;
19226
19227       cp_lexer_consume_token (parser->lexer);
19228       arg = cp_parser_assignment_expression (parser, false, NULL);
19229
19230       addl_args
19231         = chainon (addl_args,
19232                    build_tree_list (NULL_TREE, arg));
19233
19234       token = cp_lexer_peek_token (parser->lexer);
19235     }
19236
19237   return build_tree_list (sel_args, addl_args);
19238 }
19239
19240 /* Parse an Objective-C encode expression.
19241
19242    objc-encode-expression:
19243      @encode objc-typename
19244
19245    Returns an encoded representation of the type argument.  */
19246
19247 static tree
19248 cp_parser_objc_encode_expression (cp_parser* parser)
19249 {
19250   tree type;
19251   cp_token *token;
19252
19253   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19254   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19255   token = cp_lexer_peek_token (parser->lexer);
19256   type = complete_type (cp_parser_type_id (parser));
19257   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19258
19259   if (!type)
19260     {
19261       error ("%H%<@encode%> must specify a type as an argument",
19262              &token->location);
19263       return error_mark_node;
19264     }
19265
19266   return objc_build_encode_expr (type);
19267 }
19268
19269 /* Parse an Objective-C @defs expression.  */
19270
19271 static tree
19272 cp_parser_objc_defs_expression (cp_parser *parser)
19273 {
19274   tree name;
19275
19276   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19277   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19278   name = cp_parser_identifier (parser);
19279   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19280
19281   return objc_get_class_ivars (name);
19282 }
19283
19284 /* Parse an Objective-C protocol expression.
19285
19286   objc-protocol-expression:
19287     @protocol ( identifier )
19288
19289   Returns a representation of the protocol expression.  */
19290
19291 static tree
19292 cp_parser_objc_protocol_expression (cp_parser* parser)
19293 {
19294   tree proto;
19295
19296   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19297   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19298   proto = cp_parser_identifier (parser);
19299   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19300
19301   return objc_build_protocol_expr (proto);
19302 }
19303
19304 /* Parse an Objective-C selector expression.
19305
19306    objc-selector-expression:
19307      @selector ( objc-method-signature )
19308
19309    objc-method-signature:
19310      objc-selector
19311      objc-selector-seq
19312
19313    objc-selector-seq:
19314      objc-selector :
19315      objc-selector-seq objc-selector :
19316
19317   Returns a representation of the method selector.  */
19318
19319 static tree
19320 cp_parser_objc_selector_expression (cp_parser* parser)
19321 {
19322   tree sel_seq = NULL_TREE;
19323   bool maybe_unary_selector_p = true;
19324   cp_token *token;
19325
19326   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19327   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19328   token = cp_lexer_peek_token (parser->lexer);
19329
19330   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19331          || token->type == CPP_SCOPE)
19332     {
19333       tree selector = NULL_TREE;
19334
19335       if (token->type != CPP_COLON
19336           || token->type == CPP_SCOPE)
19337         selector = cp_parser_objc_selector (parser);
19338
19339       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19340           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19341         {
19342           /* Detect if we have a unary selector.  */
19343           if (maybe_unary_selector_p)
19344             {
19345               sel_seq = selector;
19346               goto finish_selector;
19347             }
19348           else
19349             {
19350               cp_parser_error (parser, "expected %<:%>");
19351             }
19352         }
19353       maybe_unary_selector_p = false;
19354       token = cp_lexer_consume_token (parser->lexer);
19355
19356       if (token->type == CPP_SCOPE)
19357         {
19358           sel_seq
19359             = chainon (sel_seq,
19360                        build_tree_list (selector, NULL_TREE));
19361           sel_seq
19362             = chainon (sel_seq,
19363                        build_tree_list (NULL_TREE, NULL_TREE));
19364         }
19365       else
19366         sel_seq
19367           = chainon (sel_seq,
19368                      build_tree_list (selector, NULL_TREE));
19369
19370       token = cp_lexer_peek_token (parser->lexer);
19371     }
19372
19373  finish_selector:
19374   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19375
19376   return objc_build_selector_expr (sel_seq);
19377 }
19378
19379 /* Parse a list of identifiers.
19380
19381    objc-identifier-list:
19382      identifier
19383      objc-identifier-list , identifier
19384
19385    Returns a TREE_LIST of identifier nodes.  */
19386
19387 static tree
19388 cp_parser_objc_identifier_list (cp_parser* parser)
19389 {
19390   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19391   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19392
19393   while (sep->type == CPP_COMMA)
19394     {
19395       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19396       list = chainon (list,
19397                       build_tree_list (NULL_TREE,
19398                                        cp_parser_identifier (parser)));
19399       sep = cp_lexer_peek_token (parser->lexer);
19400     }
19401
19402   return list;
19403 }
19404
19405 /* Parse an Objective-C alias declaration.
19406
19407    objc-alias-declaration:
19408      @compatibility_alias identifier identifier ;
19409
19410    This function registers the alias mapping with the Objective-C front end.
19411    It returns nothing.  */
19412
19413 static void
19414 cp_parser_objc_alias_declaration (cp_parser* parser)
19415 {
19416   tree alias, orig;
19417
19418   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19419   alias = cp_parser_identifier (parser);
19420   orig = cp_parser_identifier (parser);
19421   objc_declare_alias (alias, orig);
19422   cp_parser_consume_semicolon_at_end_of_statement (parser);
19423 }
19424
19425 /* Parse an Objective-C class forward-declaration.
19426
19427    objc-class-declaration:
19428      @class objc-identifier-list ;
19429
19430    The function registers the forward declarations with the Objective-C
19431    front end.  It returns nothing.  */
19432
19433 static void
19434 cp_parser_objc_class_declaration (cp_parser* parser)
19435 {
19436   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19437   objc_declare_class (cp_parser_objc_identifier_list (parser));
19438   cp_parser_consume_semicolon_at_end_of_statement (parser);
19439 }
19440
19441 /* Parse a list of Objective-C protocol references.
19442
19443    objc-protocol-refs-opt:
19444      objc-protocol-refs [opt]
19445
19446    objc-protocol-refs:
19447      < objc-identifier-list >
19448
19449    Returns a TREE_LIST of identifiers, if any.  */
19450
19451 static tree
19452 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19453 {
19454   tree protorefs = NULL_TREE;
19455
19456   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19457     {
19458       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19459       protorefs = cp_parser_objc_identifier_list (parser);
19460       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19461     }
19462
19463   return protorefs;
19464 }
19465
19466 /* Parse a Objective-C visibility specification.  */
19467
19468 static void
19469 cp_parser_objc_visibility_spec (cp_parser* parser)
19470 {
19471   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19472
19473   switch (vis->keyword)
19474     {
19475     case RID_AT_PRIVATE:
19476       objc_set_visibility (2);
19477       break;
19478     case RID_AT_PROTECTED:
19479       objc_set_visibility (0);
19480       break;
19481     case RID_AT_PUBLIC:
19482       objc_set_visibility (1);
19483       break;
19484     default:
19485       return;
19486     }
19487
19488   /* Eat '@private'/'@protected'/'@public'.  */
19489   cp_lexer_consume_token (parser->lexer);
19490 }
19491
19492 /* Parse an Objective-C method type.  */
19493
19494 static void
19495 cp_parser_objc_method_type (cp_parser* parser)
19496 {
19497   objc_set_method_type
19498    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19499     ? PLUS_EXPR
19500     : MINUS_EXPR);
19501 }
19502
19503 /* Parse an Objective-C protocol qualifier.  */
19504
19505 static tree
19506 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19507 {
19508   tree quals = NULL_TREE, node;
19509   cp_token *token = cp_lexer_peek_token (parser->lexer);
19510
19511   node = token->u.value;
19512
19513   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19514          && (node == ridpointers [(int) RID_IN]
19515              || node == ridpointers [(int) RID_OUT]
19516              || node == ridpointers [(int) RID_INOUT]
19517              || node == ridpointers [(int) RID_BYCOPY]
19518              || node == ridpointers [(int) RID_BYREF]
19519              || node == ridpointers [(int) RID_ONEWAY]))
19520     {
19521       quals = tree_cons (NULL_TREE, node, quals);
19522       cp_lexer_consume_token (parser->lexer);
19523       token = cp_lexer_peek_token (parser->lexer);
19524       node = token->u.value;
19525     }
19526
19527   return quals;
19528 }
19529
19530 /* Parse an Objective-C typename.  */
19531
19532 static tree
19533 cp_parser_objc_typename (cp_parser* parser)
19534 {
19535   tree type_name = NULL_TREE;
19536
19537   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19538     {
19539       tree proto_quals, cp_type = NULL_TREE;
19540
19541       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19542       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19543
19544       /* An ObjC type name may consist of just protocol qualifiers, in which
19545          case the type shall default to 'id'.  */
19546       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19547         cp_type = cp_parser_type_id (parser);
19548
19549       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19550       type_name = build_tree_list (proto_quals, cp_type);
19551     }
19552
19553   return type_name;
19554 }
19555
19556 /* Check to see if TYPE refers to an Objective-C selector name.  */
19557
19558 static bool
19559 cp_parser_objc_selector_p (enum cpp_ttype type)
19560 {
19561   return (type == CPP_NAME || type == CPP_KEYWORD
19562           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19563           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19564           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19565           || type == CPP_XOR || type == CPP_XOR_EQ);
19566 }
19567
19568 /* Parse an Objective-C selector.  */
19569
19570 static tree
19571 cp_parser_objc_selector (cp_parser* parser)
19572 {
19573   cp_token *token = cp_lexer_consume_token (parser->lexer);
19574
19575   if (!cp_parser_objc_selector_p (token->type))
19576     {
19577       error ("%Hinvalid Objective-C++ selector name", &token->location);
19578       return error_mark_node;
19579     }
19580
19581   /* C++ operator names are allowed to appear in ObjC selectors.  */
19582   switch (token->type)
19583     {
19584     case CPP_AND_AND: return get_identifier ("and");
19585     case CPP_AND_EQ: return get_identifier ("and_eq");
19586     case CPP_AND: return get_identifier ("bitand");
19587     case CPP_OR: return get_identifier ("bitor");
19588     case CPP_COMPL: return get_identifier ("compl");
19589     case CPP_NOT: return get_identifier ("not");
19590     case CPP_NOT_EQ: return get_identifier ("not_eq");
19591     case CPP_OR_OR: return get_identifier ("or");
19592     case CPP_OR_EQ: return get_identifier ("or_eq");
19593     case CPP_XOR: return get_identifier ("xor");
19594     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19595     default: return token->u.value;
19596     }
19597 }
19598
19599 /* Parse an Objective-C params list.  */
19600
19601 static tree
19602 cp_parser_objc_method_keyword_params (cp_parser* parser)
19603 {
19604   tree params = NULL_TREE;
19605   bool maybe_unary_selector_p = true;
19606   cp_token *token = cp_lexer_peek_token (parser->lexer);
19607
19608   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19609     {
19610       tree selector = NULL_TREE, type_name, identifier;
19611
19612       if (token->type != CPP_COLON)
19613         selector = cp_parser_objc_selector (parser);
19614
19615       /* Detect if we have a unary selector.  */
19616       if (maybe_unary_selector_p
19617           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19618         return selector;
19619
19620       maybe_unary_selector_p = false;
19621       cp_parser_require (parser, CPP_COLON, "%<:%>");
19622       type_name = cp_parser_objc_typename (parser);
19623       identifier = cp_parser_identifier (parser);
19624
19625       params
19626         = chainon (params,
19627                    objc_build_keyword_decl (selector,
19628                                             type_name,
19629                                             identifier));
19630
19631       token = cp_lexer_peek_token (parser->lexer);
19632     }
19633
19634   return params;
19635 }
19636
19637 /* Parse the non-keyword Objective-C params.  */
19638
19639 static tree
19640 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19641 {
19642   tree params = make_node (TREE_LIST);
19643   cp_token *token = cp_lexer_peek_token (parser->lexer);
19644   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19645
19646   while (token->type == CPP_COMMA)
19647     {
19648       cp_parameter_declarator *parmdecl;
19649       tree parm;
19650
19651       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19652       token = cp_lexer_peek_token (parser->lexer);
19653
19654       if (token->type == CPP_ELLIPSIS)
19655         {
19656           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19657           *ellipsisp = true;
19658           break;
19659         }
19660
19661       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19662       parm = grokdeclarator (parmdecl->declarator,
19663                              &parmdecl->decl_specifiers,
19664                              PARM, /*initialized=*/0,
19665                              /*attrlist=*/NULL);
19666
19667       chainon (params, build_tree_list (NULL_TREE, parm));
19668       token = cp_lexer_peek_token (parser->lexer);
19669     }
19670
19671   return params;
19672 }
19673
19674 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19675
19676 static void
19677 cp_parser_objc_interstitial_code (cp_parser* parser)
19678 {
19679   cp_token *token = cp_lexer_peek_token (parser->lexer);
19680
19681   /* If the next token is `extern' and the following token is a string
19682      literal, then we have a linkage specification.  */
19683   if (token->keyword == RID_EXTERN
19684       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19685     cp_parser_linkage_specification (parser);
19686   /* Handle #pragma, if any.  */
19687   else if (token->type == CPP_PRAGMA)
19688     cp_parser_pragma (parser, pragma_external);
19689   /* Allow stray semicolons.  */
19690   else if (token->type == CPP_SEMICOLON)
19691     cp_lexer_consume_token (parser->lexer);
19692   /* Finally, try to parse a block-declaration, or a function-definition.  */
19693   else
19694     cp_parser_block_declaration (parser, /*statement_p=*/false);
19695 }
19696
19697 /* Parse a method signature.  */
19698
19699 static tree
19700 cp_parser_objc_method_signature (cp_parser* parser)
19701 {
19702   tree rettype, kwdparms, optparms;
19703   bool ellipsis = false;
19704
19705   cp_parser_objc_method_type (parser);
19706   rettype = cp_parser_objc_typename (parser);
19707   kwdparms = cp_parser_objc_method_keyword_params (parser);
19708   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19709
19710   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19711 }
19712
19713 /* Pars an Objective-C method prototype list.  */
19714
19715 static void
19716 cp_parser_objc_method_prototype_list (cp_parser* parser)
19717 {
19718   cp_token *token = cp_lexer_peek_token (parser->lexer);
19719
19720   while (token->keyword != RID_AT_END)
19721     {
19722       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19723         {
19724           objc_add_method_declaration
19725            (cp_parser_objc_method_signature (parser));
19726           cp_parser_consume_semicolon_at_end_of_statement (parser);
19727         }
19728       else
19729         /* Allow for interspersed non-ObjC++ code.  */
19730         cp_parser_objc_interstitial_code (parser);
19731
19732       token = cp_lexer_peek_token (parser->lexer);
19733     }
19734
19735   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19736   objc_finish_interface ();
19737 }
19738
19739 /* Parse an Objective-C method definition list.  */
19740
19741 static void
19742 cp_parser_objc_method_definition_list (cp_parser* parser)
19743 {
19744   cp_token *token = cp_lexer_peek_token (parser->lexer);
19745
19746   while (token->keyword != RID_AT_END)
19747     {
19748       tree meth;
19749
19750       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19751         {
19752           push_deferring_access_checks (dk_deferred);
19753           objc_start_method_definition
19754            (cp_parser_objc_method_signature (parser));
19755
19756           /* For historical reasons, we accept an optional semicolon.  */
19757           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19758             cp_lexer_consume_token (parser->lexer);
19759
19760           perform_deferred_access_checks ();
19761           stop_deferring_access_checks ();
19762           meth = cp_parser_function_definition_after_declarator (parser,
19763                                                                  false);
19764           pop_deferring_access_checks ();
19765           objc_finish_method_definition (meth);
19766         }
19767       else
19768         /* Allow for interspersed non-ObjC++ code.  */
19769         cp_parser_objc_interstitial_code (parser);
19770
19771       token = cp_lexer_peek_token (parser->lexer);
19772     }
19773
19774   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19775   objc_finish_implementation ();
19776 }
19777
19778 /* Parse Objective-C ivars.  */
19779
19780 static void
19781 cp_parser_objc_class_ivars (cp_parser* parser)
19782 {
19783   cp_token *token = cp_lexer_peek_token (parser->lexer);
19784
19785   if (token->type != CPP_OPEN_BRACE)
19786     return;     /* No ivars specified.  */
19787
19788   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19789   token = cp_lexer_peek_token (parser->lexer);
19790
19791   while (token->type != CPP_CLOSE_BRACE)
19792     {
19793       cp_decl_specifier_seq declspecs;
19794       int decl_class_or_enum_p;
19795       tree prefix_attributes;
19796
19797       cp_parser_objc_visibility_spec (parser);
19798
19799       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19800         break;
19801
19802       cp_parser_decl_specifier_seq (parser,
19803                                     CP_PARSER_FLAGS_OPTIONAL,
19804                                     &declspecs,
19805                                     &decl_class_or_enum_p);
19806       prefix_attributes = declspecs.attributes;
19807       declspecs.attributes = NULL_TREE;
19808
19809       /* Keep going until we hit the `;' at the end of the
19810          declaration.  */
19811       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19812         {
19813           tree width = NULL_TREE, attributes, first_attribute, decl;
19814           cp_declarator *declarator = NULL;
19815           int ctor_dtor_or_conv_p;
19816
19817           /* Check for a (possibly unnamed) bitfield declaration.  */
19818           token = cp_lexer_peek_token (parser->lexer);
19819           if (token->type == CPP_COLON)
19820             goto eat_colon;
19821
19822           if (token->type == CPP_NAME
19823               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19824                   == CPP_COLON))
19825             {
19826               /* Get the name of the bitfield.  */
19827               declarator = make_id_declarator (NULL_TREE,
19828                                                cp_parser_identifier (parser),
19829                                                sfk_none);
19830
19831              eat_colon:
19832               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19833               /* Get the width of the bitfield.  */
19834               width
19835                 = cp_parser_constant_expression (parser,
19836                                                  /*allow_non_constant=*/false,
19837                                                  NULL);
19838             }
19839           else
19840             {
19841               /* Parse the declarator.  */
19842               declarator
19843                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19844                                         &ctor_dtor_or_conv_p,
19845                                         /*parenthesized_p=*/NULL,
19846                                         /*member_p=*/false);
19847             }
19848
19849           /* Look for attributes that apply to the ivar.  */
19850           attributes = cp_parser_attributes_opt (parser);
19851           /* Remember which attributes are prefix attributes and
19852              which are not.  */
19853           first_attribute = attributes;
19854           /* Combine the attributes.  */
19855           attributes = chainon (prefix_attributes, attributes);
19856
19857           if (width)
19858               /* Create the bitfield declaration.  */
19859               decl = grokbitfield (declarator, &declspecs,
19860                                    width,
19861                                    attributes);
19862           else
19863             decl = grokfield (declarator, &declspecs,
19864                               NULL_TREE, /*init_const_expr_p=*/false,
19865                               NULL_TREE, attributes);
19866
19867           /* Add the instance variable.  */
19868           objc_add_instance_variable (decl);
19869
19870           /* Reset PREFIX_ATTRIBUTES.  */
19871           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19872             attributes = TREE_CHAIN (attributes);
19873           if (attributes)
19874             TREE_CHAIN (attributes) = NULL_TREE;
19875
19876           token = cp_lexer_peek_token (parser->lexer);
19877
19878           if (token->type == CPP_COMMA)
19879             {
19880               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19881               continue;
19882             }
19883           break;
19884         }
19885
19886       cp_parser_consume_semicolon_at_end_of_statement (parser);
19887       token = cp_lexer_peek_token (parser->lexer);
19888     }
19889
19890   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19891   /* For historical reasons, we accept an optional semicolon.  */
19892   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19893     cp_lexer_consume_token (parser->lexer);
19894 }
19895
19896 /* Parse an Objective-C protocol declaration.  */
19897
19898 static void
19899 cp_parser_objc_protocol_declaration (cp_parser* parser)
19900 {
19901   tree proto, protorefs;
19902   cp_token *tok;
19903
19904   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19905   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19906     {
19907       tok = cp_lexer_peek_token (parser->lexer);
19908       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19909       goto finish;
19910     }
19911
19912   /* See if we have a forward declaration or a definition.  */
19913   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19914
19915   /* Try a forward declaration first.  */
19916   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19917     {
19918       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19919      finish:
19920       cp_parser_consume_semicolon_at_end_of_statement (parser);
19921     }
19922
19923   /* Ok, we got a full-fledged definition (or at least should).  */
19924   else
19925     {
19926       proto = cp_parser_identifier (parser);
19927       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19928       objc_start_protocol (proto, protorefs);
19929       cp_parser_objc_method_prototype_list (parser);
19930     }
19931 }
19932
19933 /* Parse an Objective-C superclass or category.  */
19934
19935 static void
19936 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19937                                                           tree *categ)
19938 {
19939   cp_token *next = cp_lexer_peek_token (parser->lexer);
19940
19941   *super = *categ = NULL_TREE;
19942   if (next->type == CPP_COLON)
19943     {
19944       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19945       *super = cp_parser_identifier (parser);
19946     }
19947   else if (next->type == CPP_OPEN_PAREN)
19948     {
19949       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19950       *categ = cp_parser_identifier (parser);
19951       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19952     }
19953 }
19954
19955 /* Parse an Objective-C class interface.  */
19956
19957 static void
19958 cp_parser_objc_class_interface (cp_parser* parser)
19959 {
19960   tree name, super, categ, protos;
19961
19962   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19963   name = cp_parser_identifier (parser);
19964   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19965   protos = cp_parser_objc_protocol_refs_opt (parser);
19966
19967   /* We have either a class or a category on our hands.  */
19968   if (categ)
19969     objc_start_category_interface (name, categ, protos);
19970   else
19971     {
19972       objc_start_class_interface (name, super, protos);
19973       /* Handle instance variable declarations, if any.  */
19974       cp_parser_objc_class_ivars (parser);
19975       objc_continue_interface ();
19976     }
19977
19978   cp_parser_objc_method_prototype_list (parser);
19979 }
19980
19981 /* Parse an Objective-C class implementation.  */
19982
19983 static void
19984 cp_parser_objc_class_implementation (cp_parser* parser)
19985 {
19986   tree name, super, categ;
19987
19988   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19989   name = cp_parser_identifier (parser);
19990   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19991
19992   /* We have either a class or a category on our hands.  */
19993   if (categ)
19994     objc_start_category_implementation (name, categ);
19995   else
19996     {
19997       objc_start_class_implementation (name, super);
19998       /* Handle instance variable declarations, if any.  */
19999       cp_parser_objc_class_ivars (parser);
20000       objc_continue_implementation ();
20001     }
20002
20003   cp_parser_objc_method_definition_list (parser);
20004 }
20005
20006 /* Consume the @end token and finish off the implementation.  */
20007
20008 static void
20009 cp_parser_objc_end_implementation (cp_parser* parser)
20010 {
20011   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20012   objc_finish_implementation ();
20013 }
20014
20015 /* Parse an Objective-C declaration.  */
20016
20017 static void
20018 cp_parser_objc_declaration (cp_parser* parser)
20019 {
20020   /* Try to figure out what kind of declaration is present.  */
20021   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20022
20023   switch (kwd->keyword)
20024     {
20025     case RID_AT_ALIAS:
20026       cp_parser_objc_alias_declaration (parser);
20027       break;
20028     case RID_AT_CLASS:
20029       cp_parser_objc_class_declaration (parser);
20030       break;
20031     case RID_AT_PROTOCOL:
20032       cp_parser_objc_protocol_declaration (parser);
20033       break;
20034     case RID_AT_INTERFACE:
20035       cp_parser_objc_class_interface (parser);
20036       break;
20037     case RID_AT_IMPLEMENTATION:
20038       cp_parser_objc_class_implementation (parser);
20039       break;
20040     case RID_AT_END:
20041       cp_parser_objc_end_implementation (parser);
20042       break;
20043     default:
20044       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20045              &kwd->location, kwd->u.value);
20046       cp_parser_skip_to_end_of_block_or_statement (parser);
20047     }
20048 }
20049
20050 /* Parse an Objective-C try-catch-finally statement.
20051
20052    objc-try-catch-finally-stmt:
20053      @try compound-statement objc-catch-clause-seq [opt]
20054        objc-finally-clause [opt]
20055
20056    objc-catch-clause-seq:
20057      objc-catch-clause objc-catch-clause-seq [opt]
20058
20059    objc-catch-clause:
20060      @catch ( exception-declaration ) compound-statement
20061
20062    objc-finally-clause
20063      @finally compound-statement
20064
20065    Returns NULL_TREE.  */
20066
20067 static tree
20068 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20069   location_t location;
20070   tree stmt;
20071
20072   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20073   location = cp_lexer_peek_token (parser->lexer)->location;
20074   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20075      node, lest it get absorbed into the surrounding block.  */
20076   stmt = push_stmt_list ();
20077   cp_parser_compound_statement (parser, NULL, false);
20078   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20079
20080   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20081     {
20082       cp_parameter_declarator *parmdecl;
20083       tree parm;
20084
20085       cp_lexer_consume_token (parser->lexer);
20086       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20087       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20088       parm = grokdeclarator (parmdecl->declarator,
20089                              &parmdecl->decl_specifiers,
20090                              PARM, /*initialized=*/0,
20091                              /*attrlist=*/NULL);
20092       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20093       objc_begin_catch_clause (parm);
20094       cp_parser_compound_statement (parser, NULL, false);
20095       objc_finish_catch_clause ();
20096     }
20097
20098   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20099     {
20100       cp_lexer_consume_token (parser->lexer);
20101       location = cp_lexer_peek_token (parser->lexer)->location;
20102       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20103          node, lest it get absorbed into the surrounding block.  */
20104       stmt = push_stmt_list ();
20105       cp_parser_compound_statement (parser, NULL, false);
20106       objc_build_finally_clause (location, pop_stmt_list (stmt));
20107     }
20108
20109   return objc_finish_try_stmt ();
20110 }
20111
20112 /* Parse an Objective-C synchronized statement.
20113
20114    objc-synchronized-stmt:
20115      @synchronized ( expression ) compound-statement
20116
20117    Returns NULL_TREE.  */
20118
20119 static tree
20120 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20121   location_t location;
20122   tree lock, stmt;
20123
20124   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20125
20126   location = cp_lexer_peek_token (parser->lexer)->location;
20127   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20128   lock = cp_parser_expression (parser, false, NULL);
20129   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20130
20131   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20132      node, lest it get absorbed into the surrounding block.  */
20133   stmt = push_stmt_list ();
20134   cp_parser_compound_statement (parser, NULL, false);
20135
20136   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20137 }
20138
20139 /* Parse an Objective-C throw statement.
20140
20141    objc-throw-stmt:
20142      @throw assignment-expression [opt] ;
20143
20144    Returns a constructed '@throw' statement.  */
20145
20146 static tree
20147 cp_parser_objc_throw_statement (cp_parser *parser) {
20148   tree expr = NULL_TREE;
20149
20150   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20151
20152   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20153     expr = cp_parser_assignment_expression (parser, false, NULL);
20154
20155   cp_parser_consume_semicolon_at_end_of_statement (parser);
20156
20157   return objc_build_throw_stmt (expr);
20158 }
20159
20160 /* Parse an Objective-C statement.  */
20161
20162 static tree
20163 cp_parser_objc_statement (cp_parser * parser) {
20164   /* Try to figure out what kind of declaration is present.  */
20165   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20166
20167   switch (kwd->keyword)
20168     {
20169     case RID_AT_TRY:
20170       return cp_parser_objc_try_catch_finally_statement (parser);
20171     case RID_AT_SYNCHRONIZED:
20172       return cp_parser_objc_synchronized_statement (parser);
20173     case RID_AT_THROW:
20174       return cp_parser_objc_throw_statement (parser);
20175     default:
20176       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20177              &kwd->location, kwd->u.value);
20178       cp_parser_skip_to_end_of_block_or_statement (parser);
20179     }
20180
20181   return error_mark_node;
20182 }
20183 \f
20184 /* OpenMP 2.5 parsing routines.  */
20185
20186 /* Returns name of the next clause.
20187    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20188    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20189    returned and the token is consumed.  */
20190
20191 static pragma_omp_clause
20192 cp_parser_omp_clause_name (cp_parser *parser)
20193 {
20194   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20195
20196   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20197     result = PRAGMA_OMP_CLAUSE_IF;
20198   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20199     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20200   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20201     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20202   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20203     {
20204       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20205       const char *p = IDENTIFIER_POINTER (id);
20206
20207       switch (p[0])
20208         {
20209         case 'c':
20210           if (!strcmp ("collapse", p))
20211             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20212           else if (!strcmp ("copyin", p))
20213             result = PRAGMA_OMP_CLAUSE_COPYIN;
20214           else if (!strcmp ("copyprivate", p))
20215             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20216           break;
20217         case 'f':
20218           if (!strcmp ("firstprivate", p))
20219             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20220           break;
20221         case 'l':
20222           if (!strcmp ("lastprivate", p))
20223             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20224           break;
20225         case 'n':
20226           if (!strcmp ("nowait", p))
20227             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20228           else if (!strcmp ("num_threads", p))
20229             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20230           break;
20231         case 'o':
20232           if (!strcmp ("ordered", p))
20233             result = PRAGMA_OMP_CLAUSE_ORDERED;
20234           break;
20235         case 'r':
20236           if (!strcmp ("reduction", p))
20237             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20238           break;
20239         case 's':
20240           if (!strcmp ("schedule", p))
20241             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20242           else if (!strcmp ("shared", p))
20243             result = PRAGMA_OMP_CLAUSE_SHARED;
20244           break;
20245         case 'u':
20246           if (!strcmp ("untied", p))
20247             result = PRAGMA_OMP_CLAUSE_UNTIED;
20248           break;
20249         }
20250     }
20251
20252   if (result != PRAGMA_OMP_CLAUSE_NONE)
20253     cp_lexer_consume_token (parser->lexer);
20254
20255   return result;
20256 }
20257
20258 /* Validate that a clause of the given type does not already exist.  */
20259
20260 static void
20261 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20262                            const char *name, location_t location)
20263 {
20264   tree c;
20265
20266   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20267     if (OMP_CLAUSE_CODE (c) == code)
20268       {
20269         error ("%Htoo many %qs clauses", &location, name);
20270         break;
20271       }
20272 }
20273
20274 /* OpenMP 2.5:
20275    variable-list:
20276      identifier
20277      variable-list , identifier
20278
20279    In addition, we match a closing parenthesis.  An opening parenthesis
20280    will have been consumed by the caller.
20281
20282    If KIND is nonzero, create the appropriate node and install the decl
20283    in OMP_CLAUSE_DECL and add the node to the head of the list.
20284
20285    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20286    return the list created.  */
20287
20288 static tree
20289 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20290                                 tree list)
20291 {
20292   cp_token *token;
20293   while (1)
20294     {
20295       tree name, decl;
20296
20297       token = cp_lexer_peek_token (parser->lexer);
20298       name = cp_parser_id_expression (parser, /*template_p=*/false,
20299                                       /*check_dependency_p=*/true,
20300                                       /*template_p=*/NULL,
20301                                       /*declarator_p=*/false,
20302                                       /*optional_p=*/false);
20303       if (name == error_mark_node)
20304         goto skip_comma;
20305
20306       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20307       if (decl == error_mark_node)
20308         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20309       else if (kind != 0)
20310         {
20311           tree u = build_omp_clause (kind);
20312           OMP_CLAUSE_DECL (u) = decl;
20313           OMP_CLAUSE_CHAIN (u) = list;
20314           list = u;
20315         }
20316       else
20317         list = tree_cons (decl, NULL_TREE, list);
20318
20319     get_comma:
20320       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20321         break;
20322       cp_lexer_consume_token (parser->lexer);
20323     }
20324
20325   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20326     {
20327       int ending;
20328
20329       /* Try to resync to an unnested comma.  Copied from
20330          cp_parser_parenthesized_expression_list.  */
20331     skip_comma:
20332       ending = cp_parser_skip_to_closing_parenthesis (parser,
20333                                                       /*recovering=*/true,
20334                                                       /*or_comma=*/true,
20335                                                       /*consume_paren=*/true);
20336       if (ending < 0)
20337         goto get_comma;
20338     }
20339
20340   return list;
20341 }
20342
20343 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20344    common case for omp clauses.  */
20345
20346 static tree
20347 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20348 {
20349   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20350     return cp_parser_omp_var_list_no_open (parser, kind, list);
20351   return list;
20352 }
20353
20354 /* OpenMP 3.0:
20355    collapse ( constant-expression ) */
20356
20357 static tree
20358 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20359 {
20360   tree c, num;
20361   location_t loc;
20362   HOST_WIDE_INT n;
20363
20364   loc = cp_lexer_peek_token (parser->lexer)->location;
20365   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20366     return list;
20367
20368   num = cp_parser_constant_expression (parser, false, NULL);
20369
20370   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20371     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20372                                            /*or_comma=*/false,
20373                                            /*consume_paren=*/true);
20374
20375   if (num == error_mark_node)
20376     return list;
20377   num = fold_non_dependent_expr (num);
20378   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20379       || !host_integerp (num, 0)
20380       || (n = tree_low_cst (num, 0)) <= 0
20381       || (int) n != n)
20382     {
20383       error ("%Hcollapse argument needs positive constant integer expression",
20384              &loc);
20385       return list;
20386     }
20387
20388   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20389   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20390   OMP_CLAUSE_CHAIN (c) = list;
20391   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20392
20393   return c;
20394 }
20395
20396 /* OpenMP 2.5:
20397    default ( shared | none ) */
20398
20399 static tree
20400 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20401 {
20402   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20403   tree c;
20404
20405   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20406     return list;
20407   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20408     {
20409       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20410       const char *p = IDENTIFIER_POINTER (id);
20411
20412       switch (p[0])
20413         {
20414         case 'n':
20415           if (strcmp ("none", p) != 0)
20416             goto invalid_kind;
20417           kind = OMP_CLAUSE_DEFAULT_NONE;
20418           break;
20419
20420         case 's':
20421           if (strcmp ("shared", p) != 0)
20422             goto invalid_kind;
20423           kind = OMP_CLAUSE_DEFAULT_SHARED;
20424           break;
20425
20426         default:
20427           goto invalid_kind;
20428         }
20429
20430       cp_lexer_consume_token (parser->lexer);
20431     }
20432   else
20433     {
20434     invalid_kind:
20435       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20436     }
20437
20438   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20439     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20440                                            /*or_comma=*/false,
20441                                            /*consume_paren=*/true);
20442
20443   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20444     return list;
20445
20446   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20447   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20448   OMP_CLAUSE_CHAIN (c) = list;
20449   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20450
20451   return c;
20452 }
20453
20454 /* OpenMP 2.5:
20455    if ( expression ) */
20456
20457 static tree
20458 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20459 {
20460   tree t, c;
20461
20462   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20463     return list;
20464
20465   t = cp_parser_condition (parser);
20466
20467   if (t == error_mark_node
20468       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20469     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20470                                            /*or_comma=*/false,
20471                                            /*consume_paren=*/true);
20472
20473   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20474
20475   c = build_omp_clause (OMP_CLAUSE_IF);
20476   OMP_CLAUSE_IF_EXPR (c) = t;
20477   OMP_CLAUSE_CHAIN (c) = list;
20478
20479   return c;
20480 }
20481
20482 /* OpenMP 2.5:
20483    nowait */
20484
20485 static tree
20486 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20487                              tree list, location_t location)
20488 {
20489   tree c;
20490
20491   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20492
20493   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20494   OMP_CLAUSE_CHAIN (c) = list;
20495   return c;
20496 }
20497
20498 /* OpenMP 2.5:
20499    num_threads ( expression ) */
20500
20501 static tree
20502 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20503                                   location_t location)
20504 {
20505   tree t, c;
20506
20507   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20508     return list;
20509
20510   t = cp_parser_expression (parser, false, NULL);
20511
20512   if (t == error_mark_node
20513       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20514     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20515                                            /*or_comma=*/false,
20516                                            /*consume_paren=*/true);
20517
20518   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20519                              "num_threads", location);
20520
20521   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20522   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20523   OMP_CLAUSE_CHAIN (c) = list;
20524
20525   return c;
20526 }
20527
20528 /* OpenMP 2.5:
20529    ordered */
20530
20531 static tree
20532 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20533                               tree list, location_t location)
20534 {
20535   tree c;
20536
20537   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20538                              "ordered", location);
20539
20540   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20541   OMP_CLAUSE_CHAIN (c) = list;
20542   return c;
20543 }
20544
20545 /* OpenMP 2.5:
20546    reduction ( reduction-operator : variable-list )
20547
20548    reduction-operator:
20549      One of: + * - & ^ | && || */
20550
20551 static tree
20552 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20553 {
20554   enum tree_code code;
20555   tree nlist, c;
20556
20557   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20558     return list;
20559
20560   switch (cp_lexer_peek_token (parser->lexer)->type)
20561     {
20562     case CPP_PLUS:
20563       code = PLUS_EXPR;
20564       break;
20565     case CPP_MULT:
20566       code = MULT_EXPR;
20567       break;
20568     case CPP_MINUS:
20569       code = MINUS_EXPR;
20570       break;
20571     case CPP_AND:
20572       code = BIT_AND_EXPR;
20573       break;
20574     case CPP_XOR:
20575       code = BIT_XOR_EXPR;
20576       break;
20577     case CPP_OR:
20578       code = BIT_IOR_EXPR;
20579       break;
20580     case CPP_AND_AND:
20581       code = TRUTH_ANDIF_EXPR;
20582       break;
20583     case CPP_OR_OR:
20584       code = TRUTH_ORIF_EXPR;
20585       break;
20586     default:
20587       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20588                                "%<|%>, %<&&%>, or %<||%>");
20589     resync_fail:
20590       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20591                                              /*or_comma=*/false,
20592                                              /*consume_paren=*/true);
20593       return list;
20594     }
20595   cp_lexer_consume_token (parser->lexer);
20596
20597   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20598     goto resync_fail;
20599
20600   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20601   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20602     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20603
20604   return nlist;
20605 }
20606
20607 /* OpenMP 2.5:
20608    schedule ( schedule-kind )
20609    schedule ( schedule-kind , expression )
20610
20611    schedule-kind:
20612      static | dynamic | guided | runtime | auto  */
20613
20614 static tree
20615 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20616 {
20617   tree c, t;
20618
20619   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20620     return list;
20621
20622   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20623
20624   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20625     {
20626       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20627       const char *p = IDENTIFIER_POINTER (id);
20628
20629       switch (p[0])
20630         {
20631         case 'd':
20632           if (strcmp ("dynamic", p) != 0)
20633             goto invalid_kind;
20634           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20635           break;
20636
20637         case 'g':
20638           if (strcmp ("guided", p) != 0)
20639             goto invalid_kind;
20640           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20641           break;
20642
20643         case 'r':
20644           if (strcmp ("runtime", p) != 0)
20645             goto invalid_kind;
20646           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20647           break;
20648
20649         default:
20650           goto invalid_kind;
20651         }
20652     }
20653   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20654     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20655   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20656     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20657   else
20658     goto invalid_kind;
20659   cp_lexer_consume_token (parser->lexer);
20660
20661   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20662     {
20663       cp_token *token;
20664       cp_lexer_consume_token (parser->lexer);
20665
20666       token = cp_lexer_peek_token (parser->lexer);
20667       t = cp_parser_assignment_expression (parser, false, NULL);
20668
20669       if (t == error_mark_node)
20670         goto resync_fail;
20671       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20672         error ("%Hschedule %<runtime%> does not take "
20673                "a %<chunk_size%> parameter", &token->location);
20674       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20675         error ("%Hschedule %<auto%> does not take "
20676                "a %<chunk_size%> parameter", &token->location);
20677       else
20678         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20679
20680       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20681         goto resync_fail;
20682     }
20683   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20684     goto resync_fail;
20685
20686   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20687   OMP_CLAUSE_CHAIN (c) = list;
20688   return c;
20689
20690  invalid_kind:
20691   cp_parser_error (parser, "invalid schedule kind");
20692  resync_fail:
20693   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20694                                          /*or_comma=*/false,
20695                                          /*consume_paren=*/true);
20696   return list;
20697 }
20698
20699 /* OpenMP 3.0:
20700    untied */
20701
20702 static tree
20703 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20704                              tree list, location_t location)
20705 {
20706   tree c;
20707
20708   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20709
20710   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20711   OMP_CLAUSE_CHAIN (c) = list;
20712   return c;
20713 }
20714
20715 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20716    is a bitmask in MASK.  Return the list of clauses found; the result
20717    of clause default goes in *pdefault.  */
20718
20719 static tree
20720 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20721                            const char *where, cp_token *pragma_tok)
20722 {
20723   tree clauses = NULL;
20724   bool first = true;
20725   cp_token *token = NULL;
20726
20727   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20728     {
20729       pragma_omp_clause c_kind;
20730       const char *c_name;
20731       tree prev = clauses;
20732
20733       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20734         cp_lexer_consume_token (parser->lexer);
20735
20736       token = cp_lexer_peek_token (parser->lexer);
20737       c_kind = cp_parser_omp_clause_name (parser);
20738       first = false;
20739
20740       switch (c_kind)
20741         {
20742         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20743           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20744                                                    token->location);
20745           c_name = "collapse";
20746           break;
20747         case PRAGMA_OMP_CLAUSE_COPYIN:
20748           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20749           c_name = "copyin";
20750           break;
20751         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20752           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20753                                             clauses);
20754           c_name = "copyprivate";
20755           break;
20756         case PRAGMA_OMP_CLAUSE_DEFAULT:
20757           clauses = cp_parser_omp_clause_default (parser, clauses,
20758                                                   token->location);
20759           c_name = "default";
20760           break;
20761         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20762           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20763                                             clauses);
20764           c_name = "firstprivate";
20765           break;
20766         case PRAGMA_OMP_CLAUSE_IF:
20767           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20768           c_name = "if";
20769           break;
20770         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20771           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20772                                             clauses);
20773           c_name = "lastprivate";
20774           break;
20775         case PRAGMA_OMP_CLAUSE_NOWAIT:
20776           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20777           c_name = "nowait";
20778           break;
20779         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20780           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20781                                                       token->location);
20782           c_name = "num_threads";
20783           break;
20784         case PRAGMA_OMP_CLAUSE_ORDERED:
20785           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20786                                                   token->location);
20787           c_name = "ordered";
20788           break;
20789         case PRAGMA_OMP_CLAUSE_PRIVATE:
20790           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20791                                             clauses);
20792           c_name = "private";
20793           break;
20794         case PRAGMA_OMP_CLAUSE_REDUCTION:
20795           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20796           c_name = "reduction";
20797           break;
20798         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20799           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20800                                                    token->location);
20801           c_name = "schedule";
20802           break;
20803         case PRAGMA_OMP_CLAUSE_SHARED:
20804           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20805                                             clauses);
20806           c_name = "shared";
20807           break;
20808         case PRAGMA_OMP_CLAUSE_UNTIED:
20809           clauses = cp_parser_omp_clause_untied (parser, clauses,
20810                                                  token->location);
20811           c_name = "nowait";
20812           break;
20813         default:
20814           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20815           goto saw_error;
20816         }
20817
20818       if (((mask >> c_kind) & 1) == 0)
20819         {
20820           /* Remove the invalid clause(s) from the list to avoid
20821              confusing the rest of the compiler.  */
20822           clauses = prev;
20823           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20824         }
20825     }
20826  saw_error:
20827   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20828   return finish_omp_clauses (clauses);
20829 }
20830
20831 /* OpenMP 2.5:
20832    structured-block:
20833      statement
20834
20835    In practice, we're also interested in adding the statement to an
20836    outer node.  So it is convenient if we work around the fact that
20837    cp_parser_statement calls add_stmt.  */
20838
20839 static unsigned
20840 cp_parser_begin_omp_structured_block (cp_parser *parser)
20841 {
20842   unsigned save = parser->in_statement;
20843
20844   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20845      This preserves the "not within loop or switch" style error messages
20846      for nonsense cases like
20847         void foo() {
20848         #pragma omp single
20849           break;
20850         }
20851   */
20852   if (parser->in_statement)
20853     parser->in_statement = IN_OMP_BLOCK;
20854
20855   return save;
20856 }
20857
20858 static void
20859 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20860 {
20861   parser->in_statement = save;
20862 }
20863
20864 static tree
20865 cp_parser_omp_structured_block (cp_parser *parser)
20866 {
20867   tree stmt = begin_omp_structured_block ();
20868   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20869
20870   cp_parser_statement (parser, NULL_TREE, false, NULL);
20871
20872   cp_parser_end_omp_structured_block (parser, save);
20873   return finish_omp_structured_block (stmt);
20874 }
20875
20876 /* OpenMP 2.5:
20877    # pragma omp atomic new-line
20878      expression-stmt
20879
20880    expression-stmt:
20881      x binop= expr | x++ | ++x | x-- | --x
20882    binop:
20883      +, *, -, /, &, ^, |, <<, >>
20884
20885   where x is an lvalue expression with scalar type.  */
20886
20887 static void
20888 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20889 {
20890   tree lhs, rhs;
20891   enum tree_code code;
20892
20893   cp_parser_require_pragma_eol (parser, pragma_tok);
20894
20895   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20896                                     /*cast_p=*/false, NULL);
20897   switch (TREE_CODE (lhs))
20898     {
20899     case ERROR_MARK:
20900       goto saw_error;
20901
20902     case PREINCREMENT_EXPR:
20903     case POSTINCREMENT_EXPR:
20904       lhs = TREE_OPERAND (lhs, 0);
20905       code = PLUS_EXPR;
20906       rhs = integer_one_node;
20907       break;
20908
20909     case PREDECREMENT_EXPR:
20910     case POSTDECREMENT_EXPR:
20911       lhs = TREE_OPERAND (lhs, 0);
20912       code = MINUS_EXPR;
20913       rhs = integer_one_node;
20914       break;
20915
20916     default:
20917       switch (cp_lexer_peek_token (parser->lexer)->type)
20918         {
20919         case CPP_MULT_EQ:
20920           code = MULT_EXPR;
20921           break;
20922         case CPP_DIV_EQ:
20923           code = TRUNC_DIV_EXPR;
20924           break;
20925         case CPP_PLUS_EQ:
20926           code = PLUS_EXPR;
20927           break;
20928         case CPP_MINUS_EQ:
20929           code = MINUS_EXPR;
20930           break;
20931         case CPP_LSHIFT_EQ:
20932           code = LSHIFT_EXPR;
20933           break;
20934         case CPP_RSHIFT_EQ:
20935           code = RSHIFT_EXPR;
20936           break;
20937         case CPP_AND_EQ:
20938           code = BIT_AND_EXPR;
20939           break;
20940         case CPP_OR_EQ:
20941           code = BIT_IOR_EXPR;
20942           break;
20943         case CPP_XOR_EQ:
20944           code = BIT_XOR_EXPR;
20945           break;
20946         default:
20947           cp_parser_error (parser,
20948                            "invalid operator for %<#pragma omp atomic%>");
20949           goto saw_error;
20950         }
20951       cp_lexer_consume_token (parser->lexer);
20952
20953       rhs = cp_parser_expression (parser, false, NULL);
20954       if (rhs == error_mark_node)
20955         goto saw_error;
20956       break;
20957     }
20958   finish_omp_atomic (code, lhs, rhs);
20959   cp_parser_consume_semicolon_at_end_of_statement (parser);
20960   return;
20961
20962  saw_error:
20963   cp_parser_skip_to_end_of_block_or_statement (parser);
20964 }
20965
20966
20967 /* OpenMP 2.5:
20968    # pragma omp barrier new-line  */
20969
20970 static void
20971 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20972 {
20973   cp_parser_require_pragma_eol (parser, pragma_tok);
20974   finish_omp_barrier ();
20975 }
20976
20977 /* OpenMP 2.5:
20978    # pragma omp critical [(name)] new-line
20979      structured-block  */
20980
20981 static tree
20982 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20983 {
20984   tree stmt, name = NULL;
20985
20986   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20987     {
20988       cp_lexer_consume_token (parser->lexer);
20989
20990       name = cp_parser_identifier (parser);
20991
20992       if (name == error_mark_node
20993           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20994         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20995                                                /*or_comma=*/false,
20996                                                /*consume_paren=*/true);
20997       if (name == error_mark_node)
20998         name = NULL;
20999     }
21000   cp_parser_require_pragma_eol (parser, pragma_tok);
21001
21002   stmt = cp_parser_omp_structured_block (parser);
21003   return c_finish_omp_critical (stmt, name);
21004 }
21005
21006 /* OpenMP 2.5:
21007    # pragma omp flush flush-vars[opt] new-line
21008
21009    flush-vars:
21010      ( variable-list ) */
21011
21012 static void
21013 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21014 {
21015   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21016     (void) cp_parser_omp_var_list (parser, 0, NULL);
21017   cp_parser_require_pragma_eol (parser, pragma_tok);
21018
21019   finish_omp_flush ();
21020 }
21021
21022 /* Helper function, to parse omp for increment expression.  */
21023
21024 static tree
21025 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21026 {
21027   tree cond = cp_parser_binary_expression (parser, false, true,
21028                                            PREC_NOT_OPERATOR, NULL);
21029   bool overloaded_p;
21030
21031   if (cond == error_mark_node
21032       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21033     {
21034       cp_parser_skip_to_end_of_statement (parser);
21035       return error_mark_node;
21036     }
21037
21038   switch (TREE_CODE (cond))
21039     {
21040     case GT_EXPR:
21041     case GE_EXPR:
21042     case LT_EXPR:
21043     case LE_EXPR:
21044       break;
21045     default:
21046       return error_mark_node;
21047     }
21048
21049   /* If decl is an iterator, preserve LHS and RHS of the relational
21050      expr until finish_omp_for.  */
21051   if (decl
21052       && (type_dependent_expression_p (decl)
21053           || CLASS_TYPE_P (TREE_TYPE (decl))))
21054     return cond;
21055
21056   return build_x_binary_op (TREE_CODE (cond),
21057                             TREE_OPERAND (cond, 0), ERROR_MARK,
21058                             TREE_OPERAND (cond, 1), ERROR_MARK,
21059                             &overloaded_p, tf_warning_or_error);
21060 }
21061
21062 /* Helper function, to parse omp for increment expression.  */
21063
21064 static tree
21065 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21066 {
21067   cp_token *token = cp_lexer_peek_token (parser->lexer);
21068   enum tree_code op;
21069   tree lhs, rhs;
21070   cp_id_kind idk;
21071   bool decl_first;
21072
21073   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21074     {
21075       op = (token->type == CPP_PLUS_PLUS
21076             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21077       cp_lexer_consume_token (parser->lexer);
21078       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21079       if (lhs != decl)
21080         return error_mark_node;
21081       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21082     }
21083
21084   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21085   if (lhs != decl)
21086     return error_mark_node;
21087
21088   token = cp_lexer_peek_token (parser->lexer);
21089   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21090     {
21091       op = (token->type == CPP_PLUS_PLUS
21092             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21093       cp_lexer_consume_token (parser->lexer);
21094       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21095     }
21096
21097   op = cp_parser_assignment_operator_opt (parser);
21098   if (op == ERROR_MARK)
21099     return error_mark_node;
21100
21101   if (op != NOP_EXPR)
21102     {
21103       rhs = cp_parser_assignment_expression (parser, false, NULL);
21104       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21105       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21106     }
21107
21108   lhs = cp_parser_binary_expression (parser, false, false,
21109                                      PREC_ADDITIVE_EXPRESSION, NULL);
21110   token = cp_lexer_peek_token (parser->lexer);
21111   decl_first = lhs == decl;
21112   if (decl_first)
21113     lhs = NULL_TREE;
21114   if (token->type != CPP_PLUS
21115       && token->type != CPP_MINUS)
21116     return error_mark_node;
21117
21118   do
21119     {
21120       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21121       cp_lexer_consume_token (parser->lexer);
21122       rhs = cp_parser_binary_expression (parser, false, false,
21123                                          PREC_ADDITIVE_EXPRESSION, NULL);
21124       token = cp_lexer_peek_token (parser->lexer);
21125       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21126         {
21127           if (lhs == NULL_TREE)
21128             {
21129               if (op == PLUS_EXPR)
21130                 lhs = rhs;
21131               else
21132                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21133             }
21134           else
21135             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21136                                      NULL, tf_warning_or_error);
21137         }
21138     }
21139   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21140
21141   if (!decl_first)
21142     {
21143       if (rhs != decl || op == MINUS_EXPR)
21144         return error_mark_node;
21145       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21146     }
21147   else
21148     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21149
21150   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21151 }
21152
21153 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21154
21155 static tree
21156 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21157 {
21158   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21159   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21160   tree this_pre_body, cl;
21161   location_t loc_first;
21162   bool collapse_err = false;
21163   int i, collapse = 1, nbraces = 0;
21164
21165   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21166     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21167       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21168
21169   gcc_assert (collapse >= 1);
21170
21171   declv = make_tree_vec (collapse);
21172   initv = make_tree_vec (collapse);
21173   condv = make_tree_vec (collapse);
21174   incrv = make_tree_vec (collapse);
21175
21176   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21177
21178   for (i = 0; i < collapse; i++)
21179     {
21180       int bracecount = 0;
21181       bool add_private_clause = false;
21182       location_t loc;
21183
21184       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21185         {
21186           cp_parser_error (parser, "for statement expected");
21187           return NULL;
21188         }
21189       loc = cp_lexer_consume_token (parser->lexer)->location;
21190
21191       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21192         return NULL;
21193
21194       init = decl = real_decl = NULL;
21195       this_pre_body = push_stmt_list ();
21196       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21197         {
21198           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21199
21200              init-expr:
21201                        var = lb
21202                        integer-type var = lb
21203                        random-access-iterator-type var = lb
21204                        pointer-type var = lb
21205           */
21206           cp_decl_specifier_seq type_specifiers;
21207
21208           /* First, try to parse as an initialized declaration.  See
21209              cp_parser_condition, from whence the bulk of this is copied.  */
21210
21211           cp_parser_parse_tentatively (parser);
21212           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21213                                         &type_specifiers);
21214           if (cp_parser_parse_definitely (parser))
21215             {
21216               /* If parsing a type specifier seq succeeded, then this
21217                  MUST be a initialized declaration.  */
21218               tree asm_specification, attributes;
21219               cp_declarator *declarator;
21220
21221               declarator = cp_parser_declarator (parser,
21222                                                  CP_PARSER_DECLARATOR_NAMED,
21223                                                  /*ctor_dtor_or_conv_p=*/NULL,
21224                                                  /*parenthesized_p=*/NULL,
21225                                                  /*member_p=*/false);
21226               attributes = cp_parser_attributes_opt (parser);
21227               asm_specification = cp_parser_asm_specification_opt (parser);
21228
21229               if (declarator == cp_error_declarator) 
21230                 cp_parser_skip_to_end_of_statement (parser);
21231
21232               else 
21233                 {
21234                   tree pushed_scope, auto_node;
21235
21236                   decl = start_decl (declarator, &type_specifiers,
21237                                      SD_INITIALIZED, attributes,
21238                                      /*prefix_attributes=*/NULL_TREE,
21239                                      &pushed_scope);
21240
21241                   auto_node = type_uses_auto (TREE_TYPE (decl));
21242                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21243                     {
21244                       if (cp_lexer_next_token_is (parser->lexer, 
21245                                                   CPP_OPEN_PAREN))
21246                         error ("parenthesized initialization is not allowed in "
21247                                "OpenMP %<for%> loop");
21248                       else
21249                         /* Trigger an error.  */
21250                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21251
21252                       init = error_mark_node;
21253                       cp_parser_skip_to_end_of_statement (parser);
21254                     }
21255                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21256                            || type_dependent_expression_p (decl)
21257                            || auto_node)
21258                     {
21259                       bool is_direct_init, is_non_constant_init;
21260
21261                       init = cp_parser_initializer (parser,
21262                                                     &is_direct_init,
21263                                                     &is_non_constant_init);
21264
21265                       if (auto_node && describable_type (init))
21266                         {
21267                           TREE_TYPE (decl)
21268                             = do_auto_deduction (TREE_TYPE (decl), init,
21269                                                  auto_node);
21270
21271                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21272                               && !type_dependent_expression_p (decl))
21273                             goto non_class;
21274                         }
21275                       
21276                       cp_finish_decl (decl, init, !is_non_constant_init,
21277                                       asm_specification,
21278                                       LOOKUP_ONLYCONVERTING);
21279                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21280                         {
21281                           for_block
21282                             = tree_cons (NULL, this_pre_body, for_block);
21283                           init = NULL_TREE;
21284                         }
21285                       else
21286                         init = pop_stmt_list (this_pre_body);
21287                       this_pre_body = NULL_TREE;
21288                     }
21289                   else
21290                     {
21291                       /* Consume '='.  */
21292                       cp_lexer_consume_token (parser->lexer);
21293                       init = cp_parser_assignment_expression (parser, false, NULL);
21294
21295                     non_class:
21296                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21297                         init = error_mark_node;
21298                       else
21299                         cp_finish_decl (decl, NULL_TREE,
21300                                         /*init_const_expr_p=*/false,
21301                                         asm_specification,
21302                                         LOOKUP_ONLYCONVERTING);
21303                     }
21304
21305                   if (pushed_scope)
21306                     pop_scope (pushed_scope);
21307                 }
21308             }
21309           else 
21310             {
21311               cp_id_kind idk;
21312               /* If parsing a type specifier sequence failed, then
21313                  this MUST be a simple expression.  */
21314               cp_parser_parse_tentatively (parser);
21315               decl = cp_parser_primary_expression (parser, false, false,
21316                                                    false, &idk);
21317               if (!cp_parser_error_occurred (parser)
21318                   && decl
21319                   && DECL_P (decl)
21320                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21321                 {
21322                   tree rhs;
21323
21324                   cp_parser_parse_definitely (parser);
21325                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21326                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21327                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21328                                                          rhs,
21329                                                          tf_warning_or_error));
21330                   add_private_clause = true;
21331                 }
21332               else
21333                 {
21334                   decl = NULL;
21335                   cp_parser_abort_tentative_parse (parser);
21336                   init = cp_parser_expression (parser, false, NULL);
21337                   if (init)
21338                     {
21339                       if (TREE_CODE (init) == MODIFY_EXPR
21340                           || TREE_CODE (init) == MODOP_EXPR)
21341                         real_decl = TREE_OPERAND (init, 0);
21342                     }
21343                 }
21344             }
21345         }
21346       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21347       if (this_pre_body)
21348         {
21349           this_pre_body = pop_stmt_list (this_pre_body);
21350           if (pre_body)
21351             {
21352               tree t = pre_body;
21353               pre_body = push_stmt_list ();
21354               add_stmt (t);
21355               add_stmt (this_pre_body);
21356               pre_body = pop_stmt_list (pre_body);
21357             }
21358           else
21359             pre_body = this_pre_body;
21360         }
21361
21362       if (decl)
21363         real_decl = decl;
21364       if (par_clauses != NULL && real_decl != NULL_TREE)
21365         {
21366           tree *c;
21367           for (c = par_clauses; *c ; )
21368             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21369                 && OMP_CLAUSE_DECL (*c) == real_decl)
21370               {
21371                 error ("%Hiteration variable %qD should not be firstprivate",
21372                        &loc, real_decl);
21373                 *c = OMP_CLAUSE_CHAIN (*c);
21374               }
21375             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21376                      && OMP_CLAUSE_DECL (*c) == real_decl)
21377               {
21378                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21379                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21380                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21381                 OMP_CLAUSE_DECL (l) = real_decl;
21382                 OMP_CLAUSE_CHAIN (l) = clauses;
21383                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21384                 clauses = l;
21385                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21386                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21387                 add_private_clause = false;
21388               }
21389             else
21390               {
21391                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21392                     && OMP_CLAUSE_DECL (*c) == real_decl)
21393                   add_private_clause = false;
21394                 c = &OMP_CLAUSE_CHAIN (*c);
21395               }
21396         }
21397
21398       if (add_private_clause)
21399         {
21400           tree c;
21401           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21402             {
21403               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21404                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21405                   && OMP_CLAUSE_DECL (c) == decl)
21406                 break;
21407               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21408                        && OMP_CLAUSE_DECL (c) == decl)
21409                 error ("%Hiteration variable %qD should not be firstprivate",
21410                        &loc, decl);
21411               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21412                        && OMP_CLAUSE_DECL (c) == decl)
21413                 error ("%Hiteration variable %qD should not be reduction",
21414                        &loc, decl);
21415             }
21416           if (c == NULL)
21417             {
21418               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21419               OMP_CLAUSE_DECL (c) = decl;
21420               c = finish_omp_clauses (c);
21421               if (c)
21422                 {
21423                   OMP_CLAUSE_CHAIN (c) = clauses;
21424                   clauses = c;
21425                 }
21426             }
21427         }
21428
21429       cond = NULL;
21430       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21431         cond = cp_parser_omp_for_cond (parser, decl);
21432       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21433
21434       incr = NULL;
21435       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21436         {
21437           /* If decl is an iterator, preserve the operator on decl
21438              until finish_omp_for.  */
21439           if (decl
21440               && (type_dependent_expression_p (decl)
21441                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21442             incr = cp_parser_omp_for_incr (parser, decl);
21443           else
21444             incr = cp_parser_expression (parser, false, NULL);
21445         }
21446
21447       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21448         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21449                                                /*or_comma=*/false,
21450                                                /*consume_paren=*/true);
21451
21452       TREE_VEC_ELT (declv, i) = decl;
21453       TREE_VEC_ELT (initv, i) = init;
21454       TREE_VEC_ELT (condv, i) = cond;
21455       TREE_VEC_ELT (incrv, i) = incr;
21456
21457       if (i == collapse - 1)
21458         break;
21459
21460       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21461          in between the collapsed for loops to be still considered perfectly
21462          nested.  Hopefully the final version clarifies this.
21463          For now handle (multiple) {'s and empty statements.  */
21464       cp_parser_parse_tentatively (parser);
21465       do
21466         {
21467           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21468             break;
21469           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21470             {
21471               cp_lexer_consume_token (parser->lexer);
21472               bracecount++;
21473             }
21474           else if (bracecount
21475                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21476             cp_lexer_consume_token (parser->lexer);
21477           else
21478             {
21479               loc = cp_lexer_peek_token (parser->lexer)->location;
21480               error ("%Hnot enough collapsed for loops", &loc);
21481               collapse_err = true;
21482               cp_parser_abort_tentative_parse (parser);
21483               declv = NULL_TREE;
21484               break;
21485             }
21486         }
21487       while (1);
21488
21489       if (declv)
21490         {
21491           cp_parser_parse_definitely (parser);
21492           nbraces += bracecount;
21493         }
21494     }
21495
21496   /* Note that we saved the original contents of this flag when we entered
21497      the structured block, and so we don't need to re-save it here.  */
21498   parser->in_statement = IN_OMP_FOR;
21499
21500   /* Note that the grammar doesn't call for a structured block here,
21501      though the loop as a whole is a structured block.  */
21502   body = push_stmt_list ();
21503   cp_parser_statement (parser, NULL_TREE, false, NULL);
21504   body = pop_stmt_list (body);
21505
21506   if (declv == NULL_TREE)
21507     ret = NULL_TREE;
21508   else
21509     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21510                           pre_body, clauses);
21511
21512   while (nbraces)
21513     {
21514       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21515         {
21516           cp_lexer_consume_token (parser->lexer);
21517           nbraces--;
21518         }
21519       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21520         cp_lexer_consume_token (parser->lexer);
21521       else
21522         {
21523           if (!collapse_err)
21524             {
21525               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21526               error ("%Hcollapsed loops not perfectly nested", &loc);
21527             }
21528           collapse_err = true;
21529           cp_parser_statement_seq_opt (parser, NULL);
21530           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21531         }
21532     }
21533
21534   while (for_block)
21535     {
21536       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21537       for_block = TREE_CHAIN (for_block);
21538     }
21539
21540   return ret;
21541 }
21542
21543 /* OpenMP 2.5:
21544    #pragma omp for for-clause[optseq] new-line
21545      for-loop  */
21546
21547 #define OMP_FOR_CLAUSE_MASK                             \
21548         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21549         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21550         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21551         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21552         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21553         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21554         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21555         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21556
21557 static tree
21558 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21559 {
21560   tree clauses, sb, ret;
21561   unsigned int save;
21562
21563   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21564                                        "#pragma omp for", pragma_tok);
21565
21566   sb = begin_omp_structured_block ();
21567   save = cp_parser_begin_omp_structured_block (parser);
21568
21569   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21570
21571   cp_parser_end_omp_structured_block (parser, save);
21572   add_stmt (finish_omp_structured_block (sb));
21573
21574   return ret;
21575 }
21576
21577 /* OpenMP 2.5:
21578    # pragma omp master new-line
21579      structured-block  */
21580
21581 static tree
21582 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21583 {
21584   cp_parser_require_pragma_eol (parser, pragma_tok);
21585   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21586 }
21587
21588 /* OpenMP 2.5:
21589    # pragma omp ordered new-line
21590      structured-block  */
21591
21592 static tree
21593 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21594 {
21595   cp_parser_require_pragma_eol (parser, pragma_tok);
21596   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21597 }
21598
21599 /* OpenMP 2.5:
21600
21601    section-scope:
21602      { section-sequence }
21603
21604    section-sequence:
21605      section-directive[opt] structured-block
21606      section-sequence section-directive structured-block  */
21607
21608 static tree
21609 cp_parser_omp_sections_scope (cp_parser *parser)
21610 {
21611   tree stmt, substmt;
21612   bool error_suppress = false;
21613   cp_token *tok;
21614
21615   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21616     return NULL_TREE;
21617
21618   stmt = push_stmt_list ();
21619
21620   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21621     {
21622       unsigned save;
21623
21624       substmt = begin_omp_structured_block ();
21625       save = cp_parser_begin_omp_structured_block (parser);
21626
21627       while (1)
21628         {
21629           cp_parser_statement (parser, NULL_TREE, false, NULL);
21630
21631           tok = cp_lexer_peek_token (parser->lexer);
21632           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21633             break;
21634           if (tok->type == CPP_CLOSE_BRACE)
21635             break;
21636           if (tok->type == CPP_EOF)
21637             break;
21638         }
21639
21640       cp_parser_end_omp_structured_block (parser, save);
21641       substmt = finish_omp_structured_block (substmt);
21642       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21643       add_stmt (substmt);
21644     }
21645
21646   while (1)
21647     {
21648       tok = cp_lexer_peek_token (parser->lexer);
21649       if (tok->type == CPP_CLOSE_BRACE)
21650         break;
21651       if (tok->type == CPP_EOF)
21652         break;
21653
21654       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21655         {
21656           cp_lexer_consume_token (parser->lexer);
21657           cp_parser_require_pragma_eol (parser, tok);
21658           error_suppress = false;
21659         }
21660       else if (!error_suppress)
21661         {
21662           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21663           error_suppress = true;
21664         }
21665
21666       substmt = cp_parser_omp_structured_block (parser);
21667       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21668       add_stmt (substmt);
21669     }
21670   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21671
21672   substmt = pop_stmt_list (stmt);
21673
21674   stmt = make_node (OMP_SECTIONS);
21675   TREE_TYPE (stmt) = void_type_node;
21676   OMP_SECTIONS_BODY (stmt) = substmt;
21677
21678   add_stmt (stmt);
21679   return stmt;
21680 }
21681
21682 /* OpenMP 2.5:
21683    # pragma omp sections sections-clause[optseq] newline
21684      sections-scope  */
21685
21686 #define OMP_SECTIONS_CLAUSE_MASK                        \
21687         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21688         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21689         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21690         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21691         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21692
21693 static tree
21694 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21695 {
21696   tree clauses, ret;
21697
21698   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21699                                        "#pragma omp sections", pragma_tok);
21700
21701   ret = cp_parser_omp_sections_scope (parser);
21702   if (ret)
21703     OMP_SECTIONS_CLAUSES (ret) = clauses;
21704
21705   return ret;
21706 }
21707
21708 /* OpenMP 2.5:
21709    # pragma parallel parallel-clause new-line
21710    # pragma parallel for parallel-for-clause new-line
21711    # pragma parallel sections parallel-sections-clause new-line  */
21712
21713 #define OMP_PARALLEL_CLAUSE_MASK                        \
21714         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21715         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21716         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21717         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21718         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21719         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21720         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21721         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21722
21723 static tree
21724 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21725 {
21726   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21727   const char *p_name = "#pragma omp parallel";
21728   tree stmt, clauses, par_clause, ws_clause, block;
21729   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21730   unsigned int save;
21731
21732   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21733     {
21734       cp_lexer_consume_token (parser->lexer);
21735       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21736       p_name = "#pragma omp parallel for";
21737       mask |= OMP_FOR_CLAUSE_MASK;
21738       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21739     }
21740   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21741     {
21742       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21743       const char *p = IDENTIFIER_POINTER (id);
21744       if (strcmp (p, "sections") == 0)
21745         {
21746           cp_lexer_consume_token (parser->lexer);
21747           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21748           p_name = "#pragma omp parallel sections";
21749           mask |= OMP_SECTIONS_CLAUSE_MASK;
21750           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21751         }
21752     }
21753
21754   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21755   block = begin_omp_parallel ();
21756   save = cp_parser_begin_omp_structured_block (parser);
21757
21758   switch (p_kind)
21759     {
21760     case PRAGMA_OMP_PARALLEL:
21761       cp_parser_statement (parser, NULL_TREE, false, NULL);
21762       par_clause = clauses;
21763       break;
21764
21765     case PRAGMA_OMP_PARALLEL_FOR:
21766       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21767       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21768       break;
21769
21770     case PRAGMA_OMP_PARALLEL_SECTIONS:
21771       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21772       stmt = cp_parser_omp_sections_scope (parser);
21773       if (stmt)
21774         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21775       break;
21776
21777     default:
21778       gcc_unreachable ();
21779     }
21780
21781   cp_parser_end_omp_structured_block (parser, save);
21782   stmt = finish_omp_parallel (par_clause, block);
21783   if (p_kind != PRAGMA_OMP_PARALLEL)
21784     OMP_PARALLEL_COMBINED (stmt) = 1;
21785   return stmt;
21786 }
21787
21788 /* OpenMP 2.5:
21789    # pragma omp single single-clause[optseq] new-line
21790      structured-block  */
21791
21792 #define OMP_SINGLE_CLAUSE_MASK                          \
21793         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21794         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21795         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21796         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21797
21798 static tree
21799 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21800 {
21801   tree stmt = make_node (OMP_SINGLE);
21802   TREE_TYPE (stmt) = void_type_node;
21803
21804   OMP_SINGLE_CLAUSES (stmt)
21805     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21806                                  "#pragma omp single", pragma_tok);
21807   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21808
21809   return add_stmt (stmt);
21810 }
21811
21812 /* OpenMP 3.0:
21813    # pragma omp task task-clause[optseq] new-line
21814      structured-block  */
21815
21816 #define OMP_TASK_CLAUSE_MASK                            \
21817         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21818         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21819         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21820         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21821         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21822         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21823
21824 static tree
21825 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21826 {
21827   tree clauses, block;
21828   unsigned int save;
21829
21830   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21831                                        "#pragma omp task", pragma_tok);
21832   block = begin_omp_task ();
21833   save = cp_parser_begin_omp_structured_block (parser);
21834   cp_parser_statement (parser, NULL_TREE, false, NULL);
21835   cp_parser_end_omp_structured_block (parser, save);
21836   return finish_omp_task (clauses, block);
21837 }
21838
21839 /* OpenMP 3.0:
21840    # pragma omp taskwait new-line  */
21841
21842 static void
21843 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21844 {
21845   cp_parser_require_pragma_eol (parser, pragma_tok);
21846   finish_omp_taskwait ();
21847 }
21848
21849 /* OpenMP 2.5:
21850    # pragma omp threadprivate (variable-list) */
21851
21852 static void
21853 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21854 {
21855   tree vars;
21856
21857   vars = cp_parser_omp_var_list (parser, 0, NULL);
21858   cp_parser_require_pragma_eol (parser, pragma_tok);
21859
21860   finish_omp_threadprivate (vars);
21861 }
21862
21863 /* Main entry point to OpenMP statement pragmas.  */
21864
21865 static void
21866 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21867 {
21868   tree stmt;
21869
21870   switch (pragma_tok->pragma_kind)
21871     {
21872     case PRAGMA_OMP_ATOMIC:
21873       cp_parser_omp_atomic (parser, pragma_tok);
21874       return;
21875     case PRAGMA_OMP_CRITICAL:
21876       stmt = cp_parser_omp_critical (parser, pragma_tok);
21877       break;
21878     case PRAGMA_OMP_FOR:
21879       stmt = cp_parser_omp_for (parser, pragma_tok);
21880       break;
21881     case PRAGMA_OMP_MASTER:
21882       stmt = cp_parser_omp_master (parser, pragma_tok);
21883       break;
21884     case PRAGMA_OMP_ORDERED:
21885       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21886       break;
21887     case PRAGMA_OMP_PARALLEL:
21888       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21889       break;
21890     case PRAGMA_OMP_SECTIONS:
21891       stmt = cp_parser_omp_sections (parser, pragma_tok);
21892       break;
21893     case PRAGMA_OMP_SINGLE:
21894       stmt = cp_parser_omp_single (parser, pragma_tok);
21895       break;
21896     case PRAGMA_OMP_TASK:
21897       stmt = cp_parser_omp_task (parser, pragma_tok);
21898       break;
21899     default:
21900       gcc_unreachable ();
21901     }
21902
21903   if (stmt)
21904     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21905 }
21906 \f
21907 /* The parser.  */
21908
21909 static GTY (()) cp_parser *the_parser;
21910
21911 \f
21912 /* Special handling for the first token or line in the file.  The first
21913    thing in the file might be #pragma GCC pch_preprocess, which loads a
21914    PCH file, which is a GC collection point.  So we need to handle this
21915    first pragma without benefit of an existing lexer structure.
21916
21917    Always returns one token to the caller in *FIRST_TOKEN.  This is
21918    either the true first token of the file, or the first token after
21919    the initial pragma.  */
21920
21921 static void
21922 cp_parser_initial_pragma (cp_token *first_token)
21923 {
21924   tree name = NULL;
21925
21926   cp_lexer_get_preprocessor_token (NULL, first_token);
21927   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21928     return;
21929
21930   cp_lexer_get_preprocessor_token (NULL, first_token);
21931   if (first_token->type == CPP_STRING)
21932     {
21933       name = first_token->u.value;
21934
21935       cp_lexer_get_preprocessor_token (NULL, first_token);
21936       if (first_token->type != CPP_PRAGMA_EOL)
21937         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21938                &first_token->location);
21939     }
21940   else
21941     error ("%Hexpected string literal", &first_token->location);
21942
21943   /* Skip to the end of the pragma.  */
21944   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21945     cp_lexer_get_preprocessor_token (NULL, first_token);
21946
21947   /* Now actually load the PCH file.  */
21948   if (name)
21949     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21950
21951   /* Read one more token to return to our caller.  We have to do this
21952      after reading the PCH file in, since its pointers have to be
21953      live.  */
21954   cp_lexer_get_preprocessor_token (NULL, first_token);
21955 }
21956
21957 /* Normal parsing of a pragma token.  Here we can (and must) use the
21958    regular lexer.  */
21959
21960 static bool
21961 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21962 {
21963   cp_token *pragma_tok;
21964   unsigned int id;
21965
21966   pragma_tok = cp_lexer_consume_token (parser->lexer);
21967   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21968   parser->lexer->in_pragma = true;
21969
21970   id = pragma_tok->pragma_kind;
21971   switch (id)
21972     {
21973     case PRAGMA_GCC_PCH_PREPROCESS:
21974       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21975              &pragma_tok->location);
21976       break;
21977
21978     case PRAGMA_OMP_BARRIER:
21979       switch (context)
21980         {
21981         case pragma_compound:
21982           cp_parser_omp_barrier (parser, pragma_tok);
21983           return false;
21984         case pragma_stmt:
21985           error ("%H%<#pragma omp barrier%> may only be "
21986                  "used in compound statements", &pragma_tok->location);
21987           break;
21988         default:
21989           goto bad_stmt;
21990         }
21991       break;
21992
21993     case PRAGMA_OMP_FLUSH:
21994       switch (context)
21995         {
21996         case pragma_compound:
21997           cp_parser_omp_flush (parser, pragma_tok);
21998           return false;
21999         case pragma_stmt:
22000           error ("%H%<#pragma omp flush%> may only be "
22001                  "used in compound statements", &pragma_tok->location);
22002           break;
22003         default:
22004           goto bad_stmt;
22005         }
22006       break;
22007
22008     case PRAGMA_OMP_TASKWAIT:
22009       switch (context)
22010         {
22011         case pragma_compound:
22012           cp_parser_omp_taskwait (parser, pragma_tok);
22013           return false;
22014         case pragma_stmt:
22015           error ("%H%<#pragma omp taskwait%> may only be "
22016                  "used in compound statements",
22017                  &pragma_tok->location);
22018           break;
22019         default:
22020           goto bad_stmt;
22021         }
22022       break;
22023
22024     case PRAGMA_OMP_THREADPRIVATE:
22025       cp_parser_omp_threadprivate (parser, pragma_tok);
22026       return false;
22027
22028     case PRAGMA_OMP_ATOMIC:
22029     case PRAGMA_OMP_CRITICAL:
22030     case PRAGMA_OMP_FOR:
22031     case PRAGMA_OMP_MASTER:
22032     case PRAGMA_OMP_ORDERED:
22033     case PRAGMA_OMP_PARALLEL:
22034     case PRAGMA_OMP_SECTIONS:
22035     case PRAGMA_OMP_SINGLE:
22036     case PRAGMA_OMP_TASK:
22037       if (context == pragma_external)
22038         goto bad_stmt;
22039       cp_parser_omp_construct (parser, pragma_tok);
22040       return true;
22041
22042     case PRAGMA_OMP_SECTION:
22043       error ("%H%<#pragma omp section%> may only be used in "
22044              "%<#pragma omp sections%> construct", &pragma_tok->location);
22045       break;
22046
22047     default:
22048       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22049       c_invoke_pragma_handler (id);
22050       break;
22051
22052     bad_stmt:
22053       cp_parser_error (parser, "expected declaration specifiers");
22054       break;
22055     }
22056
22057   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22058   return false;
22059 }
22060
22061 /* The interface the pragma parsers have to the lexer.  */
22062
22063 enum cpp_ttype
22064 pragma_lex (tree *value)
22065 {
22066   cp_token *tok;
22067   enum cpp_ttype ret;
22068
22069   tok = cp_lexer_peek_token (the_parser->lexer);
22070
22071   ret = tok->type;
22072   *value = tok->u.value;
22073
22074   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22075     ret = CPP_EOF;
22076   else if (ret == CPP_STRING)
22077     *value = cp_parser_string_literal (the_parser, false, false);
22078   else
22079     {
22080       cp_lexer_consume_token (the_parser->lexer);
22081       if (ret == CPP_KEYWORD)
22082         ret = CPP_NAME;
22083     }
22084
22085   return ret;
22086 }
22087
22088 \f
22089 /* External interface.  */
22090
22091 /* Parse one entire translation unit.  */
22092
22093 void
22094 c_parse_file (void)
22095 {
22096   bool error_occurred;
22097   static bool already_called = false;
22098
22099   if (already_called)
22100     {
22101       sorry ("inter-module optimizations not implemented for C++");
22102       return;
22103     }
22104   already_called = true;
22105
22106   the_parser = cp_parser_new ();
22107   push_deferring_access_checks (flag_access_control
22108                                 ? dk_no_deferred : dk_no_check);
22109   error_occurred = cp_parser_translation_unit (the_parser);
22110   the_parser = NULL;
22111 }
22112
22113 #include "gt-cp-parser.h"