OSDN Git Service

PR c++/18451
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40 #include "plugin.h"
41
42 \f
43 /* The lexer.  */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46    and c-lex.c) and the C++ parser.  */
47
48 /* A token's value and its associated deferred access checks and
49    qualifying scope.  */
50
51 struct GTY(()) tree_check {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct GTY (()) cp_token {
64   /* The kind of token.  */
65   ENUM_BITFIELD (cpp_ttype) type : 8;
66   /* If this token is a keyword, this value indicates which keyword.
67      Otherwise, this value is RID_MAX.  */
68   ENUM_BITFIELD (rid) keyword : 8;
69   /* Token flags.  */
70   unsigned char flags;
71   /* Identifier for the pragma.  */
72   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73   /* True if this token is from a context where it is implicitly extern "C" */
74   BOOL_BITFIELD implicit_extern_c : 1;
75   /* True for a CPP_NAME token that is not a keyword (i.e., for which
76      KEYWORD is RID_MAX) iff this name was looked up and found to be
77      ambiguous.  An error has already been reported.  */
78   BOOL_BITFIELD ambiguous_p : 1;
79   /* The location at which this token was found.  */
80   location_t location;
81   /* The value associated with this token, if any.  */
82   union cp_token_value {
83     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
84     struct tree_check* GTY((tag ("1"))) tree_check_value;
85     /* Use for all other tokens.  */
86     tree GTY((tag ("0"))) value;
87   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
88 } cp_token;
89
90 /* We use a stack of token pointer for saving token sets.  */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
94
95 static cp_token eof_token =
96 {
97   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
98 };
99
100 /* The cp_lexer structure represents the C++ lexer.  It is responsible
101    for managing the token stream from the preprocessor and supplying
102    it to the parser.  Tokens are never added to the cp_lexer after
103    it is created.  */
104
105 typedef struct GTY (()) cp_lexer {
106   /* The memory allocated for the buffer.  NULL if this lexer does not
107      own the token buffer.  */
108   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109   /* If the lexer owns the buffer, this is the number of tokens in the
110      buffer.  */
111   size_t buffer_length;
112
113   /* A pointer just past the last available token.  The tokens
114      in this lexer are [buffer, last_token).  */
115   cp_token_position GTY ((skip)) last_token;
116
117   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
118      no more available tokens.  */
119   cp_token_position GTY ((skip)) next_token;
120
121   /* A stack indicating positions at which cp_lexer_save_tokens was
122      called.  The top entry is the most recent position at which we
123      began saving tokens.  If the stack is non-empty, we are saving
124      tokens.  */
125   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126
127   /* The next lexer in a linked list of lexers.  */
128   struct cp_lexer *next;
129
130   /* True if we should output debugging information.  */
131   bool debugging_p;
132
133   /* True if we're in the context of parsing a pragma, and should not
134      increment past the end-of-line marker.  */
135   bool in_pragma;
136 } cp_lexer;
137
138 /* cp_token_cache is a range of tokens.  There is no need to represent
139    allocate heap memory for it, since tokens are never removed from the
140    lexer's array.  There is also no need for the GC to walk through
141    a cp_token_cache, since everything in here is referenced through
142    a lexer.  */
143
144 typedef struct GTY(()) cp_token_cache {
145   /* The beginning of the token range.  */
146   cp_token * GTY((skip)) first;
147
148   /* Points immediately after the last token in the range.  */
149   cp_token * GTY ((skip)) last;
150 } cp_token_cache;
151
152 /* Prototypes.  */
153
154 static cp_lexer *cp_lexer_new_main
155   (void);
156 static cp_lexer *cp_lexer_new_from_tokens
157   (cp_token_cache *tokens);
158 static void cp_lexer_destroy
159   (cp_lexer *);
160 static int cp_lexer_saving_tokens
161   (const cp_lexer *);
162 static cp_token_position cp_lexer_token_position
163   (cp_lexer *, bool);
164 static cp_token *cp_lexer_token_at
165   (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167   (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
169   (cp_lexer *);
170 static cp_token *cp_lexer_peek_nth_token
171   (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173   (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175   (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177   (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
179   (cp_lexer *);
180 static void cp_lexer_purge_token
181   (cp_lexer *);
182 static void cp_lexer_purge_tokens_after
183   (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
185   (cp_lexer *);
186 static void cp_lexer_commit_tokens
187   (cp_lexer *);
188 static void cp_lexer_rollback_tokens
189   (cp_lexer *);
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192   (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
194   (cp_lexer *);
195 static void cp_lexer_start_debugging
196   (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198   (cp_lexer *) ATTRIBUTE_UNUSED;
199 #else
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201    about passing NULL to functions that require non-NULL arguments
202    (fputs, fprintf).  It will never be used, so all we need is a value
203    of the right type that's guaranteed not to be NULL.  */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
208
209 static cp_token_cache *cp_token_cache_new
210   (cp_token *, cp_token *);
211
212 static void cp_parser_initial_pragma
213   (cp_token *);
214
215 /* Manifest constants.  */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
218
219 /* A token type for keywords, as opposed to ordinary identifiers.  */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
221
222 /* A token type for template-ids.  If a template-id is processed while
223    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224    the value of the CPP_TEMPLATE_ID is whatever was returned by
225    cp_parser_template_id.  */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
227
228 /* A token type for nested-name-specifiers.  If a
229    nested-name-specifier is processed while parsing tentatively, it is
230    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232    cp_parser_nested_name_specifier_opt.  */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
234
235 /* A token type for tokens that are not tokens at all; these are used
236    to represent slots in the array where there used to be a token
237    that has now been deleted.  */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
239
240 /* The number of token types, including C++-specific ones.  */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
242
243 /* Variables.  */
244
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written.  */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
249
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251    sizeof, typeof, or alignof.  */
252 int cp_unevaluated_operand;
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255    preprocessor.  */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260   cp_token first_token;
261   cp_lexer *lexer;
262   cp_token *pos;
263   size_t alloc;
264   size_t space;
265   cp_token *buffer;
266
267   /* It's possible that parsing the first pragma will load a PCH file,
268      which is a GC collection point.  So we have to do that before
269      allocating any memory.  */
270   cp_parser_initial_pragma (&first_token);
271
272   c_common_no_more_pch ();
273
274   /* Allocate the memory.  */
275   lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278   /* Initially we are not debugging.  */
279   lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282                                    CP_SAVED_TOKEN_STACK);
283
284   /* Create the buffer.  */
285   alloc = CP_LEXER_BUFFER_SIZE;
286   buffer = GGC_NEWVEC (cp_token, alloc);
287
288   /* Put the first token in the buffer.  */
289   space = alloc;
290   pos = buffer;
291   *pos = first_token;
292
293   /* Get the remaining tokens from the preprocessor.  */
294   while (pos->type != CPP_EOF)
295     {
296       pos++;
297       if (!--space)
298         {
299           space = alloc;
300           alloc *= 2;
301           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302           pos = buffer + space;
303         }
304       cp_lexer_get_preprocessor_token (lexer, pos);
305     }
306   lexer->buffer = buffer;
307   lexer->buffer_length = alloc - space;
308   lexer->last_token = pos;
309   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311   /* Subsequent preprocessor diagnostics should use compiler
312      diagnostic functions to get the compiler source location.  */
313   done_lexing = true;
314
315   gcc_assert (lexer->next_token->type != CPP_PURGED);
316   return lexer;
317 }
318
319 /* Create a new lexer whose token stream is primed with the tokens in
320    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
321
322 static cp_lexer *
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
324 {
325   cp_token *first = cache->first;
326   cp_token *last = cache->last;
327   cp_lexer *lexer = GGC_CNEW (cp_lexer);
328
329   /* We do not own the buffer.  */
330   lexer->buffer = NULL;
331   lexer->buffer_length = 0;
332   lexer->next_token = first == last ? &eof_token : first;
333   lexer->last_token = last;
334
335   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336                                    CP_SAVED_TOKEN_STACK);
337
338 #ifdef ENABLE_CHECKING
339   /* Initially we are not debugging.  */
340   lexer->debugging_p = false;
341 #endif
342
343   gcc_assert (lexer->next_token->type != CPP_PURGED);
344   return lexer;
345 }
346
347 /* Frees all resources associated with LEXER.  */
348
349 static void
350 cp_lexer_destroy (cp_lexer *lexer)
351 {
352   if (lexer->buffer)
353     ggc_free (lexer->buffer);
354   VEC_free (cp_token_position, heap, lexer->saved_tokens);
355   ggc_free (lexer);
356 }
357
358 /* Returns nonzero if debugging information should be output.  */
359
360 #ifdef ENABLE_CHECKING
361
362 static inline bool
363 cp_lexer_debugging_p (cp_lexer *lexer)
364 {
365   return lexer->debugging_p;
366 }
367
368 #endif /* ENABLE_CHECKING */
369
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
372 {
373   gcc_assert (!previous_p || lexer->next_token != &eof_token);
374
375   return lexer->next_token - previous_p;
376 }
377
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
380 {
381   return pos;
382 }
383
384 /* nonzero if we are presently saving tokens.  */
385
386 static inline int
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
388 {
389   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
390 }
391
392 /* Store the next token from the preprocessor in *TOKEN.  Return true
393    if we reach EOF.  If LEXER is NULL, assume we are handling an
394    initial #pragma pch_preprocess, and thus want the lexer to return
395    processed strings.  */
396
397 static void
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
399 {
400   static int is_extern_c = 0;
401
402    /* Get a new token from the preprocessor.  */
403   token->type
404     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
406   token->keyword = RID_MAX;
407   token->pragma_kind = PRAGMA_NONE;
408
409   /* On some systems, some header files are surrounded by an
410      implicit extern "C" block.  Set a flag in the token if it
411      comes from such a header.  */
412   is_extern_c += pending_lang_change;
413   pending_lang_change = 0;
414   token->implicit_extern_c = is_extern_c > 0;
415
416   /* Check to see if this token is a keyword.  */
417   if (token->type == CPP_NAME)
418     {
419       if (C_IS_RESERVED_WORD (token->u.value))
420         {
421           /* Mark this token as a keyword.  */
422           token->type = CPP_KEYWORD;
423           /* Record which keyword.  */
424           token->keyword = C_RID_CODE (token->u.value);
425         }
426       else
427         {
428           if (warn_cxx0x_compat
429               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
431             {
432               /* Warn about the C++0x keyword (but still treat it as
433                  an identifier).  */
434               warning (OPT_Wc__0x_compat, 
435                        "identifier %qE will become a keyword in C++0x",
436                        token->u.value);
437
438               /* Clear out the C_RID_CODE so we don't warn about this
439                  particular identifier-turned-keyword again.  */
440               C_SET_RID_CODE (token->u.value, RID_MAX);
441             }
442
443           token->ambiguous_p = false;
444           token->keyword = RID_MAX;
445         }
446     }
447   /* Handle Objective-C++ keywords.  */
448   else if (token->type == CPP_AT_NAME)
449     {
450       token->type = CPP_KEYWORD;
451       switch (C_RID_CODE (token->u.value))
452         {
453         /* Map 'class' to '@class', 'private' to '@private', etc.  */
454         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458         case RID_THROW: token->keyword = RID_AT_THROW; break;
459         case RID_TRY: token->keyword = RID_AT_TRY; break;
460         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461         default: token->keyword = C_RID_CODE (token->u.value);
462         }
463     }
464   else if (token->type == CPP_PRAGMA)
465     {
466       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
467       token->pragma_kind = ((enum pragma_kind)
468                             TREE_INT_CST_LOW (token->u.value));
469       token->u.value = NULL_TREE;
470     }
471 }
472
473 /* Update the globals input_location and the input file stack from TOKEN.  */
474 static inline void
475 cp_lexer_set_source_position_from_token (cp_token *token)
476 {
477   if (token->type != CPP_EOF)
478     {
479       input_location = token->location;
480     }
481 }
482
483 /* Return a pointer to the next token in the token stream, but do not
484    consume it.  */
485
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
488 {
489   if (cp_lexer_debugging_p (lexer))
490     {
491       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493       putc ('\n', cp_lexer_debug_stream);
494     }
495   return lexer->next_token;
496 }
497
498 /* Return true if the next token has the indicated TYPE.  */
499
500 static inline bool
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
502 {
503   return cp_lexer_peek_token (lexer)->type == type;
504 }
505
506 /* Return true if the next token does not have the indicated TYPE.  */
507
508 static inline bool
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
510 {
511   return !cp_lexer_next_token_is (lexer, type);
512 }
513
514 /* Return true if the next token is the indicated KEYWORD.  */
515
516 static inline bool
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
518 {
519   return cp_lexer_peek_token (lexer)->keyword == keyword;
520 }
521
522 /* Return true if the next token is not the indicated KEYWORD.  */
523
524 static inline bool
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
526 {
527   return cp_lexer_peek_token (lexer)->keyword != keyword;
528 }
529
530 /* Return true if the next token is a keyword for a decl-specifier.  */
531
532 static bool
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
534 {
535   cp_token *token;
536
537   token = cp_lexer_peek_token (lexer);
538   switch (token->keyword) 
539     {
540       /* auto specifier: storage-class-specifier in C++,
541          simple-type-specifier in C++0x.  */
542     case RID_AUTO:
543       /* Storage classes.  */
544     case RID_REGISTER:
545     case RID_STATIC:
546     case RID_EXTERN:
547     case RID_MUTABLE:
548     case RID_THREAD:
549       /* Elaborated type specifiers.  */
550     case RID_ENUM:
551     case RID_CLASS:
552     case RID_STRUCT:
553     case RID_UNION:
554     case RID_TYPENAME:
555       /* Simple type specifiers.  */
556     case RID_CHAR:
557     case RID_CHAR16:
558     case RID_CHAR32:
559     case RID_WCHAR:
560     case RID_BOOL:
561     case RID_SHORT:
562     case RID_INT:
563     case RID_LONG:
564     case RID_SIGNED:
565     case RID_UNSIGNED:
566     case RID_FLOAT:
567     case RID_DOUBLE:
568     case RID_VOID:
569       /* GNU extensions.  */ 
570     case RID_ATTRIBUTE:
571     case RID_TYPEOF:
572       /* C++0x extensions.  */
573     case RID_DECLTYPE:
574       return true;
575
576     default:
577       return false;
578     }
579 }
580
581 /* Return a pointer to the Nth token in the token stream.  If N is 1,
582    then this is precisely equivalent to cp_lexer_peek_token (except
583    that it is not inline).  One would like to disallow that case, but
584    there is one case (cp_parser_nth_token_starts_template_id) where
585    the caller passes a variable for N and it might be 1.  */
586
587 static cp_token *
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
589 {
590   cp_token *token;
591
592   /* N is 1-based, not zero-based.  */
593   gcc_assert (n > 0);
594
595   if (cp_lexer_debugging_p (lexer))
596     fprintf (cp_lexer_debug_stream,
597              "cp_lexer: peeking ahead %ld at token: ", (long)n);
598
599   --n;
600   token = lexer->next_token;
601   gcc_assert (!n || token != &eof_token);
602   while (n != 0)
603     {
604       ++token;
605       if (token == lexer->last_token)
606         {
607           token = &eof_token;
608           break;
609         }
610
611       if (token->type != CPP_PURGED)
612         --n;
613     }
614
615   if (cp_lexer_debugging_p (lexer))
616     {
617       cp_lexer_print_token (cp_lexer_debug_stream, token);
618       putc ('\n', cp_lexer_debug_stream);
619     }
620
621   return token;
622 }
623
624 /* Return the next token, and advance the lexer's next_token pointer
625    to point to the next non-purged token.  */
626
627 static cp_token *
628 cp_lexer_consume_token (cp_lexer* lexer)
629 {
630   cp_token *token = lexer->next_token;
631
632   gcc_assert (token != &eof_token);
633   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
634
635   do
636     {
637       lexer->next_token++;
638       if (lexer->next_token == lexer->last_token)
639         {
640           lexer->next_token = &eof_token;
641           break;
642         }
643
644     }
645   while (lexer->next_token->type == CPP_PURGED);
646
647   cp_lexer_set_source_position_from_token (token);
648
649   /* Provide debugging output.  */
650   if (cp_lexer_debugging_p (lexer))
651     {
652       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653       cp_lexer_print_token (cp_lexer_debug_stream, token);
654       putc ('\n', cp_lexer_debug_stream);
655     }
656
657   return token;
658 }
659
660 /* Permanently remove the next token from the token stream, and
661    advance the next_token pointer to refer to the next non-purged
662    token.  */
663
664 static void
665 cp_lexer_purge_token (cp_lexer *lexer)
666 {
667   cp_token *tok = lexer->next_token;
668
669   gcc_assert (tok != &eof_token);
670   tok->type = CPP_PURGED;
671   tok->location = UNKNOWN_LOCATION;
672   tok->u.value = NULL_TREE;
673   tok->keyword = RID_MAX;
674
675   do
676     {
677       tok++;
678       if (tok == lexer->last_token)
679         {
680           tok = &eof_token;
681           break;
682         }
683     }
684   while (tok->type == CPP_PURGED);
685   lexer->next_token = tok;
686 }
687
688 /* Permanently remove all tokens after TOK, up to, but not
689    including, the token that will be returned next by
690    cp_lexer_peek_token.  */
691
692 static void
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
694 {
695   cp_token *peek = lexer->next_token;
696
697   if (peek == &eof_token)
698     peek = lexer->last_token;
699
700   gcc_assert (tok < peek);
701
702   for ( tok += 1; tok != peek; tok += 1)
703     {
704       tok->type = CPP_PURGED;
705       tok->location = UNKNOWN_LOCATION;
706       tok->u.value = NULL_TREE;
707       tok->keyword = RID_MAX;
708     }
709 }
710
711 /* Begin saving tokens.  All tokens consumed after this point will be
712    preserved.  */
713
714 static void
715 cp_lexer_save_tokens (cp_lexer* lexer)
716 {
717   /* Provide debugging output.  */
718   if (cp_lexer_debugging_p (lexer))
719     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
720
721   VEC_safe_push (cp_token_position, heap,
722                  lexer->saved_tokens, lexer->next_token);
723 }
724
725 /* Commit to the portion of the token stream most recently saved.  */
726
727 static void
728 cp_lexer_commit_tokens (cp_lexer* lexer)
729 {
730   /* Provide debugging output.  */
731   if (cp_lexer_debugging_p (lexer))
732     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
733
734   VEC_pop (cp_token_position, lexer->saved_tokens);
735 }
736
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738    to the token stream.  Stop saving tokens.  */
739
740 static void
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
742 {
743   /* Provide debugging output.  */
744   if (cp_lexer_debugging_p (lexer))
745     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
746
747   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
748 }
749
750 /* Print a representation of the TOKEN on the STREAM.  */
751
752 #ifdef ENABLE_CHECKING
753
754 static void
755 cp_lexer_print_token (FILE * stream, cp_token *token)
756 {
757   /* We don't use cpp_type2name here because the parser defines
758      a few tokens of its own.  */
759   static const char *const token_names[] = {
760     /* cpplib-defined token types */
761 #define OP(e, s) #e,
762 #define TK(e, s) #e,
763     TTYPE_TABLE
764 #undef OP
765 #undef TK
766     /* C++ parser token types - see "Manifest constants", above.  */
767     "KEYWORD",
768     "TEMPLATE_ID",
769     "NESTED_NAME_SPECIFIER",
770     "PURGED"
771   };
772
773   /* If we have a name for the token, print it out.  Otherwise, we
774      simply give the numeric code.  */
775   gcc_assert (token->type < ARRAY_SIZE(token_names));
776   fputs (token_names[token->type], stream);
777
778   /* For some tokens, print the associated data.  */
779   switch (token->type)
780     {
781     case CPP_KEYWORD:
782       /* Some keywords have a value that is not an IDENTIFIER_NODE.
783          For example, `struct' is mapped to an INTEGER_CST.  */
784       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
785         break;
786       /* else fall through */
787     case CPP_NAME:
788       fputs (IDENTIFIER_POINTER (token->u.value), stream);
789       break;
790
791     case CPP_STRING:
792     case CPP_STRING16:
793     case CPP_STRING32:
794     case CPP_WSTRING:
795     case CPP_UTF8STRING:
796       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
797       break;
798
799     default:
800       break;
801     }
802 }
803
804 /* Start emitting debugging information.  */
805
806 static void
807 cp_lexer_start_debugging (cp_lexer* lexer)
808 {
809   lexer->debugging_p = true;
810 }
811
812 /* Stop emitting debugging information.  */
813
814 static void
815 cp_lexer_stop_debugging (cp_lexer* lexer)
816 {
817   lexer->debugging_p = false;
818 }
819
820 #endif /* ENABLE_CHECKING */
821
822 /* Create a new cp_token_cache, representing a range of tokens.  */
823
824 static cp_token_cache *
825 cp_token_cache_new (cp_token *first, cp_token *last)
826 {
827   cp_token_cache *cache = GGC_NEW (cp_token_cache);
828   cache->first = first;
829   cache->last = last;
830   return cache;
831 }
832
833 \f
834 /* Decl-specifiers.  */
835
836 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
837
838 static void
839 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
840 {
841   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
842 }
843
844 /* Declarators.  */
845
846 /* Nothing other than the parser should be creating declarators;
847    declarators are a semi-syntactic representation of C++ entities.
848    Other parts of the front end that need to create entities (like
849    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
850
851 static cp_declarator *make_call_declarator
852   (cp_declarator *, tree, cp_cv_quals, tree, tree);
853 static cp_declarator *make_array_declarator
854   (cp_declarator *, tree);
855 static cp_declarator *make_pointer_declarator
856   (cp_cv_quals, cp_declarator *);
857 static cp_declarator *make_reference_declarator
858   (cp_cv_quals, cp_declarator *, bool);
859 static cp_parameter_declarator *make_parameter_declarator
860   (cp_decl_specifier_seq *, cp_declarator *, tree);
861 static cp_declarator *make_ptrmem_declarator
862   (cp_cv_quals, tree, cp_declarator *);
863
864 /* An erroneous declarator.  */
865 static cp_declarator *cp_error_declarator;
866
867 /* The obstack on which declarators and related data structures are
868    allocated.  */
869 static struct obstack declarator_obstack;
870
871 /* Alloc BYTES from the declarator memory pool.  */
872
873 static inline void *
874 alloc_declarator (size_t bytes)
875 {
876   return obstack_alloc (&declarator_obstack, bytes);
877 }
878
879 /* Allocate a declarator of the indicated KIND.  Clear fields that are
880    common to all declarators.  */
881
882 static cp_declarator *
883 make_declarator (cp_declarator_kind kind)
884 {
885   cp_declarator *declarator;
886
887   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
888   declarator->kind = kind;
889   declarator->attributes = NULL_TREE;
890   declarator->declarator = NULL;
891   declarator->parameter_pack_p = false;
892
893   return declarator;
894 }
895
896 /* Make a declarator for a generalized identifier.  If
897    QUALIFYING_SCOPE is non-NULL, the identifier is
898    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
899    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
900    is, if any.   */
901
902 static cp_declarator *
903 make_id_declarator (tree qualifying_scope, tree unqualified_name,
904                     special_function_kind sfk)
905 {
906   cp_declarator *declarator;
907
908   /* It is valid to write:
909
910        class C { void f(); };
911        typedef C D;
912        void D::f();
913
914      The standard is not clear about whether `typedef const C D' is
915      legal; as of 2002-09-15 the committee is considering that
916      question.  EDG 3.0 allows that syntax.  Therefore, we do as
917      well.  */
918   if (qualifying_scope && TYPE_P (qualifying_scope))
919     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
920
921   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
922               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
923               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
924
925   declarator = make_declarator (cdk_id);
926   declarator->u.id.qualifying_scope = qualifying_scope;
927   declarator->u.id.unqualified_name = unqualified_name;
928   declarator->u.id.sfk = sfk;
929   
930   return declarator;
931 }
932
933 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
934    of modifiers such as const or volatile to apply to the pointer
935    type, represented as identifiers.  */
936
937 cp_declarator *
938 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
939 {
940   cp_declarator *declarator;
941
942   declarator = make_declarator (cdk_pointer);
943   declarator->declarator = target;
944   declarator->u.pointer.qualifiers = cv_qualifiers;
945   declarator->u.pointer.class_type = NULL_TREE;
946   if (target)
947     {
948       declarator->parameter_pack_p = target->parameter_pack_p;
949       target->parameter_pack_p = false;
950     }
951   else
952     declarator->parameter_pack_p = false;
953
954   return declarator;
955 }
956
957 /* Like make_pointer_declarator -- but for references.  */
958
959 cp_declarator *
960 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
961                            bool rvalue_ref)
962 {
963   cp_declarator *declarator;
964
965   declarator = make_declarator (cdk_reference);
966   declarator->declarator = target;
967   declarator->u.reference.qualifiers = cv_qualifiers;
968   declarator->u.reference.rvalue_ref = rvalue_ref;
969   if (target)
970     {
971       declarator->parameter_pack_p = target->parameter_pack_p;
972       target->parameter_pack_p = false;
973     }
974   else
975     declarator->parameter_pack_p = false;
976
977   return declarator;
978 }
979
980 /* Like make_pointer_declarator -- but for a pointer to a non-static
981    member of CLASS_TYPE.  */
982
983 cp_declarator *
984 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
985                         cp_declarator *pointee)
986 {
987   cp_declarator *declarator;
988
989   declarator = make_declarator (cdk_ptrmem);
990   declarator->declarator = pointee;
991   declarator->u.pointer.qualifiers = cv_qualifiers;
992   declarator->u.pointer.class_type = class_type;
993
994   if (pointee)
995     {
996       declarator->parameter_pack_p = pointee->parameter_pack_p;
997       pointee->parameter_pack_p = false;
998     }
999   else
1000     declarator->parameter_pack_p = false;
1001
1002   return declarator;
1003 }
1004
1005 /* Make a declarator for the function given by TARGET, with the
1006    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1007    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1008    indicates what exceptions can be thrown.  */
1009
1010 cp_declarator *
1011 make_call_declarator (cp_declarator *target,
1012                       tree parms,
1013                       cp_cv_quals cv_qualifiers,
1014                       tree exception_specification,
1015                       tree late_return_type)
1016 {
1017   cp_declarator *declarator;
1018
1019   declarator = make_declarator (cdk_function);
1020   declarator->declarator = target;
1021   declarator->u.function.parameters = parms;
1022   declarator->u.function.qualifiers = cv_qualifiers;
1023   declarator->u.function.exception_specification = exception_specification;
1024   declarator->u.function.late_return_type = late_return_type;
1025   if (target)
1026     {
1027       declarator->parameter_pack_p = target->parameter_pack_p;
1028       target->parameter_pack_p = false;
1029     }
1030   else
1031     declarator->parameter_pack_p = false;
1032
1033   return declarator;
1034 }
1035
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037    defined by ELEMENT.  */
1038
1039 cp_declarator *
1040 make_array_declarator (cp_declarator *element, tree bounds)
1041 {
1042   cp_declarator *declarator;
1043
1044   declarator = make_declarator (cdk_array);
1045   declarator->declarator = element;
1046   declarator->u.array.bounds = bounds;
1047   if (element)
1048     {
1049       declarator->parameter_pack_p = element->parameter_pack_p;
1050       element->parameter_pack_p = false;
1051     }
1052   else
1053     declarator->parameter_pack_p = false;
1054
1055   return declarator;
1056 }
1057
1058 /* Determine whether the declarator we've seen so far can be a
1059    parameter pack, when followed by an ellipsis.  */
1060 static bool 
1061 declarator_can_be_parameter_pack (cp_declarator *declarator)
1062 {
1063   /* Search for a declarator name, or any other declarator that goes
1064      after the point where the ellipsis could appear in a parameter
1065      pack. If we find any of these, then this declarator can not be
1066      made into a parameter pack.  */
1067   bool found = false;
1068   while (declarator && !found)
1069     {
1070       switch ((int)declarator->kind)
1071         {
1072         case cdk_id:
1073         case cdk_array:
1074           found = true;
1075           break;
1076
1077         case cdk_error:
1078           return true;
1079
1080         default:
1081           declarator = declarator->declarator;
1082           break;
1083         }
1084     }
1085
1086   return !found;
1087 }
1088
1089 cp_parameter_declarator *no_parameters;
1090
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092    DECLARATOR and DEFAULT_ARGUMENT.  */
1093
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096                            cp_declarator *declarator,
1097                            tree default_argument)
1098 {
1099   cp_parameter_declarator *parameter;
1100
1101   parameter = ((cp_parameter_declarator *)
1102                alloc_declarator (sizeof (cp_parameter_declarator)));
1103   parameter->next = NULL;
1104   if (decl_specifiers)
1105     parameter->decl_specifiers = *decl_specifiers;
1106   else
1107     clear_decl_specs (&parameter->decl_specifiers);
1108   parameter->declarator = declarator;
1109   parameter->default_argument = default_argument;
1110   parameter->ellipsis_p = false;
1111
1112   return parameter;
1113 }
1114
1115 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1116
1117 static bool
1118 function_declarator_p (const cp_declarator *declarator)
1119 {
1120   while (declarator)
1121     {
1122       if (declarator->kind == cdk_function
1123           && declarator->declarator->kind == cdk_id)
1124         return true;
1125       if (declarator->kind == cdk_id
1126           || declarator->kind == cdk_error)
1127         return false;
1128       declarator = declarator->declarator;
1129     }
1130   return false;
1131 }
1132  
1133 /* The parser.  */
1134
1135 /* Overview
1136    --------
1137
1138    A cp_parser parses the token stream as specified by the C++
1139    grammar.  Its job is purely parsing, not semantic analysis.  For
1140    example, the parser breaks the token stream into declarators,
1141    expressions, statements, and other similar syntactic constructs.
1142    It does not check that the types of the expressions on either side
1143    of an assignment-statement are compatible, or that a function is
1144    not declared with a parameter of type `void'.
1145
1146    The parser invokes routines elsewhere in the compiler to perform
1147    semantic analysis and to build up the abstract syntax tree for the
1148    code processed.
1149
1150    The parser (and the template instantiation code, which is, in a
1151    way, a close relative of parsing) are the only parts of the
1152    compiler that should be calling push_scope and pop_scope, or
1153    related functions.  The parser (and template instantiation code)
1154    keeps track of what scope is presently active; everything else
1155    should simply honor that.  (The code that generates static
1156    initializers may also need to set the scope, in order to check
1157    access control correctly when emitting the initializers.)
1158
1159    Methodology
1160    -----------
1161
1162    The parser is of the standard recursive-descent variety.  Upcoming
1163    tokens in the token stream are examined in order to determine which
1164    production to use when parsing a non-terminal.  Some C++ constructs
1165    require arbitrary look ahead to disambiguate.  For example, it is
1166    impossible, in the general case, to tell whether a statement is an
1167    expression or declaration without scanning the entire statement.
1168    Therefore, the parser is capable of "parsing tentatively."  When the
1169    parser is not sure what construct comes next, it enters this mode.
1170    Then, while we attempt to parse the construct, the parser queues up
1171    error messages, rather than issuing them immediately, and saves the
1172    tokens it consumes.  If the construct is parsed successfully, the
1173    parser "commits", i.e., it issues any queued error messages and
1174    the tokens that were being preserved are permanently discarded.
1175    If, however, the construct is not parsed successfully, the parser
1176    rolls back its state completely so that it can resume parsing using
1177    a different alternative.
1178
1179    Future Improvements
1180    -------------------
1181
1182    The performance of the parser could probably be improved substantially.
1183    We could often eliminate the need to parse tentatively by looking ahead
1184    a little bit.  In some places, this approach might not entirely eliminate
1185    the need to parse tentatively, but it might still speed up the average
1186    case.  */
1187
1188 /* Flags that are passed to some parsing functions.  These values can
1189    be bitwise-ored together.  */
1190
1191 enum
1192 {
1193   /* No flags.  */
1194   CP_PARSER_FLAGS_NONE = 0x0,
1195   /* The construct is optional.  If it is not present, then no error
1196      should be issued.  */
1197   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198   /* When parsing a type-specifier, treat user-defined type-names
1199      as non-type identifiers.  */
1200   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1201   /* When parsing a type-specifier, do not try to parse a class-specifier
1202      or enum-specifier.  */
1203   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1204 };
1205
1206 /* This type is used for parameters and variables which hold
1207    combinations of the above flags.  */
1208 typedef int cp_parser_flags;
1209
1210 /* The different kinds of declarators we want to parse.  */
1211
1212 typedef enum cp_parser_declarator_kind
1213 {
1214   /* We want an abstract declarator.  */
1215   CP_PARSER_DECLARATOR_ABSTRACT,
1216   /* We want a named declarator.  */
1217   CP_PARSER_DECLARATOR_NAMED,
1218   /* We don't mind, but the name must be an unqualified-id.  */
1219   CP_PARSER_DECLARATOR_EITHER
1220 } cp_parser_declarator_kind;
1221
1222 /* The precedence values used to parse binary expressions.  The minimum value
1223    of PREC must be 1, because zero is reserved to quickly discriminate
1224    binary operators from other tokens.  */
1225
1226 enum cp_parser_prec
1227 {
1228   PREC_NOT_OPERATOR,
1229   PREC_LOGICAL_OR_EXPRESSION,
1230   PREC_LOGICAL_AND_EXPRESSION,
1231   PREC_INCLUSIVE_OR_EXPRESSION,
1232   PREC_EXCLUSIVE_OR_EXPRESSION,
1233   PREC_AND_EXPRESSION,
1234   PREC_EQUALITY_EXPRESSION,
1235   PREC_RELATIONAL_EXPRESSION,
1236   PREC_SHIFT_EXPRESSION,
1237   PREC_ADDITIVE_EXPRESSION,
1238   PREC_MULTIPLICATIVE_EXPRESSION,
1239   PREC_PM_EXPRESSION,
1240   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1241 };
1242
1243 /* A mapping from a token type to a corresponding tree node type, with a
1244    precedence value.  */
1245
1246 typedef struct cp_parser_binary_operations_map_node
1247 {
1248   /* The token type.  */
1249   enum cpp_ttype token_type;
1250   /* The corresponding tree code.  */
1251   enum tree_code tree_type;
1252   /* The precedence of this operator.  */
1253   enum cp_parser_prec prec;
1254 } cp_parser_binary_operations_map_node;
1255
1256 /* The status of a tentative parse.  */
1257
1258 typedef enum cp_parser_status_kind
1259 {
1260   /* No errors have occurred.  */
1261   CP_PARSER_STATUS_KIND_NO_ERROR,
1262   /* An error has occurred.  */
1263   CP_PARSER_STATUS_KIND_ERROR,
1264   /* We are committed to this tentative parse, whether or not an error
1265      has occurred.  */
1266   CP_PARSER_STATUS_KIND_COMMITTED
1267 } cp_parser_status_kind;
1268
1269 typedef struct cp_parser_expression_stack_entry
1270 {
1271   /* Left hand side of the binary operation we are currently
1272      parsing.  */
1273   tree lhs;
1274   /* Original tree code for left hand side, if it was a binary
1275      expression itself (used for -Wparentheses).  */
1276   enum tree_code lhs_type;
1277   /* Tree code for the binary operation we are parsing.  */
1278   enum tree_code tree_type;
1279   /* Precedence of the binary operation we are parsing.  */
1280   enum cp_parser_prec prec;
1281 } cp_parser_expression_stack_entry;
1282
1283 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1284    entries because precedence levels on the stack are monotonically
1285    increasing.  */
1286 typedef struct cp_parser_expression_stack_entry
1287   cp_parser_expression_stack[NUM_PREC_VALUES];
1288
1289 /* Context that is saved and restored when parsing tentatively.  */
1290 typedef struct GTY (()) cp_parser_context {
1291   /* If this is a tentative parsing context, the status of the
1292      tentative parse.  */
1293   enum cp_parser_status_kind status;
1294   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1295      that are looked up in this context must be looked up both in the
1296      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1297      the context of the containing expression.  */
1298   tree object_type;
1299
1300   /* The next parsing context in the stack.  */
1301   struct cp_parser_context *next;
1302 } cp_parser_context;
1303
1304 /* Prototypes.  */
1305
1306 /* Constructors and destructors.  */
1307
1308 static cp_parser_context *cp_parser_context_new
1309   (cp_parser_context *);
1310
1311 /* Class variables.  */
1312
1313 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1314
1315 /* The operator-precedence table used by cp_parser_binary_expression.
1316    Transformed into an associative array (binops_by_token) by
1317    cp_parser_new.  */
1318
1319 static const cp_parser_binary_operations_map_node binops[] = {
1320   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1321   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1322
1323   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326
1327   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1328   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1329
1330   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1331   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1332
1333   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1334   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1336   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1337
1338   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1339   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1340
1341   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1342
1343   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1344
1345   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1346
1347   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1348
1349   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1350 };
1351
1352 /* The same as binops, but initialized by cp_parser_new so that
1353    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1354    for speed.  */
1355 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1356
1357 /* Constructors and destructors.  */
1358
1359 /* Construct a new context.  The context below this one on the stack
1360    is given by NEXT.  */
1361
1362 static cp_parser_context *
1363 cp_parser_context_new (cp_parser_context* next)
1364 {
1365   cp_parser_context *context;
1366
1367   /* Allocate the storage.  */
1368   if (cp_parser_context_free_list != NULL)
1369     {
1370       /* Pull the first entry from the free list.  */
1371       context = cp_parser_context_free_list;
1372       cp_parser_context_free_list = context->next;
1373       memset (context, 0, sizeof (*context));
1374     }
1375   else
1376     context = GGC_CNEW (cp_parser_context);
1377
1378   /* No errors have occurred yet in this context.  */
1379   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1380   /* If this is not the bottommost context, copy information that we
1381      need from the previous context.  */
1382   if (next)
1383     {
1384       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1385          expression, then we are parsing one in this context, too.  */
1386       context->object_type = next->object_type;
1387       /* Thread the stack.  */
1388       context->next = next;
1389     }
1390
1391   return context;
1392 }
1393
1394 /* The cp_parser structure represents the C++ parser.  */
1395
1396 typedef struct GTY(()) cp_parser {
1397   /* The lexer from which we are obtaining tokens.  */
1398   cp_lexer *lexer;
1399
1400   /* The scope in which names should be looked up.  If NULL_TREE, then
1401      we look up names in the scope that is currently open in the
1402      source program.  If non-NULL, this is either a TYPE or
1403      NAMESPACE_DECL for the scope in which we should look.  It can
1404      also be ERROR_MARK, when we've parsed a bogus scope.
1405
1406      This value is not cleared automatically after a name is looked
1407      up, so we must be careful to clear it before starting a new look
1408      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1409      will look up `Z' in the scope of `X', rather than the current
1410      scope.)  Unfortunately, it is difficult to tell when name lookup
1411      is complete, because we sometimes peek at a token, look it up,
1412      and then decide not to consume it.   */
1413   tree scope;
1414
1415   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1416      last lookup took place.  OBJECT_SCOPE is used if an expression
1417      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1418      respectively.  QUALIFYING_SCOPE is used for an expression of the
1419      form "X::Y"; it refers to X.  */
1420   tree object_scope;
1421   tree qualifying_scope;
1422
1423   /* A stack of parsing contexts.  All but the bottom entry on the
1424      stack will be tentative contexts.
1425
1426      We parse tentatively in order to determine which construct is in
1427      use in some situations.  For example, in order to determine
1428      whether a statement is an expression-statement or a
1429      declaration-statement we parse it tentatively as a
1430      declaration-statement.  If that fails, we then reparse the same
1431      token stream as an expression-statement.  */
1432   cp_parser_context *context;
1433
1434   /* True if we are parsing GNU C++.  If this flag is not set, then
1435      GNU extensions are not recognized.  */
1436   bool allow_gnu_extensions_p;
1437
1438   /* TRUE if the `>' token should be interpreted as the greater-than
1439      operator.  FALSE if it is the end of a template-id or
1440      template-parameter-list. In C++0x mode, this flag also applies to
1441      `>>' tokens, which are viewed as two consecutive `>' tokens when
1442      this flag is FALSE.  */
1443   bool greater_than_is_operator_p;
1444
1445   /* TRUE if default arguments are allowed within a parameter list
1446      that starts at this point. FALSE if only a gnu extension makes
1447      them permissible.  */
1448   bool default_arg_ok_p;
1449
1450   /* TRUE if we are parsing an integral constant-expression.  See
1451      [expr.const] for a precise definition.  */
1452   bool integral_constant_expression_p;
1453
1454   /* TRUE if we are parsing an integral constant-expression -- but a
1455      non-constant expression should be permitted as well.  This flag
1456      is used when parsing an array bound so that GNU variable-length
1457      arrays are tolerated.  */
1458   bool allow_non_integral_constant_expression_p;
1459
1460   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1461      been seen that makes the expression non-constant.  */
1462   bool non_integral_constant_expression_p;
1463
1464   /* TRUE if local variable names and `this' are forbidden in the
1465      current context.  */
1466   bool local_variables_forbidden_p;
1467
1468   /* TRUE if the declaration we are parsing is part of a
1469      linkage-specification of the form `extern string-literal
1470      declaration'.  */
1471   bool in_unbraced_linkage_specification_p;
1472
1473   /* TRUE if we are presently parsing a declarator, after the
1474      direct-declarator.  */
1475   bool in_declarator_p;
1476
1477   /* TRUE if we are presently parsing a template-argument-list.  */
1478   bool in_template_argument_list_p;
1479
1480   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1481      to IN_OMP_BLOCK if parsing OpenMP structured block and
1482      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1483      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1484      iteration-statement, OpenMP block or loop within that switch.  */
1485 #define IN_SWITCH_STMT          1
1486 #define IN_ITERATION_STMT       2
1487 #define IN_OMP_BLOCK            4
1488 #define IN_OMP_FOR              8
1489 #define IN_IF_STMT             16
1490   unsigned char in_statement;
1491
1492   /* TRUE if we are presently parsing the body of a switch statement.
1493      Note that this doesn't quite overlap with in_statement above.
1494      The difference relates to giving the right sets of error messages:
1495      "case not in switch" vs "break statement used with OpenMP...".  */
1496   bool in_switch_statement_p;
1497
1498   /* TRUE if we are parsing a type-id in an expression context.  In
1499      such a situation, both "type (expr)" and "type (type)" are valid
1500      alternatives.  */
1501   bool in_type_id_in_expr_p;
1502
1503   /* TRUE if we are currently in a header file where declarations are
1504      implicitly extern "C".  */
1505   bool implicit_extern_c;
1506
1507   /* TRUE if strings in expressions should be translated to the execution
1508      character set.  */
1509   bool translate_strings_p;
1510
1511   /* TRUE if we are presently parsing the body of a function, but not
1512      a local class.  */
1513   bool in_function_body;
1514
1515   /* If non-NULL, then we are parsing a construct where new type
1516      definitions are not permitted.  The string stored here will be
1517      issued as an error message if a type is defined.  */
1518   const char *type_definition_forbidden_message;
1519
1520   /* A list of lists. The outer list is a stack, used for member
1521      functions of local classes. At each level there are two sub-list,
1522      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1523      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1524      TREE_VALUE's. The functions are chained in reverse declaration
1525      order.
1526
1527      The TREE_PURPOSE sublist contains those functions with default
1528      arguments that need post processing, and the TREE_VALUE sublist
1529      contains those functions with definitions that need post
1530      processing.
1531
1532      These lists can only be processed once the outermost class being
1533      defined is complete.  */
1534   tree unparsed_functions_queues;
1535
1536   /* The number of classes whose definitions are currently in
1537      progress.  */
1538   unsigned num_classes_being_defined;
1539
1540   /* The number of template parameter lists that apply directly to the
1541      current declaration.  */
1542   unsigned num_template_parameter_lists;
1543 } cp_parser;
1544
1545 /* Prototypes.  */
1546
1547 /* Constructors and destructors.  */
1548
1549 static cp_parser *cp_parser_new
1550   (void);
1551
1552 /* Routines to parse various constructs.
1553
1554    Those that return `tree' will return the error_mark_node (rather
1555    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1556    Sometimes, they will return an ordinary node if error-recovery was
1557    attempted, even though a parse error occurred.  So, to check
1558    whether or not a parse error occurred, you should always use
1559    cp_parser_error_occurred.  If the construct is optional (indicated
1560    either by an `_opt' in the name of the function that does the
1561    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1562    the construct is not present.  */
1563
1564 /* Lexical conventions [gram.lex]  */
1565
1566 static tree cp_parser_identifier
1567   (cp_parser *);
1568 static tree cp_parser_string_literal
1569   (cp_parser *, bool, bool);
1570
1571 /* Basic concepts [gram.basic]  */
1572
1573 static bool cp_parser_translation_unit
1574   (cp_parser *);
1575
1576 /* Expressions [gram.expr]  */
1577
1578 static tree cp_parser_primary_expression
1579   (cp_parser *, bool, bool, bool, cp_id_kind *);
1580 static tree cp_parser_id_expression
1581   (cp_parser *, bool, bool, bool *, bool, bool);
1582 static tree cp_parser_unqualified_id
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier_opt
1585   (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier
1587   (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_qualifying_entity
1589   (cp_parser *, bool, bool, bool, bool, bool);
1590 static tree cp_parser_postfix_expression
1591   (cp_parser *, bool, bool, bool, cp_id_kind *);
1592 static tree cp_parser_postfix_open_square_expression
1593   (cp_parser *, tree, bool);
1594 static tree cp_parser_postfix_dot_deref_expression
1595   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1596 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1597   (cp_parser *, bool, bool, bool, bool *);
1598 static void cp_parser_pseudo_destructor_name
1599   (cp_parser *, tree *, tree *);
1600 static tree cp_parser_unary_expression
1601   (cp_parser *, bool, bool, cp_id_kind *);
1602 static enum tree_code cp_parser_unary_operator
1603   (cp_token *);
1604 static tree cp_parser_new_expression
1605   (cp_parser *);
1606 static VEC(tree,gc) *cp_parser_new_placement
1607   (cp_parser *);
1608 static tree cp_parser_new_type_id
1609   (cp_parser *, tree *);
1610 static cp_declarator *cp_parser_new_declarator_opt
1611   (cp_parser *);
1612 static cp_declarator *cp_parser_direct_new_declarator
1613   (cp_parser *);
1614 static VEC(tree,gc) *cp_parser_new_initializer
1615   (cp_parser *);
1616 static tree cp_parser_delete_expression
1617   (cp_parser *);
1618 static tree cp_parser_cast_expression
1619   (cp_parser *, bool, bool, cp_id_kind *);
1620 static tree cp_parser_binary_expression
1621   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1622 static tree cp_parser_question_colon_clause
1623   (cp_parser *, tree);
1624 static tree cp_parser_assignment_expression
1625   (cp_parser *, bool, cp_id_kind *);
1626 static enum tree_code cp_parser_assignment_operator_opt
1627   (cp_parser *);
1628 static tree cp_parser_expression
1629   (cp_parser *, bool, cp_id_kind *);
1630 static tree cp_parser_constant_expression
1631   (cp_parser *, bool, bool *);
1632 static tree cp_parser_builtin_offsetof
1633   (cp_parser *);
1634 static tree cp_parser_lambda_expression
1635   (cp_parser *);
1636 static void cp_parser_lambda_introducer
1637   (cp_parser *, tree);
1638 static void cp_parser_lambda_declarator_opt
1639   (cp_parser *, tree);
1640 static void cp_parser_lambda_body
1641   (cp_parser *, tree);
1642
1643 /* Statements [gram.stmt.stmt]  */
1644
1645 static void cp_parser_statement
1646   (cp_parser *, tree, bool, bool *);
1647 static void cp_parser_label_for_labeled_statement
1648   (cp_parser *);
1649 static tree cp_parser_expression_statement
1650   (cp_parser *, tree);
1651 static tree cp_parser_compound_statement
1652   (cp_parser *, tree, bool);
1653 static void cp_parser_statement_seq_opt
1654   (cp_parser *, tree);
1655 static tree cp_parser_selection_statement
1656   (cp_parser *, bool *);
1657 static tree cp_parser_condition
1658   (cp_parser *);
1659 static tree cp_parser_iteration_statement
1660   (cp_parser *);
1661 static void cp_parser_for_init_statement
1662   (cp_parser *);
1663 static tree cp_parser_jump_statement
1664   (cp_parser *);
1665 static void cp_parser_declaration_statement
1666   (cp_parser *);
1667
1668 static tree cp_parser_implicitly_scoped_statement
1669   (cp_parser *, bool *);
1670 static void cp_parser_already_scoped_statement
1671   (cp_parser *);
1672
1673 /* Declarations [gram.dcl.dcl] */
1674
1675 static void cp_parser_declaration_seq_opt
1676   (cp_parser *);
1677 static void cp_parser_declaration
1678   (cp_parser *);
1679 static void cp_parser_block_declaration
1680   (cp_parser *, bool);
1681 static void cp_parser_simple_declaration
1682   (cp_parser *, bool);
1683 static void cp_parser_decl_specifier_seq
1684   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1685 static tree cp_parser_storage_class_specifier_opt
1686   (cp_parser *);
1687 static tree cp_parser_function_specifier_opt
1688   (cp_parser *, cp_decl_specifier_seq *);
1689 static tree cp_parser_type_specifier
1690   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1691    int *, bool *);
1692 static tree cp_parser_simple_type_specifier
1693   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1694 static tree cp_parser_type_name
1695   (cp_parser *);
1696 static tree cp_parser_nonclass_name 
1697   (cp_parser* parser);
1698 static tree cp_parser_elaborated_type_specifier
1699   (cp_parser *, bool, bool);
1700 static tree cp_parser_enum_specifier
1701   (cp_parser *);
1702 static void cp_parser_enumerator_list
1703   (cp_parser *, tree);
1704 static void cp_parser_enumerator_definition
1705   (cp_parser *, tree);
1706 static tree cp_parser_namespace_name
1707   (cp_parser *);
1708 static void cp_parser_namespace_definition
1709   (cp_parser *);
1710 static void cp_parser_namespace_body
1711   (cp_parser *);
1712 static tree cp_parser_qualified_namespace_specifier
1713   (cp_parser *);
1714 static void cp_parser_namespace_alias_definition
1715   (cp_parser *);
1716 static bool cp_parser_using_declaration
1717   (cp_parser *, bool);
1718 static void cp_parser_using_directive
1719   (cp_parser *);
1720 static void cp_parser_asm_definition
1721   (cp_parser *);
1722 static void cp_parser_linkage_specification
1723   (cp_parser *);
1724 static void cp_parser_static_assert
1725   (cp_parser *, bool);
1726 static tree cp_parser_decltype
1727   (cp_parser *);
1728
1729 /* Declarators [gram.dcl.decl] */
1730
1731 static tree cp_parser_init_declarator
1732   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1733 static cp_declarator *cp_parser_declarator
1734   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1735 static cp_declarator *cp_parser_direct_declarator
1736   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1737 static enum tree_code cp_parser_ptr_operator
1738   (cp_parser *, tree *, cp_cv_quals *);
1739 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1740   (cp_parser *);
1741 static tree cp_parser_late_return_type_opt
1742   (cp_parser *);
1743 static tree cp_parser_declarator_id
1744   (cp_parser *, bool);
1745 static tree cp_parser_type_id
1746   (cp_parser *);
1747 static tree cp_parser_template_type_arg
1748   (cp_parser *);
1749 static tree cp_parser_trailing_type_id (cp_parser *);
1750 static tree cp_parser_type_id_1
1751   (cp_parser *, bool, bool);
1752 static void cp_parser_type_specifier_seq
1753   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1754 static tree cp_parser_parameter_declaration_clause
1755   (cp_parser *);
1756 static tree cp_parser_parameter_declaration_list
1757   (cp_parser *, bool *);
1758 static cp_parameter_declarator *cp_parser_parameter_declaration
1759   (cp_parser *, bool, bool *);
1760 static tree cp_parser_default_argument 
1761   (cp_parser *, bool);
1762 static void cp_parser_function_body
1763   (cp_parser *);
1764 static tree cp_parser_initializer
1765   (cp_parser *, bool *, bool *);
1766 static tree cp_parser_initializer_clause
1767   (cp_parser *, bool *);
1768 static tree cp_parser_braced_list
1769   (cp_parser*, bool*);
1770 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1771   (cp_parser *, bool *);
1772
1773 static bool cp_parser_ctor_initializer_opt_and_function_body
1774   (cp_parser *);
1775
1776 /* Classes [gram.class] */
1777
1778 static tree cp_parser_class_name
1779   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1780 static tree cp_parser_class_specifier
1781   (cp_parser *);
1782 static tree cp_parser_class_head
1783   (cp_parser *, bool *, tree *, tree *);
1784 static enum tag_types cp_parser_class_key
1785   (cp_parser *);
1786 static void cp_parser_member_specification_opt
1787   (cp_parser *);
1788 static void cp_parser_member_declaration
1789   (cp_parser *);
1790 static tree cp_parser_pure_specifier
1791   (cp_parser *);
1792 static tree cp_parser_constant_initializer
1793   (cp_parser *);
1794
1795 /* Derived classes [gram.class.derived] */
1796
1797 static tree cp_parser_base_clause
1798   (cp_parser *);
1799 static tree cp_parser_base_specifier
1800   (cp_parser *);
1801
1802 /* Special member functions [gram.special] */
1803
1804 static tree cp_parser_conversion_function_id
1805   (cp_parser *);
1806 static tree cp_parser_conversion_type_id
1807   (cp_parser *);
1808 static cp_declarator *cp_parser_conversion_declarator_opt
1809   (cp_parser *);
1810 static bool cp_parser_ctor_initializer_opt
1811   (cp_parser *);
1812 static void cp_parser_mem_initializer_list
1813   (cp_parser *);
1814 static tree cp_parser_mem_initializer
1815   (cp_parser *);
1816 static tree cp_parser_mem_initializer_id
1817   (cp_parser *);
1818
1819 /* Overloading [gram.over] */
1820
1821 static tree cp_parser_operator_function_id
1822   (cp_parser *);
1823 static tree cp_parser_operator
1824   (cp_parser *);
1825
1826 /* Templates [gram.temp] */
1827
1828 static void cp_parser_template_declaration
1829   (cp_parser *, bool);
1830 static tree cp_parser_template_parameter_list
1831   (cp_parser *);
1832 static tree cp_parser_template_parameter
1833   (cp_parser *, bool *, bool *);
1834 static tree cp_parser_type_parameter
1835   (cp_parser *, bool *);
1836 static tree cp_parser_template_id
1837   (cp_parser *, bool, bool, bool);
1838 static tree cp_parser_template_name
1839   (cp_parser *, bool, bool, bool, bool *);
1840 static tree cp_parser_template_argument_list
1841   (cp_parser *);
1842 static tree cp_parser_template_argument
1843   (cp_parser *);
1844 static void cp_parser_explicit_instantiation
1845   (cp_parser *);
1846 static void cp_parser_explicit_specialization
1847   (cp_parser *);
1848
1849 /* Exception handling [gram.exception] */
1850
1851 static tree cp_parser_try_block
1852   (cp_parser *);
1853 static bool cp_parser_function_try_block
1854   (cp_parser *);
1855 static void cp_parser_handler_seq
1856   (cp_parser *);
1857 static void cp_parser_handler
1858   (cp_parser *);
1859 static tree cp_parser_exception_declaration
1860   (cp_parser *);
1861 static tree cp_parser_throw_expression
1862   (cp_parser *);
1863 static tree cp_parser_exception_specification_opt
1864   (cp_parser *);
1865 static tree cp_parser_type_id_list
1866   (cp_parser *);
1867
1868 /* GNU Extensions */
1869
1870 static tree cp_parser_asm_specification_opt
1871   (cp_parser *);
1872 static tree cp_parser_asm_operand_list
1873   (cp_parser *);
1874 static tree cp_parser_asm_clobber_list
1875   (cp_parser *);
1876 static tree cp_parser_asm_label_list
1877   (cp_parser *);
1878 static tree cp_parser_attributes_opt
1879   (cp_parser *);
1880 static tree cp_parser_attribute_list
1881   (cp_parser *);
1882 static bool cp_parser_extension_opt
1883   (cp_parser *, int *);
1884 static void cp_parser_label_declaration
1885   (cp_parser *);
1886
1887 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1888 static bool cp_parser_pragma
1889   (cp_parser *, enum pragma_context);
1890
1891 /* Objective-C++ Productions */
1892
1893 static tree cp_parser_objc_message_receiver
1894   (cp_parser *);
1895 static tree cp_parser_objc_message_args
1896   (cp_parser *);
1897 static tree cp_parser_objc_message_expression
1898   (cp_parser *);
1899 static tree cp_parser_objc_encode_expression
1900   (cp_parser *);
1901 static tree cp_parser_objc_defs_expression
1902   (cp_parser *);
1903 static tree cp_parser_objc_protocol_expression
1904   (cp_parser *);
1905 static tree cp_parser_objc_selector_expression
1906   (cp_parser *);
1907 static tree cp_parser_objc_expression
1908   (cp_parser *);
1909 static bool cp_parser_objc_selector_p
1910   (enum cpp_ttype);
1911 static tree cp_parser_objc_selector
1912   (cp_parser *);
1913 static tree cp_parser_objc_protocol_refs_opt
1914   (cp_parser *);
1915 static void cp_parser_objc_declaration
1916   (cp_parser *);
1917 static tree cp_parser_objc_statement
1918   (cp_parser *);
1919
1920 /* Utility Routines */
1921
1922 static tree cp_parser_lookup_name
1923   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1924 static tree cp_parser_lookup_name_simple
1925   (cp_parser *, tree, location_t);
1926 static tree cp_parser_maybe_treat_template_as_class
1927   (tree, bool);
1928 static bool cp_parser_check_declarator_template_parameters
1929   (cp_parser *, cp_declarator *, location_t);
1930 static bool cp_parser_check_template_parameters
1931   (cp_parser *, unsigned, location_t, cp_declarator *);
1932 static tree cp_parser_simple_cast_expression
1933   (cp_parser *);
1934 static tree cp_parser_global_scope_opt
1935   (cp_parser *, bool);
1936 static bool cp_parser_constructor_declarator_p
1937   (cp_parser *, bool);
1938 static tree cp_parser_function_definition_from_specifiers_and_declarator
1939   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1940 static tree cp_parser_function_definition_after_declarator
1941   (cp_parser *, bool);
1942 static void cp_parser_template_declaration_after_export
1943   (cp_parser *, bool);
1944 static void cp_parser_perform_template_parameter_access_checks
1945   (VEC (deferred_access_check,gc)*);
1946 static tree cp_parser_single_declaration
1947   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1948 static tree cp_parser_functional_cast
1949   (cp_parser *, tree);
1950 static tree cp_parser_save_member_function_body
1951   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1952 static tree cp_parser_enclosed_template_argument_list
1953   (cp_parser *);
1954 static void cp_parser_save_default_args
1955   (cp_parser *, tree);
1956 static void cp_parser_late_parsing_for_member
1957   (cp_parser *, tree);
1958 static void cp_parser_late_parsing_default_args
1959   (cp_parser *, tree);
1960 static tree cp_parser_sizeof_operand
1961   (cp_parser *, enum rid);
1962 static tree cp_parser_trait_expr
1963   (cp_parser *, enum rid);
1964 static bool cp_parser_declares_only_class_p
1965   (cp_parser *);
1966 static void cp_parser_set_storage_class
1967   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1968 static void cp_parser_set_decl_spec_type
1969   (cp_decl_specifier_seq *, tree, location_t, bool);
1970 static bool cp_parser_friend_p
1971   (const cp_decl_specifier_seq *);
1972 static cp_token *cp_parser_require
1973   (cp_parser *, enum cpp_ttype, const char *);
1974 static cp_token *cp_parser_require_keyword
1975   (cp_parser *, enum rid, const char *);
1976 static bool cp_parser_token_starts_function_definition_p
1977   (cp_token *);
1978 static bool cp_parser_next_token_starts_class_definition_p
1979   (cp_parser *);
1980 static bool cp_parser_next_token_ends_template_argument_p
1981   (cp_parser *);
1982 static bool cp_parser_nth_token_starts_template_argument_list_p
1983   (cp_parser *, size_t);
1984 static enum tag_types cp_parser_token_is_class_key
1985   (cp_token *);
1986 static void cp_parser_check_class_key
1987   (enum tag_types, tree type);
1988 static void cp_parser_check_access_in_redeclaration
1989   (tree type, location_t location);
1990 static bool cp_parser_optional_template_keyword
1991   (cp_parser *);
1992 static void cp_parser_pre_parsed_nested_name_specifier
1993   (cp_parser *);
1994 static bool cp_parser_cache_group
1995   (cp_parser *, enum cpp_ttype, unsigned);
1996 static void cp_parser_parse_tentatively
1997   (cp_parser *);
1998 static void cp_parser_commit_to_tentative_parse
1999   (cp_parser *);
2000 static void cp_parser_abort_tentative_parse
2001   (cp_parser *);
2002 static bool cp_parser_parse_definitely
2003   (cp_parser *);
2004 static inline bool cp_parser_parsing_tentatively
2005   (cp_parser *);
2006 static bool cp_parser_uncommitted_to_tentative_parse_p
2007   (cp_parser *);
2008 static void cp_parser_error
2009   (cp_parser *, const char *);
2010 static void cp_parser_name_lookup_error
2011   (cp_parser *, tree, tree, const char *, location_t);
2012 static bool cp_parser_simulate_error
2013   (cp_parser *);
2014 static bool cp_parser_check_type_definition
2015   (cp_parser *);
2016 static void cp_parser_check_for_definition_in_return_type
2017   (cp_declarator *, tree, location_t type_location);
2018 static void cp_parser_check_for_invalid_template_id
2019   (cp_parser *, tree, location_t location);
2020 static bool cp_parser_non_integral_constant_expression
2021   (cp_parser *, const char *);
2022 static void cp_parser_diagnose_invalid_type_name
2023   (cp_parser *, tree, tree, location_t);
2024 static bool cp_parser_parse_and_diagnose_invalid_type_name
2025   (cp_parser *);
2026 static int cp_parser_skip_to_closing_parenthesis
2027   (cp_parser *, bool, bool, bool);
2028 static void cp_parser_skip_to_end_of_statement
2029   (cp_parser *);
2030 static void cp_parser_consume_semicolon_at_end_of_statement
2031   (cp_parser *);
2032 static void cp_parser_skip_to_end_of_block_or_statement
2033   (cp_parser *);
2034 static bool cp_parser_skip_to_closing_brace
2035   (cp_parser *);
2036 static void cp_parser_skip_to_end_of_template_parameter_list
2037   (cp_parser *);
2038 static void cp_parser_skip_to_pragma_eol
2039   (cp_parser*, cp_token *);
2040 static bool cp_parser_error_occurred
2041   (cp_parser *);
2042 static bool cp_parser_allow_gnu_extensions_p
2043   (cp_parser *);
2044 static bool cp_parser_is_string_literal
2045   (cp_token *);
2046 static bool cp_parser_is_keyword
2047   (cp_token *, enum rid);
2048 static tree cp_parser_make_typename_type
2049   (cp_parser *, tree, tree, location_t location);
2050 static cp_declarator * cp_parser_make_indirect_declarator
2051   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2052
2053 /* Returns nonzero if we are parsing tentatively.  */
2054
2055 static inline bool
2056 cp_parser_parsing_tentatively (cp_parser* parser)
2057 {
2058   return parser->context->next != NULL;
2059 }
2060
2061 /* Returns nonzero if TOKEN is a string literal.  */
2062
2063 static bool
2064 cp_parser_is_string_literal (cp_token* token)
2065 {
2066   return (token->type == CPP_STRING ||
2067           token->type == CPP_STRING16 ||
2068           token->type == CPP_STRING32 ||
2069           token->type == CPP_WSTRING ||
2070           token->type == CPP_UTF8STRING);
2071 }
2072
2073 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2074
2075 static bool
2076 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2077 {
2078   return token->keyword == keyword;
2079 }
2080
2081 /* If not parsing tentatively, issue a diagnostic of the form
2082       FILE:LINE: MESSAGE before TOKEN
2083    where TOKEN is the next token in the input stream.  MESSAGE
2084    (specified by the caller) is usually of the form "expected
2085    OTHER-TOKEN".  */
2086
2087 static void
2088 cp_parser_error (cp_parser* parser, const char* message)
2089 {
2090   if (!cp_parser_simulate_error (parser))
2091     {
2092       cp_token *token = cp_lexer_peek_token (parser->lexer);
2093       /* This diagnostic makes more sense if it is tagged to the line
2094          of the token we just peeked at.  */
2095       cp_lexer_set_source_position_from_token (token);
2096
2097       if (token->type == CPP_PRAGMA)
2098         {
2099           error_at (token->location,
2100                     "%<#pragma%> is not allowed here");
2101           cp_parser_skip_to_pragma_eol (parser, token);
2102           return;
2103         }
2104
2105       c_parse_error (message,
2106                      /* Because c_parser_error does not understand
2107                         CPP_KEYWORD, keywords are treated like
2108                         identifiers.  */
2109                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2110                      token->u.value, token->flags);
2111     }
2112 }
2113
2114 /* Issue an error about name-lookup failing.  NAME is the
2115    IDENTIFIER_NODE DECL is the result of
2116    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2117    the thing that we hoped to find.  */
2118
2119 static void
2120 cp_parser_name_lookup_error (cp_parser* parser,
2121                              tree name,
2122                              tree decl,
2123                              const char* desired,
2124                              location_t location)
2125 {
2126   /* If name lookup completely failed, tell the user that NAME was not
2127      declared.  */
2128   if (decl == error_mark_node)
2129     {
2130       if (parser->scope && parser->scope != global_namespace)
2131         error_at (location, "%<%E::%E%> has not been declared",
2132                   parser->scope, name);
2133       else if (parser->scope == global_namespace)
2134         error_at (location, "%<::%E%> has not been declared", name);
2135       else if (parser->object_scope
2136                && !CLASS_TYPE_P (parser->object_scope))
2137         error_at (location, "request for member %qE in non-class type %qT",
2138                   name, parser->object_scope);
2139       else if (parser->object_scope)
2140         error_at (location, "%<%T::%E%> has not been declared",
2141                   parser->object_scope, name);
2142       else
2143         error_at (location, "%qE has not been declared", name);
2144     }
2145   else if (parser->scope && parser->scope != global_namespace)
2146     error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2147   else if (parser->scope == global_namespace)
2148     error_at (location, "%<::%E%> %s", name, desired);
2149   else
2150     error_at (location, "%qE %s", name, desired);
2151 }
2152
2153 /* If we are parsing tentatively, remember that an error has occurred
2154    during this tentative parse.  Returns true if the error was
2155    simulated; false if a message should be issued by the caller.  */
2156
2157 static bool
2158 cp_parser_simulate_error (cp_parser* parser)
2159 {
2160   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2161     {
2162       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2163       return true;
2164     }
2165   return false;
2166 }
2167
2168 /* Check for repeated decl-specifiers.  */
2169
2170 static void
2171 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2172                            location_t location)
2173 {
2174   int ds;
2175
2176   for (ds = ds_first; ds != ds_last; ++ds)
2177     {
2178       unsigned count = decl_specs->specs[ds];
2179       if (count < 2)
2180         continue;
2181       /* The "long" specifier is a special case because of "long long".  */
2182       if (ds == ds_long)
2183         {
2184           if (count > 2)
2185             error_at (location, "%<long long long%> is too long for GCC");
2186           else 
2187             pedwarn_cxx98 (location, OPT_Wlong_long, 
2188                            "ISO C++ 1998 does not support %<long long%>");
2189         }
2190       else if (count > 1)
2191         {
2192           static const char *const decl_spec_names[] = {
2193             "signed",
2194             "unsigned",
2195             "short",
2196             "long",
2197             "const",
2198             "volatile",
2199             "restrict",
2200             "inline",
2201             "virtual",
2202             "explicit",
2203             "friend",
2204             "typedef",
2205             "constexpr",
2206             "__complex",
2207             "__thread"
2208           };
2209           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2210         }
2211     }
2212 }
2213
2214 /* This function is called when a type is defined.  If type
2215    definitions are forbidden at this point, an error message is
2216    issued.  */
2217
2218 static bool
2219 cp_parser_check_type_definition (cp_parser* parser)
2220 {
2221   /* If types are forbidden here, issue a message.  */
2222   if (parser->type_definition_forbidden_message)
2223     {
2224       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2225          in the message need to be interpreted.  */
2226       error (parser->type_definition_forbidden_message);
2227       return false;
2228     }
2229   return true;
2230 }
2231
2232 /* This function is called when the DECLARATOR is processed.  The TYPE
2233    was a type defined in the decl-specifiers.  If it is invalid to
2234    define a type in the decl-specifiers for DECLARATOR, an error is
2235    issued. TYPE_LOCATION is the location of TYPE and is used
2236    for error reporting.  */
2237
2238 static void
2239 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2240                                                tree type, location_t type_location)
2241 {
2242   /* [dcl.fct] forbids type definitions in return types.
2243      Unfortunately, it's not easy to know whether or not we are
2244      processing a return type until after the fact.  */
2245   while (declarator
2246          && (declarator->kind == cdk_pointer
2247              || declarator->kind == cdk_reference
2248              || declarator->kind == cdk_ptrmem))
2249     declarator = declarator->declarator;
2250   if (declarator
2251       && declarator->kind == cdk_function)
2252     {
2253       error_at (type_location,
2254                 "new types may not be defined in a return type");
2255       inform (type_location, 
2256               "(perhaps a semicolon is missing after the definition of %qT)",
2257               type);
2258     }
2259 }
2260
2261 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2262    "<" in any valid C++ program.  If the next token is indeed "<",
2263    issue a message warning the user about what appears to be an
2264    invalid attempt to form a template-id. LOCATION is the location
2265    of the type-specifier (TYPE) */
2266
2267 static void
2268 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2269                                          tree type, location_t location)
2270 {
2271   cp_token_position start = 0;
2272
2273   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2274     {
2275       if (TYPE_P (type))
2276         error_at (location, "%qT is not a template", type);
2277       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2278         error_at (location, "%qE is not a template", type);
2279       else
2280         error_at (location, "invalid template-id");
2281       /* Remember the location of the invalid "<".  */
2282       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2283         start = cp_lexer_token_position (parser->lexer, true);
2284       /* Consume the "<".  */
2285       cp_lexer_consume_token (parser->lexer);
2286       /* Parse the template arguments.  */
2287       cp_parser_enclosed_template_argument_list (parser);
2288       /* Permanently remove the invalid template arguments so that
2289          this error message is not issued again.  */
2290       if (start)
2291         cp_lexer_purge_tokens_after (parser->lexer, start);
2292     }
2293 }
2294
2295 /* If parsing an integral constant-expression, issue an error message
2296    about the fact that THING appeared and return true.  Otherwise,
2297    return false.  In either case, set
2298    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2299
2300 static bool
2301 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2302                                             const char *thing)
2303 {
2304   parser->non_integral_constant_expression_p = true;
2305   if (parser->integral_constant_expression_p)
2306     {
2307       if (!parser->allow_non_integral_constant_expression_p)
2308         {
2309           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2310              in the message need to be interpreted.  */
2311           char *message = concat (thing,
2312                                   " cannot appear in a constant-expression",
2313                                   NULL);
2314           error (message);
2315           free (message);
2316           return true;
2317         }
2318     }
2319   return false;
2320 }
2321
2322 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2323    qualifying scope (or NULL, if none) for ID.  This function commits
2324    to the current active tentative parse, if any.  (Otherwise, the
2325    problematic construct might be encountered again later, resulting
2326    in duplicate error messages.) LOCATION is the location of ID.  */
2327
2328 static void
2329 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2330                                       tree scope, tree id,
2331                                       location_t location)
2332 {
2333   tree decl, old_scope;
2334   /* Try to lookup the identifier.  */
2335   old_scope = parser->scope;
2336   parser->scope = scope;
2337   decl = cp_parser_lookup_name_simple (parser, id, location);
2338   parser->scope = old_scope;
2339   /* If the lookup found a template-name, it means that the user forgot
2340   to specify an argument list. Emit a useful error message.  */
2341   if (TREE_CODE (decl) == TEMPLATE_DECL)
2342     error_at (location,
2343               "invalid use of template-name %qE without an argument list",
2344               decl);
2345   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2346     error_at (location, "invalid use of destructor %qD as a type", id);
2347   else if (TREE_CODE (decl) == TYPE_DECL)
2348     /* Something like 'unsigned A a;'  */
2349     error_at (location, "invalid combination of multiple type-specifiers");
2350   else if (!parser->scope)
2351     {
2352       /* Issue an error message.  */
2353       error_at (location, "%qE does not name a type", id);
2354       /* If we're in a template class, it's possible that the user was
2355          referring to a type from a base class.  For example:
2356
2357            template <typename T> struct A { typedef T X; };
2358            template <typename T> struct B : public A<T> { X x; };
2359
2360          The user should have said "typename A<T>::X".  */
2361       if (processing_template_decl && current_class_type
2362           && TYPE_BINFO (current_class_type))
2363         {
2364           tree b;
2365
2366           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2367                b;
2368                b = TREE_CHAIN (b))
2369             {
2370               tree base_type = BINFO_TYPE (b);
2371               if (CLASS_TYPE_P (base_type)
2372                   && dependent_type_p (base_type))
2373                 {
2374                   tree field;
2375                   /* Go from a particular instantiation of the
2376                      template (which will have an empty TYPE_FIELDs),
2377                      to the main version.  */
2378                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2379                   for (field = TYPE_FIELDS (base_type);
2380                        field;
2381                        field = TREE_CHAIN (field))
2382                     if (TREE_CODE (field) == TYPE_DECL
2383                         && DECL_NAME (field) == id)
2384                       {
2385                         inform (location, 
2386                                 "(perhaps %<typename %T::%E%> was intended)",
2387                                 BINFO_TYPE (b), id);
2388                         break;
2389                       }
2390                   if (field)
2391                     break;
2392                 }
2393             }
2394         }
2395     }
2396   /* Here we diagnose qualified-ids where the scope is actually correct,
2397      but the identifier does not resolve to a valid type name.  */
2398   else if (parser->scope != error_mark_node)
2399     {
2400       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2401         error_at (location, "%qE in namespace %qE does not name a type",
2402                   id, parser->scope);
2403       else if (TYPE_P (parser->scope)
2404                && dependent_scope_p (parser->scope))
2405         error_at (location, "need %<typename%> before %<%T::%E%> because "
2406                   "%qT is a dependent scope",
2407                   parser->scope, id, parser->scope);
2408       else if (TYPE_P (parser->scope))
2409         error_at (location, "%qE in class %qT does not name a type",
2410                   id, parser->scope);
2411       else
2412         gcc_unreachable ();
2413     }
2414   cp_parser_commit_to_tentative_parse (parser);
2415 }
2416
2417 /* Check for a common situation where a type-name should be present,
2418    but is not, and issue a sensible error message.  Returns true if an
2419    invalid type-name was detected.
2420
2421    The situation handled by this function are variable declarations of the
2422    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2423    Usually, `ID' should name a type, but if we got here it means that it
2424    does not. We try to emit the best possible error message depending on
2425    how exactly the id-expression looks like.  */
2426
2427 static bool
2428 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2429 {
2430   tree id;
2431   cp_token *token = cp_lexer_peek_token (parser->lexer);
2432
2433   cp_parser_parse_tentatively (parser);
2434   id = cp_parser_id_expression (parser,
2435                                 /*template_keyword_p=*/false,
2436                                 /*check_dependency_p=*/true,
2437                                 /*template_p=*/NULL,
2438                                 /*declarator_p=*/true,
2439                                 /*optional_p=*/false);
2440   /* If the next token is a (, this is a function with no explicit return
2441      type, i.e. constructor, destructor or conversion op.  */
2442   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2443       || TREE_CODE (id) == TYPE_DECL)
2444     {
2445       cp_parser_abort_tentative_parse (parser);
2446       return false;
2447     }
2448   if (!cp_parser_parse_definitely (parser))
2449     return false;
2450
2451   /* Emit a diagnostic for the invalid type.  */
2452   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2453                                         id, token->location);
2454   /* If we aren't in the middle of a declarator (i.e. in a
2455      parameter-declaration-clause), skip to the end of the declaration;
2456      there's no point in trying to process it.  */
2457   if (!parser->in_declarator_p)
2458     cp_parser_skip_to_end_of_block_or_statement (parser);
2459   return true;
2460 }
2461
2462 /* Consume tokens up to, and including, the next non-nested closing `)'.
2463    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2464    are doing error recovery. Returns -1 if OR_COMMA is true and we
2465    found an unnested comma.  */
2466
2467 static int
2468 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2469                                        bool recovering,
2470                                        bool or_comma,
2471                                        bool consume_paren)
2472 {
2473   unsigned paren_depth = 0;
2474   unsigned brace_depth = 0;
2475   unsigned square_depth = 0;
2476
2477   if (recovering && !or_comma
2478       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2479     return 0;
2480
2481   while (true)
2482     {
2483       cp_token * token = cp_lexer_peek_token (parser->lexer);
2484
2485       switch (token->type)
2486         {
2487         case CPP_EOF:
2488         case CPP_PRAGMA_EOL:
2489           /* If we've run out of tokens, then there is no closing `)'.  */
2490           return 0;
2491
2492         /* This is good for lambda expression capture-lists.  */
2493         case CPP_OPEN_SQUARE:
2494           ++square_depth;
2495           break;
2496         case CPP_CLOSE_SQUARE:
2497           if (!square_depth--)
2498             return 0;
2499           break;
2500
2501         case CPP_SEMICOLON:
2502           /* This matches the processing in skip_to_end_of_statement.  */
2503           if (!brace_depth)
2504             return 0;
2505           break;
2506
2507         case CPP_OPEN_BRACE:
2508           ++brace_depth;
2509           break;
2510         case CPP_CLOSE_BRACE:
2511           if (!brace_depth--)
2512             return 0;
2513           break;
2514
2515         case CPP_COMMA:
2516           if (recovering && or_comma && !brace_depth && !paren_depth
2517               && !square_depth)
2518             return -1;
2519           break;
2520
2521         case CPP_OPEN_PAREN:
2522           if (!brace_depth)
2523             ++paren_depth;
2524           break;
2525
2526         case CPP_CLOSE_PAREN:
2527           if (!brace_depth && !paren_depth--)
2528             {
2529               if (consume_paren)
2530                 cp_lexer_consume_token (parser->lexer);
2531               return 1;
2532             }
2533           break;
2534
2535         default:
2536           break;
2537         }
2538
2539       /* Consume the token.  */
2540       cp_lexer_consume_token (parser->lexer);
2541     }
2542 }
2543
2544 /* Consume tokens until we reach the end of the current statement.
2545    Normally, that will be just before consuming a `;'.  However, if a
2546    non-nested `}' comes first, then we stop before consuming that.  */
2547
2548 static void
2549 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2550 {
2551   unsigned nesting_depth = 0;
2552
2553   while (true)
2554     {
2555       cp_token *token = cp_lexer_peek_token (parser->lexer);
2556
2557       switch (token->type)
2558         {
2559         case CPP_EOF:
2560         case CPP_PRAGMA_EOL:
2561           /* If we've run out of tokens, stop.  */
2562           return;
2563
2564         case CPP_SEMICOLON:
2565           /* If the next token is a `;', we have reached the end of the
2566              statement.  */
2567           if (!nesting_depth)
2568             return;
2569           break;
2570
2571         case CPP_CLOSE_BRACE:
2572           /* If this is a non-nested '}', stop before consuming it.
2573              That way, when confronted with something like:
2574
2575                { 3 + }
2576
2577              we stop before consuming the closing '}', even though we
2578              have not yet reached a `;'.  */
2579           if (nesting_depth == 0)
2580             return;
2581
2582           /* If it is the closing '}' for a block that we have
2583              scanned, stop -- but only after consuming the token.
2584              That way given:
2585
2586                 void f g () { ... }
2587                 typedef int I;
2588
2589              we will stop after the body of the erroneously declared
2590              function, but before consuming the following `typedef'
2591              declaration.  */
2592           if (--nesting_depth == 0)
2593             {
2594               cp_lexer_consume_token (parser->lexer);
2595               return;
2596             }
2597
2598         case CPP_OPEN_BRACE:
2599           ++nesting_depth;
2600           break;
2601
2602         default:
2603           break;
2604         }
2605
2606       /* Consume the token.  */
2607       cp_lexer_consume_token (parser->lexer);
2608     }
2609 }
2610
2611 /* This function is called at the end of a statement or declaration.
2612    If the next token is a semicolon, it is consumed; otherwise, error
2613    recovery is attempted.  */
2614
2615 static void
2616 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2617 {
2618   /* Look for the trailing `;'.  */
2619   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2620     {
2621       /* If there is additional (erroneous) input, skip to the end of
2622          the statement.  */
2623       cp_parser_skip_to_end_of_statement (parser);
2624       /* If the next token is now a `;', consume it.  */
2625       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2626         cp_lexer_consume_token (parser->lexer);
2627     }
2628 }
2629
2630 /* Skip tokens until we have consumed an entire block, or until we
2631    have consumed a non-nested `;'.  */
2632
2633 static void
2634 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2635 {
2636   int nesting_depth = 0;
2637
2638   while (nesting_depth >= 0)
2639     {
2640       cp_token *token = cp_lexer_peek_token (parser->lexer);
2641
2642       switch (token->type)
2643         {
2644         case CPP_EOF:
2645         case CPP_PRAGMA_EOL:
2646           /* If we've run out of tokens, stop.  */
2647           return;
2648
2649         case CPP_SEMICOLON:
2650           /* Stop if this is an unnested ';'. */
2651           if (!nesting_depth)
2652             nesting_depth = -1;
2653           break;
2654
2655         case CPP_CLOSE_BRACE:
2656           /* Stop if this is an unnested '}', or closes the outermost
2657              nesting level.  */
2658           nesting_depth--;
2659           if (nesting_depth < 0)
2660             return;
2661           if (!nesting_depth)
2662             nesting_depth = -1;
2663           break;
2664
2665         case CPP_OPEN_BRACE:
2666           /* Nest. */
2667           nesting_depth++;
2668           break;
2669
2670         default:
2671           break;
2672         }
2673
2674       /* Consume the token.  */
2675       cp_lexer_consume_token (parser->lexer);
2676     }
2677 }
2678
2679 /* Skip tokens until a non-nested closing curly brace is the next
2680    token, or there are no more tokens. Return true in the first case,
2681    false otherwise.  */
2682
2683 static bool
2684 cp_parser_skip_to_closing_brace (cp_parser *parser)
2685 {
2686   unsigned nesting_depth = 0;
2687
2688   while (true)
2689     {
2690       cp_token *token = cp_lexer_peek_token (parser->lexer);
2691
2692       switch (token->type)
2693         {
2694         case CPP_EOF:
2695         case CPP_PRAGMA_EOL:
2696           /* If we've run out of tokens, stop.  */
2697           return false;
2698
2699         case CPP_CLOSE_BRACE:
2700           /* If the next token is a non-nested `}', then we have reached
2701              the end of the current block.  */
2702           if (nesting_depth-- == 0)
2703             return true;
2704           break;
2705
2706         case CPP_OPEN_BRACE:
2707           /* If it the next token is a `{', then we are entering a new
2708              block.  Consume the entire block.  */
2709           ++nesting_depth;
2710           break;
2711
2712         default:
2713           break;
2714         }
2715
2716       /* Consume the token.  */
2717       cp_lexer_consume_token (parser->lexer);
2718     }
2719 }
2720
2721 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2722    parameter is the PRAGMA token, allowing us to purge the entire pragma
2723    sequence.  */
2724
2725 static void
2726 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2727 {
2728   cp_token *token;
2729
2730   parser->lexer->in_pragma = false;
2731
2732   do
2733     token = cp_lexer_consume_token (parser->lexer);
2734   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2735
2736   /* Ensure that the pragma is not parsed again.  */
2737   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2738 }
2739
2740 /* Require pragma end of line, resyncing with it as necessary.  The
2741    arguments are as for cp_parser_skip_to_pragma_eol.  */
2742
2743 static void
2744 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2745 {
2746   parser->lexer->in_pragma = false;
2747   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2748     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2749 }
2750
2751 /* This is a simple wrapper around make_typename_type. When the id is
2752    an unresolved identifier node, we can provide a superior diagnostic
2753    using cp_parser_diagnose_invalid_type_name.  */
2754
2755 static tree
2756 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2757                               tree id, location_t id_location)
2758 {
2759   tree result;
2760   if (TREE_CODE (id) == IDENTIFIER_NODE)
2761     {
2762       result = make_typename_type (scope, id, typename_type,
2763                                    /*complain=*/tf_none);
2764       if (result == error_mark_node)
2765         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2766       return result;
2767     }
2768   return make_typename_type (scope, id, typename_type, tf_error);
2769 }
2770
2771 /* This is a wrapper around the
2772    make_{pointer,ptrmem,reference}_declarator functions that decides
2773    which one to call based on the CODE and CLASS_TYPE arguments. The
2774    CODE argument should be one of the values returned by
2775    cp_parser_ptr_operator. */
2776 static cp_declarator *
2777 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2778                                     cp_cv_quals cv_qualifiers,
2779                                     cp_declarator *target)
2780 {
2781   if (code == ERROR_MARK)
2782     return cp_error_declarator;
2783
2784   if (code == INDIRECT_REF)
2785     if (class_type == NULL_TREE)
2786       return make_pointer_declarator (cv_qualifiers, target);
2787     else
2788       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2789   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2790     return make_reference_declarator (cv_qualifiers, target, false);
2791   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2792     return make_reference_declarator (cv_qualifiers, target, true);
2793   gcc_unreachable ();
2794 }
2795
2796 /* Create a new C++ parser.  */
2797
2798 static cp_parser *
2799 cp_parser_new (void)
2800 {
2801   cp_parser *parser;
2802   cp_lexer *lexer;
2803   unsigned i;
2804
2805   /* cp_lexer_new_main is called before calling ggc_alloc because
2806      cp_lexer_new_main might load a PCH file.  */
2807   lexer = cp_lexer_new_main ();
2808
2809   /* Initialize the binops_by_token so that we can get the tree
2810      directly from the token.  */
2811   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2812     binops_by_token[binops[i].token_type] = binops[i];
2813
2814   parser = GGC_CNEW (cp_parser);
2815   parser->lexer = lexer;
2816   parser->context = cp_parser_context_new (NULL);
2817
2818   /* For now, we always accept GNU extensions.  */
2819   parser->allow_gnu_extensions_p = 1;
2820
2821   /* The `>' token is a greater-than operator, not the end of a
2822      template-id.  */
2823   parser->greater_than_is_operator_p = true;
2824
2825   parser->default_arg_ok_p = true;
2826
2827   /* We are not parsing a constant-expression.  */
2828   parser->integral_constant_expression_p = false;
2829   parser->allow_non_integral_constant_expression_p = false;
2830   parser->non_integral_constant_expression_p = false;
2831
2832   /* Local variable names are not forbidden.  */
2833   parser->local_variables_forbidden_p = false;
2834
2835   /* We are not processing an `extern "C"' declaration.  */
2836   parser->in_unbraced_linkage_specification_p = false;
2837
2838   /* We are not processing a declarator.  */
2839   parser->in_declarator_p = false;
2840
2841   /* We are not processing a template-argument-list.  */
2842   parser->in_template_argument_list_p = false;
2843
2844   /* We are not in an iteration statement.  */
2845   parser->in_statement = 0;
2846
2847   /* We are not in a switch statement.  */
2848   parser->in_switch_statement_p = false;
2849
2850   /* We are not parsing a type-id inside an expression.  */
2851   parser->in_type_id_in_expr_p = false;
2852
2853   /* Declarations aren't implicitly extern "C".  */
2854   parser->implicit_extern_c = false;
2855
2856   /* String literals should be translated to the execution character set.  */
2857   parser->translate_strings_p = true;
2858
2859   /* We are not parsing a function body.  */
2860   parser->in_function_body = false;
2861
2862   /* The unparsed function queue is empty.  */
2863   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2864
2865   /* There are no classes being defined.  */
2866   parser->num_classes_being_defined = 0;
2867
2868   /* No template parameters apply.  */
2869   parser->num_template_parameter_lists = 0;
2870
2871   return parser;
2872 }
2873
2874 /* Create a cp_lexer structure which will emit the tokens in CACHE
2875    and push it onto the parser's lexer stack.  This is used for delayed
2876    parsing of in-class method bodies and default arguments, and should
2877    not be confused with tentative parsing.  */
2878 static void
2879 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2880 {
2881   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2882   lexer->next = parser->lexer;
2883   parser->lexer = lexer;
2884
2885   /* Move the current source position to that of the first token in the
2886      new lexer.  */
2887   cp_lexer_set_source_position_from_token (lexer->next_token);
2888 }
2889
2890 /* Pop the top lexer off the parser stack.  This is never used for the
2891    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2892 static void
2893 cp_parser_pop_lexer (cp_parser *parser)
2894 {
2895   cp_lexer *lexer = parser->lexer;
2896   parser->lexer = lexer->next;
2897   cp_lexer_destroy (lexer);
2898
2899   /* Put the current source position back where it was before this
2900      lexer was pushed.  */
2901   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2902 }
2903
2904 /* Lexical conventions [gram.lex]  */
2905
2906 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2907    identifier.  */
2908
2909 static tree
2910 cp_parser_identifier (cp_parser* parser)
2911 {
2912   cp_token *token;
2913
2914   /* Look for the identifier.  */
2915   token = cp_parser_require (parser, CPP_NAME, "identifier");
2916   /* Return the value.  */
2917   return token ? token->u.value : error_mark_node;
2918 }
2919
2920 /* Parse a sequence of adjacent string constants.  Returns a
2921    TREE_STRING representing the combined, nul-terminated string
2922    constant.  If TRANSLATE is true, translate the string to the
2923    execution character set.  If WIDE_OK is true, a wide string is
2924    invalid here.
2925
2926    C++98 [lex.string] says that if a narrow string literal token is
2927    adjacent to a wide string literal token, the behavior is undefined.
2928    However, C99 6.4.5p4 says that this results in a wide string literal.
2929    We follow C99 here, for consistency with the C front end.
2930
2931    This code is largely lifted from lex_string() in c-lex.c.
2932
2933    FUTURE: ObjC++ will need to handle @-strings here.  */
2934 static tree
2935 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2936 {
2937   tree value;
2938   size_t count;
2939   struct obstack str_ob;
2940   cpp_string str, istr, *strs;
2941   cp_token *tok;
2942   enum cpp_ttype type;
2943
2944   tok = cp_lexer_peek_token (parser->lexer);
2945   if (!cp_parser_is_string_literal (tok))
2946     {
2947       cp_parser_error (parser, "expected string-literal");
2948       return error_mark_node;
2949     }
2950
2951   type = tok->type;
2952
2953   /* Try to avoid the overhead of creating and destroying an obstack
2954      for the common case of just one string.  */
2955   if (!cp_parser_is_string_literal
2956       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2957     {
2958       cp_lexer_consume_token (parser->lexer);
2959
2960       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2961       str.len = TREE_STRING_LENGTH (tok->u.value);
2962       count = 1;
2963
2964       strs = &str;
2965     }
2966   else
2967     {
2968       gcc_obstack_init (&str_ob);
2969       count = 0;
2970
2971       do
2972         {
2973           cp_lexer_consume_token (parser->lexer);
2974           count++;
2975           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2976           str.len = TREE_STRING_LENGTH (tok->u.value);
2977
2978           if (type != tok->type)
2979             {
2980               if (type == CPP_STRING)
2981                 type = tok->type;
2982               else if (tok->type != CPP_STRING)
2983                 error_at (tok->location,
2984                           "unsupported non-standard concatenation "
2985                           "of string literals");
2986             }
2987
2988           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2989
2990           tok = cp_lexer_peek_token (parser->lexer);
2991         }
2992       while (cp_parser_is_string_literal (tok));
2993
2994       strs = (cpp_string *) obstack_finish (&str_ob);
2995     }
2996
2997   if (type != CPP_STRING && !wide_ok)
2998     {
2999       cp_parser_error (parser, "a wide string is invalid in this context");
3000       type = CPP_STRING;
3001     }
3002
3003   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3004       (parse_in, strs, count, &istr, type))
3005     {
3006       value = build_string (istr.len, (const char *)istr.text);
3007       free (CONST_CAST (unsigned char *, istr.text));
3008
3009       switch (type)
3010         {
3011         default:
3012         case CPP_STRING:
3013         case CPP_UTF8STRING:
3014           TREE_TYPE (value) = char_array_type_node;
3015           break;
3016         case CPP_STRING16:
3017           TREE_TYPE (value) = char16_array_type_node;
3018           break;
3019         case CPP_STRING32:
3020           TREE_TYPE (value) = char32_array_type_node;
3021           break;
3022         case CPP_WSTRING:
3023           TREE_TYPE (value) = wchar_array_type_node;
3024           break;
3025         }
3026
3027       value = fix_string_type (value);
3028     }
3029   else
3030     /* cpp_interpret_string has issued an error.  */
3031     value = error_mark_node;
3032
3033   if (count > 1)
3034     obstack_free (&str_ob, 0);
3035
3036   return value;
3037 }
3038
3039
3040 /* Basic concepts [gram.basic]  */
3041
3042 /* Parse a translation-unit.
3043
3044    translation-unit:
3045      declaration-seq [opt]
3046
3047    Returns TRUE if all went well.  */
3048
3049 static bool
3050 cp_parser_translation_unit (cp_parser* parser)
3051 {
3052   /* The address of the first non-permanent object on the declarator
3053      obstack.  */
3054   static void *declarator_obstack_base;
3055
3056   bool success;
3057
3058   /* Create the declarator obstack, if necessary.  */
3059   if (!cp_error_declarator)
3060     {
3061       gcc_obstack_init (&declarator_obstack);
3062       /* Create the error declarator.  */
3063       cp_error_declarator = make_declarator (cdk_error);
3064       /* Create the empty parameter list.  */
3065       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3066       /* Remember where the base of the declarator obstack lies.  */
3067       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3068     }
3069
3070   cp_parser_declaration_seq_opt (parser);
3071
3072   /* If there are no tokens left then all went well.  */
3073   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3074     {
3075       /* Get rid of the token array; we don't need it any more.  */
3076       cp_lexer_destroy (parser->lexer);
3077       parser->lexer = NULL;
3078
3079       /* This file might have been a context that's implicitly extern
3080          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3081       if (parser->implicit_extern_c)
3082         {
3083           pop_lang_context ();
3084           parser->implicit_extern_c = false;
3085         }
3086
3087       /* Finish up.  */
3088       finish_translation_unit ();
3089
3090       success = true;
3091     }
3092   else
3093     {
3094       cp_parser_error (parser, "expected declaration");
3095       success = false;
3096     }
3097
3098   /* Make sure the declarator obstack was fully cleaned up.  */
3099   gcc_assert (obstack_next_free (&declarator_obstack)
3100               == declarator_obstack_base);
3101
3102   /* All went well.  */
3103   return success;
3104 }
3105
3106 /* Expressions [gram.expr] */
3107
3108 /* Parse a primary-expression.
3109
3110    primary-expression:
3111      literal
3112      this
3113      ( expression )
3114      id-expression
3115
3116    GNU Extensions:
3117
3118    primary-expression:
3119      ( compound-statement )
3120      __builtin_va_arg ( assignment-expression , type-id )
3121      __builtin_offsetof ( type-id , offsetof-expression )
3122
3123    C++ Extensions:
3124      __has_nothrow_assign ( type-id )   
3125      __has_nothrow_constructor ( type-id )
3126      __has_nothrow_copy ( type-id )
3127      __has_trivial_assign ( type-id )   
3128      __has_trivial_constructor ( type-id )
3129      __has_trivial_copy ( type-id )
3130      __has_trivial_destructor ( type-id )
3131      __has_virtual_destructor ( type-id )     
3132      __is_abstract ( type-id )
3133      __is_base_of ( type-id , type-id )
3134      __is_class ( type-id )
3135      __is_convertible_to ( type-id , type-id )     
3136      __is_empty ( type-id )
3137      __is_enum ( type-id )
3138      __is_pod ( type-id )
3139      __is_polymorphic ( type-id )
3140      __is_union ( type-id )
3141
3142    Objective-C++ Extension:
3143
3144    primary-expression:
3145      objc-expression
3146
3147    literal:
3148      __null
3149
3150    ADDRESS_P is true iff this expression was immediately preceded by
3151    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3152    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3153    true iff this expression is a template argument.
3154
3155    Returns a representation of the expression.  Upon return, *IDK
3156    indicates what kind of id-expression (if any) was present.  */
3157
3158 static tree
3159 cp_parser_primary_expression (cp_parser *parser,
3160                               bool address_p,
3161                               bool cast_p,
3162                               bool template_arg_p,
3163                               cp_id_kind *idk)
3164 {
3165   cp_token *token = NULL;
3166
3167   /* Assume the primary expression is not an id-expression.  */
3168   *idk = CP_ID_KIND_NONE;
3169
3170   /* Peek at the next token.  */
3171   token = cp_lexer_peek_token (parser->lexer);
3172   switch (token->type)
3173     {
3174       /* literal:
3175            integer-literal
3176            character-literal
3177            floating-literal
3178            string-literal
3179            boolean-literal  */
3180     case CPP_CHAR:
3181     case CPP_CHAR16:
3182     case CPP_CHAR32:
3183     case CPP_WCHAR:
3184     case CPP_NUMBER:
3185       token = cp_lexer_consume_token (parser->lexer);
3186       if (TREE_CODE (token->u.value) == FIXED_CST)
3187         {
3188           error_at (token->location,
3189                     "fixed-point types not supported in C++");
3190           return error_mark_node;
3191         }
3192       /* Floating-point literals are only allowed in an integral
3193          constant expression if they are cast to an integral or
3194          enumeration type.  */
3195       if (TREE_CODE (token->u.value) == REAL_CST
3196           && parser->integral_constant_expression_p
3197           && pedantic)
3198         {
3199           /* CAST_P will be set even in invalid code like "int(2.7 +
3200              ...)".   Therefore, we have to check that the next token
3201              is sure to end the cast.  */
3202           if (cast_p)
3203             {
3204               cp_token *next_token;
3205
3206               next_token = cp_lexer_peek_token (parser->lexer);
3207               if (/* The comma at the end of an
3208                      enumerator-definition.  */
3209                   next_token->type != CPP_COMMA
3210                   /* The curly brace at the end of an enum-specifier.  */
3211                   && next_token->type != CPP_CLOSE_BRACE
3212                   /* The end of a statement.  */
3213                   && next_token->type != CPP_SEMICOLON
3214                   /* The end of the cast-expression.  */
3215                   && next_token->type != CPP_CLOSE_PAREN
3216                   /* The end of an array bound.  */
3217                   && next_token->type != CPP_CLOSE_SQUARE
3218                   /* The closing ">" in a template-argument-list.  */
3219                   && (next_token->type != CPP_GREATER
3220                       || parser->greater_than_is_operator_p)
3221                   /* C++0x only: A ">>" treated like two ">" tokens,
3222                      in a template-argument-list.  */
3223                   && (next_token->type != CPP_RSHIFT
3224                       || (cxx_dialect == cxx98)
3225                       || parser->greater_than_is_operator_p))
3226                 cast_p = false;
3227             }
3228
3229           /* If we are within a cast, then the constraint that the
3230              cast is to an integral or enumeration type will be
3231              checked at that point.  If we are not within a cast, then
3232              this code is invalid.  */
3233           if (!cast_p)
3234             cp_parser_non_integral_constant_expression
3235               (parser, "floating-point literal");
3236         }
3237       return token->u.value;
3238
3239     case CPP_STRING:
3240     case CPP_STRING16:
3241     case CPP_STRING32:
3242     case CPP_WSTRING:
3243     case CPP_UTF8STRING:
3244       /* ??? Should wide strings be allowed when parser->translate_strings_p
3245          is false (i.e. in attributes)?  If not, we can kill the third
3246          argument to cp_parser_string_literal.  */
3247       return cp_parser_string_literal (parser,
3248                                        parser->translate_strings_p,
3249                                        true);
3250
3251     case CPP_OPEN_PAREN:
3252       {
3253         tree expr;
3254         bool saved_greater_than_is_operator_p;
3255
3256         /* Consume the `('.  */
3257         cp_lexer_consume_token (parser->lexer);
3258         /* Within a parenthesized expression, a `>' token is always
3259            the greater-than operator.  */
3260         saved_greater_than_is_operator_p
3261           = parser->greater_than_is_operator_p;
3262         parser->greater_than_is_operator_p = true;
3263         /* If we see `( { ' then we are looking at the beginning of
3264            a GNU statement-expression.  */
3265         if (cp_parser_allow_gnu_extensions_p (parser)
3266             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3267           {
3268             /* Statement-expressions are not allowed by the standard.  */
3269             pedwarn (token->location, OPT_pedantic, 
3270                      "ISO C++ forbids braced-groups within expressions");
3271
3272             /* And they're not allowed outside of a function-body; you
3273                cannot, for example, write:
3274
3275                  int i = ({ int j = 3; j + 1; });
3276
3277                at class or namespace scope.  */
3278             if (!parser->in_function_body
3279                 || parser->in_template_argument_list_p)
3280               {
3281                 error_at (token->location,
3282                           "statement-expressions are not allowed outside "
3283                           "functions nor in template-argument lists");
3284                 cp_parser_skip_to_end_of_block_or_statement (parser);
3285                 expr = error_mark_node;
3286               }
3287             else
3288               {
3289                 /* Start the statement-expression.  */
3290                 expr = begin_stmt_expr ();
3291                 /* Parse the compound-statement.  */
3292                 cp_parser_compound_statement (parser, expr, false);
3293                 /* Finish up.  */
3294                 expr = finish_stmt_expr (expr, false);
3295               }
3296           }
3297         else
3298           {
3299             /* Parse the parenthesized expression.  */
3300             expr = cp_parser_expression (parser, cast_p, idk);
3301             /* Let the front end know that this expression was
3302                enclosed in parentheses. This matters in case, for
3303                example, the expression is of the form `A::B', since
3304                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3305                not.  */
3306             finish_parenthesized_expr (expr);
3307           }
3308         /* The `>' token might be the end of a template-id or
3309            template-parameter-list now.  */
3310         parser->greater_than_is_operator_p
3311           = saved_greater_than_is_operator_p;
3312         /* Consume the `)'.  */
3313         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3314           cp_parser_skip_to_end_of_statement (parser);
3315
3316         return expr;
3317       }
3318
3319     case CPP_OPEN_SQUARE:
3320       if (c_dialect_objc ())
3321         /* We have an Objective-C++ message. */
3322         return cp_parser_objc_expression (parser);
3323       maybe_warn_cpp0x ("lambda expressions");
3324       return cp_parser_lambda_expression (parser);
3325
3326     case CPP_OBJC_STRING:
3327       if (c_dialect_objc ())
3328         /* We have an Objective-C++ string literal. */
3329         return cp_parser_objc_expression (parser);
3330       cp_parser_error (parser, "expected primary-expression");
3331       return error_mark_node;
3332
3333     case CPP_KEYWORD:
3334       switch (token->keyword)
3335         {
3336           /* These two are the boolean literals.  */
3337         case RID_TRUE:
3338           cp_lexer_consume_token (parser->lexer);
3339           return boolean_true_node;
3340         case RID_FALSE:
3341           cp_lexer_consume_token (parser->lexer);
3342           return boolean_false_node;
3343
3344           /* The `__null' literal.  */
3345         case RID_NULL:
3346           cp_lexer_consume_token (parser->lexer);
3347           return null_node;
3348
3349           /* Recognize the `this' keyword.  */
3350         case RID_THIS:
3351           cp_lexer_consume_token (parser->lexer);
3352           if (parser->local_variables_forbidden_p)
3353             {
3354               error_at (token->location,
3355                         "%<this%> may not be used in this context");
3356               return error_mark_node;
3357             }
3358           /* Pointers cannot appear in constant-expressions.  */
3359           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3360             return error_mark_node;
3361           return finish_this_expr ();
3362
3363           /* The `operator' keyword can be the beginning of an
3364              id-expression.  */
3365         case RID_OPERATOR:
3366           goto id_expression;
3367
3368         case RID_FUNCTION_NAME:
3369         case RID_PRETTY_FUNCTION_NAME:
3370         case RID_C99_FUNCTION_NAME:
3371           {
3372             const char *name;
3373
3374             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3375                __func__ are the names of variables -- but they are
3376                treated specially.  Therefore, they are handled here,
3377                rather than relying on the generic id-expression logic
3378                below.  Grammatically, these names are id-expressions.
3379
3380                Consume the token.  */
3381             token = cp_lexer_consume_token (parser->lexer);
3382
3383             switch (token->keyword)
3384               {
3385               case RID_FUNCTION_NAME:
3386                 name = "%<__FUNCTION__%>";
3387                 break;
3388               case RID_PRETTY_FUNCTION_NAME:
3389                 name = "%<__PRETTY_FUNCTION__%>";
3390                 break;
3391               case RID_C99_FUNCTION_NAME:
3392                 name = "%<__func__%>";
3393                 break;
3394               default:
3395                 gcc_unreachable ();
3396               }
3397
3398             if (cp_parser_non_integral_constant_expression (parser, name))
3399               return error_mark_node;
3400
3401             /* Look up the name.  */
3402             return finish_fname (token->u.value);
3403           }
3404
3405         case RID_VA_ARG:
3406           {
3407             tree expression;
3408             tree type;
3409
3410             /* The `__builtin_va_arg' construct is used to handle
3411                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3412             cp_lexer_consume_token (parser->lexer);
3413             /* Look for the opening `('.  */
3414             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3415             /* Now, parse the assignment-expression.  */
3416             expression = cp_parser_assignment_expression (parser,
3417                                                           /*cast_p=*/false, NULL);
3418             /* Look for the `,'.  */
3419             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3420             /* Parse the type-id.  */
3421             type = cp_parser_type_id (parser);
3422             /* Look for the closing `)'.  */
3423             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3424             /* Using `va_arg' in a constant-expression is not
3425                allowed.  */
3426             if (cp_parser_non_integral_constant_expression (parser,
3427                                                             "%<va_arg%>"))
3428               return error_mark_node;
3429             return build_x_va_arg (expression, type);
3430           }
3431
3432         case RID_OFFSETOF:
3433           return cp_parser_builtin_offsetof (parser);
3434
3435         case RID_HAS_NOTHROW_ASSIGN:
3436         case RID_HAS_NOTHROW_CONSTRUCTOR:
3437         case RID_HAS_NOTHROW_COPY:        
3438         case RID_HAS_TRIVIAL_ASSIGN:
3439         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3440         case RID_HAS_TRIVIAL_COPY:        
3441         case RID_HAS_TRIVIAL_DESTRUCTOR:
3442         case RID_HAS_VIRTUAL_DESTRUCTOR:
3443         case RID_IS_ABSTRACT:
3444         case RID_IS_BASE_OF:
3445         case RID_IS_CLASS:
3446         case RID_IS_CONVERTIBLE_TO:
3447         case RID_IS_EMPTY:
3448         case RID_IS_ENUM:
3449         case RID_IS_POD:
3450         case RID_IS_POLYMORPHIC:
3451         case RID_IS_STD_LAYOUT:
3452         case RID_IS_TRIVIAL:
3453         case RID_IS_UNION:
3454           return cp_parser_trait_expr (parser, token->keyword);
3455
3456         /* Objective-C++ expressions.  */
3457         case RID_AT_ENCODE:
3458         case RID_AT_PROTOCOL:
3459         case RID_AT_SELECTOR:
3460           return cp_parser_objc_expression (parser);
3461
3462         default:
3463           cp_parser_error (parser, "expected primary-expression");
3464           return error_mark_node;
3465         }
3466
3467       /* An id-expression can start with either an identifier, a
3468          `::' as the beginning of a qualified-id, or the "operator"
3469          keyword.  */
3470     case CPP_NAME:
3471     case CPP_SCOPE:
3472     case CPP_TEMPLATE_ID:
3473     case CPP_NESTED_NAME_SPECIFIER:
3474       {
3475         tree id_expression;
3476         tree decl;
3477         const char *error_msg;
3478         bool template_p;
3479         bool done;
3480         cp_token *id_expr_token;
3481
3482       id_expression:
3483         /* Parse the id-expression.  */
3484         id_expression
3485           = cp_parser_id_expression (parser,
3486                                      /*template_keyword_p=*/false,
3487                                      /*check_dependency_p=*/true,
3488                                      &template_p,
3489                                      /*declarator_p=*/false,
3490                                      /*optional_p=*/false);
3491         if (id_expression == error_mark_node)
3492           return error_mark_node;
3493         id_expr_token = token;
3494         token = cp_lexer_peek_token (parser->lexer);
3495         done = (token->type != CPP_OPEN_SQUARE
3496                 && token->type != CPP_OPEN_PAREN
3497                 && token->type != CPP_DOT
3498                 && token->type != CPP_DEREF
3499                 && token->type != CPP_PLUS_PLUS
3500                 && token->type != CPP_MINUS_MINUS);
3501         /* If we have a template-id, then no further lookup is
3502            required.  If the template-id was for a template-class, we
3503            will sometimes have a TYPE_DECL at this point.  */
3504         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3505                  || TREE_CODE (id_expression) == TYPE_DECL)
3506           decl = id_expression;
3507         /* Look up the name.  */
3508         else
3509           {
3510             tree ambiguous_decls;
3511
3512             decl = cp_parser_lookup_name (parser, id_expression,
3513                                           none_type,
3514                                           template_p,
3515                                           /*is_namespace=*/false,
3516                                           /*check_dependency=*/true,
3517                                           &ambiguous_decls,
3518                                           id_expr_token->location);
3519             /* If the lookup was ambiguous, an error will already have
3520                been issued.  */
3521             if (ambiguous_decls)
3522               return error_mark_node;
3523
3524             /* In Objective-C++, an instance variable (ivar) may be preferred
3525                to whatever cp_parser_lookup_name() found.  */
3526             decl = objc_lookup_ivar (decl, id_expression);
3527
3528             /* If name lookup gives us a SCOPE_REF, then the
3529                qualifying scope was dependent.  */
3530             if (TREE_CODE (decl) == SCOPE_REF)
3531               {
3532                 /* At this point, we do not know if DECL is a valid
3533                    integral constant expression.  We assume that it is
3534                    in fact such an expression, so that code like:
3535
3536                       template <int N> struct A {
3537                         int a[B<N>::i];
3538                       };
3539                      
3540                    is accepted.  At template-instantiation time, we
3541                    will check that B<N>::i is actually a constant.  */
3542                 return decl;
3543               }
3544             /* Check to see if DECL is a local variable in a context
3545                where that is forbidden.  */
3546             if (parser->local_variables_forbidden_p
3547                 && local_variable_p (decl))
3548               {
3549                 /* It might be that we only found DECL because we are
3550                    trying to be generous with pre-ISO scoping rules.
3551                    For example, consider:
3552
3553                      int i;
3554                      void g() {
3555                        for (int i = 0; i < 10; ++i) {}
3556                        extern void f(int j = i);
3557                      }
3558
3559                    Here, name look up will originally find the out
3560                    of scope `i'.  We need to issue a warning message,
3561                    but then use the global `i'.  */
3562                 decl = check_for_out_of_scope_variable (decl);
3563                 if (local_variable_p (decl))
3564                   {
3565                     error_at (id_expr_token->location,
3566                               "local variable %qD may not appear in this context",
3567                               decl);
3568                     return error_mark_node;
3569                   }
3570               }
3571           }
3572
3573         decl = (finish_id_expression
3574                 (id_expression, decl, parser->scope,
3575                  idk,
3576                  parser->integral_constant_expression_p,
3577                  parser->allow_non_integral_constant_expression_p,
3578                  &parser->non_integral_constant_expression_p,
3579                  template_p, done, address_p,
3580                  template_arg_p,
3581                  &error_msg,
3582                  id_expr_token->location));
3583         if (error_msg)
3584           cp_parser_error (parser, error_msg);
3585         return decl;
3586       }
3587
3588       /* Anything else is an error.  */
3589     default:
3590       cp_parser_error (parser, "expected primary-expression");
3591       return error_mark_node;
3592     }
3593 }
3594
3595 /* Parse an id-expression.
3596
3597    id-expression:
3598      unqualified-id
3599      qualified-id
3600
3601    qualified-id:
3602      :: [opt] nested-name-specifier template [opt] unqualified-id
3603      :: identifier
3604      :: operator-function-id
3605      :: template-id
3606
3607    Return a representation of the unqualified portion of the
3608    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3609    a `::' or nested-name-specifier.
3610
3611    Often, if the id-expression was a qualified-id, the caller will
3612    want to make a SCOPE_REF to represent the qualified-id.  This
3613    function does not do this in order to avoid wastefully creating
3614    SCOPE_REFs when they are not required.
3615
3616    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3617    `template' keyword.
3618
3619    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3620    uninstantiated templates.
3621
3622    If *TEMPLATE_P is non-NULL, it is set to true iff the
3623    `template' keyword is used to explicitly indicate that the entity
3624    named is a template.
3625
3626    If DECLARATOR_P is true, the id-expression is appearing as part of
3627    a declarator, rather than as part of an expression.  */
3628
3629 static tree
3630 cp_parser_id_expression (cp_parser *parser,
3631                          bool template_keyword_p,
3632                          bool check_dependency_p,
3633                          bool *template_p,
3634                          bool declarator_p,
3635                          bool optional_p)
3636 {
3637   bool global_scope_p;
3638   bool nested_name_specifier_p;
3639
3640   /* Assume the `template' keyword was not used.  */
3641   if (template_p)
3642     *template_p = template_keyword_p;
3643
3644   /* Look for the optional `::' operator.  */
3645   global_scope_p
3646     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3647        != NULL_TREE);
3648   /* Look for the optional nested-name-specifier.  */
3649   nested_name_specifier_p
3650     = (cp_parser_nested_name_specifier_opt (parser,
3651                                             /*typename_keyword_p=*/false,
3652                                             check_dependency_p,
3653                                             /*type_p=*/false,
3654                                             declarator_p)
3655        != NULL_TREE);
3656   /* If there is a nested-name-specifier, then we are looking at
3657      the first qualified-id production.  */
3658   if (nested_name_specifier_p)
3659     {
3660       tree saved_scope;
3661       tree saved_object_scope;
3662       tree saved_qualifying_scope;
3663       tree unqualified_id;
3664       bool is_template;
3665
3666       /* See if the next token is the `template' keyword.  */
3667       if (!template_p)
3668         template_p = &is_template;
3669       *template_p = cp_parser_optional_template_keyword (parser);
3670       /* Name lookup we do during the processing of the
3671          unqualified-id might obliterate SCOPE.  */
3672       saved_scope = parser->scope;
3673       saved_object_scope = parser->object_scope;
3674       saved_qualifying_scope = parser->qualifying_scope;
3675       /* Process the final unqualified-id.  */
3676       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3677                                                  check_dependency_p,
3678                                                  declarator_p,
3679                                                  /*optional_p=*/false);
3680       /* Restore the SAVED_SCOPE for our caller.  */
3681       parser->scope = saved_scope;
3682       parser->object_scope = saved_object_scope;
3683       parser->qualifying_scope = saved_qualifying_scope;
3684
3685       return unqualified_id;
3686     }
3687   /* Otherwise, if we are in global scope, then we are looking at one
3688      of the other qualified-id productions.  */
3689   else if (global_scope_p)
3690     {
3691       cp_token *token;
3692       tree id;
3693
3694       /* Peek at the next token.  */
3695       token = cp_lexer_peek_token (parser->lexer);
3696
3697       /* If it's an identifier, and the next token is not a "<", then
3698          we can avoid the template-id case.  This is an optimization
3699          for this common case.  */
3700       if (token->type == CPP_NAME
3701           && !cp_parser_nth_token_starts_template_argument_list_p
3702                (parser, 2))
3703         return cp_parser_identifier (parser);
3704
3705       cp_parser_parse_tentatively (parser);
3706       /* Try a template-id.  */
3707       id = cp_parser_template_id (parser,
3708                                   /*template_keyword_p=*/false,
3709                                   /*check_dependency_p=*/true,
3710                                   declarator_p);
3711       /* If that worked, we're done.  */
3712       if (cp_parser_parse_definitely (parser))
3713         return id;
3714
3715       /* Peek at the next token.  (Changes in the token buffer may
3716          have invalidated the pointer obtained above.)  */
3717       token = cp_lexer_peek_token (parser->lexer);
3718
3719       switch (token->type)
3720         {
3721         case CPP_NAME:
3722           return cp_parser_identifier (parser);
3723
3724         case CPP_KEYWORD:
3725           if (token->keyword == RID_OPERATOR)
3726             return cp_parser_operator_function_id (parser);
3727           /* Fall through.  */
3728
3729         default:
3730           cp_parser_error (parser, "expected id-expression");
3731           return error_mark_node;
3732         }
3733     }
3734   else
3735     return cp_parser_unqualified_id (parser, template_keyword_p,
3736                                      /*check_dependency_p=*/true,
3737                                      declarator_p,
3738                                      optional_p);
3739 }
3740
3741 /* Parse an unqualified-id.
3742
3743    unqualified-id:
3744      identifier
3745      operator-function-id
3746      conversion-function-id
3747      ~ class-name
3748      template-id
3749
3750    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3751    keyword, in a construct like `A::template ...'.
3752
3753    Returns a representation of unqualified-id.  For the `identifier'
3754    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3755    production a BIT_NOT_EXPR is returned; the operand of the
3756    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3757    other productions, see the documentation accompanying the
3758    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3759    names are looked up in uninstantiated templates.  If DECLARATOR_P
3760    is true, the unqualified-id is appearing as part of a declarator,
3761    rather than as part of an expression.  */
3762
3763 static tree
3764 cp_parser_unqualified_id (cp_parser* parser,
3765                           bool template_keyword_p,
3766                           bool check_dependency_p,
3767                           bool declarator_p,
3768                           bool optional_p)
3769 {
3770   cp_token *token;
3771
3772   /* Peek at the next token.  */
3773   token = cp_lexer_peek_token (parser->lexer);
3774
3775   switch (token->type)
3776     {
3777     case CPP_NAME:
3778       {
3779         tree id;
3780
3781         /* We don't know yet whether or not this will be a
3782            template-id.  */
3783         cp_parser_parse_tentatively (parser);
3784         /* Try a template-id.  */
3785         id = cp_parser_template_id (parser, template_keyword_p,
3786                                     check_dependency_p,
3787                                     declarator_p);
3788         /* If it worked, we're done.  */
3789         if (cp_parser_parse_definitely (parser))
3790           return id;
3791         /* Otherwise, it's an ordinary identifier.  */
3792         return cp_parser_identifier (parser);
3793       }
3794
3795     case CPP_TEMPLATE_ID:
3796       return cp_parser_template_id (parser, template_keyword_p,
3797                                     check_dependency_p,
3798                                     declarator_p);
3799
3800     case CPP_COMPL:
3801       {
3802         tree type_decl;
3803         tree qualifying_scope;
3804         tree object_scope;
3805         tree scope;
3806         bool done;
3807
3808         /* Consume the `~' token.  */
3809         cp_lexer_consume_token (parser->lexer);
3810         /* Parse the class-name.  The standard, as written, seems to
3811            say that:
3812
3813              template <typename T> struct S { ~S (); };
3814              template <typename T> S<T>::~S() {}
3815
3816            is invalid, since `~' must be followed by a class-name, but
3817            `S<T>' is dependent, and so not known to be a class.
3818            That's not right; we need to look in uninstantiated
3819            templates.  A further complication arises from:
3820
3821              template <typename T> void f(T t) {
3822                t.T::~T();
3823              }
3824
3825            Here, it is not possible to look up `T' in the scope of `T'
3826            itself.  We must look in both the current scope, and the
3827            scope of the containing complete expression.
3828
3829            Yet another issue is:
3830
3831              struct S {
3832                int S;
3833                ~S();
3834              };
3835
3836              S::~S() {}
3837
3838            The standard does not seem to say that the `S' in `~S'
3839            should refer to the type `S' and not the data member
3840            `S::S'.  */
3841
3842         /* DR 244 says that we look up the name after the "~" in the
3843            same scope as we looked up the qualifying name.  That idea
3844            isn't fully worked out; it's more complicated than that.  */
3845         scope = parser->scope;
3846         object_scope = parser->object_scope;
3847         qualifying_scope = parser->qualifying_scope;
3848
3849         /* Check for invalid scopes.  */
3850         if (scope == error_mark_node)
3851           {
3852             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3853               cp_lexer_consume_token (parser->lexer);
3854             return error_mark_node;
3855           }
3856         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3857           {
3858             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3859               error_at (token->location,
3860                         "scope %qT before %<~%> is not a class-name",
3861                         scope);
3862             cp_parser_simulate_error (parser);
3863             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3864               cp_lexer_consume_token (parser->lexer);
3865             return error_mark_node;
3866           }
3867         gcc_assert (!scope || TYPE_P (scope));
3868
3869         /* If the name is of the form "X::~X" it's OK.  */
3870         token = cp_lexer_peek_token (parser->lexer);
3871         if (scope
3872             && token->type == CPP_NAME
3873             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3874                 == CPP_OPEN_PAREN)
3875             && constructor_name_p (token->u.value, scope))
3876           {
3877             cp_lexer_consume_token (parser->lexer);
3878             return build_nt (BIT_NOT_EXPR, scope);
3879           }
3880
3881         /* If there was an explicit qualification (S::~T), first look
3882            in the scope given by the qualification (i.e., S).  */
3883         done = false;
3884         type_decl = NULL_TREE;
3885         if (scope)
3886           {
3887             cp_parser_parse_tentatively (parser);
3888             type_decl = cp_parser_class_name (parser,
3889                                               /*typename_keyword_p=*/false,
3890                                               /*template_keyword_p=*/false,
3891                                               none_type,
3892                                               /*check_dependency=*/false,
3893                                               /*class_head_p=*/false,
3894                                               declarator_p);
3895             if (cp_parser_parse_definitely (parser))
3896               done = true;
3897           }
3898         /* In "N::S::~S", look in "N" as well.  */
3899         if (!done && scope && qualifying_scope)
3900           {
3901             cp_parser_parse_tentatively (parser);
3902             parser->scope = qualifying_scope;
3903             parser->object_scope = NULL_TREE;
3904             parser->qualifying_scope = NULL_TREE;
3905             type_decl
3906               = cp_parser_class_name (parser,
3907                                       /*typename_keyword_p=*/false,
3908                                       /*template_keyword_p=*/false,
3909                                       none_type,
3910                                       /*check_dependency=*/false,
3911                                       /*class_head_p=*/false,
3912                                       declarator_p);
3913             if (cp_parser_parse_definitely (parser))
3914               done = true;
3915           }
3916         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3917         else if (!done && object_scope)
3918           {
3919             cp_parser_parse_tentatively (parser);
3920             parser->scope = object_scope;
3921             parser->object_scope = NULL_TREE;
3922             parser->qualifying_scope = NULL_TREE;
3923             type_decl
3924               = cp_parser_class_name (parser,
3925                                       /*typename_keyword_p=*/false,
3926                                       /*template_keyword_p=*/false,
3927                                       none_type,
3928                                       /*check_dependency=*/false,
3929                                       /*class_head_p=*/false,
3930                                       declarator_p);
3931             if (cp_parser_parse_definitely (parser))
3932               done = true;
3933           }
3934         /* Look in the surrounding context.  */
3935         if (!done)
3936           {
3937             parser->scope = NULL_TREE;
3938             parser->object_scope = NULL_TREE;
3939             parser->qualifying_scope = NULL_TREE;
3940             if (processing_template_decl)
3941               cp_parser_parse_tentatively (parser);
3942             type_decl
3943               = cp_parser_class_name (parser,
3944                                       /*typename_keyword_p=*/false,
3945                                       /*template_keyword_p=*/false,
3946                                       none_type,
3947                                       /*check_dependency=*/false,
3948                                       /*class_head_p=*/false,
3949                                       declarator_p);
3950             if (processing_template_decl
3951                 && ! cp_parser_parse_definitely (parser))
3952               {
3953                 /* We couldn't find a type with this name, so just accept
3954                    it and check for a match at instantiation time.  */
3955                 type_decl = cp_parser_identifier (parser);
3956                 if (type_decl != error_mark_node)
3957                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3958                 return type_decl;
3959               }
3960           }
3961         /* If an error occurred, assume that the name of the
3962            destructor is the same as the name of the qualifying
3963            class.  That allows us to keep parsing after running
3964            into ill-formed destructor names.  */
3965         if (type_decl == error_mark_node && scope)
3966           return build_nt (BIT_NOT_EXPR, scope);
3967         else if (type_decl == error_mark_node)
3968           return error_mark_node;
3969
3970         /* Check that destructor name and scope match.  */
3971         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3972           {
3973             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3974               error_at (token->location,
3975                         "declaration of %<~%T%> as member of %qT",
3976                         type_decl, scope);
3977             cp_parser_simulate_error (parser);
3978             return error_mark_node;
3979           }
3980
3981         /* [class.dtor]
3982
3983            A typedef-name that names a class shall not be used as the
3984            identifier in the declarator for a destructor declaration.  */
3985         if (declarator_p
3986             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3987             && !DECL_SELF_REFERENCE_P (type_decl)
3988             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3989           error_at (token->location,
3990                     "typedef-name %qD used as destructor declarator",
3991                     type_decl);
3992
3993         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3994       }
3995
3996     case CPP_KEYWORD:
3997       if (token->keyword == RID_OPERATOR)
3998         {
3999           tree id;
4000
4001           /* This could be a template-id, so we try that first.  */
4002           cp_parser_parse_tentatively (parser);
4003           /* Try a template-id.  */
4004           id = cp_parser_template_id (parser, template_keyword_p,
4005                                       /*check_dependency_p=*/true,
4006                                       declarator_p);
4007           /* If that worked, we're done.  */
4008           if (cp_parser_parse_definitely (parser))
4009             return id;
4010           /* We still don't know whether we're looking at an
4011              operator-function-id or a conversion-function-id.  */
4012           cp_parser_parse_tentatively (parser);
4013           /* Try an operator-function-id.  */
4014           id = cp_parser_operator_function_id (parser);
4015           /* If that didn't work, try a conversion-function-id.  */
4016           if (!cp_parser_parse_definitely (parser))
4017             id = cp_parser_conversion_function_id (parser);
4018
4019           return id;
4020         }
4021       /* Fall through.  */
4022
4023     default:
4024       if (optional_p)
4025         return NULL_TREE;
4026       cp_parser_error (parser, "expected unqualified-id");
4027       return error_mark_node;
4028     }
4029 }
4030
4031 /* Parse an (optional) nested-name-specifier.
4032
4033    nested-name-specifier: [C++98]
4034      class-or-namespace-name :: nested-name-specifier [opt]
4035      class-or-namespace-name :: template nested-name-specifier [opt]
4036
4037    nested-name-specifier: [C++0x]
4038      type-name ::
4039      namespace-name ::
4040      nested-name-specifier identifier ::
4041      nested-name-specifier template [opt] simple-template-id ::
4042
4043    PARSER->SCOPE should be set appropriately before this function is
4044    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4045    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4046    in name lookups.
4047
4048    Sets PARSER->SCOPE to the class (TYPE) or namespace
4049    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4050    it unchanged if there is no nested-name-specifier.  Returns the new
4051    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4052
4053    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4054    part of a declaration and/or decl-specifier.  */
4055
4056 static tree
4057 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4058                                      bool typename_keyword_p,
4059                                      bool check_dependency_p,
4060                                      bool type_p,
4061                                      bool is_declaration)
4062 {
4063   bool success = false;
4064   cp_token_position start = 0;
4065   cp_token *token;
4066
4067   /* Remember where the nested-name-specifier starts.  */
4068   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4069     {
4070       start = cp_lexer_token_position (parser->lexer, false);
4071       push_deferring_access_checks (dk_deferred);
4072     }
4073
4074   while (true)
4075     {
4076       tree new_scope;
4077       tree old_scope;
4078       tree saved_qualifying_scope;
4079       bool template_keyword_p;
4080
4081       /* Spot cases that cannot be the beginning of a
4082          nested-name-specifier.  */
4083       token = cp_lexer_peek_token (parser->lexer);
4084
4085       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4086          the already parsed nested-name-specifier.  */
4087       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4088         {
4089           /* Grab the nested-name-specifier and continue the loop.  */
4090           cp_parser_pre_parsed_nested_name_specifier (parser);
4091           /* If we originally encountered this nested-name-specifier
4092              with IS_DECLARATION set to false, we will not have
4093              resolved TYPENAME_TYPEs, so we must do so here.  */
4094           if (is_declaration
4095               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4096             {
4097               new_scope = resolve_typename_type (parser->scope,
4098                                                  /*only_current_p=*/false);
4099               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4100                 parser->scope = new_scope;
4101             }
4102           success = true;
4103           continue;
4104         }
4105
4106       /* Spot cases that cannot be the beginning of a
4107          nested-name-specifier.  On the second and subsequent times
4108          through the loop, we look for the `template' keyword.  */
4109       if (success && token->keyword == RID_TEMPLATE)
4110         ;
4111       /* A template-id can start a nested-name-specifier.  */
4112       else if (token->type == CPP_TEMPLATE_ID)
4113         ;
4114       else
4115         {
4116           /* If the next token is not an identifier, then it is
4117              definitely not a type-name or namespace-name.  */
4118           if (token->type != CPP_NAME)
4119             break;
4120           /* If the following token is neither a `<' (to begin a
4121              template-id), nor a `::', then we are not looking at a
4122              nested-name-specifier.  */
4123           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4124           if (token->type != CPP_SCOPE
4125               && !cp_parser_nth_token_starts_template_argument_list_p
4126                   (parser, 2))
4127             break;
4128         }
4129
4130       /* The nested-name-specifier is optional, so we parse
4131          tentatively.  */
4132       cp_parser_parse_tentatively (parser);
4133
4134       /* Look for the optional `template' keyword, if this isn't the
4135          first time through the loop.  */
4136       if (success)
4137         template_keyword_p = cp_parser_optional_template_keyword (parser);
4138       else
4139         template_keyword_p = false;
4140
4141       /* Save the old scope since the name lookup we are about to do
4142          might destroy it.  */
4143       old_scope = parser->scope;
4144       saved_qualifying_scope = parser->qualifying_scope;
4145       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4146          look up names in "X<T>::I" in order to determine that "Y" is
4147          a template.  So, if we have a typename at this point, we make
4148          an effort to look through it.  */
4149       if (is_declaration
4150           && !typename_keyword_p
4151           && parser->scope
4152           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4153         parser->scope = resolve_typename_type (parser->scope,
4154                                                /*only_current_p=*/false);
4155       /* Parse the qualifying entity.  */
4156       new_scope
4157         = cp_parser_qualifying_entity (parser,
4158                                        typename_keyword_p,
4159                                        template_keyword_p,
4160                                        check_dependency_p,
4161                                        type_p,
4162                                        is_declaration);
4163       /* Look for the `::' token.  */
4164       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4165
4166       /* If we found what we wanted, we keep going; otherwise, we're
4167          done.  */
4168       if (!cp_parser_parse_definitely (parser))
4169         {
4170           bool error_p = false;
4171
4172           /* Restore the OLD_SCOPE since it was valid before the
4173              failed attempt at finding the last
4174              class-or-namespace-name.  */
4175           parser->scope = old_scope;
4176           parser->qualifying_scope = saved_qualifying_scope;
4177           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4178             break;
4179           /* If the next token is an identifier, and the one after
4180              that is a `::', then any valid interpretation would have
4181              found a class-or-namespace-name.  */
4182           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4183                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4184                      == CPP_SCOPE)
4185                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4186                      != CPP_COMPL))
4187             {
4188               token = cp_lexer_consume_token (parser->lexer);
4189               if (!error_p)
4190                 {
4191                   if (!token->ambiguous_p)
4192                     {
4193                       tree decl;
4194                       tree ambiguous_decls;
4195
4196                       decl = cp_parser_lookup_name (parser, token->u.value,
4197                                                     none_type,
4198                                                     /*is_template=*/false,
4199                                                     /*is_namespace=*/false,
4200                                                     /*check_dependency=*/true,
4201                                                     &ambiguous_decls,
4202                                                     token->location);
4203                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4204                         error_at (token->location,
4205                                   "%qD used without template parameters",
4206                                   decl);
4207                       else if (ambiguous_decls)
4208                         {
4209                           error_at (token->location,
4210                                     "reference to %qD is ambiguous",
4211                                     token->u.value);
4212                           print_candidates (ambiguous_decls);
4213                           decl = error_mark_node;
4214                         }
4215                       else
4216                         {
4217                           const char* msg = "is not a class or namespace";
4218                           if (cxx_dialect != cxx98)
4219                             msg = "is not a class, namespace, or enumeration";
4220                           cp_parser_name_lookup_error
4221                             (parser, token->u.value, decl, msg,
4222                              token->location);
4223                         }
4224                     }
4225                   parser->scope = error_mark_node;
4226                   error_p = true;
4227                   /* Treat this as a successful nested-name-specifier
4228                      due to:
4229
4230                      [basic.lookup.qual]
4231
4232                      If the name found is not a class-name (clause
4233                      _class_) or namespace-name (_namespace.def_), the
4234                      program is ill-formed.  */
4235                   success = true;
4236                 }
4237               cp_lexer_consume_token (parser->lexer);
4238             }
4239           break;
4240         }
4241       /* We've found one valid nested-name-specifier.  */
4242       success = true;
4243       /* Name lookup always gives us a DECL.  */
4244       if (TREE_CODE (new_scope) == TYPE_DECL)
4245         new_scope = TREE_TYPE (new_scope);
4246       /* Uses of "template" must be followed by actual templates.  */
4247       if (template_keyword_p
4248           && !(CLASS_TYPE_P (new_scope)
4249                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4250                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4251                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4252           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4253                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4254                    == TEMPLATE_ID_EXPR)))
4255         permerror (input_location, TYPE_P (new_scope)
4256                    ? "%qT is not a template"
4257                    : "%qD is not a template",
4258                    new_scope);
4259       /* If it is a class scope, try to complete it; we are about to
4260          be looking up names inside the class.  */
4261       if (TYPE_P (new_scope)
4262           /* Since checking types for dependency can be expensive,
4263              avoid doing it if the type is already complete.  */
4264           && !COMPLETE_TYPE_P (new_scope)
4265           /* Do not try to complete dependent types.  */
4266           && !dependent_type_p (new_scope))
4267         {
4268           new_scope = complete_type (new_scope);
4269           /* If it is a typedef to current class, use the current
4270              class instead, as the typedef won't have any names inside
4271              it yet.  */
4272           if (!COMPLETE_TYPE_P (new_scope)
4273               && currently_open_class (new_scope))
4274             new_scope = TYPE_MAIN_VARIANT (new_scope);
4275         }
4276       /* Make sure we look in the right scope the next time through
4277          the loop.  */
4278       parser->scope = new_scope;
4279     }
4280
4281   /* If parsing tentatively, replace the sequence of tokens that makes
4282      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4283      token.  That way, should we re-parse the token stream, we will
4284      not have to repeat the effort required to do the parse, nor will
4285      we issue duplicate error messages.  */
4286   if (success && start)
4287     {
4288       cp_token *token;
4289
4290       token = cp_lexer_token_at (parser->lexer, start);
4291       /* Reset the contents of the START token.  */
4292       token->type = CPP_NESTED_NAME_SPECIFIER;
4293       /* Retrieve any deferred checks.  Do not pop this access checks yet
4294          so the memory will not be reclaimed during token replacing below.  */
4295       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4296       token->u.tree_check_value->value = parser->scope;
4297       token->u.tree_check_value->checks = get_deferred_access_checks ();
4298       token->u.tree_check_value->qualifying_scope =
4299         parser->qualifying_scope;
4300       token->keyword = RID_MAX;
4301
4302       /* Purge all subsequent tokens.  */
4303       cp_lexer_purge_tokens_after (parser->lexer, start);
4304     }
4305
4306   if (start)
4307     pop_to_parent_deferring_access_checks ();
4308
4309   return success ? parser->scope : NULL_TREE;
4310 }
4311
4312 /* Parse a nested-name-specifier.  See
4313    cp_parser_nested_name_specifier_opt for details.  This function
4314    behaves identically, except that it will an issue an error if no
4315    nested-name-specifier is present.  */
4316
4317 static tree
4318 cp_parser_nested_name_specifier (cp_parser *parser,
4319                                  bool typename_keyword_p,
4320                                  bool check_dependency_p,
4321                                  bool type_p,
4322                                  bool is_declaration)
4323 {
4324   tree scope;
4325
4326   /* Look for the nested-name-specifier.  */
4327   scope = cp_parser_nested_name_specifier_opt (parser,
4328                                                typename_keyword_p,
4329                                                check_dependency_p,
4330                                                type_p,
4331                                                is_declaration);
4332   /* If it was not present, issue an error message.  */
4333   if (!scope)
4334     {
4335       cp_parser_error (parser, "expected nested-name-specifier");
4336       parser->scope = NULL_TREE;
4337     }
4338
4339   return scope;
4340 }
4341
4342 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4343    this is either a class-name or a namespace-name (which corresponds
4344    to the class-or-namespace-name production in the grammar). For
4345    C++0x, it can also be a type-name that refers to an enumeration
4346    type.
4347
4348    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4349    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4350    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4351    TYPE_P is TRUE iff the next name should be taken as a class-name,
4352    even the same name is declared to be another entity in the same
4353    scope.
4354
4355    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4356    specified by the class-or-namespace-name.  If neither is found the
4357    ERROR_MARK_NODE is returned.  */
4358
4359 static tree
4360 cp_parser_qualifying_entity (cp_parser *parser,
4361                              bool typename_keyword_p,
4362                              bool template_keyword_p,
4363                              bool check_dependency_p,
4364                              bool type_p,
4365                              bool is_declaration)
4366 {
4367   tree saved_scope;
4368   tree saved_qualifying_scope;
4369   tree saved_object_scope;
4370   tree scope;
4371   bool only_class_p;
4372   bool successful_parse_p;
4373
4374   /* Before we try to parse the class-name, we must save away the
4375      current PARSER->SCOPE since cp_parser_class_name will destroy
4376      it.  */
4377   saved_scope = parser->scope;
4378   saved_qualifying_scope = parser->qualifying_scope;
4379   saved_object_scope = parser->object_scope;
4380   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4381      there is no need to look for a namespace-name.  */
4382   only_class_p = template_keyword_p 
4383     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4384   if (!only_class_p)
4385     cp_parser_parse_tentatively (parser);
4386   scope = cp_parser_class_name (parser,
4387                                 typename_keyword_p,
4388                                 template_keyword_p,
4389                                 type_p ? class_type : none_type,
4390                                 check_dependency_p,
4391                                 /*class_head_p=*/false,
4392                                 is_declaration);
4393   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4394   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4395   if (!only_class_p 
4396       && cxx_dialect != cxx98
4397       && !successful_parse_p)
4398     {
4399       /* Restore the saved scope.  */
4400       parser->scope = saved_scope;
4401       parser->qualifying_scope = saved_qualifying_scope;
4402       parser->object_scope = saved_object_scope;
4403
4404       /* Parse tentatively.  */
4405       cp_parser_parse_tentatively (parser);
4406      
4407       /* Parse a typedef-name or enum-name.  */
4408       scope = cp_parser_nonclass_name (parser);
4409       successful_parse_p = cp_parser_parse_definitely (parser);
4410     }
4411   /* If that didn't work, try for a namespace-name.  */
4412   if (!only_class_p && !successful_parse_p)
4413     {
4414       /* Restore the saved scope.  */
4415       parser->scope = saved_scope;
4416       parser->qualifying_scope = saved_qualifying_scope;
4417       parser->object_scope = saved_object_scope;
4418       /* If we are not looking at an identifier followed by the scope
4419          resolution operator, then this is not part of a
4420          nested-name-specifier.  (Note that this function is only used
4421          to parse the components of a nested-name-specifier.)  */
4422       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4423           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4424         return error_mark_node;
4425       scope = cp_parser_namespace_name (parser);
4426     }
4427
4428   return scope;
4429 }
4430
4431 /* Parse a postfix-expression.
4432
4433    postfix-expression:
4434      primary-expression
4435      postfix-expression [ expression ]
4436      postfix-expression ( expression-list [opt] )
4437      simple-type-specifier ( expression-list [opt] )
4438      typename :: [opt] nested-name-specifier identifier
4439        ( expression-list [opt] )
4440      typename :: [opt] nested-name-specifier template [opt] template-id
4441        ( expression-list [opt] )
4442      postfix-expression . template [opt] id-expression
4443      postfix-expression -> template [opt] id-expression
4444      postfix-expression . pseudo-destructor-name
4445      postfix-expression -> pseudo-destructor-name
4446      postfix-expression ++
4447      postfix-expression --
4448      dynamic_cast < type-id > ( expression )
4449      static_cast < type-id > ( expression )
4450      reinterpret_cast < type-id > ( expression )
4451      const_cast < type-id > ( expression )
4452      typeid ( expression )
4453      typeid ( type-id )
4454
4455    GNU Extension:
4456
4457    postfix-expression:
4458      ( type-id ) { initializer-list , [opt] }
4459
4460    This extension is a GNU version of the C99 compound-literal
4461    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4462    but they are essentially the same concept.)
4463
4464    If ADDRESS_P is true, the postfix expression is the operand of the
4465    `&' operator.  CAST_P is true if this expression is the target of a
4466    cast.
4467
4468    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4469    class member access expressions [expr.ref].
4470
4471    Returns a representation of the expression.  */
4472
4473 static tree
4474 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4475                               bool member_access_only_p,
4476                               cp_id_kind * pidk_return)
4477 {
4478   cp_token *token;
4479   enum rid keyword;
4480   cp_id_kind idk = CP_ID_KIND_NONE;
4481   tree postfix_expression = NULL_TREE;
4482   bool is_member_access = false;
4483
4484   /* Peek at the next token.  */
4485   token = cp_lexer_peek_token (parser->lexer);
4486   /* Some of the productions are determined by keywords.  */
4487   keyword = token->keyword;
4488   switch (keyword)
4489     {
4490     case RID_DYNCAST:
4491     case RID_STATCAST:
4492     case RID_REINTCAST:
4493     case RID_CONSTCAST:
4494       {
4495         tree type;
4496         tree expression;
4497         const char *saved_message;
4498
4499         /* All of these can be handled in the same way from the point
4500            of view of parsing.  Begin by consuming the token
4501            identifying the cast.  */
4502         cp_lexer_consume_token (parser->lexer);
4503
4504         /* New types cannot be defined in the cast.  */
4505         saved_message = parser->type_definition_forbidden_message;
4506         parser->type_definition_forbidden_message
4507           = "types may not be defined in casts";
4508
4509         /* Look for the opening `<'.  */
4510         cp_parser_require (parser, CPP_LESS, "%<<%>");
4511         /* Parse the type to which we are casting.  */
4512         type = cp_parser_type_id (parser);
4513         /* Look for the closing `>'.  */
4514         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4515         /* Restore the old message.  */
4516         parser->type_definition_forbidden_message = saved_message;
4517
4518         /* And the expression which is being cast.  */
4519         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4520         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4521         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4522
4523         /* Only type conversions to integral or enumeration types
4524            can be used in constant-expressions.  */
4525         if (!cast_valid_in_integral_constant_expression_p (type)
4526             && (cp_parser_non_integral_constant_expression
4527                 (parser,
4528                  "a cast to a type other than an integral or "
4529                  "enumeration type")))
4530           return error_mark_node;
4531
4532         switch (keyword)
4533           {
4534           case RID_DYNCAST:
4535             postfix_expression
4536               = build_dynamic_cast (type, expression, tf_warning_or_error);
4537             break;
4538           case RID_STATCAST:
4539             postfix_expression
4540               = build_static_cast (type, expression, tf_warning_or_error);
4541             break;
4542           case RID_REINTCAST:
4543             postfix_expression
4544               = build_reinterpret_cast (type, expression, 
4545                                         tf_warning_or_error);
4546             break;
4547           case RID_CONSTCAST:
4548             postfix_expression
4549               = build_const_cast (type, expression, tf_warning_or_error);
4550             break;
4551           default:
4552             gcc_unreachable ();
4553           }
4554       }
4555       break;
4556
4557     case RID_TYPEID:
4558       {
4559         tree type;
4560         const char *saved_message;
4561         bool saved_in_type_id_in_expr_p;
4562
4563         /* Consume the `typeid' token.  */
4564         cp_lexer_consume_token (parser->lexer);
4565         /* Look for the `(' token.  */
4566         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4567         /* Types cannot be defined in a `typeid' expression.  */
4568         saved_message = parser->type_definition_forbidden_message;
4569         parser->type_definition_forbidden_message
4570           = "types may not be defined in a %<typeid%> expression";
4571         /* We can't be sure yet whether we're looking at a type-id or an
4572            expression.  */
4573         cp_parser_parse_tentatively (parser);
4574         /* Try a type-id first.  */
4575         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4576         parser->in_type_id_in_expr_p = true;
4577         type = cp_parser_type_id (parser);
4578         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4579         /* Look for the `)' token.  Otherwise, we can't be sure that
4580            we're not looking at an expression: consider `typeid (int
4581            (3))', for example.  */
4582         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4583         /* If all went well, simply lookup the type-id.  */
4584         if (cp_parser_parse_definitely (parser))
4585           postfix_expression = get_typeid (type);
4586         /* Otherwise, fall back to the expression variant.  */
4587         else
4588           {
4589             tree expression;
4590
4591             /* Look for an expression.  */
4592             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4593             /* Compute its typeid.  */
4594             postfix_expression = build_typeid (expression);
4595             /* Look for the `)' token.  */
4596             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4597           }
4598         /* Restore the saved message.  */
4599         parser->type_definition_forbidden_message = saved_message;
4600         /* `typeid' may not appear in an integral constant expression.  */
4601         if (cp_parser_non_integral_constant_expression(parser,
4602                                                        "%<typeid%> operator"))
4603           return error_mark_node;
4604       }
4605       break;
4606
4607     case RID_TYPENAME:
4608       {
4609         tree type;
4610         /* The syntax permitted here is the same permitted for an
4611            elaborated-type-specifier.  */
4612         type = cp_parser_elaborated_type_specifier (parser,
4613                                                     /*is_friend=*/false,
4614                                                     /*is_declaration=*/false);
4615         postfix_expression = cp_parser_functional_cast (parser, type);
4616       }
4617       break;
4618
4619     default:
4620       {
4621         tree type;
4622
4623         /* If the next thing is a simple-type-specifier, we may be
4624            looking at a functional cast.  We could also be looking at
4625            an id-expression.  So, we try the functional cast, and if
4626            that doesn't work we fall back to the primary-expression.  */
4627         cp_parser_parse_tentatively (parser);
4628         /* Look for the simple-type-specifier.  */
4629         type = cp_parser_simple_type_specifier (parser,
4630                                                 /*decl_specs=*/NULL,
4631                                                 CP_PARSER_FLAGS_NONE);
4632         /* Parse the cast itself.  */
4633         if (!cp_parser_error_occurred (parser))
4634           postfix_expression
4635             = cp_parser_functional_cast (parser, type);
4636         /* If that worked, we're done.  */
4637         if (cp_parser_parse_definitely (parser))
4638           break;
4639
4640         /* If the functional-cast didn't work out, try a
4641            compound-literal.  */
4642         if (cp_parser_allow_gnu_extensions_p (parser)
4643             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4644           {
4645             VEC(constructor_elt,gc) *initializer_list = NULL;
4646             bool saved_in_type_id_in_expr_p;
4647
4648             cp_parser_parse_tentatively (parser);
4649             /* Consume the `('.  */
4650             cp_lexer_consume_token (parser->lexer);
4651             /* Parse the type.  */
4652             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4653             parser->in_type_id_in_expr_p = true;
4654             type = cp_parser_type_id (parser);
4655             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4656             /* Look for the `)'.  */
4657             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4658             /* Look for the `{'.  */
4659             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4660             /* If things aren't going well, there's no need to
4661                keep going.  */
4662             if (!cp_parser_error_occurred (parser))
4663               {
4664                 bool non_constant_p;
4665                 /* Parse the initializer-list.  */
4666                 initializer_list
4667                   = cp_parser_initializer_list (parser, &non_constant_p);
4668                 /* Allow a trailing `,'.  */
4669                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4670                   cp_lexer_consume_token (parser->lexer);
4671                 /* Look for the final `}'.  */
4672                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4673               }
4674             /* If that worked, we're definitely looking at a
4675                compound-literal expression.  */
4676             if (cp_parser_parse_definitely (parser))
4677               {
4678                 /* Warn the user that a compound literal is not
4679                    allowed in standard C++.  */
4680                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4681                 /* For simplicity, we disallow compound literals in
4682                    constant-expressions.  We could
4683                    allow compound literals of integer type, whose
4684                    initializer was a constant, in constant
4685                    expressions.  Permitting that usage, as a further
4686                    extension, would not change the meaning of any
4687                    currently accepted programs.  (Of course, as
4688                    compound literals are not part of ISO C++, the
4689                    standard has nothing to say.)  */
4690                 if (cp_parser_non_integral_constant_expression 
4691                     (parser, "non-constant compound literals"))
4692                   {
4693                     postfix_expression = error_mark_node;
4694                     break;
4695                   }
4696                 /* Form the representation of the compound-literal.  */
4697                 postfix_expression
4698                   = (finish_compound_literal
4699                      (type, build_constructor (init_list_type_node,
4700                                                initializer_list)));
4701                 break;
4702               }
4703           }
4704
4705         /* It must be a primary-expression.  */
4706         postfix_expression
4707           = cp_parser_primary_expression (parser, address_p, cast_p,
4708                                           /*template_arg_p=*/false,
4709                                           &idk);
4710       }
4711       break;
4712     }
4713
4714   /* Keep looping until the postfix-expression is complete.  */
4715   while (true)
4716     {
4717       if (idk == CP_ID_KIND_UNQUALIFIED
4718           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4719           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4720         /* It is not a Koenig lookup function call.  */
4721         postfix_expression
4722           = unqualified_name_lookup_error (postfix_expression);
4723
4724       /* Peek at the next token.  */
4725       token = cp_lexer_peek_token (parser->lexer);
4726
4727       switch (token->type)
4728         {
4729         case CPP_OPEN_SQUARE:
4730           postfix_expression
4731             = cp_parser_postfix_open_square_expression (parser,
4732                                                         postfix_expression,
4733                                                         false);
4734           idk = CP_ID_KIND_NONE;
4735           is_member_access = false;
4736           break;
4737
4738         case CPP_OPEN_PAREN:
4739           /* postfix-expression ( expression-list [opt] ) */
4740           {
4741             bool koenig_p;
4742             bool is_builtin_constant_p;
4743             bool saved_integral_constant_expression_p = false;
4744             bool saved_non_integral_constant_expression_p = false;
4745             VEC(tree,gc) *args;
4746
4747             is_member_access = false;
4748
4749             is_builtin_constant_p
4750               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4751             if (is_builtin_constant_p)
4752               {
4753                 /* The whole point of __builtin_constant_p is to allow
4754                    non-constant expressions to appear as arguments.  */
4755                 saved_integral_constant_expression_p
4756                   = parser->integral_constant_expression_p;
4757                 saved_non_integral_constant_expression_p
4758                   = parser->non_integral_constant_expression_p;
4759                 parser->integral_constant_expression_p = false;
4760               }
4761             args = (cp_parser_parenthesized_expression_list
4762                     (parser, /*is_attribute_list=*/false,
4763                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4764                      /*non_constant_p=*/NULL));
4765             if (is_builtin_constant_p)
4766               {
4767                 parser->integral_constant_expression_p
4768                   = saved_integral_constant_expression_p;
4769                 parser->non_integral_constant_expression_p
4770                   = saved_non_integral_constant_expression_p;
4771               }
4772
4773             if (args == NULL)
4774               {
4775                 postfix_expression = error_mark_node;
4776                 break;
4777               }
4778
4779             /* Function calls are not permitted in
4780                constant-expressions.  */
4781             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4782                 && cp_parser_non_integral_constant_expression (parser,
4783                                                                "a function call"))
4784               {
4785                 postfix_expression = error_mark_node;
4786                 release_tree_vector (args);
4787                 break;
4788               }
4789
4790             koenig_p = false;
4791             if (idk == CP_ID_KIND_UNQUALIFIED
4792                 || idk == CP_ID_KIND_TEMPLATE_ID)
4793               {
4794                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4795                   {
4796                     if (!VEC_empty (tree, args))
4797                       {
4798                         koenig_p = true;
4799                         if (!any_type_dependent_arguments_p (args))
4800                           postfix_expression
4801                             = perform_koenig_lookup (postfix_expression, args);
4802                       }
4803                     else
4804                       postfix_expression
4805                         = unqualified_fn_lookup_error (postfix_expression);
4806                   }
4807                 /* We do not perform argument-dependent lookup if
4808                    normal lookup finds a non-function, in accordance
4809                    with the expected resolution of DR 218.  */
4810                 else if (!VEC_empty (tree, args)
4811                          && is_overloaded_fn (postfix_expression))
4812                   {
4813                     tree fn = get_first_fn (postfix_expression);
4814
4815                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4816                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4817
4818                     /* Only do argument dependent lookup if regular
4819                        lookup does not find a set of member functions.
4820                        [basic.lookup.koenig]/2a  */
4821                     if (!DECL_FUNCTION_MEMBER_P (fn))
4822                       {
4823                         koenig_p = true;
4824                         if (!any_type_dependent_arguments_p (args))
4825                           postfix_expression
4826                             = perform_koenig_lookup (postfix_expression, args);
4827                       }
4828                   }
4829               }
4830
4831             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4832               {
4833                 tree instance = TREE_OPERAND (postfix_expression, 0);
4834                 tree fn = TREE_OPERAND (postfix_expression, 1);
4835
4836                 if (processing_template_decl
4837                     && (type_dependent_expression_p (instance)
4838                         || (!BASELINK_P (fn)
4839                             && TREE_CODE (fn) != FIELD_DECL)
4840                         || type_dependent_expression_p (fn)
4841                         || any_type_dependent_arguments_p (args)))
4842                   {
4843                     postfix_expression
4844                       = build_nt_call_vec (postfix_expression, args);
4845                     release_tree_vector (args);
4846                     break;
4847                   }
4848
4849                 if (BASELINK_P (fn))
4850                   {
4851                   postfix_expression
4852                     = (build_new_method_call
4853                        (instance, fn, &args, NULL_TREE,
4854                         (idk == CP_ID_KIND_QUALIFIED
4855                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4856                         /*fn_p=*/NULL,
4857                         tf_warning_or_error));
4858                   }
4859                 else
4860                   postfix_expression
4861                     = finish_call_expr (postfix_expression, &args,
4862                                         /*disallow_virtual=*/false,
4863                                         /*koenig_p=*/false,
4864                                         tf_warning_or_error);
4865               }
4866             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4867                      || TREE_CODE (postfix_expression) == MEMBER_REF
4868                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4869               postfix_expression = (build_offset_ref_call_from_tree
4870                                     (postfix_expression, &args));
4871             else if (idk == CP_ID_KIND_QUALIFIED)
4872               /* A call to a static class member, or a namespace-scope
4873                  function.  */
4874               postfix_expression
4875                 = finish_call_expr (postfix_expression, &args,
4876                                     /*disallow_virtual=*/true,
4877                                     koenig_p,
4878                                     tf_warning_or_error);
4879             else
4880               /* All other function calls.  */
4881               postfix_expression
4882                 = finish_call_expr (postfix_expression, &args,
4883                                     /*disallow_virtual=*/false,
4884                                     koenig_p,
4885                                     tf_warning_or_error);
4886
4887             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4888             idk = CP_ID_KIND_NONE;
4889
4890             release_tree_vector (args);
4891           }
4892           break;
4893
4894         case CPP_DOT:
4895         case CPP_DEREF:
4896           /* postfix-expression . template [opt] id-expression
4897              postfix-expression . pseudo-destructor-name
4898              postfix-expression -> template [opt] id-expression
4899              postfix-expression -> pseudo-destructor-name */
4900
4901           /* Consume the `.' or `->' operator.  */
4902           cp_lexer_consume_token (parser->lexer);
4903
4904           postfix_expression
4905             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4906                                                       postfix_expression,
4907                                                       false, &idk,
4908                                                       token->location);
4909
4910           is_member_access = true;
4911           break;
4912
4913         case CPP_PLUS_PLUS:
4914           /* postfix-expression ++  */
4915           /* Consume the `++' token.  */
4916           cp_lexer_consume_token (parser->lexer);
4917           /* Generate a representation for the complete expression.  */
4918           postfix_expression
4919             = finish_increment_expr (postfix_expression,
4920                                      POSTINCREMENT_EXPR);
4921           /* Increments may not appear in constant-expressions.  */
4922           if (cp_parser_non_integral_constant_expression (parser,
4923                                                           "an increment"))
4924             postfix_expression = error_mark_node;
4925           idk = CP_ID_KIND_NONE;
4926           is_member_access = false;
4927           break;
4928
4929         case CPP_MINUS_MINUS:
4930           /* postfix-expression -- */
4931           /* Consume the `--' token.  */
4932           cp_lexer_consume_token (parser->lexer);
4933           /* Generate a representation for the complete expression.  */
4934           postfix_expression
4935             = finish_increment_expr (postfix_expression,
4936                                      POSTDECREMENT_EXPR);
4937           /* Decrements may not appear in constant-expressions.  */
4938           if (cp_parser_non_integral_constant_expression (parser,
4939                                                           "a decrement"))
4940             postfix_expression = error_mark_node;
4941           idk = CP_ID_KIND_NONE;
4942           is_member_access = false;
4943           break;
4944
4945         default:
4946           if (pidk_return != NULL)
4947             * pidk_return = idk;
4948           if (member_access_only_p)
4949             return is_member_access? postfix_expression : error_mark_node;
4950           else
4951             return postfix_expression;
4952         }
4953     }
4954
4955   /* We should never get here.  */
4956   gcc_unreachable ();
4957   return error_mark_node;
4958 }
4959
4960 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4961    by cp_parser_builtin_offsetof.  We're looking for
4962
4963      postfix-expression [ expression ]
4964
4965    FOR_OFFSETOF is set if we're being called in that context, which
4966    changes how we deal with integer constant expressions.  */
4967
4968 static tree
4969 cp_parser_postfix_open_square_expression (cp_parser *parser,
4970                                           tree postfix_expression,
4971                                           bool for_offsetof)
4972 {
4973   tree index;
4974
4975   /* Consume the `[' token.  */
4976   cp_lexer_consume_token (parser->lexer);
4977
4978   /* Parse the index expression.  */
4979   /* ??? For offsetof, there is a question of what to allow here.  If
4980      offsetof is not being used in an integral constant expression context,
4981      then we *could* get the right answer by computing the value at runtime.
4982      If we are in an integral constant expression context, then we might
4983      could accept any constant expression; hard to say without analysis.
4984      Rather than open the barn door too wide right away, allow only integer
4985      constant expressions here.  */
4986   if (for_offsetof)
4987     index = cp_parser_constant_expression (parser, false, NULL);
4988   else
4989     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4990
4991   /* Look for the closing `]'.  */
4992   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4993
4994   /* Build the ARRAY_REF.  */
4995   postfix_expression = grok_array_decl (postfix_expression, index);
4996
4997   /* When not doing offsetof, array references are not permitted in
4998      constant-expressions.  */
4999   if (!for_offsetof
5000       && (cp_parser_non_integral_constant_expression
5001           (parser, "an array reference")))
5002     postfix_expression = error_mark_node;
5003
5004   return postfix_expression;
5005 }
5006
5007 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5008    by cp_parser_builtin_offsetof.  We're looking for
5009
5010      postfix-expression . template [opt] id-expression
5011      postfix-expression . pseudo-destructor-name
5012      postfix-expression -> template [opt] id-expression
5013      postfix-expression -> pseudo-destructor-name
5014
5015    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5016    limits what of the above we'll actually accept, but nevermind.
5017    TOKEN_TYPE is the "." or "->" token, which will already have been
5018    removed from the stream.  */
5019
5020 static tree
5021 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5022                                         enum cpp_ttype token_type,
5023                                         tree postfix_expression,
5024                                         bool for_offsetof, cp_id_kind *idk,
5025                                         location_t location)
5026 {
5027   tree name;
5028   bool dependent_p;
5029   bool pseudo_destructor_p;
5030   tree scope = NULL_TREE;
5031
5032   /* If this is a `->' operator, dereference the pointer.  */
5033   if (token_type == CPP_DEREF)
5034     postfix_expression = build_x_arrow (postfix_expression);
5035   /* Check to see whether or not the expression is type-dependent.  */
5036   dependent_p = type_dependent_expression_p (postfix_expression);
5037   /* The identifier following the `->' or `.' is not qualified.  */
5038   parser->scope = NULL_TREE;
5039   parser->qualifying_scope = NULL_TREE;
5040   parser->object_scope = NULL_TREE;
5041   *idk = CP_ID_KIND_NONE;
5042
5043   /* Enter the scope corresponding to the type of the object
5044      given by the POSTFIX_EXPRESSION.  */
5045   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5046     {
5047       scope = TREE_TYPE (postfix_expression);
5048       /* According to the standard, no expression should ever have
5049          reference type.  Unfortunately, we do not currently match
5050          the standard in this respect in that our internal representation
5051          of an expression may have reference type even when the standard
5052          says it does not.  Therefore, we have to manually obtain the
5053          underlying type here.  */
5054       scope = non_reference (scope);
5055       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5056       if (scope == unknown_type_node)
5057         {
5058           error_at (location, "%qE does not have class type",
5059                     postfix_expression);
5060           scope = NULL_TREE;
5061         }
5062       else
5063         scope = complete_type_or_else (scope, NULL_TREE);
5064       /* Let the name lookup machinery know that we are processing a
5065          class member access expression.  */
5066       parser->context->object_type = scope;
5067       /* If something went wrong, we want to be able to discern that case,
5068          as opposed to the case where there was no SCOPE due to the type
5069          of expression being dependent.  */
5070       if (!scope)
5071         scope = error_mark_node;
5072       /* If the SCOPE was erroneous, make the various semantic analysis
5073          functions exit quickly -- and without issuing additional error
5074          messages.  */
5075       if (scope == error_mark_node)
5076         postfix_expression = error_mark_node;
5077     }
5078
5079   /* Assume this expression is not a pseudo-destructor access.  */
5080   pseudo_destructor_p = false;
5081
5082   /* If the SCOPE is a scalar type, then, if this is a valid program,
5083      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5084      is type dependent, it can be pseudo-destructor-name or something else.
5085      Try to parse it as pseudo-destructor-name first.  */
5086   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5087     {
5088       tree s;
5089       tree type;
5090
5091       cp_parser_parse_tentatively (parser);
5092       /* Parse the pseudo-destructor-name.  */
5093       s = NULL_TREE;
5094       cp_parser_pseudo_destructor_name (parser, &s, &type);
5095       if (dependent_p
5096           && (cp_parser_error_occurred (parser)
5097               || TREE_CODE (type) != TYPE_DECL
5098               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5099         cp_parser_abort_tentative_parse (parser);
5100       else if (cp_parser_parse_definitely (parser))
5101         {
5102           pseudo_destructor_p = true;
5103           postfix_expression
5104             = finish_pseudo_destructor_expr (postfix_expression,
5105                                              s, TREE_TYPE (type));
5106         }
5107     }
5108
5109   if (!pseudo_destructor_p)
5110     {
5111       /* If the SCOPE is not a scalar type, we are looking at an
5112          ordinary class member access expression, rather than a
5113          pseudo-destructor-name.  */
5114       bool template_p;
5115       cp_token *token = cp_lexer_peek_token (parser->lexer);
5116       /* Parse the id-expression.  */
5117       name = (cp_parser_id_expression
5118               (parser,
5119                cp_parser_optional_template_keyword (parser),
5120                /*check_dependency_p=*/true,
5121                &template_p,
5122                /*declarator_p=*/false,
5123                /*optional_p=*/false));
5124       /* In general, build a SCOPE_REF if the member name is qualified.
5125          However, if the name was not dependent and has already been
5126          resolved; there is no need to build the SCOPE_REF.  For example;
5127
5128              struct X { void f(); };
5129              template <typename T> void f(T* t) { t->X::f(); }
5130
5131          Even though "t" is dependent, "X::f" is not and has been resolved
5132          to a BASELINK; there is no need to include scope information.  */
5133
5134       /* But we do need to remember that there was an explicit scope for
5135          virtual function calls.  */
5136       if (parser->scope)
5137         *idk = CP_ID_KIND_QUALIFIED;
5138
5139       /* If the name is a template-id that names a type, we will get a
5140          TYPE_DECL here.  That is invalid code.  */
5141       if (TREE_CODE (name) == TYPE_DECL)
5142         {
5143           error_at (token->location, "invalid use of %qD", name);
5144           postfix_expression = error_mark_node;
5145         }
5146       else
5147         {
5148           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5149             {
5150               name = build_qualified_name (/*type=*/NULL_TREE,
5151                                            parser->scope,
5152                                            name,
5153                                            template_p);
5154               parser->scope = NULL_TREE;
5155               parser->qualifying_scope = NULL_TREE;
5156               parser->object_scope = NULL_TREE;
5157             }
5158           if (scope && name && BASELINK_P (name))
5159             adjust_result_of_qualified_name_lookup
5160               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5161           postfix_expression
5162             = finish_class_member_access_expr (postfix_expression, name,
5163                                                template_p, 
5164                                                tf_warning_or_error);
5165         }
5166     }
5167
5168   /* We no longer need to look up names in the scope of the object on
5169      the left-hand side of the `.' or `->' operator.  */
5170   parser->context->object_type = NULL_TREE;
5171
5172   /* Outside of offsetof, these operators may not appear in
5173      constant-expressions.  */
5174   if (!for_offsetof
5175       && (cp_parser_non_integral_constant_expression
5176           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5177     postfix_expression = error_mark_node;
5178
5179   return postfix_expression;
5180 }
5181
5182 /* Parse a parenthesized expression-list.
5183
5184    expression-list:
5185      assignment-expression
5186      expression-list, assignment-expression
5187
5188    attribute-list:
5189      expression-list
5190      identifier
5191      identifier, expression-list
5192
5193    CAST_P is true if this expression is the target of a cast.
5194
5195    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5196    argument pack.
5197
5198    Returns a vector of trees.  Each element is a representation of an
5199    assignment-expression.  NULL is returned if the ( and or ) are
5200    missing.  An empty, but allocated, vector is returned on no
5201    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5202    if this is really an attribute list being parsed.  If
5203    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5204    not all of the expressions in the list were constant.  */
5205
5206 static VEC(tree,gc) *
5207 cp_parser_parenthesized_expression_list (cp_parser* parser,
5208                                          bool is_attribute_list,
5209                                          bool cast_p,
5210                                          bool allow_expansion_p,
5211                                          bool *non_constant_p)
5212 {
5213   VEC(tree,gc) *expression_list;
5214   bool fold_expr_p = is_attribute_list;
5215   tree identifier = NULL_TREE;
5216   bool saved_greater_than_is_operator_p;
5217
5218   /* Assume all the expressions will be constant.  */
5219   if (non_constant_p)
5220     *non_constant_p = false;
5221
5222   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5223     return NULL;
5224
5225   expression_list = make_tree_vector ();
5226
5227   /* Within a parenthesized expression, a `>' token is always
5228      the greater-than operator.  */
5229   saved_greater_than_is_operator_p
5230     = parser->greater_than_is_operator_p;
5231   parser->greater_than_is_operator_p = true;
5232
5233   /* Consume expressions until there are no more.  */
5234   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5235     while (true)
5236       {
5237         tree expr;
5238
5239         /* At the beginning of attribute lists, check to see if the
5240            next token is an identifier.  */
5241         if (is_attribute_list
5242             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5243           {
5244             cp_token *token;
5245
5246             /* Consume the identifier.  */
5247             token = cp_lexer_consume_token (parser->lexer);
5248             /* Save the identifier.  */
5249             identifier = token->u.value;
5250           }
5251         else
5252           {
5253             bool expr_non_constant_p;
5254
5255             /* Parse the next assignment-expression.  */
5256             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5257               {
5258                 /* A braced-init-list.  */
5259                 maybe_warn_cpp0x ("extended initializer lists");
5260                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5261                 if (non_constant_p && expr_non_constant_p)
5262                   *non_constant_p = true;
5263               }
5264             else if (non_constant_p)
5265               {
5266                 expr = (cp_parser_constant_expression
5267                         (parser, /*allow_non_constant_p=*/true,
5268                          &expr_non_constant_p));
5269                 if (expr_non_constant_p)
5270                   *non_constant_p = true;
5271               }
5272             else
5273               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5274
5275             if (fold_expr_p)
5276               expr = fold_non_dependent_expr (expr);
5277
5278             /* If we have an ellipsis, then this is an expression
5279                expansion.  */
5280             if (allow_expansion_p
5281                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5282               {
5283                 /* Consume the `...'.  */
5284                 cp_lexer_consume_token (parser->lexer);
5285
5286                 /* Build the argument pack.  */
5287                 expr = make_pack_expansion (expr);
5288               }
5289
5290              /* Add it to the list.  We add error_mark_node
5291                 expressions to the list, so that we can still tell if
5292                 the correct form for a parenthesized expression-list
5293                 is found. That gives better errors.  */
5294             VEC_safe_push (tree, gc, expression_list, expr);
5295
5296             if (expr == error_mark_node)
5297               goto skip_comma;
5298           }
5299
5300         /* After the first item, attribute lists look the same as
5301            expression lists.  */
5302         is_attribute_list = false;
5303
5304       get_comma:;
5305         /* If the next token isn't a `,', then we are done.  */
5306         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5307           break;
5308
5309         /* Otherwise, consume the `,' and keep going.  */
5310         cp_lexer_consume_token (parser->lexer);
5311       }
5312
5313   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5314     {
5315       int ending;
5316
5317     skip_comma:;
5318       /* We try and resync to an unnested comma, as that will give the
5319          user better diagnostics.  */
5320       ending = cp_parser_skip_to_closing_parenthesis (parser,
5321                                                       /*recovering=*/true,
5322                                                       /*or_comma=*/true,
5323                                                       /*consume_paren=*/true);
5324       if (ending < 0)
5325         goto get_comma;
5326       if (!ending)
5327         {
5328           parser->greater_than_is_operator_p
5329             = saved_greater_than_is_operator_p;
5330           return NULL;
5331         }
5332     }
5333
5334   parser->greater_than_is_operator_p
5335     = saved_greater_than_is_operator_p;
5336
5337   if (identifier)
5338     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5339
5340   return expression_list;
5341 }
5342
5343 /* Parse a pseudo-destructor-name.
5344
5345    pseudo-destructor-name:
5346      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5347      :: [opt] nested-name-specifier template template-id :: ~ type-name
5348      :: [opt] nested-name-specifier [opt] ~ type-name
5349
5350    If either of the first two productions is used, sets *SCOPE to the
5351    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5352    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5353    or ERROR_MARK_NODE if the parse fails.  */
5354
5355 static void
5356 cp_parser_pseudo_destructor_name (cp_parser* parser,
5357                                   tree* scope,
5358                                   tree* type)
5359 {
5360   bool nested_name_specifier_p;
5361
5362   /* Assume that things will not work out.  */
5363   *type = error_mark_node;
5364
5365   /* Look for the optional `::' operator.  */
5366   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5367   /* Look for the optional nested-name-specifier.  */
5368   nested_name_specifier_p
5369     = (cp_parser_nested_name_specifier_opt (parser,
5370                                             /*typename_keyword_p=*/false,
5371                                             /*check_dependency_p=*/true,
5372                                             /*type_p=*/false,
5373                                             /*is_declaration=*/false)
5374        != NULL_TREE);
5375   /* Now, if we saw a nested-name-specifier, we might be doing the
5376      second production.  */
5377   if (nested_name_specifier_p
5378       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5379     {
5380       /* Consume the `template' keyword.  */
5381       cp_lexer_consume_token (parser->lexer);
5382       /* Parse the template-id.  */
5383       cp_parser_template_id (parser,
5384                              /*template_keyword_p=*/true,
5385                              /*check_dependency_p=*/false,
5386                              /*is_declaration=*/true);
5387       /* Look for the `::' token.  */
5388       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5389     }
5390   /* If the next token is not a `~', then there might be some
5391      additional qualification.  */
5392   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5393     {
5394       /* At this point, we're looking for "type-name :: ~".  The type-name
5395          must not be a class-name, since this is a pseudo-destructor.  So,
5396          it must be either an enum-name, or a typedef-name -- both of which
5397          are just identifiers.  So, we peek ahead to check that the "::"
5398          and "~" tokens are present; if they are not, then we can avoid
5399          calling type_name.  */
5400       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5401           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5402           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5403         {
5404           cp_parser_error (parser, "non-scalar type");
5405           return;
5406         }
5407
5408       /* Look for the type-name.  */
5409       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5410       if (*scope == error_mark_node)
5411         return;
5412
5413       /* Look for the `::' token.  */
5414       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5415     }
5416   else
5417     *scope = NULL_TREE;
5418
5419   /* Look for the `~'.  */
5420   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5421   /* Look for the type-name again.  We are not responsible for
5422      checking that it matches the first type-name.  */
5423   *type = cp_parser_nonclass_name (parser);
5424 }
5425
5426 /* Parse a unary-expression.
5427
5428    unary-expression:
5429      postfix-expression
5430      ++ cast-expression
5431      -- cast-expression
5432      unary-operator cast-expression
5433      sizeof unary-expression
5434      sizeof ( type-id )
5435      new-expression
5436      delete-expression
5437
5438    GNU Extensions:
5439
5440    unary-expression:
5441      __extension__ cast-expression
5442      __alignof__ unary-expression
5443      __alignof__ ( type-id )
5444      __real__ cast-expression
5445      __imag__ cast-expression
5446      && identifier
5447
5448    ADDRESS_P is true iff the unary-expression is appearing as the
5449    operand of the `&' operator.   CAST_P is true if this expression is
5450    the target of a cast.
5451
5452    Returns a representation of the expression.  */
5453
5454 static tree
5455 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5456                             cp_id_kind * pidk)
5457 {
5458   cp_token *token;
5459   enum tree_code unary_operator;
5460
5461   /* Peek at the next token.  */
5462   token = cp_lexer_peek_token (parser->lexer);
5463   /* Some keywords give away the kind of expression.  */
5464   if (token->type == CPP_KEYWORD)
5465     {
5466       enum rid keyword = token->keyword;
5467
5468       switch (keyword)
5469         {
5470         case RID_ALIGNOF:
5471         case RID_SIZEOF:
5472           {
5473             tree operand;
5474             enum tree_code op;
5475
5476             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5477             /* Consume the token.  */
5478             cp_lexer_consume_token (parser->lexer);
5479             /* Parse the operand.  */
5480             operand = cp_parser_sizeof_operand (parser, keyword);
5481
5482             if (TYPE_P (operand))
5483               return cxx_sizeof_or_alignof_type (operand, op, true);
5484             else
5485               return cxx_sizeof_or_alignof_expr (operand, op, true);
5486           }
5487
5488         case RID_NEW:
5489           return cp_parser_new_expression (parser);
5490
5491         case RID_DELETE:
5492           return cp_parser_delete_expression (parser);
5493
5494         case RID_EXTENSION:
5495           {
5496             /* The saved value of the PEDANTIC flag.  */
5497             int saved_pedantic;
5498             tree expr;
5499
5500             /* Save away the PEDANTIC flag.  */
5501             cp_parser_extension_opt (parser, &saved_pedantic);
5502             /* Parse the cast-expression.  */
5503             expr = cp_parser_simple_cast_expression (parser);
5504             /* Restore the PEDANTIC flag.  */
5505             pedantic = saved_pedantic;
5506
5507             return expr;
5508           }
5509
5510         case RID_REALPART:
5511         case RID_IMAGPART:
5512           {
5513             tree expression;
5514
5515             /* Consume the `__real__' or `__imag__' token.  */
5516             cp_lexer_consume_token (parser->lexer);
5517             /* Parse the cast-expression.  */
5518             expression = cp_parser_simple_cast_expression (parser);
5519             /* Create the complete representation.  */
5520             return build_x_unary_op ((keyword == RID_REALPART
5521                                       ? REALPART_EXPR : IMAGPART_EXPR),
5522                                      expression,
5523                                      tf_warning_or_error);
5524           }
5525           break;
5526
5527         default:
5528           break;
5529         }
5530     }
5531
5532   /* Look for the `:: new' and `:: delete', which also signal the
5533      beginning of a new-expression, or delete-expression,
5534      respectively.  If the next token is `::', then it might be one of
5535      these.  */
5536   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5537     {
5538       enum rid keyword;
5539
5540       /* See if the token after the `::' is one of the keywords in
5541          which we're interested.  */
5542       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5543       /* If it's `new', we have a new-expression.  */
5544       if (keyword == RID_NEW)
5545         return cp_parser_new_expression (parser);
5546       /* Similarly, for `delete'.  */
5547       else if (keyword == RID_DELETE)
5548         return cp_parser_delete_expression (parser);
5549     }
5550
5551   /* Look for a unary operator.  */
5552   unary_operator = cp_parser_unary_operator (token);
5553   /* The `++' and `--' operators can be handled similarly, even though
5554      they are not technically unary-operators in the grammar.  */
5555   if (unary_operator == ERROR_MARK)
5556     {
5557       if (token->type == CPP_PLUS_PLUS)
5558         unary_operator = PREINCREMENT_EXPR;
5559       else if (token->type == CPP_MINUS_MINUS)
5560         unary_operator = PREDECREMENT_EXPR;
5561       /* Handle the GNU address-of-label extension.  */
5562       else if (cp_parser_allow_gnu_extensions_p (parser)
5563                && token->type == CPP_AND_AND)
5564         {
5565           tree identifier;
5566           tree expression;
5567           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5568
5569           /* Consume the '&&' token.  */
5570           cp_lexer_consume_token (parser->lexer);
5571           /* Look for the identifier.  */
5572           identifier = cp_parser_identifier (parser);
5573           /* Create an expression representing the address.  */
5574           expression = finish_label_address_expr (identifier, loc);
5575           if (cp_parser_non_integral_constant_expression (parser,
5576                                                 "the address of a label"))
5577             expression = error_mark_node;
5578           return expression;
5579         }
5580     }
5581   if (unary_operator != ERROR_MARK)
5582     {
5583       tree cast_expression;
5584       tree expression = error_mark_node;
5585       const char *non_constant_p = NULL;
5586
5587       /* Consume the operator token.  */
5588       token = cp_lexer_consume_token (parser->lexer);
5589       /* Parse the cast-expression.  */
5590       cast_expression
5591         = cp_parser_cast_expression (parser,
5592                                      unary_operator == ADDR_EXPR,
5593                                      /*cast_p=*/false, pidk);
5594       /* Now, build an appropriate representation.  */
5595       switch (unary_operator)
5596         {
5597         case INDIRECT_REF:
5598           non_constant_p = "%<*%>";
5599           expression = build_x_indirect_ref (cast_expression, "unary *",
5600                                              tf_warning_or_error);
5601           break;
5602
5603         case ADDR_EXPR:
5604           non_constant_p = "%<&%>";
5605           /* Fall through.  */
5606         case BIT_NOT_EXPR:
5607           expression = build_x_unary_op (unary_operator, cast_expression,
5608                                          tf_warning_or_error);
5609           break;
5610
5611         case PREINCREMENT_EXPR:
5612         case PREDECREMENT_EXPR:
5613           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5614                             ? "%<++%>" : "%<--%>");
5615           /* Fall through.  */
5616         case UNARY_PLUS_EXPR:
5617         case NEGATE_EXPR:
5618         case TRUTH_NOT_EXPR:
5619           expression = finish_unary_op_expr (unary_operator, cast_expression);
5620           break;
5621
5622         default:
5623           gcc_unreachable ();
5624         }
5625
5626       if (non_constant_p
5627           && cp_parser_non_integral_constant_expression (parser,
5628                                                          non_constant_p))
5629         expression = error_mark_node;
5630
5631       return expression;
5632     }
5633
5634   return cp_parser_postfix_expression (parser, address_p, cast_p,
5635                                        /*member_access_only_p=*/false,
5636                                        pidk);
5637 }
5638
5639 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5640    unary-operator, the corresponding tree code is returned.  */
5641
5642 static enum tree_code
5643 cp_parser_unary_operator (cp_token* token)
5644 {
5645   switch (token->type)
5646     {
5647     case CPP_MULT:
5648       return INDIRECT_REF;
5649
5650     case CPP_AND:
5651       return ADDR_EXPR;
5652
5653     case CPP_PLUS:
5654       return UNARY_PLUS_EXPR;
5655
5656     case CPP_MINUS:
5657       return NEGATE_EXPR;
5658
5659     case CPP_NOT:
5660       return TRUTH_NOT_EXPR;
5661
5662     case CPP_COMPL:
5663       return BIT_NOT_EXPR;
5664
5665     default:
5666       return ERROR_MARK;
5667     }
5668 }
5669
5670 /* Parse a new-expression.
5671
5672    new-expression:
5673      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5674      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5675
5676    Returns a representation of the expression.  */
5677
5678 static tree
5679 cp_parser_new_expression (cp_parser* parser)
5680 {
5681   bool global_scope_p;
5682   VEC(tree,gc) *placement;
5683   tree type;
5684   VEC(tree,gc) *initializer;
5685   tree nelts;
5686   tree ret;
5687
5688   /* Look for the optional `::' operator.  */
5689   global_scope_p
5690     = (cp_parser_global_scope_opt (parser,
5691                                    /*current_scope_valid_p=*/false)
5692        != NULL_TREE);
5693   /* Look for the `new' operator.  */
5694   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5695   /* There's no easy way to tell a new-placement from the
5696      `( type-id )' construct.  */
5697   cp_parser_parse_tentatively (parser);
5698   /* Look for a new-placement.  */
5699   placement = cp_parser_new_placement (parser);
5700   /* If that didn't work out, there's no new-placement.  */
5701   if (!cp_parser_parse_definitely (parser))
5702     {
5703       if (placement != NULL)
5704         release_tree_vector (placement);
5705       placement = NULL;
5706     }
5707
5708   /* If the next token is a `(', then we have a parenthesized
5709      type-id.  */
5710   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5711     {
5712       cp_token *token;
5713       /* Consume the `('.  */
5714       cp_lexer_consume_token (parser->lexer);
5715       /* Parse the type-id.  */
5716       type = cp_parser_type_id (parser);
5717       /* Look for the closing `)'.  */
5718       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5719       token = cp_lexer_peek_token (parser->lexer);
5720       /* There should not be a direct-new-declarator in this production,
5721          but GCC used to allowed this, so we check and emit a sensible error
5722          message for this case.  */
5723       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5724         {
5725           error_at (token->location,
5726                     "array bound forbidden after parenthesized type-id");
5727           inform (token->location, 
5728                   "try removing the parentheses around the type-id");
5729           cp_parser_direct_new_declarator (parser);
5730         }
5731       nelts = NULL_TREE;
5732     }
5733   /* Otherwise, there must be a new-type-id.  */
5734   else
5735     type = cp_parser_new_type_id (parser, &nelts);
5736
5737   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5738   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5739       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5740     initializer = cp_parser_new_initializer (parser);
5741   else
5742     initializer = NULL;
5743
5744   /* A new-expression may not appear in an integral constant
5745      expression.  */
5746   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5747     ret = error_mark_node;
5748   else
5749     {
5750       /* Create a representation of the new-expression.  */
5751       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5752                        tf_warning_or_error);
5753     }
5754
5755   if (placement != NULL)
5756     release_tree_vector (placement);
5757   if (initializer != NULL)
5758     release_tree_vector (initializer);
5759
5760   return ret;
5761 }
5762
5763 /* Parse a new-placement.
5764
5765    new-placement:
5766      ( expression-list )
5767
5768    Returns the same representation as for an expression-list.  */
5769
5770 static VEC(tree,gc) *
5771 cp_parser_new_placement (cp_parser* parser)
5772 {
5773   VEC(tree,gc) *expression_list;
5774
5775   /* Parse the expression-list.  */
5776   expression_list = (cp_parser_parenthesized_expression_list
5777                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5778                       /*non_constant_p=*/NULL));
5779
5780   return expression_list;
5781 }
5782
5783 /* Parse a new-type-id.
5784
5785    new-type-id:
5786      type-specifier-seq new-declarator [opt]
5787
5788    Returns the TYPE allocated.  If the new-type-id indicates an array
5789    type, *NELTS is set to the number of elements in the last array
5790    bound; the TYPE will not include the last array bound.  */
5791
5792 static tree
5793 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5794 {
5795   cp_decl_specifier_seq type_specifier_seq;
5796   cp_declarator *new_declarator;
5797   cp_declarator *declarator;
5798   cp_declarator *outer_declarator;
5799   const char *saved_message;
5800   tree type;
5801
5802   /* The type-specifier sequence must not contain type definitions.
5803      (It cannot contain declarations of new types either, but if they
5804      are not definitions we will catch that because they are not
5805      complete.)  */
5806   saved_message = parser->type_definition_forbidden_message;
5807   parser->type_definition_forbidden_message
5808     = "types may not be defined in a new-type-id";
5809   /* Parse the type-specifier-seq.  */
5810   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5811                                 /*is_trailing_return=*/false,
5812                                 &type_specifier_seq);
5813   /* Restore the old message.  */
5814   parser->type_definition_forbidden_message = saved_message;
5815   /* Parse the new-declarator.  */
5816   new_declarator = cp_parser_new_declarator_opt (parser);
5817
5818   /* Determine the number of elements in the last array dimension, if
5819      any.  */
5820   *nelts = NULL_TREE;
5821   /* Skip down to the last array dimension.  */
5822   declarator = new_declarator;
5823   outer_declarator = NULL;
5824   while (declarator && (declarator->kind == cdk_pointer
5825                         || declarator->kind == cdk_ptrmem))
5826     {
5827       outer_declarator = declarator;
5828       declarator = declarator->declarator;
5829     }
5830   while (declarator
5831          && declarator->kind == cdk_array
5832          && declarator->declarator
5833          && declarator->declarator->kind == cdk_array)
5834     {
5835       outer_declarator = declarator;
5836       declarator = declarator->declarator;
5837     }
5838
5839   if (declarator && declarator->kind == cdk_array)
5840     {
5841       *nelts = declarator->u.array.bounds;
5842       if (*nelts == error_mark_node)
5843         *nelts = integer_one_node;
5844
5845       if (outer_declarator)
5846         outer_declarator->declarator = declarator->declarator;
5847       else
5848         new_declarator = NULL;
5849     }
5850
5851   type = groktypename (&type_specifier_seq, new_declarator, false);
5852   return type;
5853 }
5854
5855 /* Parse an (optional) new-declarator.
5856
5857    new-declarator:
5858      ptr-operator new-declarator [opt]
5859      direct-new-declarator
5860
5861    Returns the declarator.  */
5862
5863 static cp_declarator *
5864 cp_parser_new_declarator_opt (cp_parser* parser)
5865 {
5866   enum tree_code code;
5867   tree type;
5868   cp_cv_quals cv_quals;
5869
5870   /* We don't know if there's a ptr-operator next, or not.  */
5871   cp_parser_parse_tentatively (parser);
5872   /* Look for a ptr-operator.  */
5873   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5874   /* If that worked, look for more new-declarators.  */
5875   if (cp_parser_parse_definitely (parser))
5876     {
5877       cp_declarator *declarator;
5878
5879       /* Parse another optional declarator.  */
5880       declarator = cp_parser_new_declarator_opt (parser);
5881
5882       return cp_parser_make_indirect_declarator
5883         (code, type, cv_quals, declarator);
5884     }
5885
5886   /* If the next token is a `[', there is a direct-new-declarator.  */
5887   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5888     return cp_parser_direct_new_declarator (parser);
5889
5890   return NULL;
5891 }
5892
5893 /* Parse a direct-new-declarator.
5894
5895    direct-new-declarator:
5896      [ expression ]
5897      direct-new-declarator [constant-expression]
5898
5899    */
5900
5901 static cp_declarator *
5902 cp_parser_direct_new_declarator (cp_parser* parser)
5903 {
5904   cp_declarator *declarator = NULL;
5905
5906   while (true)
5907     {
5908       tree expression;
5909
5910       /* Look for the opening `['.  */
5911       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5912       /* The first expression is not required to be constant.  */
5913       if (!declarator)
5914         {
5915           cp_token *token = cp_lexer_peek_token (parser->lexer);
5916           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5917           /* The standard requires that the expression have integral
5918              type.  DR 74 adds enumeration types.  We believe that the
5919              real intent is that these expressions be handled like the
5920              expression in a `switch' condition, which also allows
5921              classes with a single conversion to integral or
5922              enumeration type.  */
5923           if (!processing_template_decl)
5924             {
5925               expression
5926                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5927                                               expression,
5928                                               /*complain=*/true);
5929               if (!expression)
5930                 {
5931                   error_at (token->location,
5932                             "expression in new-declarator must have integral "
5933                             "or enumeration type");
5934                   expression = error_mark_node;
5935                 }
5936             }
5937         }
5938       /* But all the other expressions must be.  */
5939       else
5940         expression
5941           = cp_parser_constant_expression (parser,
5942                                            /*allow_non_constant=*/false,
5943                                            NULL);
5944       /* Look for the closing `]'.  */
5945       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5946
5947       /* Add this bound to the declarator.  */
5948       declarator = make_array_declarator (declarator, expression);
5949
5950       /* If the next token is not a `[', then there are no more
5951          bounds.  */
5952       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5953         break;
5954     }
5955
5956   return declarator;
5957 }
5958
5959 /* Parse a new-initializer.
5960
5961    new-initializer:
5962      ( expression-list [opt] )
5963      braced-init-list
5964
5965    Returns a representation of the expression-list.  */
5966
5967 static VEC(tree,gc) *
5968 cp_parser_new_initializer (cp_parser* parser)
5969 {
5970   VEC(tree,gc) *expression_list;
5971
5972   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5973     {
5974       tree t;
5975       bool expr_non_constant_p;
5976       maybe_warn_cpp0x ("extended initializer lists");
5977       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5978       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5979       expression_list = make_tree_vector_single (t);
5980     }
5981   else
5982     expression_list = (cp_parser_parenthesized_expression_list
5983                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5984                         /*non_constant_p=*/NULL));
5985
5986   return expression_list;
5987 }
5988
5989 /* Parse a delete-expression.
5990
5991    delete-expression:
5992      :: [opt] delete cast-expression
5993      :: [opt] delete [ ] cast-expression
5994
5995    Returns a representation of the expression.  */
5996
5997 static tree
5998 cp_parser_delete_expression (cp_parser* parser)
5999 {
6000   bool global_scope_p;
6001   bool array_p;
6002   tree expression;
6003
6004   /* Look for the optional `::' operator.  */
6005   global_scope_p
6006     = (cp_parser_global_scope_opt (parser,
6007                                    /*current_scope_valid_p=*/false)
6008        != NULL_TREE);
6009   /* Look for the `delete' keyword.  */
6010   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6011   /* See if the array syntax is in use.  */
6012   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6013     {
6014       /* Consume the `[' token.  */
6015       cp_lexer_consume_token (parser->lexer);
6016       /* Look for the `]' token.  */
6017       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6018       /* Remember that this is the `[]' construct.  */
6019       array_p = true;
6020     }
6021   else
6022     array_p = false;
6023
6024   /* Parse the cast-expression.  */
6025   expression = cp_parser_simple_cast_expression (parser);
6026
6027   /* A delete-expression may not appear in an integral constant
6028      expression.  */
6029   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6030     return error_mark_node;
6031
6032   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6033 }
6034
6035 /* Returns true if TOKEN may start a cast-expression and false
6036    otherwise.  */
6037
6038 static bool
6039 cp_parser_token_starts_cast_expression (cp_token *token)
6040 {
6041   switch (token->type)
6042     {
6043     case CPP_COMMA:
6044     case CPP_SEMICOLON:
6045     case CPP_QUERY:
6046     case CPP_COLON:
6047     case CPP_CLOSE_SQUARE:
6048     case CPP_CLOSE_PAREN:
6049     case CPP_CLOSE_BRACE:
6050     case CPP_DOT:
6051     case CPP_DOT_STAR:
6052     case CPP_DEREF:
6053     case CPP_DEREF_STAR:
6054     case CPP_DIV:
6055     case CPP_MOD:
6056     case CPP_LSHIFT:
6057     case CPP_RSHIFT:
6058     case CPP_LESS:
6059     case CPP_GREATER:
6060     case CPP_LESS_EQ:
6061     case CPP_GREATER_EQ:
6062     case CPP_EQ_EQ:
6063     case CPP_NOT_EQ:
6064     case CPP_EQ:
6065     case CPP_MULT_EQ:
6066     case CPP_DIV_EQ:
6067     case CPP_MOD_EQ:
6068     case CPP_PLUS_EQ:
6069     case CPP_MINUS_EQ:
6070     case CPP_RSHIFT_EQ:
6071     case CPP_LSHIFT_EQ:
6072     case CPP_AND_EQ:
6073     case CPP_XOR_EQ:
6074     case CPP_OR_EQ:
6075     case CPP_XOR:
6076     case CPP_OR:
6077     case CPP_OR_OR:
6078     case CPP_EOF:
6079       return false;
6080
6081       /* '[' may start a primary-expression in obj-c++.  */
6082     case CPP_OPEN_SQUARE:
6083       return c_dialect_objc ();
6084
6085     default:
6086       return true;
6087     }
6088 }
6089
6090 /* Parse a cast-expression.
6091
6092    cast-expression:
6093      unary-expression
6094      ( type-id ) cast-expression
6095
6096    ADDRESS_P is true iff the unary-expression is appearing as the
6097    operand of the `&' operator.   CAST_P is true if this expression is
6098    the target of a cast.
6099
6100    Returns a representation of the expression.  */
6101
6102 static tree
6103 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6104                            cp_id_kind * pidk)
6105 {
6106   /* If it's a `(', then we might be looking at a cast.  */
6107   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6108     {
6109       tree type = NULL_TREE;
6110       tree expr = NULL_TREE;
6111       bool compound_literal_p;
6112       const char *saved_message;
6113
6114       /* There's no way to know yet whether or not this is a cast.
6115          For example, `(int (3))' is a unary-expression, while `(int)
6116          3' is a cast.  So, we resort to parsing tentatively.  */
6117       cp_parser_parse_tentatively (parser);
6118       /* Types may not be defined in a cast.  */
6119       saved_message = parser->type_definition_forbidden_message;
6120       parser->type_definition_forbidden_message
6121         = "types may not be defined in casts";
6122       /* Consume the `('.  */
6123       cp_lexer_consume_token (parser->lexer);
6124       /* A very tricky bit is that `(struct S) { 3 }' is a
6125          compound-literal (which we permit in C++ as an extension).
6126          But, that construct is not a cast-expression -- it is a
6127          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6128          is legal; if the compound-literal were a cast-expression,
6129          you'd need an extra set of parentheses.)  But, if we parse
6130          the type-id, and it happens to be a class-specifier, then we
6131          will commit to the parse at that point, because we cannot
6132          undo the action that is done when creating a new class.  So,
6133          then we cannot back up and do a postfix-expression.
6134
6135          Therefore, we scan ahead to the closing `)', and check to see
6136          if the token after the `)' is a `{'.  If so, we are not
6137          looking at a cast-expression.
6138
6139          Save tokens so that we can put them back.  */
6140       cp_lexer_save_tokens (parser->lexer);
6141       /* Skip tokens until the next token is a closing parenthesis.
6142          If we find the closing `)', and the next token is a `{', then
6143          we are looking at a compound-literal.  */
6144       compound_literal_p
6145         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6146                                                   /*consume_paren=*/true)
6147            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6148       /* Roll back the tokens we skipped.  */
6149       cp_lexer_rollback_tokens (parser->lexer);
6150       /* If we were looking at a compound-literal, simulate an error
6151          so that the call to cp_parser_parse_definitely below will
6152          fail.  */
6153       if (compound_literal_p)
6154         cp_parser_simulate_error (parser);
6155       else
6156         {
6157           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6158           parser->in_type_id_in_expr_p = true;
6159           /* Look for the type-id.  */
6160           type = cp_parser_type_id (parser);
6161           /* Look for the closing `)'.  */
6162           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6163           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6164         }
6165
6166       /* Restore the saved message.  */
6167       parser->type_definition_forbidden_message = saved_message;
6168
6169       /* At this point this can only be either a cast or a
6170          parenthesized ctor such as `(T ())' that looks like a cast to
6171          function returning T.  */
6172       if (!cp_parser_error_occurred (parser)
6173           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6174                                                      (parser->lexer)))
6175         {
6176           cp_parser_parse_definitely (parser);
6177           expr = cp_parser_cast_expression (parser,
6178                                             /*address_p=*/false,
6179                                             /*cast_p=*/true, pidk);
6180
6181           /* Warn about old-style casts, if so requested.  */
6182           if (warn_old_style_cast
6183               && !in_system_header
6184               && !VOID_TYPE_P (type)
6185               && current_lang_name != lang_name_c)
6186             warning (OPT_Wold_style_cast, "use of old-style cast");
6187
6188           /* Only type conversions to integral or enumeration types
6189              can be used in constant-expressions.  */
6190           if (!cast_valid_in_integral_constant_expression_p (type)
6191               && (cp_parser_non_integral_constant_expression
6192                   (parser,
6193                    "a cast to a type other than an integral or "
6194                    "enumeration type")))
6195             return error_mark_node;
6196
6197           /* Perform the cast.  */
6198           expr = build_c_cast (input_location, type, expr);
6199           return expr;
6200         }
6201       else 
6202         cp_parser_abort_tentative_parse (parser);
6203     }
6204
6205   /* If we get here, then it's not a cast, so it must be a
6206      unary-expression.  */
6207   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6208 }
6209
6210 /* Parse a binary expression of the general form:
6211
6212    pm-expression:
6213      cast-expression
6214      pm-expression .* cast-expression
6215      pm-expression ->* cast-expression
6216
6217    multiplicative-expression:
6218      pm-expression
6219      multiplicative-expression * pm-expression
6220      multiplicative-expression / pm-expression
6221      multiplicative-expression % pm-expression
6222
6223    additive-expression:
6224      multiplicative-expression
6225      additive-expression + multiplicative-expression
6226      additive-expression - multiplicative-expression
6227
6228    shift-expression:
6229      additive-expression
6230      shift-expression << additive-expression
6231      shift-expression >> additive-expression
6232
6233    relational-expression:
6234      shift-expression
6235      relational-expression < shift-expression
6236      relational-expression > shift-expression
6237      relational-expression <= shift-expression
6238      relational-expression >= shift-expression
6239
6240   GNU Extension:
6241
6242    relational-expression:
6243      relational-expression <? shift-expression
6244      relational-expression >? shift-expression
6245
6246    equality-expression:
6247      relational-expression
6248      equality-expression == relational-expression
6249      equality-expression != relational-expression
6250
6251    and-expression:
6252      equality-expression
6253      and-expression & equality-expression
6254
6255    exclusive-or-expression:
6256      and-expression
6257      exclusive-or-expression ^ and-expression
6258
6259    inclusive-or-expression:
6260      exclusive-or-expression
6261      inclusive-or-expression | exclusive-or-expression
6262
6263    logical-and-expression:
6264      inclusive-or-expression
6265      logical-and-expression && inclusive-or-expression
6266
6267    logical-or-expression:
6268      logical-and-expression
6269      logical-or-expression || logical-and-expression
6270
6271    All these are implemented with a single function like:
6272
6273    binary-expression:
6274      simple-cast-expression
6275      binary-expression <token> binary-expression
6276
6277    CAST_P is true if this expression is the target of a cast.
6278
6279    The binops_by_token map is used to get the tree codes for each <token> type.
6280    binary-expressions are associated according to a precedence table.  */
6281
6282 #define TOKEN_PRECEDENCE(token)                              \
6283 (((token->type == CPP_GREATER                                \
6284    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6285   && !parser->greater_than_is_operator_p)                    \
6286  ? PREC_NOT_OPERATOR                                         \
6287  : binops_by_token[token->type].prec)
6288
6289 static tree
6290 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6291                              bool no_toplevel_fold_p,
6292                              enum cp_parser_prec prec,
6293                              cp_id_kind * pidk)
6294 {
6295   cp_parser_expression_stack stack;
6296   cp_parser_expression_stack_entry *sp = &stack[0];
6297   tree lhs, rhs;
6298   cp_token *token;
6299   enum tree_code tree_type, lhs_type, rhs_type;
6300   enum cp_parser_prec new_prec, lookahead_prec;
6301   bool overloaded_p;
6302
6303   /* Parse the first expression.  */
6304   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6305   lhs_type = ERROR_MARK;
6306
6307   for (;;)
6308     {
6309       /* Get an operator token.  */
6310       token = cp_lexer_peek_token (parser->lexer);
6311
6312       if (warn_cxx0x_compat
6313           && token->type == CPP_RSHIFT
6314           && !parser->greater_than_is_operator_p)
6315         {
6316           if (warning_at (token->location, OPT_Wc__0x_compat, 
6317                           "%<>>%> operator will be treated as"
6318                           " two right angle brackets in C++0x"))
6319             inform (token->location,
6320                     "suggest parentheses around %<>>%> expression");
6321         }
6322
6323       new_prec = TOKEN_PRECEDENCE (token);
6324
6325       /* Popping an entry off the stack means we completed a subexpression:
6326          - either we found a token which is not an operator (`>' where it is not
6327            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6328            will happen repeatedly;
6329          - or, we found an operator which has lower priority.  This is the case
6330            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6331            parsing `3 * 4'.  */
6332       if (new_prec <= prec)
6333         {
6334           if (sp == stack)
6335             break;
6336           else
6337             goto pop;
6338         }
6339
6340      get_rhs:
6341       tree_type = binops_by_token[token->type].tree_type;
6342
6343       /* We used the operator token.  */
6344       cp_lexer_consume_token (parser->lexer);
6345
6346       /* For "false && x" or "true || x", x will never be executed;
6347          disable warnings while evaluating it.  */
6348       if (tree_type == TRUTH_ANDIF_EXPR)
6349         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6350       else if (tree_type == TRUTH_ORIF_EXPR)
6351         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6352
6353       /* Extract another operand.  It may be the RHS of this expression
6354          or the LHS of a new, higher priority expression.  */
6355       rhs = cp_parser_simple_cast_expression (parser);
6356       rhs_type = ERROR_MARK;
6357
6358       /* Get another operator token.  Look up its precedence to avoid
6359          building a useless (immediately popped) stack entry for common
6360          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6361       token = cp_lexer_peek_token (parser->lexer);
6362       lookahead_prec = TOKEN_PRECEDENCE (token);
6363       if (lookahead_prec > new_prec)
6364         {
6365           /* ... and prepare to parse the RHS of the new, higher priority
6366              expression.  Since precedence levels on the stack are
6367              monotonically increasing, we do not have to care about
6368              stack overflows.  */
6369           sp->prec = prec;
6370           sp->tree_type = tree_type;
6371           sp->lhs = lhs;
6372           sp->lhs_type = lhs_type;
6373           sp++;
6374           lhs = rhs;
6375           lhs_type = rhs_type;
6376           prec = new_prec;
6377           new_prec = lookahead_prec;
6378           goto get_rhs;
6379
6380          pop:
6381           lookahead_prec = new_prec;
6382           /* If the stack is not empty, we have parsed into LHS the right side
6383              (`4' in the example above) of an expression we had suspended.
6384              We can use the information on the stack to recover the LHS (`3')
6385              from the stack together with the tree code (`MULT_EXPR'), and
6386              the precedence of the higher level subexpression
6387              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6388              which will be used to actually build the additive expression.  */
6389           --sp;
6390           prec = sp->prec;
6391           tree_type = sp->tree_type;
6392           rhs = lhs;
6393           rhs_type = lhs_type;
6394           lhs = sp->lhs;
6395           lhs_type = sp->lhs_type;
6396         }
6397
6398       /* Undo the disabling of warnings done above.  */
6399       if (tree_type == TRUTH_ANDIF_EXPR)
6400         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6401       else if (tree_type == TRUTH_ORIF_EXPR)
6402         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6403
6404       overloaded_p = false;
6405       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6406          ERROR_MARK for everything that is not a binary expression.
6407          This makes warn_about_parentheses miss some warnings that
6408          involve unary operators.  For unary expressions we should
6409          pass the correct tree_code unless the unary expression was
6410          surrounded by parentheses.
6411       */
6412       if (no_toplevel_fold_p
6413           && lookahead_prec <= prec
6414           && sp == stack
6415           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6416         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6417       else
6418         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6419                                  &overloaded_p, tf_warning_or_error);
6420       lhs_type = tree_type;
6421
6422       /* If the binary operator required the use of an overloaded operator,
6423          then this expression cannot be an integral constant-expression.
6424          An overloaded operator can be used even if both operands are
6425          otherwise permissible in an integral constant-expression if at
6426          least one of the operands is of enumeration type.  */
6427
6428       if (overloaded_p
6429           && (cp_parser_non_integral_constant_expression
6430               (parser, "calls to overloaded operators")))
6431         return error_mark_node;
6432     }
6433
6434   return lhs;
6435 }
6436
6437
6438 /* Parse the `? expression : assignment-expression' part of a
6439    conditional-expression.  The LOGICAL_OR_EXPR is the
6440    logical-or-expression that started the conditional-expression.
6441    Returns a representation of the entire conditional-expression.
6442
6443    This routine is used by cp_parser_assignment_expression.
6444
6445      ? expression : assignment-expression
6446
6447    GNU Extensions:
6448
6449      ? : assignment-expression */
6450
6451 static tree
6452 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6453 {
6454   tree expr;
6455   tree assignment_expr;
6456
6457   /* Consume the `?' token.  */
6458   cp_lexer_consume_token (parser->lexer);
6459   if (cp_parser_allow_gnu_extensions_p (parser)
6460       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6461     {
6462       /* Implicit true clause.  */
6463       expr = NULL_TREE;
6464       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6465     }
6466   else
6467     {
6468       /* Parse the expression.  */
6469       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6470       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6471       c_inhibit_evaluation_warnings +=
6472         ((logical_or_expr == truthvalue_true_node)
6473          - (logical_or_expr == truthvalue_false_node));
6474     }
6475
6476   /* The next token should be a `:'.  */
6477   cp_parser_require (parser, CPP_COLON, "%<:%>");
6478   /* Parse the assignment-expression.  */
6479   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6480   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6481
6482   /* Build the conditional-expression.  */
6483   return build_x_conditional_expr (logical_or_expr,
6484                                    expr,
6485                                    assignment_expr,
6486                                    tf_warning_or_error);
6487 }
6488
6489 /* Parse an assignment-expression.
6490
6491    assignment-expression:
6492      conditional-expression
6493      logical-or-expression assignment-operator assignment_expression
6494      throw-expression
6495
6496    CAST_P is true if this expression is the target of a cast.
6497
6498    Returns a representation for the expression.  */
6499
6500 static tree
6501 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6502                                  cp_id_kind * pidk)
6503 {
6504   tree expr;
6505
6506   /* If the next token is the `throw' keyword, then we're looking at
6507      a throw-expression.  */
6508   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6509     expr = cp_parser_throw_expression (parser);
6510   /* Otherwise, it must be that we are looking at a
6511      logical-or-expression.  */
6512   else
6513     {
6514       /* Parse the binary expressions (logical-or-expression).  */
6515       expr = cp_parser_binary_expression (parser, cast_p, false,
6516                                           PREC_NOT_OPERATOR, pidk);
6517       /* If the next token is a `?' then we're actually looking at a
6518          conditional-expression.  */
6519       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6520         return cp_parser_question_colon_clause (parser, expr);
6521       else
6522         {
6523           enum tree_code assignment_operator;
6524
6525           /* If it's an assignment-operator, we're using the second
6526              production.  */
6527           assignment_operator
6528             = cp_parser_assignment_operator_opt (parser);
6529           if (assignment_operator != ERROR_MARK)
6530             {
6531               bool non_constant_p;
6532
6533               /* Parse the right-hand side of the assignment.  */
6534               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6535
6536               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6537                 maybe_warn_cpp0x ("extended initializer lists");
6538
6539               /* An assignment may not appear in a
6540                  constant-expression.  */
6541               if (cp_parser_non_integral_constant_expression (parser,
6542                                                               "an assignment"))
6543                 return error_mark_node;
6544               /* Build the assignment expression.  */
6545               expr = build_x_modify_expr (expr,
6546                                           assignment_operator,
6547                                           rhs,
6548                                           tf_warning_or_error);
6549             }
6550         }
6551     }
6552
6553   return expr;
6554 }
6555
6556 /* Parse an (optional) assignment-operator.
6557
6558    assignment-operator: one of
6559      = *= /= %= += -= >>= <<= &= ^= |=
6560
6561    GNU Extension:
6562
6563    assignment-operator: one of
6564      <?= >?=
6565
6566    If the next token is an assignment operator, the corresponding tree
6567    code is returned, and the token is consumed.  For example, for
6568    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6569    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6570    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6571    operator, ERROR_MARK is returned.  */
6572
6573 static enum tree_code
6574 cp_parser_assignment_operator_opt (cp_parser* parser)
6575 {
6576   enum tree_code op;
6577   cp_token *token;
6578
6579   /* Peek at the next token.  */
6580   token = cp_lexer_peek_token (parser->lexer);
6581
6582   switch (token->type)
6583     {
6584     case CPP_EQ:
6585       op = NOP_EXPR;
6586       break;
6587
6588     case CPP_MULT_EQ:
6589       op = MULT_EXPR;
6590       break;
6591
6592     case CPP_DIV_EQ:
6593       op = TRUNC_DIV_EXPR;
6594       break;
6595
6596     case CPP_MOD_EQ:
6597       op = TRUNC_MOD_EXPR;
6598       break;
6599
6600     case CPP_PLUS_EQ:
6601       op = PLUS_EXPR;
6602       break;
6603
6604     case CPP_MINUS_EQ:
6605       op = MINUS_EXPR;
6606       break;
6607
6608     case CPP_RSHIFT_EQ:
6609       op = RSHIFT_EXPR;
6610       break;
6611
6612     case CPP_LSHIFT_EQ:
6613       op = LSHIFT_EXPR;
6614       break;
6615
6616     case CPP_AND_EQ:
6617       op = BIT_AND_EXPR;
6618       break;
6619
6620     case CPP_XOR_EQ:
6621       op = BIT_XOR_EXPR;
6622       break;
6623
6624     case CPP_OR_EQ:
6625       op = BIT_IOR_EXPR;
6626       break;
6627
6628     default:
6629       /* Nothing else is an assignment operator.  */
6630       op = ERROR_MARK;
6631     }
6632
6633   /* If it was an assignment operator, consume it.  */
6634   if (op != ERROR_MARK)
6635     cp_lexer_consume_token (parser->lexer);
6636
6637   return op;
6638 }
6639
6640 /* Parse an expression.
6641
6642    expression:
6643      assignment-expression
6644      expression , assignment-expression
6645
6646    CAST_P is true if this expression is the target of a cast.
6647
6648    Returns a representation of the expression.  */
6649
6650 static tree
6651 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6652 {
6653   tree expression = NULL_TREE;
6654
6655   while (true)
6656     {
6657       tree assignment_expression;
6658
6659       /* Parse the next assignment-expression.  */
6660       assignment_expression
6661         = cp_parser_assignment_expression (parser, cast_p, pidk);
6662       /* If this is the first assignment-expression, we can just
6663          save it away.  */
6664       if (!expression)
6665         expression = assignment_expression;
6666       else
6667         expression = build_x_compound_expr (expression,
6668                                             assignment_expression,
6669                                             tf_warning_or_error);
6670       /* If the next token is not a comma, then we are done with the
6671          expression.  */
6672       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6673         break;
6674       /* Consume the `,'.  */
6675       cp_lexer_consume_token (parser->lexer);
6676       /* A comma operator cannot appear in a constant-expression.  */
6677       if (cp_parser_non_integral_constant_expression (parser,
6678                                                       "a comma operator"))
6679         expression = error_mark_node;
6680     }
6681
6682   return expression;
6683 }
6684
6685 /* Parse a constant-expression.
6686
6687    constant-expression:
6688      conditional-expression
6689
6690   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6691   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6692   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6693   is false, NON_CONSTANT_P should be NULL.  */
6694
6695 static tree
6696 cp_parser_constant_expression (cp_parser* parser,
6697                                bool allow_non_constant_p,
6698                                bool *non_constant_p)
6699 {
6700   bool saved_integral_constant_expression_p;
6701   bool saved_allow_non_integral_constant_expression_p;
6702   bool saved_non_integral_constant_expression_p;
6703   tree expression;
6704
6705   /* It might seem that we could simply parse the
6706      conditional-expression, and then check to see if it were
6707      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6708      one that the compiler can figure out is constant, possibly after
6709      doing some simplifications or optimizations.  The standard has a
6710      precise definition of constant-expression, and we must honor
6711      that, even though it is somewhat more restrictive.
6712
6713      For example:
6714
6715        int i[(2, 3)];
6716
6717      is not a legal declaration, because `(2, 3)' is not a
6718      constant-expression.  The `,' operator is forbidden in a
6719      constant-expression.  However, GCC's constant-folding machinery
6720      will fold this operation to an INTEGER_CST for `3'.  */
6721
6722   /* Save the old settings.  */
6723   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6724   saved_allow_non_integral_constant_expression_p
6725     = parser->allow_non_integral_constant_expression_p;
6726   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6727   /* We are now parsing a constant-expression.  */
6728   parser->integral_constant_expression_p = true;
6729   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6730   parser->non_integral_constant_expression_p = false;
6731   /* Although the grammar says "conditional-expression", we parse an
6732      "assignment-expression", which also permits "throw-expression"
6733      and the use of assignment operators.  In the case that
6734      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6735      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6736      actually essential that we look for an assignment-expression.
6737      For example, cp_parser_initializer_clauses uses this function to
6738      determine whether a particular assignment-expression is in fact
6739      constant.  */
6740   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6741   /* Restore the old settings.  */
6742   parser->integral_constant_expression_p
6743     = saved_integral_constant_expression_p;
6744   parser->allow_non_integral_constant_expression_p
6745     = saved_allow_non_integral_constant_expression_p;
6746   if (allow_non_constant_p)
6747     *non_constant_p = parser->non_integral_constant_expression_p;
6748   else if (parser->non_integral_constant_expression_p)
6749     expression = error_mark_node;
6750   parser->non_integral_constant_expression_p
6751     = saved_non_integral_constant_expression_p;
6752
6753   return expression;
6754 }
6755
6756 /* Parse __builtin_offsetof.
6757
6758    offsetof-expression:
6759      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6760
6761    offsetof-member-designator:
6762      id-expression
6763      | offsetof-member-designator "." id-expression
6764      | offsetof-member-designator "[" expression "]"
6765      | offsetof-member-designator "->" id-expression  */
6766
6767 static tree
6768 cp_parser_builtin_offsetof (cp_parser *parser)
6769 {
6770   int save_ice_p, save_non_ice_p;
6771   tree type, expr;
6772   cp_id_kind dummy;
6773   cp_token *token;
6774
6775   /* We're about to accept non-integral-constant things, but will
6776      definitely yield an integral constant expression.  Save and
6777      restore these values around our local parsing.  */
6778   save_ice_p = parser->integral_constant_expression_p;
6779   save_non_ice_p = parser->non_integral_constant_expression_p;
6780
6781   /* Consume the "__builtin_offsetof" token.  */
6782   cp_lexer_consume_token (parser->lexer);
6783   /* Consume the opening `('.  */
6784   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6785   /* Parse the type-id.  */
6786   type = cp_parser_type_id (parser);
6787   /* Look for the `,'.  */
6788   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6789   token = cp_lexer_peek_token (parser->lexer);
6790
6791   /* Build the (type *)null that begins the traditional offsetof macro.  */
6792   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6793                             tf_warning_or_error);
6794
6795   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6796   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6797                                                  true, &dummy, token->location);
6798   while (true)
6799     {
6800       token = cp_lexer_peek_token (parser->lexer);
6801       switch (token->type)
6802         {
6803         case CPP_OPEN_SQUARE:
6804           /* offsetof-member-designator "[" expression "]" */
6805           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6806           break;
6807
6808         case CPP_DEREF:
6809           /* offsetof-member-designator "->" identifier */
6810           expr = grok_array_decl (expr, integer_zero_node);
6811           /* FALLTHRU */
6812
6813         case CPP_DOT:
6814           /* offsetof-member-designator "." identifier */
6815           cp_lexer_consume_token (parser->lexer);
6816           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6817                                                          expr, true, &dummy,
6818                                                          token->location);
6819           break;
6820
6821         case CPP_CLOSE_PAREN:
6822           /* Consume the ")" token.  */
6823           cp_lexer_consume_token (parser->lexer);
6824           goto success;
6825
6826         default:
6827           /* Error.  We know the following require will fail, but
6828              that gives the proper error message.  */
6829           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6830           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6831           expr = error_mark_node;
6832           goto failure;
6833         }
6834     }
6835
6836  success:
6837   /* If we're processing a template, we can't finish the semantics yet.
6838      Otherwise we can fold the entire expression now.  */
6839   if (processing_template_decl)
6840     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6841   else
6842     expr = finish_offsetof (expr);
6843
6844  failure:
6845   parser->integral_constant_expression_p = save_ice_p;
6846   parser->non_integral_constant_expression_p = save_non_ice_p;
6847
6848   return expr;
6849 }
6850
6851 /* Parse a trait expression.  */
6852
6853 static tree
6854 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6855 {
6856   cp_trait_kind kind;
6857   tree type1, type2 = NULL_TREE;
6858   bool binary = false;
6859   cp_decl_specifier_seq decl_specs;
6860
6861   switch (keyword)
6862     {
6863     case RID_HAS_NOTHROW_ASSIGN:
6864       kind = CPTK_HAS_NOTHROW_ASSIGN;
6865       break;
6866     case RID_HAS_NOTHROW_CONSTRUCTOR:
6867       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6868       break;
6869     case RID_HAS_NOTHROW_COPY:
6870       kind = CPTK_HAS_NOTHROW_COPY;
6871       break;
6872     case RID_HAS_TRIVIAL_ASSIGN:
6873       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6874       break;
6875     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6876       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6877       break;
6878     case RID_HAS_TRIVIAL_COPY:
6879       kind = CPTK_HAS_TRIVIAL_COPY;
6880       break;
6881     case RID_HAS_TRIVIAL_DESTRUCTOR:
6882       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6883       break;
6884     case RID_HAS_VIRTUAL_DESTRUCTOR:
6885       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6886       break;
6887     case RID_IS_ABSTRACT:
6888       kind = CPTK_IS_ABSTRACT;
6889       break;
6890     case RID_IS_BASE_OF:
6891       kind = CPTK_IS_BASE_OF;
6892       binary = true;
6893       break;
6894     case RID_IS_CLASS:
6895       kind = CPTK_IS_CLASS;
6896       break;
6897     case RID_IS_CONVERTIBLE_TO:
6898       kind = CPTK_IS_CONVERTIBLE_TO;
6899       binary = true;
6900       break;
6901     case RID_IS_EMPTY:
6902       kind = CPTK_IS_EMPTY;
6903       break;
6904     case RID_IS_ENUM:
6905       kind = CPTK_IS_ENUM;
6906       break;
6907     case RID_IS_POD:
6908       kind = CPTK_IS_POD;
6909       break;
6910     case RID_IS_POLYMORPHIC:
6911       kind = CPTK_IS_POLYMORPHIC;
6912       break;
6913     case RID_IS_STD_LAYOUT:
6914       kind = CPTK_IS_STD_LAYOUT;
6915       break;
6916     case RID_IS_TRIVIAL:
6917       kind = CPTK_IS_TRIVIAL;
6918       break;
6919     case RID_IS_UNION:
6920       kind = CPTK_IS_UNION;
6921       break;
6922     default:
6923       gcc_unreachable ();
6924     }
6925
6926   /* Consume the token.  */
6927   cp_lexer_consume_token (parser->lexer);
6928
6929   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6930
6931   type1 = cp_parser_type_id (parser);
6932
6933   if (type1 == error_mark_node)
6934     return error_mark_node;
6935
6936   /* Build a trivial decl-specifier-seq.  */
6937   clear_decl_specs (&decl_specs);
6938   decl_specs.type = type1;
6939
6940   /* Call grokdeclarator to figure out what type this is.  */
6941   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6942                           /*initialized=*/0, /*attrlist=*/NULL);
6943
6944   if (binary)
6945     {
6946       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6947  
6948       type2 = cp_parser_type_id (parser);
6949
6950       if (type2 == error_mark_node)
6951         return error_mark_node;
6952
6953       /* Build a trivial decl-specifier-seq.  */
6954       clear_decl_specs (&decl_specs);
6955       decl_specs.type = type2;
6956
6957       /* Call grokdeclarator to figure out what type this is.  */
6958       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6959                               /*initialized=*/0, /*attrlist=*/NULL);
6960     }
6961
6962   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6963
6964   /* Complete the trait expression, which may mean either processing
6965      the trait expr now or saving it for template instantiation.  */
6966   return finish_trait_expr (kind, type1, type2);
6967 }
6968
6969 /* Lambdas that appear in variable initializer or default argument scope
6970    get that in their mangling, so we need to record it.  We might as well
6971    use the count for function and namespace scopes as well.  */
6972 static GTY(()) tree lambda_scope;
6973 static GTY(()) int lambda_count;
6974 typedef struct GTY(()) tree_int
6975 {
6976   tree t;
6977   int i;
6978 } tree_int;
6979 DEF_VEC_O(tree_int);
6980 DEF_VEC_ALLOC_O(tree_int,gc);
6981 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6982
6983 static void
6984 start_lambda_scope (tree decl)
6985 {
6986   tree_int ti;
6987   gcc_assert (decl);
6988   /* Once we're inside a function, we ignore other scopes and just push
6989      the function again so that popping works properly.  */
6990   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6991     decl = current_function_decl;
6992   ti.t = lambda_scope;
6993   ti.i = lambda_count;
6994   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6995   if (lambda_scope != decl)
6996     {
6997       /* Don't reset the count if we're still in the same function.  */
6998       lambda_scope = decl;
6999       lambda_count = 0;
7000     }
7001 }
7002
7003 static void
7004 record_lambda_scope (tree lambda)
7005 {
7006   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7007   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7008 }
7009
7010 static void
7011 finish_lambda_scope (void)
7012 {
7013   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7014   if (lambda_scope != p->t)
7015     {
7016       lambda_scope = p->t;
7017       lambda_count = p->i;
7018     }
7019   VEC_pop (tree_int, lambda_scope_stack);
7020 }
7021
7022 /* Parse a lambda expression.
7023
7024    lambda-expression:
7025      lambda-introducer lambda-declarator [opt] compound-statement
7026
7027    Returns a representation of the expression.  */
7028
7029 static tree
7030 cp_parser_lambda_expression (cp_parser* parser)
7031 {
7032   tree lambda_expr = build_lambda_expr ();
7033   tree type;
7034
7035   LAMBDA_EXPR_LOCATION (lambda_expr)
7036     = cp_lexer_peek_token (parser->lexer)->location;
7037
7038   /* We may be in the middle of deferred access check.  Disable
7039      it now.  */
7040   push_deferring_access_checks (dk_no_deferred);
7041
7042   type = begin_lambda_type (lambda_expr);
7043
7044   record_lambda_scope (lambda_expr);
7045
7046   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7047   determine_visibility (TYPE_NAME (type));
7048
7049   {
7050     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7051     unsigned int saved_num_template_parameter_lists
7052         = parser->num_template_parameter_lists;
7053
7054     parser->num_template_parameter_lists = 0;
7055
7056     cp_parser_lambda_introducer (parser, lambda_expr);
7057
7058     /* By virtue of defining a local class, a lambda expression has access to
7059        the private variables of enclosing classes.  */
7060
7061     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7062
7063     cp_parser_lambda_body (parser, lambda_expr);
7064
7065     /* The capture list was built up in reverse order; fix that now.  */
7066     {
7067       tree newlist = NULL_TREE;
7068       tree elt, next;
7069
7070       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7071            elt; elt = next)
7072         {
7073           tree field = TREE_PURPOSE (elt);
7074           char *buf;
7075
7076           next = TREE_CHAIN (elt);
7077           TREE_CHAIN (elt) = newlist;
7078           newlist = elt;
7079
7080           /* Also add __ to the beginning of the field name so that code
7081              outside the lambda body can't see the captured name.  We could
7082              just remove the name entirely, but this is more useful for
7083              debugging.  */
7084           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7085             /* The 'this' capture already starts with __.  */
7086             continue;
7087
7088           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7089           buf[1] = buf[0] = '_';
7090           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7091                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7092           DECL_NAME (field) = get_identifier (buf);
7093         }
7094       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7095     }
7096
7097     maybe_add_lambda_conv_op (type);
7098
7099     type = finish_struct (type, /*attributes=*/NULL_TREE);
7100
7101     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7102   }
7103
7104   pop_deferring_access_checks ();
7105
7106   return build_lambda_object (lambda_expr);
7107 }
7108
7109 /* Parse the beginning of a lambda expression.
7110
7111    lambda-introducer:
7112      [ lambda-capture [opt] ]
7113
7114    LAMBDA_EXPR is the current representation of the lambda expression.  */
7115
7116 static void
7117 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7118 {
7119   /* Need commas after the first capture.  */
7120   bool first = true;
7121
7122   /* Eat the leading `['.  */
7123   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7124
7125   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7126   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7127       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7128     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7129   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7130     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7131
7132   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7133     {
7134       cp_lexer_consume_token (parser->lexer);
7135       first = false;
7136     }
7137
7138   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7139     {
7140       cp_token* capture_token;
7141       tree capture_id;
7142       tree capture_init_expr;
7143       cp_id_kind idk = CP_ID_KIND_NONE;
7144       bool explicit_init_p = false;
7145
7146       enum capture_kind_type
7147       {
7148         BY_COPY,
7149         BY_REFERENCE
7150       };
7151       enum capture_kind_type capture_kind = BY_COPY;
7152
7153       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7154         {
7155           error ("expected end of capture-list");
7156           return;
7157         }
7158
7159       if (first)
7160         first = false;
7161       else
7162         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7163
7164       /* Possibly capture `this'.  */
7165       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7166         {
7167           cp_lexer_consume_token (parser->lexer);
7168           add_capture (lambda_expr,
7169                        /*id=*/get_identifier ("__this"),
7170                        /*initializer=*/finish_this_expr(),
7171                        /*by_reference_p=*/false,
7172                        explicit_init_p);
7173           continue;
7174         }
7175
7176       /* Remember whether we want to capture as a reference or not.  */
7177       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7178         {
7179           capture_kind = BY_REFERENCE;
7180           cp_lexer_consume_token (parser->lexer);
7181         }
7182
7183       /* Get the identifier.  */
7184       capture_token = cp_lexer_peek_token (parser->lexer);
7185       capture_id = cp_parser_identifier (parser);
7186
7187       if (capture_id == error_mark_node)
7188         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7189            delimiters, but I modified this to stop on unnested ']' as well.  It
7190            was already changed to stop on unnested '}', so the
7191            "closing_parenthesis" name is no more misleading with my change.  */
7192         {
7193           cp_parser_skip_to_closing_parenthesis (parser,
7194                                                  /*recovering=*/true,
7195                                                  /*or_comma=*/true,
7196                                                  /*consume_paren=*/true);
7197           break;
7198         }
7199
7200       /* Find the initializer for this capture.  */
7201       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7202         {
7203           /* An explicit expression exists.  */
7204           cp_lexer_consume_token (parser->lexer);
7205           pedwarn (input_location, OPT_pedantic,
7206                    "ISO C++ does not allow initializers "
7207                    "in lambda expression capture lists");
7208           capture_init_expr = cp_parser_assignment_expression (parser,
7209                                                                /*cast_p=*/true,
7210                                                                &idk);
7211           explicit_init_p = true;
7212         }
7213       else
7214         {
7215           const char* error_msg;
7216
7217           /* Turn the identifier into an id-expression.  */
7218           capture_init_expr
7219             = cp_parser_lookup_name
7220                 (parser,
7221                  capture_id,
7222                  none_type,
7223                  /*is_template=*/false,
7224                  /*is_namespace=*/false,
7225                  /*check_dependency=*/true,
7226                  /*ambiguous_decls=*/NULL,
7227                  capture_token->location);
7228
7229           capture_init_expr
7230             = finish_id_expression
7231                 (capture_id,
7232                  capture_init_expr,
7233                  parser->scope,
7234                  &idk,
7235                  /*integral_constant_expression_p=*/false,
7236                  /*allow_non_integral_constant_expression_p=*/false,
7237                  /*non_integral_constant_expression_p=*/NULL,
7238                  /*template_p=*/false,
7239                  /*done=*/true,
7240                  /*address_p=*/false,
7241                  /*template_arg_p=*/false,
7242                  &error_msg,
7243                  capture_token->location);
7244         }
7245
7246       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7247         capture_init_expr
7248           = unqualified_name_lookup_error (capture_init_expr);
7249
7250       add_capture (lambda_expr,
7251                    capture_id,
7252                    capture_init_expr,
7253                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7254                    explicit_init_p);
7255     }
7256
7257   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7258 }
7259
7260 /* Parse the (optional) middle of a lambda expression.
7261
7262    lambda-declarator:
7263      ( parameter-declaration-clause [opt] )
7264        attribute-specifier [opt]
7265        mutable [opt]
7266        exception-specification [opt]
7267        lambda-return-type-clause [opt]
7268
7269    LAMBDA_EXPR is the current representation of the lambda expression.  */
7270
7271 static void
7272 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7273 {
7274   /* 5.1.1.4 of the standard says:
7275        If a lambda-expression does not include a lambda-declarator, it is as if
7276        the lambda-declarator were ().
7277      This means an empty parameter list, no attributes, and no exception
7278      specification.  */
7279   tree param_list = void_list_node;
7280   tree attributes = NULL_TREE;
7281   tree exception_spec = NULL_TREE;
7282   tree t;
7283
7284   /* The lambda-declarator is optional, but must begin with an opening
7285      parenthesis if present.  */
7286   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7287     {
7288       cp_lexer_consume_token (parser->lexer);
7289
7290       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7291
7292       /* Parse parameters.  */
7293       param_list = cp_parser_parameter_declaration_clause (parser);
7294
7295       /* Default arguments shall not be specified in the
7296          parameter-declaration-clause of a lambda-declarator.  */
7297       for (t = param_list; t; t = TREE_CHAIN (t))
7298         if (TREE_PURPOSE (t))
7299           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7300                    "default argument specified for lambda parameter");
7301
7302       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7303
7304       attributes = cp_parser_attributes_opt (parser);
7305
7306       /* Parse optional `mutable' keyword.  */
7307       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7308         {
7309           cp_lexer_consume_token (parser->lexer);
7310           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7311         }
7312
7313       /* Parse optional exception specification.  */
7314       exception_spec = cp_parser_exception_specification_opt (parser);
7315
7316       /* Parse optional trailing return type.  */
7317       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7318         {
7319           cp_lexer_consume_token (parser->lexer);
7320           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7321         }
7322
7323       /* The function parameters must be in scope all the way until after the
7324          trailing-return-type in case of decltype.  */
7325       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7326         pop_binding (DECL_NAME (t), t);
7327
7328       leave_scope ();
7329     }
7330
7331   /* Create the function call operator.
7332
7333      Messing with declarators like this is no uglier than building up the
7334      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7335      other code.  */
7336   {
7337     cp_decl_specifier_seq return_type_specs;
7338     cp_declarator* declarator;
7339     tree fco;
7340     int quals;
7341     void *p;
7342
7343     clear_decl_specs (&return_type_specs);
7344     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7345       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7346     else
7347       /* Maybe we will deduce the return type later, but we can use void
7348          as a placeholder return type anyways.  */
7349       return_type_specs.type = void_type_node;
7350
7351     p = obstack_alloc (&declarator_obstack, 0);
7352
7353     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7354                                      sfk_none);
7355
7356     quals = TYPE_UNQUALIFIED;
7357     if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7358         && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7359       {
7360         /* A lambda with no captures has a static op() and a conversion op
7361            to function type.  */
7362         if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7363           error ("lambda expression with no captures declared mutable");
7364         return_type_specs.storage_class = sc_static;
7365       }
7366     else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7367       quals = TYPE_QUAL_CONST;
7368     declarator = make_call_declarator (declarator, param_list, quals,
7369                                        exception_spec,
7370                                        /*late_return_type=*/NULL_TREE);
7371
7372     fco = grokmethod (&return_type_specs,
7373                       declarator,
7374                       attributes);
7375     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7376     DECL_ARTIFICIAL (fco) = 1;
7377
7378     finish_member_declaration (fco);
7379
7380     obstack_free (&declarator_obstack, p);
7381   }
7382 }
7383
7384 /* Parse the body of a lambda expression, which is simply
7385
7386    compound-statement
7387
7388    but which requires special handling.
7389    LAMBDA_EXPR is the current representation of the lambda expression.  */
7390
7391 static void
7392 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7393 {
7394   bool nested = (current_function_decl != NULL_TREE);
7395   if (nested)
7396     push_function_context ();
7397
7398   /* Finish the function call operator
7399      - class_specifier
7400      + late_parsing_for_member
7401      + function_definition_after_declarator
7402      + ctor_initializer_opt_and_function_body  */
7403   {
7404     tree fco = lambda_function (lambda_expr);
7405     tree body;
7406     bool done = false;
7407
7408     /* Let the front end know that we are going to be defining this
7409        function.  */
7410     start_preparsed_function (fco,
7411                               NULL_TREE,
7412                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7413
7414     start_lambda_scope (fco);
7415     body = begin_function_body ();
7416
7417     /* 5.1.1.4 of the standard says:
7418          If a lambda-expression does not include a trailing-return-type, it
7419          is as if the trailing-return-type denotes the following type:
7420           * if the compound-statement is of the form
7421                { return attribute-specifier [opt] expression ; }
7422              the type of the returned expression after lvalue-to-rvalue
7423              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7424              (_conv.array_ 4.2), and function-to-pointer conversion
7425              (_conv.func_ 4.3);
7426           * otherwise, void.  */
7427
7428     /* In a lambda that has neither a lambda-return-type-clause
7429        nor a deducible form, errors should be reported for return statements
7430        in the body.  Since we used void as the placeholder return type, parsing
7431        the body as usual will give such desired behavior.  */
7432     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7433         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7434         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7435         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7436       {
7437         tree compound_stmt;
7438         tree expr = NULL_TREE;
7439         cp_id_kind idk = CP_ID_KIND_NONE;
7440
7441         /* Parse tentatively in case there's more after the initial return
7442            statement.  */
7443         cp_parser_parse_tentatively (parser);
7444
7445         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7446         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7447
7448         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7449
7450         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7451         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7452
7453         if (cp_parser_parse_definitely (parser))
7454           {
7455             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7456
7457             compound_stmt = begin_compound_stmt (0);
7458             /* Will get error here if type not deduced yet.  */
7459             finish_return_stmt (expr);
7460             finish_compound_stmt (compound_stmt);
7461
7462             done = true;
7463           }
7464       }
7465
7466     if (!done)
7467       {
7468         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7469           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7470         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7471            cp_parser_compound_stmt does not pass it.  */
7472         cp_parser_function_body (parser);
7473         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7474       }
7475
7476     finish_function_body (body);
7477     finish_lambda_scope ();
7478
7479     /* Finish the function and generate code for it if necessary.  */
7480     expand_or_defer_fn (finish_function (/*inline*/2));
7481   }
7482
7483   if (nested)
7484     pop_function_context();
7485 }
7486
7487 /* Statements [gram.stmt.stmt]  */
7488
7489 /* Parse a statement.
7490
7491    statement:
7492      labeled-statement
7493      expression-statement
7494      compound-statement
7495      selection-statement
7496      iteration-statement
7497      jump-statement
7498      declaration-statement
7499      try-block
7500
7501   IN_COMPOUND is true when the statement is nested inside a
7502   cp_parser_compound_statement; this matters for certain pragmas.
7503
7504   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7505   is a (possibly labeled) if statement which is not enclosed in braces
7506   and has an else clause.  This is used to implement -Wparentheses.  */
7507
7508 static void
7509 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7510                      bool in_compound, bool *if_p)
7511 {
7512   tree statement;
7513   cp_token *token;
7514   location_t statement_location;
7515
7516  restart:
7517   if (if_p != NULL)
7518     *if_p = false;
7519   /* There is no statement yet.  */
7520   statement = NULL_TREE;
7521   /* Peek at the next token.  */
7522   token = cp_lexer_peek_token (parser->lexer);
7523   /* Remember the location of the first token in the statement.  */
7524   statement_location = token->location;
7525   /* If this is a keyword, then that will often determine what kind of
7526      statement we have.  */
7527   if (token->type == CPP_KEYWORD)
7528     {
7529       enum rid keyword = token->keyword;
7530
7531       switch (keyword)
7532         {
7533         case RID_CASE:
7534         case RID_DEFAULT:
7535           /* Looks like a labeled-statement with a case label.
7536              Parse the label, and then use tail recursion to parse
7537              the statement.  */
7538           cp_parser_label_for_labeled_statement (parser);
7539           goto restart;
7540
7541         case RID_IF:
7542         case RID_SWITCH:
7543           statement = cp_parser_selection_statement (parser, if_p);
7544           break;
7545
7546         case RID_WHILE:
7547         case RID_DO:
7548         case RID_FOR:
7549           statement = cp_parser_iteration_statement (parser);
7550           break;
7551
7552         case RID_BREAK:
7553         case RID_CONTINUE:
7554         case RID_RETURN:
7555         case RID_GOTO:
7556           statement = cp_parser_jump_statement (parser);
7557           break;
7558
7559           /* Objective-C++ exception-handling constructs.  */
7560         case RID_AT_TRY:
7561         case RID_AT_CATCH:
7562         case RID_AT_FINALLY:
7563         case RID_AT_SYNCHRONIZED:
7564         case RID_AT_THROW:
7565           statement = cp_parser_objc_statement (parser);
7566           break;
7567
7568         case RID_TRY:
7569           statement = cp_parser_try_block (parser);
7570           break;
7571
7572         case RID_NAMESPACE:
7573           /* This must be a namespace alias definition.  */
7574           cp_parser_declaration_statement (parser);
7575           return;
7576           
7577         default:
7578           /* It might be a keyword like `int' that can start a
7579              declaration-statement.  */
7580           break;
7581         }
7582     }
7583   else if (token->type == CPP_NAME)
7584     {
7585       /* If the next token is a `:', then we are looking at a
7586          labeled-statement.  */
7587       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7588       if (token->type == CPP_COLON)
7589         {
7590           /* Looks like a labeled-statement with an ordinary label.
7591              Parse the label, and then use tail recursion to parse
7592              the statement.  */
7593           cp_parser_label_for_labeled_statement (parser);
7594           goto restart;
7595         }
7596     }
7597   /* Anything that starts with a `{' must be a compound-statement.  */
7598   else if (token->type == CPP_OPEN_BRACE)
7599     statement = cp_parser_compound_statement (parser, NULL, false);
7600   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7601      a statement all its own.  */
7602   else if (token->type == CPP_PRAGMA)
7603     {
7604       /* Only certain OpenMP pragmas are attached to statements, and thus
7605          are considered statements themselves.  All others are not.  In
7606          the context of a compound, accept the pragma as a "statement" and
7607          return so that we can check for a close brace.  Otherwise we
7608          require a real statement and must go back and read one.  */
7609       if (in_compound)
7610         cp_parser_pragma (parser, pragma_compound);
7611       else if (!cp_parser_pragma (parser, pragma_stmt))
7612         goto restart;
7613       return;
7614     }
7615   else if (token->type == CPP_EOF)
7616     {
7617       cp_parser_error (parser, "expected statement");
7618       return;
7619     }
7620
7621   /* Everything else must be a declaration-statement or an
7622      expression-statement.  Try for the declaration-statement
7623      first, unless we are looking at a `;', in which case we know that
7624      we have an expression-statement.  */
7625   if (!statement)
7626     {
7627       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7628         {
7629           cp_parser_parse_tentatively (parser);
7630           /* Try to parse the declaration-statement.  */
7631           cp_parser_declaration_statement (parser);
7632           /* If that worked, we're done.  */
7633           if (cp_parser_parse_definitely (parser))
7634             return;
7635         }
7636       /* Look for an expression-statement instead.  */
7637       statement = cp_parser_expression_statement (parser, in_statement_expr);
7638     }
7639
7640   /* Set the line number for the statement.  */
7641   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7642     SET_EXPR_LOCATION (statement, statement_location);
7643 }
7644
7645 /* Parse the label for a labeled-statement, i.e.
7646
7647    identifier :
7648    case constant-expression :
7649    default :
7650
7651    GNU Extension:
7652    case constant-expression ... constant-expression : statement
7653
7654    When a label is parsed without errors, the label is added to the
7655    parse tree by the finish_* functions, so this function doesn't
7656    have to return the label.  */
7657
7658 static void
7659 cp_parser_label_for_labeled_statement (cp_parser* parser)
7660 {
7661   cp_token *token;
7662   tree label = NULL_TREE;
7663
7664   /* The next token should be an identifier.  */
7665   token = cp_lexer_peek_token (parser->lexer);
7666   if (token->type != CPP_NAME
7667       && token->type != CPP_KEYWORD)
7668     {
7669       cp_parser_error (parser, "expected labeled-statement");
7670       return;
7671     }
7672
7673   switch (token->keyword)
7674     {
7675     case RID_CASE:
7676       {
7677         tree expr, expr_hi;
7678         cp_token *ellipsis;
7679
7680         /* Consume the `case' token.  */
7681         cp_lexer_consume_token (parser->lexer);
7682         /* Parse the constant-expression.  */
7683         expr = cp_parser_constant_expression (parser,
7684                                               /*allow_non_constant_p=*/false,
7685                                               NULL);
7686
7687         ellipsis = cp_lexer_peek_token (parser->lexer);
7688         if (ellipsis->type == CPP_ELLIPSIS)
7689           {
7690             /* Consume the `...' token.  */
7691             cp_lexer_consume_token (parser->lexer);
7692             expr_hi =
7693               cp_parser_constant_expression (parser,
7694                                              /*allow_non_constant_p=*/false,
7695                                              NULL);
7696             /* We don't need to emit warnings here, as the common code
7697                will do this for us.  */
7698           }
7699         else
7700           expr_hi = NULL_TREE;
7701
7702         if (parser->in_switch_statement_p)
7703           finish_case_label (token->location, expr, expr_hi);
7704         else
7705           error_at (token->location,
7706                     "case label %qE not within a switch statement",
7707                     expr);
7708       }
7709       break;
7710
7711     case RID_DEFAULT:
7712       /* Consume the `default' token.  */
7713       cp_lexer_consume_token (parser->lexer);
7714
7715       if (parser->in_switch_statement_p)
7716         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7717       else
7718         error_at (token->location, "case label not within a switch statement");
7719       break;
7720
7721     default:
7722       /* Anything else must be an ordinary label.  */
7723       label = finish_label_stmt (cp_parser_identifier (parser));
7724       break;
7725     }
7726
7727   /* Require the `:' token.  */
7728   cp_parser_require (parser, CPP_COLON, "%<:%>");
7729
7730   /* An ordinary label may optionally be followed by attributes.
7731      However, this is only permitted if the attributes are then
7732      followed by a semicolon.  This is because, for backward
7733      compatibility, when parsing
7734        lab: __attribute__ ((unused)) int i;
7735      we want the attribute to attach to "i", not "lab".  */
7736   if (label != NULL_TREE
7737       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7738     {
7739       tree attrs;
7740
7741       cp_parser_parse_tentatively (parser);
7742       attrs = cp_parser_attributes_opt (parser);
7743       if (attrs == NULL_TREE
7744           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7745         cp_parser_abort_tentative_parse (parser);
7746       else if (!cp_parser_parse_definitely (parser))
7747         ;
7748       else
7749         cplus_decl_attributes (&label, attrs, 0);
7750     }
7751 }
7752
7753 /* Parse an expression-statement.
7754
7755    expression-statement:
7756      expression [opt] ;
7757
7758    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7759    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7760    indicates whether this expression-statement is part of an
7761    expression statement.  */
7762
7763 static tree
7764 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7765 {
7766   tree statement = NULL_TREE;
7767   cp_token *token = cp_lexer_peek_token (parser->lexer);
7768
7769   /* If the next token is a ';', then there is no expression
7770      statement.  */
7771   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7772     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7773
7774   /* Give a helpful message for "A<T>::type t;"  */
7775   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7776       && !cp_parser_uncommitted_to_tentative_parse_p (parser)
7777       && TREE_CODE (statement) == SCOPE_REF)
7778     error_at (token->location, "need %<typename%> before %qE because "
7779               "%qT is a dependent scope",
7780               statement, TREE_OPERAND (statement, 0));
7781
7782   /* Consume the final `;'.  */
7783   cp_parser_consume_semicolon_at_end_of_statement (parser);
7784
7785   if (in_statement_expr
7786       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7787     /* This is the final expression statement of a statement
7788        expression.  */
7789     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7790   else if (statement)
7791     statement = finish_expr_stmt (statement);
7792   else
7793     finish_stmt ();
7794
7795   return statement;
7796 }
7797
7798 /* Parse a compound-statement.
7799
7800    compound-statement:
7801      { statement-seq [opt] }
7802
7803    GNU extension:
7804
7805    compound-statement:
7806      { label-declaration-seq [opt] statement-seq [opt] }
7807
7808    label-declaration-seq:
7809      label-declaration
7810      label-declaration-seq label-declaration
7811
7812    Returns a tree representing the statement.  */
7813
7814 static tree
7815 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7816                               bool in_try)
7817 {
7818   tree compound_stmt;
7819
7820   /* Consume the `{'.  */
7821   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7822     return error_mark_node;
7823   /* Begin the compound-statement.  */
7824   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7825   /* If the next keyword is `__label__' we have a label declaration.  */
7826   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7827     cp_parser_label_declaration (parser);
7828   /* Parse an (optional) statement-seq.  */
7829   cp_parser_statement_seq_opt (parser, in_statement_expr);
7830   /* Finish the compound-statement.  */
7831   finish_compound_stmt (compound_stmt);
7832   /* Consume the `}'.  */
7833   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7834
7835   return compound_stmt;
7836 }
7837
7838 /* Parse an (optional) statement-seq.
7839
7840    statement-seq:
7841      statement
7842      statement-seq [opt] statement  */
7843
7844 static void
7845 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7846 {
7847   /* Scan statements until there aren't any more.  */
7848   while (true)
7849     {
7850       cp_token *token = cp_lexer_peek_token (parser->lexer);
7851
7852       /* If we're looking at a `}', then we've run out of statements.  */
7853       if (token->type == CPP_CLOSE_BRACE
7854           || token->type == CPP_EOF
7855           || token->type == CPP_PRAGMA_EOL)
7856         break;
7857       
7858       /* If we are in a compound statement and find 'else' then
7859          something went wrong.  */
7860       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7861         {
7862           if (parser->in_statement & IN_IF_STMT) 
7863             break;
7864           else
7865             {
7866               token = cp_lexer_consume_token (parser->lexer);
7867               error_at (token->location, "%<else%> without a previous %<if%>");
7868             }
7869         }
7870
7871       /* Parse the statement.  */
7872       cp_parser_statement (parser, in_statement_expr, true, NULL);
7873     }
7874 }
7875
7876 /* Parse a selection-statement.
7877
7878    selection-statement:
7879      if ( condition ) statement
7880      if ( condition ) statement else statement
7881      switch ( condition ) statement
7882
7883    Returns the new IF_STMT or SWITCH_STMT.
7884
7885    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7886    is a (possibly labeled) if statement which is not enclosed in
7887    braces and has an else clause.  This is used to implement
7888    -Wparentheses.  */
7889
7890 static tree
7891 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7892 {
7893   cp_token *token;
7894   enum rid keyword;
7895
7896   if (if_p != NULL)
7897     *if_p = false;
7898
7899   /* Peek at the next token.  */
7900   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7901
7902   /* See what kind of keyword it is.  */
7903   keyword = token->keyword;
7904   switch (keyword)
7905     {
7906     case RID_IF:
7907     case RID_SWITCH:
7908       {
7909         tree statement;
7910         tree condition;
7911
7912         /* Look for the `('.  */
7913         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7914           {
7915             cp_parser_skip_to_end_of_statement (parser);
7916             return error_mark_node;
7917           }
7918
7919         /* Begin the selection-statement.  */
7920         if (keyword == RID_IF)
7921           statement = begin_if_stmt ();
7922         else
7923           statement = begin_switch_stmt ();
7924
7925         /* Parse the condition.  */
7926         condition = cp_parser_condition (parser);
7927         /* Look for the `)'.  */
7928         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7929           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7930                                                  /*consume_paren=*/true);
7931
7932         if (keyword == RID_IF)
7933           {
7934             bool nested_if;
7935             unsigned char in_statement;
7936
7937             /* Add the condition.  */
7938             finish_if_stmt_cond (condition, statement);
7939
7940             /* Parse the then-clause.  */
7941             in_statement = parser->in_statement;
7942             parser->in_statement |= IN_IF_STMT;
7943             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7944               {
7945                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7946                 add_stmt (build_empty_stmt (loc));
7947                 cp_lexer_consume_token (parser->lexer);
7948                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7949                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7950                               "empty body in an %<if%> statement");
7951                 nested_if = false;
7952               }
7953             else
7954               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7955             parser->in_statement = in_statement;
7956
7957             finish_then_clause (statement);
7958
7959             /* If the next token is `else', parse the else-clause.  */
7960             if (cp_lexer_next_token_is_keyword (parser->lexer,
7961                                                 RID_ELSE))
7962               {
7963                 /* Consume the `else' keyword.  */
7964                 cp_lexer_consume_token (parser->lexer);
7965                 begin_else_clause (statement);
7966                 /* Parse the else-clause.  */
7967                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7968                   {
7969                     location_t loc;
7970                     loc = cp_lexer_peek_token (parser->lexer)->location;
7971                     warning_at (loc,
7972                                 OPT_Wempty_body, "suggest braces around "
7973                                 "empty body in an %<else%> statement");
7974                     add_stmt (build_empty_stmt (loc));
7975                     cp_lexer_consume_token (parser->lexer);
7976                   }
7977                 else
7978                   cp_parser_implicitly_scoped_statement (parser, NULL);
7979
7980                 finish_else_clause (statement);
7981
7982                 /* If we are currently parsing a then-clause, then
7983                    IF_P will not be NULL.  We set it to true to
7984                    indicate that this if statement has an else clause.
7985                    This may trigger the Wparentheses warning below
7986                    when we get back up to the parent if statement.  */
7987                 if (if_p != NULL)
7988                   *if_p = true;
7989               }
7990             else
7991               {
7992                 /* This if statement does not have an else clause.  If
7993                    NESTED_IF is true, then the then-clause is an if
7994                    statement which does have an else clause.  We warn
7995                    about the potential ambiguity.  */
7996                 if (nested_if)
7997                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7998                               "suggest explicit braces to avoid ambiguous"
7999                               " %<else%>");
8000               }
8001
8002             /* Now we're all done with the if-statement.  */
8003             finish_if_stmt (statement);
8004           }
8005         else
8006           {
8007             bool in_switch_statement_p;
8008             unsigned char in_statement;
8009
8010             /* Add the condition.  */
8011             finish_switch_cond (condition, statement);
8012
8013             /* Parse the body of the switch-statement.  */
8014             in_switch_statement_p = parser->in_switch_statement_p;
8015             in_statement = parser->in_statement;
8016             parser->in_switch_statement_p = true;
8017             parser->in_statement |= IN_SWITCH_STMT;
8018             cp_parser_implicitly_scoped_statement (parser, NULL);
8019             parser->in_switch_statement_p = in_switch_statement_p;
8020             parser->in_statement = in_statement;
8021
8022             /* Now we're all done with the switch-statement.  */
8023             finish_switch_stmt (statement);
8024           }
8025
8026         return statement;
8027       }
8028       break;
8029
8030     default:
8031       cp_parser_error (parser, "expected selection-statement");
8032       return error_mark_node;
8033     }
8034 }
8035
8036 /* Parse a condition.
8037
8038    condition:
8039      expression
8040      type-specifier-seq declarator = initializer-clause
8041      type-specifier-seq declarator braced-init-list
8042
8043    GNU Extension:
8044
8045    condition:
8046      type-specifier-seq declarator asm-specification [opt]
8047        attributes [opt] = assignment-expression
8048
8049    Returns the expression that should be tested.  */
8050
8051 static tree
8052 cp_parser_condition (cp_parser* parser)
8053 {
8054   cp_decl_specifier_seq type_specifiers;
8055   const char *saved_message;
8056
8057   /* Try the declaration first.  */
8058   cp_parser_parse_tentatively (parser);
8059   /* New types are not allowed in the type-specifier-seq for a
8060      condition.  */
8061   saved_message = parser->type_definition_forbidden_message;
8062   parser->type_definition_forbidden_message
8063     = "types may not be defined in conditions";
8064   /* Parse the type-specifier-seq.  */
8065   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8066                                 /*is_trailing_return=*/false,
8067                                 &type_specifiers);
8068   /* Restore the saved message.  */
8069   parser->type_definition_forbidden_message = saved_message;
8070   /* If all is well, we might be looking at a declaration.  */
8071   if (!cp_parser_error_occurred (parser))
8072     {
8073       tree decl;
8074       tree asm_specification;
8075       tree attributes;
8076       cp_declarator *declarator;
8077       tree initializer = NULL_TREE;
8078
8079       /* Parse the declarator.  */
8080       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8081                                          /*ctor_dtor_or_conv_p=*/NULL,
8082                                          /*parenthesized_p=*/NULL,
8083                                          /*member_p=*/false);
8084       /* Parse the attributes.  */
8085       attributes = cp_parser_attributes_opt (parser);
8086       /* Parse the asm-specification.  */
8087       asm_specification = cp_parser_asm_specification_opt (parser);
8088       /* If the next token is not an `=' or '{', then we might still be
8089          looking at an expression.  For example:
8090
8091            if (A(a).x)
8092
8093          looks like a decl-specifier-seq and a declarator -- but then
8094          there is no `=', so this is an expression.  */
8095       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8096           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8097         cp_parser_simulate_error (parser);
8098         
8099       /* If we did see an `=' or '{', then we are looking at a declaration
8100          for sure.  */
8101       if (cp_parser_parse_definitely (parser))
8102         {
8103           tree pushed_scope;
8104           bool non_constant_p;
8105           bool flags = LOOKUP_ONLYCONVERTING;
8106
8107           /* Create the declaration.  */
8108           decl = start_decl (declarator, &type_specifiers,
8109                              /*initialized_p=*/true,
8110                              attributes, /*prefix_attributes=*/NULL_TREE,
8111                              &pushed_scope);
8112
8113           /* Parse the initializer.  */
8114           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8115             {
8116               initializer = cp_parser_braced_list (parser, &non_constant_p);
8117               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8118               flags = 0;
8119             }
8120           else
8121             {
8122               /* Consume the `='.  */
8123               cp_parser_require (parser, CPP_EQ, "%<=%>");
8124               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8125             }
8126           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8127             maybe_warn_cpp0x ("extended initializer lists");
8128
8129           if (!non_constant_p)
8130             initializer = fold_non_dependent_expr (initializer);
8131
8132           /* Process the initializer.  */
8133           cp_finish_decl (decl,
8134                           initializer, !non_constant_p,
8135                           asm_specification,
8136                           flags);
8137
8138           if (pushed_scope)
8139             pop_scope (pushed_scope);
8140
8141           return convert_from_reference (decl);
8142         }
8143     }
8144   /* If we didn't even get past the declarator successfully, we are
8145      definitely not looking at a declaration.  */
8146   else
8147     cp_parser_abort_tentative_parse (parser);
8148
8149   /* Otherwise, we are looking at an expression.  */
8150   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8151 }
8152
8153 /* Parse an iteration-statement.
8154
8155    iteration-statement:
8156      while ( condition ) statement
8157      do statement while ( expression ) ;
8158      for ( for-init-statement condition [opt] ; expression [opt] )
8159        statement
8160
8161    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8162
8163 static tree
8164 cp_parser_iteration_statement (cp_parser* parser)
8165 {
8166   cp_token *token;
8167   enum rid keyword;
8168   tree statement;
8169   unsigned char in_statement;
8170
8171   /* Peek at the next token.  */
8172   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8173   if (!token)
8174     return error_mark_node;
8175
8176   /* Remember whether or not we are already within an iteration
8177      statement.  */
8178   in_statement = parser->in_statement;
8179
8180   /* See what kind of keyword it is.  */
8181   keyword = token->keyword;
8182   switch (keyword)
8183     {
8184     case RID_WHILE:
8185       {
8186         tree condition;
8187
8188         /* Begin the while-statement.  */
8189         statement = begin_while_stmt ();
8190         /* Look for the `('.  */
8191         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8192         /* Parse the condition.  */
8193         condition = cp_parser_condition (parser);
8194         finish_while_stmt_cond (condition, statement);
8195         /* Look for the `)'.  */
8196         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8197         /* Parse the dependent statement.  */
8198         parser->in_statement = IN_ITERATION_STMT;
8199         cp_parser_already_scoped_statement (parser);
8200         parser->in_statement = in_statement;
8201         /* We're done with the while-statement.  */
8202         finish_while_stmt (statement);
8203       }
8204       break;
8205
8206     case RID_DO:
8207       {
8208         tree expression;
8209
8210         /* Begin the do-statement.  */
8211         statement = begin_do_stmt ();
8212         /* Parse the body of the do-statement.  */
8213         parser->in_statement = IN_ITERATION_STMT;
8214         cp_parser_implicitly_scoped_statement (parser, NULL);
8215         parser->in_statement = in_statement;
8216         finish_do_body (statement);
8217         /* Look for the `while' keyword.  */
8218         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8219         /* Look for the `('.  */
8220         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8221         /* Parse the expression.  */
8222         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8223         /* We're done with the do-statement.  */
8224         finish_do_stmt (expression, statement);
8225         /* Look for the `)'.  */
8226         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8227         /* Look for the `;'.  */
8228         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8229       }
8230       break;
8231
8232     case RID_FOR:
8233       {
8234         tree condition = NULL_TREE;
8235         tree expression = NULL_TREE;
8236
8237         /* Begin the for-statement.  */
8238         statement = begin_for_stmt ();
8239         /* Look for the `('.  */
8240         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8241         /* Parse the initialization.  */
8242         cp_parser_for_init_statement (parser);
8243         finish_for_init_stmt (statement);
8244
8245         /* If there's a condition, process it.  */
8246         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8247           condition = cp_parser_condition (parser);
8248         finish_for_cond (condition, statement);
8249         /* Look for the `;'.  */
8250         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8251
8252         /* If there's an expression, process it.  */
8253         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8254           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8255         finish_for_expr (expression, statement);
8256         /* Look for the `)'.  */
8257         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8258
8259         /* Parse the body of the for-statement.  */
8260         parser->in_statement = IN_ITERATION_STMT;
8261         cp_parser_already_scoped_statement (parser);
8262         parser->in_statement = in_statement;
8263
8264         /* We're done with the for-statement.  */
8265         finish_for_stmt (statement);
8266       }
8267       break;
8268
8269     default:
8270       cp_parser_error (parser, "expected iteration-statement");
8271       statement = error_mark_node;
8272       break;
8273     }
8274
8275   return statement;
8276 }
8277
8278 /* Parse a for-init-statement.
8279
8280    for-init-statement:
8281      expression-statement
8282      simple-declaration  */
8283
8284 static void
8285 cp_parser_for_init_statement (cp_parser* parser)
8286 {
8287   /* If the next token is a `;', then we have an empty
8288      expression-statement.  Grammatically, this is also a
8289      simple-declaration, but an invalid one, because it does not
8290      declare anything.  Therefore, if we did not handle this case
8291      specially, we would issue an error message about an invalid
8292      declaration.  */
8293   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8294     {
8295       /* We're going to speculatively look for a declaration, falling back
8296          to an expression, if necessary.  */
8297       cp_parser_parse_tentatively (parser);
8298       /* Parse the declaration.  */
8299       cp_parser_simple_declaration (parser,
8300                                     /*function_definition_allowed_p=*/false);
8301       /* If the tentative parse failed, then we shall need to look for an
8302          expression-statement.  */
8303       if (cp_parser_parse_definitely (parser))
8304         return;
8305     }
8306
8307   cp_parser_expression_statement (parser, false);
8308 }
8309
8310 /* Parse a jump-statement.
8311
8312    jump-statement:
8313      break ;
8314      continue ;
8315      return expression [opt] ;
8316      return braced-init-list ;
8317      goto identifier ;
8318
8319    GNU extension:
8320
8321    jump-statement:
8322      goto * expression ;
8323
8324    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8325
8326 static tree
8327 cp_parser_jump_statement (cp_parser* parser)
8328 {
8329   tree statement = error_mark_node;
8330   cp_token *token;
8331   enum rid keyword;
8332   unsigned char in_statement;
8333
8334   /* Peek at the next token.  */
8335   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8336   if (!token)
8337     return error_mark_node;
8338
8339   /* See what kind of keyword it is.  */
8340   keyword = token->keyword;
8341   switch (keyword)
8342     {
8343     case RID_BREAK:
8344       in_statement = parser->in_statement & ~IN_IF_STMT;      
8345       switch (in_statement)
8346         {
8347         case 0:
8348           error_at (token->location, "break statement not within loop or switch");
8349           break;
8350         default:
8351           gcc_assert ((in_statement & IN_SWITCH_STMT)
8352                       || in_statement == IN_ITERATION_STMT);
8353           statement = finish_break_stmt ();
8354           break;
8355         case IN_OMP_BLOCK:
8356           error_at (token->location, "invalid exit from OpenMP structured block");
8357           break;
8358         case IN_OMP_FOR:
8359           error_at (token->location, "break statement used with OpenMP for loop");
8360           break;
8361         }
8362       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8363       break;
8364
8365     case RID_CONTINUE:
8366       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8367         {
8368         case 0:
8369           error_at (token->location, "continue statement not within a loop");
8370           break;
8371         case IN_ITERATION_STMT:
8372         case IN_OMP_FOR:
8373           statement = finish_continue_stmt ();
8374           break;
8375         case IN_OMP_BLOCK:
8376           error_at (token->location, "invalid exit from OpenMP structured block");
8377           break;
8378         default:
8379           gcc_unreachable ();
8380         }
8381       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8382       break;
8383
8384     case RID_RETURN:
8385       {
8386         tree expr;
8387         bool expr_non_constant_p;
8388
8389         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8390           {
8391             maybe_warn_cpp0x ("extended initializer lists");
8392             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8393           }
8394         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8395           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8396         else
8397           /* If the next token is a `;', then there is no
8398              expression.  */
8399           expr = NULL_TREE;
8400         /* Build the return-statement.  */
8401         statement = finish_return_stmt (expr);
8402         /* Look for the final `;'.  */
8403         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8404       }
8405       break;
8406
8407     case RID_GOTO:
8408       /* Create the goto-statement.  */
8409       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8410         {
8411           /* Issue a warning about this use of a GNU extension.  */
8412           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8413           /* Consume the '*' token.  */
8414           cp_lexer_consume_token (parser->lexer);
8415           /* Parse the dependent expression.  */
8416           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8417         }
8418       else
8419         finish_goto_stmt (cp_parser_identifier (parser));
8420       /* Look for the final `;'.  */
8421       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8422       break;
8423
8424     default:
8425       cp_parser_error (parser, "expected jump-statement");
8426       break;
8427     }
8428
8429   return statement;
8430 }
8431
8432 /* Parse a declaration-statement.
8433
8434    declaration-statement:
8435      block-declaration  */
8436
8437 static void
8438 cp_parser_declaration_statement (cp_parser* parser)
8439 {
8440   void *p;
8441
8442   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8443   p = obstack_alloc (&declarator_obstack, 0);
8444
8445  /* Parse the block-declaration.  */
8446   cp_parser_block_declaration (parser, /*statement_p=*/true);
8447
8448   /* Free any declarators allocated.  */
8449   obstack_free (&declarator_obstack, p);
8450
8451   /* Finish off the statement.  */
8452   finish_stmt ();
8453 }
8454
8455 /* Some dependent statements (like `if (cond) statement'), are
8456    implicitly in their own scope.  In other words, if the statement is
8457    a single statement (as opposed to a compound-statement), it is
8458    none-the-less treated as if it were enclosed in braces.  Any
8459    declarations appearing in the dependent statement are out of scope
8460    after control passes that point.  This function parses a statement,
8461    but ensures that is in its own scope, even if it is not a
8462    compound-statement.
8463
8464    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8465    is a (possibly labeled) if statement which is not enclosed in
8466    braces and has an else clause.  This is used to implement
8467    -Wparentheses.
8468
8469    Returns the new statement.  */
8470
8471 static tree
8472 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8473 {
8474   tree statement;
8475
8476   if (if_p != NULL)
8477     *if_p = false;
8478
8479   /* Mark if () ; with a special NOP_EXPR.  */
8480   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8481     {
8482       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8483       cp_lexer_consume_token (parser->lexer);
8484       statement = add_stmt (build_empty_stmt (loc));
8485     }
8486   /* if a compound is opened, we simply parse the statement directly.  */
8487   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8488     statement = cp_parser_compound_statement (parser, NULL, false);
8489   /* If the token is not a `{', then we must take special action.  */
8490   else
8491     {
8492       /* Create a compound-statement.  */
8493       statement = begin_compound_stmt (0);
8494       /* Parse the dependent-statement.  */
8495       cp_parser_statement (parser, NULL_TREE, false, if_p);
8496       /* Finish the dummy compound-statement.  */
8497       finish_compound_stmt (statement);
8498     }
8499
8500   /* Return the statement.  */
8501   return statement;
8502 }
8503
8504 /* For some dependent statements (like `while (cond) statement'), we
8505    have already created a scope.  Therefore, even if the dependent
8506    statement is a compound-statement, we do not want to create another
8507    scope.  */
8508
8509 static void
8510 cp_parser_already_scoped_statement (cp_parser* parser)
8511 {
8512   /* If the token is a `{', then we must take special action.  */
8513   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8514     cp_parser_statement (parser, NULL_TREE, false, NULL);
8515   else
8516     {
8517       /* Avoid calling cp_parser_compound_statement, so that we
8518          don't create a new scope.  Do everything else by hand.  */
8519       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8520       /* If the next keyword is `__label__' we have a label declaration.  */
8521       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8522         cp_parser_label_declaration (parser);
8523       /* Parse an (optional) statement-seq.  */
8524       cp_parser_statement_seq_opt (parser, NULL_TREE);
8525       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8526     }
8527 }
8528
8529 /* Declarations [gram.dcl.dcl] */
8530
8531 /* Parse an optional declaration-sequence.
8532
8533    declaration-seq:
8534      declaration
8535      declaration-seq declaration  */
8536
8537 static void
8538 cp_parser_declaration_seq_opt (cp_parser* parser)
8539 {
8540   while (true)
8541     {
8542       cp_token *token;
8543
8544       token = cp_lexer_peek_token (parser->lexer);
8545
8546       if (token->type == CPP_CLOSE_BRACE
8547           || token->type == CPP_EOF
8548           || token->type == CPP_PRAGMA_EOL)
8549         break;
8550
8551       if (token->type == CPP_SEMICOLON)
8552         {
8553           /* A declaration consisting of a single semicolon is
8554              invalid.  Allow it unless we're being pedantic.  */
8555           cp_lexer_consume_token (parser->lexer);
8556           if (!in_system_header)
8557             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8558           continue;
8559         }
8560
8561       /* If we're entering or exiting a region that's implicitly
8562          extern "C", modify the lang context appropriately.  */
8563       if (!parser->implicit_extern_c && token->implicit_extern_c)
8564         {
8565           push_lang_context (lang_name_c);
8566           parser->implicit_extern_c = true;
8567         }
8568       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8569         {
8570           pop_lang_context ();
8571           parser->implicit_extern_c = false;
8572         }
8573
8574       if (token->type == CPP_PRAGMA)
8575         {
8576           /* A top-level declaration can consist solely of a #pragma.
8577              A nested declaration cannot, so this is done here and not
8578              in cp_parser_declaration.  (A #pragma at block scope is
8579              handled in cp_parser_statement.)  */
8580           cp_parser_pragma (parser, pragma_external);
8581           continue;
8582         }
8583
8584       /* Parse the declaration itself.  */
8585       cp_parser_declaration (parser);
8586     }
8587 }
8588
8589 /* Parse a declaration.
8590
8591    declaration:
8592      block-declaration
8593      function-definition
8594      template-declaration
8595      explicit-instantiation
8596      explicit-specialization
8597      linkage-specification
8598      namespace-definition
8599
8600    GNU extension:
8601
8602    declaration:
8603       __extension__ declaration */
8604
8605 static void
8606 cp_parser_declaration (cp_parser* parser)
8607 {
8608   cp_token token1;
8609   cp_token token2;
8610   int saved_pedantic;
8611   void *p;
8612
8613   /* Check for the `__extension__' keyword.  */
8614   if (cp_parser_extension_opt (parser, &saved_pedantic))
8615     {
8616       /* Parse the qualified declaration.  */
8617       cp_parser_declaration (parser);
8618       /* Restore the PEDANTIC flag.  */
8619       pedantic = saved_pedantic;
8620
8621       return;
8622     }
8623
8624   /* Try to figure out what kind of declaration is present.  */
8625   token1 = *cp_lexer_peek_token (parser->lexer);
8626
8627   if (token1.type != CPP_EOF)
8628     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8629   else
8630     {
8631       token2.type = CPP_EOF;
8632       token2.keyword = RID_MAX;
8633     }
8634
8635   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8636   p = obstack_alloc (&declarator_obstack, 0);
8637
8638   /* If the next token is `extern' and the following token is a string
8639      literal, then we have a linkage specification.  */
8640   if (token1.keyword == RID_EXTERN
8641       && cp_parser_is_string_literal (&token2))
8642     cp_parser_linkage_specification (parser);
8643   /* If the next token is `template', then we have either a template
8644      declaration, an explicit instantiation, or an explicit
8645      specialization.  */
8646   else if (token1.keyword == RID_TEMPLATE)
8647     {
8648       /* `template <>' indicates a template specialization.  */
8649       if (token2.type == CPP_LESS
8650           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8651         cp_parser_explicit_specialization (parser);
8652       /* `template <' indicates a template declaration.  */
8653       else if (token2.type == CPP_LESS)
8654         cp_parser_template_declaration (parser, /*member_p=*/false);
8655       /* Anything else must be an explicit instantiation.  */
8656       else
8657         cp_parser_explicit_instantiation (parser);
8658     }
8659   /* If the next token is `export', then we have a template
8660      declaration.  */
8661   else if (token1.keyword == RID_EXPORT)
8662     cp_parser_template_declaration (parser, /*member_p=*/false);
8663   /* If the next token is `extern', 'static' or 'inline' and the one
8664      after that is `template', we have a GNU extended explicit
8665      instantiation directive.  */
8666   else if (cp_parser_allow_gnu_extensions_p (parser)
8667            && (token1.keyword == RID_EXTERN
8668                || token1.keyword == RID_STATIC
8669                || token1.keyword == RID_INLINE)
8670            && token2.keyword == RID_TEMPLATE)
8671     cp_parser_explicit_instantiation (parser);
8672   /* If the next token is `namespace', check for a named or unnamed
8673      namespace definition.  */
8674   else if (token1.keyword == RID_NAMESPACE
8675            && (/* A named namespace definition.  */
8676                (token2.type == CPP_NAME
8677                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8678                     != CPP_EQ))
8679                /* An unnamed namespace definition.  */
8680                || token2.type == CPP_OPEN_BRACE
8681                || token2.keyword == RID_ATTRIBUTE))
8682     cp_parser_namespace_definition (parser);
8683   /* An inline (associated) namespace definition.  */
8684   else if (token1.keyword == RID_INLINE
8685            && token2.keyword == RID_NAMESPACE)
8686     cp_parser_namespace_definition (parser);
8687   /* Objective-C++ declaration/definition.  */
8688   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8689     cp_parser_objc_declaration (parser);
8690   /* We must have either a block declaration or a function
8691      definition.  */
8692   else
8693     /* Try to parse a block-declaration, or a function-definition.  */
8694     cp_parser_block_declaration (parser, /*statement_p=*/false);
8695
8696   /* Free any declarators allocated.  */
8697   obstack_free (&declarator_obstack, p);
8698 }
8699
8700 /* Parse a block-declaration.
8701
8702    block-declaration:
8703      simple-declaration
8704      asm-definition
8705      namespace-alias-definition
8706      using-declaration
8707      using-directive
8708
8709    GNU Extension:
8710
8711    block-declaration:
8712      __extension__ block-declaration
8713
8714    C++0x Extension:
8715
8716    block-declaration:
8717      static_assert-declaration
8718
8719    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8720    part of a declaration-statement.  */
8721
8722 static void
8723 cp_parser_block_declaration (cp_parser *parser,
8724                              bool      statement_p)
8725 {
8726   cp_token *token1;
8727   int saved_pedantic;
8728
8729   /* Check for the `__extension__' keyword.  */
8730   if (cp_parser_extension_opt (parser, &saved_pedantic))
8731     {
8732       /* Parse the qualified declaration.  */
8733       cp_parser_block_declaration (parser, statement_p);
8734       /* Restore the PEDANTIC flag.  */
8735       pedantic = saved_pedantic;
8736
8737       return;
8738     }
8739
8740   /* Peek at the next token to figure out which kind of declaration is
8741      present.  */
8742   token1 = cp_lexer_peek_token (parser->lexer);
8743
8744   /* If the next keyword is `asm', we have an asm-definition.  */
8745   if (token1->keyword == RID_ASM)
8746     {
8747       if (statement_p)
8748         cp_parser_commit_to_tentative_parse (parser);
8749       cp_parser_asm_definition (parser);
8750     }
8751   /* If the next keyword is `namespace', we have a
8752      namespace-alias-definition.  */
8753   else if (token1->keyword == RID_NAMESPACE)
8754     cp_parser_namespace_alias_definition (parser);
8755   /* If the next keyword is `using', we have either a
8756      using-declaration or a using-directive.  */
8757   else if (token1->keyword == RID_USING)
8758     {
8759       cp_token *token2;
8760
8761       if (statement_p)
8762         cp_parser_commit_to_tentative_parse (parser);
8763       /* If the token after `using' is `namespace', then we have a
8764          using-directive.  */
8765       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8766       if (token2->keyword == RID_NAMESPACE)
8767         cp_parser_using_directive (parser);
8768       /* Otherwise, it's a using-declaration.  */
8769       else
8770         cp_parser_using_declaration (parser,
8771                                      /*access_declaration_p=*/false);
8772     }
8773   /* If the next keyword is `__label__' we have a misplaced label
8774      declaration.  */
8775   else if (token1->keyword == RID_LABEL)
8776     {
8777       cp_lexer_consume_token (parser->lexer);
8778       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8779       cp_parser_skip_to_end_of_statement (parser);
8780       /* If the next token is now a `;', consume it.  */
8781       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8782         cp_lexer_consume_token (parser->lexer);
8783     }
8784   /* If the next token is `static_assert' we have a static assertion.  */
8785   else if (token1->keyword == RID_STATIC_ASSERT)
8786     cp_parser_static_assert (parser, /*member_p=*/false);
8787   /* Anything else must be a simple-declaration.  */
8788   else
8789     cp_parser_simple_declaration (parser, !statement_p);
8790 }
8791
8792 /* Parse a simple-declaration.
8793
8794    simple-declaration:
8795      decl-specifier-seq [opt] init-declarator-list [opt] ;
8796
8797    init-declarator-list:
8798      init-declarator
8799      init-declarator-list , init-declarator
8800
8801    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8802    function-definition as a simple-declaration.  */
8803
8804 static void
8805 cp_parser_simple_declaration (cp_parser* parser,
8806                               bool function_definition_allowed_p)
8807 {
8808   cp_decl_specifier_seq decl_specifiers;
8809   int declares_class_or_enum;
8810   bool saw_declarator;
8811
8812   /* Defer access checks until we know what is being declared; the
8813      checks for names appearing in the decl-specifier-seq should be
8814      done as if we were in the scope of the thing being declared.  */
8815   push_deferring_access_checks (dk_deferred);
8816
8817   /* Parse the decl-specifier-seq.  We have to keep track of whether
8818      or not the decl-specifier-seq declares a named class or
8819      enumeration type, since that is the only case in which the
8820      init-declarator-list is allowed to be empty.
8821
8822      [dcl.dcl]
8823
8824      In a simple-declaration, the optional init-declarator-list can be
8825      omitted only when declaring a class or enumeration, that is when
8826      the decl-specifier-seq contains either a class-specifier, an
8827      elaborated-type-specifier, or an enum-specifier.  */
8828   cp_parser_decl_specifier_seq (parser,
8829                                 CP_PARSER_FLAGS_OPTIONAL,
8830                                 &decl_specifiers,
8831                                 &declares_class_or_enum);
8832   /* We no longer need to defer access checks.  */
8833   stop_deferring_access_checks ();
8834
8835   /* In a block scope, a valid declaration must always have a
8836      decl-specifier-seq.  By not trying to parse declarators, we can
8837      resolve the declaration/expression ambiguity more quickly.  */
8838   if (!function_definition_allowed_p
8839       && !decl_specifiers.any_specifiers_p)
8840     {
8841       cp_parser_error (parser, "expected declaration");
8842       goto done;
8843     }
8844
8845   /* If the next two tokens are both identifiers, the code is
8846      erroneous. The usual cause of this situation is code like:
8847
8848        T t;
8849
8850      where "T" should name a type -- but does not.  */
8851   if (!decl_specifiers.any_type_specifiers_p
8852       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8853     {
8854       /* If parsing tentatively, we should commit; we really are
8855          looking at a declaration.  */
8856       cp_parser_commit_to_tentative_parse (parser);
8857       /* Give up.  */
8858       goto done;
8859     }
8860
8861   /* If we have seen at least one decl-specifier, and the next token
8862      is not a parenthesis, then we must be looking at a declaration.
8863      (After "int (" we might be looking at a functional cast.)  */
8864   if (decl_specifiers.any_specifiers_p
8865       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8866       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8867       && !cp_parser_error_occurred (parser))
8868     cp_parser_commit_to_tentative_parse (parser);
8869
8870   /* Keep going until we hit the `;' at the end of the simple
8871      declaration.  */
8872   saw_declarator = false;
8873   while (cp_lexer_next_token_is_not (parser->lexer,
8874                                      CPP_SEMICOLON))
8875     {
8876       cp_token *token;
8877       bool function_definition_p;
8878       tree decl;
8879
8880       if (saw_declarator)
8881         {
8882           /* If we are processing next declarator, coma is expected */
8883           token = cp_lexer_peek_token (parser->lexer);
8884           gcc_assert (token->type == CPP_COMMA);
8885           cp_lexer_consume_token (parser->lexer);
8886         }
8887       else
8888         saw_declarator = true;
8889
8890       /* Parse the init-declarator.  */
8891       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8892                                         /*checks=*/NULL,
8893                                         function_definition_allowed_p,
8894                                         /*member_p=*/false,
8895                                         declares_class_or_enum,
8896                                         &function_definition_p);
8897       /* If an error occurred while parsing tentatively, exit quickly.
8898          (That usually happens when in the body of a function; each
8899          statement is treated as a declaration-statement until proven
8900          otherwise.)  */
8901       if (cp_parser_error_occurred (parser))
8902         goto done;
8903       /* Handle function definitions specially.  */
8904       if (function_definition_p)
8905         {
8906           /* If the next token is a `,', then we are probably
8907              processing something like:
8908
8909                void f() {}, *p;
8910
8911              which is erroneous.  */
8912           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8913             {
8914               cp_token *token = cp_lexer_peek_token (parser->lexer);
8915               error_at (token->location,
8916                         "mixing"
8917                         " declarations and function-definitions is forbidden");
8918             }
8919           /* Otherwise, we're done with the list of declarators.  */
8920           else
8921             {
8922               pop_deferring_access_checks ();
8923               return;
8924             }
8925         }
8926       /* The next token should be either a `,' or a `;'.  */
8927       token = cp_lexer_peek_token (parser->lexer);
8928       /* If it's a `,', there are more declarators to come.  */
8929       if (token->type == CPP_COMMA)
8930         /* will be consumed next time around */;
8931       /* If it's a `;', we are done.  */
8932       else if (token->type == CPP_SEMICOLON)
8933         break;
8934       /* Anything else is an error.  */
8935       else
8936         {
8937           /* If we have already issued an error message we don't need
8938              to issue another one.  */
8939           if (decl != error_mark_node
8940               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8941             cp_parser_error (parser, "expected %<,%> or %<;%>");
8942           /* Skip tokens until we reach the end of the statement.  */
8943           cp_parser_skip_to_end_of_statement (parser);
8944           /* If the next token is now a `;', consume it.  */
8945           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8946             cp_lexer_consume_token (parser->lexer);
8947           goto done;
8948         }
8949       /* After the first time around, a function-definition is not
8950          allowed -- even if it was OK at first.  For example:
8951
8952            int i, f() {}
8953
8954          is not valid.  */
8955       function_definition_allowed_p = false;
8956     }
8957
8958   /* Issue an error message if no declarators are present, and the
8959      decl-specifier-seq does not itself declare a class or
8960      enumeration.  */
8961   if (!saw_declarator)
8962     {
8963       if (cp_parser_declares_only_class_p (parser))
8964         shadow_tag (&decl_specifiers);
8965       /* Perform any deferred access checks.  */
8966       perform_deferred_access_checks ();
8967     }
8968
8969   /* Consume the `;'.  */
8970   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8971
8972  done:
8973   pop_deferring_access_checks ();
8974 }
8975
8976 /* Parse a decl-specifier-seq.
8977
8978    decl-specifier-seq:
8979      decl-specifier-seq [opt] decl-specifier
8980
8981    decl-specifier:
8982      storage-class-specifier
8983      type-specifier
8984      function-specifier
8985      friend
8986      typedef
8987
8988    GNU Extension:
8989
8990    decl-specifier:
8991      attributes
8992
8993    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8994
8995    The parser flags FLAGS is used to control type-specifier parsing.
8996
8997    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8998    flags:
8999
9000      1: one of the decl-specifiers is an elaborated-type-specifier
9001         (i.e., a type declaration)
9002      2: one of the decl-specifiers is an enum-specifier or a
9003         class-specifier (i.e., a type definition)
9004
9005    */
9006
9007 static void
9008 cp_parser_decl_specifier_seq (cp_parser* parser,
9009                               cp_parser_flags flags,
9010                               cp_decl_specifier_seq *decl_specs,
9011                               int* declares_class_or_enum)
9012 {
9013   bool constructor_possible_p = !parser->in_declarator_p;
9014   cp_token *start_token = NULL;
9015
9016   /* Clear DECL_SPECS.  */
9017   clear_decl_specs (decl_specs);
9018
9019   /* Assume no class or enumeration type is declared.  */
9020   *declares_class_or_enum = 0;
9021
9022   /* Keep reading specifiers until there are no more to read.  */
9023   while (true)
9024     {
9025       bool constructor_p;
9026       bool found_decl_spec;
9027       cp_token *token;
9028
9029       /* Peek at the next token.  */
9030       token = cp_lexer_peek_token (parser->lexer);
9031
9032       /* Save the first token of the decl spec list for error
9033          reporting.  */
9034       if (!start_token)
9035         start_token = token;
9036       /* Handle attributes.  */
9037       if (token->keyword == RID_ATTRIBUTE)
9038         {
9039           /* Parse the attributes.  */
9040           decl_specs->attributes
9041             = chainon (decl_specs->attributes,
9042                        cp_parser_attributes_opt (parser));
9043           continue;
9044         }
9045       /* Assume we will find a decl-specifier keyword.  */
9046       found_decl_spec = true;
9047       /* If the next token is an appropriate keyword, we can simply
9048          add it to the list.  */
9049       switch (token->keyword)
9050         {
9051           /* decl-specifier:
9052                friend
9053                constexpr */
9054         case RID_FRIEND:
9055           if (!at_class_scope_p ())
9056             {
9057               error_at (token->location, "%<friend%> used outside of class");
9058               cp_lexer_purge_token (parser->lexer);
9059             }
9060           else
9061             {
9062               ++decl_specs->specs[(int) ds_friend];
9063               /* Consume the token.  */
9064               cp_lexer_consume_token (parser->lexer);
9065             }
9066           break;
9067
9068         case RID_CONSTEXPR:
9069           ++decl_specs->specs[(int) ds_constexpr];
9070           cp_lexer_consume_token (parser->lexer);
9071           break;
9072
9073           /* function-specifier:
9074                inline
9075                virtual
9076                explicit  */
9077         case RID_INLINE:
9078         case RID_VIRTUAL:
9079         case RID_EXPLICIT:
9080           cp_parser_function_specifier_opt (parser, decl_specs);
9081           break;
9082
9083           /* decl-specifier:
9084                typedef  */
9085         case RID_TYPEDEF:
9086           ++decl_specs->specs[(int) ds_typedef];
9087           /* Consume the token.  */
9088           cp_lexer_consume_token (parser->lexer);
9089           /* A constructor declarator cannot appear in a typedef.  */
9090           constructor_possible_p = false;
9091           /* The "typedef" keyword can only occur in a declaration; we
9092              may as well commit at this point.  */
9093           cp_parser_commit_to_tentative_parse (parser);
9094
9095           if (decl_specs->storage_class != sc_none)
9096             decl_specs->conflicting_specifiers_p = true;
9097           break;
9098
9099           /* storage-class-specifier:
9100                auto
9101                register
9102                static
9103                extern
9104                mutable
9105
9106              GNU Extension:
9107                thread  */
9108         case RID_AUTO:
9109           if (cxx_dialect == cxx98) 
9110             {
9111               /* Consume the token.  */
9112               cp_lexer_consume_token (parser->lexer);
9113
9114               /* Complain about `auto' as a storage specifier, if
9115                  we're complaining about C++0x compatibility.  */
9116               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9117                           " will change meaning in C++0x; please remove it");
9118
9119               /* Set the storage class anyway.  */
9120               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9121                                            token->location);
9122             }
9123           else
9124             /* C++0x auto type-specifier.  */
9125             found_decl_spec = false;
9126           break;
9127
9128         case RID_REGISTER:
9129         case RID_STATIC:
9130         case RID_EXTERN:
9131         case RID_MUTABLE:
9132           /* Consume the token.  */
9133           cp_lexer_consume_token (parser->lexer);
9134           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9135                                        token->location);
9136           break;
9137         case RID_THREAD:
9138           /* Consume the token.  */
9139           cp_lexer_consume_token (parser->lexer);
9140           ++decl_specs->specs[(int) ds_thread];
9141           break;
9142
9143         default:
9144           /* We did not yet find a decl-specifier yet.  */
9145           found_decl_spec = false;
9146           break;
9147         }
9148
9149       /* Constructors are a special case.  The `S' in `S()' is not a
9150          decl-specifier; it is the beginning of the declarator.  */
9151       constructor_p
9152         = (!found_decl_spec
9153            && constructor_possible_p
9154            && (cp_parser_constructor_declarator_p
9155                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9156
9157       /* If we don't have a DECL_SPEC yet, then we must be looking at
9158          a type-specifier.  */
9159       if (!found_decl_spec && !constructor_p)
9160         {
9161           int decl_spec_declares_class_or_enum;
9162           bool is_cv_qualifier;
9163           tree type_spec;
9164
9165           type_spec
9166             = cp_parser_type_specifier (parser, flags,
9167                                         decl_specs,
9168                                         /*is_declaration=*/true,
9169                                         &decl_spec_declares_class_or_enum,
9170                                         &is_cv_qualifier);
9171           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9172
9173           /* If this type-specifier referenced a user-defined type
9174              (a typedef, class-name, etc.), then we can't allow any
9175              more such type-specifiers henceforth.
9176
9177              [dcl.spec]
9178
9179              The longest sequence of decl-specifiers that could
9180              possibly be a type name is taken as the
9181              decl-specifier-seq of a declaration.  The sequence shall
9182              be self-consistent as described below.
9183
9184              [dcl.type]
9185
9186              As a general rule, at most one type-specifier is allowed
9187              in the complete decl-specifier-seq of a declaration.  The
9188              only exceptions are the following:
9189
9190              -- const or volatile can be combined with any other
9191                 type-specifier.
9192
9193              -- signed or unsigned can be combined with char, long,
9194                 short, or int.
9195
9196              -- ..
9197
9198              Example:
9199
9200                typedef char* Pc;
9201                void g (const int Pc);
9202
9203              Here, Pc is *not* part of the decl-specifier seq; it's
9204              the declarator.  Therefore, once we see a type-specifier
9205              (other than a cv-qualifier), we forbid any additional
9206              user-defined types.  We *do* still allow things like `int
9207              int' to be considered a decl-specifier-seq, and issue the
9208              error message later.  */
9209           if (type_spec && !is_cv_qualifier)
9210             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9211           /* A constructor declarator cannot follow a type-specifier.  */
9212           if (type_spec)
9213             {
9214               constructor_possible_p = false;
9215               found_decl_spec = true;
9216               if (!is_cv_qualifier)
9217                 decl_specs->any_type_specifiers_p = true;
9218             }
9219         }
9220
9221       /* If we still do not have a DECL_SPEC, then there are no more
9222          decl-specifiers.  */
9223       if (!found_decl_spec)
9224         break;
9225
9226       decl_specs->any_specifiers_p = true;
9227       /* After we see one decl-specifier, further decl-specifiers are
9228          always optional.  */
9229       flags |= CP_PARSER_FLAGS_OPTIONAL;
9230     }
9231
9232   cp_parser_check_decl_spec (decl_specs, start_token->location);
9233
9234   /* Don't allow a friend specifier with a class definition.  */
9235   if (decl_specs->specs[(int) ds_friend] != 0
9236       && (*declares_class_or_enum & 2))
9237     error_at (start_token->location,
9238               "class definition may not be declared a friend");
9239 }
9240
9241 /* Parse an (optional) storage-class-specifier.
9242
9243    storage-class-specifier:
9244      auto
9245      register
9246      static
9247      extern
9248      mutable
9249
9250    GNU Extension:
9251
9252    storage-class-specifier:
9253      thread
9254
9255    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9256
9257 static tree
9258 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9259 {
9260   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9261     {
9262     case RID_AUTO:
9263       if (cxx_dialect != cxx98)
9264         return NULL_TREE;
9265       /* Fall through for C++98.  */
9266
9267     case RID_REGISTER:
9268     case RID_STATIC:
9269     case RID_EXTERN:
9270     case RID_MUTABLE:
9271     case RID_THREAD:
9272       /* Consume the token.  */
9273       return cp_lexer_consume_token (parser->lexer)->u.value;
9274
9275     default:
9276       return NULL_TREE;
9277     }
9278 }
9279
9280 /* Parse an (optional) function-specifier.
9281
9282    function-specifier:
9283      inline
9284      virtual
9285      explicit
9286
9287    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9288    Updates DECL_SPECS, if it is non-NULL.  */
9289
9290 static tree
9291 cp_parser_function_specifier_opt (cp_parser* parser,
9292                                   cp_decl_specifier_seq *decl_specs)
9293 {
9294   cp_token *token = cp_lexer_peek_token (parser->lexer);
9295   switch (token->keyword)
9296     {
9297     case RID_INLINE:
9298       if (decl_specs)
9299         ++decl_specs->specs[(int) ds_inline];
9300       break;
9301
9302     case RID_VIRTUAL:
9303       /* 14.5.2.3 [temp.mem]
9304
9305          A member function template shall not be virtual.  */
9306       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9307         error_at (token->location, "templates may not be %<virtual%>");
9308       else if (decl_specs)
9309         ++decl_specs->specs[(int) ds_virtual];
9310       break;
9311
9312     case RID_EXPLICIT:
9313       if (decl_specs)
9314         ++decl_specs->specs[(int) ds_explicit];
9315       break;
9316
9317     default:
9318       return NULL_TREE;
9319     }
9320
9321   /* Consume the token.  */
9322   return cp_lexer_consume_token (parser->lexer)->u.value;
9323 }
9324
9325 /* Parse a linkage-specification.
9326
9327    linkage-specification:
9328      extern string-literal { declaration-seq [opt] }
9329      extern string-literal declaration  */
9330
9331 static void
9332 cp_parser_linkage_specification (cp_parser* parser)
9333 {
9334   tree linkage;
9335
9336   /* Look for the `extern' keyword.  */
9337   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9338
9339   /* Look for the string-literal.  */
9340   linkage = cp_parser_string_literal (parser, false, false);
9341
9342   /* Transform the literal into an identifier.  If the literal is a
9343      wide-character string, or contains embedded NULs, then we can't
9344      handle it as the user wants.  */
9345   if (strlen (TREE_STRING_POINTER (linkage))
9346       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9347     {
9348       cp_parser_error (parser, "invalid linkage-specification");
9349       /* Assume C++ linkage.  */
9350       linkage = lang_name_cplusplus;
9351     }
9352   else
9353     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9354
9355   /* We're now using the new linkage.  */
9356   push_lang_context (linkage);
9357
9358   /* If the next token is a `{', then we're using the first
9359      production.  */
9360   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9361     {
9362       /* Consume the `{' token.  */
9363       cp_lexer_consume_token (parser->lexer);
9364       /* Parse the declarations.  */
9365       cp_parser_declaration_seq_opt (parser);
9366       /* Look for the closing `}'.  */
9367       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9368     }
9369   /* Otherwise, there's just one declaration.  */
9370   else
9371     {
9372       bool saved_in_unbraced_linkage_specification_p;
9373
9374       saved_in_unbraced_linkage_specification_p
9375         = parser->in_unbraced_linkage_specification_p;
9376       parser->in_unbraced_linkage_specification_p = true;
9377       cp_parser_declaration (parser);
9378       parser->in_unbraced_linkage_specification_p
9379         = saved_in_unbraced_linkage_specification_p;
9380     }
9381
9382   /* We're done with the linkage-specification.  */
9383   pop_lang_context ();
9384 }
9385
9386 /* Parse a static_assert-declaration.
9387
9388    static_assert-declaration:
9389      static_assert ( constant-expression , string-literal ) ; 
9390
9391    If MEMBER_P, this static_assert is a class member.  */
9392
9393 static void 
9394 cp_parser_static_assert(cp_parser *parser, bool member_p)
9395 {
9396   tree condition;
9397   tree message;
9398   cp_token *token;
9399   location_t saved_loc;
9400
9401   /* Peek at the `static_assert' token so we can keep track of exactly
9402      where the static assertion started.  */
9403   token = cp_lexer_peek_token (parser->lexer);
9404   saved_loc = token->location;
9405
9406   /* Look for the `static_assert' keyword.  */
9407   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9408                                   "%<static_assert%>"))
9409     return;
9410
9411   /*  We know we are in a static assertion; commit to any tentative
9412       parse.  */
9413   if (cp_parser_parsing_tentatively (parser))
9414     cp_parser_commit_to_tentative_parse (parser);
9415
9416   /* Parse the `(' starting the static assertion condition.  */
9417   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9418
9419   /* Parse the constant-expression.  */
9420   condition = 
9421     cp_parser_constant_expression (parser,
9422                                    /*allow_non_constant_p=*/false,
9423                                    /*non_constant_p=*/NULL);
9424
9425   /* Parse the separating `,'.  */
9426   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9427
9428   /* Parse the string-literal message.  */
9429   message = cp_parser_string_literal (parser, 
9430                                       /*translate=*/false,
9431                                       /*wide_ok=*/true);
9432
9433   /* A `)' completes the static assertion.  */
9434   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9435     cp_parser_skip_to_closing_parenthesis (parser, 
9436                                            /*recovering=*/true, 
9437                                            /*or_comma=*/false,
9438                                            /*consume_paren=*/true);
9439
9440   /* A semicolon terminates the declaration.  */
9441   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9442
9443   /* Complete the static assertion, which may mean either processing 
9444      the static assert now or saving it for template instantiation.  */
9445   finish_static_assert (condition, message, saved_loc, member_p);
9446 }
9447
9448 /* Parse a `decltype' type. Returns the type. 
9449
9450    simple-type-specifier:
9451      decltype ( expression )  */
9452
9453 static tree
9454 cp_parser_decltype (cp_parser *parser)
9455 {
9456   tree expr;
9457   bool id_expression_or_member_access_p = false;
9458   const char *saved_message;
9459   bool saved_integral_constant_expression_p;
9460   bool saved_non_integral_constant_expression_p;
9461   cp_token *id_expr_start_token;
9462
9463   /* Look for the `decltype' token.  */
9464   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9465     return error_mark_node;
9466
9467   /* Types cannot be defined in a `decltype' expression.  Save away the
9468      old message.  */
9469   saved_message = parser->type_definition_forbidden_message;
9470
9471   /* And create the new one.  */
9472   parser->type_definition_forbidden_message
9473     = "types may not be defined in %<decltype%> expressions";
9474
9475   /* The restrictions on constant-expressions do not apply inside
9476      decltype expressions.  */
9477   saved_integral_constant_expression_p
9478     = parser->integral_constant_expression_p;
9479   saved_non_integral_constant_expression_p
9480     = parser->non_integral_constant_expression_p;
9481   parser->integral_constant_expression_p = false;
9482
9483   /* Do not actually evaluate the expression.  */
9484   ++cp_unevaluated_operand;
9485
9486   /* Do not warn about problems with the expression.  */
9487   ++c_inhibit_evaluation_warnings;
9488
9489   /* Parse the opening `('.  */
9490   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9491     return error_mark_node;
9492   
9493   /* First, try parsing an id-expression.  */
9494   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9495   cp_parser_parse_tentatively (parser);
9496   expr = cp_parser_id_expression (parser,
9497                                   /*template_keyword_p=*/false,
9498                                   /*check_dependency_p=*/true,
9499                                   /*template_p=*/NULL,
9500                                   /*declarator_p=*/false,
9501                                   /*optional_p=*/false);
9502
9503   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9504     {
9505       bool non_integral_constant_expression_p = false;
9506       tree id_expression = expr;
9507       cp_id_kind idk;
9508       const char *error_msg;
9509
9510       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9511         /* Lookup the name we got back from the id-expression.  */
9512         expr = cp_parser_lookup_name (parser, expr,
9513                                       none_type,
9514                                       /*is_template=*/false,
9515                                       /*is_namespace=*/false,
9516                                       /*check_dependency=*/true,
9517                                       /*ambiguous_decls=*/NULL,
9518                                       id_expr_start_token->location);
9519
9520       if (expr
9521           && expr != error_mark_node
9522           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9523           && TREE_CODE (expr) != TYPE_DECL
9524           && (TREE_CODE (expr) != BIT_NOT_EXPR
9525               || !TYPE_P (TREE_OPERAND (expr, 0)))
9526           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9527         {
9528           /* Complete lookup of the id-expression.  */
9529           expr = (finish_id_expression
9530                   (id_expression, expr, parser->scope, &idk,
9531                    /*integral_constant_expression_p=*/false,
9532                    /*allow_non_integral_constant_expression_p=*/true,
9533                    &non_integral_constant_expression_p,
9534                    /*template_p=*/false,
9535                    /*done=*/true,
9536                    /*address_p=*/false,
9537                    /*template_arg_p=*/false,
9538                    &error_msg,
9539                    id_expr_start_token->location));
9540
9541           if (expr == error_mark_node)
9542             /* We found an id-expression, but it was something that we
9543                should not have found. This is an error, not something
9544                we can recover from, so note that we found an
9545                id-expression and we'll recover as gracefully as
9546                possible.  */
9547             id_expression_or_member_access_p = true;
9548         }
9549
9550       if (expr 
9551           && expr != error_mark_node
9552           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9553         /* We have an id-expression.  */
9554         id_expression_or_member_access_p = true;
9555     }
9556
9557   if (!id_expression_or_member_access_p)
9558     {
9559       /* Abort the id-expression parse.  */
9560       cp_parser_abort_tentative_parse (parser);
9561
9562       /* Parsing tentatively, again.  */
9563       cp_parser_parse_tentatively (parser);
9564
9565       /* Parse a class member access.  */
9566       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9567                                            /*cast_p=*/false,
9568                                            /*member_access_only_p=*/true, NULL);
9569
9570       if (expr 
9571           && expr != error_mark_node
9572           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9573         /* We have an id-expression.  */
9574         id_expression_or_member_access_p = true;
9575     }
9576
9577   if (id_expression_or_member_access_p)
9578     /* We have parsed the complete id-expression or member access.  */
9579     cp_parser_parse_definitely (parser);
9580   else
9581     {
9582       bool saved_greater_than_is_operator_p;
9583
9584       /* Abort our attempt to parse an id-expression or member access
9585          expression.  */
9586       cp_parser_abort_tentative_parse (parser);
9587
9588       /* Within a parenthesized expression, a `>' token is always
9589          the greater-than operator.  */
9590       saved_greater_than_is_operator_p
9591         = parser->greater_than_is_operator_p;
9592       parser->greater_than_is_operator_p = true;
9593
9594       /* Parse a full expression.  */
9595       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9596
9597       /* The `>' token might be the end of a template-id or
9598          template-parameter-list now.  */
9599       parser->greater_than_is_operator_p
9600         = saved_greater_than_is_operator_p;
9601     }
9602
9603   /* Go back to evaluating expressions.  */
9604   --cp_unevaluated_operand;
9605   --c_inhibit_evaluation_warnings;
9606
9607   /* Restore the old message and the integral constant expression
9608      flags.  */
9609   parser->type_definition_forbidden_message = saved_message;
9610   parser->integral_constant_expression_p
9611     = saved_integral_constant_expression_p;
9612   parser->non_integral_constant_expression_p
9613     = saved_non_integral_constant_expression_p;
9614
9615   if (expr == error_mark_node)
9616     {
9617       /* Skip everything up to the closing `)'.  */
9618       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9619                                              /*consume_paren=*/true);
9620       return error_mark_node;
9621     }
9622   
9623   /* Parse to the closing `)'.  */
9624   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9625     {
9626       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9627                                              /*consume_paren=*/true);
9628       return error_mark_node;
9629     }
9630
9631   return finish_decltype_type (expr, id_expression_or_member_access_p);
9632 }
9633
9634 /* Special member functions [gram.special] */
9635
9636 /* Parse a conversion-function-id.
9637
9638    conversion-function-id:
9639      operator conversion-type-id
9640
9641    Returns an IDENTIFIER_NODE representing the operator.  */
9642
9643 static tree
9644 cp_parser_conversion_function_id (cp_parser* parser)
9645 {
9646   tree type;
9647   tree saved_scope;
9648   tree saved_qualifying_scope;
9649   tree saved_object_scope;
9650   tree pushed_scope = NULL_TREE;
9651
9652   /* Look for the `operator' token.  */
9653   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9654     return error_mark_node;
9655   /* When we parse the conversion-type-id, the current scope will be
9656      reset.  However, we need that information in able to look up the
9657      conversion function later, so we save it here.  */
9658   saved_scope = parser->scope;
9659   saved_qualifying_scope = parser->qualifying_scope;
9660   saved_object_scope = parser->object_scope;
9661   /* We must enter the scope of the class so that the names of
9662      entities declared within the class are available in the
9663      conversion-type-id.  For example, consider:
9664
9665        struct S {
9666          typedef int I;
9667          operator I();
9668        };
9669
9670        S::operator I() { ... }
9671
9672      In order to see that `I' is a type-name in the definition, we
9673      must be in the scope of `S'.  */
9674   if (saved_scope)
9675     pushed_scope = push_scope (saved_scope);
9676   /* Parse the conversion-type-id.  */
9677   type = cp_parser_conversion_type_id (parser);
9678   /* Leave the scope of the class, if any.  */
9679   if (pushed_scope)
9680     pop_scope (pushed_scope);
9681   /* Restore the saved scope.  */
9682   parser->scope = saved_scope;
9683   parser->qualifying_scope = saved_qualifying_scope;
9684   parser->object_scope = saved_object_scope;
9685   /* If the TYPE is invalid, indicate failure.  */
9686   if (type == error_mark_node)
9687     return error_mark_node;
9688   return mangle_conv_op_name_for_type (type);
9689 }
9690
9691 /* Parse a conversion-type-id:
9692
9693    conversion-type-id:
9694      type-specifier-seq conversion-declarator [opt]
9695
9696    Returns the TYPE specified.  */
9697
9698 static tree
9699 cp_parser_conversion_type_id (cp_parser* parser)
9700 {
9701   tree attributes;
9702   cp_decl_specifier_seq type_specifiers;
9703   cp_declarator *declarator;
9704   tree type_specified;
9705
9706   /* Parse the attributes.  */
9707   attributes = cp_parser_attributes_opt (parser);
9708   /* Parse the type-specifiers.  */
9709   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9710                                 /*is_trailing_return=*/false,
9711                                 &type_specifiers);
9712   /* If that didn't work, stop.  */
9713   if (type_specifiers.type == error_mark_node)
9714     return error_mark_node;
9715   /* Parse the conversion-declarator.  */
9716   declarator = cp_parser_conversion_declarator_opt (parser);
9717
9718   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9719                                     /*initialized=*/0, &attributes);
9720   if (attributes)
9721     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9722
9723   /* Don't give this error when parsing tentatively.  This happens to
9724      work because we always parse this definitively once.  */
9725   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9726       && type_uses_auto (type_specified))
9727     {
9728       error ("invalid use of %<auto%> in conversion operator");
9729       return error_mark_node;
9730     }
9731
9732   return type_specified;
9733 }
9734
9735 /* Parse an (optional) conversion-declarator.
9736
9737    conversion-declarator:
9738      ptr-operator conversion-declarator [opt]
9739
9740    */
9741
9742 static cp_declarator *
9743 cp_parser_conversion_declarator_opt (cp_parser* parser)
9744 {
9745   enum tree_code code;
9746   tree class_type;
9747   cp_cv_quals cv_quals;
9748
9749   /* We don't know if there's a ptr-operator next, or not.  */
9750   cp_parser_parse_tentatively (parser);
9751   /* Try the ptr-operator.  */
9752   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9753   /* If it worked, look for more conversion-declarators.  */
9754   if (cp_parser_parse_definitely (parser))
9755     {
9756       cp_declarator *declarator;
9757
9758       /* Parse another optional declarator.  */
9759       declarator = cp_parser_conversion_declarator_opt (parser);
9760
9761       return cp_parser_make_indirect_declarator
9762         (code, class_type, cv_quals, declarator);
9763    }
9764
9765   return NULL;
9766 }
9767
9768 /* Parse an (optional) ctor-initializer.
9769
9770    ctor-initializer:
9771      : mem-initializer-list
9772
9773    Returns TRUE iff the ctor-initializer was actually present.  */
9774
9775 static bool
9776 cp_parser_ctor_initializer_opt (cp_parser* parser)
9777 {
9778   /* If the next token is not a `:', then there is no
9779      ctor-initializer.  */
9780   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9781     {
9782       /* Do default initialization of any bases and members.  */
9783       if (DECL_CONSTRUCTOR_P (current_function_decl))
9784         finish_mem_initializers (NULL_TREE);
9785
9786       return false;
9787     }
9788
9789   /* Consume the `:' token.  */
9790   cp_lexer_consume_token (parser->lexer);
9791   /* And the mem-initializer-list.  */
9792   cp_parser_mem_initializer_list (parser);
9793
9794   return true;
9795 }
9796
9797 /* Parse a mem-initializer-list.
9798
9799    mem-initializer-list:
9800      mem-initializer ... [opt]
9801      mem-initializer ... [opt] , mem-initializer-list  */
9802
9803 static void
9804 cp_parser_mem_initializer_list (cp_parser* parser)
9805 {
9806   tree mem_initializer_list = NULL_TREE;
9807   cp_token *token = cp_lexer_peek_token (parser->lexer);
9808
9809   /* Let the semantic analysis code know that we are starting the
9810      mem-initializer-list.  */
9811   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9812     error_at (token->location,
9813               "only constructors take base initializers");
9814
9815   /* Loop through the list.  */
9816   while (true)
9817     {
9818       tree mem_initializer;
9819
9820       token = cp_lexer_peek_token (parser->lexer);
9821       /* Parse the mem-initializer.  */
9822       mem_initializer = cp_parser_mem_initializer (parser);
9823       /* If the next token is a `...', we're expanding member initializers. */
9824       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9825         {
9826           /* Consume the `...'. */
9827           cp_lexer_consume_token (parser->lexer);
9828
9829           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9830              can be expanded but members cannot. */
9831           if (mem_initializer != error_mark_node
9832               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9833             {
9834               error_at (token->location,
9835                         "cannot expand initializer for member %<%D%>",
9836                         TREE_PURPOSE (mem_initializer));
9837               mem_initializer = error_mark_node;
9838             }
9839
9840           /* Construct the pack expansion type. */
9841           if (mem_initializer != error_mark_node)
9842             mem_initializer = make_pack_expansion (mem_initializer);
9843         }
9844       /* Add it to the list, unless it was erroneous.  */
9845       if (mem_initializer != error_mark_node)
9846         {
9847           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9848           mem_initializer_list = mem_initializer;
9849         }
9850       /* If the next token is not a `,', we're done.  */
9851       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9852         break;
9853       /* Consume the `,' token.  */
9854       cp_lexer_consume_token (parser->lexer);
9855     }
9856
9857   /* Perform semantic analysis.  */
9858   if (DECL_CONSTRUCTOR_P (current_function_decl))
9859     finish_mem_initializers (mem_initializer_list);
9860 }
9861
9862 /* Parse a mem-initializer.
9863
9864    mem-initializer:
9865      mem-initializer-id ( expression-list [opt] )
9866      mem-initializer-id braced-init-list
9867
9868    GNU extension:
9869
9870    mem-initializer:
9871      ( expression-list [opt] )
9872
9873    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9874    class) or FIELD_DECL (for a non-static data member) to initialize;
9875    the TREE_VALUE is the expression-list.  An empty initialization
9876    list is represented by void_list_node.  */
9877
9878 static tree
9879 cp_parser_mem_initializer (cp_parser* parser)
9880 {
9881   tree mem_initializer_id;
9882   tree expression_list;
9883   tree member;
9884   cp_token *token = cp_lexer_peek_token (parser->lexer);
9885
9886   /* Find out what is being initialized.  */
9887   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9888     {
9889       permerror (token->location,
9890                  "anachronistic old-style base class initializer");
9891       mem_initializer_id = NULL_TREE;
9892     }
9893   else
9894     {
9895       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9896       if (mem_initializer_id == error_mark_node)
9897         return mem_initializer_id;
9898     }
9899   member = expand_member_init (mem_initializer_id);
9900   if (member && !DECL_P (member))
9901     in_base_initializer = 1;
9902
9903   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9904     {
9905       bool expr_non_constant_p;
9906       maybe_warn_cpp0x ("extended initializer lists");
9907       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9908       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9909       expression_list = build_tree_list (NULL_TREE, expression_list);
9910     }
9911   else
9912     {
9913       VEC(tree,gc)* vec;
9914       vec = cp_parser_parenthesized_expression_list (parser, false,
9915                                                      /*cast_p=*/false,
9916                                                      /*allow_expansion_p=*/true,
9917                                                      /*non_constant_p=*/NULL);
9918       if (vec == NULL)
9919         return error_mark_node;
9920       expression_list = build_tree_list_vec (vec);
9921       release_tree_vector (vec);
9922     }
9923
9924   if (expression_list == error_mark_node)
9925     return error_mark_node;
9926   if (!expression_list)
9927     expression_list = void_type_node;
9928
9929   in_base_initializer = 0;
9930
9931   return member ? build_tree_list (member, expression_list) : error_mark_node;
9932 }
9933
9934 /* Parse a mem-initializer-id.
9935
9936    mem-initializer-id:
9937      :: [opt] nested-name-specifier [opt] class-name
9938      identifier
9939
9940    Returns a TYPE indicating the class to be initializer for the first
9941    production.  Returns an IDENTIFIER_NODE indicating the data member
9942    to be initialized for the second production.  */
9943
9944 static tree
9945 cp_parser_mem_initializer_id (cp_parser* parser)
9946 {
9947   bool global_scope_p;
9948   bool nested_name_specifier_p;
9949   bool template_p = false;
9950   tree id;
9951
9952   cp_token *token = cp_lexer_peek_token (parser->lexer);
9953
9954   /* `typename' is not allowed in this context ([temp.res]).  */
9955   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9956     {
9957       error_at (token->location, 
9958                 "keyword %<typename%> not allowed in this context (a qualified "
9959                 "member initializer is implicitly a type)");
9960       cp_lexer_consume_token (parser->lexer);
9961     }
9962   /* Look for the optional `::' operator.  */
9963   global_scope_p
9964     = (cp_parser_global_scope_opt (parser,
9965                                    /*current_scope_valid_p=*/false)
9966        != NULL_TREE);
9967   /* Look for the optional nested-name-specifier.  The simplest way to
9968      implement:
9969
9970        [temp.res]
9971
9972        The keyword `typename' is not permitted in a base-specifier or
9973        mem-initializer; in these contexts a qualified name that
9974        depends on a template-parameter is implicitly assumed to be a
9975        type name.
9976
9977      is to assume that we have seen the `typename' keyword at this
9978      point.  */
9979   nested_name_specifier_p
9980     = (cp_parser_nested_name_specifier_opt (parser,
9981                                             /*typename_keyword_p=*/true,
9982                                             /*check_dependency_p=*/true,
9983                                             /*type_p=*/true,
9984                                             /*is_declaration=*/true)
9985        != NULL_TREE);
9986   if (nested_name_specifier_p)
9987     template_p = cp_parser_optional_template_keyword (parser);
9988   /* If there is a `::' operator or a nested-name-specifier, then we
9989      are definitely looking for a class-name.  */
9990   if (global_scope_p || nested_name_specifier_p)
9991     return cp_parser_class_name (parser,
9992                                  /*typename_keyword_p=*/true,
9993                                  /*template_keyword_p=*/template_p,
9994                                  none_type,
9995                                  /*check_dependency_p=*/true,
9996                                  /*class_head_p=*/false,
9997                                  /*is_declaration=*/true);
9998   /* Otherwise, we could also be looking for an ordinary identifier.  */
9999   cp_parser_parse_tentatively (parser);
10000   /* Try a class-name.  */
10001   id = cp_parser_class_name (parser,
10002                              /*typename_keyword_p=*/true,
10003                              /*template_keyword_p=*/false,
10004                              none_type,
10005                              /*check_dependency_p=*/true,
10006                              /*class_head_p=*/false,
10007                              /*is_declaration=*/true);
10008   /* If we found one, we're done.  */
10009   if (cp_parser_parse_definitely (parser))
10010     return id;
10011   /* Otherwise, look for an ordinary identifier.  */
10012   return cp_parser_identifier (parser);
10013 }
10014
10015 /* Overloading [gram.over] */
10016
10017 /* Parse an operator-function-id.
10018
10019    operator-function-id:
10020      operator operator
10021
10022    Returns an IDENTIFIER_NODE for the operator which is a
10023    human-readable spelling of the identifier, e.g., `operator +'.  */
10024
10025 static tree
10026 cp_parser_operator_function_id (cp_parser* parser)
10027 {
10028   /* Look for the `operator' keyword.  */
10029   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10030     return error_mark_node;
10031   /* And then the name of the operator itself.  */
10032   return cp_parser_operator (parser);
10033 }
10034
10035 /* Parse an operator.
10036
10037    operator:
10038      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10039      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10040      || ++ -- , ->* -> () []
10041
10042    GNU Extensions:
10043
10044    operator:
10045      <? >? <?= >?=
10046
10047    Returns an IDENTIFIER_NODE for the operator which is a
10048    human-readable spelling of the identifier, e.g., `operator +'.  */
10049
10050 static tree
10051 cp_parser_operator (cp_parser* parser)
10052 {
10053   tree id = NULL_TREE;
10054   cp_token *token;
10055
10056   /* Peek at the next token.  */
10057   token = cp_lexer_peek_token (parser->lexer);
10058   /* Figure out which operator we have.  */
10059   switch (token->type)
10060     {
10061     case CPP_KEYWORD:
10062       {
10063         enum tree_code op;
10064
10065         /* The keyword should be either `new' or `delete'.  */
10066         if (token->keyword == RID_NEW)
10067           op = NEW_EXPR;
10068         else if (token->keyword == RID_DELETE)
10069           op = DELETE_EXPR;
10070         else
10071           break;
10072
10073         /* Consume the `new' or `delete' token.  */
10074         cp_lexer_consume_token (parser->lexer);
10075
10076         /* Peek at the next token.  */
10077         token = cp_lexer_peek_token (parser->lexer);
10078         /* If it's a `[' token then this is the array variant of the
10079            operator.  */
10080         if (token->type == CPP_OPEN_SQUARE)
10081           {
10082             /* Consume the `[' token.  */
10083             cp_lexer_consume_token (parser->lexer);
10084             /* Look for the `]' token.  */
10085             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10086             id = ansi_opname (op == NEW_EXPR
10087                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10088           }
10089         /* Otherwise, we have the non-array variant.  */
10090         else
10091           id = ansi_opname (op);
10092
10093         return id;
10094       }
10095
10096     case CPP_PLUS:
10097       id = ansi_opname (PLUS_EXPR);
10098       break;
10099
10100     case CPP_MINUS:
10101       id = ansi_opname (MINUS_EXPR);
10102       break;
10103
10104     case CPP_MULT:
10105       id = ansi_opname (MULT_EXPR);
10106       break;
10107
10108     case CPP_DIV:
10109       id = ansi_opname (TRUNC_DIV_EXPR);
10110       break;
10111
10112     case CPP_MOD:
10113       id = ansi_opname (TRUNC_MOD_EXPR);
10114       break;
10115
10116     case CPP_XOR:
10117       id = ansi_opname (BIT_XOR_EXPR);
10118       break;
10119
10120     case CPP_AND:
10121       id = ansi_opname (BIT_AND_EXPR);
10122       break;
10123
10124     case CPP_OR:
10125       id = ansi_opname (BIT_IOR_EXPR);
10126       break;
10127
10128     case CPP_COMPL:
10129       id = ansi_opname (BIT_NOT_EXPR);
10130       break;
10131
10132     case CPP_NOT:
10133       id = ansi_opname (TRUTH_NOT_EXPR);
10134       break;
10135
10136     case CPP_EQ:
10137       id = ansi_assopname (NOP_EXPR);
10138       break;
10139
10140     case CPP_LESS:
10141       id = ansi_opname (LT_EXPR);
10142       break;
10143
10144     case CPP_GREATER:
10145       id = ansi_opname (GT_EXPR);
10146       break;
10147
10148     case CPP_PLUS_EQ:
10149       id = ansi_assopname (PLUS_EXPR);
10150       break;
10151
10152     case CPP_MINUS_EQ:
10153       id = ansi_assopname (MINUS_EXPR);
10154       break;
10155
10156     case CPP_MULT_EQ:
10157       id = ansi_assopname (MULT_EXPR);
10158       break;
10159
10160     case CPP_DIV_EQ:
10161       id = ansi_assopname (TRUNC_DIV_EXPR);
10162       break;
10163
10164     case CPP_MOD_EQ:
10165       id = ansi_assopname (TRUNC_MOD_EXPR);
10166       break;
10167
10168     case CPP_XOR_EQ:
10169       id = ansi_assopname (BIT_XOR_EXPR);
10170       break;
10171
10172     case CPP_AND_EQ:
10173       id = ansi_assopname (BIT_AND_EXPR);
10174       break;
10175
10176     case CPP_OR_EQ:
10177       id = ansi_assopname (BIT_IOR_EXPR);
10178       break;
10179
10180     case CPP_LSHIFT:
10181       id = ansi_opname (LSHIFT_EXPR);
10182       break;
10183
10184     case CPP_RSHIFT:
10185       id = ansi_opname (RSHIFT_EXPR);
10186       break;
10187
10188     case CPP_LSHIFT_EQ:
10189       id = ansi_assopname (LSHIFT_EXPR);
10190       break;
10191
10192     case CPP_RSHIFT_EQ:
10193       id = ansi_assopname (RSHIFT_EXPR);
10194       break;
10195
10196     case CPP_EQ_EQ:
10197       id = ansi_opname (EQ_EXPR);
10198       break;
10199
10200     case CPP_NOT_EQ:
10201       id = ansi_opname (NE_EXPR);
10202       break;
10203
10204     case CPP_LESS_EQ:
10205       id = ansi_opname (LE_EXPR);
10206       break;
10207
10208     case CPP_GREATER_EQ:
10209       id = ansi_opname (GE_EXPR);
10210       break;
10211
10212     case CPP_AND_AND:
10213       id = ansi_opname (TRUTH_ANDIF_EXPR);
10214       break;
10215
10216     case CPP_OR_OR:
10217       id = ansi_opname (TRUTH_ORIF_EXPR);
10218       break;
10219
10220     case CPP_PLUS_PLUS:
10221       id = ansi_opname (POSTINCREMENT_EXPR);
10222       break;
10223
10224     case CPP_MINUS_MINUS:
10225       id = ansi_opname (PREDECREMENT_EXPR);
10226       break;
10227
10228     case CPP_COMMA:
10229       id = ansi_opname (COMPOUND_EXPR);
10230       break;
10231
10232     case CPP_DEREF_STAR:
10233       id = ansi_opname (MEMBER_REF);
10234       break;
10235
10236     case CPP_DEREF:
10237       id = ansi_opname (COMPONENT_REF);
10238       break;
10239
10240     case CPP_OPEN_PAREN:
10241       /* Consume the `('.  */
10242       cp_lexer_consume_token (parser->lexer);
10243       /* Look for the matching `)'.  */
10244       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10245       return ansi_opname (CALL_EXPR);
10246
10247     case CPP_OPEN_SQUARE:
10248       /* Consume the `['.  */
10249       cp_lexer_consume_token (parser->lexer);
10250       /* Look for the matching `]'.  */
10251       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10252       return ansi_opname (ARRAY_REF);
10253
10254     default:
10255       /* Anything else is an error.  */
10256       break;
10257     }
10258
10259   /* If we have selected an identifier, we need to consume the
10260      operator token.  */
10261   if (id)
10262     cp_lexer_consume_token (parser->lexer);
10263   /* Otherwise, no valid operator name was present.  */
10264   else
10265     {
10266       cp_parser_error (parser, "expected operator");
10267       id = error_mark_node;
10268     }
10269
10270   return id;
10271 }
10272
10273 /* Parse a template-declaration.
10274
10275    template-declaration:
10276      export [opt] template < template-parameter-list > declaration
10277
10278    If MEMBER_P is TRUE, this template-declaration occurs within a
10279    class-specifier.
10280
10281    The grammar rule given by the standard isn't correct.  What
10282    is really meant is:
10283
10284    template-declaration:
10285      export [opt] template-parameter-list-seq
10286        decl-specifier-seq [opt] init-declarator [opt] ;
10287      export [opt] template-parameter-list-seq
10288        function-definition
10289
10290    template-parameter-list-seq:
10291      template-parameter-list-seq [opt]
10292      template < template-parameter-list >  */
10293
10294 static void
10295 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10296 {
10297   /* Check for `export'.  */
10298   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10299     {
10300       /* Consume the `export' token.  */
10301       cp_lexer_consume_token (parser->lexer);
10302       /* Warn that we do not support `export'.  */
10303       warning (0, "keyword %<export%> not implemented, and will be ignored");
10304     }
10305
10306   cp_parser_template_declaration_after_export (parser, member_p);
10307 }
10308
10309 /* Parse a template-parameter-list.
10310
10311    template-parameter-list:
10312      template-parameter
10313      template-parameter-list , template-parameter
10314
10315    Returns a TREE_LIST.  Each node represents a template parameter.
10316    The nodes are connected via their TREE_CHAINs.  */
10317
10318 static tree
10319 cp_parser_template_parameter_list (cp_parser* parser)
10320 {
10321   tree parameter_list = NULL_TREE;
10322
10323   begin_template_parm_list ();
10324   while (true)
10325     {
10326       tree parameter;
10327       bool is_non_type;
10328       bool is_parameter_pack;
10329       location_t parm_loc;
10330
10331       /* Parse the template-parameter.  */
10332       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10333       parameter = cp_parser_template_parameter (parser, 
10334                                                 &is_non_type,
10335                                                 &is_parameter_pack);
10336       /* Add it to the list.  */
10337       if (parameter != error_mark_node)
10338         parameter_list = process_template_parm (parameter_list,
10339                                                 parm_loc,
10340                                                 parameter,
10341                                                 is_non_type,
10342                                                 is_parameter_pack);
10343       else
10344        {
10345          tree err_parm = build_tree_list (parameter, parameter);
10346          TREE_VALUE (err_parm) = error_mark_node;
10347          parameter_list = chainon (parameter_list, err_parm);
10348        }
10349
10350       /* If the next token is not a `,', we're done.  */
10351       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10352         break;
10353       /* Otherwise, consume the `,' token.  */
10354       cp_lexer_consume_token (parser->lexer);
10355     }
10356
10357   return end_template_parm_list (parameter_list);
10358 }
10359
10360 /* Parse a template-parameter.
10361
10362    template-parameter:
10363      type-parameter
10364      parameter-declaration
10365
10366    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10367    the parameter.  The TREE_PURPOSE is the default value, if any.
10368    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10369    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10370    set to true iff this parameter is a parameter pack. */
10371
10372 static tree
10373 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10374                               bool *is_parameter_pack)
10375 {
10376   cp_token *token;
10377   cp_parameter_declarator *parameter_declarator;
10378   cp_declarator *id_declarator;
10379   tree parm;
10380
10381   /* Assume it is a type parameter or a template parameter.  */
10382   *is_non_type = false;
10383   /* Assume it not a parameter pack. */
10384   *is_parameter_pack = false;
10385   /* Peek at the next token.  */
10386   token = cp_lexer_peek_token (parser->lexer);
10387   /* If it is `class' or `template', we have a type-parameter.  */
10388   if (token->keyword == RID_TEMPLATE)
10389     return cp_parser_type_parameter (parser, is_parameter_pack);
10390   /* If it is `class' or `typename' we do not know yet whether it is a
10391      type parameter or a non-type parameter.  Consider:
10392
10393        template <typename T, typename T::X X> ...
10394
10395      or:
10396
10397        template <class C, class D*> ...
10398
10399      Here, the first parameter is a type parameter, and the second is
10400      a non-type parameter.  We can tell by looking at the token after
10401      the identifier -- if it is a `,', `=', or `>' then we have a type
10402      parameter.  */
10403   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10404     {
10405       /* Peek at the token after `class' or `typename'.  */
10406       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10407       /* If it's an ellipsis, we have a template type parameter
10408          pack. */
10409       if (token->type == CPP_ELLIPSIS)
10410         return cp_parser_type_parameter (parser, is_parameter_pack);
10411       /* If it's an identifier, skip it.  */
10412       if (token->type == CPP_NAME)
10413         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10414       /* Now, see if the token looks like the end of a template
10415          parameter.  */
10416       if (token->type == CPP_COMMA
10417           || token->type == CPP_EQ
10418           || token->type == CPP_GREATER)
10419         return cp_parser_type_parameter (parser, is_parameter_pack);
10420     }
10421
10422   /* Otherwise, it is a non-type parameter.
10423
10424      [temp.param]
10425
10426      When parsing a default template-argument for a non-type
10427      template-parameter, the first non-nested `>' is taken as the end
10428      of the template parameter-list rather than a greater-than
10429      operator.  */
10430   *is_non_type = true;
10431   parameter_declarator
10432      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10433                                         /*parenthesized_p=*/NULL);
10434
10435   /* If the parameter declaration is marked as a parameter pack, set
10436      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10437      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10438      grokdeclarator. */
10439   if (parameter_declarator
10440       && parameter_declarator->declarator
10441       && parameter_declarator->declarator->parameter_pack_p)
10442     {
10443       *is_parameter_pack = true;
10444       parameter_declarator->declarator->parameter_pack_p = false;
10445     }
10446
10447   /* If the next token is an ellipsis, and we don't already have it
10448      marked as a parameter pack, then we have a parameter pack (that
10449      has no declarator).  */
10450   if (!*is_parameter_pack
10451       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10452       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10453     {
10454       /* Consume the `...'.  */
10455       cp_lexer_consume_token (parser->lexer);
10456       maybe_warn_variadic_templates ();
10457       
10458       *is_parameter_pack = true;
10459     }
10460   /* We might end up with a pack expansion as the type of the non-type
10461      template parameter, in which case this is a non-type template
10462      parameter pack.  */
10463   else if (parameter_declarator
10464            && parameter_declarator->decl_specifiers.type
10465            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10466     {
10467       *is_parameter_pack = true;
10468       parameter_declarator->decl_specifiers.type = 
10469         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10470     }
10471
10472   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10473     {
10474       /* Parameter packs cannot have default arguments.  However, a
10475          user may try to do so, so we'll parse them and give an
10476          appropriate diagnostic here.  */
10477
10478       /* Consume the `='.  */
10479       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10480       cp_lexer_consume_token (parser->lexer);
10481       
10482       /* Find the name of the parameter pack.  */     
10483       id_declarator = parameter_declarator->declarator;
10484       while (id_declarator && id_declarator->kind != cdk_id)
10485         id_declarator = id_declarator->declarator;
10486       
10487       if (id_declarator && id_declarator->kind == cdk_id)
10488         error_at (start_token->location,
10489                   "template parameter pack %qD cannot have a default argument",
10490                   id_declarator->u.id.unqualified_name);
10491       else
10492         error_at (start_token->location,
10493                   "template parameter pack cannot have a default argument");
10494       
10495       /* Parse the default argument, but throw away the result.  */
10496       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10497     }
10498
10499   parm = grokdeclarator (parameter_declarator->declarator,
10500                          &parameter_declarator->decl_specifiers,
10501                          PARM, /*initialized=*/0,
10502                          /*attrlist=*/NULL);
10503   if (parm == error_mark_node)
10504     return error_mark_node;
10505
10506   return build_tree_list (parameter_declarator->default_argument, parm);
10507 }
10508
10509 /* Parse a type-parameter.
10510
10511    type-parameter:
10512      class identifier [opt]
10513      class identifier [opt] = type-id
10514      typename identifier [opt]
10515      typename identifier [opt] = type-id
10516      template < template-parameter-list > class identifier [opt]
10517      template < template-parameter-list > class identifier [opt]
10518        = id-expression
10519
10520    GNU Extension (variadic templates):
10521
10522    type-parameter:
10523      class ... identifier [opt]
10524      typename ... identifier [opt]
10525
10526    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10527    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10528    the declaration of the parameter.
10529
10530    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10531
10532 static tree
10533 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10534 {
10535   cp_token *token;
10536   tree parameter;
10537
10538   /* Look for a keyword to tell us what kind of parameter this is.  */
10539   token = cp_parser_require (parser, CPP_KEYWORD,
10540                              "%<class%>, %<typename%>, or %<template%>");
10541   if (!token)
10542     return error_mark_node;
10543
10544   switch (token->keyword)
10545     {
10546     case RID_CLASS:
10547     case RID_TYPENAME:
10548       {
10549         tree identifier;
10550         tree default_argument;
10551
10552         /* If the next token is an ellipsis, we have a template
10553            argument pack. */
10554         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10555           {
10556             /* Consume the `...' token. */
10557             cp_lexer_consume_token (parser->lexer);
10558             maybe_warn_variadic_templates ();
10559
10560             *is_parameter_pack = true;
10561           }
10562
10563         /* If the next token is an identifier, then it names the
10564            parameter.  */
10565         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10566           identifier = cp_parser_identifier (parser);
10567         else
10568           identifier = NULL_TREE;
10569
10570         /* Create the parameter.  */
10571         parameter = finish_template_type_parm (class_type_node, identifier);
10572
10573         /* If the next token is an `=', we have a default argument.  */
10574         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10575           {
10576             /* Consume the `=' token.  */
10577             cp_lexer_consume_token (parser->lexer);
10578             /* Parse the default-argument.  */
10579             push_deferring_access_checks (dk_no_deferred);
10580             default_argument = cp_parser_type_id (parser);
10581
10582             /* Template parameter packs cannot have default
10583                arguments. */
10584             if (*is_parameter_pack)
10585               {
10586                 if (identifier)
10587                   error_at (token->location,
10588                             "template parameter pack %qD cannot have a "
10589                             "default argument", identifier);
10590                 else
10591                   error_at (token->location,
10592                             "template parameter packs cannot have "
10593                             "default arguments");
10594                 default_argument = NULL_TREE;
10595               }
10596             pop_deferring_access_checks ();
10597           }
10598         else
10599           default_argument = NULL_TREE;
10600
10601         /* Create the combined representation of the parameter and the
10602            default argument.  */
10603         parameter = build_tree_list (default_argument, parameter);
10604       }
10605       break;
10606
10607     case RID_TEMPLATE:
10608       {
10609         tree parameter_list;
10610         tree identifier;
10611         tree default_argument;
10612
10613         /* Look for the `<'.  */
10614         cp_parser_require (parser, CPP_LESS, "%<<%>");
10615         /* Parse the template-parameter-list.  */
10616         parameter_list = cp_parser_template_parameter_list (parser);
10617         /* Look for the `>'.  */
10618         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10619         /* Look for the `class' keyword.  */
10620         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10621         /* If the next token is an ellipsis, we have a template
10622            argument pack. */
10623         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10624           {
10625             /* Consume the `...' token. */
10626             cp_lexer_consume_token (parser->lexer);
10627             maybe_warn_variadic_templates ();
10628
10629             *is_parameter_pack = true;
10630           }
10631         /* If the next token is an `=', then there is a
10632            default-argument.  If the next token is a `>', we are at
10633            the end of the parameter-list.  If the next token is a `,',
10634            then we are at the end of this parameter.  */
10635         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10636             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10637             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10638           {
10639             identifier = cp_parser_identifier (parser);
10640             /* Treat invalid names as if the parameter were nameless.  */
10641             if (identifier == error_mark_node)
10642               identifier = NULL_TREE;
10643           }
10644         else
10645           identifier = NULL_TREE;
10646
10647         /* Create the template parameter.  */
10648         parameter = finish_template_template_parm (class_type_node,
10649                                                    identifier);
10650
10651         /* If the next token is an `=', then there is a
10652            default-argument.  */
10653         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10654           {
10655             bool is_template;
10656
10657             /* Consume the `='.  */
10658             cp_lexer_consume_token (parser->lexer);
10659             /* Parse the id-expression.  */
10660             push_deferring_access_checks (dk_no_deferred);
10661             /* save token before parsing the id-expression, for error
10662                reporting */
10663             token = cp_lexer_peek_token (parser->lexer);
10664             default_argument
10665               = cp_parser_id_expression (parser,
10666                                          /*template_keyword_p=*/false,
10667                                          /*check_dependency_p=*/true,
10668                                          /*template_p=*/&is_template,
10669                                          /*declarator_p=*/false,
10670                                          /*optional_p=*/false);
10671             if (TREE_CODE (default_argument) == TYPE_DECL)
10672               /* If the id-expression was a template-id that refers to
10673                  a template-class, we already have the declaration here,
10674                  so no further lookup is needed.  */
10675                  ;
10676             else
10677               /* Look up the name.  */
10678               default_argument
10679                 = cp_parser_lookup_name (parser, default_argument,
10680                                          none_type,
10681                                          /*is_template=*/is_template,
10682                                          /*is_namespace=*/false,
10683                                          /*check_dependency=*/true,
10684                                          /*ambiguous_decls=*/NULL,
10685                                          token->location);
10686             /* See if the default argument is valid.  */
10687             default_argument
10688               = check_template_template_default_arg (default_argument);
10689
10690             /* Template parameter packs cannot have default
10691                arguments. */
10692             if (*is_parameter_pack)
10693               {
10694                 if (identifier)
10695                   error_at (token->location,
10696                             "template parameter pack %qD cannot "
10697                             "have a default argument",
10698                             identifier);
10699                 else
10700                   error_at (token->location, "template parameter packs cannot "
10701                             "have default arguments");
10702                 default_argument = NULL_TREE;
10703               }
10704             pop_deferring_access_checks ();
10705           }
10706         else
10707           default_argument = NULL_TREE;
10708
10709         /* Create the combined representation of the parameter and the
10710            default argument.  */
10711         parameter = build_tree_list (default_argument, parameter);
10712       }
10713       break;
10714
10715     default:
10716       gcc_unreachable ();
10717       break;
10718     }
10719
10720   return parameter;
10721 }
10722
10723 /* Parse a template-id.
10724
10725    template-id:
10726      template-name < template-argument-list [opt] >
10727
10728    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10729    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10730    returned.  Otherwise, if the template-name names a function, or set
10731    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10732    names a class, returns a TYPE_DECL for the specialization.
10733
10734    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10735    uninstantiated templates.  */
10736
10737 static tree
10738 cp_parser_template_id (cp_parser *parser,
10739                        bool template_keyword_p,
10740                        bool check_dependency_p,
10741                        bool is_declaration)
10742 {
10743   int i;
10744   tree templ;
10745   tree arguments;
10746   tree template_id;
10747   cp_token_position start_of_id = 0;
10748   deferred_access_check *chk;
10749   VEC (deferred_access_check,gc) *access_check;
10750   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10751   bool is_identifier;
10752
10753   /* If the next token corresponds to a template-id, there is no need
10754      to reparse it.  */
10755   next_token = cp_lexer_peek_token (parser->lexer);
10756   if (next_token->type == CPP_TEMPLATE_ID)
10757     {
10758       struct tree_check *check_value;
10759
10760       /* Get the stored value.  */
10761       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10762       /* Perform any access checks that were deferred.  */
10763       access_check = check_value->checks;
10764       if (access_check)
10765         {
10766           for (i = 0 ;
10767                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10768                ++i)
10769             {
10770               perform_or_defer_access_check (chk->binfo,
10771                                              chk->decl,
10772                                              chk->diag_decl);
10773             }
10774         }
10775       /* Return the stored value.  */
10776       return check_value->value;
10777     }
10778
10779   /* Avoid performing name lookup if there is no possibility of
10780      finding a template-id.  */
10781   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10782       || (next_token->type == CPP_NAME
10783           && !cp_parser_nth_token_starts_template_argument_list_p
10784                (parser, 2)))
10785     {
10786       cp_parser_error (parser, "expected template-id");
10787       return error_mark_node;
10788     }
10789
10790   /* Remember where the template-id starts.  */
10791   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10792     start_of_id = cp_lexer_token_position (parser->lexer, false);
10793
10794   push_deferring_access_checks (dk_deferred);
10795
10796   /* Parse the template-name.  */
10797   is_identifier = false;
10798   token = cp_lexer_peek_token (parser->lexer);
10799   templ = cp_parser_template_name (parser, template_keyword_p,
10800                                    check_dependency_p,
10801                                    is_declaration,
10802                                    &is_identifier);
10803   if (templ == error_mark_node || is_identifier)
10804     {
10805       pop_deferring_access_checks ();
10806       return templ;
10807     }
10808
10809   /* If we find the sequence `[:' after a template-name, it's probably
10810      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10811      parse correctly the argument list.  */
10812   next_token = cp_lexer_peek_token (parser->lexer);
10813   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10814   if (next_token->type == CPP_OPEN_SQUARE
10815       && next_token->flags & DIGRAPH
10816       && next_token_2->type == CPP_COLON
10817       && !(next_token_2->flags & PREV_WHITE))
10818     {
10819       cp_parser_parse_tentatively (parser);
10820       /* Change `:' into `::'.  */
10821       next_token_2->type = CPP_SCOPE;
10822       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10823          CPP_LESS.  */
10824       cp_lexer_consume_token (parser->lexer);
10825
10826       /* Parse the arguments.  */
10827       arguments = cp_parser_enclosed_template_argument_list (parser);
10828       if (!cp_parser_parse_definitely (parser))
10829         {
10830           /* If we couldn't parse an argument list, then we revert our changes
10831              and return simply an error. Maybe this is not a template-id
10832              after all.  */
10833           next_token_2->type = CPP_COLON;
10834           cp_parser_error (parser, "expected %<<%>");
10835           pop_deferring_access_checks ();
10836           return error_mark_node;
10837         }
10838       /* Otherwise, emit an error about the invalid digraph, but continue
10839          parsing because we got our argument list.  */
10840       if (permerror (next_token->location,
10841                      "%<<::%> cannot begin a template-argument list"))
10842         {
10843           static bool hint = false;
10844           inform (next_token->location,
10845                   "%<<:%> is an alternate spelling for %<[%>."
10846                   " Insert whitespace between %<<%> and %<::%>");
10847           if (!hint && !flag_permissive)
10848             {
10849               inform (next_token->location, "(if you use %<-fpermissive%>"
10850                       " G++ will accept your code)");
10851               hint = true;
10852             }
10853         }
10854     }
10855   else
10856     {
10857       /* Look for the `<' that starts the template-argument-list.  */
10858       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10859         {
10860           pop_deferring_access_checks ();
10861           return error_mark_node;
10862         }
10863       /* Parse the arguments.  */
10864       arguments = cp_parser_enclosed_template_argument_list (parser);
10865     }
10866
10867   /* Build a representation of the specialization.  */
10868   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10869     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10870   else if (DECL_CLASS_TEMPLATE_P (templ)
10871            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10872     {
10873       bool entering_scope;
10874       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10875          template (rather than some instantiation thereof) only if
10876          is not nested within some other construct.  For example, in
10877          "template <typename T> void f(T) { A<T>::", A<T> is just an
10878          instantiation of A.  */
10879       entering_scope = (template_parm_scope_p ()
10880                         && cp_lexer_next_token_is (parser->lexer,
10881                                                    CPP_SCOPE));
10882       template_id
10883         = finish_template_type (templ, arguments, entering_scope);
10884     }
10885   else
10886     {
10887       /* If it's not a class-template or a template-template, it should be
10888          a function-template.  */
10889       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10890                    || TREE_CODE (templ) == OVERLOAD
10891                    || BASELINK_P (templ)));
10892
10893       template_id = lookup_template_function (templ, arguments);
10894     }
10895
10896   /* If parsing tentatively, replace the sequence of tokens that makes
10897      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10898      should we re-parse the token stream, we will not have to repeat
10899      the effort required to do the parse, nor will we issue duplicate
10900      error messages about problems during instantiation of the
10901      template.  */
10902   if (start_of_id)
10903     {
10904       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10905
10906       /* Reset the contents of the START_OF_ID token.  */
10907       token->type = CPP_TEMPLATE_ID;
10908       /* Retrieve any deferred checks.  Do not pop this access checks yet
10909          so the memory will not be reclaimed during token replacing below.  */
10910       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10911       token->u.tree_check_value->value = template_id;
10912       token->u.tree_check_value->checks = get_deferred_access_checks ();
10913       token->keyword = RID_MAX;
10914
10915       /* Purge all subsequent tokens.  */
10916       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10917
10918       /* ??? Can we actually assume that, if template_id ==
10919          error_mark_node, we will have issued a diagnostic to the
10920          user, as opposed to simply marking the tentative parse as
10921          failed?  */
10922       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10923         error_at (token->location, "parse error in template argument list");
10924     }
10925
10926   pop_deferring_access_checks ();
10927   return template_id;
10928 }
10929
10930 /* Parse a template-name.
10931
10932    template-name:
10933      identifier
10934
10935    The standard should actually say:
10936
10937    template-name:
10938      identifier
10939      operator-function-id
10940
10941    A defect report has been filed about this issue.
10942
10943    A conversion-function-id cannot be a template name because they cannot
10944    be part of a template-id. In fact, looking at this code:
10945
10946    a.operator K<int>()
10947
10948    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10949    It is impossible to call a templated conversion-function-id with an
10950    explicit argument list, since the only allowed template parameter is
10951    the type to which it is converting.
10952
10953    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10954    `template' keyword, in a construction like:
10955
10956      T::template f<3>()
10957
10958    In that case `f' is taken to be a template-name, even though there
10959    is no way of knowing for sure.
10960
10961    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10962    name refers to a set of overloaded functions, at least one of which
10963    is a template, or an IDENTIFIER_NODE with the name of the template,
10964    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10965    names are looked up inside uninstantiated templates.  */
10966
10967 static tree
10968 cp_parser_template_name (cp_parser* parser,
10969                          bool template_keyword_p,
10970                          bool check_dependency_p,
10971                          bool is_declaration,
10972                          bool *is_identifier)
10973 {
10974   tree identifier;
10975   tree decl;
10976   tree fns;
10977   cp_token *token = cp_lexer_peek_token (parser->lexer);
10978
10979   /* If the next token is `operator', then we have either an
10980      operator-function-id or a conversion-function-id.  */
10981   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10982     {
10983       /* We don't know whether we're looking at an
10984          operator-function-id or a conversion-function-id.  */
10985       cp_parser_parse_tentatively (parser);
10986       /* Try an operator-function-id.  */
10987       identifier = cp_parser_operator_function_id (parser);
10988       /* If that didn't work, try a conversion-function-id.  */
10989       if (!cp_parser_parse_definitely (parser))
10990         {
10991           cp_parser_error (parser, "expected template-name");
10992           return error_mark_node;
10993         }
10994     }
10995   /* Look for the identifier.  */
10996   else
10997     identifier = cp_parser_identifier (parser);
10998
10999   /* If we didn't find an identifier, we don't have a template-id.  */
11000   if (identifier == error_mark_node)
11001     return error_mark_node;
11002
11003   /* If the name immediately followed the `template' keyword, then it
11004      is a template-name.  However, if the next token is not `<', then
11005      we do not treat it as a template-name, since it is not being used
11006      as part of a template-id.  This enables us to handle constructs
11007      like:
11008
11009        template <typename T> struct S { S(); };
11010        template <typename T> S<T>::S();
11011
11012      correctly.  We would treat `S' as a template -- if it were `S<T>'
11013      -- but we do not if there is no `<'.  */
11014
11015   if (processing_template_decl
11016       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11017     {
11018       /* In a declaration, in a dependent context, we pretend that the
11019          "template" keyword was present in order to improve error
11020          recovery.  For example, given:
11021
11022            template <typename T> void f(T::X<int>);
11023
11024          we want to treat "X<int>" as a template-id.  */
11025       if (is_declaration
11026           && !template_keyword_p
11027           && parser->scope && TYPE_P (parser->scope)
11028           && check_dependency_p
11029           && dependent_scope_p (parser->scope)
11030           /* Do not do this for dtors (or ctors), since they never
11031              need the template keyword before their name.  */
11032           && !constructor_name_p (identifier, parser->scope))
11033         {
11034           cp_token_position start = 0;
11035
11036           /* Explain what went wrong.  */
11037           error_at (token->location, "non-template %qD used as template",
11038                     identifier);
11039           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11040                   parser->scope, identifier);
11041           /* If parsing tentatively, find the location of the "<" token.  */
11042           if (cp_parser_simulate_error (parser))
11043             start = cp_lexer_token_position (parser->lexer, true);
11044           /* Parse the template arguments so that we can issue error
11045              messages about them.  */
11046           cp_lexer_consume_token (parser->lexer);
11047           cp_parser_enclosed_template_argument_list (parser);
11048           /* Skip tokens until we find a good place from which to
11049              continue parsing.  */
11050           cp_parser_skip_to_closing_parenthesis (parser,
11051                                                  /*recovering=*/true,
11052                                                  /*or_comma=*/true,
11053                                                  /*consume_paren=*/false);
11054           /* If parsing tentatively, permanently remove the
11055              template argument list.  That will prevent duplicate
11056              error messages from being issued about the missing
11057              "template" keyword.  */
11058           if (start)
11059             cp_lexer_purge_tokens_after (parser->lexer, start);
11060           if (is_identifier)
11061             *is_identifier = true;
11062           return identifier;
11063         }
11064
11065       /* If the "template" keyword is present, then there is generally
11066          no point in doing name-lookup, so we just return IDENTIFIER.
11067          But, if the qualifying scope is non-dependent then we can
11068          (and must) do name-lookup normally.  */
11069       if (template_keyword_p
11070           && (!parser->scope
11071               || (TYPE_P (parser->scope)
11072                   && dependent_type_p (parser->scope))))
11073         return identifier;
11074     }
11075
11076   /* Look up the name.  */
11077   decl = cp_parser_lookup_name (parser, identifier,
11078                                 none_type,
11079                                 /*is_template=*/false,
11080                                 /*is_namespace=*/false,
11081                                 check_dependency_p,
11082                                 /*ambiguous_decls=*/NULL,
11083                                 token->location);
11084   decl = maybe_get_template_decl_from_type_decl (decl);
11085
11086   /* If DECL is a template, then the name was a template-name.  */
11087   if (TREE_CODE (decl) == TEMPLATE_DECL)
11088     ;
11089   else
11090     {
11091       tree fn = NULL_TREE;
11092
11093       /* The standard does not explicitly indicate whether a name that
11094          names a set of overloaded declarations, some of which are
11095          templates, is a template-name.  However, such a name should
11096          be a template-name; otherwise, there is no way to form a
11097          template-id for the overloaded templates.  */
11098       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11099       if (TREE_CODE (fns) == OVERLOAD)
11100         for (fn = fns; fn; fn = OVL_NEXT (fn))
11101           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11102             break;
11103
11104       if (!fn)
11105         {
11106           /* The name does not name a template.  */
11107           cp_parser_error (parser, "expected template-name");
11108           return error_mark_node;
11109         }
11110     }
11111
11112   /* If DECL is dependent, and refers to a function, then just return
11113      its name; we will look it up again during template instantiation.  */
11114   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11115     {
11116       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11117       if (TYPE_P (scope) && dependent_type_p (scope))
11118         return identifier;
11119     }
11120
11121   return decl;
11122 }
11123
11124 /* Parse a template-argument-list.
11125
11126    template-argument-list:
11127      template-argument ... [opt]
11128      template-argument-list , template-argument ... [opt]
11129
11130    Returns a TREE_VEC containing the arguments.  */
11131
11132 static tree
11133 cp_parser_template_argument_list (cp_parser* parser)
11134 {
11135   tree fixed_args[10];
11136   unsigned n_args = 0;
11137   unsigned alloced = 10;
11138   tree *arg_ary = fixed_args;
11139   tree vec;
11140   bool saved_in_template_argument_list_p;
11141   bool saved_ice_p;
11142   bool saved_non_ice_p;
11143
11144   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11145   parser->in_template_argument_list_p = true;
11146   /* Even if the template-id appears in an integral
11147      constant-expression, the contents of the argument list do
11148      not.  */
11149   saved_ice_p = parser->integral_constant_expression_p;
11150   parser->integral_constant_expression_p = false;
11151   saved_non_ice_p = parser->non_integral_constant_expression_p;
11152   parser->non_integral_constant_expression_p = false;
11153   /* Parse the arguments.  */
11154   do
11155     {
11156       tree argument;
11157
11158       if (n_args)
11159         /* Consume the comma.  */
11160         cp_lexer_consume_token (parser->lexer);
11161
11162       /* Parse the template-argument.  */
11163       argument = cp_parser_template_argument (parser);
11164
11165       /* If the next token is an ellipsis, we're expanding a template
11166          argument pack. */
11167       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11168         {
11169           if (argument == error_mark_node)
11170             {
11171               cp_token *token = cp_lexer_peek_token (parser->lexer);
11172               error_at (token->location,
11173                         "expected parameter pack before %<...%>");
11174             }
11175           /* Consume the `...' token. */
11176           cp_lexer_consume_token (parser->lexer);
11177
11178           /* Make the argument into a TYPE_PACK_EXPANSION or
11179              EXPR_PACK_EXPANSION. */
11180           argument = make_pack_expansion (argument);
11181         }
11182
11183       if (n_args == alloced)
11184         {
11185           alloced *= 2;
11186
11187           if (arg_ary == fixed_args)
11188             {
11189               arg_ary = XNEWVEC (tree, alloced);
11190               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11191             }
11192           else
11193             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11194         }
11195       arg_ary[n_args++] = argument;
11196     }
11197   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11198
11199   vec = make_tree_vec (n_args);
11200
11201   while (n_args--)
11202     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11203
11204   if (arg_ary != fixed_args)
11205     free (arg_ary);
11206   parser->non_integral_constant_expression_p = saved_non_ice_p;
11207   parser->integral_constant_expression_p = saved_ice_p;
11208   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11209   return vec;
11210 }
11211
11212 /* Parse a template-argument.
11213
11214    template-argument:
11215      assignment-expression
11216      type-id
11217      id-expression
11218
11219    The representation is that of an assignment-expression, type-id, or
11220    id-expression -- except that the qualified id-expression is
11221    evaluated, so that the value returned is either a DECL or an
11222    OVERLOAD.
11223
11224    Although the standard says "assignment-expression", it forbids
11225    throw-expressions or assignments in the template argument.
11226    Therefore, we use "conditional-expression" instead.  */
11227
11228 static tree
11229 cp_parser_template_argument (cp_parser* parser)
11230 {
11231   tree argument;
11232   bool template_p;
11233   bool address_p;
11234   bool maybe_type_id = false;
11235   cp_token *token = NULL, *argument_start_token = NULL;
11236   cp_id_kind idk;
11237
11238   /* There's really no way to know what we're looking at, so we just
11239      try each alternative in order.
11240
11241        [temp.arg]
11242
11243        In a template-argument, an ambiguity between a type-id and an
11244        expression is resolved to a type-id, regardless of the form of
11245        the corresponding template-parameter.
11246
11247      Therefore, we try a type-id first.  */
11248   cp_parser_parse_tentatively (parser);
11249   argument = cp_parser_template_type_arg (parser);
11250   /* If there was no error parsing the type-id but the next token is a
11251      '>>', our behavior depends on which dialect of C++ we're
11252      parsing. In C++98, we probably found a typo for '> >'. But there
11253      are type-id which are also valid expressions. For instance:
11254
11255      struct X { int operator >> (int); };
11256      template <int V> struct Foo {};
11257      Foo<X () >> 5> r;
11258
11259      Here 'X()' is a valid type-id of a function type, but the user just
11260      wanted to write the expression "X() >> 5". Thus, we remember that we
11261      found a valid type-id, but we still try to parse the argument as an
11262      expression to see what happens. 
11263
11264      In C++0x, the '>>' will be considered two separate '>'
11265      tokens.  */
11266   if (!cp_parser_error_occurred (parser)
11267       && cxx_dialect == cxx98
11268       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11269     {
11270       maybe_type_id = true;
11271       cp_parser_abort_tentative_parse (parser);
11272     }
11273   else
11274     {
11275       /* If the next token isn't a `,' or a `>', then this argument wasn't
11276       really finished. This means that the argument is not a valid
11277       type-id.  */
11278       if (!cp_parser_next_token_ends_template_argument_p (parser))
11279         cp_parser_error (parser, "expected template-argument");
11280       /* If that worked, we're done.  */
11281       if (cp_parser_parse_definitely (parser))
11282         return argument;
11283     }
11284   /* We're still not sure what the argument will be.  */
11285   cp_parser_parse_tentatively (parser);
11286   /* Try a template.  */
11287   argument_start_token = cp_lexer_peek_token (parser->lexer);
11288   argument = cp_parser_id_expression (parser,
11289                                       /*template_keyword_p=*/false,
11290                                       /*check_dependency_p=*/true,
11291                                       &template_p,
11292                                       /*declarator_p=*/false,
11293                                       /*optional_p=*/false);
11294   /* If the next token isn't a `,' or a `>', then this argument wasn't
11295      really finished.  */
11296   if (!cp_parser_next_token_ends_template_argument_p (parser))
11297     cp_parser_error (parser, "expected template-argument");
11298   if (!cp_parser_error_occurred (parser))
11299     {
11300       /* Figure out what is being referred to.  If the id-expression
11301          was for a class template specialization, then we will have a
11302          TYPE_DECL at this point.  There is no need to do name lookup
11303          at this point in that case.  */
11304       if (TREE_CODE (argument) != TYPE_DECL)
11305         argument = cp_parser_lookup_name (parser, argument,
11306                                           none_type,
11307                                           /*is_template=*/template_p,
11308                                           /*is_namespace=*/false,
11309                                           /*check_dependency=*/true,
11310                                           /*ambiguous_decls=*/NULL,
11311                                           argument_start_token->location);
11312       if (TREE_CODE (argument) != TEMPLATE_DECL
11313           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11314         cp_parser_error (parser, "expected template-name");
11315     }
11316   if (cp_parser_parse_definitely (parser))
11317     return argument;
11318   /* It must be a non-type argument.  There permitted cases are given
11319      in [temp.arg.nontype]:
11320
11321      -- an integral constant-expression of integral or enumeration
11322         type; or
11323
11324      -- the name of a non-type template-parameter; or
11325
11326      -- the name of an object or function with external linkage...
11327
11328      -- the address of an object or function with external linkage...
11329
11330      -- a pointer to member...  */
11331   /* Look for a non-type template parameter.  */
11332   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11333     {
11334       cp_parser_parse_tentatively (parser);
11335       argument = cp_parser_primary_expression (parser,
11336                                                /*address_p=*/false,
11337                                                /*cast_p=*/false,
11338                                                /*template_arg_p=*/true,
11339                                                &idk);
11340       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11341           || !cp_parser_next_token_ends_template_argument_p (parser))
11342         cp_parser_simulate_error (parser);
11343       if (cp_parser_parse_definitely (parser))
11344         return argument;
11345     }
11346
11347   /* If the next token is "&", the argument must be the address of an
11348      object or function with external linkage.  */
11349   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11350   if (address_p)
11351     cp_lexer_consume_token (parser->lexer);
11352   /* See if we might have an id-expression.  */
11353   token = cp_lexer_peek_token (parser->lexer);
11354   if (token->type == CPP_NAME
11355       || token->keyword == RID_OPERATOR
11356       || token->type == CPP_SCOPE
11357       || token->type == CPP_TEMPLATE_ID
11358       || token->type == CPP_NESTED_NAME_SPECIFIER)
11359     {
11360       cp_parser_parse_tentatively (parser);
11361       argument = cp_parser_primary_expression (parser,
11362                                                address_p,
11363                                                /*cast_p=*/false,
11364                                                /*template_arg_p=*/true,
11365                                                &idk);
11366       if (cp_parser_error_occurred (parser)
11367           || !cp_parser_next_token_ends_template_argument_p (parser))
11368         cp_parser_abort_tentative_parse (parser);
11369       else
11370         {
11371           if (TREE_CODE (argument) == INDIRECT_REF)
11372             {
11373               gcc_assert (REFERENCE_REF_P (argument));
11374               argument = TREE_OPERAND (argument, 0);
11375             }
11376
11377           if (TREE_CODE (argument) == VAR_DECL)
11378             {
11379               /* A variable without external linkage might still be a
11380                  valid constant-expression, so no error is issued here
11381                  if the external-linkage check fails.  */
11382               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
11383                 cp_parser_simulate_error (parser);
11384             }
11385           else if (is_overloaded_fn (argument))
11386             /* All overloaded functions are allowed; if the external
11387                linkage test does not pass, an error will be issued
11388                later.  */
11389             ;
11390           else if (address_p
11391                    && (TREE_CODE (argument) == OFFSET_REF
11392                        || TREE_CODE (argument) == SCOPE_REF))
11393             /* A pointer-to-member.  */
11394             ;
11395           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11396             ;
11397           else
11398             cp_parser_simulate_error (parser);
11399
11400           if (cp_parser_parse_definitely (parser))
11401             {
11402               if (address_p)
11403                 argument = build_x_unary_op (ADDR_EXPR, argument,
11404                                              tf_warning_or_error);
11405               return argument;
11406             }
11407         }
11408     }
11409   /* If the argument started with "&", there are no other valid
11410      alternatives at this point.  */
11411   if (address_p)
11412     {
11413       cp_parser_error (parser, "invalid non-type template argument");
11414       return error_mark_node;
11415     }
11416
11417   /* If the argument wasn't successfully parsed as a type-id followed
11418      by '>>', the argument can only be a constant expression now.
11419      Otherwise, we try parsing the constant-expression tentatively,
11420      because the argument could really be a type-id.  */
11421   if (maybe_type_id)
11422     cp_parser_parse_tentatively (parser);
11423   argument = cp_parser_constant_expression (parser,
11424                                             /*allow_non_constant_p=*/false,
11425                                             /*non_constant_p=*/NULL);
11426   argument = fold_non_dependent_expr (argument);
11427   if (!maybe_type_id)
11428     return argument;
11429   if (!cp_parser_next_token_ends_template_argument_p (parser))
11430     cp_parser_error (parser, "expected template-argument");
11431   if (cp_parser_parse_definitely (parser))
11432     return argument;
11433   /* We did our best to parse the argument as a non type-id, but that
11434      was the only alternative that matched (albeit with a '>' after
11435      it). We can assume it's just a typo from the user, and a
11436      diagnostic will then be issued.  */
11437   return cp_parser_template_type_arg (parser);
11438 }
11439
11440 /* Parse an explicit-instantiation.
11441
11442    explicit-instantiation:
11443      template declaration
11444
11445    Although the standard says `declaration', what it really means is:
11446
11447    explicit-instantiation:
11448      template decl-specifier-seq [opt] declarator [opt] ;
11449
11450    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11451    supposed to be allowed.  A defect report has been filed about this
11452    issue.
11453
11454    GNU Extension:
11455
11456    explicit-instantiation:
11457      storage-class-specifier template
11458        decl-specifier-seq [opt] declarator [opt] ;
11459      function-specifier template
11460        decl-specifier-seq [opt] declarator [opt] ;  */
11461
11462 static void
11463 cp_parser_explicit_instantiation (cp_parser* parser)
11464 {
11465   int declares_class_or_enum;
11466   cp_decl_specifier_seq decl_specifiers;
11467   tree extension_specifier = NULL_TREE;
11468   cp_token *token;
11469
11470   /* Look for an (optional) storage-class-specifier or
11471      function-specifier.  */
11472   if (cp_parser_allow_gnu_extensions_p (parser))
11473     {
11474       extension_specifier
11475         = cp_parser_storage_class_specifier_opt (parser);
11476       if (!extension_specifier)
11477         extension_specifier
11478           = cp_parser_function_specifier_opt (parser,
11479                                               /*decl_specs=*/NULL);
11480     }
11481
11482   /* Look for the `template' keyword.  */
11483   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11484   /* Let the front end know that we are processing an explicit
11485      instantiation.  */
11486   begin_explicit_instantiation ();
11487   /* [temp.explicit] says that we are supposed to ignore access
11488      control while processing explicit instantiation directives.  */
11489   push_deferring_access_checks (dk_no_check);
11490   /* Parse a decl-specifier-seq.  */
11491   token = cp_lexer_peek_token (parser->lexer);
11492   cp_parser_decl_specifier_seq (parser,
11493                                 CP_PARSER_FLAGS_OPTIONAL,
11494                                 &decl_specifiers,
11495                                 &declares_class_or_enum);
11496   /* If there was exactly one decl-specifier, and it declared a class,
11497      and there's no declarator, then we have an explicit type
11498      instantiation.  */
11499   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11500     {
11501       tree type;
11502
11503       type = check_tag_decl (&decl_specifiers);
11504       /* Turn access control back on for names used during
11505          template instantiation.  */
11506       pop_deferring_access_checks ();
11507       if (type)
11508         do_type_instantiation (type, extension_specifier,
11509                                /*complain=*/tf_error);
11510     }
11511   else
11512     {
11513       cp_declarator *declarator;
11514       tree decl;
11515
11516       /* Parse the declarator.  */
11517       declarator
11518         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11519                                 /*ctor_dtor_or_conv_p=*/NULL,
11520                                 /*parenthesized_p=*/NULL,
11521                                 /*member_p=*/false);
11522       if (declares_class_or_enum & 2)
11523         cp_parser_check_for_definition_in_return_type (declarator,
11524                                                        decl_specifiers.type,
11525                                                        decl_specifiers.type_location);
11526       if (declarator != cp_error_declarator)
11527         {
11528           decl = grokdeclarator (declarator, &decl_specifiers,
11529                                  NORMAL, 0, &decl_specifiers.attributes);
11530           /* Turn access control back on for names used during
11531              template instantiation.  */
11532           pop_deferring_access_checks ();
11533           /* Do the explicit instantiation.  */
11534           do_decl_instantiation (decl, extension_specifier);
11535         }
11536       else
11537         {
11538           pop_deferring_access_checks ();
11539           /* Skip the body of the explicit instantiation.  */
11540           cp_parser_skip_to_end_of_statement (parser);
11541         }
11542     }
11543   /* We're done with the instantiation.  */
11544   end_explicit_instantiation ();
11545
11546   cp_parser_consume_semicolon_at_end_of_statement (parser);
11547 }
11548
11549 /* Parse an explicit-specialization.
11550
11551    explicit-specialization:
11552      template < > declaration
11553
11554    Although the standard says `declaration', what it really means is:
11555
11556    explicit-specialization:
11557      template <> decl-specifier [opt] init-declarator [opt] ;
11558      template <> function-definition
11559      template <> explicit-specialization
11560      template <> template-declaration  */
11561
11562 static void
11563 cp_parser_explicit_specialization (cp_parser* parser)
11564 {
11565   bool need_lang_pop;
11566   cp_token *token = cp_lexer_peek_token (parser->lexer);
11567
11568   /* Look for the `template' keyword.  */
11569   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11570   /* Look for the `<'.  */
11571   cp_parser_require (parser, CPP_LESS, "%<<%>");
11572   /* Look for the `>'.  */
11573   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11574   /* We have processed another parameter list.  */
11575   ++parser->num_template_parameter_lists;
11576   /* [temp]
11577
11578      A template ... explicit specialization ... shall not have C
11579      linkage.  */
11580   if (current_lang_name == lang_name_c)
11581     {
11582       error_at (token->location, "template specialization with C linkage");
11583       /* Give it C++ linkage to avoid confusing other parts of the
11584          front end.  */
11585       push_lang_context (lang_name_cplusplus);
11586       need_lang_pop = true;
11587     }
11588   else
11589     need_lang_pop = false;
11590   /* Let the front end know that we are beginning a specialization.  */
11591   if (!begin_specialization ())
11592     {
11593       end_specialization ();
11594       return;
11595     }
11596
11597   /* If the next keyword is `template', we need to figure out whether
11598      or not we're looking a template-declaration.  */
11599   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11600     {
11601       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11602           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11603         cp_parser_template_declaration_after_export (parser,
11604                                                      /*member_p=*/false);
11605       else
11606         cp_parser_explicit_specialization (parser);
11607     }
11608   else
11609     /* Parse the dependent declaration.  */
11610     cp_parser_single_declaration (parser,
11611                                   /*checks=*/NULL,
11612                                   /*member_p=*/false,
11613                                   /*explicit_specialization_p=*/true,
11614                                   /*friend_p=*/NULL);
11615   /* We're done with the specialization.  */
11616   end_specialization ();
11617   /* For the erroneous case of a template with C linkage, we pushed an
11618      implicit C++ linkage scope; exit that scope now.  */
11619   if (need_lang_pop)
11620     pop_lang_context ();
11621   /* We're done with this parameter list.  */
11622   --parser->num_template_parameter_lists;
11623 }
11624
11625 /* Parse a type-specifier.
11626
11627    type-specifier:
11628      simple-type-specifier
11629      class-specifier
11630      enum-specifier
11631      elaborated-type-specifier
11632      cv-qualifier
11633
11634    GNU Extension:
11635
11636    type-specifier:
11637      __complex__
11638
11639    Returns a representation of the type-specifier.  For a
11640    class-specifier, enum-specifier, or elaborated-type-specifier, a
11641    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11642
11643    The parser flags FLAGS is used to control type-specifier parsing.
11644
11645    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11646    in a decl-specifier-seq.
11647
11648    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11649    class-specifier, enum-specifier, or elaborated-type-specifier, then
11650    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11651    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11652    zero.
11653
11654    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11655    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11656    is set to FALSE.  */
11657
11658 static tree
11659 cp_parser_type_specifier (cp_parser* parser,
11660                           cp_parser_flags flags,
11661                           cp_decl_specifier_seq *decl_specs,
11662                           bool is_declaration,
11663                           int* declares_class_or_enum,
11664                           bool* is_cv_qualifier)
11665 {
11666   tree type_spec = NULL_TREE;
11667   cp_token *token;
11668   enum rid keyword;
11669   cp_decl_spec ds = ds_last;
11670
11671   /* Assume this type-specifier does not declare a new type.  */
11672   if (declares_class_or_enum)
11673     *declares_class_or_enum = 0;
11674   /* And that it does not specify a cv-qualifier.  */
11675   if (is_cv_qualifier)
11676     *is_cv_qualifier = false;
11677   /* Peek at the next token.  */
11678   token = cp_lexer_peek_token (parser->lexer);
11679
11680   /* If we're looking at a keyword, we can use that to guide the
11681      production we choose.  */
11682   keyword = token->keyword;
11683   switch (keyword)
11684     {
11685     case RID_ENUM:
11686       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11687         goto elaborated_type_specifier;
11688
11689       /* Look for the enum-specifier.  */
11690       type_spec = cp_parser_enum_specifier (parser);
11691       /* If that worked, we're done.  */
11692       if (type_spec)
11693         {
11694           if (declares_class_or_enum)
11695             *declares_class_or_enum = 2;
11696           if (decl_specs)
11697             cp_parser_set_decl_spec_type (decl_specs,
11698                                           type_spec,
11699                                           token->location,
11700                                           /*user_defined_p=*/true);
11701           return type_spec;
11702         }
11703       else
11704         goto elaborated_type_specifier;
11705
11706       /* Any of these indicate either a class-specifier, or an
11707          elaborated-type-specifier.  */
11708     case RID_CLASS:
11709     case RID_STRUCT:
11710     case RID_UNION:
11711       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11712         goto elaborated_type_specifier;
11713
11714       /* Parse tentatively so that we can back up if we don't find a
11715          class-specifier.  */
11716       cp_parser_parse_tentatively (parser);
11717       /* Look for the class-specifier.  */
11718       type_spec = cp_parser_class_specifier (parser);
11719       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11720       /* If that worked, we're done.  */
11721       if (cp_parser_parse_definitely (parser))
11722         {
11723           if (declares_class_or_enum)
11724             *declares_class_or_enum = 2;
11725           if (decl_specs)
11726             cp_parser_set_decl_spec_type (decl_specs,
11727                                           type_spec,
11728                                           token->location,
11729                                           /*user_defined_p=*/true);
11730           return type_spec;
11731         }
11732
11733       /* Fall through.  */
11734     elaborated_type_specifier:
11735       /* We're declaring (not defining) a class or enum.  */
11736       if (declares_class_or_enum)
11737         *declares_class_or_enum = 1;
11738
11739       /* Fall through.  */
11740     case RID_TYPENAME:
11741       /* Look for an elaborated-type-specifier.  */
11742       type_spec
11743         = (cp_parser_elaborated_type_specifier
11744            (parser,
11745             decl_specs && decl_specs->specs[(int) ds_friend],
11746             is_declaration));
11747       if (decl_specs)
11748         cp_parser_set_decl_spec_type (decl_specs,
11749                                       type_spec,
11750                                       token->location,
11751                                       /*user_defined_p=*/true);
11752       return type_spec;
11753
11754     case RID_CONST:
11755       ds = ds_const;
11756       if (is_cv_qualifier)
11757         *is_cv_qualifier = true;
11758       break;
11759
11760     case RID_VOLATILE:
11761       ds = ds_volatile;
11762       if (is_cv_qualifier)
11763         *is_cv_qualifier = true;
11764       break;
11765
11766     case RID_RESTRICT:
11767       ds = ds_restrict;
11768       if (is_cv_qualifier)
11769         *is_cv_qualifier = true;
11770       break;
11771
11772     case RID_COMPLEX:
11773       /* The `__complex__' keyword is a GNU extension.  */
11774       ds = ds_complex;
11775       break;
11776
11777     default:
11778       break;
11779     }
11780
11781   /* Handle simple keywords.  */
11782   if (ds != ds_last)
11783     {
11784       if (decl_specs)
11785         {
11786           ++decl_specs->specs[(int)ds];
11787           decl_specs->any_specifiers_p = true;
11788         }
11789       return cp_lexer_consume_token (parser->lexer)->u.value;
11790     }
11791
11792   /* If we do not already have a type-specifier, assume we are looking
11793      at a simple-type-specifier.  */
11794   type_spec = cp_parser_simple_type_specifier (parser,
11795                                                decl_specs,
11796                                                flags);
11797
11798   /* If we didn't find a type-specifier, and a type-specifier was not
11799      optional in this context, issue an error message.  */
11800   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11801     {
11802       cp_parser_error (parser, "expected type specifier");
11803       return error_mark_node;
11804     }
11805
11806   return type_spec;
11807 }
11808
11809 /* Parse a simple-type-specifier.
11810
11811    simple-type-specifier:
11812      :: [opt] nested-name-specifier [opt] type-name
11813      :: [opt] nested-name-specifier template template-id
11814      char
11815      wchar_t
11816      bool
11817      short
11818      int
11819      long
11820      signed
11821      unsigned
11822      float
11823      double
11824      void
11825
11826    C++0x Extension:
11827
11828    simple-type-specifier:
11829      auto
11830      decltype ( expression )   
11831      char16_t
11832      char32_t
11833
11834    GNU Extension:
11835
11836    simple-type-specifier:
11837      __typeof__ unary-expression
11838      __typeof__ ( type-id )
11839
11840    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11841    appropriately updated.  */
11842
11843 static tree
11844 cp_parser_simple_type_specifier (cp_parser* parser,
11845                                  cp_decl_specifier_seq *decl_specs,
11846                                  cp_parser_flags flags)
11847 {
11848   tree type = NULL_TREE;
11849   cp_token *token;
11850
11851   /* Peek at the next token.  */
11852   token = cp_lexer_peek_token (parser->lexer);
11853
11854   /* If we're looking at a keyword, things are easy.  */
11855   switch (token->keyword)
11856     {
11857     case RID_CHAR:
11858       if (decl_specs)
11859         decl_specs->explicit_char_p = true;
11860       type = char_type_node;
11861       break;
11862     case RID_CHAR16:
11863       type = char16_type_node;
11864       break;
11865     case RID_CHAR32:
11866       type = char32_type_node;
11867       break;
11868     case RID_WCHAR:
11869       type = wchar_type_node;
11870       break;
11871     case RID_BOOL:
11872       type = boolean_type_node;
11873       break;
11874     case RID_SHORT:
11875       if (decl_specs)
11876         ++decl_specs->specs[(int) ds_short];
11877       type = short_integer_type_node;
11878       break;
11879     case RID_INT:
11880       if (decl_specs)
11881         decl_specs->explicit_int_p = true;
11882       type = integer_type_node;
11883       break;
11884     case RID_LONG:
11885       if (decl_specs)
11886         ++decl_specs->specs[(int) ds_long];
11887       type = long_integer_type_node;
11888       break;
11889     case RID_SIGNED:
11890       if (decl_specs)
11891         ++decl_specs->specs[(int) ds_signed];
11892       type = integer_type_node;
11893       break;
11894     case RID_UNSIGNED:
11895       if (decl_specs)
11896         ++decl_specs->specs[(int) ds_unsigned];
11897       type = unsigned_type_node;
11898       break;
11899     case RID_FLOAT:
11900       type = float_type_node;
11901       break;
11902     case RID_DOUBLE:
11903       type = double_type_node;
11904       break;
11905     case RID_VOID:
11906       type = void_type_node;
11907       break;
11908       
11909     case RID_AUTO:
11910       maybe_warn_cpp0x ("C++0x auto");
11911       type = make_auto ();
11912       break;
11913
11914     case RID_DECLTYPE:
11915       /* Parse the `decltype' type.  */
11916       type = cp_parser_decltype (parser);
11917
11918       if (decl_specs)
11919         cp_parser_set_decl_spec_type (decl_specs, type,
11920                                       token->location,
11921                                       /*user_defined_p=*/true);
11922
11923       return type;
11924
11925     case RID_TYPEOF:
11926       /* Consume the `typeof' token.  */
11927       cp_lexer_consume_token (parser->lexer);
11928       /* Parse the operand to `typeof'.  */
11929       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11930       /* If it is not already a TYPE, take its type.  */
11931       if (!TYPE_P (type))
11932         type = finish_typeof (type);
11933
11934       if (decl_specs)
11935         cp_parser_set_decl_spec_type (decl_specs, type,
11936                                       token->location,
11937                                       /*user_defined_p=*/true);
11938
11939       return type;
11940
11941     default:
11942       break;
11943     }
11944
11945   /* If the type-specifier was for a built-in type, we're done.  */
11946   if (type)
11947     {
11948       tree id;
11949
11950       /* Record the type.  */
11951       if (decl_specs
11952           && (token->keyword != RID_SIGNED
11953               && token->keyword != RID_UNSIGNED
11954               && token->keyword != RID_SHORT
11955               && token->keyword != RID_LONG))
11956         cp_parser_set_decl_spec_type (decl_specs,
11957                                       type,
11958                                       token->location,
11959                                       /*user_defined=*/false);
11960       if (decl_specs)
11961         decl_specs->any_specifiers_p = true;
11962
11963       /* Consume the token.  */
11964       id = cp_lexer_consume_token (parser->lexer)->u.value;
11965
11966       /* There is no valid C++ program where a non-template type is
11967          followed by a "<".  That usually indicates that the user thought
11968          that the type was a template.  */
11969       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11970
11971       return TYPE_NAME (type);
11972     }
11973
11974   /* The type-specifier must be a user-defined type.  */
11975   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11976     {
11977       bool qualified_p;
11978       bool global_p;
11979
11980       /* Don't gobble tokens or issue error messages if this is an
11981          optional type-specifier.  */
11982       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11983         cp_parser_parse_tentatively (parser);
11984
11985       /* Look for the optional `::' operator.  */
11986       global_p
11987         = (cp_parser_global_scope_opt (parser,
11988                                        /*current_scope_valid_p=*/false)
11989            != NULL_TREE);
11990       /* Look for the nested-name specifier.  */
11991       qualified_p
11992         = (cp_parser_nested_name_specifier_opt (parser,
11993                                                 /*typename_keyword_p=*/false,
11994                                                 /*check_dependency_p=*/true,
11995                                                 /*type_p=*/false,
11996                                                 /*is_declaration=*/false)
11997            != NULL_TREE);
11998       token = cp_lexer_peek_token (parser->lexer);
11999       /* If we have seen a nested-name-specifier, and the next token
12000          is `template', then we are using the template-id production.  */
12001       if (parser->scope
12002           && cp_parser_optional_template_keyword (parser))
12003         {
12004           /* Look for the template-id.  */
12005           type = cp_parser_template_id (parser,
12006                                         /*template_keyword_p=*/true,
12007                                         /*check_dependency_p=*/true,
12008                                         /*is_declaration=*/false);
12009           /* If the template-id did not name a type, we are out of
12010              luck.  */
12011           if (TREE_CODE (type) != TYPE_DECL)
12012             {
12013               cp_parser_error (parser, "expected template-id for type");
12014               type = NULL_TREE;
12015             }
12016         }
12017       /* Otherwise, look for a type-name.  */
12018       else
12019         type = cp_parser_type_name (parser);
12020       /* Keep track of all name-lookups performed in class scopes.  */
12021       if (type
12022           && !global_p
12023           && !qualified_p
12024           && TREE_CODE (type) == TYPE_DECL
12025           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12026         maybe_note_name_used_in_class (DECL_NAME (type), type);
12027       /* If it didn't work out, we don't have a TYPE.  */
12028       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12029           && !cp_parser_parse_definitely (parser))
12030         type = NULL_TREE;
12031       if (type && decl_specs)
12032         cp_parser_set_decl_spec_type (decl_specs, type,
12033                                       token->location,
12034                                       /*user_defined=*/true);
12035     }
12036
12037   /* If we didn't get a type-name, issue an error message.  */
12038   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12039     {
12040       cp_parser_error (parser, "expected type-name");
12041       return error_mark_node;
12042     }
12043
12044   /* There is no valid C++ program where a non-template type is
12045      followed by a "<".  That usually indicates that the user thought
12046      that the type was a template.  */
12047   if (type && type != error_mark_node)
12048     {
12049       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12050          If it is, then the '<'...'>' enclose protocol names rather than
12051          template arguments, and so everything is fine.  */
12052       if (c_dialect_objc ()
12053           && (objc_is_id (type) || objc_is_class_name (type)))
12054         {
12055           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12056           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12057
12058           /* Clobber the "unqualified" type previously entered into
12059              DECL_SPECS with the new, improved protocol-qualified version.  */
12060           if (decl_specs)
12061             decl_specs->type = qual_type;
12062
12063           return qual_type;
12064         }
12065
12066       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12067                                                token->location);
12068     }
12069
12070   return type;
12071 }
12072
12073 /* Parse a type-name.
12074
12075    type-name:
12076      class-name
12077      enum-name
12078      typedef-name
12079
12080    enum-name:
12081      identifier
12082
12083    typedef-name:
12084      identifier
12085
12086    Returns a TYPE_DECL for the type.  */
12087
12088 static tree
12089 cp_parser_type_name (cp_parser* parser)
12090 {
12091   tree type_decl;
12092
12093   /* We can't know yet whether it is a class-name or not.  */
12094   cp_parser_parse_tentatively (parser);
12095   /* Try a class-name.  */
12096   type_decl = cp_parser_class_name (parser,
12097                                     /*typename_keyword_p=*/false,
12098                                     /*template_keyword_p=*/false,
12099                                     none_type,
12100                                     /*check_dependency_p=*/true,
12101                                     /*class_head_p=*/false,
12102                                     /*is_declaration=*/false);
12103   /* If it's not a class-name, keep looking.  */
12104   if (!cp_parser_parse_definitely (parser))
12105     {
12106       /* It must be a typedef-name or an enum-name.  */
12107       return cp_parser_nonclass_name (parser);
12108     }
12109
12110   return type_decl;
12111 }
12112
12113 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12114
12115    enum-name:
12116      identifier
12117
12118    typedef-name:
12119      identifier
12120
12121    Returns a TYPE_DECL for the type.  */
12122
12123 static tree
12124 cp_parser_nonclass_name (cp_parser* parser)
12125 {
12126   tree type_decl;
12127   tree identifier;
12128
12129   cp_token *token = cp_lexer_peek_token (parser->lexer);
12130   identifier = cp_parser_identifier (parser);
12131   if (identifier == error_mark_node)
12132     return error_mark_node;
12133
12134   /* Look up the type-name.  */
12135   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12136
12137   if (TREE_CODE (type_decl) != TYPE_DECL
12138       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12139     {
12140       /* See if this is an Objective-C type.  */
12141       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12142       tree type = objc_get_protocol_qualified_type (identifier, protos);
12143       if (type)
12144         type_decl = TYPE_NAME (type);
12145     }
12146   
12147   /* Issue an error if we did not find a type-name.  */
12148   if (TREE_CODE (type_decl) != TYPE_DECL)
12149     {
12150       if (!cp_parser_simulate_error (parser))
12151         cp_parser_name_lookup_error (parser, identifier, type_decl,
12152                                      "is not a type", token->location);
12153       return error_mark_node;
12154     }
12155   /* Remember that the name was used in the definition of the
12156      current class so that we can check later to see if the
12157      meaning would have been different after the class was
12158      entirely defined.  */
12159   else if (type_decl != error_mark_node
12160            && !parser->scope)
12161     maybe_note_name_used_in_class (identifier, type_decl);
12162   
12163   return type_decl;
12164 }
12165
12166 /* Parse an elaborated-type-specifier.  Note that the grammar given
12167    here incorporates the resolution to DR68.
12168
12169    elaborated-type-specifier:
12170      class-key :: [opt] nested-name-specifier [opt] identifier
12171      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12172      enum-key :: [opt] nested-name-specifier [opt] identifier
12173      typename :: [opt] nested-name-specifier identifier
12174      typename :: [opt] nested-name-specifier template [opt]
12175        template-id
12176
12177    GNU extension:
12178
12179    elaborated-type-specifier:
12180      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12181      class-key attributes :: [opt] nested-name-specifier [opt]
12182                template [opt] template-id
12183      enum attributes :: [opt] nested-name-specifier [opt] identifier
12184
12185    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12186    declared `friend'.  If IS_DECLARATION is TRUE, then this
12187    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12188    something is being declared.
12189
12190    Returns the TYPE specified.  */
12191
12192 static tree
12193 cp_parser_elaborated_type_specifier (cp_parser* parser,
12194                                      bool is_friend,
12195                                      bool is_declaration)
12196 {
12197   enum tag_types tag_type;
12198   tree identifier;
12199   tree type = NULL_TREE;
12200   tree attributes = NULL_TREE;
12201   tree globalscope;
12202   cp_token *token = NULL;
12203
12204   /* See if we're looking at the `enum' keyword.  */
12205   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12206     {
12207       /* Consume the `enum' token.  */
12208       cp_lexer_consume_token (parser->lexer);
12209       /* Remember that it's an enumeration type.  */
12210       tag_type = enum_type;
12211       /* Parse the optional `struct' or `class' key (for C++0x scoped
12212          enums).  */
12213       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12214           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12215         {
12216           if (cxx_dialect == cxx98)
12217             maybe_warn_cpp0x ("scoped enums");
12218
12219           /* Consume the `struct' or `class'.  */
12220           cp_lexer_consume_token (parser->lexer);
12221         }
12222       /* Parse the attributes.  */
12223       attributes = cp_parser_attributes_opt (parser);
12224     }
12225   /* Or, it might be `typename'.  */
12226   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12227                                            RID_TYPENAME))
12228     {
12229       /* Consume the `typename' token.  */
12230       cp_lexer_consume_token (parser->lexer);
12231       /* Remember that it's a `typename' type.  */
12232       tag_type = typename_type;
12233     }
12234   /* Otherwise it must be a class-key.  */
12235   else
12236     {
12237       tag_type = cp_parser_class_key (parser);
12238       if (tag_type == none_type)
12239         return error_mark_node;
12240       /* Parse the attributes.  */
12241       attributes = cp_parser_attributes_opt (parser);
12242     }
12243
12244   /* Look for the `::' operator.  */
12245   globalscope =  cp_parser_global_scope_opt (parser,
12246                                              /*current_scope_valid_p=*/false);
12247   /* Look for the nested-name-specifier.  */
12248   if (tag_type == typename_type && !globalscope)
12249     {
12250       if (!cp_parser_nested_name_specifier (parser,
12251                                            /*typename_keyword_p=*/true,
12252                                            /*check_dependency_p=*/true,
12253                                            /*type_p=*/true,
12254                                             is_declaration))
12255         return error_mark_node;
12256     }
12257   else
12258     /* Even though `typename' is not present, the proposed resolution
12259        to Core Issue 180 says that in `class A<T>::B', `B' should be
12260        considered a type-name, even if `A<T>' is dependent.  */
12261     cp_parser_nested_name_specifier_opt (parser,
12262                                          /*typename_keyword_p=*/true,
12263                                          /*check_dependency_p=*/true,
12264                                          /*type_p=*/true,
12265                                          is_declaration);
12266  /* For everything but enumeration types, consider a template-id.
12267     For an enumeration type, consider only a plain identifier.  */
12268   if (tag_type != enum_type)
12269     {
12270       bool template_p = false;
12271       tree decl;
12272
12273       /* Allow the `template' keyword.  */
12274       template_p = cp_parser_optional_template_keyword (parser);
12275       /* If we didn't see `template', we don't know if there's a
12276          template-id or not.  */
12277       if (!template_p)
12278         cp_parser_parse_tentatively (parser);
12279       /* Parse the template-id.  */
12280       token = cp_lexer_peek_token (parser->lexer);
12281       decl = cp_parser_template_id (parser, template_p,
12282                                     /*check_dependency_p=*/true,
12283                                     is_declaration);
12284       /* If we didn't find a template-id, look for an ordinary
12285          identifier.  */
12286       if (!template_p && !cp_parser_parse_definitely (parser))
12287         ;
12288       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12289          in effect, then we must assume that, upon instantiation, the
12290          template will correspond to a class.  */
12291       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12292                && tag_type == typename_type)
12293         type = make_typename_type (parser->scope, decl,
12294                                    typename_type,
12295                                    /*complain=*/tf_error);
12296       /* If the `typename' keyword is in effect and DECL is not a type
12297          decl. Then type is non existant.   */
12298       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12299         type = NULL_TREE; 
12300       else 
12301         type = TREE_TYPE (decl);
12302     }
12303
12304   if (!type)
12305     {
12306       token = cp_lexer_peek_token (parser->lexer);
12307       identifier = cp_parser_identifier (parser);
12308
12309       if (identifier == error_mark_node)
12310         {
12311           parser->scope = NULL_TREE;
12312           return error_mark_node;
12313         }
12314
12315       /* For a `typename', we needn't call xref_tag.  */
12316       if (tag_type == typename_type
12317           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12318         return cp_parser_make_typename_type (parser, parser->scope,
12319                                              identifier,
12320                                              token->location);
12321       /* Look up a qualified name in the usual way.  */
12322       if (parser->scope)
12323         {
12324           tree decl;
12325           tree ambiguous_decls;
12326
12327           decl = cp_parser_lookup_name (parser, identifier,
12328                                         tag_type,
12329                                         /*is_template=*/false,
12330                                         /*is_namespace=*/false,
12331                                         /*check_dependency=*/true,
12332                                         &ambiguous_decls,
12333                                         token->location);
12334
12335           /* If the lookup was ambiguous, an error will already have been
12336              issued.  */
12337           if (ambiguous_decls)
12338             return error_mark_node;
12339
12340           /* If we are parsing friend declaration, DECL may be a
12341              TEMPLATE_DECL tree node here.  However, we need to check
12342              whether this TEMPLATE_DECL results in valid code.  Consider
12343              the following example:
12344
12345                namespace N {
12346                  template <class T> class C {};
12347                }
12348                class X {
12349                  template <class T> friend class N::C; // #1, valid code
12350                };
12351                template <class T> class Y {
12352                  friend class N::C;                    // #2, invalid code
12353                };
12354
12355              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12356              name lookup of `N::C'.  We see that friend declaration must
12357              be template for the code to be valid.  Note that
12358              processing_template_decl does not work here since it is
12359              always 1 for the above two cases.  */
12360
12361           decl = (cp_parser_maybe_treat_template_as_class
12362                   (decl, /*tag_name_p=*/is_friend
12363                          && parser->num_template_parameter_lists));
12364
12365           if (TREE_CODE (decl) != TYPE_DECL)
12366             {
12367               cp_parser_diagnose_invalid_type_name (parser,
12368                                                     parser->scope,
12369                                                     identifier,
12370                                                     token->location);
12371               return error_mark_node;
12372             }
12373
12374           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12375             {
12376               bool allow_template = (parser->num_template_parameter_lists
12377                                       || DECL_SELF_REFERENCE_P (decl));
12378               type = check_elaborated_type_specifier (tag_type, decl, 
12379                                                       allow_template);
12380
12381               if (type == error_mark_node)
12382                 return error_mark_node;
12383             }
12384
12385           /* Forward declarations of nested types, such as
12386
12387                class C1::C2;
12388                class C1::C2::C3;
12389
12390              are invalid unless all components preceding the final '::'
12391              are complete.  If all enclosing types are complete, these
12392              declarations become merely pointless.
12393
12394              Invalid forward declarations of nested types are errors
12395              caught elsewhere in parsing.  Those that are pointless arrive
12396              here.  */
12397
12398           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12399               && !is_friend && !processing_explicit_instantiation)
12400             warning (0, "declaration %qD does not declare anything", decl);
12401
12402           type = TREE_TYPE (decl);
12403         }
12404       else
12405         {
12406           /* An elaborated-type-specifier sometimes introduces a new type and
12407              sometimes names an existing type.  Normally, the rule is that it
12408              introduces a new type only if there is not an existing type of
12409              the same name already in scope.  For example, given:
12410
12411                struct S {};
12412                void f() { struct S s; }
12413
12414              the `struct S' in the body of `f' is the same `struct S' as in
12415              the global scope; the existing definition is used.  However, if
12416              there were no global declaration, this would introduce a new
12417              local class named `S'.
12418
12419              An exception to this rule applies to the following code:
12420
12421                namespace N { struct S; }
12422
12423              Here, the elaborated-type-specifier names a new type
12424              unconditionally; even if there is already an `S' in the
12425              containing scope this declaration names a new type.
12426              This exception only applies if the elaborated-type-specifier
12427              forms the complete declaration:
12428
12429                [class.name]
12430
12431                A declaration consisting solely of `class-key identifier ;' is
12432                either a redeclaration of the name in the current scope or a
12433                forward declaration of the identifier as a class name.  It
12434                introduces the name into the current scope.
12435
12436              We are in this situation precisely when the next token is a `;'.
12437
12438              An exception to the exception is that a `friend' declaration does
12439              *not* name a new type; i.e., given:
12440
12441                struct S { friend struct T; };
12442
12443              `T' is not a new type in the scope of `S'.
12444
12445              Also, `new struct S' or `sizeof (struct S)' never results in the
12446              definition of a new type; a new type can only be declared in a
12447              declaration context.  */
12448
12449           tag_scope ts;
12450           bool template_p;
12451
12452           if (is_friend)
12453             /* Friends have special name lookup rules.  */
12454             ts = ts_within_enclosing_non_class;
12455           else if (is_declaration
12456                    && cp_lexer_next_token_is (parser->lexer,
12457                                               CPP_SEMICOLON))
12458             /* This is a `class-key identifier ;' */
12459             ts = ts_current;
12460           else
12461             ts = ts_global;
12462
12463           template_p =
12464             (parser->num_template_parameter_lists
12465              && (cp_parser_next_token_starts_class_definition_p (parser)
12466                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12467           /* An unqualified name was used to reference this type, so
12468              there were no qualifying templates.  */
12469           if (!cp_parser_check_template_parameters (parser,
12470                                                     /*num_templates=*/0,
12471                                                     token->location,
12472                                                     /*declarator=*/NULL))
12473             return error_mark_node;
12474           type = xref_tag (tag_type, identifier, ts, template_p);
12475         }
12476     }
12477
12478   if (type == error_mark_node)
12479     return error_mark_node;
12480
12481   /* Allow attributes on forward declarations of classes.  */
12482   if (attributes)
12483     {
12484       if (TREE_CODE (type) == TYPENAME_TYPE)
12485         warning (OPT_Wattributes,
12486                  "attributes ignored on uninstantiated type");
12487       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12488                && ! processing_explicit_instantiation)
12489         warning (OPT_Wattributes,
12490                  "attributes ignored on template instantiation");
12491       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12492         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12493       else
12494         warning (OPT_Wattributes,
12495                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12496     }
12497
12498   if (tag_type != enum_type)
12499     cp_parser_check_class_key (tag_type, type);
12500
12501   /* A "<" cannot follow an elaborated type specifier.  If that
12502      happens, the user was probably trying to form a template-id.  */
12503   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12504
12505   return type;
12506 }
12507
12508 /* Parse an enum-specifier.
12509
12510    enum-specifier:
12511      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12512
12513    enum-key:
12514      enum
12515      enum class   [C++0x]
12516      enum struct  [C++0x]
12517
12518    enum-base:   [C++0x]
12519      : type-specifier-seq
12520
12521    GNU Extensions:
12522      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12523        { enumerator-list [opt] }attributes[opt]
12524
12525    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12526    if the token stream isn't an enum-specifier after all.  */
12527
12528 static tree
12529 cp_parser_enum_specifier (cp_parser* parser)
12530 {
12531   tree identifier;
12532   tree type;
12533   tree attributes;
12534   bool scoped_enum_p = false;
12535   bool has_underlying_type = false;
12536   tree underlying_type = NULL_TREE;
12537
12538   /* Parse tentatively so that we can back up if we don't find a
12539      enum-specifier.  */
12540   cp_parser_parse_tentatively (parser);
12541
12542   /* Caller guarantees that the current token is 'enum', an identifier
12543      possibly follows, and the token after that is an opening brace.
12544      If we don't have an identifier, fabricate an anonymous name for
12545      the enumeration being defined.  */
12546   cp_lexer_consume_token (parser->lexer);
12547
12548   /* Parse the "class" or "struct", which indicates a scoped
12549      enumeration type in C++0x.  */
12550   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12551       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12552     {
12553       if (cxx_dialect == cxx98)
12554         maybe_warn_cpp0x ("scoped enums");
12555
12556       /* Consume the `struct' or `class' token.  */
12557       cp_lexer_consume_token (parser->lexer);
12558
12559       scoped_enum_p = true;
12560     }
12561
12562   attributes = cp_parser_attributes_opt (parser);
12563
12564   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12565     identifier = cp_parser_identifier (parser);
12566   else
12567     identifier = make_anon_name ();
12568
12569   /* Check for the `:' that denotes a specified underlying type in C++0x.
12570      Note that a ':' could also indicate a bitfield width, however.  */
12571   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12572     {
12573       cp_decl_specifier_seq type_specifiers;
12574
12575       /* Consume the `:'.  */
12576       cp_lexer_consume_token (parser->lexer);
12577
12578       /* Parse the type-specifier-seq.  */
12579       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12580                                     /*is_trailing_return=*/false,
12581                                     &type_specifiers);
12582
12583       /* At this point this is surely not elaborated type specifier.  */
12584       if (!cp_parser_parse_definitely (parser))
12585         return NULL_TREE;
12586
12587       if (cxx_dialect == cxx98)
12588         maybe_warn_cpp0x ("scoped enums");
12589
12590       has_underlying_type = true;
12591
12592       /* If that didn't work, stop.  */
12593       if (type_specifiers.type != error_mark_node)
12594         {
12595           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12596                                             /*initialized=*/0, NULL);
12597           if (underlying_type == error_mark_node)
12598             underlying_type = NULL_TREE;
12599         }
12600     }
12601
12602   /* Look for the `{' but don't consume it yet.  */
12603   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12604     {
12605       cp_parser_error (parser, "expected %<{%>");
12606       if (has_underlying_type)
12607         return NULL_TREE;
12608     }
12609
12610   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12611     return NULL_TREE;
12612
12613   /* Issue an error message if type-definitions are forbidden here.  */
12614   if (!cp_parser_check_type_definition (parser))
12615     type = error_mark_node;
12616   else
12617     /* Create the new type.  We do this before consuming the opening
12618        brace so the enum will be recorded as being on the line of its
12619        tag (or the 'enum' keyword, if there is no tag).  */
12620     type = start_enum (identifier, underlying_type, scoped_enum_p);
12621   
12622   /* Consume the opening brace.  */
12623   cp_lexer_consume_token (parser->lexer);
12624
12625   if (type == error_mark_node)
12626     {
12627       cp_parser_skip_to_end_of_block_or_statement (parser);
12628       return error_mark_node;
12629     }
12630
12631   /* If the next token is not '}', then there are some enumerators.  */
12632   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12633     cp_parser_enumerator_list (parser, type);
12634
12635   /* Consume the final '}'.  */
12636   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12637
12638   /* Look for trailing attributes to apply to this enumeration, and
12639      apply them if appropriate.  */
12640   if (cp_parser_allow_gnu_extensions_p (parser))
12641     {
12642       tree trailing_attr = cp_parser_attributes_opt (parser);
12643       trailing_attr = chainon (trailing_attr, attributes);
12644       cplus_decl_attributes (&type,
12645                              trailing_attr,
12646                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12647     }
12648
12649   /* Finish up the enumeration.  */
12650   finish_enum (type);
12651
12652   return type;
12653 }
12654
12655 /* Parse an enumerator-list.  The enumerators all have the indicated
12656    TYPE.
12657
12658    enumerator-list:
12659      enumerator-definition
12660      enumerator-list , enumerator-definition  */
12661
12662 static void
12663 cp_parser_enumerator_list (cp_parser* parser, tree type)
12664 {
12665   while (true)
12666     {
12667       /* Parse an enumerator-definition.  */
12668       cp_parser_enumerator_definition (parser, type);
12669
12670       /* If the next token is not a ',', we've reached the end of
12671          the list.  */
12672       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12673         break;
12674       /* Otherwise, consume the `,' and keep going.  */
12675       cp_lexer_consume_token (parser->lexer);
12676       /* If the next token is a `}', there is a trailing comma.  */
12677       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12678         {
12679           if (!in_system_header)
12680             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12681           break;
12682         }
12683     }
12684 }
12685
12686 /* Parse an enumerator-definition.  The enumerator has the indicated
12687    TYPE.
12688
12689    enumerator-definition:
12690      enumerator
12691      enumerator = constant-expression
12692
12693    enumerator:
12694      identifier  */
12695
12696 static void
12697 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12698 {
12699   tree identifier;
12700   tree value;
12701
12702   /* Look for the identifier.  */
12703   identifier = cp_parser_identifier (parser);
12704   if (identifier == error_mark_node)
12705     return;
12706
12707   /* If the next token is an '=', then there is an explicit value.  */
12708   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12709     {
12710       /* Consume the `=' token.  */
12711       cp_lexer_consume_token (parser->lexer);
12712       /* Parse the value.  */
12713       value = cp_parser_constant_expression (parser,
12714                                              /*allow_non_constant_p=*/false,
12715                                              NULL);
12716     }
12717   else
12718     value = NULL_TREE;
12719
12720   /* If we are processing a template, make sure the initializer of the
12721      enumerator doesn't contain any bare template parameter pack.  */
12722   if (check_for_bare_parameter_packs (value))
12723     value = error_mark_node;
12724
12725   /* Create the enumerator.  */
12726   build_enumerator (identifier, value, type);
12727 }
12728
12729 /* Parse a namespace-name.
12730
12731    namespace-name:
12732      original-namespace-name
12733      namespace-alias
12734
12735    Returns the NAMESPACE_DECL for the namespace.  */
12736
12737 static tree
12738 cp_parser_namespace_name (cp_parser* parser)
12739 {
12740   tree identifier;
12741   tree namespace_decl;
12742
12743   cp_token *token = cp_lexer_peek_token (parser->lexer);
12744
12745   /* Get the name of the namespace.  */
12746   identifier = cp_parser_identifier (parser);
12747   if (identifier == error_mark_node)
12748     return error_mark_node;
12749
12750   /* Look up the identifier in the currently active scope.  Look only
12751      for namespaces, due to:
12752
12753        [basic.lookup.udir]
12754
12755        When looking up a namespace-name in a using-directive or alias
12756        definition, only namespace names are considered.
12757
12758      And:
12759
12760        [basic.lookup.qual]
12761
12762        During the lookup of a name preceding the :: scope resolution
12763        operator, object, function, and enumerator names are ignored.
12764
12765      (Note that cp_parser_qualifying_entity only calls this
12766      function if the token after the name is the scope resolution
12767      operator.)  */
12768   namespace_decl = cp_parser_lookup_name (parser, identifier,
12769                                           none_type,
12770                                           /*is_template=*/false,
12771                                           /*is_namespace=*/true,
12772                                           /*check_dependency=*/true,
12773                                           /*ambiguous_decls=*/NULL,
12774                                           token->location);
12775   /* If it's not a namespace, issue an error.  */
12776   if (namespace_decl == error_mark_node
12777       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12778     {
12779       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12780         error_at (token->location, "%qD is not a namespace-name", identifier);
12781       cp_parser_error (parser, "expected namespace-name");
12782       namespace_decl = error_mark_node;
12783     }
12784
12785   return namespace_decl;
12786 }
12787
12788 /* Parse a namespace-definition.
12789
12790    namespace-definition:
12791      named-namespace-definition
12792      unnamed-namespace-definition
12793
12794    named-namespace-definition:
12795      original-namespace-definition
12796      extension-namespace-definition
12797
12798    original-namespace-definition:
12799      namespace identifier { namespace-body }
12800
12801    extension-namespace-definition:
12802      namespace original-namespace-name { namespace-body }
12803
12804    unnamed-namespace-definition:
12805      namespace { namespace-body } */
12806
12807 static void
12808 cp_parser_namespace_definition (cp_parser* parser)
12809 {
12810   tree identifier, attribs;
12811   bool has_visibility;
12812   bool is_inline;
12813
12814   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12815     {
12816       is_inline = true;
12817       cp_lexer_consume_token (parser->lexer);
12818     }
12819   else
12820     is_inline = false;
12821
12822   /* Look for the `namespace' keyword.  */
12823   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12824
12825   /* Get the name of the namespace.  We do not attempt to distinguish
12826      between an original-namespace-definition and an
12827      extension-namespace-definition at this point.  The semantic
12828      analysis routines are responsible for that.  */
12829   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12830     identifier = cp_parser_identifier (parser);
12831   else
12832     identifier = NULL_TREE;
12833
12834   /* Parse any specified attributes.  */
12835   attribs = cp_parser_attributes_opt (parser);
12836
12837   /* Look for the `{' to start the namespace.  */
12838   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12839   /* Start the namespace.  */
12840   push_namespace (identifier);
12841
12842   /* "inline namespace" is equivalent to a stub namespace definition
12843      followed by a strong using directive.  */
12844   if (is_inline)
12845     {
12846       tree name_space = current_namespace;
12847       /* Set up namespace association.  */
12848       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12849         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12850                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12851       /* Import the contents of the inline namespace.  */
12852       pop_namespace ();
12853       do_using_directive (name_space);
12854       push_namespace (identifier);
12855     }
12856
12857   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12858
12859   /* Parse the body of the namespace.  */
12860   cp_parser_namespace_body (parser);
12861
12862 #ifdef HANDLE_PRAGMA_VISIBILITY
12863   if (has_visibility)
12864     pop_visibility (1);
12865 #endif
12866
12867   /* Finish the namespace.  */
12868   pop_namespace ();
12869   /* Look for the final `}'.  */
12870   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12871 }
12872
12873 /* Parse a namespace-body.
12874
12875    namespace-body:
12876      declaration-seq [opt]  */
12877
12878 static void
12879 cp_parser_namespace_body (cp_parser* parser)
12880 {
12881   cp_parser_declaration_seq_opt (parser);
12882 }
12883
12884 /* Parse a namespace-alias-definition.
12885
12886    namespace-alias-definition:
12887      namespace identifier = qualified-namespace-specifier ;  */
12888
12889 static void
12890 cp_parser_namespace_alias_definition (cp_parser* parser)
12891 {
12892   tree identifier;
12893   tree namespace_specifier;
12894
12895   cp_token *token = cp_lexer_peek_token (parser->lexer);
12896
12897   /* Look for the `namespace' keyword.  */
12898   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12899   /* Look for the identifier.  */
12900   identifier = cp_parser_identifier (parser);
12901   if (identifier == error_mark_node)
12902     return;
12903   /* Look for the `=' token.  */
12904   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12905       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12906     {
12907       error_at (token->location, "%<namespace%> definition is not allowed here");
12908       /* Skip the definition.  */
12909       cp_lexer_consume_token (parser->lexer);
12910       if (cp_parser_skip_to_closing_brace (parser))
12911         cp_lexer_consume_token (parser->lexer);
12912       return;
12913     }
12914   cp_parser_require (parser, CPP_EQ, "%<=%>");
12915   /* Look for the qualified-namespace-specifier.  */
12916   namespace_specifier
12917     = cp_parser_qualified_namespace_specifier (parser);
12918   /* Look for the `;' token.  */
12919   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12920
12921   /* Register the alias in the symbol table.  */
12922   do_namespace_alias (identifier, namespace_specifier);
12923 }
12924
12925 /* Parse a qualified-namespace-specifier.
12926
12927    qualified-namespace-specifier:
12928      :: [opt] nested-name-specifier [opt] namespace-name
12929
12930    Returns a NAMESPACE_DECL corresponding to the specified
12931    namespace.  */
12932
12933 static tree
12934 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12935 {
12936   /* Look for the optional `::'.  */
12937   cp_parser_global_scope_opt (parser,
12938                               /*current_scope_valid_p=*/false);
12939
12940   /* Look for the optional nested-name-specifier.  */
12941   cp_parser_nested_name_specifier_opt (parser,
12942                                        /*typename_keyword_p=*/false,
12943                                        /*check_dependency_p=*/true,
12944                                        /*type_p=*/false,
12945                                        /*is_declaration=*/true);
12946
12947   return cp_parser_namespace_name (parser);
12948 }
12949
12950 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12951    access declaration.
12952
12953    using-declaration:
12954      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12955      using :: unqualified-id ;  
12956
12957    access-declaration:
12958      qualified-id ;  
12959
12960    */
12961
12962 static bool
12963 cp_parser_using_declaration (cp_parser* parser, 
12964                              bool access_declaration_p)
12965 {
12966   cp_token *token;
12967   bool typename_p = false;
12968   bool global_scope_p;
12969   tree decl;
12970   tree identifier;
12971   tree qscope;
12972
12973   if (access_declaration_p)
12974     cp_parser_parse_tentatively (parser);
12975   else
12976     {
12977       /* Look for the `using' keyword.  */
12978       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12979       
12980       /* Peek at the next token.  */
12981       token = cp_lexer_peek_token (parser->lexer);
12982       /* See if it's `typename'.  */
12983       if (token->keyword == RID_TYPENAME)
12984         {
12985           /* Remember that we've seen it.  */
12986           typename_p = true;
12987           /* Consume the `typename' token.  */
12988           cp_lexer_consume_token (parser->lexer);
12989         }
12990     }
12991
12992   /* Look for the optional global scope qualification.  */
12993   global_scope_p
12994     = (cp_parser_global_scope_opt (parser,
12995                                    /*current_scope_valid_p=*/false)
12996        != NULL_TREE);
12997
12998   /* If we saw `typename', or didn't see `::', then there must be a
12999      nested-name-specifier present.  */
13000   if (typename_p || !global_scope_p)
13001     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13002                                               /*check_dependency_p=*/true,
13003                                               /*type_p=*/false,
13004                                               /*is_declaration=*/true);
13005   /* Otherwise, we could be in either of the two productions.  In that
13006      case, treat the nested-name-specifier as optional.  */
13007   else
13008     qscope = cp_parser_nested_name_specifier_opt (parser,
13009                                                   /*typename_keyword_p=*/false,
13010                                                   /*check_dependency_p=*/true,
13011                                                   /*type_p=*/false,
13012                                                   /*is_declaration=*/true);
13013   if (!qscope)
13014     qscope = global_namespace;
13015
13016   if (access_declaration_p && cp_parser_error_occurred (parser))
13017     /* Something has already gone wrong; there's no need to parse
13018        further.  Since an error has occurred, the return value of
13019        cp_parser_parse_definitely will be false, as required.  */
13020     return cp_parser_parse_definitely (parser);
13021
13022   token = cp_lexer_peek_token (parser->lexer);
13023   /* Parse the unqualified-id.  */
13024   identifier = cp_parser_unqualified_id (parser,
13025                                          /*template_keyword_p=*/false,
13026                                          /*check_dependency_p=*/true,
13027                                          /*declarator_p=*/true,
13028                                          /*optional_p=*/false);
13029
13030   if (access_declaration_p)
13031     {
13032       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13033         cp_parser_simulate_error (parser);
13034       if (!cp_parser_parse_definitely (parser))
13035         return false;
13036     }
13037
13038   /* The function we call to handle a using-declaration is different
13039      depending on what scope we are in.  */
13040   if (qscope == error_mark_node || identifier == error_mark_node)
13041     ;
13042   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13043            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13044     /* [namespace.udecl]
13045
13046        A using declaration shall not name a template-id.  */
13047     error_at (token->location,
13048               "a template-id may not appear in a using-declaration");
13049   else
13050     {
13051       if (at_class_scope_p ())
13052         {
13053           /* Create the USING_DECL.  */
13054           decl = do_class_using_decl (parser->scope, identifier);
13055
13056           if (check_for_bare_parameter_packs (decl))
13057             return false;
13058           else
13059             /* Add it to the list of members in this class.  */
13060             finish_member_declaration (decl);
13061         }
13062       else
13063         {
13064           decl = cp_parser_lookup_name_simple (parser,
13065                                                identifier,
13066                                                token->location);
13067           if (decl == error_mark_node)
13068             cp_parser_name_lookup_error (parser, identifier,
13069                                          decl, NULL,
13070                                          token->location);
13071           else if (check_for_bare_parameter_packs (decl))
13072             return false;
13073           else if (!at_namespace_scope_p ())
13074             do_local_using_decl (decl, qscope, identifier);
13075           else
13076             do_toplevel_using_decl (decl, qscope, identifier);
13077         }
13078     }
13079
13080   /* Look for the final `;'.  */
13081   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13082   
13083   return true;
13084 }
13085
13086 /* Parse a using-directive.
13087
13088    using-directive:
13089      using namespace :: [opt] nested-name-specifier [opt]
13090        namespace-name ;  */
13091
13092 static void
13093 cp_parser_using_directive (cp_parser* parser)
13094 {
13095   tree namespace_decl;
13096   tree attribs;
13097
13098   /* Look for the `using' keyword.  */
13099   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13100   /* And the `namespace' keyword.  */
13101   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13102   /* Look for the optional `::' operator.  */
13103   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13104   /* And the optional nested-name-specifier.  */
13105   cp_parser_nested_name_specifier_opt (parser,
13106                                        /*typename_keyword_p=*/false,
13107                                        /*check_dependency_p=*/true,
13108                                        /*type_p=*/false,
13109                                        /*is_declaration=*/true);
13110   /* Get the namespace being used.  */
13111   namespace_decl = cp_parser_namespace_name (parser);
13112   /* And any specified attributes.  */
13113   attribs = cp_parser_attributes_opt (parser);
13114   /* Update the symbol table.  */
13115   parse_using_directive (namespace_decl, attribs);
13116   /* Look for the final `;'.  */
13117   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13118 }
13119
13120 /* Parse an asm-definition.
13121
13122    asm-definition:
13123      asm ( string-literal ) ;
13124
13125    GNU Extension:
13126
13127    asm-definition:
13128      asm volatile [opt] ( string-literal ) ;
13129      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13130      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13131                           : asm-operand-list [opt] ) ;
13132      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13133                           : asm-operand-list [opt]
13134                           : asm-clobber-list [opt] ) ;
13135      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13136                                : asm-clobber-list [opt]
13137                                : asm-goto-list ) ;  */
13138
13139 static void
13140 cp_parser_asm_definition (cp_parser* parser)
13141 {
13142   tree string;
13143   tree outputs = NULL_TREE;
13144   tree inputs = NULL_TREE;
13145   tree clobbers = NULL_TREE;
13146   tree labels = NULL_TREE;
13147   tree asm_stmt;
13148   bool volatile_p = false;
13149   bool extended_p = false;
13150   bool invalid_inputs_p = false;
13151   bool invalid_outputs_p = false;
13152   bool goto_p = false;
13153   const char *missing = NULL;
13154
13155   /* Look for the `asm' keyword.  */
13156   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13157   /* See if the next token is `volatile'.  */
13158   if (cp_parser_allow_gnu_extensions_p (parser)
13159       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13160     {
13161       /* Remember that we saw the `volatile' keyword.  */
13162       volatile_p = true;
13163       /* Consume the token.  */
13164       cp_lexer_consume_token (parser->lexer);
13165     }
13166   if (cp_parser_allow_gnu_extensions_p (parser)
13167       && parser->in_function_body
13168       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13169     {
13170       /* Remember that we saw the `goto' keyword.  */
13171       goto_p = true;
13172       /* Consume the token.  */
13173       cp_lexer_consume_token (parser->lexer);
13174     }
13175   /* Look for the opening `('.  */
13176   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13177     return;
13178   /* Look for the string.  */
13179   string = cp_parser_string_literal (parser, false, false);
13180   if (string == error_mark_node)
13181     {
13182       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13183                                              /*consume_paren=*/true);
13184       return;
13185     }
13186
13187   /* If we're allowing GNU extensions, check for the extended assembly
13188      syntax.  Unfortunately, the `:' tokens need not be separated by
13189      a space in C, and so, for compatibility, we tolerate that here
13190      too.  Doing that means that we have to treat the `::' operator as
13191      two `:' tokens.  */
13192   if (cp_parser_allow_gnu_extensions_p (parser)
13193       && parser->in_function_body
13194       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13195           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13196     {
13197       bool inputs_p = false;
13198       bool clobbers_p = false;
13199       bool labels_p = false;
13200
13201       /* The extended syntax was used.  */
13202       extended_p = true;
13203
13204       /* Look for outputs.  */
13205       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13206         {
13207           /* Consume the `:'.  */
13208           cp_lexer_consume_token (parser->lexer);
13209           /* Parse the output-operands.  */
13210           if (cp_lexer_next_token_is_not (parser->lexer,
13211                                           CPP_COLON)
13212               && cp_lexer_next_token_is_not (parser->lexer,
13213                                              CPP_SCOPE)
13214               && cp_lexer_next_token_is_not (parser->lexer,
13215                                              CPP_CLOSE_PAREN)
13216               && !goto_p)
13217             outputs = cp_parser_asm_operand_list (parser);
13218
13219             if (outputs == error_mark_node)
13220               invalid_outputs_p = true;
13221         }
13222       /* If the next token is `::', there are no outputs, and the
13223          next token is the beginning of the inputs.  */
13224       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13225         /* The inputs are coming next.  */
13226         inputs_p = true;
13227
13228       /* Look for inputs.  */
13229       if (inputs_p
13230           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13231         {
13232           /* Consume the `:' or `::'.  */
13233           cp_lexer_consume_token (parser->lexer);
13234           /* Parse the output-operands.  */
13235           if (cp_lexer_next_token_is_not (parser->lexer,
13236                                           CPP_COLON)
13237               && cp_lexer_next_token_is_not (parser->lexer,
13238                                              CPP_SCOPE)
13239               && cp_lexer_next_token_is_not (parser->lexer,
13240                                              CPP_CLOSE_PAREN))
13241             inputs = cp_parser_asm_operand_list (parser);
13242
13243             if (inputs == error_mark_node)
13244               invalid_inputs_p = true;
13245         }
13246       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13247         /* The clobbers are coming next.  */
13248         clobbers_p = true;
13249
13250       /* Look for clobbers.  */
13251       if (clobbers_p
13252           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13253         {
13254           clobbers_p = true;
13255           /* Consume the `:' or `::'.  */
13256           cp_lexer_consume_token (parser->lexer);
13257           /* Parse the clobbers.  */
13258           if (cp_lexer_next_token_is_not (parser->lexer,
13259                                           CPP_COLON)
13260               && cp_lexer_next_token_is_not (parser->lexer,
13261                                              CPP_CLOSE_PAREN))
13262             clobbers = cp_parser_asm_clobber_list (parser);
13263         }
13264       else if (goto_p
13265                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13266         /* The labels are coming next.  */
13267         labels_p = true;
13268
13269       /* Look for labels.  */
13270       if (labels_p
13271           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13272         {
13273           labels_p = true;
13274           /* Consume the `:' or `::'.  */
13275           cp_lexer_consume_token (parser->lexer);
13276           /* Parse the labels.  */
13277           labels = cp_parser_asm_label_list (parser);
13278         }
13279
13280       if (goto_p && !labels_p)
13281         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13282     }
13283   else if (goto_p)
13284     missing = "%<:%> or %<::%>";
13285
13286   /* Look for the closing `)'.  */
13287   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13288                           missing ? missing : "%<)%>"))
13289     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13290                                            /*consume_paren=*/true);
13291   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13292
13293   if (!invalid_inputs_p && !invalid_outputs_p)
13294     {
13295       /* Create the ASM_EXPR.  */
13296       if (parser->in_function_body)
13297         {
13298           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13299                                       inputs, clobbers, labels);
13300           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13301           if (!extended_p)
13302             {
13303               tree temp = asm_stmt;
13304               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13305                 temp = TREE_OPERAND (temp, 0);
13306
13307               ASM_INPUT_P (temp) = 1;
13308             }
13309         }
13310       else
13311         cgraph_add_asm_node (string);
13312     }
13313 }
13314
13315 /* Declarators [gram.dcl.decl] */
13316
13317 /* Parse an init-declarator.
13318
13319    init-declarator:
13320      declarator initializer [opt]
13321
13322    GNU Extension:
13323
13324    init-declarator:
13325      declarator asm-specification [opt] attributes [opt] initializer [opt]
13326
13327    function-definition:
13328      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13329        function-body
13330      decl-specifier-seq [opt] declarator function-try-block
13331
13332    GNU Extension:
13333
13334    function-definition:
13335      __extension__ function-definition
13336
13337    The DECL_SPECIFIERS apply to this declarator.  Returns a
13338    representation of the entity declared.  If MEMBER_P is TRUE, then
13339    this declarator appears in a class scope.  The new DECL created by
13340    this declarator is returned.
13341
13342    The CHECKS are access checks that should be performed once we know
13343    what entity is being declared (and, therefore, what classes have
13344    befriended it).
13345
13346    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13347    for a function-definition here as well.  If the declarator is a
13348    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13349    be TRUE upon return.  By that point, the function-definition will
13350    have been completely parsed.
13351
13352    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13353    is FALSE.  */
13354
13355 static tree
13356 cp_parser_init_declarator (cp_parser* parser,
13357                            cp_decl_specifier_seq *decl_specifiers,
13358                            VEC (deferred_access_check,gc)* checks,
13359                            bool function_definition_allowed_p,
13360                            bool member_p,
13361                            int declares_class_or_enum,
13362                            bool* function_definition_p)
13363 {
13364   cp_token *token = NULL, *asm_spec_start_token = NULL,
13365            *attributes_start_token = NULL;
13366   cp_declarator *declarator;
13367   tree prefix_attributes;
13368   tree attributes;
13369   tree asm_specification;
13370   tree initializer;
13371   tree decl = NULL_TREE;
13372   tree scope;
13373   int is_initialized;
13374   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13375      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13376      "(...)".  */
13377   enum cpp_ttype initialization_kind;
13378   bool is_direct_init = false;
13379   bool is_non_constant_init;
13380   int ctor_dtor_or_conv_p;
13381   bool friend_p;
13382   tree pushed_scope = NULL;
13383
13384   /* Gather the attributes that were provided with the
13385      decl-specifiers.  */
13386   prefix_attributes = decl_specifiers->attributes;
13387
13388   /* Assume that this is not the declarator for a function
13389      definition.  */
13390   if (function_definition_p)
13391     *function_definition_p = false;
13392
13393   /* Defer access checks while parsing the declarator; we cannot know
13394      what names are accessible until we know what is being
13395      declared.  */
13396   resume_deferring_access_checks ();
13397
13398   /* Parse the declarator.  */
13399   token = cp_lexer_peek_token (parser->lexer);
13400   declarator
13401     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13402                             &ctor_dtor_or_conv_p,
13403                             /*parenthesized_p=*/NULL,
13404                             /*member_p=*/false);
13405   /* Gather up the deferred checks.  */
13406   stop_deferring_access_checks ();
13407
13408   /* If the DECLARATOR was erroneous, there's no need to go
13409      further.  */
13410   if (declarator == cp_error_declarator)
13411     return error_mark_node;
13412
13413   /* Check that the number of template-parameter-lists is OK.  */
13414   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13415                                                        token->location))
13416     return error_mark_node;
13417
13418   if (declares_class_or_enum & 2)
13419     cp_parser_check_for_definition_in_return_type (declarator,
13420                                                    decl_specifiers->type,
13421                                                    decl_specifiers->type_location);
13422
13423   /* Figure out what scope the entity declared by the DECLARATOR is
13424      located in.  `grokdeclarator' sometimes changes the scope, so
13425      we compute it now.  */
13426   scope = get_scope_of_declarator (declarator);
13427
13428   /* If we're allowing GNU extensions, look for an asm-specification
13429      and attributes.  */
13430   if (cp_parser_allow_gnu_extensions_p (parser))
13431     {
13432       /* Look for an asm-specification.  */
13433       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13434       asm_specification = cp_parser_asm_specification_opt (parser);
13435       /* And attributes.  */
13436       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13437       attributes = cp_parser_attributes_opt (parser);
13438     }
13439   else
13440     {
13441       asm_specification = NULL_TREE;
13442       attributes = NULL_TREE;
13443     }
13444
13445   /* Peek at the next token.  */
13446   token = cp_lexer_peek_token (parser->lexer);
13447   /* Check to see if the token indicates the start of a
13448      function-definition.  */
13449   if (function_declarator_p (declarator)
13450       && cp_parser_token_starts_function_definition_p (token))
13451     {
13452       if (!function_definition_allowed_p)
13453         {
13454           /* If a function-definition should not appear here, issue an
13455              error message.  */
13456           cp_parser_error (parser,
13457                            "a function-definition is not allowed here");
13458           return error_mark_node;
13459         }
13460       else
13461         {
13462           location_t func_brace_location
13463             = cp_lexer_peek_token (parser->lexer)->location;
13464
13465           /* Neither attributes nor an asm-specification are allowed
13466              on a function-definition.  */
13467           if (asm_specification)
13468             error_at (asm_spec_start_token->location,
13469                       "an asm-specification is not allowed "
13470                       "on a function-definition");
13471           if (attributes)
13472             error_at (attributes_start_token->location,
13473                       "attributes are not allowed on a function-definition");
13474           /* This is a function-definition.  */
13475           *function_definition_p = true;
13476
13477           /* Parse the function definition.  */
13478           if (member_p)
13479             decl = cp_parser_save_member_function_body (parser,
13480                                                         decl_specifiers,
13481                                                         declarator,
13482                                                         prefix_attributes);
13483           else
13484             decl
13485               = (cp_parser_function_definition_from_specifiers_and_declarator
13486                  (parser, decl_specifiers, prefix_attributes, declarator));
13487
13488           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13489             {
13490               /* This is where the prologue starts...  */
13491               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13492                 = func_brace_location;
13493             }
13494
13495           return decl;
13496         }
13497     }
13498
13499   /* [dcl.dcl]
13500
13501      Only in function declarations for constructors, destructors, and
13502      type conversions can the decl-specifier-seq be omitted.
13503
13504      We explicitly postpone this check past the point where we handle
13505      function-definitions because we tolerate function-definitions
13506      that are missing their return types in some modes.  */
13507   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13508     {
13509       cp_parser_error (parser,
13510                        "expected constructor, destructor, or type conversion");
13511       return error_mark_node;
13512     }
13513
13514   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13515   if (token->type == CPP_EQ
13516       || token->type == CPP_OPEN_PAREN
13517       || token->type == CPP_OPEN_BRACE)
13518     {
13519       is_initialized = SD_INITIALIZED;
13520       initialization_kind = token->type;
13521
13522       if (token->type == CPP_EQ
13523           && function_declarator_p (declarator))
13524         {
13525           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13526           if (t2->keyword == RID_DEFAULT)
13527             is_initialized = SD_DEFAULTED;
13528           else if (t2->keyword == RID_DELETE)
13529             is_initialized = SD_DELETED;
13530         }
13531     }
13532   else
13533     {
13534       /* If the init-declarator isn't initialized and isn't followed by a
13535          `,' or `;', it's not a valid init-declarator.  */
13536       if (token->type != CPP_COMMA
13537           && token->type != CPP_SEMICOLON)
13538         {
13539           cp_parser_error (parser, "expected initializer");
13540           return error_mark_node;
13541         }
13542       is_initialized = SD_UNINITIALIZED;
13543       initialization_kind = CPP_EOF;
13544     }
13545
13546   /* Because start_decl has side-effects, we should only call it if we
13547      know we're going ahead.  By this point, we know that we cannot
13548      possibly be looking at any other construct.  */
13549   cp_parser_commit_to_tentative_parse (parser);
13550
13551   /* If the decl specifiers were bad, issue an error now that we're
13552      sure this was intended to be a declarator.  Then continue
13553      declaring the variable(s), as int, to try to cut down on further
13554      errors.  */
13555   if (decl_specifiers->any_specifiers_p
13556       && decl_specifiers->type == error_mark_node)
13557     {
13558       cp_parser_error (parser, "invalid type in declaration");
13559       decl_specifiers->type = integer_type_node;
13560     }
13561
13562   /* Check to see whether or not this declaration is a friend.  */
13563   friend_p = cp_parser_friend_p (decl_specifiers);
13564
13565   /* Enter the newly declared entry in the symbol table.  If we're
13566      processing a declaration in a class-specifier, we wait until
13567      after processing the initializer.  */
13568   if (!member_p)
13569     {
13570       if (parser->in_unbraced_linkage_specification_p)
13571         decl_specifiers->storage_class = sc_extern;
13572       decl = start_decl (declarator, decl_specifiers,
13573                          is_initialized, attributes, prefix_attributes,
13574                          &pushed_scope);
13575     }
13576   else if (scope)
13577     /* Enter the SCOPE.  That way unqualified names appearing in the
13578        initializer will be looked up in SCOPE.  */
13579     pushed_scope = push_scope (scope);
13580
13581   /* Perform deferred access control checks, now that we know in which
13582      SCOPE the declared entity resides.  */
13583   if (!member_p && decl)
13584     {
13585       tree saved_current_function_decl = NULL_TREE;
13586
13587       /* If the entity being declared is a function, pretend that we
13588          are in its scope.  If it is a `friend', it may have access to
13589          things that would not otherwise be accessible.  */
13590       if (TREE_CODE (decl) == FUNCTION_DECL)
13591         {
13592           saved_current_function_decl = current_function_decl;
13593           current_function_decl = decl;
13594         }
13595
13596       /* Perform access checks for template parameters.  */
13597       cp_parser_perform_template_parameter_access_checks (checks);
13598
13599       /* Perform the access control checks for the declarator and the
13600          decl-specifiers.  */
13601       perform_deferred_access_checks ();
13602
13603       /* Restore the saved value.  */
13604       if (TREE_CODE (decl) == FUNCTION_DECL)
13605         current_function_decl = saved_current_function_decl;
13606     }
13607
13608   /* Parse the initializer.  */
13609   initializer = NULL_TREE;
13610   is_direct_init = false;
13611   is_non_constant_init = true;
13612   if (is_initialized)
13613     {
13614       if (function_declarator_p (declarator))
13615         {
13616           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13617            if (initialization_kind == CPP_EQ)
13618              initializer = cp_parser_pure_specifier (parser);
13619            else
13620              {
13621                /* If the declaration was erroneous, we don't really
13622                   know what the user intended, so just silently
13623                   consume the initializer.  */
13624                if (decl != error_mark_node)
13625                  error_at (initializer_start_token->location,
13626                            "initializer provided for function");
13627                cp_parser_skip_to_closing_parenthesis (parser,
13628                                                       /*recovering=*/true,
13629                                                       /*or_comma=*/false,
13630                                                       /*consume_paren=*/true);
13631              }
13632         }
13633       else
13634         {
13635           /* We want to record the extra mangling scope for in-class
13636              initializers of class members and initializers of static data
13637              member templates.  The former is a C++0x feature which isn't
13638              implemented yet, and I expect it will involve deferring
13639              parsing of the initializer until end of class as with default
13640              arguments.  So right here we only handle the latter.  */
13641           if (!member_p && processing_template_decl)
13642             start_lambda_scope (decl);
13643           initializer = cp_parser_initializer (parser,
13644                                                &is_direct_init,
13645                                                &is_non_constant_init);
13646           if (!member_p && processing_template_decl)
13647             finish_lambda_scope ();
13648         }
13649     }
13650
13651   /* The old parser allows attributes to appear after a parenthesized
13652      initializer.  Mark Mitchell proposed removing this functionality
13653      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13654      attributes -- but ignores them.  */
13655   if (cp_parser_allow_gnu_extensions_p (parser)
13656       && initialization_kind == CPP_OPEN_PAREN)
13657     if (cp_parser_attributes_opt (parser))
13658       warning (OPT_Wattributes,
13659                "attributes after parenthesized initializer ignored");
13660
13661   /* For an in-class declaration, use `grokfield' to create the
13662      declaration.  */
13663   if (member_p)
13664     {
13665       if (pushed_scope)
13666         {
13667           pop_scope (pushed_scope);
13668           pushed_scope = false;
13669         }
13670       decl = grokfield (declarator, decl_specifiers,
13671                         initializer, !is_non_constant_init,
13672                         /*asmspec=*/NULL_TREE,
13673                         prefix_attributes);
13674       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13675         cp_parser_save_default_args (parser, decl);
13676     }
13677
13678   /* Finish processing the declaration.  But, skip friend
13679      declarations.  */
13680   if (!friend_p && decl && decl != error_mark_node)
13681     {
13682       cp_finish_decl (decl,
13683                       initializer, !is_non_constant_init,
13684                       asm_specification,
13685                       /* If the initializer is in parentheses, then this is
13686                          a direct-initialization, which means that an
13687                          `explicit' constructor is OK.  Otherwise, an
13688                          `explicit' constructor cannot be used.  */
13689                       ((is_direct_init || !is_initialized)
13690                        ? 0 : LOOKUP_ONLYCONVERTING));
13691     }
13692   else if ((cxx_dialect != cxx98) && friend_p
13693            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13694     /* Core issue #226 (C++0x only): A default template-argument
13695        shall not be specified in a friend class template
13696        declaration. */
13697     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13698                              /*is_partial=*/0, /*is_friend_decl=*/1);
13699
13700   if (!friend_p && pushed_scope)
13701     pop_scope (pushed_scope);
13702
13703   return decl;
13704 }
13705
13706 /* Parse a declarator.
13707
13708    declarator:
13709      direct-declarator
13710      ptr-operator declarator
13711
13712    abstract-declarator:
13713      ptr-operator abstract-declarator [opt]
13714      direct-abstract-declarator
13715
13716    GNU Extensions:
13717
13718    declarator:
13719      attributes [opt] direct-declarator
13720      attributes [opt] ptr-operator declarator
13721
13722    abstract-declarator:
13723      attributes [opt] ptr-operator abstract-declarator [opt]
13724      attributes [opt] direct-abstract-declarator
13725
13726    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13727    detect constructor, destructor or conversion operators. It is set
13728    to -1 if the declarator is a name, and +1 if it is a
13729    function. Otherwise it is set to zero. Usually you just want to
13730    test for >0, but internally the negative value is used.
13731
13732    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13733    a decl-specifier-seq unless it declares a constructor, destructor,
13734    or conversion.  It might seem that we could check this condition in
13735    semantic analysis, rather than parsing, but that makes it difficult
13736    to handle something like `f()'.  We want to notice that there are
13737    no decl-specifiers, and therefore realize that this is an
13738    expression, not a declaration.)
13739
13740    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13741    the declarator is a direct-declarator of the form "(...)".
13742
13743    MEMBER_P is true iff this declarator is a member-declarator.  */
13744
13745 static cp_declarator *
13746 cp_parser_declarator (cp_parser* parser,
13747                       cp_parser_declarator_kind dcl_kind,
13748                       int* ctor_dtor_or_conv_p,
13749                       bool* parenthesized_p,
13750                       bool member_p)
13751 {
13752   cp_token *token;
13753   cp_declarator *declarator;
13754   enum tree_code code;
13755   cp_cv_quals cv_quals;
13756   tree class_type;
13757   tree attributes = NULL_TREE;
13758
13759   /* Assume this is not a constructor, destructor, or type-conversion
13760      operator.  */
13761   if (ctor_dtor_or_conv_p)
13762     *ctor_dtor_or_conv_p = 0;
13763
13764   if (cp_parser_allow_gnu_extensions_p (parser))
13765     attributes = cp_parser_attributes_opt (parser);
13766
13767   /* Peek at the next token.  */
13768   token = cp_lexer_peek_token (parser->lexer);
13769
13770   /* Check for the ptr-operator production.  */
13771   cp_parser_parse_tentatively (parser);
13772   /* Parse the ptr-operator.  */
13773   code = cp_parser_ptr_operator (parser,
13774                                  &class_type,
13775                                  &cv_quals);
13776   /* If that worked, then we have a ptr-operator.  */
13777   if (cp_parser_parse_definitely (parser))
13778     {
13779       /* If a ptr-operator was found, then this declarator was not
13780          parenthesized.  */
13781       if (parenthesized_p)
13782         *parenthesized_p = true;
13783       /* The dependent declarator is optional if we are parsing an
13784          abstract-declarator.  */
13785       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13786         cp_parser_parse_tentatively (parser);
13787
13788       /* Parse the dependent declarator.  */
13789       declarator = cp_parser_declarator (parser, dcl_kind,
13790                                          /*ctor_dtor_or_conv_p=*/NULL,
13791                                          /*parenthesized_p=*/NULL,
13792                                          /*member_p=*/false);
13793
13794       /* If we are parsing an abstract-declarator, we must handle the
13795          case where the dependent declarator is absent.  */
13796       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13797           && !cp_parser_parse_definitely (parser))
13798         declarator = NULL;
13799
13800       declarator = cp_parser_make_indirect_declarator
13801         (code, class_type, cv_quals, declarator);
13802     }
13803   /* Everything else is a direct-declarator.  */
13804   else
13805     {
13806       if (parenthesized_p)
13807         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13808                                                    CPP_OPEN_PAREN);
13809       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13810                                                 ctor_dtor_or_conv_p,
13811                                                 member_p);
13812     }
13813
13814   if (attributes && declarator && declarator != cp_error_declarator)
13815     declarator->attributes = attributes;
13816
13817   return declarator;
13818 }
13819
13820 /* Parse a direct-declarator or direct-abstract-declarator.
13821
13822    direct-declarator:
13823      declarator-id
13824      direct-declarator ( parameter-declaration-clause )
13825        cv-qualifier-seq [opt]
13826        exception-specification [opt]
13827      direct-declarator [ constant-expression [opt] ]
13828      ( declarator )
13829
13830    direct-abstract-declarator:
13831      direct-abstract-declarator [opt]
13832        ( parameter-declaration-clause )
13833        cv-qualifier-seq [opt]
13834        exception-specification [opt]
13835      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13836      ( abstract-declarator )
13837
13838    Returns a representation of the declarator.  DCL_KIND is
13839    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13840    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13841    we are parsing a direct-declarator.  It is
13842    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13843    of ambiguity we prefer an abstract declarator, as per
13844    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13845    cp_parser_declarator.  */
13846
13847 static cp_declarator *
13848 cp_parser_direct_declarator (cp_parser* parser,
13849                              cp_parser_declarator_kind dcl_kind,
13850                              int* ctor_dtor_or_conv_p,
13851                              bool member_p)
13852 {
13853   cp_token *token;
13854   cp_declarator *declarator = NULL;
13855   tree scope = NULL_TREE;
13856   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13857   bool saved_in_declarator_p = parser->in_declarator_p;
13858   bool first = true;
13859   tree pushed_scope = NULL_TREE;
13860
13861   while (true)
13862     {
13863       /* Peek at the next token.  */
13864       token = cp_lexer_peek_token (parser->lexer);
13865       if (token->type == CPP_OPEN_PAREN)
13866         {
13867           /* This is either a parameter-declaration-clause, or a
13868              parenthesized declarator. When we know we are parsing a
13869              named declarator, it must be a parenthesized declarator
13870              if FIRST is true. For instance, `(int)' is a
13871              parameter-declaration-clause, with an omitted
13872              direct-abstract-declarator. But `((*))', is a
13873              parenthesized abstract declarator. Finally, when T is a
13874              template parameter `(T)' is a
13875              parameter-declaration-clause, and not a parenthesized
13876              named declarator.
13877
13878              We first try and parse a parameter-declaration-clause,
13879              and then try a nested declarator (if FIRST is true).
13880
13881              It is not an error for it not to be a
13882              parameter-declaration-clause, even when FIRST is
13883              false. Consider,
13884
13885                int i (int);
13886                int i (3);
13887
13888              The first is the declaration of a function while the
13889              second is the definition of a variable, including its
13890              initializer.
13891
13892              Having seen only the parenthesis, we cannot know which of
13893              these two alternatives should be selected.  Even more
13894              complex are examples like:
13895
13896                int i (int (a));
13897                int i (int (3));
13898
13899              The former is a function-declaration; the latter is a
13900              variable initialization.
13901
13902              Thus again, we try a parameter-declaration-clause, and if
13903              that fails, we back out and return.  */
13904
13905           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13906             {
13907               tree params;
13908               unsigned saved_num_template_parameter_lists;
13909               bool is_declarator = false;
13910               tree t;
13911
13912               /* In a member-declarator, the only valid interpretation
13913                  of a parenthesis is the start of a
13914                  parameter-declaration-clause.  (It is invalid to
13915                  initialize a static data member with a parenthesized
13916                  initializer; only the "=" form of initialization is
13917                  permitted.)  */
13918               if (!member_p)
13919                 cp_parser_parse_tentatively (parser);
13920
13921               /* Consume the `('.  */
13922               cp_lexer_consume_token (parser->lexer);
13923               if (first)
13924                 {
13925                   /* If this is going to be an abstract declarator, we're
13926                      in a declarator and we can't have default args.  */
13927                   parser->default_arg_ok_p = false;
13928                   parser->in_declarator_p = true;
13929                 }
13930
13931               /* Inside the function parameter list, surrounding
13932                  template-parameter-lists do not apply.  */
13933               saved_num_template_parameter_lists
13934                 = parser->num_template_parameter_lists;
13935               parser->num_template_parameter_lists = 0;
13936
13937               begin_scope (sk_function_parms, NULL_TREE);
13938
13939               /* Parse the parameter-declaration-clause.  */
13940               params = cp_parser_parameter_declaration_clause (parser);
13941
13942               parser->num_template_parameter_lists
13943                 = saved_num_template_parameter_lists;
13944
13945               /* If all went well, parse the cv-qualifier-seq and the
13946                  exception-specification.  */
13947               if (member_p || cp_parser_parse_definitely (parser))
13948                 {
13949                   cp_cv_quals cv_quals;
13950                   tree exception_specification;
13951                   tree late_return;
13952
13953                   is_declarator = true;
13954
13955                   if (ctor_dtor_or_conv_p)
13956                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13957                   first = false;
13958                   /* Consume the `)'.  */
13959                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13960
13961                   /* Parse the cv-qualifier-seq.  */
13962                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13963                   /* And the exception-specification.  */
13964                   exception_specification
13965                     = cp_parser_exception_specification_opt (parser);
13966
13967                   late_return
13968                     = cp_parser_late_return_type_opt (parser);
13969
13970                   /* Create the function-declarator.  */
13971                   declarator = make_call_declarator (declarator,
13972                                                      params,
13973                                                      cv_quals,
13974                                                      exception_specification,
13975                                                      late_return);
13976                   /* Any subsequent parameter lists are to do with
13977                      return type, so are not those of the declared
13978                      function.  */
13979                   parser->default_arg_ok_p = false;
13980                 }
13981
13982               /* Remove the function parms from scope.  */
13983               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13984                 pop_binding (DECL_NAME (t), t);
13985               leave_scope();
13986
13987               if (is_declarator)
13988                 /* Repeat the main loop.  */
13989                 continue;
13990             }
13991
13992           /* If this is the first, we can try a parenthesized
13993              declarator.  */
13994           if (first)
13995             {
13996               bool saved_in_type_id_in_expr_p;
13997
13998               parser->default_arg_ok_p = saved_default_arg_ok_p;
13999               parser->in_declarator_p = saved_in_declarator_p;
14000
14001               /* Consume the `('.  */
14002               cp_lexer_consume_token (parser->lexer);
14003               /* Parse the nested declarator.  */
14004               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14005               parser->in_type_id_in_expr_p = true;
14006               declarator
14007                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14008                                         /*parenthesized_p=*/NULL,
14009                                         member_p);
14010               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14011               first = false;
14012               /* Expect a `)'.  */
14013               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14014                 declarator = cp_error_declarator;
14015               if (declarator == cp_error_declarator)
14016                 break;
14017
14018               goto handle_declarator;
14019             }
14020           /* Otherwise, we must be done.  */
14021           else
14022             break;
14023         }
14024       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14025                && token->type == CPP_OPEN_SQUARE)
14026         {
14027           /* Parse an array-declarator.  */
14028           tree bounds;
14029
14030           if (ctor_dtor_or_conv_p)
14031             *ctor_dtor_or_conv_p = 0;
14032
14033           first = false;
14034           parser->default_arg_ok_p = false;
14035           parser->in_declarator_p = true;
14036           /* Consume the `['.  */
14037           cp_lexer_consume_token (parser->lexer);
14038           /* Peek at the next token.  */
14039           token = cp_lexer_peek_token (parser->lexer);
14040           /* If the next token is `]', then there is no
14041              constant-expression.  */
14042           if (token->type != CPP_CLOSE_SQUARE)
14043             {
14044               bool non_constant_p;
14045
14046               bounds
14047                 = cp_parser_constant_expression (parser,
14048                                                  /*allow_non_constant=*/true,
14049                                                  &non_constant_p);
14050               if (!non_constant_p)
14051                 bounds = fold_non_dependent_expr (bounds);
14052               /* Normally, the array bound must be an integral constant
14053                  expression.  However, as an extension, we allow VLAs
14054                  in function scopes.  */
14055               else if (!parser->in_function_body)
14056                 {
14057                   error_at (token->location,
14058                             "array bound is not an integer constant");
14059                   bounds = error_mark_node;
14060                 }
14061               else if (processing_template_decl && !error_operand_p (bounds))
14062                 {
14063                   /* Remember this wasn't a constant-expression.  */
14064                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14065                   TREE_SIDE_EFFECTS (bounds) = 1;
14066                 }
14067             }
14068           else
14069             bounds = NULL_TREE;
14070           /* Look for the closing `]'.  */
14071           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14072             {
14073               declarator = cp_error_declarator;
14074               break;
14075             }
14076
14077           declarator = make_array_declarator (declarator, bounds);
14078         }
14079       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14080         {
14081           {
14082             tree qualifying_scope;
14083             tree unqualified_name;
14084             special_function_kind sfk;
14085             bool abstract_ok;
14086             bool pack_expansion_p = false;
14087             cp_token *declarator_id_start_token;
14088
14089             /* Parse a declarator-id */
14090             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14091             if (abstract_ok)
14092               {
14093                 cp_parser_parse_tentatively (parser);
14094
14095                 /* If we see an ellipsis, we should be looking at a
14096                    parameter pack. */
14097                 if (token->type == CPP_ELLIPSIS)
14098                   {
14099                     /* Consume the `...' */
14100                     cp_lexer_consume_token (parser->lexer);
14101
14102                     pack_expansion_p = true;
14103                   }
14104               }
14105
14106             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14107             unqualified_name
14108               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14109             qualifying_scope = parser->scope;
14110             if (abstract_ok)
14111               {
14112                 bool okay = false;
14113
14114                 if (!unqualified_name && pack_expansion_p)
14115                   {
14116                     /* Check whether an error occurred. */
14117                     okay = !cp_parser_error_occurred (parser);
14118
14119                     /* We already consumed the ellipsis to mark a
14120                        parameter pack, but we have no way to report it,
14121                        so abort the tentative parse. We will be exiting
14122                        immediately anyway. */
14123                     cp_parser_abort_tentative_parse (parser);
14124                   }
14125                 else
14126                   okay = cp_parser_parse_definitely (parser);
14127
14128                 if (!okay)
14129                   unqualified_name = error_mark_node;
14130                 else if (unqualified_name
14131                          && (qualifying_scope
14132                              || (TREE_CODE (unqualified_name)
14133                                  != IDENTIFIER_NODE)))
14134                   {
14135                     cp_parser_error (parser, "expected unqualified-id");
14136                     unqualified_name = error_mark_node;
14137                   }
14138               }
14139
14140             if (!unqualified_name)
14141               return NULL;
14142             if (unqualified_name == error_mark_node)
14143               {
14144                 declarator = cp_error_declarator;
14145                 pack_expansion_p = false;
14146                 declarator->parameter_pack_p = false;
14147                 break;
14148               }
14149
14150             if (qualifying_scope && at_namespace_scope_p ()
14151                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14152               {
14153                 /* In the declaration of a member of a template class
14154                    outside of the class itself, the SCOPE will sometimes
14155                    be a TYPENAME_TYPE.  For example, given:
14156
14157                    template <typename T>
14158                    int S<T>::R::i = 3;
14159
14160                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14161                    this context, we must resolve S<T>::R to an ordinary
14162                    type, rather than a typename type.
14163
14164                    The reason we normally avoid resolving TYPENAME_TYPEs
14165                    is that a specialization of `S' might render
14166                    `S<T>::R' not a type.  However, if `S' is
14167                    specialized, then this `i' will not be used, so there
14168                    is no harm in resolving the types here.  */
14169                 tree type;
14170
14171                 /* Resolve the TYPENAME_TYPE.  */
14172                 type = resolve_typename_type (qualifying_scope,
14173                                               /*only_current_p=*/false);
14174                 /* If that failed, the declarator is invalid.  */
14175                 if (TREE_CODE (type) == TYPENAME_TYPE)
14176                   error_at (declarator_id_start_token->location,
14177                             "%<%T::%E%> is not a type",
14178                             TYPE_CONTEXT (qualifying_scope),
14179                             TYPE_IDENTIFIER (qualifying_scope));
14180                 qualifying_scope = type;
14181               }
14182
14183             sfk = sfk_none;
14184
14185             if (unqualified_name)
14186               {
14187                 tree class_type;
14188
14189                 if (qualifying_scope
14190                     && CLASS_TYPE_P (qualifying_scope))
14191                   class_type = qualifying_scope;
14192                 else
14193                   class_type = current_class_type;
14194
14195                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14196                   {
14197                     tree name_type = TREE_TYPE (unqualified_name);
14198                     if (class_type && same_type_p (name_type, class_type))
14199                       {
14200                         if (qualifying_scope
14201                             && CLASSTYPE_USE_TEMPLATE (name_type))
14202                           {
14203                             error_at (declarator_id_start_token->location,
14204                                       "invalid use of constructor as a template");
14205                             inform (declarator_id_start_token->location,
14206                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14207                                     "name the constructor in a qualified name",
14208                                     class_type,
14209                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14210                                     class_type, name_type);
14211                             declarator = cp_error_declarator;
14212                             break;
14213                           }
14214                         else
14215                           unqualified_name = constructor_name (class_type);
14216                       }
14217                     else
14218                       {
14219                         /* We do not attempt to print the declarator
14220                            here because we do not have enough
14221                            information about its original syntactic
14222                            form.  */
14223                         cp_parser_error (parser, "invalid declarator");
14224                         declarator = cp_error_declarator;
14225                         break;
14226                       }
14227                   }
14228
14229                 if (class_type)
14230                   {
14231                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14232                       sfk = sfk_destructor;
14233                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14234                       sfk = sfk_conversion;
14235                     else if (/* There's no way to declare a constructor
14236                                 for an anonymous type, even if the type
14237                                 got a name for linkage purposes.  */
14238                              !TYPE_WAS_ANONYMOUS (class_type)
14239                              && constructor_name_p (unqualified_name,
14240                                                     class_type))
14241                       {
14242                         unqualified_name = constructor_name (class_type);
14243                         sfk = sfk_constructor;
14244                       }
14245
14246                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14247                       *ctor_dtor_or_conv_p = -1;
14248                   }
14249               }
14250             declarator = make_id_declarator (qualifying_scope,
14251                                              unqualified_name,
14252                                              sfk);
14253             declarator->id_loc = token->location;
14254             declarator->parameter_pack_p = pack_expansion_p;
14255
14256             if (pack_expansion_p)
14257               maybe_warn_variadic_templates ();
14258           }
14259
14260         handle_declarator:;
14261           scope = get_scope_of_declarator (declarator);
14262           if (scope)
14263             /* Any names that appear after the declarator-id for a
14264                member are looked up in the containing scope.  */
14265             pushed_scope = push_scope (scope);
14266           parser->in_declarator_p = true;
14267           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14268               || (declarator && declarator->kind == cdk_id))
14269             /* Default args are only allowed on function
14270                declarations.  */
14271             parser->default_arg_ok_p = saved_default_arg_ok_p;
14272           else
14273             parser->default_arg_ok_p = false;
14274
14275           first = false;
14276         }
14277       /* We're done.  */
14278       else
14279         break;
14280     }
14281
14282   /* For an abstract declarator, we might wind up with nothing at this
14283      point.  That's an error; the declarator is not optional.  */
14284   if (!declarator)
14285     cp_parser_error (parser, "expected declarator");
14286
14287   /* If we entered a scope, we must exit it now.  */
14288   if (pushed_scope)
14289     pop_scope (pushed_scope);
14290
14291   parser->default_arg_ok_p = saved_default_arg_ok_p;
14292   parser->in_declarator_p = saved_in_declarator_p;
14293
14294   return declarator;
14295 }
14296
14297 /* Parse a ptr-operator.
14298
14299    ptr-operator:
14300      * cv-qualifier-seq [opt]
14301      &
14302      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14303
14304    GNU Extension:
14305
14306    ptr-operator:
14307      & cv-qualifier-seq [opt]
14308
14309    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14310    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14311    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14312    filled in with the TYPE containing the member.  *CV_QUALS is
14313    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14314    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14315    Note that the tree codes returned by this function have nothing
14316    to do with the types of trees that will be eventually be created
14317    to represent the pointer or reference type being parsed. They are
14318    just constants with suggestive names. */
14319 static enum tree_code
14320 cp_parser_ptr_operator (cp_parser* parser,
14321                         tree* type,
14322                         cp_cv_quals *cv_quals)
14323 {
14324   enum tree_code code = ERROR_MARK;
14325   cp_token *token;
14326
14327   /* Assume that it's not a pointer-to-member.  */
14328   *type = NULL_TREE;
14329   /* And that there are no cv-qualifiers.  */
14330   *cv_quals = TYPE_UNQUALIFIED;
14331
14332   /* Peek at the next token.  */
14333   token = cp_lexer_peek_token (parser->lexer);
14334
14335   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14336   if (token->type == CPP_MULT)
14337     code = INDIRECT_REF;
14338   else if (token->type == CPP_AND)
14339     code = ADDR_EXPR;
14340   else if ((cxx_dialect != cxx98) &&
14341            token->type == CPP_AND_AND) /* C++0x only */
14342     code = NON_LVALUE_EXPR;
14343
14344   if (code != ERROR_MARK)
14345     {
14346       /* Consume the `*', `&' or `&&'.  */
14347       cp_lexer_consume_token (parser->lexer);
14348
14349       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14350          `&', if we are allowing GNU extensions.  (The only qualifier
14351          that can legally appear after `&' is `restrict', but that is
14352          enforced during semantic analysis.  */
14353       if (code == INDIRECT_REF
14354           || cp_parser_allow_gnu_extensions_p (parser))
14355         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14356     }
14357   else
14358     {
14359       /* Try the pointer-to-member case.  */
14360       cp_parser_parse_tentatively (parser);
14361       /* Look for the optional `::' operator.  */
14362       cp_parser_global_scope_opt (parser,
14363                                   /*current_scope_valid_p=*/false);
14364       /* Look for the nested-name specifier.  */
14365       token = cp_lexer_peek_token (parser->lexer);
14366       cp_parser_nested_name_specifier (parser,
14367                                        /*typename_keyword_p=*/false,
14368                                        /*check_dependency_p=*/true,
14369                                        /*type_p=*/false,
14370                                        /*is_declaration=*/false);
14371       /* If we found it, and the next token is a `*', then we are
14372          indeed looking at a pointer-to-member operator.  */
14373       if (!cp_parser_error_occurred (parser)
14374           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14375         {
14376           /* Indicate that the `*' operator was used.  */
14377           code = INDIRECT_REF;
14378
14379           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14380             error_at (token->location, "%qD is a namespace", parser->scope);
14381           else
14382             {
14383               /* The type of which the member is a member is given by the
14384                  current SCOPE.  */
14385               *type = parser->scope;
14386               /* The next name will not be qualified.  */
14387               parser->scope = NULL_TREE;
14388               parser->qualifying_scope = NULL_TREE;
14389               parser->object_scope = NULL_TREE;
14390               /* Look for the optional cv-qualifier-seq.  */
14391               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14392             }
14393         }
14394       /* If that didn't work we don't have a ptr-operator.  */
14395       if (!cp_parser_parse_definitely (parser))
14396         cp_parser_error (parser, "expected ptr-operator");
14397     }
14398
14399   return code;
14400 }
14401
14402 /* Parse an (optional) cv-qualifier-seq.
14403
14404    cv-qualifier-seq:
14405      cv-qualifier cv-qualifier-seq [opt]
14406
14407    cv-qualifier:
14408      const
14409      volatile
14410
14411    GNU Extension:
14412
14413    cv-qualifier:
14414      __restrict__
14415
14416    Returns a bitmask representing the cv-qualifiers.  */
14417
14418 static cp_cv_quals
14419 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14420 {
14421   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14422
14423   while (true)
14424     {
14425       cp_token *token;
14426       cp_cv_quals cv_qualifier;
14427
14428       /* Peek at the next token.  */
14429       token = cp_lexer_peek_token (parser->lexer);
14430       /* See if it's a cv-qualifier.  */
14431       switch (token->keyword)
14432         {
14433         case RID_CONST:
14434           cv_qualifier = TYPE_QUAL_CONST;
14435           break;
14436
14437         case RID_VOLATILE:
14438           cv_qualifier = TYPE_QUAL_VOLATILE;
14439           break;
14440
14441         case RID_RESTRICT:
14442           cv_qualifier = TYPE_QUAL_RESTRICT;
14443           break;
14444
14445         default:
14446           cv_qualifier = TYPE_UNQUALIFIED;
14447           break;
14448         }
14449
14450       if (!cv_qualifier)
14451         break;
14452
14453       if (cv_quals & cv_qualifier)
14454         {
14455           error_at (token->location, "duplicate cv-qualifier");
14456           cp_lexer_purge_token (parser->lexer);
14457         }
14458       else
14459         {
14460           cp_lexer_consume_token (parser->lexer);
14461           cv_quals |= cv_qualifier;
14462         }
14463     }
14464
14465   return cv_quals;
14466 }
14467
14468 /* Parse a late-specified return type, if any.  This is not a separate
14469    non-terminal, but part of a function declarator, which looks like
14470
14471    -> trailing-type-specifier-seq abstract-declarator(opt)
14472
14473    Returns the type indicated by the type-id.  */
14474
14475 static tree
14476 cp_parser_late_return_type_opt (cp_parser* parser)
14477 {
14478   cp_token *token;
14479
14480   /* Peek at the next token.  */
14481   token = cp_lexer_peek_token (parser->lexer);
14482   /* A late-specified return type is indicated by an initial '->'. */
14483   if (token->type != CPP_DEREF)
14484     return NULL_TREE;
14485
14486   /* Consume the ->.  */
14487   cp_lexer_consume_token (parser->lexer);
14488
14489   return cp_parser_trailing_type_id (parser);
14490 }
14491
14492 /* Parse a declarator-id.
14493
14494    declarator-id:
14495      id-expression
14496      :: [opt] nested-name-specifier [opt] type-name
14497
14498    In the `id-expression' case, the value returned is as for
14499    cp_parser_id_expression if the id-expression was an unqualified-id.
14500    If the id-expression was a qualified-id, then a SCOPE_REF is
14501    returned.  The first operand is the scope (either a NAMESPACE_DECL
14502    or TREE_TYPE), but the second is still just a representation of an
14503    unqualified-id.  */
14504
14505 static tree
14506 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14507 {
14508   tree id;
14509   /* The expression must be an id-expression.  Assume that qualified
14510      names are the names of types so that:
14511
14512        template <class T>
14513        int S<T>::R::i = 3;
14514
14515      will work; we must treat `S<T>::R' as the name of a type.
14516      Similarly, assume that qualified names are templates, where
14517      required, so that:
14518
14519        template <class T>
14520        int S<T>::R<T>::i = 3;
14521
14522      will work, too.  */
14523   id = cp_parser_id_expression (parser,
14524                                 /*template_keyword_p=*/false,
14525                                 /*check_dependency_p=*/false,
14526                                 /*template_p=*/NULL,
14527                                 /*declarator_p=*/true,
14528                                 optional_p);
14529   if (id && BASELINK_P (id))
14530     id = BASELINK_FUNCTIONS (id);
14531   return id;
14532 }
14533
14534 /* Parse a type-id.
14535
14536    type-id:
14537      type-specifier-seq abstract-declarator [opt]
14538
14539    Returns the TYPE specified.  */
14540
14541 static tree
14542 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14543                      bool is_trailing_return)
14544 {
14545   cp_decl_specifier_seq type_specifier_seq;
14546   cp_declarator *abstract_declarator;
14547
14548   /* Parse the type-specifier-seq.  */
14549   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14550                                 is_trailing_return,
14551                                 &type_specifier_seq);
14552   if (type_specifier_seq.type == error_mark_node)
14553     return error_mark_node;
14554
14555   /* There might or might not be an abstract declarator.  */
14556   cp_parser_parse_tentatively (parser);
14557   /* Look for the declarator.  */
14558   abstract_declarator
14559     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14560                             /*parenthesized_p=*/NULL,
14561                             /*member_p=*/false);
14562   /* Check to see if there really was a declarator.  */
14563   if (!cp_parser_parse_definitely (parser))
14564     abstract_declarator = NULL;
14565
14566   if (type_specifier_seq.type
14567       && type_uses_auto (type_specifier_seq.type))
14568     {
14569       /* A type-id with type 'auto' is only ok if the abstract declarator
14570          is a function declarator with a late-specified return type.  */
14571       if (abstract_declarator
14572           && abstract_declarator->kind == cdk_function
14573           && abstract_declarator->u.function.late_return_type)
14574         /* OK */;
14575       else
14576         {
14577           error ("invalid use of %<auto%>");
14578           return error_mark_node;
14579         }
14580     }
14581   
14582   return groktypename (&type_specifier_seq, abstract_declarator,
14583                        is_template_arg);
14584 }
14585
14586 static tree cp_parser_type_id (cp_parser *parser)
14587 {
14588   return cp_parser_type_id_1 (parser, false, false);
14589 }
14590
14591 static tree cp_parser_template_type_arg (cp_parser *parser)
14592 {
14593   return cp_parser_type_id_1 (parser, true, false);
14594 }
14595
14596 static tree cp_parser_trailing_type_id (cp_parser *parser)
14597 {
14598   return cp_parser_type_id_1 (parser, false, true);
14599 }
14600
14601 /* Parse a type-specifier-seq.
14602
14603    type-specifier-seq:
14604      type-specifier type-specifier-seq [opt]
14605
14606    GNU extension:
14607
14608    type-specifier-seq:
14609      attributes type-specifier-seq [opt]
14610
14611    If IS_DECLARATION is true, we are at the start of a "condition" or
14612    exception-declaration, so we might be followed by a declarator-id.
14613
14614    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14615    i.e. we've just seen "->".
14616
14617    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14618
14619 static void
14620 cp_parser_type_specifier_seq (cp_parser* parser,
14621                               bool is_declaration,
14622                               bool is_trailing_return,
14623                               cp_decl_specifier_seq *type_specifier_seq)
14624 {
14625   bool seen_type_specifier = false;
14626   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14627   cp_token *start_token = NULL;
14628
14629   /* Clear the TYPE_SPECIFIER_SEQ.  */
14630   clear_decl_specs (type_specifier_seq);
14631
14632   /* In the context of a trailing return type, enum E { } is an
14633      elaborated-type-specifier followed by a function-body, not an
14634      enum-specifier.  */
14635   if (is_trailing_return)
14636     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14637
14638   /* Parse the type-specifiers and attributes.  */
14639   while (true)
14640     {
14641       tree type_specifier;
14642       bool is_cv_qualifier;
14643
14644       /* Check for attributes first.  */
14645       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14646         {
14647           type_specifier_seq->attributes =
14648             chainon (type_specifier_seq->attributes,
14649                      cp_parser_attributes_opt (parser));
14650           continue;
14651         }
14652
14653       /* record the token of the beginning of the type specifier seq,
14654          for error reporting purposes*/
14655      if (!start_token)
14656        start_token = cp_lexer_peek_token (parser->lexer);
14657
14658       /* Look for the type-specifier.  */
14659       type_specifier = cp_parser_type_specifier (parser,
14660                                                  flags,
14661                                                  type_specifier_seq,
14662                                                  /*is_declaration=*/false,
14663                                                  NULL,
14664                                                  &is_cv_qualifier);
14665       if (!type_specifier)
14666         {
14667           /* If the first type-specifier could not be found, this is not a
14668              type-specifier-seq at all.  */
14669           if (!seen_type_specifier)
14670             {
14671               cp_parser_error (parser, "expected type-specifier");
14672               type_specifier_seq->type = error_mark_node;
14673               return;
14674             }
14675           /* If subsequent type-specifiers could not be found, the
14676              type-specifier-seq is complete.  */
14677           break;
14678         }
14679
14680       seen_type_specifier = true;
14681       /* The standard says that a condition can be:
14682
14683             type-specifier-seq declarator = assignment-expression
14684
14685          However, given:
14686
14687            struct S {};
14688            if (int S = ...)
14689
14690          we should treat the "S" as a declarator, not as a
14691          type-specifier.  The standard doesn't say that explicitly for
14692          type-specifier-seq, but it does say that for
14693          decl-specifier-seq in an ordinary declaration.  Perhaps it
14694          would be clearer just to allow a decl-specifier-seq here, and
14695          then add a semantic restriction that if any decl-specifiers
14696          that are not type-specifiers appear, the program is invalid.  */
14697       if (is_declaration && !is_cv_qualifier)
14698         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14699     }
14700
14701   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14702 }
14703
14704 /* Parse a parameter-declaration-clause.
14705
14706    parameter-declaration-clause:
14707      parameter-declaration-list [opt] ... [opt]
14708      parameter-declaration-list , ...
14709
14710    Returns a representation for the parameter declarations.  A return
14711    value of NULL indicates a parameter-declaration-clause consisting
14712    only of an ellipsis.  */
14713
14714 static tree
14715 cp_parser_parameter_declaration_clause (cp_parser* parser)
14716 {
14717   tree parameters;
14718   cp_token *token;
14719   bool ellipsis_p;
14720   bool is_error;
14721
14722   /* Peek at the next token.  */
14723   token = cp_lexer_peek_token (parser->lexer);
14724   /* Check for trivial parameter-declaration-clauses.  */
14725   if (token->type == CPP_ELLIPSIS)
14726     {
14727       /* Consume the `...' token.  */
14728       cp_lexer_consume_token (parser->lexer);
14729       return NULL_TREE;
14730     }
14731   else if (token->type == CPP_CLOSE_PAREN)
14732     /* There are no parameters.  */
14733     {
14734 #ifndef NO_IMPLICIT_EXTERN_C
14735       if (in_system_header && current_class_type == NULL
14736           && current_lang_name == lang_name_c)
14737         return NULL_TREE;
14738       else
14739 #endif
14740         return void_list_node;
14741     }
14742   /* Check for `(void)', too, which is a special case.  */
14743   else if (token->keyword == RID_VOID
14744            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14745                == CPP_CLOSE_PAREN))
14746     {
14747       /* Consume the `void' token.  */
14748       cp_lexer_consume_token (parser->lexer);
14749       /* There are no parameters.  */
14750       return void_list_node;
14751     }
14752
14753   /* Parse the parameter-declaration-list.  */
14754   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14755   /* If a parse error occurred while parsing the
14756      parameter-declaration-list, then the entire
14757      parameter-declaration-clause is erroneous.  */
14758   if (is_error)
14759     return NULL;
14760
14761   /* Peek at the next token.  */
14762   token = cp_lexer_peek_token (parser->lexer);
14763   /* If it's a `,', the clause should terminate with an ellipsis.  */
14764   if (token->type == CPP_COMMA)
14765     {
14766       /* Consume the `,'.  */
14767       cp_lexer_consume_token (parser->lexer);
14768       /* Expect an ellipsis.  */
14769       ellipsis_p
14770         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14771     }
14772   /* It might also be `...' if the optional trailing `,' was
14773      omitted.  */
14774   else if (token->type == CPP_ELLIPSIS)
14775     {
14776       /* Consume the `...' token.  */
14777       cp_lexer_consume_token (parser->lexer);
14778       /* And remember that we saw it.  */
14779       ellipsis_p = true;
14780     }
14781   else
14782     ellipsis_p = false;
14783
14784   /* Finish the parameter list.  */
14785   if (!ellipsis_p)
14786     parameters = chainon (parameters, void_list_node);
14787
14788   return parameters;
14789 }
14790
14791 /* Parse a parameter-declaration-list.
14792
14793    parameter-declaration-list:
14794      parameter-declaration
14795      parameter-declaration-list , parameter-declaration
14796
14797    Returns a representation of the parameter-declaration-list, as for
14798    cp_parser_parameter_declaration_clause.  However, the
14799    `void_list_node' is never appended to the list.  Upon return,
14800    *IS_ERROR will be true iff an error occurred.  */
14801
14802 static tree
14803 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14804 {
14805   tree parameters = NULL_TREE;
14806   tree *tail = &parameters; 
14807   bool saved_in_unbraced_linkage_specification_p;
14808   int index = 0;
14809
14810   /* Assume all will go well.  */
14811   *is_error = false;
14812   /* The special considerations that apply to a function within an
14813      unbraced linkage specifications do not apply to the parameters
14814      to the function.  */
14815   saved_in_unbraced_linkage_specification_p 
14816     = parser->in_unbraced_linkage_specification_p;
14817   parser->in_unbraced_linkage_specification_p = false;
14818
14819   /* Look for more parameters.  */
14820   while (true)
14821     {
14822       cp_parameter_declarator *parameter;
14823       tree decl = error_mark_node;
14824       bool parenthesized_p;
14825       /* Parse the parameter.  */
14826       parameter
14827         = cp_parser_parameter_declaration (parser,
14828                                            /*template_parm_p=*/false,
14829                                            &parenthesized_p);
14830
14831       /* We don't know yet if the enclosing context is deprecated, so wait
14832          and warn in grokparms if appropriate.  */
14833       deprecated_state = DEPRECATED_SUPPRESS;
14834
14835       if (parameter)
14836         decl = grokdeclarator (parameter->declarator,
14837                                &parameter->decl_specifiers,
14838                                PARM,
14839                                parameter->default_argument != NULL_TREE,
14840                                &parameter->decl_specifiers.attributes);
14841
14842       deprecated_state = DEPRECATED_NORMAL;
14843
14844       /* If a parse error occurred parsing the parameter declaration,
14845          then the entire parameter-declaration-list is erroneous.  */
14846       if (decl == error_mark_node)
14847         {
14848           *is_error = true;
14849           parameters = error_mark_node;
14850           break;
14851         }
14852
14853       if (parameter->decl_specifiers.attributes)
14854         cplus_decl_attributes (&decl,
14855                                parameter->decl_specifiers.attributes,
14856                                0);
14857       if (DECL_NAME (decl))
14858         decl = pushdecl (decl);
14859
14860       if (decl != error_mark_node)
14861         {
14862           retrofit_lang_decl (decl);
14863           DECL_PARM_INDEX (decl) = ++index;
14864         }
14865
14866       /* Add the new parameter to the list.  */
14867       *tail = build_tree_list (parameter->default_argument, decl);
14868       tail = &TREE_CHAIN (*tail);
14869
14870       /* Peek at the next token.  */
14871       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14872           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14873           /* These are for Objective-C++ */
14874           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14875           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14876         /* The parameter-declaration-list is complete.  */
14877         break;
14878       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14879         {
14880           cp_token *token;
14881
14882           /* Peek at the next token.  */
14883           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14884           /* If it's an ellipsis, then the list is complete.  */
14885           if (token->type == CPP_ELLIPSIS)
14886             break;
14887           /* Otherwise, there must be more parameters.  Consume the
14888              `,'.  */
14889           cp_lexer_consume_token (parser->lexer);
14890           /* When parsing something like:
14891
14892                 int i(float f, double d)
14893
14894              we can tell after seeing the declaration for "f" that we
14895              are not looking at an initialization of a variable "i",
14896              but rather at the declaration of a function "i".
14897
14898              Due to the fact that the parsing of template arguments
14899              (as specified to a template-id) requires backtracking we
14900              cannot use this technique when inside a template argument
14901              list.  */
14902           if (!parser->in_template_argument_list_p
14903               && !parser->in_type_id_in_expr_p
14904               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14905               /* However, a parameter-declaration of the form
14906                  "foat(f)" (which is a valid declaration of a
14907                  parameter "f") can also be interpreted as an
14908                  expression (the conversion of "f" to "float").  */
14909               && !parenthesized_p)
14910             cp_parser_commit_to_tentative_parse (parser);
14911         }
14912       else
14913         {
14914           cp_parser_error (parser, "expected %<,%> or %<...%>");
14915           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14916             cp_parser_skip_to_closing_parenthesis (parser,
14917                                                    /*recovering=*/true,
14918                                                    /*or_comma=*/false,
14919                                                    /*consume_paren=*/false);
14920           break;
14921         }
14922     }
14923
14924   parser->in_unbraced_linkage_specification_p
14925     = saved_in_unbraced_linkage_specification_p;
14926
14927   return parameters;
14928 }
14929
14930 /* Parse a parameter declaration.
14931
14932    parameter-declaration:
14933      decl-specifier-seq ... [opt] declarator
14934      decl-specifier-seq declarator = assignment-expression
14935      decl-specifier-seq ... [opt] abstract-declarator [opt]
14936      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14937
14938    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14939    declares a template parameter.  (In that case, a non-nested `>'
14940    token encountered during the parsing of the assignment-expression
14941    is not interpreted as a greater-than operator.)
14942
14943    Returns a representation of the parameter, or NULL if an error
14944    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14945    true iff the declarator is of the form "(p)".  */
14946
14947 static cp_parameter_declarator *
14948 cp_parser_parameter_declaration (cp_parser *parser,
14949                                  bool template_parm_p,
14950                                  bool *parenthesized_p)
14951 {
14952   int declares_class_or_enum;
14953   bool greater_than_is_operator_p;
14954   cp_decl_specifier_seq decl_specifiers;
14955   cp_declarator *declarator;
14956   tree default_argument;
14957   cp_token *token = NULL, *declarator_token_start = NULL;
14958   const char *saved_message;
14959
14960   /* In a template parameter, `>' is not an operator.
14961
14962      [temp.param]
14963
14964      When parsing a default template-argument for a non-type
14965      template-parameter, the first non-nested `>' is taken as the end
14966      of the template parameter-list rather than a greater-than
14967      operator.  */
14968   greater_than_is_operator_p = !template_parm_p;
14969
14970   /* Type definitions may not appear in parameter types.  */
14971   saved_message = parser->type_definition_forbidden_message;
14972   parser->type_definition_forbidden_message
14973     = "types may not be defined in parameter types";
14974
14975   /* Parse the declaration-specifiers.  */
14976   cp_parser_decl_specifier_seq (parser,
14977                                 CP_PARSER_FLAGS_NONE,
14978                                 &decl_specifiers,
14979                                 &declares_class_or_enum);
14980
14981   /* Complain about missing 'typename' or other invalid type names.  */
14982   if (!decl_specifiers.any_type_specifiers_p)
14983     cp_parser_parse_and_diagnose_invalid_type_name (parser);
14984
14985   /* If an error occurred, there's no reason to attempt to parse the
14986      rest of the declaration.  */
14987   if (cp_parser_error_occurred (parser))
14988     {
14989       parser->type_definition_forbidden_message = saved_message;
14990       return NULL;
14991     }
14992
14993   /* Peek at the next token.  */
14994   token = cp_lexer_peek_token (parser->lexer);
14995
14996   /* If the next token is a `)', `,', `=', `>', or `...', then there
14997      is no declarator. However, when variadic templates are enabled,
14998      there may be a declarator following `...'.  */
14999   if (token->type == CPP_CLOSE_PAREN
15000       || token->type == CPP_COMMA
15001       || token->type == CPP_EQ
15002       || token->type == CPP_GREATER)
15003     {
15004       declarator = NULL;
15005       if (parenthesized_p)
15006         *parenthesized_p = false;
15007     }
15008   /* Otherwise, there should be a declarator.  */
15009   else
15010     {
15011       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15012       parser->default_arg_ok_p = false;
15013
15014       /* After seeing a decl-specifier-seq, if the next token is not a
15015          "(", there is no possibility that the code is a valid
15016          expression.  Therefore, if parsing tentatively, we commit at
15017          this point.  */
15018       if (!parser->in_template_argument_list_p
15019           /* In an expression context, having seen:
15020
15021                (int((char ...
15022
15023              we cannot be sure whether we are looking at a
15024              function-type (taking a "char" as a parameter) or a cast
15025              of some object of type "char" to "int".  */
15026           && !parser->in_type_id_in_expr_p
15027           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15028           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15029         cp_parser_commit_to_tentative_parse (parser);
15030       /* Parse the declarator.  */
15031       declarator_token_start = token;
15032       declarator = cp_parser_declarator (parser,
15033                                          CP_PARSER_DECLARATOR_EITHER,
15034                                          /*ctor_dtor_or_conv_p=*/NULL,
15035                                          parenthesized_p,
15036                                          /*member_p=*/false);
15037       parser->default_arg_ok_p = saved_default_arg_ok_p;
15038       /* After the declarator, allow more attributes.  */
15039       decl_specifiers.attributes
15040         = chainon (decl_specifiers.attributes,
15041                    cp_parser_attributes_opt (parser));
15042     }
15043
15044   /* If the next token is an ellipsis, and we have not seen a
15045      declarator name, and the type of the declarator contains parameter
15046      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15047      a parameter pack expansion expression. Otherwise, leave the
15048      ellipsis for a C-style variadic function. */
15049   token = cp_lexer_peek_token (parser->lexer);
15050   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15051     {
15052       tree type = decl_specifiers.type;
15053
15054       if (type && DECL_P (type))
15055         type = TREE_TYPE (type);
15056
15057       if (type
15058           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15059           && declarator_can_be_parameter_pack (declarator)
15060           && (!declarator || !declarator->parameter_pack_p)
15061           && uses_parameter_packs (type))
15062         {
15063           /* Consume the `...'. */
15064           cp_lexer_consume_token (parser->lexer);
15065           maybe_warn_variadic_templates ();
15066           
15067           /* Build a pack expansion type */
15068           if (declarator)
15069             declarator->parameter_pack_p = true;
15070           else
15071             decl_specifiers.type = make_pack_expansion (type);
15072         }
15073     }
15074
15075   /* The restriction on defining new types applies only to the type
15076      of the parameter, not to the default argument.  */
15077   parser->type_definition_forbidden_message = saved_message;
15078
15079   /* If the next token is `=', then process a default argument.  */
15080   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15081     {
15082       /* Consume the `='.  */
15083       cp_lexer_consume_token (parser->lexer);
15084
15085       /* If we are defining a class, then the tokens that make up the
15086          default argument must be saved and processed later.  */
15087       if (!template_parm_p && at_class_scope_p ()
15088           && TYPE_BEING_DEFINED (current_class_type)
15089           && !LAMBDA_TYPE_P (current_class_type))
15090         {
15091           unsigned depth = 0;
15092           int maybe_template_id = 0;
15093           cp_token *first_token;
15094           cp_token *token;
15095
15096           /* Add tokens until we have processed the entire default
15097              argument.  We add the range [first_token, token).  */
15098           first_token = cp_lexer_peek_token (parser->lexer);
15099           while (true)
15100             {
15101               bool done = false;
15102
15103               /* Peek at the next token.  */
15104               token = cp_lexer_peek_token (parser->lexer);
15105               /* What we do depends on what token we have.  */
15106               switch (token->type)
15107                 {
15108                   /* In valid code, a default argument must be
15109                      immediately followed by a `,' `)', or `...'.  */
15110                 case CPP_COMMA:
15111                   if (depth == 0 && maybe_template_id)
15112                     {
15113                       /* If we've seen a '<', we might be in a
15114                          template-argument-list.  Until Core issue 325 is
15115                          resolved, we don't know how this situation ought
15116                          to be handled, so try to DTRT.  We check whether
15117                          what comes after the comma is a valid parameter
15118                          declaration list.  If it is, then the comma ends
15119                          the default argument; otherwise the default
15120                          argument continues.  */
15121                       bool error = false;
15122
15123                       /* Set ITALP so cp_parser_parameter_declaration_list
15124                          doesn't decide to commit to this parse.  */
15125                       bool saved_italp = parser->in_template_argument_list_p;
15126                       parser->in_template_argument_list_p = true;
15127
15128                       cp_parser_parse_tentatively (parser);
15129                       cp_lexer_consume_token (parser->lexer);
15130                       cp_parser_parameter_declaration_list (parser, &error);
15131                       if (!cp_parser_error_occurred (parser) && !error)
15132                         done = true;
15133                       cp_parser_abort_tentative_parse (parser);
15134
15135                       parser->in_template_argument_list_p = saved_italp;
15136                       break;
15137                     }
15138                 case CPP_CLOSE_PAREN:
15139                 case CPP_ELLIPSIS:
15140                   /* If we run into a non-nested `;', `}', or `]',
15141                      then the code is invalid -- but the default
15142                      argument is certainly over.  */
15143                 case CPP_SEMICOLON:
15144                 case CPP_CLOSE_BRACE:
15145                 case CPP_CLOSE_SQUARE:
15146                   if (depth == 0)
15147                     done = true;
15148                   /* Update DEPTH, if necessary.  */
15149                   else if (token->type == CPP_CLOSE_PAREN
15150                            || token->type == CPP_CLOSE_BRACE
15151                            || token->type == CPP_CLOSE_SQUARE)
15152                     --depth;
15153                   break;
15154
15155                 case CPP_OPEN_PAREN:
15156                 case CPP_OPEN_SQUARE:
15157                 case CPP_OPEN_BRACE:
15158                   ++depth;
15159                   break;
15160
15161                 case CPP_LESS:
15162                   if (depth == 0)
15163                     /* This might be the comparison operator, or it might
15164                        start a template argument list.  */
15165                     ++maybe_template_id;
15166                   break;
15167
15168                 case CPP_RSHIFT:
15169                   if (cxx_dialect == cxx98)
15170                     break;
15171                   /* Fall through for C++0x, which treats the `>>'
15172                      operator like two `>' tokens in certain
15173                      cases.  */
15174
15175                 case CPP_GREATER:
15176                   if (depth == 0)
15177                     {
15178                       /* This might be an operator, or it might close a
15179                          template argument list.  But if a previous '<'
15180                          started a template argument list, this will have
15181                          closed it, so we can't be in one anymore.  */
15182                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15183                       if (maybe_template_id < 0)
15184                         maybe_template_id = 0;
15185                     }
15186                   break;
15187
15188                   /* If we run out of tokens, issue an error message.  */
15189                 case CPP_EOF:
15190                 case CPP_PRAGMA_EOL:
15191                   error_at (token->location, "file ends in default argument");
15192                   done = true;
15193                   break;
15194
15195                 case CPP_NAME:
15196                 case CPP_SCOPE:
15197                   /* In these cases, we should look for template-ids.
15198                      For example, if the default argument is
15199                      `X<int, double>()', we need to do name lookup to
15200                      figure out whether or not `X' is a template; if
15201                      so, the `,' does not end the default argument.
15202
15203                      That is not yet done.  */
15204                   break;
15205
15206                 default:
15207                   break;
15208                 }
15209
15210               /* If we've reached the end, stop.  */
15211               if (done)
15212                 break;
15213
15214               /* Add the token to the token block.  */
15215               token = cp_lexer_consume_token (parser->lexer);
15216             }
15217
15218           /* Create a DEFAULT_ARG to represent the unparsed default
15219              argument.  */
15220           default_argument = make_node (DEFAULT_ARG);
15221           DEFARG_TOKENS (default_argument)
15222             = cp_token_cache_new (first_token, token);
15223           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15224         }
15225       /* Outside of a class definition, we can just parse the
15226          assignment-expression.  */
15227       else
15228         {
15229           token = cp_lexer_peek_token (parser->lexer);
15230           default_argument 
15231             = cp_parser_default_argument (parser, template_parm_p);
15232         }
15233
15234       if (!parser->default_arg_ok_p)
15235         {
15236           if (flag_permissive)
15237             warning (0, "deprecated use of default argument for parameter of non-function");
15238           else
15239             {
15240               error_at (token->location,
15241                         "default arguments are only "
15242                         "permitted for function parameters");
15243               default_argument = NULL_TREE;
15244             }
15245         }
15246       else if ((declarator && declarator->parameter_pack_p)
15247                || (decl_specifiers.type
15248                    && PACK_EXPANSION_P (decl_specifiers.type)))
15249         {
15250           /* Find the name of the parameter pack.  */     
15251           cp_declarator *id_declarator = declarator;
15252           while (id_declarator && id_declarator->kind != cdk_id)
15253             id_declarator = id_declarator->declarator;
15254           
15255           if (id_declarator && id_declarator->kind == cdk_id)
15256             error_at (declarator_token_start->location,
15257                       template_parm_p 
15258                       ? "template parameter pack %qD"
15259                       " cannot have a default argument"
15260                       : "parameter pack %qD cannot have a default argument",
15261                       id_declarator->u.id.unqualified_name);
15262           else
15263             error_at (declarator_token_start->location,
15264                       template_parm_p 
15265                       ? "template parameter pack cannot have a default argument"
15266                       : "parameter pack cannot have a default argument");
15267           
15268           default_argument = NULL_TREE;
15269         }
15270     }
15271   else
15272     default_argument = NULL_TREE;
15273
15274   return make_parameter_declarator (&decl_specifiers,
15275                                     declarator,
15276                                     default_argument);
15277 }
15278
15279 /* Parse a default argument and return it.
15280
15281    TEMPLATE_PARM_P is true if this is a default argument for a
15282    non-type template parameter.  */
15283 static tree
15284 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15285 {
15286   tree default_argument = NULL_TREE;
15287   bool saved_greater_than_is_operator_p;
15288   bool saved_local_variables_forbidden_p;
15289
15290   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15291      set correctly.  */
15292   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15293   parser->greater_than_is_operator_p = !template_parm_p;
15294   /* Local variable names (and the `this' keyword) may not
15295      appear in a default argument.  */
15296   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15297   parser->local_variables_forbidden_p = true;
15298   /* Parse the assignment-expression.  */
15299   if (template_parm_p)
15300     push_deferring_access_checks (dk_no_deferred);
15301   default_argument
15302     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15303   if (template_parm_p)
15304     pop_deferring_access_checks ();
15305   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15306   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15307
15308   return default_argument;
15309 }
15310
15311 /* Parse a function-body.
15312
15313    function-body:
15314      compound_statement  */
15315
15316 static void
15317 cp_parser_function_body (cp_parser *parser)
15318 {
15319   cp_parser_compound_statement (parser, NULL, false);
15320 }
15321
15322 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15323    true if a ctor-initializer was present.  */
15324
15325 static bool
15326 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15327 {
15328   tree body;
15329   bool ctor_initializer_p;
15330
15331   /* Begin the function body.  */
15332   body = begin_function_body ();
15333   /* Parse the optional ctor-initializer.  */
15334   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15335   /* Parse the function-body.  */
15336   cp_parser_function_body (parser);
15337   /* Finish the function body.  */
15338   finish_function_body (body);
15339
15340   return ctor_initializer_p;
15341 }
15342
15343 /* Parse an initializer.
15344
15345    initializer:
15346      = initializer-clause
15347      ( expression-list )
15348
15349    Returns an expression representing the initializer.  If no
15350    initializer is present, NULL_TREE is returned.
15351
15352    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15353    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15354    set to TRUE if there is no initializer present.  If there is an
15355    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15356    is set to true; otherwise it is set to false.  */
15357
15358 static tree
15359 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15360                        bool* non_constant_p)
15361 {
15362   cp_token *token;
15363   tree init;
15364
15365   /* Peek at the next token.  */
15366   token = cp_lexer_peek_token (parser->lexer);
15367
15368   /* Let our caller know whether or not this initializer was
15369      parenthesized.  */
15370   *is_direct_init = (token->type != CPP_EQ);
15371   /* Assume that the initializer is constant.  */
15372   *non_constant_p = false;
15373
15374   if (token->type == CPP_EQ)
15375     {
15376       /* Consume the `='.  */
15377       cp_lexer_consume_token (parser->lexer);
15378       /* Parse the initializer-clause.  */
15379       init = cp_parser_initializer_clause (parser, non_constant_p);
15380     }
15381   else if (token->type == CPP_OPEN_PAREN)
15382     {
15383       VEC(tree,gc) *vec;
15384       vec = cp_parser_parenthesized_expression_list (parser, false,
15385                                                      /*cast_p=*/false,
15386                                                      /*allow_expansion_p=*/true,
15387                                                      non_constant_p);
15388       if (vec == NULL)
15389         return error_mark_node;
15390       init = build_tree_list_vec (vec);
15391       release_tree_vector (vec);
15392     }
15393   else if (token->type == CPP_OPEN_BRACE)
15394     {
15395       maybe_warn_cpp0x ("extended initializer lists");
15396       init = cp_parser_braced_list (parser, non_constant_p);
15397       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15398     }
15399   else
15400     {
15401       /* Anything else is an error.  */
15402       cp_parser_error (parser, "expected initializer");
15403       init = error_mark_node;
15404     }
15405
15406   return init;
15407 }
15408
15409 /* Parse an initializer-clause.
15410
15411    initializer-clause:
15412      assignment-expression
15413      braced-init-list
15414
15415    Returns an expression representing the initializer.
15416
15417    If the `assignment-expression' production is used the value
15418    returned is simply a representation for the expression.
15419
15420    Otherwise, calls cp_parser_braced_list.  */
15421
15422 static tree
15423 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15424 {
15425   tree initializer;
15426
15427   /* Assume the expression is constant.  */
15428   *non_constant_p = false;
15429
15430   /* If it is not a `{', then we are looking at an
15431      assignment-expression.  */
15432   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15433     {
15434       initializer
15435         = cp_parser_constant_expression (parser,
15436                                         /*allow_non_constant_p=*/true,
15437                                         non_constant_p);
15438       if (!*non_constant_p)
15439         initializer = fold_non_dependent_expr (initializer);
15440     }
15441   else
15442     initializer = cp_parser_braced_list (parser, non_constant_p);
15443
15444   return initializer;
15445 }
15446
15447 /* Parse a brace-enclosed initializer list.
15448
15449    braced-init-list:
15450      { initializer-list , [opt] }
15451      { }
15452
15453    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15454    the elements of the initializer-list (or NULL, if the last
15455    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15456    NULL_TREE.  There is no way to detect whether or not the optional
15457    trailing `,' was provided.  NON_CONSTANT_P is as for
15458    cp_parser_initializer.  */     
15459
15460 static tree
15461 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15462 {
15463   tree initializer;
15464
15465   /* Consume the `{' token.  */
15466   cp_lexer_consume_token (parser->lexer);
15467   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15468   initializer = make_node (CONSTRUCTOR);
15469   /* If it's not a `}', then there is a non-trivial initializer.  */
15470   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15471     {
15472       /* Parse the initializer list.  */
15473       CONSTRUCTOR_ELTS (initializer)
15474         = cp_parser_initializer_list (parser, non_constant_p);
15475       /* A trailing `,' token is allowed.  */
15476       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15477         cp_lexer_consume_token (parser->lexer);
15478     }
15479   /* Now, there should be a trailing `}'.  */
15480   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15481   TREE_TYPE (initializer) = init_list_type_node;
15482   return initializer;
15483 }
15484
15485 /* Parse an initializer-list.
15486
15487    initializer-list:
15488      initializer-clause ... [opt]
15489      initializer-list , initializer-clause ... [opt]
15490
15491    GNU Extension:
15492
15493    initializer-list:
15494      identifier : initializer-clause
15495      initializer-list, identifier : initializer-clause
15496
15497    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15498    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15499    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15500    as for cp_parser_initializer.  */
15501
15502 static VEC(constructor_elt,gc) *
15503 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15504 {
15505   VEC(constructor_elt,gc) *v = NULL;
15506
15507   /* Assume all of the expressions are constant.  */
15508   *non_constant_p = false;
15509
15510   /* Parse the rest of the list.  */
15511   while (true)
15512     {
15513       cp_token *token;
15514       tree identifier;
15515       tree initializer;
15516       bool clause_non_constant_p;
15517
15518       /* If the next token is an identifier and the following one is a
15519          colon, we are looking at the GNU designated-initializer
15520          syntax.  */
15521       if (cp_parser_allow_gnu_extensions_p (parser)
15522           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15523           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15524         {
15525           /* Warn the user that they are using an extension.  */
15526           pedwarn (input_location, OPT_pedantic, 
15527                    "ISO C++ does not allow designated initializers");
15528           /* Consume the identifier.  */
15529           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15530           /* Consume the `:'.  */
15531           cp_lexer_consume_token (parser->lexer);
15532         }
15533       else
15534         identifier = NULL_TREE;
15535
15536       /* Parse the initializer.  */
15537       initializer = cp_parser_initializer_clause (parser,
15538                                                   &clause_non_constant_p);
15539       /* If any clause is non-constant, so is the entire initializer.  */
15540       if (clause_non_constant_p)
15541         *non_constant_p = true;
15542
15543       /* If we have an ellipsis, this is an initializer pack
15544          expansion.  */
15545       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15546         {
15547           /* Consume the `...'.  */
15548           cp_lexer_consume_token (parser->lexer);
15549
15550           /* Turn the initializer into an initializer expansion.  */
15551           initializer = make_pack_expansion (initializer);
15552         }
15553
15554       /* Add it to the vector.  */
15555       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15556
15557       /* If the next token is not a comma, we have reached the end of
15558          the list.  */
15559       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15560         break;
15561
15562       /* Peek at the next token.  */
15563       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15564       /* If the next token is a `}', then we're still done.  An
15565          initializer-clause can have a trailing `,' after the
15566          initializer-list and before the closing `}'.  */
15567       if (token->type == CPP_CLOSE_BRACE)
15568         break;
15569
15570       /* Consume the `,' token.  */
15571       cp_lexer_consume_token (parser->lexer);
15572     }
15573
15574   return v;
15575 }
15576
15577 /* Classes [gram.class] */
15578
15579 /* Parse a class-name.
15580
15581    class-name:
15582      identifier
15583      template-id
15584
15585    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15586    to indicate that names looked up in dependent types should be
15587    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15588    keyword has been used to indicate that the name that appears next
15589    is a template.  TAG_TYPE indicates the explicit tag given before
15590    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15591    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15592    is the class being defined in a class-head.
15593
15594    Returns the TYPE_DECL representing the class.  */
15595
15596 static tree
15597 cp_parser_class_name (cp_parser *parser,
15598                       bool typename_keyword_p,
15599                       bool template_keyword_p,
15600                       enum tag_types tag_type,
15601                       bool check_dependency_p,
15602                       bool class_head_p,
15603                       bool is_declaration)
15604 {
15605   tree decl;
15606   tree scope;
15607   bool typename_p;
15608   cp_token *token;
15609   tree identifier = NULL_TREE;
15610
15611   /* All class-names start with an identifier.  */
15612   token = cp_lexer_peek_token (parser->lexer);
15613   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15614     {
15615       cp_parser_error (parser, "expected class-name");
15616       return error_mark_node;
15617     }
15618
15619   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15620      to a template-id, so we save it here.  */
15621   scope = parser->scope;
15622   if (scope == error_mark_node)
15623     return error_mark_node;
15624
15625   /* Any name names a type if we're following the `typename' keyword
15626      in a qualified name where the enclosing scope is type-dependent.  */
15627   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15628                 && dependent_type_p (scope));
15629   /* Handle the common case (an identifier, but not a template-id)
15630      efficiently.  */
15631   if (token->type == CPP_NAME
15632       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15633     {
15634       cp_token *identifier_token;
15635       bool ambiguous_p;
15636
15637       /* Look for the identifier.  */
15638       identifier_token = cp_lexer_peek_token (parser->lexer);
15639       ambiguous_p = identifier_token->ambiguous_p;
15640       identifier = cp_parser_identifier (parser);
15641       /* If the next token isn't an identifier, we are certainly not
15642          looking at a class-name.  */
15643       if (identifier == error_mark_node)
15644         decl = error_mark_node;
15645       /* If we know this is a type-name, there's no need to look it
15646          up.  */
15647       else if (typename_p)
15648         decl = identifier;
15649       else
15650         {
15651           tree ambiguous_decls;
15652           /* If we already know that this lookup is ambiguous, then
15653              we've already issued an error message; there's no reason
15654              to check again.  */
15655           if (ambiguous_p)
15656             {
15657               cp_parser_simulate_error (parser);
15658               return error_mark_node;
15659             }
15660           /* If the next token is a `::', then the name must be a type
15661              name.
15662
15663              [basic.lookup.qual]
15664
15665              During the lookup for a name preceding the :: scope
15666              resolution operator, object, function, and enumerator
15667              names are ignored.  */
15668           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15669             tag_type = typename_type;
15670           /* Look up the name.  */
15671           decl = cp_parser_lookup_name (parser, identifier,
15672                                         tag_type,
15673                                         /*is_template=*/false,
15674                                         /*is_namespace=*/false,
15675                                         check_dependency_p,
15676                                         &ambiguous_decls,
15677                                         identifier_token->location);
15678           if (ambiguous_decls)
15679             {
15680               error_at (identifier_token->location,
15681                         "reference to %qD is ambiguous", identifier);
15682               print_candidates (ambiguous_decls);
15683               if (cp_parser_parsing_tentatively (parser))
15684                 {
15685                   identifier_token->ambiguous_p = true;
15686                   cp_parser_simulate_error (parser);
15687                 }
15688               return error_mark_node;
15689             }
15690         }
15691     }
15692   else
15693     {
15694       /* Try a template-id.  */
15695       decl = cp_parser_template_id (parser, template_keyword_p,
15696                                     check_dependency_p,
15697                                     is_declaration);
15698       if (decl == error_mark_node)
15699         return error_mark_node;
15700     }
15701
15702   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15703
15704   /* If this is a typename, create a TYPENAME_TYPE.  */
15705   if (typename_p && decl != error_mark_node)
15706     {
15707       decl = make_typename_type (scope, decl, typename_type,
15708                                  /*complain=*/tf_error);
15709       if (decl != error_mark_node)
15710         decl = TYPE_NAME (decl);
15711     }
15712
15713   /* Check to see that it is really the name of a class.  */
15714   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15715       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15716       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15717     /* Situations like this:
15718
15719          template <typename T> struct A {
15720            typename T::template X<int>::I i;
15721          };
15722
15723        are problematic.  Is `T::template X<int>' a class-name?  The
15724        standard does not seem to be definitive, but there is no other
15725        valid interpretation of the following `::'.  Therefore, those
15726        names are considered class-names.  */
15727     {
15728       decl = make_typename_type (scope, decl, tag_type, tf_error);
15729       if (decl != error_mark_node)
15730         decl = TYPE_NAME (decl);
15731     }
15732   else if (TREE_CODE (decl) != TYPE_DECL
15733            || TREE_TYPE (decl) == error_mark_node
15734            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15735     decl = error_mark_node;
15736
15737   if (decl == error_mark_node)
15738     cp_parser_error (parser, "expected class-name");
15739   else if (identifier && !parser->scope)
15740     maybe_note_name_used_in_class (identifier, decl);
15741
15742   return decl;
15743 }
15744
15745 /* Parse a class-specifier.
15746
15747    class-specifier:
15748      class-head { member-specification [opt] }
15749
15750    Returns the TREE_TYPE representing the class.  */
15751
15752 static tree
15753 cp_parser_class_specifier (cp_parser* parser)
15754 {
15755   tree type;
15756   tree attributes = NULL_TREE;
15757   bool nested_name_specifier_p;
15758   unsigned saved_num_template_parameter_lists;
15759   bool saved_in_function_body;
15760   bool saved_in_unbraced_linkage_specification_p;
15761   tree old_scope = NULL_TREE;
15762   tree scope = NULL_TREE;
15763   tree bases;
15764
15765   push_deferring_access_checks (dk_no_deferred);
15766
15767   /* Parse the class-head.  */
15768   type = cp_parser_class_head (parser,
15769                                &nested_name_specifier_p,
15770                                &attributes,
15771                                &bases);
15772   /* If the class-head was a semantic disaster, skip the entire body
15773      of the class.  */
15774   if (!type)
15775     {
15776       cp_parser_skip_to_end_of_block_or_statement (parser);
15777       pop_deferring_access_checks ();
15778       return error_mark_node;
15779     }
15780
15781   /* Look for the `{'.  */
15782   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15783     {
15784       pop_deferring_access_checks ();
15785       return error_mark_node;
15786     }
15787
15788   /* Process the base classes. If they're invalid, skip the 
15789      entire class body.  */
15790   if (!xref_basetypes (type, bases))
15791     {
15792       /* Consuming the closing brace yields better error messages
15793          later on.  */
15794       if (cp_parser_skip_to_closing_brace (parser))
15795         cp_lexer_consume_token (parser->lexer);
15796       pop_deferring_access_checks ();
15797       return error_mark_node;
15798     }
15799
15800   /* Issue an error message if type-definitions are forbidden here.  */
15801   cp_parser_check_type_definition (parser);
15802   /* Remember that we are defining one more class.  */
15803   ++parser->num_classes_being_defined;
15804   /* Inside the class, surrounding template-parameter-lists do not
15805      apply.  */
15806   saved_num_template_parameter_lists
15807     = parser->num_template_parameter_lists;
15808   parser->num_template_parameter_lists = 0;
15809   /* We are not in a function body.  */
15810   saved_in_function_body = parser->in_function_body;
15811   parser->in_function_body = false;
15812   /* We are not immediately inside an extern "lang" block.  */
15813   saved_in_unbraced_linkage_specification_p
15814     = parser->in_unbraced_linkage_specification_p;
15815   parser->in_unbraced_linkage_specification_p = false;
15816
15817   /* Start the class.  */
15818   if (nested_name_specifier_p)
15819     {
15820       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15821       old_scope = push_inner_scope (scope);
15822     }
15823   type = begin_class_definition (type, attributes);
15824
15825   if (type == error_mark_node)
15826     /* If the type is erroneous, skip the entire body of the class.  */
15827     cp_parser_skip_to_closing_brace (parser);
15828   else
15829     /* Parse the member-specification.  */
15830     cp_parser_member_specification_opt (parser);
15831
15832   /* Look for the trailing `}'.  */
15833   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15834   /* Look for trailing attributes to apply to this class.  */
15835   if (cp_parser_allow_gnu_extensions_p (parser))
15836     attributes = cp_parser_attributes_opt (parser);
15837   if (type != error_mark_node)
15838     type = finish_struct (type, attributes);
15839   if (nested_name_specifier_p)
15840     pop_inner_scope (old_scope, scope);
15841   /* If this class is not itself within the scope of another class,
15842      then we need to parse the bodies of all of the queued function
15843      definitions.  Note that the queued functions defined in a class
15844      are not always processed immediately following the
15845      class-specifier for that class.  Consider:
15846
15847        struct A {
15848          struct B { void f() { sizeof (A); } };
15849        };
15850
15851      If `f' were processed before the processing of `A' were
15852      completed, there would be no way to compute the size of `A'.
15853      Note that the nesting we are interested in here is lexical --
15854      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15855      for:
15856
15857        struct A { struct B; };
15858        struct A::B { void f() { } };
15859
15860      there is no need to delay the parsing of `A::B::f'.  */
15861   if (--parser->num_classes_being_defined == 0)
15862     {
15863       tree queue_entry;
15864       tree fn;
15865       tree class_type = NULL_TREE;
15866       tree pushed_scope = NULL_TREE;
15867
15868       /* In a first pass, parse default arguments to the functions.
15869          Then, in a second pass, parse the bodies of the functions.
15870          This two-phased approach handles cases like:
15871
15872             struct S {
15873               void f() { g(); }
15874               void g(int i = 3);
15875             };
15876
15877          */
15878       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15879              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15880            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15881            TREE_PURPOSE (parser->unparsed_functions_queues)
15882              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15883         {
15884           fn = TREE_VALUE (queue_entry);
15885           /* If there are default arguments that have not yet been processed,
15886              take care of them now.  */
15887           if (class_type != TREE_PURPOSE (queue_entry))
15888             {
15889               if (pushed_scope)
15890                 pop_scope (pushed_scope);
15891               class_type = TREE_PURPOSE (queue_entry);
15892               pushed_scope = push_scope (class_type);
15893             }
15894           /* Make sure that any template parameters are in scope.  */
15895           maybe_begin_member_template_processing (fn);
15896           /* Parse the default argument expressions.  */
15897           cp_parser_late_parsing_default_args (parser, fn);
15898           /* Remove any template parameters from the symbol table.  */
15899           maybe_end_member_template_processing ();
15900         }
15901       if (pushed_scope)
15902         pop_scope (pushed_scope);
15903       /* Now parse the body of the functions.  */
15904       for (TREE_VALUE (parser->unparsed_functions_queues)
15905              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15906            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15907            TREE_VALUE (parser->unparsed_functions_queues)
15908              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15909         {
15910           /* Figure out which function we need to process.  */
15911           fn = TREE_VALUE (queue_entry);
15912           /* Parse the function.  */
15913           cp_parser_late_parsing_for_member (parser, fn);
15914         }
15915     }
15916
15917   /* Put back any saved access checks.  */
15918   pop_deferring_access_checks ();
15919
15920   /* Restore saved state.  */
15921   parser->in_function_body = saved_in_function_body;
15922   parser->num_template_parameter_lists
15923     = saved_num_template_parameter_lists;
15924   parser->in_unbraced_linkage_specification_p
15925     = saved_in_unbraced_linkage_specification_p;
15926
15927   return type;
15928 }
15929
15930 /* Parse a class-head.
15931
15932    class-head:
15933      class-key identifier [opt] base-clause [opt]
15934      class-key nested-name-specifier identifier base-clause [opt]
15935      class-key nested-name-specifier [opt] template-id
15936        base-clause [opt]
15937
15938    GNU Extensions:
15939      class-key attributes identifier [opt] base-clause [opt]
15940      class-key attributes nested-name-specifier identifier base-clause [opt]
15941      class-key attributes nested-name-specifier [opt] template-id
15942        base-clause [opt]
15943
15944    Upon return BASES is initialized to the list of base classes (or
15945    NULL, if there are none) in the same form returned by
15946    cp_parser_base_clause.
15947
15948    Returns the TYPE of the indicated class.  Sets
15949    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15950    involving a nested-name-specifier was used, and FALSE otherwise.
15951
15952    Returns error_mark_node if this is not a class-head.
15953
15954    Returns NULL_TREE if the class-head is syntactically valid, but
15955    semantically invalid in a way that means we should skip the entire
15956    body of the class.  */
15957
15958 static tree
15959 cp_parser_class_head (cp_parser* parser,
15960                       bool* nested_name_specifier_p,
15961                       tree *attributes_p,
15962                       tree *bases)
15963 {
15964   tree nested_name_specifier;
15965   enum tag_types class_key;
15966   tree id = NULL_TREE;
15967   tree type = NULL_TREE;
15968   tree attributes;
15969   bool template_id_p = false;
15970   bool qualified_p = false;
15971   bool invalid_nested_name_p = false;
15972   bool invalid_explicit_specialization_p = false;
15973   tree pushed_scope = NULL_TREE;
15974   unsigned num_templates;
15975   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15976   /* Assume no nested-name-specifier will be present.  */
15977   *nested_name_specifier_p = false;
15978   /* Assume no template parameter lists will be used in defining the
15979      type.  */
15980   num_templates = 0;
15981
15982   *bases = NULL_TREE;
15983
15984   /* Look for the class-key.  */
15985   class_key = cp_parser_class_key (parser);
15986   if (class_key == none_type)
15987     return error_mark_node;
15988
15989   /* Parse the attributes.  */
15990   attributes = cp_parser_attributes_opt (parser);
15991
15992   /* If the next token is `::', that is invalid -- but sometimes
15993      people do try to write:
15994
15995        struct ::S {};
15996
15997      Handle this gracefully by accepting the extra qualifier, and then
15998      issuing an error about it later if this really is a
15999      class-head.  If it turns out just to be an elaborated type
16000      specifier, remain silent.  */
16001   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16002     qualified_p = true;
16003
16004   push_deferring_access_checks (dk_no_check);
16005
16006   /* Determine the name of the class.  Begin by looking for an
16007      optional nested-name-specifier.  */
16008   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16009   nested_name_specifier
16010     = cp_parser_nested_name_specifier_opt (parser,
16011                                            /*typename_keyword_p=*/false,
16012                                            /*check_dependency_p=*/false,
16013                                            /*type_p=*/false,
16014                                            /*is_declaration=*/false);
16015   /* If there was a nested-name-specifier, then there *must* be an
16016      identifier.  */
16017   if (nested_name_specifier)
16018     {
16019       type_start_token = cp_lexer_peek_token (parser->lexer);
16020       /* Although the grammar says `identifier', it really means
16021          `class-name' or `template-name'.  You are only allowed to
16022          define a class that has already been declared with this
16023          syntax.
16024
16025          The proposed resolution for Core Issue 180 says that wherever
16026          you see `class T::X' you should treat `X' as a type-name.
16027
16028          It is OK to define an inaccessible class; for example:
16029
16030            class A { class B; };
16031            class A::B {};
16032
16033          We do not know if we will see a class-name, or a
16034          template-name.  We look for a class-name first, in case the
16035          class-name is a template-id; if we looked for the
16036          template-name first we would stop after the template-name.  */
16037       cp_parser_parse_tentatively (parser);
16038       type = cp_parser_class_name (parser,
16039                                    /*typename_keyword_p=*/false,
16040                                    /*template_keyword_p=*/false,
16041                                    class_type,
16042                                    /*check_dependency_p=*/false,
16043                                    /*class_head_p=*/true,
16044                                    /*is_declaration=*/false);
16045       /* If that didn't work, ignore the nested-name-specifier.  */
16046       if (!cp_parser_parse_definitely (parser))
16047         {
16048           invalid_nested_name_p = true;
16049           type_start_token = cp_lexer_peek_token (parser->lexer);
16050           id = cp_parser_identifier (parser);
16051           if (id == error_mark_node)
16052             id = NULL_TREE;
16053         }
16054       /* If we could not find a corresponding TYPE, treat this
16055          declaration like an unqualified declaration.  */
16056       if (type == error_mark_node)
16057         nested_name_specifier = NULL_TREE;
16058       /* Otherwise, count the number of templates used in TYPE and its
16059          containing scopes.  */
16060       else
16061         {
16062           tree scope;
16063
16064           for (scope = TREE_TYPE (type);
16065                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16066                scope = (TYPE_P (scope)
16067                         ? TYPE_CONTEXT (scope)
16068                         : DECL_CONTEXT (scope)))
16069             if (TYPE_P (scope)
16070                 && CLASS_TYPE_P (scope)
16071                 && CLASSTYPE_TEMPLATE_INFO (scope)
16072                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16073                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16074               ++num_templates;
16075         }
16076     }
16077   /* Otherwise, the identifier is optional.  */
16078   else
16079     {
16080       /* We don't know whether what comes next is a template-id,
16081          an identifier, or nothing at all.  */
16082       cp_parser_parse_tentatively (parser);
16083       /* Check for a template-id.  */
16084       type_start_token = cp_lexer_peek_token (parser->lexer);
16085       id = cp_parser_template_id (parser,
16086                                   /*template_keyword_p=*/false,
16087                                   /*check_dependency_p=*/true,
16088                                   /*is_declaration=*/true);
16089       /* If that didn't work, it could still be an identifier.  */
16090       if (!cp_parser_parse_definitely (parser))
16091         {
16092           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16093             {
16094               type_start_token = cp_lexer_peek_token (parser->lexer);
16095               id = cp_parser_identifier (parser);
16096             }
16097           else
16098             id = NULL_TREE;
16099         }
16100       else
16101         {
16102           template_id_p = true;
16103           ++num_templates;
16104         }
16105     }
16106
16107   pop_deferring_access_checks ();
16108
16109   if (id)
16110     cp_parser_check_for_invalid_template_id (parser, id,
16111                                              type_start_token->location);
16112
16113   /* If it's not a `:' or a `{' then we can't really be looking at a
16114      class-head, since a class-head only appears as part of a
16115      class-specifier.  We have to detect this situation before calling
16116      xref_tag, since that has irreversible side-effects.  */
16117   if (!cp_parser_next_token_starts_class_definition_p (parser))
16118     {
16119       cp_parser_error (parser, "expected %<{%> or %<:%>");
16120       return error_mark_node;
16121     }
16122
16123   /* At this point, we're going ahead with the class-specifier, even
16124      if some other problem occurs.  */
16125   cp_parser_commit_to_tentative_parse (parser);
16126   /* Issue the error about the overly-qualified name now.  */
16127   if (qualified_p)
16128     {
16129       cp_parser_error (parser,
16130                        "global qualification of class name is invalid");
16131       return error_mark_node;
16132     }
16133   else if (invalid_nested_name_p)
16134     {
16135       cp_parser_error (parser,
16136                        "qualified name does not name a class");
16137       return error_mark_node;
16138     }
16139   else if (nested_name_specifier)
16140     {
16141       tree scope;
16142
16143       /* Reject typedef-names in class heads.  */
16144       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16145         {
16146           error_at (type_start_token->location,
16147                     "invalid class name in declaration of %qD",
16148                     type);
16149           type = NULL_TREE;
16150           goto done;
16151         }
16152
16153       /* Figure out in what scope the declaration is being placed.  */
16154       scope = current_scope ();
16155       /* If that scope does not contain the scope in which the
16156          class was originally declared, the program is invalid.  */
16157       if (scope && !is_ancestor (scope, nested_name_specifier))
16158         {
16159           if (at_namespace_scope_p ())
16160             error_at (type_start_token->location,
16161                       "declaration of %qD in namespace %qD which does not "
16162                       "enclose %qD",
16163                       type, scope, nested_name_specifier);
16164           else
16165             error_at (type_start_token->location,
16166                       "declaration of %qD in %qD which does not enclose %qD",
16167                       type, scope, nested_name_specifier);
16168           type = NULL_TREE;
16169           goto done;
16170         }
16171       /* [dcl.meaning]
16172
16173          A declarator-id shall not be qualified except for the
16174          definition of a ... nested class outside of its class
16175          ... [or] the definition or explicit instantiation of a
16176          class member of a namespace outside of its namespace.  */
16177       if (scope == nested_name_specifier)
16178         {
16179           permerror (nested_name_specifier_token_start->location,
16180                      "extra qualification not allowed");
16181           nested_name_specifier = NULL_TREE;
16182           num_templates = 0;
16183         }
16184     }
16185   /* An explicit-specialization must be preceded by "template <>".  If
16186      it is not, try to recover gracefully.  */
16187   if (at_namespace_scope_p ()
16188       && parser->num_template_parameter_lists == 0
16189       && template_id_p)
16190     {
16191       error_at (type_start_token->location,
16192                 "an explicit specialization must be preceded by %<template <>%>");
16193       invalid_explicit_specialization_p = true;
16194       /* Take the same action that would have been taken by
16195          cp_parser_explicit_specialization.  */
16196       ++parser->num_template_parameter_lists;
16197       begin_specialization ();
16198     }
16199   /* There must be no "return" statements between this point and the
16200      end of this function; set "type "to the correct return value and
16201      use "goto done;" to return.  */
16202   /* Make sure that the right number of template parameters were
16203      present.  */
16204   if (!cp_parser_check_template_parameters (parser, num_templates,
16205                                             type_start_token->location,
16206                                             /*declarator=*/NULL))
16207     {
16208       /* If something went wrong, there is no point in even trying to
16209          process the class-definition.  */
16210       type = NULL_TREE;
16211       goto done;
16212     }
16213
16214   /* Look up the type.  */
16215   if (template_id_p)
16216     {
16217       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16218           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16219               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16220         {
16221           error_at (type_start_token->location,
16222                     "function template %qD redeclared as a class template", id);
16223           type = error_mark_node;
16224         }
16225       else
16226         {
16227           type = TREE_TYPE (id);
16228           type = maybe_process_partial_specialization (type);
16229         }
16230       if (nested_name_specifier)
16231         pushed_scope = push_scope (nested_name_specifier);
16232     }
16233   else if (nested_name_specifier)
16234     {
16235       tree class_type;
16236
16237       /* Given:
16238
16239             template <typename T> struct S { struct T };
16240             template <typename T> struct S<T>::T { };
16241
16242          we will get a TYPENAME_TYPE when processing the definition of
16243          `S::T'.  We need to resolve it to the actual type before we
16244          try to define it.  */
16245       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16246         {
16247           class_type = resolve_typename_type (TREE_TYPE (type),
16248                                               /*only_current_p=*/false);
16249           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16250             type = TYPE_NAME (class_type);
16251           else
16252             {
16253               cp_parser_error (parser, "could not resolve typename type");
16254               type = error_mark_node;
16255             }
16256         }
16257
16258       if (maybe_process_partial_specialization (TREE_TYPE (type))
16259           == error_mark_node)
16260         {
16261           type = NULL_TREE;
16262           goto done;
16263         }
16264
16265       class_type = current_class_type;
16266       /* Enter the scope indicated by the nested-name-specifier.  */
16267       pushed_scope = push_scope (nested_name_specifier);
16268       /* Get the canonical version of this type.  */
16269       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16270       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16271           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16272         {
16273           type = push_template_decl (type);
16274           if (type == error_mark_node)
16275             {
16276               type = NULL_TREE;
16277               goto done;
16278             }
16279         }
16280
16281       type = TREE_TYPE (type);
16282       *nested_name_specifier_p = true;
16283     }
16284   else      /* The name is not a nested name.  */
16285     {
16286       /* If the class was unnamed, create a dummy name.  */
16287       if (!id)
16288         id = make_anon_name ();
16289       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16290                        parser->num_template_parameter_lists);
16291     }
16292
16293   /* Indicate whether this class was declared as a `class' or as a
16294      `struct'.  */
16295   if (TREE_CODE (type) == RECORD_TYPE)
16296     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16297   cp_parser_check_class_key (class_key, type);
16298
16299   /* If this type was already complete, and we see another definition,
16300      that's an error.  */
16301   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16302     {
16303       error_at (type_start_token->location, "redefinition of %q#T",
16304                 type);
16305       error_at (type_start_token->location, "previous definition of %q+#T",
16306                 type);
16307       type = NULL_TREE;
16308       goto done;
16309     }
16310   else if (type == error_mark_node)
16311     type = NULL_TREE;
16312
16313   /* We will have entered the scope containing the class; the names of
16314      base classes should be looked up in that context.  For example:
16315
16316        struct A { struct B {}; struct C; };
16317        struct A::C : B {};
16318
16319      is valid.  */
16320
16321   /* Get the list of base-classes, if there is one.  */
16322   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16323     *bases = cp_parser_base_clause (parser);
16324
16325  done:
16326   /* Leave the scope given by the nested-name-specifier.  We will
16327      enter the class scope itself while processing the members.  */
16328   if (pushed_scope)
16329     pop_scope (pushed_scope);
16330
16331   if (invalid_explicit_specialization_p)
16332     {
16333       end_specialization ();
16334       --parser->num_template_parameter_lists;
16335     }
16336   *attributes_p = attributes;
16337   return type;
16338 }
16339
16340 /* Parse a class-key.
16341
16342    class-key:
16343      class
16344      struct
16345      union
16346
16347    Returns the kind of class-key specified, or none_type to indicate
16348    error.  */
16349
16350 static enum tag_types
16351 cp_parser_class_key (cp_parser* parser)
16352 {
16353   cp_token *token;
16354   enum tag_types tag_type;
16355
16356   /* Look for the class-key.  */
16357   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16358   if (!token)
16359     return none_type;
16360
16361   /* Check to see if the TOKEN is a class-key.  */
16362   tag_type = cp_parser_token_is_class_key (token);
16363   if (!tag_type)
16364     cp_parser_error (parser, "expected class-key");
16365   return tag_type;
16366 }
16367
16368 /* Parse an (optional) member-specification.
16369
16370    member-specification:
16371      member-declaration member-specification [opt]
16372      access-specifier : member-specification [opt]  */
16373
16374 static void
16375 cp_parser_member_specification_opt (cp_parser* parser)
16376 {
16377   while (true)
16378     {
16379       cp_token *token;
16380       enum rid keyword;
16381
16382       /* Peek at the next token.  */
16383       token = cp_lexer_peek_token (parser->lexer);
16384       /* If it's a `}', or EOF then we've seen all the members.  */
16385       if (token->type == CPP_CLOSE_BRACE
16386           || token->type == CPP_EOF
16387           || token->type == CPP_PRAGMA_EOL)
16388         break;
16389
16390       /* See if this token is a keyword.  */
16391       keyword = token->keyword;
16392       switch (keyword)
16393         {
16394         case RID_PUBLIC:
16395         case RID_PROTECTED:
16396         case RID_PRIVATE:
16397           /* Consume the access-specifier.  */
16398           cp_lexer_consume_token (parser->lexer);
16399           /* Remember which access-specifier is active.  */
16400           current_access_specifier = token->u.value;
16401           /* Look for the `:'.  */
16402           cp_parser_require (parser, CPP_COLON, "%<:%>");
16403           break;
16404
16405         default:
16406           /* Accept #pragmas at class scope.  */
16407           if (token->type == CPP_PRAGMA)
16408             {
16409               cp_parser_pragma (parser, pragma_external);
16410               break;
16411             }
16412
16413           /* Otherwise, the next construction must be a
16414              member-declaration.  */
16415           cp_parser_member_declaration (parser);
16416         }
16417     }
16418 }
16419
16420 /* Parse a member-declaration.
16421
16422    member-declaration:
16423      decl-specifier-seq [opt] member-declarator-list [opt] ;
16424      function-definition ; [opt]
16425      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16426      using-declaration
16427      template-declaration
16428
16429    member-declarator-list:
16430      member-declarator
16431      member-declarator-list , member-declarator
16432
16433    member-declarator:
16434      declarator pure-specifier [opt]
16435      declarator constant-initializer [opt]
16436      identifier [opt] : constant-expression
16437
16438    GNU Extensions:
16439
16440    member-declaration:
16441      __extension__ member-declaration
16442
16443    member-declarator:
16444      declarator attributes [opt] pure-specifier [opt]
16445      declarator attributes [opt] constant-initializer [opt]
16446      identifier [opt] attributes [opt] : constant-expression  
16447
16448    C++0x Extensions:
16449
16450    member-declaration:
16451      static_assert-declaration  */
16452
16453 static void
16454 cp_parser_member_declaration (cp_parser* parser)
16455 {
16456   cp_decl_specifier_seq decl_specifiers;
16457   tree prefix_attributes;
16458   tree decl;
16459   int declares_class_or_enum;
16460   bool friend_p;
16461   cp_token *token = NULL;
16462   cp_token *decl_spec_token_start = NULL;
16463   cp_token *initializer_token_start = NULL;
16464   int saved_pedantic;
16465
16466   /* Check for the `__extension__' keyword.  */
16467   if (cp_parser_extension_opt (parser, &saved_pedantic))
16468     {
16469       /* Recurse.  */
16470       cp_parser_member_declaration (parser);
16471       /* Restore the old value of the PEDANTIC flag.  */
16472       pedantic = saved_pedantic;
16473
16474       return;
16475     }
16476
16477   /* Check for a template-declaration.  */
16478   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16479     {
16480       /* An explicit specialization here is an error condition, and we
16481          expect the specialization handler to detect and report this.  */
16482       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16483           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16484         cp_parser_explicit_specialization (parser);
16485       else
16486         cp_parser_template_declaration (parser, /*member_p=*/true);
16487
16488       return;
16489     }
16490
16491   /* Check for a using-declaration.  */
16492   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16493     {
16494       /* Parse the using-declaration.  */
16495       cp_parser_using_declaration (parser,
16496                                    /*access_declaration_p=*/false);
16497       return;
16498     }
16499
16500   /* Check for @defs.  */
16501   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16502     {
16503       tree ivar, member;
16504       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16505       ivar = ivar_chains;
16506       while (ivar)
16507         {
16508           member = ivar;
16509           ivar = TREE_CHAIN (member);
16510           TREE_CHAIN (member) = NULL_TREE;
16511           finish_member_declaration (member);
16512         }
16513       return;
16514     }
16515
16516   /* If the next token is `static_assert' we have a static assertion.  */
16517   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16518     {
16519       cp_parser_static_assert (parser, /*member_p=*/true);
16520       return;
16521     }
16522
16523   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16524     return;
16525
16526   /* Parse the decl-specifier-seq.  */
16527   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16528   cp_parser_decl_specifier_seq (parser,
16529                                 CP_PARSER_FLAGS_OPTIONAL,
16530                                 &decl_specifiers,
16531                                 &declares_class_or_enum);
16532   prefix_attributes = decl_specifiers.attributes;
16533   decl_specifiers.attributes = NULL_TREE;
16534   /* Check for an invalid type-name.  */
16535   if (!decl_specifiers.any_type_specifiers_p
16536       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16537     return;
16538   /* If there is no declarator, then the decl-specifier-seq should
16539      specify a type.  */
16540   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16541     {
16542       /* If there was no decl-specifier-seq, and the next token is a
16543          `;', then we have something like:
16544
16545            struct S { ; };
16546
16547          [class.mem]
16548
16549          Each member-declaration shall declare at least one member
16550          name of the class.  */
16551       if (!decl_specifiers.any_specifiers_p)
16552         {
16553           cp_token *token = cp_lexer_peek_token (parser->lexer);
16554           if (!in_system_header_at (token->location))
16555             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16556         }
16557       else
16558         {
16559           tree type;
16560
16561           /* See if this declaration is a friend.  */
16562           friend_p = cp_parser_friend_p (&decl_specifiers);
16563           /* If there were decl-specifiers, check to see if there was
16564              a class-declaration.  */
16565           type = check_tag_decl (&decl_specifiers);
16566           /* Nested classes have already been added to the class, but
16567              a `friend' needs to be explicitly registered.  */
16568           if (friend_p)
16569             {
16570               /* If the `friend' keyword was present, the friend must
16571                  be introduced with a class-key.  */
16572                if (!declares_class_or_enum)
16573                  error_at (decl_spec_token_start->location,
16574                            "a class-key must be used when declaring a friend");
16575                /* In this case:
16576
16577                     template <typename T> struct A {
16578                       friend struct A<T>::B;
16579                     };
16580
16581                   A<T>::B will be represented by a TYPENAME_TYPE, and
16582                   therefore not recognized by check_tag_decl.  */
16583                if (!type
16584                    && decl_specifiers.type
16585                    && TYPE_P (decl_specifiers.type))
16586                  type = decl_specifiers.type;
16587                if (!type || !TYPE_P (type))
16588                  error_at (decl_spec_token_start->location,
16589                            "friend declaration does not name a class or "
16590                            "function");
16591                else
16592                  make_friend_class (current_class_type, type,
16593                                     /*complain=*/true);
16594             }
16595           /* If there is no TYPE, an error message will already have
16596              been issued.  */
16597           else if (!type || type == error_mark_node)
16598             ;
16599           /* An anonymous aggregate has to be handled specially; such
16600              a declaration really declares a data member (with a
16601              particular type), as opposed to a nested class.  */
16602           else if (ANON_AGGR_TYPE_P (type))
16603             {
16604               /* Remove constructors and such from TYPE, now that we
16605                  know it is an anonymous aggregate.  */
16606               fixup_anonymous_aggr (type);
16607               /* And make the corresponding data member.  */
16608               decl = build_decl (decl_spec_token_start->location,
16609                                  FIELD_DECL, NULL_TREE, type);
16610               /* Add it to the class.  */
16611               finish_member_declaration (decl);
16612             }
16613           else
16614             cp_parser_check_access_in_redeclaration
16615                                               (TYPE_NAME (type),
16616                                                decl_spec_token_start->location);
16617         }
16618     }
16619   else
16620     {
16621       /* See if these declarations will be friends.  */
16622       friend_p = cp_parser_friend_p (&decl_specifiers);
16623
16624       /* Keep going until we hit the `;' at the end of the
16625          declaration.  */
16626       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16627         {
16628           tree attributes = NULL_TREE;
16629           tree first_attribute;
16630
16631           /* Peek at the next token.  */
16632           token = cp_lexer_peek_token (parser->lexer);
16633
16634           /* Check for a bitfield declaration.  */
16635           if (token->type == CPP_COLON
16636               || (token->type == CPP_NAME
16637                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16638                   == CPP_COLON))
16639             {
16640               tree identifier;
16641               tree width;
16642
16643               /* Get the name of the bitfield.  Note that we cannot just
16644                  check TOKEN here because it may have been invalidated by
16645                  the call to cp_lexer_peek_nth_token above.  */
16646               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16647                 identifier = cp_parser_identifier (parser);
16648               else
16649                 identifier = NULL_TREE;
16650
16651               /* Consume the `:' token.  */
16652               cp_lexer_consume_token (parser->lexer);
16653               /* Get the width of the bitfield.  */
16654               width
16655                 = cp_parser_constant_expression (parser,
16656                                                  /*allow_non_constant=*/false,
16657                                                  NULL);
16658
16659               /* Look for attributes that apply to the bitfield.  */
16660               attributes = cp_parser_attributes_opt (parser);
16661               /* Remember which attributes are prefix attributes and
16662                  which are not.  */
16663               first_attribute = attributes;
16664               /* Combine the attributes.  */
16665               attributes = chainon (prefix_attributes, attributes);
16666
16667               /* Create the bitfield declaration.  */
16668               decl = grokbitfield (identifier
16669                                    ? make_id_declarator (NULL_TREE,
16670                                                          identifier,
16671                                                          sfk_none)
16672                                    : NULL,
16673                                    &decl_specifiers,
16674                                    width,
16675                                    attributes);
16676             }
16677           else
16678             {
16679               cp_declarator *declarator;
16680               tree initializer;
16681               tree asm_specification;
16682               int ctor_dtor_or_conv_p;
16683
16684               /* Parse the declarator.  */
16685               declarator
16686                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16687                                         &ctor_dtor_or_conv_p,
16688                                         /*parenthesized_p=*/NULL,
16689                                         /*member_p=*/true);
16690
16691               /* If something went wrong parsing the declarator, make sure
16692                  that we at least consume some tokens.  */
16693               if (declarator == cp_error_declarator)
16694                 {
16695                   /* Skip to the end of the statement.  */
16696                   cp_parser_skip_to_end_of_statement (parser);
16697                   /* If the next token is not a semicolon, that is
16698                      probably because we just skipped over the body of
16699                      a function.  So, we consume a semicolon if
16700                      present, but do not issue an error message if it
16701                      is not present.  */
16702                   if (cp_lexer_next_token_is (parser->lexer,
16703                                               CPP_SEMICOLON))
16704                     cp_lexer_consume_token (parser->lexer);
16705                   return;
16706                 }
16707
16708               if (declares_class_or_enum & 2)
16709                 cp_parser_check_for_definition_in_return_type
16710                                             (declarator, decl_specifiers.type,
16711                                              decl_specifiers.type_location);
16712
16713               /* Look for an asm-specification.  */
16714               asm_specification = cp_parser_asm_specification_opt (parser);
16715               /* Look for attributes that apply to the declaration.  */
16716               attributes = cp_parser_attributes_opt (parser);
16717               /* Remember which attributes are prefix attributes and
16718                  which are not.  */
16719               first_attribute = attributes;
16720               /* Combine the attributes.  */
16721               attributes = chainon (prefix_attributes, attributes);
16722
16723               /* If it's an `=', then we have a constant-initializer or a
16724                  pure-specifier.  It is not correct to parse the
16725                  initializer before registering the member declaration
16726                  since the member declaration should be in scope while
16727                  its initializer is processed.  However, the rest of the
16728                  front end does not yet provide an interface that allows
16729                  us to handle this correctly.  */
16730               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16731                 {
16732                   /* In [class.mem]:
16733
16734                      A pure-specifier shall be used only in the declaration of
16735                      a virtual function.
16736
16737                      A member-declarator can contain a constant-initializer
16738                      only if it declares a static member of integral or
16739                      enumeration type.
16740
16741                      Therefore, if the DECLARATOR is for a function, we look
16742                      for a pure-specifier; otherwise, we look for a
16743                      constant-initializer.  When we call `grokfield', it will
16744                      perform more stringent semantics checks.  */
16745                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16746                   if (function_declarator_p (declarator))
16747                     initializer = cp_parser_pure_specifier (parser);
16748                   else
16749                     /* Parse the initializer.  */
16750                     initializer = cp_parser_constant_initializer (parser);
16751                 }
16752               /* Otherwise, there is no initializer.  */
16753               else
16754                 initializer = NULL_TREE;
16755
16756               /* See if we are probably looking at a function
16757                  definition.  We are certainly not looking at a
16758                  member-declarator.  Calling `grokfield' has
16759                  side-effects, so we must not do it unless we are sure
16760                  that we are looking at a member-declarator.  */
16761               if (cp_parser_token_starts_function_definition_p
16762                   (cp_lexer_peek_token (parser->lexer)))
16763                 {
16764                   /* The grammar does not allow a pure-specifier to be
16765                      used when a member function is defined.  (It is
16766                      possible that this fact is an oversight in the
16767                      standard, since a pure function may be defined
16768                      outside of the class-specifier.  */
16769                   if (initializer)
16770                     error_at (initializer_token_start->location,
16771                               "pure-specifier on function-definition");
16772                   decl = cp_parser_save_member_function_body (parser,
16773                                                               &decl_specifiers,
16774                                                               declarator,
16775                                                               attributes);
16776                   /* If the member was not a friend, declare it here.  */
16777                   if (!friend_p)
16778                     finish_member_declaration (decl);
16779                   /* Peek at the next token.  */
16780                   token = cp_lexer_peek_token (parser->lexer);
16781                   /* If the next token is a semicolon, consume it.  */
16782                   if (token->type == CPP_SEMICOLON)
16783                     cp_lexer_consume_token (parser->lexer);
16784                   return;
16785                 }
16786               else
16787                 if (declarator->kind == cdk_function)
16788                   declarator->id_loc = token->location;
16789                 /* Create the declaration.  */
16790                 decl = grokfield (declarator, &decl_specifiers,
16791                                   initializer, /*init_const_expr_p=*/true,
16792                                   asm_specification,
16793                                   attributes);
16794             }
16795
16796           /* Reset PREFIX_ATTRIBUTES.  */
16797           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16798             attributes = TREE_CHAIN (attributes);
16799           if (attributes)
16800             TREE_CHAIN (attributes) = NULL_TREE;
16801
16802           /* If there is any qualification still in effect, clear it
16803              now; we will be starting fresh with the next declarator.  */
16804           parser->scope = NULL_TREE;
16805           parser->qualifying_scope = NULL_TREE;
16806           parser->object_scope = NULL_TREE;
16807           /* If it's a `,', then there are more declarators.  */
16808           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16809             cp_lexer_consume_token (parser->lexer);
16810           /* If the next token isn't a `;', then we have a parse error.  */
16811           else if (cp_lexer_next_token_is_not (parser->lexer,
16812                                                CPP_SEMICOLON))
16813             {
16814               cp_parser_error (parser, "expected %<;%>");
16815               /* Skip tokens until we find a `;'.  */
16816               cp_parser_skip_to_end_of_statement (parser);
16817
16818               break;
16819             }
16820
16821           if (decl)
16822             {
16823               /* Add DECL to the list of members.  */
16824               if (!friend_p)
16825                 finish_member_declaration (decl);
16826
16827               if (TREE_CODE (decl) == FUNCTION_DECL)
16828                 cp_parser_save_default_args (parser, decl);
16829             }
16830         }
16831     }
16832
16833   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16834 }
16835
16836 /* Parse a pure-specifier.
16837
16838    pure-specifier:
16839      = 0
16840
16841    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16842    Otherwise, ERROR_MARK_NODE is returned.  */
16843
16844 static tree
16845 cp_parser_pure_specifier (cp_parser* parser)
16846 {
16847   cp_token *token;
16848
16849   /* Look for the `=' token.  */
16850   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16851     return error_mark_node;
16852   /* Look for the `0' token.  */
16853   token = cp_lexer_peek_token (parser->lexer);
16854
16855   if (token->type == CPP_EOF
16856       || token->type == CPP_PRAGMA_EOL)
16857     return error_mark_node;
16858
16859   cp_lexer_consume_token (parser->lexer);
16860
16861   /* Accept = default or = delete in c++0x mode.  */
16862   if (token->keyword == RID_DEFAULT
16863       || token->keyword == RID_DELETE)
16864     {
16865       maybe_warn_cpp0x ("defaulted and deleted functions");
16866       return token->u.value;
16867     }
16868
16869   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16870   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16871     {
16872       cp_parser_error (parser,
16873                        "invalid pure specifier (only %<= 0%> is allowed)");
16874       cp_parser_skip_to_end_of_statement (parser);
16875       return error_mark_node;
16876     }
16877   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16878     {
16879       error_at (token->location, "templates may not be %<virtual%>");
16880       return error_mark_node;
16881     }
16882
16883   return integer_zero_node;
16884 }
16885
16886 /* Parse a constant-initializer.
16887
16888    constant-initializer:
16889      = constant-expression
16890
16891    Returns a representation of the constant-expression.  */
16892
16893 static tree
16894 cp_parser_constant_initializer (cp_parser* parser)
16895 {
16896   /* Look for the `=' token.  */
16897   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16898     return error_mark_node;
16899
16900   /* It is invalid to write:
16901
16902        struct S { static const int i = { 7 }; };
16903
16904      */
16905   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16906     {
16907       cp_parser_error (parser,
16908                        "a brace-enclosed initializer is not allowed here");
16909       /* Consume the opening brace.  */
16910       cp_lexer_consume_token (parser->lexer);
16911       /* Skip the initializer.  */
16912       cp_parser_skip_to_closing_brace (parser);
16913       /* Look for the trailing `}'.  */
16914       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16915
16916       return error_mark_node;
16917     }
16918
16919   return cp_parser_constant_expression (parser,
16920                                         /*allow_non_constant=*/false,
16921                                         NULL);
16922 }
16923
16924 /* Derived classes [gram.class.derived] */
16925
16926 /* Parse a base-clause.
16927
16928    base-clause:
16929      : base-specifier-list
16930
16931    base-specifier-list:
16932      base-specifier ... [opt]
16933      base-specifier-list , base-specifier ... [opt]
16934
16935    Returns a TREE_LIST representing the base-classes, in the order in
16936    which they were declared.  The representation of each node is as
16937    described by cp_parser_base_specifier.
16938
16939    In the case that no bases are specified, this function will return
16940    NULL_TREE, not ERROR_MARK_NODE.  */
16941
16942 static tree
16943 cp_parser_base_clause (cp_parser* parser)
16944 {
16945   tree bases = NULL_TREE;
16946
16947   /* Look for the `:' that begins the list.  */
16948   cp_parser_require (parser, CPP_COLON, "%<:%>");
16949
16950   /* Scan the base-specifier-list.  */
16951   while (true)
16952     {
16953       cp_token *token;
16954       tree base;
16955       bool pack_expansion_p = false;
16956
16957       /* Look for the base-specifier.  */
16958       base = cp_parser_base_specifier (parser);
16959       /* Look for the (optional) ellipsis. */
16960       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16961         {
16962           /* Consume the `...'. */
16963           cp_lexer_consume_token (parser->lexer);
16964
16965           pack_expansion_p = true;
16966         }
16967
16968       /* Add BASE to the front of the list.  */
16969       if (base != error_mark_node)
16970         {
16971           if (pack_expansion_p)
16972             /* Make this a pack expansion type. */
16973             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16974           
16975
16976           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16977             {
16978               TREE_CHAIN (base) = bases;
16979               bases = base;
16980             }
16981         }
16982       /* Peek at the next token.  */
16983       token = cp_lexer_peek_token (parser->lexer);
16984       /* If it's not a comma, then the list is complete.  */
16985       if (token->type != CPP_COMMA)
16986         break;
16987       /* Consume the `,'.  */
16988       cp_lexer_consume_token (parser->lexer);
16989     }
16990
16991   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16992      base class had a qualified name.  However, the next name that
16993      appears is certainly not qualified.  */
16994   parser->scope = NULL_TREE;
16995   parser->qualifying_scope = NULL_TREE;
16996   parser->object_scope = NULL_TREE;
16997
16998   return nreverse (bases);
16999 }
17000
17001 /* Parse a base-specifier.
17002
17003    base-specifier:
17004      :: [opt] nested-name-specifier [opt] class-name
17005      virtual access-specifier [opt] :: [opt] nested-name-specifier
17006        [opt] class-name
17007      access-specifier virtual [opt] :: [opt] nested-name-specifier
17008        [opt] class-name
17009
17010    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17011    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17012    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17013    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17014
17015 static tree
17016 cp_parser_base_specifier (cp_parser* parser)
17017 {
17018   cp_token *token;
17019   bool done = false;
17020   bool virtual_p = false;
17021   bool duplicate_virtual_error_issued_p = false;
17022   bool duplicate_access_error_issued_p = false;
17023   bool class_scope_p, template_p;
17024   tree access = access_default_node;
17025   tree type;
17026
17027   /* Process the optional `virtual' and `access-specifier'.  */
17028   while (!done)
17029     {
17030       /* Peek at the next token.  */
17031       token = cp_lexer_peek_token (parser->lexer);
17032       /* Process `virtual'.  */
17033       switch (token->keyword)
17034         {
17035         case RID_VIRTUAL:
17036           /* If `virtual' appears more than once, issue an error.  */
17037           if (virtual_p && !duplicate_virtual_error_issued_p)
17038             {
17039               cp_parser_error (parser,
17040                                "%<virtual%> specified more than once in base-specified");
17041               duplicate_virtual_error_issued_p = true;
17042             }
17043
17044           virtual_p = true;
17045
17046           /* Consume the `virtual' token.  */
17047           cp_lexer_consume_token (parser->lexer);
17048
17049           break;
17050
17051         case RID_PUBLIC:
17052         case RID_PROTECTED:
17053         case RID_PRIVATE:
17054           /* If more than one access specifier appears, issue an
17055              error.  */
17056           if (access != access_default_node
17057               && !duplicate_access_error_issued_p)
17058             {
17059               cp_parser_error (parser,
17060                                "more than one access specifier in base-specified");
17061               duplicate_access_error_issued_p = true;
17062             }
17063
17064           access = ridpointers[(int) token->keyword];
17065
17066           /* Consume the access-specifier.  */
17067           cp_lexer_consume_token (parser->lexer);
17068
17069           break;
17070
17071         default:
17072           done = true;
17073           break;
17074         }
17075     }
17076   /* It is not uncommon to see programs mechanically, erroneously, use
17077      the 'typename' keyword to denote (dependent) qualified types
17078      as base classes.  */
17079   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17080     {
17081       token = cp_lexer_peek_token (parser->lexer);
17082       if (!processing_template_decl)
17083         error_at (token->location,
17084                   "keyword %<typename%> not allowed outside of templates");
17085       else
17086         error_at (token->location,
17087                   "keyword %<typename%> not allowed in this context "
17088                   "(the base class is implicitly a type)");
17089       cp_lexer_consume_token (parser->lexer);
17090     }
17091
17092   /* Look for the optional `::' operator.  */
17093   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17094   /* Look for the nested-name-specifier.  The simplest way to
17095      implement:
17096
17097        [temp.res]
17098
17099        The keyword `typename' is not permitted in a base-specifier or
17100        mem-initializer; in these contexts a qualified name that
17101        depends on a template-parameter is implicitly assumed to be a
17102        type name.
17103
17104      is to pretend that we have seen the `typename' keyword at this
17105      point.  */
17106   cp_parser_nested_name_specifier_opt (parser,
17107                                        /*typename_keyword_p=*/true,
17108                                        /*check_dependency_p=*/true,
17109                                        typename_type,
17110                                        /*is_declaration=*/true);
17111   /* If the base class is given by a qualified name, assume that names
17112      we see are type names or templates, as appropriate.  */
17113   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17114   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17115
17116   /* Finally, look for the class-name.  */
17117   type = cp_parser_class_name (parser,
17118                                class_scope_p,
17119                                template_p,
17120                                typename_type,
17121                                /*check_dependency_p=*/true,
17122                                /*class_head_p=*/false,
17123                                /*is_declaration=*/true);
17124
17125   if (type == error_mark_node)
17126     return error_mark_node;
17127
17128   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17129 }
17130
17131 /* Exception handling [gram.exception] */
17132
17133 /* Parse an (optional) exception-specification.
17134
17135    exception-specification:
17136      throw ( type-id-list [opt] )
17137
17138    Returns a TREE_LIST representing the exception-specification.  The
17139    TREE_VALUE of each node is a type.  */
17140
17141 static tree
17142 cp_parser_exception_specification_opt (cp_parser* parser)
17143 {
17144   cp_token *token;
17145   tree type_id_list;
17146
17147   /* Peek at the next token.  */
17148   token = cp_lexer_peek_token (parser->lexer);
17149   /* If it's not `throw', then there's no exception-specification.  */
17150   if (!cp_parser_is_keyword (token, RID_THROW))
17151     return NULL_TREE;
17152
17153   /* Consume the `throw'.  */
17154   cp_lexer_consume_token (parser->lexer);
17155
17156   /* Look for the `('.  */
17157   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17158
17159   /* Peek at the next token.  */
17160   token = cp_lexer_peek_token (parser->lexer);
17161   /* If it's not a `)', then there is a type-id-list.  */
17162   if (token->type != CPP_CLOSE_PAREN)
17163     {
17164       const char *saved_message;
17165
17166       /* Types may not be defined in an exception-specification.  */
17167       saved_message = parser->type_definition_forbidden_message;
17168       parser->type_definition_forbidden_message
17169         = "types may not be defined in an exception-specification";
17170       /* Parse the type-id-list.  */
17171       type_id_list = cp_parser_type_id_list (parser);
17172       /* Restore the saved message.  */
17173       parser->type_definition_forbidden_message = saved_message;
17174     }
17175   else
17176     type_id_list = empty_except_spec;
17177
17178   /* Look for the `)'.  */
17179   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17180
17181   return type_id_list;
17182 }
17183
17184 /* Parse an (optional) type-id-list.
17185
17186    type-id-list:
17187      type-id ... [opt]
17188      type-id-list , type-id ... [opt]
17189
17190    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17191    in the order that the types were presented.  */
17192
17193 static tree
17194 cp_parser_type_id_list (cp_parser* parser)
17195 {
17196   tree types = NULL_TREE;
17197
17198   while (true)
17199     {
17200       cp_token *token;
17201       tree type;
17202
17203       /* Get the next type-id.  */
17204       type = cp_parser_type_id (parser);
17205       /* Parse the optional ellipsis. */
17206       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17207         {
17208           /* Consume the `...'. */
17209           cp_lexer_consume_token (parser->lexer);
17210
17211           /* Turn the type into a pack expansion expression. */
17212           type = make_pack_expansion (type);
17213         }
17214       /* Add it to the list.  */
17215       types = add_exception_specifier (types, type, /*complain=*/1);
17216       /* Peek at the next token.  */
17217       token = cp_lexer_peek_token (parser->lexer);
17218       /* If it is not a `,', we are done.  */
17219       if (token->type != CPP_COMMA)
17220         break;
17221       /* Consume the `,'.  */
17222       cp_lexer_consume_token (parser->lexer);
17223     }
17224
17225   return nreverse (types);
17226 }
17227
17228 /* Parse a try-block.
17229
17230    try-block:
17231      try compound-statement handler-seq  */
17232
17233 static tree
17234 cp_parser_try_block (cp_parser* parser)
17235 {
17236   tree try_block;
17237
17238   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17239   try_block = begin_try_block ();
17240   cp_parser_compound_statement (parser, NULL, true);
17241   finish_try_block (try_block);
17242   cp_parser_handler_seq (parser);
17243   finish_handler_sequence (try_block);
17244
17245   return try_block;
17246 }
17247
17248 /* Parse a function-try-block.
17249
17250    function-try-block:
17251      try ctor-initializer [opt] function-body handler-seq  */
17252
17253 static bool
17254 cp_parser_function_try_block (cp_parser* parser)
17255 {
17256   tree compound_stmt;
17257   tree try_block;
17258   bool ctor_initializer_p;
17259
17260   /* Look for the `try' keyword.  */
17261   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17262     return false;
17263   /* Let the rest of the front end know where we are.  */
17264   try_block = begin_function_try_block (&compound_stmt);
17265   /* Parse the function-body.  */
17266   ctor_initializer_p
17267     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17268   /* We're done with the `try' part.  */
17269   finish_function_try_block (try_block);
17270   /* Parse the handlers.  */
17271   cp_parser_handler_seq (parser);
17272   /* We're done with the handlers.  */
17273   finish_function_handler_sequence (try_block, compound_stmt);
17274
17275   return ctor_initializer_p;
17276 }
17277
17278 /* Parse a handler-seq.
17279
17280    handler-seq:
17281      handler handler-seq [opt]  */
17282
17283 static void
17284 cp_parser_handler_seq (cp_parser* parser)
17285 {
17286   while (true)
17287     {
17288       cp_token *token;
17289
17290       /* Parse the handler.  */
17291       cp_parser_handler (parser);
17292       /* Peek at the next token.  */
17293       token = cp_lexer_peek_token (parser->lexer);
17294       /* If it's not `catch' then there are no more handlers.  */
17295       if (!cp_parser_is_keyword (token, RID_CATCH))
17296         break;
17297     }
17298 }
17299
17300 /* Parse a handler.
17301
17302    handler:
17303      catch ( exception-declaration ) compound-statement  */
17304
17305 static void
17306 cp_parser_handler (cp_parser* parser)
17307 {
17308   tree handler;
17309   tree declaration;
17310
17311   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17312   handler = begin_handler ();
17313   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17314   declaration = cp_parser_exception_declaration (parser);
17315   finish_handler_parms (declaration, handler);
17316   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17317   cp_parser_compound_statement (parser, NULL, false);
17318   finish_handler (handler);
17319 }
17320
17321 /* Parse an exception-declaration.
17322
17323    exception-declaration:
17324      type-specifier-seq declarator
17325      type-specifier-seq abstract-declarator
17326      type-specifier-seq
17327      ...
17328
17329    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17330    ellipsis variant is used.  */
17331
17332 static tree
17333 cp_parser_exception_declaration (cp_parser* parser)
17334 {
17335   cp_decl_specifier_seq type_specifiers;
17336   cp_declarator *declarator;
17337   const char *saved_message;
17338
17339   /* If it's an ellipsis, it's easy to handle.  */
17340   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17341     {
17342       /* Consume the `...' token.  */
17343       cp_lexer_consume_token (parser->lexer);
17344       return NULL_TREE;
17345     }
17346
17347   /* Types may not be defined in exception-declarations.  */
17348   saved_message = parser->type_definition_forbidden_message;
17349   parser->type_definition_forbidden_message
17350     = "types may not be defined in exception-declarations";
17351
17352   /* Parse the type-specifier-seq.  */
17353   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17354                                 /*is_trailing_return=*/false,
17355                                 &type_specifiers);
17356   /* If it's a `)', then there is no declarator.  */
17357   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17358     declarator = NULL;
17359   else
17360     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17361                                        /*ctor_dtor_or_conv_p=*/NULL,
17362                                        /*parenthesized_p=*/NULL,
17363                                        /*member_p=*/false);
17364
17365   /* Restore the saved message.  */
17366   parser->type_definition_forbidden_message = saved_message;
17367
17368   if (!type_specifiers.any_specifiers_p)
17369     return error_mark_node;
17370
17371   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17372 }
17373
17374 /* Parse a throw-expression.
17375
17376    throw-expression:
17377      throw assignment-expression [opt]
17378
17379    Returns a THROW_EXPR representing the throw-expression.  */
17380
17381 static tree
17382 cp_parser_throw_expression (cp_parser* parser)
17383 {
17384   tree expression;
17385   cp_token* token;
17386
17387   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17388   token = cp_lexer_peek_token (parser->lexer);
17389   /* Figure out whether or not there is an assignment-expression
17390      following the "throw" keyword.  */
17391   if (token->type == CPP_COMMA
17392       || token->type == CPP_SEMICOLON
17393       || token->type == CPP_CLOSE_PAREN
17394       || token->type == CPP_CLOSE_SQUARE
17395       || token->type == CPP_CLOSE_BRACE
17396       || token->type == CPP_COLON)
17397     expression = NULL_TREE;
17398   else
17399     expression = cp_parser_assignment_expression (parser,
17400                                                   /*cast_p=*/false, NULL);
17401
17402   return build_throw (expression);
17403 }
17404
17405 /* GNU Extensions */
17406
17407 /* Parse an (optional) asm-specification.
17408
17409    asm-specification:
17410      asm ( string-literal )
17411
17412    If the asm-specification is present, returns a STRING_CST
17413    corresponding to the string-literal.  Otherwise, returns
17414    NULL_TREE.  */
17415
17416 static tree
17417 cp_parser_asm_specification_opt (cp_parser* parser)
17418 {
17419   cp_token *token;
17420   tree asm_specification;
17421
17422   /* Peek at the next token.  */
17423   token = cp_lexer_peek_token (parser->lexer);
17424   /* If the next token isn't the `asm' keyword, then there's no
17425      asm-specification.  */
17426   if (!cp_parser_is_keyword (token, RID_ASM))
17427     return NULL_TREE;
17428
17429   /* Consume the `asm' token.  */
17430   cp_lexer_consume_token (parser->lexer);
17431   /* Look for the `('.  */
17432   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17433
17434   /* Look for the string-literal.  */
17435   asm_specification = cp_parser_string_literal (parser, false, false);
17436
17437   /* Look for the `)'.  */
17438   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17439
17440   return asm_specification;
17441 }
17442
17443 /* Parse an asm-operand-list.
17444
17445    asm-operand-list:
17446      asm-operand
17447      asm-operand-list , asm-operand
17448
17449    asm-operand:
17450      string-literal ( expression )
17451      [ string-literal ] string-literal ( expression )
17452
17453    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17454    each node is the expression.  The TREE_PURPOSE is itself a
17455    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17456    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17457    is a STRING_CST for the string literal before the parenthesis. Returns
17458    ERROR_MARK_NODE if any of the operands are invalid.  */
17459
17460 static tree
17461 cp_parser_asm_operand_list (cp_parser* parser)
17462 {
17463   tree asm_operands = NULL_TREE;
17464   bool invalid_operands = false;
17465
17466   while (true)
17467     {
17468       tree string_literal;
17469       tree expression;
17470       tree name;
17471
17472       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17473         {
17474           /* Consume the `[' token.  */
17475           cp_lexer_consume_token (parser->lexer);
17476           /* Read the operand name.  */
17477           name = cp_parser_identifier (parser);
17478           if (name != error_mark_node)
17479             name = build_string (IDENTIFIER_LENGTH (name),
17480                                  IDENTIFIER_POINTER (name));
17481           /* Look for the closing `]'.  */
17482           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17483         }
17484       else
17485         name = NULL_TREE;
17486       /* Look for the string-literal.  */
17487       string_literal = cp_parser_string_literal (parser, false, false);
17488
17489       /* Look for the `('.  */
17490       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17491       /* Parse the expression.  */
17492       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17493       /* Look for the `)'.  */
17494       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17495
17496       if (name == error_mark_node 
17497           || string_literal == error_mark_node 
17498           || expression == error_mark_node)
17499         invalid_operands = true;
17500
17501       /* Add this operand to the list.  */
17502       asm_operands = tree_cons (build_tree_list (name, string_literal),
17503                                 expression,
17504                                 asm_operands);
17505       /* If the next token is not a `,', there are no more
17506          operands.  */
17507       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17508         break;
17509       /* Consume the `,'.  */
17510       cp_lexer_consume_token (parser->lexer);
17511     }
17512
17513   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17514 }
17515
17516 /* Parse an asm-clobber-list.
17517
17518    asm-clobber-list:
17519      string-literal
17520      asm-clobber-list , string-literal
17521
17522    Returns a TREE_LIST, indicating the clobbers in the order that they
17523    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17524
17525 static tree
17526 cp_parser_asm_clobber_list (cp_parser* parser)
17527 {
17528   tree clobbers = NULL_TREE;
17529
17530   while (true)
17531     {
17532       tree string_literal;
17533
17534       /* Look for the string literal.  */
17535       string_literal = cp_parser_string_literal (parser, false, false);
17536       /* Add it to the list.  */
17537       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17538       /* If the next token is not a `,', then the list is
17539          complete.  */
17540       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17541         break;
17542       /* Consume the `,' token.  */
17543       cp_lexer_consume_token (parser->lexer);
17544     }
17545
17546   return clobbers;
17547 }
17548
17549 /* Parse an asm-label-list.
17550
17551    asm-label-list:
17552      identifier
17553      asm-label-list , identifier
17554
17555    Returns a TREE_LIST, indicating the labels in the order that they
17556    appeared.  The TREE_VALUE of each node is a label.  */
17557
17558 static tree
17559 cp_parser_asm_label_list (cp_parser* parser)
17560 {
17561   tree labels = NULL_TREE;
17562
17563   while (true)
17564     {
17565       tree identifier, label, name;
17566
17567       /* Look for the identifier.  */
17568       identifier = cp_parser_identifier (parser);
17569       if (!error_operand_p (identifier))
17570         {
17571           label = lookup_label (identifier);
17572           if (TREE_CODE (label) == LABEL_DECL)
17573             {
17574               TREE_USED (label) = 1;
17575               check_goto (label);
17576               name = build_string (IDENTIFIER_LENGTH (identifier),
17577                                    IDENTIFIER_POINTER (identifier));
17578               labels = tree_cons (name, label, labels);
17579             }
17580         }
17581       /* If the next token is not a `,', then the list is
17582          complete.  */
17583       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17584         break;
17585       /* Consume the `,' token.  */
17586       cp_lexer_consume_token (parser->lexer);
17587     }
17588
17589   return nreverse (labels);
17590 }
17591
17592 /* Parse an (optional) series of attributes.
17593
17594    attributes:
17595      attributes attribute
17596
17597    attribute:
17598      __attribute__ (( attribute-list [opt] ))
17599
17600    The return value is as for cp_parser_attribute_list.  */
17601
17602 static tree
17603 cp_parser_attributes_opt (cp_parser* parser)
17604 {
17605   tree attributes = NULL_TREE;
17606
17607   while (true)
17608     {
17609       cp_token *token;
17610       tree attribute_list;
17611
17612       /* Peek at the next token.  */
17613       token = cp_lexer_peek_token (parser->lexer);
17614       /* If it's not `__attribute__', then we're done.  */
17615       if (token->keyword != RID_ATTRIBUTE)
17616         break;
17617
17618       /* Consume the `__attribute__' keyword.  */
17619       cp_lexer_consume_token (parser->lexer);
17620       /* Look for the two `(' tokens.  */
17621       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17622       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17623
17624       /* Peek at the next token.  */
17625       token = cp_lexer_peek_token (parser->lexer);
17626       if (token->type != CPP_CLOSE_PAREN)
17627         /* Parse the attribute-list.  */
17628         attribute_list = cp_parser_attribute_list (parser);
17629       else
17630         /* If the next token is a `)', then there is no attribute
17631            list.  */
17632         attribute_list = NULL;
17633
17634       /* Look for the two `)' tokens.  */
17635       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17636       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17637
17638       /* Add these new attributes to the list.  */
17639       attributes = chainon (attributes, attribute_list);
17640     }
17641
17642   return attributes;
17643 }
17644
17645 /* Parse an attribute-list.
17646
17647    attribute-list:
17648      attribute
17649      attribute-list , attribute
17650
17651    attribute:
17652      identifier
17653      identifier ( identifier )
17654      identifier ( identifier , expression-list )
17655      identifier ( expression-list )
17656
17657    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17658    to an attribute.  The TREE_PURPOSE of each node is the identifier
17659    indicating which attribute is in use.  The TREE_VALUE represents
17660    the arguments, if any.  */
17661
17662 static tree
17663 cp_parser_attribute_list (cp_parser* parser)
17664 {
17665   tree attribute_list = NULL_TREE;
17666   bool save_translate_strings_p = parser->translate_strings_p;
17667
17668   parser->translate_strings_p = false;
17669   while (true)
17670     {
17671       cp_token *token;
17672       tree identifier;
17673       tree attribute;
17674
17675       /* Look for the identifier.  We also allow keywords here; for
17676          example `__attribute__ ((const))' is legal.  */
17677       token = cp_lexer_peek_token (parser->lexer);
17678       if (token->type == CPP_NAME
17679           || token->type == CPP_KEYWORD)
17680         {
17681           tree arguments = NULL_TREE;
17682
17683           /* Consume the token.  */
17684           token = cp_lexer_consume_token (parser->lexer);
17685
17686           /* Save away the identifier that indicates which attribute
17687              this is.  */
17688           identifier = (token->type == CPP_KEYWORD) 
17689             /* For keywords, use the canonical spelling, not the
17690                parsed identifier.  */
17691             ? ridpointers[(int) token->keyword]
17692             : token->u.value;
17693           
17694           attribute = build_tree_list (identifier, NULL_TREE);
17695
17696           /* Peek at the next token.  */
17697           token = cp_lexer_peek_token (parser->lexer);
17698           /* If it's an `(', then parse the attribute arguments.  */
17699           if (token->type == CPP_OPEN_PAREN)
17700             {
17701               VEC(tree,gc) *vec;
17702               vec = cp_parser_parenthesized_expression_list
17703                     (parser, true, /*cast_p=*/false,
17704                      /*allow_expansion_p=*/false,
17705                      /*non_constant_p=*/NULL);
17706               if (vec == NULL)
17707                 arguments = error_mark_node;
17708               else
17709                 {
17710                   arguments = build_tree_list_vec (vec);
17711                   release_tree_vector (vec);
17712                 }
17713               /* Save the arguments away.  */
17714               TREE_VALUE (attribute) = arguments;
17715             }
17716
17717           if (arguments != error_mark_node)
17718             {
17719               /* Add this attribute to the list.  */
17720               TREE_CHAIN (attribute) = attribute_list;
17721               attribute_list = attribute;
17722             }
17723
17724           token = cp_lexer_peek_token (parser->lexer);
17725         }
17726       /* Now, look for more attributes.  If the next token isn't a
17727          `,', we're done.  */
17728       if (token->type != CPP_COMMA)
17729         break;
17730
17731       /* Consume the comma and keep going.  */
17732       cp_lexer_consume_token (parser->lexer);
17733     }
17734   parser->translate_strings_p = save_translate_strings_p;
17735
17736   /* We built up the list in reverse order.  */
17737   return nreverse (attribute_list);
17738 }
17739
17740 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17741    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17742    current value of the PEDANTIC flag, regardless of whether or not
17743    the `__extension__' keyword is present.  The caller is responsible
17744    for restoring the value of the PEDANTIC flag.  */
17745
17746 static bool
17747 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17748 {
17749   /* Save the old value of the PEDANTIC flag.  */
17750   *saved_pedantic = pedantic;
17751
17752   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17753     {
17754       /* Consume the `__extension__' token.  */
17755       cp_lexer_consume_token (parser->lexer);
17756       /* We're not being pedantic while the `__extension__' keyword is
17757          in effect.  */
17758       pedantic = 0;
17759
17760       return true;
17761     }
17762
17763   return false;
17764 }
17765
17766 /* Parse a label declaration.
17767
17768    label-declaration:
17769      __label__ label-declarator-seq ;
17770
17771    label-declarator-seq:
17772      identifier , label-declarator-seq
17773      identifier  */
17774
17775 static void
17776 cp_parser_label_declaration (cp_parser* parser)
17777 {
17778   /* Look for the `__label__' keyword.  */
17779   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17780
17781   while (true)
17782     {
17783       tree identifier;
17784
17785       /* Look for an identifier.  */
17786       identifier = cp_parser_identifier (parser);
17787       /* If we failed, stop.  */
17788       if (identifier == error_mark_node)
17789         break;
17790       /* Declare it as a label.  */
17791       finish_label_decl (identifier);
17792       /* If the next token is a `;', stop.  */
17793       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17794         break;
17795       /* Look for the `,' separating the label declarations.  */
17796       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17797     }
17798
17799   /* Look for the final `;'.  */
17800   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17801 }
17802
17803 /* Support Functions */
17804
17805 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17806    NAME should have one of the representations used for an
17807    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17808    is returned.  If PARSER->SCOPE is a dependent type, then a
17809    SCOPE_REF is returned.
17810
17811    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17812    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17813    was formed.  Abstractly, such entities should not be passed to this
17814    function, because they do not need to be looked up, but it is
17815    simpler to check for this special case here, rather than at the
17816    call-sites.
17817
17818    In cases not explicitly covered above, this function returns a
17819    DECL, OVERLOAD, or baselink representing the result of the lookup.
17820    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17821    is returned.
17822
17823    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17824    (e.g., "struct") that was used.  In that case bindings that do not
17825    refer to types are ignored.
17826
17827    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17828    ignored.
17829
17830    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17831    are ignored.
17832
17833    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17834    types.
17835
17836    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17837    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17838    NULL_TREE otherwise.  */
17839
17840 static tree
17841 cp_parser_lookup_name (cp_parser *parser, tree name,
17842                        enum tag_types tag_type,
17843                        bool is_template,
17844                        bool is_namespace,
17845                        bool check_dependency,
17846                        tree *ambiguous_decls,
17847                        location_t name_location)
17848 {
17849   int flags = 0;
17850   tree decl;
17851   tree object_type = parser->context->object_type;
17852
17853   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17854     flags |= LOOKUP_COMPLAIN;
17855
17856   /* Assume that the lookup will be unambiguous.  */
17857   if (ambiguous_decls)
17858     *ambiguous_decls = NULL_TREE;
17859
17860   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17861      no longer valid.  Note that if we are parsing tentatively, and
17862      the parse fails, OBJECT_TYPE will be automatically restored.  */
17863   parser->context->object_type = NULL_TREE;
17864
17865   if (name == error_mark_node)
17866     return error_mark_node;
17867
17868   /* A template-id has already been resolved; there is no lookup to
17869      do.  */
17870   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17871     return name;
17872   if (BASELINK_P (name))
17873     {
17874       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17875                   == TEMPLATE_ID_EXPR);
17876       return name;
17877     }
17878
17879   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17880      it should already have been checked to make sure that the name
17881      used matches the type being destroyed.  */
17882   if (TREE_CODE (name) == BIT_NOT_EXPR)
17883     {
17884       tree type;
17885
17886       /* Figure out to which type this destructor applies.  */
17887       if (parser->scope)
17888         type = parser->scope;
17889       else if (object_type)
17890         type = object_type;
17891       else
17892         type = current_class_type;
17893       /* If that's not a class type, there is no destructor.  */
17894       if (!type || !CLASS_TYPE_P (type))
17895         return error_mark_node;
17896       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17897         lazily_declare_fn (sfk_destructor, type);
17898       if (!CLASSTYPE_DESTRUCTORS (type))
17899           return error_mark_node;
17900       /* If it was a class type, return the destructor.  */
17901       return CLASSTYPE_DESTRUCTORS (type);
17902     }
17903
17904   /* By this point, the NAME should be an ordinary identifier.  If
17905      the id-expression was a qualified name, the qualifying scope is
17906      stored in PARSER->SCOPE at this point.  */
17907   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17908
17909   /* Perform the lookup.  */
17910   if (parser->scope)
17911     {
17912       bool dependent_p;
17913
17914       if (parser->scope == error_mark_node)
17915         return error_mark_node;
17916
17917       /* If the SCOPE is dependent, the lookup must be deferred until
17918          the template is instantiated -- unless we are explicitly
17919          looking up names in uninstantiated templates.  Even then, we
17920          cannot look up the name if the scope is not a class type; it
17921          might, for example, be a template type parameter.  */
17922       dependent_p = (TYPE_P (parser->scope)
17923                      && dependent_scope_p (parser->scope));
17924       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17925           && dependent_p)
17926         /* Defer lookup.  */
17927         decl = error_mark_node;
17928       else
17929         {
17930           tree pushed_scope = NULL_TREE;
17931
17932           /* If PARSER->SCOPE is a dependent type, then it must be a
17933              class type, and we must not be checking dependencies;
17934              otherwise, we would have processed this lookup above.  So
17935              that PARSER->SCOPE is not considered a dependent base by
17936              lookup_member, we must enter the scope here.  */
17937           if (dependent_p)
17938             pushed_scope = push_scope (parser->scope);
17939           /* If the PARSER->SCOPE is a template specialization, it
17940              may be instantiated during name lookup.  In that case,
17941              errors may be issued.  Even if we rollback the current
17942              tentative parse, those errors are valid.  */
17943           decl = lookup_qualified_name (parser->scope, name,
17944                                         tag_type != none_type,
17945                                         /*complain=*/true);
17946
17947           /* If we have a single function from a using decl, pull it out.  */
17948           if (TREE_CODE (decl) == OVERLOAD
17949               && !really_overloaded_fn (decl))
17950             decl = OVL_FUNCTION (decl);
17951
17952           if (pushed_scope)
17953             pop_scope (pushed_scope);
17954         }
17955
17956       /* If the scope is a dependent type and either we deferred lookup or
17957          we did lookup but didn't find the name, rememeber the name.  */
17958       if (decl == error_mark_node && TYPE_P (parser->scope)
17959           && dependent_type_p (parser->scope))
17960         {
17961           if (tag_type)
17962             {
17963               tree type;
17964
17965               /* The resolution to Core Issue 180 says that `struct
17966                  A::B' should be considered a type-name, even if `A'
17967                  is dependent.  */
17968               type = make_typename_type (parser->scope, name, tag_type,
17969                                          /*complain=*/tf_error);
17970               decl = TYPE_NAME (type);
17971             }
17972           else if (is_template
17973                    && (cp_parser_next_token_ends_template_argument_p (parser)
17974                        || cp_lexer_next_token_is (parser->lexer,
17975                                                   CPP_CLOSE_PAREN)))
17976             decl = make_unbound_class_template (parser->scope,
17977                                                 name, NULL_TREE,
17978                                                 /*complain=*/tf_error);
17979           else
17980             decl = build_qualified_name (/*type=*/NULL_TREE,
17981                                          parser->scope, name,
17982                                          is_template);
17983         }
17984       parser->qualifying_scope = parser->scope;
17985       parser->object_scope = NULL_TREE;
17986     }
17987   else if (object_type)
17988     {
17989       tree object_decl = NULL_TREE;
17990       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17991          OBJECT_TYPE is not a class.  */
17992       if (CLASS_TYPE_P (object_type))
17993         /* If the OBJECT_TYPE is a template specialization, it may
17994            be instantiated during name lookup.  In that case, errors
17995            may be issued.  Even if we rollback the current tentative
17996            parse, those errors are valid.  */
17997         object_decl = lookup_member (object_type,
17998                                      name,
17999                                      /*protect=*/0,
18000                                      tag_type != none_type);
18001       /* Look it up in the enclosing context, too.  */
18002       decl = lookup_name_real (name, tag_type != none_type,
18003                                /*nonclass=*/0,
18004                                /*block_p=*/true, is_namespace, flags);
18005       parser->object_scope = object_type;
18006       parser->qualifying_scope = NULL_TREE;
18007       if (object_decl)
18008         decl = object_decl;
18009     }
18010   else
18011     {
18012       decl = lookup_name_real (name, tag_type != none_type,
18013                                /*nonclass=*/0,
18014                                /*block_p=*/true, is_namespace, flags);
18015       parser->qualifying_scope = NULL_TREE;
18016       parser->object_scope = NULL_TREE;
18017     }
18018
18019   /* If the lookup failed, let our caller know.  */
18020   if (!decl || decl == error_mark_node)
18021     return error_mark_node;
18022
18023   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18024   if (TREE_CODE (decl) == TREE_LIST)
18025     {
18026       if (ambiguous_decls)
18027         *ambiguous_decls = decl;
18028       /* The error message we have to print is too complicated for
18029          cp_parser_error, so we incorporate its actions directly.  */
18030       if (!cp_parser_simulate_error (parser))
18031         {
18032           error_at (name_location, "reference to %qD is ambiguous",
18033                     name);
18034           print_candidates (decl);
18035         }
18036       return error_mark_node;
18037     }
18038
18039   gcc_assert (DECL_P (decl)
18040               || TREE_CODE (decl) == OVERLOAD
18041               || TREE_CODE (decl) == SCOPE_REF
18042               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18043               || BASELINK_P (decl));
18044
18045   /* If we have resolved the name of a member declaration, check to
18046      see if the declaration is accessible.  When the name resolves to
18047      set of overloaded functions, accessibility is checked when
18048      overload resolution is done.
18049
18050      During an explicit instantiation, access is not checked at all,
18051      as per [temp.explicit].  */
18052   if (DECL_P (decl))
18053     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18054
18055   return decl;
18056 }
18057
18058 /* Like cp_parser_lookup_name, but for use in the typical case where
18059    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18060    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18061
18062 static tree
18063 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18064 {
18065   return cp_parser_lookup_name (parser, name,
18066                                 none_type,
18067                                 /*is_template=*/false,
18068                                 /*is_namespace=*/false,
18069                                 /*check_dependency=*/true,
18070                                 /*ambiguous_decls=*/NULL,
18071                                 location);
18072 }
18073
18074 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18075    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18076    true, the DECL indicates the class being defined in a class-head,
18077    or declared in an elaborated-type-specifier.
18078
18079    Otherwise, return DECL.  */
18080
18081 static tree
18082 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18083 {
18084   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18085      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18086
18087        struct A {
18088          template <typename T> struct B;
18089        };
18090
18091        template <typename T> struct A::B {};
18092
18093      Similarly, in an elaborated-type-specifier:
18094
18095        namespace N { struct X{}; }
18096
18097        struct A {
18098          template <typename T> friend struct N::X;
18099        };
18100
18101      However, if the DECL refers to a class type, and we are in
18102      the scope of the class, then the name lookup automatically
18103      finds the TYPE_DECL created by build_self_reference rather
18104      than a TEMPLATE_DECL.  For example, in:
18105
18106        template <class T> struct S {
18107          S s;
18108        };
18109
18110      there is no need to handle such case.  */
18111
18112   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18113     return DECL_TEMPLATE_RESULT (decl);
18114
18115   return decl;
18116 }
18117
18118 /* If too many, or too few, template-parameter lists apply to the
18119    declarator, issue an error message.  Returns TRUE if all went well,
18120    and FALSE otherwise.  */
18121
18122 static bool
18123 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18124                                                 cp_declarator *declarator,
18125                                                 location_t declarator_location)
18126 {
18127   unsigned num_templates;
18128
18129   /* We haven't seen any classes that involve template parameters yet.  */
18130   num_templates = 0;
18131
18132   switch (declarator->kind)
18133     {
18134     case cdk_id:
18135       if (declarator->u.id.qualifying_scope)
18136         {
18137           tree scope;
18138           tree member;
18139
18140           scope = declarator->u.id.qualifying_scope;
18141           member = declarator->u.id.unqualified_name;
18142
18143           while (scope && CLASS_TYPE_P (scope))
18144             {
18145               /* You're supposed to have one `template <...>'
18146                  for every template class, but you don't need one
18147                  for a full specialization.  For example:
18148
18149                  template <class T> struct S{};
18150                  template <> struct S<int> { void f(); };
18151                  void S<int>::f () {}
18152
18153                  is correct; there shouldn't be a `template <>' for
18154                  the definition of `S<int>::f'.  */
18155               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18156                 /* If SCOPE does not have template information of any
18157                    kind, then it is not a template, nor is it nested
18158                    within a template.  */
18159                 break;
18160               if (explicit_class_specialization_p (scope))
18161                 break;
18162               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18163                 ++num_templates;
18164
18165               scope = TYPE_CONTEXT (scope);
18166             }
18167         }
18168       else if (TREE_CODE (declarator->u.id.unqualified_name)
18169                == TEMPLATE_ID_EXPR)
18170         /* If the DECLARATOR has the form `X<y>' then it uses one
18171            additional level of template parameters.  */
18172         ++num_templates;
18173
18174       return cp_parser_check_template_parameters 
18175         (parser, num_templates, declarator_location, declarator);
18176
18177
18178     case cdk_function:
18179     case cdk_array:
18180     case cdk_pointer:
18181     case cdk_reference:
18182     case cdk_ptrmem:
18183       return (cp_parser_check_declarator_template_parameters
18184               (parser, declarator->declarator, declarator_location));
18185
18186     case cdk_error:
18187       return true;
18188
18189     default:
18190       gcc_unreachable ();
18191     }
18192   return false;
18193 }
18194
18195 /* NUM_TEMPLATES were used in the current declaration.  If that is
18196    invalid, return FALSE and issue an error messages.  Otherwise,
18197    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18198    declarator and we can print more accurate diagnostics.  */
18199
18200 static bool
18201 cp_parser_check_template_parameters (cp_parser* parser,
18202                                      unsigned num_templates,
18203                                      location_t location,
18204                                      cp_declarator *declarator)
18205 {
18206   /* If there are the same number of template classes and parameter
18207      lists, that's OK.  */
18208   if (parser->num_template_parameter_lists == num_templates)
18209     return true;
18210   /* If there are more, but only one more, then we are referring to a
18211      member template.  That's OK too.  */
18212   if (parser->num_template_parameter_lists == num_templates + 1)
18213     return true;
18214   /* If there are more template classes than parameter lists, we have
18215      something like:
18216
18217        template <class T> void S<T>::R<T>::f ();  */
18218   if (parser->num_template_parameter_lists < num_templates)
18219     {
18220       if (declarator && !current_function_decl)
18221         error_at (location, "specializing member %<%T::%E%> "
18222                   "requires %<template<>%> syntax", 
18223                   declarator->u.id.qualifying_scope,
18224                   declarator->u.id.unqualified_name);
18225       else if (declarator)
18226         error_at (location, "invalid declaration of %<%T::%E%>",
18227                   declarator->u.id.qualifying_scope,
18228                   declarator->u.id.unqualified_name);
18229       else 
18230         error_at (location, "too few template-parameter-lists");
18231       return false;
18232     }
18233   /* Otherwise, there are too many template parameter lists.  We have
18234      something like:
18235
18236      template <class T> template <class U> void S::f();  */
18237   error_at (location, "too many template-parameter-lists");
18238   return false;
18239 }
18240
18241 /* Parse an optional `::' token indicating that the following name is
18242    from the global namespace.  If so, PARSER->SCOPE is set to the
18243    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18244    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18245    Returns the new value of PARSER->SCOPE, if the `::' token is
18246    present, and NULL_TREE otherwise.  */
18247
18248 static tree
18249 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18250 {
18251   cp_token *token;
18252
18253   /* Peek at the next token.  */
18254   token = cp_lexer_peek_token (parser->lexer);
18255   /* If we're looking at a `::' token then we're starting from the
18256      global namespace, not our current location.  */
18257   if (token->type == CPP_SCOPE)
18258     {
18259       /* Consume the `::' token.  */
18260       cp_lexer_consume_token (parser->lexer);
18261       /* Set the SCOPE so that we know where to start the lookup.  */
18262       parser->scope = global_namespace;
18263       parser->qualifying_scope = global_namespace;
18264       parser->object_scope = NULL_TREE;
18265
18266       return parser->scope;
18267     }
18268   else if (!current_scope_valid_p)
18269     {
18270       parser->scope = NULL_TREE;
18271       parser->qualifying_scope = NULL_TREE;
18272       parser->object_scope = NULL_TREE;
18273     }
18274
18275   return NULL_TREE;
18276 }
18277
18278 /* Returns TRUE if the upcoming token sequence is the start of a
18279    constructor declarator.  If FRIEND_P is true, the declarator is
18280    preceded by the `friend' specifier.  */
18281
18282 static bool
18283 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18284 {
18285   bool constructor_p;
18286   tree type_decl = NULL_TREE;
18287   bool nested_name_p;
18288   cp_token *next_token;
18289
18290   /* The common case is that this is not a constructor declarator, so
18291      try to avoid doing lots of work if at all possible.  It's not
18292      valid declare a constructor at function scope.  */
18293   if (parser->in_function_body)
18294     return false;
18295   /* And only certain tokens can begin a constructor declarator.  */
18296   next_token = cp_lexer_peek_token (parser->lexer);
18297   if (next_token->type != CPP_NAME
18298       && next_token->type != CPP_SCOPE
18299       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18300       && next_token->type != CPP_TEMPLATE_ID)
18301     return false;
18302
18303   /* Parse tentatively; we are going to roll back all of the tokens
18304      consumed here.  */
18305   cp_parser_parse_tentatively (parser);
18306   /* Assume that we are looking at a constructor declarator.  */
18307   constructor_p = true;
18308
18309   /* Look for the optional `::' operator.  */
18310   cp_parser_global_scope_opt (parser,
18311                               /*current_scope_valid_p=*/false);
18312   /* Look for the nested-name-specifier.  */
18313   nested_name_p
18314     = (cp_parser_nested_name_specifier_opt (parser,
18315                                             /*typename_keyword_p=*/false,
18316                                             /*check_dependency_p=*/false,
18317                                             /*type_p=*/false,
18318                                             /*is_declaration=*/false)
18319        != NULL_TREE);
18320   /* Outside of a class-specifier, there must be a
18321      nested-name-specifier.  */
18322   if (!nested_name_p &&
18323       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18324        || friend_p))
18325     constructor_p = false;
18326   /* If we still think that this might be a constructor-declarator,
18327      look for a class-name.  */
18328   if (constructor_p)
18329     {
18330       /* If we have:
18331
18332            template <typename T> struct S { S(); };
18333            template <typename T> S<T>::S ();
18334
18335          we must recognize that the nested `S' names a class.
18336          Similarly, for:
18337
18338            template <typename T> S<T>::S<T> ();
18339
18340          we must recognize that the nested `S' names a template.  */
18341       type_decl = cp_parser_class_name (parser,
18342                                         /*typename_keyword_p=*/false,
18343                                         /*template_keyword_p=*/false,
18344                                         none_type,
18345                                         /*check_dependency_p=*/false,
18346                                         /*class_head_p=*/false,
18347                                         /*is_declaration=*/false);
18348       /* If there was no class-name, then this is not a constructor.  */
18349       constructor_p = !cp_parser_error_occurred (parser);
18350     }
18351
18352   /* If we're still considering a constructor, we have to see a `(',
18353      to begin the parameter-declaration-clause, followed by either a
18354      `)', an `...', or a decl-specifier.  We need to check for a
18355      type-specifier to avoid being fooled into thinking that:
18356
18357        S::S (f) (int);
18358
18359      is a constructor.  (It is actually a function named `f' that
18360      takes one parameter (of type `int') and returns a value of type
18361      `S::S'.  */
18362   if (constructor_p
18363       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18364     {
18365       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18366           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18367           /* A parameter declaration begins with a decl-specifier,
18368              which is either the "attribute" keyword, a storage class
18369              specifier, or (usually) a type-specifier.  */
18370           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18371         {
18372           tree type;
18373           tree pushed_scope = NULL_TREE;
18374           unsigned saved_num_template_parameter_lists;
18375
18376           /* Names appearing in the type-specifier should be looked up
18377              in the scope of the class.  */
18378           if (current_class_type)
18379             type = NULL_TREE;
18380           else
18381             {
18382               type = TREE_TYPE (type_decl);
18383               if (TREE_CODE (type) == TYPENAME_TYPE)
18384                 {
18385                   type = resolve_typename_type (type,
18386                                                 /*only_current_p=*/false);
18387                   if (TREE_CODE (type) == TYPENAME_TYPE)
18388                     {
18389                       cp_parser_abort_tentative_parse (parser);
18390                       return false;
18391                     }
18392                 }
18393               pushed_scope = push_scope (type);
18394             }
18395
18396           /* Inside the constructor parameter list, surrounding
18397              template-parameter-lists do not apply.  */
18398           saved_num_template_parameter_lists
18399             = parser->num_template_parameter_lists;
18400           parser->num_template_parameter_lists = 0;
18401
18402           /* Look for the type-specifier.  */
18403           cp_parser_type_specifier (parser,
18404                                     CP_PARSER_FLAGS_NONE,
18405                                     /*decl_specs=*/NULL,
18406                                     /*is_declarator=*/true,
18407                                     /*declares_class_or_enum=*/NULL,
18408                                     /*is_cv_qualifier=*/NULL);
18409
18410           parser->num_template_parameter_lists
18411             = saved_num_template_parameter_lists;
18412
18413           /* Leave the scope of the class.  */
18414           if (pushed_scope)
18415             pop_scope (pushed_scope);
18416
18417           constructor_p = !cp_parser_error_occurred (parser);
18418         }
18419     }
18420   else
18421     constructor_p = false;
18422   /* We did not really want to consume any tokens.  */
18423   cp_parser_abort_tentative_parse (parser);
18424
18425   return constructor_p;
18426 }
18427
18428 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18429    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18430    they must be performed once we are in the scope of the function.
18431
18432    Returns the function defined.  */
18433
18434 static tree
18435 cp_parser_function_definition_from_specifiers_and_declarator
18436   (cp_parser* parser,
18437    cp_decl_specifier_seq *decl_specifiers,
18438    tree attributes,
18439    const cp_declarator *declarator)
18440 {
18441   tree fn;
18442   bool success_p;
18443
18444   /* Begin the function-definition.  */
18445   success_p = start_function (decl_specifiers, declarator, attributes);
18446
18447   /* The things we're about to see are not directly qualified by any
18448      template headers we've seen thus far.  */
18449   reset_specialization ();
18450
18451   /* If there were names looked up in the decl-specifier-seq that we
18452      did not check, check them now.  We must wait until we are in the
18453      scope of the function to perform the checks, since the function
18454      might be a friend.  */
18455   perform_deferred_access_checks ();
18456
18457   if (!success_p)
18458     {
18459       /* Skip the entire function.  */
18460       cp_parser_skip_to_end_of_block_or_statement (parser);
18461       fn = error_mark_node;
18462     }
18463   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18464     {
18465       /* Seen already, skip it.  An error message has already been output.  */
18466       cp_parser_skip_to_end_of_block_or_statement (parser);
18467       fn = current_function_decl;
18468       current_function_decl = NULL_TREE;
18469       /* If this is a function from a class, pop the nested class.  */
18470       if (current_class_name)
18471         pop_nested_class ();
18472     }
18473   else
18474     fn = cp_parser_function_definition_after_declarator (parser,
18475                                                          /*inline_p=*/false);
18476
18477   return fn;
18478 }
18479
18480 /* Parse the part of a function-definition that follows the
18481    declarator.  INLINE_P is TRUE iff this function is an inline
18482    function defined within a class-specifier.
18483
18484    Returns the function defined.  */
18485
18486 static tree
18487 cp_parser_function_definition_after_declarator (cp_parser* parser,
18488                                                 bool inline_p)
18489 {
18490   tree fn;
18491   bool ctor_initializer_p = false;
18492   bool saved_in_unbraced_linkage_specification_p;
18493   bool saved_in_function_body;
18494   unsigned saved_num_template_parameter_lists;
18495   cp_token *token;
18496
18497   saved_in_function_body = parser->in_function_body;
18498   parser->in_function_body = true;
18499   /* If the next token is `return', then the code may be trying to
18500      make use of the "named return value" extension that G++ used to
18501      support.  */
18502   token = cp_lexer_peek_token (parser->lexer);
18503   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18504     {
18505       /* Consume the `return' keyword.  */
18506       cp_lexer_consume_token (parser->lexer);
18507       /* Look for the identifier that indicates what value is to be
18508          returned.  */
18509       cp_parser_identifier (parser);
18510       /* Issue an error message.  */
18511       error_at (token->location,
18512                 "named return values are no longer supported");
18513       /* Skip tokens until we reach the start of the function body.  */
18514       while (true)
18515         {
18516           cp_token *token = cp_lexer_peek_token (parser->lexer);
18517           if (token->type == CPP_OPEN_BRACE
18518               || token->type == CPP_EOF
18519               || token->type == CPP_PRAGMA_EOL)
18520             break;
18521           cp_lexer_consume_token (parser->lexer);
18522         }
18523     }
18524   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18525      anything declared inside `f'.  */
18526   saved_in_unbraced_linkage_specification_p
18527     = parser->in_unbraced_linkage_specification_p;
18528   parser->in_unbraced_linkage_specification_p = false;
18529   /* Inside the function, surrounding template-parameter-lists do not
18530      apply.  */
18531   saved_num_template_parameter_lists
18532     = parser->num_template_parameter_lists;
18533   parser->num_template_parameter_lists = 0;
18534
18535   start_lambda_scope (current_function_decl);
18536
18537   /* If the next token is `try', then we are looking at a
18538      function-try-block.  */
18539   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18540     ctor_initializer_p = cp_parser_function_try_block (parser);
18541   /* A function-try-block includes the function-body, so we only do
18542      this next part if we're not processing a function-try-block.  */
18543   else
18544     ctor_initializer_p
18545       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18546
18547   finish_lambda_scope ();
18548
18549   /* Finish the function.  */
18550   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18551                         (inline_p ? 2 : 0));
18552   /* Generate code for it, if necessary.  */
18553   expand_or_defer_fn (fn);
18554   /* Restore the saved values.  */
18555   parser->in_unbraced_linkage_specification_p
18556     = saved_in_unbraced_linkage_specification_p;
18557   parser->num_template_parameter_lists
18558     = saved_num_template_parameter_lists;
18559   parser->in_function_body = saved_in_function_body;
18560
18561   return fn;
18562 }
18563
18564 /* Parse a template-declaration, assuming that the `export' (and
18565    `extern') keywords, if present, has already been scanned.  MEMBER_P
18566    is as for cp_parser_template_declaration.  */
18567
18568 static void
18569 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18570 {
18571   tree decl = NULL_TREE;
18572   VEC (deferred_access_check,gc) *checks;
18573   tree parameter_list;
18574   bool friend_p = false;
18575   bool need_lang_pop;
18576   cp_token *token;
18577
18578   /* Look for the `template' keyword.  */
18579   token = cp_lexer_peek_token (parser->lexer);
18580   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18581     return;
18582
18583   /* And the `<'.  */
18584   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18585     return;
18586   if (at_class_scope_p () && current_function_decl)
18587     {
18588       /* 14.5.2.2 [temp.mem]
18589
18590          A local class shall not have member templates.  */
18591       error_at (token->location,
18592                 "invalid declaration of member template in local class");
18593       cp_parser_skip_to_end_of_block_or_statement (parser);
18594       return;
18595     }
18596   /* [temp]
18597
18598      A template ... shall not have C linkage.  */
18599   if (current_lang_name == lang_name_c)
18600     {
18601       error_at (token->location, "template with C linkage");
18602       /* Give it C++ linkage to avoid confusing other parts of the
18603          front end.  */
18604       push_lang_context (lang_name_cplusplus);
18605       need_lang_pop = true;
18606     }
18607   else
18608     need_lang_pop = false;
18609
18610   /* We cannot perform access checks on the template parameter
18611      declarations until we know what is being declared, just as we
18612      cannot check the decl-specifier list.  */
18613   push_deferring_access_checks (dk_deferred);
18614
18615   /* If the next token is `>', then we have an invalid
18616      specialization.  Rather than complain about an invalid template
18617      parameter, issue an error message here.  */
18618   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18619     {
18620       cp_parser_error (parser, "invalid explicit specialization");
18621       begin_specialization ();
18622       parameter_list = NULL_TREE;
18623     }
18624   else
18625     /* Parse the template parameters.  */
18626     parameter_list = cp_parser_template_parameter_list (parser);
18627
18628   /* Get the deferred access checks from the parameter list.  These
18629      will be checked once we know what is being declared, as for a
18630      member template the checks must be performed in the scope of the
18631      class containing the member.  */
18632   checks = get_deferred_access_checks ();
18633
18634   /* Look for the `>'.  */
18635   cp_parser_skip_to_end_of_template_parameter_list (parser);
18636   /* We just processed one more parameter list.  */
18637   ++parser->num_template_parameter_lists;
18638   /* If the next token is `template', there are more template
18639      parameters.  */
18640   if (cp_lexer_next_token_is_keyword (parser->lexer,
18641                                       RID_TEMPLATE))
18642     cp_parser_template_declaration_after_export (parser, member_p);
18643   else
18644     {
18645       /* There are no access checks when parsing a template, as we do not
18646          know if a specialization will be a friend.  */
18647       push_deferring_access_checks (dk_no_check);
18648       token = cp_lexer_peek_token (parser->lexer);
18649       decl = cp_parser_single_declaration (parser,
18650                                            checks,
18651                                            member_p,
18652                                            /*explicit_specialization_p=*/false,
18653                                            &friend_p);
18654       pop_deferring_access_checks ();
18655
18656       /* If this is a member template declaration, let the front
18657          end know.  */
18658       if (member_p && !friend_p && decl)
18659         {
18660           if (TREE_CODE (decl) == TYPE_DECL)
18661             cp_parser_check_access_in_redeclaration (decl, token->location);
18662
18663           decl = finish_member_template_decl (decl);
18664         }
18665       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18666         make_friend_class (current_class_type, TREE_TYPE (decl),
18667                            /*complain=*/true);
18668     }
18669   /* We are done with the current parameter list.  */
18670   --parser->num_template_parameter_lists;
18671
18672   pop_deferring_access_checks ();
18673
18674   /* Finish up.  */
18675   finish_template_decl (parameter_list);
18676
18677   /* Register member declarations.  */
18678   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18679     finish_member_declaration (decl);
18680   /* For the erroneous case of a template with C linkage, we pushed an
18681      implicit C++ linkage scope; exit that scope now.  */
18682   if (need_lang_pop)
18683     pop_lang_context ();
18684   /* If DECL is a function template, we must return to parse it later.
18685      (Even though there is no definition, there might be default
18686      arguments that need handling.)  */
18687   if (member_p && decl
18688       && (TREE_CODE (decl) == FUNCTION_DECL
18689           || DECL_FUNCTION_TEMPLATE_P (decl)))
18690     TREE_VALUE (parser->unparsed_functions_queues)
18691       = tree_cons (NULL_TREE, decl,
18692                    TREE_VALUE (parser->unparsed_functions_queues));
18693 }
18694
18695 /* Perform the deferred access checks from a template-parameter-list.
18696    CHECKS is a TREE_LIST of access checks, as returned by
18697    get_deferred_access_checks.  */
18698
18699 static void
18700 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18701 {
18702   ++processing_template_parmlist;
18703   perform_access_checks (checks);
18704   --processing_template_parmlist;
18705 }
18706
18707 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18708    `function-definition' sequence.  MEMBER_P is true, this declaration
18709    appears in a class scope.
18710
18711    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18712    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18713
18714 static tree
18715 cp_parser_single_declaration (cp_parser* parser,
18716                               VEC (deferred_access_check,gc)* checks,
18717                               bool member_p,
18718                               bool explicit_specialization_p,
18719                               bool* friend_p)
18720 {
18721   int declares_class_or_enum;
18722   tree decl = NULL_TREE;
18723   cp_decl_specifier_seq decl_specifiers;
18724   bool function_definition_p = false;
18725   cp_token *decl_spec_token_start;
18726
18727   /* This function is only used when processing a template
18728      declaration.  */
18729   gcc_assert (innermost_scope_kind () == sk_template_parms
18730               || innermost_scope_kind () == sk_template_spec);
18731
18732   /* Defer access checks until we know what is being declared.  */
18733   push_deferring_access_checks (dk_deferred);
18734
18735   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18736      alternative.  */
18737   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18738   cp_parser_decl_specifier_seq (parser,
18739                                 CP_PARSER_FLAGS_OPTIONAL,
18740                                 &decl_specifiers,
18741                                 &declares_class_or_enum);
18742   if (friend_p)
18743     *friend_p = cp_parser_friend_p (&decl_specifiers);
18744
18745   /* There are no template typedefs.  */
18746   if (decl_specifiers.specs[(int) ds_typedef])
18747     {
18748       error_at (decl_spec_token_start->location,
18749                 "template declaration of %<typedef%>");
18750       decl = error_mark_node;
18751     }
18752
18753   /* Gather up the access checks that occurred the
18754      decl-specifier-seq.  */
18755   stop_deferring_access_checks ();
18756
18757   /* Check for the declaration of a template class.  */
18758   if (declares_class_or_enum)
18759     {
18760       if (cp_parser_declares_only_class_p (parser))
18761         {
18762           decl = shadow_tag (&decl_specifiers);
18763
18764           /* In this case:
18765
18766                struct C {
18767                  friend template <typename T> struct A<T>::B;
18768                };
18769
18770              A<T>::B will be represented by a TYPENAME_TYPE, and
18771              therefore not recognized by shadow_tag.  */
18772           if (friend_p && *friend_p
18773               && !decl
18774               && decl_specifiers.type
18775               && TYPE_P (decl_specifiers.type))
18776             decl = decl_specifiers.type;
18777
18778           if (decl && decl != error_mark_node)
18779             decl = TYPE_NAME (decl);
18780           else
18781             decl = error_mark_node;
18782
18783           /* Perform access checks for template parameters.  */
18784           cp_parser_perform_template_parameter_access_checks (checks);
18785         }
18786     }
18787
18788   /* Complain about missing 'typename' or other invalid type names.  */
18789   if (!decl_specifiers.any_type_specifiers_p)
18790     cp_parser_parse_and_diagnose_invalid_type_name (parser);
18791
18792   /* If it's not a template class, try for a template function.  If
18793      the next token is a `;', then this declaration does not declare
18794      anything.  But, if there were errors in the decl-specifiers, then
18795      the error might well have come from an attempted class-specifier.
18796      In that case, there's no need to warn about a missing declarator.  */
18797   if (!decl
18798       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18799           || decl_specifiers.type != error_mark_node))
18800     {
18801       decl = cp_parser_init_declarator (parser,
18802                                         &decl_specifiers,
18803                                         checks,
18804                                         /*function_definition_allowed_p=*/true,
18805                                         member_p,
18806                                         declares_class_or_enum,
18807                                         &function_definition_p);
18808
18809     /* 7.1.1-1 [dcl.stc]
18810
18811        A storage-class-specifier shall not be specified in an explicit
18812        specialization...  */
18813     if (decl
18814         && explicit_specialization_p
18815         && decl_specifiers.storage_class != sc_none)
18816       {
18817         error_at (decl_spec_token_start->location,
18818                   "explicit template specialization cannot have a storage class");
18819         decl = error_mark_node;
18820       }
18821     }
18822
18823   pop_deferring_access_checks ();
18824
18825   /* Clear any current qualification; whatever comes next is the start
18826      of something new.  */
18827   parser->scope = NULL_TREE;
18828   parser->qualifying_scope = NULL_TREE;
18829   parser->object_scope = NULL_TREE;
18830   /* Look for a trailing `;' after the declaration.  */
18831   if (!function_definition_p
18832       && (decl == error_mark_node
18833           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18834     cp_parser_skip_to_end_of_block_or_statement (parser);
18835
18836   return decl;
18837 }
18838
18839 /* Parse a cast-expression that is not the operand of a unary "&".  */
18840
18841 static tree
18842 cp_parser_simple_cast_expression (cp_parser *parser)
18843 {
18844   return cp_parser_cast_expression (parser, /*address_p=*/false,
18845                                     /*cast_p=*/false, NULL);
18846 }
18847
18848 /* Parse a functional cast to TYPE.  Returns an expression
18849    representing the cast.  */
18850
18851 static tree
18852 cp_parser_functional_cast (cp_parser* parser, tree type)
18853 {
18854   VEC(tree,gc) *vec;
18855   tree expression_list;
18856   tree cast;
18857   bool nonconst_p;
18858
18859   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18860     {
18861       maybe_warn_cpp0x ("extended initializer lists");
18862       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18863       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18864       if (TREE_CODE (type) == TYPE_DECL)
18865         type = TREE_TYPE (type);
18866       return finish_compound_literal (type, expression_list);
18867     }
18868
18869
18870   vec = cp_parser_parenthesized_expression_list (parser, false,
18871                                                  /*cast_p=*/true,
18872                                                  /*allow_expansion_p=*/true,
18873                                                  /*non_constant_p=*/NULL);
18874   if (vec == NULL)
18875     expression_list = error_mark_node;
18876   else
18877     {
18878       expression_list = build_tree_list_vec (vec);
18879       release_tree_vector (vec);
18880     }
18881
18882   cast = build_functional_cast (type, expression_list,
18883                                 tf_warning_or_error);
18884   /* [expr.const]/1: In an integral constant expression "only type
18885      conversions to integral or enumeration type can be used".  */
18886   if (TREE_CODE (type) == TYPE_DECL)
18887     type = TREE_TYPE (type);
18888   if (cast != error_mark_node
18889       && !cast_valid_in_integral_constant_expression_p (type)
18890       && (cp_parser_non_integral_constant_expression
18891           (parser, "a call to a constructor")))
18892     return error_mark_node;
18893   return cast;
18894 }
18895
18896 /* Save the tokens that make up the body of a member function defined
18897    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18898    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18899    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18900    for the member function.  */
18901
18902 static tree
18903 cp_parser_save_member_function_body (cp_parser* parser,
18904                                      cp_decl_specifier_seq *decl_specifiers,
18905                                      cp_declarator *declarator,
18906                                      tree attributes)
18907 {
18908   cp_token *first;
18909   cp_token *last;
18910   tree fn;
18911
18912   /* Create the FUNCTION_DECL.  */
18913   fn = grokmethod (decl_specifiers, declarator, attributes);
18914   /* If something went badly wrong, bail out now.  */
18915   if (fn == error_mark_node)
18916     {
18917       /* If there's a function-body, skip it.  */
18918       if (cp_parser_token_starts_function_definition_p
18919           (cp_lexer_peek_token (parser->lexer)))
18920         cp_parser_skip_to_end_of_block_or_statement (parser);
18921       return error_mark_node;
18922     }
18923
18924   /* Remember it, if there default args to post process.  */
18925   cp_parser_save_default_args (parser, fn);
18926
18927   /* Save away the tokens that make up the body of the
18928      function.  */
18929   first = parser->lexer->next_token;
18930   /* We can have braced-init-list mem-initializers before the fn body.  */
18931   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18932     {
18933       cp_lexer_consume_token (parser->lexer);
18934       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18935              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18936         {
18937           /* cache_group will stop after an un-nested { } pair, too.  */
18938           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18939             break;
18940
18941           /* variadic mem-inits have ... after the ')'.  */
18942           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18943             cp_lexer_consume_token (parser->lexer);
18944         }
18945     }
18946   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18947   /* Handle function try blocks.  */
18948   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18949     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18950   last = parser->lexer->next_token;
18951
18952   /* Save away the inline definition; we will process it when the
18953      class is complete.  */
18954   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18955   DECL_PENDING_INLINE_P (fn) = 1;
18956
18957   /* We need to know that this was defined in the class, so that
18958      friend templates are handled correctly.  */
18959   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18960
18961   /* Add FN to the queue of functions to be parsed later.  */
18962   TREE_VALUE (parser->unparsed_functions_queues)
18963     = tree_cons (NULL_TREE, fn,
18964                  TREE_VALUE (parser->unparsed_functions_queues));
18965
18966   return fn;
18967 }
18968
18969 /* Parse a template-argument-list, as well as the trailing ">" (but
18970    not the opening ">").  See cp_parser_template_argument_list for the
18971    return value.  */
18972
18973 static tree
18974 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18975 {
18976   tree arguments;
18977   tree saved_scope;
18978   tree saved_qualifying_scope;
18979   tree saved_object_scope;
18980   bool saved_greater_than_is_operator_p;
18981   int saved_unevaluated_operand;
18982   int saved_inhibit_evaluation_warnings;
18983
18984   /* [temp.names]
18985
18986      When parsing a template-id, the first non-nested `>' is taken as
18987      the end of the template-argument-list rather than a greater-than
18988      operator.  */
18989   saved_greater_than_is_operator_p
18990     = parser->greater_than_is_operator_p;
18991   parser->greater_than_is_operator_p = false;
18992   /* Parsing the argument list may modify SCOPE, so we save it
18993      here.  */
18994   saved_scope = parser->scope;
18995   saved_qualifying_scope = parser->qualifying_scope;
18996   saved_object_scope = parser->object_scope;
18997   /* We need to evaluate the template arguments, even though this
18998      template-id may be nested within a "sizeof".  */
18999   saved_unevaluated_operand = cp_unevaluated_operand;
19000   cp_unevaluated_operand = 0;
19001   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19002   c_inhibit_evaluation_warnings = 0;
19003   /* Parse the template-argument-list itself.  */
19004   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19005       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19006     arguments = NULL_TREE;
19007   else
19008     arguments = cp_parser_template_argument_list (parser);
19009   /* Look for the `>' that ends the template-argument-list. If we find
19010      a '>>' instead, it's probably just a typo.  */
19011   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19012     {
19013       if (cxx_dialect != cxx98)
19014         {
19015           /* In C++0x, a `>>' in a template argument list or cast
19016              expression is considered to be two separate `>'
19017              tokens. So, change the current token to a `>', but don't
19018              consume it: it will be consumed later when the outer
19019              template argument list (or cast expression) is parsed.
19020              Note that this replacement of `>' for `>>' is necessary
19021              even if we are parsing tentatively: in the tentative
19022              case, after calling
19023              cp_parser_enclosed_template_argument_list we will always
19024              throw away all of the template arguments and the first
19025              closing `>', either because the template argument list
19026              was erroneous or because we are replacing those tokens
19027              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19028              not have been thrown away) is needed either to close an
19029              outer template argument list or to complete a new-style
19030              cast.  */
19031           cp_token *token = cp_lexer_peek_token (parser->lexer);
19032           token->type = CPP_GREATER;
19033         }
19034       else if (!saved_greater_than_is_operator_p)
19035         {
19036           /* If we're in a nested template argument list, the '>>' has
19037             to be a typo for '> >'. We emit the error message, but we
19038             continue parsing and we push a '>' as next token, so that
19039             the argument list will be parsed correctly.  Note that the
19040             global source location is still on the token before the
19041             '>>', so we need to say explicitly where we want it.  */
19042           cp_token *token = cp_lexer_peek_token (parser->lexer);
19043           error_at (token->location, "%<>>%> should be %<> >%> "
19044                     "within a nested template argument list");
19045
19046           token->type = CPP_GREATER;
19047         }
19048       else
19049         {
19050           /* If this is not a nested template argument list, the '>>'
19051             is a typo for '>'. Emit an error message and continue.
19052             Same deal about the token location, but here we can get it
19053             right by consuming the '>>' before issuing the diagnostic.  */
19054           cp_token *token = cp_lexer_consume_token (parser->lexer);
19055           error_at (token->location,
19056                     "spurious %<>>%>, use %<>%> to terminate "
19057                     "a template argument list");
19058         }
19059     }
19060   else
19061     cp_parser_skip_to_end_of_template_parameter_list (parser);
19062   /* The `>' token might be a greater-than operator again now.  */
19063   parser->greater_than_is_operator_p
19064     = saved_greater_than_is_operator_p;
19065   /* Restore the SAVED_SCOPE.  */
19066   parser->scope = saved_scope;
19067   parser->qualifying_scope = saved_qualifying_scope;
19068   parser->object_scope = saved_object_scope;
19069   cp_unevaluated_operand = saved_unevaluated_operand;
19070   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19071
19072   return arguments;
19073 }
19074
19075 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19076    arguments, or the body of the function have not yet been parsed,
19077    parse them now.  */
19078
19079 static void
19080 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19081 {
19082   /* If this member is a template, get the underlying
19083      FUNCTION_DECL.  */
19084   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19085     member_function = DECL_TEMPLATE_RESULT (member_function);
19086
19087   /* There should not be any class definitions in progress at this
19088      point; the bodies of members are only parsed outside of all class
19089      definitions.  */
19090   gcc_assert (parser->num_classes_being_defined == 0);
19091   /* While we're parsing the member functions we might encounter more
19092      classes.  We want to handle them right away, but we don't want
19093      them getting mixed up with functions that are currently in the
19094      queue.  */
19095   parser->unparsed_functions_queues
19096     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19097
19098   /* Make sure that any template parameters are in scope.  */
19099   maybe_begin_member_template_processing (member_function);
19100
19101   /* If the body of the function has not yet been parsed, parse it
19102      now.  */
19103   if (DECL_PENDING_INLINE_P (member_function))
19104     {
19105       tree function_scope;
19106       cp_token_cache *tokens;
19107
19108       /* The function is no longer pending; we are processing it.  */
19109       tokens = DECL_PENDING_INLINE_INFO (member_function);
19110       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19111       DECL_PENDING_INLINE_P (member_function) = 0;
19112
19113       /* If this is a local class, enter the scope of the containing
19114          function.  */
19115       function_scope = current_function_decl;
19116       if (function_scope)
19117         push_function_context ();
19118
19119       /* Push the body of the function onto the lexer stack.  */
19120       cp_parser_push_lexer_for_tokens (parser, tokens);
19121
19122       /* Let the front end know that we going to be defining this
19123          function.  */
19124       start_preparsed_function (member_function, NULL_TREE,
19125                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19126
19127       /* Don't do access checking if it is a templated function.  */
19128       if (processing_template_decl)
19129         push_deferring_access_checks (dk_no_check);
19130
19131       /* Now, parse the body of the function.  */
19132       cp_parser_function_definition_after_declarator (parser,
19133                                                       /*inline_p=*/true);
19134
19135       if (processing_template_decl)
19136         pop_deferring_access_checks ();
19137
19138       /* Leave the scope of the containing function.  */
19139       if (function_scope)
19140         pop_function_context ();
19141       cp_parser_pop_lexer (parser);
19142     }
19143
19144   /* Remove any template parameters from the symbol table.  */
19145   maybe_end_member_template_processing ();
19146
19147   /* Restore the queue.  */
19148   parser->unparsed_functions_queues
19149     = TREE_CHAIN (parser->unparsed_functions_queues);
19150 }
19151
19152 /* If DECL contains any default args, remember it on the unparsed
19153    functions queue.  */
19154
19155 static void
19156 cp_parser_save_default_args (cp_parser* parser, tree decl)
19157 {
19158   tree probe;
19159
19160   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19161        probe;
19162        probe = TREE_CHAIN (probe))
19163     if (TREE_PURPOSE (probe))
19164       {
19165         TREE_PURPOSE (parser->unparsed_functions_queues)
19166           = tree_cons (current_class_type, decl,
19167                        TREE_PURPOSE (parser->unparsed_functions_queues));
19168         break;
19169       }
19170 }
19171
19172 /* FN is a FUNCTION_DECL which may contains a parameter with an
19173    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19174    assumes that the current scope is the scope in which the default
19175    argument should be processed.  */
19176
19177 static void
19178 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19179 {
19180   bool saved_local_variables_forbidden_p;
19181   tree parm, parmdecl;
19182
19183   /* While we're parsing the default args, we might (due to the
19184      statement expression extension) encounter more classes.  We want
19185      to handle them right away, but we don't want them getting mixed
19186      up with default args that are currently in the queue.  */
19187   parser->unparsed_functions_queues
19188     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19189
19190   /* Local variable names (and the `this' keyword) may not appear
19191      in a default argument.  */
19192   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19193   parser->local_variables_forbidden_p = true;
19194
19195   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19196          parmdecl = DECL_ARGUMENTS (fn);
19197        parm && parm != void_list_node;
19198        parm = TREE_CHAIN (parm),
19199          parmdecl = TREE_CHAIN (parmdecl))
19200     {
19201       cp_token_cache *tokens;
19202       tree default_arg = TREE_PURPOSE (parm);
19203       tree parsed_arg;
19204       VEC(tree,gc) *insts;
19205       tree copy;
19206       unsigned ix;
19207
19208       if (!default_arg)
19209         continue;
19210
19211       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19212         /* This can happen for a friend declaration for a function
19213            already declared with default arguments.  */
19214         continue;
19215
19216        /* Push the saved tokens for the default argument onto the parser's
19217           lexer stack.  */
19218       tokens = DEFARG_TOKENS (default_arg);
19219       cp_parser_push_lexer_for_tokens (parser, tokens);
19220
19221       start_lambda_scope (parmdecl);
19222
19223       /* Parse the assignment-expression.  */
19224       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19225       if (parsed_arg == error_mark_node)
19226         {
19227           cp_parser_pop_lexer (parser);
19228           continue;
19229         }
19230
19231       if (!processing_template_decl)
19232         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19233
19234       TREE_PURPOSE (parm) = parsed_arg;
19235
19236       /* Update any instantiations we've already created.  */
19237       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19238            VEC_iterate (tree, insts, ix, copy); ix++)
19239         TREE_PURPOSE (copy) = parsed_arg;
19240
19241       finish_lambda_scope ();
19242
19243       /* If the token stream has not been completely used up, then
19244          there was extra junk after the end of the default
19245          argument.  */
19246       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19247         cp_parser_error (parser, "expected %<,%>");
19248
19249       /* Revert to the main lexer.  */
19250       cp_parser_pop_lexer (parser);
19251     }
19252
19253   /* Make sure no default arg is missing.  */
19254   check_default_args (fn);
19255
19256   /* Restore the state of local_variables_forbidden_p.  */
19257   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19258
19259   /* Restore the queue.  */
19260   parser->unparsed_functions_queues
19261     = TREE_CHAIN (parser->unparsed_functions_queues);
19262 }
19263
19264 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19265    either a TYPE or an expression, depending on the form of the
19266    input.  The KEYWORD indicates which kind of expression we have
19267    encountered.  */
19268
19269 static tree
19270 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19271 {
19272   tree expr = NULL_TREE;
19273   const char *saved_message;
19274   char *tmp;
19275   bool saved_integral_constant_expression_p;
19276   bool saved_non_integral_constant_expression_p;
19277   bool pack_expansion_p = false;
19278
19279   /* Types cannot be defined in a `sizeof' expression.  Save away the
19280      old message.  */
19281   saved_message = parser->type_definition_forbidden_message;
19282   /* And create the new one.  */
19283   tmp = concat ("types may not be defined in %<",
19284                 IDENTIFIER_POINTER (ridpointers[keyword]),
19285                 "%> expressions", NULL);
19286   parser->type_definition_forbidden_message = tmp;
19287
19288   /* The restrictions on constant-expressions do not apply inside
19289      sizeof expressions.  */
19290   saved_integral_constant_expression_p
19291     = parser->integral_constant_expression_p;
19292   saved_non_integral_constant_expression_p
19293     = parser->non_integral_constant_expression_p;
19294   parser->integral_constant_expression_p = false;
19295
19296   /* If it's a `...', then we are computing the length of a parameter
19297      pack.  */
19298   if (keyword == RID_SIZEOF
19299       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19300     {
19301       /* Consume the `...'.  */
19302       cp_lexer_consume_token (parser->lexer);
19303       maybe_warn_variadic_templates ();
19304
19305       /* Note that this is an expansion.  */
19306       pack_expansion_p = true;
19307     }
19308
19309   /* Do not actually evaluate the expression.  */
19310   ++cp_unevaluated_operand;
19311   ++c_inhibit_evaluation_warnings;
19312   /* If it's a `(', then we might be looking at the type-id
19313      construction.  */
19314   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19315     {
19316       tree type;
19317       bool saved_in_type_id_in_expr_p;
19318
19319       /* We can't be sure yet whether we're looking at a type-id or an
19320          expression.  */
19321       cp_parser_parse_tentatively (parser);
19322       /* Consume the `('.  */
19323       cp_lexer_consume_token (parser->lexer);
19324       /* Parse the type-id.  */
19325       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19326       parser->in_type_id_in_expr_p = true;
19327       type = cp_parser_type_id (parser);
19328       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19329       /* Now, look for the trailing `)'.  */
19330       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19331       /* If all went well, then we're done.  */
19332       if (cp_parser_parse_definitely (parser))
19333         {
19334           cp_decl_specifier_seq decl_specs;
19335
19336           /* Build a trivial decl-specifier-seq.  */
19337           clear_decl_specs (&decl_specs);
19338           decl_specs.type = type;
19339
19340           /* Call grokdeclarator to figure out what type this is.  */
19341           expr = grokdeclarator (NULL,
19342                                  &decl_specs,
19343                                  TYPENAME,
19344                                  /*initialized=*/0,
19345                                  /*attrlist=*/NULL);
19346         }
19347     }
19348
19349   /* If the type-id production did not work out, then we must be
19350      looking at the unary-expression production.  */
19351   if (!expr)
19352     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19353                                        /*cast_p=*/false, NULL);
19354
19355   if (pack_expansion_p)
19356     /* Build a pack expansion. */
19357     expr = make_pack_expansion (expr);
19358
19359   /* Go back to evaluating expressions.  */
19360   --cp_unevaluated_operand;
19361   --c_inhibit_evaluation_warnings;
19362
19363   /* Free the message we created.  */
19364   free (tmp);
19365   /* And restore the old one.  */
19366   parser->type_definition_forbidden_message = saved_message;
19367   parser->integral_constant_expression_p
19368     = saved_integral_constant_expression_p;
19369   parser->non_integral_constant_expression_p
19370     = saved_non_integral_constant_expression_p;
19371
19372   return expr;
19373 }
19374
19375 /* If the current declaration has no declarator, return true.  */
19376
19377 static bool
19378 cp_parser_declares_only_class_p (cp_parser *parser)
19379 {
19380   /* If the next token is a `;' or a `,' then there is no
19381      declarator.  */
19382   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19383           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19384 }
19385
19386 /* Update the DECL_SPECS to reflect the storage class indicated by
19387    KEYWORD.  */
19388
19389 static void
19390 cp_parser_set_storage_class (cp_parser *parser,
19391                              cp_decl_specifier_seq *decl_specs,
19392                              enum rid keyword,
19393                              location_t location)
19394 {
19395   cp_storage_class storage_class;
19396
19397   if (parser->in_unbraced_linkage_specification_p)
19398     {
19399       error_at (location, "invalid use of %qD in linkage specification",
19400                 ridpointers[keyword]);
19401       return;
19402     }
19403   else if (decl_specs->storage_class != sc_none)
19404     {
19405       decl_specs->conflicting_specifiers_p = true;
19406       return;
19407     }
19408
19409   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19410       && decl_specs->specs[(int) ds_thread])
19411     {
19412       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19413       decl_specs->specs[(int) ds_thread] = 0;
19414     }
19415
19416   switch (keyword)
19417     {
19418     case RID_AUTO:
19419       storage_class = sc_auto;
19420       break;
19421     case RID_REGISTER:
19422       storage_class = sc_register;
19423       break;
19424     case RID_STATIC:
19425       storage_class = sc_static;
19426       break;
19427     case RID_EXTERN:
19428       storage_class = sc_extern;
19429       break;
19430     case RID_MUTABLE:
19431       storage_class = sc_mutable;
19432       break;
19433     default:
19434       gcc_unreachable ();
19435     }
19436   decl_specs->storage_class = storage_class;
19437
19438   /* A storage class specifier cannot be applied alongside a typedef 
19439      specifier. If there is a typedef specifier present then set 
19440      conflicting_specifiers_p which will trigger an error later
19441      on in grokdeclarator. */
19442   if (decl_specs->specs[(int)ds_typedef])
19443     decl_specs->conflicting_specifiers_p = true;
19444 }
19445
19446 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19447    is true, the type is a user-defined type; otherwise it is a
19448    built-in type specified by a keyword.  */
19449
19450 static void
19451 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19452                               tree type_spec,
19453                               location_t location,
19454                               bool user_defined_p)
19455 {
19456   decl_specs->any_specifiers_p = true;
19457
19458   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19459      (with, for example, in "typedef int wchar_t;") we remember that
19460      this is what happened.  In system headers, we ignore these
19461      declarations so that G++ can work with system headers that are not
19462      C++-safe.  */
19463   if (decl_specs->specs[(int) ds_typedef]
19464       && !user_defined_p
19465       && (type_spec == boolean_type_node
19466           || type_spec == char16_type_node
19467           || type_spec == char32_type_node
19468           || type_spec == wchar_type_node)
19469       && (decl_specs->type
19470           || decl_specs->specs[(int) ds_long]
19471           || decl_specs->specs[(int) ds_short]
19472           || decl_specs->specs[(int) ds_unsigned]
19473           || decl_specs->specs[(int) ds_signed]))
19474     {
19475       decl_specs->redefined_builtin_type = type_spec;
19476       if (!decl_specs->type)
19477         {
19478           decl_specs->type = type_spec;
19479           decl_specs->user_defined_type_p = false;
19480           decl_specs->type_location = location;
19481         }
19482     }
19483   else if (decl_specs->type)
19484     decl_specs->multiple_types_p = true;
19485   else
19486     {
19487       decl_specs->type = type_spec;
19488       decl_specs->user_defined_type_p = user_defined_p;
19489       decl_specs->redefined_builtin_type = NULL_TREE;
19490       decl_specs->type_location = location;
19491     }
19492 }
19493
19494 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19495    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19496
19497 static bool
19498 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19499 {
19500   return decl_specifiers->specs[(int) ds_friend] != 0;
19501 }
19502
19503 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19504    issue an error message indicating that TOKEN_DESC was expected.
19505
19506    Returns the token consumed, if the token had the appropriate type.
19507    Otherwise, returns NULL.  */
19508
19509 static cp_token *
19510 cp_parser_require (cp_parser* parser,
19511                    enum cpp_ttype type,
19512                    const char* token_desc)
19513 {
19514   if (cp_lexer_next_token_is (parser->lexer, type))
19515     return cp_lexer_consume_token (parser->lexer);
19516   else
19517     {
19518       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19519       if (!cp_parser_simulate_error (parser))
19520         {
19521           char *message = concat ("expected ", token_desc, NULL);
19522           cp_parser_error (parser, message);
19523           free (message);
19524         }
19525       return NULL;
19526     }
19527 }
19528
19529 /* An error message is produced if the next token is not '>'.
19530    All further tokens are skipped until the desired token is
19531    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19532
19533 static void
19534 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19535 {
19536   /* Current level of '< ... >'.  */
19537   unsigned level = 0;
19538   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19539   unsigned nesting_depth = 0;
19540
19541   /* Are we ready, yet?  If not, issue error message.  */
19542   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19543     return;
19544
19545   /* Skip tokens until the desired token is found.  */
19546   while (true)
19547     {
19548       /* Peek at the next token.  */
19549       switch (cp_lexer_peek_token (parser->lexer)->type)
19550         {
19551         case CPP_LESS:
19552           if (!nesting_depth)
19553             ++level;
19554           break;
19555
19556         case CPP_RSHIFT:
19557           if (cxx_dialect == cxx98)
19558             /* C++0x views the `>>' operator as two `>' tokens, but
19559                C++98 does not. */
19560             break;
19561           else if (!nesting_depth && level-- == 0)
19562             {
19563               /* We've hit a `>>' where the first `>' closes the
19564                  template argument list, and the second `>' is
19565                  spurious.  Just consume the `>>' and stop; we've
19566                  already produced at least one error.  */
19567               cp_lexer_consume_token (parser->lexer);
19568               return;
19569             }
19570           /* Fall through for C++0x, so we handle the second `>' in
19571              the `>>'.  */
19572
19573         case CPP_GREATER:
19574           if (!nesting_depth && level-- == 0)
19575             {
19576               /* We've reached the token we want, consume it and stop.  */
19577               cp_lexer_consume_token (parser->lexer);
19578               return;
19579             }
19580           break;
19581
19582         case CPP_OPEN_PAREN:
19583         case CPP_OPEN_SQUARE:
19584           ++nesting_depth;
19585           break;
19586
19587         case CPP_CLOSE_PAREN:
19588         case CPP_CLOSE_SQUARE:
19589           if (nesting_depth-- == 0)
19590             return;
19591           break;
19592
19593         case CPP_EOF:
19594         case CPP_PRAGMA_EOL:
19595         case CPP_SEMICOLON:
19596         case CPP_OPEN_BRACE:
19597         case CPP_CLOSE_BRACE:
19598           /* The '>' was probably forgotten, don't look further.  */
19599           return;
19600
19601         default:
19602           break;
19603         }
19604
19605       /* Consume this token.  */
19606       cp_lexer_consume_token (parser->lexer);
19607     }
19608 }
19609
19610 /* If the next token is the indicated keyword, consume it.  Otherwise,
19611    issue an error message indicating that TOKEN_DESC was expected.
19612
19613    Returns the token consumed, if the token had the appropriate type.
19614    Otherwise, returns NULL.  */
19615
19616 static cp_token *
19617 cp_parser_require_keyword (cp_parser* parser,
19618                            enum rid keyword,
19619                            const char* token_desc)
19620 {
19621   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19622
19623   if (token && token->keyword != keyword)
19624     {
19625       dyn_string_t error_msg;
19626
19627       /* Format the error message.  */
19628       error_msg = dyn_string_new (0);
19629       dyn_string_append_cstr (error_msg, "expected ");
19630       dyn_string_append_cstr (error_msg, token_desc);
19631       cp_parser_error (parser, error_msg->s);
19632       dyn_string_delete (error_msg);
19633       return NULL;
19634     }
19635
19636   return token;
19637 }
19638
19639 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19640    function-definition.  */
19641
19642 static bool
19643 cp_parser_token_starts_function_definition_p (cp_token* token)
19644 {
19645   return (/* An ordinary function-body begins with an `{'.  */
19646           token->type == CPP_OPEN_BRACE
19647           /* A ctor-initializer begins with a `:'.  */
19648           || token->type == CPP_COLON
19649           /* A function-try-block begins with `try'.  */
19650           || token->keyword == RID_TRY
19651           /* The named return value extension begins with `return'.  */
19652           || token->keyword == RID_RETURN);
19653 }
19654
19655 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19656    definition.  */
19657
19658 static bool
19659 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19660 {
19661   cp_token *token;
19662
19663   token = cp_lexer_peek_token (parser->lexer);
19664   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19665 }
19666
19667 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19668    C++0x) ending a template-argument.  */
19669
19670 static bool
19671 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19672 {
19673   cp_token *token;
19674
19675   token = cp_lexer_peek_token (parser->lexer);
19676   return (token->type == CPP_COMMA 
19677           || token->type == CPP_GREATER
19678           || token->type == CPP_ELLIPSIS
19679           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19680 }
19681
19682 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19683    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19684
19685 static bool
19686 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19687                                                      size_t n)
19688 {
19689   cp_token *token;
19690
19691   token = cp_lexer_peek_nth_token (parser->lexer, n);
19692   if (token->type == CPP_LESS)
19693     return true;
19694   /* Check for the sequence `<::' in the original code. It would be lexed as
19695      `[:', where `[' is a digraph, and there is no whitespace before
19696      `:'.  */
19697   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19698     {
19699       cp_token *token2;
19700       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19701       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19702         return true;
19703     }
19704   return false;
19705 }
19706
19707 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19708    or none_type otherwise.  */
19709
19710 static enum tag_types
19711 cp_parser_token_is_class_key (cp_token* token)
19712 {
19713   switch (token->keyword)
19714     {
19715     case RID_CLASS:
19716       return class_type;
19717     case RID_STRUCT:
19718       return record_type;
19719     case RID_UNION:
19720       return union_type;
19721
19722     default:
19723       return none_type;
19724     }
19725 }
19726
19727 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19728
19729 static void
19730 cp_parser_check_class_key (enum tag_types class_key, tree type)
19731 {
19732   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19733     permerror (input_location, "%qs tag used in naming %q#T",
19734             class_key == union_type ? "union"
19735              : class_key == record_type ? "struct" : "class",
19736              type);
19737 }
19738
19739 /* Issue an error message if DECL is redeclared with different
19740    access than its original declaration [class.access.spec/3].
19741    This applies to nested classes and nested class templates.
19742    [class.mem/1].  */
19743
19744 static void
19745 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19746 {
19747   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19748     return;
19749
19750   if ((TREE_PRIVATE (decl)
19751        != (current_access_specifier == access_private_node))
19752       || (TREE_PROTECTED (decl)
19753           != (current_access_specifier == access_protected_node)))
19754     error_at (location, "%qD redeclared with different access", decl);
19755 }
19756
19757 /* Look for the `template' keyword, as a syntactic disambiguator.
19758    Return TRUE iff it is present, in which case it will be
19759    consumed.  */
19760
19761 static bool
19762 cp_parser_optional_template_keyword (cp_parser *parser)
19763 {
19764   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19765     {
19766       /* The `template' keyword can only be used within templates;
19767          outside templates the parser can always figure out what is a
19768          template and what is not.  */
19769       if (!processing_template_decl)
19770         {
19771           cp_token *token = cp_lexer_peek_token (parser->lexer);
19772           error_at (token->location,
19773                     "%<template%> (as a disambiguator) is only allowed "
19774                     "within templates");
19775           /* If this part of the token stream is rescanned, the same
19776              error message would be generated.  So, we purge the token
19777              from the stream.  */
19778           cp_lexer_purge_token (parser->lexer);
19779           return false;
19780         }
19781       else
19782         {
19783           /* Consume the `template' keyword.  */
19784           cp_lexer_consume_token (parser->lexer);
19785           return true;
19786         }
19787     }
19788
19789   return false;
19790 }
19791
19792 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19793    set PARSER->SCOPE, and perform other related actions.  */
19794
19795 static void
19796 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19797 {
19798   int i;
19799   struct tree_check *check_value;
19800   deferred_access_check *chk;
19801   VEC (deferred_access_check,gc) *checks;
19802
19803   /* Get the stored value.  */
19804   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19805   /* Perform any access checks that were deferred.  */
19806   checks = check_value->checks;
19807   if (checks)
19808     {
19809       for (i = 0 ;
19810            VEC_iterate (deferred_access_check, checks, i, chk) ;
19811            ++i)
19812         {
19813           perform_or_defer_access_check (chk->binfo,
19814                                          chk->decl,
19815                                          chk->diag_decl);
19816         }
19817     }
19818   /* Set the scope from the stored value.  */
19819   parser->scope = check_value->value;
19820   parser->qualifying_scope = check_value->qualifying_scope;
19821   parser->object_scope = NULL_TREE;
19822 }
19823
19824 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19825    encounter the end of a block before what we were looking for.  */
19826
19827 static bool
19828 cp_parser_cache_group (cp_parser *parser,
19829                        enum cpp_ttype end,
19830                        unsigned depth)
19831 {
19832   while (true)
19833     {
19834       cp_token *token = cp_lexer_peek_token (parser->lexer);
19835
19836       /* Abort a parenthesized expression if we encounter a semicolon.  */
19837       if ((end == CPP_CLOSE_PAREN || depth == 0)
19838           && token->type == CPP_SEMICOLON)
19839         return true;
19840       /* If we've reached the end of the file, stop.  */
19841       if (token->type == CPP_EOF
19842           || (end != CPP_PRAGMA_EOL
19843               && token->type == CPP_PRAGMA_EOL))
19844         return true;
19845       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19846         /* We've hit the end of an enclosing block, so there's been some
19847            kind of syntax error.  */
19848         return true;
19849
19850       /* Consume the token.  */
19851       cp_lexer_consume_token (parser->lexer);
19852       /* See if it starts a new group.  */
19853       if (token->type == CPP_OPEN_BRACE)
19854         {
19855           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19856           /* In theory this should probably check end == '}', but
19857              cp_parser_save_member_function_body needs it to exit
19858              after either '}' or ')' when called with ')'.  */
19859           if (depth == 0)
19860             return false;
19861         }
19862       else if (token->type == CPP_OPEN_PAREN)
19863         {
19864           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19865           if (depth == 0 && end == CPP_CLOSE_PAREN)
19866             return false;
19867         }
19868       else if (token->type == CPP_PRAGMA)
19869         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19870       else if (token->type == end)
19871         return false;
19872     }
19873 }
19874
19875 /* Begin parsing tentatively.  We always save tokens while parsing
19876    tentatively so that if the tentative parsing fails we can restore the
19877    tokens.  */
19878
19879 static void
19880 cp_parser_parse_tentatively (cp_parser* parser)
19881 {
19882   /* Enter a new parsing context.  */
19883   parser->context = cp_parser_context_new (parser->context);
19884   /* Begin saving tokens.  */
19885   cp_lexer_save_tokens (parser->lexer);
19886   /* In order to avoid repetitive access control error messages,
19887      access checks are queued up until we are no longer parsing
19888      tentatively.  */
19889   push_deferring_access_checks (dk_deferred);
19890 }
19891
19892 /* Commit to the currently active tentative parse.  */
19893
19894 static void
19895 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19896 {
19897   cp_parser_context *context;
19898   cp_lexer *lexer;
19899
19900   /* Mark all of the levels as committed.  */
19901   lexer = parser->lexer;
19902   for (context = parser->context; context->next; context = context->next)
19903     {
19904       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19905         break;
19906       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19907       while (!cp_lexer_saving_tokens (lexer))
19908         lexer = lexer->next;
19909       cp_lexer_commit_tokens (lexer);
19910     }
19911 }
19912
19913 /* Abort the currently active tentative parse.  All consumed tokens
19914    will be rolled back, and no diagnostics will be issued.  */
19915
19916 static void
19917 cp_parser_abort_tentative_parse (cp_parser* parser)
19918 {
19919   cp_parser_simulate_error (parser);
19920   /* Now, pretend that we want to see if the construct was
19921      successfully parsed.  */
19922   cp_parser_parse_definitely (parser);
19923 }
19924
19925 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19926    token stream.  Otherwise, commit to the tokens we have consumed.
19927    Returns true if no error occurred; false otherwise.  */
19928
19929 static bool
19930 cp_parser_parse_definitely (cp_parser* parser)
19931 {
19932   bool error_occurred;
19933   cp_parser_context *context;
19934
19935   /* Remember whether or not an error occurred, since we are about to
19936      destroy that information.  */
19937   error_occurred = cp_parser_error_occurred (parser);
19938   /* Remove the topmost context from the stack.  */
19939   context = parser->context;
19940   parser->context = context->next;
19941   /* If no parse errors occurred, commit to the tentative parse.  */
19942   if (!error_occurred)
19943     {
19944       /* Commit to the tokens read tentatively, unless that was
19945          already done.  */
19946       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19947         cp_lexer_commit_tokens (parser->lexer);
19948
19949       pop_to_parent_deferring_access_checks ();
19950     }
19951   /* Otherwise, if errors occurred, roll back our state so that things
19952      are just as they were before we began the tentative parse.  */
19953   else
19954     {
19955       cp_lexer_rollback_tokens (parser->lexer);
19956       pop_deferring_access_checks ();
19957     }
19958   /* Add the context to the front of the free list.  */
19959   context->next = cp_parser_context_free_list;
19960   cp_parser_context_free_list = context;
19961
19962   return !error_occurred;
19963 }
19964
19965 /* Returns true if we are parsing tentatively and are not committed to
19966    this tentative parse.  */
19967
19968 static bool
19969 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19970 {
19971   return (cp_parser_parsing_tentatively (parser)
19972           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19973 }
19974
19975 /* Returns nonzero iff an error has occurred during the most recent
19976    tentative parse.  */
19977
19978 static bool
19979 cp_parser_error_occurred (cp_parser* parser)
19980 {
19981   return (cp_parser_parsing_tentatively (parser)
19982           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19983 }
19984
19985 /* Returns nonzero if GNU extensions are allowed.  */
19986
19987 static bool
19988 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19989 {
19990   return parser->allow_gnu_extensions_p;
19991 }
19992 \f
19993 /* Objective-C++ Productions */
19994
19995
19996 /* Parse an Objective-C expression, which feeds into a primary-expression
19997    above.
19998
19999    objc-expression:
20000      objc-message-expression
20001      objc-string-literal
20002      objc-encode-expression
20003      objc-protocol-expression
20004      objc-selector-expression
20005
20006   Returns a tree representation of the expression.  */
20007
20008 static tree
20009 cp_parser_objc_expression (cp_parser* parser)
20010 {
20011   /* Try to figure out what kind of declaration is present.  */
20012   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20013
20014   switch (kwd->type)
20015     {
20016     case CPP_OPEN_SQUARE:
20017       return cp_parser_objc_message_expression (parser);
20018
20019     case CPP_OBJC_STRING:
20020       kwd = cp_lexer_consume_token (parser->lexer);
20021       return objc_build_string_object (kwd->u.value);
20022
20023     case CPP_KEYWORD:
20024       switch (kwd->keyword)
20025         {
20026         case RID_AT_ENCODE:
20027           return cp_parser_objc_encode_expression (parser);
20028
20029         case RID_AT_PROTOCOL:
20030           return cp_parser_objc_protocol_expression (parser);
20031
20032         case RID_AT_SELECTOR:
20033           return cp_parser_objc_selector_expression (parser);
20034
20035         default:
20036           break;
20037         }
20038     default:
20039       error_at (kwd->location,
20040                 "misplaced %<@%D%> Objective-C++ construct",
20041                 kwd->u.value);
20042       cp_parser_skip_to_end_of_block_or_statement (parser);
20043     }
20044
20045   return error_mark_node;
20046 }
20047
20048 /* Parse an Objective-C message expression.
20049
20050    objc-message-expression:
20051      [ objc-message-receiver objc-message-args ]
20052
20053    Returns a representation of an Objective-C message.  */
20054
20055 static tree
20056 cp_parser_objc_message_expression (cp_parser* parser)
20057 {
20058   tree receiver, messageargs;
20059
20060   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20061   receiver = cp_parser_objc_message_receiver (parser);
20062   messageargs = cp_parser_objc_message_args (parser);
20063   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20064
20065   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20066 }
20067
20068 /* Parse an objc-message-receiver.
20069
20070    objc-message-receiver:
20071      expression
20072      simple-type-specifier
20073
20074   Returns a representation of the type or expression.  */
20075
20076 static tree
20077 cp_parser_objc_message_receiver (cp_parser* parser)
20078 {
20079   tree rcv;
20080
20081   /* An Objective-C message receiver may be either (1) a type
20082      or (2) an expression.  */
20083   cp_parser_parse_tentatively (parser);
20084   rcv = cp_parser_expression (parser, false, NULL);
20085
20086   if (cp_parser_parse_definitely (parser))
20087     return rcv;
20088
20089   rcv = cp_parser_simple_type_specifier (parser,
20090                                          /*decl_specs=*/NULL,
20091                                          CP_PARSER_FLAGS_NONE);
20092
20093   return objc_get_class_reference (rcv);
20094 }
20095
20096 /* Parse the arguments and selectors comprising an Objective-C message.
20097
20098    objc-message-args:
20099      objc-selector
20100      objc-selector-args
20101      objc-selector-args , objc-comma-args
20102
20103    objc-selector-args:
20104      objc-selector [opt] : assignment-expression
20105      objc-selector-args objc-selector [opt] : assignment-expression
20106
20107    objc-comma-args:
20108      assignment-expression
20109      objc-comma-args , assignment-expression
20110
20111    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20112    selector arguments and TREE_VALUE containing a list of comma
20113    arguments.  */
20114
20115 static tree
20116 cp_parser_objc_message_args (cp_parser* parser)
20117 {
20118   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20119   bool maybe_unary_selector_p = true;
20120   cp_token *token = cp_lexer_peek_token (parser->lexer);
20121
20122   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20123     {
20124       tree selector = NULL_TREE, arg;
20125
20126       if (token->type != CPP_COLON)
20127         selector = cp_parser_objc_selector (parser);
20128
20129       /* Detect if we have a unary selector.  */
20130       if (maybe_unary_selector_p
20131           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20132         return build_tree_list (selector, NULL_TREE);
20133
20134       maybe_unary_selector_p = false;
20135       cp_parser_require (parser, CPP_COLON, "%<:%>");
20136       arg = cp_parser_assignment_expression (parser, false, NULL);
20137
20138       sel_args
20139         = chainon (sel_args,
20140                    build_tree_list (selector, arg));
20141
20142       token = cp_lexer_peek_token (parser->lexer);
20143     }
20144
20145   /* Handle non-selector arguments, if any. */
20146   while (token->type == CPP_COMMA)
20147     {
20148       tree arg;
20149
20150       cp_lexer_consume_token (parser->lexer);
20151       arg = cp_parser_assignment_expression (parser, false, NULL);
20152
20153       addl_args
20154         = chainon (addl_args,
20155                    build_tree_list (NULL_TREE, arg));
20156
20157       token = cp_lexer_peek_token (parser->lexer);
20158     }
20159
20160   return build_tree_list (sel_args, addl_args);
20161 }
20162
20163 /* Parse an Objective-C encode expression.
20164
20165    objc-encode-expression:
20166      @encode objc-typename
20167
20168    Returns an encoded representation of the type argument.  */
20169
20170 static tree
20171 cp_parser_objc_encode_expression (cp_parser* parser)
20172 {
20173   tree type;
20174   cp_token *token;
20175
20176   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20177   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20178   token = cp_lexer_peek_token (parser->lexer);
20179   type = complete_type (cp_parser_type_id (parser));
20180   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20181
20182   if (!type)
20183     {
20184       error_at (token->location, 
20185                 "%<@encode%> must specify a type as an argument");
20186       return error_mark_node;
20187     }
20188
20189   return objc_build_encode_expr (type);
20190 }
20191
20192 /* Parse an Objective-C @defs expression.  */
20193
20194 static tree
20195 cp_parser_objc_defs_expression (cp_parser *parser)
20196 {
20197   tree name;
20198
20199   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20200   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20201   name = cp_parser_identifier (parser);
20202   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20203
20204   return objc_get_class_ivars (name);
20205 }
20206
20207 /* Parse an Objective-C protocol expression.
20208
20209   objc-protocol-expression:
20210     @protocol ( identifier )
20211
20212   Returns a representation of the protocol expression.  */
20213
20214 static tree
20215 cp_parser_objc_protocol_expression (cp_parser* parser)
20216 {
20217   tree proto;
20218
20219   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20220   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20221   proto = cp_parser_identifier (parser);
20222   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20223
20224   return objc_build_protocol_expr (proto);
20225 }
20226
20227 /* Parse an Objective-C selector expression.
20228
20229    objc-selector-expression:
20230      @selector ( objc-method-signature )
20231
20232    objc-method-signature:
20233      objc-selector
20234      objc-selector-seq
20235
20236    objc-selector-seq:
20237      objc-selector :
20238      objc-selector-seq objc-selector :
20239
20240   Returns a representation of the method selector.  */
20241
20242 static tree
20243 cp_parser_objc_selector_expression (cp_parser* parser)
20244 {
20245   tree sel_seq = NULL_TREE;
20246   bool maybe_unary_selector_p = true;
20247   cp_token *token;
20248   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20249
20250   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20251   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20252   token = cp_lexer_peek_token (parser->lexer);
20253
20254   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20255          || token->type == CPP_SCOPE)
20256     {
20257       tree selector = NULL_TREE;
20258
20259       if (token->type != CPP_COLON
20260           || token->type == CPP_SCOPE)
20261         selector = cp_parser_objc_selector (parser);
20262
20263       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20264           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20265         {
20266           /* Detect if we have a unary selector.  */
20267           if (maybe_unary_selector_p)
20268             {
20269               sel_seq = selector;
20270               goto finish_selector;
20271             }
20272           else
20273             {
20274               cp_parser_error (parser, "expected %<:%>");
20275             }
20276         }
20277       maybe_unary_selector_p = false;
20278       token = cp_lexer_consume_token (parser->lexer);
20279
20280       if (token->type == CPP_SCOPE)
20281         {
20282           sel_seq
20283             = chainon (sel_seq,
20284                        build_tree_list (selector, NULL_TREE));
20285           sel_seq
20286             = chainon (sel_seq,
20287                        build_tree_list (NULL_TREE, NULL_TREE));
20288         }
20289       else
20290         sel_seq
20291           = chainon (sel_seq,
20292                      build_tree_list (selector, NULL_TREE));
20293
20294       token = cp_lexer_peek_token (parser->lexer);
20295     }
20296
20297  finish_selector:
20298   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20299
20300   return objc_build_selector_expr (loc, sel_seq);
20301 }
20302
20303 /* Parse a list of identifiers.
20304
20305    objc-identifier-list:
20306      identifier
20307      objc-identifier-list , identifier
20308
20309    Returns a TREE_LIST of identifier nodes.  */
20310
20311 static tree
20312 cp_parser_objc_identifier_list (cp_parser* parser)
20313 {
20314   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20315   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20316
20317   while (sep->type == CPP_COMMA)
20318     {
20319       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20320       list = chainon (list,
20321                       build_tree_list (NULL_TREE,
20322                                        cp_parser_identifier (parser)));
20323       sep = cp_lexer_peek_token (parser->lexer);
20324     }
20325
20326   return list;
20327 }
20328
20329 /* Parse an Objective-C alias declaration.
20330
20331    objc-alias-declaration:
20332      @compatibility_alias identifier identifier ;
20333
20334    This function registers the alias mapping with the Objective-C front end.
20335    It returns nothing.  */
20336
20337 static void
20338 cp_parser_objc_alias_declaration (cp_parser* parser)
20339 {
20340   tree alias, orig;
20341
20342   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20343   alias = cp_parser_identifier (parser);
20344   orig = cp_parser_identifier (parser);
20345   objc_declare_alias (alias, orig);
20346   cp_parser_consume_semicolon_at_end_of_statement (parser);
20347 }
20348
20349 /* Parse an Objective-C class forward-declaration.
20350
20351    objc-class-declaration:
20352      @class objc-identifier-list ;
20353
20354    The function registers the forward declarations with the Objective-C
20355    front end.  It returns nothing.  */
20356
20357 static void
20358 cp_parser_objc_class_declaration (cp_parser* parser)
20359 {
20360   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20361   objc_declare_class (cp_parser_objc_identifier_list (parser));
20362   cp_parser_consume_semicolon_at_end_of_statement (parser);
20363 }
20364
20365 /* Parse a list of Objective-C protocol references.
20366
20367    objc-protocol-refs-opt:
20368      objc-protocol-refs [opt]
20369
20370    objc-protocol-refs:
20371      < objc-identifier-list >
20372
20373    Returns a TREE_LIST of identifiers, if any.  */
20374
20375 static tree
20376 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20377 {
20378   tree protorefs = NULL_TREE;
20379
20380   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20381     {
20382       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20383       protorefs = cp_parser_objc_identifier_list (parser);
20384       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20385     }
20386
20387   return protorefs;
20388 }
20389
20390 /* Parse a Objective-C visibility specification.  */
20391
20392 static void
20393 cp_parser_objc_visibility_spec (cp_parser* parser)
20394 {
20395   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20396
20397   switch (vis->keyword)
20398     {
20399     case RID_AT_PRIVATE:
20400       objc_set_visibility (2);
20401       break;
20402     case RID_AT_PROTECTED:
20403       objc_set_visibility (0);
20404       break;
20405     case RID_AT_PUBLIC:
20406       objc_set_visibility (1);
20407       break;
20408     default:
20409       return;
20410     }
20411
20412   /* Eat '@private'/'@protected'/'@public'.  */
20413   cp_lexer_consume_token (parser->lexer);
20414 }
20415
20416 /* Parse an Objective-C method type.  */
20417
20418 static void
20419 cp_parser_objc_method_type (cp_parser* parser)
20420 {
20421   objc_set_method_type
20422    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20423     ? PLUS_EXPR
20424     : MINUS_EXPR);
20425 }
20426
20427 /* Parse an Objective-C protocol qualifier.  */
20428
20429 static tree
20430 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20431 {
20432   tree quals = NULL_TREE, node;
20433   cp_token *token = cp_lexer_peek_token (parser->lexer);
20434
20435   node = token->u.value;
20436
20437   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20438          && (node == ridpointers [(int) RID_IN]
20439              || node == ridpointers [(int) RID_OUT]
20440              || node == ridpointers [(int) RID_INOUT]
20441              || node == ridpointers [(int) RID_BYCOPY]
20442              || node == ridpointers [(int) RID_BYREF]
20443              || node == ridpointers [(int) RID_ONEWAY]))
20444     {
20445       quals = tree_cons (NULL_TREE, node, quals);
20446       cp_lexer_consume_token (parser->lexer);
20447       token = cp_lexer_peek_token (parser->lexer);
20448       node = token->u.value;
20449     }
20450
20451   return quals;
20452 }
20453
20454 /* Parse an Objective-C typename.  */
20455
20456 static tree
20457 cp_parser_objc_typename (cp_parser* parser)
20458 {
20459   tree type_name = NULL_TREE;
20460
20461   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20462     {
20463       tree proto_quals, cp_type = NULL_TREE;
20464
20465       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20466       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20467
20468       /* An ObjC type name may consist of just protocol qualifiers, in which
20469          case the type shall default to 'id'.  */
20470       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20471         cp_type = cp_parser_type_id (parser);
20472
20473       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20474       type_name = build_tree_list (proto_quals, cp_type);
20475     }
20476
20477   return type_name;
20478 }
20479
20480 /* Check to see if TYPE refers to an Objective-C selector name.  */
20481
20482 static bool
20483 cp_parser_objc_selector_p (enum cpp_ttype type)
20484 {
20485   return (type == CPP_NAME || type == CPP_KEYWORD
20486           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20487           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20488           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20489           || type == CPP_XOR || type == CPP_XOR_EQ);
20490 }
20491
20492 /* Parse an Objective-C selector.  */
20493
20494 static tree
20495 cp_parser_objc_selector (cp_parser* parser)
20496 {
20497   cp_token *token = cp_lexer_consume_token (parser->lexer);
20498
20499   if (!cp_parser_objc_selector_p (token->type))
20500     {
20501       error_at (token->location, "invalid Objective-C++ selector name");
20502       return error_mark_node;
20503     }
20504
20505   /* C++ operator names are allowed to appear in ObjC selectors.  */
20506   switch (token->type)
20507     {
20508     case CPP_AND_AND: return get_identifier ("and");
20509     case CPP_AND_EQ: return get_identifier ("and_eq");
20510     case CPP_AND: return get_identifier ("bitand");
20511     case CPP_OR: return get_identifier ("bitor");
20512     case CPP_COMPL: return get_identifier ("compl");
20513     case CPP_NOT: return get_identifier ("not");
20514     case CPP_NOT_EQ: return get_identifier ("not_eq");
20515     case CPP_OR_OR: return get_identifier ("or");
20516     case CPP_OR_EQ: return get_identifier ("or_eq");
20517     case CPP_XOR: return get_identifier ("xor");
20518     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20519     default: return token->u.value;
20520     }
20521 }
20522
20523 /* Parse an Objective-C params list.  */
20524
20525 static tree
20526 cp_parser_objc_method_keyword_params (cp_parser* parser)
20527 {
20528   tree params = NULL_TREE;
20529   bool maybe_unary_selector_p = true;
20530   cp_token *token = cp_lexer_peek_token (parser->lexer);
20531
20532   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20533     {
20534       tree selector = NULL_TREE, type_name, identifier;
20535
20536       if (token->type != CPP_COLON)
20537         selector = cp_parser_objc_selector (parser);
20538
20539       /* Detect if we have a unary selector.  */
20540       if (maybe_unary_selector_p
20541           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20542         return selector;
20543
20544       maybe_unary_selector_p = false;
20545       cp_parser_require (parser, CPP_COLON, "%<:%>");
20546       type_name = cp_parser_objc_typename (parser);
20547       identifier = cp_parser_identifier (parser);
20548
20549       params
20550         = chainon (params,
20551                    objc_build_keyword_decl (selector,
20552                                             type_name,
20553                                             identifier));
20554
20555       token = cp_lexer_peek_token (parser->lexer);
20556     }
20557
20558   return params;
20559 }
20560
20561 /* Parse the non-keyword Objective-C params.  */
20562
20563 static tree
20564 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20565 {
20566   tree params = make_node (TREE_LIST);
20567   cp_token *token = cp_lexer_peek_token (parser->lexer);
20568   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20569
20570   while (token->type == CPP_COMMA)
20571     {
20572       cp_parameter_declarator *parmdecl;
20573       tree parm;
20574
20575       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20576       token = cp_lexer_peek_token (parser->lexer);
20577
20578       if (token->type == CPP_ELLIPSIS)
20579         {
20580           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20581           *ellipsisp = true;
20582           break;
20583         }
20584
20585       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20586       parm = grokdeclarator (parmdecl->declarator,
20587                              &parmdecl->decl_specifiers,
20588                              PARM, /*initialized=*/0,
20589                              /*attrlist=*/NULL);
20590
20591       chainon (params, build_tree_list (NULL_TREE, parm));
20592       token = cp_lexer_peek_token (parser->lexer);
20593     }
20594
20595   return params;
20596 }
20597
20598 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20599
20600 static void
20601 cp_parser_objc_interstitial_code (cp_parser* parser)
20602 {
20603   cp_token *token = cp_lexer_peek_token (parser->lexer);
20604
20605   /* If the next token is `extern' and the following token is a string
20606      literal, then we have a linkage specification.  */
20607   if (token->keyword == RID_EXTERN
20608       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20609     cp_parser_linkage_specification (parser);
20610   /* Handle #pragma, if any.  */
20611   else if (token->type == CPP_PRAGMA)
20612     cp_parser_pragma (parser, pragma_external);
20613   /* Allow stray semicolons.  */
20614   else if (token->type == CPP_SEMICOLON)
20615     cp_lexer_consume_token (parser->lexer);
20616   /* Finally, try to parse a block-declaration, or a function-definition.  */
20617   else
20618     cp_parser_block_declaration (parser, /*statement_p=*/false);
20619 }
20620
20621 /* Parse a method signature.  */
20622
20623 static tree
20624 cp_parser_objc_method_signature (cp_parser* parser)
20625 {
20626   tree rettype, kwdparms, optparms;
20627   bool ellipsis = false;
20628
20629   cp_parser_objc_method_type (parser);
20630   rettype = cp_parser_objc_typename (parser);
20631   kwdparms = cp_parser_objc_method_keyword_params (parser);
20632   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20633
20634   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20635 }
20636
20637 /* Pars an Objective-C method prototype list.  */
20638
20639 static void
20640 cp_parser_objc_method_prototype_list (cp_parser* parser)
20641 {
20642   cp_token *token = cp_lexer_peek_token (parser->lexer);
20643
20644   while (token->keyword != RID_AT_END)
20645     {
20646       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20647         {
20648           objc_add_method_declaration
20649            (cp_parser_objc_method_signature (parser));
20650           cp_parser_consume_semicolon_at_end_of_statement (parser);
20651         }
20652       else
20653         /* Allow for interspersed non-ObjC++ code.  */
20654         cp_parser_objc_interstitial_code (parser);
20655
20656       token = cp_lexer_peek_token (parser->lexer);
20657     }
20658
20659   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20660   objc_finish_interface ();
20661 }
20662
20663 /* Parse an Objective-C method definition list.  */
20664
20665 static void
20666 cp_parser_objc_method_definition_list (cp_parser* parser)
20667 {
20668   cp_token *token = cp_lexer_peek_token (parser->lexer);
20669
20670   while (token->keyword != RID_AT_END)
20671     {
20672       tree meth;
20673
20674       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20675         {
20676           push_deferring_access_checks (dk_deferred);
20677           objc_start_method_definition
20678            (cp_parser_objc_method_signature (parser));
20679
20680           /* For historical reasons, we accept an optional semicolon.  */
20681           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20682             cp_lexer_consume_token (parser->lexer);
20683
20684           perform_deferred_access_checks ();
20685           stop_deferring_access_checks ();
20686           meth = cp_parser_function_definition_after_declarator (parser,
20687                                                                  false);
20688           pop_deferring_access_checks ();
20689           objc_finish_method_definition (meth);
20690         }
20691       else
20692         /* Allow for interspersed non-ObjC++ code.  */
20693         cp_parser_objc_interstitial_code (parser);
20694
20695       token = cp_lexer_peek_token (parser->lexer);
20696     }
20697
20698   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20699   objc_finish_implementation ();
20700 }
20701
20702 /* Parse Objective-C ivars.  */
20703
20704 static void
20705 cp_parser_objc_class_ivars (cp_parser* parser)
20706 {
20707   cp_token *token = cp_lexer_peek_token (parser->lexer);
20708
20709   if (token->type != CPP_OPEN_BRACE)
20710     return;     /* No ivars specified.  */
20711
20712   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20713   token = cp_lexer_peek_token (parser->lexer);
20714
20715   while (token->type != CPP_CLOSE_BRACE)
20716     {
20717       cp_decl_specifier_seq declspecs;
20718       int decl_class_or_enum_p;
20719       tree prefix_attributes;
20720
20721       cp_parser_objc_visibility_spec (parser);
20722
20723       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20724         break;
20725
20726       cp_parser_decl_specifier_seq (parser,
20727                                     CP_PARSER_FLAGS_OPTIONAL,
20728                                     &declspecs,
20729                                     &decl_class_or_enum_p);
20730       prefix_attributes = declspecs.attributes;
20731       declspecs.attributes = NULL_TREE;
20732
20733       /* Keep going until we hit the `;' at the end of the
20734          declaration.  */
20735       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20736         {
20737           tree width = NULL_TREE, attributes, first_attribute, decl;
20738           cp_declarator *declarator = NULL;
20739           int ctor_dtor_or_conv_p;
20740
20741           /* Check for a (possibly unnamed) bitfield declaration.  */
20742           token = cp_lexer_peek_token (parser->lexer);
20743           if (token->type == CPP_COLON)
20744             goto eat_colon;
20745
20746           if (token->type == CPP_NAME
20747               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20748                   == CPP_COLON))
20749             {
20750               /* Get the name of the bitfield.  */
20751               declarator = make_id_declarator (NULL_TREE,
20752                                                cp_parser_identifier (parser),
20753                                                sfk_none);
20754
20755              eat_colon:
20756               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20757               /* Get the width of the bitfield.  */
20758               width
20759                 = cp_parser_constant_expression (parser,
20760                                                  /*allow_non_constant=*/false,
20761                                                  NULL);
20762             }
20763           else
20764             {
20765               /* Parse the declarator.  */
20766               declarator
20767                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20768                                         &ctor_dtor_or_conv_p,
20769                                         /*parenthesized_p=*/NULL,
20770                                         /*member_p=*/false);
20771             }
20772
20773           /* Look for attributes that apply to the ivar.  */
20774           attributes = cp_parser_attributes_opt (parser);
20775           /* Remember which attributes are prefix attributes and
20776              which are not.  */
20777           first_attribute = attributes;
20778           /* Combine the attributes.  */
20779           attributes = chainon (prefix_attributes, attributes);
20780
20781           if (width)
20782               /* Create the bitfield declaration.  */
20783               decl = grokbitfield (declarator, &declspecs,
20784                                    width,
20785                                    attributes);
20786           else
20787             decl = grokfield (declarator, &declspecs,
20788                               NULL_TREE, /*init_const_expr_p=*/false,
20789                               NULL_TREE, attributes);
20790
20791           /* Add the instance variable.  */
20792           objc_add_instance_variable (decl);
20793
20794           /* Reset PREFIX_ATTRIBUTES.  */
20795           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20796             attributes = TREE_CHAIN (attributes);
20797           if (attributes)
20798             TREE_CHAIN (attributes) = NULL_TREE;
20799
20800           token = cp_lexer_peek_token (parser->lexer);
20801
20802           if (token->type == CPP_COMMA)
20803             {
20804               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20805               continue;
20806             }
20807           break;
20808         }
20809
20810       cp_parser_consume_semicolon_at_end_of_statement (parser);
20811       token = cp_lexer_peek_token (parser->lexer);
20812     }
20813
20814   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20815   /* For historical reasons, we accept an optional semicolon.  */
20816   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20817     cp_lexer_consume_token (parser->lexer);
20818 }
20819
20820 /* Parse an Objective-C protocol declaration.  */
20821
20822 static void
20823 cp_parser_objc_protocol_declaration (cp_parser* parser)
20824 {
20825   tree proto, protorefs;
20826   cp_token *tok;
20827
20828   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20829   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20830     {
20831       tok = cp_lexer_peek_token (parser->lexer);
20832       error_at (tok->location, "identifier expected after %<@protocol%>");
20833       goto finish;
20834     }
20835
20836   /* See if we have a forward declaration or a definition.  */
20837   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20838
20839   /* Try a forward declaration first.  */
20840   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20841     {
20842       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20843      finish:
20844       cp_parser_consume_semicolon_at_end_of_statement (parser);
20845     }
20846
20847   /* Ok, we got a full-fledged definition (or at least should).  */
20848   else
20849     {
20850       proto = cp_parser_identifier (parser);
20851       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20852       objc_start_protocol (proto, protorefs);
20853       cp_parser_objc_method_prototype_list (parser);
20854     }
20855 }
20856
20857 /* Parse an Objective-C superclass or category.  */
20858
20859 static void
20860 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20861                                                           tree *categ)
20862 {
20863   cp_token *next = cp_lexer_peek_token (parser->lexer);
20864
20865   *super = *categ = NULL_TREE;
20866   if (next->type == CPP_COLON)
20867     {
20868       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20869       *super = cp_parser_identifier (parser);
20870     }
20871   else if (next->type == CPP_OPEN_PAREN)
20872     {
20873       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20874       *categ = cp_parser_identifier (parser);
20875       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20876     }
20877 }
20878
20879 /* Parse an Objective-C class interface.  */
20880
20881 static void
20882 cp_parser_objc_class_interface (cp_parser* parser)
20883 {
20884   tree name, super, categ, protos;
20885
20886   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20887   name = cp_parser_identifier (parser);
20888   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20889   protos = cp_parser_objc_protocol_refs_opt (parser);
20890
20891   /* We have either a class or a category on our hands.  */
20892   if (categ)
20893     objc_start_category_interface (name, categ, protos);
20894   else
20895     {
20896       objc_start_class_interface (name, super, protos);
20897       /* Handle instance variable declarations, if any.  */
20898       cp_parser_objc_class_ivars (parser);
20899       objc_continue_interface ();
20900     }
20901
20902   cp_parser_objc_method_prototype_list (parser);
20903 }
20904
20905 /* Parse an Objective-C class implementation.  */
20906
20907 static void
20908 cp_parser_objc_class_implementation (cp_parser* parser)
20909 {
20910   tree name, super, categ;
20911
20912   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20913   name = cp_parser_identifier (parser);
20914   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20915
20916   /* We have either a class or a category on our hands.  */
20917   if (categ)
20918     objc_start_category_implementation (name, categ);
20919   else
20920     {
20921       objc_start_class_implementation (name, super);
20922       /* Handle instance variable declarations, if any.  */
20923       cp_parser_objc_class_ivars (parser);
20924       objc_continue_implementation ();
20925     }
20926
20927   cp_parser_objc_method_definition_list (parser);
20928 }
20929
20930 /* Consume the @end token and finish off the implementation.  */
20931
20932 static void
20933 cp_parser_objc_end_implementation (cp_parser* parser)
20934 {
20935   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20936   objc_finish_implementation ();
20937 }
20938
20939 /* Parse an Objective-C declaration.  */
20940
20941 static void
20942 cp_parser_objc_declaration (cp_parser* parser)
20943 {
20944   /* Try to figure out what kind of declaration is present.  */
20945   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20946
20947   switch (kwd->keyword)
20948     {
20949     case RID_AT_ALIAS:
20950       cp_parser_objc_alias_declaration (parser);
20951       break;
20952     case RID_AT_CLASS:
20953       cp_parser_objc_class_declaration (parser);
20954       break;
20955     case RID_AT_PROTOCOL:
20956       cp_parser_objc_protocol_declaration (parser);
20957       break;
20958     case RID_AT_INTERFACE:
20959       cp_parser_objc_class_interface (parser);
20960       break;
20961     case RID_AT_IMPLEMENTATION:
20962       cp_parser_objc_class_implementation (parser);
20963       break;
20964     case RID_AT_END:
20965       cp_parser_objc_end_implementation (parser);
20966       break;
20967     default:
20968       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20969                 kwd->u.value);
20970       cp_parser_skip_to_end_of_block_or_statement (parser);
20971     }
20972 }
20973
20974 /* Parse an Objective-C try-catch-finally statement.
20975
20976    objc-try-catch-finally-stmt:
20977      @try compound-statement objc-catch-clause-seq [opt]
20978        objc-finally-clause [opt]
20979
20980    objc-catch-clause-seq:
20981      objc-catch-clause objc-catch-clause-seq [opt]
20982
20983    objc-catch-clause:
20984      @catch ( exception-declaration ) compound-statement
20985
20986    objc-finally-clause
20987      @finally compound-statement
20988
20989    Returns NULL_TREE.  */
20990
20991 static tree
20992 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20993   location_t location;
20994   tree stmt;
20995
20996   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20997   location = cp_lexer_peek_token (parser->lexer)->location;
20998   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20999      node, lest it get absorbed into the surrounding block.  */
21000   stmt = push_stmt_list ();
21001   cp_parser_compound_statement (parser, NULL, false);
21002   objc_begin_try_stmt (location, pop_stmt_list (stmt));
21003
21004   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21005     {
21006       cp_parameter_declarator *parmdecl;
21007       tree parm;
21008
21009       cp_lexer_consume_token (parser->lexer);
21010       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21011       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21012       parm = grokdeclarator (parmdecl->declarator,
21013                              &parmdecl->decl_specifiers,
21014                              PARM, /*initialized=*/0,
21015                              /*attrlist=*/NULL);
21016       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21017       objc_begin_catch_clause (parm);
21018       cp_parser_compound_statement (parser, NULL, false);
21019       objc_finish_catch_clause ();
21020     }
21021
21022   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21023     {
21024       cp_lexer_consume_token (parser->lexer);
21025       location = cp_lexer_peek_token (parser->lexer)->location;
21026       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21027          node, lest it get absorbed into the surrounding block.  */
21028       stmt = push_stmt_list ();
21029       cp_parser_compound_statement (parser, NULL, false);
21030       objc_build_finally_clause (location, pop_stmt_list (stmt));
21031     }
21032
21033   return objc_finish_try_stmt ();
21034 }
21035
21036 /* Parse an Objective-C synchronized statement.
21037
21038    objc-synchronized-stmt:
21039      @synchronized ( expression ) compound-statement
21040
21041    Returns NULL_TREE.  */
21042
21043 static tree
21044 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21045   location_t location;
21046   tree lock, stmt;
21047
21048   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21049
21050   location = cp_lexer_peek_token (parser->lexer)->location;
21051   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21052   lock = cp_parser_expression (parser, false, NULL);
21053   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21054
21055   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21056      node, lest it get absorbed into the surrounding block.  */
21057   stmt = push_stmt_list ();
21058   cp_parser_compound_statement (parser, NULL, false);
21059
21060   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21061 }
21062
21063 /* Parse an Objective-C throw statement.
21064
21065    objc-throw-stmt:
21066      @throw assignment-expression [opt] ;
21067
21068    Returns a constructed '@throw' statement.  */
21069
21070 static tree
21071 cp_parser_objc_throw_statement (cp_parser *parser) {
21072   tree expr = NULL_TREE;
21073   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21074
21075   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21076
21077   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21078     expr = cp_parser_assignment_expression (parser, false, NULL);
21079
21080   cp_parser_consume_semicolon_at_end_of_statement (parser);
21081
21082   return objc_build_throw_stmt (loc, expr);
21083 }
21084
21085 /* Parse an Objective-C statement.  */
21086
21087 static tree
21088 cp_parser_objc_statement (cp_parser * parser) {
21089   /* Try to figure out what kind of declaration is present.  */
21090   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21091
21092   switch (kwd->keyword)
21093     {
21094     case RID_AT_TRY:
21095       return cp_parser_objc_try_catch_finally_statement (parser);
21096     case RID_AT_SYNCHRONIZED:
21097       return cp_parser_objc_synchronized_statement (parser);
21098     case RID_AT_THROW:
21099       return cp_parser_objc_throw_statement (parser);
21100     default:
21101       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21102                kwd->u.value);
21103       cp_parser_skip_to_end_of_block_or_statement (parser);
21104     }
21105
21106   return error_mark_node;
21107 }
21108 \f
21109 /* OpenMP 2.5 parsing routines.  */
21110
21111 /* Returns name of the next clause.
21112    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21113    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21114    returned and the token is consumed.  */
21115
21116 static pragma_omp_clause
21117 cp_parser_omp_clause_name (cp_parser *parser)
21118 {
21119   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21120
21121   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21122     result = PRAGMA_OMP_CLAUSE_IF;
21123   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21124     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21125   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21126     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21127   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21128     {
21129       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21130       const char *p = IDENTIFIER_POINTER (id);
21131
21132       switch (p[0])
21133         {
21134         case 'c':
21135           if (!strcmp ("collapse", p))
21136             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21137           else if (!strcmp ("copyin", p))
21138             result = PRAGMA_OMP_CLAUSE_COPYIN;
21139           else if (!strcmp ("copyprivate", p))
21140             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21141           break;
21142         case 'f':
21143           if (!strcmp ("firstprivate", p))
21144             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21145           break;
21146         case 'l':
21147           if (!strcmp ("lastprivate", p))
21148             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21149           break;
21150         case 'n':
21151           if (!strcmp ("nowait", p))
21152             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21153           else if (!strcmp ("num_threads", p))
21154             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21155           break;
21156         case 'o':
21157           if (!strcmp ("ordered", p))
21158             result = PRAGMA_OMP_CLAUSE_ORDERED;
21159           break;
21160         case 'r':
21161           if (!strcmp ("reduction", p))
21162             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21163           break;
21164         case 's':
21165           if (!strcmp ("schedule", p))
21166             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21167           else if (!strcmp ("shared", p))
21168             result = PRAGMA_OMP_CLAUSE_SHARED;
21169           break;
21170         case 'u':
21171           if (!strcmp ("untied", p))
21172             result = PRAGMA_OMP_CLAUSE_UNTIED;
21173           break;
21174         }
21175     }
21176
21177   if (result != PRAGMA_OMP_CLAUSE_NONE)
21178     cp_lexer_consume_token (parser->lexer);
21179
21180   return result;
21181 }
21182
21183 /* Validate that a clause of the given type does not already exist.  */
21184
21185 static void
21186 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21187                            const char *name, location_t location)
21188 {
21189   tree c;
21190
21191   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21192     if (OMP_CLAUSE_CODE (c) == code)
21193       {
21194         error_at (location, "too many %qs clauses", name);
21195         break;
21196       }
21197 }
21198
21199 /* OpenMP 2.5:
21200    variable-list:
21201      identifier
21202      variable-list , identifier
21203
21204    In addition, we match a closing parenthesis.  An opening parenthesis
21205    will have been consumed by the caller.
21206
21207    If KIND is nonzero, create the appropriate node and install the decl
21208    in OMP_CLAUSE_DECL and add the node to the head of the list.
21209
21210    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21211    return the list created.  */
21212
21213 static tree
21214 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21215                                 tree list)
21216 {
21217   cp_token *token;
21218   while (1)
21219     {
21220       tree name, decl;
21221
21222       token = cp_lexer_peek_token (parser->lexer);
21223       name = cp_parser_id_expression (parser, /*template_p=*/false,
21224                                       /*check_dependency_p=*/true,
21225                                       /*template_p=*/NULL,
21226                                       /*declarator_p=*/false,
21227                                       /*optional_p=*/false);
21228       if (name == error_mark_node)
21229         goto skip_comma;
21230
21231       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21232       if (decl == error_mark_node)
21233         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21234       else if (kind != 0)
21235         {
21236           tree u = build_omp_clause (token->location, kind);
21237           OMP_CLAUSE_DECL (u) = decl;
21238           OMP_CLAUSE_CHAIN (u) = list;
21239           list = u;
21240         }
21241       else
21242         list = tree_cons (decl, NULL_TREE, list);
21243
21244     get_comma:
21245       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21246         break;
21247       cp_lexer_consume_token (parser->lexer);
21248     }
21249
21250   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21251     {
21252       int ending;
21253
21254       /* Try to resync to an unnested comma.  Copied from
21255          cp_parser_parenthesized_expression_list.  */
21256     skip_comma:
21257       ending = cp_parser_skip_to_closing_parenthesis (parser,
21258                                                       /*recovering=*/true,
21259                                                       /*or_comma=*/true,
21260                                                       /*consume_paren=*/true);
21261       if (ending < 0)
21262         goto get_comma;
21263     }
21264
21265   return list;
21266 }
21267
21268 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21269    common case for omp clauses.  */
21270
21271 static tree
21272 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21273 {
21274   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21275     return cp_parser_omp_var_list_no_open (parser, kind, list);
21276   return list;
21277 }
21278
21279 /* OpenMP 3.0:
21280    collapse ( constant-expression ) */
21281
21282 static tree
21283 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21284 {
21285   tree c, num;
21286   location_t loc;
21287   HOST_WIDE_INT n;
21288
21289   loc = cp_lexer_peek_token (parser->lexer)->location;
21290   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21291     return list;
21292
21293   num = cp_parser_constant_expression (parser, false, NULL);
21294
21295   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21296     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21297                                            /*or_comma=*/false,
21298                                            /*consume_paren=*/true);
21299
21300   if (num == error_mark_node)
21301     return list;
21302   num = fold_non_dependent_expr (num);
21303   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21304       || !host_integerp (num, 0)
21305       || (n = tree_low_cst (num, 0)) <= 0
21306       || (int) n != n)
21307     {
21308       error_at (loc, "collapse argument needs positive constant integer expression");
21309       return list;
21310     }
21311
21312   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21313   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21314   OMP_CLAUSE_CHAIN (c) = list;
21315   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21316
21317   return c;
21318 }
21319
21320 /* OpenMP 2.5:
21321    default ( shared | none ) */
21322
21323 static tree
21324 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21325 {
21326   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21327   tree c;
21328
21329   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21330     return list;
21331   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21332     {
21333       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21334       const char *p = IDENTIFIER_POINTER (id);
21335
21336       switch (p[0])
21337         {
21338         case 'n':
21339           if (strcmp ("none", p) != 0)
21340             goto invalid_kind;
21341           kind = OMP_CLAUSE_DEFAULT_NONE;
21342           break;
21343
21344         case 's':
21345           if (strcmp ("shared", p) != 0)
21346             goto invalid_kind;
21347           kind = OMP_CLAUSE_DEFAULT_SHARED;
21348           break;
21349
21350         default:
21351           goto invalid_kind;
21352         }
21353
21354       cp_lexer_consume_token (parser->lexer);
21355     }
21356   else
21357     {
21358     invalid_kind:
21359       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21360     }
21361
21362   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21363     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21364                                            /*or_comma=*/false,
21365                                            /*consume_paren=*/true);
21366
21367   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21368     return list;
21369
21370   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21371   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21372   OMP_CLAUSE_CHAIN (c) = list;
21373   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21374
21375   return c;
21376 }
21377
21378 /* OpenMP 2.5:
21379    if ( expression ) */
21380
21381 static tree
21382 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21383 {
21384   tree t, c;
21385
21386   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21387     return list;
21388
21389   t = cp_parser_condition (parser);
21390
21391   if (t == error_mark_node
21392       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21393     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21394                                            /*or_comma=*/false,
21395                                            /*consume_paren=*/true);
21396
21397   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21398
21399   c = build_omp_clause (location, OMP_CLAUSE_IF);
21400   OMP_CLAUSE_IF_EXPR (c) = t;
21401   OMP_CLAUSE_CHAIN (c) = list;
21402
21403   return c;
21404 }
21405
21406 /* OpenMP 2.5:
21407    nowait */
21408
21409 static tree
21410 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21411                              tree list, location_t location)
21412 {
21413   tree c;
21414
21415   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21416
21417   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21418   OMP_CLAUSE_CHAIN (c) = list;
21419   return c;
21420 }
21421
21422 /* OpenMP 2.5:
21423    num_threads ( expression ) */
21424
21425 static tree
21426 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21427                                   location_t location)
21428 {
21429   tree t, c;
21430
21431   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21432     return list;
21433
21434   t = cp_parser_expression (parser, false, NULL);
21435
21436   if (t == error_mark_node
21437       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21438     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21439                                            /*or_comma=*/false,
21440                                            /*consume_paren=*/true);
21441
21442   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21443                              "num_threads", location);
21444
21445   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21446   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21447   OMP_CLAUSE_CHAIN (c) = list;
21448
21449   return c;
21450 }
21451
21452 /* OpenMP 2.5:
21453    ordered */
21454
21455 static tree
21456 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21457                               tree list, location_t location)
21458 {
21459   tree c;
21460
21461   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21462                              "ordered", location);
21463
21464   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21465   OMP_CLAUSE_CHAIN (c) = list;
21466   return c;
21467 }
21468
21469 /* OpenMP 2.5:
21470    reduction ( reduction-operator : variable-list )
21471
21472    reduction-operator:
21473      One of: + * - & ^ | && || */
21474
21475 static tree
21476 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21477 {
21478   enum tree_code code;
21479   tree nlist, c;
21480
21481   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21482     return list;
21483
21484   switch (cp_lexer_peek_token (parser->lexer)->type)
21485     {
21486     case CPP_PLUS:
21487       code = PLUS_EXPR;
21488       break;
21489     case CPP_MULT:
21490       code = MULT_EXPR;
21491       break;
21492     case CPP_MINUS:
21493       code = MINUS_EXPR;
21494       break;
21495     case CPP_AND:
21496       code = BIT_AND_EXPR;
21497       break;
21498     case CPP_XOR:
21499       code = BIT_XOR_EXPR;
21500       break;
21501     case CPP_OR:
21502       code = BIT_IOR_EXPR;
21503       break;
21504     case CPP_AND_AND:
21505       code = TRUTH_ANDIF_EXPR;
21506       break;
21507     case CPP_OR_OR:
21508       code = TRUTH_ORIF_EXPR;
21509       break;
21510     default:
21511       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21512                                "%<|%>, %<&&%>, or %<||%>");
21513     resync_fail:
21514       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21515                                              /*or_comma=*/false,
21516                                              /*consume_paren=*/true);
21517       return list;
21518     }
21519   cp_lexer_consume_token (parser->lexer);
21520
21521   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21522     goto resync_fail;
21523
21524   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21525   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21526     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21527
21528   return nlist;
21529 }
21530
21531 /* OpenMP 2.5:
21532    schedule ( schedule-kind )
21533    schedule ( schedule-kind , expression )
21534
21535    schedule-kind:
21536      static | dynamic | guided | runtime | auto  */
21537
21538 static tree
21539 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21540 {
21541   tree c, t;
21542
21543   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21544     return list;
21545
21546   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21547
21548   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21549     {
21550       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21551       const char *p = IDENTIFIER_POINTER (id);
21552
21553       switch (p[0])
21554         {
21555         case 'd':
21556           if (strcmp ("dynamic", p) != 0)
21557             goto invalid_kind;
21558           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21559           break;
21560
21561         case 'g':
21562           if (strcmp ("guided", p) != 0)
21563             goto invalid_kind;
21564           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21565           break;
21566
21567         case 'r':
21568           if (strcmp ("runtime", p) != 0)
21569             goto invalid_kind;
21570           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21571           break;
21572
21573         default:
21574           goto invalid_kind;
21575         }
21576     }
21577   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21578     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21579   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21580     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21581   else
21582     goto invalid_kind;
21583   cp_lexer_consume_token (parser->lexer);
21584
21585   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21586     {
21587       cp_token *token;
21588       cp_lexer_consume_token (parser->lexer);
21589
21590       token = cp_lexer_peek_token (parser->lexer);
21591       t = cp_parser_assignment_expression (parser, false, NULL);
21592
21593       if (t == error_mark_node)
21594         goto resync_fail;
21595       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21596         error_at (token->location, "schedule %<runtime%> does not take "
21597                   "a %<chunk_size%> parameter");
21598       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21599         error_at (token->location, "schedule %<auto%> does not take "
21600                   "a %<chunk_size%> parameter");
21601       else
21602         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21603
21604       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21605         goto resync_fail;
21606     }
21607   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21608     goto resync_fail;
21609
21610   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21611   OMP_CLAUSE_CHAIN (c) = list;
21612   return c;
21613
21614  invalid_kind:
21615   cp_parser_error (parser, "invalid schedule kind");
21616  resync_fail:
21617   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21618                                          /*or_comma=*/false,
21619                                          /*consume_paren=*/true);
21620   return list;
21621 }
21622
21623 /* OpenMP 3.0:
21624    untied */
21625
21626 static tree
21627 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21628                              tree list, location_t location)
21629 {
21630   tree c;
21631
21632   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21633
21634   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21635   OMP_CLAUSE_CHAIN (c) = list;
21636   return c;
21637 }
21638
21639 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21640    is a bitmask in MASK.  Return the list of clauses found; the result
21641    of clause default goes in *pdefault.  */
21642
21643 static tree
21644 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21645                            const char *where, cp_token *pragma_tok)
21646 {
21647   tree clauses = NULL;
21648   bool first = true;
21649   cp_token *token = NULL;
21650
21651   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21652     {
21653       pragma_omp_clause c_kind;
21654       const char *c_name;
21655       tree prev = clauses;
21656
21657       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21658         cp_lexer_consume_token (parser->lexer);
21659
21660       token = cp_lexer_peek_token (parser->lexer);
21661       c_kind = cp_parser_omp_clause_name (parser);
21662       first = false;
21663
21664       switch (c_kind)
21665         {
21666         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21667           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21668                                                    token->location);
21669           c_name = "collapse";
21670           break;
21671         case PRAGMA_OMP_CLAUSE_COPYIN:
21672           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21673           c_name = "copyin";
21674           break;
21675         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21676           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21677                                             clauses);
21678           c_name = "copyprivate";
21679           break;
21680         case PRAGMA_OMP_CLAUSE_DEFAULT:
21681           clauses = cp_parser_omp_clause_default (parser, clauses,
21682                                                   token->location);
21683           c_name = "default";
21684           break;
21685         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21686           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21687                                             clauses);
21688           c_name = "firstprivate";
21689           break;
21690         case PRAGMA_OMP_CLAUSE_IF:
21691           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21692           c_name = "if";
21693           break;
21694         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21695           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21696                                             clauses);
21697           c_name = "lastprivate";
21698           break;
21699         case PRAGMA_OMP_CLAUSE_NOWAIT:
21700           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21701           c_name = "nowait";
21702           break;
21703         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21704           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21705                                                       token->location);
21706           c_name = "num_threads";
21707           break;
21708         case PRAGMA_OMP_CLAUSE_ORDERED:
21709           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21710                                                   token->location);
21711           c_name = "ordered";
21712           break;
21713         case PRAGMA_OMP_CLAUSE_PRIVATE:
21714           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21715                                             clauses);
21716           c_name = "private";
21717           break;
21718         case PRAGMA_OMP_CLAUSE_REDUCTION:
21719           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21720           c_name = "reduction";
21721           break;
21722         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21723           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21724                                                    token->location);
21725           c_name = "schedule";
21726           break;
21727         case PRAGMA_OMP_CLAUSE_SHARED:
21728           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21729                                             clauses);
21730           c_name = "shared";
21731           break;
21732         case PRAGMA_OMP_CLAUSE_UNTIED:
21733           clauses = cp_parser_omp_clause_untied (parser, clauses,
21734                                                  token->location);
21735           c_name = "nowait";
21736           break;
21737         default:
21738           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21739           goto saw_error;
21740         }
21741
21742       if (((mask >> c_kind) & 1) == 0)
21743         {
21744           /* Remove the invalid clause(s) from the list to avoid
21745              confusing the rest of the compiler.  */
21746           clauses = prev;
21747           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21748         }
21749     }
21750  saw_error:
21751   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21752   return finish_omp_clauses (clauses);
21753 }
21754
21755 /* OpenMP 2.5:
21756    structured-block:
21757      statement
21758
21759    In practice, we're also interested in adding the statement to an
21760    outer node.  So it is convenient if we work around the fact that
21761    cp_parser_statement calls add_stmt.  */
21762
21763 static unsigned
21764 cp_parser_begin_omp_structured_block (cp_parser *parser)
21765 {
21766   unsigned save = parser->in_statement;
21767
21768   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21769      This preserves the "not within loop or switch" style error messages
21770      for nonsense cases like
21771         void foo() {
21772         #pragma omp single
21773           break;
21774         }
21775   */
21776   if (parser->in_statement)
21777     parser->in_statement = IN_OMP_BLOCK;
21778
21779   return save;
21780 }
21781
21782 static void
21783 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21784 {
21785   parser->in_statement = save;
21786 }
21787
21788 static tree
21789 cp_parser_omp_structured_block (cp_parser *parser)
21790 {
21791   tree stmt = begin_omp_structured_block ();
21792   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21793
21794   cp_parser_statement (parser, NULL_TREE, false, NULL);
21795
21796   cp_parser_end_omp_structured_block (parser, save);
21797   return finish_omp_structured_block (stmt);
21798 }
21799
21800 /* OpenMP 2.5:
21801    # pragma omp atomic new-line
21802      expression-stmt
21803
21804    expression-stmt:
21805      x binop= expr | x++ | ++x | x-- | --x
21806    binop:
21807      +, *, -, /, &, ^, |, <<, >>
21808
21809   where x is an lvalue expression with scalar type.  */
21810
21811 static void
21812 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21813 {
21814   tree lhs, rhs;
21815   enum tree_code code;
21816
21817   cp_parser_require_pragma_eol (parser, pragma_tok);
21818
21819   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21820                                     /*cast_p=*/false, NULL);
21821   switch (TREE_CODE (lhs))
21822     {
21823     case ERROR_MARK:
21824       goto saw_error;
21825
21826     case PREINCREMENT_EXPR:
21827     case POSTINCREMENT_EXPR:
21828       lhs = TREE_OPERAND (lhs, 0);
21829       code = PLUS_EXPR;
21830       rhs = integer_one_node;
21831       break;
21832
21833     case PREDECREMENT_EXPR:
21834     case POSTDECREMENT_EXPR:
21835       lhs = TREE_OPERAND (lhs, 0);
21836       code = MINUS_EXPR;
21837       rhs = integer_one_node;
21838       break;
21839
21840     default:
21841       switch (cp_lexer_peek_token (parser->lexer)->type)
21842         {
21843         case CPP_MULT_EQ:
21844           code = MULT_EXPR;
21845           break;
21846         case CPP_DIV_EQ:
21847           code = TRUNC_DIV_EXPR;
21848           break;
21849         case CPP_PLUS_EQ:
21850           code = PLUS_EXPR;
21851           break;
21852         case CPP_MINUS_EQ:
21853           code = MINUS_EXPR;
21854           break;
21855         case CPP_LSHIFT_EQ:
21856           code = LSHIFT_EXPR;
21857           break;
21858         case CPP_RSHIFT_EQ:
21859           code = RSHIFT_EXPR;
21860           break;
21861         case CPP_AND_EQ:
21862           code = BIT_AND_EXPR;
21863           break;
21864         case CPP_OR_EQ:
21865           code = BIT_IOR_EXPR;
21866           break;
21867         case CPP_XOR_EQ:
21868           code = BIT_XOR_EXPR;
21869           break;
21870         default:
21871           cp_parser_error (parser,
21872                            "invalid operator for %<#pragma omp atomic%>");
21873           goto saw_error;
21874         }
21875       cp_lexer_consume_token (parser->lexer);
21876
21877       rhs = cp_parser_expression (parser, false, NULL);
21878       if (rhs == error_mark_node)
21879         goto saw_error;
21880       break;
21881     }
21882   finish_omp_atomic (code, lhs, rhs);
21883   cp_parser_consume_semicolon_at_end_of_statement (parser);
21884   return;
21885
21886  saw_error:
21887   cp_parser_skip_to_end_of_block_or_statement (parser);
21888 }
21889
21890
21891 /* OpenMP 2.5:
21892    # pragma omp barrier new-line  */
21893
21894 static void
21895 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21896 {
21897   cp_parser_require_pragma_eol (parser, pragma_tok);
21898   finish_omp_barrier ();
21899 }
21900
21901 /* OpenMP 2.5:
21902    # pragma omp critical [(name)] new-line
21903      structured-block  */
21904
21905 static tree
21906 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21907 {
21908   tree stmt, name = NULL;
21909
21910   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21911     {
21912       cp_lexer_consume_token (parser->lexer);
21913
21914       name = cp_parser_identifier (parser);
21915
21916       if (name == error_mark_node
21917           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21918         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21919                                                /*or_comma=*/false,
21920                                                /*consume_paren=*/true);
21921       if (name == error_mark_node)
21922         name = NULL;
21923     }
21924   cp_parser_require_pragma_eol (parser, pragma_tok);
21925
21926   stmt = cp_parser_omp_structured_block (parser);
21927   return c_finish_omp_critical (input_location, stmt, name);
21928 }
21929
21930 /* OpenMP 2.5:
21931    # pragma omp flush flush-vars[opt] new-line
21932
21933    flush-vars:
21934      ( variable-list ) */
21935
21936 static void
21937 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21938 {
21939   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21940     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21941   cp_parser_require_pragma_eol (parser, pragma_tok);
21942
21943   finish_omp_flush ();
21944 }
21945
21946 /* Helper function, to parse omp for increment expression.  */
21947
21948 static tree
21949 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21950 {
21951   tree cond = cp_parser_binary_expression (parser, false, true,
21952                                            PREC_NOT_OPERATOR, NULL);
21953   bool overloaded_p;
21954
21955   if (cond == error_mark_node
21956       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21957     {
21958       cp_parser_skip_to_end_of_statement (parser);
21959       return error_mark_node;
21960     }
21961
21962   switch (TREE_CODE (cond))
21963     {
21964     case GT_EXPR:
21965     case GE_EXPR:
21966     case LT_EXPR:
21967     case LE_EXPR:
21968       break;
21969     default:
21970       return error_mark_node;
21971     }
21972
21973   /* If decl is an iterator, preserve LHS and RHS of the relational
21974      expr until finish_omp_for.  */
21975   if (decl
21976       && (type_dependent_expression_p (decl)
21977           || CLASS_TYPE_P (TREE_TYPE (decl))))
21978     return cond;
21979
21980   return build_x_binary_op (TREE_CODE (cond),
21981                             TREE_OPERAND (cond, 0), ERROR_MARK,
21982                             TREE_OPERAND (cond, 1), ERROR_MARK,
21983                             &overloaded_p, tf_warning_or_error);
21984 }
21985
21986 /* Helper function, to parse omp for increment expression.  */
21987
21988 static tree
21989 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21990 {
21991   cp_token *token = cp_lexer_peek_token (parser->lexer);
21992   enum tree_code op;
21993   tree lhs, rhs;
21994   cp_id_kind idk;
21995   bool decl_first;
21996
21997   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21998     {
21999       op = (token->type == CPP_PLUS_PLUS
22000             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22001       cp_lexer_consume_token (parser->lexer);
22002       lhs = cp_parser_cast_expression (parser, false, false, NULL);
22003       if (lhs != decl)
22004         return error_mark_node;
22005       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22006     }
22007
22008   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22009   if (lhs != decl)
22010     return error_mark_node;
22011
22012   token = cp_lexer_peek_token (parser->lexer);
22013   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22014     {
22015       op = (token->type == CPP_PLUS_PLUS
22016             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22017       cp_lexer_consume_token (parser->lexer);
22018       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22019     }
22020
22021   op = cp_parser_assignment_operator_opt (parser);
22022   if (op == ERROR_MARK)
22023     return error_mark_node;
22024
22025   if (op != NOP_EXPR)
22026     {
22027       rhs = cp_parser_assignment_expression (parser, false, NULL);
22028       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22029       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22030     }
22031
22032   lhs = cp_parser_binary_expression (parser, false, false,
22033                                      PREC_ADDITIVE_EXPRESSION, NULL);
22034   token = cp_lexer_peek_token (parser->lexer);
22035   decl_first = lhs == decl;
22036   if (decl_first)
22037     lhs = NULL_TREE;
22038   if (token->type != CPP_PLUS
22039       && token->type != CPP_MINUS)
22040     return error_mark_node;
22041
22042   do
22043     {
22044       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22045       cp_lexer_consume_token (parser->lexer);
22046       rhs = cp_parser_binary_expression (parser, false, false,
22047                                          PREC_ADDITIVE_EXPRESSION, NULL);
22048       token = cp_lexer_peek_token (parser->lexer);
22049       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22050         {
22051           if (lhs == NULL_TREE)
22052             {
22053               if (op == PLUS_EXPR)
22054                 lhs = rhs;
22055               else
22056                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22057             }
22058           else
22059             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22060                                      NULL, tf_warning_or_error);
22061         }
22062     }
22063   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22064
22065   if (!decl_first)
22066     {
22067       if (rhs != decl || op == MINUS_EXPR)
22068         return error_mark_node;
22069       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22070     }
22071   else
22072     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22073
22074   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22075 }
22076
22077 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22078
22079 static tree
22080 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22081 {
22082   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22083   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22084   tree this_pre_body, cl;
22085   location_t loc_first;
22086   bool collapse_err = false;
22087   int i, collapse = 1, nbraces = 0;
22088
22089   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22090     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22091       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22092
22093   gcc_assert (collapse >= 1);
22094
22095   declv = make_tree_vec (collapse);
22096   initv = make_tree_vec (collapse);
22097   condv = make_tree_vec (collapse);
22098   incrv = make_tree_vec (collapse);
22099
22100   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22101
22102   for (i = 0; i < collapse; i++)
22103     {
22104       int bracecount = 0;
22105       bool add_private_clause = false;
22106       location_t loc;
22107
22108       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22109         {
22110           cp_parser_error (parser, "for statement expected");
22111           return NULL;
22112         }
22113       loc = cp_lexer_consume_token (parser->lexer)->location;
22114
22115       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22116         return NULL;
22117
22118       init = decl = real_decl = NULL;
22119       this_pre_body = push_stmt_list ();
22120       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22121         {
22122           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22123
22124              init-expr:
22125                        var = lb
22126                        integer-type var = lb
22127                        random-access-iterator-type var = lb
22128                        pointer-type var = lb
22129           */
22130           cp_decl_specifier_seq type_specifiers;
22131
22132           /* First, try to parse as an initialized declaration.  See
22133              cp_parser_condition, from whence the bulk of this is copied.  */
22134
22135           cp_parser_parse_tentatively (parser);
22136           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22137                                         /*is_trailing_return=*/false,
22138                                         &type_specifiers);
22139           if (cp_parser_parse_definitely (parser))
22140             {
22141               /* If parsing a type specifier seq succeeded, then this
22142                  MUST be a initialized declaration.  */
22143               tree asm_specification, attributes;
22144               cp_declarator *declarator;
22145
22146               declarator = cp_parser_declarator (parser,
22147                                                  CP_PARSER_DECLARATOR_NAMED,
22148                                                  /*ctor_dtor_or_conv_p=*/NULL,
22149                                                  /*parenthesized_p=*/NULL,
22150                                                  /*member_p=*/false);
22151               attributes = cp_parser_attributes_opt (parser);
22152               asm_specification = cp_parser_asm_specification_opt (parser);
22153
22154               if (declarator == cp_error_declarator) 
22155                 cp_parser_skip_to_end_of_statement (parser);
22156
22157               else 
22158                 {
22159                   tree pushed_scope, auto_node;
22160
22161                   decl = start_decl (declarator, &type_specifiers,
22162                                      SD_INITIALIZED, attributes,
22163                                      /*prefix_attributes=*/NULL_TREE,
22164                                      &pushed_scope);
22165
22166                   auto_node = type_uses_auto (TREE_TYPE (decl));
22167                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22168                     {
22169                       if (cp_lexer_next_token_is (parser->lexer, 
22170                                                   CPP_OPEN_PAREN))
22171                         error ("parenthesized initialization is not allowed in "
22172                                "OpenMP %<for%> loop");
22173                       else
22174                         /* Trigger an error.  */
22175                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22176
22177                       init = error_mark_node;
22178                       cp_parser_skip_to_end_of_statement (parser);
22179                     }
22180                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22181                            || type_dependent_expression_p (decl)
22182                            || auto_node)
22183                     {
22184                       bool is_direct_init, is_non_constant_init;
22185
22186                       init = cp_parser_initializer (parser,
22187                                                     &is_direct_init,
22188                                                     &is_non_constant_init);
22189
22190                       if (auto_node && describable_type (init))
22191                         {
22192                           TREE_TYPE (decl)
22193                             = do_auto_deduction (TREE_TYPE (decl), init,
22194                                                  auto_node);
22195
22196                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22197                               && !type_dependent_expression_p (decl))
22198                             goto non_class;
22199                         }
22200                       
22201                       cp_finish_decl (decl, init, !is_non_constant_init,
22202                                       asm_specification,
22203                                       LOOKUP_ONLYCONVERTING);
22204                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22205                         {
22206                           for_block
22207                             = tree_cons (NULL, this_pre_body, for_block);
22208                           init = NULL_TREE;
22209                         }
22210                       else
22211                         init = pop_stmt_list (this_pre_body);
22212                       this_pre_body = NULL_TREE;
22213                     }
22214                   else
22215                     {
22216                       /* Consume '='.  */
22217                       cp_lexer_consume_token (parser->lexer);
22218                       init = cp_parser_assignment_expression (parser, false, NULL);
22219
22220                     non_class:
22221                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22222                         init = error_mark_node;
22223                       else
22224                         cp_finish_decl (decl, NULL_TREE,
22225                                         /*init_const_expr_p=*/false,
22226                                         asm_specification,
22227                                         LOOKUP_ONLYCONVERTING);
22228                     }
22229
22230                   if (pushed_scope)
22231                     pop_scope (pushed_scope);
22232                 }
22233             }
22234           else 
22235             {
22236               cp_id_kind idk;
22237               /* If parsing a type specifier sequence failed, then
22238                  this MUST be a simple expression.  */
22239               cp_parser_parse_tentatively (parser);
22240               decl = cp_parser_primary_expression (parser, false, false,
22241                                                    false, &idk);
22242               if (!cp_parser_error_occurred (parser)
22243                   && decl
22244                   && DECL_P (decl)
22245                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22246                 {
22247                   tree rhs;
22248
22249                   cp_parser_parse_definitely (parser);
22250                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22251                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22252                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22253                                                          rhs,
22254                                                          tf_warning_or_error));
22255                   add_private_clause = true;
22256                 }
22257               else
22258                 {
22259                   decl = NULL;
22260                   cp_parser_abort_tentative_parse (parser);
22261                   init = cp_parser_expression (parser, false, NULL);
22262                   if (init)
22263                     {
22264                       if (TREE_CODE (init) == MODIFY_EXPR
22265                           || TREE_CODE (init) == MODOP_EXPR)
22266                         real_decl = TREE_OPERAND (init, 0);
22267                     }
22268                 }
22269             }
22270         }
22271       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22272       if (this_pre_body)
22273         {
22274           this_pre_body = pop_stmt_list (this_pre_body);
22275           if (pre_body)
22276             {
22277               tree t = pre_body;
22278               pre_body = push_stmt_list ();
22279               add_stmt (t);
22280               add_stmt (this_pre_body);
22281               pre_body = pop_stmt_list (pre_body);
22282             }
22283           else
22284             pre_body = this_pre_body;
22285         }
22286
22287       if (decl)
22288         real_decl = decl;
22289       if (par_clauses != NULL && real_decl != NULL_TREE)
22290         {
22291           tree *c;
22292           for (c = par_clauses; *c ; )
22293             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22294                 && OMP_CLAUSE_DECL (*c) == real_decl)
22295               {
22296                 error_at (loc, "iteration variable %qD"
22297                           " should not be firstprivate", real_decl);
22298                 *c = OMP_CLAUSE_CHAIN (*c);
22299               }
22300             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22301                      && OMP_CLAUSE_DECL (*c) == real_decl)
22302               {
22303                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22304                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22305                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22306                 OMP_CLAUSE_DECL (l) = real_decl;
22307                 OMP_CLAUSE_CHAIN (l) = clauses;
22308                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22309                 clauses = l;
22310                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22311                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22312                 add_private_clause = false;
22313               }
22314             else
22315               {
22316                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22317                     && OMP_CLAUSE_DECL (*c) == real_decl)
22318                   add_private_clause = false;
22319                 c = &OMP_CLAUSE_CHAIN (*c);
22320               }
22321         }
22322
22323       if (add_private_clause)
22324         {
22325           tree c;
22326           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22327             {
22328               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22329                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22330                   && OMP_CLAUSE_DECL (c) == decl)
22331                 break;
22332               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22333                        && OMP_CLAUSE_DECL (c) == decl)
22334                 error_at (loc, "iteration variable %qD "
22335                           "should not be firstprivate",
22336                           decl);
22337               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22338                        && OMP_CLAUSE_DECL (c) == decl)
22339                 error_at (loc, "iteration variable %qD should not be reduction",
22340                           decl);
22341             }
22342           if (c == NULL)
22343             {
22344               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22345               OMP_CLAUSE_DECL (c) = decl;
22346               c = finish_omp_clauses (c);
22347               if (c)
22348                 {
22349                   OMP_CLAUSE_CHAIN (c) = clauses;
22350                   clauses = c;
22351                 }
22352             }
22353         }
22354
22355       cond = NULL;
22356       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22357         cond = cp_parser_omp_for_cond (parser, decl);
22358       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22359
22360       incr = NULL;
22361       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22362         {
22363           /* If decl is an iterator, preserve the operator on decl
22364              until finish_omp_for.  */
22365           if (decl
22366               && (type_dependent_expression_p (decl)
22367                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22368             incr = cp_parser_omp_for_incr (parser, decl);
22369           else
22370             incr = cp_parser_expression (parser, false, NULL);
22371         }
22372
22373       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22374         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22375                                                /*or_comma=*/false,
22376                                                /*consume_paren=*/true);
22377
22378       TREE_VEC_ELT (declv, i) = decl;
22379       TREE_VEC_ELT (initv, i) = init;
22380       TREE_VEC_ELT (condv, i) = cond;
22381       TREE_VEC_ELT (incrv, i) = incr;
22382
22383       if (i == collapse - 1)
22384         break;
22385
22386       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22387          in between the collapsed for loops to be still considered perfectly
22388          nested.  Hopefully the final version clarifies this.
22389          For now handle (multiple) {'s and empty statements.  */
22390       cp_parser_parse_tentatively (parser);
22391       do
22392         {
22393           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22394             break;
22395           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22396             {
22397               cp_lexer_consume_token (parser->lexer);
22398               bracecount++;
22399             }
22400           else if (bracecount
22401                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22402             cp_lexer_consume_token (parser->lexer);
22403           else
22404             {
22405               loc = cp_lexer_peek_token (parser->lexer)->location;
22406               error_at (loc, "not enough collapsed for loops");
22407               collapse_err = true;
22408               cp_parser_abort_tentative_parse (parser);
22409               declv = NULL_TREE;
22410               break;
22411             }
22412         }
22413       while (1);
22414
22415       if (declv)
22416         {
22417           cp_parser_parse_definitely (parser);
22418           nbraces += bracecount;
22419         }
22420     }
22421
22422   /* Note that we saved the original contents of this flag when we entered
22423      the structured block, and so we don't need to re-save it here.  */
22424   parser->in_statement = IN_OMP_FOR;
22425
22426   /* Note that the grammar doesn't call for a structured block here,
22427      though the loop as a whole is a structured block.  */
22428   body = push_stmt_list ();
22429   cp_parser_statement (parser, NULL_TREE, false, NULL);
22430   body = pop_stmt_list (body);
22431
22432   if (declv == NULL_TREE)
22433     ret = NULL_TREE;
22434   else
22435     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22436                           pre_body, clauses);
22437
22438   while (nbraces)
22439     {
22440       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22441         {
22442           cp_lexer_consume_token (parser->lexer);
22443           nbraces--;
22444         }
22445       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22446         cp_lexer_consume_token (parser->lexer);
22447       else
22448         {
22449           if (!collapse_err)
22450             {
22451               error_at (cp_lexer_peek_token (parser->lexer)->location,
22452                         "collapsed loops not perfectly nested");
22453             }
22454           collapse_err = true;
22455           cp_parser_statement_seq_opt (parser, NULL);
22456           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22457             break;
22458         }
22459     }
22460
22461   while (for_block)
22462     {
22463       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22464       for_block = TREE_CHAIN (for_block);
22465     }
22466
22467   return ret;
22468 }
22469
22470 /* OpenMP 2.5:
22471    #pragma omp for for-clause[optseq] new-line
22472      for-loop  */
22473
22474 #define OMP_FOR_CLAUSE_MASK                             \
22475         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22476         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22477         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22478         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22479         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22480         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22481         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22482         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22483
22484 static tree
22485 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22486 {
22487   tree clauses, sb, ret;
22488   unsigned int save;
22489
22490   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22491                                        "#pragma omp for", pragma_tok);
22492
22493   sb = begin_omp_structured_block ();
22494   save = cp_parser_begin_omp_structured_block (parser);
22495
22496   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22497
22498   cp_parser_end_omp_structured_block (parser, save);
22499   add_stmt (finish_omp_structured_block (sb));
22500
22501   return ret;
22502 }
22503
22504 /* OpenMP 2.5:
22505    # pragma omp master new-line
22506      structured-block  */
22507
22508 static tree
22509 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22510 {
22511   cp_parser_require_pragma_eol (parser, pragma_tok);
22512   return c_finish_omp_master (input_location,
22513                               cp_parser_omp_structured_block (parser));
22514 }
22515
22516 /* OpenMP 2.5:
22517    # pragma omp ordered new-line
22518      structured-block  */
22519
22520 static tree
22521 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22522 {
22523   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22524   cp_parser_require_pragma_eol (parser, pragma_tok);
22525   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22526 }
22527
22528 /* OpenMP 2.5:
22529
22530    section-scope:
22531      { section-sequence }
22532
22533    section-sequence:
22534      section-directive[opt] structured-block
22535      section-sequence section-directive structured-block  */
22536
22537 static tree
22538 cp_parser_omp_sections_scope (cp_parser *parser)
22539 {
22540   tree stmt, substmt;
22541   bool error_suppress = false;
22542   cp_token *tok;
22543
22544   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22545     return NULL_TREE;
22546
22547   stmt = push_stmt_list ();
22548
22549   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22550     {
22551       unsigned save;
22552
22553       substmt = begin_omp_structured_block ();
22554       save = cp_parser_begin_omp_structured_block (parser);
22555
22556       while (1)
22557         {
22558           cp_parser_statement (parser, NULL_TREE, false, NULL);
22559
22560           tok = cp_lexer_peek_token (parser->lexer);
22561           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22562             break;
22563           if (tok->type == CPP_CLOSE_BRACE)
22564             break;
22565           if (tok->type == CPP_EOF)
22566             break;
22567         }
22568
22569       cp_parser_end_omp_structured_block (parser, save);
22570       substmt = finish_omp_structured_block (substmt);
22571       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22572       add_stmt (substmt);
22573     }
22574
22575   while (1)
22576     {
22577       tok = cp_lexer_peek_token (parser->lexer);
22578       if (tok->type == CPP_CLOSE_BRACE)
22579         break;
22580       if (tok->type == CPP_EOF)
22581         break;
22582
22583       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22584         {
22585           cp_lexer_consume_token (parser->lexer);
22586           cp_parser_require_pragma_eol (parser, tok);
22587           error_suppress = false;
22588         }
22589       else if (!error_suppress)
22590         {
22591           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22592           error_suppress = true;
22593         }
22594
22595       substmt = cp_parser_omp_structured_block (parser);
22596       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22597       add_stmt (substmt);
22598     }
22599   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22600
22601   substmt = pop_stmt_list (stmt);
22602
22603   stmt = make_node (OMP_SECTIONS);
22604   TREE_TYPE (stmt) = void_type_node;
22605   OMP_SECTIONS_BODY (stmt) = substmt;
22606
22607   add_stmt (stmt);
22608   return stmt;
22609 }
22610
22611 /* OpenMP 2.5:
22612    # pragma omp sections sections-clause[optseq] newline
22613      sections-scope  */
22614
22615 #define OMP_SECTIONS_CLAUSE_MASK                        \
22616         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22617         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22618         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22619         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22620         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22621
22622 static tree
22623 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22624 {
22625   tree clauses, ret;
22626
22627   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22628                                        "#pragma omp sections", pragma_tok);
22629
22630   ret = cp_parser_omp_sections_scope (parser);
22631   if (ret)
22632     OMP_SECTIONS_CLAUSES (ret) = clauses;
22633
22634   return ret;
22635 }
22636
22637 /* OpenMP 2.5:
22638    # pragma parallel parallel-clause new-line
22639    # pragma parallel for parallel-for-clause new-line
22640    # pragma parallel sections parallel-sections-clause new-line  */
22641
22642 #define OMP_PARALLEL_CLAUSE_MASK                        \
22643         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22644         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22645         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22646         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22647         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22648         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22649         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22650         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22651
22652 static tree
22653 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22654 {
22655   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22656   const char *p_name = "#pragma omp parallel";
22657   tree stmt, clauses, par_clause, ws_clause, block;
22658   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22659   unsigned int save;
22660   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22661
22662   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22663     {
22664       cp_lexer_consume_token (parser->lexer);
22665       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22666       p_name = "#pragma omp parallel for";
22667       mask |= OMP_FOR_CLAUSE_MASK;
22668       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22669     }
22670   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22671     {
22672       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22673       const char *p = IDENTIFIER_POINTER (id);
22674       if (strcmp (p, "sections") == 0)
22675         {
22676           cp_lexer_consume_token (parser->lexer);
22677           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22678           p_name = "#pragma omp parallel sections";
22679           mask |= OMP_SECTIONS_CLAUSE_MASK;
22680           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22681         }
22682     }
22683
22684   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22685   block = begin_omp_parallel ();
22686   save = cp_parser_begin_omp_structured_block (parser);
22687
22688   switch (p_kind)
22689     {
22690     case PRAGMA_OMP_PARALLEL:
22691       cp_parser_statement (parser, NULL_TREE, false, NULL);
22692       par_clause = clauses;
22693       break;
22694
22695     case PRAGMA_OMP_PARALLEL_FOR:
22696       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22697       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22698       break;
22699
22700     case PRAGMA_OMP_PARALLEL_SECTIONS:
22701       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22702       stmt = cp_parser_omp_sections_scope (parser);
22703       if (stmt)
22704         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22705       break;
22706
22707     default:
22708       gcc_unreachable ();
22709     }
22710
22711   cp_parser_end_omp_structured_block (parser, save);
22712   stmt = finish_omp_parallel (par_clause, block);
22713   if (p_kind != PRAGMA_OMP_PARALLEL)
22714     OMP_PARALLEL_COMBINED (stmt) = 1;
22715   return stmt;
22716 }
22717
22718 /* OpenMP 2.5:
22719    # pragma omp single single-clause[optseq] new-line
22720      structured-block  */
22721
22722 #define OMP_SINGLE_CLAUSE_MASK                          \
22723         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22724         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22725         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22726         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22727
22728 static tree
22729 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22730 {
22731   tree stmt = make_node (OMP_SINGLE);
22732   TREE_TYPE (stmt) = void_type_node;
22733
22734   OMP_SINGLE_CLAUSES (stmt)
22735     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22736                                  "#pragma omp single", pragma_tok);
22737   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22738
22739   return add_stmt (stmt);
22740 }
22741
22742 /* OpenMP 3.0:
22743    # pragma omp task task-clause[optseq] new-line
22744      structured-block  */
22745
22746 #define OMP_TASK_CLAUSE_MASK                            \
22747         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22748         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22749         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22750         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22751         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22752         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22753
22754 static tree
22755 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22756 {
22757   tree clauses, block;
22758   unsigned int save;
22759
22760   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22761                                        "#pragma omp task", pragma_tok);
22762   block = begin_omp_task ();
22763   save = cp_parser_begin_omp_structured_block (parser);
22764   cp_parser_statement (parser, NULL_TREE, false, NULL);
22765   cp_parser_end_omp_structured_block (parser, save);
22766   return finish_omp_task (clauses, block);
22767 }
22768
22769 /* OpenMP 3.0:
22770    # pragma omp taskwait new-line  */
22771
22772 static void
22773 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22774 {
22775   cp_parser_require_pragma_eol (parser, pragma_tok);
22776   finish_omp_taskwait ();
22777 }
22778
22779 /* OpenMP 2.5:
22780    # pragma omp threadprivate (variable-list) */
22781
22782 static void
22783 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22784 {
22785   tree vars;
22786
22787   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22788   cp_parser_require_pragma_eol (parser, pragma_tok);
22789
22790   finish_omp_threadprivate (vars);
22791 }
22792
22793 /* Main entry point to OpenMP statement pragmas.  */
22794
22795 static void
22796 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22797 {
22798   tree stmt;
22799
22800   switch (pragma_tok->pragma_kind)
22801     {
22802     case PRAGMA_OMP_ATOMIC:
22803       cp_parser_omp_atomic (parser, pragma_tok);
22804       return;
22805     case PRAGMA_OMP_CRITICAL:
22806       stmt = cp_parser_omp_critical (parser, pragma_tok);
22807       break;
22808     case PRAGMA_OMP_FOR:
22809       stmt = cp_parser_omp_for (parser, pragma_tok);
22810       break;
22811     case PRAGMA_OMP_MASTER:
22812       stmt = cp_parser_omp_master (parser, pragma_tok);
22813       break;
22814     case PRAGMA_OMP_ORDERED:
22815       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22816       break;
22817     case PRAGMA_OMP_PARALLEL:
22818       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22819       break;
22820     case PRAGMA_OMP_SECTIONS:
22821       stmt = cp_parser_omp_sections (parser, pragma_tok);
22822       break;
22823     case PRAGMA_OMP_SINGLE:
22824       stmt = cp_parser_omp_single (parser, pragma_tok);
22825       break;
22826     case PRAGMA_OMP_TASK:
22827       stmt = cp_parser_omp_task (parser, pragma_tok);
22828       break;
22829     default:
22830       gcc_unreachable ();
22831     }
22832
22833   if (stmt)
22834     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22835 }
22836 \f
22837 /* The parser.  */
22838
22839 static GTY (()) cp_parser *the_parser;
22840
22841 \f
22842 /* Special handling for the first token or line in the file.  The first
22843    thing in the file might be #pragma GCC pch_preprocess, which loads a
22844    PCH file, which is a GC collection point.  So we need to handle this
22845    first pragma without benefit of an existing lexer structure.
22846
22847    Always returns one token to the caller in *FIRST_TOKEN.  This is
22848    either the true first token of the file, or the first token after
22849    the initial pragma.  */
22850
22851 static void
22852 cp_parser_initial_pragma (cp_token *first_token)
22853 {
22854   tree name = NULL;
22855
22856   cp_lexer_get_preprocessor_token (NULL, first_token);
22857   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22858     return;
22859
22860   cp_lexer_get_preprocessor_token (NULL, first_token);
22861   if (first_token->type == CPP_STRING)
22862     {
22863       name = first_token->u.value;
22864
22865       cp_lexer_get_preprocessor_token (NULL, first_token);
22866       if (first_token->type != CPP_PRAGMA_EOL)
22867         error_at (first_token->location,
22868                   "junk at end of %<#pragma GCC pch_preprocess%>");
22869     }
22870   else
22871     error_at (first_token->location, "expected string literal");
22872
22873   /* Skip to the end of the pragma.  */
22874   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22875     cp_lexer_get_preprocessor_token (NULL, first_token);
22876
22877   /* Now actually load the PCH file.  */
22878   if (name)
22879     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22880
22881   /* Read one more token to return to our caller.  We have to do this
22882      after reading the PCH file in, since its pointers have to be
22883      live.  */
22884   cp_lexer_get_preprocessor_token (NULL, first_token);
22885 }
22886
22887 /* Normal parsing of a pragma token.  Here we can (and must) use the
22888    regular lexer.  */
22889
22890 static bool
22891 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22892 {
22893   cp_token *pragma_tok;
22894   unsigned int id;
22895
22896   pragma_tok = cp_lexer_consume_token (parser->lexer);
22897   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22898   parser->lexer->in_pragma = true;
22899
22900   id = pragma_tok->pragma_kind;
22901   switch (id)
22902     {
22903     case PRAGMA_GCC_PCH_PREPROCESS:
22904       error_at (pragma_tok->location,
22905                 "%<#pragma GCC pch_preprocess%> must be first");
22906       break;
22907
22908     case PRAGMA_OMP_BARRIER:
22909       switch (context)
22910         {
22911         case pragma_compound:
22912           cp_parser_omp_barrier (parser, pragma_tok);
22913           return false;
22914         case pragma_stmt:
22915           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22916                     "used in compound statements");
22917           break;
22918         default:
22919           goto bad_stmt;
22920         }
22921       break;
22922
22923     case PRAGMA_OMP_FLUSH:
22924       switch (context)
22925         {
22926         case pragma_compound:
22927           cp_parser_omp_flush (parser, pragma_tok);
22928           return false;
22929         case pragma_stmt:
22930           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22931                     "used in compound statements");
22932           break;
22933         default:
22934           goto bad_stmt;
22935         }
22936       break;
22937
22938     case PRAGMA_OMP_TASKWAIT:
22939       switch (context)
22940         {
22941         case pragma_compound:
22942           cp_parser_omp_taskwait (parser, pragma_tok);
22943           return false;
22944         case pragma_stmt:
22945           error_at (pragma_tok->location,
22946                     "%<#pragma omp taskwait%> may only be "
22947                     "used in compound statements");
22948           break;
22949         default:
22950           goto bad_stmt;
22951         }
22952       break;
22953
22954     case PRAGMA_OMP_THREADPRIVATE:
22955       cp_parser_omp_threadprivate (parser, pragma_tok);
22956       return false;
22957
22958     case PRAGMA_OMP_ATOMIC:
22959     case PRAGMA_OMP_CRITICAL:
22960     case PRAGMA_OMP_FOR:
22961     case PRAGMA_OMP_MASTER:
22962     case PRAGMA_OMP_ORDERED:
22963     case PRAGMA_OMP_PARALLEL:
22964     case PRAGMA_OMP_SECTIONS:
22965     case PRAGMA_OMP_SINGLE:
22966     case PRAGMA_OMP_TASK:
22967       if (context == pragma_external)
22968         goto bad_stmt;
22969       cp_parser_omp_construct (parser, pragma_tok);
22970       return true;
22971
22972     case PRAGMA_OMP_SECTION:
22973       error_at (pragma_tok->location, 
22974                 "%<#pragma omp section%> may only be used in "
22975                 "%<#pragma omp sections%> construct");
22976       break;
22977
22978     default:
22979       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22980       c_invoke_pragma_handler (id);
22981       break;
22982
22983     bad_stmt:
22984       cp_parser_error (parser, "expected declaration specifiers");
22985       break;
22986     }
22987
22988   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22989   return false;
22990 }
22991
22992 /* The interface the pragma parsers have to the lexer.  */
22993
22994 enum cpp_ttype
22995 pragma_lex (tree *value)
22996 {
22997   cp_token *tok;
22998   enum cpp_ttype ret;
22999
23000   tok = cp_lexer_peek_token (the_parser->lexer);
23001
23002   ret = tok->type;
23003   *value = tok->u.value;
23004
23005   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23006     ret = CPP_EOF;
23007   else if (ret == CPP_STRING)
23008     *value = cp_parser_string_literal (the_parser, false, false);
23009   else
23010     {
23011       cp_lexer_consume_token (the_parser->lexer);
23012       if (ret == CPP_KEYWORD)
23013         ret = CPP_NAME;
23014     }
23015
23016   return ret;
23017 }
23018
23019 \f
23020 /* External interface.  */
23021
23022 /* Parse one entire translation unit.  */
23023
23024 void
23025 c_parse_file (void)
23026 {
23027   bool error_occurred;
23028   static bool already_called = false;
23029
23030   if (already_called)
23031     {
23032       sorry ("inter-module optimizations not implemented for C++");
23033       return;
23034     }
23035   already_called = true;
23036
23037   the_parser = cp_parser_new ();
23038   push_deferring_access_checks (flag_access_control
23039                                 ? dk_no_deferred : dk_no_check);
23040   error_occurred = cp_parser_translation_unit (the_parser);
23041   the_parser = NULL;
23042 }
23043
23044 #include "gt-cp-parser.h"